| 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  | 
            ||
| 129 | def get_annual_household_el_demand_cells():  | 
            ||
| 130 | """  | 
            ||
| 131 | Annual electricity demand per cell is determined  | 
            ||
| 132 | |||
| 133 | Timeseries for every cell are accumulated, the maximum value  | 
            ||
| 134 | determined and with the respective nuts3 factor scaled for 2035 and 2050  | 
            ||
| 135 | scenario.  | 
            ||
| 136 | |||
| 137 | Note  | 
            ||
| 138 | ----------  | 
            ||
| 139 | In test-mode 'SH' the iteration takes place by 'cell_id' to avoid  | 
            ||
| 140 | intensive RAM usage. For whole Germany 'nuts3' are taken and  | 
            ||
| 141 | RAM > 32GB is necessary.  | 
            ||
| 142 | """  | 
            ||
| 143 | |||
| 144 | with db.session_scope() as session:  | 
            ||
| 145 | cells_query = (  | 
            ||
| 146 | session.query(  | 
            ||
| 147 | HouseholdElectricityProfilesOfBuildings,  | 
            ||
| 148 | HouseholdElectricityProfilesInCensusCells.nuts3,  | 
            ||
| 149 | HouseholdElectricityProfilesInCensusCells.factor_2019,  | 
            ||
| 150 | HouseholdElectricityProfilesInCensusCells.factor_2023,  | 
            ||
| 151 | HouseholdElectricityProfilesInCensusCells.factor_2035,  | 
            ||
| 152 | HouseholdElectricityProfilesInCensusCells.factor_2050,  | 
            ||
| 153 | )  | 
            ||
| 154 | .filter(  | 
            ||
| 155 | HouseholdElectricityProfilesOfBuildings.cell_id  | 
            ||
| 156 | == HouseholdElectricityProfilesInCensusCells.cell_id  | 
            ||
| 157 | )  | 
            ||
| 158 | .order_by(HouseholdElectricityProfilesOfBuildings.id)  | 
            ||
| 159 | )  | 
            ||
| 160 | |||
| 161 | df_buildings_and_profiles = pd.read_sql(  | 
            ||
| 162 | cells_query.statement, cells_query.session.bind, index_col="id"  | 
            ||
| 163 | )  | 
            ||
| 164 | |||
| 165 | # Read demand profiles from egon-data-bundle  | 
            ||
| 166 | df_profiles = get_iee_hh_demand_profiles_raw()  | 
            ||
| 167 | |||
| 168 | def ve(s):  | 
            ||
| 169 | raise (ValueError(s))  | 
            ||
| 170 | |||
| 171 | dataset = egon.data.config.settings()["egon-data"]["--dataset-boundary"]  | 
            ||
| 172 | scenarios = egon.data.config.settings()["egon-data"]["--scenarios"]  | 
            ||
| 173 | |||
| 174 | iterate_over = (  | 
            ||
| 175 | "nuts3"  | 
            ||
| 176 | if dataset == "Everything"  | 
            ||
| 177 | else "cell_id"  | 
            ||
| 178 | if dataset == "Schleswig-Holstein"  | 
            ||
| 179 |         else ve(f"'{dataset}' is not a valid dataset boundary.") | 
            ||
| 180 | )  | 
            ||
| 181 | |||
| 182 | df_annual_demand = pd.DataFrame(  | 
            ||
| 183 | columns=scenarios + ["zensus_population_id"]  | 
            ||
| 184 | )  | 
            ||
| 185 | |||
| 186 | for _, df in df_buildings_and_profiles.groupby(by=iterate_over):  | 
            ||
| 187 | df_annual_demand_iter = pd.DataFrame(  | 
            ||
| 188 | columns=scenarios + ["zensus_population_id"]  | 
            ||
| 189 | )  | 
            ||
| 190 | |||
| 191 | if "eGon2035" in scenarios:  | 
            ||
| 192 | df_annual_demand_iter["eGon2035"] = (  | 
            ||
| 193 | df_profiles.loc[:, df["profile_id"]].sum(axis=0)  | 
            ||
| 194 | * df["factor_2035"].values  | 
            ||
| 195 | )  | 
            ||
| 196 | if "eGon100RE" in scenarios:  | 
            ||
| 197 | df_annual_demand_iter["eGon100RE"] = (  | 
            ||
| 198 | df_profiles.loc[:, df["profile_id"]].sum(axis=0)  | 
            ||
| 199 | * df["factor_2050"].values  | 
            ||
| 200 | )  | 
            ||
| 201 | if "status2019" in scenarios:  | 
            ||
| 202 | df_annual_demand_iter["status2019"] = (  | 
            ||
| 203 | df_profiles.loc[:, df["profile_id"]].sum(axis=0)  | 
            ||
| 204 | * df["factor_2019"].values  | 
            ||
| 205 | )  | 
            ||
| 206 | |||
| 207 | if "status2023" in scenarios:  | 
            ||
| 208 | df_annual_demand_iter["status2023"] = (  | 
            ||
| 209 | df_profiles.loc[:, df["profile_id"]].sum(axis=0)  | 
            ||
| 210 | * df["factor_2023"].values  | 
            ||
| 211 | )  | 
            ||
| 212 | df_annual_demand_iter["zensus_population_id"] = df["cell_id"].values  | 
            ||
| 213 | df_annual_demand = pd.concat([df_annual_demand, df_annual_demand_iter])  | 
            ||
| 214 | |||
| 215 | df_annual_demand = (  | 
            ||
| 216 |         df_annual_demand.groupby("zensus_population_id").sum().reset_index() | 
            ||
| 217 | )  | 
            ||
| 218 | df_annual_demand["sector"] = "residential"  | 
            ||
| 219 | df_annual_demand = df_annual_demand.melt(  | 
            ||
| 220 | id_vars=["zensus_population_id", "sector"],  | 
            ||
| 221 | var_name="scenario",  | 
            ||
| 222 | value_name="demand",  | 
            ||
| 223 | )  | 
            ||
| 224 | # convert from Wh to MWh  | 
            ||
| 225 | df_annual_demand["demand"] = df_annual_demand["demand"] / 1e6  | 
            ||
| 226 | |||
| 227 | # delete all cells for residentials  | 
            ||
| 228 | with db.session_scope() as session:  | 
            ||
| 229 | session.query(EgonDemandRegioZensusElectricity).filter(  | 
            ||
| 230 | EgonDemandRegioZensusElectricity.sector == "residential"  | 
            ||
| 231 | ).delete()  | 
            ||
| 232 | |||
| 233 | # Insert data to target table  | 
            ||
| 234 | df_annual_demand.to_sql(  | 
            ||
| 235 | name=EgonDemandRegioZensusElectricity.__table__.name,  | 
            ||
| 236 | schema=EgonDemandRegioZensusElectricity.__table__.schema,  | 
            ||
| 237 | con=db.engine(),  | 
            ||
| 238 | index=False,  | 
            ||
| 239 | if_exists="append",  | 
            ||
| 240 | )  | 
            ||
| 326 |