Skip to content

Commit 810fec4

Browse files
generate categorized hip-ra-x-result.json schema
1 parent 7b0c97e commit 810fec4

6 files changed

Lines changed: 559 additions & 63 deletions

File tree

src/geophires_x_schema_generator/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ all_messages_conf.log
33
!geophires-request.json
44
!geophires-result.json
55
!hip-ra-x-request.json
6+
!hip-ra-x-result.json

src/geophires_x_schema_generator/__init__.py

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from __future__ import annotations
2+
13
import json
24
import os
35
import sys
46
from pathlib import Path
5-
from typing import Tuple, Any
7+
from typing import Any, Callable
68

79
# Ruff disabled because imports are order-dependent
810
# ruff: noqa: I001
@@ -38,7 +40,8 @@
3840

3941
class GeophiresXSchemaGenerator:
4042
def __init__(self):
41-
pass
43+
# noinspection PyProtectedMember
44+
self.result_fields_by_category: dict[str, list[Any]] = GeophiresXResult._RESULT_FIELDS_BY_CATEGORY
4245

4346
@staticmethod
4447
def _get_dummy_model():
@@ -85,7 +88,7 @@ def get_parameter_sources(self) -> list:
8588
def get_schema_title(self) -> str:
8689
return 'GEOPHIRES'
8790

88-
def get_parameters_json(self) -> Tuple[str, str]:
91+
def get_parameters_json(self) -> tuple[str, str]:
8992

9093
def with_category(param_dict: dict, category: str):
9194
def _with_cat(p: Parameter, cat: str):
@@ -103,7 +106,7 @@ def _with_cat(p: Parameter, cat: str):
103106

104107
return json_dumpse(input_params), json_dumpse(output_params)
105108

106-
def generate_json_schema(self) -> Tuple[dict, dict]:
109+
def generate_json_schema(self) -> tuple[dict, dict]:
107110
"""
108111
:return: request schema, result schema
109112
:rtype: Tuple[dict, dict]
@@ -153,6 +156,10 @@ def get_result_json_schema(self, output_params_json) -> dict:
153156
properties = {}
154157
required = []
155158

159+
# if result_fields_by_category is None:
160+
# # noinspection PyProtectedMember
161+
# result_fields_by_category = GeophiresXResult._RESULT_FIELDS_BY_CATEGORY
162+
156163
output_params = json.loads(output_params_json)
157164
display_name_aliases = {}
158165
for param_name in output_params:
@@ -165,11 +172,10 @@ def get_result_json_schema(self, output_params_json) -> dict:
165172

166173
output_params = {**output_params, **display_name_aliases}
167174

168-
# noinspection PyProtectedMember
169-
for category in GeophiresXResult._RESULT_FIELDS_BY_CATEGORY:
175+
for category in self.result_fields_by_category:
170176
cat_properties = {}
171-
# noinspection PyProtectedMember
172-
for field in GeophiresXResult._RESULT_FIELDS_BY_CATEGORY[category]:
177+
178+
for field in self.result_fields_by_category[category]:
173179
param_name = field if isinstance(field, str) else field.field_name
174180

175181
ignored_output_param_names = ['After-Tax IRR'] # Silently ignored in favor of "After-tax IRR"
@@ -357,7 +363,7 @@ def _get_key(param: dict, k: str, default_val='') -> Any:
357363
return default_val
358364

359365

360-
def _get_min_and_max(param: dict, default_val='') -> Tuple:
366+
def _get_min_and_max(param: dict, default_val='') -> tuple:
361367
min_val = _get_key(param, 'Min', default_val=default_val)
362368
max_val = _get_key(param, 'Max', default_val=default_val)
363369

@@ -377,8 +383,30 @@ def _fix_floating_point_error(val: Any) -> Any:
377383

378384

379385
class HipRaXSchemaGenerator(GeophiresXSchemaGenerator):
386+
387+
def __init__(self):
388+
dummy_model = HIP_RA_X()
389+
390+
def _get_result_fields_for_category(
391+
output_config: list[tuple[Parameter, Callable[[Parameter], str]]],
392+
) -> list[Any]:
393+
return [it[0].Name for it in output_config]
394+
395+
# noinspection PyProtectedMember
396+
self.result_fields_by_category: dict[str, list[Any]] = {
397+
HIP_RA_X._SUMMARY_OF_RESULTS_OUTPUT_CATEGORY: _get_result_fields_for_category(
398+
dummy_model._get_output_config_for_summary_of_results_category(None, None)
399+
),
400+
HIP_RA_X._SUMMARY_OF_INPUTS_OUTPUT_CATEGORY: _get_result_fields_for_category(
401+
dummy_model._get_output_config_for_summary_of_inputs_category(None, None)
402+
),
403+
}
404+
380405
def get_parameter_sources(self) -> list:
381406
"""
407+
Single implicit input category used to keep the result schema shape consistent
408+
with the GEOPHIRES result schema (top-level properties -> category -> properties -> fields).
409+
382410
:rtype: list[Tuple[Any, str]]
383411
"""
384412
dummy_model = HIP_RA_X()
@@ -388,7 +416,37 @@ def get_schema_title(self) -> str:
388416
return 'HIP-RA-X'
389417

390418
def get_result_json_schema(self, output_params_json) -> dict:
391-
return None # FIXME TODO
419+
output_params = json.loads(output_params_json)
420+
421+
properties = {}
422+
423+
# noinspection PyProtectedMember
424+
for category in [HIP_RA_X._SUMMARY_OF_RESULTS_OUTPUT_CATEGORY, HIP_RA_X._SUMMARY_OF_INPUTS_OUTPUT_CATEGORY]:
425+
cat_properties = {}
426+
for param_name, output_param in output_params.items():
427+
description = _get_key(output_param, 'ToolTipText', default_val=None) or None
428+
units_val = output_param['CurrentUnits'] if isinstance(output_param.get('CurrentUnits'), str) else None
429+
cat_properties[param_name] = {
430+
'type': _get_key(output_param, 'json_parameter_type', default_val=None) or None,
431+
'description': description,
432+
'units': units_val,
433+
}
434+
435+
properties[category] = {
436+
'type': 'object',
437+
'properties': cat_properties,
438+
}
439+
440+
result_schema = {
441+
'definitions': {},
442+
'$schema': 'http://json-schema.org/draft-04/schema#',
443+
'type': 'object',
444+
'title': f'{self.get_schema_title()} Result Schema',
445+
'required': [],
446+
'properties': properties,
447+
}
448+
449+
return result_schema
392450

393451
def get_output_params_table_rst(self, output_params_json) -> str:
394452
"""
@@ -426,4 +484,4 @@ def get_input_schema_reference(self) -> str:
426484
return 'hip-ra-x-request.json'
427485

428486
def get_output_schema_reference(self) -> str:
429-
return None
487+
return 'hip-ra-x-result.json'

0 commit comments

Comments
 (0)