Esempio n. 1
0
def test_project_generator(gsod_722880_2012_2014_weather_source,
                           tmy3_722880_weather_source):
    electricity_model = AverageDailyTemperatureSensitivityModel(cooling=True,
                                                                heating=True)
    gas_model = AverageDailyTemperatureSensitivityModel(cooling=False,
                                                        heating=True)
    electricity_param_distributions = {
        "cooling_slope": uniform(loc=1, scale=.5),
        "heating_slope": uniform(loc=1, scale=.5),
        "base_daily_consumption": uniform(loc=5, scale=5),
        "cooling_balance_temperature": uniform(loc=70, scale=5),
        "heating_balance_temperature": uniform(loc=60, scale=5)
    }
    electricity_param_delta_distributions = {
        "cooling_slope": uniform(loc=-.2, scale=.3),
        "heating_slope": uniform(loc=-.2, scale=.3),
        "base_daily_consumption": uniform(loc=-2, scale=3),
        "cooling_balance_temperature": uniform(loc=0, scale=0),
        "heating_balance_temperature": uniform(loc=0, scale=0)
    }
    gas_param_distributions = {
        "heating_slope": uniform(loc=1, scale=.5),
        "base_daily_consumption": uniform(loc=5, scale=5),
        "heating_balance_temperature": uniform(loc=60, scale=5)
    }
    gas_param_delta_distributions = {
        "heating_slope": uniform(loc=-.2, scale=.3),
        "base_daily_consumption": uniform(loc=-2, scale=3),
        "heating_balance_temperature": uniform(loc=0, scale=0)
    }

    generator = ProjectGenerator(electricity_model, gas_model,
                                 electricity_param_distributions,
                                 electricity_param_delta_distributions,
                                 gas_param_distributions,
                                 gas_param_delta_distributions)

    location = Location(station="722880")
    period = Period(datetime(2012, 1, 1), datetime(2013, 1, 1))
    baseline_period = Period(datetime(2012, 1, 1), datetime(2012, 4, 1))
    reporting_period = Period(datetime(2012, 5, 1), datetime(2013, 1, 1))

    results = generator.generate(location, period, period, baseline_period,
                                 reporting_period)

    project = results["project"]
    elec_data = project.consumption[0].data.values
    gas_data = project.consumption[1].data.values
    assert project.location == location
    assert results.get("electricity_estimated_savings") is not None
    assert results.get("natural_gas_estimated_savings") is not None
    assert results.get("electricity_pre_params") is not None
    assert results.get("natural_gas_pre_params") is not None
    assert results.get("electricity_post_params") is not None
    assert results.get("natural_gas_post_params") is not None

    assert len(elec_data) in range(9, 16)
    assert len(gas_data) in range(9, 16)
    assert elec_data[0] < 750  # could probably lower this upper bound
    assert gas_data[0] < 750  # could probably lower this upper bound
Esempio n. 2
0
def test_location_init(lat_lng_zipcode_station):
    lat_lng, zipcode, station = lat_lng_zipcode_station

    l = Location(station=station)
    assert_allclose(lat_lng, (l.lat, l.lng), rtol=RTOL, atol=ATOL)
    assert station == l.station
    assert zipcode == l.zipcode

    l = Location(lat_lng=lat_lng)
    assert_allclose(lat_lng, (l.lat, l.lng), rtol=RTOL, atol=ATOL)
    assert station == l.station
    assert zipcode == l.zipcode

    l = Location(zipcode=zipcode)
    assert_allclose(lat_lng, (l.lat, l.lng), rtol=RTOL, atol=ATOL)
    assert station == l.station
    assert zipcode == l.zipcode
Esempio n. 3
0
def test_project_attributes(generated_consumption_data_1):
    cd,params = generated_consumption_data_1
    baseline_period = Period(datetime(2014,1,1),datetime(2014,1,1))
    location = Location(zipcode="91104")
    project = Project(location,[cd,cd],baseline_period,None)
    meter = ProjectFuelTypes(project)
    result = meter.evaluate_raw(project=project)
    assert len(result["fuel_types"]) == 2
    assert result["fuel_types"][0]["value"] == "electricity"
    assert result["fuel_types"][0]["tags"] == ["electricity"]
    assert result["fuel_types"][1]["value"] == "electricity"
    assert result["fuel_types"][1]["tags"] == ["electricity"]
