dnnbrain.utils.util module¶
- dnnbrain.utils.util.ceiling_RSA(rdms, corr_type='pearson')¶
Adapted from (Nili et al., 2014, PLOS Computational Biology) Given a set of observed RDMs (e.g. from multiple subjects), this function estimates upper and lower bounds on the ceiling, i.e. the highest average RDM correlation (across the observed RDMs) that the true model’s RDM prediction achieves given the variability of the estimates.
- Parameters
rdms (ndarray) – shape=(n_observation, n_item, n_item) observed RDMs
corr_type (str) – Correlation measure to be used, choices=(‘pearson’,). If is ‘pearson’, z-transforming the dissimilarities in each observed RDM before estimating. Because the estimating procedure involves averaging across observations. Default is ‘pearson’.
- Returns
lower_boundary (float)
upper_boundary (float)
best_fitted_rdm (ndarray) – shape=(n_item, n_item) the average RDM across observations after preprocessing according to corr_type.
- dnnbrain.utils.util.clustering(data, n_clusters, method, **kwargs)¶
- Parameters
data ({array-like, sparse matrix} of shape (n_samples, n_features)) –
n_clusters (int) – The number of clusters to form It will be ignored when the method is ‘DBSCAN’.
method (str) – specify the cluster method
kwargs (keyword arguments) –
- Returns
labels
- Return type
ndarray of shape (n_samples,)
- dnnbrain.utils.util.gen_dmask(layers=None, channels='all', dmask_file=None)¶
Generate DNN mask object by: 1. combining layers and channels. 2. loading from dmask file.
- Parameters
layers (list) – Layer names.
channels (str, list) – Channel numbers. It will be ignored if layers is None.
dmask_file (str) – A .dmask.csv file.
- Returns
dmask – DNN mask.
- Return type
- dnnbrain.utils.util.get_frame_time_info(vid_file, original_onset, interval=1, before_vid=0, after_vid=0)¶
Extract frames of interest from a video with their onsets and durations, according to the experimental design.
- Parameters
vid_file (str) – Video file path.
original_onset (float) – The first stimulus’ time point relative to the beginning of the response. For example, if the response begins at 14 seconds after the first stimulus, the original_onset is -14.
interval (int) – Get one frame per ‘interval’ frames,
before_vid (float) – Display the first frame as a static picture for ‘before_vid’ seconds before video.
after_vid (float) – Display the last frame as a static picture for ‘after_vid’ seconds after video.
- Returns
frame_nums (list) – Sequence numbers of the frames of interest.
onsets (list) – Onsets of the frames of interest.
durations (list) – Durations of the frames of interest.
- dnnbrain.utils.util.normalize(array)¶
Normalize an array’s value domain to [0, 1]
- arrayndarray
A numpy array.
- arrayndarray
A numpy array after normalization.
- dnnbrain.utils.util.permutation_RSA(rdm1, rdm2, corr_type='pearson', n_iter=10000)¶
Adapted from (Nili et al., 2014, PLOS Computational Biology) Test the relatedness of two RDMs by permutating item labels.
- Parameters
rdm1 (ndarray) – shape=(n_item, n_item)
rdm2 (ndarray) – shape=(n_item, n_item)
corr_type (str) – correlation measure to be used choices=(‘pearson’, ‘spearman’, ‘kendall’) Default is ‘pearson’.
n_iter (int) – the number of iterations of permutation Default is 10000.
- Returns
observed_R (float) – the correlation between two RDMs
permuted_Rs (ndarray) – shape=(n_iter,) correlations between two RDMs during permutation
P (float) – P-value of the observed correlation based on the distribution of permuted correlations.
- dnnbrain.utils.util.topk_accuracy(pred_labels, true_labels, k)¶
Calculate top k accuracy for the classification results.
- pred_labelsarray-like
Predicted labels, 2d array with shape as (n_stim, n_class). Each row’s labels are sorted from large to small their probabilities.
- true_valuesarray-like
True values, 1d array with shape as (n_stim,).
- kint
The number of tops.
Return: acc : float
Top k accuracy.