コード例 #1
0
 def setup(self):
     super().setup()  # this sets number of rewards and intervals
     self.buzz_pin = int(
         self.configDict.get('buzz_pin',
                             AHF_Stimulator_Buzzer.buzz_pin_def))
     self.buzz_lead = float(
         self.configDict.get('buzz_lead',
                             AHF_Stimulator_Buzzer.buzz_lead_def))
     self.buzz_num = int(
         self.configDict.get('buzz_num',
                             AHF_Stimulator_Buzzer.buzz_num_def))
     self.buzz_len = float(
         self.configDict.get('buzz_len',
                             AHF_Stimulator_Buzzer.buzz_len_def))
     self.buzz_period = float(
         self.configDict.get('buzz_period',
                             AHF_Stimulator_Buzzer.buzz_period_def))
     self.buzzer = Train(PTSimpleGPIO.INIT_PULSES, self.buzz_len,
                         (self.buzz_period - self.buzz_len), self.buzz_num,
                         self.buzz_pin,
                         PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
     self.configDict.update({
         'buzz_pin': self.buzz_pin,
         'buzz_lead': self.buzz_lead,
         'buzz_num': self.buzz_num
     })
     self.configDict.update({
         'buzz_len': self.buzz_len,
         "buzz_period": self.buzz_period
     })
コード例 #2
0
    def __init__(self, configDict, rewarder, lickDetector, textfp):
        super().__init__(configDict, rewarder, lickDetector, textfp)
        self.buzz_pulseProb = float(
            self.configDict.get(
                'buzz_pulseProb',
                AHF_Stimulator_LickNoLickSpeaker.buzz_pulseProb_def))
        self.speakerPin = int(
            self.configDict.get(
                'speaker_pin',
                AHF_Stimulator_LickNoLickSpeaker.speakerPin_def))
        self.speakerFreq = float(
            self.configDict.get(
                'speaker_freq',
                AHF_Stimulator_LickNoLickSpeaker.speakerFreq_def))
        self.speakerDuty = float(
            self.configDict.get(
                'speaker_duty',
                AHF_Stimulator_LickNoLickSpeaker.speakerDuty_def))
        self.speakerOffForReward = float(
            self.configDict.get(
                'speaker_OffForReward',
                AHF_Stimulator_LickNoLickSpeaker.speakerOffForReward_def))
        self.speaker = Infinite_train(PTSimpleGPIO.MODE_FREQ, self.speakerPin,
                                      self.speakerFreq, self.speakerDuty,
                                      PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
        self.pulseDelay = self.buzz_period - self.buzz_len
        self.pulseDuration = (self.buzz_period *
                              self.buzz_num) - self.pulseDelay
        # make a second train, we already have self.buzzer for pulsed version, add self.buzzer1 for single pulse version
        self.buzzer1 = Train(PTSimpleGPIO.MODE_PULSES, self.buzz_pin, 0,
                             self.pulseDelay, self.pulseDuration, 1,
                             PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
        self.configDict.update({
            'speaker_pin': self.speakerPin,
            'speaker_duty': self.speakerDuty,
            'speaker_freq': self.speakerFreq
        })
        self.configDict.update({
            'speaker_OffForReward': self.speakerOffForReward,
            'buzz_pulseProb': self.buzz_pulseProb
        })
        print("buzzlead=", self.buzz_lead)

        self.posArray = array('B', [0] * 76)
        self.posArray[75] = 249
        now = datetime.fromtimestamp(int(time()))
        startDay = datetime(now.year, now.month, now.day, kDAYSTARTHOUR, 0, 0)
        filename = '/home/pi/Documents/Lever_' + str(
            startDay.year) + '_' + '{:02}'.format(
                startDay.month) + '_' + '{:02}'.format(startDay.day)
        self.outFile = open(filename, 'ab')
        self.lever = leverThread.new(self.posArray, 25, 0, 0)
        #leverThread.zeroLever(self.lever, 0, 1)
        #leverThread.setConstForce (self.lever, 1000)
        #leverThread.applyForce (self.lever, -1)
        leverThread.setHoldParams(self.lever, 10, 250, 50)
コード例 #3
0
 def __init__ (self, configDict, rewarder, lickDetector,textfp):
     super().__init__(configDict, rewarder, lickDetector, textfp)
     self.headFixTime = float (self.configDict.get ('headFixTime', AHF_Stimulator_LickNoLick.headFixTime_def))
     print (self.headFixTime)
     self.lickWitholdTime = float (self.configDict.get ('lickWitholdTime', AHF_Stimulator_LickNoLick.lickWitholdTime_def))
     self.buzz_pin = int(self.configDict.get ('buzz_pin', AHF_Stimulator_LickNoLick.buzz_pin_def))
     self.buzz_lead = float (self.configDict.get ('buzz_lead', AHF_Stimulator_LickNoLick.buzz_lead_def))
     self.buzz_num = int (self.configDict.get ('buzz_num', AHF_Stimulator_LickNoLick.buzz_num_def))
     self.buzz_len = float (self.configDict.get ('buzz_len', AHF_Stimulator_LickNoLick.buzz_len_def))
     self.buzz_period = float (self.configDict.get ('buzz_period', AHF_Stimulator_LickNoLick.buzz_period_def))
     print (self.buzz_period, self.buzz_len, self.buzz_num)
     self.buzzer=Train (PTSimpleGPIO.INIT_PULSES, self.buzz_len, (self.buzz_period - self.buzz_len), self.buzz_num, self.buzz_pin, PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
     self.configDict.update({'headFixTime' : self.headFixTime, 'lickWitholdTime' : self.lickWitholdTime})
     self.configDict.update({'buzz_pin' : self.buzz_pin, 'buzz_lead' : self.buzz_lead, 'buzz_num' : self.buzz_num})
     self.configDict.update({'buzz_len' : self.buzz_len, "buzz_period" : self.buzz_period})
コード例 #4
0
 def __init__(self, configDict, rewarder, lickDetector, textfp):
     super().__init__(configDict, rewarder, lickDetector, textfp)
     self.buzz_pulseProb = float(
         self.configDict.get(
             'buzz_pulseProb',
             AHF_Stimulator_LickNoLickSpeaker.buzz_pulseProb_def))
     self.speakerPin = int(
         self.configDict.get(
             'speaker_pin',
             AHF_Stimulator_LickNoLickSpeaker.speakerPin_def))
     self.speakerFreq = float(
         self.configDict.get(
             'speaker_freq',
             AHF_Stimulator_LickNoLickSpeaker.speakerFreq_def))
     self.speakerDuty = float(
         self.configDict.get(
             'speaker_duty',
             AHF_Stimulator_LickNoLickSpeaker.speakerDuty_def))
     self.speakerOffForReward = float(
         self.configDict.get(
             'speaker_OffForReward',
             AHF_Stimulator_LickNoLickSpeaker.speakerOffForReward_def))
     self.afterStimWitholdTime = float(
         self.configDict.get(
             'after_Stim_Withold_Time',
             AHF_Stimulator_LickNoLickSpeaker.afterStimWitholdTime_def))
     self.speaker = Infinite_train(PTSimpleGPIO.MODE_FREQ, self.speakerPin,
                                   self.speakerFreq, self.speakerDuty,
                                   PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
     self.pulseDelay = self.buzz_period - self.buzz_len
     self.pulseDuration = (self.buzz_period *
                           self.buzz_num) - self.pulseDelay
     # make a second train, we already have self.buzzer for pulsed version, add self.buzzer1 for single pulse version
     self.buzzer1 = Train(PTSimpleGPIO.MODE_PULSES, self.buzz_pin, 0,
                          self.pulseDelay, self.pulseDuration, 1,
                          PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
     self.configDict.update({
         'speaker_pin': self.speakerPin,
         'speaker_duty': self.speakerDuty,
         'speaker_freq': self.speakerFreq
     })
     self.configDict.update({
         'speaker_OffForReward': self.speakerOffForReward,
         'buzz_pulseProb': self.buzz_pulseProb
     })
     print("buzzlead=", self.buzz_lead)
コード例 #5
0
 def setup(self):
     super().setup()
     self.lickWrongTimeout = float(
         self.configDict.get(
             'lickWrongTimeout',
             AHF_Stimulator_LickNoLickDisc.lickWrongTimeout_def))
     self.buzz_pulseProb = float(
         self.configDict.get(
             'buzz_pulseProb',
             AHF_Stimulator_LickNoLickDisc.buzz_pulseProb_def))
     self.pulseDelay = self.buzz_period - self.buzz_len
     print(self.pulseDelay, self.buzz_period, self.buzz_len)
     self.pulseDuration = (self.buzz_period *
                           self.buzz_num) - self.pulseDelay
     # make a second train, we already have self.buzzer for pulsed version, add self.buzzer1 for single pulse version
     self.buzzer1 = Train(PTSimpleGPIO.INIT_PULSES, self.pulseDuration,
                          self.pulseDelay, 1, self.buzz_pin,
                          PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
コード例 #6
0
ファイル: AHF_Camera.py プロジェクト: jamieboyd/AutoHeadFix
        tempInput = input(
            'Set white balancing for video, 1 for True, or 0 for Flase (currently '
            + str(whiteBalance) + ') to :')
        if tempInput != '':
            tempInput = bool(int(tempInput))
        paramDict.update({'whiteBalance': whiteBalance})
        # return already modified dictionary, needed when making a new dictionary
        return paramDict


if __name__ == '__main__':
    import time
    import PTSimpleGPIO
    from PTSimpleGPIO import PTSimpleGPIO, Pulse, Train, Infinite_train

    t1 = Train(PTSimpleGPIO.INIT_FREQ, 14.4, 0.5, 30, 17,
               PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)

    videoFormat = 'rgb'
    quality = 0
    resolution = (256, 256)
    frameRate = 30
    iso = 0
    whiteBalance = True
    previewWin = (0, 0, 256, 256)
    #userDict = AHF_Camera.dict_from_user ({})
    #camera=AHF_Camera (userDict)
    camera = AHF_Camera({
        'format': videoFormat,
        'quality': quality,
        'resolution': resolution,
        'iso': iso,
コード例 #7
0
class AHF_Stimulator_LickNoLickDisc(AHF_Stimulator_LickNoLick):
    lickWrongTimeout_def = 2
    buzz_pulseProb_def = 0.5

    def __init__(self, configDict, rewarder, lickDetector, textfp):
        super().__init__(configDict, rewarder, lickDetector, textfp)
        self.setup()

    def setup(self):
        super().setup()
        self.lickWrongTimeout = float(
            self.configDict.get(
                'lickWrongTimeout',
                AHF_Stimulator_LickNoLickDisc.lickWrongTimeout_def))
        self.buzz_pulseProb = float(
            self.configDict.get(
                'buzz_pulseProb',
                AHF_Stimulator_LickNoLickDisc.buzz_pulseProb_def))
        self.pulseDelay = self.buzz_period - self.buzz_len
        print(self.pulseDelay, self.buzz_period, self.buzz_len)
        self.pulseDuration = (self.buzz_period *
                              self.buzz_num) - self.pulseDelay
        # make a second train, we already have self.buzzer for pulsed version, add self.buzzer1 for single pulse version
        self.buzzer1 = Train(PTSimpleGPIO.INIT_PULSES, self.pulseDuration,
                             self.pulseDelay, 1, self.buzz_pin,
                             PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)

    @staticmethod
    def dict_from_user(stimDict):
        if not 'lickWrongTimeout' in stimDict:
            stimDict.update({
                'lickWrongTimeout':
                AHF_Stimulator_LickNoLickDisc.lickWrongTimeout_def
            })
        if not 'buzz_pulseProb' in stimDict:
            stimDict.update({
                'buzz_pulseProb':
                AHF_Stimulator_LickNoLickDisc.buzz_pulseProb_def
            })
        return super(AHF_Stimulator_LickNoLickDisc,
                     AHF_Stimulator_LickNoLickDisc).dict_from_user(stimDict)

    def run(self):
        """
        every time lickWitholdtime passes with no licks, make a buzz or buzz train, then monitor for licking.
        """
        self.buzzTimes = []
        self.buzzTypes = []
        self.rewardTimes = []
        endTime = time() + self.headFixTime
        adjustedWitholdTime = self.lickWitholdTime
        while time() < endTime:
            anyLicks = self.lickDetector.waitForLick_Soft(adjustedWitholdTime,
                                                          startFromZero=True)
            if anyLicks == 0:  # successfully waited for withold time
                buzzLeadEnd = time() + self.buzz_lead
                self.buzzTimes.append(time())
                if random(
                ) < self.buzz_pulseProb:  # set up for pulses that get rewarded
                    self.buzzer1.do_train()
                    anyLicks = self.lickDetector.waitForLick_Soft(
                        self.buzz_lead, startFromZero=True)
                    if anyLicks > 0:  # licked when was supposed to lick
                        print('licked when was supposed to lick')
                        if buzzLeadEnd > time() + 0.05:
                            sleep(buzzLeadEnd - time())
                        self.rewardTimes.append(time())
                        self.rewarder.giveReward('task')
                        self.buzzTypes.append(2)
                    else:  # failed to lick when should have licked
                        print('failed to lick when should have licked')
                        self.buzzTypes.append(-2)
                        sleep(self.lickWrongTimeout)
                    adjustedWitholdTime = self.lickWitholdTime
                else:  # set up for constant buzz that does NOT get rearded
                    self.buzzer1.do_train()
                    anyLicks = self.lickDetector.waitForLick_Soft(
                        self.buzz_lead, startFromZero=True)
                    if anyLicks == 0:  #refrained from licking success
                        print(
                            'refrained from licking when supposed to refrain')
                        self.buzzTypes.append(1)
                        adjustedWitholdTime = self.lickWitholdTime - self.buzz_lead  # slightly less wait time, that's your reward
                    else:  # licked when should not have licked, time out
                        print('licked when should not have licked')
                        sleep(buzzLeadEnd - time() + self.lickWrongTimeout)
                        self.buzzTypes.append(-1)
                        adjustedWitholdTime = self.lickWitholdTime
                        sleep(self.lickWrongTimeout)

    def logfile(self):
        rewardStr = 'reward'
        buzz2Str = 'Buzz:N=' + str(
            self.buzz_num) + ',length=' + '{:.2f}'.format(
                self.buzz_len) + ',period=' + '{:.2f}'.format(self.buzz_period)
        buzz1Str = 'Buzz:N=1,length=' + '{:.2f}'.format(
            self.pulseDuration) + ',period=' + '{:.2f}'.format(
                self.pulseDuration + self.pulseDelay)
        mStr = '{:013}'.format(self.mouse.tag)
        iReward = 0
        for i in range(0, len(self.buzzTypes)):
            if self.buzzTypes[i] == 2:  # only rewarded condition
                outPutStr = mStr + '\t' + datetime.fromtimestamp(
                    int(self.rewardTimes[iReward])).isoformat(
                        ' ') + '\t' + rewardStr
                print(outPutStr)
                iReward += 1
                buzzStr = buzz2Str + ',GO=2'
            elif self.buzzTypes[i] == -2:
                buzzStr = buzz2Str + ',GO=-2'
            elif self.buzzTypes[i] == 1:
                buzzStr = buzz1Str + ',GO=1'
            elif self.buzzTypes[i] == -1:
                buzzStr = buzz1Str + ',GO=-1'
            outPutStr = mStr + '\t' + datetime.fromtimestamp(
                int(self.buzzTimes[i])).isoformat(' ') + '\t' + buzzStr
            print(outPutStr)

        if self.textfp != None:
            iReward = 0
            for i in range(0, len(self.buzzTypes)):
                if self.buzzTypes[i] == 2:  # only rewarded condition
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.rewardTimes[iReward]
                    ) + '\t' + rewardStr + '\t' + datetime.fromtimestamp(
                        int(self.rewardTimes[iReward])).isoformat(' ') + '\n'
                    self.textfp.write(outPutStr)
                    iReward += 1
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + buzz2Str + ',GO=2' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == -2:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + buzz2Str + ',GO=-2' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == 1:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + buzz1Str + ',GO=1' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == -1:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + buzz1Str + ',GO=-1' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                self.textfp.write(outPutStr)
            self.textfp.flush()
コード例 #8
0
class AHF_Stimulator_LickNoLickSpeaker(AHF_Stimulator_LickNoLick):
    speakerPin_def = 17
    speakerFreq_def = 300
    speakerDuty_def = 0.8
    speakerOffForReward_def = 1.5  #time for consuming reward withot getting buzzed at
    buzz_pulseProb_def = 0.5

    def __init__(self, configDict, rewarder, lickDetector, textfp):
        super().__init__(configDict, rewarder, lickDetector, textfp)
        self.buzz_pulseProb = float(
            self.configDict.get(
                'buzz_pulseProb',
                AHF_Stimulator_LickNoLickSpeaker.buzz_pulseProb_def))
        self.speakerPin = int(
            self.configDict.get(
                'speaker_pin',
                AHF_Stimulator_LickNoLickSpeaker.speakerPin_def))
        self.speakerFreq = float(
            self.configDict.get(
                'speaker_freq',
                AHF_Stimulator_LickNoLickSpeaker.speakerFreq_def))
        self.speakerDuty = float(
            self.configDict.get(
                'speaker_duty',
                AHF_Stimulator_LickNoLickSpeaker.speakerDuty_def))
        self.speakerOffForReward = float(
            self.configDict.get(
                'speaker_OffForReward',
                AHF_Stimulator_LickNoLickSpeaker.speakerOffForReward_def))
        self.speaker = Infinite_train(PTSimpleGPIO.INIT_FREQ, self.speakerFreq,
                                      self.speakerDuty, self.speakerPin,
                                      PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
        self.pulseDelay = self.buzz_period - self.buzz_len
        self.pulseDuration = (self.buzz_period *
                              self.buzz_num) - self.pulseDelay
        # make a second train, we already have self.buzzer for pulsed version, add self.buzzer1 for single pulse version
        self.buzzer1 = Train(PTSimpleGPIO.INIT_PULSES, self.pulseDuration,
                             self.pulseDelay, 1, self.buzz_pin,
                             PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
        self.configDict.update({
            'speaker_pin': self.speakerPin,
            'speaker_duty': self.speakerDuty,
            'speaker_freq': self.speakerFreq
        })
        self.configDict.update({
            'speaker_OffForReward': self.speakerOffForReward,
            'buzz_pulseProb': self.buzz_pulseProb
        })
        print("buzzlead=", self.buzz_lead)

    @staticmethod
    def dict_from_user(stimDict):
        if not 'speaker_pin' in stimDict:
            stimDict.update({
                'speaker_pin':
                AHF_Stimulator_LickNoLickSpeaker.speakerPin_def
            })
        if not 'speaker_freq' in stimDict:
            stimDict.update({
                'speaker_freq':
                AHF_Stimulator_LickNoLickSpeaker.speakerFreq_def
            })
        if not 'speaker_duty' in stimDict:
            stimDict.update({
                'speaker_duty':
                AHF_Stimulator_LickNoLickSpeaker.speakerduty_def
            })
        if not 'speaker_OffForReward' in stimDict:
            stimDict.update({
                'speaker_OffForReward':
                AHF_Stimulator_LickNoLickSpeaker.speakerOffForReward_def
            })
        if not 'buzz_pulseProb' in stimDict:
            stimDict.update({
                'buzz_pulseProb':
                AHF_Stimulator_LickNoLickSpeaker.buzz_pulseProb_def
            })
        return super(AHF_Stimulator_LickNoLickSpeaker,
                     AHF_Stimulator_LickNoLickSpeaker).dict_from_user(stimDict)

    def run(self):
        """
        every time lickWitholdtime passes with no licks, make a buzz then give a reward after buzz_lead time.
        turn on speaker 
        """
        self.buzzTimes = []
        self.buzzTypes = []
        self.lickWitholdTimes = []
        self.rewardTimes = []
        endTime = time() + self.headFixTime
        speakerIsOn = False
        OffForRewardEnd = 0.0
        # outer loop runs trials until time is up
        while time() < endTime:
            # setup to start a trial, witholding licking for lickWitholdRandom secs till buzzer
            lickWitholdRandom = self.lickWitholdTime + (0.5 - random())
            lickWitholdEnd = time() + lickWitholdRandom
            # inner loop keeps resetting lickWitholdEnd time until  a succsful withhold
            while time() < lickWitholdEnd and time() < endTime:
                anyLicks = self.lickDetector.waitForLick_Soft(0.05)
                if anyLicks == 0:
                    if speakerIsOn == True:
                        self.speaker.stop_train()
                        speakerIsOn = False
                else:  # there were licks in witholding period
                    if (speakerIsOn == False) and (time() > OffForRewardEnd):
                        self.speaker.start_train()
                        speakerIsOn = True
                    lickWitholdRandom = self.lickWitholdTime + (0.5 - random())
                    lickWitholdEnd = time() + lickWitholdRandom
            # while loop only exits if trial time is up or lick witholding time passed with no licking
            if anyLicks > 0:
                break
            # at this point, mouse has just witheld licking for lickWitholdTime
            self.lickWitholdTimes.append(lickWitholdRandom)
            # Give a buzz and monitor for no licking in next 0.75 secs
            self.buzzTimes.append(time())
            afterBuzzEndTime = time() + 0.75
            buzzLeadEnd = afterBuzzWaitTime + self.buzz_lead
            if random(
            ) < self.buzz_pulseProb:  # set up for pulses that get rewarded
                trialType = 2
                self.buzzer.do_train()
            else:
                trialType = 1
                self.buzzer1.do_train()
            # wait for licks - there shouldn't be any for 0.75 secods after start of train
            anyLicks = self.lickDetector.waitForLick_Soft(0.75)
            if anyLicks > 0:  # licked before 0.75 second after buzzer wait period
                self.speaker.start_train()  # turn on speaker and start over
                speakerIsOn = True
                if trialType == 2:
                    self.buzzTypes.append(-4)
                else:
                    self.buzzTypes.append(-3)
                continue
            else:  # animal waited for 0.75 seconds after start of buzzer
                # wait for licks. Animal SHOULD lick for trialType = 2, SHOULD NOT lick for trialType = 1
                anyLicks = self.lickDetector.waitForLick_Soft(self.buzz_lead)
                if trialType == 2:  #
                    if anyLicks > 0:  # licked when was supposed to lick
                        if time() < buzzLeadEnd:
                            sleep(
                                buzzLeadEnd -
                                time())  # wait out duration of buzz lead time
                        self.rewardTimes.append(time())
                        self.rewarder.giveReward('task')
                        self.buzzTypes.append(2)
                        OffForRewardEnd = time() + self.speakerOffForReward
                    else:  #did not lick when was supposed to lick
                        self.buzzTypes.append(-2)
                else:  # we gave a NO-LICK trial
                    if anyLicks > 0:  # licked when was NOT supposed to lick
                        self.buzzTypes.append(-1)
                        self.speaker.start_train()
                        speakerIsOn = True
                    else:  # did not lick when was not supposed to lick
                        self.buzzTypes.append(1)
        # make sure to turn off buzzer at end of loop when we exit
        if speakerIsOn == True:
            self.speaker.stop_train()

    def logfile(self):

        rewardStr = 'reward'
        buzz2Str = 'Buzz:N=' + str(
            self.buzz_num) + ',length=' + '{:.2f}'.format(
                self.buzz_len) + ',period=' + '{:.2f}'.format(self.buzz_period)
        buzz1Str = 'Buzz:N=1,length=' + '{:.2f}'.format(
            self.pulseDuration) + ',period=' + '{:.2f}'.format(
                self.pulseDuration + self.pulseDelay)
        mStr = '{:013}'.format(self.mouse.tag)
        iReward = 0
        for i in range(0, len(self.buzzTypes)):
            lickWitholdStr = 'lickWitholdTime=' + '{:.2f}'.format(
                self.lickWitholdTimes[i]) + ','
            if self.buzzTypes[i] == 2:  # only rewarded condition
                outPutStr = mStr + '\t' + datetime.fromtimestamp(
                    int(self.rewardTimes[iReward])).isoformat(
                        ' ') + '\t' + rewardStr
                print(outPutStr)
                iReward += 1
                buzzStr = buzz2Str + ',GO=2'
            elif self.buzzTypes[i] == -2:
                buzzStr = buzz2Str + ',GO=-2'
            elif self.buzzTypes[i] == 1:
                buzzStr = buzz1Str + ',GO=1'
            elif self.buzzTypes[i] == -1:
                buzzStr = buzz1Str + ',GO=-1'
            elif self.buzzTypes[i] == -3:
                buzzStr = buzz1Str + ',GO=-3'
            elif self.buzzTypes[i] == -4:
                buzzStr = buzz1Str + ',GO=-4'

            outPutStr = mStr + '\t' + datetime.fromtimestamp(
                int(self.buzzTimes[i])).isoformat(
                    ' ') + '\t' + lickWitholdStr + buzzStr
            print(outPutStr)

        if self.textfp != None:
            iReward = 0
            for i in range(0, len(self.buzzTypes)):
                lickWitholdStr = 'lickWitholdTime=' + '{:.2f}'.format(
                    self.lickWitholdTimes[i]) + ','
                if self.buzzTypes[i] == 2:  # only rewarded condition
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.rewardTimes[iReward]
                    ) + '\t' + rewardStr + '\t' + datetime.fromtimestamp(
                        int(self.rewardTimes[iReward])).isoformat(' ') + '\n'
                    self.textfp.write(outPutStr)
                    iReward += 1
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + lickWitholdStr + buzz2Str + ',GO=2' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == -2:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + lickWitholdStr + buzz2Str + ',GO=-2' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == 1:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + lickWitholdStr + buzz1Str + ',GO=1' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == -1:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + lickWitholdStr + buzz1Str + ',GO=-1' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == -3:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + lickWitholdStr + buzz2Str + ',GO=-3' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'
                elif self.buzzTypes[i] == -4:
                    outPutStr = mStr + '\t' + '{:.2f}'.format(
                        self.buzzTimes[i]
                    ) + '\t' + lickWitholdStr + buzz2Str + ',GO=-4' + '\t' + datetime.fromtimestamp(
                        int(self.buzzTimes[i])).isoformat(' ') + '\n'

                self.textfp.write(outPutStr)
            self.textfp.flush()
コード例 #9
0
class AHF_Stimulator_Buzzer(AHF_Stimulator_Rewards):
    buzz_pin_def = 18
    buzz_lead_def = 0.5
    buzz_num_def = 2
    buzz_len_def = 0.1
    buzz_period_def = 0.2

    def __init__(self, configDict, rewarder, lickDetector, textfp):
        super().__init__(configDict, rewarder, lickDetector, textfp)
        self.setup()

    def setup(self):
        super().setup()  # this sets number of rewards and intervals
        self.buzz_pin = int(
            self.configDict.get('buzz_pin',
                                AHF_Stimulator_Buzzer.buzz_pin_def))
        self.buzz_lead = float(
            self.configDict.get('buzz_lead',
                                AHF_Stimulator_Buzzer.buzz_lead_def))
        self.buzz_num = int(
            self.configDict.get('buzz_num',
                                AHF_Stimulator_Buzzer.buzz_num_def))
        self.buzz_len = float(
            self.configDict.get('buzz_len',
                                AHF_Stimulator_Buzzer.buzz_len_def))
        self.buzz_period = float(
            self.configDict.get('buzz_period',
                                AHF_Stimulator_Buzzer.buzz_period_def))
        self.buzzer = Train(PTSimpleGPIO.INIT_PULSES, self.buzz_len,
                            (self.buzz_period - self.buzz_len), self.buzz_num,
                            self.buzz_pin,
                            PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
        self.configDict.update({
            'buzz_pin': self.buzz_pin,
            'buzz_lead': self.buzz_lead,
            'buzz_num': self.buzz_num
        })
        self.configDict.update({
            'buzz_len': self.buzz_len,
            "buzz_period": self.buzz_period
        })

    @staticmethod
    def dict_from_user(stimDict):
        if not 'buzz_pin' in stimDict:
            stimDict.update({'buzz_pin': AHF_Stimulator_Buzzer.buzz_pin_def})
        if not 'buzz_lead' in stimDict:
            stimDict.update({'buzz_lead': AHF_Stimulator_Buzzer.buzz_lead_def})
        if not 'buzz_num' in stimDict:
            stimDict.update({'buzz_num': AHF_Stimulator_Buzzer.buzz_num_def})
        if not 'buzz_len' in stimDict:
            stimDict.update({'buzz_len': AHF_Stimulator_Buzzer.buzz_len_def})
        if not 'buzz_period' in stimDict:
            stimDict.update(
                {'buzz_period': AHF_Stimulator_Buzzer.buzz_period_def})
        return super(AHF_Stimulator_Buzzer,
                     AHF_Stimulator_Buzzer).dict_from_user(stimDict)

    def configStim(self, mouse):
        self.timeInterval = self.rewardInterval - self.rewarder.rewardDict.get(
            'task')
        doPulses = array(
            'i', (round(random())
                  for i in range(0, self.nRewards)))  # 50% chance of 1 or 0
        self.rewardTimes = []
        self.buzzTimes = []
        stimStr = 'stim'
        for value in doPulses:
            stimStr += str(value)
        return stimStr

    def run(self):
        """
        do a buzz sleep for lead time - buzz time. give a reward. sleep for reward interval - reward time - buzz lead time
        """
        #buzzTime = self.buzz_period * self.buzz_num
        #buzzInterSleep = self.buzz_period - self.buzz_len
        #buzzLeadSleep = self.buzz_lead - buzzTime

        for reward in range(self.nRewards):
            self.buzzTimes.append(time())
            self.buzzer.do_train()
            sleep(buzzLeadSleep)
            self.rewardTimes.append(time())
            self.rewarder.giveReward('task')
            sleep(timeInterval)
        self.mouse.headFixRewards += self.nRewards

    def logfile(self):
        super().logfile()
        event = '\tBuzz:N=' + str(self.buzz_num) + 'length=' + str(
            self.buzz_len) + ',period=' + str(self.buzz_period)
        mStr = '{:013}'.format(self.mouse.tag) + '\t'
        for buzzTime in self.buzzTimes:
            outPutStr = mStr + datetime.fromtimestamp(
                int(buzzTime)).isoformat(' ') + event
            print(outPutStr)
        if self.textfp != None:
            for buzzTime in self.buzzTimes:
                outPutStr = mStr + datetime.fromtimestamp(
                    int(buzzTime)).isoformat(' ') + "\t" + '{:.2f}'.format(
                        buzzTime) + event
                self.textfp.write(outPutStr + '\n')
            self.textfp.flush()
コード例 #10
0
ファイル: SimpleGPIO_test.py プロジェクト: tf4ong/GPIO_Thread
    def endFunc(self, *args):
        #trainFrequency=args[0]
        #trainDutyCycle=args[1]
        #trainDuration=args[2]
        #pulseDelay = args[3]
        #pulseDur=args[4]
        #nPulses = args[5]
        ptSimpleGPIO.modTrainDuty(
            self.task_ptr, self.periodArray[(self.iArray % self.period)])
        self.iArray += 1


if __name__ == '__main__':
    import ptSimpleGPIO
    t1Pin = 23
    t1 = Train(PTSimpleGPIO.MODE_FREQ, t1Pin, 0, 110, 0.5, 0.5,
               PTSimpleGPIO.ACC_MODE_SLEEPS_AND_OR_SPINS)
    try:
        scaler = MajorScale(t1.task_ptr)
        #t1.set_endFunc_obj (PTSimpleGPIO.MODE_FREQ, 0, addObj = scaler)
        ptSimpleGPIO.setEndFuncObj(t1.task_ptr, scaler, PTSimpleGPIO.MODE_FREQ,
                                   0)
        t1.do_trains(21)
    except Exception as e:
        print(str(e))
    finally:
        del (t1)
        del (scaler)
"""
a1 = array('f', (0.01*i for i in range (0, 100)))
t1.set_array_endFunc (a1, 0, 0)
t1.add_endFunc (scaler)
コード例 #11
0
class AHF_Stimulator_LickNoLick(AHF_Stimulator):
    headFixTime_def = 30
    lickWitholdTime_def = 1
    buzz_pin_def = 27
    buzz_num_def = 2
    buzz_len_def = 0.1
    buzz_period_def = 0.2
    buzz_lead_def = 1

    def __init__(self, cageSettings, expSettings, rewarder, lickDetector,
                 camera):
        super().__init__(cageSettings, expSettings, rewarder, lickDetector,
                         camera)
        self.headFixTime = float(
            self.configDict.get('headFixTime',
                                AHF_Stimulator_LickNoLick.headFixTime_def))
        self.lickWitholdTime = float(
            self.configDict.get('lickWitholdTime',
                                AHF_Stimulator_LickNoLick.lickWitholdTime_def))
        self.buzz_pin = int(
            self.configDict.get('buzz_pin',
                                AHF_Stimulator_LickNoLick.buzz_pin_def))
        self.buzz_lead = float(
            self.configDict.get('buzz_lead',
                                AHF_Stimulator_LickNoLick.buzz_lead_def))
        self.buzz_num = int(
            self.configDict.get('buzz_num',
                                AHF_Stimulator_LickNoLick.buzz_num_def))
        self.buzz_len = float(
            self.configDict.get('buzz_len',
                                AHF_Stimulator_LickNoLick.buzz_len_def))
        self.buzz_period = float(
            self.configDict.get('buzz_period',
                                AHF_Stimulator_LickNoLick.buzz_period_def))
        print(self.buzz_period, self.buzz_len, self.buzz_num)
        self.buzzer = Train(PTSimpleGPIO.MODE_PULSES, self.buzz_pin, 0,
                            self.buzz_len, (self.buzz_period - self.buzz_len),
                            self.buzz_num,
                            PTSimpleGPIO.ACC_MODE_SLEEPS_AND_SPINS)
        self.configDict.update({
            'headFixTime': self.headFixTime,
            'lickWitholdTime': self.lickWitholdTime
        })
        self.configDict.update({
            'buzz_pin': self.buzz_pin,
            'buzz_lead': self.buzz_lead,
            'buzz_num': self.buzz_num
        })
        self.configDict.update({
            'buzz_len': self.buzz_len,
            "buzz_period": self.buzz_period
        })

    @staticmethod
    def dict_from_user(stimDict):
        if not 'headFixTime' in stimDict:
            stimDict.update(
                {'headFixTime': AHF_Stimulator_LickNoLick.headFixTime_def})
        if not 'lickWitholdTime' in stimDict:
            stimDict.update({
                'lickWitholdTime':
                AHF_Stimulator_LickNoLick.lickWitholdTime_def
            })
        if not 'buzz_pin' in stimDict:
            stimDict.update(
                {'buzz_pin': AHF_Stimulator_LickNoLick.buzz_pin_def})
        if not 'buzz_lead' in stimDict:
            stimDict.update(
                {'buzz_lead': AHF_Stimulator_LickNoLick.buzz_lead_def})
        if not 'buzz_num' in stimDict:
            stimDict.update(
                {'buzz_num': AHF_Stimulator_LickNoLick.buzz_num_def})
        if not 'buzz_len' in stimDict:
            stimDict.update(
                {'buzz_len': AHF_Stimulator_LickNoLick.buzz_len_def})
        if not 'buzz_period' in stimDict:
            stimDict.update(
                {'buzz_period': AHF_Stimulator_LickNoLick.buzz_period_def})
        return super(AHF_Stimulator_LickNoLick,
                     AHF_Stimulator_LickNoLick).dict_from_user(stimDict)

    def run(self):
        """
        every time lickWitholdtime passes with no licks, make a buzz then give a reward after buzz_lead time.
        """
        self.buzzTimes = []
        self.rewardTimes = []
        endTime = time() + self.headFixTime
        while time() < endTime:
            anyLicks = self.lickDetector.waitForLick_Soft(self.lickWitholdTime,
                                                          startFromZero=True)
            #print ('Licks = ', anyLicks)
            if anyLicks == 0:
                self.buzzTimes.append(time())
                self.buzzer.do_train()
                sleep(self.buzz_lead)
                self.rewardTimes.append(time())
                self.rewarder.giveReward('task')

    def logfile(self):
        rewardStr = 'reward'
        buzzStr = 'Buzz:N=' + str(
            self.buzz_num) + ',length=' + '{:.2f}'.format(
                self.buzz_len) + ',period=' + '{:.2f}'.format(self.buzz_period)
        mStr = '{:013}'.format(self.mouse.tag)
        for i in range(0, len(self.buzzTimes)):
            outPutStr = mStr + '\t' + datetime.fromtimestamp(
                int(self.buzzTimes[i])).isoformat(' ') + '\t' + buzzStr
            print(outPutStr)
        for i in range(0, len(self.rewardTimes)):
            outPutStr = mStr + '\t' + datetime.fromtimestamp(
                int(self.rewardTimes[i])).isoformat(' ') + '\t' + rewardStr
            print(outPutStr)
        if self.textfp != None:
            for i in range(0, len(self.buzzTimes)):
                outPutStr = mStr + '\t' + '{:.2f}'.format(
                    self.buzzTimes[i]
                ) + '\t' + buzzStr + '\t' + datetime.fromtimestamp(
                    int(self.buzzTimes[i])).isoformat(' ') + '\n'
                self.textfp.write(outPutStr)
            for i in range(0, len(self.rewardTimes)):
                outPutStr = mStr + '\t' + '{:.2f}'.format(
                    self.rewardTimes[i]
                ) + '\t' + rewardStr + '\t' + datetime.fromtimestamp(
                    int(self.rewardTimes[i])).isoformat(' ') + '\n'
                self.textfp.write(outPutStr)
            self.textfp.flush()