fixed metadata handling in to_json() for UserWarning/UserError handling
@@ -400,21 +400,26 @@ class Results(object):
import json
+ # copy results dataframe so that we don't perform modifications on original
df = deepcopy(self.df)
+ # reindex manually to remove one level of nesting
try:
for column in df.index.names:
df[column] = df.index.get_level_values(column)
except AttributeError:
+ # trend() results are stored a bit differently, this needs to be addressed
self.dbg(-1, "trend() results are not supported yet")
return None
+ # reset the index
df = df.reset_index(drop=True).copy()
+ # fill numpy nans with string nans
df.fillna("nan", inplace=True)
+ # traverse dataframe and generate json tree, look at test output for an example
json_tree = {}
-
stop_value = None
variants = []
for variant in df.value.keys():
@@ -440,9 +445,16 @@ class Results(object):
else:
metrics.append({"name": metric, "subgroup_metrics": subgroup_metrics})
variants.append({"name": variant, "metrics": metrics})
-
json_tree['variants'] = variants
- json_tree['metadata'] = self.metadata
+
+ # store metadata in temporary variable as UserWarning() needs to be converted to string so that JSON serialization can continue
+ metadata = self.metadata
+ for m in metadata:
+ if m == 'errors' or m == 'warnings':
+ for k in metadata[m]:
+ metadata[m][k] = str(metadata[m][k])
+
+ json_tree['metadata'] = metadata
json_string = json.dumps(json_tree)
