low, high = 1, 5  # Possible classifications [1, 2, 3, 4]

# 2D array of spectra (n, w), 1D array of labels (n,)
spectra, labels = s(n, w, low, high)

#%%
# Next, we shall import :func:`mcalf.visualisation.plot_classifications`.

from mcalf.visualisation import plot_classifications

#%%
# We can now plot a simple grid of the spectra grouped by their
# classification. By default, a maximum of 20 spectra are plotted
# for each classification. The first 20 spectra are selected.

plot_classifications(spectra, labels)

#%%
# A specific number of rows or columns can be requested,

plot_classifications(spectra, labels, ncols=1)

#%%
# The plot settings can be adjusted,

plot_classifications(spectra,
                     labels,
                     show_labels=False,
                     nlines=5,
                     style='viridis',
                     plot_settings={
예제 #2
0
            _X = spectra
            _y = [c] * len(spectra)
    return _X, _y


X, y = select_training_set(training_data, model)

print(X.shape)  # spectra
print(y.shape)  # labels/classifications

#%%
# These classifications look as follows,

from mcalf.visualisation import plot_classifications

plot_classifications(X, y)

#%%
# Now we can train the neural network on
# 100 labelled spectra (even indices),

model.train(X[::2], y[::2])

#%%
# And now we can use the other 100
# labelled spectra (odd indices)
# to test the performance of the neural network,

model.test(X[1::2], y[1::2])

#%%
예제 #3
0
def test_plot_classifications_3plots(pytestconfig):
    s, r = spectra(40, 30, 2, 5)  # 3 plots
    plot_classifications(s, r, show_labels=False)
#%%
# If a GridSpec returned by the plot_classification function has
# free space, a new axes can be added to the returned GridSpec.
# We can then request plot_class_map to plot onto this
# new axes.
# The colorbar axes can be set to ``fig.axes`` such that
# the colorbar takes the full height of the figure, as
# in this case, its colours are the same as the line plots.

import matplotlib.pyplot as plt
from mcalf.visualisation import plot_classifications, plot_class_map

fig = plt.figure(constrained_layout=True)

gs = plot_classifications(spectra, labels, nrows=2, show_labels=False)

ax = fig.add_subplot(gs[-1])
plot_class_map(class_map,
               ax=ax,
               colorbar_settings={
                   'ax': fig.axes,
                   'label': 'classification',
               })

#%%
# The function :func:`mcalf.visualisation.init_class_data`` is
# intended to be an internal function for generating data that
# is common to multiple plotting functions. However, it may be
# used externally if necessary.
예제 #5
0
def test_plot_classifications():

    with pytest.raises(TypeError) as e:
        plot_classifications([[0, 1], [1, 2]], np.array([3, 4]))
    assert '`spectra` must be a numpy.ndarray' in str(e.value)

    with pytest.raises(TypeError) as e:
        plot_classifications(np.array([[0, 1], [1, 2]]), [3, 4])
    assert '`labels` must be a numpy.ndarray' in str(e.value)

    with pytest.raises(TypeError) as e:
        plot_classifications(np.array([0, 1]), np.array([3, 4]))
    assert '`spectra` must be a 2D array' in str(e.value)

    for r in (np.array([[3, 4]]), np.array([3.5, 4])):
        with pytest.raises(TypeError) as e:
            plot_classifications(np.array([[0, 1], [1, 2]]), r)
        assert '`labels` must be a 1D array of integers' in str(e.value)

    with pytest.raises(ValueError) as e:
        plot_classifications(np.array([[0, 1], [1, 2]]), np.array([3, 4, 6]))
    assert '`spectra` and `labels` must be the same length' in str(e.value)

    s, r = spectra(10, 5, 0, 6)

    with pytest.raises(ValueError) as e:
        plot_classifications(s, r, nrows=3, ncols=2)
    assert 'Both `nrows` and `ncols` cannot be given together' in str(e.value)

    with pytest.raises(TypeError) as e:
        plot_classifications(s, r, nrows=2.5)
    assert '`nrows` must be an integer' in str(e.value)

    with pytest.raises(TypeError) as e:
        plot_classifications(s, r, ncols=3.5)
    assert '`ncols` must be an integer' in str(e.value)

    for nlines in (3.5, -3, 0):
        with pytest.raises(TypeError) as e:
            plot_classifications(s, r, nlines=nlines)
        assert '`nlines` must be a positive integer' in str(e.value)

    s, r = spectra(100, 5, 2, 7)  # 5 plots

    with pytest.raises(ValueError) as e:
        plot_classifications(s, r, nrows=6)  # 6x1, not 5x1
    assert '`nrows` is larger than it needs to be' in str(e.value)

    with pytest.raises(ValueError) as e:
        plot_classifications(s, r, ncols=4)  # 2x4, not 2x3
    assert '`ncols` is larger than it needs to be' in str(e.value)
예제 #6
0
def test_plot_classifications_4plots(pytestconfig):
    s, r = spectra(40, 30, 1, 5)  # 4 plots
    plot_classifications(s, r)
예제 #7
0
def test_plot_classifications_not01(pytestconfig):
    s, r = spectra(40, 30, 3, 5)  # 2 plots
    s[r == 4] += 0.5  # classification 4 not in [0, 1]
    plot_classifications(s, r)
예제 #8
0
def test_plot_classifications_3x2(pytestconfig):
    s, r = spectra(40, 30, 2, 8)  # 6 plots
    plot_classifications(s, r, nrows=3)
예제 #9
0
def test_plot_classifications_3x2_5(pytestconfig):
    s, r = spectra(40, 30, 0, 5)  # 5 plots
    plot_classifications(s, r, nrows=3)
예제 #10
0
def test_plot_classifications_1plot(pytestconfig):
    s, r = spectra(40, 30, 3, 4)  # 1 plot
    plot_classifications(s, r)
예제 #11
0
# Classify spectra
# ~~~~~~~~~~~~~~~~
#
# We can also classify the loaded spectra and create plots,

classifications = model.classify_spectra(row=[0, 1], column=range(3))

print('classifications are:', classifications)

#%%
# This function plots the spectra grouped
# by classification,

from mcalf.visualisation import plot_classifications

plot_classifications(spectra_1d, classifications.flatten())

#%%
# This function plots a spatial map of
# the classifications on the 2x3 grid,

from mcalf.visualisation import plot_class_map

plot_class_map(classifications)

#%%
# Merge output data
# ~~~~~~~~~~~~~~~~~
#
# The :class:`~mcalf.models.FitResult` objects in the
# ``fits`` 1D list can be merged into a grid.