Пример #1
0
def my_t_test(a, b, a_label='a', b_label='b'):
    """Given two array-like objects, calculates the P-Value"""
    p_value = t_test(a, b)[1]
    mean_a = round(a.mean(), 2)
    mean_b = round(b.mean(), 2)
    p_value = round(p_value, 4)
    if p_value < 0.05:
        print('The difference is statistically significant. p =', p_value)
        print('Mean of array "%s": %s. N=%s' % (a_label, mean_a, len(a)))
        print('Mean of array "%s": %s. N=%s' % (b_label, mean_b, len(b)))
    else:
        print('Cannot reject the null hypothesis. p =', p_value)
        print('Mean of array "%s": %s. N=%s' % (a_label, mean_a, len(a)))
        print('Mean of array "%s": %s. N=%s' % (b_label, mean_b, len(b)))
def streak_test(rolls: list, die_size: int = None):
    if die_size is None:
        die_size = max(rolls)

    exp_diff, exp_list = expected_diff(die_size)

    diffs = []
    abs_diffs = []
    for i in range(1, len(rolls)):
        seq_diff = rolls[i] - rolls[i - 1]
        diffs.append(seq_diff)
        abs_diffs.append(abs(seq_diff))

    mean_diff = sum(diffs) / len(diffs)
    mean_abs_diff = sum(abs_diffs) / len(abs_diffs)

    t_test_result = t_test(abs_diffs, exp_list)
    p_val = t_test_result.pvalue
    return p_val, mean_abs_diff, exp_diff
Пример #3
0
def Plot_electrode_welch(attention, motor, freqs, electrode, con1, con2, comparison_type, PLOT_ALL = True):


    plt.close('all')
    mean_att = np.mean(attention, axis = 0)[3:36]
    mean_mot = np.mean(motor, axis = 0)[3:36]

    p_vals = []
    for frequency in range(3,36):
        t,p = t_test(attention[:, frequency], motor[:, frequency])
        p_vals.append(p)

    sigs = [idx for idx,p_val in enumerate(p_vals) if p_val < 0.005]


    if(PLOT_ALL):

        fig = plt.figure()
        fig.suptitle(ch_names[electrode])
        gs = gridspec.GridSpec(2, 1, height_ratios=[2,1])
        att_mot = plt.subplot(gs[0])
        test = plt.subplot(gs[1])

        att_mot.plot(FREQS[3:36], np.log10(mean_att) , color = 'green', label = con1)
        att_mot.plot(FREQS[3:36], np.log10(mean_mot), color = 'red', label = con2)

        test.plot(FREQS[3:36], p_vals, color = 'black', linestyle = '--', label = 'p value')
        test.hlines(y = 0.005, xmin =FREQS[3], xmax = FREQS[36],  color = 'red', linestyle = '--', label = 'alpha 0.005')

        test.set_xlim(FREQS[3], FREQS[36])
        att_mot.set_xlim(FREQS[3], FREQS[36])

        test.set_ylabel('Independent t-test')
        att_mot.set_ylabel('Welch')
        test.set_xlabel('Frequency')
        test.legend(loc = 'best')
        att_mot.legend(loc = 'best')

        directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' + comparison_type + '/All/' + con1 + '_' + con2
        if not os.path.exists(directory):
            os.makedirs(directory)

        savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' + comparison_type + '/All/' + con1 + '_' + con2 +'/'+ ch_names[electrode] + '.png')

        if(sigs != []):
            directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' + comparison_type + '/Sig/' + con1 + '_' + con2
            if not os.path.exists(directory):
                os.makedirs(directory)
            savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' + comparison_type + '/Sig/' + con1 + '_' + con2 +'/'+ ch_names[electrode] + '.png')

            test.set_axis_bgcolor('red')
            print('FOUND DIFFRENCE AT: ')
            print(np.array(sigs) *2)




    if((sigs != []) & (PLOT_ALL == False)):

        fig = plt.figure()
        fig.suptitle(ch_names[electrode])
        gs = gridspec.GridSpec(2, 1, height_ratios=[2,1])
        att_mot = plt.subplot(gs[0])
        test = plt.subplot(gs[1])

        att_mot.plot(FREQS[3:36], np.log10(mean_att) , color = 'green', label =con1)
        att_mot.plot(FREQS[3:36], np.log10(mean_mot), color = 'red', label = con2)

        test.plot(FREQS[3:36], p_vals, color = 'black', linestyle = '--', label = 'p value')
        test.hlines(y = 0.005, xmin =FREQS[3], xmax = FREQS[36],  color = 'red', linestyle = '--', label = 'alpha 0.005')

        test.set_xlim(FREQS[3], FREQS[36])
        att_mot.set_xlim(FREQS[3], FREQS[36])

        test.set_ylabel('Independent t-test')
        att_mot.set_ylabel('Welch')
        test.set_xlabel('Frequency')
        test.legend(loc = 'best')
        att_mot.legend(loc = 'best')

        test.set_axis_bgcolor('red')
        print('FOUND DIFFRENCE AT: ')
        print(np.array(sigs) *2)
