コード例 #1
0
    def test_digital_data_shape(self):
        di = DAQ.DigitalInput('cDAQ1Mod2/port0/line0:1', 2, 1000, 3)
        di.DoTask()

        print(di.digitalData.shape)

        self.assertEqual(di.digitalData.shape[0] * di.digitalData.shape[1],
                         2 * 1000 * 3)
コード例 #2
0
def check_beam(device, channels, beam_channel):
    try:
        check = daq.ThreadSafeAnalogInput(device, channels, 1000, 0.1)
        analog_data = check.DoTask()

        check_mean = np.mean(analog_data[beam_channel])
        return check_mean < 1
    # TODO - need to check if this version is thread / repeat safe. Try/Except is for extra caution
    except:
        return False
コード例 #3
0
ファイル: QueueControl.py プロジェクト: dhb2128/PulseBoy
    def trial(self):
        while True:
            sleep(0.05)
            if self.parent.should_run:
                self.trial_start.emit()

                trial_params = self.experiment.arraydata[
                    self.experiment.current_trial][1]
                hardware_params = self.get_hardware_params()
                global_params = self.get_global_params()
                export_params = self.get_export_params()

                pulses, t = PulseInterface.make_pulse(
                    hardware_params['samp_rate'],
                    global_params['global_onset'],
                    global_params['global_offset'], trial_params)

                self.trial_daq = daq.DoAiMultiTask(
                    hardware_params['analog_dev'],
                    hardware_params['analog_channels'],
                    hardware_params['digital_dev'],
                    hardware_params['samp_rate'],
                    len(t) / hardware_params['samp_rate'], pulses,
                    hardware_params['sync_clock'])

                self.analog_data = self.trial_daq.DoTask()

                # Save data
                save_string = export_params['export_path'] + str(self.experiment.current_trial) + \
                              export_params['export_suffix'] + '.mat'
                sio.savemat(save_string, {
                    'analog_data': self.analog_data,
                    'pulses': pulses,
                    't': t
                })

                if self.experiment.total_trials(
                ) - self.experiment.current_trial == 1:
                    self.parent.should_run = False
                    self.experiment.reset_trials()

                if self.parent.should_run:
                    self.experiment.advance_trial()

                self.finished.emit()
コード例 #4
0
    def trial(self):
        while True:
            sleep(0.05)
            if self.parent.should_run:
                self.trial_start.emit()

                trial_params = self.experiment.arraydata[
                    self.experiment.current_trial][1]
                hardware_params = self.get_hardware_params()
                global_params = self.get_global_params()
                export_params = self.get_export_params()
                invert_valves = []
                if global_params['inverted_blank_off_state']:
                    invert_valves = global_params['inverted_blank_valves']
                pulses, t = PulseInterface.make_pulse(
                    hardware_params['samp_rate'],
                    global_params['global_onset'],
                    global_params['global_offset'],
                    trial_params,
                    invert_chan_list=invert_valves)

                if hardware_params['control_carrier']:
                    while len(pulses) < hardware_params['digital_channels']:
                        pulses = np.append(pulses,
                                           np.zeros((1, pulses.shape[1])),
                                           axis=0)
                    carrier_control = np.append(np.ones(pulses.shape[1] - 0),
                                                np.zeros(0))
                    pulses = np.append(pulses,
                                       carrier_control[np.newaxis],
                                       axis=0)
                # in standard configuration we want to run each trial sequentially
                if not self.parent.trigger_state():
                    if hardware_params['analog_channels'] > 0:
                        self.trial_daq = daq.DoAiMultiTask(
                            hardware_params['analog_dev'],
                            hardware_params['analog_channels'],
                            hardware_params['digital_dev'],
                            hardware_params['samp_rate'],
                            len(t) / hardware_params['samp_rate'], pulses,
                            hardware_params['sync_clock'])

                        self.analog_data = self.trial_daq.DoTask()
                    else:
                        self.trial_daq = daq.DoCoTask(
                            hardware_params['digital_dev'], '',
                            hardware_params['samp_rate'],
                            len(t) / hardware_params['samp_rate'], pulses)
                        self.trial_daq.DoTask()
                        # close_valves= daq.DoCoTask(hardware_params['digital_dev'], '', hardware_params['samp_rate'],
                        #                               len(t) / hardware_params['samp_rate'], np.zeros((len(pulses), 10)))
                        # close_valves.DoTask()
                        self.analog_data = []
                # unless the 'wait for trigger' box is checked, in which case we want to wait for our trigger in
                else:
                    if hardware_params['analog_channels'] > 0:
                        self.trial_daq = daq.DoAiTriggeredMultiTask(
                            hardware_params['analog_dev'],
                            hardware_params['analog_channels'],
                            hardware_params['digital_dev'],
                            hardware_params['samp_rate'],
                            len(t) / hardware_params['samp_rate'], pulses,
                            hardware_params['sync_clock'],
                            hardware_params['trigger_source'])

                        self.analog_data = self.trial_daq.DoTask()
                    else:
                        self.trial_daq = daq.DoTriggeredCoTask(
                            hardware_params['digital_dev'], '',
                            hardware_params['samp_rate'],
                            len(t) / hardware_params['samp_rate'], pulses,
                            hardware_params['trigger_source'])
                        self.trial_daq.DoTask()
                        self.analog_data = []

                # Save data
                if export_params['save_pulses']:
                    save_string = export_params['export_path'] + str(self.experiment.current_trial) + \
                                  export_params['pulse_suffix'] + '.mat'
                    sio.savemat(save_string, {
                        'analog_data': self.analog_data,
                        'pulses': pulses,
                        't': t
                    })

                if self.experiment.total_trials(
                ) - self.experiment.current_trial == 1:
                    self.experiment.reset_trials()
                    if export_params['save_names']:
                        names = [i[-1] for i in self.experiment.arraydata]
                        date = datetime.today().strftime('%Y-%m-%d')
                        time = datetime.today().strftime('%H:%M:%S')
                        f = open(
                            export_params['export_path'] + date +
                            export_params['trial_suffix'] + '.txt', 'a')
                        f.write(time)
                        f.write('\n')
                        f.write('\n'.join(names))
                        f.write('\n')
                        f.close()
                    self.parent.repeats_done += 1
                    print('repeats done ', self.parent.repeats_done)
                    if self.parent.repeats_done == global_params['repeats']:
                        self.parent.should_run = False
                        self.parent.repeats_done = 0
                    else:
                        if global_params['shuffle_repeats']:
                            self.experiment.randomise_trials(global_params)

                elif self.parent.should_run:
                    self.experiment.advance_trial()

                self.finished.emit()
