@@ -238,8 +238,9 @@ def flow_system_piecewise_conversion_scenarios(flow_system_complex_scenarios) ->
238238def test_weights (flow_system_piecewise_conversion_scenarios ):
239239 """Test that scenario weights are correctly used in the model."""
240240 scenarios = flow_system_piecewise_conversion_scenarios .scenarios
241- weights = np .linspace (0.5 , 1 , len (scenarios ))
242- flow_system_piecewise_conversion_scenarios .weights = weights
241+ scenario_weights = np .linspace (0.5 , 1 , len (scenarios ))
242+ flow_system_piecewise_conversion_scenarios .scenario_weights = scenario_weights
243+ flow_system_piecewise_conversion_scenarios .weights = flow_system_piecewise_conversion_scenarios ._compute_weights ()
243244 model = create_linopy_model (flow_system_piecewise_conversion_scenarios )
244245 normalized_weights = (
245246 flow_system_piecewise_conversion_scenarios .weights / flow_system_piecewise_conversion_scenarios .weights .sum ()
@@ -254,11 +255,14 @@ def test_weights(flow_system_piecewise_conversion_scenarios):
254255def test_weights_io (flow_system_piecewise_conversion_scenarios ):
255256 """Test that scenario weights are correctly used in the model."""
256257 scenarios = flow_system_piecewise_conversion_scenarios .scenarios
257- weights = np .linspace (0.5 , 1 , len (scenarios )) / np .sum (np .linspace (0.5 , 1 , len (scenarios )))
258- flow_system_piecewise_conversion_scenarios .weights = weights
258+ scenario_weights = np .linspace (0.5 , 1 , len (scenarios )) / np .sum (np .linspace (0.5 , 1 , len (scenarios )))
259+ flow_system_piecewise_conversion_scenarios .scenario_weights = scenario_weights
260+ flow_system_piecewise_conversion_scenarios .weights = flow_system_piecewise_conversion_scenarios ._compute_weights ()
259261 model = create_linopy_model (flow_system_piecewise_conversion_scenarios )
260- np .testing .assert_allclose (model .objective_weights .values , weights )
261- assert_linequal (model .objective .expression , (model .variables ['costs' ] * weights ).sum () + model .variables ['Penalty' ])
262+ np .testing .assert_allclose (model .objective_weights .values , scenario_weights )
263+ assert_linequal (
264+ model .objective .expression , (model .variables ['costs' ] * scenario_weights ).sum () + model .variables ['Penalty' ]
265+ )
262266 assert np .isclose (model .objective_weights .sum ().item (), 1.0 )
263267
264268
@@ -317,8 +321,9 @@ def test_io_persistence(flow_system_piecewise_conversion_scenarios):
317321def test_scenarios_selection (flow_system_piecewise_conversion_scenarios ):
318322 flow_system_full = flow_system_piecewise_conversion_scenarios
319323 scenarios = flow_system_full .scenarios
320- weights = np .linspace (0.5 , 1 , len (scenarios )) / np .sum (np .linspace (0.5 , 1 , len (scenarios )))
321- flow_system_full .weights = weights
324+ scenario_weights = np .linspace (0.5 , 1 , len (scenarios )) / np .sum (np .linspace (0.5 , 1 , len (scenarios )))
325+ flow_system_full .scenario_weights = scenario_weights
326+ flow_system_full .weights = flow_system_full ._compute_weights ()
322327 flow_system = flow_system_full .sel (scenario = scenarios [0 :2 ])
323328
324329 assert flow_system .scenarios .equals (flow_system_full .scenarios [0 :2 ])
@@ -696,13 +701,13 @@ def test_weights_io_persistence():
696701 """Test that weights persist through IO operations (to_dataset/from_dataset)."""
697702 timesteps = pd .date_range ('2023-01-01' , periods = 24 , freq = 'h' )
698703 scenarios = pd .Index (['base' , 'mid' , 'high' ], name = 'scenario' )
699- custom_weights = np .array ([0.3 , 0.5 , 0.2 ])
704+ custom_scenario_weights = np .array ([0.3 , 0.5 , 0.2 ])
700705
701- # Create FlowSystem with custom weights
706+ # Create FlowSystem with custom scenario weights
702707 fs_original = fx .FlowSystem (
703708 timesteps = timesteps ,
704709 scenarios = scenarios ,
705- weights = custom_weights ,
710+ scenario_weights = custom_scenario_weights ,
706711 )
707712
708713 bus = fx .Bus ('grid' )
@@ -737,13 +742,13 @@ def test_weights_selection():
737742 """Test that weights are correctly sliced when using FlowSystem.sel()."""
738743 timesteps = pd .date_range ('2023-01-01' , periods = 24 , freq = 'h' )
739744 scenarios = pd .Index (['base' , 'mid' , 'high' ], name = 'scenario' )
740- custom_weights = np .array ([0.3 , 0.5 , 0.2 ])
745+ custom_scenario_weights = np .array ([0.3 , 0.5 , 0.2 ])
741746
742- # Create FlowSystem with custom weights
747+ # Create FlowSystem with custom scenario weights
743748 fs_full = fx .FlowSystem (
744749 timesteps = timesteps ,
745750 scenarios = scenarios ,
746- weights = custom_weights ,
751+ scenario_weights = custom_scenario_weights ,
747752 )
748753
749754 bus = fx .Bus ('grid' )
@@ -765,7 +770,7 @@ def test_weights_selection():
765770
766771 # Verify weights are correctly sliced
767772 assert fs_subset .scenarios .equals (pd .Index (['base' , 'high' ], name = 'scenario' ))
768- np .testing .assert_allclose (fs_subset .weights .values , custom_weights [[0 , 2 ]])
773+ np .testing .assert_allclose (fs_subset .weights .values , custom_scenario_weights [[0 , 2 ]])
769774
770775 # Verify weights are 1D with just scenario dimension (no period dimension)
771776 assert fs_subset .weights .dims == ('scenario' ,)
0 commit comments