def _run_single_scan(self, idx): in_img = ScanWrapper(self._in_data_folder.get_file_path(idx)) in_mask = ScanWrapper(self._in_mask_folder_obj.get_file_path(idx)) in_img_data = in_img.get_data() in_mask_data = in_mask.get_data() mask_data = np.zeros(in_mask_data.shape, dtype=int) for lung_label in self._lung_label_list: mask_match_map = (in_mask_data == lung_label).astype(int) print(f'lung label: {lung_label}') print(f'match_map shape: {mask_match_map.shape}') print(f'num posi voxels: {np.sum(mask_match_map)}') mask_data += mask_match_map mask_data = ~mask_data.astype('bool') print(f'Final mask: {np.sum(mask_data)}') in_img_data_with_mask = np.ma.array(in_img_data, mask=mask_data.astype('bool')) mean_in_mask = in_img_data_with_mask.mean() file_name = self._in_data_folder.get_file_name(idx) result_dict = { 'file_name': file_name, 'mean': mean_in_mask } print(result_dict) return result_dict
def _run_single_scan(self, idx): in_file_1_path = self._in_data_folder.get_file_path(idx) in_file_2_path = self._in_data_folder_2.get_file_path(idx) in_img_1 = ScanWrapper(in_file_1_path).get_data() in_img_2 = ScanWrapper(in_file_2_path).get_data() nrmse = compare_nrmse(np.abs(in_img_1), np.abs(in_img_2)) self._nrmse_diff.append(nrmse)
def clip_plot_3_view(self, out_png_folder): in_img_obj = ScanWrapper(self._in_img_path) in_mask_obj = ScanWrapper(self._in_mask_path) in_img_data = in_img_obj.get_data() in_mask_data = in_mask_obj.get_data() masked_img_data = np.zeros(in_img_data.shape, dtype=float) masked_img_data.fill(np.nan) masked_img_data[in_mask_data == 1] = in_img_data[in_mask_data == 1]
def clip_plot(self, out_png_folder): in_img_obj = ScanWrapper(self._in_img_path) in_back_obj = ScanWrapper(self._in_back_img_path) in_img_data = in_img_obj.get_data() in_back_data = in_back_obj.get_data() masked_img_data = None masked_back_data = None if self._in_mask_path is not None: in_mask_obj = ScanWrapper(self._in_mask_path) in_mask_data = in_mask_obj.get_data() masked_img_data = np.zeros(in_img_data.shape, dtype=float) masked_img_data.fill(np.nan) masked_img_data[in_mask_data == 1] = in_img_data[in_mask_data == 1] masked_back_data = np.zeros(in_back_data.shape, dtype=float) masked_back_data.fill(np.nan) masked_back_data[in_mask_data == 1] = in_back_data[in_mask_data == 1] else: masked_img_data = in_img_data masked_back_data = in_back_data self._plot_stacked_view( self._num_clip, self._step_axial, masked_img_data, masked_back_data, 'axial', out_png_folder, 1 ) self._plot_stacked_view( self._num_clip, self._step_sagittal, masked_img_data, masked_back_data, 'sagittal', out_png_folder, 5.23438 / 2.28335 ) self._plot_stacked_view( self._num_clip, self._step_coronal, masked_img_data, masked_back_data, 'coronal', out_png_folder, 5.23438 / 2.17388 )
def _run_single_scan(self, idx): in_ori_data = ScanWrapper( self._in_data_folder.get_file_path(idx)).get_data() in_jac_data = ScanWrapper( self._in_jac_folder.get_file_path(idx)).get_data() self._data_matrix[self._cur_idx, :self._ref_ori.get_number_voxel( )] = convert_3d_2_flat(in_ori_data) self._data_matrix[ self._cur_idx, self._ref_ori.get_number_voxel():] = convert_3d_2_flat(in_jac_data) self._cur_idx += 1
def __init__(self, config, in_ori_folder, in_jac_folder, batch_size, file_list_txt=None): super().__init__(config, in_ori_folder, file_list_txt) self._in_jac_folder = DataFolder(in_jac_folder, file_list_txt) self._ref_ori = ScanWrapper(self._in_data_folder.get_file_path(0)) self._ref_jac = ScanWrapper(self._in_jac_folder.get_file_path(0)) self._chunk_list = self._in_data_folder.get_chunks_list_batch_size( batch_size) self._data_matrix = [] self._cur_idx = 0
def get_average_map(file_list, save_path): first_img = ScanWrapper(file_list[0]) im_shape = first_img.get_shape() sum_map = np.zeros(im_shape, dtype=float) for idx_image in range(len(file_list)): img = ScanWrapper(file_list[idx_image]) img_data = img.get_data() print(f'Adding {file_list[idx_image]} ({idx_image} / {len(file_list)})') print(f'Max intensity {np.max(img_data)}') sum_map += img_data average_map = sum_map / float(len(file_list)) print(f'Average map max int {np.max(average_map)}') first_img.save_scan_same_space(save_path, average_map)
def _run_single_scan(self, idx): in_file_path = self._in_data_folder.get_file_path(idx) in_data = ScanWrapper(in_file_path) in_img = in_data.get_data() # self._data_matrix[idx, :] = in_img.reshape(in_data.get_number_voxel()) self._data_matrix[idx, :] = convert_3d_2_flat(in_img)
def __init__(self, config, in_folder, mask_img, out_png_folder, file_list_txt): super().__init__(config, in_folder, file_list_txt) self._mask_img = ScanWrapper(mask_img) self._out_png_folder = DataFolder.get_data_folder_obj(config, out_png_folder, data_list_txt=file_list_txt) self._out_png_folder.change_suffix('.png') self._vmax = 500 self._vmin = -1000
def plot_pc(self, out_png): fig = plt.figure(figsize=(self._num_show_pc * 20, self._num_clip * self._num_view * 15)) gs = gridspec.GridSpec(1, self._num_show_pc) sub_gs_list = [] for idx_pc in range(self._num_show_pc): sub_gs = gs[idx_pc].subgridspec(self._num_clip * self._num_view, 1) sub_gs_list.append(sub_gs) for idx_pc in range(self._num_show_pc): img_data_obj = ScanWrapper( self._pc_folder_obj.get_file_path(idx_pc)) img_data = img_data_obj.get_data() img_name = os.path.basename( self._pc_folder_obj.get_file_path(idx_pc)).replace( '.nii.gz', '') logger.info( f'Reading image {self._pc_folder_obj.get_file_path(idx_pc)}') self._plot_one_pc(img_data, sub_gs_list[idx_pc], f'{img_name}') # out_eps = out_png.replace('.png', '.eps') logger.info(f'Save fig to {out_png}') # plt.savefig(out_eps, bbox_inches='tight', pad_inches=0, dpi=self._out_dpi, format='pdf') plt.savefig(out_png, bbox_inches='tight', pad_inches=0, dpi=self._out_dpi) plt.close(fig=fig)
def main(): parser = argparse.ArgumentParser(description='Load a saved pca object') parser.add_argument('--load-pca-bin-path', type=str, help='Location of the pca bin') parser.add_argument('--in-folder', type=str) parser.add_argument('--out-folder', type=str) parser.add_argument('--mask-img', type=str) parser.add_argument('--num-mode', type=int) parser.add_argument('--file-list-txt', type=str) parser.add_argument('--num-process', type=int, default=10) args = parser.parse_args() in_folder_obj = DataFolder(args.in_folder, args.file_list_txt) out_folder_obj = DataFolder(args.out_folder, args.file_list_txt) mask_img_obj = ScanWrapper(args.mask_img) paral_obj = ParalGetResidual( args.load_pca_bin_path, in_folder_obj, out_folder_obj, mask_img_obj, args.num_mode, args.num_process ) paral_obj.run_parallel()
def __init__(self, config, in_folder, ref_img, batch_size, file_list=None): super().__init__(config, in_folder, file_list) self._ref_img = ScanWrapper(self._in_data_folder.get_file_path(0)) self._chunk_list = self._in_data_folder.get_chunks_list_batch_size( batch_size) self._data_matrix = [] self._cur_idx = 0
def __init__(self, config, in_dat_folder, out_folder, ref_img, file_list_txt): super().__init__(config, in_dat_folder, file_list_txt) self._ref_img = ScanWrapper(ref_img) self._out_data_folder = DataFolder.get_data_folder_obj( config, out_folder, data_list_txt=file_list_txt) self._jacobian_tool = config['deedsBCV_jacobian']
def _run_chunk(self, chunk_list): pca_nii_3d = PCA_NII_3D(None, None, 1) pca_nii_3d.load_pca(self._pca_bin_path) result_list = [] for idx in chunk_list: self._in_data_folder.print_idx(idx) image_obj = ScanWrapper(self._in_data_folder.get_file_path(idx)) jac_obj = ScanWrapper(self._in_jac_folder_obj.get_file_path(idx)) low_dim_representation = pca_nii_3d.transform_concat(image_obj, jac_obj) result = { 'scan_name': self._in_data_folder.get_file_name(idx), 'low_dim': low_dim_representation } print(result) result_list.append(result) return result_list
def __init__(self, config, in_folder, out_folder, ref_img, order=3): super().__init__(config, in_folder) self._out_data_folder = DataFolder.get_data_folder_obj( config, out_folder) self._ref_img = ScanWrapper(ref_img) self._order = order self._reg_resample = config['niftyreg_resample'] self._reg_resmaple_ref = config['reg_resample_ref_img']
def __init__(self, config, in_jac_folder, in_affine_mat_folder, out_folder, ref_img, file_list_txt): super().__init__(config, in_jac_folder, file_list_txt) self._ref_img = ScanWrapper(ref_img) self._in_affine_mat_folder = DataFolder.get_data_folder_obj( config, in_affine_mat_folder, data_list_txt=file_list_txt) self._out_data_folder = DataFolder.get_data_folder_obj( config, out_folder, data_list_txt=file_list_txt)
def _run_single_scan(self, idx): in_img = ScanWrapper(self._in_data_folder.get_file_path(idx)) out_mask_path = self._out_folder.get_file_path(idx) in_img_data = in_img.get_data() non_nan_mask = in_img_data == in_img_data logger.info(f'Save non-nan mask to {out_mask_path}') in_img.save_scan_same_space(out_mask_path, non_nan_mask.astype(int))
def clip_plot(self, out_png_folder): in_int_obj = ScanWrapper(self._in_int_path) in_jac_obj = ScanWrapper(self._in_jac_path) in_att_obj = ScanWrapper(self._in_att_path) in_int_data = in_int_obj.get_data() in_jac_data = in_jac_obj.get_data() in_att_data = in_att_obj.get_data() fig, axs = plt.subplots(1, 3, constrained_layout=True, figsize=(30, 30)) # plt.axis('off') for ax in axs: ax.axis('off') self._plot_view( self._num_clip, self._step_axial, in_int_data, in_jac_data, in_att_data, 'axial', 1, axs[0] ) self._plot_view( self._num_clip, self._step_coronal, in_int_data, in_jac_data, in_att_data, 'coronal', 5.23438 / 2.17388, axs[1] ) self._plot_view( self._num_clip, self._step_sagittal, in_int_data, in_jac_data, in_att_data, 'sagittal', 5.23438 / 2.28335, axs[2] ) out_root_folder = os.path.join(out_png_folder, 'stacked') mkdir_p(out_root_folder) out_png_path = os.path.join(out_root_folder, f'{self._in_img_file_name}.png') print(f'Save overlay png to {out_png_path}') plt.savefig(out_png_path, bbox_inches='tight', pad_inches=0) plt.close()
def _run_single_scan(self, idx): in_file_path = self._in_data_folder.get_file_path(idx) out_file_path = self._out_data_folder.get_file_path(idx) in_img = ScanWrapper(in_file_path).get_data() average_img = self._average_img.get_data() np.copyto(in_img, average_img, where=(in_img != in_img)) np.copyto(in_img, 0, where=(in_img != in_img)) self._average_img.save_scan_same_space(out_file_path, in_img)
def _run_chunk(self, chunk_list): result_list = [] im_shape = self._ref_img.get_shape() sum_image_union = np.zeros(im_shape) region_mask_count_image = np.zeros(im_shape) for idx in chunk_list: self._in_data_folder.print_idx(idx) img_obj = ScanWrapper(self._in_data_folder.get_file_path(idx)) mask_obj = ScanWrapper( self._in_region_mask_folder.get_file_path(idx)) sum_image_union += img_obj.get_data() region_mask_count_image += mask_obj.get_data() result = { 'sum_image': sum_image_union, 'region_count': region_mask_count_image } result_list.append(result) return result_list
def __init__(self, config, in_folder, out_folder, average_img, file_list_txt=None): super().__init__(config, in_folder, file_list_txt=file_list_txt) self._out_data_folder = DataFolder.get_data_folder_obj( config, out_folder, data_list_txt=file_list_txt) mkdir_p(out_folder) self._average_img = ScanWrapper(average_img)
def __init__(self, config, in_folder=None, data_file_txt=None, in_data_folder_obj=None): self._data_folder = None if in_data_folder_obj is None: self._data_folder = DataFolder(in_folder, data_file_txt) else: self._data_folder = in_data_folder_obj self._standard_ref = ScanWrapper(self._data_folder.get_first_path()) self._num_processes = config['num_processes']
def _plot_pc_one_view(self, idx_view, out_png_path): view_config_list = self._get_view_config() clip_plane = view_config_list[idx_view]['clip plane'] num_files = self._pc_folder_obj.num_files() num_lines = int(num_files / self._num_show_pc) fig = plt.figure(figsize=(self._num_show_pc * 15, num_lines * 12)) gs = gridspec.GridSpec(num_lines, self._num_show_pc) gs.update(wspace=0.025, hspace=0.025) for idx_row in range(num_lines): # if idx_row != 0: # continue for idx_column in range(self._num_show_pc): # if idx_column != 0: # continue idx_file = idx_column + self._num_show_pc * idx_row logger.info( f'Plot scan {self._pc_folder_obj.get_file_path(idx_file)}') img_data_obj = ScanWrapper( self._pc_folder_obj.get_file_path(idx_file)) img_data = img_data_obj.get_data() range_norm = np.max([np.min(img_data), np.max(img_data)]) # vmin = - 0.5 * range_norm # vmax = 0.5 * range_norm vmin = -0.0015 vmax = 0.0015 clip = self._clip_image(img_data, clip_plane, 0) ax = plt.subplot(gs[idx_row, idx_column]) plt.axis('off') im = plt.imshow(clip, interpolation='none', cmap=self._cm, norm=colors.Normalize(vmin=vmin, vmax=vmax)) # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # # cb = plt.colorbar(im, cax=cax) # # cb.set_label('Intensity of eigen image') # cb.ax.tick_params(labelsize=self._sub_title_font_size / 2) logger.info(f'Save plot to {out_png_path}') plt.savefig(out_png_path, bbox_inches='tight', pad_inches=0.1, dpi=self._out_dpi) plt.close(fig=fig)
def __init__(self, config, in_scan_folder, in_region_mask_folder, file_list_txt, out_average_img, ambient=-5000): super().__init__(config, in_scan_folder, file_list_txt) self._in_region_mask_folder = DataFolder.get_data_folder_obj( config, in_region_mask_folder, data_list_txt=file_list_txt) self._out_img_path = out_average_img self._ref_img = ScanWrapper(self._in_data_folder.get_first_path()) self._ambient = ambient
def _run_single_scan(self, idx): in_data_path = self._in_data_folder.get_file_path(idx) in_affine_mat_path = self._in_affine_mat_folder.get_file_path( idx).replace('.nii.gz', '.txt') out_file_path = self._out_data_folder.get_file_path(idx) in_image = ScanWrapper(in_data_path) affine_mat = self._get_affine_matrix(in_affine_mat_path) new_log_jacobian_det = in_image.get_data() + np.log( np.linalg.det(affine_mat)) self._ref_img.save_scan_same_space(out_file_path, new_log_jacobian_det)
def __init__(self, mask_img_path, config, in_ori_folder, in_jac_folder, batch_size, file_list_txt=None): super().__init__(config, in_ori_folder, in_jac_folder, batch_size, file_list_txt) self._mask_img = ScanWrapper(mask_img_path) self._masked_ref_ori = ScanWrapperWithMask( self._in_data_folder.get_file_path(0), self._mask_img.get_path()) self._masked_ref_jac = ScanWrapperWithMask( self._in_jac_folder.get_file_path(0), self._mask_img.get_path())
def _get_slices_dim_list(pc_folder, file_list_txt): center_slices_x = [] center_slices_y = [] center_slices_z = [] data_folder = DataFolder(pc_folder, file_list_txt) for file_idx in range(show_pc_number): data_folder.print_idx(file_idx) file_path = data_folder.get_file_path(file_idx) scan = ScanWrapper(file_path) slice_x, slice_y, slice_z = scan.get_center_slices() center_slices_x.append(slice_x) center_slices_y.append(slice_y) center_slices_z.append(slice_z) return center_slices_x, center_slices_y, center_slices_z
def _run_single_scan(self, idx): in_data_path = self._in_data_folder.get_file_path(idx) in_img = ScanWrapper(in_data_path) mse_val = self._calc_nmse(self._ref_img, in_img) nmi_val = self._calc_nmi(self._niftyreg_reg_measure_path, self._ref_img, in_img) val = { 'Scan': self._in_data_folder.get_file_name(idx), 'MSE': mse_val, 'NMI': nmi_val } print(val) return val
def _run_single_scan(self, idx): in_img_path = self._in_data_folder.get_file_path(idx) in_img = ScanWrapper(in_img_path) slice_in_img = self._clip_nifti(in_img.get_data()) slice_mask_img = self._clip_nifti(self._mask_img.get_data()) plt.figure(figsize=(15, 15)) plt.axis('off') clip_x_nii_rescale = exposure.rescale_intensity(slice_in_img, in_range=(self._vmin, self._vmax), out_range=(0, 1)) clip_x_nii_rgb = color.gray2rgb(clip_x_nii_rescale) plt.imshow(clip_x_nii_rgb, alpha=0.8) plt.imshow(slice_mask_img, interpolation='none', cmap='jet', norm=colors.Normalize(vmin=0, vmax=1), alpha=0.3) out_png_path = self._out_png_folder.get_file_path(idx) print(f'Saving image to {out_png_path}') plt.savefig(out_png_path, bbox_inches='tight', pad_inches=0)
def __init__(self, config, in_folder, ref_img, file_list_txt): super().__init__(config, in_folder, file_list_txt) self._ref_img = ScanWrapper(ref_img) self._df_mse_table = None self._niftyreg_reg_measure_path = config['niftyreg_reg_measure']