最后活跃于 1759958159

bin_shift_test.py 原始文件
1import 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.
8start_exponent = 0
9stop_exponent = 3
10num_bins = 10
11x = np.logspace(start_exponent, stop_exponent, num=num_bins, base=10.0)
12
13# Check the binning of the array on a logarithmic scale.
14logged_x = np.log(x)
15print("1 - Bin width on a logarithmic scale before shift, ", np.diff(logged_x))
16
17# Shift the array by half of the 'bin'.
18xdiff = np.append(np.diff(x), x[-1] - x[-2])
19x -= xdiff / 2
20
21# Check the binning of the array on a logarithmic scale, after the shift.
22print("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.
29start_exponent = 0
30stop_exponent = 3
31num_bins = 10
32x = np.logspace(start_exponent, stop_exponent, num=num_bins, base=10.0)
33
34# Check the binning of the array on a logarithmic scale.
35logged_x = np.log(x)
36print("2 - Bin width on a logarithmic scale before shift, ", np.diff(logged_x))
37
38# Shift the array by half of the 'bin'.
39x = np.log(x)
40bin_size = x[-1] - x[-2]
41x = np.exp(x - bin_size / 2.)
42
43# Check the binning of the array on a logarithmic scale, after the shift.
44logged_x = np.log(x)
45print("2 - Bin width on a logarithmic scale after shift, ", np.diff(logged_x))