pynir package — pynir documentation

# pynir package

## Submodules

## pynir.Calibration module

Created on Wed Sep 28 11:00:35 2022

@author: chinn

pynir.Calibration.binaryClassificationReport(ytrue, ypred)

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)

  Bases: `LinearSVC`

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

class pynir.Calibration.multiClass_to_binaryMatrix

  Bases: `object`

  fit(x)
  reTransform(xnew)
  transform(x)

pynir.Calibration.multiClassificationReport(ytrue, ypred)

pynir.Calibration.plot_confusion_matrix(cm, target_names, title='Confusion matrix', cmap=None, normalize=True)

  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)

  Bases: `object`

  crossValidation_predict(nfold=10)

    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)

    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)
  get_vip()
  plot_prediction(y, yhat, xlabel='Reference', ylabel='Prediction', title='', ax=None)
  predict(Xnew, n_components=None)

    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)

class pynir.Calibration.plsda(n_components=2, scale=True, **kwargs)

  Bases: `PLSRegression`

  crossValidation_predict(nfold=10)
  fit(X, y)

    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)
  get_optLV(nfold=10)
  get_vip()
  permutation_test(X, y, n_repeats=100, n_jobs=None)
  predict(X)

    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)
  predict_proba(X)

pynir.Calibration.regresssionReport(ytrue, ypred)

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)

  Bases: `RandomForestClassifier`

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

pynir.Calibration.sampleSplit_KS(X, test_size=0.25, metric='euclidean', *args, **kwargs)

  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)

pynir.Calibration.simpls(X, y, n_components)

  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)

  Bases: `SVC`

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

## pynir.CalibrationTransfer module

Created on Wed Sep 28 11:00:35 2022

@author: chinn

class pynir.CalibrationTransfer.BS

  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)
  transform(y2)

class pynir.CalibrationTransfer.FS_PFCE(thres=0.98, constrType=1)

  Bases: `object`

  fit(X1, X2, y, b1)
  transform(X)

class pynir.CalibrationTransfer.MT_PFCE(thres=0.98, constrType=1)

  Bases: `object`

  fit(X, y, b1)
  transform(X, itask)

class pynir.CalibrationTransfer.NS_PFCE(thres=0.98, constrType=1)

  Bases: `object`

  fit(X1, X2, b1)
  transform(X)

class pynir.CalibrationTransfer.PDS(halfWindowSize=7, regType='mlr', **kwargs)

  Bases: `object`

  fit(X1, X2)

    ### Parameters

    X12D array

      Standard spectra of master.

    X22D array

      Standard spectra of master.

  get_windowRange(nFeatrues, i)
  transform(X)

class pynir.CalibrationTransfer.SST(n_components=2)

  Bases: `object`

  fit(X1, X2)

    ### Parameters

    X12D array

      Standard spectra of the first instrument.

    X22D array

      Standard spectra of the second instrument.

  transform(X)

class pynir.CalibrationTransfer.SS_PFCE(thres=0.98, constrType=1)

  Bases: `object`

  fit(X2, y, b1)
  transform(X)

pynir.CalibrationTransfer.cost_FS_PFCE(b1, b2, X1, X2, y)

pynir.CalibrationTransfer.cost_MT_PFCE(B, X, y)

pynir.CalibrationTransfer.cost_NS_PFCE(b1, b2, X1, X2)

pynir.CalibrationTransfer.cost_SS_PFCE(b2, X2, y)

pynir.CalibrationTransfer.pfce_constr1(B, thres)

pynir.CalibrationTransfer.pfce_constr2(B, thres)

pynir.CalibrationTransfer.pfce_constr3(B, thres)

## pynir.FeatureSelection module

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

  Bases: `object`

  Transform(X, nSelFeatures=None)
  evalFeatures(cv=5)
  fit()

class pynir.FeatureSelection.MSVC(x, y, ncomp=1, nrep=7000, ncut=50, testSize=0.2)

  Bases: `object`

  calcCriteria()
  cutFeature(*args)
  evalCriteria(cv=3)

class pynir.FeatureSelection.RT(x, y, ncomp=2, nrep=500, testSize=0.2)

  Bases: `MCUVE`

  evalFeatures(cv=5)
  fit()

class pynir.FeatureSelection.VC(x, y, ncomp=2, nrep=500, testSize=0.2)

  Bases: `RT`

  fit(cv=5, isSmooth=True)

pynir.FeatureSelection.plotFeatureSelection(wv, X, selFeatures, methodNames=None, ylabel='Intensity (A.U.)', xlabel='Wavelength (nm)', title='Feature selection results', ax=None)

## pynir.OutlierDection module

Created on Wed Sep 28 11:00:35 2022

@author: chinn

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

  Bases: `object`

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

## 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)

  Bases: `object`

  transform(X)

class pynir.Preprocessing.centralization

  Bases: `object`

  fit(X)
  fit_transform(X)
  transform(X)

class pynir.Preprocessing.cwt(wavelet='morl', scale=20)

  Bases: `object`

  getContinuousWavelet()
  getDiscreteWavelet()
  transform(X)

class pynir.Preprocessing.derivate(deriv=1, window_length=13, polyorder=2, **kwargs)

  Bases: `object`

  transform(X)

class pynir.Preprocessing.msc

  Bases: `object`

  fit(X)
  fit_transform(X)
  transform(X)

class pynir.Preprocessing.smooth(window_length=13, polyorder=2, **kwargs)

  Bases: `object`

  transform(X)

class pynir.Preprocessing.snv

  Bases: `object`

  fit(X)
  fit_transform(X)
  transform(X)

## 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)

  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)

  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