コード例 #5
0
 def reset_all_chans(self):
     daq.closeValves(self.get_hardware_params()['digital_dev'])
コード例 #6
0
    def test_digital_input(self):
        di = DAQ.DigitalInput('Dev6/port0/line0', 1, 1000, 1)
        di.DoTask()

        self.assertEqual(np.sum(di.digitalData[0]), 0)
コード例 #7
0

def load_pkl(filename):
    with open(filename, 'rb') as fn:
        data = pickle.load(fn)
    return data


# file names
trial_file = 'PulseBoy/TrialBanks/PWMTest.trialbank'
config_file = 'PulseBoy/params.config'

config = load_pkl(config_file)
hardware_params = config['hardware_params']
global_params = config['global_params']

arraydata = load_pkl(trial_file)
trial_params = arraydata[0][1]

pulses, t = PulseInterface.make_pulse(hardware_params['samp_rate'],
                                      global_params['global_onset'],
                                      global_params['global_offset'],
                                      trial_params)

trial_daq = DAQ.DigitalOut(hardware_params['digital_dev'],
                           hardware_params['samp_rate'],
                           len(t) / hardware_params['samp_rate'], pulses,
                           hardware_params['sync_clock'])

trial_daq.DoTask()
コード例 #8
0
    def trial(self):
        while self.parent.should_run:
            start = time()
            """ Is there an animal present? """
            if self.animal_present():
                """ Check which animal is present and get a reference to it """
                animal = self.get_present_animal()
                """ Look up current trial for this mouse """
                current_trial = animal.current_trial()
                """ Parse this trial into a set of DAQ commands """
                current_trial_pulse = animal.current_trial_pulse()
                pulses, t = PulseInterface.make_pulse(
                    self.hardware_prefs['samp_rate'], 0.0, 0.0,
                    current_trial_pulse)
                """ Send the data to the DAQ """
                trial_daq = daq.DoAiMultiTask(
                    self.hardware_prefs['analog_input'],
                    self.hardware_prefs['analog_channels'],
                    self.hardware_prefs['digital_output'],
                    self.hardware_prefs['samp_rate'],
                    len(t) / self.hardware_prefs['samp_rate'], pulses,
                    self.hardware_prefs['sync_clock'])

                analog_data = trial_daq.DoTask()
                lick_data = analog_data[self.hardware_prefs['lick_channel']]
                self.experiment.last_data = lick_data
                """ Analyse the lick response """
                rewarded = current_trial[0]
                # TODO - reference to rewarded (current_trial[0]) and lick fraction are bug prone here.
                # TODO - A little too inflexible
                response = TrialConditions.lick_detect(
                    lick_data, 2,
                    float(current_trial_pulse[0]['lick_fraction']))
                result, correct, timeout = TrialConditions.trial_result(
                    rewarded, response)
                """ Update database """
                timestamp = datetime.datetime.now()
                animal.schedule_list[
                    animal.current_schedule_idx].add_trial_data(
                        timestamp, response, correct, timeout)
                self.experiment.add_trial(animal.id, timestamp,
                                          animal.current_schedule_idx,
                                          animal.current_trial_idx, rewarded,
                                          response, correct, timeout)
                """ Determine reward conditions and enact """
                if result == TrialConditions.TrialResult.correct_response:
                    self.reward(animal)
                elif result == TrialConditions.TrialResult.false_alarm:
                    self.timeout()
                """ Advance animal to next trial """
                animal.advance_trial()
                """ Save bulkiest part of data to disk and save experiment if necessary """
                self.save_data(animal.id, timestamp, analog_data, rewarded,
                               response, correct, timeout, pulses, t)
                if len(self.experiment.trials) % 20 == 0:
                    self.experiment.save()
                """ Signal that trial has finished """
                print(time() - start)
                self.trial_end.emit()

            sleep(3.0)

        self.finished.emit()