Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from scikit-hep/issue-27
Browse files Browse the repository at this point in the history
odds and ends over the break
  • Loading branch information
jpivarski authored Jan 4, 2019
2 parents b5a462b + 0e19d23 commit 3d26831
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 43 deletions.
29 changes: 15 additions & 14 deletions uproot_methods/classes/TH1.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,28 @@ def allbins(self):

@property
def values(self):
return numpy.array(self[1:-1])
return self.allvalues[1:-1]

@property
def allvalues(self):
return numpy.array(self)
return numpy.array(self, dtype=getattr(self, "_dtype", numpy.dtype(numpy.float64)).newbyteorder("="))

@property
def variances(self):
return numpy.array(self._fSumw2[1:-1])
return self.allvariances[1:-1]

@property
def allvariances(self):
return numpy.array(self._fSumw2)
if len(getattr(self, "_fSumw2", [])) != len(self):
return numpy.array(self, dtype=numpy.float64)
else:
return numpy.array(self._fSumw2, dtype=numpy.float64)

def numpy(self):
return self.values, self.edges

def allnumpy(self):
return self.allvalues, self.alledges

def interval(self, index):
if index < 0:
Expand Down Expand Up @@ -221,17 +230,9 @@ def show(self, width=80, minimum=None, maximum=None, stream=sys.stdout):
stream.write(out)
stream.write("\n")

def numpy(self):
freq = numpy.array(self.values, dtype=self._dtype.newbyteorder("="))
if getattr(self._fXaxis, "_fXbins", None):
edges = numpy.array(self._fXaxis._fXbins)
else:
edges = numpy.linspace(self._fXaxis._fXmin, self._fXaxis._fXmax, self._fXaxis._fNbins + 1)
return freq, edges

def pandas(self, underflow=True, overflow=True, variance=True):
import pandas
freq = numpy.array(self.allvalues, dtype=self._dtype.newbyteorder("="))
freq = numpy.array(self.allvalues, dtype=getattr(self, "_dtype", numpy.dtype(numpy.float64)).newbyteorder("="))

if not underflow and not overflow:
freq = freq[1:-1]
Expand Down Expand Up @@ -288,7 +289,7 @@ def pandas(self, underflow=True, overflow=True, variance=True):
def physt(self):
import physt.binnings
import physt.histogram1d
freq = numpy.array(self.allvalues, dtype=self._dtype.newbyteorder("="))
freq = numpy.array(self.allvalues, dtype=getattr(self, "_dtype", numpy.dtype(numpy.float64)).newbyteorder("="))
if getattr(self._fXaxis, "_fXbins", None):
binning = physt.binnings.NumpyBinning(numpy.array(self._fXaxis._fXbins))
else:
Expand Down
93 changes: 68 additions & 25 deletions uproot_methods/classes/TH2.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,40 +103,83 @@ def xoverflows(self):
def yoverflows(self):
return self.overflows[1]

@property
def edges(self):
xaxis = self._fXaxis
yaxis = self._fYaxis
if len(getattr(xaxis, "_fXbins", [])) > 0:
xedges = numpy.array(xaxis._fXbins)
else:
xedges = numpy.linspace(xaxis._fXmin, xaxis._fXmax, xaxis._fNbins + 1)
if len(getattr(yaxis, "_fYbins", [])) > 0:
yedges = numpy.array(yaxis._fXbins)
else:
yedges = numpy.linspace(yaxis._fXmin, yaxis._fXmax, yaxis._fNbins + 1)
return xedges, yedges

@property
def alledges(self):
xedges, yedges = self.edges
vx = numpy.empty(len(xedges) + 2)
vy = numpy.empty(len(yedges) + 2)
vx[0] = -numpy.inf
vx[-1] = numpy.inf
vx[1:-1] = xedges
vy[0] = -numpy.inf
vy[-1] = numpy.inf
vy[1:-1] = yedges
return vx, vy

@property
def bins(self):
xedges, yedges = self.edges
vx = numpy.empty((len(xedges) - 1, 2))
vy = numpy.empty((len(yedges) - 1, 2))
vx[:, 0] = xedges[:-1]
vx[:, 1] = xedges[1:]
vy[:, 0] = yedges[:-1]
vy[:, 1] = yedges[1:]
return vx, vy

