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 Jul 9, 2024
1 parent 5f60071 commit de4ceed
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 70 deletions.
123 changes: 93 additions & 30 deletions src/alfasim_sdk/result_reader/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import namedtuple
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Type
from typing import Any
from typing import Callable
from typing import DefaultDict
from typing import Dict
Expand All @@ -13,6 +13,7 @@
from typing import Literal
from typing import Optional
from typing import Tuple
from typing import Type
from typing import Union

import attr
Expand All @@ -22,9 +23,7 @@
from typing_extensions import Self

from alfasim_sdk.result_reader.aggregator_constants import (
GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME, UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME,
UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS, UNCERTAINTY_PROPAGATION_GROUP_NAME,
UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT, UNCERTAINTY_PROPAGATION_DSET_STD_RESULT,
GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import (
HISTORY_MATCHING_DETERMINISTIC_DSET_NAME,
Expand All @@ -42,6 +41,21 @@
from alfasim_sdk.result_reader.aggregator_constants import RESULT_FILE_PREFIX
from alfasim_sdk.result_reader.aggregator_constants import TIME_SET_DSET_NAME
from alfasim_sdk.result_reader.aggregator_constants import TRENDS_GROUP_NAME
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_DSET_STD_RESULT,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_GROUP_NAME,
)

OutputKeyType = str
"""\
Expand Down Expand Up @@ -106,6 +120,7 @@ class ResultsNeedFullReloadError(RuntimeError):
"restart file" and the option "keep old results" is selected.
"""


@attr.s(slots=True, hash=False)
class BaseUQMetaData:
"""
Expand All @@ -126,6 +141,7 @@ class BaseUQMetaData:
:ivar unit:
The unit of a dimensionless property (-, %)
"""

property_id: str = attr.ib(validator=attr.validators.instance_of(str))
trend_id: str = attr.ib(validator=attr.validators.instance_of(str))
category: str = attr.ib(validator=attr.validators.instance_of(str))
Expand Down Expand Up @@ -158,9 +174,12 @@ class UPItem(BaseUQMetaData):
:ivar sample_indexes:
The indexes to access each sample of an item.
"""

samples: int = attr.ib(validator=attr.validators.instance_of(int))
result_index: int = attr.ib(validator=attr.validators.instance_of(int))
sample_indexes: List[List[int]] = attr.ib(validator=attr.validators.instance_of(List))
sample_indexes: List[List[int]] = attr.ib(
validator=attr.validators.instance_of(List)
)

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> Self:
Expand All @@ -173,8 +192,8 @@ def from_dict(cls, data: Dict[str, Any]) -> Self:
position_unit=data["position_unit"],
unit=data["unit"],
samples=data["samples"],
result_index=data['result_index'],
sample_indexes=data['sample_indexes']
result_index=data["result_index"],
sample_indexes=data["sample_indexes"],
)

items: Dict[str, UPItem] = attr.ib(validator=attr.validators.instance_of(Dict))
Expand All @@ -188,7 +207,6 @@ def empty(cls, result_directory: Path) -> Self:

@classmethod
def get_metadata_from_dir(cls, result_directory: Path) -> Self:

def map_data(
up_metadata: Dict,
) -> Dict[str, UncertaintyPropagationAnalysesMetaData.UPItem]:
Expand All @@ -197,7 +215,13 @@ def map_data(
for key, data in up_metadata.items()
}

return read_results_file(result_directory=result_directory, metadata_class=cls, meta_data_attrs=UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME, map_data=map_data)
return read_results_file(
result_directory=result_directory,
metadata_class=cls,
meta_data_attrs=UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME,
map_data=map_data,
)


@attr.s(slots=True, hash=False)
class GlobalSensitivityAnalysisMetadata:
Expand All @@ -219,6 +243,7 @@ class GSAItem(BaseUQMetaData):
:ivar qoi_data_index:
The data index of a quantity of interest.
"""

parametric_var_id: str = attr.ib(validator=attr.validators.instance_of(str))
parametric_var_name: str = attr.ib(validator=attr.validators.instance_of(str))
qoi_index: Optional[int] = attr.ib(
Expand Down Expand Up @@ -269,25 +294,50 @@ def map_data(
key: GlobalSensitivityAnalysisMetadata.GSAItem.from_dict(data)
for key, data in gsa_metadata.items()
}
return read_results_file(result_directory=result_directory,metadata_class=cls, map_data=map_data,meta_data_attrs=GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME)

UQMetadataClass = Union[GlobalSensitivityAnalysisMetadata,UncertaintyPropagationAnalysesMetaData]
return read_results_file(
result_directory=result_directory,
metadata_class=cls,
map_data=map_data,
meta_data_attrs=GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME,
)


UQMetadataClass = Union[
GlobalSensitivityAnalysisMetadata, UncertaintyPropagationAnalysesMetaData
]


@attr.s(frozen=True)
class UPResult:
"""
Holder for each uncertainty propagation result.
"""
realization_output: List[np.ndarray] = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(List)))
mean_result: np.ndarray = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(np.ndarray)))
std_result: np.ndarray = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(np.ndarray)))
category: str = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(str)))
unit: str = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(str)))

