Skip to content

Commit

Permalink
address #537 (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrakenhoff committed Jul 10, 2023
2 parents cb06df7 + 16c4dd3 commit 676d76b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
8 changes: 4 additions & 4 deletions doc/benchmarks/noisemodel.ipynb

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion pastas/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ def simulate(

if sim.hasnans:
sim = sim.dropna()
msg = "Simulation contains Nan-values. Check stresses time series settings!"
msg = (
"Simulation contains NaN-values. Check if time series settings "
"are provided for each stress model "
"(e.g. `ps.StressModel(stress, settings='prec')`!"
)
self.logger.error(msg)
raise ValueError(msg)

Expand Down
5 changes: 5 additions & 0 deletions pastas/stats/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ def rsq(
return nan

w = _get_weights(err, weighted=weighted, max_gap=max_gap)
if len(w) != obs.index.size:
raise ValueError(
"Weights and observations time series have different lengths! "
"Check observation and simulation time series."
)
mu = average(obs.to_numpy(), weights=w)
rss = (w * err.to_numpy() ** 2.0).sum()
tss = (w * (obs.to_numpy() - mu) ** 2.0).sum()
Expand Down
56 changes: 37 additions & 19 deletions pastas/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
if settings["fill_nan"] == "drop":
raise UserWarning(
"The fill_nan setting 'drop' for a stress is not allowed "
"because the stress time series need to be equidistant. "
"because the stress time series need to be equidistant. "
"Please change this."
)
validate_stress(series)
Expand All @@ -91,8 +91,8 @@ def __init__(
"fill_nan": "interpolate",
"fill_before": None,
"fill_after": None,
"tmin": series.index.min(),
"tmax": series.index.max(),
"tmin": series.first_valid_index(),
"tmax": series.last_valid_index(),
"time_offset": pd.Timedelta(0),
}
self.metadata = {"x": 0.0, "y": 0.0, "z": 0.0, "projection": None}
Expand Down Expand Up @@ -292,10 +292,10 @@ def _sample_up(self, series: Series) -> Series:
success = False

if success:
logger.info("Time Series %s were sampled up using %s.", self.name, method)
logger.info("Time Series '%s' were sampled up using %s.", self.name, method)
else:
logger.warning(
"Time Series %s: User-defined option for sample_up %s is not "
"Time Series '%s': User-defined option for sample_up %s is not "
"supported",
self.name,
method,
Expand Down Expand Up @@ -348,14 +348,14 @@ def _sample_down(self, series: Series) -> Series:

if success:
logger.info(
"Time Series %s was sampled down to freq %s with method " "%s.",
"Time Series '%s' was sampled down to freq %s with method " "%s.",
self.name,
freq,
method,
)
else:
logger.warning(
"Time Series %s: User-defined option for sample down %s is not "
"Time Series '%s': User-defined option for sample down %s is not "
"supported",
self.name,
method,
Expand Down Expand Up @@ -383,14 +383,14 @@ def _fill_nan(self, series: Series) -> Series:

if success:
logger.info(
"Time Series %s: %s nan-value(s) was/were found and filled with: %s.",
"Time Series '%s': %s nan-value(s) was/were found and filled with: %s.",
self.name,
n,
method,
)
else:
logger.warning(
"Time Series %s: User-defined option for fill_nan %s is not supported.",
"Time Series '%s': User-defined option for fill_nan %s is not supported.",
self.name,
method,
)
Expand Down Expand Up @@ -422,7 +422,7 @@ def _fill_before(self, series: Series) -> Series:
mean_value = series.mean()
series = series.fillna(mean_value) # Default option
logger.info(
"Time Series %s was extended in the past to %s with the mean "
"Time Series '%s' was extended in the past to %s with the mean "
"value (%.2g) of the time series.",
self.name,
series.index.min(),
Expand All @@ -432,7 +432,7 @@ def _fill_before(self, series: Series) -> Series:
first_value = series.loc[series.first_valid_index()]
series = series.fillna(method="bfill") # Default option
logger.info(
"Time Series %s was extended in the past to %s with the first "
"Time Series '%s' was extended in the past to %s with the first "
"value (%.2g) of the time series.",
self.name,
series.index.min(),
Expand All @@ -441,15 +441,24 @@ def _fill_before(self, series: Series) -> Series:
elif isinstance(method, float):
series = series.fillna(method)
logger.info(
"Time Series %s was extended in the past to %s by adding %s "
"Time Series '%s' was extended in the past to %s by adding %s "
"values.",
self.name,
series.index.min(),
method,
)
elif method is None:
msg = (
f"Time Series '{self.name}': cannot be extended into past to"
f" {series.index.min()} as 'fill_before' method is 'None'. "
"Provide settings to stress model, e.g. "
"`ps.StressModel(stress, settings='prec')`."
)
logger.error(msg)
raise ValueError(msg)
else:
logger.info(
"Time Series %s: User-defined option for fill_before '%s' is not "
"Time Series '%s': User-defined option for fill_before '%s' is not "
"supported.",
self.name,
method,
Expand Down Expand Up @@ -482,7 +491,7 @@ def _fill_after(self, series: Series) -> Series:
mean_value = series.mean()
series = series.fillna(mean_value) # Default option
logger.info(
"Time Series %s was extended in the future to %s with the mean "
"Time Series '%s' was extended in the future to %s with the mean "
"value (%.2g) of the time series.",
self.name,
series.index.max(),
Expand All @@ -492,7 +501,7 @@ def _fill_after(self, series: Series) -> Series:
last_value = series.loc[series.last_valid_index()]
series = series.fillna(method="ffill")
logger.info(
"Time Series %s was extended in the future to %s with the last "
"Time Series '%s' was extended in the future to %s with the last "
"value (%.2g) of the time series.",
self.name,
series.index.max(),
Expand All @@ -501,15 +510,24 @@ def _fill_after(self, series: Series) -> Series:
elif isinstance(method, float):
series = series.fillna(method)
logger.info(
"Time Series %s was extended in the future to %s by adding %s "
"Time Series '%s' was extended in the future to %s by adding %s "
"values.",
self.name,
series.index.max(),
method,
)
elif method is None:
msg = (
f"Time Series '{self.name}': cannot be extended into future to"
f" {series.index.max()} as 'fill_after' method is 'None'. "
"Provide settings to stress model, e.g. "
"`ps.StressModel(stress, settings='prec')`."
)
logger.error(msg)
raise ValueError(msg)
else:
logger.info(
"Time Series %s: User-defined option for fill_after '%s' is not "
logger.warning(
"Time Series '%s': User-defined option for fill_after '%s' is not "
"supported",
self.name,
method,
Expand Down Expand Up @@ -696,7 +714,7 @@ def _validate_series(series: Series, equidistant: bool = True):
# 7. Make sure the time series has no nan-values
if series.hasnans:
msg = (
"The time series %s has nan-values. Pastas will use the fill_nan "
"The Time Series '%s' has nan-values. Pastas will use the fill_nan "
"settings to fill up the nan-values."
)
logger.warning(msg, name)
Expand Down

0 comments on commit 676d76b

Please sign in to comment.