Skip to content

Commit

Permalink
Fixed black
Browse files Browse the repository at this point in the history
Co-authored-by: harry <[email protected]>
Co-authored-by: Erika Nesse <[email protected]>
Co-authored-by: Debajyoti Debnath <[email protected]>
Co-authored-by: KKaempen <[email protected]>
Co-authored-by: Alan Pinkert <[email protected]>
  • Loading branch information
6 people committed Nov 14, 2023
1 parent ff5c17c commit 95a3dad
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 43 deletions.
63 changes: 35 additions & 28 deletions rules-engine/src/rules_engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
)


def getOutputsNaturalGas(summaryInput: SummaryInput, dhwInput: Optional[DhwInput], naturalGasBillingInput: NaturalGasBillingInput)->(SummaryOutput, BalancePointGraph):

def getOutputsNaturalGas(
summaryInput: SummaryInput,
dhwInput: Optional[DhwInput],
naturalGasBillingInput: NaturalGasBillingInput,
) -> (SummaryOutput, BalancePointGraph):
# home = Home(summaryInput, naturalGasBillingInput, dhwInput, naturalGasBillingInput)
# home.calculate()
# return(home.summaryOutput, home.balancePointGraph)

pass
pass


def hdd(avg_temp: float, balance_point: float) -> float:
Expand Down Expand Up @@ -109,6 +112,7 @@ def max_heat_load(design_set_point: float, design_temp: float, ua: float) -> flo
"""
return (design_set_point - design_temp) * ua


class Home:
"""
Defines attributes and methods for calculating home heat metrics.
Expand Down Expand Up @@ -152,27 +156,26 @@ def _initialize_billing_periods(
# winter months 1; summer months -1; shoulder months 0
for i, usage in enumerate(usages):
billing_period = BillingPeriod(
avg_temps=temps[i],
usage=usage,
balance_point=self.balance_point,
inclusion_code=inclusion_codes[i]
avg_temps=temps[i],
usage=usage,
balance_point=self.balance_point,
inclusion_code=inclusion_codes[i],
)
if inclusion_codes[i] == 1:
self.bills_winter.append( billing_period )
self.bills_winter.append(billing_period)
elif inclusion_codes[i] == -1:
self.bills_summer.append( billing_period )
self.bills_summer.append(billing_period)
else:
self.bills_shoulder.append( billing_period )
self.bills_shoulder.append(billing_period)

self._calculate_avg_summer_usage()
self._calculate_avg_non_heating_usage()
for billing_period in self.bills_winter:
self.initialize_ua(billing_period)


def _initialize_billing_periods_reworked(
self, billingperiods: NaturalGasBillingInput
) -> None:
) -> None:
"""
TODO
"""
Expand All @@ -185,29 +188,27 @@ def _initialize_billing_periods_reworked(
ngb_start_date = billingperiods.period_start_date
ngbs = billingperiods.records


# winter months 1; summer months -1; shoulder months 0
for i, usage in enumerate(usages):
billing_period = BillingPeriod(
avg_temps=temps[i],
usage=usage,
balance_point=self.balance_point,
inclusion_code=inclusion_codes[i]
avg_temps=temps[i],
usage=usage,
balance_point=self.balance_point,
inclusion_code=inclusion_codes[i],
)

if inclusion_codes[i] == 1:
self.bills_winter.append( billing_period )
self.bills_winter.append(billing_period)
elif inclusion_codes[i] == -1:
self.bills_summer.append( billing_period )
self.bills_summer.append(billing_period)
else:
self.bills_shoulder.append( billing_period )
self.bills_shoulder.append(billing_period)

self._calculate_avg_summer_usage()
self._calculate_avg_non_heating_usage()
for billing_period in self.bills_winter:
self.initialize_ua(billing_period)


def _calculate_avg_summer_usage(self) -> None:
"""
Calculate average daily summer usage
Expand Down Expand Up @@ -297,7 +298,7 @@ def _calculate_balance_point_and_ua(
self.uas, self.avg_ua, self.stdev_pct = uas_i, avg_ua_i, stdev_pct_i

self._refine_balance_point(next_balance_point_sensitivity)

def _refine_balance_point(self, balance_point_sensitivity: float) -> None:
"""
Tries different balance points plus or minus a given number
Expand Down Expand Up @@ -340,19 +341,25 @@ def _refine_balance_point(self, balance_point_sensitivity: float) -> None:
if len(directions_to_check) == 2:
directions_to_check.pop(-1)

def calculate(self,
def calculate(
self,
initial_balance_point_sensitivity: float = 2,
stdev_pct_max: float = 0.10,
max_stdev_pct_diff: float = 0.01,
next_balance_point_sensitivity: float = 0.5):
next_balance_point_sensitivity: float = 0.5,
):
"""
For this Home, calculates avg non heating usage and then the estimated balance point
For this Home, calculates avg non heating usage and then the estimated balance point
and UA coefficient for the home, removing UA outliers based on a normalized standard
deviation threshold.
"""
self._calculate_avg_non_heating_usage()
self._calculate_balance_point_and_ua(initial_balance_point_sensitivity,
stdev_pct_max, max_stdev_pct_diff, next_balance_point_sensitivity)
self._calculate_balance_point_and_ua(
initial_balance_point_sensitivity,
stdev_pct_max,
max_stdev_pct_diff,
next_balance_point_sensitivity,
)

def initialize_ua(self, billing_period: BillingPeriod) -> None:
"""
Expand Down Expand Up @@ -384,7 +391,7 @@ def __init__(
avg_temps: List[float],
usage: float,
balance_point: float,
inclusion_code: int
inclusion_code: int,
) -> None:
self.avg_temps = avg_temps
self.usage = usage
Expand Down
25 changes: 21 additions & 4 deletions rules-engine/src/rules_engine/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@

class FuelType(Enum):
"""Enum for fuel types. Values are BTU per usage"""

GAS = 100000
OIL = 139600
PROPANE = 91333


class SummaryInput(BaseModel):
"""From Summary Tab"""
#design_temperature_override: float

# design_temperature_override: float
living_area: float = Field(description="Summary!B10")
fuel_type: FuelType = Field(description="Summary!B11")
heating_system_efficiency: float = Field(description="Summary!B12")
Expand All @@ -28,52 +30,67 @@ class SummaryInput(BaseModel):


class DhwInput(BaseModel):
"""From DHW (Domestic Hot Water) Tab """
"""From DHW (Domestic Hot Water) Tab"""

number_of_occupants: int = Field(description="DHW!B4")
estimated_water_heating_efficiency: float = Field(description="DHW!B5")
stand_by_losses: float = Field(description="DHW!B6")


class OilPropaneBillingInput(BaseModel):
"""From Oil-Propane tab"""

period_end_date: date = Field(description="Oil-Propane!B")
gallons: float = Field(description="Oil-Propane!C")
exclude: bool


class NaturalGasBillingRecordInput(BaseModel):
"""From Natural Gas tab. A single row of the Billing input table."""

period_end_date: date = Field(description="Natural Gas!B")
usage_therms: float = Field(description="Natural Gas!D")
exclude: bool


class NaturalGasBillingInput(BaseModel):
"""From Natural Gas tab. Container for holding all rows of the billing input table."""

period_start_date: date
records: List[NaturalGasBillingRecordInput]


class SummaryOutput(BaseModel):
"""From Summary tab"""
estimated_balance_point: float = Field(description="Summary!B20") # This is hand-calculated in the spreadsheet

estimated_balance_point: float = Field(
description="Summary!B20"
) # This is hand-calculated in the spreadsheet
other_fuel_usage: float = Field(description="Summary!B15")
average_indoor_temperature: float = Field(description="Summary!B24")
difference_between_ti_and_tbp: float = Field(description="Summary!B25")
design_temperature: float = Field(description="Summary!B26")
whole_home_heat_loss_rate: float = Field(description="Summary!B27") # UA = heat loss rate
whole_home_heat_loss_rate: float = Field(
description="Summary!B27"
) # UA = heat loss rate
standard_deviation_of_heat_loss_rate: float = Field(description="Summary!B28")
average_heat_load: float = Field(description="Summary!B29")
maximum_heat_load: float = Field(description="Summary!B30")


class BalancePointGraphRow(BaseModel):
"""From Summary page"""

balance_pt: float = Field(description="Summary!G33:35")
ua: float = Field(description="Summary!H33:35")
change_in_ua: float = Field(description="Summary!I33:35")
pct_change: float = Field(description="Summary!J33:35")
std_dev: float = Field(description="Summary!K33:35")


class BalancePointGraph(BaseModel):
"""From Summary page"""

records: List[BalancePointGraphRow]


Expand Down
37 changes: 26 additions & 11 deletions rules-engine/tests/test_rules_engine/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,23 @@ def test_bp_ua_estimates():
setback_temperature = 60
setback_hours_per_day = 8
fuel_type = FuelType.GAS
summary_input = SummaryInput(living_area=living_area, fuel_type=fuel_type, heating_system_efficiency=heat_sys_efficiency, thermostat_set_point=thermostat_set_point, setback_temperature=setback_temperature,setback_hours_per_day=setback_hours_per_day)

summary_input = SummaryInput(
living_area=living_area,
fuel_type=fuel_type,
heating_system_efficiency=heat_sys_efficiency,
thermostat_set_point=thermostat_set_point,
setback_temperature=setback_temperature,
setback_hours_per_day=setback_hours_per_day,
)

home = engine.Home(
summary_input, daily_temps_lists, usages, inclusion_codes, initial_balance_point=58
summary_input,
daily_temps_lists,
usages,
inclusion_codes,
initial_balance_point=58,
)



home.calculate()

ua_1, ua_2, ua_3 = [bill.ua for bill in home.bills_winter]
Expand All @@ -84,8 +92,6 @@ def test_bp_ua_estimates():


def test_bp_ua_with_outlier():


daily_temps_lists = [
[41.7, 41.6, 32, 25.4],
[28, 29, 30, 29],
Expand All @@ -102,14 +108,23 @@ def test_bp_ua_with_outlier():
setback_temperature = 60
setback_hours_per_day = 8
fuel_type = FuelType.GAS
summary_input = SummaryInput(living_area=living_area, fuel_type=fuel_type, heating_system_efficiency=heat_sys_efficiency, thermostat_set_point=thermostat_set_point, setback_temperature=setback_temperature,setback_hours_per_day=setback_hours_per_day)

summary_input = SummaryInput(
living_area=living_area,
fuel_type=fuel_type,
heating_system_efficiency=heat_sys_efficiency,
thermostat_set_point=thermostat_set_point,
setback_temperature=setback_temperature,
setback_hours_per_day=setback_hours_per_day,
)

home = engine.Home(
summary_input, daily_temps_lists, usages, inclusion_codes, initial_balance_point=58
summary_input,
daily_temps_lists,
usages,
inclusion_codes,
initial_balance_point=58,
)


home.calculate()

ua_1, ua_2, ua_3 = [bill.ua for bill in home.bills_winter]
Expand Down

0 comments on commit 95a3dad

Please sign in to comment.