Skip to content

Commit b7418e9

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

12 files changed

Lines changed: 49 additions & 68 deletions

File tree

temoa/components/time.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,7 @@ def create_time_sequence(model: TemoaModel) -> None:
308308

309309

310310
def create_time_season_to_sequential(model: TemoaModel) -> None:
311-
if all(
312-
(
313-
not model.tech_seasonal_storage,
314-
not model.ramp_up_hourly,
315-
not model.ramp_down_hourly,
316-
)
317-
):
311+
if not model.tech_seasonal_storage:
318312
# Don't need it anyway
319313
return
320314

@@ -334,11 +328,9 @@ def create_time_season_to_sequential(model: TemoaModel) -> None:
334328
else:
335329
msg = (
336330
f'No data in time_season_sequential but time_sequencing parameter set to '
337-
f'{model.time_sequencing.first()} and inter-season features used. '
331+
f'{model.time_sequencing.first()} and seasonal storage used. '
338332
'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.'
333+
'seasonal storage is used. Check the config file.'
342334
)
343335
logger.error(msg)
344336
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/discrete_capacity/extension.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
register_model_components=register_model_components,
1717
build_manifest_items=build_manifest_items,
1818
schema_sql_path=str(Path(__file__).parent / 'tables.sql'),
19-
fail_if_tables_populated_when_disabled=True,
2019
)

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/economies_of_scale/extension.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@
1717
register_model_components=register_model_components,
1818
build_manifest_items=build_manifest_items,
1919
schema_sql_path=str(Path(__file__).parent / 'tables.sql'),
20-
fail_if_tables_populated_when_disabled=True,
2120
)

temoa/extensions/framework.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class ExtensionSpec:
2929
register_model_components: ModelHook | None = None
3030
build_manifest_items: ManifestHook | None = None
3131
schema_sql_path: str | None = None
32-
fail_if_tables_populated_when_disabled: bool = False
3332

3433

3534
def normalize_extension_ids(extension_ids: Sequence[str | object] | None) -> tuple[str, ...]:
@@ -43,7 +42,7 @@ def normalize_extension_ids(extension_ids: Sequence[str | object] | None) -> tup
4342
if not isinstance(ext_id, str):
4443
msg = f'Extension ids must be strings. Received: {type(ext_id).__name__}'
4544
logger.error(msg)
46-
raise ValueError(msg)
45+
raise TypeError(msg)
4746
cleaned = ext_id.strip().lower()
4847
if not cleaned:
4948
continue
@@ -125,24 +124,19 @@ def merge_regional_group_tables(
125124
def assert_disabled_extension_tables_are_empty(
126125
con: Connection, enabled_specs: Sequence[ExtensionSpec]
127126
) -> None:
128-
"""Fail if disabled extensions with strict guards own tables populated with data."""
127+
"""Warn if disabled extension has table populated with data."""
129128
enabled_ids = {spec.extension_id for spec in enabled_specs}
130129
for spec in get_known_extension_specs().values():
131130
if spec.extension_id in enabled_ids:
132131
continue
133-
if not spec.fail_if_tables_populated_when_disabled:
134-
continue
135132

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

141135
if populated:
142136
table_list = ', '.join(sorted(populated))
143137
msg = (
144138
f"Extension '{spec.extension_id}' is not enabled, but extension-owned table(s) "
145-
f'contain data: {table_list}. Enable the extension or remove those rows.'
139+
f'contain data: {table_list}.'
146140
)
147141
logger.warning(msg)
148142

temoa/extensions/growth_rates/extension.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@
2727
register_model_components=register_model_components,
2828
build_manifest_items=build_manifest_items,
2929
schema_sql_path=str(Path(__file__).parent / 'tables.sql'),
30-
fail_if_tables_populated_when_disabled=True,
3130
)

temoa/extensions/template/extension.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@
2828
build_manifest_items=build_manifest_items,
2929
# Schema applied (with consent) when enabled but tables are missing.
3030
schema_sql_path=str(Path(__file__).parents[0] / 'tables.sql'),
31-
# If True, loading fails when this extension is DISABLED but its tables hold
32-
# data, preventing silently-ignored inputs.
33-
fail_if_tables_populated_when_disabled=True,
3431
)

temoa/extensions/unit_commitment/extension.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@
1919
register_model_components=register_model_components,
2020
build_manifest_items=build_manifest_items,
2121
schema_sql_path=str(Path(__file__).parent / 'tables.sql'),
22-
fail_if_tables_populated_when_disabled=True,
2322
)

0 commit comments

Comments
 (0)