1616
1717import math
1818import numbers
19+ import numpy as np
1920
2021from histogrammar .defs import Container , Factory , identity , JsonFormatException , ContainerException
2122from histogrammar .util import n_dim , datatype , serializable , inheritdoc , maybeAdd , floatToJson , hasKeys , numeq , \
@@ -186,8 +187,12 @@ def fill(self, datum, weight=1.0):
186187
187188 if weight > 0.0 :
188189 q = self .quantity (datum )
189- if not isinstance (q , basestring ):
190- raise TypeError ("function return value ({0}) must be a string" .format (q ))
190+ if isinstance (q , (basestring , bool )):
191+ pass
192+ elif q is None or np .isnan (q ):
193+ q = 'NaN'
194+ if not isinstance (q , (basestring , bool )):
195+ raise TypeError ("function return value ({0}) must be a string or bool" .format (q ))
191196
192197 if q not in self .bins :
193198 self .bins [q ] = self .value .zero ()
@@ -275,6 +280,8 @@ def _c99StructName(self):
275280
276281 def _numpy (self , data , weights , shape ):
277282 q = self .quantity (data )
283+ if isinstance (q , (list , tuple )):
284+ q = np .array (q )
278285 self ._checkNPQuantity (q , shape )
279286 self ._checkNPWeights (weights , shape )
280287 weights = self ._makeNPWeights (weights , shape )
@@ -283,17 +290,19 @@ def _numpy(self, data, weights, shape):
283290 subweights = weights .copy ()
284291 subweights [weights < 0.0 ] = 0.0
285292
286- import numpy
287- selection = numpy .empty (q .shape , dtype = numpy .bool )
288-
289- uniques , inverse = numpy .unique (q , return_inverse = True )
293+ selection = np .empty (q .shape , dtype = np .bool )
294+ uniques , inverse = np .unique (q , return_inverse = True )
290295
291296 # no possibility of exception from here on out (for rollback)
292297 for i , x in enumerate (uniques ):
298+ if isinstance (x , (basestring , bool )):
299+ pass
300+ elif x is None or np .isnan (x ):
301+ x = 'NaN'
293302 if x not in self .bins :
294303 self .bins [x ] = self .value .zero ()
295304
296- numpy .not_equal (inverse , i , selection )
305+ np .not_equal (inverse , i , selection )
297306 subweights [:] = weights
298307 subweights [selection ] = 0.0
299308 self .bins [x ]._numpy (data , subweights , shape )
@@ -412,7 +421,6 @@ def bin_entries(self, labels=[]):
412421 :returns: array of bin-entries
413422 :rtype: numpy.array
414423 """
415- import numpy as np
416424 if len (labels ) == 0 :
417425 return np .array ([self .bins [i ].entries for i in self .bins ])
418426 entries = [self .bins [lab ].entries if lab in self .bins else 0.0 for lab in labels ]
@@ -426,7 +434,6 @@ def bin_labels(self, max_length=-1):
426434 :returns: array of labels
427435 :rtype: numpy.array
428436 """
429- import numpy as np
430437 labels = []
431438
432439 for i , key in enumerate (self .bins .keys ()):
0 commit comments