Skip to content

Commit

Permalink
Fix floating point errors in default/min/max values in schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareengineerprogrammer committed Jan 22, 2025
1 parent 7953430 commit 1520f0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/geophires_x_schema_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def generate_json_schema(self) -> dict:
'type': param['json_parameter_type'],
'units': units_val,
'category': param['parameter_category'],
'default': param['DefaultValue'],
'default': _fix_floating_point_error(param['DefaultValue']),
'minimum': min_val,
'maximum': max_val,
}
Expand Down Expand Up @@ -168,13 +168,15 @@ def get_input_params_table(category_params, category_name) -> str:
# if param['Required']:
# TODO designate required params

default_value = _fix_floating_point_error(_get_key(param, 'DefaultValue'))

min_val, max_val = _get_min_and_max(param)

input_rst += f"""\n * - {param['Name']}
- {_get_key(param, 'ToolTipText')}
- {_get_key(param, 'PreferredUnits')}
- {_get_key(param, 'json_parameter_type')}
- {_get_key(param, 'DefaultValue')}
- {default_value}
- {min_val}
- {max_val}"""

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

return (min_val, max_val)
return _fix_floating_point_error(min_val), _fix_floating_point_error(max_val)


def _fix_floating_point_error(val: Any) -> Any:
if '.0000' in str(val):
return format(float(val), '.1f')

return val


class HipRaXSchemaGenerator(GeophiresXSchemaGenerator):
Expand Down
4 changes: 2 additions & 2 deletions src/geophires_x_schema_generator/geophires-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@
"category": "Well Bores",
"default": 1.0,
"minimum": 0.0,
"maximum": 1.000001
"maximum": "1.0"
},
"Is AGS": {
"description": "Set to true if the model is for an Advanced Geothermal System (AGS)",
Expand Down Expand Up @@ -2086,7 +2086,7 @@
"type": "number",
"units": "%",
"category": "Economics",
"default": 7.000000000000001,
"default": "7.0",
"minimum": 0.0,
"maximum": 100.0
},
Expand Down

0 comments on commit 1520f0a

Please sign in to comment.