Skip to content

Inverse Model

InverseModel

Source code in src/openstudio_hpxml_calibration/weather_normalization/inverse_model.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class InverseModel:
    def __init__(self, hpxml: HpxmlDoc, user_config: dict, building_id: str | None = None):
        """
        Initialize the InverseModel for weather normalization.

        Sets up regression models and bill data for each fuel type based on the HPXML document.

        :param hpxml: HPXML document object.
        :type hpxml: HpxmlDoc
        :param user_config: Optional user configuration dictionary.
        :type user_config: dict, optional
        """
        self.user_config = user_config
        self.hpxml = hpxml
        self.building_id = building_id
        self.bills_by_fuel_type, self.bill_units, self.tz = ud.get_bills_from_hpxml(
            hpxml, building_id
        )
        self.bills_weather_by_fuel_type_in_btu = {}
        self.lat_lon = hpxml.get_lat_lon()
        self.regression_models: dict[FuelType, UtilityBillRegressionModel] = {}
        for fuel_type, bills in self.bills_by_fuel_type.items():
            bills_weather, _ = ud.join_bills_weather(bills, *self.lat_lon)
            for col in ["consumption", "daily_consumption"]:
                bills_weather[col] = convert_hpxml_energy_units(
                    bills_weather[col],
                    self.bill_units[fuel_type],
                    EnergyUnitType.BTU,
                    fuel_type,
                )
            self.bills_weather_by_fuel_type_in_btu[fuel_type] = bills_weather

    def get_model(self, fuel_type: FuelType) -> UtilityBillRegressionModel:
        """
        Retrieve or fit the regression model for a given fuel type.

        This method returns the regression model for the specified fuel type, fitting it if necessary
        using the bill and weather data from the HPXML document.

        :param fuel_type: The fuel type for which to retrieve or fit the regression model.
        :type fuel_type: FuelType
        :return: The fitted regression model for the specified fuel type.
        :rtype: UtilityBillRegressionModel
        """
        try:
            return self.regression_models[fuel_type]
        except KeyError:
            bills_weather = self.bills_weather_by_fuel_type_in_btu[fuel_type]
            fuel_types = self.hpxml.get_fuel_types()
            conditioning_fuels = fuel_types["heating"] | fuel_types["cooling"]
            model = fit_model(
                bills_weather,
                cvrmse_requirement=self.user_config["acceptance_criteria"][
                    "bill_regression_max_cvrmse"
                ],
                conditioning_fuels=conditioning_fuels,
                fuel_type=fuel_type,
            )
            self.regression_models[fuel_type] = model
            return model

    def predict_epw_daily(self, fuel_type: FuelType) -> pd.Series:
        """
        Predict daily energy consumption using the regression model for a given fuel type.

        Uses the fitted regression model to estimate daily consumption for each day in the EPW weather file.

        :param fuel_type: The fuel type for which to predict daily consumption.
        :type fuel_type: FuelType
        :return: Array of predicted daily consumption values.
        :rtype: np.ndarray
        """
        model = self.get_model(fuel_type)
        epw, _ = self.hpxml.get_epw_data(coerce_year=2007)
        epw_daily_avg_temp = epw["temp_air"].groupby(pd.Grouper(freq="D")).mean() * 1.8 + 32
        daily_predicted_fuel_use = model.predict_disaggregated(epw_daily_avg_temp.to_numpy())
        return daily_predicted_fuel_use

__init__(hpxml, user_config, building_id=None)

Initialize the InverseModel for weather normalization.

Sets up regression models and bill data for each fuel type based on the HPXML document.

Parameters:

Name Type Description Default
hpxml HpxmlDoc

HPXML document object.

required
user_config dict

Optional user configuration dictionary.

