Esempio n. 1
0
class ProcessedOphysNWBConverter(OphysNWBConverter):
    def __init__(self, metadata, nwbfile=None, source_paths=None):
        super(ProcessedOphysNWBConverter,
              self).__init__(metadata,
                             nwbfile=nwbfile,
                             source_paths=source_paths)

        self.image_segmentation = ImageSegmentation()
        self.ophys_mod.add_data_interface(self.image_segmentation)

    def create_plane_segmentation(self, metadata):

        input_kwargs = dict(
            name='PlaneSegmentation',
            description='output from segmenting my favorite imaging plane',
            imaging_plane=self.imaging_plane)

        if metadata:
            input_kwargs.update(metadata)
        elif 'Ophys' in self.metadata and 'PlaneSegmentation' in self.metadata[
                'Ophys']:
            input_kwargs.update(self.metadata['Ophys']['PlaneSegmentation'])

        self.plane_segmentation = self.image_segmentation.create_plane_segmentation(
            **input_kwargs)
Esempio n. 2
0
    def __init__(self,
                 nwbfile,
                 emission_lambda=np.nan,
                 excitation_lambda=np.nan,
                 frame_rate=np.nan,
                 indicator='unknown',
                 location='unknown'):

        super(OphysProcessing2NWB, self).__init__(nwbfile)

        device = Device('microscope')
        self.nwbfile.add_device(device)
        optical_channel = OpticalChannel('OpticalChannel',
                                         'description',
                                         emission_lambda=emission_lambda)
        imaging_plane = self.nwbfile.create_imaging_plane(
            'ImagingPlane',
            optical_channel,
            description='description',
            device=device,
            excitation_lambda=excitation_lambda,
            imaging_rate=frame_rate,
            indicator=indicator,
            location=location)

        self.ophys_mod = self.nwbfile.create_processing_module(
            'ophys', 'contains optical physiology processed data')
        img_seg = ImageSegmentation()
        self.ophys_mod.add_data_interface(img_seg)
        self.ps = img_seg.create_plane_segmentation(
            'output from segmenting my favorite imaging plane', imaging_plane,
            'PlaneSegmentation')
Esempio n. 3
0
    def add_ophys_processed(self):
        """Add Fluorescence data"""
        imaging_plane = self._get_imaging_plane()
        with h5py.File(self.source_paths['path_processed'], 'r') as f:
            # Stores segmented data
            ophys_module = self.nwbfile.create_processing_module(
                name='ophys',
                description='contains optical physiology processed data')

            meta_imgseg = self.metadata['Ophys']['ImageSegmentation']
            img_seg = ImageSegmentation(name=meta_imgseg['name'])
            ophys_module.add(img_seg)

            meta_planeseg = meta_imgseg['plane_segmentations'][0]
            if meta_planeseg['reference_images'] in self.nwbfile.acquisition:
                reference_images = self.nwbfile.acquisition[
                    meta_planeseg['reference_images']]
            else:
                reference_images = None
            plane_segmentation = img_seg.create_plane_segmentation(
                name=meta_planeseg['name'],
                description=meta_planeseg['description'],
                imaging_plane=imaging_plane,
                reference_images=reference_images,
            )

            # ROIs
            n_rows = int(f['linesPerFrame'][0])
            n_cols = int(f['pixelsPerLine'][0][0])
            pixel_mask = []
            for pi in np.squeeze(f['pixel_list'][:]):
                row = int(pi // n_rows)
                col = int(pi % n_rows)
                pixel_mask.append([col, row, 1])
            plane_segmentation.add_roi(pixel_mask=pixel_mask)

            # Fluorescene data
            meta_fluorescence = self.metadata['Ophys']['Fluorescence']
            fl = Fluorescence(name=meta_fluorescence['name'])
            ophys_module.add(fl)

            with h5py.File(self.source_paths['path_calibration'], 'r') as fc:
                fluorescence_mean_trace = np.squeeze(fc['dff'])
                rt_region = plane_segmentation.create_roi_table_region(
                    description='unique cell ROI', region=[0])

                imaging_rate = 1 / np.array(fc['dto'])
                fl.create_roi_response_series(
                    name=meta_fluorescence['roi_response_series'][0]['name'],
                    data=fluorescence_mean_trace,
                    rois=rt_region,
                    rate=imaging_rate,
                    starting_time=0.,
                    unit='no unit')
Esempio n. 4
0
def add_rois(nwbfile, module, expt):

    img_seg = ImageSegmentation()
    module.add_data_interface(img_seg)
    ps = img_seg.create_plane_segmentation(
        name='Plane Segmentation',
        description='ROIs',
        imaging_plane=nwbfile.get_imaging_plane('Imaging Data'))

    rois = expt.rois()
    for roi in rois:
        ps.add_roi(image_mask=get_image_mask(roi))

    return ps
Esempio n. 5
0
    def setUp(self):
        start_time = datetime(2017, 4, 3, 11, 0, 0)
        create_date = datetime(2017, 4, 15, 12, 0, 0)

        # create your NWBFile object
        nwbfile = NWBFile('PyNWB Sample File', 'A simple NWB file', 'NWB_test', start_time,
                        file_create_date=create_date)
        
        # create acquisition metadata
        optical_channel = OpticalChannel('test_optical_channel', 'optical channel source',
                                        'optical channel description', 3.14)
        imaging_plane = nwbfile.create_imaging_plane('test_imaging_plane',
                                                    'ophys integration tests',
                                                    optical_channel,
                                                    'imaging plane description',
                                                    'imaging_device_1',
                                                    6.28, '2.718', 'GFP', 'somewhere in the brain',
                                                    (1, 2, 1, 2, 3), 4.0, 'manifold unit', 'A frame to refer to')

        # create acquisition data
        image_series = TwoPhotonSeries(name='test_iS', source='a hypothetical source', dimension=[2],
                                    external_file=['images.tiff'], imaging_plane=imaging_plane,
                                    starting_frame=[1, 2, 3], format='tiff', timestamps=list())
        nwbfile.add_acquisition(image_series)


        mod = nwbfile.create_processing_module('img_seg_example', 'ophys demo', 'an example of writing Ca2+ imaging data')
        img_seg = ImageSegmentation('a toy image segmentation container')
        mod.add_data_interface(img_seg)
        ps = img_seg.create_plane_segmentation('integration test PlaneSegmentation', 'plane segmentation description',
                                            imaging_plane, 'test_plane_seg_name', image_series)
        # add two ROIs
        # - first argument is the pixel mask i.e. a list of pixels and their weights
        # - second argument is the image mask
        w, h = 3, 3
        pix_mask1 = [(0, 0, 1.1), (1, 1, 1.2), (2, 2, 1.3)]
        img_mask1 = [[0.0 for x in range(w)] for y in range(h)]
        img_mask1[0][0] = 1.1
        img_mask1[1][1] = 1.2
        img_mask1[2][2] = 1.3
        ps.add_roi('1234', pix_mask1, img_mask1)

        pix_mask2 = [(0, 0, 2.1), (1, 1, 2.2)]
        img_mask2 = [[0.0 for x in range(w)] for y in range(h)]
        img_mask2[0][0] = 2.1
        img_mask2[1][1] = 2.2
        ps.add_roi('5678', pix_mask2, img_mask2)

        # add a Fluorescence container
        fl = Fluorescence('a toy fluorescence container')
        mod.add_data_interface(fl)
        # get an ROI table region i.e. a subset of ROIs to create a RoiResponseSeries from
        rt_region = ps.create_roi_table_region('the first of two ROIs', region=[0])
        # make some fake timeseries data
        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]
        rrs = fl.create_roi_response_series('test_roi_response_series', 'RoiResponseSeries integration test',
                                            data, 'lumens', rt_region, timestamps=timestamps)
        # write data
        nwb_path = './test_data/nwb_test_file.nwb'
        with NWBHDF5IO(nwb_path, 'w') as io:
            io.write(nwbfile)
