def test_update_write_transform(self): motion_simulator = ms.RandomRigidMotionSimulator(dimension=3, angle_max_deg=20, translation_max=30) filenames = ["fetal_brain_%d" % d for d in range(3)] stacks = [ st.Stack.from_filename( os.path.join(self.dir_test_data, "%s.nii.gz" % f)) for f in filenames ] # Generate random motions for all slices of each stack motions_sitk = {f: {} for f in filenames} for i, stack in enumerate(stacks): motion_simulator.simulate_motion( seed=i, simulations=stack.get_number_of_slices()) motions_sitk[stack.get_filename()] = \ motion_simulator.get_transforms_sitk() # Apply random motion to all slices of all stacks dir_output = os.path.join(DIR_TMP, "test_update_write_transform") for i, stack in enumerate(stacks): for j, slice in enumerate(stack.get_slices()): slice.update_motion_correction( motions_sitk[stack.get_filename()][j]) # Write stacks to directory stack.write(dir_output, write_slices=True, write_transforms=True) # Read written stacks/slices/transformations data_reader = dr.ImageSlicesDirectoryReader(dir_output) data_reader.read_data() stacks_2 = data_reader.get_data() data_reader = dr.SliceTransformationDirectoryReader(dir_output) data_reader.read_data() transformations_dic = data_reader.get_data() filenames_2 = [s.get_filename() for s in stacks_2] for i, stack in enumerate(stacks): stack_2 = stacks_2[filenames_2.index(stack.get_filename())] slices = stack.get_slices() slices_2 = stack_2.get_slices() # test number of slices match self.assertEqual(len(slices), len(slices_2)) # Test whether header of written slice coincides with transformed # slice for j in range(stack.get_number_of_slices()): # Check Spacing self.assertAlmostEqual(np.max( np.abs( np.array(slices[j].sitk.GetSpacing()) - np.array(slices_2[j].sitk.GetSpacing()))), 0, places=10) # Check Origin self.assertAlmostEqual(np.max( np.abs( np.array(slices[j].sitk.GetOrigin()) - np.array(slices_2[j].sitk.GetOrigin()))), 0, places=4) # Check Direction self.assertAlmostEqual(np.max( np.abs( np.array(slices[j].sitk.GetDirection()) - np.array(slices_2[j].sitk.GetDirection()))), 0, places=4) # Test whether parameters of written slice transforms match params = np.array( motions_sitk[stack.get_filename()][j].GetParameters()) params_2 = np.array( transformations_dic[stack.get_filename()][j].GetParameters()) self.assertAlmostEqual(np.max(np.abs(params - params_2)), 0, places=16)
def main(): input_parser = InputArgparser( description="Simulate stacks from obtained reconstruction. " "Script simulates/projects the slices at estimated positions " "within reconstructed volume. Ideally, if motion correction was " "correct, the resulting stack of such obtained projected slices, " "corresponds to the originally acquired (motion corrupted) data.", ) input_parser.add_dir_input(required=True) input_parser.add_reconstruction(required=True) input_parser.add_dir_output(required=True) input_parser.add_suffix_mask(default="_mask") input_parser.add_prefix_output(default="Simulated_") input_parser.add_option( option_string="--copy-data", type=int, help="Turn on/off copying of original data (including masks) to " "output folder.", default=0) input_parser.add_option( option_string="--reconstruction-mask", type=str, help="If given, reconstruction image mask is propagated to " "simulated stack(s) of slices as well", default=None) input_parser.add_interpolator( option_string="--interpolator-mask", help="Choose the interpolator type to propagate the reconstruction " "mask (%s)." % (INTERPOLATOR_TYPES), default="NearestNeighbor") input_parser.add_verbose(default=0) args = input_parser.parse_args() input_parser.print_arguments(args) if args.interpolator_mask not in ALLOWED_INTERPOLATORS: raise IOError( "Unknown interpolator provided. Possible choices are %s" % ( INTERPOLATOR_TYPES)) # Read motion corrected data data_reader = dr.ImageSlicesDirectoryReader( path_to_directory=args.dir_input, suffix_mask=args.suffix_mask) data_reader.read_data() stacks = data_reader.get_data() reconstruction = st.Stack.from_filename( args.reconstruction, args.reconstruction_mask, extract_slices=False) linear_operators = lin_op.LinearOperators() for i, stack in enumerate(stacks): # initialize image data array(s) nda = np.zeros_like(sitk.GetArrayFromImage(stack.sitk)) if args.reconstruction_mask: nda_mask = np.zeros_like(sitk.GetArrayFromImage(stack.sitk_mask)) # Simulate slices at estimated positions within reconstructed volume simulated_slices = [ linear_operators.A( reconstruction, s, interpolator_mask=args.interpolator_mask) for s in stack.get_slices() ] # Fill stack information "as if slice was acquired consecutively" # Therefore, simulated stack slices correspond to acquired slices # (in case motion correction was correct) for j, simulated_slice in enumerate(simulated_slices): nda[j, :, :] = sitk.GetArrayFromImage(simulated_slice.sitk) if args.reconstruction_mask: nda_mask[j, :, :] = sitk.GetArrayFromImage( simulated_slice.sitk_mask) # Create nifti image with same image header as original stack simulated_stack_sitk = sitk.GetImageFromArray(nda) simulated_stack_sitk.CopyInformation(stack.sitk) if args.reconstruction_mask: simulated_stack_sitk_mask = sitk.GetImageFromArray(nda_mask) simulated_stack_sitk_mask.CopyInformation(stack.sitk_mask) else: simulated_stack_sitk_mask = None simulated_stack = st.Stack.from_sitk_image( image_sitk=simulated_stack_sitk, image_sitk_mask=simulated_stack_sitk_mask, filename=args.prefix_output + stack.get_filename(), extract_slices=False) if args.verbose: sitkh.show_stacks([ stack, simulated_stack], segmentation=simulated_stack if args.reconstruction_mask else None) simulated_stack.write( args.dir_output, write_mask=True, write_slices=False, suffix_mask=args.suffix_mask) if args.copy_data: stack.write( args.dir_output, write_mask=True, write_slices=False, suffix_mask=args.suffix_mask) return 0
def main(): time_start = ph.start_timing() # Set print options for numpy np.set_printoptions(precision=3) # Read input input_parser = InputArgparser( description="Script to study reconstruction parameters and their " "impact on the volumetric reconstruction quality.", ) input_parser.add_dir_input() input_parser.add_filenames() input_parser.add_image_selection() input_parser.add_dir_output(required=True) input_parser.add_suffix_mask(default="_mask") input_parser.add_reconstruction_space() input_parser.add_reference( help="Path to reference NIfTI image file. If given the volumetric " "reconstructed is performed in this physical space. " "Either a reconstruction space or a reference must be provided", required=False) input_parser.add_reference_mask(default=None) input_parser.add_study_name() input_parser.add_reconstruction_type(default="TK1L2") input_parser.add_measures(default=["PSNR", "RMSE", "SSIM", "NCC", "NMI"]) input_parser.add_tv_solver(default="PD") input_parser.add_iterations(default=50) input_parser.add_rho(default=0.1) input_parser.add_iter_max(default=10) input_parser.add_minimizer(default="lsmr") input_parser.add_alpha(default=0.01) input_parser.add_data_loss(default="linear") input_parser.add_data_loss_scale(default=1) input_parser.add_log_script_execution(default=1) input_parser.add_verbose(default=1) # Range for parameter sweeps input_parser.add_alpha_range(default=[0.001, 0.05, 20]) # TK1L2 # input_parser.add_alpha_range(default=[0.001, 0.003, 10]) # TVL2, HuberL2 input_parser.add_data_losses( # default=["linear", "arctan"] ) input_parser.add_data_loss_scale_range( # default=[0.1, 1.5, 2] ) args = input_parser.parse_args() input_parser.print_arguments(args) if args.reference is None and args.reconstruction_space is None: raise IOError("Either reference (--reference) or reconstruction space " "(--reconstruction-space) must be provided.") # Write script execution call if args.log_script_execution: input_parser.write_performed_script_execution( os.path.abspath(__file__)) # --------------------------------Read Data-------------------------------- ph.print_title("Read Data") # Neither '--dir-input' nor '--filenames' was specified if args.filenames is not None and args.dir_input is not None: raise IOError( "Provide input by either '--dir-input' or '--filenames' " "but not both together") # '--dir-input' specified elif args.dir_input is not None: data_reader = dr.ImageSlicesDirectoryReader( path_to_directory=args.dir_input, suffix_mask=args.suffix_mask, image_selection=args.image_selection) # '--filenames' specified elif args.filenames is not None: data_reader = dr.MultipleImagesReader( args.filenames, suffix_mask=args.suffix_mask) else: raise IOError( "Provide input by either '--dir-input' or '--filenames'") data_reader.read_data() stacks = data_reader.get_data() ph.print_info("%d input stacks read for further processing" % len(stacks)) if args.reference is not None: reference = st.Stack.from_filename( file_path=args.reference, file_path_mask=args.reference_mask, extract_slices=False) reconstruction_space = stacks[0].get_resampled_stack(reference.sitk) reconstruction_space = \ reconstruction_space.get_stack_multiplied_with_mask() x_ref = sitk.GetArrayFromImage(reference.sitk).flatten() x_ref_mask = sitk.GetArrayFromImage(reference.sitk_mask).flatten() else: reconstruction_space = st.Stack.from_filename( file_path=args.reconstruction_space, extract_slices=False) reconstruction_space = stacks[0].get_resampled_stack( reconstruction_space.sitk) reconstruction_space = \ reconstruction_space.get_stack_multiplied_with_mask() x_ref = None x_ref_mask = None # ----------------------------Set Up Parameters---------------------------- parameters = {} parameters["alpha"] = np.linspace( args.alpha_range[0], args.alpha_range[1], int(args.alpha_range[2])) if args.data_losses is not None: parameters["data_loss"] = args.data_losses if args.data_loss_scale_range is not None: parameters["data_loss_scale"] = np.linspace( args.data_loss_scale_range[0], args.data_loss_scale_range[1], int(args.data_loss_scale_range[2])) # --------------------------Set Up Parameter Study------------------------- if args.study_name is None: name = args.reconstruction_type else: name = args.study_name reconstruction_info = { "shape": reconstruction_space.sitk.GetSize()[::-1], "origin": reconstruction_space.sitk.GetOrigin(), "spacing": reconstruction_space.sitk.GetSpacing(), "direction": reconstruction_space.sitk.GetDirection(), } # Create Tikhonov solver from which all information can be extracted # (also for other reconstruction types) tmp = tk.TikhonovSolver( stacks=stacks, reconstruction=reconstruction_space, alpha=args.alpha, iter_max=args.iter_max, data_loss=args.data_loss, data_loss_scale=args.data_loss_scale, reg_type="TK1", minimizer=args.minimizer, verbose=args.verbose, ) solver = tmp.get_solver() parameter_study_interface = \ deconv_interface.DeconvolutionParameterStudyInterface( A=solver.get_A(), A_adj=solver.get_A_adj(), D=solver.get_B(), D_adj=solver.get_B_adj(), b=solver.get_b(), x0=solver.get_x0(), alpha=solver.get_alpha(), x_scale=solver.get_x_scale(), data_loss=solver.get_data_loss(), data_loss_scale=solver.get_data_loss_scale(), iter_max=solver.get_iter_max(), minimizer=solver.get_minimizer(), iterations=args.iterations, measures=args.measures, dimension=3, L2=16./reconstruction_space.sitk.GetSpacing()[0]**2, reconstruction_type=args.reconstruction_type, rho=args.rho, dir_output=args.dir_output, parameters=parameters, name=name, reconstruction_info=reconstruction_info, x_ref=x_ref, x_ref_mask=x_ref_mask, tv_solver=args.tv_solver, verbose=args.verbose, ) parameter_study_interface.set_up_parameter_study() parameter_study = parameter_study_interface.get_parameter_study() # Run parameter study parameter_study.run() print("\nComputational time for Deconvolution Parameter Study %s: %s" % (name, parameter_study.get_computational_time())) return 0
def main(): time_start = ph.start_timing() # Set print options for numpy np.set_printoptions(precision=3) # Read input input_parser = InputArgparser( description="Volumetric MRI reconstruction framework to reconstruct " "an isotropic, high-resolution 3D volume from multiple " "motion-corrected (or static) stacks of low-resolution slices.", ) input_parser.add_dir_input() input_parser.add_filenames() input_parser.add_image_selection() input_parser.add_dir_output(required=True) input_parser.add_prefix_output(default="SRR_") input_parser.add_suffix_mask(default="_mask") input_parser.add_target_stack_index(default=0) input_parser.add_extra_frame_target(default=10) input_parser.add_isotropic_resolution(default=None) input_parser.add_reconstruction_space(default=None) input_parser.add_minimizer(default="lsmr") input_parser.add_iter_max(default=10) input_parser.add_reconstruction_type(default="TK1L2") input_parser.add_data_loss(default="linear") input_parser.add_data_loss_scale(default=1) input_parser.add_alpha(default=0.02 # TK1L2 # default=0.006 #TVL2, HuberL2 ) input_parser.add_rho(default=0.5) input_parser.add_tv_solver(default="PD") input_parser.add_pd_alg_type(default="ALG2") input_parser.add_iterations(default=15) input_parser.add_subfolder_comparison() input_parser.add_provide_comparison(default=0) input_parser.add_log_script_execution(default=1) input_parser.add_verbose(default=0) args = input_parser.parse_args() input_parser.print_arguments(args) # Write script execution call if args.log_script_execution: input_parser.write_performed_script_execution( os.path.abspath(__file__)) # --------------------------------Read Data-------------------------------- ph.print_title("Read Data") # Neither '--dir-input' nor '--filenames' was specified if args.filenames is not None and args.dir_input is not None: raise IOError("Provide input by either '--dir-input' or '--filenames' " "but not both together") # '--dir-input' specified elif args.dir_input is not None: data_reader = dr.ImageSlicesDirectoryReader( path_to_directory=args.dir_input, suffix_mask=args.suffix_mask, image_selection=args.image_selection) # '--filenames' specified elif args.filenames is not None: data_reader = dr.MultipleImagesReader(args.filenames, suffix_mask=args.suffix_mask) else: raise IOError("Provide input by either '--dir-input' or '--filenames'") if args.reconstruction_type not in ["TK1L2", "TVL2", "HuberL2"]: raise IOError("Reconstruction type unknown") data_reader.read_data() stacks = data_reader.get_data() ph.print_info("%d input stacks read for further processing" % len(stacks)) # Reconstruction space is given isotropically resampled target stack if args.reconstruction_space is None: recon0 = \ stacks[args.target_stack_index].get_isotropically_resampled_stack( resolution=args.isotropic_resolution, extra_frame=args.extra_frame_target) # Reconstruction space was provided by user else: recon0 = st.Stack.from_filename(args.reconstruction_space, extract_slices=False) # Change resolution for isotropic resolution if provided by user if args.isotropic_resolution is not None: recon0 = recon0.get_isotropically_resampled_stack( args.isotropic_resolution) # Use image information of selected target stack as recon0 serves # as initial value for reconstruction recon0 = \ stacks[args.target_stack_index].get_resampled_stack(recon0.sitk) recon0 = recon0.get_stack_multiplied_with_mask() if args.reconstruction_type in ["TVL2", "HuberL2"]: ph.print_title("Compute Initial value for %s" % args.reconstruction_type) SRR0 = tk.TikhonovSolver( stacks=stacks, reconstruction=recon0, alpha=args.alpha, iter_max=args.iter_max, reg_type="TK1", minimizer=args.minimizer, data_loss=args.data_loss, data_loss_scale=args.data_loss_scale, # verbose=args.verbose, ) SRR0.run() recon = SRR0.get_reconstruction() recon.set_filename(SRR0.get_setting_specific_filename(args.prefix_output)) recon.write(args.dir_output) # List to store SRRs recons = [] for i in range(0, len(stacks)): recons.append(stacks[i]) recons.insert(0, recon) if args.reconstruction_type in ["TVL2", "HuberL2"]: ph.print_title("Compute %s reconstruction" % args.reconstruction_type) if args.tv_solver == "ADMM": SRR = admm.ADMMSolver( stacks=stacks, reconstruction=st.Stack.from_stack(SRR0.get_reconstruction()), minimizer=args.minimizer, alpha=args.alpha, iter_max=args.iter_max, rho=args.rho, data_loss=args.data_loss, iterations=args.iterations, verbose=args.verbose, ) SRR.run() recon = SRR.get_reconstruction() recon.set_filename( SRR.get_setting_specific_filename(args.prefix_output)) recons.insert(0, recon) recon.write(args.dir_output) else: SRR = pd.PrimalDualSolver( stacks=stacks, reconstruction=st.Stack.from_stack(SRR0.get_reconstruction()), minimizer=args.minimizer, alpha=args.alpha, iter_max=args.iter_max, iterations=args.iterations, alg_type=args.pd_alg_type, reg_type="TV" if args.reconstruction_type == "TVL2" else "huber", data_loss=args.data_loss, verbose=args.verbose, ) SRR.run() recon = SRR.get_reconstruction() recon.set_filename( SRR.get_setting_specific_filename(args.prefix_output)) recons.insert(0, recon) recon.write(args.dir_output) if args.verbose and not args.provide_comparison: sitkh.show_stacks(recons) # Show SRR together with linearly resampled input data. # Additionally, a script is generated to open files if args.provide_comparison: sitkh.show_stacks( recons, show_comparison_file=args.provide_comparison, dir_output=os.path.join(args.dir_output, args.subfolder_comparison), ) ph.print_line_separator() elapsed_time = ph.stop_timing(time_start) ph.print_title("Summary") print("Computational Time for Volumetric Reconstruction: %s" % (elapsed_time)) return 0
def main(): time_start = ph.start_timing() np.set_printoptions(precision=3) input_parser = InputArgparser( description="Register an obtained reconstruction (moving) " "to a template image/space (fixed) using rigid registration. " "The resulting registration can optionally be applied to previously " "obtained motion correction slice transforms so that a volumetric " "reconstruction is possible in the (standard anatomical) space " "defined by the fixed.", ) input_parser.add_fixed(required=True) input_parser.add_moving( required=True, nargs="+", help="Specify moving image to be warped to fixed space. " "If multiple images are provided, all images will be transformed " "uniformly according to the registration obtained for the first one.") input_parser.add_dir_output(required=True) input_parser.add_dir_input() input_parser.add_suffix_mask(default="_mask") input_parser.add_search_angle(default=180) input_parser.add_option( option_string="--transform-only", type=int, help="Turn on/off functionality to transform moving image(s) to fixed " "image only, i.e. no resampling to fixed image space", default=0) input_parser.add_option( option_string="--write-transform", type=int, help="Turn on/off functionality to write registration transform", default=0) input_parser.add_verbose(default=0) args = input_parser.parse_args() input_parser.print_arguments(args) use_reg_aladin_for_refinement = True # --------------------------------Read Data-------------------------------- ph.print_title("Read Data") data_reader = dr.MultipleImagesReader(args.moving, suffix_mask="_mask") data_reader.read_data() moving = data_reader.get_data() data_reader = dr.MultipleImagesReader([args.fixed], suffix_mask="_mask") data_reader.read_data() fixed = data_reader.get_data()[0] # -------------------Register Reconstruction to Template------------------- ph.print_title("Register Reconstruction to Template") # Define search angle ranges for FLIRT in all three dimensions search_angles = [ "-searchr%s -%d %d" % (x, args.search_angle, args.search_angle) for x in ["x", "y", "z"] ] search_angles = (" ").join(search_angles) options_args = [] options_args.append(search_angles) # cost = "mutualinfo" # options_args.append("-searchcost %s -cost %s" % (cost, cost)) registration = regflirt.FLIRT( fixed=moving[0], moving=fixed, # use_fixed_mask=True, # use_moving_mask=True, # moving mask only seems to work for SB cases registration_type="Rigid", use_verbose=False, options=(" ").join(options_args), ) ph.print_info("Run Registration (FLIRT) ... ", newline=False) registration.run() print("done") transform_sitk = registration.get_registration_transform_sitk() if args.write_transform: path_to_transform = os.path.join(args.dir_output, "registration_transform_sitk.txt") sitk.WriteTransform(transform_sitk, path_to_transform) # Apply rigidly transform to align reconstruction (moving) with template # (fixed) for m in moving: m.update_motion_correction(transform_sitk) # Additionally, use RegAladin for more accurate alignment # Rationale: FLIRT has better capture range, but RegAladin seems to # find better alignment once it is within its capture range. if use_reg_aladin_for_refinement: registration = niftyreg.RegAladin( fixed=m, use_fixed_mask=True, moving=fixed, registration_type="Rigid", use_verbose=False, ) ph.print_info("Run Registration (RegAladin) ... ", newline=False) registration.run() print("done") transform2_sitk = registration.get_registration_transform_sitk() m.update_motion_correction(transform2_sitk) transform_sitk = sitkh.get_composite_sitk_affine_transform( transform2_sitk, transform_sitk) if args.transform_only: for m in moving: m.write(args.dir_output, write_mask=False) ph.exit() # Resample reconstruction (moving) to template space (fixed) warped_moving = [ m.get_resampled_stack(fixed.sitk, interpolator="Linear") for m in moving ] for wm in warped_moving: wm.set_filename(wm.get_filename() + "ResamplingToTemplateSpace") if args.verbose: sitkh.show_stacks([fixed, wm], segmentation=fixed) # Write resampled reconstruction (moving) wm.write(args.dir_output, write_mask=False) if args.dir_input is not None: data_reader = dr.ImageSlicesDirectoryReader( path_to_directory=args.dir_input, suffix_mask=args.suffix_mask) data_reader.read_data() stacks = data_reader.get_data() for i, stack in enumerate(stacks): stack.update_motion_correction(transform_sitk) ph.print_info("Stack %d/%d: All slice transforms updated" % (i + 1, len(stacks))) # Write transformed slices stack.write( os.path.join(args.dir_output, "motion_correction"), write_mask=True, write_slices=True, write_transforms=True, suffix_mask=args.suffix_mask, ) elapsed_time_total = ph.stop_timing(time_start) # Summary ph.print_title("Summary") print("Computational Time: %s" % (elapsed_time_total)) return 0