コード例 #1
0
    def setUp(self) -> None:
        data1 = np.array([1, 2, 2, 3, 1, 1, 3, 2, 3])
        data2 = np.array([3, 4, 2, 4, 3, 2, 2, 4, 4])
        device = Device(name="device")
        eg_1 = ElectrodeGroup(name="electrodegroup1",
                              description="desc",
                              location="brain",
                              device=device)
        eg_2 = ElectrodeGroup(name="electrodegroup2",
                              description="desc",
                              location="brain",
                              device=device)
        data3 = [eg_1, eg_2, eg_1, eg_1, eg_1, eg_1, eg_1, eg_1, eg_1]
        vd1 = VectorData("Data1",
                         "vector data for creating a DynamicTable",
                         data=data1)
        vd2 = VectorData("Data2",
                         "vector data for creating a DynamicTable",
                         data=data2)
        vd3 = VectorData("ElectrodeGroup",
                         "vector data for creating a DynamicTable",
                         data=data3)
        vd = [vd1, vd2, vd3]

        self.dynamic_table = DynamicTable(
            name="test table",
            description="This is a test table",
            columns=vd,
            colnames=["Data1", "Data2", "ElectrodeGroup"],
        )
コード例 #2
0
ファイル: test_misc.py プロジェクト: MohammadJavadD/pynwb
    def test_init(self):
        timeseries = TimeSeries(name='dummy timeseries',
                                description='desc',
                                data=np.ones((3, 3)),
                                unit='Volts',
                                timestamps=np.ones((3, )))
        bands = DynamicTable(name='bands',
                             description='band info for LFPSpectralAnalysis',
                             columns=[
                                 VectorData(name='band_name',
                                            description='name of bands',
                                            data=['alpha', 'beta', 'gamma']),
                                 VectorData(
                                     name='band_limits',
                                     description='low and high cutoffs in Hz',
                                     data=np.ones((3, 2)))
                             ])
        spec_anal = DecompositionSeries(name='LFPSpectralAnalysis',
                                        description='my description',
                                        data=np.ones((3, 3, 3)),
                                        timestamps=np.ones((3, )),
                                        source_timeseries=timeseries,
                                        metric='amplitude',
                                        bands=bands)

        self.assertEqual(spec_anal.name, 'LFPSpectralAnalysis')
        self.assertEqual(spec_anal.description, 'my description')
        np.testing.assert_equal(spec_anal.data, np.ones((3, 3, 3)))
        np.testing.assert_equal(spec_anal.timestamps, np.ones((3, )))
        self.assertEqual(spec_anal.bands['band_name'].data,
                         ['alpha', 'beta', 'gamma'])
        np.testing.assert_equal(spec_anal.bands['band_limits'].data,
                                np.ones((3, 2)))
        self.assertEqual(spec_anal.source_timeseries, timeseries)
        self.assertEqual(spec_anal.metric, 'amplitude')
コード例 #3
0
ファイル: dna_table.py プロジェクト: kellyhuang21/deep-taxon
    def __init__(self, **kwargs):
        sequence_name, index, sequence, taxon, taxon_table = popargs('sequence_name',
                                                             'sequence_index',
                                                             'sequence',
                                                             'taxon',
                                                             'taxon_table',
                                                             kwargs)
        self._bitpacked = popargs('bitpacked', kwargs)
        vocab = popargs('vocab', kwargs)

        self.pad = popargs('pad', kwargs)
        seqlens = popargs('length', kwargs)
        columns = list()
        self.maxlen = None
        if isinstance(sequence_name, VectorData):      # data is being read -- here we assume that everything is a VectorData
            columns.append(sequence_name)
            columns.append(index)
            columns.append(sequence)
            columns.append(seqlens)
            columns.append(taxon)
            if self.pad:   # if we need to pad, compute the maxlen
                self.maxlen = np.max(seqlens.data[:])
        else:
            columns.append(VectorData('sequence_name', 'sequence names', data=sequence_name))
            seq = self.get_sequence_data(sequence)
            columns.append(self.get_sequence_index(index, seq))
            columns.append(seq)
            columns.append(VectorData('length', 'sequence lengths', data=seqlens))
            columns.append(DynamicTableRegion('taxon', taxon, 'taxa for each sequence', taxon_table))
        kwargs['columns'] = columns
        call_docval_func(super().__init__, kwargs)
        self.convert = self.get_numpy_conversion(maxlen=self.maxlen)
