Beispiel #1
0
    def test_get_keys_subset(self):
        er = ExternalResources('terms')
        er.add_ref(container='uuid1',
                   key='key1',
                   resource_name='resource1',
                   resource_uri='resource_uri1',
                   entity_id="id11",
                   entity_uri='url11')
        er.add_ref(container='uuid2',
                   key='key2',
                   resource_name='resource2',
                   resource_uri='resource_uri2',
                   entity_id="id12",
                   entity_uri='url21')
        er.add_ref(container='uuid1',
                   key=er.get_key(key_name='key1'),
                   resource_name='resource3',
                   resource_uri='resource_uri3',
                   entity_id="id13",
                   entity_uri='url31')
        key = er.keys.row[0]
        received = er.get_keys(keys=key)

        expected = pd.DataFrame(
            data=[['key1', 0, 'id11', 'url11'], ['key1', 2, 'id13', 'url31']],
            columns=['key_name', 'resources_idx', 'entity_id', 'entity_uri'])
        pd.testing.assert_frame_equal(received, expected)
Beispiel #2
0
    def test_get_object_resources(self):
        er = ExternalResources('terms')
        data = Data(name='data_name',
                    data=np.array([('Mus musculus', 9, 81.0),
                                   ('H**o sapien', 3, 27.0)],
                                  dtype=[('species', 'U14'), ('age', 'i4'),
                                         ('weight', 'f4')]))

        er.add_ref(
            container=data,
            key='Mus musculus',
            resource_name='NCBI_Taxonomy',
            resource_uri='https://www.ncbi.nlm.nih.gov/taxonomy',
            entity_id='NCBI:txid10090',
            entity_uri=
            'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090'
        )
        received = er.get_object_resources(data)
        expected = pd.DataFrame(data=[[
            0, 0, 'NCBI:txid10090',
            'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090'
        ]],
                                columns=[
                                    'keys_idx', 'resource_idx', 'entity_id',
                                    'entity_uri'
                                ])
        pd.testing.assert_frame_equal(received, expected)
Beispiel #3
0
    def test_add_ref(self):
        er = ExternalResources('terms')
        er.add_ref('uuid1', 'field1', 'key1', 'resource1', 'resource_id1', 'uri1')

        self.assertEqual(er.keys.data, [('key1',)])
        self.assertEqual(er.resources.data, [(0, 'resource1', 'resource_id1', 'uri1')])
        self.assertEqual(er.objects.data, [('uuid1', 'field1')])
Beispiel #4
0
    def test_get_key_same_keyname_all(self):
        self.er = ExternalResources('terms')
        key1 = self.er._add_key('key1')
        key2 = self.er._add_key('key1')
        self.er.add_ref('uuid1',
                        key=key1,
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key=key2,
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url12')
        self.er.add_ref('uuid1',
                        key=self.er.get_key('key1', 'uuid1', ''),
                        resource_name='resource3',
                        resource_uri='resource_uri3',
                        entity_id="id13",
                        entity_uri='url13')

        keys = self.er.get_key('key1')

        self.assertIsInstance(keys, Key)
        self.assertEqual(keys[0].key, 'key1')
        self.assertEqual(keys[1].key, 'key1')
Beispiel #5
0
    def test_get_key_same_keyname_specific(self):
        self.er = ExternalResources('terms')

        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key='key2',
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url12')
        self.er.add_ref('uuid1',
                        key=self.er.get_key('key1', 'uuid1', ''),
                        resource_name='resource3',
                        resource_uri='resource_uri3',
                        entity_id="id13",
                        entity_uri='url13')

        keys = self.er.get_key('key1', 'uuid1', '')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key, 'key1')
        self.assertEqual(self.er.keys.data, [('key1', ), ('key2', )])
Beispiel #6
0
 def test_add_ref_deep_nested(self):
     er = ExternalResources(name='example', type_map=self.type_map)
     er.add_ref(container=self.bar,
                attribute='attr2',
                key='key1',
                resource_name='resource0',
                resource_uri='resource0_uri',
                entity_id='entity_0',
                entity_uri='entity_0_uri')
     self.assertEqual(er.objects.data[0][1], 'Bar/data/attr2', '')
Beispiel #7
0
    def test_add_ref_same_key_diff_objfield(self):
        er = ExternalResources('terms')
        er.add_ref('uuid1', 'field1', 'key1', 'resource1', 'resource_id1', 'url1')
        er.add_ref('uuid2', 'field2', 'key1', 'resource2', 'resource_id2', 'url2')

        self.assertEqual(er.keys.data, [('key1',)])
        self.assertEqual(er.resources.data,
                         [(0, 'resource1', 'resource_id1', 'url1'),
                          (0, 'resource2', 'resource_id2', 'url2')])
        self.assertEqual(er.objects.data, [('uuid1', 'field1'),
                                           ('uuid2', 'field2')])
