Skip to content

Commit

Permalink
stop storing stresses as list
Browse files Browse the repository at this point in the history
Finally after 5 years fix issue #441
  • Loading branch information
raoulcollenteur committed Jan 26, 2023
1 parent 622e9f7 commit 8ffe446
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
13 changes: 10 additions & 3 deletions pastas/io/base.py
Expand Up @@ -184,9 +184,16 @@ def _load_stressmodel(ts, data):

# Unpack the stress time series
if "stress" in ts.keys():
for i, stress in enumerate(ts["stress"]):
series, meta, setting = _unpack_series(stress)
ts["stress"][i] = series
# Only in the case of the wellmodel stresses are a list
if isinstance(ts["stress"], list):
for i, stress in enumerate(ts["stress"]):
series, meta, setting = _unpack_series(stress)
ts["stress"][i] = series
metadata.append(meta)
settings.append(setting)
else:
series, meta, setting = _unpack_series(ts["stress"])
ts["stress"] = series
metadata.append(meta)
settings.append(setting)

Expand Down
62 changes: 35 additions & 27 deletions pastas/stressmodels.py
Expand Up @@ -170,28 +170,6 @@ def update_stress(
if freq:
self.freq = freq

def dump_stress(self, series: bool = True) -> list:
"""Method to dump all stresses in the stresses list.
Parameters
----------
series: bool, optional
True if time series are to be exported, False if only the name
of the time series are needed. Settings are always exported.
Returns
-------
data: dict
dictionary with the dump of the stresses.
"""
data = []

for stress in self.stress:
stress.name = validate_name(stress.name, raise_error=True)
data.append(stress.to_dict(series=series))

return data

def get_stress(
self,
p: Optional[ArrayLike] = None,
Expand Down Expand Up @@ -320,7 +298,11 @@ def __init__(
raise_deprecations(meanstress=meanstress, cutoff=cutoff)

if isinstance(stress, list):
stress = stress[0] # TODO Temporary fix Raoul, 2017-10-24
logger.warning(
"providing a stress as list is deprecated and will results "
"in an Error in Pastas 1.0."
)
stress = stress[0] # TODO Remove in Pastas 1.0

stress = TimeSeries(stress, settings=settings, metadata=metadata)

Expand All @@ -338,7 +320,7 @@ def __init__(

self.gain_scale_factor = gain_scale_factor
self.freq = stress.settings["freq"]
self.stress = [stress]
self.stress.append(stress)
self.set_init_parameters()

def set_init_parameters(self) -> None:
Expand Down Expand Up @@ -401,7 +383,7 @@ def to_dict(self, series: bool = True) -> dict:
"rfunc": self.rfunc.to_dict(),
"name": self.name,
"up": self.rfunc.up,
"stress": self.dump_stress(series),
"stress": self.stress[0].to_dict(series=series),
"gain_scale_factor": self.gain_scale_factor,
}
return data
Expand Down Expand Up @@ -924,6 +906,28 @@ def get_parameters(self, model=None, istress: Optional[int] = None) -> ArrayLike
p_with_r = np.r_[p, distances]
return p_with_r

def dump_stress(self, series: bool = True) -> list:
"""Method to dump all stresses in the stresses list.
Parameters
----------
series: bool, optional
True if time series are to be exported, False if only the name
of the time series are needed. Settings are always exported.
Returns
-------
data: dict
dictionary with the dump of the stresses.
"""
data = []

for stress in self.stress:
stress.name = validate_name(stress.name, raise_error=True)
data.append(stress.to_dict(series=series))

return data

def to_dict(self, series: bool = True) -> dict:
"""Method to export the WellModel object.
Expand Down Expand Up @@ -1636,7 +1640,11 @@ def __init__(
raise_deprecations(cutoff=cutoff)

if isinstance(stress, list):
stress = stress[0] # TODO Temporary fix Raoul, 2017-10-24
logger.warning(
"providing a stress as list is deprecated and will results "
"in an Error in Pastas 1.0."
)
stress = stress[0] # TODO Remove in Pastas 1.0

stress = TimeSeries(stress, settings=settings, metadata=metadata)

Expand Down Expand Up @@ -1745,7 +1753,7 @@ def to_dict(self, series: bool = True):
"""
data = {
"stress": self.dump_stress(series=series),
"stress": self.stress[0].to_dict(series=series),
"rfunc1": self.rfunc1.to_dict(),
"rfunc2": self.rfunc2.to_dict(),
"name": self.name,
Expand Down

0 comments on commit 8ffe446

Please sign in to comment.