|
23 | 23 | from ..utils.decorators import deprecated
|
24 | 24 | from ..utils.misc import check_if_jupyter
|
25 | 25 |
|
| 26 | +try: |
| 27 | + # noinspection PyPackageRequirements |
| 28 | + import numpy as np |
| 29 | + |
| 30 | + |
| 31 | + class NpEncoder(json.JSONEncoder): |
| 32 | + def default(self, obj): |
| 33 | + if isinstance(obj, np.integer): |
| 34 | + return int(obj) |
| 35 | + if isinstance(obj, np.floating): |
| 36 | + return float(obj) |
| 37 | + if isinstance(obj, np.ndarray): |
| 38 | + return obj.tolist() |
| 39 | + return json.JSONEncoder.default(self, obj) |
| 40 | + |
| 41 | +except ImportError: |
| 42 | + np = None |
| 43 | + |
| 44 | + |
| 45 | + class NpEncoder(json.JSONEncoder): |
| 46 | + pass |
| 47 | + |
26 | 48 | # TODO: add converter for any type of dataset (list, dataframe, numpy array)
|
27 | 49 |
|
28 | 50 | # Constants
|
@@ -112,26 +134,6 @@ def write_var_json(
|
112 | 134 | Dictionary containing a key-value pair representing the file name and json
|
113 | 135 | dump respectively.
|
114 | 136 | """
|
115 |
| - try: |
116 |
| - # noinspection PyPackageRequirements |
117 |
| - import numpy as np |
118 |
| - |
119 |
| - class NpEncoder(json.JSONEncoder): |
120 |
| - def default(self, obj): |
121 |
| - if isinstance(obj, np.integer): |
122 |
| - return int(obj) |
123 |
| - if isinstance(obj, np.floating): |
124 |
| - return float(obj) |
125 |
| - if isinstance(obj, np.ndarray): |
126 |
| - return obj.tolist() |
127 |
| - return json.JSONEncoder.default(self, obj) |
128 |
| - |
129 |
| - except ImportError: |
130 |
| - np = None |
131 |
| - |
132 |
| - class NpEncoder(json.JSONEncoder): |
133 |
| - pass |
134 |
| - |
135 | 137 | # MLFlow model handling
|
136 | 138 | if isinstance(input_data, list):
|
137 | 139 | dict_list = cls.generate_mlflow_variable_properties(input_data)
|
@@ -597,14 +599,14 @@ def input_fit_statistics(
|
597 | 599 |
|
598 | 600 | if json_path:
|
599 | 601 | with open(Path(json_path) / FITSTAT, "w") as json_file:
|
600 |
| - json_file.write(json.dumps(json_dict, indent=4)) |
| 602 | + json_file.write(json.dumps(json_dict, indent=4, cls=NpEncoder)) |
601 | 603 | if cls.notebook_output:
|
602 | 604 | print(
|
603 | 605 | f"{FITSTAT} was successfully written and saved to "
|
604 | 606 | f"{Path(json_path) / FITSTAT}"
|
605 | 607 | )
|
606 | 608 | else:
|
607 |
| - return {FITSTAT: json.dumps(json_dict, indent=4)} |
| 609 | + return {FITSTAT: json.dumps(json_dict, indent=4, cls=NpEncoder)} |
608 | 610 |
|
609 | 611 | @classmethod
|
610 | 612 | def add_tuple_to_fitstat(
|
@@ -881,17 +883,17 @@ def calculate_model_statistics(
|
881 | 883 | if json_path:
|
882 | 884 | for name in [FITSTAT, ROC, LIFT]:
|
883 | 885 | with open(Path(json_path) / name, "w") as json_file:
|
884 |
| - json_file.write(json.dumps(json_dict, indent=4)) |
| 886 | + json_file.write(json.dumps(json_dict, indent=4, cls=NpEncoder)) |
885 | 887 | if cls.notebook_output:
|
886 | 888 | print(
|
887 | 889 | f"{name} was successfully written and saved to "
|
888 | 890 | f"{Path(json_path) / name}"
|
889 | 891 | )
|
890 | 892 | else:
|
891 | 893 | return {
|
892 |
| - FITSTAT: json.dumps(json_dict[0], indent=4), |
893 |
| - ROC: json.dumps(json_dict[1], indent=4), |
894 |
| - LIFT: json.dumps(json_dict[2], indent=4), |
| 894 | + FITSTAT: json.dumps(json_dict[0], indent=4, cls=NpEncoder), |
| 895 | + ROC: json.dumps(json_dict[1], indent=4, cls=NpEncoder), |
| 896 | + LIFT: json.dumps(json_dict[2], indent=4, cls=NpEncoder), |
895 | 897 | }
|
896 | 898 |
|
897 | 899 | @staticmethod
|
@@ -1044,7 +1046,7 @@ def apply_dataframe_to_json(
|
1044 | 1046 | json_dict[row_num + partition * len(stat_df)]["dataMap"].update(row_dict)
|
1045 | 1047 | return json_dict
|
1046 | 1048 |
|
1047 |
| - # noinspection PyCallingNonCallable,PyNestedDecorators |
| 1049 | + # noinspection PyCallingNonCallable, PyNestedDecorators |
1048 | 1050 | @deprecated(
|
1049 | 1051 | "Please use the calculate_model_statistics method instead.",
|
1050 | 1052 | version="1.9",
|
|
0 commit comments