Пример #1
0
def read_beliefs_from_csv(sensor, source, cp, event_resolution: timedelta, tz_hour_difference: float = 0) -> list:
    """
    Returns a timely_beliefs DataFrame read from a csv file
    @param sensor : beliefsensor
    @param source : BeliefSource
    @param cp : float, cummulative probability
    @param event_resolution : timedelta object, event resolution
    @param tz_hour_difference : float,time difference
    """
    sensor_descriptions = (("Temperature", "°C"),)

    cols = [0, 1]  # Columns with datetime index and observed values
    horizons = list(range(0, 169, 1))
    cols.extend([h + 2 for h in horizons])
    n_horizons = 169
    n_events = None
    beliefs = pd.read_csv("%s-%s-%s.csv" % (sensor.name.replace(' ', '_').lower(), source.name.replace(' ', '_').lower(), cp),
                          index_col=0, parse_dates=[0], date_parser=lambda col: pd.to_datetime(col, utc=True) - timedelta(hours=tz_hour_difference),
                          nrows=n_events, usecols=cols)
    beliefs = beliefs.resample(event_resolution).mean()
    assert beliefs.index.tzinfo == pytz.utc
    # Construct the BeliefsDataFrame by looping over the belief horizons
    blfs = load_time_series(beliefs.iloc[:, 0].head(n_events), sensor=sensor, source=source,
                            belief_horizon=timedelta(hours=0), cumulative_probability=0.5)  # load the observations (keep cp=0.5)
    for h in beliefs.iloc[:, 1 :n_horizons + 1] :
        try:
            blfs += load_time_series(beliefs[h].head(n_events), sensor=sensor, source=source,
                                     belief_horizon=(isodate.parse_duration(
                                     "PT%s" % h)) + event_resolution, cumulative_probability=cp)  # load the forecasts
        except isodate.isoerror.ISO8601Error:  # In case of old headers that don't yet follow the ISO 8601 standard
            blfs += load_time_series(beliefs[h].head(n_events), sensor=sensor, source=source,
                                     belief_horizon=(isodate.parse_duration(
                                     "%s" % h)) + event_resolution, cumulative_probability=cp)  # load the forecasts
    return blfs
def read_beliefs_from_csv(sensor,
                          csv_in,
                          source,
                          event_resolution: timedelta = None,
                          tz_hour_difference: float = 0,
                          n_events: int = None) -> list:
    beliefs = pd.read_csv(
        csv_in,
        index_col=0,
        parse_dates=[0],
        date_parser=lambda col: pd.to_datetime(col, utc=True) - timedelta(
            hours=tz_hour_difference),
        nrows=n_events,
        usecols=["datetime", sensor.name.replace(' ', '_').lower()])
    if event_resolution is not None:
        beliefs = beliefs.resample(event_resolution).mean()
    assert beliefs.index.tzinfo == pytz.utc
    # Construct the BeliefsDataFrame by looping over the belief horizons
    blfs = load_time_series(
        beliefs[sensor.name.replace(' ', '_').lower()],
        sensor=sensor,
        source=source,
        belief_horizon=timedelta(hours=0),
        cumulative_probability=0.5)  # load the observations (keep cp=0.5)
    return blfs
def read_beliefs_from_csv(sensor,
                          source,
                          cp,
                          event_resolution: timedelta,
                          tz_hour_difference: float = 0) -> list:
    beliefs = pd.read_csv("../%s-%s-%s.csv" % (sensor.name.replace(
        ' ', '_').lower(), source.name.replace(' ', '_').lower(), cp),
                          index_col=0,
                          parse_dates=[0],
                          date_parser=lambda col: pd.to_datetime(col, utc=True)
                          - timedelta(hours=tz_hour_difference),
                          nrows=n_events,
                          usecols=cols)
    beliefs = beliefs.resample(event_resolution).mean()
    assert beliefs.index.tzinfo == pytz.utc

    # Construct the BeliefsDataFrame by looping over the belief horizons
    blfs = load_time_series(
        beliefs.iloc[:, 0].head(n_events),
        sensor=sensor,
        source=source,
        belief_horizon=timedelta(hours=0),
        cumulative_probability=0.5)  # load the observations (keep cp=0.5)
    for h in beliefs.iloc[:, 1:n_horizons + 1]:
        try:
            blfs += load_time_series(
                beliefs[h].head(n_events),
                sensor=sensor,
                source=source,
                belief_horizon=(isodate.parse_duration("PT%s" % h)) +
                event_resolution,
                cumulative_probability=cp)  # load the forecasts
        except isodate.isoerror.ISO8601Error:  # In case of old headers that don't yet follow the ISO 8601 standard
            blfs += load_time_series(
                beliefs[h].head(n_events),
                sensor=sensor,
                source=source,
                belief_horizon=(isodate.parse_duration("%s" % h)) +
                event_resolution,
                cumulative_probability=cp)  # load the forecasts
    return blfs
Пример #4
0
 def append_from_time_series(
     self,
     event_value_series: pd.Series,
     source: BeliefSource,
     belief_horizon: timedelta,
     cumulative_probability: float = 0.5,
 ) -> "BeliefsDataFrame":
     """Append beliefs from time series entries into this BeliefsDataFrame. Sensor is assumed to be the same.
     Returns a new BeliefsDataFrame object."""
     beliefs = belief_utils.load_time_series(
         event_value_series,
         self.sensor,
         source,
         belief_horizon,
         cumulative_probability,
     )
     return self.append(
         BeliefsDataFrame(sensor=self.sensor, beliefs=beliefs))