required
Source code in src/openstudio_hpxml_calibration/weather_normalization/inverse_model.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(self, hpxml: HpxmlDoc, user_config: dict, building_id: str | None = None):
    """
    Initialize the InverseModel for weather normalization.

    Sets up regression models and bill data for each fuel type based on the HPXML document.

    :param hpxml: HPXML document object.
    :type hpxml: HpxmlDoc
    :param user_config: Optional user configuration dictionary.
    :type user_config: dict, optional
    """
    self.user_config = user_config
    self.hpxml = hpxml
    self.building_id = building_id
    self.bills_by_fuel_type, self.bill_units, self.tz = ud.get_bills_from_hpxml(
        hpxml, building_id
    )
    self.bills_weather_by_fuel_type_in_btu = {}
    self.lat_lon = hpxml.get_lat_lon()
    self.regression_models: dict[FuelType, UtilityBillRegressionModel] = {}
    for fuel_type, bills in self.bills_by_fuel_type.items():
        bills_weather, _ = ud.join_bills_weather(bills, *self.lat_lon)
        for col in ["consumption", "daily_consumption"]:
            bills_weather[col] = convert_hpxml_energy_units(
                bills_weather[col],
                self.bill_units[fuel_type],
                EnergyUnitType.BTU,
                fuel_type,
            )
        self.bills_weather_by_fuel_type_in_btu[fuel_type] = bills_weather

get_model(fuel_type)

Retrieve or fit the regression model for a given fuel type.

This method returns the regression model for the specified fuel type, fitting it if necessary using the bill and weather data from the HPXML document.

Parameters:

Name Type Description Default
fuel_type FuelType

The fuel type for which to retrieve or fit the regression model.

required

Returns:

Type Description
UtilityBillRegressionModel

The fitted regression model for the specified fuel type.

Source code in src/openstudio_hpxml_calibration/weather_normalization/inverse_model.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def get_model(self, fuel_type: FuelType) -> UtilityBillRegressionModel:
    """
    Retrieve or fit the regression model for a given fuel type.

    This method returns the regression model for the specified fuel type, fitting it if necessary
    using the bill and weather data from the HPXML document.

    :param fuel_type: The fuel type for which to retrieve or fit the regression model.
    :type fuel_type: FuelType
    :return: The fitted regression model for the specified fuel type.
    :rtype: UtilityBillRegressionModel
    """
    try:
        return self.regression_models[fuel_type]
    except KeyError:
        bills_weather = self.bills_weather_by_fuel_type_in_btu[fuel_type]
        fuel_types = self.hpxml.get_fuel_types()
        conditioning_fuels = fuel_types["heating"] | fuel_types["cooling"]
        model = fit_model(
            bills_weather,
            cvrmse_requirement=self.user_config["acceptance_criteria"][
                "bill_regression_max_cvrmse"
            ],
            conditioning_fuels=conditioning_fuels,
            fuel_type=fuel_type,
        )
        self.regression_models[fuel_type] = model
        return model

predict_epw_daily(fuel_type)

Predict daily energy consumption using the regression model for a given fuel type.

Uses the fitted regression model to estimate daily consumption for each day in the EPW weather file.

Parameters:

Name Type Description Default
fuel_type FuelType

The fuel type for which to predict daily consumption.

required

Returns:

Type Description
np.ndarray

Array of predicted daily consumption values.

Source code in src/openstudio_hpxml_calibration/weather_normalization/inverse_model.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def predict_epw_daily(self, fuel_type: FuelType) -> pd.Series:
    """
    Predict daily energy consumption using the regression model for a given fuel type.

    Uses the fitted regression model to estimate daily consumption for each day in the EPW weather file.

    :param fuel_type: The fuel type for which to predict daily consumption.
    :type fuel_type: FuelType
    :return: Array of predicted daily consumption values.
    :rtype: np.ndarray
    """
    model = self.get_model(fuel_type)
    epw, _ = self.hpxml.get_epw_data(coerce_year=2007)
    epw_daily_avg_temp = epw["temp_air"].groupby(pd.Grouper(freq="D")).mean() * 1.8 + 32
    daily_predicted_fuel_use = model.predict_disaggregated(epw_daily_avg_temp.to_numpy())
    return daily_predicted_fuel_use