Skip to content

Commit 796f7ad

Browse files
authored
Feature/format (#209)
ruff format
1 parent 87a3ff0 commit 796f7ad

29 files changed

Lines changed: 1231 additions & 1093 deletions

examples/00_Minmal/minimal_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
calculation.do_modeling()
5656
calculation.solve(fx.solvers.HighsSolver(0.01, 60))
5757

58-
5958
# --- Analyze Results ---
6059
# Access the results of an element
6160
df1 = calculation.results['costs'].filter_solution('time').to_dataframe()

examples/01_Simple/simple_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,5 @@
116116
df = calculation.results['Storage'].node_balance_with_charge_state()
117117
print(df)
118118

119-
#Save results to file for later usage
119+
# Save results to file for later usage
120120
calculation.results.to_file()
121-

examples/02_Complex/complex_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
Gaskessel = fx.linear_converters.Boiler(
5151
'Kessel',
5252
eta=0.5, # Efficiency ratio
53-
on_off_parameters=fx.OnOffParameters(effects_per_running_hour={Costs.label: 0, CO2.label: 1000}), # CO2 emissions per hour
53+
on_off_parameters=fx.OnOffParameters(
54+
effects_per_running_hour={Costs.label: 0, CO2.label: 1000}
55+
), # CO2 emissions per hour
5456
Q_th=fx.Flow(
5557
label='Q_th', # Thermal output
5658
bus='Fernwärme', # Linked bus

examples/03_Calculation_types/example_calculation_types.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@
121121

122122
# Gas Tariff
123123
a_gas_tarif = fx.Source(
124-
'Gastarif', source=fx.Flow('Q_Gas', bus='Gas', size=1000, effects_per_flow_hour={costs.label: gas_price, CO2.label: 0.3})
124+
'Gastarif',
125+
source=fx.Flow('Q_Gas', bus='Gas', size=1000, effects_per_flow_hour={costs.label: gas_price, CO2.label: 0.3}),
125126
)
126127

127128
# Coal Tariff
128129
a_kohle_tarif = fx.Source(
129-
'Kohletarif', source=fx.Flow('Q_Kohle', bus='Kohle', size=1000, effects_per_flow_hour={costs.label: 4.6, CO2.label: 0.3})
130+
'Kohletarif',
131+
source=fx.Flow('Q_Kohle', bus='Kohle', size=1000, effects_per_flow_hour={costs.label: 4.6, CO2.label: 0.3}),
130132
)
131133

132134
# Electricity Tariff and Feed-in
@@ -136,7 +138,9 @@
136138

137139
a_strom_tarif = fx.Source(
138140
'Stromtarif',
139-
source=fx.Flow('P_el', bus='Strom', size=1000, effects_per_flow_hour={costs.label: TS_electricity_price_buy, CO2: 0.3}),
141+
source=fx.Flow(
142+
'P_el', bus='Strom', size=1000, effects_per_flow_hour={costs.label: TS_electricity_price_buy, CO2: 0.3}
143+
),
140144
)
141145

142146
# Flow System Setup
@@ -190,26 +194,34 @@ def get_solutions(calcs: List, variable: str) -> xr.Dataset:
190194
# --- Plotting for comparison ---
191195
fx.plotting.with_plotly(
192196
get_solutions(calculations, 'Speicher|charge_state').to_dataframe(),
193-
mode='line', title='Charge State Comparison', ylabel='Charge state',
197+
mode='line',
198+
title='Charge State Comparison',
199+
ylabel='Charge state',
194200
).write_html('results/Charge State.html')
195201

196202
fx.plotting.with_plotly(
197203
get_solutions(calculations, 'BHKW2(Q_th)|flow_rate').to_dataframe(),
198-
mode='line', title='BHKW2(Q_th) Flow Rate Comparison', ylabel='Flow rate',
204+
mode='line',
205+
title='BHKW2(Q_th) Flow Rate Comparison',
206+
ylabel='Flow rate',
199207
).write_html('results/BHKW2 Thermal Power.html')
200208

201209
fx.plotting.with_plotly(
202210
get_solutions(calculations, 'costs(operation)|total_per_timestep').to_dataframe(),
203-
mode='line', title='Operation Cost Comparison', ylabel='Costs [€]'
211+
mode='line',
212+
title='Operation Cost Comparison',
213+
ylabel='Costs [€]',
204214
).write_html('results/Operation Costs.html')
205215

206216
fx.plotting.with_plotly(
207217
pd.DataFrame(get_solutions(calculations, 'costs(operation)|total_per_timestep').to_dataframe().sum()).T,
208-
mode='bar', title='Total Cost Comparison', ylabel='Costs [€]'
218+
mode='bar',
219+
title='Total Cost Comparison',
220+
ylabel='Costs [€]',
209221
).update_layout(barmode='group').write_html('results/Total Costs.html')
210222

211223
fx.plotting.with_plotly(
212224
pd.DataFrame([calc.durations for calc in calculations], index=[calc.name for calc in calculations]), 'bar'
213-
).update_layout(
214-
title='Duration Comparison', xaxis_title='Calculation type', yaxis_title='Time (s)'
215-
).write_html('results/Speed Comparison.html')
225+
).update_layout(title='Duration Comparison', xaxis_title='Calculation type', yaxis_title='Time (s)').write_html(
226+
'results/Speed Comparison.html'
227+
)

flixopt/aggregation.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
try:
1818
import tsam.timeseriesaggregation as tsam
19+
1920
TSAM_AVAILABLE = True
2021
except ImportError:
2122
TSAM_AVAILABLE = False
@@ -52,7 +53,6 @@ def __init__(
5253
time_series_for_high_peaks: List[str] = None,
5354
time_series_for_low_peaks: List[str] = None,
5455
):
55-
5656
"""
5757
Args:
5858
original_data: The original data to aggregate
@@ -64,8 +64,9 @@ def __init__(
6464
time_series_for_low_peaks: List of time series to use for explicitly selecting periods with low values.
6565
"""
6666
if not TSAM_AVAILABLE:
67-
raise ImportError("The 'tsam' package is required for clustering functionality. "
68-
"Install it with 'pip install tsam'.")
67+
raise ImportError(
68+
"The 'tsam' package is required for clustering functionality. Install it with 'pip install tsam'."
69+
)
6970
self.original_data = copy.deepcopy(original_data)
7071
self.hours_per_time_step = hours_per_time_step
7172
self.hours_per_period = hours_per_period
@@ -140,12 +141,7 @@ def describe_clusters(self) -> str:
140141
def use_extreme_periods(self):
141142
return self.time_series_for_high_peaks or self.time_series_for_low_peaks
142143

143-
def plot(
144-
self,
145-
colormap: str = 'viridis',
146-
show: bool = True,
147-
save: Optional[pathlib.Path] = None
148-
) -> 'go.Figure':
144+
def plot(self, colormap: str = 'viridis', show: bool = True, save: Optional[pathlib.Path] = None) -> 'go.Figure':
149145
from . import plotting
150146

151147
df_org = self.original_data.copy().rename(
@@ -339,33 +335,44 @@ def do_modeling(self):
339335
penalty = self.aggregation_parameters.penalty_of_period_freedom
340336
if (self.aggregation_parameters.percentage_of_period_freedom > 0) and penalty != 0:
341337
for variable in self.variables_direct.values():
342-
self._model.effects.add_share_to_penalty(
343-
'Aggregation',
344-
variable * penalty
345-
)
338+
self._model.effects.add_share_to_penalty('Aggregation', variable * penalty)
346339

347340
def _equate_indices(self, variable: linopy.Variable, indices: Tuple[np.ndarray, np.ndarray]) -> None:
348341
assert len(indices[0]) == len(indices[1]), 'The length of the indices must match!!'
349342
length = len(indices[0])
350343

351344
# Gleichung:
352345
# eq1: x(p1,t) - x(p3,t) = 0 # wobei p1 und p3 im gleichen Cluster sind und t = 0..N_p
353-
con = self.add(self._model.add_constraints(
354-
variable.isel(time=indices[0]) - variable.isel(time=indices[1]) == 0,
355-
name=f'{self.label_full}|equate_indices|{variable.name}'),
356-
f'equate_indices|{variable.name}')
346+
con = self.add(
347+
self._model.add_constraints(
348+
variable.isel(time=indices[0]) - variable.isel(time=indices[1]) == 0,
349+
name=f'{self.label_full}|equate_indices|{variable.name}',
350+
),
351+
f'equate_indices|{variable.name}',
352+
)
357353

358354
# Korrektur: (bisher nur für Binärvariablen:)
359-
if variable.name in self._model.variables.binaries and self.aggregation_parameters.percentage_of_period_freedom > 0:
360-
var_k1 = self.add(self._model.add_variables(
361-
binary=True,
362-
coords={'time': variable.isel(time=indices[0]).indexes['time']},
363-
name=f'{self.label_full}|correction1|{variable.name}'), f'correction1|{variable.name}')
355+
if (
356+
variable.name in self._model.variables.binaries
357+
and self.aggregation_parameters.percentage_of_period_freedom > 0
358+
):
359+
var_k1 = self.add(
360+
self._model.add_variables(
361+
binary=True,
362+
coords={'time': variable.isel(time=indices[0]).indexes['time']},
363+
name=f'{self.label_full}|correction1|{variable.name}',
364+
),
365+
f'correction1|{variable.name}',
366+
)
364367

365-
var_k0 = self.add(self._model.add_variables(
366-
binary=True,
367-
coords={'time': variable.isel(time=indices[0]).indexes['time']},
368-
name=f'{self.label_full}|correction0|{variable.name}'), f'correction0|{variable.name}')
368+
var_k0 = self.add(
369+
self._model.add_variables(
370+
binary=True,
371+
coords={'time': variable.isel(time=indices[0]).indexes['time']},
372+
name=f'{self.label_full}|correction0|{variable.name}',
373+
),
374+
f'correction0|{variable.name}',
375+
)
369376

370377
# equation extends ...
371378
# --> On(p3) can be 0/1 independent of On(p1,t)!
@@ -377,16 +384,20 @@ def _equate_indices(self, variable: linopy.Variable, indices: Tuple[np.ndarray,
377384

378385
# interlock var_k1 and var_K2:
379386
# eq: var_k0(t)+var_k1(t) <= 1.1
380-
self.add(self._model.add_constraints(
381-
var_k0 + var_k1 <= 1.1,
382-
name=f'{self.label_full}|lock_k0_and_k1|{variable.name}'),
383-
f'lock_k0_and_k1|{variable.name}'
387+
self.add(
388+
self._model.add_constraints(
389+
var_k0 + var_k1 <= 1.1, name=f'{self.label_full}|lock_k0_and_k1|{variable.name}'
390+
),
391+
f'lock_k0_and_k1|{variable.name}',
384392
)
385393

386394
# Begrenzung der Korrektur-Anzahl:
387395
# eq: sum(K) <= n_Corr_max
388-
self.add(self._model.add_constraints(
389-
sum(var_k0) + sum(var_k1) <= round(self.aggregation_parameters.percentage_of_period_freedom / 100 * length),
390-
name=f'{self.label_full}|limit_corrections|{variable.name}'),
391-
f'limit_corrections|{variable.name}'
396+
self.add(
397+
self._model.add_constraints(
398+
sum(var_k0) + sum(var_k1)
399+
<= round(self.aggregation_parameters.percentage_of_period_freedom / 100 * length),
400+
name=f'{self.label_full}|limit_corrections|{variable.name}',
401+
),
402+
f'limit_corrections|{variable.name}',
392403
)

0 commit comments

Comments
 (0)