| Conditions | 10 |
| Total Lines | 111 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like data.datasets.electricity_demand.get_annual_household_el_demand_cells() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """The central module containing all code dealing with processing |
||
| 75 | def get_annual_household_el_demand_cells(): |
||
| 76 | """ |
||
| 77 | Annual electricity demand per cell is determined |
||
| 78 | |||
| 79 | Timeseries for every cell are accumulated, the maximum value |
||
| 80 | determined and with the respective nuts3 factor scaled for 2035 and 2050 |
||
| 81 | scenario. |
||
| 82 | |||
| 83 | Note |
||
| 84 | ---------- |
||
| 85 | In test-mode 'SH' the iteration takes place by 'cell_id' to avoid |
||
| 86 | intensive RAM usage. For whole Germany 'nuts3' are taken and |
||
| 87 | RAM > 32GB is necessary. |
||
| 88 | """ |
||
| 89 | |||
| 90 | with db.session_scope() as session: |
||
| 91 | cells_query = ( |
||
| 92 | session.query( |
||
| 93 | HouseholdElectricityProfilesOfBuildings, |
||
| 94 | HouseholdElectricityProfilesInCensusCells.nuts3, |
||
| 95 | HouseholdElectricityProfilesInCensusCells.factor_2019, |
||
| 96 | HouseholdElectricityProfilesInCensusCells.factor_2023, |
||
| 97 | HouseholdElectricityProfilesInCensusCells.factor_2035, |
||
| 98 | HouseholdElectricityProfilesInCensusCells.factor_2050, |
||
| 99 | ) |
||
| 100 | .filter( |
||
| 101 | HouseholdElectricityProfilesOfBuildings.cell_id |
||
| 102 | == HouseholdElectricityProfilesInCensusCells.cell_id |
||
| 103 | ) |
||
| 104 | .order_by(HouseholdElectricityProfilesOfBuildings.id) |
||
| 105 | ) |
||
| 106 | |||
| 107 | df_buildings_and_profiles = pd.read_sql( |
||
| 108 | cells_query.statement, cells_query.session.bind, index_col="id" |
||
| 109 | ) |
||
| 110 | |||
| 111 | # Read demand profiles from egon-data-bundle |
||
| 112 | df_profiles = get_iee_hh_demand_profiles_raw() |
||
| 113 | |||
| 114 | def ve(s): |
||
| 115 | raise (ValueError(s)) |
||
| 116 | |||
| 117 | dataset = egon.data.config.settings()["egon-data"]["--dataset-boundary"] |
||
| 118 | scenarios = egon.data.config.settings()["egon-data"]["--scenarios"] |
||
| 119 | |||
| 120 | iterate_over = ( |
||
| 121 | "nuts3" |
||
| 122 | if dataset == "Everything" |
||
| 123 | else "cell_id" |
||
| 124 | if dataset == "Schleswig-Holstein" |
||
| 125 | else ve(f"'{dataset}' is not a valid dataset boundary.") |
||
| 126 | ) |
||
| 127 | |||
| 128 | df_annual_demand = pd.DataFrame( |
||
| 129 | columns=scenarios + ["zensus_population_id"] |
||
| 130 | ) |
||
| 131 | |||
| 132 | for _, df in df_buildings_and_profiles.groupby(by=iterate_over): |
||
| 133 | df_annual_demand_iter = pd.DataFrame( |
||
| 134 | columns=scenarios + ["zensus_population_id"] |
||
| 135 | ) |
||
| 136 | |||
| 137 | if "eGon2035" in scenarios: |
||
| 138 | df_annual_demand_iter["eGon2035"] = ( |
||
| 139 | df_profiles.loc[:, df["profile_id"]].sum(axis=0) |
||
| 140 | * df["factor_2035"].values |
||
| 141 | ) |
||
| 142 | if "eGon100RE" in scenarios: |
||
| 143 | df_annual_demand_iter["eGon100RE"] = ( |
||
| 144 | df_profiles.loc[:, df["profile_id"]].sum(axis=0) |
||
| 145 | * df["factor_2050"].values |
||
| 146 | ) |
||
| 147 | if "status2019" in scenarios: |
||
| 148 | df_annual_demand_iter["status2019"] = ( |
||
| 149 | df_profiles.loc[:, df["profile_id"]].sum(axis=0) |
||
| 150 | * df["factor_2019"].values |
||
| 151 | ) |
||
| 152 | |||
| 153 | if "status2023" in scenarios: |
||
| 154 | df_annual_demand_iter["status2023"] = ( |
||
| 155 | df_profiles.loc[:, df["profile_id"]].sum(axis=0) |
||
| 156 | * df["factor_2023"].values |
||
| 157 | ) |
||
| 158 | df_annual_demand_iter["zensus_population_id"] = df["cell_id"].values |
||
| 159 | df_annual_demand = pd.concat([df_annual_demand, df_annual_demand_iter]) |
||
| 160 | |||
| 161 | df_annual_demand = ( |
||
| 162 | df_annual_demand.groupby("zensus_population_id").sum().reset_index() |
||
| 163 | ) |
||
| 164 | df_annual_demand["sector"] = "residential" |
||
| 165 | df_annual_demand = df_annual_demand.melt( |
||
| 166 | id_vars=["zensus_population_id", "sector"], |
||
| 167 | var_name="scenario", |
||
| 168 | value_name="demand", |
||
| 169 | ) |
||
| 170 | # convert from Wh to MWh |
||
| 171 | df_annual_demand["demand"] = df_annual_demand["demand"] / 1e6 |
||
| 172 | |||
| 173 | # delete all cells for residentials |
||
| 174 | with db.session_scope() as session: |
||
| 175 | session.query(EgonDemandRegioZensusElectricity).filter( |
||
| 176 | EgonDemandRegioZensusElectricity.sector == "residential" |
||
| 177 | ).delete() |
||
| 178 | |||
| 179 | # Insert data to target table |
||
| 180 | df_annual_demand.to_sql( |
||
| 181 | name=EgonDemandRegioZensusElectricity.__table__.name, |
||
| 182 | schema=EgonDemandRegioZensusElectricity.__table__.schema, |
||
| 183 | con=db.engine(), |
||
| 184 | index=False, |
||
| 185 | if_exists="append", |
||
| 186 | ) |
||
| 272 |