Пример #4
0
def get_significant_groupings(groupdata, labels=None, cutoff=0.05):
    print("\t\tDetermining significant letter groupings for plot\n")
    if labels == None:
        labels = ["Data" + str(i + 1)
                  for i in range(len(groupdata))]  # Labels for each dataset

    # Calculate p-values
    pvals = np.empty(shape=(len(groupdata), len(groupdata)))
    pvals[:] = np.nan
    for i in range(len(groupdata)):
        myset = set()
        for j in range(i + 1, len(groupdata)):
            t, p = t_test(groupdata[i], groupdata[j])
            pvals[i, j], pvals[j, i] = p, p
    pvals = pd.DataFrame(pvals, columns=labels, index=labels)
    print("P-values:\n", pvals)

    # Make all possible combinations of groups
    allgroups = list()
    for size in range(2, len(labels) + 1):
        for mygroup in itertools.combinations(labels, size):
            allgroups.append(mygroup)
            #print(mygroup)

    # Subset to just the ones where all members are statistically indistinguishable
    goodgroups = list()
    for mygroup in allgroups:
        isgood = True
        for i in range(len(mygroup)):
            for j in range(i, len(mygroup)):
                if pvals.loc[mygroup[i], mygroup[j]] <= cutoff:
                    isgood = False
        #print(isgood, mygroup)
        if isgood: goodgroups.append(mygroup)

    # Check if one-member groups exist by collapsing groups to a set of labels
    already_included = set(np.hstack(goodgroups))
    for l in labels:
        if l not in already_included:
            goodgroups.append([l])  #Append 1-item list of groups
    #print(goodgroups)

    # Now assign letter values; do in a loop over labels to preserve order
    lettercodes = {l: ""
                   for l in labels
                   }  # Dictionary of which letter codes each label has
    letter_i = 0  # Keeping track of which group label we're using
    for l in labels:
        for g in goodgroups:
            if l not in g: continue  # Skip groups this label is not in
            for mylabel in g:
                lettercodes[mylabel] += string.ascii_lowercase[letter_i]
            goodgroups.remove(
                g
            )  # Remove a group after it's been processed so it doesn't get processed a second time
        letter_i += 1
    #print(lettercodes)

    # Make letters in order of original list
    lettervals = [lettercodes[l] for l in labels]
    #print(lettervals)
    return lettervals, pvals
Пример #5
0
def Plot_electrode_welch(all_results, training, event_type, conditions):
    """Takes for an input a dictionary which has two fields, electrodes_a and b. event_type is a string. condition is a list of strings describing what comparison is made"""
    #plt.close('all')

    for (name_a, electrode_a), (name_b, electrode_b) in zip(all_results[conditions[0]].items(), all_results[conditions[1]].items()):
        fig = plt.figure()
        fig.suptitle(name_a + '\n'+  conditions[0] + ' vs ' + conditions[1] + '\n' + event_type, fontweight = 'bold')
        a_b_conditions = fig.add_subplot(111)
