Ejemplo n.º 1
0
    def run(self):
        """
        Runs when measurement is started. Runs in a separate thread from GUI.
        It should not update the graphical interface directly, and should only
        focus on data acquisition.
        """
        #         # first, create a data file
        #         if self.settings['save_h5']:
        #             # if enabled will create an HDF5 file with the plotted data
        #             # first we create an H5 file (by default autosaved to app.settings['save_dir']
        #             # This stores all the hardware and app meta-data in the H5 file
        #             self.h5file = h5_io.h5_base_file(app=self.app, measurement=self)
        #
        #             # create a measurement H5 group (folder) within self.h5file
        #             # This stores all the measurement meta-data in this group
        #             self.h5_group = h5_io.h5_create_measurement_group(measurement=self, h5group=self.h5file)
        #
        #             # create an h5 dataset to store the data
        #             self.buffer_h5 = self.h5_group.create_dataset(name  = 'buffer',
        #                                                           shape = self.buffer.shape,
        #                                                           dtype = self.buffer.dtype)

        # We use a try/finally block, so that if anything goes wrong during a measurement,
        # the finally block can clean things up, e.g. close the data file object.

        #self.wide_cam._dev.set_buffer_count(500)

        if self.settings.save_video.value():
            save_dir = self.app.settings.save_dir.value()
            data_path = os.path.join(save_dir,
                                     self.app.settings.sample.value())
            try:
                os.makedirs(data_path)
            except OSError:
                print(
                    'directory already exists, writing to existing directory')

            self.recorder.settings.path.update_value(data_path)

            #frame_rate = self.wide_cam.settings.frame_rate.value()
            #self.recorder.create_file('wide_mov',frame_rate)
#
#         # create a subthread and connect it to the interrupt subthread signal
#         self.wide_disp_queue = queue.Queue(1000)
#         self.camera_thread = SubMeasurementQThread(self.camera_action)
#         self.interrupt_subthread.connect(self.camera_thread.interrupt)
#         #start camera
#         self.wide_cam.start()
#
#         #start camera subthread
#         self.camera_thread.start()

