Beispiel #1
0
    def setUpContainer(self):
        self.plane_segmentation = TestPlaneSegmentation.buildPlaneSegmentation(self)
        self.rt_region = self.plane_segmentation.create_roi_table_region('the first of two ROIs', region=[0])

        data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        timestamps = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

        return RoiResponseSeries('test_roi_response_series', data, 'lumens', self.rt_region, timestamps=timestamps)
Beispiel #2
0
    def test_init(self):
        ip = CreatePlaneSegmentation()

        rt_region = ip.create_roi_table_region('the second ROI', region=[0])

        ts = RoiResponseSeries('test_ts', list(), rt_region, unit='unit', timestamps=list())
        self.assertEqual(ts.name, 'test_ts')
        self.assertEqual(ts.unit, 'unit')
        self.assertEqual(ts.rois, rt_region)
Beispiel #3
0
    def test_init(self):
        ip = CreatePlaneSegmentation()

        rt_region = ip.create_roi_table_region('the second ROI', region=[1])

        rrs = RoiResponseSeries('test_ts', list(), rt_region, unit='unit', timestamps=list())

        dof = DfOverF(rrs)
        self.assertEqual(dof.roi_response_series['test_ts'], rrs)
Beispiel #4
0
    def test_init(self):
        ip = CreatePlaneSegmentation()

        rt_region = ip.create_roi_table_region('the second ROI', region=[1])

        ts = RoiResponseSeries('test_ts', list(), rt_region, unit='unit', timestamps=list())

        ff = Fluorescence(ts)
        self.assertEqual(ff.roi_response_series['test_ts'], ts)
        self.assertEqual(ff.roi_response_series['test_ts'], ts)
Beispiel #5
0
    def test_init(self):
        ps = create_plane_segmentation()
        rt_region = ps.create_roi_table_region(description='the second ROI',
                                               region=[1])

        rrs = RoiResponseSeries(name='test_ts',
                                data=list(),
                                rois=rt_region,
                                unit='unit',
                                timestamps=list())

        dof = DfOverF(rrs)
        self.assertEqual(dof.roi_response_series['test_ts'], rrs)
Beispiel #6
0
    def test_init(self):
        ps = create_plane_segmentation()
        rt_region = ps.create_roi_table_region(description='the second ROI',
                                               region=[0])

        ts = RoiResponseSeries(name='test_ts',
                               data=[1, 2, 3],
                               rois=rt_region,
                               unit='unit',
                               timestamps=[0.1, 0.2, 0.3])
        self.assertEqual(ts.name, 'test_ts')
        self.assertEqual(ts.unit, 'unit')
        self.assertEqual(ts.rois, rt_region)
Beispiel #7
0
    def test_init(self):
        ps = create_plane_segmentation()
        rt_region = ps.create_roi_table_region(description='the second ROI',
                                               region=[1])

        ts = RoiResponseSeries(name='test_ts',
                               data=[1, 2, 3],
                               rois=rt_region,
                               unit='unit',
                               timestamps=[0.1, 0.2, 0.3])

        ff = Fluorescence(ts)
        self.assertEqual(ff.roi_response_series['test_ts'], ts)
Beispiel #8
0
    def setUpContainer(self):
        self.plane_segmentation = TestPlaneSegmentation.buildPlaneSegmentation(
            self)
        self.rt_region = self.plane_segmentation.create_roi_table_region(
            'the first of two ROIs', region=[0])

        data = [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]
        timestamps = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

        return RoiResponseSeries('test_roi_response_series',
                                 data,
                                 self.rt_region,
                                 unit='lumens',
                                 timestamps=timestamps)
Beispiel #9
0
    def test_init(self):
        ip = CreatePlaneSegmentation()
        iS = ImageSegmentation('test source', ip, name='test_iS')

        rrs = RoiResponseSeries('test_ts',
                                'a hypothetical source',
                                list(),
                                'unit', ['name1'],
                                iS,
                                timestamps=list())

        dof = DfOverF('test_dof', rrs)
        self.assertEqual(dof.source, 'test_dof')
        self.assertEqual(dof.roi_response_series, rrs)