Beispiel #8
0
    def test_get_key_same_keyname_specific(self):
        self.er = ExternalResources('terms')
        key1 = self.er.add_key('key1')
        key2 = self.er.add_key('key1')
        self.er.add_ref('uuid1', 'field1', key1, 'resource11', 'resource_id11', 'url11')
        self.er.add_ref('uuid2', 'field2', key2, 'resource21', 'resource_id21', 'url21')
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource12', 'resource_id12', 'url12')

        keys = self.er.get_key('key1', 'uuid1', 'field1')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key_name, 'key1')
Beispiel #9
0
    def test_keys_roundtrip(self):
        er = ExternalResources('terms')
        keys = pd.DataFrame(
            data=[['key1', 'resource11', 'resource_id11', 'url11'],
                  ['key1', 'resource12', 'resource_id12', 'url12'],
                  ['key2', 'resource21', 'resource_id21', 'url21']],
            columns=['key_name', 'resource_name', 'resource_entity_id', 'resource_entity_uri']
        )
        er.add_keys(keys)
        received = er.get_keys()

        pd.testing.assert_frame_equal(received, keys)
Beispiel #10
0
 def test_add_ref(self):
     er = ExternalResources('terms')
     data = Data(name="species", data=['H**o sapiens', 'Mus musculus'])
     er.add_ref(container=data,
                key='key1',
                resource_name='resource1',
                resource_uri='uri1',
                entity_id='entity_id1',
                entity_uri='entity1')
     self.assertEqual(er.keys.data, [('key1', )])
     self.assertEqual(er.resources.data, [('resource1', 'uri1')])
     self.assertEqual(er.entities.data, [(0, 0, 'entity_id1', 'entity1')])
     self.assertEqual(er.objects.data, [(data.object_id, '', '')])
Beispiel #11
0
    def test_add_ref_same_keyname(self):
        er = ExternalResources('terms')
        er.add_ref(container='uuid1',
                   key='key1',
                   resource_name='resource1',
                   resource_uri='resource_uri1',
                   entity_id="id11",
                   entity_uri='url11')
        er.add_ref(container='uuid2',
                   key='key1',
                   resource_name='resource2',
                   resource_uri='resource_uri2',
                   entity_id="id12",
                   entity_uri='url21')
        er.add_ref(container='uuid3',
                   key='key1',
                   resource_name='resource3',
                   resource_uri='resource_uri3',
                   entity_id="id13",
                   entity_uri='url31')

        self.assertEqual(er.keys.data, [('key1', ), ('key1', ), ('key1', )])
        self.assertEqual(er.resources.data, [('resource1', 'resource_uri1'),
                                             ('resource2', 'resource_uri2'),
                                             ('resource3', 'resource_uri3')])
        self.assertEqual(er.entities.data, [(0, 0, 'id11', 'url11'),
                                            (1, 1, 'id12', 'url21'),
                                            (2, 2, 'id13', 'url31')])
        self.assertEqual(er.objects.data,
                         [('uuid1', '', ''), ('uuid2', '', ''),
                          ('uuid3', '', '')])
Beispiel #12
0
 def test_check_object_field_error(self):
     er = ExternalResources('terms')
     data = Data(name="species", data=['H**o sapiens', 'Mus musculus'])
     er._check_object_field(data, '')
     er._add_object(data, '', '')
     with self.assertRaises(ValueError):
         er._check_object_field(data, '')
Beispiel #13
0
    def test_add_keys(self):
        er = ExternalResources('terms')
        keys = pd.DataFrame(
            data=[['key1', 'resource11', 'resource_id11', 'url11'],
                  ['key1', 'resource12', 'resource_id12', 'url12'],
                  ['key2', 'resource21', 'resource_id21', 'url21']],
            columns=['key_name', 'resource_name', 'resource_entity_id', 'resource_entity_uri']
        )
        ret = er.add_keys(keys)

        self.assertEqual({'key1', 'key2'}, set(ret))
        self.assertEqual(er.keys.data, [('key1',), ('key2',)])
        self.assertEqual(er.resources.data,
                         [(0, 'resource11', 'resource_id11', 'url11'),
                          (0, 'resource12', 'resource_id12', 'url12'),
                          (1, 'resource21', 'resource_id21', 'url21')])
Beispiel #14
0
 def test_add_ref_bad_arg(self):
     er = ExternalResources('terms')
     # The contents of the message are not important. Just make sure an error is raised
     with self.assertRaises(ValueError):
         er.add_ref('uuid1', 'field1', 'key1', resource_name='resource1', entity_id='resource_id1')
     with self.assertRaises(ValueError):
         er.add_ref('uuid1', 'field1', 'key1', resource_name='resource1', entity_uri='uri1')
     with self.assertRaises(ValueError):
         er.add_ref('uuid1', 'field1', 'key1', entity_id='resource_id1', entity_uri='uri1')
