def compare_displacement_field_simulated_registered(self, plot=None): if plot is None: plot = self.plot disp_sim, mesh_sim, subdomains_sim, boundaries_sim = dio.load_function_mesh( self.path_forward_disp, functionspace='vector') disp_est, mesh_est, subdomains_est, boundaries_est = dio.load_function_mesh( self.path_forward_disp_reconstructed, functionspace='vector') # -- chose simulation mesh as reference funspace_ref = disp_sim.function_space() # -- project/interpolate estimated displacement field over that mesh disp_est_ref = self.interpolate_non_matching(disp_est, funspace_ref) # compute errornorm error = fenics.errornorm(disp_sim, disp_est_ref) self.measures['errornorm_displacement_simulated_vs_registered'] = error # compute difference field disp_diff = fenics.project(disp_sim - disp_est_ref, funspace_ref, annotate=False) if plot: plott.show_img_seg_f(function=disp_sim, show=False, path=os.path.join( self.steps_path_map['plots'], 'displacement_field_from_simulation.png'), dpi=300) plott.show_img_seg_f( function=disp_est_ref, show=False, path=os.path.join( self.steps_path_map['plots'], 'displacement_field_from_registration_ref_space.png'), dpi=300) plott.show_img_seg_f(function=disp_diff, show=False, path=os.path.join( self.steps_path_map['plots'], 'displacement_field_difference.png'), dpi=300) self._save_state()
def test_function_to_image_3d_scalar(self): n_rep = 10 img_list = [] fun_list = [] fun_list.append(self.conc3) for i in range(1, n_rep): # write to image img = dio.create_image_from_fenics_function(fun_list[i - 1], size_new=None) path_to_img = os.path.join(self.test_path, 'image_from_function_3d_scalar_%i.nii' % i) sitk.WriteImage(img, path_to_img) img_list.append(img) # read from image img_read = sitk.ReadImage(path_to_img) function = dio.create_fenics_function_from_image(img_read) fun_list.append(function) # plot function # path_to_fun_plot = os.path.join(config.output_dir_testing, 'conc_%i.png' % i) # plott.show_img_seg_f(function=fun_list[i], show=True, path=path_to_fun_plot) # compare function with previous one self.assertLess(fenics.errornorm(fun_list[i - 1], fun_list[i]),1E-5)
def compare_original_optimized(self, plot=None): if plot is None: plot = self.plot output_path = self.steps_path_map['comparison'] self.sim_forward.init_postprocess(output_path) self.sim_optimized.init_postprocess(output_path) # -- get solution at sim time from original forward model conc_orig_full = self.sim_forward.postprocess.get_solution_concentration( self.params_forward['sim_params']['sim_time']).copy() disp_orig_full = self.sim_forward.postprocess.get_solution_displacement( self.params_forward['sim_params']['sim_time']).copy() # -- get solution at sim time from optimized simulation conc_opt = self.sim_optimized.postprocess.get_solution_concentration( self.params_optimized['sim_params']['sim_time']).copy() disp_opt = self.sim_optimized.postprocess.get_solution_displacement( self.params_optimized['sim_params']['sim_time']).copy() # -- project original solution into domain of optimized solution # -- chose simulation mesh as reference funspace_disp_opt = self.sim_optimized.functionspace.get_functionspace( subspace_id=0) funspace_conc_opt = self.sim_optimized.functionspace.get_functionspace( subspace_id=1) conc_orig = self.interpolate_non_matching(conc_orig_full, funspace_conc_opt) disp_orig = self.interpolate_non_matching(disp_orig_full, funspace_disp_opt) # -- compute error norms error_conc = fenics.errornorm(conc_orig, conc_opt) error_disp = fenics.errornorm(disp_orig, disp_opt) self.measures[ 'errornorm_displacement_forward_vs_optimized'] = error_disp self.measures[ 'errornorm_concentration_forward_vs_optimized'] = error_conc if plot: plott.show_img_seg_f(function=conc_orig, path=os.path.join(output_path, 'conc_forward.png')) plott.show_img_seg_f(function=conc_opt, path=os.path.join(output_path, 'conc_opt.png')) conc_diff = fenics.project(conc_orig - conc_opt, funspace_conc_opt, annotate=False) plott.show_img_seg_f(function=conc_diff, path=os.path.join(output_path, 'conc_diff.png')) plott.show_img_seg_f(function=disp_orig, path=os.path.join(output_path, 'disp_forward.png')) plott.show_img_seg_f(function=disp_opt, path=os.path.join(output_path, 'disp_opt.png')) disp_diff = fenics.project(disp_orig - disp_opt, funspace_disp_opt, annotate=False) plott.show_img_seg_f(function=disp_diff, path=os.path.join(output_path, 'disp_diff.png')) self._save_state()