Beispiel #10
0
    def test_init(self):
        ip = CreatePlaneSegmentation()
        iS = ImageSegmentation('test source', ip, name='test_iS')

        ts = RoiResponseSeries('test_ts',
                               'a hypothetical source',
                               list(),
                               'unit', ['name1'],
                               iS,
                               timestamps=list())

        ff = Fluorescence('test_ff', ts)
        self.assertEqual(ff.source, 'test_ff')
        self.assertEqual(ff.roi_response_series, ts)
Beispiel #11
0
    def cicada_add_roi_response_series(self,
                                       module,
                                       traces_data=None,
                                       rois=None):
        """  class pynwb.ophys.RoiResponseSeries(name, data, unit, rois,
             resolution=0.0, conversion=1.0, timestamps=None, starting_time=None, rate=None, comments='no comments',
             description='no description', control=None, control_description=None, parent=None)
        """

        required_metadata = [
            "roi_response_series_name", "roi_response_series_unit"
        ]
        metadata_to_add = list(
            set(required_metadata) -
            set(list(self.data['ophys_metadata'].keys())))
        for i in metadata_to_add:
            self.data["ophys_metadata"] = self.add_required_metadata(
                self.data["ophys_metadata"], i, "ophys")

        # Nom du module où récupérer les infos de métadonnée
        name_module = "roi_response_series_"

        roi_response_series = RoiResponseSeries(
            name=self.data['ophys_metadata'].get(name_module + "name"),
            data=traces_data,
            unit=self.data['ophys_metadata'].get(name_module + "unit"),
            rois=self.table_region,
            resolution=0.0,
            conversion=1.0,
            timestamps=self.data['ophys_metadata'].get(name_module +
                                                       "timestamp"),
            starting_time=self.data['ophys_metadata'].get(name_module +
                                                          "starting_time"),
            rate=1.0,
            comments="no comments",
            description="no description",
            control=self.data['ophys_metadata'].get(name_module + "control"),
            control_description=self.data['ophys_metadata'].get(
                name_module + "control_description"),
            parent=self.data['ophys_metadata'].get(name_module + "parent"))

        if module == "DfOverF":
            self.DfOverF.add_roi_response_series(roi_response_series)
        elif module == "fluorescence":
            self.fluorescence.add_roi_response_series(roi_response_series)
        else:
            print(
                f"erreur : le nom du module doit être 'DfOverF' ou 'fluorescence', et non {module} !"
            )
Beispiel #12
0
    def test_init(self):
        ip = CreatePlaneSegmentation()

        rt_region = ip.create_roi_table_region([1], 'the second ROI')

        rrs = RoiResponseSeries('test_ts',
                                'a hypothetical source',
                                list(),
                                'unit',
                                rt_region,
                                timestamps=list())

        dof = DfOverF('test_dof', rrs)
        self.assertEqual(dof.source, 'test_dof')
        self.assertEqual(dof.roi_response_series['test_ts'], rrs)
Beispiel #13
0
    def test_init(self):
        ip = CreatePlaneSegmentation()
        iS = ImageSegmentation('test source', ip, name='test_iS')

        ts = RoiResponseSeries('test_ts',
                               'a hypothetical source',
                               list(),
                               'unit', ['name1'],
                               iS,
                               timestamps=list())
        self.assertEqual(ts.name, 'test_ts')
        self.assertEqual(ts.source, 'a hypothetical source')
        self.assertEqual(ts.unit, 'unit')
        self.assertEqual(ts.roi_names, ['name1'])
        self.assertEqual(ts.segmentation_interface, iS)
Beispiel #14
0
    def test_init(self):
        ip = CreatePlaneSegmentation()

        rt_region = ip.create_roi_table_region([1], 'the second ROI')

        ts = RoiResponseSeries('test_ts',
                               'a hypothetical source',
                               list(),
                               'unit',
                               rt_region,
                               timestamps=list())
        self.assertEqual(ts.name, 'test_ts')
        self.assertEqual(ts.source, 'a hypothetical source')
        self.assertEqual(ts.unit, 'unit')
        self.assertEqual(ts.rois, rt_region)
