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 Mar 1, 2024
1 parent 2b4705e commit 23c07ef
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
36 changes: 22 additions & 14 deletions src/alfasim_sdk/result_reader/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
from alfasim_sdk.result_reader.aggregator_constants import (
GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import (
HISTORY_MATCHING_DETERMINISTIC_DSET_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import HISTORY_MATCHING_GROUP_NAME
from alfasim_sdk.result_reader.aggregator_constants import HISTORY_MATCHING_PROBABILISTIC_DSET_NAME
from alfasim_sdk.result_reader.aggregator_constants import HISTORY_MATCHING_DETERMINISTIC_DSET_NAME
from alfasim_sdk.result_reader.aggregator_constants import (
HISTORY_MATCHING_PROBABILISTIC_DSET_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import META_GROUP_NAME
from alfasim_sdk.result_reader.aggregator_constants import PROFILES_GROUP_NAME
from alfasim_sdk.result_reader.aggregator_constants import (
Expand Down Expand Up @@ -200,7 +204,9 @@ def map_data(
for key, data in gsa_metadata.items()
}

with open_result_file(result_directory, result_filename='uq_result') as result_file:
with open_result_file(
result_directory, result_filename="uq_result"
) as result_file:
if not result_file:
return cls.empty(result_directory=result_directory)

Expand Down Expand Up @@ -1658,7 +1664,7 @@ def read_global_sensitivity_analysis_time_set(
"""
Get the time set for sensitivity analysis results.
"""
with open_result_file(result_directory, result_filename='uq_result') as result_file:
with open_result_file(result_directory, result_filename="uq_result") as result_file:
if not result_file:
return
return result_file[GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME]["time_set"][:]
Expand All @@ -1675,7 +1681,9 @@ def read_global_sensitivity_coefficients(
if not metadata.gsa_items:
return None
meta = metadata.gsa_items[coefficients_key]
with open_result_file(metadata.result_directory, result_filename='uq_result') as result_file:
with open_result_file(
metadata.result_directory, result_filename="uq_result"
) as result_file:
gsa_group = result_file[GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME]
coefficients_dset = gsa_group["global_sensitivity_analysis"]
return coefficients_dset[meta.qoi_index, meta.qoi_data_index]
Expand All @@ -1690,8 +1698,8 @@ def read_history_matching_metadata(result_directory: Path) -> HistoryMatchingMet


def read_history_matching_result(
metadata: HistoryMatchingMetadata,
hm_type: Literal['deterministic', 'probabilistic'],
metadata: HistoryMatchingMetadata,
hm_type: Literal["deterministic", "probabilistic"],
hm_result_key: HistoryMatchingResultKeyType | None = None,
) -> Dict[HistoryMatchingResultKeyType, np.ndarray | float]:
"""
Expand All @@ -1707,21 +1715,21 @@ def read_history_matching_result(
could be an array with N values (N being the sampling size) for the probabilistic or a single
float for the deterministic.
"""
if hm_type not in ('deterministic', 'probabilistic'):
raise ValueError(f'history matching of type {hm_type} not supported')
if hm_type not in ("deterministic", "probabilistic"):
raise ValueError(f"history matching of type {hm_type} not supported")

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

View check run for this annotation

Codecov / codecov/patch

src/alfasim_sdk/result_reader/aggregator.py#L1719

Added line #L1719 was not covered by tests

if hm_type == 'deterministic':
if hm_type == "deterministic":
dataset_key = HISTORY_MATCHING_DETERMINISTIC_DSET_NAME
else:
assert hm_type == 'probabilistic'
assert hm_type == "probabilistic"
dataset_key = HISTORY_MATCHING_PROBABILISTIC_DSET_NAME

with open_result_file(metadata.result_directory) as result_file:
if not result_file:
return {}

result = result_file[HISTORY_MATCHING_GROUP_NAME][dataset_key]

result_map = {}
if hm_result_key is None:
for key, meta in metadata.hm_items.items():
Expand All @@ -1736,7 +1744,7 @@ def read_history_matching_result(

@contextmanager
def open_result_file(
result_directory: Path, result_filename: str = 'result'
result_directory: Path, result_filename: str = "result"
) -> Iterator[Optional[h5py.File]]:
"""
:param result_directory:
Expand All @@ -1747,7 +1755,7 @@ def open_result_file(
The result HDF file, or None if it doesn't exist or is still being created.
"""
filepath = result_directory / result_filename
ignored_file = result_directory / f'{result_filename}.creating'
ignored_file = result_directory / f"{result_filename}.creating"

if not filepath.is_file():
yield None
Expand Down
34 changes: 21 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import shutil
import textwrap
from pathlib import Path
from typing import List, Sequence, Tuple
from typing import List
from typing import Sequence
from typing import Tuple

import h5py
import numpy as np
Expand All @@ -13,8 +15,12 @@

from alfasim_sdk.result_reader.aggregator_constants import (
GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import (
HISTORY_MATCHING_DETERMINISTIC_DSET_NAME,
HISTORY_MATCHING_GROUP_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import HISTORY_MATCHING_GROUP_NAME
from alfasim_sdk.result_reader.aggregator_constants import (
HISTORY_MATCHING_PROBABILISTIC_DSET_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import META_GROUP_NAME
Expand Down Expand Up @@ -269,11 +275,11 @@ def _create_and_populate_hm_result_file(
result_dir: Path,
result: np.ndarray,
dataset_key: str,
limits: Sequence[Tuple[float, float]]
limits: Sequence[Tuple[float, float]],
) -> None:
result_dir.mkdir(parents=True, exist_ok=True)
result_filepath = result_dir / "result"

file = h5py.File(result_filepath, "x", libver="latest", locking=False)
meta_group = file.create_group(META_GROUP_NAME, track_order=True)
data_group = file.create_group(HISTORY_MATCHING_GROUP_NAME, track_order=True)
Expand Down Expand Up @@ -304,7 +310,7 @@ def _create_and_populate_hm_result_file(

meta_group.attrs[HISTORY_MATCHING_GROUP_NAME] = json.dumps(fake_meta)
dataset[:] = result

file.swmr_mode = True
file.close()

Expand All @@ -318,16 +324,18 @@ def hm_probabilistic_results_dir(datadir: Path) -> Path:
import numpy as np

result_dir = datadir / "main-HM-probabilistic"
probabilistic_result = np.array([[0.1, 0.22, 1.0, 0.8, 0.55], [3.0, 6.0, 5.1, 4.7, 6.3]])
probabilistic_result = np.array(
[[0.1, 0.22, 1.0, 0.8, 0.55], [3.0, 6.0, 5.1, 4.7, 6.3]]
)
limits = [(0.0, 1.0), (2.5, 7.5)]

_create_and_populate_hm_result_file(
result_dir=result_dir,
result=probabilistic_result,
dataset_key=HISTORY_MATCHING_PROBABILISTIC_DSET_NAME,
limits=limits
limits=limits,
)

return result_dir


Expand All @@ -342,12 +350,12 @@ def hm_deterministic_results_dir(datadir: Path) -> Path:
result_dir = datadir / "main-HM-deterministic"
deterministic_result = np.array([0.1, 3.2])
limits = [(0.0, 1.0), (2.5, 7.5)]

_create_and_populate_hm_result_file(
result_dir=result_dir,
result=deterministic_result,
dataset_key=HISTORY_MATCHING_DETERMINISTIC_DSET_NAME,
limits=limits
limits=limits,
)
return result_dir

return result_dir
72 changes: 40 additions & 32 deletions tests/results/test_aggregator.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import itertools
import re
from pathlib import Path
from typing import List, Literal
from typing import List
from typing import Literal

import attr
import numpy
import pytest
from pytest_mock import MockerFixture
from pytest_regressions.num_regression import NumericRegressionFixture

from alfasim_sdk.result_reader.aggregator import HistoryMatchingMetadata, concatenate_metadata, read_history_matching_metadata, read_history_matching_result
from alfasim_sdk.result_reader.aggregator import concatenate_metadata
from alfasim_sdk.result_reader.aggregator import HistoryMatchingMetadata
from alfasim_sdk.result_reader.aggregator import open_result_files
from alfasim_sdk.result_reader.aggregator import (
read_global_sensitivity_analysis_meta_data,
Expand All @@ -18,6 +20,8 @@
read_global_sensitivity_analysis_time_set,
)
from alfasim_sdk.result_reader.aggregator import read_global_sensitivity_coefficients
from alfasim_sdk.result_reader.aggregator import read_history_matching_metadata
from alfasim_sdk.result_reader.aggregator import read_history_matching_result
from alfasim_sdk.result_reader.aggregator import read_metadata
from alfasim_sdk.result_reader.aggregator import read_profiles_local_statistics
from alfasim_sdk.result_reader.aggregator import read_time_sets
Expand Down Expand Up @@ -372,98 +376,102 @@ def test_read_incomplete_gsa_metadata(global_sa_results_dir: Path) -> None:
assert coefficients is None


def test_read_history_matching_result_metadata(hm_probabilistic_results_dir: Path) -> None:
def test_read_history_matching_result_metadata(
hm_probabilistic_results_dir: Path,
) -> None:
"""
Check reading the HM metadata from a probabilistic result file, which should be enough to
evaluate the deterministic metadata too as they are handled exactly the same.
"""
hm_results_dir = hm_probabilistic_results_dir

# Existent and completed result file, metadata should be filled.
metadata = read_history_matching_metadata(hm_results_dir)

assert metadata.result_directory == hm_results_dir
items_meta = metadata.hm_items

expected_meta1 = HistoryMatchingMetadata.HMItem(
parametric_var_id='parametric_var_1',
parametric_var_name='mg',
parametric_var_id="parametric_var_1",
parametric_var_name="mg",
min_value=0.0,
max_value=1.0,
data_index=0,
)

expected_meta2 = HistoryMatchingMetadata.HMItem(
parametric_var_id='parametric_var_2',
parametric_var_name='mo',
parametric_var_id="parametric_var_2",
parametric_var_name="mo",
min_value=2.5,
max_value=7.5,
data_index=1,
)
assert items_meta['parametric_var_1'] == expected_meta1
assert items_meta['parametric_var_2'] == expected_meta2

assert items_meta["parametric_var_1"] == expected_meta1
assert items_meta["parametric_var_2"] == expected_meta2

# Result file still being created, metadata should be empty.
creating_file = hm_results_dir / "result.creating"
creating_file.touch()

metadata = read_history_matching_metadata(hm_results_dir)
assert metadata.result_directory == hm_results_dir
assert metadata.hm_items == {}

creating_file.unlink()

# Non-existent result directory, metadata should be empty.
unexistent_result_dir = Path('foo/bar')
unexistent_result_dir = Path("foo/bar")
metadata = read_history_matching_metadata(unexistent_result_dir)
assert metadata.result_directory == unexistent_result_dir
assert metadata.hm_items == {}


@pytest.mark.parametrize('hm_type', ('probabilistic', 'deterministic'))
@pytest.mark.parametrize("hm_type", ("probabilistic", "deterministic"))
def test_read_history_matching_result_data(
hm_probabilistic_results_dir: Path,
hm_deterministic_results_dir: Path,
hm_type: Literal['probabilistic', 'deterministic'],
hm_type: Literal["probabilistic", "deterministic"],
) -> None:
"""
Check reading the result of both HM type analysis. Both results are available simultaneously by
the means of the fixtures, but only one is used at a time.
"""
# Setup.
if hm_type == 'probabilistic':
if hm_type == "probabilistic":
expected_results = ([0.1, 0.22, 1.0, 0.8, 0.55], [3.0, 6.0, 5.1, 4.7, 6.3])
results_dir = hm_probabilistic_results_dir
else:
assert hm_type == 'deterministic'
assert hm_type == "deterministic"
expected_results = (0.1, 3.2)
results_dir = hm_deterministic_results_dir

metadata = read_history_matching_metadata(results_dir)

# Read the result of a single parametric var entry.
result = read_history_matching_result(
metadata, hm_type=hm_type, hm_result_key='parametric_var_1'
metadata, hm_type=hm_type, hm_result_key="parametric_var_1"
)
assert len(result) == 1
assert result['parametric_var_1'] == pytest.approx(expected_results[0])
assert result["parametric_var_1"] == pytest.approx(expected_results[0])

# Read the result of all entries.
result = read_history_matching_result(metadata, hm_type=hm_type)
assert len(result) == 2
assert result['parametric_var_1'] == pytest.approx(expected_results[0])
assert result['parametric_var_2'] == pytest.approx(expected_results[1])
assert result["parametric_var_1"] == pytest.approx(expected_results[0])
assert result["parametric_var_2"] == pytest.approx(expected_results[1])

# Unexistent result key, result should be empty.
result = read_history_matching_result(metadata, hm_type=hm_type, hm_result_key='foo')
result = read_history_matching_result(
metadata, hm_type=hm_type, hm_result_key="foo"
)
assert result == {}

# Result still being created, result should be empty.
creating_file = results_dir / "result.creating"
creating_file.touch()

result = read_history_matching_result(metadata, hm_type=hm_type)
assert result == {}

creating_file.unlink()

0 comments on commit 23c07ef

Please sign in to comment.