コード例 #1
0
def test_probabilistic_units_data_object_matching(
        prob_forecasts, single_observation):
    params = prob_forecasts.to_dict()
    assert 'constant_value_units' not in params
    params['variable'] = 'ghi'
    params['axis'] = 'x'

    pfx = datamodel.ProbabilisticForecast.from_dict(params)
    datamodel.ForecastObservation(pfx, single_observation)
コード例 #2
0
    def process_report_dict(self, rep_dict):
        """
        Load parameters from rep_dict into a Report object, getting forecasts
        and observations as necessary

        Parameters
        ----------
        rep_dict : dict
            Report dictionary as posted to the the API. See the API schema for
            details

        Returns
        -------
        datamodel.Report
        """
        rep_params = rep_dict['report_parameters'].copy()
        req_dict = {}
        for key in ('report_id', 'status', 'provider'):
            req_dict[key] = rep_dict.get(key, '')
        pairs = []
        for o in rep_params['object_pairs']:
            fx_type = o.get('forecast_type', 'forecast')
            fx_method = self._forecast_get_by_type(fx_type)
            fx = fx_method(o['forecast'])
            norm = o.get('normalization')
            unc = o.get('uncertainty')
            cost = o.get('cost')
            ref_fx = o.get('reference_forecast')
            if ref_fx is not None:
                ref_fx = fx_method(ref_fx)
            if 'observation' in o:
                obs = self.get_observation(o['observation'])
                pair = datamodel.ForecastObservation(fx,
                                                     obs,
                                                     normalization=norm,
                                                     uncertainty=unc,
                                                     reference_forecast=ref_fx,
                                                     cost=cost)
            elif 'aggregate' in o:
                agg = self.get_aggregate(o['aggregate'])
                pair = datamodel.ForecastAggregate(fx,
                                                   agg,
                                                   normalization=norm,
                                                   uncertainty=unc,
                                                   reference_forecast=ref_fx,
                                                   cost=cost)
            else:
                raise ValueError('must provide observation or aggregate in all'
                                 'object_pairs')
            pairs.append(pair)
        rep_params['object_pairs'] = tuple(pairs)
        req_dict['report_parameters'] = rep_params
        return datamodel.Report.from_dict(req_dict)
コード例 #3
0
def test_ForecastObservation_uncertainty_invalid(
        single_forecast, single_observation):
    with pytest.raises(ValueError):
        datamodel.ForecastObservation(
            single_forecast, single_observation, uncertainty='nope')
コード例 #4
0
def test_ForecastObservation_uncertainty(
        single_forecast, single_observation, uncertainty, expected):
    fxobs = datamodel.ForecastObservation(
        single_forecast, single_observation, uncertainty=uncertainty)
    assert fxobs.uncertainty == expected
