def test_selection_changed(self): """Test modify selection.""" color_searcher = mock() view = mock() color_highlighter = mock() color_selection_listener = ColorSelectionListener( color_searcher, view, color_highlighter) color_region1 = (NormalizedRegion(10, 11), 1) color_region2 = (NormalizedRegion(11, 12), 2) color_region3 = (NormalizedRegion(17, 21), 3) sel_region1 = NormalizedRegion(10, 13) sel_region2 = NormalizedRegion(16, 20) sel_line1 = NormalizedRegion(14, 15) sel_line2 = NormalizedRegion(16, 17) when(view).lines(sel_region1).thenReturn([sel_line1]) when(view).lines(sel_region2).thenReturn([sel_line2]) when(view).sel().thenReturn([sel_region1, sel_region2]) when(color_searcher).search(view, sel_line1).thenReturn( [color_region1 + (None, ), color_region2 + (None, )]) when(color_searcher).search(view, sel_line2).thenReturn( [color_region3 + (None, )]) color_selection_listener.on_selection_modified() color_regions = captor() verify(color_highlighter).highlight_regions(color_regions) self.assertEqual([color_region1, color_region2, color_region3], [region for region in color_regions.value])
def testShouldReturnLastMatchedValue(self): c = captor(contains("foo")) c.matches("foobar") c.matches("foobam") c.matches("bambaz") self.assertListEqual(["foobar", "foobam"], c.all_values) self.assertEqual("foobam", c.value)
def test_marked_dataframe_has_abtauzyklus_column(self): test_config = config.copy() test_config['selected_event'] = 'standard' enriched_df = pd.DataFrame.from_dict({ "time": [1.0, 2.0, 3.0], "outdoor": [14.2, 4.1, 1.32], "inlet": [99.2, 91.2, 192.2], "room": [13.2, 61.2, 02.3], "freshAirIntake": [13.0, 34.2, 34.0], "condenser": [17.0, 17.0, 17.0], "evaporator": [17.0, 17.0, 17.0], "outlet": [17.0, 17.0, 17.0], 'evaporator_deriv': [17.0, 17.0, 17.0], 'evaporator_pct_ch': [17.0, 17.0, 17.0], 'evaporator_ch_abs': [17.0, 17.0, 17.0], 'evaporator_diff': [0.0, 0.0, 0.0], 'condenser_deriv': [17.0, 17.0, 17.0], 'condenser_pct_ch': [17.0, 17.0, 17.0], 'condenser_ch_abs': [17.0, 17.0, 17.0], 'condenser_diff': [0.0, 0.0, 0.0] }) enriched_df["time"] = pd.to_datetime(enriched_df["time"]) enriched_df = enriched_df.set_index("time") marked_df = pd.DataFrame.from_dict({ "time": [1.0, 2.0, 3.0], "outdoor": [14.2, 4.1, 1.32], "inlet": [99.2, 91.2, 192.2], "room": [13.2, 61.2, 02.3], "freshAirIntake": [13.0, 34.2, 34.0], "condenser": [17.0, 17.0, 17.0], "evaporator": [17.0, 17.0, 17.0], "outlet": [17.0, 17.0, 17.0], 'evaporator_deriv': [17.0, 17.0, 17.0], 'evaporator_pct_ch': [17.0, 17.0, 17.0], 'evaporator_ch_abs': [17.0, 17.0, 17.0], 'evaporator_diff': [17.0, 17.0, 17.0], 'condenser_deriv': [17.0, 17.0, 17.0], 'condenser_pct_ch': [17.0, 17.0, 17.0], 'condenser_ch_abs': [17.0, 17.0, 17.0], 'condenser_diff': [17.0, 17.0, 17.0], 'abtauzyklus_marker': [1.0, 0.0, 0.0], 'abtauzyklus': [True, True, False], 'warmwasseraufbereitung_marker': [True, True, False], 'warmwasseraufbereitung': [True, True, False] }) marked_df["time"] = pd.to_datetime(marked_df["time"]) marked_df = marked_df.set_index("time") when2(read_manager.read_query, ANY, ANY).thenReturn(enriched_df) df_captor = captor(any(pd.DataFrame)) when2(write_manager.write_dataframe, ANY, df_captor, ANY) trainingsdata.mark_data(test_config) df = df_captor.value self.assertListEqual(df.columns.tolist(), marked_df.columns.tolist())
def test_enriched_dataframe_has_correct_format(self): test_config = config.copy() test_config['selected_event'] = 'standard' example_df = pd.DataFrame.from_dict({ "time": [1.0, 2.0, 3.0], "outdoor": [14.2, 4.1, 1.32], "inlet": [99.2, 91.2, 192.2], "room": [13.2, 61.2, 02.3], "freshAirIntake": [13.0, 34.2, 34.0], "condenser": [17.0, 17.0, 17.0], "evaporator": [17.0, 17.0, 17.0], "outlet": [17.0, 17.0, 17.0] }) example_df["time"] = pd.to_datetime(example_df["time"]) example_df = example_df.set_index("time") enriched_df = pd.DataFrame.from_dict({ "time": [1.0, 2.0, 3.0], "outdoor": [14.2, 4.1, 1.32], "inlet": [99.2, 91.2, 192.2], "room": [13.2, 61.2, 02.3], "freshAirIntake": [13.0, 34.2, 34.0], "condenser": [17.0, 17.0, 17.0], "evaporator": [17.0, 17.0, 17.0], "outlet": [17.0, 17.0, 17.0], 'evaporator_deriv': [17.0, 17.0, 17.0], 'evaporator_pct_ch': [17.0, 17.0, 17.0], 'evaporator_ch_abs': [17.0, 17.0, 17.0], 'evaporator_diff': [17.0, 17.0, 17.0], 'condenser_deriv': [17.0, 17.0, 17.0], 'condenser_pct_ch': [17.0, 17.0, 17.0], 'condenser_ch_abs': [17.0, 17.0, 17.0], 'condenser_diff': [17.0, 17.0, 17.0], 'room_deriv': [17.0, 17.0, 17.0], 'room_pct_ch': [17.0, 17.0, 17.0], 'room_ch_abs': [17.0, 17.0, 17.0], 'room_diff': [17.0, 17.0, 17.0], 'inlet_deriv': [17.0, 17.0, 17.0], 'inlet_pct_ch': [17.0, 17.0, 17.0], 'inlet_ch_abs': [17.0, 17.0, 17.0], 'inlet_diff': [17.0, 17.0, 17.0] }) enriched_df["time"] = pd.to_datetime(enriched_df["time"]) enriched_df = enriched_df.set_index("time") when2(read_manager.read_query, ANY, ANY).thenReturn(example_df) df_captor = captor(any(pd.DataFrame)) when2(write_manager.write_dataframe, ANY, df_captor, ANY) trainingsdata.enrich_data(test_config) df = df_captor.value self.assertListEqual(df.columns.tolist(), enriched_df.columns.tolist())
def test_selection_no_intersections(self): """Test modify selection with color searher returning regions with no intersections with the selection.""" color_searcher = mock() view = mock() color_highlighter = mock() color_selection_listener = ColorSelectionListener(color_searcher, view, color_highlighter) color_region = (NormalizedRegion(4, 5), 1) sel_region = NormalizedRegion(10, 13) sel_line = NormalizedRegion(14, 15) when(view).lines(sel_region).thenReturn([sel_line]) when(view).sel().thenReturn([sel_region]) when(color_searcher).search(view, sel_line).thenReturn([color_region + (None,)]) color_selection_listener.on_selection_modified() color_regions = captor() verify(color_highlighter).highlight_regions(color_regions) self.assertEqual([], [region for region in color_regions.value])
def test_deduplicate_lines(self): """Test that having two selections on the same line yields that line only one time .""" color_searcher = mock() view = mock() color_highlighter = mock() color_selection_listener = ColorSelectionListener(color_searcher, view, color_highlighter) color_region = (NormalizedRegion(11, 14), 1) sel_region1 = NormalizedRegion(10, 11) sel_region2 = NormalizedRegion(12, 13) sel_line = NormalizedRegion(14, 15) when(view).lines(sel_region1).thenReturn([sel_line]) when(view).lines(sel_region2).thenReturn([sel_line]) when(view).sel().thenReturn([sel_region1, sel_region2]) when(color_searcher).search(view, sel_line).thenReturn([color_region + (None,)]) color_selection_listener.on_selection_modified() color_regions = captor() verify(color_highlighter).highlight_regions(color_regions) self.assertEqual([color_region], [region for region in color_regions.value])
def test_selection_no_intersections(self): """Test modify selection with color searher returning regions with no intersections with the selection.""" color_searcher = mock() view = mock() color_highlighter = mock() color_selection_listener = ColorSelectionListener( color_searcher, view, color_highlighter) color_region = (NormalizedRegion(4, 5), 1) sel_region = NormalizedRegion(10, 13) sel_line = NormalizedRegion(14, 15) when(view).lines(sel_region).thenReturn([sel_line]) when(view).sel().thenReturn([sel_region]) when(color_searcher).search(view, sel_line).thenReturn( [color_region + (None, )]) color_selection_listener.on_selection_modified() color_regions = captor() verify(color_highlighter).highlight_regions(color_regions) self.assertEqual([], [region for region in color_regions.value])
def test_deduplicate_lines(self): """Test that having two selections on the same line yields that line only one time .""" color_searcher = mock() view = mock() color_highlighter = mock() color_selection_listener = ColorSelectionListener( color_searcher, view, color_highlighter) color_region = (NormalizedRegion(11, 14), 1) sel_region1 = NormalizedRegion(10, 11) sel_region2 = NormalizedRegion(12, 13) sel_line = NormalizedRegion(14, 15) when(view).lines(sel_region1).thenReturn([sel_line]) when(view).lines(sel_region2).thenReturn([sel_line]) when(view).sel().thenReturn([sel_region1, sel_region2]) when(color_searcher).search(view, sel_line).thenReturn( [color_region + (None, )]) color_selection_listener.on_selection_modified() color_regions = captor() verify(color_highlighter).highlight_regions(color_regions) self.assertEqual([color_region], [region for region in color_regions.value])
def test_selection_changed(self): """Test modify selection.""" color_searcher = mock() view = mock() color_highlighter = mock() color_selection_listener = ColorSelectionListener(color_searcher, view, color_highlighter) color_region1 = (NormalizedRegion(10, 11), 1) color_region2 = (NormalizedRegion(11, 12), 2) color_region3 = (NormalizedRegion(17, 21), 3) sel_region1 = NormalizedRegion(10, 13) sel_region2 = NormalizedRegion(16, 20) sel_line1 = NormalizedRegion(14, 15) sel_line2 = NormalizedRegion(16, 17) when(view).lines(sel_region1).thenReturn([sel_line1]) when(view).lines(sel_region2).thenReturn([sel_line2]) when(view).sel().thenReturn([sel_region1, sel_region2]) when(color_searcher).search(view, sel_line1).thenReturn([color_region1 + (None,), color_region2 + (None,)]) when(color_searcher).search(view, sel_line2).thenReturn([color_region3 + (None,)]) color_selection_listener.on_selection_modified() color_regions = captor() verify(color_highlighter).highlight_regions(color_regions) self.assertEqual([color_region1, color_region2, color_region3], [region for region in color_regions.value])
def testShouldDefaultMatcherToAny(self): c = captor() c.matches("foo") c.matches(123) self.assertEqual(123, c.value)
def testShouldReturnLastMatchedValue(self): c = captor(contains("foo")) c.matches("foobar") c.matches("foobam") c.matches("bambaz") self.assertEqual("foobam", c.value)
def testShouldReturnNoneValueIfDidntMatch(self): c = captor(contains("foo")) c.matches("bar") self.assertEqual(None, c.value)
def testShouldReturnNoneValueByDefault(self): c = captor(contains("foo")) self.assertEqual(None, c.value)
def testShouldNotSatisfyIfInnerMatcherIsNotSatisfied(self): c = captor(contains("foo")) self.assertFalse(c.matches("barbam"))
def testShouldSatisfyIfInnerMatcherIsSatisfied(self): c = captor(contains("foo")) self.assertTrue(c.matches("foobar"))
def testShouldDefaultMatcherToAny(self): c = captor() c.matches("foo") c.matches(123) self.assertListEqual(["foo", 123], c.all_values) self.assertEqual(123, c.value)
def testShouldReturnNoneValueIfDidntMatch(self): c = captor(contains("foo")) c.matches("bar") self.assertListEqual([], c.all_values) with self.assertRaises(MatcherError): _ = c.value
def testShouldReturnNoneValueByDefault(self): c = captor(contains("foo")) self.assertListEqual([], c.all_values) with self.assertRaises(MatcherError): _ = c.value
def testShouldSatisfyIfInnerMatcherIsSatisfied(self): c = captor(contains("foo")) self.assertTrue(c.matches("foobar")) self.assertListEqual([ "foobar", ], c.all_values)
def test_config_is_valid_should_send_predictions_to_database(self): # valid config valid_config = { "selected_value": "default", "database_options": { "training": { "datasource_nilan_dbname": "name", "datasource_nilan_measurement": "name", "datasource_weatherdata_dbname": "name", "datasource_weatherdata_measurement": "name" }, "prediction": { "datasource_forecast_dbname": "yalla", "datasource_forecast_measurement": "name", "datasource_forecast_register": "name", "datasink_prediction_dbname": "name", "datasink_prediction_measurement": "name" } }, "prediction_options": { "default": [ { "independent": ["outdoor"], "dependent": ["inlet"], "test_sample_size": 0.2 }, { "independent": ["inlet", "outdoor"], "dependent": ["room"], "test_sample_size": 0.2 }, { "independent": ["room"], "dependent": ["freshAirIntake", "condenser", "evaporator", "outlet"], "test_sample_size": 0.2 } ] } } # all_prediction_models example_values = pd.DataFrame.from_dict({ "time": [1.0, 2.0, 3.0], "outdoor": [1.0, 2.0, 3.0], "inlet": [2.0, 4.0, 6.0], # outdoor * 2 "room": [3.0, 6.0, 9.0], # outdoor + inlet "freshAirIntake": [3.0, 3.0, 3.0], # 3 "condenser": [3.0, 3.0, 3.0], # 3 "evaporator": [3.0, 3.0, 3.0], # 3 "outlet": [3.0, 3.0, 3.0] # 3 }) example_values["time"] = pd.to_datetime(example_values["time"]) example_values = example_values.set_index("time") normal = lm.LinearRegression().fit(example_values[["outdoor"]], example_values[["inlet"]]) multiple = lm.LinearRegression().fit(example_values[["inlet", "outdoor"]], example_values[["room"]]) multivariate = lm.LinearRegression().fit(example_values[["room"]], example_values[["freshAirIntake", "condenser", "evaporator", "outlet"]]) all_prediction_models = { "average_score": 0.91, "config": valid_config, "models": [ { "dependent": ["inlet"], "model": normal, }, { "dependent": ["room"], "model": multiple, }, { "dependent": ["freshAirIntake", "condenser", "evaporator", "outlet"], "model": multivariate, }, ] } # weather forecast temperature = { "time": [1, 2, 3], "outdoor": [2, 4, 6] # TODO might need to adjust the key of this } weather_forecast_dataframe = pd.DataFrame(temperature) weather_forecast_dataframe["time"] = pd.to_datetime( weather_forecast_dataframe["time"]) weather_forecast_dataframe = weather_forecast_dataframe.set_index("time") # just say config is valid when2(cfg_validator.validate_config, ANY).thenReturn(True) # when the function tries to get the weather forecast from database, return the custom forecast DataFrame above when2(read_manager.read_data, ANY, measurement=ANY).thenReturn(weather_forecast_dataframe) # when the function tries to get the persisted models, pass the custom one from above when2(model_persistor.load).thenReturn(all_prediction_models) # when the function tries to write the prediction to the database, capture the prediction DataFrame forecast_captor = captor(any(pd.DataFrame)) when2(write_manager.write_dataframe, ANY, forecast_captor, ANY).thenReturn(None) # call function under test with custom config above calculate_prediction(valid_config) # the actual prediction DataFrame actual_forecasts = forecast_captor.value expected_forecasts = pd.DataFrame.from_dict({ "time": [1.0, 2.0, 3.0], "outdoor": [2.0, 4.0, 6.0], "inlet": [4.0, 8.0, 12.0], # outdoor * 2 "room": [6.0, 12.0, 18.0], # outdoor + inlet "freshAirIntake": [3.0, 3.0, 3.0], # 3 "condenser": [3.0, 3.0, 3.0], # 3 "evaporator": [3.0, 3.0, 3.0], # 3 "outlet": [3.0, 3.0, 3.0], # 3 }) expected_forecasts["time"] = pd.to_datetime(expected_forecasts["time"]) expected_forecasts = expected_forecasts.set_index("time") # verify all interactions (default times is 1) verify(pred_api).send_classification_request(ANY) verify(read_manager).read_data(ANY, measurement=ANY) verify(write_manager).write_dataframe(ANY, ANY, ANY) verify(cfg_validator).validate_config(ANY) verify(model_persistor).load() pd.testing.assert_frame_equal(actual_forecasts, expected_forecasts, check_dtype=False)