def get_roi_mask(self, cell_specimen_ids=None): ''' Returns an array of all the ROI masks Parameters ---------- cell specimen IDs: list or array (optional) List of cell IDs to return traces for. If this is None (default) then all are returned Returns ------- List of ROI_Mask objects ''' all_cell_specimen_ids = self.get_cell_specimen_ids() with h5py.File(self.nwb_file, 'r') as f: mask_loc = f['processing'][self.PIPELINE_DATASET]['ImageSegmentation']['imaging_plane_1'] roi_list = f['processing'][self.PIPELINE_DATASET]['ImageSegmentation']['imaging_plane_1']['roi_list'].value inds = None if cell_specimen_ids is None: inds = range(len(all_cell_specimen_ids)) else: inds = [ list(all_cell_specimen_ids).index(i) for i in cell_specimen_ids ] roi_array = [] for i in inds: v = roi_list[i] m = roi.create_roi_mask(self.MOVIE_FOV_PX[0], self.MOVIE_FOV_PX[1], [0,0,0,0], pix_list=mask_loc[v]["pix_mask"].value, label=v) roi_array.append(m) return roi_array
def create_mask_objects(masks, motion_border, roi_ids, union_threshold=0.7): """ create_mask_objects(masks, motion_border, roi_ids) Returns mask objects, labeled for overlapping the motion border, as well as for labels, duplicates and being empty. Required args: - masks (3D array) : ROI masks, structured as ROI x height x width - motion border (list): motion border values for [x0, x1, y1, y0] (right, left, down, up shifts) - roi_ids (list) : ID for each ROI Returns: - all_mask_objs (list) : list of ROI Mask objects, with exclusion labels (allensdk roi_masks.py) """ all_mask_objs = [] hei, wid = masks.shape[1:] for _, (mask, roi_id) in enumerate(zip(masks, roi_ids)): all_mask_objs.append(roi_masks.create_roi_mask( wid, hei, motion_border, roi_mask=mask, label=str(roi_id))) # add empty list of labels all_mask_objs[-1].labels = [] all_mask_objs = label_unions_and_duplicates(all_mask_objs, masks, union_threshold=0.7) return all_mask_objs
def get_roi_mask(self, cell_specimen_ids=None): ''' Returns an array of all the ROI masks Parameters ---------- cell specimen IDs: list or array (optional) List of cell IDs to return traces for. If this is None (default) then all are returned Returns ------- List of ROI_Mask objects ''' with h5py.File(self.nwb_file, 'r') as f: mask_loc = f['processing'][self.PIPELINE_DATASET][ 'ImageSegmentation']['imaging_plane_1'] roi_list = f['processing'][self.PIPELINE_DATASET][ 'ImageSegmentation']['imaging_plane_1']['roi_list'].value inds = None if cell_specimen_ids is None: inds = range(self.number_of_cells) else: inds = self.get_cell_specimen_indices(cell_specimen_ids) roi_array = [] for i in inds: v = roi_list[i] roi_mask = mask_loc[v]["img_mask"].value m = roi.create_roi_mask(roi_mask.shape[1], roi_mask.shape[0], [0, 0, 0, 0], roi_mask=roi_mask, label=v) roi_array.append(m) return roi_array
def test_create_neuropil_mask(): image_width = 100 image_height = 80 # border = [image_width-1, 0, image_height-1, 0] border = [5, 5, 5, 5] roi_mask = np.zeros((image_height, image_width), dtype=np.uint8) roi_mask[40:45, 30:35] = 1 combined_binary_mask = np.zeros((image_height, image_width), dtype=np.uint8) combined_binary_mask[:, 45:] = 1 roi = roi_masks.create_roi_mask(image_w=image_width, image_h=image_height, border=border, roi_mask=roi_mask) obtained = roi_masks.create_neuropil_mask(roi, border, combined_binary_mask) expected_mask = np.zeros((58 - 27, 45 - 17), dtype=np.uint8) expected_mask[:, :] = 1 assert np.allclose(expected_mask, obtained.mask) assert obtained.x == 17 assert obtained.y == 27 assert obtained.width == 28 assert obtained.height == 31
def get_roi_mask(self, cell_specimen_ids=None): ''' Returns an array of all the ROI masks Parameters ---------- cell specimen IDs: list or array (optional) List of cell IDs to return traces for. If this is None (default) then all are returned Returns ------- List of ROI_Mask objects ''' with h5py.File(self.nwb_file, 'r') as f: mask_loc = f['processing'][self.PIPELINE_DATASET][ 'ImageSegmentation']['imaging_plane_1'] roi_list = f['processing'][self.PIPELINE_DATASET][ 'ImageSegmentation']['imaging_plane_1']['roi_list'].value inds = None if cell_specimen_ids is None: inds = range(self.number_of_cells) else: inds = self.get_cell_specimen_indices(cell_specimen_ids) roi_array = [] for i in inds: v = roi_list[i] roi_mask = mask_loc[v]["img_mask"].value m = roi.create_roi_mask(roi_mask.shape[1], roi_mask.shape[0], [0, 0, 0, 0], roi_mask=roi_mask, label=v) roi_array.append(m) return roi_array
def test_init_by_pixels_large(): a = np.random.random((512, 512)) a[a > 0.5] = 1 m = roi_masks.create_roi_mask(512, 512, [0, 0, 0, 0], pix_list=np.argwhere(a)) npx = len(np.where(a)[0]) assert npx == len(np.where(m.get_mask_plane())[0])
def test_init_by_pixels_large(): a = np.random.random((512, 512)) a[a > 0.5] = 1 m = roi_masks.create_roi_mask( 512, 512, [0, 0, 0, 0], pix_list=np.argwhere(a)) npx = len(np.where(a)[0]) assert npx == len(np.where(m.get_mask_plane())[0])
def test_init_by_pixels_with_border(): a = np.array([[1, 1], [2, 1]]) m = roi_masks.create_roi_mask(3, 3, [1, 1, 1, 1], pix_list=a) assert m.x == 1 assert m.width == 2 assert m.y == 1 assert m.height == 1 assert m.valid is False
def test_init_by_pixels_with_border(): a = np.array([[1, 1], [2, 1]]) m = roi_masks.create_roi_mask(3, 3, [1, 1, 1, 1], pix_list=a) assert m.x == 1 assert m.width == 2 assert m.y == 1 assert m.height == 1 assert m.valid is False
def test_init_by_pixels_with_border(): a = np.array([[1, 1], [2, 1]]) m = roi_masks.create_roi_mask(3, 3, [1, 1, 1, 1], pix_list=a) assert m.x == 1 assert m.width == 2 assert m.y == 1 assert m.height == 1 assert m.overlaps_motion_border is True
def get_roi_mask(self, cell_specimen_ids=None): segmentations_df = self.nwb_session.modules[self.BRAIN_OBSERVATORY_PIPELINE]['ImageSegmentation']['PlaneSegmentation'].to_dataframe() if cell_specimen_ids is not None: segmentations_df = segmentations_df.loc[cell_specimen_ids] roi_array = [] for roi_id, roi_seg_df in segmentations_df.iterrows(): roi_mask = roi_seg_df['image_mask'] m = roi.create_roi_mask(roi_mask.shape[1], roi_mask.shape[0], [0, 0, 0, 0], roi_mask=roi_mask, label=roi_id) roi_array.append(m) return roi_array
def get_cell_specimen_table(self): cell_specimen_table = pd.DataFrame.from_dict(self.get_raw_cell_specimen_table_dict()).set_index('cell_roi_id').sort_index() fov_width, fov_height = self.get_field_of_view_shape()['width'], self.get_field_of_view_shape()['height'] image_mask_list = [] for sub_mask in cell_specimen_table['image_mask'].values: curr_roi = roi.create_roi_mask(fov_width, fov_height, [(fov_width - 1), 0, (fov_height - 1), 0], roi_mask=np.array(sub_mask, dtype=np.bool)) image_mask_list.append(curr_roi.get_mask_plane().astype(np.bool)) cell_specimen_table['image_mask'] = image_mask_list cell_specimen_table = cell_specimen_table[sorted(cell_specimen_table.columns)] cell_specimen_table.index.rename('cell_roi_id', inplace=True) cell_specimen_table.reset_index(inplace=True) cell_specimen_table.set_index('cell_specimen_id', inplace=True) return cell_specimen_table
def roi_mask_list(image_dims, motion_border): base_pixels = np.argwhere(np.ones((10, 10))) masks = [] for ii in range(10): pixels = base_pixels + ii * 10 masks.append( roi_masks.create_roi_mask(image_dims['width'], image_dims['height'], motion_border, pix_list=pixels, label=str(ii), mask_group=-1)) return masks
def test_init_by_pixels(): a = np.array([[0, 0], [1, 1], [1, 0]]) m = roi_masks.create_roi_mask(2, 2, [0, 0, 0, 0], pix_list=a) mp = m.get_mask_plane() assert mp[0, 0] == 1 assert mp[1, 1] == 1 assert mp[1, 0] == 0 assert mp[1, 1] == 1 assert m.x == 0 assert m.width == 2 assert m.y == 0 assert m.height == 2
def test_init_by_pixels(): a = np.array([[0, 0], [1, 1], [1, 0]]) m = roi_masks.create_roi_mask(2, 2, [0, 0, 0, 0], pix_list=a) mp = m.get_mask_plane() assert mp[0, 0] == 1 assert mp[1, 1] == 1 assert mp[1, 0] == 0 assert mp[1, 1] == 1 assert m.x == 0 assert m.width == 2 assert m.y == 0 assert m.height == 2
def get_roi_mask(self, cell_specimen_ids=None): ''' Returns an array of all the ROI masks Parameters ---------- cell specimen IDs: list or array (optional) List of cell IDs to return traces for. If this is None (default) then all are returned Returns ------- List of ROI_Mask objects ''' all_cell_specimen_ids = self.get_cell_specimen_ids() with h5py.File(self.nwb_file, 'r') as f: mask_loc = f['processing'][ self.PIPELINE_DATASET]['ImageSegmentation']['imaging_plane_1'] roi_list = f['processing'][self.PIPELINE_DATASET][ 'ImageSegmentation']['imaging_plane_1']['roi_list'].value inds = None if cell_specimen_ids is None: inds = range(len(all_cell_specimen_ids)) else: inds = [ list(all_cell_specimen_ids).index(i) for i in cell_specimen_ids ] roi_array = [] for i in inds: v = roi_list[i] m = roi.create_roi_mask(self.MOVIE_FOV_PX[0], self.MOVIE_FOV_PX[1], [0, 0, 0, 0], pix_list=mask_loc[v]["pix_mask"].value, label=v) roi_array.append(m) return roi_array
def get_rois(segmentation_stack, border=None): '''Extract a list of rois from the segmentation data array. Parameters ---------- segmentation_stack : numpy.ndarray The array from the maxInt_masks file showing the object masks. border : list [right_shift, left_shift, down_shift, up_shift] bounding box determined from motion correction. Returns ------- list List of RoiMask objects. ''' rois = [] if border is None: border = [0, 0, 0, 0] height = segmentation_stack.shape[1] width = segmentation_stack.shape[2] for i in range(segmentation_stack.shape[0]): page = segmentation_stack[i, :, :] label_mask, num_labels = measurements.label(page, structure=[[1, 1, 1], [1, 1, 1], [1, 1, 1]]) for label in range(1, num_labels + 1): img_mask = label_mask == label mask = create_roi_mask(width, height, border, roi_mask=img_mask, label="ROI {}:{}".format(i, label), mask_group=i) mask.labels = [] if mask.overlaps_motion_border: mask.labels.append("motion_border") rois.append(mask) return rois
def create_roi_masks(rois, w, h, motion_border): roi_list = [] for roi in rois: mask = np.array(roi["mask"], dtype=bool) px = np.argwhere(mask) px[:, 0] += roi["y"] px[:, 1] += roi["x"] mask = roi_masks.create_roi_mask(w, h, motion_border, pix_list=px[:, [1, 0]], label=str(roi["id"]), mask_group=roi.get("mask_page", -1)) roi_list.append(mask) # sort by roi id roi_list.sort(key=lambda x: x.label) return roi_list
def test_create_empty_neuropil_mask(): image_width = 100 image_height = 80 # border = [image_width-1, 0, image_height-1, 0] border = [5, 5, 5, 5] roi_mask = np.zeros((image_height, image_width), dtype=np.uint8) roi_mask[40:45, 30:35] = 1 combined_binary_mask = np.zeros((image_height, image_width), dtype=np.uint8) combined_binary_mask[:, :] = 1 roi = roi_masks.create_roi_mask(image_w=image_width, image_h=image_height, border=border, roi_mask=roi_mask) obtained = roi_masks.create_neuropil_mask(roi, border, combined_binary_mask) assert obtained.mask is None assert 'zero_pixels' in obtained.flags
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
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