-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'ert-storage/main' into merge-ert-storage
- Loading branch information
Showing
22 changed files
with
459 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .misfits import calculate_misfits_from_pandas | ||
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,42 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any, Mapping, Sequence | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
if TYPE_CHECKING: | ||
import numpy.typing as npt | ||
|
||
|
||
def _calculate_misfit( | ||
obs_value: npt.NDArray[Any], | ||
response_value: npt.NDArray[Any], | ||
obs_std: npt.NDArray[Any], | ||
) -> Sequence[float]: | ||
difference = response_value - obs_value | ||
misfit = (difference / obs_std) ** 2 | ||
return (misfit * np.sign(difference)).tolist() | ||
|
||
|
||
def calculate_misfits_from_pandas( | ||
reponses_dict: Mapping[int, pd.DataFrame], | ||
observation: pd.DataFrame, | ||
summary_misfits: bool = False, | ||
) -> pd.DataFrame: | ||
""" | ||
Compute misfits from reponses_dict (real_id, values in dataframe) | ||
and observation | ||
""" | ||
misfits_dict = {} | ||
for realization_index in reponses_dict: | ||
misfits_dict[realization_index] = _calculate_misfit( | ||
observation["values"], | ||
reponses_dict[realization_index].loc[:, observation.index].values.flatten(), | ||
observation["errors"], | ||
) | ||
|
||
df = pd.DataFrame(data=misfits_dict, index=observation.index) | ||
if summary_misfits: | ||
df = pd.DataFrame([df.abs().sum(axis=0)], columns=df.columns, index=[0]) | ||
return df.T |
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
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,30 @@ | ||
from typing import Any | ||
|
||
from fastapi import status | ||
|
||
|
||
class ErtStorageError(RuntimeError): | ||
""" | ||
Base error class for all the rest of errors | ||
""" | ||
|
||
__status_code__ = status.HTTP_200_OK | ||
|
||
def __init__(self, message: str, **kwargs: Any): | ||
super().__init__(message, kwargs) | ||
|
||
|
||
class NotFoundError(ErtStorageError): | ||
__status_code__ = status.HTTP_404_NOT_FOUND | ||
|
||
|
||
class ConflictError(ErtStorageError): | ||
__status_code__ = status.HTTP_409_CONFLICT | ||
|
||
|
||
class ExpectationError(ErtStorageError): | ||
__status_code__ = status.HTTP_417_EXPECTATION_FAILED | ||
|
||
|
||
class UnprocessableError(ErtStorageError): | ||
__status_code__ = status.HTTP_422_UNPROCESSABLE_ENTITY |
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,11 @@ | ||
from .ensemble import EnsembleIn, EnsembleOut | ||
from .experiment import ExperimentIn, ExperimentOut | ||
from .observation import ( | ||
ObservationIn, | ||
ObservationOut, | ||
ObservationTransformationIn, | ||
ObservationTransformationOut, | ||
) | ||
from .prior import Prior | ||
from .record import RecordOut | ||
from .update import UpdateIn, UpdateOut |
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,37 @@ | ||
from typing import Any, List, Mapping, Optional | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel, Field, root_validator | ||
|
||
|
||
class _Ensemble(BaseModel): | ||
size: int | ||
parameter_names: List[str] | ||
response_names: List[str] | ||
active_realizations: List[int] = [] | ||
|
||
|
||
class EnsembleIn(_Ensemble): | ||
update_id: Optional[UUID] = None | ||
userdata: Mapping[str, Any] = {} | ||
|
||
@root_validator | ||
def _check_names_no_overlap(cls, values: Mapping[str, Any]) -> Mapping[str, Any]: | ||
""" | ||
Verify that `parameter_names` and `response_names` don't overlap. Ie, no | ||
record can be both a parameter and a response. | ||
""" | ||
if not set(values["parameter_names"]).isdisjoint(set(values["response_names"])): | ||
raise ValueError("parameters and responses cannot have a name in common") | ||
return values | ||
|
||
|
||
class EnsembleOut(_Ensemble): | ||
id: UUID | ||
children: List[UUID] = Field(alias="child_ensemble_ids") | ||
parent: Optional[UUID] = Field(alias="parent_ensemble_id") | ||
experiment_id: Optional[UUID] = None | ||
userdata: Mapping[str, Any] | ||
|
||
class Config: | ||
orm_mode = True |
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,24 @@ | ||
from typing import Any, Mapping, Sequence | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
from .prior import Prior | ||
|
||
|
||
class _Experiment(BaseModel): | ||
name: str | ||
|
||
|
||
class ExperimentIn(_Experiment): | ||
priors: Mapping[str, Prior] = {} | ||
|
||
|
||
class ExperimentOut(_Experiment): | ||
id: UUID | ||
ensemble_ids: Sequence[UUID] | ||
priors: Mapping[str, Mapping[str, Any]] | ||
userdata: Mapping[str, Any] | ||
|
||
class Config: | ||
orm_mode = True |
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,43 @@ | ||
from typing import Any, List, Mapping, Optional | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class _ObservationTransformation(BaseModel): | ||
name: str | ||
active: List[bool] | ||
scale: List[float] | ||
observation_id: UUID | ||
|
||
|
||
class ObservationTransformationIn(_ObservationTransformation): | ||
pass | ||
|
||
|
||
class ObservationTransformationOut(_ObservationTransformation): | ||
id: UUID | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
|
||
class _Observation(BaseModel): | ||
name: str | ||
errors: List[float] | ||
values: List[float] | ||
x_axis: List[Any] | ||
records: Optional[List[UUID]] = None | ||
|
||
|
||
class ObservationIn(_Observation): | ||
pass | ||
|
||
|
||
class ObservationOut(_Observation): | ||
id: UUID | ||
transformation: Optional[ObservationTransformationOut] = None | ||
userdata: Mapping[str, Any] = {} | ||
|
||
class Config: | ||
orm_mode = True |
Oops, something went wrong.