コード例 #5
0
def pdid_params(request, many_sites, many_sites_text,
                single_observation, single_observation_text,
                single_site, single_forecast_text, single_forecast,
                single_event_observation_text, single_event_observation,
                single_event_forecast_text, single_event_forecast,
                prob_forecast_constant_value,
                prob_forecast_constant_value_text, prob_forecasts,
                prob_forecast_text, aggregate, aggregate_observations,
                aggregate_text, aggregate_forecast_text,
                aggregateforecast, aggregate_prob_forecast,
                aggregate_prob_forecast_text,
                agg_prob_forecast_constant_value,
                single_aggregate_observation,
                single_aggregate_observation_text, report_objects,
                report_dict, quality_filter, quality_filter_dict,
                timeofdayfilter, timeofdayfilter_dict, valuefilter,
                valuefilter_dict, metric_value_dict, metric_value,
                metric_result_dict, metric_result,
                validation_result_dict, validation_result,
                preprocessing_result_dict, preprocessing_result,
                plotly_report_figure_dict, plotly_report_figure,
                bokeh_report_figure_dict, bokeh_report_figure,
                report_message_dict, report_message,
                report_params_dict, report_params,
                constant_cost, timeofday_cost, datetime_cost,
                errorband_cost, banded_cost_params, cost_dicts):
    if request.param == 'site':
        return (many_sites[0], json.loads(many_sites_text)[0],
                datamodel.Site)
    elif request.param == 'fixed':
        return (many_sites[1].modeling_parameters,
                json.loads(many_sites_text)[1]['modeling_parameters'],
                datamodel.FixedTiltModelingParameters)
    elif request.param == 'single':
        return (many_sites[2].modeling_parameters,
                json.loads(many_sites_text)[2]['modeling_parameters'],
                datamodel.SingleAxisModelingParameters)
    elif request.param == 'observation':
        obs_dict = json.loads(single_observation_text)
        obs_dict['site'] = single_site
        return (single_observation, obs_dict,
                datamodel.Observation)
    elif request.param == 'forecast':
        fx_dict = json.loads(single_forecast_text)
        fx_dict['site'] = single_site
        return (single_forecast, fx_dict, datamodel.Forecast)
    elif request.param == 'eventobservation':
        obs_dict = json.loads(single_event_observation_text)
        obs_dict['site'] = single_site
        return (single_event_observation, obs_dict, datamodel.Observation)
    elif request.param == 'eventforecast':
        fx_dict = json.loads(single_event_forecast_text)
        fx_dict['site'] = single_site
        return (single_event_forecast, fx_dict, datamodel.EventForecast)
    elif request.param == 'probabilisticforecastconstantvalue':
        fx_dict = json.loads(prob_forecast_constant_value_text)
        fx_dict['site'] = single_site
        return (prob_forecast_constant_value, fx_dict,
                datamodel.ProbabilisticForecastConstantValue)
    elif request.param == 'probabilisticforecast':
        fx_dict = json.loads(prob_forecast_text)
        fx_dict['site'] = single_site
        fx_dict['constant_values'] = (prob_forecast_constant_value, )
        return (prob_forecasts, fx_dict, datamodel.ProbabilisticForecast)
    elif request.param == 'forecastobservation':
        fx_dict = json.loads(single_forecast_text)
        fx_dict['site'] = single_site
        obs_dict = json.loads(single_observation_text)
        obs_dict['site'] = single_site
        fxobs_dict = {'forecast': fx_dict, 'observation': obs_dict}
        fxobs = datamodel.ForecastObservation(
            single_forecast, single_observation)
        return (fxobs, fxobs_dict, datamodel.ForecastObservation)
    elif request.param == 'aggregate':
        agg_dict = json.loads(aggregate_text)
        agg_dict['observations'] = aggregate_observations
        return (aggregate, agg_dict, datamodel.Aggregate)
    elif request.param == 'aggregateforecast':
        aggfx_dict = json.loads(aggregate_forecast_text)
        aggfx_dict['aggregate'] = aggregate.to_dict()
        return (aggregateforecast, aggfx_dict, datamodel.Forecast)
    elif request.param == 'aggregateprobforecast':
        fx_dict = json.loads(aggregate_prob_forecast_text)
        fx_dict['aggregate'] = aggregate.to_dict()
        fx_dict['constant_values'] = (agg_prob_forecast_constant_value, )
        return (aggregate_prob_forecast, fx_dict,
                datamodel.ProbabilisticForecast)
    elif request.param == 'forecastaggregate':
        aggfx_dict = json.loads(aggregate_forecast_text)
        aggfx_dict['aggregate'] = aggregate.to_dict()
        agg_dict = json.loads(aggregate_text)
        agg_dict['observations'] = aggregate_observations
        fxobs_dict = {'forecast': aggfx_dict, 'aggregate': agg_dict}
        fxobs = datamodel.ForecastAggregate(aggregateforecast, aggregate)
        return (fxobs, fxobs_dict, datamodel.ForecastAggregate)
    elif request.param == 'aggregateobservation':
        aggobs_dict = json.loads(single_aggregate_observation_text)
        return (single_aggregate_observation, aggobs_dict,
                datamodel.AggregateObservation)
    elif request.param == 'report':
        report, *_ = report_objects
        return (report, report_dict.copy(), datamodel.Report)
    elif request.param == 'quality_filter':
        return (quality_filter, quality_filter_dict,
                datamodel.QualityFlagFilter)
    elif request.param == 'timeofdayfilter':
        return (timeofdayfilter, timeofdayfilter_dict,
                datamodel.TimeOfDayFilter)
    elif request.param == 'valuefilter':
        return (valuefilter, valuefilter_dict,
                datamodel.ValueFilter)
    elif request.param == 'metricvalue':
        return (metric_value, metric_value_dict, datamodel.MetricValue)
    elif request.param == 'metricresult':
        return (metric_result, metric_result_dict, datamodel.MetricResult)
    elif request.param == 'validationresult':
        return (validation_result, validation_result_dict,
                datamodel.ValidationResult)
    elif request.param == 'preprocessing_result':
        return (preprocessing_result, preprocessing_result_dict,
                datamodel.PreprocessingResult)
    elif request.param == 'reportparameters':
        return (report_params, report_params_dict,
                datamodel.ReportParameters)
    elif request.param == 'plotlyreportfigure':
        return (plotly_report_figure, plotly_report_figure_dict,
                datamodel.PlotlyReportFigure)
    elif request.param == 'bokehreportfigure':
        return (bokeh_report_figure, bokeh_report_figure_dict,
                datamodel.BokehReportFigure)
    elif request.param == 'reportmessage':
        return (report_message, report_message_dict, datamodel.ReportMessage)
    elif request.param == 'constantcost':
        return (constant_cost, cost_dicts['constant'], datamodel.ConstantCost)
    elif request.param == 'timeofdaycost':
        return (timeofday_cost, cost_dicts['timeofday'],
                datamodel.TimeOfDayCost)
    elif request.param == 'datetimecost':
        return (datetime_cost, cost_dicts['datetime'], datamodel.DatetimeCost)
    elif request.param == 'errorbandcost':
        return (errorband_cost, cost_dicts['errorband'],
                datamodel.ErrorBandCost)
    elif request.param == 'cost':
        return (banded_cost_params, cost_dicts['fullcost'], datamodel.Cost)
