Skip to content

Commit

Permalink
Merge pull request #315 from anandxkumar/example
Browse files Browse the repository at this point in the history
Add: SpecDatabase Example in Gallery
  • Loading branch information
erwanp committed Jul 15, 2021
2 parents 081c0e8 + 787bb1c commit c2c2c2f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
60 changes: 60 additions & 0 deletions examples/plot_SpecDatabase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
================================================
Spectrum Database
================================================
RADIS has :py:class:`~radis.tools.database.SpecDatabase` feature used to store and retrieve calculated Spectrums. A path can be specified for SpecDatabase all Spectrums are stored as .spec files which can be loaded
from the SpecDatabase object itself. A csv file is generated which contains all input and conditional parameters of Spectrum.
RADIS also has :py:meth:`~radis.lbl.loader.DatabankLoader.init_database` feature which initializes the SpecDatabase for the SpectrumFactory and every Spectrum
generated from it will be stored in the SpecDatabase automatically.
You can use :py:meth:`~radis.tools.database.SpecList.plot_cond` to make a 2D plot using the conditions of the Spectrums in the SpecDatabase and use a z_label to plot a heat map based on it.
"""

from radis import SpectrumFactory
from radis.tools import SpecDatabase

sf = SpectrumFactory(
wavenum_min=2900,
wavenum_max=3200,
molecule="OH",
broadening_max_width=10, # cm-1
medium="vacuum",
verbose=0, # more for more details
pressure=10,
wstep="auto",
)
sf.fetch_databank("hitemp")

# Generating 2 Spectrums
s1 = sf.eq_spectrum(Tgas=300, path_length=1)
s2 = sf.eq_spectrum(Tgas=600, path_length=1)

# Creating SpecDatabase
my_folder = r"/home/pipebomb/Desktop/SpecDatabase_Test/"
db = SpecDatabase(my_folder, lazy_loading=False)

# Method 1: Creating .spec file
db.add(s1)
db.add(s2)

# Method 2: Using init_database()
sf.init_database(my_folder)

# Generates Spectrum and adds to SpecDatabase automatically
# Increasing Temperature and decreasing wstep
wstep_value = 2
for i in range(900, 3100, 300):
wstep_value = wstep_value / 2
sf.wstep = wstep_value
sf.params.wstep = wstep_value
sf.eq_spectrum(Tgas=i, path_length=1)


# Loading SpecDatabase
db_new = SpecDatabase(my_folder)

# Comparing data conditions of different spectrum from csv generated file
db_new.plot_cond("Tgas", "wstep", "calculation_time")
4 changes: 4 additions & 0 deletions radis/lbl/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,7 @@ def init_database(
"""Init a :class:`~radis.tools.database.SpecDatabase` folder in
``path`` to later store our spectra. Spectra can also be automatically
retrieved from the database instead of being calculated.
Parameters
----------
path: str
Expand Down Expand Up @@ -1362,6 +1363,9 @@ def init_database(
-------
db: SpecDatabase
the database where spectra will be stored or retrieved
.. minigallery:: radis.lbl.loader.DatabankLoader.init_database
"""

db = SpecDatabase(path, add_info=add_info, add_date=add_date, binary=compress)
Expand Down
18 changes: 13 additions & 5 deletions radis/tools/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,8 @@ def plot_cond(self, cond_x, cond_y, z_value=None, nfig=None):
>>> db.plot(Tvib, Trot, residual) # where residual is calculated by a fitting
# procedure...
-------
.. minigallery:: radis.tools.database.SpecList.plot_cond
"""
# %%

Expand All @@ -1686,17 +1688,21 @@ def plot_cond(self, cond_x, cond_y, z_value=None, nfig=None):

# Overlay color
if z_value is not None:
assert (len(z_value)) == len(self.df)
if type(z_value) is str:
z = self.df[z_value]
else:
z = z_value

z = np.array(z_value) ** 0.5 # because the lower the better
assert len(z) == len(self.df)

z = np.array(z) ** 0.5 # because the lower the better
# norm = cm.colors.Normalize(vmax=z.max(), vmin=z.min())
# cmap = cm.PRGn

xarr = np.linspace(min(x), max(x))
yarr = np.linspace(min(y), max(y))
mx, my = np.meshgrid(xarr, yarr)
zgrid = griddata((x, y), z, (mx, my), method="linear")
zgrid = griddata((x, y), z, (mx, my), method="nearest")
levels = np.linspace(min(z), max(z), 20)
ax.contourf(
mx,
Expand Down Expand Up @@ -1805,7 +1811,6 @@ class SpecDatabase(SpecList):
requires all conditions to be either float, string, or boolean. List
won't work!
See Also
--------
:func:`~radis.tools.database.load_spec`,
Expand All @@ -1827,7 +1832,10 @@ class SpecDatabase(SpecList):
Compare another Spectrum to all spectra in the database:
:meth:`~radis.tools.database.SpecDatabase.fit_spectrum`,
:meth:`~radis.tools.database.SpecDatabase.fit_spectrum`
.. minigallery:: radis.SpecDatabase
"""

def __init__(
Expand Down

0 comments on commit c2c2c2f

Please sign in to comment.