Skip to content

Commit 1520f0a

Browse files
Fix floating point errors in default/min/max values in schema generation
1 parent 7953430 commit 1520f0a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/geophires_x_schema_generator/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def generate_json_schema(self) -> dict:
112112
'type': param['json_parameter_type'],
113113
'units': units_val,
114114
'category': param['parameter_category'],
115-
'default': param['DefaultValue'],
115+
'default': _fix_floating_point_error(param['DefaultValue']),
116116
'minimum': min_val,
117117
'maximum': max_val,
118118
}
@@ -168,13 +168,15 @@ def get_input_params_table(category_params, category_name) -> str:
168168
# if param['Required']:
169169
# TODO designate required params
170170

171+
default_value = _fix_floating_point_error(_get_key(param, 'DefaultValue'))
172+
171173
min_val, max_val = _get_min_and_max(param)
172174

173175
input_rst += f"""\n * - {param['Name']}
174176
- {_get_key(param, 'ToolTipText')}
175177
- {_get_key(param, 'PreferredUnits')}
176178
- {_get_key(param, 'json_parameter_type')}
177-
- {_get_key(param, 'DefaultValue')}
179+
- {default_value}
178180
- {min_val}
179181
- {max_val}"""
180182

@@ -247,7 +249,14 @@ def _get_min_and_max(param: dict, default_val='') -> Tuple:
247249
min_val = min(param['AllowableRange'])
248250
max_val = max(param['AllowableRange'])
249251

250-
return (min_val, max_val)
252+
return _fix_floating_point_error(min_val), _fix_floating_point_error(max_val)
253+
254+
255+
def _fix_floating_point_error(val: Any) -> Any:
256+
if '.0000' in str(val):
257+
return format(float(val), '.1f')
258+
259+
return val
251260

252261

253262
class HipRaXSchemaGenerator(GeophiresXSchemaGenerator):

src/geophires_x_schema_generator/geophires-request.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@
726726
"category": "Well Bores",
727727
"default": 1.0,
728728
"minimum": 0.0,
729-
"maximum": 1.000001
729+
"maximum": "1.0"
730730
},
731731
"Is AGS": {
732732
"description": "Set to true if the model is for an Advanced Geothermal System (AGS)",
@@ -2086,7 +2086,7 @@
20862086
"type": "number",
20872087
"units": "%",
20882088
"category": "Economics",
2089-
"default": 7.000000000000001,
2089+
"default": "7.0",
20902090
"minimum": 0.0,
20912091
"maximum": 100.0
20922092
},

0 commit comments

Comments
 (0)