コード例 #6
0
def test_render_pdf_special_chars(
        ac_power_observation_metadata, ac_power_forecast_metadata, dash_url,
        fail_pdf, preprocessing_result_types, report_metrics):
    if shutil.which('pdflatex') is None:  # pragma: no cover
        pytest.skip('pdflatex must be on PATH to generate PDF reports')
    quality_flag_filter = datamodel.QualityFlagFilter(
        (
            "USER FLAGGED",
        )
    )
    forecast = ac_power_forecast_metadata.replace(
        name="ac_power forecast (why,)  ()'-_,")
    observation = ac_power_observation_metadata.replace(
        name="ac_power observations  ()'-_,")
    fxobs = datamodel.ForecastObservation(forecast,
                                          observation)
    tz = 'America/Phoenix'
    start = pd.Timestamp('20190401 0000', tz=tz)
    end = pd.Timestamp('20190404 2359', tz=tz)
    report_params = datamodel.ReportParameters(
        name="NREL MIDC OASIS GHI Forecast Analysis  ()'-_,",
        start=start,
        end=end,
        object_pairs=(fxobs,),
        metrics=("mae", "rmse", "mbe", "s"),
        categories=("total", "date", "hour"),
        filters=(quality_flag_filter,)
    )
    report = datamodel.Report(
        report_id="56c67770-9832-11e9-a535-f4939feddd83",
        report_parameters=report_params
    )
    qflags = list(
        f.quality_flags for f in report.report_parameters.filters if
        isinstance(f, datamodel.QualityFlagFilter)
    )
    qflags = list(qflags[0])
    ser_index = pd.date_range(
        start, end,
        freq=to_offset(forecast.interval_length),
        name='timestamp')
    ser = pd.Series(
        np.repeat(100, len(ser_index)), name='value',
        index=ser_index)
    pfxobs = datamodel.ProcessedForecastObservation(
        forecast.name,
        fxobs,
        forecast.interval_value_type,
        forecast.interval_length,
        forecast.interval_label,
        valid_point_count=len(ser),
        validation_results=tuple(datamodel.ValidationResult(
            flag=f, count=0) for f in qflags),
        preprocessing_results=tuple(datamodel.PreprocessingResult(
            name=t, count=0) for t in preprocessing_result_types),
        forecast_values=ser,
        observation_values=ser
    )

    figs = datamodel.RawReportPlots(
        (
            datamodel.PlotlyReportFigure.from_dict(
                {
                    'name': 'mae tucson ac_power',
                    'spec': '{"data":[{"x":[1],"y":[1],"type":"bar"}]}',
                    'pdf': fail_pdf,
                    'figure_type': 'bar',
                    'category': 'total',
                    'metric': 'mae',
                    'figure_class': 'plotly',
                }
            ),), '4.5.3',
    )
    raw = datamodel.RawReport(
        generated_at=report.report_parameters.end,
        timezone=tz,
        plots=figs,
        metrics=report_metrics(report),
        processed_forecasts_observations=(pfxobs,),
        versions=(('test',  'test_with_underscore?'),),
        messages=(datamodel.ReportMessage(
            message="Failed to make metrics for ac_power forecast ()'-_,",
            step='', level='', function=''),))
    rr = report.replace(raw_report=raw)
    rendered = template.render_pdf(rr, dash_url)
    assert rendered.startswith(b'%PDF')