-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser.py
431 lines (343 loc) · 11.5 KB
/
laser.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
# Time
from datetime import datetime
import time
# Array and maths
import numpy as np
import math
import random
import csv
import pandas as pd
import scipy as sci
from scipy import stats
import sklearn as skl
import scipy.stats as st
import statsmodels as sm
# import matplotlib
# matplotlib.use("TkAgg")
# import matplotlib.pyplot as plt
from numpy import (
isscalar,
r_,
log,
around,
unique,
asarray,
zeros,
arange,
sort,
amin,
amax,
any,
atleast_1d,
sqrt,
ceil,
floor,
array,
compress,
pi,
exp,
ravel,
count_nonzero,
sin,
cos,
arctan2,
hypot,
)
# System
import threading
from multiprocessing import pool
from multiprocessing.pool import ThreadPool
import sys
import warnings
import itertools
import keyboard
from tkinter import *
from tkinter import filedialog as tkfd
import zaber.serial as zs
import serial
# # MATLAB
# try:
# import matlab.engine as me
# except:
# print('No module Matlab')
## NIDAQMX
import nidaqmx.system.watchdog as nsw
import nidaqmx.stream_writers as nsw
import nidaqmx.stream_readers as nsr
import nidaqmx._task_modules.out_stream as nto
import nidaqmx._task_modules.timing as ntt
import nidaqmx.constants as NC
import nidaqmx.task as NT
# Image
from PIL import Image
from PIL import ImageTk
## Sound
try:
import winsound
except:
pass
# print('This is not a Windows device')
# import audio
## My scripts
try:
import globals
except:
pass
class Laser(object):
def __init__(self, lower=30, upper=60):
self.range_temp = np.arange(30, 60, 0.3)
self.temp = np.array([30, 60])
self.mVolts = np.array([0, 1])
# Interpolating
self.range_volt = np.interp(self.range_temp, self.temp, self.mVolts)
self.temp_volt = np.stack((self.range_temp, self.range_volt))
def ramp(self, start_temp, target_temp):
self.start_temp = start_temp
self.target_temp = target_temp
self.nearest_temp_start = find_nearest(self.temp_volt[0], start_temp)
self.nearest_temp_target = find_nearest(self.temp_volt[0], target_temp)
self.volt_ref_start = self.temp_volt[1, self.nearest_temp_start]
self.volt_ref_target = self.temp_volt[1, self.nearest_temp_target]
if start_temp > target_temp:
self.data = np.arange(
self.volt_ref_start, self.volt_ref_target, -self.volt_ref_target / 1000
)
elif target_temp > start_temp:
self.data = np.arange(
self.volt_ref_start, self.volt_ref_target, self.volt_ref_target / 1000
)
def constant(self, target_temp, length):
self.target_temp = target_temp
self.nearest_temp = find_nearest(self.temp_volt[0], target_temp)
self.volt_ref = self.temp_volt[1, self.nearest_temp]
self.data = np.repeat(self.volt_ref, length)
def oscillation(
self, lower_bound, upper_bound, freq, phase=0, repeats=1, rate=globals.rate_NI
):
self.nearest_temp_low = find_nearest(self.temp_volt[0], lower_bound)
self.nearest_temp_upper = find_nearest(self.temp_volt[0], upper_bound)
self.volt_ref_low = self.temp_volt[1, self.nearest_temp_low]
self.volt_ref_upper = self.temp_volt[1, self.nearest_temp_upper]
self.t = np.arange(0, repeats * 10, 0.01)
self.w = 2 * math.pi * freq
self.phi = phase # phase to change the phase of sine function
self.A = (self.volt_ref_upper - self.volt_ref_low) / 2
self.data = (
self.A * np.sin(self.w * self.t + self.phi)
+ (self.volt_ref_upper + self.volt_ref_low) / 2
)
self.duration = int(1000 * repeats / rate) # duration of sound
def run(self, rate=globals.rate_NI):
# print('we are here')
self.tosk = NT.Task()
self.tosk.ao_channels.add_ao_voltage_chan(
"/{}/{}".format(globals.dev, globals.nqO)
)
self.tosk.timing.cfg_samp_clk_timing(
rate=rate,
samps_per_chan=len(self.data),
sample_mode=NC.AcquisitionType.FINITE,
) # samps_per_chan = 1000
globals.status = "active"
# print('laser almost on')
time.sleep(1)
self.tosk.write(self.data)
self.tosk.start()
# print('laser on')
self.tosk.wait_until_done(timeout=50)
self.tosk.close()
# print('laser killed')
# globals.status = 'inactive'
def runTGIfam(self, rate=globals.rate_NI):
while True:
if globals.fam == "tgi":
self.tosk = NT.Task()
self.tosk.ao_channels.add_ao_voltage_chan(
"/{}/{}".format(globals.dev, globals.nqO)
)
self.tosk.timing.cfg_samp_clk_timing(
rate=rate,
samps_per_chan=len(self.data),
sample_mode=NC.AcquisitionType.FINITE,
) # samps_per_chan = 1000
self.tosk.write(self.data)
self.tosk.start()
self.tosk.wait_until_done(timeout=50)
self.tosk.close()
input("Press to close the shutter")
globals.status = "inactive"
input("Press to stop noise and laser the shutter")
winsound.PlaySound(None, winsound.SND_PURGE)
globals.trial == "off"
break
else:
continue
def threshold(self):
start = time.time()
while True: # making a loop
try:
if keyboard.is_pressed(" "):
# print('space pressed')
globals.status = "inactive"
end = time.time()
self.threshold = end - start
winsound.PlaySound(None, winsound.SND_PURGE)
globals.trials = "off"
globals.shutter = "close"
globals.thres = 1
print(self.threshold)
break # finishing the loop
except:
pass
# print('thres killed')
class screening(object):
def __init__(self):
pass
def instructions(self, message):
self.win = Tk()
self.win.attributes("-fullscreen", True)
label = Label(
self.win,
text="{}".format(message),
bg="black",
fg="white",
font="none 50 bold",
anchor=CENTER,
)
label.grid(column=0, row=0)
self.win.configure(background="black")
self.win.columnconfigure(0, weight=1)
self.win.rowconfigure(0, weight=1)
self.win.bind("<Return>", lambda e: self.win.destroy())
self.win.mainloop()
def instructionsTime(self, message, seconds):
self.win = Tk()
self.win.attributes("-fullscreen", True)
label = Label(
self.win,
text="{}".format(message),
bg="black",
fg="white",
font="none 50 bold",
anchor=CENTER,
)
label.grid(column=0, row=0)
self.win.configure(background="black")
self.win.columnconfigure(0, weight=1)
self.win.rowconfigure(0, weight=1)
self.win.after(
int(seconds * 1000), lambda: self.win.destroy()
) # Destroy the widget after 30 seconds
self.win.mainloop()
def handTGI(self, n_subject):
self.win = Tk()
### Getting image
self.img = Image.open("./hands/subj_{}.jpg".format(n_subject))
self.basewidth = 500
self.wpercent = self.basewidth / float(self.img.size[0])
self.hsize = int((float(self.img.size[1]) * float(self.wpercent)))
self.image = self.img.resize((self.basewidth, self.hsize), Image.ANTIALIAS)
# self.image.save('./hands/test.jpg')
### Configuration of the frame
self.win.geometry("{}x{}".format(self.basewidth, self.hsize))
# setting up a tkinter canvas
self.frame = Frame(self.win, bd=2, relief=SUNKEN)
self.frame.grid_rowconfigure(0, weight=1)
self.frame.grid_columnconfigure(0, weight=1)
self.canvas = Canvas(self.frame, bd=0)
self.canvas.grid(row=0, column=0, sticky=N + S + E + W)
self.frame.pack(fill=BOTH, expand=1)
# adding the image
self.image = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, image=self.image, anchor="nw")
self.canvas.config(scrollregion=self.canvas.bbox(ALL))
# function to be called when mouse is clicked
def getcoords(event):
# outputting x and y coords to console
self.xCOOR = event.x
self.yCOOR = event.y
self.win.destroy()
# mouseclick event
self.canvas.bind("<Button 1>", getcoords)
self.win.mainloop()
def PayAttention(self, message):
self.win = Tk()
time.sleep(0.0001)
# winsound.PlaySound('beep.wav', winsound.SND_ASYNC)
self.win.attributes("-fullscreen", True)
label = Label(
self.win,
text="{}".format(message),
bg="black",
fg="white",
font="none 50 bold",
anchor=CENTER,
)
label.grid(column=0, row=0)
self.win.configure(background="black")
self.win.columnconfigure(0, weight=1)
self.win.rowconfigure(0, weight=1)
self.win.bind("<space>", lambda event: self.win.destroy())
self.win.mainloop()
def Scores(self, score1, score2):
self.win = Tk()
message1 = "Your score was {} out of 10 and {} out of 10".format(score1, score2)
self.win.attributes("-fullscreen", True)
label = Label(
self.win,
text="{}".format(message1),
bg="black",
fg="white",
font="none 30 bold",
anchor=CENTER,
)
label2 = Label(
self.win,
text="{}".format("\n\n\n Click and press enter to continue"),
bg="black",
fg="white",
font="none 20 bold",
anchor=CENTER,
)
label.grid(column=0, row=0)
label2.grid(column=1, row=0)
self.win.configure(background="black")
self.win.columnconfigure(0)
self.win.rowconfigure(0, weight=1)
self.win.bind("<Return>", lambda e: self.win.destroy())
self.win.mainloop()
def FinalScore(self, score):
self.win = Tk()
message = " Your had {} % correct responses".format(
score
)
self.win.attributes("-fullscreen", True)
label = Label(
self.win,
text="{}".format(message),
bg="black",
fg="white",
font="none 30 bold",
anchor=CENTER,
)
label2 = Label(
self.win,
text="{}".format("\n\n\n Click and press enter to continue"),
bg="black",
fg="white",
font="none 20 bold",
anchor=CENTER,
)
label.grid(column=0, row=0)
label2.grid(column=1, row=0)
self.win.configure(background="black")
self.win.columnconfigure(0)
self.win.rowconfigure(0, weight=1)
self.win.bind("<Return>", lambda e: self.win.destroy())
self.win.mainloop()
def find_nearest(array, value):
idx = (np.abs(array - value)).argmin()
return idx
# def subjHandIn(self, library):
# pass