Beispiel #15
0
    def test_object_key_unqiueness(self):
        er = ExternalResources('terms')
        data = Data(name='data_name',
                    data=np.array([('Mus musculus', 9, 81.0),
                                   ('H**o sapien', 3, 27.0)],
                                  dtype=[('species', 'U14'), ('age', 'i4'),
                                         ('weight', 'f4')]))

        er.add_ref(
            container=data,
            key='Mus musculus',
            resource_name='NCBI_Taxonomy',
            resource_uri='https://www.ncbi.nlm.nih.gov/taxonomy',
            entity_id='NCBI:txid10090',
            entity_uri=
            'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090'
        )
        existing_key = er.get_key('Mus musculus')
        er.add_ref(container=data,
                   key=existing_key,
                   resource_name='resource2',
                   resource_uri='resource_uri2',
                   entity_id='entity2',
                   entity_uri='entity_uri2')

        self.assertEqual(er.object_keys.data, [(0, 0)])
Beispiel #16
0
 def setUpContainer(self):
     er = ExternalResources('terms')
     key1 = er.add_key('key1')
     key2 = er.add_key('key1')
     er.add_ref('uuid1', 'field1', key1, 'resource11', 'resource_id11', 'url11')
     er.add_ref('uuid2', 'field2', key2, 'resource21', 'resource_id21', 'url21')
     er.add_ref('uuid1', 'field1', 'key1', 'resource12', 'resource_id12', 'url12')
     return er
Beispiel #17
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', '')])
Beispiel #18
0
    def test_add_ref_compound_data(self):
        er = ExternalResources(name='example')

        data = Data(name='data_name',
                    data=np.array([('Mus musculus', 9, 81.0),
                                   ('H**o sapiens', 3, 27.0)],
                                  dtype=[('species', 'U14'), ('age', 'i4'),
                                         ('weight', 'f4')]))
        er.add_ref(container=data,
                   field='species',
                   key='Mus musculus',
                   resource_name='NCBI_Taxonomy',
                   resource_uri='resource0_uri',
                   entity_id='NCBI:txid10090',
                   entity_uri='entity_0_uri')
        self.assertEqual(er.keys.data, [('Mus musculus', )])
        self.assertEqual(er.resources.data,
                         [('NCBI_Taxonomy', 'resource0_uri')])
        self.assertEqual(er.entities.data,
                         [(0, 0, 'NCBI:txid10090', 'entity_0_uri')])
        self.assertEqual(er.objects.data, [(data.object_id, '', 'species')])
Beispiel #19
0
    def test_add_ref_column_as_attribute(self):
        # Test to make sure the attribute object is being used for the id
        # for the external reference.
        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='col1',
                   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['col1'].object_id, '', '')])
Beispiel #20
0
    def test_check_object_field_add(self):
        er = ExternalResources('terms')
        data = Data(name="species", data=['H**o sapiens', 'Mus musculus'])
        er._check_object_field('uuid1', '')
        er._check_object_field(data, '')

        self.assertEqual(er.objects.data, [('uuid1', '', ''),
                                           (data.object_id, '', '')])
Beispiel #21
0
 def test_get_resources(self):
     er = ExternalResources('terms')
     er.add_ref(container='uuid1',
                key='key1',
                resource_name='resource1',
                resource_uri='resource_uri1',
                entity_id="id11",
                entity_uri='url11')
     resource = er.get_resource('resource1')
     self.assertIsInstance(resource, Resource)
     with self.assertRaises(ValueError):
         er.get_resource('unknown_resource')
Beispiel #22
0
    def test_piecewise_add(self):
        er = ExternalResources('terms')

        # this is the term the user wants to use. They will need to specify this
        key = er._add_key('mouse')

        resource1 = er._add_resource(resource='resource0', uri='resource_uri0')
        # the user will have to supply this info as well. This is the information
        # needed to retrieve info about the controled term
        er._add_entity(key, resource1, '10090', 'uri')

        # The user can also pass in the container or it can be wrapped up under NWBFILE
        obj = er._add_object('object', 'species')

        # This could also be wrapped up under NWBFile
        er._add_object_key(obj, key)

        self.assertEqual(er.keys.data, [('mouse', )])
        self.assertEqual(er.entities.data, [(0, 0, '10090', 'uri')])
        self.assertEqual(er.objects.data, [('object', 'species')])
