Conditions | 3 |
Total Lines | 128 |
Code Lines | 65 |
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:
1 | """ |
||
144 | def get_data() -> dict[gpd.GeoDataFrame]: |
||
145 | """ |
||
146 | Load all data necessary for TracBEV. Data loaded: |
||
147 | |||
148 | * 'hpc_positions' - Potential hpc positions |
||
149 | * 'landuse' - Potential work related positions |
||
150 | * 'poi_cluster' - Potential public related positions |
||
151 | * 'public_positions' - Potential public related positions |
||
152 | * 'housing_data' - Potential home related positions loaded from DB |
||
153 | * 'boundaries' - MV grid boundaries |
||
154 | * miscellaneous found in *datasets.yml* in section *charging_infrastructure* |
||
155 | |||
156 | Returns |
||
157 | ------- |
||
158 | |||
159 | """ |
||
160 | tracbev_cfg = DATASET_CFG["original_data"]["sources"]["tracbev"] |
||
161 | srid = tracbev_cfg["srid"] |
||
162 | |||
163 | # TODO: get zensus housing data from DB instead of gpkg? |
||
164 | files = tracbev_cfg["files_to_use"] |
||
165 | |||
166 | data_dict = {} |
||
167 | |||
168 | # get TracBEV files |
||
169 | for f in files: |
||
170 | file = WORKING_DIR / "data" / f |
||
171 | name = f.split(".")[0] |
||
172 | |||
173 | data_dict[name] = gpd.read_file(file) |
||
174 | |||
175 | if "undefined" in data_dict[name].crs.name.lower(): |
||
176 | data_dict[name] = data_dict[name].set_crs( |
||
177 | epsg=srid, allow_override=True |
||
178 | ) |
||
179 | else: |
||
180 | data_dict[name] = data_dict[name].to_crs(epsg=srid) |
||
181 | |||
182 | # get housing data from DB |
||
183 | sql = """ |
||
184 | SELECT building_id, cell_id |
||
185 | FROM demand.egon_household_electricity_profile_of_buildings |
||
186 | """ |
||
187 | |||
188 | df = db.select_dataframe(sql) |
||
189 | |||
190 | count_df = ( |
||
191 | df.groupby(["building_id", "cell_id"]) |
||
192 | .size() |
||
193 | .reset_index() |
||
194 | .rename(columns={0: "count"}) |
||
195 | ) |
||
196 | |||
197 | mfh_df = ( |
||
198 | count_df.loc[count_df["count"] > 1] |
||
199 | .groupby(["cell_id"]) |
||
200 | .size() |
||
201 | .reset_index() |
||
202 | .rename(columns={0: "num_mfh"}) |
||
203 | ) |
||
204 | efh_df = ( |
||
205 | count_df.loc[count_df["count"] <= 1] |
||
206 | .groupby(["cell_id"]) |
||
207 | .size() |
||
208 | .reset_index() |
||
209 | .rename(columns={0: "num"}) |
||
210 | ) |
||
211 | |||
212 | comb_df = ( |
||
213 | mfh_df.merge( |
||
214 | right=efh_df, how="outer", left_on="cell_id", right_on="cell_id" |
||
215 | ) |
||
216 | .fillna(0) |
||
217 | .astype(int) |
||
218 | ) |
||
219 | |||
220 | sql = """ |
||
221 | SELECT zensus_population_id, geom as geometry |
||
222 | FROM society.egon_destatis_zensus_apartment_building_population_per_ha |
||
223 | """ |
||
224 | |||
225 | gdf = db.select_geodataframe(sql, geom_col="geometry", epsg=srid) |
||
226 | |||
227 | data_dict["housing_data"] = gpd.GeoDataFrame( |
||
228 | gdf.merge( |
||
229 | right=comb_df, left_on="zensus_population_id", right_on="cell_id" |
||
230 | ), |
||
231 | crs=gdf.crs, |
||
232 | ).drop(columns=["cell_id"]) |
||
233 | |||
234 | # get boundaries aka grid districts |
||
235 | sql = """ |
||
236 | SELECT bus_id, geom FROM grid.egon_mv_grid_district |
||
237 | """ |
||
238 | |||
239 | data_dict["boundaries"] = db.select_geodataframe( |
||
240 | sql, geom_col="geom", epsg=srid |
||
241 | ) |
||
242 | |||
243 | data_dict["regions"] = pd.DataFrame( |
||
244 | columns=["mv_grid_id"], |
||
245 | data=data_dict["boundaries"].bus_id.unique(), |
||
246 | ) |
||
247 | |||
248 | data_dict["work_dict"] = { |
||
249 | "retail": DATASET_CFG["constants"]["work_weight_retail"], |
||
250 | "commercial": DATASET_CFG["constants"]["work_weight_commercial"], |
||
251 | "industrial": DATASET_CFG["constants"]["work_weight_industrial"], |
||
252 | } |
||
253 | |||
254 | data_dict["sfh_available"] = DATASET_CFG["constants"][ |
||
255 | "single_family_home_share" |
||
256 | ] |
||
257 | data_dict["sfh_avg_spots"] = DATASET_CFG["constants"][ |
||
258 | "single_family_home_spots" |
||
259 | ] |
||
260 | data_dict["mfh_available"] = DATASET_CFG["constants"][ |
||
261 | "multi_family_home_share" |
||
262 | ] |
||
263 | data_dict["mfh_avg_spots"] = DATASET_CFG["constants"][ |
||
264 | "multi_family_home_spots" |
||
265 | ] |
||
266 | |||
267 | data_dict["random_seed"] = np.random.default_rng( |
||
268 | DATASET_CFG["constants"]["random_seed"] |
||
269 | ) |
||
270 | |||
271 | return data_dict |
||
272 |