Esempio n. 6
0
def add_cell_specimen_table(nwbfile: NWBFile,
                            cell_specimen_table: pd.DataFrame,
                            session_metadata: dict):
    """
    This function takes the cell specimen table and writes the ROIs
    contained within. It writes these to a new NWB imaging plane
    based off the previously supplied metadata

    Parameters
    ----------
    nwbfile: NWBFile
        this is the in memory NWBFile currently being written to which ROI data
        is added
    cell_specimen_table: pd.DataFrame
        this is the DataFrame containing the cells segmented from a ophys
        experiment, stored in json file and loaded.
        example: /home/nicholasc/projects/allensdk/allensdk/test/
                 brain_observatory/behavior/cell_specimen_table_789359614.json
    session_metadata: dict
        Dictionary containing cell_specimen_table related metadata. Should
        include at minimum the following fields:
            "emission_lambda", "excitation_lambda", "indicator",
            "targeted_structure", and ophys_frame_rate"

    Returns
    -------
    nwbfile: NWBFile
        The altered in memory NWBFile object that now has a specimen table
    """
    cell_specimen_metadata = NwbOphysMetadataSchema().load(
        session_metadata, unknown=marshmallow.EXCLUDE)
    cell_roi_table = cell_specimen_table.reset_index().set_index('cell_roi_id')

    # Device:
    device_name: str = nwbfile.lab_meta_data['metadata'].equipment_name
    if device_name.startswith("MESO"):
        device_config = {
            "name": device_name,
            "description": "Allen Brain Observatory - Mesoscope 2P Rig"
        }
    else:
        device_config = {
            "name": device_name,
            "description": "Allen Brain Observatory - Scientifica 2P Rig",
            "manufacturer": "Scientifica"
        }
    nwbfile.create_device(**device_config)
    device = nwbfile.get_device(device_name)

    # FOV:
    fov_width = nwbfile.lab_meta_data['metadata'].field_of_view_width
    fov_height = nwbfile.lab_meta_data['metadata'].field_of_view_height
    imaging_plane_description = "{} field of view in {} at depth {} um".format(
        (fov_width, fov_height), cell_specimen_metadata['targeted_structure'],
        nwbfile.lab_meta_data['metadata'].imaging_depth)

    # Optical Channel:
    optical_channel = OpticalChannel(
        name='channel_1',
        description='2P Optical Channel',
        emission_lambda=cell_specimen_metadata['emission_lambda'])

    # Imaging Plane:
    imaging_plane = nwbfile.create_imaging_plane(
        name='imaging_plane_1',
        optical_channel=optical_channel,
        description=imaging_plane_description,
        device=device,
        excitation_lambda=cell_specimen_metadata['excitation_lambda'],
        imaging_rate=cell_specimen_metadata['ophys_frame_rate'],
        indicator=cell_specimen_metadata['indicator'],
        location=cell_specimen_metadata['targeted_structure'])

    # Image Segmentation:
    image_segmentation = ImageSegmentation(name="image_segmentation")

    if 'ophys' not in nwbfile.processing:
        ophys_module = ProcessingModule('ophys', 'Ophys processing module')
        nwbfile.add_processing_module(ophys_module)
    else:
        ophys_module = nwbfile.processing['ophys']

    ophys_module.add_data_interface(image_segmentation)

    # Plane Segmentation:
    plane_segmentation = image_segmentation.create_plane_segmentation(
        name='cell_specimen_table',
        description="Segmented rois",
        imaging_plane=imaging_plane)

    for col_name in cell_roi_table.columns:
        # the columns 'roi_mask', 'pixel_mask', and 'voxel_mask' are
        # already defined in the nwb.ophys::PlaneSegmentation Object
        if col_name not in [
                'id', 'mask_matrix', 'roi_mask', 'pixel_mask', 'voxel_mask'
        ]:
            # This builds the columns with name of column and description
            # of column both equal to the column name in the cell_roi_table
            plane_segmentation.add_column(
                col_name,
                CELL_SPECIMEN_COL_DESCRIPTIONS.get(col_name,
                                                   "No Description Available"))

    # go through each roi and add it to the plan segmentation object
    for cell_roi_id, table_row in cell_roi_table.iterrows():

        # NOTE: The 'roi_mask' in this cell_roi_table has already been
        # processing by the function from
        # allensdk.brain_observatory.behavior.session_apis.data_io.ophys_lims_api
        # get_cell_specimen_table() method. As a result, the ROI is stored in
        # an array that is the same shape as the FULL field of view of the
        # experiment (e.g. 512 x 512).
        mask = table_row.pop('roi_mask')

        csid = table_row.pop('cell_specimen_id')
        table_row['cell_specimen_id'] = -1 if csid is None else csid
        table_row['id'] = cell_roi_id
        plane_segmentation.add_roi(image_mask=mask, **table_row.to_dict())

    return nwbfile
Esempio n. 7
0
#   .. image:: ../../_static/ImageSegmentation.png
#     :width: 800
#     :alt: image segmentation UML diagram
#     :align: center
#
# First, we create an :py:class:`~pynwb.ophys.ImageSegmentation` object, then
# from that object we create a :py:class:`~pynwb.ophys.PlaneSegmentation` table
# with a link to the :py:class:`~pynwb.ophys.ImagingPlane` created earlier.
# Then we will add the :py:class:`~pynwb.ophys.ImageSegmentation` object
# to the previously created :py:class:`~pynwb.base.ProcessingModule`.

img_seg = ImageSegmentation()

ps = img_seg.create_plane_segmentation(
    name='PlaneSegmentation',
    description='output from segmenting my favorite imaging plane',
    imaging_plane=imaging_plane,
    reference_images=image_series1  # optional
)

ophys_module.add(img_seg)

