From 97bac4cf281f4e809bce2ce301a73fe9128dd8fc Mon Sep 17 00:00:00 2001 From: Don Yin Date: Fri, 27 Nov 2026 13:24:10 +0000 Subject: [PATCH] Fix NWB export with current PyNWB --- caiman/source_extraction/cnmf/estimates.py | 4 ++-- caiman/tests/test_hdf5.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/caiman/source_extraction/cnmf/estimates.py b/caiman/source_extraction/cnmf/estimates.py index cf296071c..38b233661 100644 --- a/caiman/source_extraction/cnmf/estimates.py +++ b/caiman/source_extraction/cnmf/estimates.py @@ -1602,7 +1602,7 @@ def save_NWB(self, # Add processing results # Create the module as 'ophys' unless it is taken and append 'ophysX' instead - ophysmodules = [s[5:] for s in list(nwbfile.modules) if s.startswith('ophys')] + ophysmodules = [s[5:] for s in nwbfile.processing if s.startswith('ophys')] if any('' in s for s in ophysmodules): if any([s for s in ophysmodules if s.isdigit()]): nummodules = max([int(s) for s in ophysmodules if s.isdigit()])+1 @@ -1619,7 +1619,7 @@ def save_NWB(self, img_seg = ImageSegmentation() mod.add(img_seg) fl = Fluorescence() - mod.add_data_interface(fl) + mod.add(fl) # mot_crct = MotionCorrection() # mod.add_data_interface(mot_crct) diff --git a/caiman/tests/test_hdf5.py b/caiman/tests/test_hdf5.py index ab228d463..91fbe96fd 100644 --- a/caiman/tests/test_hdf5.py +++ b/caiman/tests/test_hdf5.py @@ -1,8 +1,12 @@ #!/usr/bin/env python +from datetime import datetime, timezone import numpy as np import numpy.testing as npt import os +from pynwb import NWBHDF5IO +from scipy.sparse import csc_matrix +from caiman.source_extraction.cnmf.estimates import Estimates from caiman.utils import utils from caiman.paths import get_tempdir @@ -47,5 +51,20 @@ def test_save_and_load_dict_to_hdf5(): _recursively_assert_array_equal(dict_to_save, loaded) +def test_save_nwb_estimates(tmp_path): + traces = np.array([[1., 3., 2.]]) + estimates = Estimates(A=csc_matrix([[1.], [0.], [0.], [0.]]), b=np.ones((4, 1)), + C=traces, f=np.ones_like(traces), dims=(2, 2)) + estimates.YrA = np.zeros_like(traces) + estimates.r_values = np.array([0.9]) + estimates.SNR_comp = np.array([3.]) + filename = tmp_path / 'estimates.nwb' + estimates.save_NWB(filename, session_start_time=datetime.now(timezone.utc)) + + with NWBHDF5IO(filename, 'r') as io: + fluorescence = io.read().processing['ophys']['Fluorescence'] + npt.assert_array_equal(fluorescence.roi_response_series['RoiResponseSeries'].data[:], traces.T) + + if __name__ == '__main__': test_save_and_load_dict_to_hdf5() \ No newline at end of file