Beispiel #23
0
    def test_get_keys_subset(self):
        er = ExternalResources('terms')
        er.add_ref('uuid1', 'field1', 'key1', 'resource11', 'resource_id11', 'url11')
        er.add_ref('uuid2', 'field2', 'key2', 'resource21', 'resource_id21', 'url21')
        er.add_ref('uuid1', 'field1', 'key1', 'resource12', 'resource_id12', 'url12')
        key = er.keys.row[0]
        received = er.get_keys(keys=key)

        expected = pd.DataFrame(
            data=[['key1', 'resource11', 'resource_id11', 'url11'],
                  ['key1', 'resource12', 'resource_id12', 'url12']],
            columns=['key_name', 'resource_name', 'resource_entity_id', 'resource_entity_uri']
        )
        pd.testing.assert_frame_equal(received, expected)
Beispiel #24
0
    def test_add_ref_same_keyname(self):
        er = ExternalResources('terms')
        key1 = er.add_key('key1')
        key2 = er.add_key('key1')
        er.add_ref('uuid1', 'field1', key1, 'resource11', 'resource_id11', 'url11')
        er.add_ref('uuid2', 'field2', key2, 'resource21', 'resource_id21', 'url21')
        er.add_ref('uuid1', 'field1', 'key1', 'resource12', 'resource_id12', 'url12')

        self.assertEqual(er.keys.data, [('key1',), ('key1',)])
        self.assertEqual(er.resources.data,
                         [(0, 'resource11', 'resource_id11', 'url11'),
                          (1, 'resource21', 'resource_id21', 'url21'),
                          (0, 'resource12', 'resource_id12', 'url12')])
        self.assertEqual(er.objects.data, [('uuid1', 'field1'),
                                           ('uuid2', 'field2')])
Beispiel #25
0
    def test_piecewise_add(self):
        er = ExternalResources('terms')

        # this is the term the user wants to use. They will need to specify this
        key = er.add_key('mouse')

        # the user will have to supply this info as well. This is the information
        # needed to retrieve info about the controled term
        er.add_resource(key, 'NCBI Taxonomy', '10090',
                        'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=10090')

        # The user can also pass in the container or it can be wrapped up under NWBFILE
        obj = er.add_object('ca885753-e8a3-418a-86f4-7748fc2252a8', 'species')

        # This could also be wrapped up under NWBFile
        er.add_external_reference(obj, key)

        self.assertEqual(er.keys.data, [('mouse',)])
        self.assertEqual(er.resources.data,
                         [(0, 'NCBI Taxonomy', '10090',
                           'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=10090')])
        self.assertEqual(er.objects.data, [('ca885753-e8a3-418a-86f4-7748fc2252a8', 'species')])
Beispiel #26
0
    def setUpContainer(self):
        er = ExternalResources('terms')
        er.add_ref(container='uuid1',
                   key='key1',
                   resource_name='resource11',
                   resource_uri='resource_uri11',
                   entity_id="id11",
                   entity_uri='url11')

        er.add_ref(container='uuid2',
                   key='key2',
                   resource_name='resource21',
                   resource_uri='resource_uri21',
                   entity_id="id12",
                   entity_uri='url21')
        return er
Beispiel #27
0
 def test_add_ref_duplicate_resource(self):
     er = ExternalResources('terms')
     er.add_ref(container='uuid1',
                key='key1',
                resource_name='resource0',
                resource_uri='uri0',
                entity_id='entity_id1',
                entity_uri='entity1')
     er.add_ref(container='uuid2',
                key='key2',
                resource_name='resource0',
                resource_uri='uri0',
                entity_id='entity_id2',
                entity_uri='entity2')
     resource_list = er.resources.which(resource='resource0')
     self.assertEqual(len(resource_list), 1)
Beispiel #28
0
 def test_add_ref_two_resources(self):
     er = ExternalResources('terms')
     er.add_ref(container='uuid1',
                key='key1',
                resource_name='resource1',
                resource_uri='resource_uri1',
                entity_id="id11",
                entity_uri='url11')
     er.add_ref(container='uuid1',
                key=er.get_key(key_name='key1'),
                resource_name='resource2',
                resource_uri='resource_uri2',
                entity_id="id12",
                entity_uri='url21')
     self.assertEqual(er.keys.data, [('key1', )])
     self.assertEqual(er.resources.data, [('resource1', 'resource_uri1'),
                                          ('resource2', 'resource_uri2')])
     self.assertEqual(er.objects.data, [('uuid1', '', '')])
     self.assertEqual(er.entities.data, [(0, 0, 'id11', 'url11'),
                                         (0, 1, 'id12', 'url21')])
Beispiel #29
0
 def test_get_key_without_container(self):
     self.er = ExternalResources('terms')
     self.er._add_key('key1')
     keys = self.er.get_key('key1')
     self.assertIsInstance(keys, Key)
Beispiel #30
0
 def setUp(self):
     self.er = ExternalResources('terms')