Skip to content

Commit 7d131ff

Browse files
committed
[ModelicaSystem] define doe_get_solutions() as separate method
1 parent 88a228b commit 7d131ff

2 files changed

Lines changed: 70 additions & 48 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,66 +2494,83 @@ def get_doe_solutions(
24942494
var_list: Optional[list] = None,
24952495
) -> Optional[tuple[str] | dict[str, dict[str, np.ndarray]]]:
24962496
"""
2497-
Get all solutions of the DoE run. The following return values are possible:
2497+
Wrapper for doe_get_solutions()
2498+
"""
2499+
if not isinstance(self._mod, ModelicaSystemOMC):
2500+
raise ModelicaSystemError(f"Invalid definition for mod: {type(self._mod)} - expect ModelicaSystemOMC!")
24982501

2499-
* A list of variables if val_list == None
2502+
return doe_get_solutions(
2503+
msomc=self._mod,
2504+
resultpath=self._resultpath,
2505+
doe_def=self.get_doe_definition(),
2506+
var_list=var_list,
2507+
)
25002508

2501-
* The Solutions as dict[str, pd.DataFrame] if a value list (== val_list) is defined.
25022509

2503-
The following code snippet can be used to convert the solution data for each run to a pandas dataframe:
2510+
def doe_get_solutions(
2511+
msomc: ModelicaSystemOMC,
2512+
resultpath: OMCPath,
2513+
doe_def: Optional[dict] = None,
2514+
var_list: Optional[list] = None,
2515+
) -> Optional[tuple[str] | dict[str, dict[str, np.ndarray]]]:
2516+
"""
2517+
Get all solutions of the DoE run. The following return values are possible:
25042518
2505-
```
2506-
import pandas as pd
2519+
* A list of variables if val_list == None
25072520
2508-
doe_sol = doe_mod.get_doe_solutions()
2509-
for key in doe_sol:
2510-
data = doe_sol[key]['data']
2511-
if data:
2512-
doe_sol[key]['df'] = pd.DataFrame.from_dict(data=data)
2513-
else:
2514-
doe_sol[key]['df'] = None
2515-
```
2521+
* The Solutions as dict[str, pd.DataFrame] if a value list (== val_list) is defined.
25162522
2517-
"""
2518-
if not isinstance(self._mod, ModelicaSystemOMC):
2519-
raise ModelicaSystemError(f"Invalid definition for mod: {type(self._mod)} - expect ModelicaSystemOMC!")
2523+
The following code snippet can be used to convert the solution data for each run to a pandas dataframe:
25202524
2521-
if not isinstance(self._doe_def, dict):
2522-
return None
2525+
```
2526+
import pandas as pd
25232527
2524-
if len(self._doe_def) == 0:
2525-
raise ModelicaSystemError("No result files available - all simulations did fail?")
2528+
doe_sol = doe_mod.get_doe_solutions()
2529+
for key in doe_sol:
2530+
data = doe_sol[key]['data']
2531+
if data:
2532+
doe_sol[key]['df'] = pd.DataFrame.from_dict(data=data)
2533+
else:
2534+
doe_sol[key]['df'] = None
2535+
```
25262536
2527-
sol_dict: dict[str, dict[str, Any]] = {}
2528-
for resultfilename in self._doe_def:
2529-
resultfile = self._resultpath / resultfilename
2537+
"""
2538+
if not isinstance(doe_def, dict):
2539+
return None
25302540

2531-
sol_dict[resultfilename] = {}
2541+
if len(doe_def) == 0:
2542+
raise ModelicaSystemError("No result files available - all simulations did fail?")
25322543

2533-
if not self._doe_def[resultfilename][self.DICT_RESULT_AVAILABLE]:
2534-
msg = f"No result file available for {resultfilename}"
2535-
logger.warning(msg)
2536-
sol_dict[resultfilename]['msg'] = msg
2537-
sol_dict[resultfilename]['data'] = {}
2538-
continue
2544+
sol_dict: dict[str, dict[str, Any]] = {}
2545+
for resultfilename in doe_def:
2546+
resultfile = resultpath / resultfilename
25392547

2540-
if var_list is None:
2541-
var_list_row = list(self._mod.getSolutions(resultfile=resultfile))
2542-
else:
2543-
var_list_row = var_list
2544-
2545-
try:
2546-
sol = self._mod.getSolutions(varList=var_list_row, resultfile=resultfile)
2547-
sol_data = {var: sol[idx] for idx, var in enumerate(var_list_row)}
2548-
sol_dict[resultfilename]['msg'] = 'Simulation available'
2549-
sol_dict[resultfilename]['data'] = sol_data
2550-
except ModelicaSystemError as ex:
2551-
msg = f"Error reading solution for {resultfilename}: {ex}"
2552-
logger.warning(msg)
2553-
sol_dict[resultfilename]['msg'] = msg
2554-
sol_dict[resultfilename]['data'] = {}
2555-
2556-
return sol_dict
2548+
sol_dict[resultfilename] = {}
2549+
2550+
if not doe_def[resultfilename][ModelicaDoEABC.DICT_RESULT_AVAILABLE]:
2551+
msg = f"No result file available for {resultfilename}"
2552+
logger.warning(msg)
2553+
sol_dict[resultfilename]['msg'] = msg
2554+
sol_dict[resultfilename]['data'] = {}
2555+
continue
2556+
2557+
if var_list is None:
2558+
var_list_row = list(msomc.getSolutions(resultfile=resultfile))
2559+
else:
2560+
var_list_row = var_list
2561+
2562+
try:
2563+
sol = msomc.getSolutions(varList=var_list_row, resultfile=resultfile)
2564+
sol_data = {var: sol[idx] for idx, var in enumerate(var_list_row)}
2565+
sol_dict[resultfilename]['msg'] = 'Simulation available'
2566+
sol_dict[resultfilename]['data'] = sol_data
2567+
except ModelicaSystemError as ex:
2568+
msg = f"Error reading solution for {resultfilename}: {ex}"
2569+
logger.warning(msg)
2570+
sol_dict[resultfilename]['msg'] = msg
2571+
sol_dict[resultfilename]['data'] = {}
2572+
2573+
return sol_dict
25572574

25582575

25592576
class ModelicaSystemDoE(ModelicaDoEOMC):

OMPython/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
ModelicaSystemDoE,
2020
ModelicaDoEOMC,
2121
ModelicaSystemError,
22+
23+
doe_get_solutions,
2224
)
2325
from OMPython.OMCSession import (
2426
OMCPath,
@@ -54,6 +56,9 @@
5456
'OMCPath',
5557

5658
'OMCSession',
59+
60+
'doe_get_solutions',
61+
5762
'OMCSessionCmd',
5863
'OMCSessionDocker',
5964
'OMCSessionDockerContainer',

0 commit comments

Comments
 (0)