# initiate all the odor solenoids
        odor0 = DAQSimpleDOTask('Dev3/port0/line0')
        clean0 = DAQSimpleDOTask('Dev3/port0/line1')
        odor1 = DAQSimpleDOTask('Dev3/port0/line2')
        clean1 = DAQSimpleDOTask('Dev3/port0/line3')
        odor2 = DAQSimpleDOTask('Dev3/port0/line4')
        clean2 = DAQSimpleDOTask('Dev3/port0/line5')
        odor3 = DAQSimpleDOTask('Dev3/port0/line6')
        clean3 = DAQSimpleDOTask('Dev3/port0/line7')
        odor4 = DAQSimpleDOTask('Dev3/port1/line0')
        clean4 = DAQSimpleDOTask('Dev3/port1/line1')
        odor5 = DAQSimpleDOTask('Dev3/port1/line2')
        clean5 = DAQSimpleDOTask('Dev3/port1/line3')
        odor6 = DAQSimpleDOTask('Dev3/port1/line4')
        clean6 = DAQSimpleDOTask('Dev3/port1/line5')
        odor7 = DAQSimpleDOTask('Dev3/port1/line6')
        clean7 = DAQSimpleDOTask('Dev3/port1/line7')

        # #         # D2-8
        #         reward_odor = odor5
        #         reward_odor_clean = clean5
        #         punish_odor = odor4
        #         punish_odor_clean = clean4
        #         reward_sound = 1
        #         punish_sound = 2
        #         events_filename = 'd2-8_7-11-2019_odor_sound.xlsx'
        #         #events_filename = 'test.xlsx'

        #         # D2-9
        #         reward_odor = odor4
        #         reward_odor_clean = clean4
        #         punish_odor = odor5
        #         punish_odor_clean = clean5
        #         reward_sound = 1
        #         punish_sound = 2
        #         events_filename = 'd2-9_7-11-2019_odor_sound.xlsx'
        #
        #         # D2-10
        #         reward_odor = odor5
        #         reward_odor_clean = clean5
        #         punish_odor = odor4
        #         punish_odor_clean = clean4
        #         reward_sound = 2
        #         punish_sound = 1
        #         events_filename = 'd2-10_7-11-2019_odor_sound.xlsx'

        # D1-7 rewards: odor4, sound 1
        #         reward_odor = odor4
        #         reward_odor_clean = clean4
        #         punish_odor = odor5
        #         punish_odor_clean = clean5
        #         reward_sound = 1
        #         punish_sound = 2
        #         events_filename = 'd1-7_7-12-2019_odor_sound.xlsx'
        #         events_filename = 'test.xlsx'

        #         # D1-12 rewards: odor5, sound 1
        #         reward_odor = odor5
        #         reward_odor_clean = clean5
        #         punish_odor = odor4
        #         punish_odor_clean = clean4
        #         reward_sound = 1
        #         punish_sound = 2
        #         events_filename = 'd1-12_7-11-2019_odor_sound.xlsx'

        # D1-13 rewards: odor4, sound 2
        #         reward_odor = odor4
        #         reward_odor_clean = clean4
        #         punish_odor = odor5
        #         punish_odor_clean = clean5
        #         reward_sound = 2
        #         punish_sound = 1
        #         events_filename = 'd1-13_7-11-2019_odor_sound.xlsx'

        # D1-14 rewards: odor5, sound 2
        reward_odor = odor5
        reward_odor_clean = clean5
        punish_odor = odor4
        punish_odor_clean = clean4
        reward_sound = 2
        punish_sound = 1
        events_filename = 'd1-14_7-11-2019_odor_sound.xlsx'

        odor0.low()
        odor1.low()
        odor2.low()
        odor3.low()
        odor4.low()
        odor5.low()
        odor6.low()
        odor7.low()

        clean0.low()
        clean1.low()
        clean2.low()
        clean3.high()
        clean4.high()
        clean5.high()
        clean6.high()
        clean7.high()

        odor0.close()
        odor1.close()
        odor2.close()
        odor3.close()
        odor6.close()
        odor7.close()

        clean0.close()
        clean1.close()
        clean2.close()
        clean3.close()
        clean6.close()
        clean7.close()

        LED = DAQSimpleDOTask('Dev3/port2/line2')
        LED.low()
        self.OdorOnCopy = DAQSimpleDOTask('Dev3/port2/line5')
        self.OdorOnCopy.low()
        waterR = DAQSimpleDOTask('Dev3/port2/line0')
        waterR.low()
        airpuff1 = DAQSimpleDOTask('Dev3/port2/line3')
        airpuff1.low()
        airpuff2 = DAQSimpleDOTask('Dev3/port2/line6')
        airpuff2.low()
        self.lickR = DAQSimpleDITask('Dev3/port2/line4')

        # EVENT CODES
        # video recording start / start trial = 101
        # lick on = 11, lick off = 10
        # right water on = 51, right water off = 50
        # airpuff on = 81, off = 80

        # reward odor on = 131, off = 130
        # punish odor on = 141, off = 140
        # reward sound on = 151, off = 150
        # punish sound on = 161, off = 160

        #create excel workbook
        self.wb = Workbook()
        self.ws = self.wb.active

        numtrials = 120
        a = np.empty(int(numtrials / 4))
        a.fill(2)
        b = np.empty(int(numtrials / 4))
        b.fill(3)
        trialtypes = np.concatenate((np.ones(
            (int(numtrials / 4))), np.zeros((int(numtrials / 4))), a, b))
        random.shuffle(trialtypes)
        print(trialtypes)

        #         # arrays to randomize skipped trials for each trial type
        #         num_to_skip = int((numtrials/5)*0.1)
        #         skip0 = np.concatenate((np.zeros(int(numtrials/10)-num_to_skip),np.ones(num_to_skip)))
        #         random.shuffle(skip0)
        #         skip0 = np.concatenate((np.zeros(int(numtrials/10)),skip0))
        #         skip1 = np.concatenate((np.zeros(int(numtrials/10)-num_to_skip),np.ones(num_to_skip)))
        #         random.shuffle(skip1)
        #         skip1 = np.concatenate((np.zeros(int(numtrials/10)),skip1))
        #         skip2 = np.concatenate((np.zeros(int(numtrials/10)-num_to_skip),np.ones(num_to_skip)))
        #         random.shuffle(skip2)
        #         skip2 = np.concatenate((np.zeros(int(numtrials/10)),skip2))
        #         skip3 = np.concatenate((np.zeros(int(numtrials/10)-num_to_skip),np.ones(num_to_skip)))
        #         random.shuffle(skip3)
        #         skip3 = np.concatenate((np.zeros(int(numtrials/10)),skip3))

        # counters for each trial type
        h = 0
        i = 0
        j = 0
        k = 0

        duration_rec_off = 6.5
        duration_rec_on_before = 4
        duration_odor_to_outcome = 1.3
        duration_water_large = 0.2
        duration_airpuff = 0.2
        duration_rec_on_after = 8

        sound_thread = SubMeasurementQThread(self.sound_action)
        self.interrupt_subthread.connect(sound_thread.interrupt)
        sound_thread.start()

        for t in range(0, numtrials):

            if bool(trialtypes[t]):
                self.settings.filename.update_value('reward_trial' + str(t))
            else:
                self.settings.filename.update_value('punish_trial' + str(t))

            self.recorder.create_file(
                self.settings.filename.value(),
                self.wide_cam.settings.frame_rate.value())

            #print('turn on LED')
            LED.high()
            self.check_licking_1spout(1)

            #self.settings.save_video.update_value(True):
            print(t)
            self.settings.in_trial.update_value(True)
            d = self.ws.cell(row=(self.ws.max_row + 1),
                             column=1,
                             value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2, value=101)
            self.check_licking_1spout(duration_rec_on_before)

            if trialtypes[t] == 0:
                print('odor reward trial ' + str(h))
                print('opening odor port')
                reward_odor.high()
                self.OdorOnCopy.high()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=131)

                reward_odor_clean.low()
                self.check_licking_1spout(duration_odor_to_outcome)

                print('opening water valve')
                waterR.high()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=51)
                self.check_licking_1spout(duration_water_large)

                print('closing odor port')
                reward_odor.low()
                self.OdorOnCopy.low()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=130)
                reward_odor_clean.high()

                waterR.low()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=50)

                h += 1

                self.wb.save(events_filename)

            elif trialtypes[t] == 1:
                print('punish odor trial ' + str(i))
                print('opening odor port')
                punish_odor.high()
                self.OdorOnCopy.high()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=141)

                punish_odor_clean.low()
                self.check_licking_1spout(duration_odor_to_outcome)

                print('delivering airpuff')
                airpuff1.high()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=81)

                self.check_licking_1spout(duration_airpuff)

                print('closing odor port')
                punish_odor.low()
                self.OdorOnCopy.low()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=140)
                punish_odor_clean.high()

                airpuff1.low()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=80)

                i += 1

                self.wb.save(events_filename)

            elif trialtypes[t] == 2:
                print('sound reward trial ' + str(j))
                print('playing reward sound')

                self.settings.play_frequency.update_value(reward_sound)
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=151)

                self.check_licking_1spout(duration_odor_to_outcome)

                print('opening water valve')
                waterR.high()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=51)
                self.check_licking_1spout(duration_water_large)

                waterR.low()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=50)

                j += 1

                self.wb.save(events_filename)

            else:
                print('sound punish trial ' + str(k))
                print('playing punish sound')

                self.settings.play_frequency.update_value(punish_sound)
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=161)

                self.check_licking_1spout(duration_odor_to_outcome)

                print('delivering airpuff')
                airpuff1.high()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=81)

                self.check_licking_1spout(duration_airpuff)

                airpuff1.low()
                d = self.ws.cell(row=(self.ws.max_row + 1),
                                 column=1,
                                 value=time.clock())
                d = self.ws.cell(row=self.ws.max_row, column=2, value=80)

                k += 1

                self.wb.save(events_filename)

            self.check_licking_1spout(duration_rec_on_after)
            self.settings.in_trial.update_value(False)
            #self.settings.save_video.update_value(False):
            LED.low()

            self.wb.save(events_filename)

            self.check_licking_1spout(duration_rec_off)

            if self.interrupt_measurement_called:
                #tell subtherad to stop
                self.interrupt_subthread.emit()
                break

        reward_odor.close()
        reward_odor_clean.close()
        punish_odor.close()
        punish_odor_clean.close()
        waterR.close()
        waterR.close()
        airpuff1.close()
        airpuff2.close()
        LED.close()
        self.OdorOnCopy.close()
        print('FINISHED ASSOCIATION TRAINING')

        #sound_thread.stop()
        del sound_thread

        if self.settings.save_video.value():
            self.recorder.close()
