dnnbrain.dnn.base module¶
- class dnnbrain.dnn.base.ImageProcessor¶
Bases:
objectMetrics for pre-processing pictures to further DNN operations.
- crop(image, box)¶
Crop image with a rectangular region.
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
box (tuple) – The crop rectangle as a (left, upper, right, lower)-tuple.
- Returns
image – Image data after crop.
- Return type
ndarray, Tensor, PIL.Image
- norm(image, ord)¶
Calculate norms of the image by the following formula:
sum(abs(image)**ord)**(1./ord)
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
ord (int) – The order of the norm.
- Returns
norm – The norm of the image.
- Return type
float
- resize(image, size, interpolation='nearest')¶
Resize image.
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
size (tuple) – The target size as a 2-tuple: (height, width).
interpolation (str) – Interpolation method for resize, check self.str2pil_interp and self.str2cv2_interp to find the available interpolation.
- Returns
image – Image data after resizing.
- Return type
ndarray, Tensor, PIL.Image
- to_array(image)¶
Convert image to array.
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
- Returns
arr – Image array.
- Return type
ndarray
- to_pil(image, normalization=False)¶
Convert image to PIL.Image
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
normalization (bool) – Normalization operation. If is True, normalize image data to integers in [0, 255].
- Returns
image – Output image with type of PIL.Image.
- Return type
PIL.Image
- to_tensor(image)¶
Convert image to tensor
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
- Returns
tensor – Image tensor.
- Return type
Tensor
- total_variation(image)¶
Calculate total variation of the image.
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
- Returns
tv – Total variation.
- Return type
float
- translate(image, bkg, startpoint, endpoint, stride)¶
Translate image on a background based on given startpoint and stride, only support one axis now.
- Parameters
image (ndarray, Tensor, PIL.Image) – Image data.
bkg (ndarray, Tensor, PIL.Image) – Same type with image.Remember bkg must bigger than image.
startpoint (tuple) – The start point of translating in upper left position as a (x_axis,y_axis)-tuple, horizontal:x_axis, vertical:y_axis.
endpoint (tuple) – The end point of translating in upper left position as a (x_axis,y_axis)-tuple (horizontal: x_axis; vertical: y_axis).
stride (int) – Stride of each translation.
- Returns
image_tran – Image data after translating, which add a dim in the first axis meaning n_stim.
- Return type
ndarray, Tensor, PIL.Image
- class dnnbrain.dnn.base.ImageSet(img_dir, img_ids, labels=None, transform=None)¶
Bases:
objectBuild a dataset to load image.
- class dnnbrain.dnn.base.MultivariateMapping(estimator=None, cv=5, scoring=None)¶
Bases:
object- map(X, Y)¶
Use all columns of X to predict each column of Y.
- Parameters
X (ndarray) – shape=(n_sample, n_feature)
Y (ndarray) – shape=(n_sample, n_target)
- Returns
map_dict – It depends on estimator type.
estimator type
key
value
classifier
score
An array with shape as (n_target, cv).
Each row contains accuracies of each cross
validation folds, when using all features to
predict the corresponding target.model
An array with shape as (n_target,).
Each element is a model fitted by all features
and the corresponding target.conf_m
An array with shape as (n_target, cv).
Each row contains confusion matrices
(n_label, n_label) of each cross validation
folds, when using all features to predict the
corresponding target.regressor
score
An array with shape as (n_target, cv).
Each row contains scores of each cross
validation folds, when using all features to
predict the corresponding target.model
An array with shape as (n_target,).
Each element is a model fitted by all features
and the corresponding target.- Return type
dict
- set_cv(cv)¶
- Parameters
cv (int) – The number of cross validation folds.
- set_estimator(estimator)¶
- Parameters
estimator (str | sklearn estimator or pipeline) –
If is str, it is a name of a estimator used to do mapping.
There are some optional names at below:name
type
description
lrc
classifier
Logistic Regression Classifier
svc
classifier
C-Support Vector Classification
glm
regressor
Ordinary least squares Linear Regression
lasso
regressor
Linear Model trained with L1 prior as regularizer
- set_scoring(scoring)¶
- Parameters
scoring (str or callable) –
The method to evaluate the predictions on the test set. It depends on estimator type.
estimator type
Scoring description
classifier
The evaluation method is fixed as accuracy and confusion matrix.
regressor
Scoring parameters or strategies supported by sklearn in addition to ‘correlation’.
- class dnnbrain.dnn.base.UnivariateMapping(estimator=None, cv=5, scoring=None)¶
Bases:
objectFor each target samples, evaluate every feature samples’ predictive ability and record location, prediction score and model of the feature with maximal predictive ability.
- map(X, Y)¶
Use all columns of X (one-by-one) to predict each column of Y;
For each column of Y: Find the location of the column of X which has the maximal prediction score; Record the location, and corresponding score and model.
- Parameters
X (ndarray) – shape=(n_sample, n_feature)
Y (ndarray) – shape=(n_sample, n_target)
- Returns
map_dict – It depends on estimator type.
estimator type
key
value
classifier
score
An array with shape as (n_target, cv).
Each row contains accuracies of each cross
validation folds, when using the feature at the
maximal location to predict the corresponding target.location
An array with shape as (n_target,).
Each element is a location of the feature
which makes the maximal score.model
An array with shape as (n_target,).
Each element is a model fitted by the feature
at the maximal location and the corresponding target.conf_m
An array with shape as (n_target, cv).
Each row contains confusion matrices
(n_label, n_label) of each cross validation folds,
when using the feature at the maximal location to
predict the corresponding target.regressor
score
An array with shape as (n_target, cv).
Each row contains scores of each cross
validation folds, when using the feature at
the maximal location to predict the corresponding target.location
An array with shape as (n_target,).
Each element is a location of the feature
which makes the maximal score.model
An array with shape as (n_target,).
Each element is a model fitted by the feature
at the maximal location and the corresponding target.correlation
score
An array with shape as (n_target,).
Each element is the maximal pearson r among all
features correlating to the corresponding target.location
An array with shape as (n_target,).
Each element is a location of the feature
which makes the maximal score.- Return type
dict
- set_cv(cv)¶
- Parameters
cv (int) – The number of cross validation folds.
- set_estimator(estimator)¶
- Parameters
estimator (str | sklearn estimator or pipeline) –
If is str, it is a name of a estimator used to do mapping.
There are some optional names at below:name
type
description
lrc
classifier
Logistic Regression Classifier
svc
classifier
C-Support Vector Classification
glm
regressor
Ordinary least squares Linear Regression
lasso
regressor
Linear Model trained with L1 prior as regularizer
Note: If name is ‘corr’, it just uses correlation rather than prediction.
- set_scoring(scoring)¶
- Parameters
scoring (str or callable) –
The method to evaluate the predictions on the test set. It depends on estimator type.
estimator type
Scoring description
classifier
The evaluation method is fixed as accuracy and confusion matrix.
regressor
Scoring parameters or strategies supported by sklearn in addition to ‘correlation’.
correlation
No evaluation method is needed.
- class dnnbrain.dnn.base.VideoClipSet(clip_files, transform=None)¶
Bases:
object
- class dnnbrain.dnn.base.VideoSet(vid_file, frame_nums, labels=None, transform=None)¶
Bases:
objectDataset for video data
- dnnbrain.dnn.base.array_statistic(arr, method, axis=None, keepdims=False)¶
extract statistic of an array
- Parameters
arr (ndarray) – A numpy array.
method (str) – Feature extraction method
axis (int, tuple) – None or int or tuple of ints. Axis or axes along which to operate. If it’s None, operate on the whole array.
keepdims (bool) – Keep the axis which is reduced.
- Returns
arr – Extracted statistic.
- Return type
ndarray
- dnnbrain.dnn.base.correlation_score(y_true, y_pred, multioutput='uniform_average')¶
- Parameters
y_true (ndarray with shape as (n_samples,) or (n_samples, n_outputs)) – Ground truth target values.
y_pred (ndarray with shape as (n_samples,) or (n_samples, n_outputs)) – Estimated target values.
multioutput (string in ['raw_values', 'uniform_average']) –
- ‘raw_values’ :
Returns a full set of scores in case of multioutput input.
- ’uniform_average’ :
Scores of all outputs are averaged with uniform weight.
- Returns
score – a single value if ‘multioutput’ is ‘uniform_average’ a ndarray if ‘multioutput’ is ‘raw_values’
- Return type
float or ndarray of floats
- dnnbrain.dnn.base.correlation_scorer(regressor, X, y)¶
- dnnbrain.dnn.base.cross_val_confusion(classifier, X, y, cv=None)¶
Evaluate confusion matrix and score from each fold of cross validation
- Parameters
classifier (classifier object) – The object used to fit the data.
X (ndarray) – Shape=(n_sample, n_feature).
y (ndarray) – Shape=(n_sample,).
cv (int) – The number of folds of the cross validation.
- Returns
conf_ms (list) – Confusion matrices of the folds.
accuracies (list) – Accuracies of the folds.
- dnnbrain.dnn.base.cross_val_scores(regressor, X, Y, scoring, cv=None, multi_trg_flag=True)¶
Evaluate scores for each run of cross validation
- Parameters
regressor (regressor object) – The estimator used to fit the data.
X (ndarray) – Shape=(n_sample, n_feature).
Y (ndarray) – Shape=(n_sample, n_target).
scoring (str | callable) –
see scoring of Regression at https://scikit-learn.org/stable/modules/model_evaluation.html#common-cases-predefined-values If is str, choices=(explained_variance, r2, correlation). If is callable, the inputs and outputs should imitate scoring metrics in sklearn.
The input parameters should include y_true, y_pred, and multioutput at least.
cv (int) – The number of runs of the cross validation.
multi_trg_flag (bool) – Whether the regressor is able to fit multi-targets at onces or not.
- Returns
scores – shape=(cv, n_target)
- Return type
ndarray
- dnnbrain.dnn.base.dnn_fe(dnn_acts, method, n_feat, axis=None)¶
Extract features of DNN activation
- Parameters
dnn_acts (ndarray) – DNN activation. A 4D array with its shape as (n_stim, n_chn, n_row, n_col).
method (str) –
Feature extraction method, choices: (‘pca’, ‘hist’, ‘psd’)
method name
description
pca
use n_feat principal components as features
hist
use histogram of activation as features
psd
use power spectral density as features
Note: In ‘hist’, n_feat equal-width bins in the given range will be used!
n_feat (int, float) – The number of features to extract. Note: It can be a float only when the method is pca.
axis (str) –
axis for feature extraction, choices=(chn, row_col)
axis
array shape
chn
(n_stim, n_feat, n_row, n_col)
row_col
(n_stim, n_chn, n_feat, 1)
None
(n_stim, n_feat, 1, 1)
Note: We always regard the shape of the result as (n_stim, n_chn, n_row, n_col)
- Returns
dnn_acts_new – DNN activation. A 4D array with its shape as (n_stim, n_chn, n_row, n_col).
- Return type
ndarray
- dnnbrain.dnn.base.dnn_mask(dnn_acts, channels='all', rows='all', columns='all')¶
Extract DNN activation
- Parameters
dnn_acts (ndarray) – DNN activation A 4D array with its shape as (n_stim, n_chn, n_row, n_col).
channels (str, list) – Channels of interest. If is str, it must be ‘all’ which means all channels. If is list, its elements are serial numbers of channels.
rows (str, list) – rows of interest. If is str, it must be ‘all’ which means all rows. If is list, its elements are serial numbers of rows.
columns (str, list) – Columns of interest. If is str, it must be ‘all’ which means all columns. If is list, its elements are serial numbers of columns.
- Returns
dnn_acts – DNN activation after mask. A 4D array with its shape as (n_stim, n_chn, n_row, n_col).
- Return type
ndarray
- dnnbrain.dnn.base.gen_estimator_from_name(name)¶
Generate sklearn estimator from name
- Parameters
name (str) – name of estimator
- Returns
estimator
- Return type
sklearn estimator
- dnnbrain.dnn.base.normalize(array)¶
Normalize an array’s value domain to [0, 1] Note: the original normalize function is at dnnbrain/utils/util.py but ‘from dnnbrain.dnn.core import Mask’ in the file causes import conflicts. Fix the conflicts in future.
- Parameters
array (ndarray) – A numpy array waiting normalize.
- Returns
array – A numpy array after normalization.
- Return type
ndarray