#Iterate in two seperate loops since they might (and in fact are for sure) of unequal lengths
        _min = np.argmax(FREQS > 0)
        _max = np.argmax(FREQS > 30)
        for trial_a in electrode_a:
            #just reshaping
            normed_a = trial_a[_min:_max] / trial_a[_min:_max].mean()

            a_b_conditions.plot(FREQS[_min: _max], np.log10(normed_a), color = 'green', alpha = 0.1)

        for trial_b in electrode_b:
            #just reshaping
            normed_b = trial_b[_min:_max] / trial_b[_min:_max].mean()
            a_b_conditions.plot(FREQS[_min:_max], np.log10(normed_b), color = 'red', alpha = 0.1)

     #   a_b_conditions.set_xlim(min(FREQS), max(FREQS))
        a_b_conditions.set_xlabel('Frequency')
        a_b_conditions.set_ylabel('Welch power spectrum')

        try:
            mean_a = electrode_a.mean(axis = 0)#[_min:_max]
            mean_b = electrode_b.mean(axis = 0)
         #   print(mean_a)
            a_b_conditions.plot(FREQS[_min:_max], np.log10(mean_a[_min:_max]/mean_a[_min:_max].mean()) ,label = conditions[0], linewidth=2, color = 'green')
            a_b_conditions.plot(FREQS[_min:_max], np.log10(mean_b[_min:_max]/mean_b[_min:_max].mean()) ,label = conditions[1], linewidth=2, color = 'red')

            p_vals = []
            for frequency in range(0, len(FREQS)):
                t,p = t_test(electrode_a[:, frequency], electrode_b[:, frequency])
                p_vals.append(p)

            sigs = [idx for idx,p_val in enumerate(p_vals) if p_val < 0.005]

            #save in all
            for sig in sigs:
                #sig times two because the index is half the actuall frequency
                a_b_conditions.vlines(x = sig*2, ymin = -2.5, ymax = 1.5, color = 'blue', linestyle = '--', alpha = 0.2)
             #   test.hlines(y = 0.005, xmin =FREQS[3], xmax = FREQS[36],  color = 'red', linestyle = '--', label = 'alpha 0.005')

            a_b_conditions.legend(loc = 'best')

            path_conditions = conditions[0] + '_' + conditions[1]
            directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/Independent/'+ event_type +'/' + path_conditions + '/'+ training + '/All/'
            if not os.path.exists(directory):
                os.makedirs(directory)

            plt.savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/Independent/'+ event_type +'/' + path_conditions+ '/'+ training + '/All/'+ name_a + '.png')



            #Save also in sigs

            if sigs != []:
                directory_sig = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/Independent/'+ event_type +'/' + path_conditions+  '/'+ training + '/Sig/'
                if not os.path.exists(directory_sig):
                    os.makedirs(directory_sig)
                plt.savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/Independent/'+ event_type +'/' + path_conditions + '/'+ training + '/Sig/'+ name_a + '.png')
        except:
           # print(a_avg)
            #print(b_avg)
            print('Probably not enouth trials')
Пример #6
0
def Plot_electrode_welch(_all_results, event_type, condition):
    plt.close('all')
    #event type, i.e. databse, CUe or Target, conditon, for example 'attention correct

