Skip to content

Commit 842324e

Browse files
authored
Merge pull request #18 from mbaak/master
All checks passed, so I'm approving it. Will need to back-port from master to 1.0.x, though.
2 parents af44951 + b2c0878 commit 842324e

File tree

4 files changed

+182
-44
lines changed

4 files changed

+182
-44
lines changed

histogrammar/plot/bokeh.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import math
2121

22+
# python 2/3 compatibility fixes
23+
from histogrammar.util import *
24+
2225
class HistogramMethods(object):
2326
def plotbokeh(self,glyphType="line",glyphSize=1,fillColor="red",lineColor="black",lineAlpha=1,fillAlpha=0.1,lineDash='solid'):
2427

histogrammar/plot/matplotlib.py

Lines changed: 170 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from __future__ import absolute_import
1818
import types
1919

20+
# python 2/3 compatibility fixes
21+
from histogrammar.util import *
2022

2123
def prepare2Dsparse(sparse):
2224
yminBins = [v.minBin for v in sparse.bins.values() if v.minBin is not None]
@@ -25,10 +27,10 @@ def prepare2Dsparse(sparse):
2527
yminBin = min(yminBins)
2628
ymaxBin = max(ymaxBins)
2729
else:
28-
yminBin = 0.0
29-
ymaxBin = 0.0
30+
yminBin = 0
31+
ymaxBin = 0
3032
sample = list(sparse.bins.values())[0]
31-
ynum = 1.0 + ymaxBin - yminBin
33+
ynum = 1 + ymaxBin - yminBin
3234
ylow = yminBin * sample.binWidth + sample.origin
3335
yhigh = (ymaxBin + 1.0) * sample.binWidth + sample.origin
3436
return yminBin, ymaxBin, ynum, ylow, yhigh
@@ -52,13 +54,11 @@ def plotmatplotlib(self, name=None, **kwargs):
5254
import numpy as np
5355
ax = plt.gca()
5456

55-
entries = [x.entries for x in self.values]
57+
edges = self.bin_edges()
58+
entries = self.bin_entries()
59+
width = self.bin_width()
5660

57-
num_bins = len(self.values)
58-
width = (self.high - self.low)/num_bins
59-
edges = np.linspace(self.low, self.high, num_bins + 1)[:-1]
60-
61-
ax.bar(edges, entries, width=width, **kwargs)
61+
ax.bar(edges[:-1], entries, width=width, **kwargs)
6262

6363
if name is not None:
6464
ax.set_title(name)
@@ -67,6 +67,42 @@ def plotmatplotlib(self, name=None, **kwargs):
6767

6868
return ax
6969

