| 1 |
""" |
| 2 |
AlignedDynamicTable |
| 3 |
=================== |
| 4 |
|
| 5 |
This is a user guide to interacting with ``AlignedDynamicTable`` objects. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
|
| 10 |
############################################################################### |
| 11 |
# Introduction |
| 12 |
# ------------ |
| 13 |
# |
| 14 |
# The class :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` represents a column-based table |
| 15 |
# with support for grouping columns by category. :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` |
| 16 |
# inherits from :py:class:`~hdmf.common.table.DynamicTable` and may contain additional |
| 17 |
# :py:class:`~hdmf.common.table.DynamicTable` objects, one per sub-category. All tables |
| 18 |
# must align, i.e., they are required to have the same number of rows. Some key features |
| 19 |
# of :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` are: |
| 20 |
# |
| 21 |
# * support custom categories, each of which is a :py:class:`~hdmf.common.table.DynamicTable` |
| 22 |
# stored as part of the :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable`, |
| 23 |
# * support interaction with category tables individually as well as treating the |
| 24 |
# :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` as a single large table, and |
| 25 |
# * because :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` is itself a |
| 26 |
# :py:class:`~hdmf.common.table.DynamicTable` users can: |
| 27 |
# |
| 28 |
# * Use :py:class:`~hdmf.common.table.DynamicTableRegion` to reference rows in |
| 29 |
# :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` |
| 30 |
# * Add custom columns to the :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable`, and |
| 31 |
# * Interact with :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` as well as |
| 32 |
# the category (sub-tables) it contains in the same fashion as with |
| 33 |
# :py:class:`~hdmf.common.table.DynamicTable` |
| 34 |
# |
| 35 |
# When to use (and not use) AlignedDynamicTable? |
| 36 |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 37 |
# |
| 38 |
# :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` is a useful data structure but it is also |
| 39 |
# fairly complex, consisting of multiple :py:class:`~hdmf.common.table.DynamicTable` objects, each of |
| 40 |
# which is itself a complex type composed of many datasets and attributes. In general, if a simpler |
| 41 |
# data structure is sufficient, then consider using those instead. For example, consider using instead: |
| 42 |
# |
| 43 |
# * :py:class:`~hdmf.common.table.DynamicTable` if a regular table is sufficient. |
| 44 |
# * A compound dataset via :py:class:`~hdmf.container.Table` if all columns of a table are fixed |
| 45 |
# and fast, column-based access is not critical but fast row-based access is. |
| 46 |
# * Multiple, separate tables if using :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` would |
| 47 |
# lead to duplication of data (i.e., de-normalize data), e.g., by having to replicate values across |
| 48 |
# rows of the table. |
| 49 |
# |
| 50 |
# Use :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` when: |
| 51 |
# |
| 52 |
# * When you need to group columns in a :py:class:`~hdmf.common.table.DynamicTable` by category |
| 53 |
# * Need to avoid name collisions between columns in a :py:class:`~hdmf.common.table.DynamicTable` |
| 54 |
# and creating compound columns is not an option |
| 55 |
# |
| 56 |
|
| 57 |
############################################################################### |
| 58 |
# Constructing a table |
| 59 |
# -------------------- |
| 60 |
# |
| 61 |
# To create an :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable`, call the constructor with: |
| 62 |
# |
| 63 |
# * ``name`` string with the name of the table, and |
| 64 |
# * ``description`` string to describe the table. |
| 65 |
# |
| 66 |
|
| 67 |
# sphinx_gallery_thumbnail_path = 'figures/gallery_thumbnail_aligneddynamictable.png' |
| 68 |
from hdmf.common import AlignedDynamicTable |
| 69 |
|
| 70 |
customer_table = AlignedDynamicTable( |
| 71 |
name='customers', |
| 72 |
description='an example aligned table', |
| 73 |
) |
| 74 |
|
| 75 |
############################################################################### |
| 76 |
# Initializing columns of the primary table |
| 77 |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 78 |
# |
| 79 |
# The basic behavior of adding data and initializing :py:class:`~hdmf.common.alignedtable.AlignedDynamicTable` |
| 80 |
# is the same as in :py:class:`~hdmf.common.table.DynamicTable`. See the :ref:`dynamictable-howtoguide` |
| 81 |
# for details. E.g., using the ``columns`` and ``colnames`` parameters (which are inherited from |
| 82 |
# :py:class:`~hdmf.common.table.DynamicTable`) we can define the columns of the primary table. |
| 83 |
# All columns must have the same length. |
| 84 |
|
| 85 |
from hdmf.common import VectorData |
| 86 |
|
| 87 |
col1 = VectorData( |
| 88 |
name='firstname', |
| 89 |
description='Customer first name', |
| 90 |
data=['Peter', 'Emma'] |
| 91 |
) |
| 92 |
col2 = VectorData( |
| 93 |
name='lastname', |
| 94 |
description='Customer last name', |
| 95 |
data=['Williams', 'Brown'] |
| 96 |
) |
| 97 |
|
| 98 |
customer_table = AlignedDynamicTable( |
| 99 |
name='customer', |
| 100 |
description='an example aligned table', |
| 101 |
columns=[col1, col2] |
| 102 |
) |
| 103 |
|
| 104 |
############################################################################### |
| 105 |
# Initializing categories |
| 106 |
# ^^^^^^^^^^^^^^^^^^^^^^^ |
| 107 |
# |
| 108 |
# By specifying the ``category_tables`` as a list of :py:class:`~hdmf.common.table.DynamicTable` |
| 109 |
# objects we can then directly specify the sub-category tables. Optionally, we can also set |
| 110 |
# the ``categories`` names of the sub-tables as an array of strings to define the ordering of categories. |
| 111 |
|
| 112 |
from hdmf.common import DynamicTable |
| 113 |
|
| 114 |
# create the home_address category table |
| 115 |
subcol1 = VectorData( |
| 116 |
name='city', |
| 117 |
description='city', |
| 118 |
data=['Rivercity', 'Mountaincity'] |
| 119 |
) |
| 120 |
subcol2 = VectorData( |
| 121 |
name='street', |
| 122 |
description='street data', |
| 123 |
data=['Amazonstreet', 'Alpinestreet'] |
| 124 |
) |
| 125 |
homeaddress_table = DynamicTable( |
| 126 |
name='home_address', |
| 127 |
description='home address of the customer', |
| 128 |
columns=[subcol1, subcol2] |
| 129 |
) |
| 130 |
|
| 131 |
# create the table |
| 132 |
customer_table = AlignedDynamicTable( |
| 133 |
name='customer', |
| 134 |
description='an example aligned table', |
| 135 |
columns=[col1, col2], |
| 136 |
category_tables=[homeaddress_table, ] |
| 137 |
) |
| 138 |
|
| 139 |
# render the table in the online docs |
| 140 |
customer_table.to_dataframe() |
| 141 |
|
| 142 |
############################################################################### |
| 143 |
# Adding more data to the table |
| 144 |
# ----------------------------- |
| 145 |
# |
| 146 |
# We can add rows, columns, and new categories to the table. |
| 147 |
# |
| 148 |
# Adding a row |
| 149 |
# ^^^^^^^^^^^^ |
| 150 |
# |
| 151 |
# To add a row via :py:func:`~hdmf.common.alignedtable.AlignedDynamicTable.add_row` we |
| 152 |
# can either: 1) provide the row data as a single dict to the ``data`` parameter or |
| 153 |
# 2) specify a dict for each category and column as keyword arguments. Additional |
| 154 |
# optional arguments include ``id`` and ``enforce_unique_id``. |
| 155 |
# |
| 156 |
|
| 157 |
customer_table.add_row( |
| 158 |
firstname='Paul', |
| 159 |
lastname='Smith', |
| 160 |
home_address={'city': 'Bugcity', |
| 161 |
'street': 'Beestree'} |
| 162 |
) |
| 163 |
|
| 164 |
# render the table in the online docs |
| 165 |
customer_table.to_dataframe() |
| 166 |
|
| 167 |
############################################################################### |
| 168 |
# Adding a column |
| 169 |
# ^^^^^^^^^^^^^^^ |
| 170 |
# |
| 171 |
# To add a columns we use :py:func:`~hdmf.common.alignedtable.AlignedDynamicTable.add_column`. |
| 172 |
# |
| 173 |
|
| 174 |
customer_table.add_column( |
| 175 |
name='zipcode', |
| 176 |
description='zip code of the city', |
| 177 |
data=[11111, 22222, 33333], # specify data for the 3 rows in the table |
| 178 |
category='home_address' # use None (or omit) to add columns to the primary table |
| 179 |
) |
| 180 |
|
| 181 |
# render the table in the online docs |
| 182 |
customer_table.to_dataframe() |
| 183 |
|
| 184 |
############################################################################### |
| 185 |
# Adding a category |
| 186 |
# ^^^^^^^^^^^^^^^^^ |
| 187 |
# |
| 188 |
# To add a new :py:class:`~hdmf.common.table.DynamicTable` as a category, |
| 189 |
# we use :py:func:`~hdmf.common.alignedtable.AlignedDynamicTable.add_category`. |
| 190 |
# |
| 191 |
# .. note:: |
| 192 |
# Only regular ``DynamicTables`` are allowed as category tables. Using |
| 193 |
# an ``AlignedDynamicTable`` as a category for another ``AlignedDynamicTable`` |
| 194 |
# is currently not supported. |
| 195 |
# |
| 196 |
|
| 197 |
# create a new category DynamicTable for the work address |
| 198 |
subcol1 = VectorData( |
| 199 |
name='city', |
| 200 |
description='city', |
| 201 |
data=['Busycity', 'Worktown', 'Labortown'] |
| 202 |
) |
| 203 |
subcol2 = VectorData( |
| 204 |
name='street', |
| 205 |
description='street data', |
| 206 |
data=['Cannery Row', 'Woodwork Avenue', 'Steel Street'] |
| 207 |
) |
| 208 |
subcol3 = VectorData( |
| 209 |
name='zipcode', |
| 210 |
description='zip code of the city', |
| 211 |
data=[33333, 44444, 55555]) |
| 212 |
workaddress_table = DynamicTable( |
| 213 |
name='work_address', |
| 214 |
description='home address of the customer', |
| 215 |
columns=[subcol1, subcol2, subcol3] |
| 216 |
) |
| 217 |
|
| 218 |
# add the category to our AlignedDynamicTable |
| 219 |
customer_table.add_category(category=workaddress_table) |
| 220 |
|
| 221 |
# render the table in the online docs |
| 222 |
customer_table.to_dataframe() |
| 223 |
|
| 224 |
############################################################################### |
| 225 |
# .. note:: |
| 226 |
# Because each category is stored as a separate :py:class:`~hdmf.common.table.DynamicTable` |
| 227 |
# there are no name collisions between the columns of the ``home_address`` and ``work_address`` |
| 228 |
# tables, so that both can contain matching ``city``, ``street``, and ``zipcode`` columns. However, |
| 229 |
# since a category table is a sub-part of the primary table, categories must not have the |
| 230 |
# same name as other columns or other categories in the primary table. |
| 231 |
|
| 232 |
############################################################################### |
| 233 |
# Accessing categories, columns, rows, and cells |
| 234 |
# ---------------------------------------------- |
| 235 |
# |
| 236 |
# Convert to a pandas DataFrame |
| 237 |
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 238 |
# |
| 239 |
# If we need to access the whole table for analysis, then converting the table |
| 240 |
# to pandas DataFrame is a convenient option. To ignore the ``id`` columns of all |
| 241 |
# category tables we can simply set the ``ignore_category_ids`` parameter. |
| 242 |
|
| 243 |
|
| 244 |
# render the table in the online docs while ignoring the id column of category tables |
| 245 |
customer_table.to_dataframe(ignore_category_ids=True) |
| 246 |
|
| 247 |
############################################################################### |
| 248 |
# Accessing categories |
| 249 |
# ^^^^^^^^^^^^^^^^^^^^ |
| 250 |
# |
| 251 |
|
| 252 |
# Get the list of all categories |
| 253 |
_ = customer_table.categories |
| 254 |
|
| 255 |
# Get the DynamicTable object of a particular category |
| 256 |
_ = customer_table.get_category(name='home_address') |
| 257 |
|
| 258 |
# Alternatively, we can use normal array slicing to get the category as a pandas DataFrame. |
| 259 |
# NOTE: In contrast to the previous call, the table is here converted to a DataFrame. |
| 260 |
_ = customer_table['home_address'] |
| 261 |
|
| 262 |
############################################################################### |
| 263 |
# Accessing columns |
| 264 |
# ^^^^^^^^^^^^^^^^^ |
| 265 |
# We can use the standard Python ``in`` operator to check if a column exists |
| 266 |
|
| 267 |
# To check if a column exists in the primary table we only need to specify the column name |
| 268 |
# or alternatively specify the category as None |
| 269 |
_ = 'firstname' in customer_table |
| 270 |
_ = (None, 'firstname') in customer_table |
| 271 |
# To check if a column exists in a category table we need to specify the category |
| 272 |
# and column name as a tuple |
| 273 |
_ = ('home_address', 'zipcode') in customer_table |
| 274 |
|
| 275 |
|
| 276 |
############################################################################### |
| 277 |
# We can use standard array slicing to get the :py:class:`~hdmf.common.table.VectorData` object of a column. |
| 278 |
|
| 279 |
# To get a column from the primary table we just provide the name. |
| 280 |
_ = customer_table['firstname'] |
| 281 |
# To get a column from a category table we provide both the category name and column name |
| 282 |
_ = customer_table['home_address', 'city'] |
| 283 |
|
| 284 |
|
| 285 |
############################################################################### |
| 286 |
# Accessing rows |
| 287 |
# ^^^^^^^^^^^^^^ |
| 288 |
# |
| 289 |
# Accessing rows works much like in :ref:`dynamictable-howtoguide` |
| 290 |
# |
| 291 |
|
| 292 |
# Get a single row by index as a DataFrame |
| 293 |
customer_table[1] |
| 294 |
|
| 295 |
############################################################################### |
| 296 |
# |
| 297 |
|
| 298 |
# Get a range of rows as a DataFrame |
| 299 |
customer_table[0:2] |
| 300 |
|
| 301 |
############################################################################### |
| 302 |
# |
| 303 |
|
| 304 |
# Get a list of rows as a DataFrame |
| 305 |
customer_table[[0, 2]] |
| 306 |
|
| 307 |
|
| 308 |
############################################################################### |
| 309 |
# Accessing cells |
| 310 |
# ^^^^^^^^^^^^^^^ |
| 311 |
# |
| 312 |
# To get a set of cells we need to specify the: 1) category, 2) column, and 3) row index when slicing into the table. |
| 313 |
# |
| 314 |
# When selecting from the primary table we need to specify None for the category, followed by the column name and |
| 315 |
# the selection. |
| 316 |
|
| 317 |
# Select rows 0:2 from the 'firstname' column in the primary table |
| 318 |
customer_table[None, 'firstname', 0:2] |
| 319 |
|
| 320 |
############################################################################### |
| 321 |
# |
| 322 |
|
| 323 |
# Select rows 1 from the 'firstname' column in the primary table |
| 324 |
customer_table[None, 'firstname', 1] |
| 325 |
|
| 326 |
############################################################################### |
| 327 |
# |
| 328 |
|
| 329 |
# Select rows 0 and 2 from the 'firstname' column in the primary table |
| 330 |
customer_table[None, 'firstname', [0, 2]] |
| 331 |
|
| 332 |
############################################################################### |
| 333 |
# |
| 334 |
|
| 335 |
# Select rows 0:2 from the 'city' column of the 'home_address' category table |
| 336 |
customer_table['home_address', 'city', 0:2] |