Skip to content

Commit

Permalink
Grad-Cam CNN neural network method added
Browse files Browse the repository at this point in the history
  • Loading branch information
ALebrun-108 committed Sep 26, 2022
1 parent ac76f70 commit c40695f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 145 deletions.
2 changes: 1 addition & 1 deletion boxsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from boxsers.machine_learning import SpectroGmixture, SpectroKmeans, SpectroRF, SpectroSVM, \
SpectroLDA, SpectroPCA, SpectroCNN, cf_matrix, clf_report
SpectroLDA, SpectroPCA, SpectroCNN, validation_metrics
from boxsers.data_augmentation import aug_mixup, aug_xshift, aug_noise, aug_multiplier, aug_linslope, aug_offset
from boxsers.preprocessing import als_baseline_cor, savgol_smoothing, spectral_cut, spline_interpolation, \
spectral_normalization, cosmic_filter
Expand Down
116 changes: 7 additions & 109 deletions boxsers/machine_learning/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import time
import seaborn as sns
import matplotlib.pyplot as plt
from boxsers._boxsers_utils import _lightdark_switch
from sklearn.inspection import permutation_importance
import warnings


class _MachineLearningClassifier:
Expand Down Expand Up @@ -134,6 +133,9 @@ def get_classif_report(self, x_test, y_test, digits=4, class_names=None, save_pa
Returns:
Scikit Learn classification report
"""
warnings.warn('\'get_classif_report\' instance method is no longer supported, use instead'
' the standalone \'clf_report\' method found in boxsers.validation_metrics module.',
DeprecationWarning)
# Converts binary labels to integer labels. Does nothing if they are already integer labels.
if y_test.ndim == 2 and y_test.shape[1] > 1:
y_test = np.argmax(y_test, axis=1)
Expand Down Expand Up @@ -199,6 +201,9 @@ def get_conf_matrix(self, x_test, y_test, normalize='true', class_names=None, ti
Return:
Scikit Learn confusion matrix
"""
warnings.warn('\'get_conf_matrix\' instance method is no longer supported, use instead'
' the standalone \'cf_matrix\' method found in boxsers.validation_metrics module.',
DeprecationWarning)
# The model returns the predicted classes
y_pred = self.model.predict(x_test)

Expand Down Expand Up @@ -262,113 +267,6 @@ def __init__(self, n_trees=250, rdm_ste=None):
# inherits the methods and arguments of the parent class _MachineLearningClassifier
super().__init__(rf_model)

def plot_feat_importance(self, wn, sp, n_repeats=10, rdm_ste=None, title=None,
xlabel='Raman Shift (cm$^{-1}$)', ylabel='Features importance',
eline_width=1.5, marker_style='o', color=None, darktheme=False,
grid=True, fontsize=10, fig_width=6.08, fig_height=3.8, save_path=None):
""" Plot feature importance based on permutation importance
Inspired by : L. Breiman, “Random Forests”, Machine Learning, 45(1), 5-32, 2001.
Parameters:
wn : array or list
X-axis(wavenumber, wavelenght, Raman shift, etc.), array shape = (n_pixels, ).
sp : array
Input Spectrum(s), array shape = (n_spectra, n_pixels) for multiple spectra and (n_pixels,)
for a single spectrum.
n_repeats : int, default=10
Number of times to permute a feature.
rdm_ste : integer, default=None
Random seed of the split. Using the same seed results in the same subsets
each time.
title : string, default=None
Font size(pts) used for the different elements of the graph. The title's font
is two points larger than "fonctsize".
xlabel : string, default='Raman Shift (cm$^{-1}$)'
X-axis title. If None, there is no title displayed.
ylabel : string, default='Intensity (a.u.)'
Y-axis title. If None, there is no title displayed.
eline_width : positive float, default= 1.5
Plot errorbar line width(s).
marker_style : string, default='o'
Marker style.
color : string, default=None
Plot line color(s).
darktheme : boolean, default=False
If True, returns a plot with a dark background.
grid : boolean, default=False
If True, a grid is displayed.
fontsize : positive float, default=10
Font size(pts) used for the different elements of the graph.
fig_width : positive float or int, default=6.08
Figure width in inches.
fig_height : positive float or int, default=3.8
Figure height in inches.
save_path : string, default=None
Path where the figure is saved. If None, saving does not occur.
Recommended format : .png, .pdf
"""
# update theme related parameters
frame_color, bg_color, alpha_value = _lightdark_switch(darktheme)

perm_imp = permutation_importance(self.model, wn, sp, n_repeats=n_repeats, random_state=rdm_ste)
imp_mean = perm_imp.importances_mean
imp_std = perm_imp.importances_std

# creates a figure object
fig = plt.figure(figsize=(fig_width, fig_height))
# add an axes object
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index

ax.errorbar(range(50), imp_mean, yerr=imp_std, fmt='none', ecolor=frame_color, elinewidth=eline_width,
capsize=5, alpha=0.85)
ax.plot(range(50), imp_mean, marker_style, color=color)

# titles settings
ax.set_title(title, fontsize=fontsize + 1.2,
color=frame_color) # sets the plot title, 1.2 points larger font size
ax.set_xlabel(xlabel, fontsize=fontsize, color=frame_color) # sets the X-axis label
ax.set_ylabel(ylabel, fontsize=fontsize, color=frame_color) # sets the Y-axis label

# tick settings
ax.minorticks_on()
ax.tick_params(axis='both', which='major',
labelsize=fontsize - 2, # 2.0 points smaller font size
color=frame_color)
ax.tick_params(axis='both', which='minor', color=frame_color)
ax.tick_params(axis='x', colors=frame_color) # setting up X-axis values color
ax.tick_params(axis='y', colors=frame_color) # setting up Y-axis values color
for spine in ['top', 'bottom', 'left', 'right']:
ax.spines[spine].set_color(frame_color) # setting up spines color
if grid is True:
# adds a grid
ax.grid(alpha=alpha_value)

# set figure and axes facecolor
fig.set_facecolor(bg_color)
ax.set_facecolor(bg_color)
# adjusts subplot params so that the subplot(s) fits in to the figure area
fig.tight_layout()
# save figure
if save_path is not None:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.show()


class SpectroSVM(_MachineLearningClassifier):
""" Support Vector Machine classification model.
Expand Down
4 changes: 2 additions & 2 deletions boxsers/machine_learning/dimension_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def pair_plot(self, sp, lab, n_components=1, class_names=None, title=None,

# title settings
ax.set_title(title, fontsize=fontsize + 1.2, color=frame_color) # 1.2 points larger font size
ax.set_xlabel(fontsize=fontsize, color=frame_color) # sets the x-axis title
ax.set_ylabel(fontsize=fontsize, color=frame_color) # sets the y-axis title
# ax.set_xlabel(fontsize=fontsize, color=frame_color) # sets the x-axis title
# ax.set_ylabel(fontsize=fontsize, color=frame_color) # sets the y-axis title

# tick settings
ax.minorticks_on()
Expand Down
Loading

0 comments on commit c40695f

Please sign in to comment.