# statistics for temporal generation decoding matrix

**URL:** <https://mne.discourse.group/t/statistics-for-temporal-generation-decoding-matrix/4428>\
**Category:** Support & Discussions\
**Tags:** statistics, machine-learning\
**Created:** [February 17, 2022, 2:15pm UTC](https://mne.discourse.group/t/statistics-for-temporal-generation-decoding-matrix/4428 "2022-02-17T14:15:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mengjin123](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mengjin123/32/217_2.png) [@mengjin123](https://mne.discourse.group/u/mengjin123)\
**Post date:** [February 17, 2022, 2:15pm UTC](https://mne.discourse.group/t/statistics-for-temporal-generation-decoding-matrix/4428/1 "2022-02-17T14:15:20Z")

</div>

- MNE version: e.g. 0.24.0
- operating system: Ubuntu 18.04

Hello MNE users,  
I am attempting to do statistics for the results of temporal generation decoding matrix across subjects, while computing the p\_values using the function “mne.stats.spatio\_temporal\_cluster\_1samp\_test(…)”, I alway get the KERNAL KILLED ERROR!! despite i changed the n\_permutations into 200; How can I fix this error? Or was my method approprite to the 2-D array (1693, 1693) statistics across subjects(n\_subject=15)? Here is my code:

```python
import os
import os.path as op
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import wilcoxon
# import seaborn as sns
# sns.set_style("white")
# sns.set_context('paper')
import pickle

import mne
from mne.parallel import parallel_func
from mne.stats import (ttest_1samp_no_p, bonferroni_correction, fdr_correction,
                       permutation_t_test, permutation_cluster_1samp_test,
                      spatio_temporal_cluster_1samp_test)

print( __doc__ )

# define functions
def stats(X):
    """Statistical test applied across subjects"""
    #check input
    X = np.array(X)
    X = X[:,:, None] if X.ndim==2 else X

    # stats function report p_value for each cluster
    T_obs_, clusters, p_values, _ = spatio_temporal_cluster_1samp_test(
    X, out_type='mask', n_permutations=200, n_jobs=-1, verbose=False)

    # format p_values to get same dimensionality as X
    p_values_ = np.one_like(X[0]).T
    for cluster, pval in zip(clusters, p_values):
        p_values_[cluster.T] = pval

        return np.squeeze(p_values_).T

# load data
condition = {'cond0':['LFF2-LHF2', 'RFF2-RHF2', 'LHH2-LFH2', 'RHH2-RFH2'],
             'cond1':['LFF4-LHF4', 'RFF4-RHF4', 'LHH4-LFH4', 'RHH4-RFH4'],
             'cond2':['LFF6-LHF6', 'RFF6-RHF6', 'LHH6-LFH6', 'RHH6-RFH6']}

dest_dir = 'allsubjs_decoding_temporal_generation/prime_contrast'
times = pd.read_csv(op.join(dest_dir, 'epochs_times.csv'))
times = times.to_numpy()

chance = .5
# N = len(condition)
N=1
for k in range(N):
    key = 'cond%d' %k
    cond = condition[key]
    for con in cond:
        print(con)
        data = list()
        for sub in range(1, 11):
            subject = 'S%02d' %sub
            filename = '%s decoding temporal generation %s.csv' %(subject, con)    
            data_dir = op.join(subject, 'Decoding_temporal_generation/prime_contrast')
            df = pd.read_csv(op.join(data_dir, filename))
            df = df.iloc[:, 1:1694]
            data.append(df)

        p_values = stats(np.array(data)-chance)

```

In my code, the data shape is (15, 1693,1693)

Here is the error pages:  
 ![origin_img_v2_e81a4006-0c2b-4641-be13-7b1a8030af9g](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/5/51dfadf2e3a03e8473d153902ffa63c517720f30.png)

Thank you very much, and best wishes,  
Mengjin

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [February 21, 2022, 3:19pm UTC](https://mne.discourse.group/t/statistics-for-temporal-generation-decoding-matrix/4428/3 "2022-02-21T15:19:41Z")

</div>

Before digging into the clustering stuff, does your `stat_fun` already produce a reasonable result for a plain ttest\_1samp? Like if you plot the t values they are reasonable (most around zero, some for t \> 0 have t values around 2-10?

Once this sanity check makes sense, then I’d dig into the clustering function. For this I actually wouldn’t use the spatio-temporal variant, but actually just the plain 1samp test. Your temporal generalization matrices have a lattice structure (left-right and up-down are “adjacent”) and it should in theory work more quickly.

I would also use `out_type='indices'` to reduce memory consumption

---

<div class="post-metadata">

**Author:** ![mengjin123](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mengjin123/32/217_2.png) [@mengjin123](https://mne.discourse.group/u/mengjin123)\
**Post date:** [February 24, 2022, 1:42pm UTC](https://mne.discourse.group/t/statistics-for-temporal-generation-decoding-matrix/4428/4 "2022-02-24T13:42:29Z")

</div>

Thanks for your reply.

> does your `stat_fun` already produce a reasonable result for a plain ttest\_1samp?

yes, the stat\_fun is work and reasonable for a plain ttest\_1samp.

BTW, how can I do the plain 1samp test for 1693\*1693 across 15 subjects?

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [February 24, 2022, 5:26pm UTC](https://mne.discourse.group/t/statistics-for-temporal-generation-decoding-matrix/4428/5 "2022-02-24T17:26:21Z")

</div>

> [@mengjin123](#):
>
> BTW, how can I do the plain 1samp test for 1693\*1693 across 15 subjects?

If your array `X` is shape `(15, 1693, 1693)` you can just do `scipy.stats.ttest_1samp(X, 0, axis=0)` (which is the default axis anyway)
