Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
out.update({'reference_forecast_id': processed_fx_obs.original.reference_forecast.forecast_id}) # NOQA: E501
# No data or metrics
if (not fx_fx_prob or
any([fx[0].empty or fx[1].empty for fx in fx_fx_prob])):
raise RuntimeError("Missing probabilistic forecast timeseries data.")
elif obs.empty:
raise RuntimeError("No observation timeseries data.")
elif len(metrics) == 0:
raise RuntimeError("No metrics specified.")
fx = processed_fx_obs.original.forecast
out['forecast_id'] = fx.forecast_id
# Determine proper metrics by instance type
if isinstance(fx, datamodel.ProbabilisticForecastConstantValue):
_metrics = list(set(metrics) - set(probabilistic._REQ_DIST))
elif isinstance(fx, datamodel.ProbabilisticForecast):
_metrics = list(set(metrics) & set(probabilistic._REQ_DIST))
# Compute metrics and create MetricResult
results = _calculate_probabilistic_metrics_from_df(
_create_prob_dataframe(obs, fx_fx_prob, ref_fx_fx_prob),
categories,
_metrics,
processed_fx_obs.original.forecast.interval_label)
out['values'] = _sort_metrics_vals(
results, datamodel.ALLOWED_PROBABILISTIC_METRICS)
result = datamodel.MetricResult.from_dict(out)
return result
valid between start and end. If None, return any data
between start and end inclusive of the endpoints.
Returns
-------
pandas.Series or pandas.DataFrame
With a datetime index. If DataFrame, (value, quality_flag)
columns
Raises
------
ValueError
If start or end cannot be converted into a Pandas Timestamp
""" # noqa: E501
# order avoids possible issues with inheritance
if isinstance(obj, datamodel.ProbabilisticForecastConstantValue):
f = self.get_probabilistic_forecast_constant_value_values
obj_id = obj.forecast_id
elif isinstance(obj, datamodel.ProbabilisticForecast):
f = self.get_probabilistic_forecast_values
obj_id = obj.forecast_id
elif isinstance(obj, datamodel.Forecast):
f = self.get_forecast_values
obj_id = obj.forecast_id
elif isinstance(obj, datamodel.Aggregate):
f = self.get_aggregate_values
obj_id = obj.aggregate_id
elif isinstance(obj, datamodel.Observation):
f = self.get_observation_values
obj_id = obj.observation_id
return f(obj_id, start, end, interval_label=interval_label)
reports can be properly rendered
"""
report_parameters: ReportParameters
raw_report: Union[None, RawReport] = None
status: str = 'pending'
report_id: str = ''
provider: str = ''
__version__: int = 0 # should add version to api
FORECAST_TYPE_MAPPING = {
'forecast': Forecast,
'event_forecast': EventForecast,
'probabilistic_forecast': ProbabilisticForecast,
'probabilistic_forecast_constant_value': ProbabilisticForecastConstantValue
}
def _name_pfxobs(current_names, forecast, i=1):
if isinstance(forecast, str):
forecast_name = forecast
else:
forecast_name = forecast.name
if isinstance(forecast, datamodel.ProbabilisticForecastConstantValue):
if forecast.axis == 'x':
forecast_name += \
f' Prob(x <= {forecast.constant_value} {forecast.units})'
else:
forecast_name += f' Prob(f <= x) = {forecast.constant_value}%'
if i > 99:
logger.warning(
'Limit of unique names for identically named forecasts reached.'
' Aligned pairs may have duplicate names.')
return forecast_name
if forecast_name in current_names:
if i == 1:
new_name = f'{forecast_name}-{i:02d}'
else:
new_name = f'{forecast_name[:-3]}-{i:02d}'
return _name_pfxobs(current_names, new_name, i + 1)
metrics : Tuple of str
Metrics to be computed in the report.
Returns
-------
None
Raises
------
ValueError
If the selected metrics are not valid for the given forecast type.
"""
if isinstance(fx, (ProbabilisticForecast,
ProbabilisticForecastConstantValue)):
if not set(metrics) <= ALLOWED_PROBABILISTIC_METRICS.keys():
raise ValueError("Metrics must be in "
"ALLOWED_PROBABILISTIC_METRICS.")
elif isinstance(fx, EventForecast):
if not set(metrics) <= ALLOWED_EVENT_METRICS.keys():
raise ValueError("Metrics must be in "
"ALLOWED_EVENT_METRICS.")
elif isinstance(fx, Forecast):
if not set(metrics) <= ALLOWED_DETERMINISTIC_METRICS.keys():
raise ValueError("Metrics must be in "
"ALLOWED_DETERMINISTIC_METRICS.")
def __set_constant_values__(self):
out = []
for cv in self.constant_values:
if isinstance(cv, ProbabilisticForecastConstantValue):
out.append(cv)
elif isinstance(cv, (float, int)):
cv_dict = self.to_dict()
cv_dict.pop('forecast_id', None)
cv_dict['constant_value'] = cv
out.append(
ProbabilisticForecastConstantValue.from_dict(cv_dict))
else:
raise TypeError(
f'Invalid type for a constant value {cv}. '
'Must be int, float, or ProbablisticConstantValue')
object.__setattr__(self, 'constant_values', tuple(out))