def test_plot_bar_and_class_map(pytestconfig): m = class_map(30, 10, 20, 8) fig, ax = plt.subplots(1, 2) data = init_class_data(m, ax=ax[0], colorbar_settings={ 'ax': ax, 'location': 'bottom' }) plot_class_map(data=data, ax=ax[0]) bar(data=data, ax=ax[1])
def test_no_args(): # Custom errors as not required if data keyword argument given. with pytest.raises(TypeError) as e: bar() assert "bar() missing 1 required positional argument: 'class_map'" == str( e.value) with pytest.raises(TypeError) as e: plot_class_map() assert "plot_class_map() missing 1 required positional argument: 'class_map'" == str( e.value)
#%% # Now that we have a trained neural network, # we can use it to classify spectra. # Usually spectra will be classified automatically # during the fitting process, however, you # can request the classification by themselves, classifications = model.classify_spectra(row=range(60), column=range(50)) #%% # These classifications look as follows, from mcalf.visualisation import plot_class_map plot_class_map(classifications) #%% # Creating a reproducible classifier # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # The neural network classifier introduces a certain amount # of randomness when it it fitting based on the training # data. This randomness arises in the initial values # of the weights and biases that are fitted during the # training process, as well as the order in which the # training data are used. # # This means that two neural networks trained on identical # data will not produce the same results. To aid the # reproducibility of results that rely on a neural
# 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. from mcalf.visualisation import init_class_data, bar fig, ax = plt.subplots(1, 2, constrained_layout=True) data = init_class_data(class_map, resolution=(0.25, 0.25), ax=ax[1])
n = 5 # Possible classifications [0, 1, 2, 3, 4] class_map = c(t, x, y, n) # 3D array of classifications (t, y, x) #%% # Next, we shall import :func:`mcalf.visualisation.plot_class_map`. from mcalf.visualisation import plot_class_map #%% # We can now simply plot the 3D array. # By default, the first dimension of a 3D array will be averaged to # produce a time average, selecting the most common classification # at each (x, y) coordinate. plot_class_map(class_map) #%% # A spatial resolution with units can be specified for each axis. import astropy.units as u plot_class_map(class_map, resolution=(0.75 * u.km, 1.75 * u.Mm), offset=(-25, -10), dimension=('x distance', 'y distance')) #%% # A narrower range of classifications to be plotted can be # requested with the ``vmin`` and ``vmax`` parameters. # Classifications outside of the range will appear as grey, # the same as pixels with a negative, unassigned classification.
def test_plot_class_map_allbad(pytestconfig): m = class_map(30, 10, 20, 8) m[:] = -1 plot_class_map(m)
def test_plot_class_map_units(pytestconfig): m = class_map(30, 10, 20, 8) plot_class_map(m, resolution=(2.5 * u.m, 3.5 * u.km), offset=(5, 10), dimension=('dim x', 'dim y'))
def test_plot_class_map_range(pytestconfig): m = class_map(30, 10, 20, 8) plot_class_map(m, vmin=2, vmax=4)
def test_plot_class_map_nocolorbar(pytestconfig): m = class_map(30, 10, 20, 8) plot_class_map(m, show_colorbar=False)
def test_plot_class_map(pytestconfig): m = class_map(30, 10, 20, 8) plot_class_map(m)
#%% # 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. # Each of these objects can be appended to a # single :class:`~mcalf.models.FitResults` object. # This allows for increased data portability, from mcalf.models import FitResults # Initialise with the grid shape and num. params. results = FitResults((2, 3), 8)