def test_adding_empty(self): ha1 = h1(None, "fixed_width", 10, adaptive=True) ha1.fill_n(np.random.normal(100, 10, 1000)) ha2 = h1(None, "fixed_width", 10, adaptive=True) ha3 = ha1 + ha2 assert ha1 == ha3 ha4 = ha2 + ha1 assert ha1 == ha4
def test_adding_full(self): ha1 = h1(None, "fixed_width", 10, adaptive=True) ha1.fill_n([1, 43, 23]) ha2 = h1(None, "fixed_width", 10, adaptive=True) ha2.fill_n([23, 51]) ha3 = ha1 + ha2 ha4 = ha2 + ha1 assert np.array_equal(ha3.frequencies, [1, 0, 2, 0, 1, 1]) assert np.array_equal(ha3.numpy_bins, [0, 10, 20, 30, 40, 50, 60]) assert ha4 == ha3
def test_update(self): example = h1(values) example.dtype = np.int16 assert example.dtype == np.int16 assert example.frequencies.dtype == np.int16 example = h1(values, weights=[1, 2, 2.1, 3.2]) with pytest.raises(RuntimeError): example.dtype = np.int16 example = h1(values, weights=[1, 2, 2, 3]) example.dtype = np.int16 assert example.dtype == np.int16
def test_fill_empty(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill(24) assert h.bin_count == 1 assert np.array_equal(h.bin_left_edges, [20]) assert np.array_equal(h.bin_right_edges, [30]) assert np.array_equal(h.frequencies, [1])
def test_non_empty(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill_n([4, 5, 11, 12]) h.fill_n([-13, 120]) assert h.bin_left_edges[0] == -20 assert h.bin_left_edges[-1] == 120 assert h.bin_count == 15
def test_coerce(self): example = h1(values, dtype=np.int32) example._coerce_dtype(np.int64) assert example.dtype == np.int64 example._coerce_dtype(np.float) assert example.dtype == np.float example._coerce_dtype(np.int32) assert example.dtype == np.float
def stair_histogram(path, width=1920, height=1280, bar=128, jitter=.1, **kwargs): histogram = h1(None, "fixed_width", bar, range=(0, width)) data = np.random.normal(width, width, int(1000 / jitter)) histogram.fill_n(data) dpi = 100 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi)) histogram.plot("fill", ax=ax, color="red", **kwargs) ax.set_axis_off() save_only_axis(ax, path, dpi=dpi)
def test_scalar_arithmetic(self): example = h1(values, dtype=np.int32) assert (example / 3).dtype == np.float assert (example * 3).dtype == np.int32 assert (example * 3.1).dtype == np.float with pytest.raises(TypeError): example * complex(4, 5)
def test_create_empty(self): h = h1(None, "fixed_width", 10, adaptive=True) assert h.bin_count == 0 assert h.total == 0 assert np.allclose(h.bin_widths, 10) assert np.isnan(h.mean()) assert np.isnan(h.std()) assert h.overflow == 0 assert h.underflow == 0
def create_h1(cursor, *args, **kwargs): axis_names = _get_axis_names(cursor) if len(axis_names) != 1: raise RuntimeError("Invalid number of columns: {0}".format(len(axis_names))) kwargs["axis_name"] = kwargs.get("axis_name", axis_names[0]) if kwargs.get("adaptive", False): h = h1(None, *args, **kwargs) for row in cursor: h << row[0] return h else: raise NotImplementedError()
def compute_pdfs(fname, d_arr): data = np.load(fname % ('data', 'y'), mmap_mode='r') pdfs = np.zeros((d_arr.size, 3, 2, 100)) for i in range(d_arr.size): d = d_arr[i] ix = data[:,d:,...,0] - data[:,:-d,...,0] iy = data[:,:,d:,:,1] - data[:,:,:-d,:,1] iz = data[:,...,d:,2] - data[:,...,:-d,2] for j, i_arr in enumerate([ix, iy, iz]): tmp = i_arr / i_arr.std() h = h1(tmp[np.abs(tmp)<20], bins=100) h.normalize(True) pdfs[i,j,0] = h.bin_centers pdfs[i,j,1] = h.densities np.save(fname % ('pdfs', 'y'), pdfs)
def compute_pdfs(samples, name): fname = name % ('pdfs', 'y') if os.path.isfile(fname): pdfs = np.load(fname) else: displ = np.array([2, 4, 8, 16, 32, 64]) pdfs = np.zeros((displ.size, 2, 100)) for i in range(displ.size): incr = samples[:, displ[i]:] - samples[:, :-displ[i]] incr /= incr.std() h = h1(incr[np.abs(incr) < 20], bins=100) h.normalize(True) pdfs[i, 0] = h.bin_centers pdfs[i, 1] = h.densities np.save(fname, pdfs) return pdfs
def test_hist_arithmetic(self): example = h1(values, dtype=np.int32) example2 = example.copy() example2.dtype = np.float example2 *= 1.01 example3 = example.copy() example3.dtype = np.int64 assert (example + example2).dtype == np.float assert (example2 + example).dtype == np.float assert (example + example3).dtype == np.int64 assert (example3 - example).dtype == np.int64 example += example2 assert example.dtype == np.float
def test_fill_non_empty(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill(4) h.fill(-14) assert h.bin_count == 3 assert h.total == 2 assert np.array_equal(h.bin_left_edges, [-20, -10, 0]) assert np.array_equal(h.bin_right_edges, [-10, 0, 10]) assert np.array_equal(h.frequencies, [1, 0, 1]) h.fill(-14) assert h.bin_count == 3 assert h.total == 3 h.fill(14) assert h.bin_count == 4 assert h.total == 4 assert np.array_equal(h.frequencies, [2, 0, 1, 1])
def compute_stats(fname): data = np.load(fname % ('data', 'y'), mmap_mode='r') num = data.shape[1] d_arr = np.arange(1, num-1, 1) # p_arr = np.arange(1, 13) p_arr = np.array([1, 2]) # energy spectrum E = np.zeros((num//2-1, 3)) f = np.fft.fftfreq(num, 2./num)[1:num//2] for i in range(3): reshaped_data = np.reshape(np.moveaxis(data, 1+i, 0), (num, -1)) cov = np.mean(np.cov(reshaped_data, bias=True), axis=-1) E[:,i] = np.abs(np.fft.fft(cov)[1:num//2]) np.savez(fname % ('energy', 'z'), f=f, E=E) del f; del E # increment moments and pdf moments = np.zeros((d_arr.size, 3, p_arr.size)) pdfs = np.zeros((d_arr.size, 100, 2, 3)) for i in range(d_arr.size): d = d_arr[i] ix = data[:,d:,...,0] - data[:,:-d,...,0] iy = data[:,:,d:,:,1] - data[:,:,:-d,:,1] iz = data[:,...,d:,2] - data[:,...,:-d,2] for j, i_arr in enumerate([ix, iy, iz]): h = h1(i_arr/np.expand_dims(i_arr.std(axis=1+j), 1+j), bins=100) h.normalize(True) pdfs[i,:,0,j] = h.bin_centers pdfs[i,:,1,j] = h.frequencies for k in range(p_arr.size): moments[i,j,k] = np.mean(np.mean(np.abs(i_arr)**p_arr[k], axis=1+j)) np.savez(fname % ('moments', 'z'), d=d_arr, m=moments, p=pdfs) del d_arr; del moments; del pdfs
def test_empty_exact(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill_n([10]) assert np.array_equal(h.bin_left_edges, [10]) assert np.array_equal(h.frequencies, [1]) assert h.total == 1
def test_copy(self): example = h1(values, dtype=np.int32) assert example.dtype == np.int32 assert example.copy().dtype == np.int32
def test_with_weights(self): example = h1(values, weights=[1, 2, 2.1, 3.2]) assert example.dtype == np.float
def test_2(self): data = np.random.rand(100) hh = h1(data, 120) hha = h1(data, 60) hhb = hh.merge_bins(2, inplace=False) assert hha == hhb
def test_empty(self): example = h1(None, "fixed_width", 10, adaptive=True) assert example.dtype == np.int64
from physt import h1 import matplotlib.pyplot as plt # Create the sample heights = [ 160, 155, 156, 198, 177, 168, 191, 183, 184, 179, 178, 172, 173, 175, 172, 177, 176, 175, 174, 173, 174, 175, 177, 169, 168, 164, 175, 188, 178, 174, 173, 181, 185, 166, 162, 163, 171, 165, 180, 189, 166, 163, 172, 173, 174, 183, 184, 161, 162, 168, 169, 174, 176, 170, 169, 165 ] hist = h1(heights, 10) # <--- get the histogram data hist << 190 # <--- add a forgotten value hist.plot(show_values=True)
def test_mean_weights(self): hw = physt.h1(values, weights=weights) assert hw.mean() == 2.8
def test_mean_no_weights(self): h = physt.h1(values) assert h.mean() == 2.5
def test_empty(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill_n([4, 5, 11, 12]) assert np.array_equal(h.bin_left_edges, [0, 10]) assert h.total == 4 assert h.mean() == 8.0
def test_empty_right_edge(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill_n([4, 4, 10]) assert np.array_equal(h.bin_left_edges, [0, 10]) assert h.total == 3 assert h.mean() == 6.0
def test_with_weights(self): h = h1(None, "fixed_width", 10, adaptive=True) h.fill_n([4, 5, 6, 12], [1, 1, 2, 3]) assert np.array_equal(h.frequencies, [4, 3]) assert np.array_equal(h.errors2, [6, 9]) assert np.array_equal(h.numpy_bins, [0, 10, 20])
def test_with_incorrect_weights(self): h = h1(None, "fixed_width", 10, adaptive=True) with pytest.raises(RuntimeError): h.fill_n([0, 1], [2, 3, 4]) with pytest.raises(RuntimeError): h.fill_n([0, 1, 2, 3], [2, 3, 4])
def make_big_h1(data): # 0.3.26 The following creates 2 additional copies!!! # 0.3.25 The following creates 5 additional copies!!! h = physt.h1(data)
def test_simple(self): example = h1(values) assert example.dtype == np.int64
def test_explicit(self): example = h1(values, dtype=float) assert example.dtype == float with pytest.raises(RuntimeError): example = h1(values, weights=[1, 2, 2.1, 3.2], dtype=int)
def test_multiplication(self): ha1 = h1(None, "fixed_width", 10, adaptive=True) ha1.fill_n([1, 43, 23]) ha1 *= 2 ha1.fill_n([-2]) assert np.array_equal(ha1.frequencies, [1, 2, 0, 2, 0, 2])
def test_stats_filled_in(self): h = physt.h1(values) assert h._stats["sum"] == 10 assert h._stats["sum2"] == 30
def test_std_no_weights(self): h = physt.h1(values) assert np.allclose(h.std(), np.sqrt(5/4))
def test_std_no_weights(self): h = physt.h1(values) assert np.allclose(h.std(), np.sqrt(5 / 4))
def test_std_weights(self): hw = physt.h1(values, weights=weights) assert np.allclose(hw.std(), np.sqrt(6.8 / 5))