#This loop is to reverse the nesting from subject * electrodes to the other way
    all_electrodes_before_and_after = {name: [] for name in ch_names}
    for subject in _all_results:
        for before_electrode, after_electrode, electrode_name in zip(subject['electrodes_before'], subject['electrodes_after'], ch_names):
           all_electrodes_before_and_after[electrode_name].append({'before':before_electrode,  'after':after_electrode})

    for key, electrode in all_electrodes_before_and_after.items():
        fig = plt.figure()
        fig.suptitle(key, fontweight = 'bold')
        before_after = plt.subplot()
        before_avg, after_avg = [],[]

        for idx, subject_avg in enumerate(electrode):
            before_after.plot(FREQS, np.log10(subject_avg['after']), color = 'red', label = 'after', alpha = 0.1)
            before_after.plot(FREQS, np.log10(subject_avg['before']), color = 'green', label = 'before', alpha = 0.1)
            before_avg.append(subject_avg['before'])
            after_avg.append(subject_avg['after'])

        before_avg = np.array(before_avg)
        after_avg = np.array(after_avg)


        before_after.plot(FREQS, np.log10(before_avg.mean(axis = 0)), linewidth=2, color = 'green')
        before_after.plot(FREQS, np.log10(after_avg.mean(axis = 0)), linewidth=2, color = 'red')

        before_after.set_xlim(min(FREQS), max(FREQS))
        before_after.set_xlabel('Frequency')
        before_after.set_ylabel('Welch power spectrum')


        p_vals = []
        for frequency in range(0, len(FREQS)):
            t,p = t_test(before_avg[:, frequency], after_avg[:, frequency])
            p_vals.append(p)

        sigs = [idx for idx,p_val in enumerate(p_vals) if p_val < 0.005]

        #save in all
        for sig in sigs:
            #sig times two because the index is half the actuall frequency
            before_after.vlines(x = sig*2, ymin = -2.5, ymax = 1.5, color = 'blue', linestyle = '--', label = 'alpha 0.005', alpha = 0.2)
         #   test.hlines(y = 0.005, xmin =FREQS[3], xmax = FREQS[36],  color = 'red', linestyle = '--', label = 'alpha 0.005')



        directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/'+ event_type +'/' + condition + '/All/'
        if not os.path.exists(directory):
            os.makedirs(directory)

        plt.savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/'+ event_type +'/' + condition + '/All/'+ key + '.png')



        #Save also in sigs

        if sigs != []:
            directory_sig = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/'+ event_type +'/' + condition + '/Sig/'
            if not os.path.exists(directory_sig):
                os.makedirs(directory_sig)
            plt.savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/'+ event_type +'/' + condition + '/Sig/'+ key + '.png')
Пример #7
0
def Plot_electrode_welch(_all_results, event_type, condition):
    plt.close('all')
    #event type, i.e. databse, CUe or Target, conditon, for example 'attention correct

    #This loop is to reverse the nesting from subject * electrodes to the other way
    all_electrodes_before_and_after = {name: [] for name in ch_names}
    for subject in _all_results:
        for before_electrode, after_electrode, electrode_name in zip(
                subject['electrodes_before'], subject['electrodes_after'],
                ch_names):
            all_electrodes_before_and_after[electrode_name].append({
                'before':
                before_electrode,
                'after':
                after_electrode
            })

    for key, electrode in all_electrodes_before_and_after.items():
        fig = plt.figure()
        fig.suptitle(key, fontweight='bold')
        before_after = plt.subplot()
        before_avg, after_avg = [], []

        for idx, subject_avg in enumerate(electrode):
            before_after.plot(FREQS,
                              np.log10(subject_avg['after']),
                              color='red',
                              label='after',
                              alpha=0.1)
            before_after.plot(FREQS,
                              np.log10(subject_avg['before']),
                              color='green',
                              label='before',
                              alpha=0.1)
            before_avg.append(subject_avg['before'])
            after_avg.append(subject_avg['after'])

        before_avg = np.array(before_avg)
        after_avg = np.array(after_avg)

        before_after.plot(FREQS,
                          np.log10(before_avg.mean(axis=0)),
                          linewidth=2,
                          color='green')
        before_after.plot(FREQS,
                          np.log10(after_avg.mean(axis=0)),
                          linewidth=2,
                          color='red')

        before_after.set_xlim(min(FREQS), max(FREQS))
        before_after.set_xlabel('Frequency')
        before_after.set_ylabel('Welch power spectrum')

        p_vals = []
        for frequency in range(0, len(FREQS)):
            t, p = t_test(before_avg[:, frequency], after_avg[:, frequency])
            p_vals.append(p)

        sigs = [idx for idx, p_val in enumerate(p_vals) if p_val < 0.005]

        #save in all
        for sig in sigs:
            #sig times two because the index is half the actuall frequency
            before_after.vlines(x=sig * 2,
                                ymin=-2.5,
                                ymax=1.5,
                                color='blue',
                                linestyle='--',
                                label='alpha 0.005',
                                alpha=0.2)
        #   test.hlines(y = 0.005, xmin =FREQS[3], xmax = FREQS[36],  color = 'red', linestyle = '--', label = 'alpha 0.005')

        directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/' + event_type + '/' + condition + '/All/'
        if not os.path.exists(directory):
            os.makedirs(directory)

        plt.savefig(
            '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/' +
            event_type + '/' + condition + '/All/' + key + '.png')

        #Save also in sigs

        if sigs != []:
            directory_sig = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/' + event_type + '/' + condition + '/Sig/'
            if not os.path.exists(directory_sig):
                os.makedirs(directory_sig)
            plt.savefig(
                '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/BeforeAfter/' +
                event_type + '/' + condition + '/Sig/' + key + '.png')