from ScopeFoundry import Measurement
from ScopeFoundry.measurement import MeasurementQThread
from AntCamHW.daq_do.daq_do_dev import DAQSimpleDOTask
from AntCamHW.daq_di.daq_di_dev import DAQSimpleDITask
import time
import cv2

# this test roughly consumes 5 ml water
# water = DAQSimpleDOTask('Dev1/port0/line0')
water = DAQSimpleDOTask('Dev2_SELECT/port0/line0')

timeout = time.time() + 1200
count_drop = 0
lickR = DAQSimpleDITask('Dev2_SELECT/port1/line0')
water.high()
time.sleep(0.3)
water.low()
while time.time() < timeout and count_drop < 200:

    lick = lickR.read()
    if not lick:
        count_drop += 1
        print(count_drop)
        time.sleep(5)
        water.high()
        time.sleep(0.06)
        water.low()

    #else:
    #   continue
    def run(self):
        try:
            os.makedirs(self.events_path)
        except OSError:
            print("The directory %s existed" % self.events_path)
        else:
            print("Successfully created the directory %s " % self.events_path)
        logname = self.filename[0:-5] + '.log'
        logging.basicConfig(filename=logname, level=logging.DEBUG)
        logging.info(self.__dict__)
        odors_cue = OdorGen(self.list)
        odors_cue.assign_odor()
        self.reward_odor, self.non_reward_odor = odors_cue.set_rewardodor(
            index=self.reward_odor_index)
        odors_cue.initiate()
        # odors_cue.odors_DAQ[i]
        print('odor done')

        self.waterR = DAQSimpleDOTask('Dev2_SELECT/port0/line{}'.format(
            self.waterline))
        self.waterR.low()
        # self.OdorOnCopy = DAQSimpleDOTask('Dev3/port2/line5')
        # self.OdorOnCopy.low()
        self.lickR = DAQSimpleDITask('Dev2_SELECT/port1/line0')
        print('water done')

        # EVENT CODES
        # video recording start / start trial = 101
        # end trial = 100
        # lick on = 11, lick off = 10

        # contingency reward odor on = 131, off = 130, water on = 51, right water off = 50
        # contingency no reward odor on = 141, off = 140, water on = 61, right water off = 60
        # non-contingency reward odor on = 151, off = 150, water on = 71, right water off = 70
        # non-contingency no reward odor on = 161, off = 160, water on = 81, right water off = 80

        # create excel workbook

        self.wb = Workbook()
        self.ws = self.wb.active

        #generate trial type

        # generate trial type
        trialtypes = np.zeros(self.numtrials)
        for i in range(int(self.numtrials / 20)):
            train_go = np.zeros(int(round(20 * self.p_go *
                                          self.p_reward_go)))  # code 0
            train_nogo = np.ones(int(20 * self.p_no_go))  # code 1
            temp_comb1 = np.concatenate((train_go, train_nogo))

            train_empty = np.ones(
                int(20 * self.p_empty *
                    (1 - self.p_reward_empty))) * 2  # code 2
            train_go_omission = np.ones(
                int(round(20 * self.p_go *
                          (1 - self.p_reward_go)))) * 3  # code 3
            train_unpredwater = np.ones(
                int(20 * self.p_empty * self.p_reward_empty)) * 4  # code 4

            temp_comb1 = np.concatenate((temp_comb1, train_empty))
            temp_comb1 = np.concatenate((temp_comb1, train_unpredwater))
            temp_comb2 = np.concatenate((temp_comb1, train_go_omission))
            random.shuffle(temp_comb2)
            trialtypes[20 * i:20 * (i + 1)] = temp_comb2
        self.trialstype = trialtypes
        print('================== Trial Types =================')
        print(trialtypes)
        for t in range(0, self.numtrials):
            print('================================================')
            print('trial number: ', t)

            d = self.ws.cell(row=(self.ws.max_row + 1),
                             column=1,
                             value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2,
                             value=101)  #trial start
            d = self.ws.cell(row=self.ws.max_row,
                             column=3,
                             value='trial{}'.format(int(
                                 trialtypes[t])))  #trial type
            # code: 0--go w rward; 1--no go; 2--empty; 3--go w/o reward
            self.check_licking_1spout(self.duration_rec_on_before)
            #           main training program
            self.run_trial_type(int(trialtypes[t]))
            self.check_licking_1spout(self.duration_rec_on_after)
            # self.settings.save_video.update_value(False):
            self.check_licking_1spout(self.duration_ITI[t])
            d = self.ws.cell(row=(self.ws.max_row + 1),
                             column=1,
                             value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2, value=100)  #end
            self.wb.save(self.filename)
        odors_cue.initiate()
        odors_cue.close()
        self.waterR.low()
        self.waterR.close()
        print('FINISHED ASSOCIATION TRAINING')
    def run(self):
        print('a ha')

        odors_cue = OdorGen(self.list)
        odors_cue.assign_odor()
        self.reward_odor = odors_cue.set_rewardodor(index=self.reward_odor_index)
        odors_cue.initiate()
        # odors_cue.odors_DAQ[i]
        print('odor done')

        self.waterR = DAQSimpleDOTask('Dev2_SELECT/port0/line{}'.format(self.waterline))
        self.waterR.low()
        # self.OdorOnCopy = DAQSimpleDOTask('Dev3/port2/line5')
        # self.OdorOnCopy.low()
        self.lickR = DAQSimpleDITask('Dev2_SELECT/port1/line0')
        print('water done')

        # EVENT CODES
        # video recording start / start trial = 101
        # lick on = 11, lick off = 10

        # contingency reward odor on = 131, off = 130, water on = 51, right water off = 50
        # contingency no reward odor on = 141, off = 140, water on = 61, right water off = 60
        # non-contingency reward odor on = 151, off = 150, water on = 71, right water off = 70
        # non-contingency no reward odor on = 161, off = 160, water on = 81, right water off = 80

        # create excel workbook
        self.wb = Workbook()
        self.ws = self.wb.active
        print('book done')


        #generate trial type

        # generate trial type

        cont_reward = np.zeros(int(self.numtrials * self.p_cont_noncont * self.p_USwCS))  # code 0
        cont_noreward = np.ones(int(self.numtrials * self.p_cont_noncont * (1 - self.p_USwCS)))  # code 1
        temp_comb1 = np.concatenate((cont_reward, cont_noreward))

        noncont_reward = np.ones(int(self.numtrials * (1 - self.p_cont_noncont) * self.p_USwoCS)) * 2  # code 2
        noncont_noreward = np.ones(int(self.numtrials * (1 - self.p_cont_noncont) * (1 - self.p_USwoCS))) * 3  # code 3
        temp_comb2 = np.concatenate((noncont_noreward, noncont_reward))

        trialtypes = np.concatenate((temp_comb1, temp_comb2))
        random.shuffle(trialtypes)
        print('================== Trial Types =================')
        print(trialtypes)

        for t in range(0, self.numtrials):
            print('================================================')

            print('trial number: ', t)
            print()

            d = self.ws.cell(row=(self.ws.max_row + 1), column=1, value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2, value=101)
            self.check_licking_1spout(self.duration_rec_on_before)

