def test_null_json_payload_to_forecast_series():
    forecast_values_text = b"""
{
  "_links": {
    "metadata": ""
  },
  "forecast_id": "OBSID",
  "values": [
    {
      "timestamp": "2019-01-01T12:00:00-0700",
      "value": null
    },
    {
      "timestamp": "2019-01-01T12:05:00-0700",
      "value": null
    }
  ]
}"""
    ind = pd.DatetimeIndex([
        pd.Timestamp("2019-01-01T19:00:00Z"),
        pd.Timestamp("2019-01-01T19:05:00Z")
    ],
                           name='timestamp')
    forecast_values = pd.Series([None, None],
                                index=ind,
                                dtype=float,
                                name='value')
    out = utils.json_payload_to_forecast_series(
        json.loads(forecast_values_text))
    pdt.assert_series_equal(out, forecast_values)
 def request_constant_value_values(cv):
     """Function to pass to ThreadPoolExecutor to make a request against
     the API while maintaining the Request/App context and the current user
     session.
     """
     return (str(cv['constant_value']),
             json_payload_to_forecast_series(
                 get_cdf_values(cv["forecast_id"], **kwargs)))
Example #3
0
def timeseries_adapter(type_, metadata, json_value_response):
    metadata = deepcopy(metadata)
    # ignores any modeling parameters as they aren't need for this
    site = datamodel.Site.from_dict(metadata['site'], raise_on_extra=False)
    metadata['site'] = site
    if type_ == 'forecast':
        obj = datamodel.Forecast.from_dict(metadata)
        data = io_utils.json_payload_to_forecast_series(json_value_response)
        return timeseries.generate_forecast_figure(obj,
                                                   data,
                                                   return_components=True)
    else:
        obj = datamodel.Observation.from_dict(metadata)
        data = io_utils.json_payload_to_observation_df(json_value_response)
        return timeseries.generate_observation_figure(obj,
                                                      data,
                                                      return_components=True)
Example #4
0
def timeseries_adapter(type_, metadata, json_value_response):
    metadata = deepcopy(metadata)
    # ignores any modeling parameters as they aren't need for this
    if 'site' in metadata:
        site = datamodel.Site.from_dict(metadata['site'], raise_on_extra=False)
        metadata['site'] = site
    elif 'aggregate' in metadata:
        # Patch aggregates so data model doesn't throw an error. We don't
        # need to load all of the observations when plotting forecasts.
        metadata['aggregate']['observations'] = []
    if type_ == 'forecast':
        obj = datamodel.Forecast.from_dict(metadata)
        data = io_utils.json_payload_to_forecast_series(json_value_response)
        return timeseries.generate_forecast_figure(obj,
                                                   data,
                                                   return_components=True,
                                                   limit=None)
    elif type_ == 'observation':
        obj = datamodel.Observation.from_dict(metadata)
        data = io_utils.json_payload_to_observation_df(json_value_response)
        return timeseries.generate_observation_figure(obj,
                                                      data,
                                                      return_components=True,
                                                      limit=None)
    elif type_ == 'probabilistic_forecast':
        cvs = []
        for constant_value in metadata['constant_values']:
            cv_dict = metadata.copy()
            cv_dict.update(constant_value)
            cvs.append(
                datamodel.ProbabilisticForecastConstantValue.from_dict(
                    cv_dict))
        metadata['constant_values'] = cvs
        obj = datamodel.ProbabilisticForecast.from_dict(metadata,
                                                        raise_on_extra=False)
        return timeseries.generate_probabilistic_forecast_figure(
            obj, json_value_response)
    else:
        # remove observations, we aren't using them for plotting aggregates
        metadata['observations'] = []
        obj = datamodel.Aggregate.from_dict(metadata, raise_on_extra=False)
        data = io_utils.json_payload_to_observation_df(json_value_response)
        return timeseries.generate_observation_figure(obj,
                                                      data,
                                                      return_components=True,
                                                      limit=None)
    def get_forecast_values(self,
                            forecast_id,
                            start,
                            end,
                            interval_label=None):
        """
        Get forecast values from start to end for forecast_id

        Parameters
        ----------
        forecast_id : string
            UUID of the forecast object
        start : timelike object
            Start of the interval to retrieve values for
        end : timelike object
            End of the interval
        interval_label : str or None
            If beginning, ending, adjust the data to return only data that is
            valid between start and end. If None or instant, return any data
            between start and end inclusive of the endpoints.

        Returns
        -------
        pandas.Series
           With the forecast values and a datetime index

        Raises
        ------
        ValueError
            If start or end cannot be converted into a Pandas Timestamp
        """
        req = self.get(f'/forecasts/single/{forecast_id}/values',
                       params={
                           'start': start,
                           'end': end
                       })
        out = json_payload_to_forecast_series(req.json())
        return adjust_timeseries_for_interval_label(out, interval_label, start,
                                                    end)
    def get_forecast_values(self, forecast_id, start, end):
        """
        Get forecast values from start to end for forecast_id

        Parameters
        ----------
        forecast_id : string
            UUID of the forecast object
        start : timelike object
            Start of the interval to retrieve values for
        end : timelike object
            End of the interval

        Returns
        -------
        pandas.Series
           With the forecast values and a datetime index
        """
        req = self.get(f'/forecasts/single/{forecast_id}/values',
                       params={
                           'start': start,
                           'end': end
                       })
        return json_payload_to_forecast_series(req.json())
def test_empty_payload_to_forecast_series():
    out = utils.json_payload_to_forecast_series({'values': []})
    assert isinstance(out.index, pd.DatetimeIndex)
def test_json_payload_to_forecast_series(forecast_values,
                                         forecast_values_text):
    out = utils.json_payload_to_forecast_series(
        json.loads(forecast_values_text))
    pdt.assert_series_equal(out, forecast_values)