def load_unit(self, unit_id): """Loads the image data for display.""" image1_path, image2_path = self.path_getter_inputs(unit_id) self.image_one = read_image(image1_path, error_msg='first image') self.image_two = read_image(image2_path, error_msg='second image') skip_subject = False if np.count_nonzero(self.image_one) == 0: skip_subject = True print('image {} of {} is empty!'.format(self.image1_name, self.current_unit_id)) if np.count_nonzero(self.image_two) == 0: skip_subject = True print('image {} of {} is empty!'.format(self.image2_name, self.current_unit_id)) if not skip_subject: # crop and rescale self.image_one, self.image_two = crop_to_seg_extents( self.image_one, self.image_two, self.padding) self.image_one = scale_0to1(self.image_one) self.image_two = scale_0to1(self.image_two) self.slices = pick_slices(self.image_one, self.views, self.num_slices_per_view) # flag to keep track of whether data has been changed. self._histogram_updated = False # # where to save the visualization to # out_vis_path = pjoin(self.out_dir, 'visual_qc_{}_{}'.format(self.vis_type, unit_id)) return skip_subject
def load_unit(self, unit_id): """Loads the image data for display.""" t1_mri_path = get_freesurfer_mri_path(self.in_dir, unit_id, self.mri_name) fs_seg_path = get_freesurfer_mri_path(self.in_dir, unit_id, self.seg_name) temp_t1_mri = read_image(t1_mri_path, error_msg='T1 mri') temp_fs_seg = read_image(fs_seg_path, error_msg='segmentation') if temp_t1_mri.shape != temp_fs_seg.shape: raise ValueError('size mismatch! MRI: {} Seg: {}\n' 'Size must match in all dimensions.'.format( self.current_t1_mri.shape, temp_fs_seg.shape)) skip_subject = False if self.vis_type in ('cortical_volumetric', 'cortical_contour'): temp_seg_uncropped, roi_set_is_empty = void_subcortical_symmetrize_cortical( temp_fs_seg) elif self.vis_type in ('labels_volumetric', 'labels_contour'): if self.label_set is not None: # TODO same colors for same labels is not guaranteed # if one subject fewer labels than others # due to remapping of labels for each subject temp_seg_uncropped, roi_set_is_empty = get_label_set( temp_fs_seg, self.label_set) else: raise ValueError( '--label_set must be specified for visualization types: ' ' labels_volumetric and labels_contour') else: raise NotImplementedError( 'Invalid visualization type - ' 'choose from: {}'.format( cfg.visualization_combination_choices)) if roi_set_is_empty: skip_subject = True print('segmentation image for {} ' 'does not contain requested label set!'.format(unit_id)) return skip_subject # T1 mri must be rescaled - to avoid strange distributions skewing plots rescaled_t1_mri = scale_0to1(temp_t1_mri, cfg.max_cmap_range_t1_mri) self.current_t1_mri, self.current_seg = crop_to_seg_extents( rescaled_t1_mri, temp_seg_uncropped, self.padding) out_vis_path = pjoin( self.out_dir, 'visual_qc_{}_{}_{}'.format(self.vis_type, self.suffix, unit_id)) return skip_subject
def load_unit(self, unit_id): """Loads the image data for display.""" # starting fresh for attr in ('defaced_img', 'orig_img', 'render_img'): if hasattr(self, attr): delattr(self, attr) self.defaced_img = read_image(self.images_for_id[unit_id]['defaced'], error_msg='defaced mri') self.orig_img = read_image(self.images_for_id[unit_id]['original'], error_msg='T1 mri') self.render_img_list = list() for rimg_path in self.images_for_id[unit_id]['render']: try: self.render_img_list.append(imread(rimg_path)) except: raise IOError('Unable to read the 3D rendered image @\n {}' ''.format(rimg_path)) # crop, trim, and rescale self.defaced_img = rescale_without_outliers( self.defaced_img, padding=self.padding, trim_percentile=cfg.defacing_trim_percentile) self.orig_img = rescale_without_outliers( self.orig_img, padding=self.padding, trim_percentile=cfg.defacing_trim_percentile) self.currently_showing = None skip_subject = False if np.count_nonzero(self.defaced_img) == 0 or \ np.count_nonzero(self.orig_img) == 0: skip_subject = True print('Defaced or original MR image is empty!') self.slice_picker = SlicePicker(self.orig_img, view_set=self.collage.view_set, num_slices=self.collage.num_slices, sampler=cfg.defacing_slice_locations) # # where to save the visualization to # out_vis_path = pjoin(self.out_dir, # 'visual_qc_{}_{}'.format(self.vis_type, unit_id)) return skip_subject
def t1_histogram_whole_scan(in_mri_path, num_bins=cfg.num_bins_histogram_intensity_distribution): """ Computes histogram over the intensity distribution over the entire scan, including brain, skull and background. Parameters ---------- in_mri_path : str Path to an MRI scan readable by Nibabel Returns ------- hist : ndarray Array of prob. densities for intensity """ img = read_image(in_mri_path) # scaled, and reshaped arr_0to1 = scale_0to1(img).flatten() # compute prob. density hist, _ = np.histogram(arr_0to1, bins=num_bins, density=True) return hist
def load_unit(self, unit_id): """Loads the image data for display.""" # starting fresh for attr in ('current_img_raw', 'current_img', 'saturated_img', 'tails_trimmed_img', 'background_img'): if hasattr(self, attr): delattr(self, attr) t1_mri_path = self.path_getter_inputs(unit_id) self.current_img_raw = read_image(t1_mri_path, error_msg='T1 mri') # crop and rescale self.current_img = scale_0to1( crop_image(self.current_img_raw, self.padding)) self.currently_showing = None skip_subject = False if np.count_nonzero(self.current_img) == 0: skip_subject = True print('MR image is empty!') # # where to save the visualization to # out_vis_path = pjoin(self.out_dir, 'visual_qc_{}_{}'.format(self.vis_type, unit_id)) return skip_subject
def _prepare_images(qcw, subject_id): """Actual routine to generate the visualizations. """ # qcw.fs_dir, qcw.subject_id, qcw.mri_name, qcw.seg_name, qcw.out_dir, qcw.vis_type, qcw.label_set # we ensured these files exist and are not empty t1_mri_path = get_path_for_subject(qcw.in_dir, subject_id, qcw.mri_name, qcw.vis_type) fs_seg_path = get_path_for_subject(qcw.in_dir, subject_id, qcw.seg_name, qcw.vis_type) t1_mri = read_image(t1_mri_path, error_msg='T1 mri') fs_seg = read_image(fs_seg_path, error_msg='segmentation') if t1_mri.shape != fs_seg.shape: raise ValueError('size mismatch! MRI: {} Seg: {}\n' 'Size must match in all dimensions.'.format( t1_mri.shape, fs_seg.shape)) skip_subject = False if qcw.label_set is not None: fs_seg, roi_set_empty = get_label_set(fs_seg, qcw.label_set) if roi_set_empty: skip_subject = True print( 'segmentation image for this subject does not contain requested label set!' ) if qcw.vis_type in ('cortical_volumetric', 'cortical_contour'): out_seg = void_subcortical_symmetrize_cortical(fs_seg) elif qcw.vis_type in ('labels_volumetric', 'labels_contour'): # TODO in addition to checking file exists, we need to requested labels exist, for label vis_type out_seg = fs_seg else: raise NotImplementedError( 'Other visualization combinations have not been implemented yet! Stay tuned.' ) out_path = pjoin( qcw.out_dir, 'visual_qc_{}_{}_{}'.format(qcw.vis_type, qcw.suffix, subject_id)) return t1_mri, out_seg, out_path, skip_subject
def _prepare_images(in_dir, subject_id, mri_name, seg_name, out_dir, vis_type, label_set): """Actual routine to generate the visualizations. """ # we ensured these files exist and are not empty t1_mri_path = get_path_for_subject( in_dir, subject_id, mri_name, vis_type) # pjoin(in_dir, subject_id, 'mri', mri_name) fs_seg_path = get_path_for_subject(in_dir, subject_id, seg_name, vis_type) t1_mri = read_image(t1_mri_path, error_msg='T1 mri') fs_seg = read_image(fs_seg_path, error_msg='segmentation') if t1_mri.shape != fs_seg.shape: raise ValueError('size mismatch! MRI: {} Seg: {}\n' 'Size must match in all dimensions.'.format( t1_mri.shape, fs_seg.shape)) if label_set is not None: fs_seg = get_label_set(fs_seg, label_set) suffix = '' if vis_type in ('cortical_volumetric', 'cortical_contour'): out_seg = void_subcortical_symmetrize_cortical(fs_seg) # generate pial surface elif vis_type in ('labels_volumetric', 'labels_contour'): out_seg = fs_seg if label_set is not None: suffix = '_'.join([str(lbl) for lbl in list(label_set)]) else: raise NotImplementedError( 'Other visualization combinations have not been implemented yet! Stay tuned.' ) out_path = pjoin(out_dir, 'visual_qc_{}_{}_{}'.format(vis_type, suffix, subject_id)) return t1_mri, out_seg, out_path
def load_unit(self, unit_id): """Loads the image data for display.""" t1_mri_path = self.path_getter_inputs(unit_id) self.current_img = read_image(t1_mri_path, error_msg='T1 mri') skip_subject = False if np.count_nonzero(self.current_img) == 0: skip_subject = True print('MR image is empty!') # # where to save the visualization to # out_vis_path = pjoin(self.out_dir, 'visual_qc_{}_{}'.format(self.vis_type, unit_id)) return skip_subject