def test_age_classes_consecutive_open_range_min(): myAgeClasses = [ AgeClass(1900, 1918), AgeClass(None, 1933), # open range in between! AgeClass(1934, None) ] assert AgeClass.are_age_classes_consecutive(myAgeClasses) == False
def test_age_classes_consecutive_ok_fully_defined(): myAgeClasses = [ AgeClass(1900, 1918), AgeClass(1919, 1925), AgeClass(1926, 1933) ] assert AgeClass.are_age_classes_consecutive(myAgeClasses)
def test_age_classes_consecutive_overlap(): myAgeClasses = [ AgeClass(1900, 1918), AgeClass(1919, 1925), AgeClass(1922, None) # overlap here! ] assert AgeClass.are_age_classes_consecutive(myAgeClasses) == False
def test_age_classes_consecutive_ok(): myAgeClasses = [ AgeClass(None, 1918), AgeClass(1919, 1925), AgeClass(1926, None) ] assert AgeClass.are_age_classes_consecutive(myAgeClasses)
def test_with_full_retrofit_only(mock_ret_rates_cfg): ureg = cesarp.common.init_unit_registry() mock_ret_rates_cfg["ENERGY_PERSPECTIVE_2050"] = {"DO_PARTIAL_RETROFIT": False} containers_prev_sim = create_mock_containers_with_results(ureg) containers_current = create_mock_bldg_containers(ureg) retrofitter = EnergyPerspective2050BldgElementsRetrofitter(ureg, custom_config=mock_ret_rates_cfg) retrofitter._per_elem_construction_retrofitter = ConstrRetrofitterMock() retrofitter.retrofit_site(2020, containers_current, containers_prev_sim) print("\nSFH\n====\n") for x in retrofitter._retrofit_categories_last_run[BldgType.SFH]: print(f"{x.constr_ac}; {x.bldg_elems_to_retrofit}; {x.target_nr_of_bldgs_to_retrofit}; {x.nr_of_bldgs_retrofitted}") if x.constr_ac == AgeClass(min_age=None, max_age=1918): assert x.target_nr_of_bldgs_to_retrofit == 1 assert x.nr_of_bldgs_retrofitted == 1 if x.constr_ac == AgeClass(min_age=1991, max_age=1995): # two buildings in that age class, but retrofit only requires one to be retrofitted assert x.target_nr_of_bldgs_to_retrofit == 1 assert x.nr_of_bldgs_retrofitted == 1 print("\nMFH\n====\n") for x in retrofitter._retrofit_categories_last_run[BldgType.MFH]: print(f"{x.constr_ac}; {x.bldg_elems_to_retrofit}; {x.target_nr_of_bldgs_to_retrofit}; {x.nr_of_bldgs_retrofitted}") if x.constr_ac == AgeClass(min_age=1971, max_age=1980): # there are three bldg in that age class, with retrofit rate of 90% acutally 3 out of 3 buildings would # be retrofitted, but one had a retrofit in the last period and one reaches energy target, thus only one # builing gets retrofitted assert x.target_nr_of_bldgs_to_retrofit == 3 assert x.nr_of_bldgs_retrofitted == 1 if x.constr_ac == AgeClass(min_age=1996, max_age=2000): # there is a bldg in that age class, but retrofit rate too low assert x.target_nr_of_bldgs_to_retrofit == 0 assert x.nr_of_bldgs_retrofitted == 0
def test_with_partial_retrofit(mock_ret_rates_cfg): ureg = cesarp.common.init_unit_registry() mock_ret_rates_cfg["ENERGY_PERSPECTIVE_2050"] = {"DO_PARTIAL_RETROFIT": True} containers_prev_sim = create_mock_containers_with_results(ureg) containers_current = create_mock_bldg_containers(ureg) retrofitter = EnergyPerspective2050BldgElementsRetrofitter(ureg, custom_config=mock_ret_rates_cfg) retrofitter._per_elem_construction_retrofitter = ConstrRetrofitterMock() retrofitter.retrofit_site(2020, containers_current, containers_prev_sim) for x in retrofitter._retrofit_categories_last_run[BldgType.SFH]: #print(f"{x.constr_ac}; {x.bldg_elems_to_retrofit}; {x.target_nr_of_bldgs_to_retrofit}; {x.nr_of_bldgs_retrofitted}") if x.constr_ac == AgeClass(min_age=1991, max_age=1995): if x.bldg_elems_to_retrofit == [BuildingElement.WINDOW]: assert x.target_nr_of_bldgs_to_retrofit == 1 assert x.nr_of_bldgs_retrofitted ==1 else: x.target_nr_of_bldgs_to_retrofit == 0 tot_retrofitted_ac_1977 = 0 tot_to_retrofit_ac_1977 = 0 for x in retrofitter._retrofit_categories_last_run[BldgType.MFH]: #print(f"{x.constr_ac}; {x.bldg_elems_to_retrofit}; {x.target_nr_of_bldgs_to_retrofit}; {x.nr_of_bldgs_retrofitted}") if x.constr_ac == AgeClass(min_age=1971, max_age=1980): tot_retrofitted_ac_1977 += x.nr_of_bldgs_retrofitted tot_to_retrofit_ac_1977 += x.target_nr_of_bldgs_to_retrofit if x.bldg_elems_to_retrofit == [BuildingElement.ROOF]: assert x.target_nr_of_bldgs_to_retrofit == 1 elif x.bldg_elems_to_retrofit == [BuildingElement.WALL, BuildingElement.WINDOW]: assert x.target_nr_of_bldgs_to_retrofit == 2 else: x.target_nr_of_bldgs_to_retrofit == 0 assert 3 == tot_to_retrofit_ac_1977 assert 1 == tot_retrofitted_ac_1977
def test_age_classes_consecutive_gap(): myAgeClasses = [ AgeClass(None, 1918), AgeClass(1919, 1925), AgeClass(1926, 1933), AgeClass(1935, None) # it's not consecutive here, gap of 2 years ] assert AgeClass.are_age_classes_consecutive(myAgeClasses) == False
def test_age_class_equal(): logging.getLogger().setLevel(logging.DEBUG) a = AgeClass() equal_to_a = AgeClass() assert a == equal_to_a a = AgeClass(1987, 2000) equal_to_a = AgeClass(1987, 2000) assert a == equal_to_a b = AgeClass(1988, 2000) assert a != b b.min_age = a.min_age assert a == b b = AgeClass(1987, 2001) assert a != b b.max_age = a.max_age assert a == b
def __read_partial_shares(self, bldg_type: BldgType): partial_ret_shares = self.__read_raw_df(self._es_cfg["RETROFIT"]["PARTIAL_SHARES"][bldg_type.name]) idx = partial_ret_shares[self._COL_LABEL_YOC].apply(lambda yoc: AgeClass.from_string(yoc)) partial_ret_shares.set_index(idx, inplace=True) partial_ret_shares.drop(self._COL_LABEL_YOC, axis="columns", inplace=True) partial_ret_shares.columns = [int(col_idx) if col_idx != self._COL_LABEL_BLDG_ELEMS else col_idx for col_idx in partial_ret_shares.columns] bldg_elem_col = partial_ret_shares[self._COL_LABEL_BLDG_ELEMS].apply(self.__convert_bldg_elems_str_to_enum_array) partial_ret_shares.drop(self._COL_LABEL_BLDG_ELEMS, axis="columns", inplace=True) # remove column to be able to use applymap on all numeric entries... partial_ret_shares = partial_ret_shares.applymap(lambda x: x / 100) # in input files retrofit rates are in % from 0..100 partial_ret_shares = pd.concat([partial_ret_shares, bldg_elem_col], axis="columns") # add bldg elem col again return partial_ret_shares
def _get_archetype_uri_for(self, year_of_construction): """ :param year_of_construction: [description] :type year_of_construction: [type] :return: [description] :rtype: [type] :raises: Exception if no archetype for given year_of_construction was found """ age_class = AgeClass.get_age_class_for(year_of_construction, self._ageclass_archetype.keys()) return self._ageclass_archetype[age_class]
def test_partial_retrofit_rate_withebox(custom_conf_wwb): ret_rates = RetrofitRates(custom_conf_wwb) partial_ret_share_per_ac = ret_rates._get_all_partial_retrofit_shares(sim_year=2020, bldg_type=BldgType.SFH) assert len(partial_ret_share_per_ac) == 4 for ac, partial_ret_shares in partial_ret_share_per_ac.items(): assert len(partial_ret_shares) == 15 for (bldg_elems, partial_share) in partial_ret_share_per_ac[AgeClass(min_age=1975, max_age=1984)]: if bldg_elems == [BuildingElement.WINDOW]: assert partial_share == pytest.approx(0.5833) break
def test_partial_retrofit_rate(custom_conf_wwb): ret_rates = RetrofitRates(custom_conf_wwb) partial_ret_rates_per_ac = ret_rates.get_partial_retrofit_rates_per_age_class(sim_year=2030, bldg_type=BldgType.SFH) assert len(partial_ret_rates_per_ac) == 19 ac_to_test = AgeClass(1946, 1960) partial_ret_rates = partial_ret_rates_per_ac[ac_to_test] assert len(partial_ret_rates) == 4 ret_rate_sum = 0 for bldg_elems, ret_rate in partial_ret_rates: ret_rate_sum += ret_rate assert ret_rate_sum == ret_rates.get_full_retrofit_rate_per_age_class(sim_year=2030, bldg_type=BldgType.SFH)[ac_to_test]
def __read_full_rates(self, bldg_type: BldgType): """ :param bldg_type: :return: pandas df, index is AgeClass() instance, representing year of construction range """ full_ret_rates = self.__read_raw_df(self._es_cfg["RETROFIT"]["FULL_RATES"][bldg_type.name]) idx = full_ret_rates[self._COL_LABEL_YOC].apply(lambda yoc: AgeClass.from_string(yoc)) full_ret_rates.set_index(idx, inplace=True) full_ret_rates.drop(self._COL_LABEL_YOC, axis="columns", inplace=True) ac_to_part_ret_ac = full_ret_rates[self._COL_LABEL_PARTIAL_AC_MAPPING].apply(lambda ac: AgeClass.from_string(ac)) full_ret_rates.drop(self._COL_LABEL_PARTIAL_AC_MAPPING, axis="columns", inplace=True) full_ret_rates.columns = [int(col_idx) for col_idx in full_ret_rates.columns] full_ret_rates = full_ret_rates.applymap(lambda x: x / 100) # in input files retrofit rates are in % from 0..100 return pd.concat([full_ret_rates, ac_to_part_ret_ac], axis="columns")
def get_age_class_of_archetype(self, archetype_uri): df = self.graph_reader.get_archetype_year_range_from_graph_for_uri(archetype_uri) if not df.empty: min_raw = df.at[0, "minValue"] if min_raw == "-": min_value = None else: min_value = int(min_raw) max_raw = df.at[0, "maxValue"] if max_raw == "-": max_value = None else: max_value = int(max_raw) return AgeClass(min_value, max_value) else: raise LookupError(f"There is no Archetype with uri {archetype_uri} in the database.")
def _init_age_class_lookup(self) -> Dict[AgeClass, str]: ageclass_archetype = {} for archetype_shortname, archetype_cfg in self._cfg[ "ARCHETYPES"].items(): arch_uri = archetype_cfg["URI"] age_class = self._constr_reader.get_age_class_of_archetype( arch_uri) ageclass_archetype[age_class] = arch_uri if not AgeClass.are_age_classes_consecutive( list(ageclass_archetype.keys())): logging.error( "age classes retrieved from database are not consecutive. check min/max age of the used age classes so that there are neighter gaps nor overlaps." ) return ageclass_archetype
def test_age_class_init_from_string(): the_ac = AgeClass.from_string(" < 1918") assert the_ac.min_age == None assert the_ac.max_age == 1917 the_ac = AgeClass.from_string("<=1922") assert the_ac.min_age == None assert the_ac.max_age == 1922 the_ac = AgeClass.from_string("1918-1947") assert the_ac.min_age == 1918 assert the_ac.max_age == 1947 the_ac = AgeClass.from_string(" 1955 -2020") assert the_ac.min_age == 1955 assert the_ac.max_age == 2020 the_ac = AgeClass.from_string(">2050") assert the_ac.min_age == 2051 assert the_ac.max_age == None the_ac = AgeClass.from_string(">= 2050") assert the_ac.min_age == 2050 assert the_ac.max_age == None
def test_full_retrofit_rate(custom_conf_wwb): ret_rates = RetrofitRates(custom_conf_wwb) full_ret_rates_per_ac = ret_rates.get_full_retrofit_rate_per_age_class(sim_year=2020, bldg_type=BldgType.SFH) assert len(full_ret_rates_per_ac) == 19 assert full_ret_rates_per_ac[AgeClass(max_age=1918)] == 0.06