Mock

When creating tests within PyNWB and in downstream libraries, it is often necessary to create example instances of neurodata objects. However, this can be quite laborious for some types. For instance, creating an RoiResponseSeries would require you to make a DynamicTableRegion of a PlaneSegmentation table with the appropriate rows. This object in turn requires input of an ImageSegmentation object, which in turn requires a Device and an OpticalChannel object. In the end, creating a single neurodata object in this case requires the creation of 5 other objects. testing.mock is a module that creates boilerplate objects with a single line of code that can be used for testing. In this case, you could simply run

from pynwb.testing.mock.ophys import mock_RoiResponseSeries

roi_response_series = mock_RoiResponseSeries()

This acts much like the standard RoiResponseSeries class constructor, except that all of the fields have defaults. It auto-magically creates a DynamicTableRegion of a mock_PlaneSegmentation, which in turn calls the mock version of all the other necessary neurodata types. You can customize any of these fields just as you would normally, overriding these defaults:

from pynwb.testing.mock.ophys import mock_RoiResponseSeries

roi_response_series = mock_RoiResponseSeries(data=[[1,2,3], [1,2,3]])

If you want to create objects and automatically add them to an NWBFile, create an NWBFile and pass it into the mock function:

from pynwb.testing.mock.file import mock_NWBFile
from pynwb.testing.mock.ophys import mock_RoiResponseSeries

nwbfile = mock_NWBFile()
mock_RoiResponseSeries(nwbfile=nwbfile)

Now this NWBFile contains an RoiResponseSeries and all the upstream classes:

>>> print(nwbfile)

root pynwb.file.NWBFile at 0x4335131760
Fields:
  devices: {
    Device <class 'pynwb.device.Device'>,
    Device2 <class 'pynwb.device.Device'>
  }
  file_create_date: [datetime.datetime(2023, 6, 26, 21, 56, 44, 322249, tzinfo=tzlocal())]
  identifier: 3c13e816-a50f-49a9-85ec-93b9944c3e79
  imaging_planes: {
    ImagingPlane <class 'pynwb.ophys.ImagingPlane'>,
    ImagingPlane2 <class 'pynwb.ophys.ImagingPlane'>
  }
  processing: {
    ophys <class 'pynwb.base.ProcessingModule'>
  }
  session_description: session_description
  session_start_time: 1970-01-01 00:00:00-05:00
  timestamps_reference_time: 1970-01-01 00:00:00-05:00

Name generator

Two neurodata objects stored in the same location within an NWB file must have unique names. This can cause an error if you want to create a few neurodata objects with the same default name. To avoid this issue, each mock neurodata function uses the name_generator to generate unique names for each neurodata object. Consecutive neurodata objects of the same type will be named e.g. “TimeSeries”, “TimeSeries2”, “TimeSeries3”, etc.