Skip to content

Commit 5509f33

Browse files
committed
Miscellaneous tidying for the rabbit
Signed-off-by: Davey Elder <iandavidelder@gmail.com>
1 parent 0d9ab4a commit 5509f33

5 files changed

Lines changed: 40 additions & 42 deletions

File tree

temoa/components/time.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,8 @@ def create_time_season_to_sequential(model: TemoaModel) -> None:
336336
f'No data in time_season_sequential but time_sequencing parameter set to '
337337
f'{model.time_sequencing.first()} and inter-season features used. '
338338
'time_season_sequential must be filled for this type of time sequencing if '
339-
'seasonal storage or inter-season constraints like ramp_up/ramp_down are used. '
340-
'Check '
341-
'the config file.'
339+
'seasonal storage or inter-season constraints like ramp_up/ramp_down or'
340+
'unit_commitment are used. Check the config file.'
342341
)
343342
logger.error(msg)
344343
raise ValueError(msg)

temoa/data_io/hybrid_loader.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def _load_tech_group_members(
564564
filtered_data: Sequence[tuple[object, ...]],
565565
) -> None:
566566
"""Loads members into the indexed set `tech_group_members`."""
567-
model = TemoaModel()
567+
model = self.model
568568
validator = self.viable_techs.members if self.viable_techs else None
569569
for group_name, tech in filtered_data:
570570
if validator is None or tech in validator:
@@ -582,7 +582,7 @@ def _load_time_season(
582582
"""
583583
Loads time_season as a flat ordered set of season names.
584584
"""
585-
model = TemoaModel()
585+
model = self.model
586586
if not filtered_data:
587587
logger.warning('No time_season table found. Loading a single filler season "S".')
588588
seasons_to_load: list[tuple[object, ...]] = [('S',)]
@@ -599,7 +599,7 @@ def _load_time_season_sequential(
599599
"""
600600
Composite loader for time_season_sequential and its associated index sets.
601601
"""
602-
model = TemoaModel()
602+
model = self.model
603603
if filtered_data:
604604
seg_frac_data = [
605605
(row[0], row[2]) for row in filtered_data
@@ -623,7 +623,7 @@ def _load_existing_capacity(
623623
Handles different queries for myopic vs. standard runs and also
624624
populates the `tech_exist` set.
625625
"""
626-
model = TemoaModel()
626+
model = self.model
627627
cur = self.con.cursor()
628628
mi = self.myopic_index
629629

@@ -652,7 +652,6 @@ def _load_existing_capacity(
652652
self._load_component_data(data, model.vintage_exist, vintage_exist_data)
653653

654654
# Collect existing capacity data indices
655-
self.viable_existing_techs = {row[1] for row in rows_to_load}
656655
self.viable_existing_rt = {(row[0], row[1]) for row in rows_to_load}
657656
self.viable_existing_rtv = {(row[0], row[1], row[2]) for row in rows_to_load}
658657

@@ -672,7 +671,7 @@ def _load_retired_existing_capacity(
672671
)
673672
return
674673

675-
model = TemoaModel()
674+
model = self.model
676675
cur = self.con.cursor()
677676
mi = self.myopic_index
678677

@@ -701,12 +700,12 @@ def _load_lifetime_tech(
701700
filtered_data: Sequence[tuple[object, ...]],
702701
) -> None:
703702
"""Loads the lifetime_tech component."""
704-
model = TemoaModel()
703+
model = self.model
705704
cur = self.con.cursor()
706705
rows_to_load = cur.execute('SELECT region, tech, lifetime FROM lifetime_tech').fetchall()
707706
rt_getter = itemgetter(0, 1)
708707
if self.viable_rt:
709-
valid_rt = self.viable_rt.members | self.viable_existing_rt
708+
valid_rt = self.viable_rt.member_tuples | self.viable_existing_rt
710709
rows_to_load = [item for item in rows_to_load if rt_getter(item) in valid_rt]
711710
self._load_component_data(data, model.lifetime_tech, rows_to_load)
712711

@@ -717,7 +716,7 @@ def _load_lifetime_process(
717716
filtered_data: Sequence[tuple[object, ...]],
718717
) -> None:
719718
"""Loads the lifetime_process component."""
720-
model = TemoaModel()
719+
model = self.model
721720
cur = self.con.cursor()
722721
mi = self.myopic_index
723722

@@ -743,7 +742,7 @@ def _load_lifetime_survival_curve(
743742
filtered_data: Sequence[tuple[object, ...]],
744743
) -> None:
745744
"""Loads the lifetime_survival_curve component."""
746-
model = TemoaModel()
745+
model = self.model
747746
cur = self.con.cursor()
748747
mi = self.myopic_index
749748

@@ -771,7 +770,7 @@ def _load_global_discount_rate(
771770
filtered_data: Sequence[tuple[object, ...]],
772771
) -> None:
773772
"""Loads the required singleton global_discount_rate."""
774-
model = TemoaModel()
773+
model = self.model
775774
if filtered_data:
776775
data[model.global_discount_rate.name] = {None: cast('float', filtered_data[0][0])}
777776
else:
@@ -787,7 +786,7 @@ def _load_default_loan_rate(
787786
filtered_data: Sequence[tuple[object, ...]],
788787
) -> None:
789788
"""Loads the optional singleton default_loan_rate."""
790-
model = TemoaModel()
789+
model = self.model
791790
if filtered_data:
792791
data[model.default_loan_rate.name] = {None: cast('float', filtered_data[0][0])}
793792

@@ -799,7 +798,7 @@ def _load_efficiency(
799798
filtered_data: Sequence[tuple[object, ...]],
800799
) -> None:
801800
"""Loads the main efficiency parameter, which is pre-calculated."""
802-
model = TemoaModel()
801+
model = self.model
803802
self._load_component_data(data, model.efficiency, self.efficiency_values)
804803

805804
def _load_linked_techs(
@@ -832,7 +831,7 @@ def _load_ramping_down(
832831
filtered_data: Sequence[tuple[object, ...]],
833832
) -> None:
834833
"""Composite loader for ramp_down_hourly and its index set `tech_downramping`."""
835-
model = TemoaModel()
834+
model = self.model
836835
self._load_component_data(data, model.ramp_down_hourly, filtered_data)
837836
if filtered_data:
838837
tech_data = sorted({(row[1],) for row in filtered_data})
@@ -848,7 +847,7 @@ def _load_ramping_up(
848847
filtered_data: Sequence[tuple[object, ...]],
849848
) -> None:
850849
"""Composite loader for ramp_up_hourly and its index set `tech_upramping`."""
851-
model = TemoaModel()
850+
model = self.model
852851
self._load_component_data(data, model.ramp_up_hourly, filtered_data)
853852
if filtered_data:
854853
tech_data = sorted({(row[1],) for row in filtered_data})
@@ -864,7 +863,7 @@ def _load_rps_requirement(
864863
filtered_data: Sequence[tuple[object, ...]],
865864
) -> None:
866865
"""Handles deprecation warning for renewable_portfolio_standard."""
867-
model = TemoaModel()
866+
model = self.model
868867
self._load_component_data(data, model.renewable_portfolio_standard, filtered_data)
869868
if filtered_data:
870869
logger.warning(

temoa/extensions/economies_of_scale/components/cost_invest_eos.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -397,25 +397,26 @@ def period_cost(model: EOSModel, r: Region, p: Period, t: Technology) -> Express
397397
# Existing capacity costs to subtract (needed for myopic)
398398
regions = geography.gather_group_regions(model, r)
399399
techs = technology.gather_group_techs(model, t)
400-
existing_capacity = quicksum(
400+
existing_capacity = sum(
401401
value(model.existing_capacity[_r, _t, _v])
402402
for _r, _t, _v in model.existing_capacity
403403
if _r in regions and _t in techs
404404
)
405-
if existing_capacity:
406-
for n in model.cost_invest_eos_segments[r, t]:
407-
cap_lower, cap_upper, _, _ = model.cost_invest_eos[r, t, n]
408-
if value(cap_lower) <= existing_capacity <= value(cap_upper):
409-
prev_cum_cost = cost_invest_eos_segment_cost(model, r, t, n, existing_capacity)
410-
continue # in case we're exactly on the boundary of two segments
411-
if not prev_cum_cost:
412-
msg = (
413-
'Existing capacity for a cost_invest_eos cluster is outside the bounds of '
414-
'the cost curve. Check the cost_invest_eos table and existing_capacity '
415-
f'for {r, t}: {existing_capacity}'
416-
)
417-
logger.error(msg)
418-
raise ValueError(msg)
405+
in_bounds = False
406+
for n in model.cost_invest_eos_segments[r, t]:
407+
cap_lower, cap_upper, _, _ = model.cost_invest_eos[r, t, n]
408+
if value(cap_lower) <= existing_capacity <= value(cap_upper):
409+
prev_cum_cost = cost_invest_eos_segment_cost(model, r, t, n, existing_capacity)
410+
in_bounds = True
411+
break
412+
if not in_bounds:
413+
msg = (
414+
'Existing capacity for a cost_invest_eos cluster is outside the bounds of '
415+
'the cost curve. Check the cost_invest_eos table and existing_capacity '
416+
f'for {r, t}: {existing_capacity}'
417+
)
418+
logger.error(msg)
419+
raise ValueError(msg)
419420

420421
return cumulative_cost - prev_cum_cost
421422

temoa/extensions/economies_of_scale/core/model.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
Var,
1515
)
1616

17-
import temoa.extensions.economies_of_scale.components.cost_fixed_eos as cost_fixed_eos
18-
import temoa.extensions.economies_of_scale.components.cost_invest_eos as cost_invest_eos
19-
import temoa.extensions.economies_of_scale.components.cost_variable_eos as cost_variable_eos
17+
from temoa.extensions.economies_of_scale.components import (
18+
cost_fixed_eos,
19+
cost_invest_eos,
20+
cost_variable_eos,
21+
)
2022

2123
if TYPE_CHECKING:
2224
from temoa.core.model import TemoaModel

temoa/extensions/framework.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def normalize_extension_ids(extension_ids: Sequence[str | object] | None) -> tup
4343
if not isinstance(ext_id, str):
4444
msg = f'Extension ids must be strings. Received: {type(ext_id).__name__}'
4545
logger.error(msg)
46-
raise ValueError(msg)
46+
raise TypeError(msg)
4747
cleaned = ext_id.strip().lower()
4848
if not cleaned:
4949
continue
@@ -133,10 +133,7 @@ def assert_disabled_extension_tables_are_empty(
133133
if not spec.fail_if_tables_populated_when_disabled:
134134
continue
135135

136-
populated: list[str] = []
137-
for table in spec.owned_tables:
138-
if _table_has_rows(con, table):
139-
populated.append(table)
136+
populated = [table for table in spec.owned_tables if _table_has_rows(con, table)]
140137

141138
if populated:
142139
table_list = ', '.join(sorted(populated))

0 commit comments

Comments
 (0)