-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make EnsembleConfig agnostic of response impls
- Loading branch information
Yngve S. Kristiansen
committed
Aug 27, 2024
1 parent
7935e45
commit 3edf135
Showing
8 changed files
with
160 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import ( | ||
Any, | ||
List, | ||
Optional, | ||
Sequence, | ||
) | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
from ._read_summary import read_summary | ||
from .parsing.config_dict import ConfigDict | ||
from .parsing.config_errors import ConfigValidationError | ||
from .parsing.config_keywords import ConfigKeys | ||
|
||
|
||
@dataclass(eq=False) | ||
class Refcase: | ||
start_date: datetime | ||
keys: List[str] | ||
dates: Sequence[datetime] | ||
values: npt.NDArray[Any] | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if not isinstance(other, Refcase): | ||
return False | ||
return bool( | ||
self.start_date == other.start_date | ||
and self.keys == other.keys | ||
and self.dates == other.dates | ||
and np.all(self.values == other.values) | ||
) | ||
|
||
@property | ||
def all_dates(self) -> List[datetime]: | ||
return [self.start_date] + list(self.dates) | ||
|
||
@classmethod | ||
def from_config_dict(cls, config_dict: ConfigDict) -> Optional["Refcase"]: | ||
data = None | ||
refcase_file_path = config_dict.get(ConfigKeys.REFCASE) # type: ignore | ||
if refcase_file_path is not None: | ||
try: | ||
start_date, refcase_keys, time_map, data = read_summary( | ||
refcase_file_path, ["*"] | ||
) | ||
except Exception as err: | ||
raise ConfigValidationError(f"Could not read refcase: {err}") from err | ||
|
||
return ( | ||
cls(start_date, refcase_keys, time_map, data) if data is not None else None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
|
||
from ert.config.gen_data_config import GenDataConfig | ||
from ert.config.responses_index import responses_index | ||
from ert.config.summary_config import SummaryConfig | ||
|
||
|
||
def test_adding_gendata_and_summary(): | ||
ri = responses_index | ||
|
||
# Manually reset it | ||
ri._items = {} | ||
|
||
assert [*ri.keys()] == [] | ||
assert [*ri.values()] == [] | ||
assert [*ri.items()] == [] | ||
|
||
ri.add_response_type(GenDataConfig) | ||
assert [*ri.keys()] == ["GenDataConfig"] | ||
assert [*ri.values()] == [GenDataConfig] | ||
assert [*ri.items()] == [("GenDataConfig", GenDataConfig)] | ||
|
||
with pytest.raises( | ||
KeyError, match="Response type with name GenDataConfig is already registered" | ||
): | ||
ri.add_response_type(GenDataConfig) | ||
|
||
ri.add_response_type(SummaryConfig) | ||
assert [*ri.keys()] == ["GenDataConfig", "SummaryConfig"] | ||
assert [*ri.values()] == [GenDataConfig, SummaryConfig] | ||
assert [*ri.items()] == [ | ||
("GenDataConfig", GenDataConfig), | ||
("SummaryConfig", SummaryConfig), | ||
] | ||
|
||
with pytest.raises( | ||
KeyError, match="Response type with name SummaryConfig is already registered" | ||
): | ||
ri.add_response_type(SummaryConfig) | ||
|
||
|
||
def test_adding_non_response_config(): | ||
ri = responses_index | ||
|
||
class NotAResponseConfig: | ||
pass | ||
|
||
with pytest.raises( | ||
ValueError, match="Response type must be subclass of ResponseConfig" | ||
): | ||
ri.add_response_type(NotAResponseConfig) |