From 727228c8bd871d0ffdcca48ac76a2010f293652d Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Sun, 25 Aug 2024 15:33:32 -0600 Subject: [PATCH] Add to_nwbfile to methods in roi extractors (#1027) --- CHANGELOG.md | 2 + .../ophys/baseimagingextractorinterface.py | 4 +- .../basesegmentationextractorinterface.py | 4 +- .../miniscopeimagingdatainterface.py | 4 +- src/neuroconv/tools/roiextractors/__init__.py | 17 +- .../tools/roiextractors/roiextractors.py | 597 +++++++++++++++--- tests/test_ophys/test_tools_roiextractors.py | 252 ++++---- 7 files changed, 645 insertions(+), 235 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1edae1bb0..50287da81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * Deprecated use of `compression` and `compression_options` in `VideoInterface` [PR #1005](https://github.com/catalystneuro/neuroconv/pull/1005) * `get_schema_from_method_signature` has been deprecated; please use `get_json_schema_from_method_signature` instead. [PR #1016](https://github.com/catalystneuro/neuroconv/pull/1016) * `neuroconv.utils.FilePathType` and `neuroconv.utils.FolderPathType` have been deprecated; please use `pydantic.FilePath` and `pydantic.DirectoryPath` instead. [PR #1017](https://github.com/catalystneuro/neuroconv/pull/1017) +* Changed the roiextractors.tool function (e.g. `add_imaging` and `add_segmentation`) to have the `_to_nwbfile` suffix [PR #1027][PR #1017](https://github.com/catalystneuro/neuroconv/pull/1027) + ### Features * Added MedPCInterface for operant behavioral output files. [PR #883](https://github.com/catalystneuro/neuroconv/pull/883) diff --git a/src/neuroconv/datainterfaces/ophys/baseimagingextractorinterface.py b/src/neuroconv/datainterfaces/ophys/baseimagingextractorinterface.py index e00bb0771..548b57d0c 100644 --- a/src/neuroconv/datainterfaces/ophys/baseimagingextractorinterface.py +++ b/src/neuroconv/datainterfaces/ophys/baseimagingextractorinterface.py @@ -147,7 +147,7 @@ def add_to_nwbfile( stub_test: bool = False, stub_frames: int = 100, ): - from ...tools.roiextractors import add_imaging + from ...tools.roiextractors import add_imaging_to_nwbfile if stub_test: stub_frames = min([stub_frames, self.imaging_extractor.get_num_frames()]) @@ -155,7 +155,7 @@ def add_to_nwbfile( else: imaging_extractor = self.imaging_extractor - add_imaging( + add_imaging_to_nwbfile( imaging=imaging_extractor, nwbfile=nwbfile, metadata=metadata, diff --git a/src/neuroconv/datainterfaces/ophys/basesegmentationextractorinterface.py b/src/neuroconv/datainterfaces/ophys/basesegmentationextractorinterface.py index 5fa6217cc..6b55b5afb 100644 --- a/src/neuroconv/datainterfaces/ophys/basesegmentationextractorinterface.py +++ b/src/neuroconv/datainterfaces/ophys/basesegmentationextractorinterface.py @@ -165,7 +165,7 @@ def add_to_nwbfile( ------- """ - from ...tools.roiextractors import add_segmentation + from ...tools.roiextractors import add_segmentation_to_nwbfile if stub_test: stub_frames = min([stub_frames, self.segmentation_extractor.get_num_frames()]) @@ -173,7 +173,7 @@ def add_to_nwbfile( else: segmentation_extractor = self.segmentation_extractor - add_segmentation( + add_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile, metadata=metadata, diff --git a/src/neuroconv/datainterfaces/ophys/miniscope/miniscopeimagingdatainterface.py b/src/neuroconv/datainterfaces/ophys/miniscope/miniscopeimagingdatainterface.py index e26cc69fb..594bf00dc 100644 --- a/src/neuroconv/datainterfaces/ophys/miniscope/miniscopeimagingdatainterface.py +++ b/src/neuroconv/datainterfaces/ophys/miniscope/miniscopeimagingdatainterface.py @@ -93,7 +93,7 @@ def add_to_nwbfile( ): from ndx_miniscope.utils import add_miniscope_device - from ....tools.roiextractors import add_photon_series + from ....tools.roiextractors import add_photon_series_to_nwbfile miniscope_timestamps = self.get_original_timestamps() imaging_extractor = self.imaging_extractor @@ -108,7 +108,7 @@ def add_to_nwbfile( device_metadata = metadata["Ophys"]["Device"][0] add_miniscope_device(nwbfile=nwbfile, device_metadata=device_metadata) - add_photon_series( + add_photon_series_to_nwbfile( imaging=imaging_extractor, nwbfile=nwbfile, metadata=metadata, diff --git a/src/neuroconv/tools/roiextractors/__init__.py b/src/neuroconv/tools/roiextractors/__init__.py index 1f45f9b89..5e009fe6d 100644 --- a/src/neuroconv/tools/roiextractors/__init__.py +++ b/src/neuroconv/tools/roiextractors/__init__.py @@ -1,6 +1,20 @@ from .roiextractors import ( + check_if_imaging_fits_into_memory, + get_nwb_imaging_metadata, + get_nwb_segmentation_metadata, add_background_fluorescence_traces, add_background_plane_segmentation, + add_devices_to_nwbfile, + add_fluorescence_traces_to_nwbfile, + add_image_segmentation_to_nwbfile, + add_imaging_to_nwbfile, + add_imaging_plane_to_nwbfile, + add_photon_series_to_nwbfile, + add_plane_segmentation_to_nwbfile, + add_segmentation_to_nwbfile, + add_summary_images_to_nwbfile, + write_imaging_to_nwbfile, + write_segmentation_to_nwbfile, add_devices, add_fluorescence_traces, add_image_segmentation, @@ -10,9 +24,6 @@ add_plane_segmentation, add_segmentation, add_summary_images, - check_if_imaging_fits_into_memory, - get_nwb_imaging_metadata, - get_nwb_segmentation_metadata, write_imaging, write_segmentation, ) diff --git a/src/neuroconv/tools/roiextractors/roiextractors.py b/src/neuroconv/tools/roiextractors/roiextractors.py index 75c5a0642..4da660914 100644 --- a/src/neuroconv/tools/roiextractors/roiextractors.py +++ b/src/neuroconv/tools/roiextractors/roiextractors.py @@ -1,8 +1,8 @@ import math +import warnings from collections import defaultdict from copy import deepcopy from typing import Literal, Optional -from warnings import warn import numpy as np import psutil @@ -42,7 +42,7 @@ from ...utils.str_utils import human_readable_size -def get_default_ophys_metadata() -> DeepDict: +def _get_default_ophys_metadata() -> DeepDict: """Fill default metadata for Device and ImagingPlane.""" metadata = get_default_nwbfile_metadata() @@ -74,9 +74,9 @@ def get_default_ophys_metadata() -> DeepDict: return metadata -def get_default_segmentation_metadata() -> DeepDict: +def _get_default_segmentation_metadata() -> DeepDict: """Fill default metadata for segmentation.""" - metadata = get_default_ophys_metadata() + metadata = _get_default_ophys_metadata() default_fluorescence_roi_response_series = dict( name="RoiResponseSeries", description="Array of raw fluorescence traces.", unit="n.a." @@ -153,7 +153,7 @@ def get_nwb_imaging_metadata( imgextractor : ImagingExtractor photon_series_type : {'OnePhotonSeries', 'TwoPhotonSeries'}, optional """ - metadata = get_default_ophys_metadata() + metadata = _get_default_ophys_metadata() channel_name_list = imgextractor.get_channel_names() or ( ["OpticalChannel"] @@ -189,6 +189,28 @@ def get_nwb_imaging_metadata( def add_devices(nwbfile: NWBFile, metadata: Optional[dict] = None) -> NWBFile: + """ + Deprecated function. Use 'add_devices_to_nwbfile' instead. + """ + + message = ( + "Function 'add_devices' is deprecated and will be removed on or after March 2025. " + "Use 'add_devices_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_devices_to_nwbfile( + nwbfile=nwbfile, + metadata=metadata, + ) + + +def add_devices_to_nwbfile(nwbfile: NWBFile, metadata: Optional[dict] = None) -> NWBFile: """ Add optical physiology devices from metadata. The metadata concerning the optical physiology should be stored in metadata["Ophys]["Device"] @@ -196,7 +218,7 @@ def add_devices(nwbfile: NWBFile, metadata: Optional[dict] = None) -> NWBFile: """ metadata_copy = {} if metadata is None else deepcopy(metadata) - default_metadata = get_default_ophys_metadata() + default_metadata = _get_default_ophys_metadata() metadata_copy = dict_deep_update(default_metadata, metadata_copy, append_list=False) device_metadata = metadata_copy["Ophys"]["Device"] @@ -243,7 +265,33 @@ def add_imaging_plane( nwbfile: NWBFile, metadata: dict, imaging_plane_name: Optional[str] = None, - imaging_plane_index: Optional[int] = None, +): + """ + Deprecated function. Use 'add_imaging_plane_to_nwbfile' instead. + """ + + message = ( + "Function 'add_imaging_plane' is deprecated and will be removed on or after March 2025. " + "Use 'add_imaging_plane_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_imaging_plane_to_nwbfile( + nwbfile=nwbfile, + metadata=metadata, + imaging_plane_name=imaging_plane_name, + ) + + +def add_imaging_plane_to_nwbfile( + nwbfile: NWBFile, + metadata: dict, + imaging_plane_name: Optional[str] = None, ) -> NWBFile: """ Adds the imaging plane specified by the metadata to the nwb file. @@ -257,27 +305,18 @@ def add_imaging_plane( The metadata in the nwb conversion tools format. imaging_plane_name: str The name of the imaging plane to be added. - imaging_plane_index: int, optional - Returns ------- NWBFile The nwbfile passed as an input with the imaging plane added. """ - if imaging_plane_index is not None: - warn( - message="Keyword argument 'imaging_plane_index' is deprecated and will be removed on or after Dec 1st, 2023. " - "Use 'imaging_plane_name' to specify which imaging plane to add by its name.", - category=DeprecationWarning, - ) - imaging_plane_name = metadata["Ophys"]["ImagingPlane"][imaging_plane_index]["name"] # Set the defaults and required infrastructure metadata_copy = deepcopy(metadata) - default_metadata = get_default_ophys_metadata() + default_metadata = _get_default_ophys_metadata() metadata_copy = dict_deep_update(default_metadata, metadata_copy, append_list=False) - add_devices(nwbfile=nwbfile, metadata=metadata_copy) + add_devices_to_nwbfile(nwbfile=nwbfile, metadata=metadata_copy) default_imaging_plane_name = default_metadata["Ophys"]["ImagingPlane"][0]["name"] imaging_plane_name = imaging_plane_name or default_imaging_plane_name @@ -306,6 +345,28 @@ def add_imaging_plane( def add_image_segmentation(nwbfile: NWBFile, metadata: dict) -> NWBFile: + """ + Deprecated function. Use 'add_image_segmentation_to_nwbfile' instead. + """ + + message = ( + "Function 'add_image_segmentation' is deprecated and will be removed on or after March 2025. " + "Use 'add_image_segmentation_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_image_segmentation_to_nwbfile( + nwbfile=nwbfile, + metadata=metadata, + ) + + +def add_image_segmentation_to_nwbfile(nwbfile: NWBFile, metadata: dict) -> NWBFile: """ Adds the image segmentation specified by the metadata to the nwb file. @@ -323,7 +384,7 @@ def add_image_segmentation(nwbfile: NWBFile, metadata: dict) -> NWBFile: """ # Set the defaults and required infrastructure metadata_copy = deepcopy(metadata) - default_metadata = get_default_segmentation_metadata() + default_metadata = _get_default_segmentation_metadata() metadata_copy = dict_deep_update(default_metadata, metadata_copy, append_list=False) image_segmentation_metadata = metadata_copy["Ophys"]["ImageSegmentation"] @@ -345,7 +406,43 @@ def add_photon_series( photon_series_type: Literal["TwoPhotonSeries", "OnePhotonSeries"] = "TwoPhotonSeries", photon_series_index: int = 0, parent_container: Literal["acquisition", "processing/ophys"] = "acquisition", - two_photon_series_index: Optional[int] = None, # TODO: to be removed + iterator_type: Optional[str] = "v2", + iterator_options: Optional[dict] = None, +) -> NWBFile: + """ + Deprecated function. Use 'add_photon_series_to_nwbfile' instead. + """ + + message = ( + "Function 'add_photon_series' is deprecated and will be removed on or after March 2025. " + "Use 'add_photon_series_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_photon_series_to_nwbfile( + imaging=imaging, + nwbfile=nwbfile, + metadata=metadata, + photon_series_type=photon_series_type, + photon_series_index=photon_series_index, + parent_container=parent_container, + iterator_type=iterator_type, + iterator_options=iterator_options, + ) + + +def add_photon_series_to_nwbfile( + imaging: ImagingExtractor, + nwbfile: NWBFile, + metadata: Optional[dict] = None, + photon_series_type: Literal["TwoPhotonSeries", "OnePhotonSeries"] = "TwoPhotonSeries", + photon_series_index: int = 0, + parent_container: Literal["acquisition", "processing/ophys"] = "acquisition", iterator_type: Optional[str] = "v2", iterator_options: Optional[dict] = None, ) -> NWBFile: @@ -382,12 +479,6 @@ def add_photon_series( The NWBFile passed as an input with the photon series added. """ - if two_photon_series_index: - warn( - "Keyword argument 'two_photon_series_index' is deprecated and it will be removed on 2024-04-16. Use 'photon_series_index' instead." - ) - photon_series_index = two_photon_series_index - iterator_options = iterator_options or dict() metadata_copy = {} if metadata is None else deepcopy(metadata) @@ -400,7 +491,7 @@ def add_photon_series( ) if photon_series_type == "TwoPhotonSeries" and "OnePhotonSeries" in metadata_copy["Ophys"]: - warn( + warnings.warn( "Received metadata for both 'OnePhotonSeries' and 'TwoPhotonSeries', make sure photon_series_type is specified correctly." ) @@ -422,7 +513,7 @@ def add_photon_series( # Add the image plane to nwb imaging_plane_name = photon_series_metadata["imaging_plane"] - add_imaging_plane(nwbfile=nwbfile, metadata=metadata_copy, imaging_plane_name=imaging_plane_name) + add_imaging_plane_to_nwbfile(nwbfile=nwbfile, metadata=metadata_copy, imaging_plane_name=imaging_plane_name) imaging_plane = nwbfile.get_imaging_plane(name=imaging_plane_name) photon_series_kwargs = deepcopy(photon_series_metadata) photon_series_kwargs.update(imaging_plane=imaging_plane) @@ -554,9 +645,46 @@ def add_imaging( iterator_type: Optional[str] = "v2", iterator_options: Optional[dict] = None, parent_container: Literal["acquisition", "processing/ophys"] = "acquisition", +) -> NWBFile: + """ + Deprecated function. Use 'add_imaging_to_nwbfile' instead. + """ + + message = ( + "Function 'add_imaging' is deprecated and will be removed on or after March 2025. " + "Use 'add_imaging_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_imaging_to_nwbfile( + imaging=imaging, + nwbfile=nwbfile, + metadata=metadata, + photon_series_type=photon_series_type, + photon_series_index=photon_series_index, + iterator_type=iterator_type, + iterator_options=iterator_options, + parent_container=parent_container, + ) + + +def add_imaging_to_nwbfile( + imaging: ImagingExtractor, + nwbfile: NWBFile, + metadata: Optional[dict] = None, + photon_series_type: Literal["TwoPhotonSeries", "OnePhotonSeries"] = "TwoPhotonSeries", + photon_series_index: int = 0, + iterator_type: Optional[str] = "v2", + iterator_options: Optional[dict] = None, + parent_container: Literal["acquisition", "processing/ophys"] = "acquisition", ): - add_devices(nwbfile=nwbfile, metadata=metadata) - add_photon_series( + add_devices_to_nwbfile(nwbfile=nwbfile, metadata=metadata) + add_photon_series_to_nwbfile( imaging=imaging, nwbfile=nwbfile, metadata=metadata, @@ -578,7 +706,45 @@ def write_imaging( iterator_type: str = "v2", iterator_options: Optional[dict] = None, photon_series_type: Literal["TwoPhotonSeries", "OnePhotonSeries"] = "TwoPhotonSeries", - buffer_size: Optional[int] = None, # TODO: to be removed +): + """ + Deprecated function. Use 'write_imaging_to_nwbfile' instead. + """ + + message = ( + "Function 'write_imaging' is deprecated and will be removed on or after March 2025. " + "Use 'write_imaging_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return write_imaging_to_nwbfile( + imaging=imaging, + nwbfile_path=nwbfile_path, + nwbfile=nwbfile, + metadata=metadata, + overwrite=overwrite, + verbose=verbose, + iterator_type=iterator_type, + iterator_options=iterator_options, + photon_series_type=photon_series_type, + ) + + +def write_imaging_to_nwbfile( + imaging: ImagingExtractor, + nwbfile_path: Optional[FilePath] = None, + nwbfile: Optional[NWBFile] = None, + metadata: Optional[dict] = None, + overwrite: bool = False, + verbose: bool = True, + iterator_type: str = "v2", + iterator_options: Optional[dict] = None, + photon_series_type: Literal["TwoPhotonSeries", "OnePhotonSeries"] = "TwoPhotonSeries", ): """ Primary method for writing an ImagingExtractor object to an NWBFile. @@ -626,11 +792,6 @@ def write_imaging( assert isinstance(nwbfile, NWBFile), "'nwbfile' should be of type pynwb.NWBFile" iterator_options = iterator_options or dict() - if buffer_size: - warn( - "Keyword argument 'buffer_size' is deprecated and will be removed on or after September 1st, 2022." - "Specify as a key in the new 'iterator_options' dictionary instead." - ) if metadata is None: metadata = dict() @@ -640,7 +801,7 @@ def write_imaging( with make_or_load_nwbfile( nwbfile_path=nwbfile_path, nwbfile=nwbfile, metadata=metadata, overwrite=overwrite, verbose=verbose ) as nwbfile_out: - add_imaging( + add_imaging_to_nwbfile( imaging=imaging, nwbfile=nwbfile, metadata=metadata, @@ -659,7 +820,7 @@ def get_nwb_segmentation_metadata(sgmextractor: SegmentationExtractor) -> dict: ---------- sgmextractor: SegmentationExtractor """ - metadata = get_default_segmentation_metadata() + metadata = _get_default_segmentation_metadata() # Optical Channel name: for i in range(sgmextractor.get_num_channels()): ch_name = sgmextractor.get_channel_names()[i] @@ -693,7 +854,45 @@ def add_plane_segmentation( nwbfile: NWBFile, metadata: Optional[dict], plane_segmentation_name: Optional[str] = None, - plane_segmentation_index: Optional[int] = None, # TODO: to be removed + include_roi_centroids: bool = True, + include_roi_acceptance: bool = True, + mask_type: Optional[str] = "image", # Optional[Literal["image", "pixel"]] + iterator_options: Optional[dict] = None, + compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 +) -> NWBFile: + """ + Deprecated function. Use 'add_plane_segmentation_to_nwbfile' instead. + """ + + message = ( + "Function 'add_plane_segmentation' is deprecated and will be removed on or after March 2025. " + "Use 'add_plane_segmentation_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_plane_segmentation_to_nwbfile( + segmentation_extractor=segmentation_extractor, + nwbfile=nwbfile, + metadata=metadata, + plane_segmentation_name=plane_segmentation_name, + include_roi_centroids=include_roi_centroids, + include_roi_acceptance=include_roi_acceptance, + mask_type=mask_type, + iterator_options=iterator_options, + compression_options=compression_options, + ) + + +def add_plane_segmentation_to_nwbfile( + segmentation_extractor: SegmentationExtractor, + nwbfile: NWBFile, + metadata: Optional[dict], + plane_segmentation_name: Optional[str] = None, include_roi_centroids: bool = True, include_roi_acceptance: bool = True, mask_type: Optional[str] = "image", # Optional[Literal["image", "pixel"]] @@ -745,7 +944,7 @@ def add_plane_segmentation( """ # TODO: remove completely after 10/1/2024 if compression_options is not None: - warn( + warnings.warn( message=( "Specifying compression methods and their options at the level of tool functions has been deprecated. " "Please use the `configure_backend` tool function for this purpose." @@ -777,6 +976,7 @@ def add_plane_segmentation( roi_locations = segmentation_extractor.get_roi_locations()[tranpose_image_convention, :].T else: roi_locations = None + nwbfile = _add_plane_segmentation( background_or_roi_ids=roi_ids, image_or_pixel_masks=image_or_pixel_masks, @@ -787,7 +987,6 @@ def add_plane_segmentation( nwbfile=nwbfile, metadata=metadata, plane_segmentation_name=plane_segmentation_name, - plane_segmentation_index=plane_segmentation_index, include_roi_centroids=include_roi_centroids, include_roi_acceptance=include_roi_acceptance, mask_type=mask_type, @@ -803,7 +1002,6 @@ def _add_plane_segmentation( nwbfile: NWBFile, metadata: Optional[dict], plane_segmentation_name: Optional[str] = None, - plane_segmentation_index: Optional[int] = None, # TODO: to be removed include_roi_centroids: bool = False, roi_locations: Optional[np.ndarray] = None, include_roi_acceptance: bool = False, @@ -812,11 +1010,12 @@ def _add_plane_segmentation( mask_type: Optional[str] = "image", # Optional[Literal["image", "pixel"]] iterator_options: Optional[dict] = None, ) -> NWBFile: + iterator_options = iterator_options or dict() # Set the defaults and required infrastructure metadata_copy = deepcopy(metadata) - default_metadata = get_default_segmentation_metadata() + default_metadata = _get_default_segmentation_metadata() metadata_copy = dict_deep_update(default_metadata, metadata_copy, append_list=False) image_segmentation_metadata = metadata_copy["Ophys"]["ImageSegmentation"] @@ -826,11 +1025,7 @@ def _add_plane_segmentation( "name" ] ) - if plane_segmentation_index: - warn( - "Keyword argument 'plane_segmentation_index' is deprecated and it will be removed on 2024-04-16. Use 'plane_segmentation_name' instead." - ) - plane_segmentation_name = image_segmentation_metadata["plane_segmentations"][plane_segmentation_index]["name"] + plane_segmentation_metadata = next( ( plane_segmentation_metadata @@ -845,8 +1040,8 @@ def _add_plane_segmentation( ) imaging_plane_name = plane_segmentation_metadata["imaging_plane"] - add_imaging_plane(nwbfile=nwbfile, metadata=metadata_copy, imaging_plane_name=imaging_plane_name) - add_image_segmentation(nwbfile=nwbfile, metadata=metadata_copy) + add_imaging_plane_to_nwbfile(nwbfile=nwbfile, metadata=metadata_copy, imaging_plane_name=imaging_plane_name) + add_image_segmentation_to_nwbfile(nwbfile=nwbfile, metadata=metadata_copy) ophys = get_module(nwbfile, "ophys") image_segmentation_name = image_segmentation_metadata["name"] @@ -877,13 +1072,13 @@ def _add_plane_segmentation( "Please open a ticket with https://github.com/catalystneuro/roiextractors/issues" ) if mask_type == "pixel" and num_pixel_dims == 4: - warn( + warnings.warn( "Specified mask_type='pixel', but ROIExtractors returned 4-dimensional masks. " "Using mask_type='voxel' instead." ) mask_type = "voxel" if mask_type == "voxel" and num_pixel_dims == 3: - warn( + warnings.warn( "Specified mask_type='voxel', but ROIExtractors returned 3-dimensional masks. " "Using mask_type='pixel' instead." ) @@ -928,9 +1123,46 @@ def add_background_plane_segmentation( iterator_options: Optional[dict] = None, compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 ) -> NWBFile: + """ + Deprecated function. Use 'add_background_plane_segmentation_to_nwbfile' instead. + """ + + message = ( + "Function 'add_background_plane_segmentation' is deprecated and will be removed on or after March 2025. " + "Use 'add_background_plane_segmentation_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_background_plane_segmentation_to_nwbfile( + segmentation_extractor=segmentation_extractor, + nwbfile=nwbfile, + metadata=metadata, + background_plane_segmentation_name=background_plane_segmentation_name, + mask_type=mask_type, + iterator_options=iterator_options, + compression_options=compression_options, + ) + + +def add_background_plane_segmentation_to_nwbfile( + segmentation_extractor: SegmentationExtractor, + nwbfile: NWBFile, + metadata: Optional[dict], + background_plane_segmentation_name: Optional[str] = None, + mask_type: Optional[str] = "image", # Optional[Literal["image", "pixel"]] + iterator_options: Optional[dict] = None, + compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 +) -> NWBFile: + # TODO needs docstring + # TODO: remove completely after 10/1/2024 if compression_options is not None: - warn( + warnings.warn( message=( "Specifying compression methods and their options at the level of tool functions has been deprecated. " "Please use the `configure_backend` tool function for this purpose." @@ -971,7 +1203,41 @@ def add_fluorescence_traces( metadata: Optional[dict], plane_segmentation_name: Optional[str] = None, include_background_segmentation: bool = False, - plane_index: Optional[int] = None, # TODO: to be removed + iterator_options: Optional[dict] = None, + compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 +) -> NWBFile: + """ + Deprecated function. Use 'add_fluorescence_traces_to_nwbfile' instead. + """ + + message = ( + "Function 'add_fluorescence_traces' is deprecated and will be removed on or after March 2025. " + "Use 'add_fluorescence_traces_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_fluorescence_traces_to_nwbfile( + segmentation_extractor=segmentation_extractor, + nwbfile=nwbfile, + metadata=metadata, + plane_segmentation_name=plane_segmentation_name, + include_background_segmentation=include_background_segmentation, + iterator_options=iterator_options, + compression_options=compression_options, + ) + + +def add_fluorescence_traces_to_nwbfile( + segmentation_extractor: SegmentationExtractor, + nwbfile: NWBFile, + metadata: Optional[dict], + plane_segmentation_name: Optional[str] = None, + include_background_segmentation: bool = False, iterator_options: Optional[dict] = None, compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 ) -> NWBFile: @@ -1002,7 +1268,7 @@ def add_fluorescence_traces( """ # TODO: remove completely after 10/1/2024 if compression_options is not None: - warn( + warnings.warn( message=( "Specifying compression methods and their options at the level of tool functions has been deprecated. " "Please use the `configure_backend` tool function for this purpose." @@ -1024,7 +1290,7 @@ def add_fluorescence_traces( return nwbfile roi_ids = segmentation_extractor.get_roi_ids() - nwbfile = _add_fluorescence_traces( + nwbfile = _add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, traces_to_add=traces_to_add, background_or_roi_ids=roi_ids, @@ -1032,13 +1298,12 @@ def add_fluorescence_traces( metadata=metadata, default_plane_segmentation_index=default_plane_segmentation_index, plane_segmentation_name=plane_segmentation_name, - plane_index=plane_index, iterator_options=iterator_options, ) return nwbfile -def _add_fluorescence_traces( +def _add_fluorescence_traces_to_nwbfile( segmentation_extractor: SegmentationExtractor, traces_to_add: dict, background_or_roi_ids: list, @@ -1046,23 +1311,15 @@ def _add_fluorescence_traces( metadata: Optional[dict], default_plane_segmentation_index: int, plane_segmentation_name: Optional[str] = None, - plane_index: Optional[int] = None, # TODO: to be removed iterator_options: Optional[dict] = None, ): iterator_options = iterator_options or dict() # Set the defaults and required infrastructure metadata_copy = deepcopy(metadata) - default_metadata = get_default_segmentation_metadata() + default_metadata = _get_default_segmentation_metadata() metadata_copy = dict_deep_update(default_metadata, metadata_copy, append_list=False) - if plane_index: - warn( - "Keyword argument 'plane_index' is deprecated and it will be removed on 2024-04-16. Use 'plane_segmentation_name' instead." - ) - plane_segmentation_name = metadata_copy["Ophys"]["ImageSegmentation"]["plane_segmentations"][plane_index][ - "name" - ] plane_segmentation_name = ( plane_segmentation_name or default_metadata["Ophys"]["ImageSegmentation"]["plane_segmentations"][default_plane_segmentation_index][ @@ -1154,7 +1411,6 @@ def _create_roi_table_region( nwbfile: NWBFile, metadata: dict, plane_segmentation_name: Optional[str] = None, - plane_index: Optional[int] = None, ): """Private method to create ROI table region. @@ -1171,13 +1427,7 @@ def _create_roi_table_region( """ image_segmentation_metadata = metadata["Ophys"]["ImageSegmentation"] - if plane_index: - warn( - "Keyword argument 'plane_index' is deprecated and it will be removed on 2024-04-16. Use 'plane_segmentation_name' instead." - ) - plane_segmentation_name = image_segmentation_metadata["plane_segmentations"][plane_index]["name"] - - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile, metadata=metadata, @@ -1227,7 +1477,39 @@ def add_background_fluorescence_traces( nwbfile: NWBFile, metadata: Optional[dict], background_plane_segmentation_name: Optional[str] = None, - plane_index: Optional[int] = None, # TODO: to be removed + iterator_options: Optional[dict] = None, + compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 +) -> NWBFile: + """ + Deprecated function. Use 'add_background_fluorescence_traces_to_nwbfile' instead. + """ + + message = ( + "Function 'add_background_fluorescence_traces' is deprecated and will be removed on or after March 2025. " + "Use 'add_background_fluorescence_traces_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_background_fluorescence_traces_to_nwbfile( + segmentation_extractor=segmentation_extractor, + nwbfile=nwbfile, + metadata=metadata, + background_plane_segmentation_name=background_plane_segmentation_name, + iterator_options=iterator_options, + compression_options=compression_options, + ) + + +def add_background_fluorescence_traces_to_nwbfile( + segmentation_extractor: SegmentationExtractor, + nwbfile: NWBFile, + metadata: Optional[dict], + background_plane_segmentation_name: Optional[str] = None, iterator_options: Optional[dict] = None, compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 ) -> NWBFile: @@ -1255,7 +1537,7 @@ def add_background_fluorescence_traces( """ # TODO: remove completely after 10/1/2024 if compression_options is not None: - warn( + warnings.warn( message=( "Specifying compression methods and their options at the level of tool functions has been deprecated. " "Please use the `configure_backend` tool function for this purpose." @@ -1277,7 +1559,7 @@ def add_background_fluorescence_traces( return nwbfile background_ids = segmentation_extractor.get_background_ids() - nwbfile = _add_fluorescence_traces( + nwbfile = _add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, traces_to_add=traces_to_add, background_or_roi_ids=background_ids, @@ -1285,18 +1567,45 @@ def add_background_fluorescence_traces( metadata=metadata, default_plane_segmentation_index=default_plane_segmentation_index, plane_segmentation_name=background_plane_segmentation_name, - plane_index=plane_index, iterator_options=iterator_options, ) return nwbfile def add_summary_images( + segmentation_extractor: SegmentationExtractor, + nwbfile: NWBFile, + metadata: Optional[dict] = None, + plane_segmentation_name: Optional[str] = None, +) -> NWBFile: + """ + Deprecated function. Use 'add_summary_images_to_nwbfile' instead. + """ + + message = ( + "Function 'add_summary_images' is deprecated and will be removed on or after March 2025. " + "Use 'add_summary_images_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_summary_images_to_nwbfile( + nwbfile=nwbfile, + segmentation_extractor=segmentation_extractor, + metadata=metadata, + plane_segmentation_name=plane_segmentation_name, + ) + + +def add_summary_images_to_nwbfile( nwbfile: NWBFile, segmentation_extractor: SegmentationExtractor, metadata: Optional[dict] = None, plane_segmentation_name: Optional[str] = None, - images_set_name: Optional[str] = None, # TODO: to be removed ) -> NWBFile: """ Adds summary images (i.e. mean and correlation) to the nwbfile using an image container object pynwb.Image @@ -1317,23 +1626,14 @@ def add_summary_images( NWBFile The nwbfile passed as an input with the summary images added. """ - if metadata is None: - metadata = dict() + metadata = metadata or dict() # Set the defaults and required infrastructure metadata_copy = deepcopy(metadata) - default_metadata = get_default_segmentation_metadata() + default_metadata = _get_default_segmentation_metadata() metadata_copy = dict_deep_update(default_metadata, metadata_copy, append_list=False) segmentation_images_metadata = metadata_copy["Ophys"]["SegmentationImages"] - - if images_set_name is not None: - warn( - "Keyword argument 'images_set_name' is deprecated it will be removed on 2024-04-16." - "Specify the name of the Images container in metadata['Ophys']['SegmentationImages'] instead." - ) - segmentation_images_metadata["name"] = images_set_name - images_container_name = segmentation_images_metadata["name"] images_dict = segmentation_extractor.get_images_dict() @@ -1375,18 +1675,61 @@ def add_segmentation( plane_segmentation_name: Optional[str] = None, background_plane_segmentation_name: Optional[str] = None, include_background_segmentation: bool = False, - plane_num: Optional[int] = None, # TODO: to be removed include_roi_centroids: bool = True, include_roi_acceptance: bool = True, mask_type: Optional[str] = "image", # Literal["image", "pixel"] iterator_options: Optional[dict] = None, compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 -): +) -> NWBFile: + """ + Deprecated function. Use 'add_segmentation_to_nwbfile' instead. + """ + + message = ( + "Function 'add_segmentation' is deprecated and will be removed on or after March 2025. " + "Use 'add_segmentation_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return add_segmentation_to_nwbfile( + segmentation_extractor=segmentation_extractor, + nwbfile=nwbfile, + metadata=metadata, + plane_segmentation_name=plane_segmentation_name, + background_plane_segmentation_name=background_plane_segmentation_name, + include_background_segmentation=include_background_segmentation, + include_roi_centroids=include_roi_centroids, + include_roi_acceptance=include_roi_acceptance, + mask_type=mask_type, + iterator_options=iterator_options, + compression_options=compression_options, + ) + + +def add_segmentation_to_nwbfile( + segmentation_extractor: SegmentationExtractor, + nwbfile: NWBFile, + metadata: Optional[dict] = None, + plane_segmentation_name: Optional[str] = None, + background_plane_segmentation_name: Optional[str] = None, + include_background_segmentation: bool = False, + include_roi_centroids: bool = True, + include_roi_acceptance: bool = True, + mask_type: Optional[str] = "image", # Literal["image", "pixel"] + iterator_options: Optional[dict] = None, + compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 +) -> NWBFile: # TODO: remove completely after 10/1/2024 if compression_options is not None: - warn( + warnings.warn( message=( "Specifying compression methods and their options at the level of tool functions has been deprecated. " + "The option will be removed after 2024-10-01. " "Please use the `configure_backend` tool function for this purpose." ), category=DeprecationWarning, @@ -1394,11 +1737,10 @@ def add_segmentation( ) # Add device: - add_devices(nwbfile=nwbfile, metadata=metadata) + add_devices_to_nwbfile(nwbfile=nwbfile, metadata=metadata) - # `add_imaging_plane` is also called from `add_plane_segmentation` so no need to call it explicitly here # Add PlaneSegmentation: - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile, metadata=metadata, @@ -1409,7 +1751,7 @@ def add_segmentation( iterator_options=iterator_options, ) if include_background_segmentation: - add_background_plane_segmentation( + add_background_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile, metadata=metadata, @@ -1419,7 +1761,7 @@ def add_segmentation( ) # Add fluorescence traces: - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile, metadata=metadata, @@ -1427,8 +1769,9 @@ def add_segmentation( include_background_segmentation=include_background_segmentation, iterator_options=iterator_options, ) + if include_background_segmentation: - add_background_fluorescence_traces( + add_background_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile, metadata=metadata, @@ -1437,13 +1780,15 @@ def add_segmentation( ) # Adding summary images (mean and correlation) - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=nwbfile, segmentation_extractor=segmentation_extractor, metadata=metadata, plane_segmentation_name=plane_segmentation_name, ) + return nwbfile + def write_segmentation( segmentation_extractor: SegmentationExtractor, @@ -1458,6 +1803,51 @@ def write_segmentation( mask_type: Optional[str] = "image", # Literal["image", "pixel"] iterator_options: Optional[dict] = None, compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 +) -> NWBFile: + """ + Deprecated function. Use 'write_segmentation_to_nwbfile' instead. + """ + + message = ( + "Function 'write_segmentation' is deprecated and will be removed on or after March 2025. " + "Use 'write_segmentation_to_nwbfile' instead." + ) + + warnings.warn( + message=message, + category=DeprecationWarning, + stacklevel=2, + ) + + return write_segmentation_to_nwbfile( + segmentation_extractor=segmentation_extractor, + nwbfile_path=nwbfile_path, + nwbfile=nwbfile, + metadata=metadata, + overwrite=overwrite, + verbose=verbose, + include_background_segmentation=include_background_segmentation, + include_roi_centroids=include_roi_centroids, + include_roi_acceptance=include_roi_acceptance, + mask_type=mask_type, + iterator_options=iterator_options, + compression_options=compression_options, + ) + + +def write_segmentation_to_nwbfile( + segmentation_extractor: SegmentationExtractor, + nwbfile_path: Optional[FilePath] = None, + nwbfile: Optional[NWBFile] = None, + metadata: Optional[dict] = None, + overwrite: bool = False, + verbose: bool = True, + include_background_segmentation: bool = False, + include_roi_centroids: bool = True, + include_roi_acceptance: bool = True, + mask_type: Optional[str] = "image", # Literal["image", "pixel"] + iterator_options: Optional[dict] = None, + compression_options: Optional[dict] = None, # TODO: remove completely after 10/1/2024 ) -> NWBFile: """ Primary method for writing an SegmentationExtractor object to an NWBFile. @@ -1516,9 +1906,10 @@ def write_segmentation( # TODO: remove completely after 10/1/2024 if compression_options is not None: - warn( + warnings.warn( message=( "Specifying compression methods and their options at the level of tool functions has been deprecated. " + "They will be removed on or after 2024-10-01. " "Please use the `configure_backend` tool function for this purpose." ), category=DeprecationWarning, @@ -1560,7 +1951,7 @@ def write_segmentation( for plane_no_loop, (segmentation_extractor, metadata) in enumerate( zip(segmentation_extractors, metadata_base_list) ): - add_segmentation( + add_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=nwbfile_out, metadata=metadata, diff --git a/tests/test_ophys/test_tools_roiextractors.py b/tests/test_ophys/test_tools_roiextractors.py index f535b6469..60162527b 100644 --- a/tests/test_ophys/test_tools_roiextractors.py +++ b/tests/test_ophys/test_tools_roiextractors.py @@ -27,20 +27,20 @@ from neuroconv.tools.nwb_helpers import get_module from neuroconv.tools.roiextractors import ( - add_devices, - add_fluorescence_traces, - add_image_segmentation, - add_imaging_plane, - add_photon_series, - add_plane_segmentation, - add_summary_images, + add_devices_to_nwbfile, + add_fluorescence_traces_to_nwbfile, + add_image_segmentation_to_nwbfile, + add_imaging_plane_to_nwbfile, + add_photon_series_to_nwbfile, + add_plane_segmentation_to_nwbfile, + add_summary_images_to_nwbfile, check_if_imaging_fits_into_memory, ) from neuroconv.tools.roiextractors.imagingextractordatachunkiterator import ( ImagingExtractorDataChunkIterator, ) from neuroconv.tools.roiextractors.roiextractors import ( - get_default_segmentation_metadata, + _get_default_segmentation_metadata, ) from neuroconv.utils import dict_deep_update @@ -60,7 +60,7 @@ def test_add_device(self): device_name = "new_device" device_list = [dict(name=device_name)] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -74,7 +74,7 @@ def test_add_device_with_further_metadata(self): device_list = [dict(name=device_name, description=description, manufacturer=manufacturer)] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices device = devices["new_device"] @@ -88,7 +88,7 @@ def test_add_two_devices(self): device_name_list = ["device1", "device2"] device_list = [dict(name=device_name) for device_name in device_name_list] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -99,12 +99,12 @@ def test_add_one_device_and_then_another(self): device_name1 = "new_device" device_list = [dict(name=device_name1)] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) device_name2 = "another_device" device_list = [dict(name=device_name2)] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -116,12 +116,12 @@ def test_not_overwriting_devices(self): device_name1 = "same_device" device_list = [dict(name=device_name1)] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) device_name2 = "same_device" device_list = [dict(name=device_name2)] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -129,7 +129,7 @@ def test_not_overwriting_devices(self): assert device_name1 in devices def test_add_device_defaults(self): - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -139,7 +139,7 @@ def test_add_device_defaults(self): def test_add_empty_device_list_in_metadata(self): device_list = [] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -150,7 +150,7 @@ def test_device_object(self): device_object = Device(name=device_name) device_list = [device_object] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -162,7 +162,7 @@ def test_device_object_and_metadata_mix(self): device_metadata = dict(name="device_metadata") device_list = [device_object, device_metadata] self.metadata["Ophys"].update(Device=device_list) - add_devices(self.nwbfile, metadata=self.metadata) + add_devices_to_nwbfile(self.nwbfile, metadata=self.metadata) devices = self.nwbfile.devices @@ -206,8 +206,10 @@ def setUp(self): self.metadata["Ophys"].update(ImagingPlane=[self.imaging_plane_metadata]) - def test_add_imaging_plane(self): - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=self.imaging_plane_name) + def test_add_imaging_plane_to_nwbfile(self): + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=self.imaging_plane_name + ) imaging_planes = self.nwbfile.imaging_planes assert len(imaging_planes) == 1 @@ -217,10 +219,14 @@ def test_add_imaging_plane(self): assert imaging_plane.description == self.imaging_plane_description def test_not_overwriting_imaging_plane_if_same_name(self): - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=self.imaging_plane_name) + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=self.imaging_plane_name + ) self.imaging_plane_metadata["description"] = "modified description" - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=self.imaging_plane_name) + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=self.imaging_plane_name + ) imaging_planes = self.nwbfile.imaging_planes assert len(imaging_planes) == 1 @@ -232,14 +238,18 @@ def test_add_two_imaging_planes(self): first_imaging_plane_description = "first_imaging_plane_description" self.imaging_plane_metadata["name"] = first_imaging_plane_name self.imaging_plane_metadata["description"] = first_imaging_plane_description - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=first_imaging_plane_name) + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=first_imaging_plane_name + ) # Add the second imaging plane second_imaging_plane_name = "second_imaging_plane_name" second_imaging_plane_description = "second_imaging_plane_description" self.imaging_plane_metadata["name"] = second_imaging_plane_name self.imaging_plane_metadata["description"] = second_imaging_plane_description - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=second_imaging_plane_name) + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=second_imaging_plane_name + ) # Test expected values imaging_planes = self.nwbfile.imaging_planes @@ -253,14 +263,16 @@ def test_add_two_imaging_planes(self): assert second_imaging_plane.name == second_imaging_plane_name assert second_imaging_plane.description == second_imaging_plane_description - def test_add_imaging_plane_raises_when_name_not_found_in_metadata(self): + def test_add_imaging_plane_to_nwbfile_raises_when_name_not_found_in_metadata(self): """Test adding an imaging plane raises an error when the name is not found in the metadata.""" imaging_plane_name = "imaging_plane_non_existing_in_the_metadata" with self.assertRaisesWith( exc_type=ValueError, exc_msg=f"Metadata for Imaging Plane '{imaging_plane_name}' not found in metadata['Ophys']['ImagingPlane'].", ): - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=imaging_plane_name) + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_name=imaging_plane_name + ) def test_add_two_imaging_planes_from_metadata(self): """Test adding two imaging planes when there are multiple imaging plane metadata.""" @@ -271,8 +283,12 @@ def test_add_two_imaging_planes_from_metadata(self): second_imaging_plane_metadata = deepcopy(metadata["Ophys"]["ImagingPlane"][0]) second_imaging_plane_metadata.update(name="second_imaging_plane_name") imaging_planes_metadata.append(second_imaging_plane_metadata) - add_imaging_plane(nwbfile=self.nwbfile, metadata=metadata, imaging_plane_name=self.imaging_plane_name) - add_imaging_plane(nwbfile=self.nwbfile, metadata=metadata, imaging_plane_name="second_imaging_plane_name") + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=metadata, imaging_plane_name=self.imaging_plane_name + ) + add_imaging_plane_to_nwbfile( + nwbfile=self.nwbfile, metadata=metadata, imaging_plane_name="second_imaging_plane_name" + ) # Test expected values imaging_planes = self.nwbfile.imaging_planes @@ -284,18 +300,6 @@ def test_add_two_imaging_planes_from_metadata(self): second_imaging_plane = imaging_planes[second_imaging_plane_name] assert second_imaging_plane.name == second_imaging_plane_name - def test_add_imaging_plane_warns_when_index_is_used(self): - """Test adding an imaging plane with the index specified warns with DeprecationWarning.""" - exc_msg = "Keyword argument 'imaging_plane_index' is deprecated and will be removed on or after Dec 1st, 2023. Use 'imaging_plane_name' to specify which imaging plane to add by its name." - with self.assertWarnsWith(warn_type=DeprecationWarning, exc_msg=exc_msg): - add_imaging_plane(nwbfile=self.nwbfile, metadata=self.metadata, imaging_plane_index=0) - # Test expected values - imaging_planes = self.nwbfile.imaging_planes - assert len(imaging_planes) == 1 - - imaging_plane = imaging_planes[self.imaging_plane_name] - assert imaging_plane.name == self.imaging_plane_name - class TestAddImageSegmentation(unittest.TestCase): def setUp(self): @@ -313,13 +317,13 @@ def setUp(self): self.metadata["Ophys"].update(image_segmentation_metadata) - def test_add_image_segmentation(self): + def test_add_image_segmentation_to_nwbfile(self): """ - Test that add_image_segmentation method adds an image segmentation to the nwbfile + Test that add_image_segmentation_to_nwbfile method adds an image segmentation to the nwbfile specified by the metadata. """ - add_image_segmentation(nwbfile=self.nwbfile, metadata=self.metadata) + add_image_segmentation_to_nwbfile(nwbfile=self.nwbfile, metadata=self.metadata) ophys = get_module(self.nwbfile, "ophys") @@ -395,10 +399,10 @@ def setUp(self): self.metadata["Ophys"].update(image_segmentation_metadata) - def test_add_plane_segmentation(self): - """Test that add_plane_segmentation method adds a plane segmentation to the nwbfile + def test_add_plane_segmentation_to_nwbfile(self): + """Test that add_plane_segmentation_to_nwbfile method adds a plane segmentation to the nwbfile specified by the metadata.""" - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -429,7 +433,7 @@ def test_add_plane_segmentation(self): def test_do_not_include_roi_centroids(self): """Test that setting `include_roi_centroids=False` prevents the centroids from being calculated and added.""" - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -445,7 +449,7 @@ def test_do_not_include_roi_centroids(self): def test_do_not_include_acceptance(self): """Test that setting `include_roi_acceptance=False` prevents the boolean acceptance columns from being added.""" - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -502,7 +506,7 @@ def test_rejected_roi_ids(self, rejected_list, expected_rejected_roi_ids): rejected_list=rejected_list, ) - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -537,7 +541,7 @@ def get_roi_pixel_masks(self, roi_ids: Optional[ArrayLike] = None) -> List[np.nd segmentation_extractor.get_roi_pixel_masks = MethodType(get_roi_pixel_masks, segmentation_extractor) - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -569,7 +573,7 @@ def get_roi_pixel_masks(self, roi_ids: Optional[ArrayLike] = None) -> List[np.nd segmentation_extractor.get_roi_pixel_masks = MethodType(get_roi_pixel_masks, segmentation_extractor) - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -594,7 +598,7 @@ def test_none_masks(self): num_columns=self.num_columns, ) - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -623,7 +627,7 @@ def test_invalid_mask_type(self): "None (to not write any masks)! Received 'invalid'." ) with pytest.raises(AssertionError, match=expected_error_message): - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -653,7 +657,7 @@ def get_roi_pixel_masks(self, roi_ids: Optional[ArrayLike] = None) -> List[np.nd "Using mask_type='pixel' instead." ), ): - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -691,7 +695,7 @@ def get_roi_pixel_masks(self, roi_ids: Optional[ArrayLike] = None) -> List[np.nd "Using mask_type='voxel' instead." ), ): - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -711,7 +715,7 @@ def test_not_overwriting_plane_segmentation_if_same_name(self): """Test that adding a plane segmentation with the same name will not overwrite the existing plane segmentation.""" - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -720,7 +724,7 @@ def test_not_overwriting_plane_segmentation_if_same_name(self): self.plane_segmentation_metadata["description"] = "modified description" - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -744,7 +748,7 @@ def test_add_two_plane_segmentation(self): first_plane_segmentation_description = "first_plane_segmentation_description" self.plane_segmentation_metadata["name"] = first_plane_segmentation_name self.plane_segmentation_metadata["description"] = first_plane_segmentation_description - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -756,7 +760,7 @@ def test_add_two_plane_segmentation(self): second_plane_segmentation_description = "second_plane_segmentation_description" self.plane_segmentation_metadata["name"] = second_plane_segmentation_name self.plane_segmentation_metadata["description"] = second_plane_segmentation_description - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -778,14 +782,14 @@ def test_add_two_plane_segmentation(self): assert second_plane_segmentation.name == second_plane_segmentation_name assert second_plane_segmentation.description == second_plane_segmentation_description - def test_add_plane_segmentation_raises_when_name_not_found_in_metadata(self): + def test_add_plane_segmentation_to_nwbfile_raises_when_name_not_found_in_metadata(self): """Test adding a plane segmentation raises an error when the name is not found in the metadata.""" plane_segmentation_name = "plane_segmentation_non_existing_in_the_metadata" with self.assertRaisesWith( exc_type=ValueError, exc_msg=f"Metadata for Plane Segmentation '{plane_segmentation_name}' not found in metadata['Ophys']['ImageSegmentation']['plane_segmentations'].", ): - add_plane_segmentation( + add_plane_segmentation_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -865,10 +869,10 @@ def setUp(self): self.metadata["Ophys"].update(fluorescence_metadata) self.metadata["Ophys"].update(dff_metadata) - def test_add_fluorescence_traces(self): + def test_add_fluorescence_traces_to_nwbfile(self): """Test fluorescence traces are added correctly to the nwbfile.""" - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -939,7 +943,7 @@ def test_add_df_over_f_trace(self): ) segmentation_extractor._roi_response_raw = None - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -986,7 +990,7 @@ def test_add_fluorescence_one_of_the_traces_is_none(self): has_neuropil_signal=False, ) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1004,7 +1008,7 @@ def test_add_fluorescence_one_of_the_traces_is_empty(self): self.segmentation_extractor._roi_response_deconvolved = np.empty((self.num_frames, 0)) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1022,7 +1026,7 @@ def test_add_fluorescence_one_of_the_traces_is_all_zeros(self): self.segmentation_extractor._roi_response_deconvolved = np.zeros((self.num_rois, self.num_frames)) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1050,7 +1054,7 @@ def test_no_traces_are_added(self): segmentation_extractor._roi_response_raw = None - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1064,7 +1068,7 @@ def test_not_overwriting_fluorescence_if_same_name(self): """Test that adding fluorescence traces container with the same name will not overwrite the existing fluorescence container in nwbfile.""" - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1072,7 +1076,7 @@ def test_not_overwriting_fluorescence_if_same_name(self): self.deconvolved_roi_response_series_metadata["description"] = "second description" - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1083,7 +1087,7 @@ def test_not_overwriting_fluorescence_if_same_name(self): self.assertNotEqual(roi_response_series["Deconvolved"].description, "second description") - def test_add_fluorescence_traces_to_existing_container(self): + def test_add_fluorescence_traces_to_nwbfile_to_existing_container(self): """Test that new traces can be added to an existing fluorescence container.""" segmentation_extractor = generate_dummy_segmentation_extractor( @@ -1097,7 +1101,7 @@ def test_add_fluorescence_traces_to_existing_container(self): has_neuropil_signal=False, ) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1117,7 +1121,7 @@ def test_add_fluorescence_traces_to_existing_container(self): num_columns=self.num_columns, ) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1131,7 +1135,7 @@ def test_add_fluorescence_traces_to_existing_container(self): # check that raw traces are not overwritten self.assertNotEqual(roi_response_series["RoiResponseSeries"].description, "second description") - def test_add_fluorescence_traces_irregular_timestamps(self): + def test_add_fluorescence_traces_to_nwbfile_irregular_timestamps(self): """Test adding traces with irregular timestamps.""" times = [0.0, 0.12, 0.15, 0.19, 0.1] @@ -1143,7 +1147,7 @@ def test_add_fluorescence_traces_irregular_timestamps(self): ) segmentation_extractor.set_times(times) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1156,7 +1160,7 @@ def test_add_fluorescence_traces_irregular_timestamps(self): self.assertEqual(roi_response_series[series_name].starting_time, None) assert_array_equal(roi_response_series[series_name].timestamps.data, times) - def test_add_fluorescence_traces_regular_timestamps(self): + def test_add_fluorescence_traces_to_nwbfile_regular_timestamps(self): """Test that adding traces with regular timestamps, the 'timestamps' are not added to the NWB file, instead 'rate' and 'starting_time' is used.""" @@ -1169,7 +1173,7 @@ def test_add_fluorescence_traces_regular_timestamps(self): ) segmentation_extractor.set_times(times) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1182,7 +1186,7 @@ def test_add_fluorescence_traces_regular_timestamps(self): self.assertEqual(roi_response_series[series_name].starting_time, times[0]) self.assertEqual(roi_response_series[series_name].timestamps, None) - def test_add_fluorescence_traces_regular_timestamps_with_metadata(self): + def test_add_fluorescence_traces_to_nwbfile_regular_timestamps_with_metadata(self): """Test adding traces with regular timestamps and also metadata-specified rate.""" times = np.arange(0, 5) segmentation_extractor = generate_dummy_segmentation_extractor( @@ -1197,7 +1201,7 @@ def test_add_fluorescence_traces_regular_timestamps_with_metadata(self): metadata["Ophys"]["Fluorescence"]["PlaneSegmentation"]["raw"].update(rate=1.23) metadata["Ophys"]["DfOverF"]["PlaneSegmentation"]["dff"].update(rate=1.23) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=metadata, @@ -1210,7 +1214,7 @@ def test_add_fluorescence_traces_regular_timestamps_with_metadata(self): self.assertEqual(roi_response_series[series_name].starting_time, 0) self.assertEqual(roi_response_series[series_name].timestamps, None) - def test_add_fluorescence_traces_irregular_timestamps_with_metadata(self): + def test_add_fluorescence_traces_to_nwbfile_irregular_timestamps_with_metadata(self): """Test adding traces with default timestamps and metadata rates (auto included in current segmentation interfaces).""" times = [0.0, 0.12, 0.15, 0.19, 0.1] segmentation_extractor = generate_dummy_segmentation_extractor( @@ -1224,7 +1228,7 @@ def test_add_fluorescence_traces_irregular_timestamps_with_metadata(self): metadata = deepcopy(self.metadata) metadata["Ophys"]["Fluorescence"]["PlaneSegmentation"]["raw"].update(rate=1.23) - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=segmentation_extractor, nwbfile=self.nwbfile, metadata=metadata, @@ -1237,9 +1241,9 @@ def test_add_fluorescence_traces_irregular_timestamps_with_metadata(self): self.assertEqual(roi_response_series[series_name].starting_time, None) assert_array_equal(roi_response_series[series_name].timestamps.data, times) - def test_add_fluorescence_traces_with_plane_segmentation_name_specified(self): + def test_add_fluorescence_traces_to_nwbfile_with_plane_segmentation_name_specified(self): plane_segmentation_name = "plane_segmentation_name" - metadata = get_default_segmentation_metadata() + metadata = _get_default_segmentation_metadata() metadata = dict_deep_update(metadata, self.metadata) metadata["Ophys"]["ImageSegmentation"]["plane_segmentations"][0].update(name=plane_segmentation_name) @@ -1248,7 +1252,7 @@ def test_add_fluorescence_traces_with_plane_segmentation_name_specified(self): ) metadata["Ophys"]["DfOverF"][plane_segmentation_name] = metadata["Ophys"]["DfOverF"].pop("PlaneSegmentation") - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor, nwbfile=self.nwbfile, metadata=metadata, @@ -1273,7 +1277,7 @@ def setUpClass(cls): cls.session_start_time = datetime.now().astimezone() - cls.metadata = get_default_segmentation_metadata() + cls.metadata = _get_default_segmentation_metadata() cls.plane_segmentation_first_plane_name = "PlaneSegmentationFirstPlane" cls.metadata["Ophys"]["ImageSegmentation"]["plane_segmentations"][0].update( @@ -1342,8 +1346,8 @@ def setUp(self): session_start_time=self.session_start_time, ) - def test_add_fluorescence_traces_for_two_plane_segmentations(self): - add_fluorescence_traces( + def test_add_fluorescence_traces_to_nwbfile_for_two_plane_segmentations(self): + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor_first_plane, nwbfile=self.nwbfile, metadata=self.metadata, @@ -1378,7 +1382,7 @@ def test_add_fluorescence_traces_for_two_plane_segmentations(self): metadata["Ophys"]["Fluorescence"][second_plane_segmentation_name]["neuropil"].update(name="NeuropilSecondPlane") metadata["Ophys"]["DfOverF"][second_plane_segmentation_name]["dff"].update(name="RoiResponseSeriesSecondPlane") - add_fluorescence_traces( + add_fluorescence_traces_to_nwbfile( segmentation_extractor=self.segmentation_extractor_second_plane, nwbfile=self.nwbfile, metadata=metadata, @@ -1475,7 +1479,7 @@ def setUp(self): def test_default_values(self): """Test adding two photon series with default values.""" - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata ) @@ -1508,7 +1512,7 @@ def test_invalid_iterator_type_raises_error(self): AssertionError, "'iterator_type' must be either 'v1', 'v2' (recommended), or None.", ): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1539,7 +1543,7 @@ def test_non_iterative_write_assertion(self): def test_non_iterative_two_photon(self): """Test adding two photon series with using DataChunkIterator as iterator type.""" - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1559,7 +1563,7 @@ def test_non_iterative_two_photon(self): def test_v1_iterator(self): """Test adding two photon series with using DataChunkIterator as iterator type.""" - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1584,7 +1588,7 @@ def test_iterator_options_propagation(self): """Test that iterator options are propagated to the data chunk iterator.""" buffer_shape = (20, 5, 5) chunk_shape = (10, 5, 5) - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1601,7 +1605,7 @@ def test_iterator_options_propagation(self): def test_iterator_options_chunk_mb_propagation(self): """Test that chunk_mb is propagated to the data chunk iterator and the chunk shape is correctly set to fit.""" chunk_mb = 10.0 - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1618,7 +1622,7 @@ def test_iterator_options_chunk_mb_propagation(self): def test_iterator_options_chunk_shape_is_at_least_one(self): """Test that when a small chunk_mb is selected the chunk shape is guaranteed to include at least one frame.""" chunk_mb = 1.0 - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1634,7 +1638,7 @@ def test_iterator_options_chunk_shape_is_at_least_one(self): def test_iterator_options_chunk_shape_does_not_exceed_maxshape(self): """Test that when a large chunk_mb is selected the chunk shape is guaranteed to not exceed maxshape.""" chunk_mb = 1000.0 - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1648,7 +1652,7 @@ def test_iterator_options_chunk_shape_does_not_exceed_maxshape(self): assert_array_equal(chunk_shape, data_chunk_iterator.maxshape) def test_add_two_photon_series_roundtrip(self): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata ) @@ -1685,14 +1689,14 @@ def test_add_invalid_photon_series_type(self): AssertionError, "'photon_series_type' must be either 'OnePhotonSeries' or 'TwoPhotonSeries'.", ): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, photon_series_type="invalid", ) - def test_add_photon_series_inconclusive_metadata(self): + def test_add_photon_series_to_nwbfile_inconclusive_metadata(self): """Test warning is raised when `photon_series_type` specifies 'TwoPhotonSeries' but metadata contains also 'OnePhotonSeries'.""" exc_msg = "Received metadata for both 'OnePhotonSeries' and 'TwoPhotonSeries', make sure photon_series_type is specified correctly." @@ -1702,7 +1706,7 @@ def test_add_photon_series_inconclusive_metadata(self): ) with self.assertWarnsWith(warn_type=UserWarning, exc_msg=exc_msg): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=photon_series_metadata, @@ -1719,7 +1723,7 @@ def test_add_one_photon_series(self): binning=2, power=500.0, ) - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=metadata, @@ -1734,7 +1738,7 @@ def test_add_one_photon_series(self): self.assertEqual(one_photon_series.unit, "n.a.") def test_add_one_photon_series_roundtrip(self): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.one_photon_series_metadata, @@ -1758,13 +1762,13 @@ def test_add_one_photon_series_roundtrip(self): expected_one_photon_series_shape = (self.num_frames, self.num_columns, self.num_rows) assert one_photon_series.shape == expected_one_photon_series_shape - def test_add_photon_series_invalid_module_name_raises(self): + def test_add_photon_series_to_nwbfile_invalid_module_name_raises(self): """Test that adding photon series with invalid module name raises error.""" with self.assertRaisesWith( exc_type=AssertionError, exc_msg="'parent_container' must be either 'acquisition' or 'processing/ophys'.", ): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1776,7 +1780,7 @@ def test_add_one_photon_series_to_processing(self): metadata = self.one_photon_series_metadata metadata["Ophys"]["OnePhotonSeries"][0].update(name="OnePhotonSeriesProcessed") - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.one_photon_series_metadata, @@ -1793,12 +1797,12 @@ def test_photon_series_not_added_to_acquisition_with_same_name(self): with self.assertRaisesWith( exc_type=ValueError, exc_msg=f"{self.two_photon_series_name} already added to nwbfile.acquisition." ): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, ) - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1812,13 +1816,13 @@ def test_photon_series_not_added_to_processing_with_same_name(self): exc_type=ValueError, exc_msg=f"{self.two_photon_series_name} already added to nwbfile.processing['ophys'].", ): - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, parent_container="processing/ophys", ) - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1828,7 +1832,7 @@ def test_photon_series_not_added_to_processing_with_same_name(self): def test_ophys_module_not_created_when_photon_series_added_to_acquisition(self): """Test that ophys module is not created when photon series are added to nwbfile.acquisition.""" - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=self.two_photon_series_metadata, @@ -1844,7 +1848,7 @@ def test_add_multiple_one_photon_series_with_same_imaging_plane(self): shared_photon_series_metadata["Ophys"]["ImagingPlane"][0]["name"] = shared_imaging_plane_name shared_photon_series_metadata["Ophys"]["OnePhotonSeries"][0]["imaging_plane"] = shared_imaging_plane_name - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=shared_photon_series_metadata, @@ -1852,7 +1856,7 @@ def test_add_multiple_one_photon_series_with_same_imaging_plane(self): ) shared_photon_series_metadata["Ophys"]["OnePhotonSeries"][0]["name"] = "second_photon_series" - add_photon_series( + add_photon_series_to_nwbfile( imaging=self.imaging_extractor, nwbfile=self.nwbfile, metadata=shared_photon_series_metadata, @@ -1892,10 +1896,10 @@ def setUp(self): session_start_time=self.session_start_time, ) - def test_add_summary_images(self): + def test_add_summary_images_to_nwbfile(self): segmentation_extractor = generate_dummy_segmentation_extractor(num_rows=10, num_columns=15) - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor, metadata=self.metadata, @@ -1923,7 +1927,7 @@ def test_extractor_with_one_summary_image_suppressed(self): segmentation_extractor = generate_dummy_segmentation_extractor(num_rows=10, num_columns=15) segmentation_extractor._image_correlation = None - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor, metadata=self.metadata, @@ -1945,7 +1949,7 @@ def test_extractor_with_no_summary_images(self): self.nwbfile.create_processing_module("ophys", "contains optical physiology processed data") - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor, metadata=self.metadata, @@ -1959,26 +1963,28 @@ def test_extractor_with_no_summary_images_and_no_ophys_module(self): num_rows=10, num_columns=15, has_summary_images=False ) - add_summary_images(nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor, metadata=self.metadata) + add_summary_images_to_nwbfile( + nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor, metadata=self.metadata + ) assert len(self.nwbfile.processing) == 0 - def test_add_summary_images_invalid_plane_segmentation_name(self): + def test_add_summary_images_to_nwbfile_invalid_plane_segmentation_name(self): with self.assertRaisesWith( exc_type=AssertionError, exc_msg="Plane segmentation 'invalid_plane_segmentation_name' not found in metadata['Ophys']['SegmentationImages']", ): - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=self.nwbfile, segmentation_extractor=generate_dummy_segmentation_extractor(num_rows=10, num_columns=15), metadata=self.metadata, plane_segmentation_name="invalid_plane_segmentation_name", ) - def test_add_summary_images_from_two_planes(self): + def test_add_summary_images_to_nwbfile_from_two_planes(self): segmentation_extractor_first_plane = generate_dummy_segmentation_extractor(num_rows=10, num_columns=15) - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor_first_plane, metadata=self.metadata, @@ -1997,7 +2003,7 @@ def test_add_summary_images_from_two_planes(self): segmentation_extractor_second_plane = generate_dummy_segmentation_extractor(num_rows=10, num_columns=15) - add_summary_images( + add_summary_images_to_nwbfile( nwbfile=self.nwbfile, segmentation_extractor=segmentation_extractor_second_plane, metadata=metadata,