From aae88b2b7664de587899b31e49958f86796267a6 Mon Sep 17 00:00:00 2001 From: donato_ax Date: Mon, 5 May 2025 12:04:41 +0200 Subject: [PATCH 1/3] Better plots --- src/axiomatic/pic_helpers.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/axiomatic/pic_helpers.py b/src/axiomatic/pic_helpers.py index b72ba55..7cfc773 100644 --- a/src/axiomatic/pic_helpers.py +++ b/src/axiomatic/pic_helpers.py @@ -112,9 +112,9 @@ def plot_interactive_spectra( A list of spectra, where each spectrum is a list of lists of float values, each corresponding to the transmission of a single wavelength. wavelengths : list of float - A list of wavelength values corresponding to the x-axis of the plot. + A list of wavelength values corresponding to the x-axis of the plot, in nm. vlines : list of float, optional - A list of x-values where vertical lines should be drawn. Defaults to an empty list. + A list of x-values where vertical lines should be drawn, in nm. Defaults to an empty list. hlines : list of float, optional A list of y-values where horizontal lines should be drawn. Defaults to an empty list. """ @@ -149,9 +149,16 @@ def plot_interactive_spectra( all_vals = [val for spec in spectra for iteration in spec for val in iteration] y_min = min(all_vals) y_max = max(all_vals) - if hlines: - y_min = min(hlines + [y_min]) * 0.95 - y_max = max(hlines + [y_max]) * 1.05 + + # dB scale + if y_max <= 0: + y_max = 0 + db = True + else: + db = False + if hlines: + y_min = min(hlines + [y_min]) * 0.95 + y_max = max(hlines + [y_max]) * 1.05 # Create hlines and vlines shapes = [] @@ -187,8 +194,8 @@ def plot_interactive_spectra( # Create the layout fig.update_layout( - xaxis_title="Wavelength", - yaxis_title="Transmission", + xaxis_title="Wavelength (nm)", + yaxis_title="Transmission " + "(dB)" if db else "(linear)", shapes=shapes, sliders=sliders, yaxis=dict(range=[y_min, y_max]), @@ -454,10 +461,10 @@ def print_statements( def _str_units_to_float(str_units: str) -> Optional[float]: unit_conversions = { - "nm": 1e-3, - "um": 1, - "mm": 1e3, - "m": 1e6, + "nm": 1, + "um": 1e3, + "mm": 1e6, + "m": 1e9, } match = re.match(r"([\d\.]+)\s*([a-zA-Z]+)", str_units) numeric_value = float(match.group(1)) if match else None @@ -469,7 +476,7 @@ def get_wavelengths_to_plot(statements: StatementDictionary, num_samples: int = """ Get the wavelengths to plot based on the statements. - Returns a list of wavelengths to plot the spectra and a list of vertical lines to plot on top the spectra. + Returns a list of wavelengths to plot the spectra and a list of vertical lines to plot on top the spectra, in nm. """ min_wl = float("inf") @@ -511,8 +518,8 @@ def update_wavelengths(mapping: Dict[str, Optional[Computation]], min_wl: float, min_wl = min(min_wl, min(vlines)) max_wl = max(max_wl, max(vlines)) if min_wl >= max_wl: - avg_wl = sum(vlines) / len(vlines) if vlines else 1.55 - min_wl, max_wl = avg_wl - 0.01, avg_wl + 0.01 + avg_wl = sum(vlines) / len(vlines) if vlines else _str_units_to_float("1550 nm") + min_wl, max_wl = avg_wl - _str_units_to_float("10 nm"), avg_wl + _str_units_to_float("10 nm") else: range_size = max_wl - min_wl min_wl -= 0.2 * range_size From 324b70ab25a4fd4cbbc783bb89c3a9e5572f50a7 Mon Sep 17 00:00:00 2001 From: donato_ax Date: Mon, 5 May 2025 12:33:00 +0200 Subject: [PATCH 2/3] Fix bug --- src/axiomatic/pic_helpers.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/axiomatic/pic_helpers.py b/src/axiomatic/pic_helpers.py index 7cfc773..7c4eee8 100644 --- a/src/axiomatic/pic_helpers.py +++ b/src/axiomatic/pic_helpers.py @@ -112,12 +112,17 @@ def plot_interactive_spectra( A list of spectra, where each spectrum is a list of lists of float values, each corresponding to the transmission of a single wavelength. wavelengths : list of float - A list of wavelength values corresponding to the x-axis of the plot, in nm. + A list of wavelength values corresponding to the x-axis of the plot, in um. vlines : list of float, optional - A list of x-values where vertical lines should be drawn, in nm. Defaults to an empty list. + A list of x-values where vertical lines should be drawn, in um. Defaults to an empty list. hlines : list of float, optional A list of y-values where horizontal lines should be drawn. Defaults to an empty list. """ + + # Convert wavelengths to nm + wavelengths = [wl*1e3 for wl in wavelengths] + vlines = [wl*1e3 for wl in vlines] + if isinstance(spectra, dict): port_keys = [] for key in spectra: @@ -460,11 +465,13 @@ def print_statements( def _str_units_to_float(str_units: str) -> Optional[float]: + """Returns the numeric value of a string with units in micrometers, e.g. '1550 nm' -> 1.55""" + """Return None if the string is not a valid unit.""" unit_conversions = { - "nm": 1, - "um": 1e3, - "mm": 1e6, - "m": 1e9, + "nm": 1e-3, + "um": 1, + "mm": 1e3, + "m": 1e6, } match = re.match(r"([\d\.]+)\s*([a-zA-Z]+)", str_units) numeric_value = float(match.group(1)) if match else None @@ -472,11 +479,11 @@ def _str_units_to_float(str_units: str) -> Optional[float]: return float(numeric_value * unit_conversions[unit]) if unit in unit_conversions and numeric_value is not None else None -def get_wavelengths_to_plot(statements: StatementDictionary, num_samples: int = 100) -> Tuple[List[float], List[float]]: +def get_wavelengths_to_plot(statements: StatementDictionary, num_samples: int = 1000) -> Tuple[List[float], List[float]]: """ Get the wavelengths to plot based on the statements. - Returns a list of wavelengths to plot the spectra and a list of vertical lines to plot on top the spectra, in nm. + Returns a list of wavelengths to plot the spectra and a list of vertical lines to plot on top the spectra, in um. """ min_wl = float("inf") From 5396edd59b698796d3609846397482b9695b8db3 Mon Sep 17 00:00:00 2001 From: donato_ax Date: Mon, 5 May 2025 12:37:57 +0200 Subject: [PATCH 3/3] Mypy --- src/axiomatic/pic_helpers.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/axiomatic/pic_helpers.py b/src/axiomatic/pic_helpers.py index 7c4eee8..1a979e6 100644 --- a/src/axiomatic/pic_helpers.py +++ b/src/axiomatic/pic_helpers.py @@ -119,9 +119,11 @@ def plot_interactive_spectra( A list of y-values where horizontal lines should be drawn. Defaults to an empty list. """ + hlines = hlines or [] + # Convert wavelengths to nm wavelengths = [wl*1e3 for wl in wavelengths] - vlines = [wl*1e3 for wl in vlines] + vlines = [wl*1e3 for wl in vlines] if vlines else [] if isinstance(spectra, dict): port_keys = [] @@ -142,10 +144,6 @@ def plot_interactive_spectra( elif spectrum_labels is None: spectrum_labels = [f"Spectrum {i}" for i in range(len(spectra))] - if vlines is None: - vlines = [] - if hlines is None: - hlines = [] if isinstance(spectra, dict): spectra = list(spectra.values())