#           main training program
            self.run_trial_type(int(trialtypes[t]))

            self.check_licking_1spout(self.duration_rec_on_after)

            # self.settings.save_video.update_value(False):

            self.wb.save(self.events_path+'.xlsm')

            self.check_licking_1spout(self.duration_ITI[t])

        odors_cue.initiate()
        odors_cue.close()
        self.waterR.low()
        self.waterR.close()

        print('FINISHED ASSOCIATION TRAINING')
    def run(self):
        try:
            os.makedirs(self.events_path)
        except OSError:
            print("The directory %s existed" % self.events_path)
        else:
            print("Successfully created the directory %s " % self.events_path)
        logname = self.filename[0:-5] + '.log'
        logging.basicConfig(filename=logname, level=logging.DEBUG)
        logging.info(self.__dict__)
        odors_cue = OdorGen(self.list)
        odors_cue.assign_odor()
        self.reward_odor, self.non_reward_odor = odors_cue.set_rewardodor(
            index=self.reward_odor_index)
        odors_cue.initiate()
        # odors_cue.odors_DAQ[i]
        print('odor done')

        self.waterR = DAQSimpleDOTask('Dev2_SELECT/port0/line{}'.format(
            self.waterline))
        self.waterR.low()
        # self.OdorOnCopy = DAQSimpleDOTask('Dev3/port2/line5')
        # self.OdorOnCopy.low()
        self.lickR = DAQSimpleDITask('Dev2_SELECT/port1/line0')
        print('water done')

        # EVENT CODES
        # video recording start / start trial = 101
        # end trial = 100
        # lick on = 11, lick off = 10

        # contingency reward odor on = 131, off = 130, water on = 51, right water off = 50
        # contingency no reward odor on = 141, off = 140, water on = 61, right water off = 60
        # non-contingency reward odor on = 151, off = 150, water on = 71, right water off = 70
        # non-contingency no reward odor on = 161, off = 160, water on = 81, right water off = 80

        # create excel workbook

        self.wb = Workbook()
        self.ws = self.wb.active

        #generate trial type

        # generate trial type
        trialtypes = self.generate_trial()

        self.trialtype = trialtypes
        print('================== Trial Types =================')
        print(trialtypes)

        for t in range(0, self.numtrials):
            print('================================================')

            print('trial number: ', t)
            print()

            d = self.ws.cell(row=(self.ws.max_row + 1),
                             column=1,
                             value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2, value=101)
            if int(trialtypes[t]) == 0:
                trialcode = "noncontR"
            elif int(trialtypes[t]) == 1:
                trialcode = 'contR'
            elif int(trialtypes[t]) == 2:
                trialcode = "noncontNR"
            else:
                trialcode = 'contNR'
            d = self.ws.cell(row=self.ws.max_row,
                             column=3,
                             value='trial_{}'.format(trialcode))  #trial type

            self.check_licking_1spout(self.duration_rec_on_before)

            #           main training program
            self.run_trial_type(int(trialtypes[t]), t)

            d = self.ws.cell(row=(self.ws.max_row + 1),
                             column=1,
                             value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2, value=100)
            self.wb.save(self.filename)

        odors_cue.initiate()
        odors_cue.close()
        self.waterR.low()
        self.waterR.close()

        print('FINISHED ASSOCIATION TRAINING')
    def run(self):
        """
        Runs when measurement is started. Runs in a separate thread from GUI.
        It should not update the graphical interface directly, and should only
        focus on data acquisition.
        """
        #         # first, create a data file
        #         if self.settings['save_h5']:
        #             # if enabled will create an HDF5 file with the plotted data
        #             # first we create an H5 file (by default autosaved to app.settings['save_dir']
        #             # This stores all the hardware and app meta-data in the H5 file
        #             self.h5file = h5_io.h5_base_file(app=self.app, measurement=self)
        #
        #             # create a measurement H5 group (folder) within self.h5file
        #             # This stores all the measurement meta-data in this group
        #             self.h5_group = h5_io.h5_create_measurement_group(measurement=self, h5group=self.h5file)
        #
        #             # create an h5 dataset to store the data
        #             self.buffer_h5 = self.h5_group.create_dataset(name  = 'buffer',
        #                                                           shape = self.buffer.shape,
        #                                                           dtype = self.buffer.dtype)

        # We use a try/finally block, so that if anything goes wrong during a measurement,
        # the finally block can clean things up, e.g. close the data file object.

        # self.wide_cam._dev.set_buffer_count(500)

        # if self.settings.save_video.value():
        #     save_dir = self.app.settings.save_dir.value()
        #     data_path = os.path.join(save_dir, self.app.settings.sample.value())
        #     try:
        #         os.makedirs(data_path)
        #     except OSError:
        #         print('directory already exists, writing to existing directory')
        #
        #     self.recorder.settings.path.update_value(data_path)

        # frame_rate = self.wide_cam.settings.frame_rate.value()
        # self.recorder.create_file('wide_mov',frame_rate)
        #
        #         # create a subthread and connect it to the interrupt subthread signal
        #         self.wide_disp_queue = queue.Queue(1000)
        #         self.camera_thread = SubMeasurementQThread(self.camera_action)
        #         self.interrupt_subthread.connect(self.camera_thread.interrupt)
        #         #start camera
        #         self.wide_cam.start()
        #
        #         #start camera subthread
        #         self.camera_thread.start()
        self.events_filename = '2019-7-17-test.xlsx'
        self.reward_odor_index = 0

        self.duration_rec_off = 6.5
        self.duration_rec_on_before = 4  # change this to exponential decay
        self.duration_odor_to_outcome = 1.3
        self.duration_water_large = 0.2
        self.duration_rec_on_after = 8
        self.duration_odor_on = 0.5

        self.lines = [0, 1, 2, 3]
        self.counter = np.zeros(4)

        self.numtrials = 200
        self.p_cont_noncont = 0.5
        self.p_USwCS = 0.5
        self.p_USwoCS = 0.5

        odors_cue = OdorGen(self.lines)
        odors_cue.assign_odor()
        self.reward_odor = odors_cue.set_rewardodor(
            index=self.reward_odor_index)
        odors_cue.initiate()
        # odors_cue.odors_DAQ[i]
        print('odor ready')

        self.waterR = DAQSimpleDOTask('Dev1/port0/line2')
        self.waterR.low()
        # self.OdorOnCopy = DAQSimpleDOTask('Dev3/port2/line5')
        # self.OdorOnCopy.low()
        self.lickR = DAQSimpleDITask('Dev2_SELECT/port1/line0')

        print('water ready')

        # create excel workbook
        self.wb = Workbook()
        self.ws = self.wb.active
        print('book ready')

        #generate trial type

        cont_reward = np.zeros(
            int(self.numtrials * self.p_cont_noncont * self.p_USwCS))  # code 0
        cont_noreward = np.ones(
            int(self.numtrials * self.p_cont_noncont *
                (1 - self.p_USwCS)))  # code 1
        temp_comb1 = np.concatenate((cont_reward, cont_noreward))

        noncont_reward = np.ones(
            int(self.numtrials *
                (1 - self.p_cont_noncont) * self.p_USwoCS)) * 2  # code 2
        noncont_noreward = np.ones(
            int(self.numtrials * (1 - self.p_cont_noncont) *
                (1 - self.p_USwoCS))) * 3  # code 3
        temp_comb2 = np.concatenate((noncont_noreward, noncont_reward))

        trialtypes = np.concatenate((temp_comb1, temp_comb2))
        random.shuffle(trialtypes)
        print('================== Trial Types =================')
        print(trialtypes)

        for t in range(0, self.numtrials):
            print('================================================')
            print('trial number: ', t)
            print()
            self.settings.in_trial.update_value(True)
            d = self.ws.cell(row=(self.ws.max_row + 1),
                             column=1,
                             value=time.clock())
            d = self.ws.cell(row=self.ws.max_row, column=2, value=101)
            # check lisking
            self.check_licking_1spout(self.duration_rec_on_before)

            #           main training program
            self.run_trial_type(int(trialtypes[t]))
            # check licking
            self.check_licking_1spout(self.duration_rec_on_after)
            self.settings.in_trial.update_value(False)
            # self.settings.save_video.update_value(False):

            self.wb.save(self.events_filename)

            self.check_licking_1spout(self.duration_rec_off)

            if self.interrupt_measurement_called:
                # tell subtherad to stop
                self.interrupt_subthread.emit()
                break

        odors_cue.initiate()
        odors_cue.close()
        self.waterR.low()
        self.waterR.close()
        print('FINISHED ASSOCIATION TRAINING')

        if self.settings.save_video.value:
            self.recorder.close()