This notebook extracts the data from the main experiment, i.e. the face morphing
import numpy as np
import pandas as pd
import os
import fnmatch
from myBasics import *
def get_logfile(whichfolder, whichexperiment):
loglist = []
for fileName in os.listdir(whichfolder):
if fnmatch.fnmatch(fileName, whichexperiment):
loglist.append(whichfolder+fileName)
return loglist
loglist = get_logfile('../experiment/data/','*facesParametric*.csv')
loglist.sort()
Example:
loglist[:5]
def getMorphResps(fileName):
fullDf = pd.read_csv(fileName)
# boil down to just the essentials
thisDf = pd.concat([fullDf['img'],
fullDf['mouseResp.leftButton'],
fullDf['mouseResp.rightButton'],
fullDf['mouseResp.time']
],axis=1)
# we output starting at row 89, because the previous
# rows belong the previous experiment (basic expression recognition)
return thisDf[89:]
Example:
getMorphResps(loglist[-1]).head()
def getCondition(stimList):
part = []
grades = []
genders = []
idents = []
pt = 1
for entry in stimList:
try:
grade = entry[entry.find('_')+1:entry.rfind('_')]
ident = entry[entry.find('Morph')+len('Morph'):entry.find('_')]
gender = entry[entry.find('Morph')+len('Morph'):entry.find('Morph')+len('Morph')+1]
grades.append(grade)
idents.append(ident+'_'+str(pt))
genders.append(gender)
part.append(pt)
except:
grades.append(float(np.nan))
idents.append(float(np.nan))
genders.append(float(np.nan))
part.append(float(np.nan))
pt = 2
return part,grades,genders,idents
def makeTable(fileName):
thisDf = getMorphResps(fileName)
# get the file names from which the conditions are extracted
stimList = thisDf['img']
#
parts,grades,genders,idents = getCondition(stimList)
# apply
#thisDf['part'] = parts
thisDf['grades'] = grades
thisDf['gender'] = genders
thisDf['idents'] = idents
# sort
#thisDf = thisDf.sort(['part','gender','grades','idents'])
thisDf = thisDf.sort_values(by=['grades','gender'])
# set index
#thisDf = thisDf.set_index(['part','gender','grades'])
thisDf = thisDf.set_index(['grades','gender','idents'])
# drop nan
thisDf = thisDf.dropna()
return thisDf
Example:
makeTable(loglist[-1]).head()
def cleanAvg(fileName):
fileNum = fileName[fileName.find('_')+1:fileName.find('faces')-1]
if int(fileNum)%2 == 0:
targetButton = 'mouseResp.leftButton'
nonTargetButton = 'mouseResp.rightButton'
else:
targetButton = 'mouseResp.rightButton'
nonTargetButton = 'mouseResp.leftButton'
pName = (fileName[fileName.rfind('/')+1:fileName.find('_')]+ ('000'+fileNum)[-3:])
thisDf = makeTable(fileName)
# get rid of everything but
thisDf = thisDf.drop('img',1)
thisDf = thisDf.drop(nonTargetButton,1)
thisDf = thisDf.drop('mouseResp.time',1)
# restructure for gender-based averaging
thisDfWithin = thisDf.unstack(0)
thisDfWithin.columns = thisDfWithin.columns.droplevel()
# average by gender
fDf = pd.DataFrame( thisDfWithin.ix['F'].mean(),columns=['F'] )
mDf = pd.DataFrame( thisDfWithin.ix['M'].mean(),columns=['M'] )
# restructure
avgDf = pd.concat([fDf,mDf],axis=1).T
# nice indexing
avgDf.index = [ [pName[:1]]*len(avgDf.index),
[pName]*len(avgDf.index),
avgDf.index
]
avgDf['group'] = [ labelCoding[avgDf.index.levels[0][-1]], labelCoding[avgDf.index.levels[0][-1] ] ]
return avgDf
Example:
cleanAvg(loglist[0])
def makeBigOne(allFiles):
for fileName in allFiles:
thisDf = cleanAvg(fileName)
try:
bigDf = pd.concat([bigDf,thisDf])
except:
bigDf = thisDf
bigDf.index.names = ['g','p','fgender']
bigDf = bigDf.sort_index()
return bigDf
bigDf = makeBigOne(loglist)
bigDf.tail()
bigDf.to_csv('../outputs/genderTable.csv')
bigDfUnstacked = bigDf[bigDf.columns[:-1]].unstack(2).stack(0).unstack(2)
bigJasp = bigDfUnstacked.copy()
myColumns = [str(x[0])+'_'+str(x[-1]) for x in bigDfUnstacked.columns]
bigJasp.columns = myColumns
bigJasp.head()
bigJasp.to_csv('../outputs/genderMorphsJASP.csv')
def makeAverage(bigDf):
meanDf = pd.DataFrame()
for entry in bigDf.index.levels[1]:
thisRow = pd.DataFrame( list(bigDf.ix[entry[0]].ix[entry].mean()) ).T
thisRow.index = [entry]
meanDf = pd.concat([meanDf,thisRow])
meanDf.index.name = 'p'
meanDf.index = [ [str(x)[0] for x in list(meanDf.index) ] ,meanDf.index]
return meanDf[meanDf.columns[:-1]]
meanDf = makeAverage(bigDf)
meanDf.head()
meanDf.to_csv('../outputs/meanMorphsTable.csv')