70+
def num_bins(self):
71+
"""
72+
Returns number of bins
73+
"""
74+
return len(self.values)
75+
76+
def bin_width(self):
77+
"""
78+
Returns bin width
79+
"""
80+
return (self.high - self.low) / self.num_bins()
81+
82+
def bin_entries(self):
83+
"""
84+
Returns bin values
85+
"""
86+
import numpy as np
87+
return np.array([x.entries for x in self.values])
88+
89+
def bin_edges(self):
90+
"""
91+
Returns bin edges
92+
"""
93+
import numpy as np
94+
num_bins = self.num_bins()
95+
edges = np.linspace(self.low, self.high, num_bins + 1)
96+
return edges
97+
98+
def bin_centers(self):
99+
"""
100+
Returns bin centers
101+
"""
102+
import numpy as np
103+
return np.array([sum(self.range(i))/2.0 for i in self.indexes])
104+
105+
70106
class SparselyHistogramMethods(object):
71107
def plotmatplotlib(self, name=None, **kwargs):
72108
"""
@@ -79,21 +115,72 @@ def plotmatplotlib(self, name=None, **kwargs):
79115
import numpy as np
80116
ax = plt.gca()
81117

82-
if self.minBin is None or self.maxBin is None:
83-
ax.bar([self.origin, self.origin + 1], self.bins[0].entries, width=self.binWidth, **kwargs)
84-
else:
85-
size = 1 + self.maxBin - self.minBin
86-
entries = [self.bins[i].entries if i in self.bins else 0.0 for i in xrange(self.minBin, self.maxBin + 1)]
87-
edges = np.linspace(self.minBin, self.maxBin, len(entries) + 1)[:-1]
88-
ax.bar(edges, entries, width=self.binWidth, **kwargs)
118+
edges = self.bin_edges()
119+
entries = self.bin_entries()
120+
width = self.bin_width()
89121

122+
ax.bar(edges[:-1], entries, width=width, **kwargs)
123+
ax.set_xlim(self.low,self.high)
124+
90125
if name is not None:
91126
ax.set_title(name)
92127
else:
93128
ax.set_title(self.name)
94129

95130
return ax
96131

132+
def num_bins(self):
133+
"""
134+
Returns number of bins
135+
"""
136+
if self.minBin is None or self.maxBin is None:
137+
return 0
138+
nbins = self.maxBin - self.minBin + 1
139+
return nbins
140+
141+
def bin_width(self):
142+
"""
143+
Returns bin width
144+
"""
145+
return self.binWidth
146+
147+
def bin_edges(self):
148+
"""
149+
Returns bin_edges
150+
"""
151+
import numpy as np
152+
153+
if self.minBin is None or self.maxBin is None:
154+
edges = np.array([self.origin, self.origin + 1])
155+
else:
156+
num_bins = self.maxBin - self.minBin + 1
157+
edges = np.linspace(self.low, self.high, num_bins + 1)
158+
return edges
159+
160+
def bin_entries(self):
161+
"""
162+
Returns bin values
163+
"""
164+
import numpy as np
165+
166+
if self.minBin is None or self.maxBin is None:
167+
entries = [self.bins[0].entries]
168+
else:
169+
entries = [self.bins[i].entries if i in self.bins else 0.0 \
170+
for i in range(self.minBin, self.maxBin + 1)]
171+
return np.array(entries)
172+
173+
def bin_centers(self):
174+
"""
175+
Returns bin centers
176+
"""
177+
import numpy as np
178+
179+
bin_edges = self.bin_edges()
180+
centers = [(bin_edges[i]+bin_edges[i+1])/2. for i in range(len(bin_edges)-1)]
181+
return np.array(centers)
182+
183+
97184
class ProfileMethods(object):
98185
def plotmatplotlib(self, name=None, **kwargs):
99186
""" Plotting method for Bin of Average
@@ -242,7 +329,7 @@ def plotmatplotlib(self, name=None, **kwargs):
242329
import numpy as np
243330
ax = plt.gca()
244331
color_cycle = plt.rcParams['axes.color_cycle']
245-
if kwargs.has_key("color"):
332+
if "color" in kwargs:
246333
kwargs.pop("color")
247334

248335
for i, hist in enumerate(self.values):
@@ -264,7 +351,7 @@ def plotmatplotlib(self, name=None, **kwargs):
264351
import numpy as np
265352
ax = plt.gca()
266353
color_cycle = plt.rcParams['axes.color_cycle']
267-
if kwargs.has_key("color"):
354+
if "color" in kwargs:
268355
kwargs.pop("color")
269356

270357
for i, hist in enumerate(self.values):
@@ -330,15 +417,9 @@ def plotmatplotlib(self, name=None, **kwargs):
330417
import numpy as np
331418
ax = plt.gca()
332419

333-
samp = self.values[0]
334-
x_ranges = np.unique(np.array([self.range(i) for i in self.indexes]).flatten())
335-
y_ranges = np.unique(np.array([samp.range(i) for i in samp.indexes]).flatten())
336-
337-
grid = np.zeros((samp.num, self.num))
338-
339-
for j in xrange(self.num):
340-
for i in xrange(samp.num):
341-
grid[i,j] = self.values[j].values[i].entries
420+
x_ranges, y_ranges, grid = self.xy_ranges_grid()
421+
ax.set_ylim(self.y_lim())
422+
ax.set_xlim(self.x_lim())
342423

343424
ax.pcolormesh(x_ranges, y_ranges, grid, **kwargs)
344425

@@ -348,6 +429,34 @@ def plotmatplotlib(self, name=None, **kwargs):
348429
ax.set_title(self.name)
349430
return ax
350431

432+
def xy_ranges_grid(self):
433+
""" Return x and y ranges and x,y grid
434+
"""
435+
import numpy as np
436+
437+
samp = self.values[0]
438+
x_ranges = np.unique(np.array([self.range(i) for i in self.indexes]).flatten())
439+
y_ranges = np.unique(np.array([samp.range(i) for i in samp.indexes]).flatten())
440+
441+
grid = np.zeros((samp.num, self.num))
442+
443+
for j in range(self.num):
444+
for i in range(samp.num):
445+
grid[i,j] = self.values[j].values[i].entries
446+
447+
return x_ranges, y_ranges, grid
448+
449+
def x_lim(self):
450+
""" return x low high tuble
451+
"""
452+
return (self.low,self.high)
453+
454+
def y_lim(self):
455+
""" return y low high tuble
456+
"""
457+
samp = self.values[0]
458+
return (samp.low,samp.high)
459+
351460