def read_results_file(result_directory: Path, metadata_class: Type[UQMetadataClass], map_data: Callable, meta_data_attrs: str) -> UQMetadataClass:
with open_result_file(
result_directory, result_filename="result"
) as result_file:
realization_output: List[np.ndarray] = attr.ib(
validator=attr.validators.optional(attr.validators.instance_of(List))
)
mean_result: np.ndarray = attr.ib(
validator=attr.validators.optional(attr.validators.instance_of(np.ndarray))
)
std_result: np.ndarray = attr.ib(
validator=attr.validators.optional(attr.validators.instance_of(np.ndarray))
)
category: str = attr.ib(
validator=attr.validators.optional(attr.validators.instance_of(str))
)
unit: str = attr.ib(
validator=attr.validators.optional(attr.validators.instance_of(str))
)


def read_results_file(
result_directory: Path,
metadata_class: Type[UQMetadataClass],
map_data: Callable,
meta_data_attrs: str,
) -> UQMetadataClass:
with open_result_file(result_directory, result_filename="result") as result_file:
if not result_file:
return metadata_class.empty(result_directory=result_directory)

Expand All @@ -298,6 +348,7 @@ def read_results_file(result_directory: Path, metadata_class: Type[UQMetadataCla
items=map_data(loaded_metadata), result_directory=result_directory
)


@attr.s(slots=True, hash=False)
class HistoryMatchingMetadata:
"""
Expand Down Expand Up @@ -1770,9 +1821,10 @@ def read_global_sensitivity_analysis_meta_data(
result_directory=result_directory
)


def read_uncertainty_propagation_analyses_meta_data(
result_directory: Path,
)-> Optional[UncertaintyPropagationAnalysesMetaData]:
) -> Optional[UncertaintyPropagationAnalysesMetaData]:
"""
Read the uncertainty propagation analyses metadata persisted in a result file.
"""
Expand All @@ -1782,7 +1834,9 @@ def read_uncertainty_propagation_analyses_meta_data(
)


def read_uncertainty_propagation_results(metadata: UncertaintyPropagationAnalysesMetaData, results_key: str) -> Optional[UPResult]:
def read_uncertainty_propagation_results(
metadata: UncertaintyPropagationAnalysesMetaData, results_key: str
) -> Optional[UPResult]:
"""
Get the uncertainty propagation results.
Expand All @@ -1798,22 +1852,31 @@ def read_uncertainty_propagation_results(metadata: UncertaintyPropagationAnalyse

with open_result_file(metadata.result_directory) as file:
up_group = file[UNCERTAINTY_PROPAGATION_GROUP_NAME]
realization_output_samples = up_group[UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS]

realization_outputs = [realization_output_samples[sample_index][qoi_index] for qoi_index, sample_index in meta.sample_indexes]
mean_result = up_group[UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT][meta.result_index]
std_result = up_group[UNCERTAINTY_PROPAGATION_DSET_STD_RESULT][meta.result_index]
realization_output_samples = up_group[
UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS
]

realization_outputs = [
realization_output_samples[sample_index][qoi_index]
for qoi_index, sample_index in meta.sample_indexes
]
mean_result = up_group[UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT][
meta.result_index
]
std_result = up_group[UNCERTAINTY_PROPAGATION_DSET_STD_RESULT][
meta.result_index
]
return UPResult(
realization_output=realization_outputs,
mean_result=mean_result,
std_result=std_result,
category=meta.category,
unit=meta.unit
unit=meta.unit,
)


def read_based_uq_time_set(
result_directory: Path,
group_name: str
result_directory: Path, group_name: str
) -> Optional[numpy.array]:
"""
Get the time set for based uq analysis results.
Expand Down
2 changes: 1 addition & 1 deletion src/alfasim_sdk/result_reader/aggregator_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT = "uncertainty_propagation_mean_results"
UNCERTAINTY_PROPAGATION_DSET_STD_RESULT = "uncertainty_propagation_std_results"
UNCERTAINTY_PROPAGATION_GROUP_NAME = 'uncertainty_propagation'
UNCERTAINTY_PROPAGATION_GROUP_NAME = "uncertainty_propagation"

TIME_SET_DSET_NAME = "time_set"

Expand Down
60 changes: 48 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
from _pytest.monkeypatch import MonkeyPatch

from alfasim_sdk.result_reader.aggregator_constants import (
GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME, UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME, UNCERTAINTY_PROPAGATION_GROUP_NAME,
UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS, UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT,
UNCERTAINTY_PROPAGATION_DSET_STD_RESULT,
GLOBAL_SENSITIVITY_ANALYSIS_GROUP_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import (
HISTORY_MATCHING_DETERMINISTIC_DSET_NAME,
Expand All @@ -25,6 +23,21 @@
)
from alfasim_sdk.result_reader.aggregator_constants import META_GROUP_NAME
from alfasim_sdk.result_reader.aggregator_constants import TIME_SET_DSET_NAME
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_DSET_STD_RESULT,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME,
)
from alfasim_sdk.result_reader.aggregator_constants import (
UNCERTAINTY_PROPAGATION_GROUP_NAME,
)
from alfasim_sdk.result_reader.reader import Results


Expand Down Expand Up @@ -270,6 +283,7 @@ def global_sa_results_dir(datadir: Path) -> Path:
file.close()
return result_dir


@pytest.fixture()
def up_results_dir(datadir: Path) -> Path:
"""
Expand All @@ -285,8 +299,13 @@ def up_results_dir(datadir: Path) -> Path:
realization_outputs = np.array(
[
[
[i + (qoi_index*10) + tl for tl in (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7)]
for qoi_index, trend_property in enumerate([('trend_id_1','temperature'), ('trend_id_1','absolute_pressure')])
[
i + (qoi_index * 10) + tl
for tl in (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7)
]
for qoi_index, trend_property in enumerate(
[("trend_id_1", "temperature"), ("trend_id_1", "absolute_pressure")]
)
]
for i in range(5)
]
Expand All @@ -307,7 +326,7 @@ def up_results_dir(datadir: Path) -> Path:
"position_unit": "m",
"unit": "K",
"samples": 5,
"sample_indexes": [[0,0], [0,1], [0,2],[0,3],[0,4]],
"sample_indexes": [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]],
"result_index": 0,
},
"absolute_pressure@trend_id_1": {
Expand All @@ -319,28 +338,45 @@ def up_results_dir(datadir: Path) -> Path:
"position_unit": "m",
"unit": "bar",
"samples": 5,
"sample_indexes": [[1, 0],[1, 1], [1, 2], [1, 3],[1,4]],
"sample_indexes": [[1, 0], [1, 1], [1, 2], [1, 3], [1, 4]],
"result_index": 1,
},
}
meta_group.attrs[UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME] = json.dumps(fake_meta)
time_set_dset = uncertainty_propagation_group.create_dataset(name=TIME_SET_DSET_NAME, shape=time_set.shape)
meta_group.attrs[UNCERTAINTY_PROPAGATION_GROUP_META_ATTR_NAME] = json.dumps(
fake_meta
)
time_set_dset = uncertainty_propagation_group.create_dataset(
name=TIME_SET_DSET_NAME, shape=time_set.shape
)
time_set_dset[:] = time_set