####################
# Regions Of Interest (ROIs)
# ---------------------------------
#
# Image masks
# ^^^^^^^^^^^
#
# You can add ROIs to the :py:class:`~pynwb.ophys.PlaneSegmentation` table using
# an image mask or a pixel mask. An image mask is an array that is the same size
# as a single frame of the :py:class:`~pynwb.ophys.TwoPhotonSeries` that
Esempio n. 8
0
def conversion_function(source_paths,
                        f_nwb,
                        metadata,
                        add_raw=False,
                        add_processed=True,
                        add_behavior=True,
                        plot_rois=False):
    """
    Copy data stored in a set of .npz files to a single NWB file.

    Parameters
    ----------
    source_paths : dict
        Dictionary with paths to source files/directories. e.g.:
        {'raw_data': {'type': 'file', 'path': ''},
         'raw_info': {'type': 'file', 'path': ''}
         'processed_data': {'type': 'file', 'path': ''},
         'sparse_matrix': {'type': 'file', 'path': ''},
         'ref_image',: {'type': 'file', 'path': ''}}
    f_nwb : str
        Path to output NWB file, e.g. 'my_file.nwb'.
    metadata : dict
        Metadata dictionary
    add_raw : bool
        Whether to convert raw data or not.
    add_processed : bool
        Whether to convert processed data or not.
    add_behavior : bool
        Whether to convert behavior data or not.
    plot_rois : bool
        Plot ROIs
    """

    # Source files
    file_raw = None
    file_info = None
    file_processed = None
    file_sparse_matrix = None
    file_reference_image = None
    for k, v in source_paths.items():
        if source_paths[k]['path'] != '':
            fname = source_paths[k]['path']
            if k == 'raw_data':
                file_raw = h5py.File(fname, 'r')
            if k == 'raw_info':
                file_info = scipy.io.loadmat(fname,
                                             struct_as_record=False,
                                             squeeze_me=True)
            if k == 'processed_data':
                file_processed = np.load(fname)
            if k == 'sparse_matrix':
                file_sparse_matrix = np.load(fname)
            if k == 'ref_image':
                file_reference_image = np.load(fname)

    # Initialize a NWB object
    nwb = NWBFile(**metadata['NWBFile'])

    # Create and add device
    device = Device(name=metadata['Ophys']['Device'][0]['name'])
    nwb.add_device(device)

    # Creates one Imaging Plane for each channel
    fs = 1. / (file_processed['time'][0][1] - file_processed['time'][0][0])
    for meta_ip in metadata['Ophys']['ImagingPlane']:
        # Optical channel
        opt_ch = OpticalChannel(
            name=meta_ip['optical_channel'][0]['name'],
            description=meta_ip['optical_channel'][0]['description'],
            emission_lambda=meta_ip['optical_channel'][0]['emission_lambda'])
        nwb.create_imaging_plane(
            name=meta_ip['name'],
            optical_channel=opt_ch,
            description=meta_ip['description'],
            device=device,
            excitation_lambda=meta_ip['excitation_lambda'],
            imaging_rate=fs,
            indicator=meta_ip['indicator'],
            location=meta_ip['location'],
        )

    # Raw optical data
    if add_raw:
        print('Adding raw data...')
        for meta_tps in metadata['Ophys']['TwoPhotonSeries']:
            if meta_tps['name'][-1] == 'R':
                raw_data = file_raw['R']
            else:
                raw_data = file_raw['Y']

            def data_gen(data):
                xl, yl, zl, tl = data.shape
                chunk = 0
                while chunk < tl:
                    val = data[:, :, :, chunk]
                    chunk += 1
                    print('adding data chunk: ', chunk)
                    yield val

            xl, yl, zl, tl = raw_data.shape
            tps_data = DataChunkIterator(data=data_gen(data=raw_data),
                                         iter_axis=0,
                                         maxshape=(tl, xl, yl, zl))

            # Change dimensions from (X,Y,Z,T) in mat file to (T,X,Y,Z) nwb standard
            #raw_data = np.moveaxis(raw_data, -1, 0)

            tps = TwoPhotonSeries(
                name=meta_tps['name'],
                imaging_plane=nwb.imaging_planes[meta_tps['imaging_plane']],
                data=tps_data,
                rate=file_info['info'].daq.scanRate)
            nwb.add_acquisition(tps)

    # Processed data
    if add_processed:
        print('Adding processed data...')
        ophys_module = ProcessingModule(
            name='Ophys',
            description='contains optical physiology processed data.',
        )
        nwb.add_processing_module(ophys_module)

        # Create Image Segmentation compartment
        img_seg = ImageSegmentation(
            name=metadata['Ophys']['ImageSegmentation']['name'])
        ophys_module.add(img_seg)

        # Create plane segmentation and add ROIs
        meta_ps = metadata['Ophys']['ImageSegmentation'][
            'plane_segmentations'][0]
        ps = img_seg.create_plane_segmentation(
            name=meta_ps['name'],
            description=meta_ps['description'],
            imaging_plane=nwb.imaging_planes[meta_ps['imaging_plane']],
        )

        # Add ROIs
        indices = file_sparse_matrix['indices']
        indptr = file_sparse_matrix['indptr']
        dims = np.squeeze(file_processed['dims'])
        for start, stop in zip(indptr, indptr[1:]):
            voxel_mask = make_voxel_mask(indices[start:stop], dims)
            ps.add_roi(voxel_mask=voxel_mask)

        # Visualize 3D voxel masks
        if plot_rois:
            plot_rois_function(plane_segmentation=ps, indptr=indptr)

        # DFF measures
        dff = DfOverF(name=metadata['Ophys']['DfOverF']['name'])
        ophys_module.add(dff)

        # create ROI regions
        n_cells = file_processed['dFF'].shape[0]
        roi_region = ps.create_roi_table_region(description='RoiTableRegion',
                                                region=list(range(n_cells)))

        # create ROI response series
        dff_data = file_processed['dFF']
        tt = file_processed['time'].ravel()
        meta_rrs = metadata['Ophys']['DfOverF']['roi_response_series'][0]
        meta_rrs['data'] = dff_data.T
        meta_rrs['rois'] = roi_region
        meta_rrs['timestamps'] = tt
        dff.create_roi_response_series(**meta_rrs)

        # Creates GrayscaleVolume containers and add a reference image
        grayscale_volume = GrayscaleVolume(
            name=metadata['Ophys']['GrayscaleVolume']['name'],
            data=file_reference_image['im'])
        ophys_module.add(grayscale_volume)

    # Behavior data
    if add_behavior:
        print('Adding behavior data...')
        # Ball motion
        behavior_mod = nwb.create_processing_module(
            name='Behavior',
            description='holds processed behavior data',
        )
        meta_ts = metadata['Behavior']['TimeSeries'][0]
        meta_ts['data'] = file_processed['ball'].ravel()
        tt = file_processed['time'].ravel()
        meta_ts['timestamps'] = tt
        behavior_ts = TimeSeries(**meta_ts)
        behavior_mod.add(behavior_ts)

        # Re-arranges spatial data of body-points positions tracking
        pos = file_processed['dlc']
        n_points = 8
        pos_reshaped = pos.reshape(
            (-1, n_points, 3))  # dims=(nSamples,n_points,3)

        # Creates a Position object and add one SpatialSeries for each body-point position
        position = Position()
        for i in range(n_points):
            position.create_spatial_series(
                name='SpatialSeries_' + str(i),
                data=pos_reshaped[:, i, :],
                timestamps=tt,
                reference_frame=
                'Description defining what the zero-position is.',
                conversion=np.nan)
        behavior_mod.add(position)

    # Trial times
    trialFlag = file_processed['trialFlag'].ravel()
    trial_inds = np.hstack(
        (0, np.where(np.diff(trialFlag))[0], trialFlag.shape[0] - 1))
    trial_times = tt[trial_inds]

    for start, stop in zip(trial_times, trial_times[1:]):
        nwb.add_trial(start_time=start, stop_time=stop)

    # Saves to NWB file
    with NWBHDF5IO(f_nwb, mode='w') as io:
        io.write(nwb)
    print('NWB file saved with size: ', os.stat(f_nwb).st_size / 1e6, ' mb')
