| Conditions | 5 |
| Total Lines | 96 |
| Code Lines | 64 |
| 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 | """ |
||
| 227 | def evaluate_df(self, df, ctx): |
||
| 228 | """ |
||
| 229 | Evaluate H2 saltcavern storage capacity against expected values. |
||
| 230 | |||
| 231 | Parameters |
||
| 232 | ---------- |
||
| 233 | df : pd.DataFrame |
||
| 234 | DataFrame with e_nom_max_germany column |
||
| 235 | ctx : dict |
||
| 236 | Context information |
||
| 237 | |||
| 238 | Returns |
||
| 239 | ------- |
||
| 240 | RuleResult |
||
| 241 | Validation result with success/failure status |
||
| 242 | """ |
||
| 243 | if df.empty or df["e_nom_max_germany"].isna().all(): |
||
| 244 | return RuleResult( |
||
| 245 | rule_id=self.rule_id, |
||
| 246 | task=self.task, |
||
| 247 | table=self.table, |
||
| 248 | kind=self.kind, |
||
| 249 | success=False, |
||
| 250 | message=f"No H2 saltcavern store data found for scenario {self.scenario}", |
||
| 251 | severity=Severity.WARNING, |
||
| 252 | schema=self.schema, |
||
| 253 | table_name=self.table_name, |
||
| 254 | rule_class=self.__class__.__name__ |
||
| 255 | ) |
||
| 256 | |||
| 257 | observed_capacity = float(df["e_nom_max_germany"].values[0]) |
||
| 258 | |||
| 259 | # Calculate expected capacity from saltcavern potential |
||
| 260 | try: |
||
| 261 | storage_potentials = calculate_and_map_saltcavern_storage_potential() |
||
| 262 | storage_potentials["storage_potential"] = ( |
||
| 263 | storage_potentials["area_fraction"] * storage_potentials["potential"] |
||
| 264 | ) |
||
| 265 | expected_capacity = sum(storage_potentials["storage_potential"].to_list()) |
||
| 266 | except Exception as e: |
||
| 267 | return RuleResult( |
||
| 268 | rule_id=self.rule_id, |
||
| 269 | task=self.task, |
||
| 270 | table=self.table, |
||
| 271 | kind=self.kind, |
||
| 272 | success=False, |
||
| 273 | message=f"Error calculating expected H2 saltcavern capacity: {str(e)}", |
||
| 274 | severity=Severity.ERROR, |
||
| 275 | schema=self.schema, |
||
| 276 | table_name=self.table_name, |
||
| 277 | rule_class=self.__class__.__name__ |
||
| 278 | ) |
||
| 279 | |||
| 280 | # Calculate relative deviation |
||
| 281 | rtol = self.params.get("rtol", 0.02) |
||
| 282 | deviation = abs(observed_capacity - expected_capacity) / expected_capacity |
||
| 283 | |||
| 284 | success = deviation <= rtol |
||
| 285 | |||
| 286 | deviation_pct = deviation * 100 |
||
| 287 | |||
| 288 | if success: |
||
| 289 | return RuleResult( |
||
| 290 | rule_id=self.rule_id, |
||
| 291 | task=self.task, |
||
| 292 | table=self.table, |
||
| 293 | kind=self.kind, |
||
| 294 | success=True, |
||
| 295 | observed=observed_capacity, |
||
| 296 | expected=expected_capacity, |
||
| 297 | message=( |
||
| 298 | f"H2 saltcavern stores capacity valid for {self.scenario}: " |
||
| 299 | f"deviation {deviation_pct:.2f}% (tolerance: {rtol*100:.2f}%)" |
||
| 300 | ), |
||
| 301 | severity=Severity.INFO, |
||
| 302 | schema=self.schema, |
||
| 303 | table_name=self.table_name, |
||
| 304 | rule_class=self.__class__.__name__ |
||
| 305 | ) |
||
| 306 | else: |
||
| 307 | return RuleResult( |
||
| 308 | rule_id=self.rule_id, |
||
| 309 | task=self.task, |
||
| 310 | table=self.table, |
||
| 311 | kind=self.kind, |
||
| 312 | success=False, |
||
| 313 | observed=observed_capacity, |
||
| 314 | expected=expected_capacity, |
||
| 315 | message=( |
||
| 316 | f"H2 saltcavern stores capacity deviation too large for {self.scenario}: " |
||
| 317 | f"{deviation_pct:.2f}% (tolerance: {rtol*100:.2f}%)" |
||
| 318 | ), |
||
| 319 | severity=Severity.ERROR, |
||
| 320 | schema=self.schema, |
||
| 321 | table_name=self.table_name, |
||
| 322 | rule_class=self.__class__.__name__ |
||
| 323 | ) |
||
| 324 |