@@ -9133,6 +9133,7 @@ def run_test_units(my_predbat):
91339133
91349134 return failed
91359135
9136+
91369137def add_incrementing_sensor_total (data ):
91379138 max_entry = max (data .keys ()) if data else 0
91389139 total = 0
@@ -9141,13 +9142,14 @@ def add_incrementing_sensor_total(data):
91419142 total += increment
91429143 return total
91439144
9145+
91449146def test_previous_days_modal_filter (my_predbat ):
91459147 """
91469148 Test the previous_days_modal_filter function
91479149 """
91489150 print ("**** Running previous_days_modal_filter tests ****" )
91499151 failed = False
9150-
9152+
91519153 # Set up test environment
91529154 my_predbat .load_minutes_age = 7 # 7 days of data
91539155 my_predbat .days_previous = [1 , 2 ] # Test with 3 days
@@ -9159,19 +9161,21 @@ def test_previous_days_modal_filter(my_predbat):
91599161 my_predbat .iboost_energy_subtract = False
91609162 my_predbat .iboost_energy_today = None
91619163 my_predbat .base_load = 0.0
9162-
9164+
91639165 # Mock the get_arg method
91649166 original_get_arg = my_predbat .get_arg
9167+
91659168 def mock_get_arg (key , default = None ):
91669169 if key == "load_filter_threshold" :
91679170 return 30
91689171 return original_get_arg (key , default )
9172+
91699173 my_predbat .get_arg = mock_get_arg
9170-
9174+
91719175 # Test 1: Empty data set - should be filled with gap filling logic
91729176 print ("Test 1: Empty data set" )
91739177 test_data = {}
9174-
9178+
91759179 # Call the function
91769180 my_predbat .previous_days_modal_filter (test_data )
91779181 data_length = len (test_data )
@@ -9182,7 +9186,7 @@ def mock_get_arg(key, default=None):
91829186 # Check that data has been filled for at least some minutes
91839187 total_filled_data = dp2 (add_incrementing_sensor_total (test_data ))
91849188 expected_total_per_day = 24.0 # 24 kWh per day as default when no data
9185-
9189+
91869190 print ("Total filled data: {} kWh" .format (dp2 (total_filled_data )))
91879191
91889192 # With 2 days and complete gaps, should use 24kWh default for each day
@@ -9195,81 +9199,81 @@ def mock_get_arg(key, default=None):
91959199 # Test 2: Data with gaps - should be filled to create 1 kWh per hour pattern
91969200 print ("Test 2: Data with some gaps" )
91979201 test_data = {}
9198-
9202+
91999203 # Create partial data for 1 day (1 kWh per hour = incrementing total)
92009204 # PREDICT_STEP is 5 minutes, so 12 steps per hour
92019205 # Each step should increment by 1/12 kWh to get 1 kWh per hour
92029206 step_increment = 1.0 / 60
92039207 running_total = 0
9204-
9208+
92059209 # Fill first half of day with proper incremental data
92069210 for minute in range (0 , 12 * 60 ): # 12 hours worth
92079211 running_total += step_increment
92089212 test_data [24 * 60 - minute ] = running_total # Backwards indexing as used in function
9209-
9213+
92109214 # Leave second half empty to test gap filling
9211-
9215+
92129216 # Set up days_previous for this test
92139217 my_predbat .days_previous = [1 ] # Only test with 1 day
92149218 my_predbat .days_previous_weight = [1.0 ]
9215-
9219+
92169220 initial_data_sum = dp2 (add_incrementing_sensor_total (test_data ))
92179221 print ("Initial partial data sum: {} kWh" .format (dp2 (initial_data_sum )))
9218-
9222+
92199223 # Call the function
92209224 my_predbat .previous_days_modal_filter (test_data )
9221-
9225+
92229226 final_data_sum = dp2 (add_incrementing_sensor_total (test_data ))
92239227 print ("Final data sum after gap filling: {} kWh" .format (dp2 (final_data_sum )))
9224-
9228+
92259229 # Should now have approximately 24 kWh total (1 kWh per hour for 24 hours)
92269230 expected_final_total = 24.0
92279231 if abs (final_data_sum - expected_final_total ) > 2.0 : # Allow 2 kWh tolerance
92289232 print ("ERROR: Expected final total around {} kWh, got {} kWh" .format (expected_final_total , final_data_sum ))
92299233 failed = True
92309234 else :
92319235 print ("Gap filling successful: filled from {} kWh to {} kWh" .format (dp2 (initial_data_sum ), dp2 (final_data_sum )))
9232-
9236+
92339237 # Test 3: Modal filtering - remove lowest consumption day
92349238 print ("Test 3: Modal filtering removes lowest day" )
9235-
9239+
92369240 # Reset for modal filter test
92379241 my_predbat .days_previous = [1 , 2 , 3 ]
92389242 my_predbat .days_previous_weight = [1.0 , 1.0 , 1.0 ]
92399243 original_days_count = len (my_predbat .days_previous )
9240-
9244+
92419245 # Create test data with different consumption per day
92429246 test_data = {}
9243-
9247+
92449248 # Day 1: Low consumption (10 kWh total)
92459249 day1_total = 10.0
92469250 step_increment_day1 = day1_total / (24 * 60 )
92479251 running_total = 0
92489252 for minute in range (0 , 24 * 60 ):
92499253 running_total += step_increment_day1
92509254 test_data [24 * 60 - minute ] = running_total
9251-
9252- # Day 2: Medium consumption (20 kWh total)
9255+
9256+ # Day 2: Medium consumption (20 kWh total)
92539257 day2_total = 20.0
92549258 step_increment_day2 = day2_total / (24 * 60 )
92559259 running_total = 0
92569260 for minute in range (0 , 24 * 60 ):
92579261 running_total += step_increment_day2
92589262 test_data [2 * 24 * 60 - minute ] = running_total
9259-
9263+
92609264 # Day 3: High consumption (30 kWh total)
92619265 day3_total = 30.0
92629266 step_increment_day3 = day3_total / (24 * 60 )
92639267 running_total = 0
92649268 for minute in range (0 , 24 * 60 ):
92659269 running_total += step_increment_day3
92669270 test_data [3 * 24 * 60 - minute ] = running_total
9267-
9271+
92689272 print ("Created test data with day totals: {} kWh, {} kWh, {} kWh" .format (day1_total , day2_total , day3_total ))
9269-
9273+
92709274 # Call the function - should remove day 1 (lowest consumption)
92719275 my_predbat .previous_days_modal_filter (test_data )
9272-
9276+
92739277 # Check that one day was removed
92749278 final_days_count = len (my_predbat .days_previous )
92759279 if final_days_count != original_days_count - 1 :
@@ -9280,10 +9284,10 @@ def mock_get_arg(key, default=None):
92809284 failed = True
92819285 else :
92829286 print ("Modal filter correctly removed lowest consumption day" )
9283-
9287+
92849288 # Restore original get_arg method
92859289 my_predbat .get_arg = original_get_arg
9286-
9290+
92879291 return failed
92889292
92899293
0 commit comments