Skip to content

Commit

Permalink
Merge branch 'dev' into electrode
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 authored Sep 25, 2024
2 parents f00b134 + dc98e84 commit dbfb805
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 101 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/run_inspector_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ jobs:
python -m pip list
git clone https://github.com/NeurodataWithoutBorders/nwbinspector.git
cd nwbinspector
python -m pip install -r requirements.txt pytest
python -m pip install . # this might install a pinned version of pynwb instead of the current one
python -m pip install pytest
python -m pip install ".[dandi]" # this might install a pinned version of pynwb instead of the current one
# Download testing data and set config path
dandi download "https://gui-staging.dandiarchive.org/#/dandiset/204919"
cd ..
python -m pip uninstall -y pynwb # uninstall the pinned version of pynwb
python -m pip install . # reinstall current branch of pynwb
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Upload distribution as a workspace artifact
if: ${{ matrix.upload-wheels }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: distributions
path: dist
Expand Down Expand Up @@ -283,7 +283,7 @@ jobs:
python-version: '3.12'

- name: Download wheel and source distributions from artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: distributions
path: dist
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ tests/coverage/htmlcov

# Version
_version.py

.core_typemap_version
core_typemap.pkl
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# PyNWB Changelog

## PyNWB 2.8.2 (Upcoming)
## PyNWB 2.8.3 (Upcoming)

### Performance
- Cache global type map to speed import 3X. @sneakers-the-rat [#1931](https://github.com/NeurodataWithoutBorders/pynwb/pull/1931)

## PyNWB 2.8.2 (September 9, 2024)

### Enhancements and minor changes
- Added support for numpy 2.0. @mavaylon1 [#1956](https://github.com/NeurodataWithoutBorders/pynwb/pull/1956)
- Make `get_cached_namespaces_to_validate` a public function @stephprince [#1961](https://github.com/NeurodataWithoutBorders/pynwb/pull/1961)

### Documentation and tutorial enhancements
- Added pre-release pull request instructions to release process documentation @stephprince [#1928](https://github.com/NeurodataWithoutBorders/pynwb/pull/1928)
- Added section on how to use the `family` driver in `h5py` for splitting data across multiple files @oruebel [#1949](https://github.com/NeurodataWithoutBorders/pynwb/pull/1949)

### Bug fixes
- Fixed `can_read` method to return False if no nwbfile version can be found @stephprince [#1934](https://github.com/NeurodataWithoutBorders/pynwb/pull/1934)
- Changed `epoch_tags` to be a NWBFile property instead of constructor argument. @stephprince [#1935](https://github.com/NeurodataWithoutBorders/pynwb/pull/1935)
- Exposed option to not cache the spec in `NWBHDF5IO.export`. @rly [#1959](https://github.com/NeurodataWithoutBorders/pynwb/pull/1959)

## PyNWB 2.8.1 (July 3, 2024)

Expand Down
2 changes: 2 additions & 0 deletions docs/gallery/advanced_io/plot_iterative_write.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
.. _iterative_write:
Iterative Data Write
====================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
HDF5 files with NWB data files via external links. To make things more concrete, let's look at the following use
case. We want to simultaneously record multiple data streams during data acquisition. Using the concept of external
links allows us to save each data stream to an external HDF5 files during data acquisition and to
afterwards link the data into a single NWB file. In this case, each recording becomes represented by a
afterward link the data into a single NWB file. In this case, each recording becomes represented by a
separate file-system object that can be set as read-only once the experiment is done. In the following
we are using :py:meth:`~pynwb.base.TimeSeries` as an example, but the same approach works for other
NWBContainers as well.
Expand Down Expand Up @@ -42,7 +42,7 @@
Creating test data
---------------------------
^^^^^^^^^^^^^^^^^^
In the following we are creating two :py:meth:`~pynwb.base.TimeSeries` each written to a separate file.
We then show how we can integrate these files into a single NWBFile.
Expand All @@ -61,7 +61,7 @@
# Create the base data
start_time = datetime(2017, 4, 3, 11, tzinfo=tzlocal())
data = np.arange(1000).reshape((100, 10))
timestamps = np.arange(100)
timestamps = np.arange(100, dtype=float)
filename1 = "external1_example.nwb"
filename2 = "external2_example.nwb"
filename3 = "external_linkcontainer_example.nwb"
Expand Down Expand Up @@ -105,12 +105,12 @@

#####################
# Linking to select datasets
# --------------------------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^
#

####################
# Step 1: Create the new NWBFile
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Create the first file
nwbfile4 = NWBFile(
Expand All @@ -122,7 +122,7 @@

####################
# Step 2: Get the dataset you want to link to
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Now let's open our test files and retrieve our timeseries.
#

Expand All @@ -134,7 +134,7 @@

####################
# Step 3: Create the object you want to link to the data
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# To link to the dataset we can simply assign the data object (here `` timeseries_1.data``) to a new ``TimeSeries``

Expand Down Expand Up @@ -167,7 +167,7 @@

####################
# Step 4: Write the data
# ^^^^^^^^^^^^^^^^^^^^^^^
# ~~~~~~~~~~~~~~~~~~~~~~~~
#
with NWBHDF5IO(filename4, "w") as io4:
# Use link_data=True to specify default behavior to link rather than copy data
Expand All @@ -185,7 +185,7 @@

####################
# Linking to whole Containers
# ---------------------------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# Appending to files and linking is made possible by passing around the same
# :py:class:`~hdmf.build.manager.BuildManager`. You can get a manager to pass around
Expand All @@ -203,7 +203,7 @@

####################
# Step 1: Get the container object you want to link to
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Now let's open our test files and retrieve our timeseries.
#

Expand All @@ -219,7 +219,7 @@

####################
# Step 2: Add the container to another NWBFile
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# To integrate both :py:meth:`~pynwb.base.TimeSeries` into a single file we simply create a new
# :py:meth:`~pynwb.file.NWBFile` and add our existing :py:meth:`~pynwb.base.TimeSeries` to it. PyNWB's
# :py:class:`~pynwb.NWBHDF5IO` backend then automatically detects that the TimeSeries have already
Expand Down Expand Up @@ -247,7 +247,7 @@
# ------------------------------
#
# Using the :py:func:`~pynwb.file.NWBFile.copy` method allows us to easily create a shallow copy
# of a whole NWB:N file with links to all data in the original file. For example, we may want to
# of a whole NWB file with links to all data in the original file. For example, we may want to
# store processed data in a new file separate from the raw data, while still being able to access
# the raw data. See the :ref:`scratch` tutorial for a detailed example.
#
Expand All @@ -259,5 +259,128 @@
# External links are convenient but to share data we may want to hand a single file with all the
# data to our collaborator rather than having to collect all relevant files. To do this,
# :py:class:`~hdmf.backends.hdf5.h5tools.HDF5IO` (and in turn :py:class:`~pynwb.NWBHDF5IO`)
# provide the convenience function :py:meth:`~hdmf.backends.hdf5.h5tools.HDF5IO.copy_file`,
# which copies an HDF5 file and resolves all external links.
# provide the convenience function :py:meth:`~hdmf.backends.hdf5.h5tools.HDF5IO.export`,
# which can copy the file and resolves all external links.


####################
# Automatically splitting large data across multiple HDF5 files
# -------------------------------------------------------------------
#
# For extremely large datasets it can be useful to split data across multiple files, e.g., in cases where
# the file stystem does not allow for large files. While we can achieve this by writing different
# components (e.g., :py:meth:`~pynwb.base.TimeSeries`) to different files as described above,
# this option does not allow splitting data from single datasets. An alternative option is to use the
# ``family`` driver in ``h5py`` to automatically split the NWB file into a collection of many HDF5 files.
# The ``family`` driver stores the file on disk as a series of fixed-length chunks (each in its own file).
# In practice, to write very large arrays, we can combine this approach with :ref:`iterative_write` to
# avoid having to load all data into memory. In the example shown here we use a manual approach to
# iterative write by using :py:class:`~hdmf.backends.hdf5.h5_utils.H5DataIO` to create an empty dataset and
# then filling in the data afterward.

####################
# Step 1: Create the NWBFile as usual
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

from pynwb import NWBFile
from pynwb.base import TimeSeries
from datetime import datetime
from hdmf.backends.hdf5 import H5DataIO
import numpy as np

# Create an NWBFile object
nwbfile = NWBFile(session_description='example file family',
identifier=str(uuid4()),
session_start_time=datetime.now().astimezone())

# Create the data as an empty dataset so that we can write to it later
data = H5DataIO(maxshape=(None, 10), # make the first dimension expandable
dtype=np.float32, # create the data as float32
shape=(0, 10), # initial data shape to initialize as empty dataset
chunks=(1000, 10)
)

# Create a TimeSeries object
time_series = TimeSeries(name='example_timeseries',
data=data,
starting_time=0.0,
rate=1.0,
unit='mV')

# Add the TimeSeries to the NWBFile
nwbfile.add_acquisition(time_series)

####################
# Step 2: Open the new file with the `family` driver and write
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Here we need to open the file with `h5py` first to set up the driver, and then we can use
# that file with :py:class:`pynwb.NWBHDF5IO`. This is required, because :py:class:`pynwb.NWBHDF5IO`
# currently does not support passing the `memb_size` option required by the `family` driver.

import h5py
from pynwb import NWBHDF5IO

# Define the size of the individual files, determining the number of files to create
# chunk_size = 1 * 1024**3 # 1GB per file
chunk_size = 1024**2 # 1MB just for testing

# filename pattern
filename_pattern = 'family_nwb_file_%d.nwb'

# Create the HDF5 file using the family driver
with h5py.File(name=filename_pattern, mode='w', driver='family', memb_size=chunk_size) as f:

# Use NWBHDF5IO to write the NWBFile to the HDF5 file
with NWBHDF5IO(file=f, mode='w') as io:
io.write(nwbfile)

# Write new data iteratively to the file
for i in range(10):
start_index = i * 1000
stop_index = start_index + 1000
data.dataset.resize((stop_index, 10)) # Resize the dataset
data.dataset[start_index: stop_index , :] = i # Set the additional values

####################
# .. note::
#
# Alternatively, we could have also used the :ref:`iterative_write` features to write the data
# iteratively directly as part of the `io.write` call instead of manually afterward.

####################
# Step 3: Read a file written with the family driver
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#


# Open the HDF5 file using the family driver
with h5py.File(name=filename_pattern, mode='r', driver='family', memb_size=chunk_size) as f:
# Use NWBHDF5IO to read the NWBFile from the HDF5 file
with NWBHDF5IO(file=f, manager=None, mode='r') as io:
nwbfile = io.read()
print(nwbfile)


####################
# .. note::
#
# The filename you provide when using the ``family`` driver must contain a printf-style integer format code
# (e.g.`%d`), which will be replaced by the file sequence number.
#
# .. note::
#
# The ``memb_size`` parameter must be set on both write and read. As such, reading the file requires
# the user to know the ``memb_size`` that was used for writing.
#
# .. warning::
#
# The DANDI archive may not support NWB files that are split in this fashion.
#
# .. note::
#
# Other file drivers, e.g., ``split`` or ``multi`` could be used in a similar fashion.
# However, not all HDF5 drivers are supported by the the high-level API of
# ``h5py`` and as such may require a bit more complex setup via the the
# low-level HDF5 API in ``h5py``.
#

10 changes: 4 additions & 6 deletions docs/source/export.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ on the :py:class:`~pynwb.file.NWBFile` before exporting.

How do I create a copy of an NWB file with different data layouts (e.g., applying compression)?
---------------------------------------------------------------------------------------------------------
Use the `h5repack <https://support.hdfgroup.org/HDF5/doc/RM/Tools.html#Tools-Repack>`_ command line tool from the HDF5 Group.
See also this `h5repack tutorial <https://support.hdfgroup.org/HDF5/Tutor/cmdtooledit.html#chglayout>`_.
Use the `h5repack <https://docs.hdfgroup.org/hdf5/v1_14/_view_tools_edit.html>`_ command line tool from the HDF5 Group.


How do I create a copy of an NWB file with different controls over how links are treated and whether copies are deep or shallow?
---------------------------------------------------------------------------------------------------------------------------------
Use the `h5copy <https://support.hdfgroup.org/HDF5/doc/RM/Tools.html#Tools-Copy>`_ command line tool from the HDF5 Group.
See also this `h5copy tutorial <https://support.hdfgroup.org/HDF5/Tutor/cmdtooledit.html#copy>`_.
Use the `h5copy <https://docs.hdfgroup.org/hdf5/v1_14/_view_tools_edit.html>`_ command line tool from the HDF5 Group.


How do I generate new object IDs for a newly exported NWB file?
Expand Down Expand Up @@ -101,8 +99,8 @@ For example:
export_io.export(src_io=read_io, nwbfile=nwbfile, write_args={'link_data': False}) # copy linked datasets
# the written file will contain no links to external datasets
You can also the `h5copy <https://support.hdfgroup.org/HDF5/doc/RM/Tools.html#Tools-Copy>`_ command line tool \
from the HDF5 Group. See also this `h5copy tutorial <https://support.hdfgroup.org/HDF5/Tutor/cmdtooledit.html#copy>`_.
You can also the `h5copy <https://docs.hdfgroup.org/hdf5/v1_14/_view_tools_edit.html>`_ command line tool \
from the HDF5 Group.


How do I write a newly instantiated ``NWBFile`` to two different file paths?
Expand Down
6 changes: 3 additions & 3 deletions environment-ros3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ channels:
dependencies:
- python==3.12
- h5py==3.11.0
- hdmf==3.14.1
- matplotlib==3.8.0
- numpy==1.26.4
- hdmf==3.14.3
- matplotlib==3.8.4
- numpy==2.1.1
- pandas==2.2.2
- python-dateutil==2.9.0
- setuptools
Expand Down
13 changes: 7 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ classifiers = [
]
dependencies = [
"h5py>=2.10",
"hdmf>=3.14.0",
"numpy>=1.18, <2.0", # pin below 2.0 until HDMF supports numpy 2.0
"hdmf>=3.14.3",
"numpy>=1.18",
"pandas>=1.1.5",
"python-dateutil>=2.7.3",
]
Expand Down Expand Up @@ -91,21 +91,22 @@ omit = [
]

[tool.ruff]
select = ["E", "F", "T100", "T201", "T203"]
lint.select = ["E", "F", "T100", "T201", "T203"]
exclude = [
".git",
".tox",
"__pycache__",
"build/",
"dist/",
"src/nwb-schema",
"src/pynwb/nwb-schema",
"docs/source/conf.py",
"docs/notebooks/*",
"src/pynwb/_due.py",
"test.py" # remove when pytest comes along
]
line-length = 120

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"tests/read_dandi/*" = ["T201"]
"docs/gallery/*" = ["E402", "T201"]
"src/*/__init__.py" = ["F401"]
Expand All @@ -115,6 +116,6 @@ line-length = 120

# "test_gallery.py" = ["T201"] # Uncomment when test_gallery.py is created

[tool.ruff.mccabe]
[tool.ruff.lint.mccabe]
max-complexity = 17

Loading

0 comments on commit dbfb805

Please sign in to comment.