.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/inverse_problems/objective_landscape.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_inverse_problems_objective_landscape.py: Explore a synthetic objective landscape ======================================= Compare candidate absorption coefficients with noiseless fluence values from an infinite-medium P1 calculation. The true and candidate data use the same model; this illustrates an objective function, not recovery accuracy for a real sensor. All values are illustrative. .. GENERATED FROM PYTHON SOURCE LINES 10-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import MomentRT ureg = MomentRT.ureg .. GENERATED FROM PYTHON SOURCE LINES 19-24 Set up the forward calculation ------------------------------ The Cartesian mesh is an observation window, not a physical boundary. An even number of points keeps the ideal point-source singularity off the grid. The source power, scattering and all other parameters are treated as known. .. GENERATED FROM PYTHON SOURCE LINES 24-37 .. code-block:: Python mesh = MomentRT.CartesianMesh( shape=(20, 20, 20), spacing=np.full(3, 0.4) * ureg.mm, origin=np.full(3, -3.8) * ureg.mm, ) source = MomentRT.PointSource( position=np.zeros(3) * ureg.mm, wavelength=850 * ureg.nm, optical_power=1 * ureg.mW, ) solver = MomentRT.InfiniteP1() .. GENERATED FROM PYTHON SOURCE LINES 38-42 Generate synthetic observations ------------------------------- Sample two nodal fluence values, not detector powers. The observations and predictions share this exact forward model; no noise or uncertainty is added. .. GENERATED FROM PYTHON SOURCE LINES 42-79 .. code-block:: Python true_absorption = 0.02 sample_indices = ((12, 10, 10), (16, 10, 10)) def calculate_fluence(*, absorption_per_mm: float) -> MomentRT.SimulationResult: """Calculate the field at a specified absorption in inverse millimeters. Parameters ---------- absorption_per_mm : float Absorption coefficient in inverse millimeters. Returns ------- SimulationResult Infinite-medium fluence sampled on the observation grid. """ material = MomentRT.Material( absorption=absorption_per_mm / ureg.mm, reduced_scattering=1 / ureg.mm, refractive_index=1.4, phase_function=MomentRT.Isotropic(), ) simulation = MomentRT.Simulation( medium=MomentRT.HomogeneousMedium(material=material), source=source, mesh=mesh, solver=solver, ) return simulation.run() observed_result = calculate_fluence(absorption_per_mm=true_absorption) observed_field = observed_result.fluence.values.to("W/m^2").magnitude observations = np.array([observed_field[index] for index in sample_indices]) .. GENERATED FROM PYTHON SOURCE LINES 80-85 Evaluate the mismatch --------------------- Sum squared relative residuals so the two observations have equal relative weight. This weighting is illustrative and is not a measurement noise model. A minimum at the true value is expected because both sides use the same model. .. GENERATED FROM PYTHON SOURCE LINES 85-103 .. code-block:: Python candidates = np.linspace(0.005, 0.04, 36) objective = [] for candidate in candidates: result = calculate_fluence(absorption_per_mm=float(candidate)) field = result.fluence.values.to("W/m^2").magnitude predictions = np.array([field[index] for index in sample_indices]) objective.append(np.sum(((predictions - observations) / observations) ** 2)) figure, axes = plt.subplots() axes.plot(candidates, objective, label="Relative squared residual") axes.axvline(true_absorption, color="black", linestyle="--", label="True value") axes.set_xlabel("Absorption coefficient (mm$^{-1}$)") axes.set_ylabel("Sum of squared relative residuals") axes.set_title("Synthetic one-parameter objective") axes.legend() figure.tight_layout() .. image-sg:: /gallery/inverse_problems/images/sphx_glr_objective_landscape_001.png :alt: Synthetic one-parameter objective :srcset: /gallery/inverse_problems/images/sphx_glr_objective_landscape_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 104-110 Inspect the synthetic fluence field ----------------------------------- This field belongs to the true absorption. The box is only an observation window; the example does not model contact detectors or finite boundaries. A well-defined minimum here does not establish identifiability with noise, unknown nuisance parameters, different models, or experimental data. .. GENERATED FROM PYTHON SOURCE LINES 110-118 .. code-block:: Python gallery_system_figure = observed_result.fluence.plot( view="volume", scale="log", overlays=[source], show=False, ) gallery_system_figure.update_layout(title="Infinite-medium fluence: observation window") .. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.201 seconds) .. _sphx_glr_download_gallery_inverse_problems_objective_landscape.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: objective_landscape.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: objective_landscape.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: objective_landscape.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_