|
| 1 | +"""MTF versus Field Analysis |
| 2 | +
|
| 3 | +This module enables the calculation of the Modulation Transfer Function (MTF) |
| 4 | +versus field coordinate of an optical system. |
| 5 | +
|
| 6 | +Kramer Harrison, 2026 |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from typing import TYPE_CHECKING |
| 12 | + |
| 13 | +import matplotlib.pyplot as plt |
| 14 | + |
| 15 | +import optiland.backend as be |
| 16 | +from optiland.analysis.base import BaseAnalysis |
| 17 | +from optiland.mtf.sampled import SampledMTF |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from matplotlib.axes import Axes |
| 21 | + from matplotlib.figure import Figure |
| 22 | + |
| 23 | + from optiland.optic import Optic |
| 24 | + |
| 25 | + |
| 26 | +class MTFvsField(BaseAnalysis): |
| 27 | + """MTF versus Field Coordinate. |
| 28 | +
|
| 29 | + This class is used to analyze the Modulation Transfer Function (MTF) versus |
| 30 | + the field coordinate of an optical system for specified spatial frequencies. |
| 31 | +
|
| 32 | + Args: |
| 33 | + optic (Optic): the optical system. |
| 34 | + frequencies (list[float]): the spatial frequencies (in cycles/mm) to analyze. |
| 35 | + num_fields (int): the number of fields in the Y direction. Default is 32. |
| 36 | + wavelengths (str or list): the wavelengths to be analyzed. Default is 'all'. |
| 37 | + num_rays (int): the number of rays across the pupil in 1D for the SampledMTF |
| 38 | + calculation. Default is 128. |
| 39 | + override_limits (bool): If True, bypasses the limit on the number of frequencies |
| 40 | + and wavelengths to prevent cluttered plots. Default is False. |
| 41 | +
|
| 42 | + """ |
| 43 | + |
| 44 | + MAX_FREQUENCIES = 5 |
| 45 | + MAX_WAVELENGTHS = 3 |
| 46 | + |
| 47 | + def __init__( |
| 48 | + self, |
| 49 | + optic: Optic, |
| 50 | + frequencies: list[float], |
| 51 | + num_fields: int = 32, |
| 52 | + wavelengths: str | list[float] = "all", |
| 53 | + num_rays: int = 128, |
| 54 | + override_limits: bool = False, |
| 55 | + ): |
| 56 | + self.frequencies = frequencies |
| 57 | + self.num_fields = num_fields |
| 58 | + self.num_rays = num_rays |
| 59 | + |
| 60 | + self._check_limits(override_limits, wavelengths, optic) |
| 61 | + |
| 62 | + # Base Analysis will set self.wavelengths |
| 63 | + super().__init__(optic, wavelengths) |
| 64 | + |
| 65 | + def _check_limits(self, override_limits: bool, wavelengths, optic): |
| 66 | + """Check to ensure inputs won't produce an overly cluttered plot.""" |
| 67 | + if override_limits: |
| 68 | + return |
| 69 | + |
| 70 | + if len(self.frequencies) > self.MAX_FREQUENCIES: |
| 71 | + raise ValueError( |
| 72 | + f"Number of frequencies ({len(self.frequencies)}) exceeds the " |
| 73 | + f"recommended limit of {self.MAX_FREQUENCIES} for clean plots. " |
| 74 | + "Set override_limits=True to bypass this check." |
| 75 | + ) |
| 76 | + |
| 77 | + from optiland.utils import resolve_wavelengths |
| 78 | + |
| 79 | + resolved_wls = resolve_wavelengths(optic, wavelengths) |
| 80 | + num_wl = len(resolved_wls) |
| 81 | + |
| 82 | + if num_wl > self.MAX_WAVELENGTHS: |
| 83 | + raise ValueError( |
| 84 | + f"Number of wavelengths ({num_wl}) exceeds the recommended " |
| 85 | + f"limit of {self.MAX_WAVELENGTHS} for clean plots. " |
| 86 | + "Set override_limits=True to bypass this check." |
| 87 | + ) |
| 88 | + |
| 89 | + def _generate_data(self): |
| 90 | + """Generate the MTF data across fields, wavelengths, and frequencies.""" |
| 91 | + fields = [(0.0, float(Hy)) for Hy in be.linspace(0.0, 1.0, self.num_fields)] |
| 92 | + self._field_coords = be.array(fields) |
| 93 | + |
| 94 | + # Pre-build list of frequencies to calculate at once |
| 95 | + freqs_to_calc = [] |
| 96 | + for freq in self.frequencies: |
| 97 | + freqs_to_calc.append((freq, 0.0)) |
| 98 | + freqs_to_calc.append((0.0, freq)) |
| 99 | + |
| 100 | + results = [] |
| 101 | + for wl in self.wavelengths: |
| 102 | + wl_results = [{"tangential": [], "sagittal": []} for _ in self.frequencies] |
| 103 | + |
| 104 | + for field in fields: |
| 105 | + sampled_mtf = SampledMTF( |
| 106 | + optic=self.optic, |
| 107 | + field=field, |
| 108 | + wavelength=wl, |
| 109 | + num_rays=self.num_rays, |
| 110 | + distribution="uniform", |
| 111 | + zernike_terms=37, |
| 112 | + zernike_type="fringe", |
| 113 | + ) |
| 114 | + |
| 115 | + mtfs = sampled_mtf.calculate_mtf(freqs_to_calc) |
| 116 | + |
| 117 | + for i_freq in range(len(self.frequencies)): |
| 118 | + wl_results[i_freq]["tangential"].append(mtfs[2 * i_freq]) |
| 119 | + wl_results[i_freq]["sagittal"].append(mtfs[2 * i_freq + 1]) |
| 120 | + |
| 121 | + for i_freq in range(len(self.frequencies)): |
| 122 | + wl_results[i_freq]["tangential"] = be.array( |
| 123 | + wl_results[i_freq]["tangential"] |
| 124 | + ) |
| 125 | + wl_results[i_freq]["sagittal"] = be.array( |
| 126 | + wl_results[i_freq]["sagittal"] |
| 127 | + ) |
| 128 | + |
| 129 | + results.append(wl_results) |
| 130 | + |
| 131 | + return results |
| 132 | + |
| 133 | + def view( |
| 134 | + self, |
| 135 | + fig_to_plot_on: Figure | None = None, |
| 136 | + figsize: tuple[float, float] = (8, 5), |
| 137 | + ) -> tuple[Figure, Axes]: |
| 138 | + """ |
| 139 | + Plots the MTF versus the field coordinate for each frequency and wavelength. |
| 140 | +
|
| 141 | + Args: |
| 142 | + fig_to_plot_on (Figure, optional): An existing matplotlib Figure to |
| 143 | + plot on. If provided, the plot will be embedded in this figure. |
| 144 | + If None (default), a new figure will be created. |
| 145 | + figsize (tuple[float, float], optional): Size of the figure to create |
| 146 | + if `fig_to_plot_on` is None. Defaults to (8, 5). |
| 147 | +
|
| 148 | + Returns: |
| 149 | + tuple[Figure, Axes]: The matplotlib Figure and Axes objects |
| 150 | + containing the plot. |
| 151 | + """ |
| 152 | + is_gui_embedding = fig_to_plot_on is not None |
| 153 | + |
| 154 | + if is_gui_embedding: |
| 155 | + current_fig = fig_to_plot_on |
| 156 | + current_fig.clear() |
| 157 | + ax = current_fig.add_subplot(111) |
| 158 | + else: |
| 159 | + current_fig, ax = plt.subplots(figsize=figsize) |
| 160 | + |
| 161 | + max_field = float(self.optic.fields.max_field) |
| 162 | + y_coords_normalized = be.to_numpy(self._field_coords[:, 1]) |
| 163 | + x_plot = y_coords_normalized * max_field |
| 164 | + |
| 165 | + # Determine X-axis label |
| 166 | + field_def = self.optic.field_definition |
| 167 | + x_label = "Field Coordinate" |
| 168 | + if field_def is not None: |
| 169 | + field_name = field_def.__class__.__name__ |
| 170 | + if "Angle" in field_name: |
| 171 | + x_label = "Angle (deg)" |
| 172 | + elif "Height" in field_name: |
| 173 | + x_label = "Height (mm)" |
| 174 | + else: |
| 175 | + # Fallback if no specific type is set but fields exist |
| 176 | + x_label = "Field Coordinate" |
| 177 | + |
| 178 | + axes_color_cycle = plt.rcParams["axes.prop_cycle"].by_key()["color"] |
| 179 | + |
| 180 | + for i_wl, wavelength in enumerate(self.wavelengths): |
| 181 | + for i_freq, freq in enumerate(self.frequencies): |
| 182 | + color_idx = (i_wl * len(self.frequencies) + i_freq) % len( |
| 183 | + axes_color_cycle |
| 184 | + ) |
| 185 | + color = axes_color_cycle[color_idx] |
| 186 | + |
| 187 | + tan_data = be.to_numpy(self.data[i_wl][i_freq]["tangential"]) |
| 188 | + sag_data = be.to_numpy(self.data[i_wl][i_freq]["sagittal"]) |
| 189 | + |
| 190 | + label_prefix = f"{freq} cyc/mm" |
| 191 | + if len(self.wavelengths) > 1: |
| 192 | + label_prefix += f", {wavelength:.4f} µm" |
| 193 | + |
| 194 | + ax.plot( |
| 195 | + x_plot, |
| 196 | + tan_data, |
| 197 | + linestyle="-", |
| 198 | + color=color, |
| 199 | + label=f"{label_prefix} (Tan)", |
| 200 | + ) |
| 201 | + ax.plot( |
| 202 | + x_plot, |
| 203 | + sag_data, |
| 204 | + linestyle="--", |
| 205 | + color=color, |
| 206 | + label=f"{label_prefix} (Sag)", |
| 207 | + ) |
| 208 | + |
| 209 | + ax.set_xlabel(x_label) |
| 210 | + ax.set_ylabel("Modulus of the OTF") |
| 211 | + ax.legend(bbox_to_anchor=(1.05, 0.5), loc="center left") |
| 212 | + |
| 213 | + if max_field > 0: |
| 214 | + ax.set_xlim(0, max_field) |
| 215 | + |
| 216 | + ax.set_ylim(0, 1.05) |
| 217 | + ax.grid(True, linestyle=":", alpha=0.5) |
| 218 | + current_fig.tight_layout() |
| 219 | + |
| 220 | + if is_gui_embedding and hasattr(current_fig, "canvas"): |
| 221 | + current_fig.canvas.draw_idle() |
| 222 | + |
| 223 | + return current_fig, ax |
0 commit comments