|
| 1 | +import unittest |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +from missing_mga import missing |
| 5 | +import warnings |
| 6 | + |
| 7 | + |
| 8 | +class TestMissingMethods(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + # Crear un DataFrame de ejemplo para usar en los tests |
| 11 | + self.df = pd.DataFrame({ |
| 12 | + 'A': [1, 2, None, 4, 5], |
| 13 | + 'B': [None, 2, 3, 4, 5], |
| 14 | + 'C': [1, 2, None, None, 5], |
| 15 | + 'D': [1, 2, 3, 4, None], |
| 16 | + }) |
| 17 | + self.missing = missing(self.df) |
| 18 | + |
| 19 | + # Apagar las advertencias de FutureWarning |
| 20 | + warnings.filterwarnings("ignore", category=FutureWarning) |
| 21 | + |
| 22 | + # Apagar las advertencias de DeprecationWarning |
| 23 | + warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 24 | + |
| 25 | + # Tabular functions tests |
| 26 | + |
| 27 | + def test_number_missing(self): |
| 28 | + self.assertEqual(self.missing.number_missing(), 5) |
| 29 | + |
| 30 | + def test_number_missing_by_column(self): |
| 31 | + self.assertEqual(self.missing.number_missing_by_column()['A'], 1) |
| 32 | + |
| 33 | + def test_number_complete(self): |
| 34 | + self.assertEqual(self.missing.number_complete(), 15) |
| 35 | + |
| 36 | + def test_number_complete_by_column(self): |
| 37 | + self.assertEqual(self.missing.number_complete_by_column()['A'], 4) |
| 38 | + |
| 39 | + def test_impute_mean(self): |
| 40 | + df_imputed = self.missing.impute_mean() |
| 41 | + self.assertFalse(df_imputed.isnull().any().any()) |
| 42 | + |
| 43 | + def test_impute_median(self): |
| 44 | + df_imputed = self.missing.impute_median() |
| 45 | + self.assertFalse(df_imputed.isnull().any().any()) |
| 46 | + |
| 47 | + def test_impute_mode(self): |
| 48 | + df_imputed = self.missing.impute_mode() |
| 49 | + self.assertFalse(df_imputed.isnull().any().any()) |
| 50 | + |
| 51 | + def test_impute_knn(self): |
| 52 | + df_imputed = self.missing.impute_knn() |
| 53 | + self.assertFalse(df_imputed.isnull().any().any()) |
| 54 | + |
| 55 | + def test_missing_value_heatmap(self): |
| 56 | + self.assertIsNone(self.missing.missing_value_heatmap()) |
| 57 | + |
| 58 | + def test_drop_missing_rows(self): |
| 59 | + self.missing = self.missing.drop_missing_rows() |
| 60 | + self.assertEqual(len(self.missing), 5) |
| 61 | + |
| 62 | + def test_drop_missing_columns(self): |
| 63 | + self.missing = self.missing.drop_missing_columns() |
| 64 | + self.assertEqual(len(self.missing.columns), 4) |
| 65 | + |
| 66 | + def test_missing_variable_summary(self): |
| 67 | + summary = self.missing.missing_variable_summary() |
| 68 | + self.assertIsInstance(summary, pd.DataFrame) |
| 69 | + |
| 70 | + def test_missing_case_summary(self): |
| 71 | + summary = self.missing.missing_case_summary() |
| 72 | + self.assertIsInstance(summary, pd.DataFrame) |
| 73 | + |
| 74 | + def test_missing_variable_table(self): |
| 75 | + table = self.missing.missing_variable_table() |
| 76 | + self.assertIsInstance(table, pd.DataFrame) |
| 77 | + |
| 78 | + def test_missing_case_table(self): |
| 79 | + table = self.missing.missing_case_table() |
| 80 | + self.assertIsInstance(table, pd.DataFrame) |
| 81 | + |
| 82 | + def test_missing_variable_span(self): |
| 83 | + span = self.missing.missing_variable_span(variable='A', span_every=2) |
| 84 | + self.assertIsInstance(span, pd.DataFrame) |
| 85 | + |
| 86 | + def test_missing_variable_run(self): |
| 87 | + run = self.missing.missing_variable_run(variable='A') |
| 88 | + self.assertIsInstance(run, pd.DataFrame) |
| 89 | + |
| 90 | + def test_sort_variables_by_missingness(self): |
| 91 | + sorted_df = self.missing.sort_variables_by_missingness() |
| 92 | + self.assertEqual(len(sorted_df.columns), 4) |
| 93 | + |
| 94 | + def test_create_shadow_matrix(self): |
| 95 | + shadow_matrix = self.missing.create_shadow_matrix() |
| 96 | + self.assertEqual(len(shadow_matrix.columns), 4) |
| 97 | + |
| 98 | + def test_bind_shadow_matrix(self): |
| 99 | + bound_matrix = self.missing.bind_shadow_matrix() |
| 100 | + self.assertEqual(len(bound_matrix.columns), 8) |
| 101 | + |
| 102 | + def test_missing_scan_count(self): |
| 103 | + scan_count = self.missing.missing_scan_count([2, 5]) |
| 104 | + self.assertIsInstance(scan_count, pd.DataFrame) |
| 105 | + |
| 106 | + # Plotting functions tests |
| 107 | + |
| 108 | + def test_missing_variable_plot(self): |
| 109 | + self.assertIsNone(self.missing.missing_variable_plot()) |
| 110 | + |
| 111 | + def test_missing_case_plot(self): |
| 112 | + self.assertIsNone(self.missing.missing_case_plot()) |
| 113 | + |
| 114 | + def test_missing_variable_span_plot(self): |
| 115 | + self.assertIsNone(self.missing.missing_variable_span_plot(variable='A', span_every=2)) |
| 116 | + |
| 117 | + def test_missing_upsetplot(self): |
| 118 | + plot = self.missing.missing_upsetplot(variables=['A', 'B']) |
| 119 | + self.assertIsNotNone(plot) |
| 120 | + |
| 121 | + def test_missing_upsetplot_2(self): |
| 122 | + plot = self.missing.missing_upsetplot() |
| 123 | + self.assertIsNotNone(plot) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + unittest.main() |
0 commit comments