Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert( (jca1.offsets == jca2.offsets).all() )
addon1 = jca1.zeros_like()
addon2 = jca2.ones_like()
jca1['addon'] = addon1
jca2['addon'] = addon2
jca1.add_attributes(addonFlat=addon1.flatten(),addonJagged=addon1)
diffm = np.abs(jca1.p4.mass - jca2.p4.mass)
assert( (jca1.offsets == jca2.offsets).all() )
diffpt = np.abs(jca1.p4.pt - jca2.p4.pt)
assert( (jca1.offsets == jca2.offsets).all() )
eta2 = jca2.p4.eta
eta1 = jca1.p4.eta
print (np.sum(eta1.counts),np.sum(eta2.counts))
diffeta_temp = np.abs(eta1 - eta2)
diffeta = np.abs(jca1.p4.eta - jca2.p4.eta)
assert( (jca1.offsets == jca2.offsets).all() )
assert (diffm < 1e-8).flatten().all()
assert (diffpt < 1e-8).flatten().all()
assert (diffeta < 1e-8).flatten().all()
#test fast functions
fastfs = ['pt','eta','phi','mass']
for func in fastfs:
func1 = getattr(jca1,func)
func2 = getattr(jca1.p4,func)
dfunc = np.abs(func1 - func2)
assert (dfunc < 1e-8).flatten().all()
adistinct = jca1.distincts()
h_mascots_2.fill(animal="bison", vocalization="baa", height=baby_bison_h, mass=baby_bison_w, weight=baby_bison_cutefactor)
h_mascots_2.fill(animal="fox", vocalization="none", height=1., mass=30.)
h_mascots = h_mascots_1 + h_mascots_2
assert h_mascots.integrate("vocalization", "h*").sum("height", "mass", "animal").values()[()] == 1040.
species_class = hist.Cat("species_class", "where the subphylum is vertibrates")
classes = {
'birds': ['goose', 'crane'],
'mammals': ['bison', 'fox'],
}
h_species = h_mascots.group("animal", species_class, classes)
assert set(h_species.integrate("vocalization").values().keys()) == set([('birds',), ('mammals',)])
nbirds_bin = np.sum((goose_h>=0.5)&(goose_h<1)&(goose_w>10)&(goose_w<100))
nbirds_bin += np.sum((crane_h>=0.5)&(crane_h<1)&(crane_w>10)&(crane_w<100))
assert h_species.integrate("vocalization").values()[('birds',)][1,2] == nbirds_bin
tally = h_species.sum("mass", "height", "vocalization").values()
assert tally[('birds',)] == 1004.
assert tally[('mammals',)] == 91.
h_species.scale({"honk": 0.1, "huff": 0.9}, axis="vocalization")
h_species.scale(5.)
tally = h_species.sum("mass", height, vocalization).values(sumw2=True)
assert tally[('birds',)] == (520., 350.)
assert tally[('mammals',)] == (435., 25*(40*(0.9**2)+20*(2.5**2)+1))
assert h_species.axis("vocalization") is vocalization
assert h_species.axis("height") is height
assert h_species.integrate("vocalization", "h*").axis("height") is height
tall_class = hist.Cat("tall_class", "species class (species above 1m)")
def test_hist():
counts, test_eta, test_pt = dummy_jagged_eta_pt()
h_nothing = hist.Hist("empty inside")
assert h_nothing.sparse_dim() == h_nothing.dense_dim() == 0
assert h_nothing.values() == {}
h_regular_bins = hist.Hist("regular joe", hist.Bin("x", "x", 20, 0, 200), hist.Bin("y", "why", 20, -3, 3))
h_regular_bins.fill(x=test_pt, y=test_eta)
nentries = np.sum(counts)
assert h_regular_bins.sum("x", "y", overflow='all').values(sumw2=True)[()] == (nentries, nentries)
# bin x=2, y=10 (when overflow removed)
count_some_bin = np.sum((test_pt>=20.)&(test_pt<30.)&(test_eta>=0.)&(test_eta<0.3))
assert h_regular_bins.integrate("x", slice(20, 30)).values()[()][10] == count_some_bin
assert h_regular_bins.integrate("y", slice(0, 0.3)).values()[()][2] == count_some_bin
h_reduced = h_regular_bins[10:,-.6:]
# bin x=1, y=2
assert h_reduced.integrate("x", slice(20, 30)).values()[()][2] == count_some_bin
assert h_reduced.integrate("y", slice(0, 0.3)).values()[()][1] == count_some_bin
h_reduced.fill(x=23, y=0.1)
assert h_reduced.integrate("x", slice(20, 30)).values()[()][2] == count_some_bin + 1
assert h_reduced.integrate("y", slice(0, 0.3)).values()[()][1] == count_some_bin + 1
animal = hist.Cat("animal", "type of animal")
vocalization = hist.Cat("vocalization", "onomatopoiea is that how you spell it?")
h_cat_bins = hist.Hist("I like cats", animal, vocalization)
h_cat_bins.fill(animal="cat", vocalization="meow", weight=2.)
h_cat_bins.fill(animal="dog", vocalization="meow", weight=np.array([-1., -1., -5.]))
def dummy_jagged_eta_pt():
np.random.seed(42)
counts = np.random.exponential(2, size=50).astype(int)
entries = np.sum(counts)
test_eta = np.random.uniform(-3., 3., size=entries)
test_pt = np.random.exponential(10., size=entries)+np.random.exponential(10, size=entries)
return (counts, test_eta, test_pt)
def test_hist():
counts, test_eta, test_pt = dummy_jagged_eta_pt()
h_nothing = hist.Hist("empty inside")
assert h_nothing.sparse_dim() == h_nothing.dense_dim() == 0
assert h_nothing.values() == {}
h_regular_bins = hist.Hist("regular joe", hist.Bin("x", "x", 20, 0, 200), hist.Bin("y", "why", 20, -3, 3))
h_regular_bins.fill(x=test_pt, y=test_eta)
nentries = np.sum(counts)
assert h_regular_bins.sum("x", "y", overflow='all').values(sumw2=True)[()] == (nentries, nentries)
# bin x=2, y=10 (when overflow removed)
count_some_bin = np.sum((test_pt>=20.)&(test_pt<30.)&(test_eta>=0.)&(test_eta<0.3))
assert h_regular_bins.integrate("x", slice(20, 30)).values()[()][10] == count_some_bin
assert h_regular_bins.integrate("y", slice(0, 0.3)).values()[()][2] == count_some_bin
h_reduced = h_regular_bins[10:,-.6:]
# bin x=1, y=2
assert h_reduced.integrate("x", slice(20, 30)).values()[()][2] == count_some_bin
assert h_reduced.integrate("y", slice(0, 0.3)).values()[()][1] == count_some_bin
h_reduced.fill(x=23, y=0.1)
assert h_reduced.integrate("x", slice(20, 30)).values()[()][2] == count_some_bin + 1
assert h_reduced.integrate("y", slice(0, 0.3)).values()[()][1] == count_some_bin + 1
animal = hist.Cat("animal", "type of animal")
vocalization = hist.Cat("vocalization", "onomatopoiea is that how you spell it?")
yaxis = hist.axes()[0]
transpose = True
if isinstance(xaxis, SparseAxis) or isinstance(yaxis, SparseAxis):
raise NotImplementedError("Plot a sparse axis (e.g. bar chart or labeled bins)")
else:
xedges = xaxis.edges(overflow=xoverflow)
yedges = yaxis.edges(overflow=yoverflow)
sumw, sumw2 = hist.values(sumw2=True, overflow='allnan')[()]
if transpose:
sumw = sumw.T
sumw2 = sumw2.T
# no support for different overflow behavior per axis, do it ourselves
sumw = sumw[overflow_behavior(xoverflow), overflow_behavior(yoverflow)]
sumw2 = sumw2[overflow_behavior(xoverflow), overflow_behavior(yoverflow)]
if (density or binwnorm is not None) and np.sum(sumw) > 0:
overallnorm = np.sum(sumw) * binwnorm if binwnorm is not None else 1.
areas = np.multiply.outer(np.diff(xedges), np.diff(yedges))
binnorms = overallnorm / (areas * np.sum(sumw))
sumw = sumw * binnorms
sumw2 = sumw2 * binnorms**2
if patch_opts is not None:
opts = {'cmap': 'viridis'}
opts.update(patch_opts)
pc = ax.pcolormesh(xedges, yedges, sumw.T, **opts)
ax.add_collection(pc)
if clear:
fig.colorbar(pc, ax=ax, label=hist.label)
if text_opts is not None:
for ix, xcenter in enumerate(xaxis.centers()):
for iy, ycenter in enumerate(yaxis.centers()):
opts = {
elif error_opts is not None and line_opts is None:
histtype = 'errorbar'
kwargs = error_opts
else:
histtype = 'step'
kwargs = line_opts
if kwargs is None:
kwargs = {}
hep.histplot(plot_info['sumw'], edges, label=plot_info['label'],
yerr=_error, histtype=histtype, ax=ax,
density=density, binwnorm=binwnorm, stack=stack,
**kwargs)
if stack and error_opts is not None:
stack_sumw = np.sum(plot_info['sumw'], axis=0)
stack_sumw2 = np.sum(plot_info['sumw2'], axis=0)
err = poisson_interval(stack_sumw, stack_sumw2)
opts = {'step': 'post', 'label': 'Sum unc.', 'hatch': '///',
'facecolor': 'none', 'edgecolor': (0, 0, 0, .5), 'linewidth': 0}
opts.update(error_opts)
ax.fill_between(x=edges, y1=np.r_[err[0, :], err[0, -1]],
y2=np.r_[err[1, :], err[1, -1]], **opts)
if legend_opts is not None:
_label = overlay.label if overlay is not None else ""
ax.legend(title=_label, **legend_opts)
else:
ax.legend(title=_label)
ax.autoscale(axis='x', tight=True)
ax.set_ylim(0, None)
def dense_op(array):
if len(dense_sum_dim) > 0:
return np.sum(array[dense_slice], axis=dense_sum_dim)
return array