def test_describe_cols(self): test_list = [[1, 2],[2, 3],[3, 4],[4, 5],[5, 6],[6, 7]] test_nd = np.array(test_list) test_sa = np.array([(1, 2, 'a'), (2, 3, 'b'), (3, 4, 'c'), (4, 5, 'd'), (5, 6, 'e'), (6, 7, 'f')], dtype=[('id', int), ('val', float), ('name', 'S1')]) ctrl_list = np.array([('f0', 6, 3.5, 1.707825127659933, 1, 6), ('f1', 6, 4.5, 1.707825127659933, 2, 7)], dtype=[('Column Name', 'S2'), ('Count', int), ('Mean', float), ('Standard Dev', float), ('Minimum', int), ('Maximum', int)]) ctrl_printout = """ Column Name Count Mean Standard Dev Minimum Maximum 0 f0 6 3.5 1.70782512766 1 6 1 f1 6 4.5 1.70782512766 2 7 """.strip() with uft.rerout_stdout() as get_stdout: self.assertTrue(uft.array_equal(ctrl_list, describe_cols( test_list))) self.assertEqual(get_stdout().strip(), ctrl_printout) self.assertTrue(uft.array_equal(ctrl_list, describe_cols( test_nd, verbose=False))) ctrl_sa = np.array([('id', 6, 3.5, 1.707825127659933, 1, 6), ('val', 6, 4.5, 1.707825127659933, 2, 7), ('name', np.nan, np.nan, np.nan, np.nan, np.nan)], dtype=[('Column Name', 'S4'), ('Count', float), ('Mean', float), ('Standard Dev', float), ('Minimum', float), ('Maximum', float)]) self.assertTrue(uft.array_equal(ctrl_sa, describe_cols( test_sa, verbose=False)))
def test_pprint_sa(self): M = [(1, 2, 3), (4, 5, 6), (7, 8, 'STRING')] ctrl = """ f0 f1 f2 0 1 2 3 1 4 5 6 2 7 8 STRING """.strip() with uft.rerout_stdout() as get_stdout: dsp.pprint_sa(M) self.assertEqual(get_stdout().strip(), ctrl) M = np.array([(1000, 'Bill'), (2000, 'Sam'), (3000, 'James')], dtype=[('number', float), ('name', 'S5')]) row_labels = [name[0] for name in M['name']] ctrl = """ number name B 1000.0 Bill S 2000.0 Sam J 3000.0 James """.strip() with uft.rerout_stdout() as get_stdout: dsp.pprint_sa(M, row_labels=row_labels) self.assertEqual(get_stdout().strip(), ctrl)
def test_print_matrix_row_col(self): M = [(1, 2, 3), (4, 5, 6), (7, 8, 'STRING')] ctrl = """ f0 f1 f2 0 1 2 3 1 4 5 6 2 7 8 STRING """.strip() with uft.rerout_stdout() as get_stdout: comm.print_matrix_row_col(M) self.assertEqual(get_stdout().strip(), ctrl) M = np.array([(1000, 'Bill'), (2000, 'Sam'), (3000, 'James')], dtype=[('number', float), ('name', 'S5')]) row_labels = [name[0] for name in M['name']] ctrl = """ number name B 1000.0 Bill S 2000.0 Sam J 3000.0 James """.strip() with uft.rerout_stdout() as get_stdout: comm.print_matrix_row_col(M, row_labels=row_labels) self.assertEqual(get_stdout().strip(), ctrl)
def test_table(self): data = np.array(['a', 'b', 'a', 'b', 'b', 'b', 'b', 'a', 'c', 'c', 'b', 'c', 'a'], dtype='O') ctrl_sa = np.array( [('a', 4), ('b', 6), ('c', 3)], dtype=[('col_name', 'S1'), ('count', int)]) ctrl_printout = """ col_name count 0 a 4 1 b 6 2 c 3 """.strip() with uft.rerout_stdout() as get_stdout: self.assertTrue(uft.array_equal(ctrl_sa, table(data))) self.assertEqual(get_stdout().strip(), ctrl_printout)
def test_crosstab(self): l1= [1, 2, 7, 7, 2, 1, 2, 1, 1] l2= [1, 3, 2, 6, 6, 3, 6, 4, 4] correct = np.array([('1', 1, 0, 1, 2, 0), ('2', 0, 0, 1, 0, 2), ('7', 0, 1, 0, 0, 1)], dtype=[('col1_value', 'S1'), ('1', int), ('2', int), ('3', int), ('4', int), ('6', int)]) correct_printout = """ col1_value 1 2 3 4 6 0 1 1 0 1 2 0 1 2 0 0 1 0 2 2 7 0 1 0 0 1 """.strip() with uft.rerout_stdout() as get_stdout: self.assertTrue(np.array_equal(correct, crosstab(l1,l2))) self.assertEqual(get_stdout().strip(), correct_printout)