Пример #8
0
def Plot_electrode_welch(attention,
                         motor,
                         freqs,
                         electrode,
                         con1,
                         con2,
                         comparison_type,
                         PLOT_ALL=True):

    plt.close('all')
    mean_att = np.mean(attention, axis=0)[3:36]
    mean_mot = np.mean(motor, axis=0)[3:36]

    p_vals = []
    for frequency in range(3, 36):
        t, p = t_test(attention[:, frequency], motor[:, frequency])
        p_vals.append(p)

    sigs = [idx for idx, p_val in enumerate(p_vals) if p_val < 0.005]

    if (PLOT_ALL):

        fig = plt.figure()
        fig.suptitle(ch_names[electrode])
        gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])
        att_mot = plt.subplot(gs[0])
        test = plt.subplot(gs[1])

        att_mot.plot(FREQS[3:36],
                     np.log10(mean_att),
                     color='green',
                     label=con1)
        att_mot.plot(FREQS[3:36], np.log10(mean_mot), color='red', label=con2)

        test.plot(FREQS[3:36],
                  p_vals,
                  color='black',
                  linestyle='--',
                  label='p value')
        test.hlines(y=0.005,
                    xmin=FREQS[3],
                    xmax=FREQS[36],
                    color='red',
                    linestyle='--',
                    label='alpha 0.005')

        test.set_xlim(FREQS[3], FREQS[36])
        att_mot.set_xlim(FREQS[3], FREQS[36])

        test.set_ylabel('Independent t-test')
        att_mot.set_ylabel('Welch')
        test.set_xlabel('Frequency')
        test.legend(loc='best')
        att_mot.legend(loc='best')

        directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' + comparison_type + '/All/' + con1 + '_' + con2
        if not os.path.exists(directory):
            os.makedirs(directory)

        savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' +
                comparison_type + '/All/' + con1 + '_' + con2 + '/' +
                ch_names[electrode] + '.png')

        if (sigs != []):
            directory = '/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' + comparison_type + '/Sig/' + con1 + '_' + con2
            if not os.path.exists(directory):
                os.makedirs(directory)
            savefig('/Users/ryszardcetnarski/Desktop/Nencki/TD/Figs/' +
                    comparison_type + '/Sig/' + con1 + '_' + con2 + '/' +
                    ch_names[electrode] + '.png')

            test.set_axis_bgcolor('red')
            print('FOUND DIFFRENCE AT: ')
            print(np.array(sigs) * 2)

    if ((sigs != []) & (PLOT_ALL == False)):

        fig = plt.figure()
        fig.suptitle(ch_names[electrode])
        gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])
        att_mot = plt.subplot(gs[0])
        test = plt.subplot(gs[1])

        att_mot.plot(FREQS[3:36],
                     np.log10(mean_att),
                     color='green',
                     label=con1)
        att_mot.plot(FREQS[3:36], np.log10(mean_mot), color='red', label=con2)

        test.plot(FREQS[3:36],
                  p_vals,
                  color='black',
                  linestyle='--',
                  label='p value')
        test.hlines(y=0.005,
                    xmin=FREQS[3],
                    xmax=FREQS[36],
                    color='red',
                    linestyle='--',
                    label='alpha 0.005')

        test.set_xlim(FREQS[3], FREQS[36])
        att_mot.set_xlim(FREQS[3], FREQS[36])

        test.set_ylabel('Independent t-test')
        att_mot.set_ylabel('Welch')
        test.set_xlabel('Frequency')
        test.legend(loc='best')
        att_mot.legend(loc='best')

        test.set_axis_bgcolor('red')
        print('FOUND DIFFRENCE AT: ')
        print(np.array(sigs) * 2)