pynir package

Submodules

pynir.Calibration module

Created on Wed Sep 28 11:00:35 2022

@author: chinn

pynir.Calibration.binaryClassificationReport(ytrue, ypred)[source]
class pynir.Calibration.lsvc(penalty='l2', loss='squared_hinge', *, dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None, max_iter=1000)[source]

Bases: LinearSVC

get_confusion_matrix(X, y)[source]
get_optParams(X, y, Params=None, nfold=10, n_jobs=None)[source]
class pynir.Calibration.multiClass_to_binaryMatrix[source]

Bases: object

fit(x)[source]
reTransform(xnew)[source]
transform(x)[source]
pynir.Calibration.multiClassificationReport(ytrue, ypred)[source]
pynir.Calibration.plot_confusion_matrix(cm, target_names, title='Confusion matrix', cmap=None, normalize=True)[source]

given a sklearn confusion matrix (cm), make a nice plot

Arguments

cm: confusion matrix from sklearn.metrics.confusion_matrix

target_names: given classification classes such as [0, 1, 2]

the class names, for example: [‘high’, ‘medium’, ‘low’]

title: the text to display at the top of the matrix

cmap: the gradient of the values displayed from matplotlib.pyplot.cm

see http://matplotlib.org/examples/color/colormaps_reference.html plt.get_cmap(‘jet’) or plt.cm.Blues

normalize: If False, plot the raw numbers

If True, plot the proportions

Usage

