Coverage for pydelica/solutions.py: 95%

1 from pydelica.exception import ResultRetrievalError

2 import pandas as pd

3 import pathlib

4 import logging

5

6

7 class SolutionHandler:

8 """Stores solutions extracted from a Modelica simulation"""

9

10 def __init__(self, session_directory: str) -> None:

11 """Extract solution information from a given session directory

12

13 Parameters

14 ----------

15 session_directory : str

16 directory from a simulation session

17 """

18 self._logger = logging.getLogger("PyDelica.Solutions")

19 self._session_dir = session_directory

20 self._solutions: dict[str, pd.DataFrame] = {}

21

22 def retrieve_session_solutions(

23 self, run_directory: pathlib.Path

24 ) -> dict[str, pd.DataFrame]:

25 """Retrieve any stored solutions

26

27 Parameters

28 ----------

29 run_directory: pathlib.Path

30 directory containing run outputs

31

32 Returns

33 -------

34 dict[str, pd.DataFrame]

35 solutions extracted from valid results files

36

37 Raises

38 ------

39 ResultRetrievalError

40 If no CSV result files were found within the session directory

41 """

42 _has_csv = run_directory.glob("*_res.csv")

43

44 if not _has_csv:

45 raise ResultRetrievalError

46

47 for out_file in _has_csv:

48 self._logger.debug("Reading results from output file '%s'", out_file)

49 _key = f"{out_file}".split("_res")[0]

50 self._solutions[_key] = pd.read_csv(out_file)

51 return self._solutions

52

53 def get_solutions(self) -> dict[str, pd.DataFrame]:

54 return self._solutions