Esempio n. 4
0
def test_project_null_station():
    location = Location(zipcode="INVALID")

    with pytest.raises(ValueError):
        baseline_period = Period(datetime(2014,1,1), datetime(2015,1,1))
        reporting_period = None
        project = Project(location, consumption=[],
                baseline_period=baseline_period,
                reporting_period=reporting_period,
                other_periods=[],
                weather_source=None,
                weather_normal_source=None)
Esempio n. 5
0
def test_project_attributes(generated_consumption_data_1):
    cd,params = generated_consumption_data_1
    baseline_period = Period(datetime(2014,1,1),datetime(2014,1,1))
    location = Location(zipcode="91104")
    project = Project(location,[cd],baseline_period,None)
    meter = ProjectAttributes(project)
    result = meter.evaluate_raw(project=project)
    assert result["location"].zipcode == location.zipcode
    assert result["consumption"][0] is not None
    assert result["baseline_period"] is not None
    assert result["reporting_period"] is None
    assert result["other_periods"] == []
    assert result["weather_source"].station_id == "722880"
    assert result["weather_normal_source"].station_id == "722880"
Esempio n. 6
0
def test_project_consumption_baseline_reporting(generated_consumption_data_1):
    cd, _ = generated_consumption_data_1
    baseline_period = Period(datetime(2011,1,1),datetime(2013,6,1))
    reporting_period = Period(datetime(2013,6,1),datetime(2016,1,1))
    location = Location(zipcode="91104")
    project = Project(location,[cd],baseline_period,reporting_period)
    meter = ProjectConsumptionDataBaselineReporting()
    result = meter.evaluate_raw(project=project)
    assert result["consumption"][0]["value"].data.index[0] == datetime(2012,1,1)
    assert result["consumption"][0]["value"].data.index[17] == datetime(2013,5,25)
    assert result["consumption"][0]["tags"][0] == "electricity"
    assert result["consumption"][0]["tags"][1] == "baseline"
    assert result["consumption"][1]["value"].data.index[0] == datetime(2013,6,24)
    assert result["consumption"][1]["value"].data.index[18] == datetime(2014,12,16)
    assert result["consumption"][1]["tags"][0] == "electricity"
    assert result["consumption"][1]["tags"][1] == "reporting"
Esempio n. 7
0
def create_eemeter_project(project_row, consumption_data_rows):
    """
    Given a row a pd.DataFrame for projects
    and a set of rows for consumption data rows, 
    return eemeter project. 
    """
    location = Location(zipcode=project_row.zipcode,
                        lat_lng=(project_row.latitude, project_row.longitude),
                        station=project_row.weather_station)
    baseline_period = Period(project_row.baseline_period_start,
                             project_row.baseline_period_end)
    reporting_period = Period(project_row.reporting_period_start,
                              project_row.reporting_period_end)
    consumptions = _create_eemeter_consumptions(consumption_data_rows)
    project = Project(location, consumptions, baseline_period,
                      reporting_period)

    return project
Esempio n. 8
0
def create_project(params_e_pre, params_e_post, params_g_pre, params_g_post,
                   baseline_period_start_date, baseline_period_end_date,
                   reporting_period_start_date, reporting_period_end_date,
                   has_electricity, has_gas, weather_source, zipcode):

    model_e = AverageDailyTemperatureSensitivityModel(heating=True,
                                                      cooling=True)
    model_g = AverageDailyTemperatureSensitivityModel(heating=True,
                                                      cooling=False)

    # generate consumption
    baseline_period = Period(baseline_period_start_date,
                             reporting_period_start_date)
    datetimes_pre = generate_monthly_billing_datetimes(baseline_period,
                                                       dist=randint(29, 31))

    reporting_period = Period(datetimes_pre[-1], reporting_period_end_date)
    datetimes_post = generate_monthly_billing_datetimes(reporting_period,
                                                        dist=randint(29, 31))
    location = Location(zipcode=zipcode)
    baseline_period = Period(baseline_period_start_date,
                             baseline_period_end_date)
    reporting_period = Period(reporting_period_start_date,
                              reporting_period_end_date)

    cds = []

    if has_electricity:
        cd_e = generate_consumption_records(model_e, params_e_pre,
                                            params_e_post, datetimes_pre,
                                            datetimes_post, "electricity",
                                            "kWh", weather_source)
        cds.append(cd_e)

    if has_gas:
        cd_g = generate_consumption_records(model_g, params_g_pre,
                                            params_g_post, datetimes_pre,
                                            datetimes_post, "natural_gas",
                                            "therm", weather_source)
        cds.append(cd_g)

    return Project(location, cds, baseline_period, reporting_period)
