Permalink
Browse files

Added questionnaire scoring scripts

  • Loading branch information...
1 parent 1df6e6f commit 4dd5e391b3ec7a4d630cef7c8c8fa803ede3e808 @TheChymera committed Mar 8, 2013
Showing with 175 additions and 0 deletions.
  1. +42 −0 filter_gen.py
  2. +62 −0 import_export.py
  3. +50 −0 list_op.py
  4. +21 −0 quest.py
View
@@ -0,0 +1,42 @@
+__author__ = 'Horea Christian'
+from os import path, makedirs
+from shutil import move
+from datetime import date, datetime
+import csv
+
+def mk_newfilter(filtername, field_names, filterdir, globaldata):
+ lefilter = globaldata+filterdir+filtername+'.csv'
+ jzt=datetime.now()
+ time = str(date.today())+str(jzt.hour)+str(jzt.minute)+str(jzt.second)
+ if path.isdir(globaldata+filterdir):
+ pass
+ else: makedirs(globaldata+filterdir)
+ if path.isfile(lefilter):
+ if path.isdir(globaldata+filterdir+'.backup'):
+ pass
+ else: makedirs(globaldata+filterdir+'.backup')
+ move(lefilter, globaldata+filterdir+'.backup/'+time+filtername+'.csv')
+ print 'moved '+ lefilter +' pre-existing filter to backup location'
+ else: pass
+ filterfile = open(lefilter, 'w')
+ filterwriter = csv.writer(filterfile, delimiter=',')
+ #print prim_data
+ filterwriter.writerow(list(field_names)+['field names'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['metadata'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['identificator'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['reverse scoring'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['gender'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['age'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['sexuality'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['soi-r'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['soi-r behaviour facet'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['soi-r attitude facet'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['soi-r desire facet'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['ras'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['tipi'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['tipi extraversion'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['tipi agreeableness'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['tipi conscientiousness'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['tipi emotional stability'])
+ filterwriter.writerow(list([0]*(len(field_names)))+['tipi openness to experiences'])
+ filterfile.close()
View
@@ -0,0 +1,62 @@
+__author__ = 'Horea Christian' #if you contribute add your name to the end of this list
+from os import path
+import csv
+import numpy as np
+from filter_gen import mk_newfilter
+
+#input files
+newfilter='' # empty unless you want to create a new filter file, specify relative to globaldata + filterdir
+filename = 'cmp22_02_2013' # survey results from said date
+filtername = 'default' #filter file
+globaldata = '~/Data/shared/2att/cq/' #global data folder
+localdata = 'localdata/' #local data folder
+filterdir= 'filters/' #location of filter files
+outputname = 'res_'+filename
+
+globaldata = path.expanduser(globaldata)
+if path.isfile(localdata+filename):
+ outputpath = localdata + outputname + '.csv'
+ datapath = localdata + filename + '.csv'
+ filterpath = localdata + filterdir + filtername + '.csv'
+else:
+ outputpath = globaldata + outputname + '.csv'
+ datapath = globaldata+filename + '.csv'
+ filterpath = globaldata+filterdir+filtername + '.csv'
+
+def get_data():
+ datafile = open(datapath, 'r')
+ readdata = csv.reader(datafile, delimiter =',')
+ prim_data = []
+ for row in readdata:
+ prim_data.append(row)
+ datafile.close()
+ field_names = np.array(prim_data[0][:])
+ prim_data = np.array(prim_data[1:])
+
+ #write a filter
+ if not path.isfile(filterpath):
+ mk_newfilter(filtername, field_names, filterdir, globaldata)
+ elif newfilter == '':
+ pass
+ else:
+ mk_newfilter(newfilter, field_names, filterdir, globaldata)
+
+ filterfile = open(filterpath, 'r')
+ readfilter = csv.reader(filterfile, delimiter =',')
+ filters = []
+ for row in readfilter:
+ filters.append(row)
+ filterfile.close()
+ filters = np.array(filters)
+
+ return prim_data, filters, field_names
+
+def export_data(leresults):
+ outputfile = open(outputpath,'w')
+ outputwriter = csv.writer(outputfile, delimiter=',')
+# outputwriter.writerow(['identificator', 'gender', 'age', 'sexuality', 'soi-r', 'ras',
+# 'tipi', 'extraversion', 'agreeableness', 'conscientiousness',
+# 'emotional stability', 'openness to experiences'])
+ for i in leresults:
+ outputwriter.writerow(i)
+ outputfile.close()
View
@@ -0,0 +1,50 @@
+__author__ = 'Horea Christian' #if you contribute add your name to the end of this list
+import numpy as np
+from import_export import get_data
+soir_max = 9
+ras_max = 5
+tipi_max = 7
+divide = np.array([['ras', 7],['tipi', 2]])
+
+
+def lefilter(keep=None):
+ data, filters, _ = get_data()
+ mask = filters[:,-1] == keep
+ lefilter = filters[mask][:,:-1]
+ value = data[:,lefilter.astype(np.bool)[0]]
+ if keep != 'gender':
+ value = value.astype(np.integer)
+ return value
+
+def reverse_score():
+ data, filters, _ = get_data()
+ data_nn = data[0:,:]
+ lefilter = filters[filters[:,-1] == 'reverse scoring'][0]
+ soirfilter = filters[filters[:,-1] == 'soi-r'][0]
+ rasfilter = filters[filters[:,-1] == 'ras'][0]
+ tipifilter = filters[filters[:,-1] == 'tipi'][0]
+# mask = lefilter != '0'
+# newfilter = filters[:,mask]
+# newdata = data[:,mask[:-1]].astype(np.integer)
+ for ixr,row in enumerate(data_nn):
+ for ixe, el in enumerate(row):
+ if lefilter[ixe] == '1':
+ if soirfilter[ixe] == '1':
+ data_nn[ixr,ixe] = soir_max + 1 - el.astype(np.integer)
+ elif rasfilter[ixe] == '1':
+ data_nn[ixr,ixe] = ras_max + 1 - el.astype(np.integer)
+ elif tipifilter[ixe] == '1':
+ data_nn[ixr,ixe] = tipi_max + 1 - el.astype(np.integer)
+ return data_nn
+
+def calc_score(param=None):
+ data, filters, _ = get_data()
+ mask = filters[:,-1] == param
+ lefilter = filters[mask][:,:-1]
+ values = data[:,lefilter.astype(np.bool)[0]].astype(np.integer)
+ value = np.sum(values, axis=1)
+ if param.startswith('tipi'):
+ value = value / divide[divide[:,0] == 'tipi',:][:,-1].astype(np.integer)
+ elif param.startswith('ras'):
+ value = value / divide[divide[:,0] == 'ras',:][:,-1].astype(np.integer)
+ return value
View
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+__author__ = 'Horea Christian' #if you contribute add your name to the end of this list
+
+from list_op import lefilter, reverse_score, calc_score
+import numpy as np
+from import_export import export_data
+
+leresults=[]
+
+reverse_score()
+for i in ['identificator', 'gender', 'age', 'sexuality']:
+ values = lefilter(i)
+ leresults=leresults+[[i] + values.tolist()]
+for i in ['soi-r', 'soi-r behaviour facet',
+ 'soi-r attitude facet', 'soi-r desire facet', 'ras',
+ 'tipi extraversion', 'tipi agreeableness', 'tipi conscientiousness',
+ 'tipi emotional stability', 'tipi openness to experiences']:
+ values = calc_score(i)
+ leresults = leresults+[[i] + values.tolist()]
+leresults = np.array(leresults)
+export_data(leresults)

0 comments on commit 4dd5e39

Please sign in to comment.