Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved staticmethods of line_info module to a dedicated plot_util module #1340

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 4 additions & 59 deletions tardis/widgets/line_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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,
Expand Down Expand Up @@ -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(
[
Expand All @@ -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,
),
Expand Down
63 changes: 63 additions & 0 deletions tardis/widgets/plot_util.py
Original file line number Diff line number Diff line change
@@ -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()