def test_eA1c_mmol_equal_mg(self): self.simple_df[GLUCOSE] = len(self.simple_df) * [100] simple_df_mmol = self.simple_df.copy() simple_df_mmol[GLUCOSE] = simple_df_mmol[GLUCOSE] / 18.02 index_mg = indices.GVeA1c(df=self.simple_df, calc_config=self.mock_5_mg_config) index_mmol = indices.GVeA1c(df=simple_df_mmol, calc_config=self.mock_5_mmol_config) self.assertAlmostEqual( index_mg.calculate(), index_mmol.calculate(), msg="\neA1c based on mg: {}. eA1c based on mmol: {}\n".format( index_mg.calculate(), index_mmol.calculate()))
def test_mage_join_extremas_minimas_first(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) minimas = [1, 3, 5] maximas = [2, 4, 6] self.assertListEqual(list1=[1, 2, 3, 4, 5, 6], list2=index._join_extremas(minimas, maximas))
def test_mage_moving_average_window_not_int(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) arr = pd.Series([1, 3, 4, 6, 7]) window_size = "test" with self.assertRaises(ValueError): index._moving_average(arr, window_size)
def test_mage_moving_average_arr_not_series(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) arr = [1, 3, 4, 6, 7] window_size = 2 with self.assertRaises(ValueError): index._moving_average(arr, window_size)
def test_mage_moving_average_simple_array(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) arr = pd.Series([1, 3, 4, 6, 7]) window_size = 2 self.assertTrue( np.array_equal(index._moving_average(arr, window_size), np.array([2, 3.5, 5, 6.5])))
def test_mage_join_extremas_util_maximas_first(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) minimas = [2, 4, 6] maximas = [1, 3, 5] joined = [1] minimas_turn = True index._join_extremas_util(joined, minimas, maximas, 0, 1, minimas_turn) self.assertListEqual(list1=[1, 2, 3, 4, 5, 6], list2=joined)
def test_mage_join_extremas_util_different_lengths_arrays(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) minimas = [1, 3, 5] maximas = [2, 4, 6, 7] joined = [1] minimas_turn = False index._join_extremas_util(joined, minimas, maximas, 1, 0, minimas_turn) self.assertListEqual(list1=[1, 2, 3, 4, 5, 6], list2=joined)
def test_mean_hypo_event_duration_no_hypo(self): self.simple_df = pd.DataFrame({ DT: pd.date_range(start="27-07-2020 12:00", periods=8, freq="5min"), GLUCOSE: [10, 10, 10, 0, 0, 10, 10, 10] }) index = indices.GVmean_hypo_event_duration( df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertEqual(first=index.calculate(threshold=9, records_duration=15), second=0)
def test_conga_all_values_the_same(self): # Variance should obviously be 0 for a measurement # made up of all the same values dates = pd.date_range("12:00 2020/09/11", periods=500, freq="5min") values = np.repeat(100, repeats=500) test_df = pd.DataFrame({GLUCOSE: values}, index=dates) index = indices.GVcongaX(df=test_df, calc_config=self.mock_5_mg_config) self.assertEqual( 0, index.calculate(2), msg="Returned: {}. Expected 0".format(index.calculate(2)), )
def test_mage_join_extremas_minimas_doubled(self): index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) minimas = [1, 3, 5] maximas = [2, 6] self.assertListEqual( list1=[1, 2, 3, 6], list2=index._join_extremas(minimas, maximas), msg="_join_extremas returned: {}, expected: {}".format( index._join_extremas(minimas, maximas), [1, 2, 3, 6], ))
def test_mage_runtime_error(self): # integration test in fact self.simple_df = pd.DataFrame({ DT: pd.date_range(start="27-07-2020 12:00", periods=50, freq="5min"), GLUCOSE: 50 * [100] }) index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) with self.assertRaises(RuntimeError): index.calculate()
def test_mage_basic_df(self): # integration test in fact self.simple_df = pd.DataFrame({ DT: pd.date_range(start="27-07-2020 12:00", periods=50, freq="5min"), GLUCOSE: 50 * [100] }) self.simple_df.iloc[10, 1] = 50 self.simple_df.iloc[20, 1] = 50 self.simple_df.iloc[30, 1] = 300 self.simple_df.iloc[40, 1] = 200 index = indices.GVmage(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertEqual(first=index.calculate(), second=0)
def test_modd_simple_examples(self): same_values = pd.DataFrame({ DT: pd.date_range(start="10/11/2020 12:00", periods=3 * 256, freq="5min"), GLUCOSE: 3 * 256 * [100], }) index = indices.GVmodd(df=same_values, calc_config=self.mock_5_mg_config) self.assertEqual( index.calculate(), 0, msg="Expected 0 for the array of all equal values. Returned: {}\n". format(index.calculate()))
def test_set_calc_config_not_calcconfig(self): with self.assertRaises(ValueError): indices.GVIndex(df=self.simple_df, calc_config="wrong_class")
def test_call_df_not_pandas(self): with self.assertRaises(ValueError): test_ind = indices.GVIndex(df=self.simple_df, calc_config=self.mock_5_mg_config) indices.GVIndex.__call__(test_ind, df="test")
def test_set_df_not_pandas(self): with self.assertRaises(ValueError): indices.GVIndex(df="test", calc_config=self.mock_5_mg_config)
def test_calculate_not_implemented(self): with self.assertRaises(NotImplementedError): indices.GVIndex(df=self.simple_df, calc_config=self.mock_5_mg_config).calculate()
def test_hbgi(self): index = indices.GVhbgi(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertAlmostEqual(first=0, second=index.calculate())
def test_lbgi(self): index = indices.GVlbgi(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertAlmostEqual(first=567.362808824868, second=index.calculate())
def test_mean_hypo_event_duration_records_duration(self): index = indices.GVmean_hypo_event_duration( df=self.simple_df, calc_config=self.mock_5_mg_config) with self.assertRaises(ValueError): index.calculate(threshold=0, records_duration="wrong_type")
def test_time_in_hypo_value_error(self): index = indices.GVtime_in_hypo(df=self.simple_df, calc_config=self.mock_5_mg_config) with self.assertRaises(ValueError): index.calculate(threshold="test")
def test_time_in_hypo(self): index = indices.GVtime_in_hypo(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertEqual(first=index.calculate(threshold=3), second=10)
def test_hypo_events_count_threshold_duration_not_int(self): index = indices.GVhypo_events_count(df=self.simple_df, calc_config=self.mock_5_mg_config) with self.assertRaises(ValueError): index.calculate(threshold=0, threshold_duration="test")
def test_call_self_calculate_on_call(self): with self.assertRaises(NotImplementedError): base_index = indices.GVIndex(calc_config=self.mock_5_mg_config) base_index(self.simple_df)
def test_eA1c_simple_df(self): self.simple_df[GLUCOSE] = len(self.simple_df) * [100] index = indices.GVeA1c(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertAlmostEqual(first=index.calculate(), second=5.09752973287909)
def test_grade_hyper_mmol(self): self.simple_df[GLUCOSE] = len(self.simple_df) * [15] index = indices.GVgrade_hyper(df=self.simple_df, calc_config=self.mock_5_mmol_config) self.assertAlmostEqual(first=index.calculate(), second=1)
def test_auc_simple_df_no_stand(self): # test on a df with all 1s self.simple_df[GLUCOSE] = len(self.simple_df) * [1] index = indices.GVauc(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertEqual(index.calculate(standardize=False), 20)
def test_time_in_range_value_error2(self): index = indices.GVtime_in_range(df=self.simple_df, calc_config=self.mock_5_mg_config) with self.assertRaises(ValueError): index.calculate(lower_bound="test", upper_bound=1)
def test_hypo_events_count_simple_df(self): self.simple_df[GLUCOSE] = [1, 1, 3, 2, 2] index = indices.GVhypo_events_count(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertEqual(index.calculate(threshold=3, threshold_duration=10), 2)
def test_time_in_range_accept_floats(self): index = indices.GVtime_in_range(df=self.simple_df, calc_config=self.mock_5_mg_config) self.assertEqual(index.calculate(lower_bound=1.0, upper_bound=5.0), 0.6)