Skip to content

Commit e181eb8

Browse files
author
KX79WQ
committed
Fix of SparselyBin and Categorize toJson() to account for contentType of sub-hists
Deserialization from json failed for sparselybin and categorize multidimensional histograms b/c contentType of sub-hists was not properly serialized. Now fixed.
1 parent bbe9535 commit e181eb8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

histogrammar/primitives/categorize.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def __init__(self, quantity, value=Count()):
8181
self.bins = {}
8282
if value is not None:
8383
self.contentType = value.name
84+
else:
85+
self.contentType = "Count"
8486
super(Categorize, self).__init__()
8587
self.specialize()
8688

@@ -286,11 +288,18 @@ def toJsonFragment(self, suppressName):
286288
else:
287289
binsName = None
288290

291+
if len(self.bins) > 0:
292+
bins_type = list(self.bins.values())[0].name
293+
elif self.value is not None:
294+
bins_type = self.value.name
295+
else:
296+
bins_type = self.contentType
297+
289298
return maybeAdd({
290299
# for json serialization all keys need to be strings, else json libs throws TypeError
291300
# e.g. boolean keys get converted to strings here
292301
"entries": floatToJson(self.entries),
293-
"bins:type": self.value.name if self.value is not None else self.contentType,
302+
"bins:type": bins_type,
294303
"bins": dict((str(k), v.toJsonFragment(True)) for k, v in self.bins.items()),
295304
}, **{"name": None if suppressName else self.quantity.name,
296305
"bins:name": binsName})

histogrammar/primitives/sparselybin.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def __init__(self, binWidth, quantity, value=Count(), nanflow=Count(), origin=0.
106106
self.value = value
107107
if value is not None:
108108
self.contentType = value.name
109+
else:
110+
self.contentType = "Count"
109111
self.bins = {}
110112
self.nanflow = nanflow.copy()
111113
self.origin = origin
@@ -424,10 +426,17 @@ def toJsonFragment(self, suppressName):
424426
else:
425427
binsName = None
426428

429+
if len(self.bins) > 0:
430+
bins_type = list(self.bins.values())[0].name
431+
elif self.value is not None:
432+
bins_type = self.value.name
433+
else:
434+
bins_type = self.contentType
435+
427436
return maybeAdd({
428437
"binWidth": floatToJson(self.binWidth),
429438
"entries": floatToJson(self.entries),
430-
"bins:type": self.value.name if self.value is not None else self.contentType,
439+
"bins:type": bins_type,
431440
"bins": dict((str(i), v.toJsonFragment(True)) for i, v in self.bins.items()),
432441
"nanflow:type": self.nanflow.name,
433442
"nanflow": self.nanflow.toJsonFragment(False),

0 commit comments

Comments
 (0)