Exemplo n.º 1
0
def test_random_state():

    # Testing that the `random_state` kwarg works as expected on the system

    # Arbitrary wavelength wavelength points
    wavelengths = np.linspace(8541, 8544, 49)

    # Initialise model
    model = IBIS8542Model(original_wavelengths=wavelengths, random_state=0)

    # Get sample classifications
    X, y = make_classification(200,
                               49,
                               n_classes=5,
                               n_informative=4,
                               random_state=0)

    # Training #1
    model.train(X[::2], y[::2])
    score_a = cross_val_score(model.neural_network, X[1::2], y[1::2])

    # Training #2
    model.train(X[::2], y[::2])
    score_b = cross_val_score(model.neural_network, X[1::2], y[1::2])

    assert score_b == pytest.approx(score_a)
    assert score_b == pytest.approx(np.array([0.45, 0.35, 0.45, 0.45, 0.35]))
Exemplo n.º 2
0
def test_ibis8542model_basic():
    # Will break if default parameters are changes in mcalf.models.IBIS8542Model
    wl = 1000.97
    x_orig = np.linspace(999.81, 1002.13, num=25)
    prefilter_main = 1 - np.abs(x_orig - wl) * 0.1
    prefilter_wvscl = x_orig - wl
    m = IBIS8542Model(stationary_line_core=wl,
                      original_wavelengths=x_orig,
                      prefilter_ref_main=prefilter_main,
                      prefilter_ref_wvscl=prefilter_wvscl)

    bg = 1327.243
    arr = voigt(x_orig, -231.42, wl + 0.05, 0.2, 0.21, bg)

    m.load_array(np.array([arr, arr]), names=['row', 'wavelength'])
    m.load_background(np.array([bg, bg]), names=['row'])

    # Fit with assumed neural network classification
    fit1, fit2 = m.fit(row=[0, 1], classifications=np.array([0, 0]))
    fit3 = m.fit_spectrum(arr, classifications=0,
                          background=bg)  # Make sure this is equiv
    truth = [-215.08275199, 1001.01035476, 0.00477063, 0.28869302]

    assert np.array_equal(fit1.parameters, fit2.parameters)
    assert np.array_equal(fit1.parameters, fit3.parameters)
    assert list(fit1.parameters) == pytest.approx(truth)
Exemplo n.º 3
0
def test_ibis8542model_validate_parameters(valid_kwargs):

    stationary_line_core = valid_kwargs['stationary_line_core']
    defaults = {
        'absorption_guess': [-1000, stationary_line_core, 0.2, 0.1],
        'emission_guess': [1000, stationary_line_core, 0.2, 0.1],
        'absorption_min_bound':
        [-np.inf, stationary_line_core - 0.15, 1e-6, 1e-6],
        'emission_min_bound': [0, -np.inf, 1e-6, 1e-6],
        'absorption_max_bound': [0, stationary_line_core + 0.15, 1, 1],
        'emission_max_bound': [np.inf, np.inf, 1, 1],
        'absorption_x_scale': [1500, 0.2, 0.3, 0.5],
        'emission_x_scale': [1500, 0.2, 0.3, 0.5]
    }

    # Incorrect Lengths
    for key, value in defaults.items():  # For each parameter
        with pytest.raises(ValueError) as e:  # Catch ValueErrors
            defaults_mod = defaults.copy(
            )  # Create a copy of the default parameters
            defaults_mod.update({key:
                                 value[:-1]})  # Crop the parameter's value
            IBIS8542Model(
                **valid_kwargs, **defaults_mod
            )  # Pass the cropped parameter with the other default params
        assert key in str(e.value) and 'length' in str(
            e.value)  # Error must be about length of current parameter

    # Check that the sign of the following amplitudes are enforced
    for sign, bad_number, parameters in [
        ('positive', -10.42, ('emission_guess', 'emission_min_bound')),
        ('negative', +10.42, ('absorption_guess', 'absorption_max_bound'))
    ]:
        for p in parameters:
            with pytest.raises(ValueError) as e:
                bad_value = defaults[p].copy()
                bad_value[0] = bad_number
                defaults_mod = defaults.copy()
                defaults_mod.update({p: bad_value})
                IBIS8542Model(**valid_kwargs, **defaults_mod)
            assert p in str(e.value) and sign in str(e.value)
Exemplo n.º 4
0
def test_ibis8542model_configfile():
    # Enter the data directory (needed to find the files referenced in the config files)
    original_dir = os.getcwd()
    os.chdir(data_path())

    # Test with config file
    IBIS8542Model(config="ibis8542model_config.yml")

    # Turn off sigma
    IBIS8542Model(config="ibis8542model_config.yml", sigma=False)

    # Test with defined prefilter
    with pytest.deprecated_call():
        IBIS8542Model(config="ibis8542model_config_prefilter.yml")

    # Test with no prefilter
    IBIS8542Model(config="ibis8542model_config_noprefilter.yml")

    # TODO Check that the parameters were imported correctly

    # Go back to original directory
    os.chdir(original_dir)
Exemplo n.º 5
0
def ibis8542model_init():
    # Enter the data directory (needed to find the files referenced in the config files)
    original_dir = os.getcwd()
    os.chdir(data_path())

    x_orig = np.loadtxt("ibis8542model_wavelengths_original.csv")

    m = IBIS8542Model(config="ibis8542model_init_config.yml",
                      constant_wavelengths=x_orig,
                      original_wavelengths=x_orig,
                      prefilter_response=np.ones(25))

    m.neural_network = DummyClassifier(trained=True, n_features=25)

    # Go back to original directory
    os.chdir(original_dir)

    return m
Exemplo n.º 6
0
def test_ibis8542model_get_sigma():
    # Enter the data directory (needed to find the files referenced in the config files)
    original_dir = os.getcwd()
    os.chdir(data_path())

    m = IBIS8542Model(config="ibis8542model_config.yml")
    sigma = np.loadtxt("ibis8542model_sigma.csv", delimiter=',')

    assert np.array_equal(m._get_sigma(classification=0), sigma[0])

    for c in (1, 2, 3, 4):
        assert np.array_equal(m._get_sigma(classification=c), sigma[1])

    for i in (0, 1):
        assert np.array_equal(m._get_sigma(sigma=i), sigma[i])

    x = np.array([1.4325, 1421.43, -1325.342, 153.3, 1.2, 433.0])
    assert np.array_equal(m._get_sigma(sigma=x), x)

    # Go back to original directory
    os.chdir(original_dir)
Exemplo n.º 7
0
wavelengths = np.delete(wavelengths, np.s_[1:6:2])
wavelengths = np.delete(wavelengths, np.s_[-6::2])

spectrum = double_voigt(wavelengths, -526, 8542, 0.1, 0.1,
                        300, 8541.9, 0.2, 0.05, 1242)

#%%

from mcalf.visualisation import plot_spectrum

plot_spectrum(wavelengths, spectrum, normalised=False)

#%%
# A basic model is created,

model = IBIS8542Model(original_wavelengths=wavelengths)

#%%
# The spectrum is provided to the model's fit method.
# A classifier has not been loaded so the classification must be
# provided manually.
# The fitting algorithm assumes that the intensity at the ends of the
# spectrum is zero, so in this case we need to provide it with a
# background value to subtract from the spectrum before fitting.
fit = model.fit(spectrum=spectrum, classifications=4, background=1242)

print(fit)

#%%
# The spectrum can now be plotted,