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

Added cutoff_error parameter to enable Automatic Linestrength Cutoff #451

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
31 changes: 29 additions & 2 deletions radis/lbl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3077,7 +3077,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 @@ -3145,7 +3145,7 @@ def calc_emission_integral(self):
return

# %%
def _cutoff_linestrength(self, cutoff=None):
def _cutoff_linestrength(self, cutoff=None, cutoff_error=None):
"""Discard linestrengths that are lower that this, to reduce
calculation times. Set the number of lines cut in
``self._Nlines_cutoff``
Expand All @@ -3157,6 +3157,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
user-inputted value of cutoff error, to keep the estimated error
below this value. If None, no error to consider. Default None.

Notes
-----

Expand All @@ -3169,9 +3173,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 @@ -3202,6 +3209,26 @@ 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:
# Remove additional lines such that cutoff error is less than user-inputted value
while cutoff_error < error:
cutoff -= 0.0001
b = df.S <= cutoff
error = df.S[b].sum() / df.S.sum() * 100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about :
1 . we sort (a copy of) the lines by intensity intensity
2. we compute the cumulated intensity on the sorted array [last value is the total sum]
3. we find the index in the sorted array so that cumulated-intensity is ERROR_fraction * the total

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this seems to be a much better option. I'll work on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know. It will be important to also add tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @sagarchotalia ,did you try the approach I suggested above ?

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(
Expand Down
4 changes: 4 additions & 0 deletions radis/lbl/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ class Parameters(ConditionDict):
"neighbour_lines",
"chunksize",
"cutoff",
"cutoff_error",
"db_use_cached",
"dbformat",
"dbpath",
Expand Down Expand Up @@ -477,6 +478,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