.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/general/object_id.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_general_object_id.py: .. _object_ids: Object IDs in NWB ================= This example focuses on how to access object IDs from NWB container objects and NWB container objects by object ID. Every NWB container object has an object ID that is a UUID_ string, such as "123e4567-e89b-12d3-a456-426655440000". These IDs have a non-zero probability of being duplicated, but are practically unique and used widely across computing platforms as if they are unique. The object ID of an NWB container object can be accessed using the :py:attr:`~hdmf.container.AbstractContainer.object_id` method. .. _UUID: https://en.wikipedia.org/wiki/Universally_unique_identifier .. GENERATED FROM PYTHON SOURCE LINES 18-52 .. code-block:: Python from datetime import datetime import numpy as np from dateutil.tz import tzlocal from pynwb import NWBFile, TimeSeries # set up the NWBFile start_time = datetime(2019, 4, 3, 11, tzinfo=tzlocal()) nwbfile = NWBFile( session_description="demonstrate NWB object IDs", identifier="NWB456", session_start_time=start_time, ) # make some simulated data timestamps = np.linspace(0, 100, 1024) data = ( np.sin(0.333 * timestamps) + np.cos(0.1 * timestamps) + np.random.randn(len(timestamps)) ) test_ts = TimeSeries(name="raw_timeseries", data=data, unit="m", timestamps=timestamps) # add it to the NWBFile nwbfile.add_acquisition(test_ts) # print the object ID of the NWB file print(nwbfile.object_id) # print the object ID of the TimeSeries print(test_ts.object_id) .. GENERATED FROM PYTHON SOURCE LINES 54-56 The :py:class:`~pynwb.file.NWBFile` class has the :py:meth:`~pynwb.file.NWBFile.objects` property, which provides a dictionary of all neurodata_type objects in the `NWBFile`, indexed by each object's object ID. .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python print(nwbfile.objects) .. GENERATED FROM PYTHON SOURCE LINES 60-61 You can iterate through the `objects` dictionary as with any other Python dictionary. .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. code-block:: Python for oid in nwbfile.objects: print(nwbfile.objects[oid]) for obj in nwbfile.objects.values(): print('%s: %s "%s"' % (obj.object_id, obj.neurodata_type, obj.name)) .. GENERATED FROM PYTHON SOURCE LINES 69-71 If you have stored the object ID of a particular NWB container object, you can use it as a key on `NWBFile.objects` to get the object. .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python ts_id = test_ts.object_id my_ts = nwbfile.objects[ts_id] # test_ts == my_ts .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. note:: It is important to note that the object ID is NOT a unique hash of the data. If the contents of an NWB container change, the object ID remains the same. .. _sphx_glr_download_tutorials_general_object_id.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: object_id.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: object_id.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: object_id.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_