Beispiel #15
0
    def test_init(self):
        ip = CreatePlaneSegmentation()

        rt_region = ip.create_roi_table_region([1], 'the second ROI')

        ts = RoiResponseSeries('test_ts',
                               'a hypothetical source',
                               list(),
                               'unit',
                               rt_region,
                               timestamps=list())

        ff = Fluorescence('test_ff', ts)
        self.assertEqual(ff.source, 'test_ff')
        self.assertEqual(ff.roi_response_series['test_ts'], ts)
        self.assertEqual(ff.roi_response_series['test_ts'], ts)
Beispiel #16
0
def add_ophys_processing_from_suite2p(save_folder, nwbfile, xml, 
                                      device=None,
                                      optical_channel=None,
                                      imaging_plane=None,
                                      image_series=None):
    """ 
    adapted from suite2p/suite2p/io/nwb.py "save_nwb" function
    """

    plane_folders = natsorted([ f.path for f in os.scandir(save_folder) if f.is_dir() and f.name[:5]=='plane'])
    OPS = [np.load(os.path.join(f, 'ops.npy'), allow_pickle=True).item() for f in plane_folders]

    if len(OPS)>1:
        multiplane, nplanes = True, len(plane_folders)
        pData_folder = os.path.join(save_folder, 'combined') # processed data folder -> using the "combined output from suite2p"
    else:
        multiplane, nplanes = False, 1
        pData_folder = os.path.join(save_folder, 'plane0') # processed data folder

    # find time sampling per plane
    functional_chan = ('Ch1' if len(xml['Ch1']['relativeTime'])>1 else 'Ch2') # functional channel is one of the two !!
    CaImaging_timestamps = xml[functional_chan]['relativeTime']+float(xml['settings']['framePeriod'])/2.

    ops = np.load(os.path.join(pData_folder, 'ops.npy'), allow_pickle=True).item() 
    
    if device is None:
        device = nwbfile.create_device(
            name='Microscope', 
            description='My two-photon microscope',
            manufacturer='The best microscope manufacturer')
    if optical_channel is None:
        optical_channel = OpticalChannel(
            name='OpticalChannel', 
            description='an optical channel', 
            emission_lambda=500.)
    if imaging_plane is None:
        imaging_plane = nwbfile.create_imaging_plane(
            name='ImagingPlane',
            optical_channel=optical_channel,
            imaging_rate=ops['fs'],
            description='standard',
            device=device,
            excitation_lambda=600.,
            indicator='GCaMP',
            location='V1',
            grid_spacing=([2,2,30] if multiplane else [2,2]),
            grid_spacing_unit='microns')

    if image_series is None:
        # link to external data
        image_series = TwoPhotonSeries(
            name='TwoPhotonSeries', 
            dimension=[ops['Ly'], ops['Lx']],
            external_file=(ops['filelist'] if 'filelist' in ops else ['']), 
            imaging_plane=imaging_plane,
            starting_frame=[0], 
            format='external', 
            starting_time=0.0, 
            rate=ops['fs'] * ops['nplanes']
        )
        nwbfile.add_acquisition(image_series) # otherwise, were added

    # processing
    img_seg = ImageSegmentation()
    ps = img_seg.create_plane_segmentation(
        name='PlaneSegmentation',
        description='suite2p output',
        imaging_plane=imaging_plane,
        reference_images=image_series
    )
    ophys_module = nwbfile.create_processing_module(
        name='ophys', 
        description='optical physiology processed data\n TSeries-folder=%s' % save_folder)
    ophys_module.add(img_seg)

    file_strs = ['F.npy', 'Fneu.npy', 'spks.npy']
    traces = []

    iscell = np.load(os.path.join(pData_folder, 'iscell.npy')).astype(bool)

    if ops['nchannels']>1:
        if os.path.isfile(os.path.join(pData_folder, 'redcell_manual.npy')):
            redcell = np.load(os.path.join(pData_folder, 'redcell_manual.npy'))[iscell[:,0], :]
        else:
            print('\n'+30*'--')
            print(' /!\ no file found for the manual labelling of red cells (generate it with the red-cell labelling GUI) /!\ ')
            print(' /!\ taking the raw suit2p output with the classifier settings /!\ ')
            print('\n'+30*'--')
            redcell = np.load(os.path.join(pData_folder, 'redcell.npy'))[iscell[:,0], :]
            
    for fstr in file_strs:
        traces.append(np.load(os.path.join(pData_folder, fstr))[iscell[:,0], :])
        
    stat = np.load(os.path.join(pData_folder, 'stat.npy'), allow_pickle=True)

    ncells = np.sum(iscell[:,0])
    plane_ID = np.zeros(ncells)
    for n in np.arange(ncells):
        pixel_mask = np.array([stat[iscell[:,0]][n]['ypix'], stat[iscell[:,0]][n]['xpix'], 
                               stat[iscell[:,0]][n]['lam']])
        ps.add_roi(pixel_mask=pixel_mask.T)
        if 'iplane' in stat[0]:
            plane_ID[n] = stat[iscell[:,0]][n]['iplane']

    if ops['nchannels']>1:
        ps.add_column('redcell', 'two columns - redcell & probcell', redcell)
    ps.add_column('plane', 'one column - plane ID', plane_ID)

    rt_region = ps.create_roi_table_region(
        region=list(np.arange(0, ncells)),
        description='all ROIs')

    # FLUORESCENCE (all are required)
    file_strs = ['F.npy', 'Fneu.npy', 'spks.npy']
    name_strs = ['Fluorescence', 'Neuropil', 'Deconvolved']

    for i, (fstr,nstr) in enumerate(zip(file_strs, name_strs)):
        roi_resp_series = RoiResponseSeries(
            name=nstr,
            data=traces[i],
            rois=rt_region,
            unit='lumens',
            timestamps=CaImaging_timestamps[::nplanes]) # ideally should be shifted for each ROI depending on the plane...
        fl = Fluorescence(roi_response_series=roi_resp_series, name=nstr)
        ophys_module.add(fl)

    # BACKGROUNDS
    # (meanImg, Vcorr and max_proj are REQUIRED)
    bg_strs = ['meanImg', 'meanImgE', 'Vcorr', 'max_proj', 'meanImg_chan2']
    nplanes = ops['nplanes']
    for iplane in range(nplanes):
        images = Images('Backgrounds_%d'%iplane)
        for bstr in bg_strs:
            if bstr in ops:
                if bstr=='Vcorr' or bstr=='max_proj':
                    img = np.zeros((ops['Ly'], ops['Lx']), np.float32)
                    img[ops['yrange'][0]:ops['yrange'][-1], 
                        ops['xrange'][0]:ops['xrange'][-1]] = ops[bstr]
                else:
                    img = ops[bstr]
                images.add_image(GrayscaleImage(name=bstr, data=img))

        ophys_module.add(images)
    def test_roundtrip(self):
        multi_commanded_voltage = MultiCommandedVoltage()

        commandedvoltage_series = (
            multi_commanded_voltage.create_commanded_voltage_series(
                name="commanded_voltage",
                data=[1.0, 2.0, 3.0],
                frequency=30.0,
                power=500.0,
                rate=30.0,
                unit='volts'))

        cmmandedvoltage_series2 = (
            multi_commanded_voltage.create_commanded_voltage_series(
                name="commanded_voltage2",
                data=[1.0, 2.0, 3.0],
                frequency=30.0,
                power=500.0,
                rate=30.0,
            ))

        excitationsources_table = ExcitationSourcesTable(
            description="excitation sources table")

        excitationsources_table.add_row(
            peak_wavelength=700.0,
            source_type="laser",
            commanded_voltage=commandedvoltage_series,
        )

        photodetectors_table = PhotodetectorsTable(
            description="photodetectors table")
        photodetectors_table.add_row(peak_wavelength=500.0,
                                     type="PMT",
                                     gain=100.0)

        fluorophores_table = FluorophoresTable(description='fluorophores')
        fluorophores_table.add_row(label='dlight',
                                   location='VTA',
                                   coordinates=(3.0, 2.0, 1.0))

        fibers_table = FibersTable(description="fibers table")

        fibers_ref = DynamicTableRegion(name="rois",
                                        data=[0],
                                        description="source fibers",
                                        table=fibers_table)

        roi_response_series = RoiResponseSeries(
            name="roi_response_series",
            description="my roi response series",
            data=np.random.randn(100, 1),
            unit='F',
            rate=30.0,
            rois=fibers_ref,
        )

        deconv_roi_response_series = DeconvolvedRoiResponseSeries(
            name="DeconvolvedRoiResponseSeries",
            description="my roi response series",
            data=np.random.randn(100, 1),
            unit='F',
            rate=30.0,
            rois=fibers_ref,
            raw=roi_response_series,
        )

        ophys_module = self.nwbfile.create_processing_module(
            name="ophys", description="fiber photometry")

        self.nwbfile.add_lab_meta_data(
            FiberPhotometry(fibers=fibers_table,
                            excitation_sources=excitationsources_table,
                            photodetectors=photodetectors_table,
                            fluorophores=fluorophores_table,
                            commanded_voltages=multi_commanded_voltage))

        fibers_table.add_fiber(excitation_source=0,
                               photodetector=0,
                               fluorophores=[0],
                               location='my location',
                               notes='notes')

        self.nwbfile.add_acquisition(roi_response_series)
        ophys_module.add(deconv_roi_response_series)

        with NWBHDF5IO(self.path, mode="w") as io:
            io.write(self.nwbfile)

        with NWBHDF5IO(self.path, mode="r", load_namespaces=True) as io:
            read_nwbfile = io.read()
            self.assertContainerEqual(ophys_module,
                                      read_nwbfile.processing["ophys"])
            self.assertContainerEqual(
                self.nwbfile.lab_meta_data['fiber_photometry'],
                read_nwbfile.lab_meta_data['fiber_photometry'])
            self.assertContainerEqual(
                roi_response_series,
                read_nwbfile.acquisition["roi_response_series"])
