def test_ray_weights_for_views(): context = make_context() views = context["views"] paths = context["paths"] paths_set = set(paths.values()) ray_weights_cache = bim.ray_weights_for_views( views, frequency=context["freq"], probe_element_width=context["element_width"]) assert ray_weights_cache.tx_ray_weights_debug_dict is None assert ray_weights_cache.rx_ray_weights_debug_dict is None assert len(paths) >= len(ray_weights_cache.tx_ray_weights_dict) > 3 assert len(paths) >= len(ray_weights_cache.rx_ray_weights_dict) > 3 assert set(ray_weights_cache.rx_ray_weights_dict.keys()) == paths_set nbytes_without_debug = ray_weights_cache.nbytes assert nbytes_without_debug > 0 ray_weights_cache = bim.ray_weights_for_views( views, frequency=context["freq"], probe_element_width=context["element_width"], save_debug=True, ) assert (ray_weights_cache.tx_ray_weights_debug_dict.keys() == ray_weights_cache.tx_ray_weights_dict.keys()) assert (ray_weights_cache.rx_ray_weights_debug_dict.keys() == ray_weights_cache.rx_ray_weights_dict.keys()) assert len(paths) >= len(ray_weights_cache.tx_ray_weights_dict) > 3 assert len(paths) >= len(ray_weights_cache.rx_ray_weights_dict) > 3 assert set(ray_weights_cache.rx_ray_weights_dict.keys()) == paths_set nbytes_with_debug = ray_weights_cache.nbytes assert nbytes_with_debug > nbytes_without_debug
def model_sensitivity(dataset_name, save): # %% conf = arim.io.load_conf(dataset_name) result_dir = conf["result_dir"] logger.info(f"dataset_name: {dataset_name}") probe = common.load_probe(conf) examination_object = arim.io.block_in_immersion_from_conf(conf) tx, rx = arim.ut.fmc(probe.numelements) numscanlines = len(tx) model_options = dict( frequency=common.get_centre_freq(conf, probe), probe_element_width=probe.dimensions.x[0], ) grid_p = common.defect_oriented_point(conf) probe_p = probe.to_oriented_points() views = bim.make_views( examination_object, probe_p, grid_p, max_number_of_reflection=1, tfm_unique_only=True, ) arim.ray.ray_tracing(views.values(), convert_to_fortran_order=True) scat_obj = arim.scat.scat_factory( **conf["scatterer"]["specs"], material=examination_object.block_material) scat_angle = np.deg2rad(conf["scatterer"]["angle_deg"]) with arim.helpers.timeit("Scattering matrices", logger=logger): # scat_mat = scat_obj.as_single_freq_matrices(model_options['frequency'], 360) # use precomputation scat_mat = scat_obj.as_angles_funcs( model_options["frequency"]) # no precomputation with arim.helpers.timeit("Computation of ray weights for all paths", logger=logger): ray_weights = bim.ray_weights_for_views(views, **model_options) theoretical_intensities_dict = dict() scanline_weights = np.ones(numscanlines) for viewname, view in views.items(): model_coefficients = arim.model.model_amplitudes_factory( tx.astype(int), rx.astype(int), view, ray_weights, scat_mat, scat_angle=scat_angle, ) # shape: numpoints, numscanlines theoretical_intensities_dict[ viewname] = model_coefficients.sensitivity_uniform_tfm( scanline_weights) # ax, _ = aplt.plot_oxz(np.abs(theoretical_intensities_dict[viewname]), grid, scale='linear', title=viewname) # %% data = [] for viewname, th_amp in theoretical_intensities_dict.items(): data.append((viewname, np.abs(th_amp[0]))) intensities = pd.DataFrame(data, columns=("view", "Model_Sensitivity")).set_index("view") if save: intensities.to_csv(result_dir / "intensities_sensitivity_unscaled.csv") # %% return intensities
def compute_sensitivity(dataset_name, save): conf = arim.io.load_conf(dataset_name) result_dir = conf["result_dir"] probe = arim.io.probe_from_conf(conf) examination_object = arim.io.block_in_immersion_from_conf(conf) tx, rx = arim.ut.fmc(probe.numelements) numscanlines = len(tx) model_options = dict( frequency=probe.frequency, probe_element_width=probe.dimensions.x[0] ) tic = time.time() grid = arim.io.grid_from_conf(conf) grid_p = grid.to_oriented_points() logger.info(f"grid numpoints: {grid.numpoints}") probe_p = probe.to_oriented_points() views = bim.make_views( examination_object, probe_p, grid_p, max_number_of_reflection=1, tfm_unique_only=True, ) aplt.plot_interfaces( [probe_p, examination_object.frontwall, examination_object.backwall, grid_p], show_orientations=False, show_last=True, # markers=[".", "-", "-"], filename=str(result_dir / "interfaces"), savefig=save, ) arim.ray.ray_tracing(views.values(), convert_to_fortran_order=True) scat_obj = arim.scat.scat_factory( **conf["scatterer"]["specs"], material=examination_object.block_material ) scat_angle = np.deg2rad(conf["scatterer"]["angle_deg"]) with arim.helpers.timeit("Scattering matrices", logger=logger): scat_mat = scat_obj.as_single_freq_matrices( model_options["frequency"], 180 ) # use precomputation with arim.helpers.timeit("Computation of ray weights for all paths", logger=logger): ray_weights = bim.ray_weights_for_views(views, **model_options) sensitivity_images_dict = dict() scanline_weights = np.ones(numscanlines) for viewname, view in views.items(): model_coefficients = arim.model.model_amplitudes_factory( tx.astype(int), rx.astype(int), view, ray_weights, scat_mat, scat_angle=scat_angle, ) sensitivity_images_dict[viewname] = model_coefficients.sensitivity_uniform_tfm( scanline_weights ) toc = time.time() elapsed = toc - tic logger.info( f"Total time for sensitivity images: {elapsed:.2f} s ({grid.numpoints} points)" ) # %% out = {"images": sensitivity_images_dict, "grid": grid} with open(result_dir / "sensitivity_images.pickle", "wb") as f: pickle.dump(out, f, pickle.HIGHEST_PROTOCOL) return sensitivity_images_dict
def test_model(scat_specs, show_plots): couplant = arim.Material( longitudinal_vel=1480.0, density=1000.0, state_of_matter="liquid", longitudinal_att=arim.material_attenuation_factory("constant", 1.0), ) block = arim.Material( longitudinal_vel=6320.0, transverse_vel=3130.0, density=2700.0, state_of_matter="solid", longitudinal_att=arim.material_attenuation_factory("constant", 2.0), transverse_att=arim.material_attenuation_factory("constant", 3.0), ) probe = arim.Probe.make_matrix_probe(5, 1e-3, 1, np.nan, 5e6) probe_element_width = 0.8e-3 probe.set_reference_element("first") probe.reset_position() probe.translate([0.0, 0.0, -5e-3]) probe.rotate(arim.geometry.rotation_matrix_y(np.deg2rad(10))) probe_p = probe.to_oriented_points() frontwall = arim.geometry.points_1d_wall_z(numpoints=1000, xmin=-5.0e-3, xmax=20.0e-3, z=0.0, name="Frontwall") backwall = arim.geometry.points_1d_wall_z(numpoints=1000, xmin=-5.0e-3, xmax=20.0e-3, z=30.0e-3, name="Backwall") scatterer_p = arim.geometry.default_oriented_points( arim.Points([[19e-3, 0.0, 20e-3]])) all_points = [probe_p, frontwall, backwall, scatterer_p] # import arim.plot as aplt # aplt.plot_interfaces(all_points, markers=['o', 'o', 'o', 'd'], # show_orientations=True) # aplt.plt.show() exam_obj = arim.BlockInImmersion(block, couplant, frontwall, backwall, scatterer_p) scat_obj = arim.scat.scat_factory(material=block, **scat_specs) scat_funcs = scat_obj.as_angles_funcs(probe.frequency) # compute only a subset of the FMC: first row and first column tx = np.zeros(probe.numelements * 2, np.int_) tx[:probe.numelements] = np.arange(probe.numelements) rx = np.zeros(probe.numelements * 2, np.int_) rx[probe.numelements:] = np.arange(probe.numelements) # Compute model views = bim.make_views(exam_obj, probe_p, scatterer_p, max_number_of_reflection=2) arim.ray.ray_tracing(views.values()) ray_weights = bim.ray_weights_for_views(views, probe.frequency, probe_element_width) lti_coefficients = collections.OrderedDict() for viewname, view in views.items(): amp_obj = arim.model.model_amplitudes_factory(tx, rx, view, ray_weights, scat_funcs) lti_coefficients[viewname] = amp_obj[0] # Test reciprocity for i, viewname in enumerate(views): viewname_r = arim.ut.reciprocal_viewname(viewname) lhs = lti_coefficients[viewname][:probe. numelements] # (tx=k, rx=0) for all k rhs = lti_coefficients[viewname_r][ probe.numelements:] # (tx=0, rx=k) for all k max_err = np.max(np.abs(lhs - rhs)) err_msg = "view {} (#{}) - max_err={}".format(viewname, i, max_err) tol = dict(rtol=1e-7, atol=1e-8) try: np.testing.assert_allclose(lhs, rhs, err_msg=err_msg, **tol) except AssertionError as e: if show_plots: import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2, sharex=True) ax = axes[0] ax.plot(lhs.real, label="tx=k, rx=0") ax.plot(rhs.real, label="tx=0, rx=k") ax.set_title(scat_obj.__class__.__name__ + "\n {} and {}".format(viewname, viewname_r)) ax.set_ylabel("real") ax.legend() ax = axes[1] ax.plot(lhs.imag, label="tx=k, rx=0") ax.plot(rhs.imag, label="tx=0, rx=k") ax.legend() ax.set_xlabel("element index k") ax.set_ylabel("imag") plt.show() raise e