def temporal(t,dur_u,inter): global i2 # deliver food arduino.food("deliver",board,servoPin,top) # delay for the duration of US arduino.c_delay(dur_u,top) # remove food arduino.food("remove",board,servoPin,top) # delay for the inter-trial interval arduino.c_delay(inter,top) i2 += 1 # increase the number of trials # if the amount has not matched the requirement, call the function for this mode again if i2 <= t: temporal(t,dur_u,inter)
def pressbutton(): global interval global duration global rangemin global rangemax global timeslist global mode global thistimes # create a variable to achieve correct detection of the state of button # it is initially set to zero, once the button is pressed, it changed to 1, indicating the button is pressed # and it will be changed to zero again only when the button is released # after it back to zero, another press will be detected and count as active press # meanwhile, this variable can count for the rising and falling edge of button upanddown = 0 currenttimes = 0 # the times that the rat already press # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): # get a list to test l=input("please enter a list of presses:") # convert into list strlist = list(l) # get the siginificant value in the list and save them into a new list # the number in this list is the range that the criterion can be selected from for x in range(len(strlist)): if strlist[x] in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]: timeslist.append(math.floor(float(strlist[x]))) # randomly select the criterion for each trial thistimes=random.choice(timeslist) # print it out print(thistimes) while True: if buttonPin.read() == 1: # if button is pressed, .read() will return 1 if upanddown == 0: upanddown = 1 LEDPin.write(1) top.update() sleep(0.1) LEDPin.write(0) # make the LED blink once to indicate the press lead to reward currenttimes += 1; # count it into presses that lead to reward # feed if currenttimes == thistimes: print ('press completed') top.update() sleep(1) print ('start feeding') arduino.food("deliver",board,servoPin,top) print ('stay feeding') sleep(2) print ('feeding end') arduino.food("remove",board,servoPin,top) currenttimes = 0 # reset currenttimes for new trials if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: interval = math.floor(float(intervalEntry.get())) # the value get from GUI window, need to be converted into floating number duration = math.floor(float(durationEntry.get())) if rangeminEntry.get() != "": # if user enter the value in range rangemin = math.floor(float(rangeminEntry.get())) rangemax = math.floor(float(rangemaxEntry.get())) else: # otherwise, save as None rangemin = "NA" rangemax = "NA" if listEntry.get() != "": # if user enter the value in list inputlist = listEntry.get() strlist = inputlist.split(" ") for x in range(len(strlist)): timeslist.append(math.floor(float(strlist[x]))) else: inputlist = "NA" mode = int(modVar.get()) with open('configuration_variable.csv', 'w', newline='') as f: # save parameters w = csv.writer(f) w.writerow(["Interval between two times", "Duration of feeding", "Min of range", "Max of range", "Input list", "mode"]) data = [interval, duration, rangemin, rangemax, timeslist, mode] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_variable.csv', 'r', newline='') as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 interval = math.floor(float(data[0])) duration= math.floor(float(data[1])) print("read result: ") print("interval: " + data[0]) print("duration: " + data[1]) if data[2] != "NA": rangemin = math.floor(float(data[2])) rangemax = math.floor(float(data[3])) print("min of range: " + data[2]) print("max of range: " + data[3]) if data[4] != "NA": inputlist = data[4] strlist = list(inputlist) for x in range(len(strlist)): if strlist[x] in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]: timeslist.append(math.floor(float(strlist[x]))) print("list: " + data[4]) mode = int(data[5]) print("mode: " + data[5]) # call the function that contains the sub-function run()
def forward (t,dur_c,dur_u,btw,inter,r,g): global i1 # if there is no overlap between CS and US if btw>=0: # turn on CS arduino.cs(g,r) # delay for the duration of CS arduino.c_delay(dur_c,top) # turn off CS arduino.cs(0,0) # delay for the interval between CS and US arduino.c_delay(btw,top) # deliver food arduino.food("delive",board,servoPin,top) # delay for the duration of US arduino.c_delay(dur_u,top) # remove food arduino.food("remove",board,servoPin,top) # delay for the inter-trial interval arduino.c_delay(inter,top) i1 += 1 # increase the number of trails # if the amount has not matched the requirement, call the function for this mode again if i1 <= t: forward (t,dur_c,dur_u,btw,inter,r,g) # if there is overlap between CS and US else: # If CS is gone before US if -1*btw<=dur_u: # -1 due to the negative value for overlap # turn on CS arduino.cs(g,r) # delay for the time until US is supposed to present arduino.c_delay(dur_c+btw,top) # deliver food arduino.food("deliver",board,servoPin,top) # delay for the time until the overlap of CS has passed, # i.e., the interval between CS and US arduino.c_delay(-1*btw,top) # turn off CS arduino.cs(0,0) # delay for the time until the duration of it has been fulfilled arduino.c_delay(btw+dur_u,top) # remove food arduino.food("remove",board,servoPin,top) # delay for the inter-trial interval arduino.c_delay(inter,top) i1 += 1 # increase the number of trails # if the amount has not matched the requirement, call the function for this mode again if i1 <= t: forward (t,dur_c,dur_u,btw,inter,r,g) # cs is gone after us else: # turn on CS arduino.cs(g,r) # delay until US is supposed to present arduino.c_delay(dur_c+btw,top) # deliver food arduino.food("deliver",board,servoPin,top) # delay for the duration of US arduino.c_delay(dur_u,top) # remove food arduino.food("remove",board,servoPin,top) # delay until fulfill the duration of CS arduino.c_delay(dur_c+btw-dur_u,top) # turn off CS arduino.cs(0,0) # delay for the inter-trial interval arduino.c_delay(inter,top) i1 += 1 # increase the number of trails # if the amount has not matched the requirement, call the function for this mode again if i1 <= t: forward (t,dur_c,dur_u,btw,inter,r,g)
def pressbutton(): global times global interval global duration # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): # let the user to press once to check button's function print('please press the button once') # create a variable to count the presses that lead to reward currenttimes = 0 # create a variable to achieve correct detection of the state of button # it is initially set to zero, once the button is pressed, it changed to 1, indicating the button is pressed # and it will be changed to zero again only when the button is released # after it back to zero, another press will be detected and count as active press # meanwhile, this variable can count for the rising and falling edge of button upanddown = 0 while True: if buttonPin.read( ) == 1: # if button is pressed, .read() will return 1 if upanddown == 0: upanddown = 1 LEDPin.write(1) top.update() sleep(0.1) LEDPin.write( 0 ) # make the LED blink once to indicate the press lead to reward currenttimes += 1 # if one press is detected, deliver the food if currenttimes == 1: print('press completed') top.update() sleep(1) print('start feeding') arduino.food("deliver", board, servoPin, top) print('stay feeding') sleep(2) print('feeding end') arduino.food("remove", board, servoPin, top) currenttimes = 0 # reset for a new trail if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: times = float( timesEntry.get() ) # the value get from GUI window, need to be converted into floating number interval = float(intervalEntry.get()) duration = float(durationEntry.get()) with open('configuration_fix.csv', 'w', newline='') as f: # save parameters w = csv.writer(f) w.writerow([ "Number of times", "Interval between two times", "Duration of feeding" ]) data = [times, interval, duration] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_fix.csv', 'r', newline='') as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 times = float(data[0]) interval = float(data[1]) duration = float(data[2]) print("read result: ") print("times: " + str(times)) print("interval: " + str(interval)) print("duration: " + str(duration)) # call the function that contains the main body run()
def pressbutton(): global times global interval global duration global step global gap # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): # let the user to press once to check button's function print ('please press the button once') currenttimes = 0 upanddown = 0 while True: if buttonPin.read() == 1: # if button is pressed, .read() will return 1 if upanddown == 0: upanddown = 1 LEDPin.write(1) top.update() sleep(0.1) LEDPin.write(0) # make the LED blink once to indicate the press lead to reward currenttimes += 1; # if one press is detected, deliver the food if currenttimes == 1: print ('press completed') top.update() sleep(1) print ('start feeding') arduino.food("deliver",board,servoPin,top) print ('stay feeding') sleep(2) print ('feeding end') arduino.food("remove",board,servoPin,top) currenttimes = 0 # reset currenttimes if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: times = float(timesEntry.get()) # the value get from GUI window, need to be converted into floating number interval = float(intervalEntry.get()) duration = float(durationEntry.get()) if randomVar.get(): step="random" else: step=int(stepEntry.get()) gap = int(gapEntry.get()) with open('configuration_p.csv', 'w', newline="") as f: # save parameters w = csv.writer(f) w.writerow(["Number of press", "Interval between press and feed", "Duration of feeding","Incresing step","Trials between increment"]) data = [times, interval, duration,step,gap] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_p.csv', 'r', newline="") as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 times = float(data[0]) interval = float(data[1]) duration= float(data[2]) if data[3]=='random': step = data[3] else: step = int(data[3]) gap = int(data[4]) print("times: " + data[0]) print("interval: " + data[1]) print("duration: " + data[2]) print("step: " + data[3]) print("gap: " + data[4]) # call the function that contains the main body run()
def pressbutton(): global times global interval global duration # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): # let the user to press once to check button's function print ('please press the button once') # create a variable to count the presses that lead to reward currenttimes = 0 # create a variable to achieve correct detection of the state of button # it is initially set to zero, once the button is pressed, it changed to 1, indicating the button is pressed # and it will be changed to zero again only when the button is released # after it back to zero, another press will be detected and count as active press # meanwhile, this variable can count for the rising and falling edge of button upanddown = 0 while True: if buttonPin.read() == 1: # if button is pressed, .read() will return 1 if upanddown == 0: upanddown = 1 LEDPin.write(1) top.update() sleep(0.1) LEDPin.write(0) # make the LED blink once to indicate the press lead to reward currenttimes += 1 # if one press is detected, deliver the food if currenttimes == 1: print ('press completed') top.update() sleep(1) print ('start feeding') arduino.food("deliver",board,servoPin,top) print ('stay feeding') sleep(2) print ('feeding end') arduino.food("remove",board,servoPin,top) currenttimes = 0 # reset for a new trail if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: times = float(timesEntry.get()) # the value get from GUI window, need to be converted into floating number interval = float(intervalEntry.get()) duration = float(durationEntry.get()) with open('configuration_fix.csv', 'w', newline='') as f: # save parameters w = csv.writer(f) w.writerow(["Number of times", "Interval between two times", "Duration of feeding"]) data = [times, interval, duration] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_fix.csv', 'r', newline='') as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 times = float(data[0]) interval = float(data[1]) duration= float(data[2]) print("read result: ") print("times: " + str(times)) print("interval: " + str(interval)) print("duration: " + str(duration)) # call the function that contains the main body run()
def pressbutton(): global times global interval global duration global step global gap # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): # let the user to press once to check button's function print('please press the button once') currenttimes = 0 upanddown = 0 while True: if buttonPin.read( ) == 1: # if button is pressed, .read() will return 1 if upanddown == 0: upanddown = 1 LEDPin.write(1) top.update() sleep(0.1) LEDPin.write( 0 ) # make the LED blink once to indicate the press lead to reward currenttimes += 1 # if one press is detected, deliver the food if currenttimes == 1: print('press completed') top.update() sleep(1) print('start feeding') arduino.food("deliver", board, servoPin, top) print('stay feeding') sleep(2) print('feeding end') arduino.food("remove", board, servoPin, top) currenttimes = 0 # reset currenttimes if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: times = float( timesEntry.get() ) # the value get from GUI window, need to be converted into floating number interval = float(intervalEntry.get()) duration = float(durationEntry.get()) if randomVar.get(): step = "random" else: step = int(stepEntry.get()) gap = int(gapEntry.get()) with open('configuration_p.csv', 'w', newline="") as f: # save parameters w = csv.writer(f) w.writerow([ "Number of press", "Interval between press and feed", "Duration of feeding", "Incresing step", "Trials between increment" ]) data = [times, interval, duration, step, gap] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_p.csv', 'r', newline="") as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 times = float(data[0]) interval = float(data[1]) duration = float(data[2]) if data[3] == 'random': step = data[3] else: step = int(data[3]) gap = int(data[4]) print("times: " + data[0]) print("interval: " + data[1]) print("duration: " + data[2]) print("step: " + data[3]) print("gap: " + data[4]) # call the function that contains the main body run()
def pressbutton(): global interval global duration global rangemin global rangemax global timeslist global mode global thistimes # create a variable to achieve correct detection of the state of button # it is initially set to zero, once the button is pressed, it changed to 1, indicating the button is pressed # and it will be changed to zero again only when the button is released # after it back to zero, another press will be detected and count as active press # meanwhile, this variable can count for the rising and falling edge of button upanddown = 0 currenttimes = 0 # the times that the rat already press # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): # get a list to test l = input("please enter a list of presses:") # convert into list strlist = list(l) # get the siginificant value in the list and save them into a new list # the number in this list is the range that the criterion can be selected from for x in range(len(strlist)): if strlist[x] in [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]: timeslist.append(math.floor(float(strlist[x]))) # randomly select the criterion for each trial thistimes = random.choice(timeslist) # print it out print(thistimes) while True: if buttonPin.read( ) == 1: # if button is pressed, .read() will return 1 if upanddown == 0: upanddown = 1 LEDPin.write(1) top.update() sleep(0.1) LEDPin.write( 0 ) # make the LED blink once to indicate the press lead to reward currenttimes += 1 # count it into presses that lead to reward # feed if currenttimes == thistimes: print('press completed') top.update() sleep(1) print('start feeding') arduino.food("deliver", board, servoPin, top) print('stay feeding') sleep(2) print('feeding end') arduino.food("remove", board, servoPin, top) currenttimes = 0 # reset currenttimes for new trials if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: interval = math.floor( float(intervalEntry.get()) ) # the value get from GUI window, need to be converted into floating number duration = math.floor(float(durationEntry.get())) if rangeminEntry.get() != "": # if user enter the value in range rangemin = math.floor(float(rangeminEntry.get())) rangemax = math.floor(float(rangemaxEntry.get())) else: # otherwise, save as None rangemin = "NA" rangemax = "NA" if listEntry.get() != "": # if user enter the value in list inputlist = listEntry.get() strlist = inputlist.split(" ") for x in range(len(strlist)): timeslist.append(math.floor(float(strlist[x]))) else: inputlist = "NA" mode = int(modVar.get()) with open('configuration_variable.csv', 'w', newline='') as f: # save parameters w = csv.writer(f) w.writerow([ "Interval between two times", "Duration of feeding", "Min of range", "Max of range", "Input list", "mode" ]) data = [ interval, duration, rangemin, rangemax, timeslist, mode ] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_variable.csv', 'r', newline='') as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 interval = math.floor(float(data[0])) duration = math.floor(float(data[1])) print("read result: ") print("interval: " + data[0]) print("duration: " + data[1]) if data[2] != "NA": rangemin = math.floor(float(data[2])) rangemax = math.floor(float(data[3])) print("min of range: " + data[2]) print("max of range: " + data[3]) if data[4] != "NA": inputlist = data[4] strlist = list(inputlist) for x in range(len(strlist)): if strlist[x] in [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]: timeslist.append(math.floor(float(strlist[x]))) print("list: " + data[4]) mode = int(data[5]) print("mode: " + data[5]) # call the function that contains the sub-function run()
def startpressbutton(): global trials global times global interval global duration global sdinterval global intertrial global looptime global upanddown global currenttimes # disable the start button to avoid double-click during executing startButton.config(state=tkinter.DISABLED) # If user choose to pre-test # automatically start a trial that implement all the hardware # in order to check basic settings if preVar.get(): sdPin.write(1) print ('please press the button once') if buttonPin.read() == 1: # if button is pressed, .read() will return 1 if upanddown==0: sdPin.write(0) LEDPin.write(1) sleep(0.1) LEDPin.write(0) # make the LED blink once to indicate the press lead to reward currenttimes += 1 # count it into presses that lead to reward # if one press is detected, deliver the food if currenttimes == 1: print ('press completed') top.update() sleep(1) print ('start feeding') arduino.food("deliver",board,servoPin,top) print ('stay feeding') sleep(2) print ('feeding end') arduino.food("remove",board,servoPin,top) currenttimes = 0 # reset for a new trail if upanddown == 1: if buttonPin.read() == 0: upanddown = 0 # implement the experiment else: # whether load the configuration # don't read from existing configuration if loadVar.get() == 0: # the value get from GUI window, need to be converted into floating number trials = float(trialsEntry.get()) times = float(timesEntry.get()) interval = float(intervalEntry.get()) duration = float(durationEntry.get()) sdinterval = float(sdintervalEntry.get()) intertrial = float(intertrialEntry.get()) with open('configuration_sd.csv', 'w', newline='') as f: w = csv.writer(f) w.writerow(["trials", "times", "interval", "duration", "sdinterval", "intertrial"]) data = [trials, times, interval, duration, sdinterval, intertrial] w.writerow(data) # read from existing configuration and print them out else: with open('configuration_sd.csv', 'r', newline='') as f: r = csv.reader(f) data = None count = 0 for row in r: if count == 1: data = row count += 1 trials = float(data[0]) times = float(data[1]) interval = float(data[2]) duration= float(data[3]) sdinterval = float(data[4]) intertrial = float(data[5]) print("trials: " + str(trials)) print("times: " + str(times)) print("interval: " + str(interval)) print("duration: " + str(duration)) print("sdinterval: " + str(sdinterval)) print("intertrial: " + str(intertrial)) looptime = time.time() # get the start time for the first trial sdPin.write(1) # turn on SD # begin the first trail # call the function that contains the main body run()