Beispiel #18
0
def add_ophys_processing_from_suite2p(save_folder,
                                      nwbfile,
                                      CaImaging_timestamps,
                                      device=None,
                                      optical_channel=None,
                                      imaging_plane=None,
                                      image_series=None):
    """ 
    adapted from suite2p/suite2p/io/nwb.py "save_nwb" function
    """

    plane_folders = natsorted([
        f.path for f in os.scandir(save_folder)
        if f.is_dir() and f.name[:5] == 'plane'
    ])
    ops1 = [
        np.load(os.path.join(f, 'ops.npy'), allow_pickle=True).item()
        for f in plane_folders
    ]
    if len(ops1) > 1:
        multiplane = True
    else:
        multiplane = False

    ops = ops1[0]

    if device is None:
        device = nwbfile.create_device(
            name='Microscope',
            description='My two-photon microscope',
            manufacturer='The best microscope manufacturer')
    if optical_channel is None:
        optical_channel = OpticalChannel(name='OpticalChannel',
                                         description='an optical channel',
                                         emission_lambda=500.)
    if imaging_plane is None:
        imaging_plane = nwbfile.create_imaging_plane(
            name='ImagingPlane',
            optical_channel=optical_channel,
            imaging_rate=ops['fs'],
            description='standard',
            device=device,
            excitation_lambda=600.,
            indicator='GCaMP',
            location='V1',
            grid_spacing=([2, 2, 30] if multiplane else [2, 2]),
            grid_spacing_unit='microns')

    if image_series is None:
        # link to external data
        image_series = TwoPhotonSeries(
            name='TwoPhotonSeries',
            dimension=[ops['Ly'], ops['Lx']],
            external_file=(ops['filelist'] if 'filelist' in ops else ['']),
            imaging_plane=imaging_plane,
            starting_frame=[0],
            format='external',
            starting_time=0.0,
            rate=ops['fs'] * ops['nplanes'])
        nwbfile.add_acquisition(image_series)  # otherwise, were added

    # processing
    img_seg = ImageSegmentation()
    ps = img_seg.create_plane_segmentation(name='PlaneSegmentation',
                                           description='suite2p output',
                                           imaging_plane=imaging_plane,
                                           reference_images=image_series)
    ophys_module = nwbfile.create_processing_module(
        name='ophys', description='optical physiology processed data')
    ophys_module.add(img_seg)

    file_strs = ['F.npy', 'Fneu.npy', 'spks.npy']
    traces = []
    ncells_all = 0
    for iplane, ops in enumerate(ops1):
        if iplane == 0:
            iscell = np.load(
                os.path.join(save_folder, 'plane%i' % iplane, 'iscell.npy'))
            for fstr in file_strs:
                traces.append(
                    np.load(os.path.join(save_folder, 'plane%i' % iplane,
                                         fstr)))
        else:
            iscell = np.append(iscell,
                               np.load(
                                   os.path.join(save_folder,
                                                'plane%i' % iplane,
                                                'iscell.npy')),
                               axis=0)
            for i, fstr in enumerate(file_strs):
                traces[i] = np.append(
                    traces[i],
                    np.load(os.path.join(save_folder, 'plane%i' % iplane,
                                         fstr)),
                    axis=0)

        stat = np.load(os.path.join(save_folder, 'plane%i' % iplane,
                                    'stat.npy'),
                       allow_pickle=True)
        ncells = len(stat)
        for n in range(ncells):
            if multiplane:
                pixel_mask = np.array([
                    stat[n]['ypix'], stat[n]['xpix'],
                    iplane * np.ones(stat[n]['npix']), stat[n]['lam']
                ])
                ps.add_roi(voxel_mask=pixel_mask.T)
            else:
                pixel_mask = np.array(
                    [stat[n]['ypix'], stat[n]['xpix'], stat[n]['lam']])
                ps.add_roi(pixel_mask=pixel_mask.T)
        ncells_all += ncells

    ps.add_column('iscell', 'two columns - iscell & probcell', iscell)

    rt_region = ps.create_roi_table_region(region=list(np.arange(
        0, ncells_all)),
                                           description='all ROIs')

    # FLUORESCENCE (all are required)
    file_strs = ['F.npy', 'Fneu.npy', 'spks.npy']
    name_strs = ['Fluorescence', 'Neuropil', 'Deconvolved']

    for i, (fstr, nstr) in enumerate(zip(file_strs, name_strs)):
        roi_resp_series = RoiResponseSeries(
            name=nstr,
            data=traces[i],
            rois=rt_region,
            unit='lumens',
            timestamps=CaImaging_timestamps
        )  # CRITICAL TO HAVE IT HERE FOR RE-ALIGNEMENT
        fl = Fluorescence(roi_response_series=roi_resp_series, name=nstr)
        ophys_module.add(fl)

    # BACKGROUNDS
    # (meanImg, Vcorr and max_proj are REQUIRED)
    bg_strs = ['meanImg', 'meanImgE', 'Vcorr', 'max_proj', 'meanImg_chan2']
    nplanes = ops['nplanes']
    for iplane in range(nplanes):
        images = Images('Backgrounds_%d' % iplane)
        for bstr in bg_strs:
            if bstr in ops:
                if bstr == 'Vcorr' or bstr == 'max_proj':
                    img = np.zeros((ops['Ly'], ops['Lx']), np.float32)
                    img[ops['yrange'][0]:ops['yrange'][-1],
                        ops['xrange'][0]:ops['xrange'][-1]] = ops[bstr]
                else:
                    img = ops[bstr]
                images.add_image(GrayscaleImage(name=bstr, data=img))

        ophys_module.add(images)
