def test_serialize_roundtrip_frame_floats(prob_forecast_values):
    # check that roundtrip is not faithful if input columns is Float64Index
    prob_forecast_floats = prob_forecast_values.copy()
    prob_forecast_floats.columns = prob_forecast_floats.columns.astype(float)
    ser = utils.serialize_timeseries(prob_forecast_floats)
    out = utils.deserialize_timeseries(ser)
    pdt.assert_frame_equal(out, prob_forecast_values)
def test_load_report_values(raw_report, report_objects):
    _, obs, fx0, fx1, agg, fxagg = report_objects
    ser = pd.Series(np.random.random(10),
                    name='value',
                    index=pd.date_range(start='20200101',
                                        freq='5min',
                                        periods=10,
                                        tz='UTC',
                                        name='timestamp'))
    val = utils.serialize_timeseries(ser)
    vals = [{
        'id': id_,
        'processed_values': val
    } for id_ in (fx0.forecast_id, fx1.forecast_id, obs.observation_id,
                  agg.aggregate_id, fxagg.forecast_id)]
    inp = raw_report(False)
    out = utils.load_report_values(inp, vals)
    for fxo in out:
        pdt.assert_series_equal(fxo.forecast_values, ser)
        pdt.assert_series_equal(fxo.observation_values, ser)
Ejemplo n.º 3
0
    def post_raw_report_processed_data(self, report_id, raw_report):
        """
        Post the processed data that was used to make the report to the
        API.

        Parameters
        ----------
        report_id : str
            ID of the report to post values to
        raw_report : datamodel.RawReport
            The raw report object with processed_forecasts_observations

        Returns
        -------
        tuple
            of datamodel.ProcessedForecastObservation with `forecast_values`
            and `observations_values` replaced with report value IDs for later
            retrieval
        """
        posted_fxobs = []
        for fxobs in raw_report.processed_forecasts_observations:
            fx_data = {
                'object_id': fxobs.original.forecast.forecast_id,
                'processed_values': serialize_timeseries(fxobs.forecast_values)
            }
            fx_post = self.post(f'/reports/{report_id}/values',
                                json=fx_data,
                                headers={'Content-Type': 'application/json'})
            if isinstance(fxobs.original, datamodel.ForecastObservation):
                obj_id = fxobs.original.observation.observation_id
            else:
                obj_id = fxobs.original.aggregate.aggregate_id
            obs_data = {
                'object_id': obj_id,
                'processed_values':
                serialize_timeseries(fxobs.observation_values)
            }
            obs_post = self.post(f'/reports/{report_id}/values',
                                 json=obs_data,
                                 headers={'Content-Type': 'application/json'})
            if fxobs.original.reference_forecast is not None:
                ref_fx_data = {
                    'object_id':
                    fxobs.original.reference_forecast.forecast_id,
                    'processed_values':
                    serialize_timeseries(fxobs.reference_forecast_values)
                }
                ref_fx_post = self.post(
                    f'/reports/{report_id}/values',
                    json=ref_fx_data,
                    headers={'Content-Type': 'application/json'})
                processed_ref_fx_id = ref_fx_post.text
            else:
                processed_ref_fx_id = None
            processed_fx_id = fx_post.text
            processed_obs_id = obs_post.text
            new_fxobs = fxobs.replace(
                forecast_values=processed_fx_id,
                observation_values=processed_obs_id,
                reference_forecast_values=processed_ref_fx_id)
            posted_fxobs.append(new_fxobs)
        return tuple(posted_fxobs)
def test_serialize_roundtrip_frame(prob_forecast_values):
    ser = utils.serialize_timeseries(prob_forecast_values)
    out = utils.deserialize_timeseries(ser)
    pdt.assert_frame_equal(out, prob_forecast_values)
def test_serialize_roundtrip():
    ser = utils.serialize_timeseries(TEST_DATA['value'])
    out = utils.deserialize_timeseries(ser)
    pdt.assert_series_equal(out, TEST_DATA['value'])
def test_serialize_timeseries(inp, exp):
    out = utils.serialize_timeseries(inp)
    outd = json.loads(out)
    assert 'schema' in outd
    assert 'data' in outd
    assert out == exp
def test_serialize_timeseries(ser):
    out = utils.serialize_timeseries(ser)
    outd = json.loads(out)
    assert 'schema' in outd
    assert 'data' in outd