|
12 | 12 | # The :py:class:`~hdmf.common.table.DynamicTable` class represents a column-based table
|
13 | 13 | # to which you can add custom columns. It consists of a name, a description, a list of
|
14 | 14 | # row IDs, and a list of columns. Columns are represented by
|
15 |
| -# :py:class:`~hdmf.common.table.VectorData` and :py:class:`~hdmf.common.table.VectorIndex` |
16 |
| -# objects. |
| 15 | +# :py:class:`~hdmf.common.table.VectorData`, :py:class:`~hdmf.common.table.VectorIndex`, |
| 16 | +# and :py:class:`~hdmf.common.table.DynamicTableRegion` objects. |
17 | 17 |
|
18 | 18 | ###############################################################################
|
19 | 19 | # Constructing a table
|
|
32 | 32 | ###############################################################################
|
33 | 33 | # Initializing columns
|
34 | 34 | # --------------------
|
35 |
| -# You can initialize a :py:class:`~hdmf.common.table.DynamicTable` with particular |
| 35 | +# You can create a :py:class:`~hdmf.common.table.DynamicTable` with particular |
36 | 36 | # columns by passing a list or tuple of
|
37 | 37 | # :py:class:`~hdmf.common.table.VectorData` objects for the ``columns`` argument
|
38 | 38 | # in the constructor.
|
|
73 | 73 | id=[100, 200],
|
74 | 74 | )
|
75 | 75 |
|
| 76 | +############################################################################### |
| 77 | +# If a list of integers in passed to ``id``, |
| 78 | +# :py:class:`~hdmf.common.table.DynamicTable` automatically creates |
| 79 | +# an :py:class:`~hdmf.common.table.ElementIdentifiers` object, which is the data type |
| 80 | +# that stores row IDs. The above command is equivalent to |
| 81 | + |
| 82 | +from hdmf.common.table import ElementIdentifiers |
| 83 | + |
| 84 | +table_set_ids = DynamicTable( |
| 85 | + name='my table', |
| 86 | + description='an example table', |
| 87 | + columns=[col1, col2], |
| 88 | + id=ElementIdentifiers(name='id', data=[100, 200]), |
| 89 | +) |
| 90 | + |
76 | 91 | ###############################################################################
|
77 | 92 | # Adding rows
|
78 | 93 | # -----------
|
79 |
| -# You can add rows to a :py:class:`~hdmf.common.table.DynamicTable` using |
| 94 | +# You can also add rows to a :py:class:`~hdmf.common.table.DynamicTable` using |
80 | 95 | # :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`.
|
81 | 96 | # A keyword argument for every column in the table must be supplied.
|
82 | 97 |
|
|
86 | 101 | )
|
87 | 102 |
|
88 | 103 | ###############################################################################
|
89 |
| -# You can also supply an optional row ID to |
| 104 | +# You can supply an optional row ID to |
90 | 105 | # :py:meth:`DynamicTable.add_row <hdmf.common.table.DynamicTable.add_row>`.
|
91 |
| -# If no ID is supplied, the ID is automatically set to the number of rows in the table |
92 |
| -# prior to adding the new row (i.e., automatic IDs start at 0). |
| 106 | +# If no ID is supplied, the automatic row IDs count up from 0. |
93 | 107 |
|
94 | 108 | table.add_row(
|
95 | 109 | col1=4,
|
|
118 | 132 | data=[True, True, False, True], # specify data for the 4 rows in the table
|
119 | 133 | )
|
120 | 134 |
|
| 135 | +############################################################################### |
| 136 | +# Enumerated Data |
| 137 | +# --------------- |
| 138 | +# :py:class:`~hdmf.common.table.EnumData` is a special type of column for storing |
| 139 | +# an enumerated data type. This way each unique value is stored once, and the data |
| 140 | +# references those values by index. Using this method is more efficient than storing |
| 141 | +# a single value many types, and has the advantage of communicating to downstream |
| 142 | +# tools that the data is categorical in nature. |
| 143 | + |
| 144 | +from hdmf.common.table import EnumData |
| 145 | + |
| 146 | +# this column has a length of 5, not 3 |
| 147 | +enum_col = EnumData( |
| 148 | + name="cell_type", |
| 149 | + description="this column holds categorical variables", |
| 150 | + data=[0, 1, 2, 1, 0], |
| 151 | + elements=["aa", "bb", "cc"] |
| 152 | +) |
| 153 | + |
| 154 | +my_table = DynamicTable( |
| 155 | + name='my table', |
| 156 | + description='an example table', |
| 157 | + columns=[enum_col], |
| 158 | +) |
| 159 | + |
| 160 | + |
121 | 161 | ###############################################################################
|
122 | 162 | # Ragged array columns
|
123 |
| -# ^^^^^^^^^^^^^^^^^^^^ |
| 163 | +# -------------------- |
124 | 164 | # A table column with a different number of elements for each row is called a
|
125 | 165 | # ragged array. To initialize a :py:class:`~hdmf.common.table.DynamicTable`
|
126 | 166 | # with a ragged array column, pass both
|
|
150 | 190 | ####################################################################################
|
151 | 191 | # VectorIndex.data provides the indices for how to break VectorData.data into cells
|
152 | 192 | #
|
153 |
| -# You can add a ragged array column to an existing |
| 193 | +# You can add an empty ragged array column to an existing |
154 | 194 | # :py:class:`~hdmf.common.table.DynamicTable` by specifying ``index=True``
|
155 | 195 | # to :py:meth:`DynamicTable.add_column <hdmf.common.table.DynamicTable.add_column>`.
|
| 196 | +# This method only works if run before any rows have been added to the table. |
156 | 197 |
|
157 | 198 | new_table = DynamicTable(
|
158 | 199 | name='my table',
|
|
179 | 220 | )
|
180 | 221 |
|
181 | 222 | ###############################################################################
|
| 223 | +# Referencing rows of other tables |
| 224 | +# -------------------------------- |
| 225 | +# You can create a column that references rows of another table using adding a |
| 226 | +# :py:class:`~hdmf.common.table.DynamicTableRegion` object as a column of your |
| 227 | +# :py:class:`~hdmf.common.table.DynamicTable`. This is analogous to |
| 228 | +# a foreign key in a relational database. |
| 229 | + |
| 230 | +from hdmf.common.table import DynamicTableRegion |
| 231 | + |
| 232 | +dtr_col = DynamicTableRegion( |
| 233 | + name='table1_ref', |
| 234 | + description='references rows of earlier table', |
| 235 | + data=[0, 1, 0, 0], |
| 236 | + table=table |
| 237 | +) |
| 238 | + |
| 239 | +data_col = VectorData( |
| 240 | + name='col2', |
| 241 | + description='column #2', |
| 242 | + data=['a', 'a', 'a', 'b'], |
| 243 | +) |
| 244 | + |
| 245 | +table2 = DynamicTable( |
| 246 | + name='my table', |
| 247 | + description='an example table', |
| 248 | + columns=[dtr_col, data_col], |
| 249 | +) |
| 250 | + |
| 251 | +############################################################################### |
| 252 | +# Here, the ``data`` of ``dtr_col`` maps to rows of ``table`` (0-indexed). |
| 253 | +# |
| 254 | +# .. note:: |
| 255 | +# The ``data`` values of :py:class:`~hdmf.common.table.DynamicTableRegion` map to the row |
| 256 | +# index, not the row ID, though if you are using default IDs. these values will be the |
| 257 | +# same. |
| 258 | +# |
| 259 | +# Reference more than one row of another table with a |
| 260 | +# :py:class:`~hdmf.common.table.DynamicTableRegion` indexed by a |
| 261 | +# :py:class:`~hdmf.common.table.VectorIndex`. |
| 262 | + |
| 263 | +indexed_dtr_col = DynamicTableRegion( |
| 264 | + name='table1_ref2', |
| 265 | + description='references multiple rows of earlier table', |
| 266 | + data=[0, 0, 1, 1, 0, 0, 1], |
| 267 | + table=table |
| 268 | +) |
| 269 | + |
| 270 | +dtr_idx = VectorIndex( |
| 271 | + name='table1_ref2_index', |
| 272 | + target=indexed_dtr_col, |
| 273 | + data=[2, 3, 5, 7], |
| 274 | +) |
| 275 | + |
| 276 | +table3 = DynamicTable( |
| 277 | + name='my table', |
| 278 | + description='an example table', |
| 279 | + columns=[dtr_idx, indexed_dtr_col], |
| 280 | +) |
| 281 | + |
| 282 | +############################################################################### |
| 283 | +# Creating an expandable table |
| 284 | +# ---------------------------- |
| 285 | +# When using the default HDF5 backend, each column of these tables is an HDF5 Dataset, |
| 286 | +# which by default are set in size. This means that once a file is written, it is not |
| 287 | +# possible to add a new row. If you want to be able to save this file, load it, and add |
| 288 | +# more rows to the table, you will need to set this up when you create the |
| 289 | +# :py:class:`~hdmf.common.table.DynamicTable`. You do this by wrapping the data with |
| 290 | +# :py:class:`~hdmf.backends.hdf5.h5_utils.H5DataIO`. |
| 291 | + |
| 292 | +from hdmf.backends.hdf5.h5_utils import H5DataIO |
| 293 | + |
| 294 | +col1 = VectorData( |
| 295 | + name='expandable col1', |
| 296 | + description='column #1', |
| 297 | + data=H5DataIO(data=[1, 2], maxshape=(None,)), |
| 298 | +) |
| 299 | +col2 = VectorData( |
| 300 | + name='expandable col2', |
| 301 | + description='column #2', |
| 302 | + data=H5DataIO(data=['a', 'b'], maxshape=(None,)), |
| 303 | +) |
| 304 | + |
| 305 | +# Don't forget to wrap the row IDs too! |
| 306 | +ids = ElementIdentifiers( |
| 307 | + name='id', |
| 308 | + data=H5DataIO( |
| 309 | + data=[0, 1], |
| 310 | + maxshape=(None,) |
| 311 | + ) |
| 312 | +) |
| 313 | + |
| 314 | +expandable_table = DynamicTable( |
| 315 | + name='table that can be expanded after being saved to file', |
| 316 | + description='an example table', |
| 317 | + columns=[col1, col2], |
| 318 | + id=ids, |
| 319 | +) |
| 320 | + |
| 321 | +############################################################################### |
| 322 | +# Now you can write the file, read it back, and run ``expandable_table.add_row()``. |
| 323 | +# In this example, we are setting ``maxshape`` to ``(None,)``, which means this is a |
| 324 | +# 1-dimensional matrix that can expand indefinitely along its single dimension. You |
| 325 | +# could also use an integer in place of ``None``. For instance, ``maxshape=(8,)`` would |
| 326 | +# allow the column to grow up to a length of 8. Whichever ``maxshape`` you choose, |
| 327 | +# it should be the same for all :py:class:`~hdmf.common.table.VectorData`, |
| 328 | +# :py:class:`~hdmf.common.table.ElementIdentifiers`, and |
| 329 | +# :py:class:`~hdmf.common.table.DynamicTableRegion` objects in the |
| 330 | +# :py:class:`~hdmf.common.table.DynamicTable`, since they must always be the same |
| 331 | +# length. The default :py:class:`~hdmf.common.table.ElementIdentifiers` automatically |
| 332 | +# generated when you pass a list of integers to the ``id`` argument of the |
| 333 | +# :py:class:`~hdmf.common.table.DynamicTable` constructor is not expandable, so do not |
| 334 | +# forget to create a :py:class:`~hdmf.common.table.ElementIdentifiers` object, and wrap |
| 335 | +# that data as well. If any of the columns are indexed, the ``data`` arg of |
| 336 | +# :py:class:`~hdmf.common.table.VectorIndex` will also need to be wrapped in |
| 337 | +# :py:class:`~hdmf.backends.hdf5.h5_utils.H5DataIO`. |
| 338 | +# |
| 339 | +# |
182 | 340 | # Converting the table to a pandas ``DataFrame``
|
183 | 341 | # ----------------------------------------------
|
184 | 342 | # `pandas`_ is a popular data analysis tool, especially for working with tabular data.
|
|
466 | 624 | table_double_ragged_col['col6'] # returns col6_ind_ind
|
467 | 625 | table_double_ragged_col.col6 # returns col6
|
468 | 626 |
|
469 |
| -############################################################################### |
470 |
| -# Referencing rows of a DynamicTable |
471 |
| -# ---------------------------------- |
472 |
| -# TODO |
473 |
| - |
474 | 627 | ###############################################################################
|
475 | 628 | # Creating custom DynamicTable subclasses
|
476 | 629 | # ---------------------------------------
|
|
0 commit comments