Esempio n. 9
0
    def write_segmentation(segext_obj,
                           save_path,
                           plane_num=0,
                           metadata=None,
                           overwrite=True):
        save_path = Path(save_path)
        assert save_path.suffix == '.nwb'
        if save_path.is_file() and not overwrite:
            nwbfile_exist = True
            file_mode = 'r+'
        else:
            if save_path.is_file():
                os.remove(save_path)
            if not save_path.parent.is_dir():
                save_path.parent.mkdir(parents=True)
            nwbfile_exist = False
            file_mode = 'w'

        # parse metadata correctly:
        if isinstance(segext_obj, MultiSegmentationExtractor):
            segext_objs = segext_obj.segmentations
            if metadata is not None:
                assert isinstance(metadata, list), "For MultiSegmentationExtractor enter 'metadata' as a list of " \
                                                   "SegmentationExtractor metadata"
                assert len(metadata) == len(segext_objs), "The 'metadata' argument should be a list with the same " \
                                                          "number of elements as the segmentations in the " \
                                                          "MultiSegmentationExtractor"
        else:
            segext_objs = [segext_obj]
            if metadata is not None and not isinstance(metadata, list):
                metadata = [metadata]
        metadata_base_list = [
            NwbSegmentationExtractor.get_nwb_metadata(sgobj)
            for sgobj in segext_objs
        ]

        print(f'writing nwb for {segext_obj.extractor_name}\n')
        # updating base metadata with new:
        for num, data in enumerate(metadata_base_list):
            metadata_input = metadata[num] if metadata else {}
            metadata_base_list[num] = dict_recursive_update(
                metadata_base_list[num], metadata_input)
        # loop for every plane:
        with NWBHDF5IO(str(save_path), file_mode) as io:
            metadata_base_common = metadata_base_list[0]
            if nwbfile_exist:
                nwbfile = io.read()
            else:
                nwbfile = NWBFile(**metadata_base_common['NWBFile'])
                # Subject:
                if metadata_base_common.get('Subject'):
                    nwbfile.subject = Subject(
                        **metadata_base_common['Subject'])

            # Processing Module:
            if 'ophys' not in nwbfile.processing:
                ophys = nwbfile.create_processing_module(
                    'ophys', 'contains optical physiology processed data')
            else:
                ophys = nwbfile.get_processing_module('ophys')

            for plane_no_loop, (segext_obj, metadata) in enumerate(
                    zip(segext_objs, metadata_base_list)):
                # Device:
                if metadata['Ophys']['Device'][0][
                        'name'] not in nwbfile.devices:
                    nwbfile.create_device(**metadata['Ophys']['Device'][0])

                # ImageSegmentation:
                image_segmentation_name = 'ImageSegmentation' if plane_no_loop == 0 else f'ImageSegmentation_Plane{plane_no_loop}'
                if image_segmentation_name not in ophys.data_interfaces:
                    image_segmentation = ImageSegmentation(
                        name=image_segmentation_name)
                    ophys.add_data_interface(image_segmentation)
                else:
                    image_segmentation = ophys.data_interfaces.get(
                        image_segmentation_name)

                # OpticalChannel:
                optical_channels = [
                    OpticalChannel(**i) for i in metadata['Ophys']
                    ['ImagingPlane'][0]['optical_channel']
                ]

                # ImagingPlane:
                image_plane_name = 'ImagingPlane' if plane_no_loop == 0 else f'ImagePlane_{plane_no_loop}'
                if image_plane_name not in nwbfile.imaging_planes.keys():
                    input_kwargs = dict(
                        name=image_plane_name,
                        device=nwbfile.get_device(metadata_base_common['Ophys']
                                                  ['Device'][0]['name']),
                    )
                    metadata['Ophys']['ImagingPlane'][0][
                        'optical_channel'] = optical_channels
                    input_kwargs.update(**metadata['Ophys']['ImagingPlane'][0])
                    if 'imaging_rate' in input_kwargs:
                        input_kwargs['imaging_rate'] = float(
                            input_kwargs['imaging_rate'])
                    imaging_plane = nwbfile.create_imaging_plane(
                        **input_kwargs)
                else:
                    imaging_plane = nwbfile.imaging_planes[image_plane_name]

                # PlaneSegmentation:
                input_kwargs = dict(
                    description='output from segmenting imaging plane',
                    imaging_plane=imaging_plane)
                ps_metadata = metadata['Ophys']['ImageSegmentation'][
                    'plane_segmentations'][0]
                if ps_metadata[
                        'name'] not in image_segmentation.plane_segmentations:
                    input_kwargs.update(**ps_metadata)
                    ps = image_segmentation.create_plane_segmentation(
                        **input_kwargs)
                    ps_exist = False
                else:
                    ps = image_segmentation.get_plane_segmentation(
                        ps_metadata['name'])
                    ps_exist = True

                # ROI add:
                image_masks = segext_obj.get_roi_image_masks()
                roi_ids = segext_obj.get_roi_ids()
                accepted_list = segext_obj.get_accepted_list()
                accepted_list = [] if accepted_list is None else accepted_list
                rejected_list = segext_obj.get_rejected_list()
                rejected_list = [] if rejected_list is None else rejected_list
                accepted_ids = [
                    1 if k in accepted_list else 0 for k in roi_ids
                ]
                rejected_ids = [
                    1 if k in rejected_list else 0 for k in roi_ids
                ]
                roi_locations = np.array(segext_obj.get_roi_locations()).T
                if not ps_exist:
                    ps.add_column(
                        name='RoiCentroid',
                        description=
                        'x,y location of centroid of the roi in image_mask')
                    ps.add_column(
                        name='Accepted',
                        description=
                        '1 if ROi was accepted or 0 if rejected as a cell during segmentation operation'
                    )
                    ps.add_column(
                        name='Rejected',
                        description=
                        '1 if ROi was rejected or 0 if accepted as a cell during segmentation operation'
                    )
                    for num, row in enumerate(roi_ids):
                        ps.add_roi(id=row,
                                   image_mask=image_masks[:, :, num],
                                   RoiCentroid=roi_locations[num, :],
                                   Accepted=accepted_ids[num],
                                   Rejected=rejected_ids[num])

                # Fluorescence Traces:
                if 'Flourescence' not in ophys.data_interfaces:
                    fluorescence = Fluorescence()
                    ophys.add_data_interface(fluorescence)
                else:
                    fluorescence = ophys.data_interfaces['Fluorescence']
                roi_response_dict = segext_obj.get_traces_dict()
                roi_table_region = ps.create_roi_table_region(
                    description=f'region for Imaging plane{plane_no_loop}',
                    region=list(range(segext_obj.get_num_rois())))
                rate = np.float('NaN') if segext_obj.get_sampling_frequency(
                ) is None else segext_obj.get_sampling_frequency()
                for i, j in roi_response_dict.items():
                    data = getattr(segext_obj, f'_roi_response_{i}')
                    if data is not None:
                        data = np.asarray(data)
                        trace_name = 'RoiResponseSeries' if i == 'raw' else i.capitalize(
                        )
                        trace_name = trace_name if plane_no_loop == 0 else trace_name + f'_Plane{plane_no_loop}'
                        input_kwargs = dict(name=trace_name,
                                            data=data.T,
                                            rois=roi_table_region,
                                            rate=rate,
                                            unit='n.a.')
                        if trace_name not in fluorescence.roi_response_series:
                            fluorescence.create_roi_response_series(
                                **input_kwargs)

                # create Two Photon Series:
                if 'TwoPhotonSeries' not in nwbfile.acquisition:
                    warn(
                        'could not find TwoPhotonSeries, using ImagingExtractor to create an nwbfile'
                    )

                # adding images:
                images_dict = segext_obj.get_images_dict()
                if any([image is not None for image in images_dict.values()]):
                    images_name = 'SegmentationImages' if plane_no_loop == 0 else f'SegmentationImages_Plane{plane_no_loop}'
                    if images_name not in ophys.data_interfaces:
                        images = Images(images_name)
                        for img_name, img_no in images_dict.items():
                            if img_no is not None:
                                images.add_image(
                                    GrayscaleImage(name=img_name, data=img_no))
                        ophys.add(images)

            # saving NWB file:
            io.write(nwbfile)

        # test read
        with NWBHDF5IO(str(save_path), 'r') as io:
            io.read()
Esempio n. 10
0
                                unit='pixel intensity')
nwbfile.add_acquisition(image_series1)

## Create a ProcessingModule to store the future processed data
ophys_module = nwbfile.create_processing_module(
    name='ophys', description='optical physiology processed data')

## ImageSegmentation object and add the ImageSegmentation to the ophy module.
img_seg = ImageSegmentation(
)  ## imagesegmentation can contain multiple planesegmentation
ophys_module.add(img_seg)

## Create PlaneSegmentation tables within the targetted imaging_plane
ps = img_seg.create_plane_segmentation(
    name='ROIs',
    description='ROIs derived from manual contour of the images',
    imaging_plane=imaging_plane,
    reference_images=image_series1)

## will be easier down the line with image_mask
for roi in rois:
    # roi = rois[0]
    x_y_true = np.transpose(np.where(roi[1] == True))
    pixel_mask = []
    for _ in x_y_true:
        ix = x_y_true[0][0]
        iy = x_y_true[0][1]
        pixel_mask.append((ix, iy, 1))
    # add pixel mask to plane segmentation
    ps.add_roi(pixel_mask=pixel_mask)
Esempio n. 11
0
def add_cell_specimen_table(nwbfile: NWBFile,
                            cell_specimen_table: pd.DataFrame):
    """
    This function takes the cell specimen table and writes the ROIs
    contained within. It writes these to a new NWB imaging plane
    based off the previously supplied metadata
    Parameters
    ----------
    nwbfile: NWBFile
        this is the in memory NWBFile currently being written to which ROI data
        is added
    cell_specimen_table: pd.DataFrame
        this is the DataFrame containing the cells segmented from a ophys
        experiment, stored in json file and loaded.
        example: /home/nicholasc/projects/allensdk/allensdk/test/
                 brain_observatory/behavior/cell_specimen_table_789359614.json

    Returns
    -------
    nwbfile: NWBFile
        The altered in memory NWBFile object that now has a specimen table
    """
    cell_roi_table = cell_specimen_table.reset_index().set_index('cell_roi_id')

    # Device:
    device_name = nwbfile.lab_meta_data['metadata'].rig_name
    nwbfile.create_device(device_name,
                          "Allen Brain Observatory")
    device = nwbfile.get_device(device_name)

    # Location:
    location_description = "Area: {}, Depth: {} um".format(
        nwbfile.lab_meta_data['metadata'].targeted_structure,
        nwbfile.lab_meta_data['metadata'].imaging_depth)

    # FOV:
    fov_width = nwbfile.lab_meta_data['metadata'].field_of_view_width
    fov_height = nwbfile.lab_meta_data['metadata'].field_of_view_height
    imaging_plane_description = "{} field of view in {} at depth {} um".format(
        (fov_width, fov_height),
        nwbfile.lab_meta_data['metadata'].targeted_structure,
        nwbfile.lab_meta_data['metadata'].imaging_depth)

    # Optical Channel:
    optical_channel = OpticalChannel(
        name='channel_1',
        description='2P Optical Channel',
        emission_lambda=nwbfile.lab_meta_data['metadata'].emission_lambda)

    # Imaging Plane:
    imaging_plane = nwbfile.create_imaging_plane(
        name='imaging_plane_1',
        optical_channel=optical_channel,
        description=imaging_plane_description,
        device=device,
        excitation_lambda=nwbfile.lab_meta_data['metadata'].excitation_lambda,
        imaging_rate=nwbfile.lab_meta_data['metadata'].ophys_frame_rate,
        indicator=nwbfile.lab_meta_data['metadata'].indicator,
        location=location_description,
        manifold=[],  # Should this be passed in for future support?
        conversion=1.0,
        unit='unknown',  # Should this be passed in for future support?
        reference_frame='unknown')  # Should this be passed in for future support?

    # Image Segmentation:
    image_segmentation = ImageSegmentation(name="image_segmentation")

    if 'two_photon_imaging' not in nwbfile.modules:
        two_photon_imaging_module = ProcessingModule('two_photon_imaging', '2P processing module')
        nwbfile.add_processing_module(two_photon_imaging_module)
    else:
        two_photon_imaging_module = nwbfile.modules['two_photon_imaging']

    two_photon_imaging_module.add_data_interface(image_segmentation)

    # Plane Segmentation:
    plane_segmentation = image_segmentation.create_plane_segmentation(
        name='cell_specimen_table',
        description="Segmented rois",
        imaging_plane=imaging_plane)

    for col_name in cell_roi_table.columns:
        # the columns 'image_mask', 'pixel_mask', and 'voxel_mask' are already defined
        # in the nwb.ophys::PlaneSegmentation Object
        if col_name not in ['id', 'mask_matrix', 'image_mask', 'pixel_mask', 'voxel_mask']:
            # This builds the columns with name of column and description of column
            # both equal to the column name in the cell_roi_table
            plane_segmentation.add_column(col_name,
                                          CELL_SPECIMEN_COL_DESCRIPTIONS.get(col_name,
                                                                             "No Description Available"))

    # go through each roi and add it to the plan segmentation object
    for cell_roi_id, row in cell_roi_table.iterrows():
        sub_mask = np.array(row.pop('image_mask'))
        curr_roi = roi.create_roi_mask(fov_width, fov_height, [(fov_width - 1), 0, (fov_height - 1), 0],
                                       roi_mask=sub_mask)
        mask = curr_roi.get_mask_plane()
        csid = row.pop('cell_specimen_id')
        row['cell_specimen_id'] = -1 if csid is None else csid
        row['id'] = cell_roi_id
        plane_segmentation.add_roi(image_mask=mask, **row.to_dict())

    return nwbfile