352461
class SparselyTwoDimensionallyHistogramMethods(object):
353462
def plotmatplotlib(self, name=None, **kwargs):
@@ -361,6 +470,24 @@ def plotmatplotlib(self, name=None, **kwargs):
361470
import numpy as np
362471
ax = plt.gca()
363472

473+
x_ranges, y_ranges, grid = self.xy_ranges_grid()
474+
475+
ax.pcolormesh(x_ranges, y_ranges, grid, **kwargs)
476+
ax.set_ylim(self.y_lim())
477+
ax.set_xlim(self.x_lim())
478+
479+
if name is not None:
480+
ax.set_title(name)
481+
else:
482+
ax.set_title(self.name)
483+
484+
return ax
485+
486+
def xy_ranges_grid(self):
487+
""" Return x and y ranges and x,y grid
488+
"""
489+
import numpy as np
490+
364491
yminBin, ymaxBin, ynum, ylow, yhigh = prepare2Dsparse(self)
365492

366493
xbinWidth = self.binWidth
@@ -377,17 +504,20 @@ def plotmatplotlib(self, name=None, **kwargs):
377504
x_ranges = np.arange(xlow, xhigh + xbinWidth, xbinWidth)
378505
y_ranges = np.arange(ylow, yhigh + ybinWidth, ybinWidth)
379506

380-
ax.pcolormesh(x_ranges, y_ranges, grid, **kwargs)
381-
ax.set_ylim((ylow, yhigh))
382-
ax.set_xlim((xlow, xhigh))
383-
384-
if name is not None:
385-
ax.set_title(name)
386-
else:
387-
ax.set_title(self.name)
388-
389-
return ax
390-
391-
392-
507+
return x_ranges, y_ranges, grid
393508

509+
def x_lim(self):
510+
""" return x low high tuble
511+
"""
512+
xmaxBin = max(self.bins.keys())
513+
xminBin = min(self.bins.keys())
514+
xlow = xminBin * self.binWidth + self.origin
515+
xhigh = (xmaxBin + 1) * self.binWidth + self.origin
516+
return (xlow,xhigh)
517+
518+
def y_lim(self):
519+
""" return y low high tuble
520+
"""
521+
yminBin, ymaxBin, ynum, ylow, yhigh = prepare2Dsparse(self)
522+
return (ylow,yhigh)
523+

histogrammar/plot/root.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import math
2020
import types
2121

22+
# python 2/3 compatibility fixes
23+
from histogrammar.util import *
24+
2225
try:
2326
from collections import OrderedDict
2427
except ImportError:
@@ -229,7 +232,8 @@ def plotroot(self, name, title="", binType="D"):
229232
import ROOT
230233
constructor = getattr(ROOT, "TH2" + binType)
231234
sample = self.values[0]
232-
th2 = constructor(name, title, self.num, self.low, self.high, sample.num, sample.low, sample.high)
235+
th2 = constructor(name, title, int(self.num), float(self.low), float(self.high), \
236+
int(sample.num), float(sample.low), float(sample.high))
233237
for i in xrange(self.num):
234238
for j in xrange(sample.num):
235239
th2.SetBinContent(i + 1, j + 1, self.values[i].values[j].entries)
@@ -240,6 +244,7 @@ def plotroot(self, name, title="", binType="D"):
240244
import ROOT
241245
constructor = getattr(ROOT, "TH2" + binType)
242246
yminBin, ymaxBin, ynum, ylow, yhigh = prepareTH2sparse(self)
243-
th2 = constructor(name, title, self.num, self.low, self.high, ynum, ylow, yhigh)
247+
th2 = constructor(name, title, int(self.num), float(self.low), float(self.high), \
248+
int(ynum), float(ylow), float(yhigh))
244249
setTH2sparse(self, yminBin, ymaxBin, th2)
245250
return th2

histogrammar/primitives/sparselybin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def high(self):
222222
else:
223223
return (self.maxBin + 1) * self.binWidth + self.origin
224224

225-
def at(index):
225+
def at(self, index):
226226
"""Extract the container at a given index, if it exists."""
227227
return self.bins.get(index, None)
228228

@@ -231,7 +231,7 @@ def indexes(self):
231231
"""Get a sequence of filled indexes."""
232232
return sorted(self.keys)
233233

234-
def range(index):
234+
def range(self, index):
235235
"""Get the low and high edge of a bin (given by index number)."""
236236
return (index * self.binWidth + self.origin, (index + 1) * self.binWidth + self.origin)
237237

0 commit comments

Comments
 (0)