| Conditions | 6 |
| Total Lines | 104 |
| Code Lines | 67 |
| 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 | """ |
||
| 68 | def evaluate_df(self, df, ctx): |
||
| 69 | """ |
||
| 70 | Evaluate CH4 store capacity against expected values. |
||
| 71 | |||
| 72 | Parameters |
||
| 73 | ---------- |
||
| 74 | df : pd.DataFrame |
||
| 75 | DataFrame with e_nom_germany column |
||
| 76 | ctx : dict |
||
| 77 | Context information |
||
| 78 | |||
| 79 | Returns |
||
| 80 | ------- |
||
| 81 | RuleResult |
||
| 82 | Validation result with success/failure status |
||
| 83 | """ |
||
| 84 | if df.empty or df["e_nom_germany"].isna().all(): |
||
| 85 | return RuleResult( |
||
| 86 | rule_id=self.rule_id, |
||
| 87 | task=self.task, |
||
| 88 | table=self.table, |
||
| 89 | kind=self.kind, |
||
| 90 | success=False, |
||
| 91 | message=f"No CH4 store data found for scenario {self.scenario}", |
||
| 92 | severity=Severity.WARNING, |
||
| 93 | schema=self.schema, |
||
| 94 | table_name=self.table_name, |
||
| 95 | rule_class=self.__class__.__name__ |
||
| 96 | ) |
||
| 97 | |||
| 98 | observed_capacity = float(df["e_nom_germany"].values[0]) |
||
| 99 | |||
| 100 | # Calculate expected capacity based on scenario |
||
| 101 | if self.scenario == "eGon2035": |
||
| 102 | grid_cap = 130000 # MWh |
||
| 103 | elif self.scenario == "eGon100RE": |
||
| 104 | # Get retrofitted share from config |
||
| 105 | from egon.data.datasets.scenario_parameters import get_sector_parameters |
||
| 106 | retrofitted_share = get_sector_parameters("gas", "eGon100RE")[ |
||
| 107 | "retrofitted_CH4pipeline-to-H2pipeline_share" |
||
| 108 | ] |
||
| 109 | grid_cap = 13000 * (1 - retrofitted_share) # MWh |
||
| 110 | else: |
||
| 111 | return RuleResult( |
||
| 112 | rule_id=self.rule_id, |
||
| 113 | task=self.task, |
||
| 114 | table=self.table, |
||
| 115 | kind=self.kind, |
||
| 116 | success=False, |
||
| 117 | message=f"Unknown scenario: {self.scenario}", |
||
| 118 | severity=Severity.ERROR, |
||
| 119 | schema=self.schema, |
||
| 120 | table_name=self.table_name, |
||
| 121 | rule_class=self.__class__.__name__ |
||
| 122 | ) |
||
| 123 | |||
| 124 | # GIE capacity: https://www.gie.eu/transparency/databases/storage-database/ |
||
| 125 | stores_cap_germany = 266424202 # MWh |
||
| 126 | |||
| 127 | expected_capacity = stores_cap_germany + grid_cap |
||
| 128 | |||
| 129 | # Calculate relative deviation |
||
| 130 | rtol = self.params.get("rtol", 0.02) |
||
| 131 | deviation = abs(observed_capacity - expected_capacity) / expected_capacity |
||
| 132 | |||
| 133 | success = deviation <= rtol |
||
| 134 | |||
| 135 | deviation_pct = deviation * 100 |
||
| 136 | |||
| 137 | if success: |
||
| 138 | return RuleResult( |
||
| 139 | rule_id=self.rule_id, |
||
| 140 | task=self.task, |
||
| 141 | table=self.table, |
||
| 142 | kind=self.kind, |
||
| 143 | success=True, |
||
| 144 | observed=observed_capacity, |
||
| 145 | expected=expected_capacity, |
||
| 146 | message=( |
||
| 147 | f"CH4 stores capacity valid for {self.scenario}: " |
||
| 148 | f"deviation {deviation_pct:.2f}% (tolerance: {rtol*100:.2f}%)" |
||
| 149 | ), |
||
| 150 | severity=Severity.INFO, |
||
| 151 | schema=self.schema, |
||
| 152 | table_name=self.table_name, |
||
| 153 | rule_class=self.__class__.__name__ |
||
| 154 | ) |
||
| 155 | else: |
||
| 156 | return RuleResult( |
||
| 157 | rule_id=self.rule_id, |
||
| 158 | task=self.task, |
||
| 159 | table=self.table, |
||
| 160 | kind=self.kind, |
||
| 161 | success=False, |
||
| 162 | observed=observed_capacity, |
||
| 163 | expected=expected_capacity, |
||
| 164 | message=( |
||
| 165 | f"CH4 stores capacity deviation too large for {self.scenario}: " |
||
| 166 | f"{deviation_pct:.2f}% (tolerance: {rtol*100:.2f}%)" |
||
| 167 | ), |
||
| 168 | severity=Severity.ERROR, |
||
| 169 | schema=self.schema, |
||
| 170 | table_name=self.table_name, |
||
| 171 | rule_class=self.__class__.__name__ |
||
| 172 | ) |
||
| 324 |