17
17
from __future__ import absolute_import
18
18
import types
19
19
20
+ # python 2/3 compatibility fixes
21
+ from histogrammar .util import *
20
22
21
23
def prepare2Dsparse (sparse ):
22
24
yminBins = [v .minBin for v in sparse .bins .values () if v .minBin is not None ]
@@ -25,10 +27,10 @@ def prepare2Dsparse(sparse):
25
27
yminBin = min (yminBins )
26
28
ymaxBin = max (ymaxBins )
27
29
else :
28
- yminBin = 0.0
29
- ymaxBin = 0.0
30
+ yminBin = 0
31
+ ymaxBin = 0
30
32
sample = list (sparse .bins .values ())[0 ]
31
- ynum = 1.0 + ymaxBin - yminBin
33
+ ynum = 1 + ymaxBin - yminBin
32
34
ylow = yminBin * sample .binWidth + sample .origin
33
35
yhigh = (ymaxBin + 1.0 ) * sample .binWidth + sample .origin
34
36
return yminBin , ymaxBin , ynum , ylow , yhigh
@@ -52,13 +54,11 @@ def plotmatplotlib(self, name=None, **kwargs):
52
54
import numpy as np
53
55
ax = plt .gca ()
54
56
55
- entries = [x .entries for x in self .values ]
57
+ edges = self .bin_edges ()
58
+ entries = self .bin_entries ()
59
+ width = self .bin_width ()
56
60
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 )
62
62
63
63
if name is not None :
64
64
ax .set_title (name )
@@ -67,6 +67,42 @@ def plotmatplotlib(self, name=None, **kwargs):
67
67
68
68
return ax
69
69
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
+
70
106
class SparselyHistogramMethods (object ):
71
107
def plotmatplotlib (self , name = None , ** kwargs ):
72
108
"""
@@ -79,21 +115,72 @@ def plotmatplotlib(self, name=None, **kwargs):
79
115
import numpy as np
80
116
ax = plt .gca ()
81
117
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 ()
89
121
122
+ ax .bar (edges [:- 1 ], entries , width = width , ** kwargs )
123
+ ax .set_xlim (self .low ,self .high )
124
+
90
125
if name is not None :
91
126
ax .set_title (name )
92
127
else :
93
128
ax .set_title (self .name )
94
129
95
130
return ax
96
131
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
+
97
184
class ProfileMethods (object ):
98
185
def plotmatplotlib (self , name = None , ** kwargs ):
99
186
""" Plotting method for Bin of Average
@@ -242,7 +329,7 @@ def plotmatplotlib(self, name=None, **kwargs):
242
329
import numpy as np
243
330
ax = plt .gca ()
244
331
color_cycle = plt .rcParams ['axes.color_cycle' ]
245
- if kwargs . has_key ( "color" ) :
332
+ if "color" in kwargs :
246
333
kwargs .pop ("color" )
247
334
248
335
for i , hist in enumerate (self .values ):
@@ -264,7 +351,7 @@ def plotmatplotlib(self, name=None, **kwargs):
264
351
import numpy as np
265
352
ax = plt .gca ()
266
353
color_cycle = plt .rcParams ['axes.color_cycle' ]
267
- if kwargs . has_key ( "color" ) :
354
+ if "color" in kwargs :
268
355
kwargs .pop ("color" )
269
356
270
357
for i , hist in enumerate (self .values ):
@@ -330,15 +417,9 @@ def plotmatplotlib(self, name=None, **kwargs):
330
417
import numpy as np
331
418
ax = plt .gca ()
332
419
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 ())
342
423
343
424
ax .pcolormesh (x_ranges , y_ranges , grid , ** kwargs )
344
425
@@ -348,6 +429,34 @@ def plotmatplotlib(self, name=None, **kwargs):
348
429
ax .set_title (self .name )
349
430
return ax
350
431
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
+
351
460
352
461
class SparselyTwoDimensionallyHistogramMethods (object ):
353
462
def plotmatplotlib (self , name = None , ** kwargs ):
@@ -361,6 +470,24 @@ def plotmatplotlib(self, name=None, **kwargs):
361
470
import numpy as np
362
471
ax = plt .gca ()
363
472
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
+
364
491
yminBin , ymaxBin , ynum , ylow , yhigh = prepare2Dsparse (self )
365
492
366
493
xbinWidth = self .binWidth
@@ -377,17 +504,20 @@ def plotmatplotlib(self, name=None, **kwargs):
377
504
x_ranges = np .arange (xlow , xhigh + xbinWidth , xbinWidth )
378
505
y_ranges = np .arange (ylow , yhigh + ybinWidth , ybinWidth )
379
506
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
393
508
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
+
0 commit comments