Beispiel #19
0
mod = nwbfile.create_processing_module('rois', 'example data module')
img_seg = ImageSegmentation()
ps = img_seg.create_plane_segmentation('Ca2+ imaging example', imaging_plane,
                                       'my_planeseg', image_series)
mod.add_data_interface(img_seg)

for i, img_mask in enumerate(zip(mat_data['cellImages'])):
    pixel_mask = np.array(np.where(img_mask)).T
    ps.add_roi(str(i), pixel_mask, img_mask)

region = ps.create_roi_table_region('all',
                                    region=list(
                                        range(len(mat_data['cellImages']))))

roi_response = RoiResponseSeries('RoiResponseSeries',
                                 mat_data['cellTraces'],
                                 'lumens?',
                                 region,
                                 rate=5.0,
                                 starting_time=0.0)
fl = Fluorescence(roi_response_series=roi_response)
mod.add_data_interface(fl)

fname_out = 'm655_D11_S1.nwb'
with NWBHDF5IO(fname_out, 'w') as io:
    io.write(nwbfile)

with NWBHDF5IO(fname_out, 'r') as io:
    io.read()
Beispiel #20
0
    notes='notes'
)

# Here we set up a list of fibers that our recording came from
fibers_ref = DynamicTableRegion(
    name="rois", 
    data=[0], # potentially multiple fibers
    description="source fibers", 
    table=fibers_table
)

