Exemplo n.º 1
0
def test_load_beliefs(csv_file):
    """Test loading BeliefsDataFrame to csv.
    The saved file does not contain the sensor information, and the sources are saved by their name.
    Therefore, we test the following functionality:
    - The user should specify the sensor upon loading
    - The user should be warned that the loaded sources are not of type BeliefSource.
    - The user should have the possibility to look up the saved source names by passing a list of sources.
    """

    # Load beliefs with tb.read_csv
    df = pd.read_csv("test.csv")
    with pytest.warns(UserWarning, match="type other than BeliefSource"):
        df = tb.BeliefsDataFrame(df, sensor=tb.Sensor("Sensor Y"))
    assert df.sensor.name == "Sensor Y"

    # No lookup should issue warning
    with pytest.warns(UserWarning, match="type other than BeliefSource"):
        df = tb.read_csv("test.csv", sensor=tb.Sensor("Sensor Y"))
    assert all(
        c != tb.BeliefSource for c in df.index.get_level_values("source").map(type)
    )

    # This lookup should fail
    with pytest.raises(ValueError, match="not in list"):
        tb.read_csv(
            "test.csv",
            sensor=tb.Sensor("Sensor Y"),
            look_up_sources=[tb.BeliefSource(name="Source X")],
        )

    # This lookup should succeed
    source_a, source_b = tb.BeliefSource("Source A"), tb.BeliefSource("Source B")
    df = tb.read_csv(
        "test.csv", sensor=tb.Sensor("Sensor Y"), look_up_sources=[source_a, source_b]
    )
    assert df.sensor.name == "Sensor Y"
    assert source_a in df.index.get_level_values("source")
    assert source_b in df.index.get_level_values("source")
    assert isinstance(df.index.get_level_values("event_start")[0], datetime)
    assert isinstance(df.index.get_level_values("belief_time")[0], datetime)
Exemplo n.º 2
0
def csv_as_belief(csv_in,tz_hour_difference,n_events = None):
    sensor_descriptions = (
        ("Temperature", "°C"),
    )

    # Create source and sensors
    source_a = tb.BeliefSource(name="KNMI")
    sensors = (tb.Sensor(name=descr[0], unit=descr[1], event_resolution=timedelta(minutes=15)) for descr in sensor_descriptions)

    # Create BeliefsDataFrame
    for sensor in sensors:
        blfs = read_beliefs_from_csv(sensor,csv_in, source=source_a, tz_hour_difference=tz_hour_difference, n_events=n_events)
        df = tb.BeliefsDataFrame(sensor=sensor, beliefs=blfs).sort_index()
        #df['2015-05-16 09:14:01+00
    return df
Exemplo n.º 3
0
def make_df(n_events = 100, n_horizons = 169, tz_hour_difference=-9, event_resolution=timedelta(hours=1)):
    """
    Returns DataFrame in which n events and n horizons are stored
    @param n_events: int,number of events in DataFrame
    @param n_horizons: int,number of horizons in DataFrame
    @param tz_hour_difference: float,time difference
    @param event_resolution: timedelta object,event resolution
    """
    sensor_descriptions = (("Temperature", "°C"),)
    source = tb.BeliefSource(name="Random forest")
    sensors = (tb.Sensor(name=descr[0], unit=descr[1], event_resolution=event_resolution) for descr in sensor_descriptions)
    blfs=[]
    for sensor in sensors:
        blfs += read_beliefs_from_csv(sensor, source=source, cp=0.05, event_resolution=event_resolution, tz_hour_difference=tz_hour_difference)
        blfs += read_beliefs_from_csv(sensor, source=source, cp=0.5, event_resolution=event_resolution, tz_hour_difference=tz_hour_difference)
        blfs += read_beliefs_from_csv(sensor, source=source, cp=0.95, event_resolution=event_resolution, tz_hour_difference=tz_hour_difference)
        bdf = tb.BeliefsDataFrame(sensor=sensor, beliefs=blfs).sort_index()
    return bdf
def csv_as_belief(csv_in, tz_hour_difference, n_events=None):
    # Uncomment desired forecasting data one at a time
    sensor_descriptions = (
        # ("Solar irradiation", "kW/m²"),
        # ("Solar power", "kW"),
        # ("Wind speed", "m/s"),
        # ("Wind power", "MW"),
        ("Temperature", "°C"), )
    # Create source and sensors
    source_a = tb.BeliefSource(name="KNMI")
    sensors = (tb.Sensor(name=descr[0],
                         unit=descr[1],
                         event_resolution=timedelta(minutes=15))
               for descr in sensor_descriptions)
    # Create BeliefsDataFrame
    for sensor in sensors:
        blfs = read_beliefs_from_csv(sensor,
                                     csv_in,
                                     source=source_a,
                                     tz_hour_difference=tz_hour_difference,
                                     n_events=n_events)
        df = tb.BeliefsDataFrame(sensor=sensor, beliefs=blfs).sort_index()
    return df
    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


# Create source and sensors
source_a = tb.BeliefSource(name="KNMI")
sensors = (tb.Sensor(name=descr[0],
                     unit=descr[1],
                     event_resolution=timedelta(minutes=15))
           for descr in sensor_descriptions)

# Create BeliefsDataFrame
for sensor in sensors:
    blfs = read_beliefs_from_csv(sensor,
                                 source=source_a,
                                 tz_hour_difference=tz_hour_difference,
                                 n_events=n_events)
    df = tb.BeliefsDataFrame(sensor=sensor, beliefs=blfs).sort_index()
    #print(df)
    df.keys()
    #df['2015-05-16 09:14:01+00:00']
Exemplo n.º 6
0
import os
from datetime import timedelta

from timely_beliefs.examples.beliefs_data_frames import sixteen_probabilistic_beliefs
import timely_beliefs as tb

example_df = sixteen_probabilistic_beliefs()
temperature_df = tb.read_csv(
    os.path.dirname(os.path.abspath(__file__)) + "/temperature.csv",
    sensor=tb.Sensor("Thermometer A",
                     unit="°C",
                     event_resolution=timedelta(hours=1)),
    source=tb.BeliefSource("Source X"),
)
            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


source_a = tb.BeliefSource(name="Linear regression")
source_b = tb.BeliefSource(name="XGBoost")
source_c = tb.BeliefSource(name="Random forest")

sensors = (tb.Sensor(name=descr[0],
                     unit=descr[1],
                     event_resolution=event_resolution)
           for descr in sensor_descriptions)

for sensor in sensors:
    blfs = read_beliefs_from_csv(sensor,
                                 source=source_a,
                                 cp=0.5,
                                 event_resolution=event_resolution,
                                 tz_hour_difference=tz_hour_difference)
    blfs += read_beliefs_from_csv(sensor,
                                  source=source_b,
                                  cp=0.5,
                                  event_resolution=event_resolution,
                                  tz_hour_difference=tz_hour_difference)
    blfs += read_beliefs_from_csv(sensor,