Esempio n. 12
0
    source='Allen Brain Observatory: Visual Coding',
    description='Processing module for 2P calcium responses',
)

########################################
# 6) First, we add an image segmentation interface to the module.  This interface implements a pre-defined schema and
# API that facilitates writing segmentation masks for ROI's:

image_segmentation_interface = ImageSegmentation(
    name='image_segmentation', source='Allen Brain Observatory: Visual Coding')

ophys_module.add_data_interface(image_segmentation_interface)

plane_segmentation = image_segmentation_interface.create_plane_segmentation(
    name='plane_segmentation',
    source='NA',
    description='Segmentation for imaging plane',
    imaging_plane=imaging_plane)

for cell_specimen_id in cell_specimen_ids:
    curr_name = str(cell_specimen_id)
    curr_image_mask = dataset.get_roi_mask_array([cell_specimen_id])[0]
    plane_segmentation.add_roi(curr_name, [], curr_image_mask)

########################################
# 7) Next, we add a dF/F  interface to the module.  This allows us to write the dF/F timeseries data associated with
# each ROI.

dff_interface = DfOverF(name='dff_interface',
                        source='Flourescence data container')
ophys_module.add_data_interface(dff_interface)
Esempio n. 13
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')
Esempio n. 14
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)
Esempio n. 15
0
    def setUp(self):

        nwbfile = NWBFile(
            'my first synthetic recording',
            'EXAMPLE_ID',
            datetime.now(tzlocal()),
            experimenter='Dr. Bilbo Baggins',
            lab='Bag End Laboratory',
            institution='University of Middle Earth at the Shire',
            experiment_description=('I went on an adventure with thirteen '
                                    'dwarves to reclaim vast treasures.'),
            session_id='LONELYMTN')

        device = Device('imaging_device_1')
        nwbfile.add_device(device)
        optical_channel = OpticalChannel('my_optchan', 'description', 500.)
        imaging_plane = nwbfile.create_imaging_plane(
            'my_imgpln', optical_channel,
            'a very interesting part of the brain',
            device, 600., 300., 'GFP', 'my favorite brain location',
            np.ones((5, 5, 3)), 4.0, 'manifold unit', 'A frame to refer to')

        self.image_series = TwoPhotonSeries(name='test_iS',
                                            dimension=[2],
                                            data=np.random.rand(10, 5, 5, 3),
                                            external_file=['images.tiff'],
                                            imaging_plane=imaging_plane,
                                            starting_frame=[0],
                                            format='tiff',
                                            starting_time=0.0,
                                            rate=1.0)
        nwbfile.add_acquisition(self.image_series)

        mod = nwbfile.create_processing_module(
            'ophys', 'contains optical physiology processed data')
        img_seg = ImageSegmentation()
        mod.add(img_seg)
        ps = img_seg.create_plane_segmentation(
            'output from segmenting my favorite imaging plane', imaging_plane,
            'my_planeseg', self.image_series)

        w, h = 3, 3
        pix_mask1 = [(0, 0, 1.1), (1, 1, 1.2), (2, 2, 1.3)]
        vox_mask1 = [(0, 0, 0, 1.1), (1, 1, 1, 1.2), (2, 2, 2, 1.3)]
        img_mask1 = [[0.0 for x in range(w)] for y in range(h)]
        img_mask1[0][0] = 1.1
        img_mask1[1][1] = 1.2
        img_mask1[2][2] = 1.3
        ps.add_roi(pixel_mask=pix_mask1,
                   image_mask=img_mask1,
                   voxel_mask=vox_mask1)

        pix_mask2 = [(0, 0, 2.1), (1, 1, 2.2)]
        vox_mask2 = [(0, 0, 0, 2.1), (1, 1, 1, 2.2)]
        img_mask2 = [[0.0 for x in range(w)] for y in range(h)]
        img_mask2[0][0] = 2.1
        img_mask2[1][1] = 2.2
        ps.add_roi(pixel_mask=pix_mask2,
                   image_mask=img_mask2,
                   voxel_mask=vox_mask2)

        fl = Fluorescence()
        mod.add(fl)

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

        data = np.array([0., 1., 2., 3., 4., 5., 6., 7., 8.,
                         9.]).reshape(10, 1)
        timestamps = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
        rrs = fl.create_roi_response_series('my_rrs',
                                            data,
                                            rt_region,
                                            unit='lumens',
                                            timestamps=timestamps)

        self.df_over_f = DfOverF(rrs)
Esempio n. 16
0
    def convert(self, **kwargs):
        """Convert the data and add to the nwb_file

        Args:
            **kwargs: arbitrary arguments
        """

        super().convert(**kwargs)
        if "suite2p_dir" not in kwargs:
            raise Exception(
                f"'suite2p_dir' argument should be pass to convert "
                f"function in class {self.__class__.__name__}")
        suite2p_dir = kwargs["suite2p_dir"]

        # looking for the motion_corrected_ci_movie, return None if it doesn't exists
        # TODO: take in consideration the movie is not available
        #  then don't construct image mask and don't build raw-traces, use F.npy is available
        image_series = self.nwb_file.acquisition.get(
            "motion_corrected_ci_movie")

        mod = self.nwb_file.create_processing_module(
            'ophys', 'contains optical physiology processed data')
        img_seg = ImageSegmentation(name="segmentation_suite2p")
        mod.add_data_interface(img_seg)
        imaging_plane = self.nwb_file.get_imaging_plane("my_imgpln")
        # description, imaging_plane, name=None
        ps = img_seg.create_plane_segmentation(
            description='output from segmenting',
            imaging_plane=imaging_plane,
            name='my_plane_seg',
            reference_images=image_series)

        stat = np.load(os.path.join(suite2p_dir, "stat.npy"),
                       allow_pickle=True)
        is_cell = np.load(os.path.join(suite2p_dir, "iscell.npy"),
                          allow_pickle=True)
        # TODO: load f.npy for raw_traces if available

        if image_series.format == "tiff":
            dim_y, dim_x = image_series.data.shape[1:]
            n_frames = image_series.data.shape[0]
            print(f"dim_y, dim_x: {image_series.data.shape[1:]}")
        elif image_series.format == "external":
            im = PIL.Image.open(image_series.external_file[0])
            n_frames = len(list(ImageSequence.Iterator(im)))
            dim_y, dim_x = np.array(im).shape
            print(f"dim_y, dim_x: {np.array(im).shape}")
        else:
            raise Exception(
                f"Format of calcium movie imaging {image_series.format} not yet implemented"
            )

        n_cells = 0
        # Add rois
        for cell in np.arange(len(stat)):
            if is_cell[cell][0] == 0:
                continue
            n_cells += 1
            pix_mask = [(y, x, 1)
                        for x, y in zip(stat[cell]["xpix"], stat[cell]["ypix"])
                        ]
            image_mask = np.zeros((dim_y, dim_x))
            for pix in pix_mask:
                image_mask[pix[0], pix[1]] = pix[2]
            # we can id to identify the cell (int) otherwise it will be incremented at each step
            ps.add_roi(pixel_mask=pix_mask, image_mask=image_mask)

        fl = Fluorescence(name="fluorescence_suite2p")
        mod.add_data_interface(fl)

        rt_region = ps.create_roi_table_region('all cells',
                                               region=list(np.arange(n_cells)))
        if format == "external":
            if image_series.external_file[0].endswith(".tiff") or \
                    image_series.external_file[0].endswith(".tif"):
                # TODO: fix this bug, so far external loading, taking in consideration frames_to_add is not possible
                #  either copy the code from ConvertCiMovieToNWB reconstructing frames_to_add from intervals
                #  or find another solution. Another solution would be to put on the yaml as argument
                #  frames_to_add but using the attribute from the ConvertMovie instance. s
                ci_movie = ConvertCiMovieToNWB.load_tiff_movie_in_memory(
                    image_series.external_file[0])
            else:
                raise Exception(
                    f"Calcium imaging format not supported yet {image_series.external_file[0]}"
                )
        else:
            ci_movie = image_series.data
        # TODO: if movie is external, see to load it
        if ci_movie:
            raw_traces = np.zeros((n_cells, ci_movie.shape[0]))
            for cell in np.arange(n_cells):
                img_mask = ps['image_mask'][cell]
                img_mask = img_mask.astype(bool)
                raw_traces[cell, :] = np.mean(ci_movie[:, img_mask], axis=1)
            rrs = fl.create_roi_response_series(name='raw_traces',
                                                data=raw_traces,
                                                unit='lumens',
                                                rois=rt_region,
                                                timestamps=np.arange(n_frames),
                                                description="raw traces")
Esempio n. 17
0
    def to_nwb(self, nwbfile: NWBFile,
               ophys_timestamps: OphysTimestamps) -> NWBFile:
        """
        :param nwbfile
            In-memory nwb file object
        :param ophys_timestamps
            ophys timestamps
        """
        # 1. Add cell specimen table
        cell_roi_table = self.table.reset_index().set_index('cell_roi_id')
        metadata = nwbfile.lab_meta_data['metadata']

        device = nwbfile.get_device()

        # FOV:
        fov_width = metadata.field_of_view_width
        fov_height = metadata.field_of_view_height
        imaging_plane_description = \
            "{} field of view in {} at depth {} " \
            "um".format(
                (fov_width, fov_height),
                self._meta.imaging_plane.targeted_structure,
                metadata.imaging_depth)

        # Optical Channel:
        optical_channel = OpticalChannel(
            name='channel_1',
            description='2P Optical Channel',
            emission_lambda=self._meta.emission_lambda)

        # Imaging Plane:
        imaging_plane = nwbfile.create_imaging_plane(
            name='imaging_plane_1',
            optical_channel=optical_channel,
            description=imaging_plane_description,
            device=device,
            excitation_lambda=self._meta.imaging_plane.excitation_lambda,
            imaging_rate=self._meta.imaging_plane.ophys_frame_rate,
            indicator=self._meta.imaging_plane.indicator,
            location=self._meta.imaging_plane.targeted_structure)

        # Image Segmentation:
        image_segmentation = ImageSegmentation(name="image_segmentation")

        if 'ophys' not in nwbfile.processing:
            ophys_module = ProcessingModule('ophys', 'Ophys processing module')
            nwbfile.add_processing_module(ophys_module)
        else:
            ophys_module = nwbfile.processing['ophys']

        ophys_module.add_data_interface(image_segmentation)

        # Plane Segmentation:
        plane_segmentation = image_segmentation.create_plane_segmentation(
            name='cell_specimen_table',
            description="Segmented rois",
            imaging_plane=imaging_plane)

        for col_name in cell_roi_table.columns:
            # the columns 'roi_mask', 'pixel_mask', and 'voxel_mask' are
            # already defined in the nwb.ophys::PlaneSegmentation Object
            if col_name not in [
                    'id', 'mask_matrix', 'roi_mask', 'pixel_mask', 'voxel_mask'
            ]:
                # This builds the columns with name of column and description
                # of column both equal to the column name in the cell_roi_table
                plane_segmentation.add_column(
                    col_name,
                    CELL_SPECIMEN_COL_DESCRIPTIONS.get(
                        col_name, "No Description Available"))

        # go through each roi and add it to the plan segmentation object
        for cell_roi_id, table_row in cell_roi_table.iterrows():
            # NOTE: The 'roi_mask' in this cell_roi_table has already been
            # processing by the function from
            # allensdk.brain_observatory.behavior.session_apis.data_io
            # .ophys_lims_api
            # get_cell_specimen_table() method. As a result, the ROI is
            # stored in
            # an array that is the same shape as the FULL field of view of the
            # experiment (e.g. 512 x 512).
            mask = table_row.pop('roi_mask')

            csid = table_row.pop('cell_specimen_id')
            table_row['cell_specimen_id'] = -1 if csid is None else csid
            table_row['id'] = cell_roi_id
            plane_segmentation.add_roi(image_mask=mask, **table_row.to_dict())

        # 2. Add DFF traces
        self._dff_traces.to_nwb(nwbfile=nwbfile,
                                ophys_timestamps=ophys_timestamps)

        # 3. Add Corrected fluorescence traces
        self._corrected_fluorescence_traces.to_nwb(nwbfile=nwbfile)

        # 4. Add events
        self._events.to_nwb(nwbfile=nwbfile)

        # 5. Add segmentation mask image
        add_image_to_nwb(nwbfile=nwbfile,
                         image_data=self._segmentation_mask_image,
                         image_name='segmentation_mask_image')

        return nwbfile