@property
def allbins(self):
xedges, yedges = self.alledges
vx = numpy.empty((len(xedges) - 1, 2))
vy = numpy.empty((len(yedges) - 1, 2))
vx[:, 0] = xedges[:-1]
vx[:, 1] = xedges[1:]
vy[:, 0] = yedges[:-1]
vy[:, 1] = yedges[1:]
return vx, vy

@property
def values(self):
va = numpy.array(self.allvalues)
va = va[1:self.ynumbins+1, 1:self.xnumbins+1]
return va.tolist()
va = self.allvalues
return va[1:self.ynumbins+1, 1:self.xnumbins+1]

@property
def allvalues(self):
v = numpy.array(self[:])
v = v.reshape(self.ynumbins + 2, self.xnumbins + 2)
return v.tolist()
v = numpy.array(self[:], dtype=getattr(self, "_dtype", numpy.dtype(numpy.float64)).newbyteorder("="))
return v.reshape(self.ynumbins + 2, self.xnumbins + 2)

def numpy(self):
xlow = self.xlow
xhigh = self.xhigh
xbins = self._fXaxis._fXbins
if not xbins:
norm = float(xhigh - xlow) / self.xnumbins
xedges = numpy.array([i*norm + xlow for i in range(self.xnumbins + 1)])
else:
xedges = numpy.array(xbins)

ylow = self.ylow
yhigh = self.yhigh
ybins = self._fYaxis._fXbins
if not ybins:
norm = (yhigh - ylow) / self.ynumbins
yedges = numpy.array([i*norm + ylow for i in range(self.ynumbins + 1)])
@property
def variances(self):
va = self.allvariances
return va[1:self.ynumbins+1, 1:self.xnumbins+1]

@property
def allvariances(self):
if len(getattr(self, "_fSumw2", [])) != len(self):
v = numpy.array(self, dtype=numpy.float64)
else:
yedges = numpy.array(ybins)
v = numpy.array(self._fSumw2, dtype=numpy.float64)
return v.reshape(self.ynumbins + 2, self.xnumbins + 2)

freq = numpy.array(self.values, dtype=self._dtype.newbyteorder("="))
def numpy(self):
return (self.values,) + self.edges

return freq, (xedges, yedges)
def numpy(self):
return (self.allvalues,) + self.alledges

def interval(self, index, axis):
if axis == "x":
Expand Down
3 changes: 3 additions & 0 deletions uproot_methods/classes/TLorentzVector.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def sum(self):
return TLorentzVector(self.x.sum(), self.y.sum(), self.z.sum(), self.t.sum())

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
if "out" in kwargs:
raise NotImplementedError("in-place operations not supported")

if method != "__call__":
return NotImplemented

Expand Down
3 changes: 3 additions & 0 deletions uproot_methods/classes/TVector2.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def sum(self):
return TVector2(self.x.sum(), self.y.sum())

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
if "out" in kwargs:
raise NotImplementedError("in-place operations not supported")

if method != "__call__":
return NotImplemented

Expand Down
9 changes: 6 additions & 3 deletions uproot_methods/classes/TVector3.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def _rotate_euler(self, phi, theta, psi):
return x, y, z

def rotatex(self, angle):
return self.rotate_axis(Methods(1.0, 0.0, 0.0), angle)
return self.rotate_axis(TVector3(1.0, 0.0, 0.0), angle)

def rotatey(self, angle):
return self.rotate_axis(Methods(0.0, 1.0, 0.0), angle)
return self.rotate_axis(TVector3(0.0, 1.0, 0.0), angle)

def rotatez(self, angle):
return self.rotate_axis(Methods(0.0, 0.0, 1.0), angle)
return self.rotate_axis(TVector3(0.0, 0.0, 1.0), angle)

class ArrayMethods(Common, uproot_methods.common.TVector.ArrayMethods, uproot_methods.base.ROOTMethods):
def _initObjectArray(self, table):
Expand Down Expand Up @@ -157,6 +157,9 @@ def sum(self):
return TVector3(self.x.sum(), self.y.sum(), self.z.sum())

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
if "out" in kwargs:
raise NotImplementedError("in-place operations not supported")

if method != "__call__":
return NotImplemented

Expand Down
2 changes: 1 addition & 1 deletion uproot_methods/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import re

__version__ = "0.3.1"
__version__ = "0.3.2"
version = __version__
version_info = tuple(re.split(r"[-\.]", __version__))

Expand Down

0 comments on commit 3d26831

Please sign in to comment.