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')
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 addContainer(self, nwbfile): nwbfile.add_device(self.device) nwbfile.add_imaging_plane(self.imaging_plane) img_seg = ImageSegmentation() img_seg.add_plane_segmentation(self.container) mod = nwbfile.create_processing_module('plane_seg_test_module', 'a plain module for testing') mod.add(img_seg)
def addContainer(self, nwbfile): nwbfile.add_imaging_plane(self.imaging_plane) img_seg = ImageSegmentation('plane segmentation round trip') img_seg.add_plane_segmentation(self.container) mod = nwbfile.create_processing_module( 'plane_seg_test_module', 'plane segmentation round trip', 'a plain module for testing') mod.add_data_interface(img_seg)
def addContainer(self, nwbfile): nwbfile.add_device(self.device) nwbfile.add_imaging_plane(self.imaging_plane) img_seg = ImageSegmentation() img_seg.add_plane_segmentation(self.plane_segmentation) mod = nwbfile.create_processing_module('plane_seg_test_module', 'a plain module for testing') mod.add(img_seg) super(TestRoiResponseSeriesIO, self).addContainer(nwbfile)
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')
def addContainer(self, nwbfile): """ Add an ImageSegmentation in processing with a PlaneSegmentation and add Device and ImagingPlane to the given NWBFile """ nwbfile.add_device(self.device) nwbfile.add_imaging_plane(self.imaging_plane) img_seg = ImageSegmentation() img_seg.add_plane_segmentation(self.container) self.mod = nwbfile.create_processing_module( 'plane_seg_test_module', 'a plain module for testing') self.mod.add(img_seg)
def addContainer(self, nwbfile): """ Add the test RoiResponseSeries as an acquisition and add Device, ImagingPlane, ImageSegmentation, and PlaneSegmentation to the given NWBFile """ nwbfile.add_device(self.device) nwbfile.add_imaging_plane(self.imaging_plane) img_seg = ImageSegmentation() img_seg.add_plane_segmentation(self.plane_segmentation) mod = nwbfile.create_processing_module('plane_seg_test_module', 'a plain module for testing') mod.add(img_seg) super().addContainer(nwbfile)
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
def test_init(self): ps = CreatePlaneSegmentation() iS = ImageSegmentation(ps, name='test_iS') self.assertEqual(iS.name, 'test_iS') self.assertEqual(iS.plane_segmentations[ps.name], ps) self.assertEqual(iS[ps.name], iS.plane_segmentations[ps.name])
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)
def test_init(self): w, h = 5, 5 img_mask = [[0 for x in range(w)] for y in range(h)] w, h = 5, 2 pix_mask = [[0 for x in range(w)] for y in range(h)] pix_mask_weight = [0 for x in range(w)] iSS = ImageSeries(name='test_iS', source='a hypothetical source', data=list(), unit='unit', external_file=['external_file'], starting_frame=[1, 2, 3], format='tiff', timestamps=list()) roi1 = ROI('roi1', 'test source', 'roi description1', pix_mask, pix_mask_weight, img_mask, iSS) roi2 = ROI('roi2', 'test source', 'roi description2', pix_mask, pix_mask_weight, img_mask, iSS) roi_list = (roi1, roi2) oc = OpticalChannel('test_optical_channel', 'test source', 'description', 'emission_lambda') ip = ImagingPlane('test_imaging_plane', 'test source', oc, 'description', 'device', 'excitation_lambda', 'imaging_rate', 'indicator', 'location', (1, 2, 1, 2, 3), 4.0, 'unit', 'reference_frame') ps = PlaneSegmentation('name', 'test source', 'description', roi_list, ip, iSS) iS = ImageSegmentation('test_source', ps, name='test_iS') self.assertEqual(iS.name, 'test_iS') self.assertEqual(iS.source, 'test_source') self.assertEqual(iS.plane_segmentations, [ps])
def cicada_create_image_segmentation(self): """ class pynwb.ophys.ImageSegmentation(plane_segmentations={}, name='ImageSegmentation') """ # Nom du module où récupérer les infos de métadonnée name_module = "image_segmentation_" plane_segmentations = {} self.image_segmentation = ImageSegmentation( plane_segmentations=plane_segmentations, name="ImageSegmentation") self.mod.add_data_interface(self.image_segmentation)
def test_init(self): ip = CreatePlaneSegmentation() iS = ImageSegmentation('test source', ip, name='test_iS') ts = RoiResponseSeries('test_ts', 'a hypothetical source', list(), 'unit', ['name1'], iS, timestamps=list()) ff = Fluorescence('test_ff', ts) self.assertEqual(ff.source, 'test_ff') self.assertEqual(ff.roi_response_series, ts)
def test_init(self): ip = CreatePlaneSegmentation() iS = ImageSegmentation('test source', ip, name='test_iS') rrs = RoiResponseSeries('test_ts', 'a hypothetical source', list(), 'unit', ['name1'], iS, timestamps=list()) dof = DfOverF('test_dof', rrs) self.assertEqual(dof.source, 'test_dof') self.assertEqual(dof.roi_response_series, rrs)
def test_init(self): ip = CreatePlaneSegmentation() iS = ImageSegmentation('test source', ip, name='test_iS') ts = RoiResponseSeries('test_ts', 'a hypothetical source', list(), 'unit', ['name1'], iS, timestamps=list()) self.assertEqual(ts.name, 'test_ts') self.assertEqual(ts.source, 'a hypothetical source') self.assertEqual(ts.unit, 'unit') self.assertEqual(ts.roi_names, ['name1']) self.assertEqual(ts.segmentation_interface, iS)
format='tiff', starting_time=0.0, rate=1.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
# :align: center # # .. only:: latex # # .. 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
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
######################################## # The Allen Insitute does not include the raw imaging signal, as this data would make the file too large. Instead, these # data are preprocessed, and a dF/F flourescence signal extracted for each region-of-interest (ROI). To store the chain # of computations necessary to describe this data processing pipeline, pynwb provides a "processing module" with # interfaces that simplify and standarize the process of adding the steps in this provenance chain to the file: ophys_module = nwbfile.create_processing_module( name='ophys_module', 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') ophys_module.add_data_interface(image_segmentation_interface) plane_segmentation = image_segmentation_interface.create_plane_segmentation( name='plane_segmentation', description='Segmentation for imaging plane', imaging_plane=imaging_plane) for cell_specimen_id in cell_specimen_ids: curr_name = cell_specimen_id curr_image_mask = dataset.get_roi_mask_array([cell_specimen_id])[0] plane_segmentation.add_roi(id=curr_name, image_mask=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
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()
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')
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 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)
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')
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
######################################## # The Allen Insitute does not include the raw imaging signal, as this data would make the file too large. Instead, these # data are preprocessed, and a dF/F flourescence signal extracted for each region-of-interest (ROI). To store the chain # of computations necessary to describe this data processing pipeline, pynwb provides a "processing module" with # interfaces that simplify and standarize the process of adding the steps in this provenance chain to the file: ophys_module = nwbfile.create_processing_module( name='ophys_module', 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) ########################################
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, )
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
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?',