-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBehaviorRig.py
More file actions
1133 lines (961 loc) · 52.9 KB
/
BehaviorRig.py
File metadata and controls
1133 lines (961 loc) · 52.9 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#Shebang to tell computer to use python to interpret program
#Initialize GPIO and pin numbering scheme
#Based on: https://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
import RPi.GPIO as GPIO #Catch GPIO pin interrupts
import time #track system time
from multiprocessing import Process, Pipe, Value #Multiprocessing set
from multiprocessing.connection import wait #Extract data from pipes when available
import pygame #Show images and log keypress events
from pygame.locals import * #Import pyGame constants locally
import re #regex
import fileinput #Allows files to be editted in place and to generate backups
from pathlib import Path #Allows for checking if a file exists without exceptions
import subprocess #Allows OS programs to be run as separate threads
import os #Allows access to os, including errors, and terminal commands
import sys #Allows execution of binary within Python - such as Pip, as well as retrieving terminal arguments
import imp #Allows for modules to be imported into program
import signal #Allows programs/processes to be terminated
from datetime import datetime #Allows recording of current date and time
import hashlib #Allows for calculating hashes of files for data verification
import random #Select from list randomly
#Setup variables
cageNumber = 1
toggleDebug = False #Whether to turn on debug funcitons - windowed image, GPIO output, block unmount, and block protocol overwrite
devnull = open(os.devnull) #Send subprocess outputs to dummy temp file
reboot = False #Global flag for whether the device needs to be rebooted to have changes take effect
temp_file = "/home/pi/temp_config.txt" #Path for storing temp data to be retrieved after reboot
pyudev = None #Object for importing the pyudev library
PIPE_PATH = "/home/pi/my_pipe.txt" #Create temporary pipe file for exporting text to lxterminal - must be in user directory
terminal = None #Instance of the lxterminal process
mountDir = "/mnt/usb/" #The directory the USB drive will be mounted to
#Experiment variables
imageDir = "/home/pi/exp_Images/" #Directory to transfer images to on the SD card
protocolFile = "Protocol.txt" #Name of the protocol file to be used - must have .txt extension
resultsFile = None #Name of active results file
resultFileBase = "Results.txt" #Base file name for the results file - must have .txt extension
imageExt = ".png" #File extension for valid protocol images
contrastProtocol = None #Whether or not the current protocl is a contrast series
#GPIO variables
#Arduino pins for testing
#~ pinWheel = 35 #TTL input from mouse wheel
#~ pinDoor = 37 #TTL input from mouse door to reward
#~ pinPump = 36 #TTL output to pump trigger
#Rig pins
pinWheel = 11 #TTL input from mouse wheel
pinDoor = 13 #TTL input from mouse door to reward
pinPump = 22 #TTL output to pump trigger
pinStrip = 29 #TTL output to power strip
doorOpen = False #Pin state when door is open
wheelBounce = 1 #Bounce time between events in which to ignore subsequent events (ms)
doorBounce = 1 #Bounce time between events in which to ignore subsequent events (ms)
syncDelay = 0.001 #Sleep delay between GPIO queries to reduce CPU load (s)
#Protocol parameter master dictionary (see retrieveExperiment(driveLabel) for initialization with parsing functions)
parameterDict = {"USB drive ID:": None, "Control image set:": None, "Reward image set:": None,
"Minimum wheel revolutions for reward:": None, "Maximum wheel revolutions for reward:": None,
"Duration of pump \"on\" state (seconds):": None, "Maximum time between wheel events (seconds):": None,
"Total duration of the experiment (hours):": None, "Duration of each reward frame (seconds):": None,
"Maximum duration of reward state (seconds):": None}
contrastDict = {"Minimum time between contrast increments:": None, "Maximum time between contrast increments:": None,
"Minimum contrast ratio (0-100):": None, "Maximum contrast ratio (0-100):": None,
"Number of contrast steps:": None}
def hasher(file):
HASH = hashlib.md5() #MD5 is used as it is faster, and this is not a cryptographic task
with open(file, "rb") as f:
while True:
block = f.read(65536) #64 kB buffer for hashing
if not block: #If block is empty (b'' = False), exit the while loop
break
HASH.update(block)
return HASH.hexdigest()
def lxprint(a): #Status display
global PIPE_PATH
with open(PIPE_PATH, "a") as p:
p.write(a + "\r\n")
#Protocol parsing functions
def matchString(key, line, refString):
#Search for refernce string in line from protocol file
if refString in line:
lxprint(key + " reference \"" + refString + "\" matches protocol \"" + line + "\"...")
return line
else:
lxprint("ERROR in " + key + " reference \"" + refString + "\" does not match protocol \"" + line + "\"...")
return None
def parseList(key, line, refArray):
refArray = []
#Search for a list in the line string
listArray = [None] #Initialize the protocol list array
line = "".join(line.split()) #This removes all white space - split cuts on \r\n\t and " ", then join puts the remaining bits back into a string
listMatch = re.search("\[.*\]", line)
#If there is a valid list string, parse the string
if listMatch:
listString = listMatch.group(0)
listString = listString[1:-1] #Remove first and last character - "[ ]"
listArray = listString.split(",")
if not (len(listArray) == 1 and listArray[0] == ""): #Parse images if array is not empty
for i in listArray:
if not i.endswith(imageExt):
lxprint("ERROR: Invalid extension in " + key[:-1] + ", \"" + i + "\" is not \"" + imageExt + "\"...")
return None
#If loop completes, then image list is valid
refArray = listArray
elif key == "Reward image set:":
refArray = []
lxprint("ERROR: No reward images found, there must be at least one reward image specified...")
return None
else:
refArray = []
lxprint(key[:-1] + " parsed: " + str(refArray))
return refArray
def parseNum(key, line, refNum):
#Float search string from: https://stackoverflow.com/questions/4703390/how-to-extract-a-floating-number-from-a-string
numeric_const_pattern = '[-+]? (?: (?: \d* \. \d+ ) | (?: \d+ \.? ) )(?: [Ee] [+-]? \d+ ) ?'
rx = re.compile(numeric_const_pattern, re.VERBOSE)
listMatch = rx.search(line)
if listMatch:
number = float(listMatch.group(0))
if(number >= 0):
lxprint(key[:-1] + " parsed: " + str(number))
return number
else:
lxprint("ERROR: " + key + " \"" + str(parameterDict[key]) + "\" is less than 0 seconds.")
return None
else:
lxprint("ERROR: \"" + key[:-1] + "\" cannot be parsed...")
return None
#----------------------------Raspberry Pi Config--------------------------------------------------------------------------------------------------------------------
def import_package(p):
global devnull
try:
#Needs directory hint - 1) dir when pip is run in sudo, 2) dir when pip is run without sudo, 3) dir when package loaded with apt-get
#https://www.raspberrypi.org/forums/viewtopic.php?t=213591
file, pathname, description = imp.find_module(p, ["/usr/local/lib/python2.7/dist-packages", "/home/pi/.local/lib/python2.7/site-packages", "/usr/lib/python2.7/dist-packages", "/usr/local/lib/python3.5/dist-packages", "/home/pi/.local/lib/python3.5/site-packages", "/usr/lib/python3.5/dist-packages"])
module_obj = imp.load_module(p, file, pathname, description)
lxprint(p + " is already installed...")
return module_obj
#If pachage is not found, use PIP3 to download package
except ImportError:
lxprint("Downloading " + p + "...")
#Make sure pip is installed before trying to install package
checkPip()
#Use .wait() rather than .communicate() as .wait() returns returnCode, while .communicate() returns tuple with stdout and stderr
retcode = subprocess.Popen(["(sudo pip3 install " + p + ")"], shell=True, stdout=devnull, stderr=devnull).wait()
if retcode == 0:
lxprint("Installing " + p + "...")
file, pathname, description = imp.find_module(p, ["/usr/local/lib/python2.7/dist-packages", "/home/pi/.local/lib/python2.7/site-packages", "/usr/lib/python2.7/dist-packages", "/usr/local/lib/python3.5/dist-packages", "/home/pi/.local/lib/python3.5/site-packages", "/usr/lib/python3.5/dist-packages"])
module_obj = imp.load_module(p, file, pathname, description)
lxprint(p + " is installed...")
return module_obj
else:
lxprint("Could not install \"" + p + "\", aborting program. Check internet connection?")
quit()
def checkPip():
global reboot
global devnull
try:
lxprint("Checking if pip3 is installed...")
#Any outputs from program are redirected to /dev/null
#Use Popen.communicate() instead of call so that output does not spam terminal - .communicate() will wait for process to terminate
subprocess.Popen(["pip3"], stdout=devnull, stderr=devnull).wait()
lxprint("Pip is already installed...")
return
except OSError as e:
if e.errno == os.errno.ENOENT:
lxprint("Installing pip, this may take a few minutes...")
#Use .wait() rather than .communicate() as .wait() returns returnCode, while .communicate() returns tuple with stdout and stderr
retcode = subprocess.Popen(["(sudo apt-get install -y python-pip)"], shell=True, stdout=devnull, stderr=devnull).wait()
if retcode != 100:
lxprint("Cannot install pip, aborting program. Check internet connection?")
quit()
else:
reboot = True #Flag device for reboot
lxprint("Pip is installed...")
def configXscreen():
global reboot
global devnull
#Check whether xScreensaver exists by trying to run it silently on a separate thread
try:
lxprint("Checking if xScreensaver is installed...")
#Any outputs from program are redirected to /dev/null
#Use Popen.communicate() instead of call so that output does not spam terminal - .communicate() will wait for process to terminate
retcode = subprocess.Popen(["(xscreensaver-command)"], shell=True, stdout=devnull, stderr=devnull).wait()
#If process was not successfull, install xscreensaver
if retcode != 1:
lxprint("Installing xScreensaver, this may take a few minutes...")
#Use .wait() rather than .communicate() as .wait() returns returnCode, while .communicate() returns tuple with stdout and stderr
retcode = subprocess.Popen(["(sudo apt-get install -y xscreensaver)"], shell=True, stdout=devnull, stderr=devnull).wait()
if retcode != 100:
lxprint("Error in installing xscreensaver. Check internet connection?")
quit()
else:
reboot = True #Flag the device for reboot
except:
lxprint("Error in installing xscreensaver. Check internet connection?")
quit()
#Verify that there is an xScreensaver config file
lxprint("Editing xScreensaver config...")
f = Path("/home/pi/.xscreensaver")
#If config file doesn't exist, toggle demo to generate a config file
if not f.is_file():
#If configuration file is missing, return false
#Make sure program is in desktop environemnt before running xscreensaver
desktop = os.environ.get('DESKTOP_SESSION')
if desktop is None:
lxprint("LXDE desktop is needed to format xScreensaver. Please run this program in the desktop enxironment.")
quit()
else:
#The following code ensures that when the main process is closed, all subprocesses spawned by main process are also closed
#It does this by attaching a session ID to the parent process, which will make it the groupd leader of all spawned processes
#This way, when the group leader is told to terminate, all spawned processes will terminate too
#https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true
xConfig = False
while not xConfig:
try:
time.sleep(1)
p = subprocess.Popen("xscreensaver-command -demo", stdout=devnull, shell = True, preexec_fn=os.setsid)
time.sleep(1)
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
xConfig = True
except:
xConfig = False
#Check xScreenSaver config file to ensure that xScreenSaver is off, overwrite if needed
#With wrapper ensures that file input closes on completion even with exceptions
#Inplace means data is moved to backup, and outputs (such as print) are directed to input file
#Backup allows a backup to be generated with the specified extension
try:
with fileinput.input(files=("/home/pi/.xscreensaver"), inplace=True, backup=".bak") as f:
for line in f:
line = line.rstrip() #removes trailing white spaces (print() will add newline back on)
#r"..." means raw string, where \ are used as escape characters
line = re.sub(r"^mode:.*", r"mode:\t\toff", line)
print(line)
lxprint("xScreensaver is configured...")
return True
except:
lxprint("Error: .xscreensaver config file could not be found.")
quit()
def configBoot():
global reboot
global devnull
lxprint("Checking if monitor is configured correctly...")
#use raspi-config to change device sonfiguration
#Any outputs from program are redirected to /dev/null
#Check if Raspberry Pi is already set to boot into terminal with autologin
out1, dummy=subprocess.Popen(["(sudo raspi-config nonint get_boot_cli)"], shell=True, stdout=subprocess.PIPE, stderr=devnull).communicate()
out1 = int(out1)
out2, dummy=subprocess.Popen(["(sudo raspi-config nonint get_autologin)"], shell=True, stdout=subprocess.PIPE, stderr=devnull).communicate()
out2 = int(out2)
out3, dummy=subprocess.Popen(["(sudo raspi-config nonint get_overscan)"], shell=True, stdout=subprocess.PIPE, stderr=devnull).communicate()
out3 = int(out3)
#Setup Raspberry pi so that it will boot into terminal with auto login
if 0 in {out1, out3} or out2 == 1:
try:
lxprint("Editing raspi-config...")
#Set pi to boot to terminal with auto-login
subprocess.Popen(["(sudo raspi-config nonint do_boot_behaviour B4)"], shell=True, stdout=devnull, stderr=devnull).communicate()
#Set pi to turn off overscan
subprocess.Popen(["(sudo raspi-config nonint do_overscan 1)"], shell=True, stdout=devnull, stderr=devnull).communicate()
#Set pi to reboot to apply new settings
reboot = True #Flag the device for reboot so changes can be applied
except:
lxprint("Error: can't edit raspi-config.")
quit()
else:
lxprint("Monitor is already configured correctly...")
#Check if program is configured for autoload on restart
lxprint("Checking if this program is set to autoload on reboot...")
with open("/etc/xdg/lxsession/LXDE-pi/autostart", "r") as f:
for line in f:
line = line.rstrip() #removes trailing white spaces (print() will add newline back on)
if(("@/usr/bin/python3 " + str(*sys.argv)) in line):
lxprint("This program is set to autoload on reboot...")
break
else:
lxprint("This program is not yet set to autoload on reboot...")
reboot = True
#Command from - https://www.raspberrypi.org/forums/viewtopic.php?t=91677#p641130
def disableAutomount():
global devnull
global reboot
lxprint("Disabling USB drive automount...")
f = Path("/home/pi/.config/pcmanfm/LXDE-pi/pcmanfm.conf")
#If config file doesn't exist, toggle demo to generate a config file
if f.is_file():
try:
with fileinput.input(files=("/home/pi/.config/pcmanfm/LXDE-pi/pcmanfm.conf"), inplace=True, backup=".bak") as f:
for line in f:
line = line.rstrip() #removes trailing white spaces (print() will add newline back on)
#r"..." means raw string, where \ are used as escape characters
line = re.sub(r"^mount_on_startup=.*", r"mount_on_startup=0", line)
line = re.sub(r"^mount_removable=.*", r"mount_removable=0", line)
print(line)
lxprint("USB automount is disabled..")
except:
lxprint("Error: pcmanfm.conf could not be edited.")
quit()
else:
lxprint("ERROR: pcman.conf not found, how is Raspbian running?")
quit()
#Install "eject" if it isn;t already installed to allow for ejecting USB drives from the command line
lxprint("Checking if \"eject\" is installed...")
retcode = subprocess.Popen(["(eject)"], shell=True, stdout=devnull, stderr=devnull).wait()
#Install eject if it is not currently installed
if retcode != 1:
reboot = True
lxprint("Installing \"eject\"...")
try:
dummy = subprocess.Popen(["(sudo apt-get install -y eject)"], shell=True, stdout=devnull, stderr=devnull).wait()
except:
lxprint("ERROR: could not install \"eject\", please check internet connection.")
quit()
#Confirm that eject works
retcode = subprocess.Popen(["(eject)"], shell=True, stdout=devnull, stderr=devnull).wait()
if retcode == 1:
lxprint("Eject was successfully installed...")
else:
lxprint("ERROR: eject failed to install, please install manually.")
else:
lxprint("Eject is already installed...")
#Auto restart sequence from: https://www.raspberrypi.org/forums/viewtopic.php?t=43509#p714119
def autorestart():
global temp_file
global devnull
#Make sure the Python file is executable
lxprint("Setting program to exectuable...")
subprocess.call(["sudo", "chmod", "+x", *sys.argv])
#Elevate to sudo to edit config files
if os.geteuid() != 0:
#Create file to flag program that a reboot has occurred, as well as reference file path for sudo to reference
with open(temp_file, "w+") as conf:
conf.write(str(*sys.argv) + "\r\n")
#Check if program is configured for autoload on restart
edit = True
with open("/etc/xdg/lxsession/LXDE-pi/autostart", "r") as f:
edit = True
for line in f:
line = line.rstrip() #removes trailing white spaces (print() will add newline back on)
if(("@/usr/bin/python3 " + str(*sys.argv)) in line):
edit = False
#If autostart is not set, elevate to su and add line to autostart before rebooting
if edit:
#If program is no configured to autoreload, elevete to su and add line to file
lxprint("Elevating to su...")
#Elevate to su
subprocess.call(["sudo", "python3", *sys.argv]) #sys.argv returns the last argument set in the console, which would be this file
#If autostart is set, ready to reboot
lxprint("Rebooting system to apply changes...")
time.sleep(3)
subprocess.call("reboot")
#if su, edit autostart file as needed
else:
try:
lxprint("Configuring LXDE autoload to boot this file on restart...")
#Load the configuration data into an array
with open(temp_file, "r") as conf:
confArray = conf.readlines()
#Modify the autostart LXDE file to run this code on boot
edit = True
with fileinput.input(files=("/etc/xdg/lxsession/LXDE-pi/autostart"), inplace=True, backup=".bak") as f:
for line in f:
line = line.rstrip() #removes trailing white spaces (print() will add newline back on)
print(line)
if(("@/usr/bin/python3 " + confArray[0]) in line):
edit = False
#If the program isn't already set to autostart, configure file
if edit:
with open("/etc/xdg/lxsession/LXDE-pi/autostart", "a") as f:
f.write("@/usr/bin/python3 " + confArray[0].rstrip())
except:
lxprint("Error: autostart config file could not be found.")
quit()
#Check setup and run experimient if logged in as pi
def checkPiConfig():
global reboot
global temp_file
global pyudev
reboot = False
if os.geteuid() != 0:
#Remove temp config file if it exists
f = Path(temp_file)
if f.is_file():
subprocess.call("sudo rm " + temp_file, shell=True)
configBoot()
if not configXscreen():
lxprint("Please re-install xScreensaver by opening terminal and typing:")
lxprint("sudo apt-get install xscreensaver")
else:
lxprint("Screen configuration successful...")
pyudev = import_package("pyudev")
disableAutomount()
if reboot == True:
autorestart()
elif os.geteuid() == 0:
autorestart()
else:
lxprint("Invalid user ID: " + str(os.geteuid))
#---------------------------------USB Drive-----------------------------------------------------------------------------------------------------------------------------------------------
def checkForUSB():
global mountDir
lxprint("Please insert USB drive:")
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
connected = False
labelName = None
while True:
time.sleep(0.2)
#Search for whether a USB drive has been connected to the Pi
for device in iter(monitor.poll, None):
#If a USB drive is connected, then check for location of drive
if device.action == 'add' and not connected:
test = 0;
timeStep = 0.1 #Time between checks for connected device
waitTime = 5 #Time out if device is not found
deviceFound = False #Flag for if a USB device was found
deviceValid = False #Flag for if USB device is valid (correct label and format)
deviceFormat = False #Flag for if device has correct partition format (contains a partition #1)
while test < waitTime:
test += timeStep
time.sleep(timeStep)
out, err = subprocess.Popen("lsblk -l -o name,label", shell=True, stdout=subprocess.PIPE).communicate() #Name returns partition directory, label returns partition name
outString = out.decode()
if re.search(r"sd[a-z]{1,3}", outString) and not deviceFound:
lxprint("USB device found...")
connected = True
deviceFound = True
if deviceFound and re.search(r"CAGE " + str(cageNumber) + "[A-B]", outString):
lxprint("Valid USB device...")
labelName = re.search(r"CAGE " + str(cageNumber) + "[A-B]", outString).group(0)
deviceValid = True
break
else:
continue
if test >= waitTime:
if deviceFound:
labelName = re.search(r"CAGE [1-9][A-B]", outString)
if labelName:
lxprint("ERROR: This drive is for " + labelName.group(0)[:-1] + ", please disconnect.")
else:
lxprint("ERROR: USB drive is not a protocol drive, please disconnect.")
else:
lxprint("ERROR: USB device was not found. Please reconnect.")
break
#Retrieve the drive location
dirSearch = re.search("sd[a-z]{1,3}1", outString)
if not dirSearch:
dirSearch = re.search("sd[a-z]{1,3}", outString)
if dirSearch:
if deviceValid:
USBdir = "/dev/" + dirSearch.group(0)
#Mount the USB drive
subprocess.call("sudo mkdir -p " + mountDir, shell=True) #Create directory to which to mount the USB drive if it doesn't exist
subprocess.call("sudo mount -o uid=pi,gid=pi " + USBdir + " " + mountDir, shell=True) #Mount USB to directory in user "pi"
if(retrieveExperiment(labelName)):
lxprint("SUCCESS!")
lxprint("Starting experiment...")
runExperiment()
else:
lxprint("FAILURE!")
##########################################DEBUG - block auto unmount
if toggleDebug:
input("Press enter...")
subprocess.call("sudo eject " + USBdir, shell=True) #Install eject using command sudo apt-get install eject
lxprint("USB drive is unmounted. It is safe to remove the drive...")
else:
lxprint("USB drive is unmounted. It is safe to remove the drive...")
else:
lxprint("ERROR: USB device is no longer found. Please reconnect.")
#if a USB drive is disconnected, print result
if device.action == 'remove':
lxprint("USB drive has been removed...")
return
def retrieveExperiment(driveLabel):
global mountDir
global imageDir
global protocolFile
global resultsFile
global resultsFileBase
global imageExt
global parameterDict
global contrastDict
global contrastProtocol
#Re-initialize the parameter dictionary with the appropriate parsing functions
parameterDict = {"USB drive ID:": matchString, "Control image set:": parseList, "Reward image set:": parseList,
"Minimum wheel revolutions for reward:": parseNum, "Maximum wheel revolutions for reward:": parseNum,
"Duration of pump \"on\" state (seconds):": parseNum, "Maximum time between wheel events (seconds):": parseNum,
"Total duration of the experiment (hours):": parseNum, "Duration of each reward frame (seconds):": parseNum,
"Maximum duration of reward state (seconds):": parseNum}
contrastDict = {"Minimum time between contrast increments:": None, "Maximum time between contrast increments:": None,
"Minimum contrast ratio (0-100):": None, "Maximum contrast ratio (0-100):": None,
"Number of contrast steps:": None}
f = Path(mountDir + protocolFile)
#Extract experiment protocol and make sure it is valid
lines = None #Export of protocol to RAM so it can be added as a header to the results file
if f.is_file():
#Append date to protocol file to flag it as being used and prevent accidental reuse
protocolHash = hasher(mountDir + protocolFile)
newProtocolFile = re.sub(".txt", " - " + str(datetime.now())[:10] + " " + protocolHash + ".txt", protocolFile)
#######################################################DEBUG - toggle protocol rename
if toggleDebug:
newProtocolFile = protocolFile
else:
os.rename(mountDir + protocolFile, mountDir + newProtocolFile)
#Parse the protocol file
ref = None #Ref variable to be passed to parsing functions if needed
with open(mountDir + newProtocolFile, "r") as exp:
lines = exp.readlines()
exp.seek(0) #Return back to the start of the file
for line in exp:
for key, func in parameterDict.items():
if(line.startswith(key)):
if "USB drive ID:" in key:
ref = driveLabel
else:
ref = None
parameterDict[key] = func(key, line.split(key, 1)[1].strip(), ref) #Send key and data afer ":" with leading and trailing whitespace removed
for key, func in contrastDict.items():
if(line.startswith(key)):
contrastDict[key] = parseNum(key, line.split(key, 1)[1].strip(), None) #Send key and data afer ":" with leading and trailing whitespace removed
#Check dict and flag missing components
nValid = 0
for key, value in parameterDict.items():
if callable(value): #If value is still function = line not found
lxprint("Cannot find: \"" + key + "\" in protocol file...")
elif value is None:
pass
else:
nValid += 1
lxprint(str(nValid) + " of " + str(len(parameterDict)) + " parameter lines parsed...")
nContrast = 0
contrastProtocol = False
for key, value in contrastDict.items(): #See if there is a contrast portion in the protocol
if value: #If value is not None, then contrast protocol is active
contrastProtocol = True
if contrastProtocol: #If there is a contrast portion, make sure it is complete
for key, value in contrastDict.items():
if value: #If value is not None, then contrast protocol is active
nContrast += 1
else:
lxprint("Cannot find: \"" + key + "\" in contrast section of protocol file...")
lxprint(str(nContrast) + " of " + str(len(contrastDict)) + " contrast lines parsed...")
else:
lxprint("Contrast protocol not found...")
#If all components parsed successfully, check that all images are available
if ((nValid == len(parameterDict)) and ((nContrast == len(contrastDict)) == contrastProtocol)): #If all elements passed parsing
#Verify that all images in the protocol are included in the file directory
if(contrastProtocol == (contrastDict["Number of contrast steps:"] == len(parameterDict["Reward image set:"]))):
imageSet = list(set(parameterDict["Control image set:"] + parameterDict["Reward image set:"])) #Create a list of all unique images in the protocol using "set"
lxprint("Transferring images to SD card...")
subprocess.call("sudo mkdir -p " + imageDir, shell=True) #Create directory to which to transfer images if it doesn't exist
for i in imageSet:
f = Path(mountDir + "images/" + i)
if f.is_file():
subprocess.call("sudo cp " + mountDir + "images/" + i + " " + imageDir, shell=True) #Move valid images to the SD card
else:
lxprint("ERROR: File \"" + i + "\" could not be found in \"" + mountDir + "images/\"")
return False
#Export the protocol to the results file
if lines is not None:
resultsFile = re.sub(".txt", " - " + str(datetime.now())[:10] + " " + protocolHash + ".txt", resultFileBase)
lines.insert(0, "Date: " + str(datetime.now()) + "\r\n")
with open(mountDir + resultsFile, "w+") as f:
for a in lines:
f.write(a)
#Add file hashes
f.write("Protocol hash: " + protocolHash + "\r\n")
f.write("Image hashes: \r\n")
for i in sorted(set(parameterDict["Control image set:"] + parameterDict["Reward image set:"])):
f.write(i + " - " + hasher(imageDir + i) + "\r\n")
f.write("\r\n-------------------------------Start of experiment-----------------------------------------------\r\n\r\n")
return True
else:
lxprint("ERROR: " + str(contrastDict["Number of contrast steps:"]) + " contrast steps not equal to " + str(len(parameterDict["Reward image set:"])) + " reward images.")
else:
lxprint("ERROR: \"" + protocolFile + "\" not found on USB drive.")
return False
#---------------------------------Run experiment-----------------------------------------------------------------------------------------------------------------------------------------------
def imageProcess(connLog, stopQueue, doorPipe, wheelPipe, expStart):
global imageDir
global syncDelay
global parameterDict
global contrastDict
global contrastProtocol
minContrastTime = contrastDict["Minimum time between contrast increments:"]
maxContrastTime = contrastDict["Maximum time between contrast increments:"]
nContrastSteps = contrastDict["Number of contrast steps:"]
if contrastProtocol:
rewardFramePeriod = random.uniform(minContrastTime, maxContrastTime)
else:
rewardFramePeriod = parameterDict["Duration of each reward frame (seconds):"]
def sendLog(image):
global imageDir
nonlocal rewardFramePeriod
#Send image data to log
HASH = str(hasher(imageDir + image)) #Get image hash - computing hash takes 2 ms
timer = time.time() - expStart #Get experiment time
connLog.send("Image - Name: " + image + ", Hash: " + HASH + ", Duration: " + "{:.3f}".format(rewardFramePeriod) + ", Time: " + "{:.3f}".format(time.time() - expStart)) #"{:.3f}".format() returns three places after the decimal point
return
def displayImage(i):
nonlocal pictureDict
windowSurfaceObj.blit(pictureDict[i],(0,0))
pygame.display.update()
sendLog(i)
return
def preloadImages(array):
global imageDir
subset = set(array) #Create list of all unique entries
pictures = {}
for i in subset:
pictures[i] = pygame.image.load(imageDir + i)
return pictures
def changeToControl():
nonlocal wheelPipe
nonlocal pictureDict
global parameterDict
nonlocal rewardIndex
nonlocal wheelWait
wheelPipe.send(1) #Tell wheel process that reward state ended
if parameterDict["Control image set:"]: #If the control array is not empty, switch to a randomly selected control image
displayImage(random.choice(parameterDict["Control image set:"]))
else:
displayImage(random.choice(parameterDict["Reward image set:"])) #If no control images are available, show a reward image
rewardIndex = 0 #Reset the reward frame index
wheelWait = False #Reset wheel wait flag
return False
def clearPipe(p):
var = None
while p.poll():
var = p.recv()
return var
connLog.send("Image starting at: " + "{:.3f}".format(time.time()-expStart))
#Exit program on any key press
run = True
#Get the current reslution of the monitor
displayObj = pygame.display.Info()
###############################################################DEBUG - toggle fullscreen and mouse cursor
if toggleDebug:
windowSurfaceObj = pygame.display.set_mode((displayObj.current_w, displayObj.current_h))
else:
windowSurfaceObj = pygame.display.set_mode((displayObj.current_w, displayObj.current_h), pygame.FULLSCREEN)
#Hide mouse cursor
pygame.mouse.set_visible(False)
#Preload images to RAM
pictureDict = preloadImages((parameterDict["Control image set:"] + parameterDict["Reward image set:"]))
#initialize varible for tracking image list index
imageIndex = 0
#Record experiemnt end time
expEnd = time.time()
#Initialize state variabes
wheelState = 0
rewardState = False
#Calculate experiment end time:
currentTime = time.time()
expEnd = 60*60*parameterDict["Total duration of the experiment (hours):"] + currentTime
frameEnd = currentTime #Track when a reward frame times out
rewardIndex = 0 #Index of current reward frame
wheelWait = False #Specific for contrast protocol - state flag for when next reward image is waiting to be triggered by wheel event
changeToControl() #Initialize to a control image
while run and expEnd > currentTime:
time.sleep(syncDelay)
#Record time for current cycle
currentTime = time.time()
#Poll for state changes
if rewardState: #If in reward state monitor door for trigger to control state
if doorPipe.poll(): #Check if door state has changed
dummy = clearPipe(doorPipe)
rewardState = changeToControl()
if(contrastProtocol):
time.sleep(syncDelay)
dummy = clearPipe(wheelPipe) #Clear all remaining wheel flags
else:
#If in contrast mode, wait until current frame times out, then index to next contrast frame
if contrastProtocol:
if (frameEnd < currentTime and rewardIndex < len(parameterDict["Reward image set:"])): #If frame has timed out, check for next wheel event
if wheelWait:
if wheelPipe.poll():
wheelWait = clearPipe(wheelPipe)
else:
if rewardIndex == 0: #If this is the first reward image, randomly shuffle the reward image sequence
random.shuffle(parameterDict["Reward image set:"])
displayImage(parameterDict["Reward image set:"][rewardIndex%len(parameterDict["Reward image set:"])]) #Show next image in reward sequence
rewardIndex += 1 #Increment reward index
rewardFramePeriod = random.uniform(minContrastTime, maxContrastTime)
frameEnd = currentTime + rewardFramePeriod #Reset frame timer
doorPipe.send(1) #Reset the reward timeout timer
wheelWait = True #Reset flag to wait for wheel event to index next frame
else: #If not yet waiting for a wheel event, keep the wheelPipe buffer clear
dummy = clearPipe(wheelPipe)
else:
if frameEnd <= currentTime: #If frame has expired move to next reward frame
displayImage(parameterDict["Reward image set:"][rewardIndex%len(parameterDict["Reward image set:"])]) #Show next image in reward sequence
rewardIndex += 1 #Increment reward index
frameEnd = currentTime + rewardFramePeriod #Reset frame timer
else: #If in control state, monitor wheel
frameEnd = currentTime
if wheelPipe.poll(): #Check if wheel state has changed
wheelState = wheelPipe.recv()
doorPipe.send(1) #Tell door process that wheel has triggered a reward event
rewardState = True
else:
wheelState = False
#Check for keypress event if available
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
lxprint("Key press")
run = False
#If keypress, exit run loop
if not run:
break
#Flag other processes to stop
stopQueue.value = 1
lxprint("Image stop at: " + str(datetime.now()))
def logProcess(connGPIO, connWheel, connImage, stopQueue):
global mountDir
global resultsFile
global syncDelay
connArray = []
connArray.append(connGPIO)
connArray.append(connWheel)
connArray.append(connImage)
##########################Debug tail - shows results file in real time
if toggleDebug:
terminal = subprocess.Popen(["lxterminal -e tail --follow \"" + (mountDir + resultsFile) + "\""], shell=True, stdout=devnull, stderr=devnull)
run = True
while run:
time.sleep(syncDelay)
#If a data entry is available in the queue, process it
#multiprocessing.connection.wait
for r in wait(connArray, timeout=0.1):
with open(mountDir + resultsFile, "a") as f: #Use with in loop so that data is continuously saved to drive preventing data loss in the event of a device failure
try:
data = r.recv()
except EOFError:
lxprint("Error reading from pipe: " + str(r))
f.write("Error reading from pipe: " + str(r) + "\r\n")
else:
f.write(str(data) + "\r\n")
#Stop process on stop command from GUI process
if stopQueue.value == 1:
run = False
#Perform last check of pipes to make sure all data has been gathered
for r in wait(connArray, timeout=1):
with open(mountDir + resultsFile, "a") as f:
try:
data = r.recv()
except EOFError:
print ("Error reading from pipe: " + str(r))
f.write("Error reading from pipe: " + str(r) + "\r\n")
else:
f.write(str(data) + "\r\n")
lxprint("Log stop at: " + str(datetime.now()))
def GPIOprocess(pin, connLog, stopQueue, imagePipe, expStart):
global pinDoor
global pinWheel
global pinPump
global pinStrip
global wheelBounce
global doorBounce
global doorOpen
global syncDelay
global parameterDict
global contrastProtocol
#Retrieve protocol parameters
wheelInterval = parameterDict["Maximum time between wheel events (seconds):"]
minRev = parameterDict["Minimum wheel revolutions for reward:"]
maxRev = parameterDict["Maximum wheel revolutions for reward:"]
pumpDuration = parameterDict["Duration of pump \"on\" state (seconds):"]
rewardDuration = parameterDict["Maximum duration of reward state (seconds):"]
delay = 0 #Debounce delay
#Set state flags
wheelCount = 0 #Number of wheel revolutions
rewardRev = 0 #Number of revolutions needed to trigger a reward event
currentTime = time.time() #Tracker of current time point
wheelEnd = currentTime #Timeout for wheel
rewardEnd = currentTime #Track when a reward state times out
runState = False #Whether to send data to the image process or to only log events
pinState = False #Previous state of GPIO pin at last state change
newState = False #Current state of GPIO pin
stateChange = False #Single loop cycle flag if an state has changed and has not yet been handled
run = True
#Set output strings
header = '' #Device string
stateStr = '' #High/low string
stopString = '' #String to print when process stops
try:
#Setup GPIO pin with pull-up resistor, and detection interrupts for both rising and falling events
#If the GPIO pin is the wheel pin, then log wheel events
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if pin == pinWheel:
header = "Wheel - "
delay = wheelBounce/1000
stopString = "Wheel stop at: "
runState = True #Initialize assuming control state
rewardRev = random.randint(minRev, maxRev)
#Have the wheel process also start the power strip to turn on monitors
GPIO.setup(pinStrip, GPIO.OUT)
GPIO.output(pinStrip, GPIO.HIGH) #Turn monitor on
connLog.send("Wheel starting at: " + "{:.3f}".format(time.time()-expStart))
connLog.send("Monitor on at: " + "{:.3f}".format(time.time()-expStart))
#Otherwise, log all other door events, and activate pump output
else:
header = "Door - "
delay = doorBounce/1000
stopString = "Door stop at: "
GPIO.setup(pinPump, GPIO.OUT)
GPIO.output(pinPump, GPIO.LOW) #Initialize with pump low
runState = False #Initialize assuming control state
connLog.send("Door starting at: " + "{:.3f}".format(time.time()-expStart))
#Poll GPIO pin and send events to log when state changes
pinState = GPIO.input(pin)
pumpOn = False
currentTime = time.time()
while run:
time.sleep(syncDelay)
currentTime = time.time()
#see if there is a state flag from the image process
if imagePipe.poll():
runState = imagePipe.recv() #Activate run state
wheelCount = 0 #Reset wheel count
wheelEnd = currentTime #Reset wheel timeout timer
rewardEnd = currentTime + rewardDuration #start reward timers
#Take action if state is active and new state change occured
if(runState and stateChange):
stateChange = False #Clear the stateChange flag
#Door trigger pump events
if(pin == pinDoor):
#If door is open and reward is active, turn on pump
if(pinState == doorOpen and not pumpOn):
GPIO.output(pinPump, GPIO.HIGH)
pumpOn = True
connLog.send("Pump - State: On, Time: " + "{:.3f}".format(currentTime - expStart))
rewardEnd = currentTime + pumpDuration #Set reward to end at end of pump cycle - this will extend reward or shorten time to match pump on time
#Otherwise check if wheel has triggered a reward event
else:
if(pinState and wheelCount > 0) and parameterDict["Control image set:"]:
connLog.send("Wheel revolution " + str(wheelCount) + " of " + str(rewardRev))
if(wheelCount == rewardRev):
imagePipe.send(1) #Tell image process that reward event has been triggered
runState = False
#If there is no control image, leave pump on while door is open
if not parameterDict["Control image set:"]:
if(pin == pinDoor):
if(pinState == doorOpen and not pumpOn):
GPIO.output(pinPump, GPIO.HIGH)
connLog.send("Pump - State: On, Time: " + "{:.3f}".format(currentTime - expStart))
pumpOn = True
elif(pinState != doorOpen and pumpOn):
GPIO.output(pinPump, GPIO.LOW)
connLog.send("Pump - State: Off, Time: " + "{:.3f}".format(currentTime - expStart))
pumpOn = False
else:
pass
#At end of reward event, turn pump off and flag image process
if(pin == pinDoor):
if(runState):
if(rewardEnd < currentTime):
if pumpOn:
GPIO.output(pinPump, GPIO.LOW)
pumpOn = False
connLog.send("Pump - State: Off, Time: " + "{:.3f}".format(currentTime - expStart))
imagePipe.send(1) #Tell image process reward state is over
runState = False
#If GPIO state changes, log event
newState = GPIO.input(pin)
if (newState ^ pinState):
stateChange = True
pinState = newState #Update current state
if pinState: