Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 22, 2024
1 parent fb3a933 commit 611fc2e
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/alfasim_sdk/result_reader/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def map_data(
class HistoryMatchingMetadata:
"""
Holder for the History Matching results metadata.
:ivar hm_items:
Map of the data id and its associated metadata.
:ivar result_directory:
Expand All @@ -230,10 +230,11 @@ class HMItem:
:ivar data_index:
The index of the data in the result datasets.
"""

parametric_var_name: str = attr.ib(validator=attr.validators.instance_of(str))
parametric_var_id: str = attr.ib(validator=attr.validators.instance_of(str))
data_index: int = attr.ib(validator=attr.validators.instance_of(int))

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> Self:
"""
Expand All @@ -242,9 +243,9 @@ def from_dict(cls, data: Dict[str, Any]) -> Self:
:raises: KeyError if some expected key is not present in the given data dict.
"""
return cls(

Check warning on line 245 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L245

Added line #L245 was not covered by tests
data_index=data['data_index'],
parametric_var_name=data['parametric_var_name'],
parametric_var_id=data['parametric_var_id'],
data_index=data["data_index"],
parametric_var_name=data["parametric_var_name"],
parametric_var_id=data["parametric_var_id"],
)

hm_items: Dict[str, HMItem] = attr.ib(validator=attr.validators.instance_of(Dict))
Expand All @@ -253,12 +254,12 @@ def from_dict(cls, data: Dict[str, Any]) -> Self:
@classmethod
def empty(cls, result_directory: Path) -> Self:
return cls(hm_items={}, result_directory=result_directory)

Check warning on line 256 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L256

Added line #L256 was not covered by tests

@classmethod
def from_result_directory(cls, result_directory: Path) -> Self:
"""
Read History Matching results metadata from result directory/file.
If the directory does not exist/is invalid, return an empty metadata.
"""

Expand All @@ -268,14 +269,18 @@ def map_data(hm_metadata: Dict) -> Dict[str, HistoryMatchingMetadata.HMItem]:
for key, data in hm_metadata.items()
}

with open_history_matching_result_file(result_directory=result_directory) as result_file:
with open_history_matching_result_file(

Check warning on line 272 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L272

Added line #L272 was not covered by tests
result_directory=result_directory
) as result_file:
if not result_file:
return cls.empty(result_directory=result_directory)

Check warning on line 276 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L275-L276

Added lines #L275 - L276 were not covered by tests

loaded_metadata = json.loads(

Check warning on line 278 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L278

Added line #L278 was not covered by tests
result_file[META_GROUP_NAME].attrs[HISTORY_MATCHING_GROUP_NAME]
)
return cls(hm_items=map_data(loaded_metadata), result_directory=result_directory)
return cls(

Check warning on line 281 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L281

Added line #L281 was not covered by tests
hm_items=map_data(loaded_metadata), result_directory=result_directory
)


@attr.s(slots=True, hash=False)
Expand Down Expand Up @@ -1696,14 +1701,16 @@ def read_global_sensitivity_coefficients(


@contextmanager
def open_history_matching_result_file(result_directory: Path) -> Iterator[Optional[h5py.File]]:
def open_history_matching_result_file(
result_directory: Path,
) -> Iterator[Optional[h5py.File]]:
"""
Open a History Matching result file.
:param result_directory:
The UQ main result directory.
"""
hm_results_path = result_directory.joinpath('history_matching/results')
hm_results_path = result_directory.joinpath("history_matching/results")
with _open_uq_result_file(hm_results_path) as file:
yield file

Check warning on line 1715 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L1713-L1715

Added lines #L1713 - L1715 were not covered by tests

Expand All @@ -1712,12 +1719,12 @@ def open_history_matching_result_file(result_directory: Path) -> Iterator[Option
def _open_uq_result_file(result_directory: Path) -> Iterator[Optional[h5py.File]]:
"""
Open a Uncertainty Quantification Analysis result file when it exists and is not being created.
:param result_directory:
The result directory for the given analysis.
"""
filename = result_directory / 'result'
ignored_file = result_directory / 'result.creating'
filename = result_directory / "result"
ignored_file = result_directory / "result.creating"

Check warning on line 1727 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L1726-L1727

Added lines #L1726 - L1727 were not covered by tests

if not filename.is_file():
yield None

Check warning on line 1730 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L1729-L1730

Added lines #L1729 - L1730 were not covered by tests
Expand All @@ -1726,4 +1733,4 @@ def _open_uq_result_file(result_directory: Path) -> Iterator[Optional[h5py.File]
with _open_result_file(filename) as file:
yield file

Check warning on line 1734 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L1732-L1734

Added lines #L1732 - L1734 were not covered by tests
else:
yield None
yield None

Check warning on line 1736 in src/alfasim_sdk/result_reader/aggregator.py

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L1736

Added line #L1736 was not covered by tests

0 comments on commit 611fc2e

Please sign in to comment.