realization_outputs_dset = uncertainty_propagation_group.create_dataset(name=UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS, shape=realization_outputs.shape, maxshape=(None, None, None))
realization_outputs_dset = uncertainty_propagation_group.create_dataset(
name=UNCERTAINTY_PROPAGATION_DSET_REALIZATION_OUTPUS,
shape=realization_outputs.shape,
maxshape=(None, None, None),
)
realization_outputs = realization_outputs
realization_outputs_dset[:] = realization_outputs

mean_results_array = np.mean(realization_outputs, axis=0)
mean_results = uncertainty_propagation_group.create_dataset(name=UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT, shape=mean_results_array.shape, maxshape=(None, None))
mean_results = uncertainty_propagation_group.create_dataset(
name=UNCERTAINTY_PROPAGATION_DSET_MEAN_RESULT,
shape=mean_results_array.shape,
maxshape=(None, None),
)
mean_results[:] = mean_results_array

std_results_array = np.std(realization_outputs, axis=0)
std_results = uncertainty_propagation_group.create_dataset(name=UNCERTAINTY_PROPAGATION_DSET_STD_RESULT, shape=std_results_array.shape, maxshape=(None, None))
std_results = uncertainty_propagation_group.create_dataset(
name=UNCERTAINTY_PROPAGATION_DSET_STD_RESULT,
shape=std_results_array.shape,
maxshape=(None, None),
)
std_results[:] = std_results_array
file.swmr_mode = True
return result_dir


def _create_and_populate_hm_result_file(
result_dir: Path,
result: np.ndarray,
Expand Down
Loading

0 comments on commit de4ceed

Please sign in to comment.