Ejemplo n.º 1
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)
Ejemplo n.º 2
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']
                          ]))
Ejemplo n.º 3
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)
    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'))
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
 def add_scratch(self, **kwargs):
     '''Add data to the scratch space.'''
     data, name, description = getargs('data', 'name', 'description',
                                       kwargs)
     if isinstance(
             data,
         (str, int, float, bytes, np.ndarray, list, tuple, pd.DataFrame)):
         if name is None:
             msg = (
                 'A name is required for NWBFile.add_scratch when adding a scalar, numpy.ndarray, '
                 'list, tuple, or pandas.DataFrame as scratch data.')
             raise ValueError(msg)
         if description is None:
             msg = (
                 'A description is required for NWBFile.add_scratch when adding a scalar, numpy.ndarray, '
                 'list, tuple, or pandas.DataFrame as scratch data.')
             raise ValueError(msg)
         if isinstance(data, pd.DataFrame):
             data = DynamicTable.from_dataframe(
                 df=data, name=name, table_description=description)
         else:
             data = ScratchData(name=name,
                                data=data,
                                description=description)
     else:
         if name is not None:
             warn(
                 'The name argument is ignored when adding an NWBContainer, ScratchData, or '
                 'DynamicTable to scratch.')
         if description is not None:
             warn(
                 'The description argument is ignored when adding an NWBContainer, ScratchData, or '
                 'DynamicTable to scratch.')
     return self._add_scratch(data)
Ejemplo n.º 7
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"],
        )
Ejemplo n.º 8
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'))
Ejemplo n.º 9
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])
Ejemplo n.º 10
0
 def add_scratch(self, **kwargs):
     '''Add data to the scratch space'''
     data, name, notes = getargs('data', 'name', 'notes', kwargs)
     if isinstance(data, (np.ndarray, pd.DataFrame, list, tuple)):
         if name is None:
             raise ValueError('please provide a name for scratch data')
         if isinstance(data, pd.DataFrame):
             table_description = getargs('table_description', kwargs)
             data = DynamicTable.from_dataframe(
                 df=data, name=name, table_description=table_description)
             if notes is not None:
                 warn(
                     'Notes argument is ignored when adding a pandas DataFrame to scratch'
                 )
         else:
             data = ScratchData(name=name, data=data, notes=notes)
     else:
         if notes is not None:
             warn(
                 'Notes argument is ignored when adding an NWBContainer to scratch'
             )
         if name is not None:
             warn(
                 'Name argument is ignored when adding an NWBContainer to scratch'
             )
     self._add_scratch(data)
Ejemplo n.º 11
0
    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')
 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))
 def test_init_with_custom_empty_categories(self):
     """Test that we can create an empty table with custom categories"""
     category_names = ['test1', 'test2', 'test3']
     categories = [DynamicTable(name=val, description=val+" description") for val in category_names]
     AlignedDynamicTable(
         name='test_aligned_table',
         description='Test aligned container',
         category_tables=categories)
Ejemplo n.º 14
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)
Ejemplo n.º 15
0
 def __init__(self, **kwargs):
     metric, source_timeseries, bands = popargs('metric', 'source_timeseries', 'bands', kwargs)
     super(DecompositionSeries, self).__init__(**kwargs)
     self.source_timeseries = source_timeseries
     if self.source_timeseries is None:
         warnings.warn("It is best practice to set `source_timeseries` if it is present to document "
                       "where the DecompositionSeries was derived from. (Optional)")
     self.metric = metric
     if bands is None:
         bands = DynamicTable("bands", "data about the frequency bands that the signal was decomposed into")
     self.bands = bands
Ejemplo n.º 16
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)
Ejemplo n.º 17
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])
 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)
 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)
 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")
Ejemplo n.º 21
0
def _tablefunc(table_name, description, columns):
    t = DynamicTable(table_name, description)
    for c in columns:
        if isinstance(c, tuple):
            t.add_column(c[0], c[1])
        elif isinstance(c, str):
            t.add_column(c)
        else:
            raise ValueError("Elements of 'columns' must be str or tuple")
    return t
 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)
 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")
 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']")
Ejemplo n.º 25
0
 def __init__(self, **kwargs):
     metric, source_timeseries, bands, source_channels = popargs(
         'metric', 'source_timeseries', 'bands', 'source_channels', kwargs)
     super(DecompositionSeries, self).__init__(**kwargs)
     self.source_timeseries = source_timeseries
     self.source_channels = source_channels
     if self.source_timeseries is None and self.source_channels is None:
         warnings.warn(
             "Neither source_timeseries nor source_channels is present in DecompositionSeries. It is "
             "recommended to indicate the source timeseries if it is present, or else to link to the "
             "corresponding source_channels. (Optional)")
     self.metric = metric
     if bands is None:
         bands = DynamicTable(
             "bands",
             "data about the frequency bands that the signal was decomposed into"
         )
     self.bands = bands
 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.")
Ejemplo n.º 27
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)))
Ejemplo n.º 28
0
    def add_scratch(self, **kwargs):
        '''Add data to the scratch space'''
        data, name, notes, table_description, description = getargs('data', 'name', 'notes', 'table_description',
                                                                    'description', kwargs)
        if notes is not None or table_description != '':
            warn('Use of the `notes` or `table_description` argument will be removed in a future version of PyNWB. '
                 'Use the `description` argument instead.', PendingDeprecationWarning)
            if description is not None:
                raise ValueError('Cannot call add_scratch with (notes or table_description) and description')

        if isinstance(data, (str, int, float, bytes, np.ndarray, list, tuple, pd.DataFrame)):
            if name is None:
                msg = ('A name is required for NWBFile.add_scratch when adding a scalar, numpy.ndarray, '
                       'list, tuple, or pandas.DataFrame as scratch data.')
                raise ValueError(msg)
            if isinstance(data, pd.DataFrame):
                if table_description != '':
                    description = table_description  # remove after deprecation
                if description is None:
                    msg = ('A description is required for NWBFile.add_scratch when adding a scalar, numpy.ndarray, '
                           'list, tuple, or pandas.DataFrame as scratch data.')
                    raise ValueError(msg)
                data = DynamicTable.from_dataframe(df=data, name=name, table_description=description)
            else:
                if notes is not None:
                    description = notes  # remove after deprecation
                if description is None:
                    msg = ('A description is required for NWBFile.add_scratch when adding a scalar, numpy.ndarray, '
                           'list, tuple, or pandas.DataFrame as scratch data.')
                    raise ValueError(msg)
                data = ScratchData(name=name, data=data, description=description)
        else:
            if name is not None:
                warn('The name argument is ignored when adding an NWBContainer, ScratchData, or '
                     'DynamicTable to scratch.')
            if description is not None:
                warn('The description argument is ignored when adding an NWBContainer, ScratchData, or '
                     'DynamicTable to scratch.')
        return self._add_scratch(data)
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
    def test_add_ref_nested(self):
        table = DynamicTable(name='table', description='table')
        table.add_column(name='col1', description="column")
        table.add_row(id=0, col1='data')

        er = ExternalResources(name='example')
        er.add_ref(container=table,
                   attribute='description',
                   key='key1',
                   resource_name='resource0',
                   resource_uri='resource0_uri',
                   entity_id='entity_0',
                   entity_uri='entity_0_uri')
        self.assertEqual(er.keys.data, [('key1', )])
        self.assertEqual(er.resources.data, [('resource0', 'resource0_uri')])
        self.assertEqual(er.entities.data,
                         [(0, 0, 'entity_0', 'entity_0_uri')])
        self.assertEqual(er.objects.data,
                         [(table.object_id, 'DynamicTable/description', '')])