def test_2(self): data1 = np.random.rand(100) data2 = np.random.rand(100) hh = h2(data1, data2, 120) hha = h2(data1, data2, 60) hhb = hh.merge_bins(2, inplace=False) assert hha == hhb
def test_add_cross_2d(self): d1 = np.asarray([1, 21, 3]) d2 = np.asarray([10, 12, 20]) h = h2(d1, d2, "fixed_width", 10, adaptive=True) hb = h2(d1, d2 + 10, "fixed_width", 10, adaptive=True) hc = h + hb assert np.array_equal(hc.numpy_bins[1], [10, 20, 30, 40])
def test_dropna(self): vals2 = np.array(vals) vals2[0, 1] = np.nan with pytest.raises(RuntimeError): hist = physt.h2(vals2[:,0], vals2[:,1], dropna=False) hist = physt.h2(vals2[:, 0], vals2[:, 1]) assert hist.frequencies.sum() == 6
def test_add_cross_2d(self): d1 = np.asarray([1, 21, 3]) d2 = np.asarray([10, 12, 20]) h = h2(d1, d2, "fixed_width", 10, adaptive=True) hb = h2(d1, d2 +10, "fixed_width", 10, adaptive=True) hc = h + hb assert np.array_equal(hc.numpy_bins[1], [10, 20, 30, 40])
def test_addition_with_adaptive(self): ha = h2([1], [11], "fixed_width", 10, adaptive=True) hb = h2([10], [5], "fixed_width", 10, adaptive=True) hha = ha + hb assert hha == hb + ha assert hha.shape == (2, 2) assert hha.total == 2 assert np.array_equal(hha.frequencies, [[0, 1], [1, 0]])
def test_divide_by_constant(self): xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2]) yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2]) h = physt.h2(xx, yy, "fixed_width", 1) i = h / 2 assert np.array_equal(i.frequencies, freqs / 2) assert np.array_equal(i.errors2, freqs / 4)
def vignette_histogram(path, width=1920, height=1280, grid=32, jitter=1, **kwargs): histogram = h2(None, None, "fixed_width", grid, range=((0, width), (0, height))) xs = [0, width, width, 0, width / 2] ys = [0, 0, height, height, height / 2] count = int((height / grid)**2 / jitter) counts = [count] * 4 + [count / 4] radii = [height / 4] * 5 for xs, ys in multicenter_data(xs, ys, counts, radii): fill_data = np.concatenate([xs[:, np.newaxis], ys[:, np.newaxis]], axis=1) histogram.fill_n(fill_data) dpi = 100 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi)) histogram.plot("image", ax=ax, show_colorbar=False, cmap="Greens", cmap_max=histogram.frequencies.max(), cmap_min=-0.2 * histogram.frequencies.max(), **kwargs) ax.set_axis_off() save_only_axis(ax, path, dpi=dpi)
def test_subtraction_with_another(self): xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2]) yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2]) h = physt.h2(xx, yy, "fixed_width", 1) i = h * 2 - h assert np.array_equal(i.frequencies, freqs) assert np.array_equal(i.errors2, 5 * freqs)
def test_fill_nonempty(self): d1 = [1, 21, 3] d2 = [10, 12, 20] h = h2(d1, d2, "fixed_width", 10, adaptive=True) h.fill([4, 60]) assert h.total == 4 assert h.shape == (3, 6)
def test_simple_random(self): x = np.random.normal(100, 1, 1000) y = np.random.normal(10, 10, 1000) h2 = physt.h2(x, y, [8, 4], name="Some histogram", axis_names=["x", "y"]) assert h2.frequencies.sum() == 1000 assert h2.shape == (8, 4) assert h2.name == "Some histogram" assert h2.axis_names == ("x", "y")
def create_h2(cursor, *args, **kwargs): axis_names = _get_axis_names(cursor) if len(axis_names) != 2: raise RuntimeError( "Invalid number of columns: {0}".format(len(axis_names))) kwargs["axis_names"] = kwargs.get("axis_names", axis_names) if kwargs.get("adaptive", False): h = h2(None, None, *args, **kwargs) for row in cursor: h << row return h else: raise NotImplementedError()
def test_multiply_by_constant(self): xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2]) yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2]) h = physt.h2(xx, yy, "fixed_width", 1) assert np.array_equal(h.frequencies, freqs) i = h * 2 assert np.array_equal(i.frequencies, freqs * 2) assert np.array_equal(i.errors2, freqs * 4) i = h * 0.5 assert np.array_equal(i.frequencies, freqs * 0.5) assert np.array_equal(i.errors2, freqs * 0.25)
def vignette_histogram(path, width=1920, height=1280, grid=32, jitter=1, **kwargs): histogram = h2(None, None, "fixed_width", grid, range=((0, width), (0, height))) xs = [0, width, width, 0, width / 2] ys = [0, 0, height, height, height / 2] count = int((height / grid) ** 2 / jitter) counts = [count] * 4 + [count / 4] radii = [height / 4] * 5 for xs, ys in multicenter_data(xs, ys, counts, radii): fill_data = np.concatenate([xs[:, np.newaxis], ys[:, np.newaxis]], axis=1) histogram.fill_n(fill_data) dpi = 100 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi)) histogram.plot("image", ax=ax, show_colorbar=False, cmap="Greens", cmap_max=histogram.frequencies.max(), cmap_min=-0.2 * histogram.frequencies.max(), **kwargs) ax.set_axis_off() save_only_axis(ax, path, dpi=dpi)
def test_create_empty(self): h = h2(None, None, "fixed_width", 10, adaptive=True) for b in h._binnings: assert b.is_adaptive() assert h.ndim == 2
def test_multiply_by_other(self): xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2]) yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2]) h = physt.h2(xx, yy, "fixed_width", 1) with pytest.raises(RuntimeError): h * h
def test_fill_empty(self): h = h2(None, None, "fixed_width", 10, adaptive=True) h.fill([4, 14]) assert h.total == 1 assert np.array_equal(h.numpy_bins, [[0, 10], [10, 20]])
def test_create_nonempty(self): d1 = [1, 21, 3] d2 = [11, 12, 13] h = h2(d1, d2, "fixed_width", 10, adaptive=True) assert h.shape == (3, 1)
def test_subtraction_by_constant(self): xx = np.array([0.5, 1.5, 2.5, 2.2, 3.3, 4.2]) yy = np.array([1.5, 1.5, 1.5, 2.2, 1.3, 1.2]) h = physt.h2(xx, yy, "fixed_width", 1) with pytest.raises(RuntimeError): h - 4
def test_create_empty_h2(self): h2(None, None, "integer", adaptive=True)
def test_simple(self): h = physt.h2(None, None, "integer", adaptive=True) h << (0, 1) json = h.to_json() read = io.parse_json(json) assert h == read
def test_json_write_2d(self): from physt import h2 values = np.random.rand(500, 2) h = h2(values[:, 0], values[:, 1], 3)
def test_json_write_2d(self): from physt import h2 values = np.random.rand(500, 2) h = h2(values[:,0], values[:,1], 3)