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

re-worked cutoff_error parameter for Automatic linestrength cutoff #646

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 47 additions & 6 deletions radis/lbl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3087,7 +3087,7 @@ def calc_linestrength_noneq(self):

# %%
def calc_emission_integral(self):
r"""Calculate the emission integral (in :math:`mW/sr`) of all lines in DataFrame ``df1``.
"""Calculate the emission integral (in :math:`mW/sr`) of all lines in DataFrame ``df1``.

.. math::

Expand Down Expand Up @@ -3155,8 +3155,8 @@ def calc_emission_integral(self):
return

# %%
def _cutoff_linestrength(self, cutoff=None):
"""Discard linestrengths that are lower that this, to reduce
def _cutoff_linestrength(self, cutoff=None, cutoff_error=None):
r"""Discard linestrengths that are lower that this, to reduce
calculation times. Set the number of lines cut in
``self._Nlines_cutoff``

Expand All @@ -3167,6 +3167,10 @@ def _cutoff_linestrength(self, cutoff=None):
discard linestrengths that are lower that this, to reduce calculation
times. If 0, no cutoff. Default 0

cutoff_error: float
percentage below which to keep the estimated error,
adjusting the cutoff if necessary. If None, no error to consider. Default None.

Notes
-----

Expand All @@ -3179,9 +3183,12 @@ def _cutoff_linestrength(self, cutoff=None):
# Update defaults
if cutoff is not None:
self.params.cutoff = cutoff
if cutoff_error is not None:
self.params.cutoff_error = cutoff_error

# Load variables
cutoff = self.params.cutoff
cutoff_error = self.params.cutoff_error
verbose = self.verbose
df = self.df1

Expand Down Expand Up @@ -3212,10 +3219,44 @@ def _cutoff_linestrength(self, cutoff=None):

error = df.S[b].sum() / df.S.sum() * 100

if cutoff_error is not None:
if cutoff_error < error:
code29563 marked this conversation as resolved.
Show resolved Hide resolved
lines_by_intensity = df.S.sort_values()
cumsummed = lines_by_intensity.cumsum()
cond = cumsummed <= (cutoff_error / 100) * df.S.sum()
post_cutoff = lines_by_intensity[cond]
# to account for the unlikely case where the cutoff in cond lies between duplicate values:
max_value = post_cutoff.max()
if post_cutoff.value_counts()[max_value] < lines_by_intensity.value_counts()[max_value]:
b = df.S < max_value # exclude max_value as including it pushes us over cutoff_error
in_or_ex = 'exclusive'
else:
b = df.S <= max_value
in_or_ex = 'inclusive'
# Print current error
error = df.S[b].sum() / df.S.sum() * 100
print(
"Cutoff for discarded lines adjusted to {0} ({1}). ".format(max_value,in_or_ex)
+ "Current percentage error: {0:.2f}% ".format(error)
+ "Inputted error: {0:.2f}%".format(cutoff_error)
)

else:
print(
"Cutoff error is greater than the current error."
+ " Inputted cutoff error percentage is {0:.2f}%".format(
cutoff_error
)
+ " Estimated error: {0:.2f}%".format(error)
)
Nlines_cutoff = b.sum()
else:
print("Cutoff error not inputted.")

if verbose >= 2:
print(
"Discarded {0:.2f}% of lines (linestrength<{1}cm-1/(#.cm-2))".format(
Nlines_cutoff / len(df.S) * 100, cutoff
"Discarded {0:.2f}% of lines".format(
Nlines_cutoff / len(df.S) * 100
)
+ " Estimated error: {0:.2f}%".format(error)
)
Expand All @@ -3224,7 +3265,7 @@ def _cutoff_linestrength(self, cutoff=None):
"Estimated error after discarding lines is large: {0:.2f}%".format(
error
)
+ ". Consider reducing cutoff",
+ ". Consider reducing cutoff (or cutoff_error if adjusted)",
"LinestrengthCutoffWarning",
)

Expand Down
4 changes: 4 additions & 0 deletions radis/lbl/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ class Parameters(ConditionDict):
"neighbour_lines",
"chunksize",
"cutoff",
"cutoff_error",
"db_use_cached",
"dbformat",
"dbpath",
Expand Down Expand Up @@ -483,6 +484,9 @@ def __init__(self):
self.truncation = None #: float: cutoff for half-width lineshape calculation (cm-1). Overwritten by SpectrumFactory
self.neighbour_lines = None #: float: extra range (cm-1) on each side of the spectrum to account for neighbouring lines. Overwritten by SpectrumFactory
self.cutoff = None #: float: linestrength cutoff (molecule/cm)
self.cutoff_error = (
None #: float: error percentage below which the cutoff error should be
)
self.broadening_method = "" #: str:``"voigt"``, ``"convolve"``, ``"fft"``
self.optimization = None #: str: ``"simple"``, ``"min-RMS"``, ``None``
self.db_use_cached = (
Expand Down