Esempio n. 18
0
    def write_segmentation(
        segext_obj: SegmentationExtractor,
        save_path: PathType = None,
        plane_num=0,
        metadata: dict = None,
        overwrite: bool = True,
        buffer_size: int = 10,
        nwbfile=None,
    ):
        assert (
            save_path is None or nwbfile is None
        ), "Either pass a save_path location, or nwbfile object, but not both!"

        # parse metadata correctly:
        if isinstance(segext_obj, MultiSegmentationExtractor):
            segext_objs = segext_obj.segmentations
            if metadata is not None:
                assert isinstance(metadata, list), (
                    "For MultiSegmentationExtractor enter 'metadata' as a list of "
                    "SegmentationExtractor metadata")
                assert len(metadata) == len(segext_objs), (
                    "The 'metadata' argument should be a list with the same "
                    "number of elements as the segmentations in the "
                    "MultiSegmentationExtractor")
        else:
            segext_objs = [segext_obj]
            if metadata is not None and not isinstance(metadata, list):
                metadata = [metadata]
        metadata_base_list = [
            NwbSegmentationExtractor.get_nwb_metadata(sgobj)
            for sgobj in segext_objs
        ]
        print(f"writing nwb for {segext_obj.extractor_name}\n")
        # updating base metadata with new:
        for num, data in enumerate(metadata_base_list):
            metadata_input = metadata[num] if metadata else {}
            metadata_base_list[num] = dict_recursive_update(
                metadata_base_list[num], metadata_input)
        metadata_base_common = metadata_base_list[0]

        # build/retrieve nwbfile:
        if nwbfile is not None:
            assert isinstance(
                nwbfile, NWBFile), "'nwbfile' should be of type pynwb.NWBFile"
            write = False
        else:
            write = True
            save_path = Path(save_path)
            assert save_path.suffix == ".nwb"
            if save_path.is_file() and not overwrite:
                nwbfile_exist = True
                file_mode = "r+"
            else:
                if save_path.is_file():
                    os.remove(save_path)
                if not save_path.parent.is_dir():
                    save_path.parent.mkdir(parents=True)
                nwbfile_exist = False
                file_mode = "w"
            io = NWBHDF5IO(str(save_path), file_mode)
            if nwbfile_exist:
                nwbfile = io.read()
            else:
                nwbfile = NWBFile(**metadata_base_common["NWBFile"])

        # Subject:
        if metadata_base_common.get("Subject") and nwbfile.subject is None:
            nwbfile.subject = Subject(**metadata_base_common["Subject"])

        # Processing Module:
        if "ophys" not in nwbfile.processing:
            ophys = nwbfile.create_processing_module(
                "ophys", "contains optical physiology processed data")
        else:
            ophys = nwbfile.get_processing_module("ophys")

        for plane_no_loop, (segext_obj, metadata) in enumerate(
                zip(segext_objs, metadata_base_list)):
            # Device:
            if metadata["Ophys"]["Device"][0]["name"] not in nwbfile.devices:
                nwbfile.create_device(**metadata["Ophys"]["Device"][0])

            # ImageSegmentation:
            image_segmentation_name = (
                "ImageSegmentation" if plane_no_loop == 0 else
                f"ImageSegmentation_Plane{plane_no_loop}")
            if image_segmentation_name not in ophys.data_interfaces:
                image_segmentation = ImageSegmentation(
                    name=image_segmentation_name)
                ophys.add(image_segmentation)
            else:
                image_segmentation = ophys.data_interfaces.get(
                    image_segmentation_name)

            # OpticalChannel:
            optical_channels = [
                OpticalChannel(**i) for i in metadata["Ophys"]["ImagingPlane"]
                [0]["optical_channel"]
            ]

            # ImagingPlane:
            image_plane_name = ("ImagingPlane" if plane_no_loop == 0 else
                                f"ImagePlane_{plane_no_loop}")
            if image_plane_name not in nwbfile.imaging_planes.keys():
                input_kwargs = dict(
                    name=image_plane_name,
                    device=nwbfile.get_device(
                        metadata_base_common["Ophys"]["Device"][0]["name"]),
                )
                metadata["Ophys"]["ImagingPlane"][0][
                    "optical_channel"] = optical_channels
                input_kwargs.update(**metadata["Ophys"]["ImagingPlane"][0])
                if "imaging_rate" in input_kwargs:
                    input_kwargs["imaging_rate"] = float(
                        input_kwargs["imaging_rate"])
                imaging_plane = nwbfile.create_imaging_plane(**input_kwargs)
            else:
                imaging_plane = nwbfile.imaging_planes[image_plane_name]

            # PlaneSegmentation:
            input_kwargs = dict(
                description="output from segmenting imaging plane",
                imaging_plane=imaging_plane,
            )
            ps_metadata = metadata["Ophys"]["ImageSegmentation"][
                "plane_segmentations"][0]
            if ps_metadata[
                    "name"] not in image_segmentation.plane_segmentations:
                ps_exist = False
            else:
                ps = image_segmentation.get_plane_segmentation(
                    ps_metadata["name"])
                ps_exist = True

            roi_ids = segext_obj.get_roi_ids()
            accepted_list = segext_obj.get_accepted_list()
            accepted_list = [] if accepted_list is None else accepted_list
            rejected_list = segext_obj.get_rejected_list()
            rejected_list = [] if rejected_list is None else rejected_list
            accepted_ids = [1 if k in accepted_list else 0 for k in roi_ids]
            rejected_ids = [1 if k in rejected_list else 0 for k in roi_ids]
            roi_locations = np.array(segext_obj.get_roi_locations()).T

            def image_mask_iterator():
                for id in segext_obj.get_roi_ids():
                    img_msks = segext_obj.get_roi_image_masks(
                        roi_ids=[id]).T.squeeze()
                    yield img_msks

            if not ps_exist:
                input_kwargs.update(
                    **ps_metadata,
                    columns=[
                        VectorData(
                            data=H5DataIO(
                                DataChunkIterator(image_mask_iterator(),
                                                  buffer_size=buffer_size),
                                compression=True,
                                compression_opts=9,
                            ),
                            name="image_mask",
                            description="image masks",
                        ),
                        VectorData(
                            data=roi_locations,
                            name="RoiCentroid",
                            description=
                            "x,y location of centroid of the roi in image_mask",
                        ),
                        VectorData(
                            data=accepted_ids,
                            name="Accepted",
                            description=
                            "1 if ROi was accepted or 0 if rejected as a cell during segmentation operation",
                        ),
                        VectorData(
                            data=rejected_ids,
                            name="Rejected",
                            description=
                            "1 if ROi was rejected or 0 if accepted as a cell during segmentation operation",
                        ),
                    ],
                    id=roi_ids,
                )

                ps = image_segmentation.create_plane_segmentation(
                    **input_kwargs)

            # Fluorescence Traces:
            if "Flourescence" not in ophys.data_interfaces:
                fluorescence = Fluorescence()
                ophys.add(fluorescence)
            else:
                fluorescence = ophys.data_interfaces["Fluorescence"]
            roi_response_dict = segext_obj.get_traces_dict()
            roi_table_region = ps.create_roi_table_region(
                description=f"region for Imaging plane{plane_no_loop}",
                region=list(range(segext_obj.get_num_rois())),
            )
            rate = (np.float("NaN")
                    if segext_obj.get_sampling_frequency() is None else
                    segext_obj.get_sampling_frequency())
            for i, j in roi_response_dict.items():
                data = getattr(segext_obj, f"_roi_response_{i}")
                if data is not None:
                    data = np.asarray(data)
                    trace_name = "RoiResponseSeries" if i == "raw" else i.capitalize(
                    )
                    trace_name = (trace_name if plane_no_loop == 0 else
                                  trace_name + f"_Plane{plane_no_loop}")
                    input_kwargs = dict(
                        name=trace_name,
                        data=data.T,
                        rois=roi_table_region,
                        rate=rate,
                        unit="n.a.",
                    )
                    if trace_name not in fluorescence.roi_response_series:
                        fluorescence.create_roi_response_series(**input_kwargs)

            # create Two Photon Series:
            if "TwoPhotonSeries" not in nwbfile.acquisition:
                warn(
                    "could not find TwoPhotonSeries, using ImagingExtractor to create an nwbfile"
                )

            # adding images:
            images_dict = segext_obj.get_images_dict()
            if any([image is not None for image in images_dict.values()]):
                images_name = ("SegmentationImages" if plane_no_loop == 0 else
                               f"SegmentationImages_Plane{plane_no_loop}")
                if images_name not in ophys.data_interfaces:
                    images = Images(images_name)
                    for img_name, img_no in images_dict.items():
                        if img_no is not None:
                            images.add_image(
                                GrayscaleImage(name=img_name, data=img_no.T))
                    ophys.add(images)

            # saving NWB file:
            if write:
                io.write(nwbfile)
                io.close()
                # test read
                with NWBHDF5IO(str(save_path), "r") as io:
                    io.read()
Esempio n. 19
0
def add_cell_specimen_table(nwbfile, cell_specimen_table):
    cell_roi_table = cell_specimen_table.reset_index().set_index('cell_roi_id')

    # Device:
    device_name = nwbfile.lab_meta_data['metadata'].rig_name
    nwbfile.create_device(device_name, "Allen Brain Observatory")
    device = nwbfile.get_device(device_name)

    # Location:
    location_description = "Area: {}, Depth: {} um".format(
        nwbfile.lab_meta_data['metadata'].targeted_structure,
        nwbfile.lab_meta_data['metadata'].imaging_depth)

    # FOV:
    fov_width = nwbfile.lab_meta_data['metadata'].field_of_view_width
    fov_height = nwbfile.lab_meta_data['metadata'].field_of_view_height
    imaging_plane_description = "{} field of view in {} at depth {} um".format(
        (fov_width, fov_height),
        nwbfile.lab_meta_data['metadata'].targeted_structure,
        nwbfile.lab_meta_data['metadata'].imaging_depth)

    # Optical Channel:
    optical_channel = OpticalChannel(
        name='channel_1',
        description='2P Optical Channel',
        emission_lambda=nwbfile.lab_meta_data['metadata'].emission_lambda)

    # Imaging Plane:
    imaging_plane = nwbfile.create_imaging_plane(
        name='imaging_plane_1',
        optical_channel=optical_channel,
        description=imaging_plane_description,
        device=device,
        excitation_lambda=nwbfile.lab_meta_data['metadata'].excitation_lambda,
        imaging_rate=nwbfile.lab_meta_data['metadata'].ophys_frame_rate,
        indicator=nwbfile.lab_meta_data['metadata'].indicator,
        location=location_description,
        manifold=[],  # Should this be passed in for future support?
        conversion=1.0,
        unit='unknown',  # Should this be passed in for future support?
        reference_frame='unknown'
    )  # Should this be passed in for future support?

    # Image Segmentation:
    image_segmentation = ImageSegmentation(name="image_segmentation")

    if 'two_photon_imaging' not in nwbfile.modules:
        two_photon_imaging_module = ProcessingModule('two_photon_imaging',
                                                     '2P processing module')
        nwbfile.add_processing_module(two_photon_imaging_module)
    else:
        two_photon_imaging_module = nwbfile.modules['two_photon_imaging']

    two_photon_imaging_module.add_data_interface(image_segmentation)

    # Plane Segmentation:
    plane_segmentation = image_segmentation.create_plane_segmentation(
        name='cell_specimen_table',
        description="Segmented rois",
        imaging_plane=imaging_plane)

    for c in [
            c for c in cell_roi_table.columns
            if c not in ['id', 'mask_matrix']
    ]:
        plane_segmentation.add_column(c, c)

    for cell_roi_id, row in cell_roi_table.iterrows():
        sub_mask = np.array(row.pop('image_mask'))
        curr_roi = roi.create_roi_mask(fov_width,
                                       fov_height, [(fov_width - 1), 0,
                                                    (fov_height - 1), 0],
                                       roi_mask=sub_mask)
        mask = curr_roi.get_mask_plane()
        csid = row.pop('cell_specimen_id')
        row['cell_specimen_id'] = -1 if csid is None else csid
        row['id'] = cell_roi_id
        plane_segmentation.add_roi(image_mask=mask, **row.to_dict())

    return nwbfile
