31
31
ErrorBarArtists = namedtuple ("ErrorBarArtists" , "errorbar" )
32
32
ColormeshArtists = namedtuple ("ColormeshArtists" , "pcolormesh cbar text" )
33
33
34
-
35
34
Hist1DArtists = Union [StairsArtists , ErrorBarArtists ]
36
35
Hist2DArtists = ColormeshArtists
37
36
@@ -188,36 +187,40 @@ def histplot(
188
187
plottables = []
189
188
flow_bins = final_bins
190
189
for i , h in enumerate (hists ):
191
- value , variance = h .values ().copy (), h .variances ()
192
- if variance is not None :
193
- variance = variance .copy ()
194
- underflow , overflow = 0 , 0
195
- underflowv , overflowv = 0 , 0
196
- # One sided flow bins - hist
197
- if hasattr (h , "axes" ) and hasattr (h .axes [0 ], "traits" ):
198
- if hasattr (h .axes [0 ].traits , "overflow" ) and h .axes [0 ].traits .overflow :
199
- overflow = h .values (flow = True )[- 1 ].copy ()
200
- overflowv = h .variances (flow = True )[- 1 ].copy ()
201
- if hasattr (h .axes [0 ].traits , "underflow" ) and h .axes [0 ].traits .underflow :
202
- underflow = h .values (flow = True )[0 ].copy ()
203
- underflowv = h .variances (flow = True )[0 ].copy ()
190
+ value , variance = np .copy (h .values ()), h .variances ()
191
+ if has_variances := variance is not None :
192
+ variance = np .copy (variance )
193
+ underflow , overflow = 0.0 , 0.0
194
+ underflowv , overflowv = 0.0 , 0.0
195
+ # One sided flow bins - hist (uproot hist does not have the over- or underflow traits)
196
+ if (
197
+ hasattr (h , "axes" )
198
+ and (traits := getattr (h .axes [0 ], "traits" , None )) is not None
199
+ and hasattr (traits , "underflow" )
200
+ and hasattr (traits , "overflow" )
201
+ ):
202
+ if traits .overflow :
203
+ overflow = np .copy (h .values (flow = True ))[- 1 ]
204
+ if has_variances :
205
+ overflowv = np .copy (h .variances (flow = True ))[- 1 ]
206
+ if traits .underflow :
207
+ underflow = np .copy (h .values (flow = True ))[0 ]
208
+ if has_variances :
209
+ underflowv = np .copy (h .variances (flow = True ))[0 ]
204
210
# Both flow bins exist - uproot
205
- if hasattr (h , "values" ) and "flow" in inspect .getfullargspec (h .values ).args :
211
+ elif hasattr (h , "values" ) and "flow" in inspect .getfullargspec (h .values ).args :
206
212
if len (h .values ()) + 2 == len (
207
213
h .values (flow = True )
208
214
): # easy case, both over/under
209
215
underflow , overflow = (
210
- h .values (flow = True )[0 ].copy (),
211
- h .values (flow = True )[- 1 ].copy (),
212
- )
213
- underflowv , overflowv = (
214
- h .variances (flow = True )[0 ].copy (),
215
- h .variances (flow = True )[- 1 ].copy (),
216
+ np .copy (h .values (flow = True ))[0 ],
217
+ np .copy (h .values (flow = True ))[- 1 ],
216
218
)
217
- # Bins cannot be parsed
218
- else :
219
- underflow , overflow = 0 , 0
220
- underflowv , overflowv = 0 , 0
219
+ if has_variances :
220
+ underflowv , overflowv = (
221
+ np .copy (h .variances (flow = True ))[0 ],
222
+ np .copy (h .variances (flow = True ))[- 1 ],
223
+ )
221
224
222
225
# Set plottables
223
226
if flow == "none" :
@@ -228,23 +231,27 @@ def histplot(
228
231
_flow_bin_size = np .max (
229
232
[0.05 * (final_bins [- 1 ] - final_bins [0 ]), np .mean (np .diff (final_bins ))]
230
233
)
231
- flow_bins = final_bins .copy ()
234
+ flow_bins = np .copy (final_bins )
232
235
if underflow > 0 :
233
236
flow_bins = np .r_ [flow_bins [0 ] - _flow_bin_size , flow_bins ]
234
237
value = np .r_ [underflow , value ]
235
- variance = np .r_ [underflowv , variance ]
238
+ if has_variances :
239
+ variance = np .r_ [underflowv , variance ]
236
240
if overflow > 0 :
237
241
flow_bins = np .r_ [flow_bins , flow_bins [- 1 ] + _flow_bin_size ]
238
242
value = np .r_ [value , overflow ]
239
- variance = np .r_ [variance , overflowv ]
243
+ if has_variances :
244
+ variance = np .r_ [variance , overflowv ]
240
245
plottables .append (Plottable (value , edges = flow_bins , variances = variance ))
241
246
elif flow == "sum" :
242
247
if underflow > 0 :
243
248
value [0 ] += underflow
244
- variance [0 ] += underflowv
249
+ if has_variances :
250
+ variance [0 ] += underflowv
245
251
if overflow > 0 :
246
252
value [- 1 ] += overflow
247
- variance [- 1 ] += overflowv
253
+ if has_variances :
254
+ variance [- 1 ] += overflowv
248
255
plottables .append (Plottable (value , edges = final_bins , variances = variance ))
249
256
else :
250
257
plottables .append (Plottable (value , edges = final_bins , variances = variance ))
@@ -675,7 +682,7 @@ def hist2dplot(
675
682
676
683
# TODO: use Histogram everywhere
677
684
678
- H = h .values (). copy ( )
685
+ H = np . copy ( h .values ())
679
686
xbins , xtick_labels = get_plottable_protocol_bins (h .axes [0 ])
680
687
ybins , ytick_labels = get_plottable_protocol_bins (h .axes [1 ])
681
688
# Show under/overflow bins
@@ -715,7 +722,7 @@ def hist2dplot(
715
722
H = padded
716
723
xbins , ybins = pxbins , pybins
717
724
elif flow == "sum" :
718
- H = h .values (). copy ( )
725
+ H = np . copy ( h .values ())
719
726
# Sum borders
720
727
H [0 ], H [- 1 ] = (
721
728
H [0 ] + h .values (flow = True )[0 , 1 :- 1 ],
0 commit comments