-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCPT_data.py
More file actions
executable file
·60 lines (49 loc) · 2.03 KB
/
Copy pathCPT_data.py
File metadata and controls
executable file
·60 lines (49 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from math import sqrt
from psychopy import logging
def standard_deviation(lst, population=True):
"""Calculates the standard deviation for a list of numbers."""
num_items = len(lst)
mean = sum(lst) / num_items
differences = [x - mean for x in lst]
sq_differences = [d ** 2 for d in differences]
ssd = sum(sq_differences)
# Note: it would be better to return a value and then print it outside
# the function, but this is just a quick way to print out the values along
# the way.
if population is True:
variance = ssd / num_items
else:
variance = ssd / (num_items - 1)
sd = sqrt(variance)
return sd
def create_data_file(
experiment_info,
task_name,
header='Participant number, Trial number, Task, Letter, Correct Response, Response, Correct, Accuracy,'
'Response Time, Trial time, Trial Type'):
file_name = experiment_info['Participant'] + '-' + experiment_info['date'] + '-' + task_name + '.csv'
folder_name = os.path.join(os.path.expanduser('~'), 'CPT_Data', experiment_info['Participant'], '')
if not os.path.exists(folder_name):
os.makedirs(folder_name)
file_path = '%s%s' % (folder_name, file_name)
with open(file_path, 'w') as f:
f.write(header)
f.write('\n')
return file_path
def create_log_file(experiment_info):
log_name = experiment_info['Participant'] + '-' + experiment_info['date'] + '.log'
folder_name = os.path.join(os.path.expanduser('~'), 'CPT_Data', experiment_info['Participant'], '')
if not os.path.exists(folder_name):
os.makedirs(folder_name)
log_path = '%s%s' % (folder_name, log_name)
log_file = logging.LogFile(log_path, level=logging.EXP)
return log_file
def write_data(filename, data):
with open(filename, 'a') as f:
row_string = ",".join(map(str, data))
f.write(row_string + '\n')
def append_data(filename, data):
with open(filename, 'a') as f:
row_string = ",".join(map(str, data))
f.write(row_string + ',')