# Create a raw roiresponseseries, this is your main acquisition
roi_response_series = RoiResponseSeries(
    name="raw_fluorescence_trace",
    description="my roi response series",
    data=np.random.randn(100, 1),
    unit='F',
    rate=30.0,
    rois=fibers_ref,
)

# This is your processed data
deconv_roi_response_series = DeconvolvedRoiResponseSeries(
    name="deconvolved_fluorescence_trace",
    description="my roi response series",
    data=np.random.randn(100, 1),
    unit='F',
    rate=30.0,
    rois=fibers_ref,
    raw=roi_response_series,
)
Beispiel #21
0
# such as a :py:class:`~pynwb.ophys.PlaneSegmentation` table by row indices.
#
#
# First, we create a :py:class:`~pynwb.core.DynamicTableRegion` that references
# the first two ROIs of the :py:class:`~pynwb.ophys.PlaneSegmentation` table.

rt_region = ps.create_roi_table_region(region=[0, 1],
                                       description='the first of two ROIs')

####################
# Then we create a :py:class:`~pynwb.ophys.RoiResponseSeries` object to store fluorescence
# data for those two ROIs.

roi_resp_series = RoiResponseSeries(
    name='RoiResponseSeries',
    data=np.ones((50, 2)),  # 50 samples, 2 ROIs
    rois=rt_region,
    unit='lumens',
    rate=30.)

