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

Commit

Permalink
fixed #30: TH2 methods are up-to-date with TH1
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Jan 4, 2019
1 parent 97a1957 commit 0e19d23
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 39 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

0 comments on commit 0e19d23

Please sign in to comment.