Exemple #1
0
    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
Exemple #2
0
    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
Exemple #3
0
    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
Exemple #4
0
    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
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #7
0
 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])
Exemple #8
0
 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])
Exemple #9
0
    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
Exemple #10
0
 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
Exemple #11
0
    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
Exemple #12
0
 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)
Exemple #14
0
    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 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)
Exemple #16
0
    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)
Exemple #17
0
 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
Exemple #18
0
 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
Exemple #19
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()
Exemple #20
0
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
Exemple #22
0
    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
Exemple #23
0
    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
Exemple #24
0
    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])
Exemple #25
0
    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])
Exemple #26
0
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
Exemple #27
0
 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
Exemple #28
0
 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
Exemple #29
0
 def test_copy(self):
     example = h1(values, dtype=np.int32)
     assert example.dtype == np.int32
     assert example.copy().dtype == np.int32
Exemple #30
0
 def test_with_weights(self):
     example = h1(values, weights=[1, 2, 2.1, 3.2])
     assert example.dtype == np.float
Exemple #31
0
 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
Exemple #32
0
 def test_empty(self):
     example = h1(None, "fixed_width", 10, adaptive=True)
     assert example.dtype == np.int64
Exemple #33
0
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)
Exemple #34
0
 def test_mean_weights(self):
     hw = physt.h1(values, weights=weights)
     assert hw.mean() == 2.8
Exemple #35
0
 def test_mean_no_weights(self):
     h = physt.h1(values)
     assert h.mean() == 2.5
Exemple #36
0
 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
Exemple #37
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
Exemple #38
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])
Exemple #39
0
 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])
Exemple #40
0
 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
Exemple #41
0
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)
Exemple #42
0
 def test_simple(self):
     example = h1(values)
     assert example.dtype == np.int64
Exemple #43
0
 def test_simple(self):
     example = h1(values)
     assert example.dtype == np.int64
Exemple #44
0
 def test_with_weights(self):
     example = h1(values, weights=[1, 2, 2.1, 3.2])
     assert example.dtype == np.float
Exemple #45
0
    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)
Exemple #46
0
    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)
Exemple #47
0
 def test_copy(self):
     example = h1(values, dtype=np.int32)
     assert example.dtype == np.int32
     assert example.copy().dtype == np.int32
Exemple #48
0
 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])
Exemple #49
0
 def test_stats_filled_in(self):
     h = physt.h1(values)
     assert h._stats["sum"] == 10
     assert h._stats["sum2"] == 30
Exemple #50
0
 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])
Exemple #51
0
 def test_empty(self):
     example = h1(None, "fixed_width", 10, adaptive=True)
     assert example.dtype == np.int64
Exemple #52
0
 def test_mean_no_weights(self):
     h = physt.h1(values)
     assert h.mean() == 2.5
Exemple #53
0
 def test_stats_filled_in(self):
     h = physt.h1(values)
     assert h._stats["sum"] == 10
     assert h._stats["sum2"] == 30
Exemple #54
0
 def test_std_no_weights(self):
     h = physt.h1(values)
     assert np.allclose(h.std(), np.sqrt(5/4))
Exemple #55
0
 def test_std_no_weights(self):
     h = physt.h1(values)
     assert np.allclose(h.std(), np.sqrt(5 / 4))
Exemple #56
0
 def test_mean_weights(self):
     hw = physt.h1(values, weights=weights)
     assert hw.mean() == 2.8
Exemple #57
0
 def test_std_weights(self):
     hw = physt.h1(values, weights=weights)
     assert np.allclose(hw.std(), np.sqrt(6.8 / 5))
Exemple #58
0
 def test_std_weights(self):
     hw = physt.h1(values, weights=weights)
     assert np.allclose(hw.std(), np.sqrt(6.8 / 5))