From caa3dafb0063fc42785e5b9bba7c40d8a74053bd Mon Sep 17 00:00:00 2001 From: Jaladh Singhal Date: Tue, 10 Nov 2020 20:44:56 +0530 Subject: [PATCH] Moved helper staticmethods to a dedicated module --- tardis/widgets/line_info.py | 63 +++---------------------------------- tardis/widgets/plot_util.py | 63 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 tardis/widgets/plot_util.py diff --git a/tardis/widgets/line_info.py b/tardis/widgets/line_info.py index 110aacc9887..17ece8d5d14 100644 --- a/tardis/widgets/line_info.py +++ b/tardis/widgets/line_info.py @@ -12,6 +12,7 @@ from tardis.analysis import LastLineInteraction from tardis.util.base import species_tuple_to_string, species_string_to_tuple from tardis.widgets.util import create_table_widget, TableSummaryLabel +from tardis.widgets import plot_util as pu class LineInfoWidget: @@ -392,41 +393,6 @@ class variable :code:`GROUP_MODES` (default value is ) return last_line_counts.sort_values(ascending=False).to_frame() - @staticmethod - def axis_label_in_latex(label_text, unit): - """ - Get axis label for plotly plots that can show units in latex. - - Parameters - ---------- - label_text : str - Text to show on label - unit : astropy.units - Unit of the label - - Returns - ------- - str - Latex string for label renderable by plotly - """ - unit_in_latex = unit.to_string("latex_inline").strip("$") - - # If present, place s^{-1} just after erg - if "erg" in unit_in_latex and "s^{-1}" in unit_in_latex: - constituent_units = ( - re.compile("\\\mathrm\{(.*)\}") - .findall(unit_in_latex)[0] - .split("\\,") - ) - constituent_units.remove("s^{-1}") - constituent_units.insert( - constituent_units.index("erg") + 1, "s^{-1}" - ) - constituent_units_string = "\\,".join(constituent_units) - unit_in_latex = f"\\mathrm{{{constituent_units_string}}}" - - return f"$\\text{{{label_text}}}\\,[{unit_in_latex}]$" - @staticmethod def get_middle_half_edges(arr): """ @@ -446,25 +412,6 @@ def get_middle_half_edges(arr): (arr[-1] - arr[0]) * 3 / 4 + arr[1], ] - @staticmethod - def get_mid_point_idx(arr): - """ - Get index of the middle point of a sorted array (ascending or descending). - - The values in array may not be evenly distributed so it picks the middle - point not by index but by their values. - - Parameters - ---------- - arr : np.array - - Returns - ------- - int - """ - mid_value = (arr[0] + arr[-1]) / 2 - return np.abs(arr - mid_value).argmin() - def plot_spectrum( self, wavelength, @@ -497,7 +444,7 @@ def plot_spectrum( # The scatter point should be a middle point in spectrum otherwise # the extra padding around it will be oddly visible when near the edge - scatter_point_idx = self.get_mid_point_idx(wavelength.value) + scatter_point_idx = pu.get_mid_point_idx(wavelength.value) return go.FigureWidget( [ @@ -523,15 +470,13 @@ def plot_spectrum( layout=go.Layout( title="Spectrum", xaxis=dict( - title=self.axis_label_in_latex( - "Wavelength", wavelength.unit - ), + title=pu.axis_label_in_latex("Wavelength", wavelength.unit), exponentformat="none", rangeslider=dict(visible=True), range=initial_zoomed_range, ), yaxis=dict( - title=self.axis_label_in_latex( + title=pu.axis_label_in_latex( "Luminosity", luminosity_density_lambda.unit, ), diff --git a/tardis/widgets/plot_util.py b/tardis/widgets/plot_util.py new file mode 100644 index 00000000000..57f5d2d8586 --- /dev/null +++ b/tardis/widgets/plot_util.py @@ -0,0 +1,63 @@ +"""Utility functions to be used in plotting.""" + +import re +import numpy as np + + +def axis_label_in_latex(label_text, unit, only_text=True): + """ + Get axis label for plotly plots that can show units in latex. + + Parameters + ---------- + label_text : str + Text to show on label, may be expressed in latex + unit : astropy.units + Unit of the label which needs to be expressed in latex + only_text : bool + If label_text is expressed purely in text (i.e. without + using latex) or not. Default value is True + + + Returns + ------- + str + Latex string for label renderable by plotly + """ + unit_in_latex = unit.to_string("latex_inline").strip("$") + + # If present, place s^{-1} just after erg + if "erg" in unit_in_latex and "s^{-1}" in unit_in_latex: + constituent_units = ( + re.compile("\\\mathrm\{(.*)\}") + .findall(unit_in_latex)[0] + .split("\\,") + ) + constituent_units.remove("s^{-1}") + constituent_units.insert(constituent_units.index("erg") + 1, "s^{-1}") + constituent_units_string = "\\,".join(constituent_units) + unit_in_latex = f"\\mathrm{{{constituent_units_string}}}" + + if only_text: + return f"$\\text{{{label_text}}}\\,[{unit_in_latex}]$" + else: + return f"${label_text}\\,[{unit_in_latex}]$" + + +def get_mid_point_idx(arr): + """ + Get index of the middle point of a sorted array (ascending or descending). + + The values in array may not be evenly distributed so it picks the middle + point not by index but by their values. + + Parameters + ---------- + arr : np.array + + Returns + ------- + int + """ + mid_value = (arr[0] + arr[-1]) / 2 + return np.abs(arr - mid_value).argmin()