plot_confusion_matrix(cm = cm, # confusion matrix created by

# sklearn.metrics.confusion_matrix

normalize = True, # show proportions target_names = y_labels_vals, # list of names of the classes title = best_estimator_name) # title of graph

Citiation

http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html

class pynir.Calibration.pls(n_components=2)[source]

Bases: object

crossValidation_predict(nfold=10)[source]

Predict dependent variable using cross validation method.

Parameters: nfold (int): number of folds for cross validation (default is 10)

Returns: yhat (np.array): predicted dependent variable based on cross validation method

fit(X, y)[source]

Fit the PLS model to training data.

Parameters: X (np.array): independent variable(s) of the training data y (np.array): dependent variable of the training data

Returns: self: the PLS model

get_optLV(nfold=10)[source]
get_vip()[source]
plot_prediction(y, yhat, xlabel='Reference', ylabel='Prediction', title='', ax=None)[source]
predict(Xnew, n_components=None)[source]

Predict the dependent variable based on independent variable(s).

Parameters: Xnew (np.array): independent variable(s) of the new data to predict n_components (int): number of components used to predict

(default is None, which uses the optimal number of

components obtained by cross validation)

Returns: ynew_hat (np.array): predicted dependent variable based on Xnew

transform(Xnew)[source]
class pynir.Calibration.plsda(n_components=2, scale=True, **kwargs)[source]

Bases: PLSRegression

crossValidation_predict(nfold=10)[source]
fit(X, y)[source]

Fit model to data.

Parameters

Xarray-like of shape (n_samples, n_features)

Training vectors, where n_samples is the number of samples and n_features is the number of predictors.

Yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target vectors, where n_samples is the number of samples and n_targets is the number of response variables.

Returns

selfobject

Fitted model.

get_confusion_matrix(X, y)[source]
get_optLV(nfold=10)[source]
get_vip()[source]
permutation_test(X, y, n_repeats=100, n_jobs=None)[source]
predict(X)[source]

Predict targets of given samples.

Parameters

Xarray-like of shape (n_samples, n_features)

Samples.

copybool, default=True

Whether to copy X and Y, or perform in-place normalization.

Returns

y_predndarray of shape (n_samples,) or (n_samples, n_targets)

Returns predicted values.

Notes

This call requires the estimation of a matrix of shape (n_features, n_targets), which may be an issue in high dimensional space.

predict_log_proba(X)[source]
predict_proba(X)[source]
pynir.Calibration.regresssionReport(ytrue, ypred)[source]
class pynir.Calibration.rf(n_estimators=100, *, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='sqrt', max_leaf_nodes=None, min_impurity_decrease=0.0, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight=None, ccp_alpha=0.0, max_samples=None)[source]

Bases: RandomForestClassifier

get_confusion_matrix(X, y)[source]
get_optParams(X, y, Params=None, nfold=10, n_jobs=None)[source]
pynir.Calibration.sampleSplit_KS(X, test_size=0.25, metric='euclidean', *args, **kwargs)[source]

Kennard Stone Sample Split method Parameters ———- spectra: ndarray, shape of i x j

i spectrums and j variables (wavelength/wavenumber/ramam shift and so on)

test_sizefloat, int

if float, then round(i x (1-test_size)) spectrums are selected as test data, by default 0.25 if int, then test_size is directly used as test data size

metricstr, optional

The distance metric to use, by default ‘euclidean’ See scipy.spatial.distance.cdist for more infomation

Returns

select_pts: list

index of selected spetrums as train data, index is zero based

remaining_pts: list

index of remaining spectrums as test data, index is zero based

References

Kennard, R. W., & Stone, L. A. (1969). Computer aided design of experiments. Technometrics, 11(1), 137-148. (https://www.jstor.org/stable/1266770)

pynir.Calibration.sampleSplit_random(X, test_size=0.25, random_state=1, shuffle=False)[source]
pynir.Calibration.simpls(X, y, n_components)[source]

Partial Least Squares, SIMPLS Ref https://github.com/freesiemens/SpectralMultivariateCalibration/blob/master/pypls.py :param X: independent variables, numpy array of shape (n_samples, n_variables) :param y: dependent variable, numpy array of shape (n_samples,) or (n_samples, 1) :param n_components: number of latent variables to decompose the data into :return: dictionary containing the results of the SIMPLS algorithm

class pynir.Calibration.svc(*, C=1.0, kernel='rbf', degree=3, gamma='scale', coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape='ovr', break_ties=False, random_state=None)[source]

Bases: SVC

get_confusion_matrix(X, y)[source]
get_optParams(X, y, Params=None, nfold=10, n_jobs=None)[source]

pynir.CalibrationTransfer module

Created on Wed Sep 28 11:00:35 2022

@author: chinn

class pynir.CalibrationTransfer.BS[source]

Bases: object

Ref Osborne B. G., Fearn T. Collaborative evaluation of universal

calibrations for the measurement of protein and moisture in flour by near infrared reflectance [J]. International Journal of Food Science & Technology, 1983, 18(4): 453-460.

fit(y1, y2)[source]
transform(y2)[source]
class pynir.CalibrationTransfer.FS_PFCE(thres=0.98, constrType=1)[source]

Bases: object

fit(X1, X2, y, b1)[source]
transform(X)[source]
class pynir.CalibrationTransfer.MT_PFCE(thres=0.98, constrType=1)[source]

Bases: object

fit(X, y, b1)[source]
transform(X, itask)[source]
class pynir.CalibrationTransfer.NS_PFCE(thres=0.98, constrType=1)[source]

Bases: object

fit(X1, X2, b1)[source]
transform(X)[source]
class pynir.CalibrationTransfer.PDS(halfWindowSize=7, regType='mlr', **kwargs)[source]

Bases: object

fit(X1, X2)[source]

Parameters

X12D array

Standard spectra of master.

X22D array

Standard spectra of master.

get_windowRange(nFeatrues, i)[source]
transform(X)[source]
class pynir.CalibrationTransfer.SST(n_components=2)[source]

Bases: object

fit(X1, X2)[source]

Parameters

X12D array

Standard spectra of the first instrument.

X22D array

Standard spectra of the second instrument.

transform(X)[source]
class pynir.CalibrationTransfer.SS_PFCE(thres=0.98, constrType=1)[source]

Bases: object

fit(X2, y, b1)[source]
transform(X)[source]
pynir.CalibrationTransfer.cost_FS_PFCE(b1, b2, X1, X2, y)[source]
pynir.CalibrationTransfer.cost_MT_PFCE(B, X, y)[source]
pynir.CalibrationTransfer.cost_NS_PFCE(b1, b2, X1, X2)[source]
pynir.CalibrationTransfer.cost_SS_PFCE(b2, X2, y)[source]
pynir.CalibrationTransfer.pfce_constr1(B, thres)[source]
pynir.CalibrationTransfer.pfce_constr2(B, thres)[source]
pynir.CalibrationTransfer.pfce_constr3(B, thres)[source]

pynir.FeatureSelection module

class pynir.FeatureSelection.MCUVE(x, y, ncomp=2, nrep=500, testSize=0.2)[source]

Bases: object

Transform(X, nSelFeatures=None)[source]
evalFeatures(cv=5)[source]
fit()[source]
class pynir.FeatureSelection.MSVC(x, y, ncomp=1, nrep=7000, ncut=50, testSize=0.2)[source]

Bases: object

calcCriteria()[source]
cutFeature(*args)[source]
evalCriteria(cv=3)[source]
class pynir.FeatureSelection.RT(x, y, ncomp=2, nrep=500, testSize=0.2)[source]

Bases: MCUVE

evalFeatures(cv=5)[source]
fit()[source]
class pynir.FeatureSelection.VC(x, y, ncomp=2, nrep=500, testSize=0.2)[source]

Bases: RT

fit(cv=5, isSmooth=True)[source]
pynir.FeatureSelection.plotFeatureSelection(wv, X, selFeatures, methodNames=None, ylabel='Intensity (A.U.)', xlabel='Wavelength (nm)', title='Feature selection results', ax=None)[source]

pynir.OutlierDection module

Created on Wed Sep 28 11:00:35 2022

@author: chinn

class pynir.OutlierDection.outlierDection_PLS(ncomp=2, conf=0.99)[source]

Bases: object

detect(X, y)[source]
fit(X, y)[source]
plot_HotellingT2_Q(Q, Tsq, Q_conf, Tsq_conf, ax=None)[source]

pynir.Preprocessing module

Created on Wed Sep 28 11:01:16 2022

@author: chinn

class pynir.Preprocessing.SG_filtering(window_length=13, polyorder=2, **kwargs)[source]

Bases: object

transform(X)[source]
class pynir.Preprocessing.centralization[source]

Bases: object

fit(X)[source]
fit_transform(X)[source]
transform(X)[source]
class pynir.Preprocessing.cwt(wavelet='morl', scale=20)[source]

Bases: object

getContinuousWavelet()[source]
getDiscreteWavelet()[source]
transform(X)[source]
class pynir.Preprocessing.derivate(deriv=1, window_length=13, polyorder=2, **kwargs)[source]

Bases: object

transform(X)[source]
class pynir.Preprocessing.msc[source]

Bases: object

fit(X)[source]
fit_transform(X)[source]
transform(X)[source]
class pynir.Preprocessing.smooth(window_length=13, polyorder=2, **kwargs)[source]

Bases: object

transform(X)[source]
class pynir.Preprocessing.snv[source]

Bases: object

fit(X)[source]
fit_transform(X)[source]
transform(X)[source]

pynir.utils module

Created on Wed Sep 28 11:02:36 2022

@author: Jin Zhang (zhangjin@mail.nankai.edu.cn)

pynir.utils.simulateNIR(nSample=100, n_components=3, refType=1, noise=0.0, seeds=1)[source]

simulating NIR spectra

Parameters

nSampleint, optional

number of samples. The default is 100.

n_componentsint, optional

number of componnet for spectral simulation. The default is 3.

refTypeint, optional

type of reference value None for no reference value output 1 for contious values as reference value output 2 or the larger integer for binary or class output.

seedsint, optimal

random seed for generating spectra and reference values. The default is 1.

Returns

X: matrix, simulated NIR spectra matrix. y: array, concentration or class of all samples.

pynir.utils.simulateNIR_calibrationTransfer(nSample=100, n_components=3, shifts=0.01, refType=1, noise=0.0, seeds=1)[source]

simulating NIR spectra for calibration transfer

Parameters

nSampleint, optional

number of samples. The default is 100.

n_componentsint, optional

number of componnet for spectral simulation. The default is 3.

shifts: float, optimal

shift level of base peaks for simulte secondary NIR spectra data

refTypeint, optional

type of reference value None for no reference value output 1 for contious values as reference value output 2 or the larger integer for binary or class output.

seedsint, optimal

random seed for generating spectra and reference values. The default is 1.

Returns

X: matrix, simulated NIR spectra matrix. y: array, concentration or class of all samples.

Module contents