def test_fill_missing_dates_shifted_clean_config_int(self): dates = pd.Series([ "2020/08/18 12:00", "2020/08/18 12:16", "2020/08/18 12:21", "2020/08/18 12:31", ]) dates = pd.to_datetime(dates) glucose = pd.Series([0, 1, 2, 3], dtype=np.float64) df = pd.DataFrame({DT: dates, GLUCOSE: glucose}) fill_config = configs.CleanConfig(interval=5, use_api=False, fill_glucose_tolerance=2) self.FileCleaner.set_clean_config(fill_config) filled = self.FileCleaner._fill_missing_dates(df) expect = pd.DataFrame({ DT: pd.date_range("2020-08-18 12:00", periods=7, freq="5min"), GLUCOSE: [0.0, np.nan, np.nan, 1.0, 2.0, np.nan, 3.0] }) self.assertTrue( filled.equals(expect), "\nMethod result:\n{}\nExpected:\n{}\n".format(filled, expect))
def test_call_with_wrong_api_name(self): wrong_api_config = configs.CleanConfig(interval=5, use_api="wrong_api_name") array = np.array(80 * [7]) fixer = DateFixer.DateFixer(wrong_api_config) with self.assertRaises(ValueError): fixer(array)
def test_predict_api_fallback_on_local(self, mocked_post, mocked_json, mocked_raise): api_config = configs.CleanConfig(interval=5, use_api="metronome") fixer = DateFixer.DateFixer(api_config) fixer._predict_local = mock.Mock() example_dict = {"key": "test"} fixer._predict_api(example_dict) fixer._predict_local.assert_called_once()
def test_fix_dates_clean_config_int(self): long_df = pd.DataFrame({ DT: pd.date_range("2020/08/18 19:00", "2020/08/18 22:00", freq="5min"), GLUCOSE: 37 * [80], }) api_config = configs.CleanConfig(5, use_api="metronome") self.FileCleaner.set_clean_config(api_config) res = self.FileCleaner.fix_dates(long_df) np.testing.assert_allclose(res, np.array(37 * [1]))
def test_predict_api_calls(self, mocked_post, mocked_json): api_config = configs.CleanConfig(interval=5, use_api="metronome") fixer = DateFixer.DateFixer(api_config) mocked_post.raise_for_status = mock.Mock() example_dict = {"key": "test"} call_res = fixer._predict_api(example_dict) self.assertTrue(call_res) mocked_post.assert_called_once_with( api_config._construct_full_api_address(), json=json.dumps(example_dict), timeout=0.5)
def test_fill_glucose_values_with_nearest_clean_config_int(self): dates = pd.Series([ "2020/08/18 12:00", "2020/08/18 12:01", "2020/08/18 12:05", "2020/08/18 12:10", "2020/08/18 12:11", "2020/08/18 12:15", "2020/08/18 12:20", "2020/08/18 12:25", "2020/08/18 12:29", "2020/08/18 12:30", ]) dates = pd.to_datetime(dates) glucose = pd.Series([np.nan, 1, 2, np.nan, 4, 5, 6, 7, 8, np.nan], dtype=np.float64) not_fixed = pd.DataFrame({DT: dates, GLUCOSE: glucose}) not_fixed[GLUCOSE] = not_fixed[GLUCOSE].astype(np.float64) date_fixed = not_fixed.iloc[[0, 2, 3, 5, 6, 7, 9], :].copy() fill_config = configs.CleanConfig(interval=5, use_api=False, fill_glucose_tolerance=2) self.FileCleaner.set_clean_config(fill_config) filled = self.FileCleaner._fill_glucose_values_with_nearest( date_fixed, not_fixed) expect = pd.DataFrame( { DT: pd.date_range( "2020-08-18 12:00:00", periods=7, freq="5min"), GLUCOSE: np.array([1, 2, 4, 5, 6, 7, 8], dtype=np.float), }, index=[0, 2, 3, 5, 6, 7, 9]) self.assertTrue(filled.equals(expect))
def setUp(self): self.config_5 = configs.CleanConfig(interval=5, use_api=False) self.api_config_5 = configs.CleanConfig(interval=5, use_api="metronome") self.fixer_5 = DateFixer.DateFixer(self.config_5) self.variables_no = WINDOW_SIZE - 1
def test_tidy_simple_df_clean_config_int(self): # DF setup # couple of issues to solve in this dates dates = [ "2020/08/18 12:00", "2020/08/18 12:05", "2020/08/18 12:05:01", # shift by 1sec "2020/08/18 12:10:01", "2020/08/18 12:15:01", "2020/08/18 12:20:02", # another shift "2020/08/18 12:25:02", np.nan, # nan "2020/08/18 12:30:02", "2020/08/18 12:35:02", "2020/08/18 12:35:57", # additional timepoints "2020/08/18 12:37:00", "2020/08/18 12:40:02", "2020/08/18 12:45:02", "2020/08/18 12:50:02", "2020/08/18 12:55:02", "2020/08/18 13:00:02", "2020/08/18 13:05:02", "2020/08/18 13:10:02", "2020/08/18 13:15:01", "2020/08/18 13:20:01", "2020/08/18 13:25", "2020/08/18 13:30", "2020/08/18 13:35", "2020/08/18 13:40", "2020/08/18 13:50", # missing timepoint "2020/08/18 13:55", "2020/08/18 14:00", "2020/08/18 14:05", "2020/08/18 14:10", "2020/08/18 14:15", "2020/08/18 14:20", "2020/08/18 14:25", "2020/08/18 14:30", "2020/08/18 14:35", "2020/08/18 14:40", ] dates = pd.to_datetime(dates) glucose = np.array(len(dates) * [80], dtype=np.float) glucose[9] = np.nan df = pd.DataFrame({ DT: dates, GLUCOSE: glucose, }) # Clean config setup clean_config = configs.CleanConfig(interval=5, use_api="metronome", fill_glucose_tolerance=2) cleaner = FileCleaner(df, clean_config) # Tidy! res = cleaner.tidy() # Expected setup expect_index = pd.date_range("2020-08-18 12:00", "2020-08-18 14:40", freq="5min") glucose = len(expect_index) * [80] glucose[7] = np.nan glucose[21] = np.nan expect = pd.DataFrame({ DT: expect_index, GLUCOSE: glucose, }) self.assertTrue(res.equals(expect))