####################
# To help data analysis and visualization tools know that this
# :py:class:`~pynwb.ophys.RoiResponseSeries` object represents fluorescence data,
# we will store the :py:class:`~pynwb.ophys.RoiResponseSeries` object inside
# of a :py:class:`~pynwb.ophys.Fluorescence` object.
# Then add the :py:class:`~pynwb.ophys.Fluorescence` object into the
# same :py:class:`~pynwb.base.ProcessingModule` named ``"ophys"`` that we created earlier.
#
# .. only:: html
#
#   .. image:: ../../_static/Fluorescence.svg
#     :width: 600
Beispiel #22
0
def save_nwb(ops1):
    if NWB and not ops1[0]['mesoscan']:
        if len(ops1) > 1:
            multiplane = True
        else:
            multiplane = False

        ops = ops1[0]

        ### INITIALIZE NWB FILE
        nwbfile = NWBFile(
            session_description='suite2p_proc',
            identifier=ops['data_path'][0],
            session_start_time=(ops['date_proc'] if 'date_proc' in ops else
                                datetime.datetime.now()))
        print(nwbfile)

        device = nwbfile.create_device(
            name='Microscope',
            description='My two-photon microscope',
            manufacturer='The best microscope manufacturer')
        optical_channel = OpticalChannel(name='OpticalChannel',
                                         description='an optical channel',
                                         emission_lambda=500.)

        imaging_plane = nwbfile.create_imaging_plane(
            name='ImagingPlane',
            optical_channel=optical_channel,
            imaging_rate=ops['fs'],
            description='standard',
            device=device,
            excitation_lambda=600.,
            indicator='GCaMP',
            location='V1',
            grid_spacing=([2, 2, 30] if multiplane else [2, 2]),
            grid_spacing_unit='microns')

        # link to external data
        image_series = TwoPhotonSeries(
            name='TwoPhotonSeries',
            dimension=[ops['Ly'], ops['Lx']],
            external_file=(ops['filelist'] if 'filelist' in ops else ['']),
            imaging_plane=imaging_plane,
            starting_frame=[0],
            format='external',
            starting_time=0.0,
            rate=ops['fs'] * ops['nplanes'])
        nwbfile.add_acquisition(image_series)

        # processing
        img_seg = ImageSegmentation()
        ps = img_seg.create_plane_segmentation(name='PlaneSegmentation',
                                               description='suite2p output',
                                               imaging_plane=imaging_plane,
                                               reference_images=image_series)
        ophys_module = nwbfile.create_processing_module(
            name='ophys', description='optical physiology processed data')
        ophys_module.add(img_seg)

        file_strs = ['F.npy', 'Fneu.npy', 'spks.npy']
        traces = []
        ncells_all = 0
        for iplane, ops in enumerate(ops1):
            if iplane == 0:
                iscell = np.load(os.path.join(ops['save_path'], 'iscell.npy'))
                for fstr in file_strs:
                    traces.append(np.load(os.path.join(ops['save_path'],
                                                       fstr)))
            else:
                iscell = np.append(iscell,
                                   np.load(
                                       os.path.join(ops['save_path'],
                                                    'iscell.npy')),
                                   axis=0)
                for i, fstr in enumerate(file_strs):
                    traces[i] = np.append(
                        traces[i],
                        np.load(os.path.join(ops['save_path'], fstr)),
                        axis=0)

            stat = np.load(os.path.join(ops['save_path'], 'stat.npy'),
                           allow_pickle=True)
            ncells = len(stat)
            for n in range(ncells):
                if multiplane:
                    pixel_mask = np.array([
                        stat[n]['ypix'], stat[n]['xpix'],
                        iplane * np.ones(stat[n]['npix']), stat[n]['lam']
                    ])
                    ps.add_roi(voxel_mask=pixel_mask.T)
                else:
                    pixel_mask = np.array(
                        [stat[n]['ypix'], stat[n]['xpix'], stat[n]['lam']])
                    ps.add_roi(pixel_mask=pixel_mask.T)
            ncells_all += ncells

        ps.add_column('iscell', 'two columns - iscell & probcell', iscell)

        rt_region = ps.create_roi_table_region(region=list(
            np.arange(0, ncells_all)),
                                               description='all ROIs')

        # FLUORESCENCE (all are required)
        file_strs = ['F.npy', 'Fneu.npy', 'spks.npy']
        name_strs = ['Fluorescence', 'Neuropil', 'Deconvolved']

        for i, (fstr, nstr) in enumerate(zip(file_strs, name_strs)):
            roi_resp_series = RoiResponseSeries(name=nstr,
                                                data=traces[i],
                                                rois=rt_region,
                                                unit='lumens',
                                                rate=ops['fs'])
            fl = Fluorescence(roi_response_series=roi_resp_series, name=nstr)
            ophys_module.add(fl)

        # BACKGROUNDS
        # (meanImg, Vcorr and max_proj are REQUIRED)
        bg_strs = ['meanImg', 'Vcorr', 'max_proj', 'meanImg_chan2']
        nplanes = ops['nplanes']
        for iplane in range(nplanes):
            images = Images('Backgrounds_%d' % iplane)
            for bstr in bg_strs:
                if bstr in ops:
                    if bstr == 'Vcorr' or bstr == 'max_proj':
                        img = np.zeros((ops['Ly'], ops['Lx']), np.float32)
                        img[ops['yrange'][0]:ops['yrange'][-1],
                            ops['xrange'][0]:ops['xrange'][-1]] = ops[bstr]
                    else:
                        img = ops[bstr]
                    images.add_image(GrayscaleImage(name=bstr, data=img))

            ophys_module.add(images)

        with NWBHDF5IO(os.path.join(ops['save_path0'], 'suite2p', 'ophys.nwb'),
                       'w') as fio:
            fio.write(nwbfile)
    else:
        print('pip install pynwb OR don"t use mesoscope recording')
rt_region = ps.create_roi_table_region(region=list(range(len(rois))),
                                       description='rois')

## Store the data of each the ROIs in their RoiResponseSeries

## first get the roi data signal
rois_data = rois_signal(data=data, rois_masks=rois)
rois_data_mean = [rois_data[i].mean(axis=1) for i in range(len(rois_data))]
[rois_data_mean[i].shape for i in range(len(rois_data_mean))]

rois_data_mean = np.stack(rois_data_mean)

## Then create RoiResponseSeries to hold the data of those ROIs
roi_resp_series = RoiResponseSeries(name='RoiResponseSeries',
                                    data=rois_data_mean,
                                    rois=rt_region,
                                    unit='intensity',
                                    rate=400.)

fl = Fluorescence(roi_response_series=roi_resp_series)
ophys_module.add(fl)

# df_over_f = DfOverF(roi_response_series=roi_resp_series)
# ophys_module.add(df_over_f)

with NWBHDF5IO('sample_data.nwb', 'w') as io:
    io.write(nwbfile)

#
# io = NWBHDF5IO('sample_data.nwb', 'r')
# nwbfile = io.read()