#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Daniel 

This script contains all the functions used to analyse the structural data. 

It contains a function to calculate spearmans correlation, cohens d and kruskal test. 
"""

import pandas as pd
from scipy import stats
import math


def correlation(column, volumearray, volume):
    
    '''
    Runs Spearmans correlation.
    
    Input: df with measures that area all used in the correlation. 
    
    Second array/df with thrid variable (string) that states which measure from this second array/df to be used in the correlation.
    
    Returns: pvalues and correlation values in list
    
    '''
    
    pvalues=[]
    correlation=[]
    for i in column.columns:
        array=pd.concat([volumearray[volume], column[i]],axis=1).dropna()
        c, p =stats.spearmanr(array[volume], array[i])
        pvalues.append(p)
        correlation.append(c)
    return pvalues, correlation



def cohen_d(group1,group2):
    
    '''
    Calculate cohens d.
    
    Input: two series/list
    
    Output: d
    
    '''
    
    
    diff = group1.mean()-group2.mean()
    pooledstdev = math.sqrt((group1.std()**2 + group2.std())/2 )
    cohend = diff / pooledstdev
    return cohend

def kruskal(x,y,z):
    
    '''
    Runs kruskal-wallis test.
    
    Input: Series from three groups.
    
    Output: df with pvals, degrees of freedom and eta/epsilon effect sizes
    
    
    '''
    
    number_of_observations = len(pd.concat([x,y,z]))
    number_of_groups = len([x, y, z] )
    h,p = stats.kruskal(x,y,z)
    degrees_of_freedom=(number_of_observations-number_of_groups)-(number_of_groups)
    eta = (h-number_of_groups+1)/(number_of_observations-number_of_groups)
    epsilon  = h/((number_of_observations**2-1)/(number_of_observations+1))
    df= pd.DataFrame({'pval': [p], 'kruskal_test_statistic': [h],'df': [degrees_of_freedom],'eta': [eta],'epsilon':[epsilon]})
    return df
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Daniel 

This script contains all the functions used to analyse the structural data. 

It contains a function to calculate spearmans correlation, cohens d and kruskal test. 
"""

import pandas as pd
from scipy import stats
import math


def correlation(column, volumearray, volume):
    
    '''
    Runs Spearmans correlation.
    
    Input: df with measures that area all used in the correlation. 
    
    Second array/df with thrid variable (string) that states which measure from this second array/df to be used in the correlation.
    
    Returns: pvalues and correlation values in list
    
    '''
    
    pvalues=[]
    correlation=[]
    for i in column.columns:
        array=pd.concat([volumearray[volume], column[i]],axis=1).dropna()
        c, p =stats.spearmanr(array[volume], array[i])
        pvalues.append(p)
        correlation.append(c)
    return pvalues, correlation



def cohen_d(group1,group2):
    
    '''
    Calculate cohens d.
    
    Input: two series/list
    
    Output: d
    
    '''
    
    
    diff = group1.mean()-group2.mean()
    pooledstdev = math.sqrt((group1.std()**2 + group2.std())/2 )
    cohend = diff / pooledstdev
    return cohend

def kruskal(x,y,z):
    
    '''
    Runs kruskal-wallis test.
    
    Input: Series from three groups.
    
    Output: df with pvals, degrees of freedom and eta/epsilon effect sizes
    
    
    '''
    
    number_of_observations = len(pd.concat([x,y,z]))
    number_of_groups = len([x, y, z] )
    h,p = stats.kruskal(x,y,z)
    degrees_of_freedom=(number_of_observations-number_of_groups)-(number_of_groups)
    eta = (h-number_of_groups+1)/(number_of_observations-number_of_groups)
    epsilon  = h/((number_of_observations**2-1)/(number_of_observations+1))
    df= pd.DataFrame({'pval': [p], 'kruskal_test_statistic': [h],'df': [degrees_of_freedom],'eta': [eta],'epsilon':[epsilon]})
    return df
#!/usr/bin/env python3 # -*- coding: utf-8 -*- @author: Daniel   This script contains all the functions used to analyse the structural data.   It contains a function to calculate spearmans correlation, cohens d and kruskal test. Runs Spearmans correlation.          Input: df with measures that area all used in the correlation.           Second array/df with thrid variable (string) that states which measure from this second array/df to be used in the correlation.          Returns: pvalues and correlation values in list Calculate cohens d.          Input: two series/list          Output: d Runs kruskal-wallis test.          Input: Series from three groups.          Output: df with pvals, degrees of freedom and eta/epsilon effect sizes