Esempio n. 9
0
def test_project():
    location = Location(zipcode="60604")
    baseline_period = Period(datetime(2014,1,1), datetime(2015,1,1))
    reporting_period = None
    project = Project(location, consumption=[],
            baseline_period=baseline_period,
            reporting_period=reporting_period,
            other_periods=[],
            weather_source=None,
            weather_normal_source=None)

    assert project.consumption == []
    assert project.baseline_period.start == datetime(2014,1,1)
    assert project.baseline_period.end == datetime(2015,1,1)
    assert project.reporting_period is None
    assert project.other_periods == []
    assert project.weather_source.station_id == "725340"
    assert project.weather_normal_source.station_id == "725340"
    assert len(project.all_periods()) == 1
    assert project._total_date_range()[0] == datetime(2014,1,1)
    assert project._total_date_range()[1] == datetime(2015,1,1)
    assert len(project.segmented_consumption_data()) == 0
Esempio n. 10
0
def test_location_init_null():
    l = Location(zipcode="Invalid")
    assert l.lat is None
    assert l.lng is None
    assert l.station is None
    assert l.zipcode == "Invalid"
Esempio n. 11
0
def get_example_project(zipcode):

    # location
    location = Location(zipcode=zipcode)
    station = location.station
    weather_source = GSODWeatherSource(station,2011,2015)

    # model
    model_e = AverageDailyTemperatureSensitivityModel(cooling=True, heating=True)
    model_g = AverageDailyTemperatureSensitivityModel(cooling=False, heating=True)

    # model params
    params_e_b = {
        "cooling_slope": 1,
        "heating_slope": 1,
        "base_daily_consumption": 30,
        "cooling_balance_temperature": 73,
        "heating_balance_temperature": 68,
    }
    params_e_r = {
        "cooling_slope": .5,
        "heating_slope": .5,
        "base_daily_consumption": 15,
        "cooling_balance_temperature": 73,
        "heating_balance_temperature": 68,
    }
    params_g_b = {
        "heating_slope": .2,
        "base_daily_consumption": 2,
        "heating_balance_temperature": 68,
    }
    params_g_r = {
        "heating_slope": .1,
        "base_daily_consumption": 1,
        "heating_balance_temperature": 68,
    }

    #generators
    gen_e_b = MonthlyBillingConsumptionGenerator("electricity", "kWh", "degF",
            model_e, params_e_b)
    gen_e_r = MonthlyBillingConsumptionGenerator("electricity", "kWh", "degF",
            model_e, params_e_r)
    gen_g_b = MonthlyBillingConsumptionGenerator("natural_gas", "therm", "degF",
            model_g, params_g_b)
    gen_g_r = MonthlyBillingConsumptionGenerator("natural_gas", "therm", "degF",
            model_g, params_g_r)

    # time periods
    period = Period(datetime(2011,1,1,tzinfo=pytz.utc), datetime(2015,1,1,tzinfo=pytz.utc))
    datetimes = generate_monthly_billing_datetimes(period, dist=randint(30,31))

    # consumption data
    cd_e_b = gen_e_b.generate(weather_source, datetimes, daily_noise_dist=None)
    cd_e_r = gen_e_r.generate(weather_source, datetimes, daily_noise_dist=None)
    cd_g_b = gen_g_b.generate(weather_source, datetimes, daily_noise_dist=None)
    cd_g_r = gen_g_r.generate(weather_source, datetimes, daily_noise_dist=None)

    # periods
    periods = cd_e_b.periods()
    reporting_period = Period(datetime(2013,1,1,tzinfo=pytz.utc), datetime(2015,1,1,tzinfo=pytz.utc))
    baseline_period = Period(datetime(2011,1,1,tzinfo=pytz.utc), datetime(2013,1,1,tzinfo=pytz.utc))

    # records
    records_e = []
    records_g = []
    for e_b, e_r, g_b, g_r, p in zip(cd_e_b.data, cd_e_r.data, cd_g_b.data, cd_g_r.data, periods):
        e = e_r if p in reporting_period else e_b
        g = g_r if p in reporting_period else g_b
        record_e = {"start": p.start, "end": p.end, "value": e}
        record_g = {"start": p.start, "end": p.end, "value": g}
        records_e.append(record_e)
        records_g.append(record_g)

    # consumption_data
    cd_e = ConsumptionData(records_e, "electricity", "kWh",
            record_type="arbitrary")
    cd_g = ConsumptionData(records_g, "natural_gas", "therm",
            record_type="arbitrary")
    consumptions = [cd_e, cd_g]

    # project
    project = Project(location, consumptions, baseline_period, reporting_period)

    return project
