dnnbrain.dnn.core module

class dnnbrain.dnn.core.Activation(layer=None, value=None)

Bases: object

DNN activation

concatenate(activations)

Concatenate activations from different batches of stimuli

Parameters

activations (list) – A list of Activation objects

Returns

activation – DNN activation

Return type

Activation

convolve_hrf(onsets, durations, n_vol, tr, ops=100)

Convolve DNN activation with HRF and align with the timeline of BOLD signal

Parameters
  • onsets (array_like) – In sec. size = n_event

  • durations (array_like) – In sec. size = n_event

  • n_vol (int) – The number of volumes of BOLD signal

  • tr (float) – Repeat time in second

  • ops (int) – Oversampling number per second

Returns

activation – DNN activation

Return type

Activation

delete(layer)

Delete DNN activation

Parameters

layer (str) – Layer name

fe(method, n_feat, axis=None)

Extract features of DNN activation

Parameters
  • method (str) –

    Feature extraction method, choices are as follows:

    Method name

    Model description

    pca

    use n_feat principal components as features

    hist

    use histogram of activation as features Note: n_feat equal-width bins in the given range will be used!

    psd

    use power spectral density as features

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

Returns

activation – DNN activation

Return type

Activation

get(layer)

Get DNN activation

Parameters

layer (str) – Layer name

Returns

act_layer – (n_stim, n_chn, n_row, n_col) array

Return type

array

property layers

Get all layers in the Activation

Returns

layers – The list of layers.

Return type

list

load(fname, dmask=None)

Load DNN activation

Parameters
  • fname (str) – DNN activation file

  • dmask (Mask) – The mask includes layers/channels/rows/columns of interest.

mask(dmask)

Mask DNN activation

Parameters

dmask (Mask) – The mask includes layers/channels/rows/columns of interest.

Returns

activation – DNN activation

Return type

Activation

pool(method)

Pooling DNN activation for each channel

Parameters

method (str) – Pooling method, choices=(max, mean, median, L1, L2)

Returns

activation – DNN activation

Return type

Activation

save(fname)

Save DNN activation

Parameters

fname (str) – Output file of DNN activation

set(layer, value)

Set DNN activation

Parameters
  • layer (str) – Layer name

  • value (array) – 4D DNN activation array with shape (n_stim, n_chn, n_row, n_col)

class dnnbrain.dnn.core.DnnProbe(dnn_activ=None, map_type=None, estimator=None, cv=5, scoring=None)

Bases: object

Decode DNN activation to behavior data. As a result,
probe the ability of DNN activation to predict the behavior.

probe(beh_data, iter_axis=None)

Probe the ability of DNN activation to predict the behavior.

Parameters
  • beh_data (ndarray) – Behavior data with shape as (n_stim, n_beh)

  • iter_axis (str) –

    Iterate along the specified axis. Different map type have different operations.

    map type

    iter_axis

    description

    uv

    channel

    Summarize the maximal prediction score for each channel

    row_col

    Summarize the maximal prediction score for each position (row_idx, col_idx)

    None

    Summarize the maximal prediction score for the whole layer

    mv

    channel

    Multivariate prediction using all units in each channel

    row_col

    Multivariate prediction using all units in each position (row_idx, col_idx)

    None

    Multivariate prediction using all units in the whole layer

Returns

probe_dict – A dict containing the score information

Map type

First key

First value

Second key

Second value

uv

layer

(str)

score

If estimator type is correlation, it’s an array with shape as (n_iter, n_beh).
Each element is the maximal pearson r among all features at corresponding iteration correlating to the corresponding behavior.
If estimator type is regressor or classifier, it’s an array with shape as (n_iter, n_beh, cv).
For each iteration and behavior, the third axis contains scores of each cross validation fold, when using the feature with maximal score to predict the corresponding behavior.

location

An array with shape as (n_iter, n_beh, 3)
Max locations of the max scores, the
size 3 of the third dimension means
channel, row and column respectively.

model

An array with shape as (n_iter, n_beh).
Fitted models of the max scores.
Note: not exists when estimator type is correlation.

conf_m

An array with shape as (n_iter, n_beh, cv)
The third dimension means confusion matrices (n_label, n_label) of each cross validation fold of the max scores.
Note: only exists when estimator type is classifier.

mv

layer

(str)

score

An array with shape as (n_iter, n_beh, cv)
The third dimension means scores of each cross validation fold at each iteration and behavior.

model

An array with shape as (n_iter, n_beh).
Each element is a model fitted at the
corresponding iteration and behavior.

conf_m

An array with shape as (n_iter, n_beh, cv).
The third dimension means confusion matrices (n_label, n_label) of each cross validation fold at the corresponding iteration and behavior.
Note: only exists when estimator type is classifier.

