コード例 #1
0
    def test_to_dataframe(self):
        """Test that the to_dataframe method works"""
        category_names = ['test1', 'test2', 'test3']
        num_rows = 10
        categories = [DynamicTable(name=val,
                                   description=val+" description",
                                   columns=[VectorData(name=t,
                                                       description=val+t+' description',
                                                       data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
                                   ) for val in category_names]
        adt = AlignedDynamicTable(
            name='test_aligned_table',
            description='Test aligned container',
            category_tables=categories,
            columns=[VectorData(name='main_' + t,
                                description='main_'+t+'_description',
                                data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']])

        # Test the to_dataframe method with default settings
        tdf = adt.to_dataframe()
        self.assertListEqual(tdf.index.data.tolist(), list(range(10)))
        self.assertTupleEqual(tdf.index.name, ('test_aligned_table', 'id'))
        expected_cols = [('test_aligned_table', 'main_c1'),
                         ('test_aligned_table', 'main_c2'),
                         ('test_aligned_table', 'main_c3'),
                         ('test1', 'id'), ('test1', 'c1'), ('test1', 'c2'), ('test1', 'c3'),
                         ('test2', 'id'), ('test2', 'c1'), ('test2', 'c2'), ('test2', 'c3'),
                         ('test3', 'id'), ('test3', 'c1'), ('test3', 'c2'), ('test3', 'c3')]
        tdf_cols = tdf.columns.tolist()
        for v in zip(expected_cols, tdf_cols):
            self.assertTupleEqual(v[0], v[1])

        # test the to_dataframe method with ignore_category_ids set to True
        tdf = adt.to_dataframe(ignore_category_ids=True)
        self.assertListEqual(tdf.index.data.tolist(), list(range(10)))
        self.assertTupleEqual(tdf.index.name, ('test_aligned_table', 'id'))
        expected_cols = [('test_aligned_table', 'main_c1'),
                         ('test_aligned_table', 'main_c2'),
                         ('test_aligned_table', 'main_c3'),
                         ('test1', 'c1'), ('test1', 'c2'), ('test1', 'c3'),
                         ('test2', 'c1'), ('test2', 'c2'), ('test2', 'c3'),
                         ('test3', 'c1'), ('test3', 'c2'), ('test3', 'c3')]
        tdf_cols = tdf.columns.tolist()
        for v in zip(expected_cols, tdf_cols):
            self.assertTupleEqual(v[0], v[1])
コード例 #2
0
 def test_get_item(self):
     """Test getting elements from the table"""
     category_names = ['test1', ]
     num_rows = 10
     categories = [DynamicTable(name=val,
                                description=val+" description",
                                columns=[VectorData(name=t,
                                                    description=val+t+' description',
                                                    data=np.arange(num_rows) + i + 3)
                                         for i, t in enumerate(['c1', 'c2'])]
                                ) for val in category_names]
     temp = AlignedDynamicTable(
         name='test_aligned_table',
         description='Test aligned container',
         category_tables=categories,
         columns=[VectorData(name='main_' + t,
                             description='main_'+t+'_description',
                             data=np.arange(num_rows)+2) for t in ['c1', 'c2']])
     self.assertListEqual(temp.categories, category_names)
     # Test slicing with a single index
     self.assertListEqual(temp[5].iloc[0].tolist(), [7, 7, 5, 8, 9])
     # Test slice with list
     self.assertListEqual(temp[[5, 7]].iloc[0].tolist(), [7, 7, 5, 8, 9])
     self.assertListEqual(temp[[5, 7]].iloc[1].tolist(), [9, 9, 7, 10, 11])
     # Test slice with slice
     self.assertListEqual(temp[5:7].iloc[0].tolist(), [7, 7, 5, 8, 9])
     self.assertListEqual(temp[5:7].iloc[1].tolist(), [8, 8, 6, 9, 10])
     # Test slice with numpy index arrya
     self.assertListEqual(temp[np.asarray([5, 8])].iloc[0].tolist(), [7, 7, 5, 8, 9])
     self.assertListEqual(temp[np.asarray([5, 8])].iloc[1].tolist(), [10, 10, 8, 11, 12])
     # Test slicing for a single column
     self.assertListEqual(temp['main_c1'][:].tolist(), (np.arange(num_rows)+2).tolist())
     # Test slicing for a single category
     assert_frame_equal(temp['test1'], categories[0].to_dataframe())
     # Test getting the main table
     assert_frame_equal(temp[None], temp.to_dataframe())
     # Test getting a specific column
     self.assertListEqual(temp['test1', 'c1'][:].tolist(), (np.arange(num_rows) + 3).tolist())
     # Test getting a specific cell
     self.assertEqual(temp[None, 'main_c1', 1], 3)
     # Test bad selection tuple
     with self.assertRaises(ValueError):
         temp[('main_c1',)]
         raise ValueError("Expected tuple of length 2 or 3 with (category, selection, row) as value.")