Skip to content

Commit 388a593

Browse files
Allow HipRaInputParameters to be initialized from either input file path or params dict
1 parent 8fec565 commit 388a593

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/geophires_monte_carlo/MC_GeoPHIRES3.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,14 @@ def work_package(pass_list: list):
218218
shutil.copyfile(result.output_file_path, tmp_output_file)
219219
elif args.Code_File.endswith('HIP_RA.py'):
220220
hip_ra_client: HipRaClient = HipRaClient()
221-
result: HipRaResult = hip_ra_client.get_hip_ra_result(HipRaInputParameters(from_file_path=Path(tmp_input_file)))
221+
result: HipRaResult = hip_ra_client.get_hip_ra_result(
222+
HipRaInputParameters(file_path_or_params_dict=Path(tmp_input_file))
223+
)
222224
shutil.copyfile(result.output_file_path, tmp_output_file)
223225
elif args.Code_File.endswith('hip_ra_x.py'):
224226
hip_ra_x_client: HipRaXClient = HipRaXClient()
225227
result: HipRaResult = hip_ra_x_client.get_hip_ra_result(
226-
HipRaInputParameters(from_file_path=Path(tmp_input_file))
228+
HipRaInputParameters(file_path_or_params_dict=Path(tmp_input_file))
227229
)
228230
shutil.copyfile(result.output_file_path, tmp_output_file)
229231
else:

src/hip_ra/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
import tempfile
55
import uuid
66
from pathlib import Path
7+
from types import MappingProxyType
78

89
from geophires_x_client.common import _get_logger
910
from hip_ra import HIP_RA
1011

1112

1213
class HipRaInputParameters:
13-
def __init__(self, from_file_path: str):
14-
self._input_file_path = Path(from_file_path)
14+
15+
def __init__(self, file_path_or_params_dict: str | MappingProxyType):
16+
if isinstance(file_path_or_params_dict, dict):
17+
tmp_file_path = Path(tempfile.gettempdir(), f'hip-ra-params_{uuid.uuid1()}.txt')
18+
with open(tmp_file_path, 'w') as f:
19+
f.writelines(
20+
[', '.join([str(p) for p in param_item]) + '\n' for param_item in file_path_or_params_dict.items()]
21+
)
22+
file_path_or_params_dict = str(tmp_file_path)
23+
24+
self._input_file_path = Path(file_path_or_params_dict)
1525
self._output_file_path = Path(
1626
tempfile.gettempdir(), f'hip-ra-result_{self._input_file_path.stem}_{uuid.uuid1()!s}.out'
1727
)

tests/hip_ra_x_tests/test_hip_ra_x.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,37 @@ def test_handling_input_file_not_found(self):
414414
with self.assertRaises(RuntimeError) as rex:
415415
client.get_hip_ra_result(
416416
HipRaInputParameters(
417-
from_file_path=Path(tempfile.gettempdir(), f'a-non-existent-file_{uuid.uuid1()!s}.txt')
417+
file_path_or_params_dict=Path(tempfile.gettempdir(), f'a-non-existent-file_{uuid.uuid1()!s}.txt')
418418
)
419419
)
420420

421421
self.assertIn('Unable to read input file', str(rex.exception))
422422
self.assertIn('.txt not found', str(rex.exception))
423423

424+
def test_hip_ra_input_parameters_init(self):
425+
input_from_params: HipRaInputParameters = HipRaInputParameters(
426+
{
427+
'Reservoir Temperature': 250.0,
428+
'Rejection Temperature': 60.0,
429+
'Reservoir Porosity': 10.0,
430+
'Reservoir Area': 55.0,
431+
'Reservoir Thickness': 0.25,
432+
'Reservoir Life Cycle': 25,
433+
}
434+
)
435+
436+
with open(input_from_params.as_file_path()) as f:
437+
input_file_contents = f.read()
438+
self.assertEqual(
439+
'Reservoir Temperature, 250.0\n'
440+
'Rejection Temperature, 60.0\n'
441+
'Reservoir Porosity, 10.0\n'
442+
'Reservoir Area, 55.0\n'
443+
'Reservoir Thickness, 0.25\n'
444+
'Reservoir Life Cycle, 25\n',
445+
input_file_contents,
446+
)
447+
424448
def _new_hip_ra_test_instance(self, enable_hip_ra_logging_config=False, pre_re_stash_runner=None) -> HIP_RA_X:
425449
stash_cwd = Path.cwd()
426450
stash_sys_argv = sys.argv

0 commit comments

Comments
 (0)