Esempio n. 12
0
def test_default_residential_meter(default_residential_outputs_1,
        gsod_722880_2012_2014_weather_source, tmy3_722880_weather_source):
    elec_consumption_data, gas_consumption_data, \
            elec_params, gas_params, \
            elec_annualized_usage, gas_annualized_usage, \
            elec_gross_savings, gas_gross_savings, \
            elec_rmse, gas_rmse, \
            elec_r_squared, gas_r_squared, \
            elec_consumption_kWh_per_day, gas_consumption_kWh_per_day, \
            elec_consumption_n_days, gas_consumption_n_days, \
            temp_unit, retrofit_start_date, retrofit_completion_date, \
            cdd_tmy, hdd_tmy, total_cdd, total_hdd = \
            default_residential_outputs_1

    meter = DefaultResidentialMeter(temperature_unit_str=temp_unit)

    location = Location(station="722880")
    baseline_period = Period(datetime(2012,1,1,tzinfo=pytz.utc),retrofit_start_date)
    reporting_period = Period(retrofit_completion_date, datetime(2014,12,31,tzinfo=pytz.utc))
    project = Project(location, [elec_consumption_data, gas_consumption_data], baseline_period,
            reporting_period)

    data_collection = DataCollection(project=project)
    result = meter.evaluate(data_collection)

    assert_allclose(result.get_data('annualized_usage',
            tags=['electricity','baseline']).value, elec_annualized_usage,
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('annualized_usage',
            tags=['natural_gas','baseline']).value, gas_annualized_usage,
            rtol=RTOL, atol=ATOL)


    assert_allclose(result.get_data('annualized_usage',
            tags=['electricity','reporting']).value, elec_annualized_usage,
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('annualized_usage',
            tags=['natural_gas','reporting']).value, gas_annualized_usage,
            rtol=RTOL, atol=ATOL)


    assert_allclose(result.get_data('gross_savings',
            tags=['electricity']).value, elec_gross_savings,
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('gross_savings',
            tags=['natural_gas']).value, gas_gross_savings,
            rtol=RTOL, atol=ATOL)


    assert_allclose(result.get_data('average_daily_usages',
            tags=['electricity', 'baseline']).value, elec_consumption_kWh_per_day[:17],
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('average_daily_usages',
            tags=['natural_gas', 'baseline']).value, gas_consumption_kWh_per_day[:17],
            rtol=RTOL, atol=ATOL)


    assert_allclose(result.get_data('average_daily_usages',
            tags=['electricity', 'reporting']).value, elec_consumption_kWh_per_day[20:],
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('average_daily_usages',
            tags=['natural_gas', 'reporting']).value, gas_consumption_kWh_per_day[20:],
            rtol=RTOL, atol=ATOL)


    assert_allclose(result.get_data('cdd_tmy',
            tags=['electricity', 'baseline']).value, cdd_tmy,
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('cdd_tmy',
            tags=['natural_gas', 'baseline']).value, cdd_tmy,
            rtol=RTOL, atol=ATOL)


    assert_allclose(result.get_data('cdd_tmy',
            tags=['electricity', 'reporting']).value, cdd_tmy,
            rtol=RTOL, atol=ATOL)

    assert_allclose(result.get_data('cdd_tmy',
            tags=['natural_gas', 'reporting']).value, cdd_tmy,
            rtol=RTOL, atol=ATOL)


    elec_rmse_results = result.search('rmse', ['electricity'])
    assert elec_rmse_results.count() == 2
    for data_container in elec_rmse_results.iteritems():
        assert_allclose(data_container.value, elec_rmse, rtol=RTOL, atol=ATOL)

    gas_rmse_results = result.search('rmse', ['natural_gas'])
    assert gas_rmse_results.count() == 2
    for data_container in gas_rmse_results.iteritems():
        assert_allclose(data_container.value, gas_rmse, rtol=RTOL, atol=ATOL)

    elec_r_squared_results = result.search('r_squared', ['electricity'])
    assert elec_r_squared_results.count() == 2
    for data_container in elec_r_squared_results.iteritems():
        assert_allclose(data_container.value, elec_r_squared, rtol=RTOL, atol=ATOL)

    gas_r_squared_results = result.search('r_squared', ['natural_gas'])
    assert gas_r_squared_results.count() == 2
    for data_container in gas_r_squared_results.iteritems():
        assert_allclose(data_container.value, gas_r_squared, rtol=RTOL, atol=ATOL)