Esempio n. 20
0
nwbfile.add_acquisition(image_series)

####################
# Storing image segmentation output
# ---------------------------------
#
# Now that the raw data is stored, you can add the image segmentation results. This is done with the
# :py:class:`~pynwb.ophys.ImageSegmentation` data interface. This class has the ability to store segmentation
# from one or more imaging planes; hence the :py:class:`~pynwb.ophys.PlaneSegmentation` class.

mod = nwbfile.create_processing_module('my_ca_imaging_module',
                                       'example data module')
img_seg = ImageSegmentation()
mod.add_data_interface(img_seg)
ps = img_seg.create_plane_segmentation(
    'output from segmenting my favorite imaging plane', imaging_plane,
    'my_planeseg', image_series)

####################
# Now that you have your :py:class:`~pynwb.ophys.PlaneSegmentation` object, you can add the resulting ROIs.
# This is done using the method :py:func:`~pynwb.ophys.PlaneSegmentation.add_roi`. The first argument to this
# method is the `pixel_mask` and the second method is the `image_mask`. Both of these arguments are required
# for schema compliance--the NWB schema requires that you store both the image mask and the pixel mask.

w, h = 3, 3
pix_mask1 = [(0, 0, 1.1), (1, 1, 1.2), (2, 2, 1.3)]
img_mask1 = [[0.0 for x in range(w)] for y in range(h)]
img_mask1[0][0] = 1.1
img_mask1[1][1] = 1.2
img_mask1[2][2] = 1.3
ps.add_roi(pixel_mask=pix_mask1, image_mask=img_mask1)
Esempio n. 21
0
class CalciumImagingTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        device = Device("imaging_device_1")
        optical_channel = OpticalChannel("my_optchan", "description", 500.0)
        self.imaging_plane = ImagingPlane(
            name="imgpln1",
            optical_channel=optical_channel,
            description="a fake ImagingPlane",
            device=device,
            excitation_lambda=600.0,
            imaging_rate=300.0,
            indicator="GFP",
            location="somewhere in the brain",
            reference_frame="unknown",
            origin_coords=[10, 20],
            origin_coords_unit="millimeters",
            grid_spacing=[0.001, 0.001],
            grid_spacing_unit="millimeters",
        )

        self.image_series = TwoPhotonSeries(
            name="test_image_series",
            data=np.random.randn(100, 5, 5),
            imaging_plane=self.imaging_plane,
            starting_frame=[0],
            rate=1.0,
            unit="n.a",
        )
        self.img_seg = ImageSegmentation()
        self.ps2 = self.img_seg.create_plane_segmentation(
            "output from segmenting my favorite imaging plane",
            self.imaging_plane,
            "2d_plane_seg",
            self.image_series,
        )
        self.ps2.add_column("type", "desc")
        self.ps2.add_column("type2", "desc")

        w, h = 3, 3
        img_mask1 = np.zeros((w, h))
        img_mask1[0, 0] = 1.1
        img_mask1[1, 1] = 1.2
        img_mask1[2, 2] = 1.3
        self.ps2.add_roi(image_mask=img_mask1, type=1, type2=0)

        img_mask2 = np.zeros((w, h))
        img_mask2[0, 0] = 2.1
        img_mask2[1, 1] = 2.2
        self.ps2.add_roi(image_mask=img_mask2, type=1, type2=1)

        img_mask2 = np.zeros((w, h))
        img_mask2[0, 0] = 9.1
        img_mask2[1, 1] = 10.2
        self.ps2.add_roi(image_mask=img_mask2, type=2, type2=0)

        img_mask2 = np.zeros((w, h))
        img_mask2[0, 0] = 3.5
        img_mask2[1, 1] = 5.6
        self.ps2.add_roi(image_mask=img_mask2, type=2, type2=1)

        fl = Fluorescence()
        rt_region = self.ps2.create_roi_table_region("the first of two ROIs",
                                                     region=[0, 1, 2, 3])

        rois_shape = 5
        data = np.arange(10 * rois_shape).reshape([10, -1], order='F')
        timestamps = np.array(
            [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
        rrs = fl.create_roi_response_series(name="my_rrs",
                                            data=data,
                                            rois=rt_region,
                                            unit="lumens",
                                            timestamps=timestamps)
        self.df_over_f = DfOverF(rrs)

    def test_show_two_photon_series(self):
        wid = TwoPhotonSeriesWidget(self.image_series,
                                    default_neurodata_vis_spec)
        assert isinstance(wid, widgets.Widget)
        wid.controls['slider'].value = 50

    def test_show_3d_two_photon_series(self):
        image_series3 = TwoPhotonSeries(
            name="test_3d_images",
            data=np.random.randn(100, 5, 5, 5),
            imaging_plane=self.imaging_plane,
            starting_frame=[0],
            rate=1.0,
            unit="n.a",
        )
        wid = TwoPhotonSeriesWidget(image_series3, default_neurodata_vis_spec)
        assert isinstance(wid, widgets.Widget)
        wid.controls['slider'].value = 50

    def test_show_df_over_f(self):
        dff = show_df_over_f(self.df_over_f, default_neurodata_vis_spec)
        assert isinstance(dff, widgets.Widget)
        dff.controls['gas'].window = [1, 2]

    def test_plane_segmentation_2d_widget(self):
        wid = PlaneSegmentation2DWidget(self.ps2)
        assert isinstance(wid, widgets.Widget)
        wid.button.click()
        wid.cat_controller.value = "type"
        wid.cat_controller.value = "type2"

    def test_show_plane_segmentation_3d_mask(self):
        ps3 = PlaneSegmentation(
            "output from segmenting my favorite imaging plane",
            self.imaging_plane,
            "3d_plane_seg",
            self.image_series,
        )

        w, h, d = 3, 3, 3
        img_mask1 = np.zeros((w, h, d))
        for i in range(3):
            img_mask1[i, i, i] = 1.0
        ps3.add_roi(image_mask=img_mask1)

        img_mask2 = np.zeros((w, h, d))
        for i in range(3):
            img_mask2[i, i, i] = 1.2
        ps3.add_roi(image_mask=img_mask2)
        wid = show_plane_segmentation_3d_mask(ps3)
        assert isinstance(wid, widgets.Widget)

    def test_show_plane_segmentation_3d_voxel(self):
        ps3v = PlaneSegmentation(
            "output from segmenting my favorite imaging plane",
            self.imaging_plane,
            "3d_voxel",
            self.image_series,
        )

        voxel_mask = [(i, i, i, 1.0) for i in range(3)]
        ps3v.add_roi(voxel_mask=voxel_mask)

        voxel_mask = [(1, 1, i, 1.2) for i in range(3)]
        ps3v.add_roi(voxel_mask=voxel_mask)
        wid = show_plane_segmentation_3d_voxel(ps3v)
        assert isinstance(wid, widgets.Widget)

    def test_show_image_segmentation(self):
        assert isinstance(
            show_image_segmentation(self.img_seg, default_neurodata_vis_spec),
            widgets.Widget,
        )
Esempio n. 22
0
images_path = os.path.join(base_dir, 'm655_D11_S1.hdf5')
with File(images_path, 'r') as file:
    image_series = TwoPhotonSeries(name='test_iS',
                                   dimension=[2],
                                   data=file['Data']['Images'][:],
                                   imaging_plane=imaging_plane,
                                   starting_frame=[0],
                                   starting_time=0.0,
                                   rate=5.0,
                                   scan_line_rate=np.nan,
                                   pmt_gain=np.nan)
nwbfile.add_acquisition(image_series)

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,
Esempio n. 23
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)