コード例 #4
0
 def test_init_with_custom_misaligned_categories(self):
     """Test that we cannot create an empty table with custom categories"""
     num_rows = 10
     val1 = 'test1'
     val2 = 'test2'
     categories = [
         DynamicTable(name=val1,
                      description=val1 + " description",
                      columns=[
                          VectorData(name=val1 + t,
                                     description=val1 + t + ' description',
                                     data=np.arange(num_rows))
                          for t in ['c1', 'c2', 'c3']
                      ]),
         DynamicTable(name=val2,
                      description=val2 + " description",
                      columns=[
                          VectorData(name=val2 + t,
                                     description=val2 + t + ' description',
                                     data=np.arange(num_rows + 1))
                          for t in ['c1', 'c2', 'c3']
                      ])
     ]
     with self.assertRaisesWith(
             ValueError,
             "Category DynamicTable test2 does not align, it has 11 rows expected 10"
     ):
         AlignedDynamicTable(name='test_aligned_table',
                             description='Test aligned container',
                             category_tables=categories)
コード例 #5
0
 def test_add_row(self):
     """Test adding a row to a non_empty 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)) for t in ['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)) for t in ['c1', 'c2']])
     self.assertListEqual(temp.categories, category_names)
     # Test successful add
     temp.add_row(test1=dict(c1=1, c2=2), main_c1=3, main_c2=5)
     self.assertListEqual(temp[10].iloc[0].tolist(), [3, 5, 10, 1, 2])
     # Test successful add version 2
     temp.add_row(data=dict(test1=dict(c1=1, c2=2), main_c1=4, main_c2=5))
     self.assertListEqual(temp[11].iloc[0].tolist(), [4, 5, 11, 1, 2])
     # Test missing categories data
     with self.assertRaises(KeyError) as ke:
         temp.add_row(main_c1=3, main_c2=5)
     self.assertTrue("row data keys don't match" in str(ke.exception))
コード例 #6
0
 def test_init_misaligned_category_tables(self):
     """Test misaligned category tables"""
     categories = [
         DynamicTable(name=val,
                      description=val + " description",
                      columns=[
                          VectorData(name=val + t,
                                     description=val + t + ' description',
                                     data=np.arange(10))
                          for t in ['c1', 'c2', 'c3']
                      ]) for val in ['test1', 'test2']
     ]
     categories.append(
         DynamicTable(
             name='test3',
             description="test3 description",
             columns=[
                 VectorData(name='test3 ' + t,
                            description='test3 ' + t + ' description',
                            data=np.arange(8)) for t in ['c1', 'c2', 'c3']
             ]))
     with self.assertRaisesWith(
             ValueError,
             "Category DynamicTable test3 does not align, it has 8 rows expected 10"
     ):
         AlignedDynamicTable(name='test_aligned_table',
                             description='Test aligned container',
                             categories=['test1', 'test2', 'test3'],
                             category_tables=categories)
コード例 #7
0
ファイル: test_misc.py プロジェクト: agencyenterprise/pynwb
    def setUpContainer(self):
        """ Return the test DecompositionSeries to read/write """
        self.timeseries = TimeSeries(name='dummy timeseries',
                                     description='desc',
                                     data=np.ones((3, 3)),
                                     unit='flibs',
                                     timestamps=np.ones((3, )))
        bands = DynamicTable(name='bands',
                             description='band info for LFPSpectralAnalysis',
                             columns=[
                                 VectorData(name='band_name',
                                            description='name of bands',
                                            data=['alpha', 'beta', 'gamma']),
                                 VectorData(
                                     name='band_limits',
                                     description='low and high cutoffs in Hz',
                                     data=np.ones((3, 2)))
                             ])
        spec_anal = DecompositionSeries(name='LFPSpectralAnalysis',
                                        description='my description',
                                        data=np.ones((3, 3, 3)),
                                        timestamps=np.ones((3, )),
                                        source_timeseries=self.timeseries,
                                        metric='amplitude',
                                        bands=bands)

        return spec_anal
コード例 #8
0
 def test_add_category_misaligned_rows(self):
     """Test that we can correct a non-empty category to an existing table"""
     category_names = ['test1', 'test2']
     num_rows = 10
     categories = [
         DynamicTable(name=val,
                      description=val + " description",
                      columns=[
                          VectorData(name=val + 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)
     self.assertListEqual(adt.categories, category_names)
     with self.assertRaisesWith(
             ValueError,
             "New category DynamicTable does not align, it has 8 rows expected 10"
     ):
         adt.add_category(
             DynamicTable(name='test3',
                          description='test3_description',
                          columns=[
                              VectorData(name='test3_' + t,
                                         description='test3 ' + t +
                                         ' description',
                                         data=np.arange(num_rows - 2))
                              for t in ['c1', 'c2', 'c3']
                          ]))
コード例 #9
0
    def test_init_with_custom_nonempty_categories_and_main(self):
        """
        Test that we can create a non-empty table with custom non-empty categories
        """
        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]
        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)) for t in ['c1', 'c2', 'c3']])

        self.assertEqual(temp.categories, category_names)
        self.assertTrue('test1' in temp)  # test that contains category works
        self.assertTrue(('test1', 'c1') in temp)  # test that contains a column works
        with self.assertRaises(ValueError):  # test the error case of a tuple with len !-2
            ('test1', 'c1', 't3') in temp
        self.assertTupleEqual(temp.colnames, ('main_c1', 'main_c2', 'main_c3'))
コード例 #10
0
    def test_nested_aligned_dynamic_table_not_allowed(self):
        """
        Test that using and AlignedDynamicTable as category for an AlignedDynamicTable is not allowed
        """
        # create an AlignedDynamicTable as category
        subsubcol1 = VectorData(name='sub_sub_column1',
                                description='test sub sub column',
                                data=['test11', 'test12'])
        sub_category = DynamicTable(name='sub_category1',
                                    description='test subcategory table',
                                    columns=[
                                        subsubcol1,
                                    ])
        subcol1 = VectorData(name='sub_column1',
                             description='test-subcolumn',
                             data=['test1', 'test2'])
        adt_category = AlignedDynamicTable(
            name='category1',
            description='test using AlignedDynamicTable as a category',
            columns=[
                subcol1,
            ],
            category_tables=[
                sub_category,
            ])

        # Create a regular column for our main AlignedDynamicTable
        col1 = VectorData(name='column1',
                          description='regular test column',
                          data=['test1', 'test2'])

        # test 1: Make sure we can't add the AlignedDynamicTable category on init
        msg = ("Category table with index %i is an AlignedDynamicTable. "
               "Nesting of AlignedDynamicTable is currently not supported." %
               0)
        with self.assertRaisesWith(ValueError, msg):
            # create the nested AlignedDynamicTable with our adt_category as a sub-category
            AlignedDynamicTable(name='nested_adt',
                                description='test nesting AlignedDynamicTable',
                                columns=[
                                    col1,
                                ],
                                category_tables=[
                                    adt_category,
                                ])

        # test 2: Make sure we can't add the AlignedDynamicTable category via add_category
        adt = AlignedDynamicTable(
            name='nested_adt',
            description='test nesting AlignedDynamicTable',
            columns=[
                col1,
            ])
        msg = "Category is an AlignedDynamicTable. Nesting of AlignedDynamicTable is currently not supported."
        with self.assertRaisesWith(ValueError, msg):
            adt.add_category(adt_category)
コード例 #11
0
 def test_get_colnames(self):
     """
     Test the AlignedDynamicTable.get_colnames function
     """
     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']
                               ])
     # Default, only get the colnames of the main table. Same as adt.colnames property
     expected_colnames = ('main_c1', 'main_c2', 'main_c3')
     self.assertTupleEqual(adt.get_colnames(), expected_colnames)
     # Same as default because if we don't include the catgories than ignore_category_ids has no effect
     self.assertTupleEqual(
         adt.get_colnames(include_category_tables=False,
                          ignore_category_ids=True), expected_colnames)
     # Full set of columns
     expected_colnames = [('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')]
     self.assertListEqual(
         adt.get_colnames(include_category_tables=True,
                          ignore_category_ids=False), expected_colnames)
     # All columns without the id columns of the category tables
     expected_colnames = [('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')]
     self.assertListEqual(
         adt.get_colnames(include_category_tables=True,
                          ignore_category_ids=True), expected_colnames)
コード例 #12
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.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.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])
コード例 #13
0
    def test_round_trip_container(self):
        """Test read and write the container by itself"""
        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
        ]
        curr = AlignedDynamicTable(name='test_aligned_table',
                                   description='Test aligned container',
                                   category_tables=categories)

        with HDF5IO(self.path, manager=get_manager(), mode='w') as io:
            io.write(curr)

        with HDF5IO(self.path, manager=get_manager(), mode='r') as io:
            incon = io.read()
            self.assertListEqual(incon.categories, curr.categories)
            for n in category_names:
                assert_frame_equal(incon[n], curr[n])
コード例 #14
0
 def test_add_column_to_subcategory(self):
     """Test adding a column to a subcategory"""
     category_names = ['test1', 'test2', 'test3']
     num_rows = 10
     categories = [
         DynamicTable(name=val,
                      description=val + " description",
                      columns=[
                          VectorData(name=val + 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)
     self.assertListEqual(adt.categories, category_names)
     # Test successful add
     adt.add_column(category='test2',
                    name='testA',
                    description='testA',
                    data=np.arange(10))
     self.assertTupleEqual(
         adt.get_category('test2').colnames,
         ('test2c1', 'test2c2', 'test2c3', 'testA'))
コード例 #15
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.")
コード例 #16
0
 def test_add_column(self):
     adt = AlignedDynamicTable(
         name='test_aligned_table',
         description='Test aligned container',
         columns=[VectorData(name='test_'+t,
                             description='test_'+t+' description',
                             data=np.arange(10)) for t in ['c1', 'c2', 'c3']])
     # Test successful add
     adt.add_column(name='testA', description='testA', data=np.arange(10))
     self.assertTupleEqual(adt.colnames,  ('test_c1', 'test_c2', 'test_c3', 'testA'))
コード例 #17
0
ファイル: dna_table.py プロジェクト: exabiome/deep-taxon
    def __init__(self, **kwargs):
        taxon_id, embedding, rep_taxon_id = popargs('taxon_id', 'embedding',
                                                    'rep_taxon_id', kwargs)
        taxonomy_labels = [
            'domain', 'phylum', 'class', 'order', 'family', 'genus', 'species'
        ]
        taxonomy = popargs(*taxonomy_labels, kwargs)
        self.__taxmap = {t: i for i, t in enumerate(taxonomy_labels)}

        columns = list()
        if isinstance(
                taxon_id, VectorData
        ):  # data is being read -- here we assume that everything is a VectorData
            columns.append(taxon_id)
            columns.extend(taxonomy)
            if embedding is not None: columns.append(embedding)
            if rep_taxon_id is not None: columns.append(rep_taxon_id)
        else:
            columns.append(
                VectorData('taxon_id', 'taxonomy IDs from NCBI',
                           data=taxon_id))
            if embedding is not None:
                columns.append(
                    VectorData('embedding',
                               'an embedding for each taxon',
                               data=embedding))
            if rep_taxon_id is not None:
                columns.append(
                    VectorData(
                        'rep_taxon_id',
                        'the taxon ID for the this species representative',
                        data=rep_taxon_id))
            for l, t in zip(taxonomy_labels, taxonomy):
                if isinstance(t, EnumData):
                    columns.append(t)
                    columns.append(t.elements)
                elif isinstance(t, VectorData):
                    columns.append(t)
                else:
                    columns.append(
                        VectorData(l, 'the %s for each taxon' % l, data=t))
        kwargs['columns'] = columns
        call_docval_func(super().__init__, kwargs)
コード例 #18
0
 def test_dynamictable_region_to_aligneddynamictable(self):
     """
     Test to ensure data is being retrieved correctly when pointing to an AlignedDynamicTable.
     In particular, make sure that all columns are being used, including those of the
     category tables, not just the ones from the main table.
     """
     temp_table = DynamicTable(name='t1',
                               description='t1',
                               colnames=['c1', 'c2'],
                               columns=[
                                   VectorData(name='c1',
                                              description='c1',
                                              data=np.arange(4)),
                                   VectorData(name='c2',
                                              description='c2',
                                              data=np.arange(4))
                               ])
     temp_aligned_table = AlignedDynamicTable(
         name='my_aligned_table',
         description='my test table',
         category_tables=[temp_table],
         colnames=['a1', 'a2'],
         columns=[
             VectorData(name='a1', description='c1', data=np.arange(4)),
             VectorData(name='a2', description='c1', data=np.arange(4))
         ])
     dtr = DynamicTableRegion(name='test',
                              description='test',
                              data=np.arange(4),
                              table=temp_aligned_table)
     dtr_df = dtr[:]
     # Full number of rows
     self.assertEqual(len(dtr_df), 4)
     # Test num columns: 2 columns from the main table, 2 columns from the category, 1 id columns from the category
     self.assertEqual(len(dtr_df.columns), 5)
     # Test that the data is correct
     for i, v in enumerate([('my_aligned_table', 'a1'),
                            ('my_aligned_table', 'a2'), ('t1', 'id'),
                            ('t1', 'c1'), ('t1', 'c2')]):
         self.assertTupleEqual(dtr_df.columns[i], v)
     # Test the column data
     for c in dtr_df.columns:
         self.assertListEqual(dtr_df[c].to_list(), list(range(4)))
コード例 #19
0
 def test_add_column_bad_category(self):
     """Test add column with bad category"""
     adt = AlignedDynamicTable(
         name='test_aligned_table',
         description='Test aligned container',
         columns=[VectorData(name='test_'+t,
                             description='test_'+t+' description',
                             data=np.arange(10)) for t in ['c1', 'c2', 'c3']])
     with self.assertRaises(KeyError) as ke:
         adt.add_column(category='mycat', name='testA', description='testA', data=np.arange(10))
     self.assertEqual(str(ke.exception), "'Category mycat not in table'")
コード例 #20
0
 def test_add_column_bad_length(self):
     """Test add column that is too short"""
     adt = AlignedDynamicTable(
         name='test_aligned_table',
         description='Test aligned container',
         columns=[VectorData(name='test_'+t,
                             description='test_'+t+' description',
                             data=np.arange(10)) for t in ['c1', 'c2', 'c3']])
     # Test successful add
     with self.assertRaises(ValueError) as ve:
         adt.add_column(name='testA', description='testA', data=np.arange(8))
     self.assertEqual(str(ve.exception), "column must have the same number of rows as 'id'")
コード例 #21
0
 def test_init_with_duplicate_custom_categories(self):
     """Test that we can create an empty table with custom categories"""
     category_names = ['test1', 'test1']
     num_rows = 10
     categories = [DynamicTable(name=val,
                                description=val+" description",
                                columns=[VectorData(name=val+t,
                                                    description=val+t+' description',
                                                    data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
                                ) for val in category_names]
     with self.assertRaises(ValueError):
         AlignedDynamicTable(
             name='test_aligned_table',
             description='Test aligned container',
             category_tables=categories)
コード例 #22
0
 def test_init_duplicate_category_table_name(self):
     # Test duplicate table name
     with self.assertRaises(ValueError) as ve:
         categories = [DynamicTable(name=val,
                                    description=val+" description",
                                    columns=[VectorData(name=val+t,
                                                        description=val+t+' description',
                                                        data=np.arange(10)) for t in ['c1', 'c2', 'c3']]
                                    ) for val in ['test1', 'test1', 'test3']]
         AlignedDynamicTable(
             name='test_aligned_table',
             description='Test aligned container',
             categories=['test1', 'test2', 'test3'],
             category_tables=categories)
     self.assertEqual(str(ve.exception), "Duplicate table name test1 found in input dynamic_tables")
コード例 #23
0
 def test_init_with_custom_nonempty_categories(self):
     """Test that we can create an empty table with custom categories"""
     category_names = ['test1', 'test2', 'test3']
     num_rows = 10
     categories = [DynamicTable(name=val,
                                description=val+" description",
                                columns=[VectorData(name=val+t,
                                                    description=val+t+' description',
                                                    data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
                                ) for val in category_names]
     temp = AlignedDynamicTable(
         name='test_aligned_table',
         description='Test aligned container',
         category_tables=categories)
     self.assertEqual(temp.categories, category_names)
コード例 #24
0
 def test_add_category(self):
     """Test that we can correct a non-empty category to an existing table"""
     category_names = ['test1', 'test2', 'test3']
     num_rows = 10
     categories = [DynamicTable(name=val,
                                description=val+" description",
                                columns=[VectorData(name=val+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[0:2])
     self.assertListEqual(adt.categories, category_names[0:2])
     adt.add_category(categories[-1])
     self.assertListEqual(adt.categories, category_names)
コード例 #25
0
 def test_add_category_already_in_table(self):
     category_names = ['test1', 'test2', 'test2']
     num_rows = 10
     categories = [DynamicTable(name=val,
                                description=val+" description",
                                columns=[VectorData(name=val+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[0:2])
     self.assertListEqual(adt.categories, category_names[0:2])
     with self.assertRaises(ValueError) as ve:
         adt.add_category(categories[-1])
     self.assertEqual(str(ve.exception), "Category test2 already in the table")
コード例 #26
0
ファイル: dna_table.py プロジェクト: exabiome/deep-taxon
 def __init__(self, **kwargs):
     columns = kwargs['columns']
     if columns is None:
         taxon_id, rep_idx, taxa_table = popargs('taxon_id', 'rep_idx',
                                                 'taxa_table', kwargs)
         if not isinstance(taxon_id, VectorData):
             taxon_id = VectorData('taxon_id',
                                   'NCBI accession',
                                   data=taxon_id)
         if not isinstance(rep_idx, VectorData):
             if taxa_table is None:
                 raise ValueError('taxa_table must be provided if writing')
             rep_idx = DynamicTableRegion('rep_idx', rep_idx,
                                          'representative taxon',
                                          taxa_table)
         kwargs['columns'] = [taxon_id, rep_idx]
     call_docval_func(super().__init__, kwargs)
コード例 #27
0
 def test_init_category_table_names_do_not_match_categories(self):
     # Construct some categories for testing
     category_names = ['test1', 'test2', 'test3']
     num_rows = 10
     categories = [DynamicTable(name=val,
                                description=val+" description",
                                columns=[VectorData(name=val+t,
                                                    description=val+t+' description',
                                                    data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
                                ) for val in category_names]
     # Test add category_table that is not listed in the categories list
     with self.assertRaises(ValueError) as ve:
         AlignedDynamicTable(
             name='test_aligned_table',
             description='Test aligned container',
             categories=['test1', 'test2', 't3'],  # bad name for 'test3'
             category_tables=categories)
     self.assertEqual(str(ve.exception), "DynamicTable test3 does not appear in categories ['test1', 'test2', 't3']")
コード例 #28
0
 def test_init_with_bad_custom_categories(self):
     """Test that we cannot provide a category that is not a DynamicTable"""
     num_rows = 10
     categories = [  # good category
         DynamicTable(name='test1',
                      description="test1 description",
                      columns=[
                          VectorData(name='test1' + t,
                                     description='test1' + t +
                                     ' description',
                                     data=np.arange(num_rows))
                          for t in ['c1', 'c2', 'c3']
                      ]),
         # use a list as a bad category example
         [0, 1, 2]
     ]
     with self.assertRaisesWith(
             ValueError,
             "Category table with index 1 is not a DynamicTable"):
         AlignedDynamicTable(name='test_aligned_table',
                             description='Test aligned container',
                             category_tables=categories)
コード例 #29
0
# :py:class:`~hdmf.common.table.VectorData` objects for the ``columns`` argument
# in the constructor.
#
# If the :py:class:`~hdmf.common.table.VectorData` objects contain data values,
# then each :py:class:`~hdmf.common.table.VectorData` object must contain the
# same number of rows as each other. A list of row IDs may be passed into the
# :py:class:`~hdmf.common.table.DynamicTable` constructor using the ``id``
# argument. If IDs are passed in, there should be the same number of rows as
# the column data. If IDs are not passed in, then the IDs will be set to
# ``range(len(column_data))`` by default.

from hdmf.common import VectorData, VectorIndex

col1 = VectorData(
    name='col1',
    description='column #1',
    data=[1, 2],
)
col2 = VectorData(
    name='col2',
    description='column #2',
    data=['a', 'b'],
)

# this table will have two rows with ids 0 and 1
table = DynamicTable(
    name='my table',
    description='an example table',
    columns=[col1, col2],
)
コード例 #30
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)
     self.assertEqual(temp[1, None, 'main_c1'], 3)
     # Test bad selection tuple
     with self.assertRaisesWith(
             ValueError,
             "Expected tuple of length 2 of the form [category, column], [row, category], "
             "[row, (category, column)] or a tuple of length 3 of the form "
             "[category, column, row], [row, category, column]"):
         temp[('main_c1', )]
     # Test selecting a single cell or row of a category table by having a
     # [int, str] or [int, (str, str)] type selection
     # Select row 0 from category 'test1'
     re = temp[0, 'test1']
     self.assertListEqual(re.columns.to_list(), ['id', 'c1', 'c2'])
     self.assertListEqual(re.index.names, [('test_aligned_table', 'id')])
     self.assertListEqual(re.values.tolist()[0], [0, 3, 4])
     # Select a single cell from a columm
     self.assertEqual(temp[1, ('test_aligned_table', 'main_c1')], 3)