def setUp(self): """Setup files and base data for utility testing.""" self.data_path = example_data self.data_path_full = example_data_full data = [ [np.nan, 48.1, 48.3, 65.1, 47.1, 49.9, 49.5, 48.9, 35.5, 44.5], [51.9, 44.3, 44.5, 58.7, 41.1, 43.9, 43.9, 44.5, 31.1, 40.5], [48.5, 38.5, 37.1, 53.9, 27.1, 40.7, 34.5, 41.7, 28.7, 37.7], [33.9, 21.5, 20.1, 48.7, 12.1, 38.5, 13.9, 33.7, 25.3, 28.1], [19.1, 10.9, 9.9, 42.3, 4.3, 36.5, 6.1, 16.9, 13.9, 13.3], [12.7, 4.7, 3.9, 31.3, 3.1, 29.1, 3.7, 4.9, 6.9, 4.7], ] keys = ["V%s" % (i + 1) for i in range(6)] self.expected_dict = {key: data[i] for i, key in enumerate(keys)} self.expected_dict_nh = {i: row for i, row in enumerate(data)} self.expected_dict_no_nan = deepcopy(self.expected_dict) self.expected_dict_no_nan["V1"][0] = 56.5 self.expected_arr = np.array(data).T self.expected_var_names = keys self.const_data = deepcopy(self.expected_arr) self.const_data[:, 0] = np.ones_like(self.expected_arr[:, 0]) self.stats_obj = ui.DVHAStats(self.data_path)
def test_control_chart_if_const_data(self): """Test that const data does not crash control chart""" stats_obj = ui.DVHAStats(self.const_data) ucc = stats_obj.univariate_control_charts(box_cox=True) ucc[0].show() self.assertEqual( ucc[0].plot_title, "Cannot calculate control chart with const data!", )
def test_pca(self): """Test PCA initialization and plot""" stats_obj = ui.DVHAStats(self.expected_dict_no_nan) pca = stats_obj.pca() fig = pca.show() pca.close(fig) # Test no transform stats_obj.pca(transform=False)
def test_hotelling_t2_box_cox(self): """Test multivariate control chart creation and values""" stats_obj = ui.DVHAStats(self.data_path_full) ht2 = stats_obj.hotelling_t2(box_cox=True, const_policy="omit") self.assertEqual(round(ht2.center_line, 3), 5.375) lcl, ucl = ht2.control_limits self.assertEqual(round(lcl, 3), 0) self.assertEqual(round(ucl, 3), 13.555) self.assertEqual(len(ht2.out_of_control), 2)
def test_del_const_var(self): """Test init deletes constant variables if del_const_vars is True""" stats_obj = ui.DVHAStats( self.const_data, var_names=self.expected_var_names, del_const_vars=True, ) self.assertEqual(stats_obj.deleted_vars, ["V1"]) assert_array_equal(stats_obj.data, np.delete(self.const_data, 0, axis=1))
def test_univariate_control_chart_box_cox(self): """Test univariate control chart creation and values with Box-Cox""" stats_obj = ui.DVHAStats(self.data_path_full) ucc = stats_obj.univariate_control_charts(box_cox=True, const_policy="omit") self.assertEqual(round(ucc[0].center_line, 3), 1835.702) lcl, ucl = ucc[0].control_limits self.assertEqual(round(lcl, 3), 136.258) self.assertEqual(round(ucl, 3), 3535.147) self.assertEqual(len(ucc[0].out_of_control), 3) self.assertEqual(len(ucc[0].out_of_control_high), 1) self.assertEqual(len(ucc[0].out_of_control_low), 2)
def test_multi_variable_regression(self): """Test Multi-Variable Linear Regression""" y = np.linspace(1, 10, 10) stats_obj = ui.DVHAStats(self.expected_dict_no_nan) mvr = stats_obj.linear_reg(y) mvr2 = stats_obj.linear_reg(y, saved_reg=mvr) assert_array_equal(mvr.residuals, mvr2.residuals) fig = mvr.show() mvr.close(fig) fig = mvr.show("prob") mvr.close(fig) self.assertIsNone(mvr.show("test")) mvr3 = stats_obj.linear_reg("V1", reg_vars=["V1", "V2", "V3"]) self.assertEqual(mvr3.var_names, ["V2", "V3"]) assert_array_equal(stats_obj.data[:, 0], mvr3.y)
def test_show_calls(self): """Test matplotlib show calls""" fig = self.stats_obj.show(0) self.stats_obj.close(fig) fig = self.stats_obj.show(0, plot_type="hist") self.stats_obj.close(fig) corr_mat = self.stats_obj.correlation_matrix(corr_type="Spearman") fig = corr_mat.show() corr_mat.close(fig) ucc = self.stats_obj.univariate_control_charts() fig = ucc[0].show() ucc[0].close(fig) ht2 = self.stats_obj.hotelling_t2() fig = ht2.show() ht2.close(fig) y = list(range(10)) s = ui.DVHAStats(self.expected_dict_no_nan) ra_cc = s.risk_adjusted_control_chart(y) fig = ra_cc.show() ra_cc.close(fig) fig = self.stats_obj.show(plot_type="box") self.stats_obj.close(fig) # test if plot_type not accepted with self.assertRaises(NotImplementedError): self.stats_obj.show(plot_type="test") # test if var_name = None and plot_type not "box" with self.assertRaises(NotImplementedError): self.stats_obj.show()
def test_invalid_data_import(self): with self.assertRaises(NotImplementedError): ui.DVHAStats("test")
def test_csv_import(self): """Test csv importing""" stats_obj = ui.DVHAStats(self.data_path) assert_array_equal(stats_obj.data, self.expected_arr) self.assertEqual(stats_obj.var_names, self.expected_var_names)
def test_dict_import(self): """Test dict importing""" stats_obj = ui.DVHAStats(self.expected_dict) assert_array_equal(stats_obj.data, self.expected_arr) self.assertEqual(stats_obj.var_names, self.expected_var_names)
def test_box_cox_const_policy_raise(self): """Test const_policy='raise' results in ValueError with const data""" stats_obj = ui.DVHAStats(self.const_data) with self.assertRaises(ValueError): stats_obj.box_cox_by_index(0, const_policy="raise")