Backfill demand defaults per (region, demand), not per demand name.
The new key shape and the final validation are region-scoped, but this block still collapses demand_specific_distribution down to just dem. If one region supplies a custom distribution and another relies on defaults, nothing gets backfilled for the second region and the later (r, dem) validation raises. Compute the unset set from (r, dem) pairs before expanding across seasons and times-of-day.
- demands_specified = set(
- map(
- demand_specific_distributon_dem,
- (i for i in demand_specific_distribution.sparse_iterkeys()),
- )
- )
- unset_demand_distributions = used_dems.difference(
- demands_specified
- ) # the demands not mentioned in DSD *at all*
+ used_r_dems = {(r, dem) for r, _p, dem in model.demand.sparse_iterkeys()}
+ specified_r_dems = {
+ (r, dem) for r, _s, _d, dem in demand_specific_distribution.sparse_iterkeys()
+ }
+ unset_r_dems = used_r_dems.difference(specified_r_dems)
- if unset_demand_distributions:
- unset_distributions = set(
- cross_product(
- model.regions,
- model.time_season,
- model.time_of_day,
- unset_demand_distributions,
- )
- )
- for r, s, d, dem in unset_distributions:
- demand_specific_distribution[r, s, d, dem] = value(
- model.segment_fraction[s, d]
- ) # DSD._constructed = True
+ if unset_r_dems:
+ for r, dem in unset_r_dems:
+ for s, d in cross_product(model.time_season, model.time_of_day):
+ demand_specific_distribution[r, s, d, dem] = value(
+ model.segment_fraction[s, d]
+ )
- used_r_dems = {(r, dem) for r, p, dem in model.demand.sparse_iterkeys()}
for r, dem in used_r_dems:
From coderabbit:
Backfill demand defaults per (region, demand), not per demand name.
The new key shape and the final validation are region-scoped, but this block still collapses demand_specific_distribution down to just dem. If one region supplies a custom distribution and another relies on defaults, nothing gets backfilled for the second region and the later (r, dem) validation raises. Compute the unset set from (r, dem) pairs before expanding across seasons and times-of-day.
Suggested fix