bin_shift_test.py
· 1.5 KiB · Python
Raw
import numpy as np
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# First, using the method from Brian and Bob.
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Generate a dummy array with a constant binning on a logarithmic scale.
start_exponent = 0
stop_exponent = 3
num_bins = 10
x = np.logspace(start_exponent, stop_exponent, num=num_bins, base=10.0)
# Check the binning of the array on a logarithmic scale.
logged_x = np.log(x)
print("1 - Bin width on a logarithmic scale before shift, ", np.diff(logged_x))
# Shift the array by half of the 'bin'.
xdiff = np.append(np.diff(x), x[-1] - x[-2])
x -= xdiff / 2
# Check the binning of the array on a logarithmic scale, after the shift.
print("1 - Bin width on a logarithmic scale after shift, ", np.diff(x))
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Second, using the method from Yuanpeng
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Generate a dummy array with a constant binning on a logarithmic scale.
start_exponent = 0
stop_exponent = 3
num_bins = 10
x = np.logspace(start_exponent, stop_exponent, num=num_bins, base=10.0)
# Check the binning of the array on a logarithmic scale.
logged_x = np.log(x)
print("2 - Bin width on a logarithmic scale before shift, ", np.diff(logged_x))
# Shift the array by half of the 'bin'.
x = np.log(x)
bin_size = x[-1] - x[-2]
x = np.exp(x - bin_size / 2.)
# Check the binning of the array on a logarithmic scale, after the shift.
logged_x = np.log(x)
print("2 - Bin width on a logarithmic scale after shift, ", np.diff(logged_x))
| 1 | import numpy as np |
| 2 | |
| 3 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 4 | # First, using the method from Brian and Bob. |
| 5 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 6 | |
| 7 | # Generate a dummy array with a constant binning on a logarithmic scale. |
| 8 | start_exponent = 0 |
| 9 | stop_exponent = 3 |
| 10 | num_bins = 10 |
| 11 | x = np.logspace(start_exponent, stop_exponent, num=num_bins, base=10.0) |
| 12 | |
| 13 | # Check the binning of the array on a logarithmic scale. |
| 14 | logged_x = np.log(x) |
| 15 | print("1 - Bin width on a logarithmic scale before shift, ", np.diff(logged_x)) |
| 16 | |
| 17 | # Shift the array by half of the 'bin'. |
| 18 | xdiff = np.append(np.diff(x), x[-1] - x[-2]) |
| 19 | x -= xdiff / 2 |
| 20 | |
| 21 | # Check the binning of the array on a logarithmic scale, after the shift. |
| 22 | print("1 - Bin width on a logarithmic scale after shift, ", np.diff(x)) |
| 23 | |
| 24 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 25 | # Second, using the method from Yuanpeng |
| 26 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 27 | |
| 28 | # Generate a dummy array with a constant binning on a logarithmic scale. |
| 29 | start_exponent = 0 |
| 30 | stop_exponent = 3 |
| 31 | num_bins = 10 |
| 32 | x = np.logspace(start_exponent, stop_exponent, num=num_bins, base=10.0) |
| 33 | |
| 34 | # Check the binning of the array on a logarithmic scale. |
| 35 | logged_x = np.log(x) |
| 36 | print("2 - Bin width on a logarithmic scale before shift, ", np.diff(logged_x)) |
| 37 | |
| 38 | # Shift the array by half of the 'bin'. |
| 39 | x = np.log(x) |
| 40 | bin_size = x[-1] - x[-2] |
| 41 | x = np.exp(x - bin_size / 2.) |
| 42 | |
| 43 | # Check the binning of the array on a logarithmic scale, after the shift. |
| 44 | logged_x = np.log(x) |
| 45 | print("2 - Bin width on a logarithmic scale after shift, ", np.diff(logged_x)) |