Return type

dict

set_activ(dnn_activ)

Set DNN activation

Parameters

dnn_activ (Activation) – DNN activation

set_mapper(map_type, estimator, cv, scoring)

Set mapping attributes

Parameters
  • map_type (str) – choices=(uv, mv)
    uv: univariate mapping
    mv: multivariate mapping

  • estimator (str | sklearn estimator or pipeline) – Estimator used in mapping.
    If is str, it is a name of a estimator used to do mapping.
    If the name is ‘corr’, it just uses correlation rather than prediction,
    and the map_type must be ‘uv’.

  • cv (int) – The number of cross validation folds.

  • scoring (str or callable) – The method to evaluate the predictions on the test set.

class dnnbrain.dnn.core.Mask(layer=None, channels='all', rows='all', columns='all')

Bases: object

DNN mask

clear()

Empty the DNN mask

copy()

Make a copy of the DNN mask

Returns

dmask – The mask includes layers/channels/rows/columns of interest.

Return type

Mask

delete(layer)

Delete a layer

Parameters

Layer (str) – Layer name

get(layer)

Get mask of a layer

Parameters

layer (str) – Layer name

Returns

mask – The mask of a specific layer

Return type

dict

property layers

Get all layers in the Mask

Returns

layers – The list of layers.

Return type

list

load(fname)

Load DNN mask, the whole mask will be overrode.

Parameters

fname (str) – DNN mask file

save(fname)

Save DNN mask

Parameters

fname (str) – Output file name of DNN mask

set(layer, **kwargs)

Set DNN mask

Parameters
  • layer (str) – Layer name. If layer is new, its corresponding mask value will be initialized as ‘all’.

  • kwargs (dict) –

    Keyword arguments. Only three keywords (‘channels’, ‘rows’, ‘columns’) are valid.

    Keywords

    Option

    Description

    channels

    str

    It must be ‘all’ which means all channels.

    list

    Its elements are serial numbers of channels.

    rows

    str

    It must be ‘all’ which means all rows.

    list

    Its elements are serial numbers of rows.

    columns

    str

    It must be ‘all’ which means all columns.

    list

    Its elements are serial numbers of columns.

class dnnbrain.dnn.core.RDM

Bases: object

Representation distance matrix

get(key, triu=False)

Get RDM according its key.

Parameters
  • key (str) – The key of the RDM

  • triu (bool) – If True, get RDM as the upper triangle vector. If False, get RDM as the square matrix.

Returns

rdm_arr – RDM

If rdm_type is bRDM: Its shape is ((n_item^2-n_item)/2,) or (n_item, n_item).

If rdm_type is dRDM: Its shape is (n_iter, (n_item^2-n_item)/2) or (n_iter, n_item, n_item).

Return type

ndarray

property keys

Get keys of RDM dictionary

Returns

keys – The list of keys

Return type

list

load(fname)

load RDM

Parameters

fname (str) – File name with suffix as .rdm.h5

property n_item

Get the number of items of RDM

Returns

n_item – The number of items

Return type

int

save(fname)

Save RDM

Parameters

fname (str) – File name with suffix as .rdm.h5

set(key, rdm_arr, triu=False)

Set RDM according its key.

Parameters
  • key (str) – The key of the RDM

  • rdm_arr (ndarray) –

    RDM

    If rdm_type is bRDM: Its shape is ((n_item^2-n_item)/2,) or (n_item, n_item).

    If rdm_type is dRDM: Its shape is (n_iter, (n_item^2-n_item)/2) or (n_iter, n_item, n_item).

  • triu (bool) – If True, RDM will be regarded as the upper triangle vector. If False, RDM will be regarded as the square matrix.

class dnnbrain.dnn.core.Stimulus(header=None, data=None)

Bases: object

Store and handle stimulus-related information

delete(item)

Delete a column of data according to item

Parameters

item (str) – Item name of each column

get(item)

Get a column of data according to the item

Parameters

item (str) – Item name of each column

Returns

col – A column of data

Return type

array

property items

Get list of items

Returns

items – The list of items

Return type

list

load(fname)

Load stimulus-related information

Parameters

fname (str) – File name with suffix as .stim.csv

permutate(item)

Permutate a column of data according to the item

Parameters

item (str) – Item name of the column

Returns

stim – A copy of the current instance of Stimulus except for the permutation on the specified column.

Return type

Stimulus

save(fname)

Save stimulus-related information

Parameters

fname (str) – File name with suffix as .stim.csv

set(item, value)

Set a column of data according to the item

Parameters
  • item (str) – Item name of the column

  • value (array_like) – An array_like data with shape as (n_stim,)