Ejemplo n.º 1
0
def test_database_functions(verbose=True, plot=True, close_plots=True, warnings=True, *args, **kwargs):
    ''' Test SpecDatabase functions '''

    if plot:
        import matplotlib.pyplot as plt
        plt.ion()
    if close_plots:
        plt.close('all')

    import pytest

    db = SpecDatabase(dirname(getTestFile('.')))

    # Database visualisation methods
    if verbose:
        print('{0} items in test database: {1}'.format(
            len(db), db.see(['Tvib', 'Trot'])))
    if plot:
        db.plot_cond('Tvib', 'Trot')

    # Database get methods
    db.get_closest(Tgas=1300, path_length=1)
    s = db.get_unique(Tgas=1500, path_length=0.01,
                      mole_fraction=0.5)  # there should be one only
    # ... note that this is just so we can test
    # ... get_unique(). If we were to add new
    # ... test cases with matching conditions
    # ... let's add more criteria to keep it unique
    match = db.get(**s.conditions)
#    assert len(match) == 1
    # TODO: not working in Python 2.7 yet

    # Database add method
    s2 = s.copy()
    s2.conditions['Tgas'] = 0  # make it unique (for testing)
    
    matching_spectrum_in_db = (s2 in db)
    
    l = db.add(s2, add_info=['Tvib', 'Trot'], discard=[],
               compress=True, if_exists_then='increment')
    assert exists(l)
    try:
        assert s2 in db
        # .. ensures that you cant add it twice
        with pytest.raises(ValueError):
            db.add(s2, add_info=['Tvib', 'Trot'])
            
    # Now remove the Spectrum, update the database and ensures it's not there anymore
    finally:
        os.remove(l)
    db.update(force_reload=True)            # update database
    assert not exists(l)
    
    # make sure we deleted it properly
    if not matching_spectrum_in_db:
        assert s2 not in db
Ejemplo n.º 2
0
def test_lazy_loading(verbose=True, *args, **kwargs):
    """ Test lazy loading """

    import numpy as np

    from radis import calc_spectrum

    DBNAME = "test_CO_OH_database"
    db = SpecDatabase(DBNAME, lazy_loading=False)  # create the database

    #%% Simulate a fake database

    # Compute spectra of CO and OH and add them to the database
    T_db = np.linspace(300, 1200, 5)  # Temperature array
    for T in T_db:
        s1 = (
            calc_spectrum(
                wavenum_min=3000,
                wavenum_max=4500,
                molecule="CO",
                Tgas=T,
                databank="hitemp",  # test by fetching directly
                verbose=False,
            ).apply_slit(2, "nm").take("radiance")
            # TODO : use https://github.com/radis/radis/issues/135 once implemented
        )
        db.add(s1, store_name=f"CO_{T}K.spec", if_exists_then="ignore")
        s2 = (
            calc_spectrum(
                wavenum_min=3000,
                wavenum_max=4500,
                molecule="OH",
                Tgas=T,
                databank="hitemp",  # test by fetching directly
                verbose=False,
            ).apply_slit(2, "nm").take("radiance")
            # TODO : use https://github.com/radis/radis/issues/135 once implemented
        )
        db.add(s2, store_name=f"OH_{T}K.spec", if_exists_then="ignore")

    #%% Access the spectra in the database via the get functions.
    # Check what species are in the database at a given temperature (300)
    s300 = db.get(Tgas=300)  # simple use of get
    if verbose:
        print([s.conditions["molecule"] for s in s300])
    # Check what species are in the database at a given species
    sCO = db.get(molecule="CO")
    if verbose:
        print([s.conditions["Tgas"] for s in sCO])
    # Advanced use of the get function
    sdb = db.get('Tgas>=600 & Tgas<900 & molecule=="CO"')
    if verbose:
        print("Number of spectra found for the given conditions: ", len(sdb))

    #%% Do the same in lazy-loading mode, and compare :

    db2 = SpecDatabase(DBNAME, lazy_loading=True)  # create the database

    #%% Access the spectra in the database via the get functions.
    # Check what species are in the database at a given temperature (300)
    s300_2 = db2.get(Tgas=300)  # simple use of get
    print([s.conditions["molecule"] for s in s300_2])

    assert s300 == s300_2

    # Check what species are in the database at a given species
    sCO_2 = db2.get(molecule="CO")
    if verbose:
        print([s.conditions["Tgas"] for s in sCO_2])

    assert sCO == sCO_2

    # Advanced use of the get function
    sdb_2 = db2.get('Tgas>=600 & Tgas<900 & molecule=="CO"')
    if verbose:
        print("Number of spectra found for the given conditions: ", len(sdb))

    assert sdb == sdb_2