-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVTC_Smoothing.py
executable file
·91 lines (73 loc) · 2.81 KB
/
VTC_Smoothing.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 13 15:17:14 2017
@author: Barry
"""
#Opening the file
print("Opening file for smoothing...\n")
import os
workDir = 'C:/...../research/VTC Tables'
os.chdir(workDir)
subjID = input('Enter participant ID: ')
if not isinstance(subjID, str):
subjID = str(subjID)
workFile = os.path.join(workDir, 'classified_study_codename_' + subjID + '_VTC_gender_task_data.csv')
#Will now attempt to read in the file
print("Reading in the normalized absolute deviance...\n")
with open(workFile, 'rb') as csvfile:
import pandas as pd
VTCtable = pd.read_csv(workFile)
absoluteVTC = VTCtable.Absolute_RT_Variance
#Begin smoothing with kernel
print("Calculating sigma to full-width-half-maximum and setting basic precision values...\n")
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(precision=4, suppress=True) # Make numpy print 4 significant digits for prettiness
np.random.seed(5) # To get predictable random numbers
def sigma2fwhm(sigma):
return sigma * np.sqrt(8 * np.log(2))
def fwhm2sigma(fwhm):
return fwhm / np.sqrt(8 * np.log(2))
sigma = 2
fwhm = sigma2fwhm(sigma)
x_vals = np.arange(0,len(absoluteVTC), 1)
y_vals = absoluteVTC
plt.bar(x_vals, y_vals)
#plt.bar(x_vals, y_vals)
#Smoothing each data point
print("Smoothing each data point with kernel now...\n")
smoothed_vals = np.zeros(y_vals.shape)
for i in range(len(absoluteVTC)):
x_position = i
kernel_at_pos = np.exp(-(x_vals - x_position) ** 2 / (2 *sigma ** 2))
kernel_at_pos = kernel_at_pos / sum(kernel_at_pos)
smoothed_vals[x_position] = sum(y_vals * kernel_at_pos)
plt.bar(x_vals, smoothed_vals)
VTCtable['Smoothed_VTC_S2'] = smoothed_vals
print(VTCtable)
#Bin based on the smoothed absolute RT Variance median value
print("Binning based on the median value of the smoothed absolute RT variance")
print("Binning based on the median value of the smoothed absolute RT variance")
adjusted_bin = ["" for x in range(len(smoothed_vals))]
med = np.median(smoothed_vals)
print("Median of smoothed RT Variance: " + str(med))
for i in range(len(smoothed_vals)):
if smoothed_vals[i] < med:
adjusted_bin[i] = 'in-the-zone'
elif smoothed_vals[i] > med:
adjusted_bin[i] = 'out-of-the-zone'
else:
adjusted_bin[i] = 'median'
VTCtable['Adj_Zone_Val'] = adjusted_bin
print(VTCtable)
#Saving the file
print("Saving the files now...\n")
import csv
fileName = 'classified_study_codename_'+ subjID + '_VTC_gender_task_data.csv'
'''
with open(fileName, 'w') as csvfile:
tabwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
[tabwriter.writerow(r) for r in VTCtable]
'''
#VTCtable.to_csv(fileName, sep=',', index=False)