Exemplo n.º 1
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', '')])
Exemplo n.º 2
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, '', '')])
Exemplo n.º 3
0
    description='an example table',
    columns=[col1, col2],
    id=ElementIdentifiers(name='id', data=[0, 1]),
)

###############################################################################
# Adding rows
# -----------
# You can also add rows to a :py:class:`~hdmf.common.table.DynamicTable` using
# :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`.
# A keyword argument for every column in the table must be supplied.
# You may also supply an optional row ID.

table.add_row(
    col1=3,
    col2='c',
    id=2,
)

###############################################################################
# .. note::
#   If no ID is supplied, the row ID is automatically set to the number of rows of the table prior to adding the new
#   row. This can result in duplicate IDs. In general, IDs should be unique, but this is not enforced by default.
#   Pass `enforce_unique_id=True` to :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`
#   to raise an error if the ID is set to an existing ID value.

# this row will have ID 3 by default
table.add_row(
    col1=4,
    col2='d',
)
Exemplo n.º 4
0
)

###############################################################################
# Adding rows
# -----------
# You can add rows to a :py:class:`~hdmf.common.table.DynamicTable` using
# :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`.
# You must pass in a keyword argument for every column in the table.
# Ragged array column arguments should be passed in as lists or numpy arrays.
# The ID of the row will automatically be set and incremented for every row,
# starting at 0.

# id will be set to 0 automatically
users_table.add_row(
    first_name='Grace',
    last_name='Hopper',
    phone_number=['123-456-7890'],
)

# id will be set to 1 automatically
users_table.add_row(
    first_name='Alan',
    last_name='Turing',
    phone_number=['555-666-7777', '888-111-2222'],
)

###############################################################################
# Displaying the table contents as a pandas ``DataFrame``
# -------------------------------------------------------
# `pandas`_ is a popular data analysis tool for working with tabular data.
# Convert your :py:class:`~hdmf.common.table.DynamicTable` to a pandas
Exemplo n.º 5
0
table_set_ids = DynamicTable(
    name='my table',
    description='an example table',
    columns=[col1, col2],
    id=[100, 200],
)

###############################################################################
# Adding rows
# -----------
# You can add rows to a :py:class:`~hdmf.common.table.DynamicTable` using
# :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`.
# A keyword argument for every column in the table must be supplied.

table.add_row(
    col1=3,
    col2='c',
)

###############################################################################
# You can also supply an optional row ID to
# :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`.
# If no ID is supplied, the ID is automatically set to the number of rows in the table
# prior to adding the new row (i.e., automatic IDs start at 0).

table.add_row(
    col1=4,
    col2='d',
    id=10,
)

###############################################################################
Exemplo n.º 6
0
# :py:class:~hdmf.common.resources.Object.object_id must be the closest parent to the target object
# (i.e., :py:class:~hdmf.common.resources.Object.relative_pathmust be the shortest possible path and
# as such cannot contain any objects with adata_typeand associatedobject_id`).

# A common example would be with the :py:class:`~hdmf.common.table.DynamicTable` class, which holds
# :py:class:`~hdmf.common.table.VectorData` objects as columns. If we wanted to add an external
# reference on a column from a :py:class:`~hdmf.common.table.DynamicTable`, then we would use the
# column as the object and not the :py:class:`~hdmf.common.table.DynamicTable` (Refer to rule 9).

# Note: :py:func:`~hdmf.common.resources.ExternalResources.add_ref` internally resolves the object
# to the closest parent, so that er.add_ref(container=genotypes, attribute='genotype_name') and
# er.add_ref(container=genotypes.genotype_name, attribute=None) will ultimatly both use the object_id
# of the genotypes.genotype_name VectorData column and not the object_id of the genotypes table.
genotypes = DynamicTable(name='genotypes', description='My genotypes')
genotypes.add_column(name='genotype_name', description="Name of genotypes")
genotypes.add_row(id=0, genotype_name='Rorb')
er.add_ref(
    container=genotypes,
    attribute='genotype_name',
    key='Rorb',
    resource_name='MGI Database',
    resource_uri='http://www.informatics.jax.org/',
    entity_id='MGI:1346434',
    entity_uri='http://www.informatics.jax.org/marker/MGI:1343464'
)

###############################################################################
# Using the get_keys method
# ------------------------------------------------------
# The :py:func:`~hdmf.common.resources.ExternalResources.get_keys` method
# returns a :py:class:`~pandas.DataFrame` of ``key_name``, ``resource_table_idx``, ``entity_id``,