Exemple #1
0
 def valve2(self,toggle_v2):
     
   if toggle_v2:
       print "valve2 ON"
       #device.setFIOState(v2_lj, 1)
       DAC1_VALUE = device.voltageToDACBits(5.5, dacNumber = 1, is16Bits = False)
       device.getFeedback(u3.DAC1_8(DAC1_VALUE))        # Set DAC0 to 5 V
       if self.Valve1.isChecked() : #if valve1 activated
           self.odo.setValue(2) #odor 2
           print "odor: 2"
       else :
           self.odo.setValue(0) #no odor
           print "odor: 0"
   else :
       print "valve2 OFF"
       #device.setFIOState(v2_lj, 0)
       DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
       device.getFeedback(u3.DAC1_8(DAC1_VALUE))        # Set DAC0 to 5 V
       if self.Valve1.isChecked() : #if valve2 activated
           self.odo.setValue(1) #odor 2
           print "odor: 1"
       else :
           self.odo.setValue(0) #no odor
           print "odor: 0"
       self.odo.setValue(1) #odor 1
Exemple #2
0
def setOdor(device,odor):
    if odor == 0:
        #device.setFIOState(v1_lj, 0)
        DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
        device.getFeedback(u3.DAC0_8(DAC0_VALUE))   
    else :
        if odor == 1 :
            #device.setFIOState(v2_lj, 0)
            DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
            device.getFeedback(u3.DAC1_8(DAC1_VALUE))   
            #device.setFIOState(v1_lj, 1)    
            DAC0_VALUE = device.voltageToDACBits(5.5, dacNumber = 0, is16Bits = False)
            device.getFeedback(u3.DAC0_8(DAC0_VALUE))  
        else:
            #device.setFIOState(v2_lj, 1)
            DAC1_VALUE = device.voltageToDACBits(5.5, dacNumber = 1, is16Bits = False)
            device.getFeedback(u3.DAC1_8(DAC1_VALUE))  
            #device.setFIOState(v1_lj, 1)
            DAC0_VALUE = device.voltageToDACBits(5.5, dacNumber =0, is16Bits = False)
            device.getFeedback(u3.DAC0_8(DAC0_VALUE))  
    print "set odor to: "+str(odor)
Exemple #3
0
 def closeEvent(self, event):   # to clean up, closes labjack and windows
   #global device
   self.win.close()
   device.setFIOState(green_lj, 0)
   device.setFIOState(red_lj, 0)
   #device.setFIOState(v1_lj, 0)
   #device.setFIOState(v2_lj, 0)
   DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
   device.getFeedback(u3.DAC0_8(DAC0_VALUE))        
   DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
   device.getFeedback(u3.DAC1_8(DAC1_VALUE))   
   device.close()
   
   print "closing device"
   ser.close() #arduino serial
   mmc.unloadAllDevices() 
         
   event.accept()   
Exemple #4
0
    def setDACVoltage(self, dac_channel, voltage):
        '''Set the voltage on the DAC0'''

        # Set DAC using Modbus
        #if dac_channel == 0:
        #    DAC0_REGISTER = 5000
        #    self.lj.writeRegister(DAC0_REGISTER, voltage)
        #elif dac_channel == 1:
        #    DAC1_REGISTER = 5002
        #    self.lj.writeRegister(DAC1_REGISTER, voltage)
        #else:
        #    print 'Error: Not a channel'

        # Set DAC using 8 bit low level command
        dac_value = int(voltage * 255 / 4.95)
        if dac_channel == 0:
            self.lj.getFeedback(u3.DAC0_8(dac_value))
        elif dac_channel == 1:
            self.lj.getFeedback(u3.DAC1_8(dac_value))
        else:
            print 'Error: Not a channel'
Exemple #5
0
    def generate_command_lists(self, states):
        command_lists = []
        previous_state = self.default
        for state in states:
            command_list = []
            #  FIOs
            for i in range(4, 8):
                if previous_state[i - 4] != state[i - 4]:
                    command_list.append(u3.BitStateWrite(i, state[i - 4]))

            # DACs
            if previous_state[4] != state[4]:
                voltage_in_bits = self._device.voltageToDACBits(volts=state[4],
                                                                dacNumber=0)
                command_list.append(u3.DAC0_8(voltage_in_bits))

            if previous_state[5] != state[5]:
                voltage_in_bits = self._device.voltageToDACBits(volts=state[5],
                                                                dacNumber=1)
                command_list.append(u3.DAC1_8(voltage_in_bits))

            command_lists.append(command_list)
            previous_state = state
        return command_lists
Exemple #6
0
def dac1out8(d,v=0.0):    # v is the floating point voltage.  Range is 0 to 4.95 
   DAC1_VALUE = d.voltageToDACBits(v, dacNumber = 1, is16Bits = False)
   d.getFeedback(u3.DAC1_8(DAC1_VALUE))        # Set DAC1 to v Volts
Exemple #7
0
def runEpoch(mmc,device,rep):
    import time
    
    global epoch_seq #epochs items as list (name,exp,led,odor,duration,noRec)
    global meta_index # list: epoch name, start frame, end frame
    
    namep=window.path.text()
    print "create hdf5" 
    hdfpath = createHdf5andClose(namep,savepath)
    
    
    n_epoch=len(epoch_seq)
    total =0
    epoch_times =[]
    
    eps = 0.005 #delay... error in time passed
    lj_delay = 0#0.012
    count = 1 #epoch 0 effectuated before loop
    m=0 #tiff saving index
    namep=window.path.text()
    print "namep..."+str(namep)

    window.progressBar.setMinimum(0)
    
    window.progressBar.setMaximum(n_epoch+(rep-1)*n_epoch)

    print str(n_epoch)
    for i in range(n_epoch):
        total += epoch_seq[i][4]
        epoch_times.append(total)
    print str(n_epoch)+" epochs"
    print "should take "+str(total)+"s and repeated "+str(rep)+" time(s)"

    mmc.prepareSequenceAcquisition('Zyla')
    #print "preparing sequence acquisition took", time.clock()-start, "seconds"                                         
    
#    cv2.namedWindow('Video')
    
    text_file_globalTime = open(savepath+"/globalTime"+namep+".txt", 'w')
    #text_file_globalTime.write(str(time.clock()-start)+",  "+ser.readline()+"\n")
    
    
    for i in range(rep): #LOOP FOR TRIALS
            print "trial "+str(i+1)
            text_file = open(savepath+"/respi"+namep+"_trial_"+str(i+1)+".txt", 'w')
            text_file_globalTime.write( str(time.time())+",  "+str(i+1)+"\n")
            frame_i=0
                         
            #mmc.setProperty(DEVICE[0], 'Exposure', epoch_seq[0][1])
            setLed(device,epoch_seq[0][2])
            setOdor(device,epoch_seq[0][3])
            mmc.initializeCircularBuffer()                          
            mmc.startContinuousSequenceAcquisition(10)
            
            start=time.clock()  

            while time.clock()-start < total:
                if  mmc.getRemainingImageCount()>0 :
                    im = mmc.popNextImage()
                    #im = mmc.getLastImage()
                    #cv2.imshow('Video', im)  
                    #raw_input("pause")   
                    frames.append(im)
                    timestamps.append(time.clock()-start) #0.6s from start !!!
                    meta.append( (epoch_seq[count-1], i)) 
                
                if  (count < n_epoch) and (time.clock()-start+eps > epoch_times[count-1]) :
                    print "count "+str(count)
                    print epoch_seq[count-1][0]

                    print frame_i
                    print len(frames)-1
                    #mmc.setProperty(DEVICE[0], 'Exposure', epoch_seq[count][1])
                    setLed(device,epoch_seq[count][2])
                    setOdor(device,epoch_seq[count][3])
                    window.progressBar.setValue(count+i*n_epoch)
                    
#                 #saving index of previous epoch (name,start frame, end frame)
#                    frame_f=len(frames)-1
#                    meta_index.append((epoch_seq[count-1][0],frame_i,frame_f))
#                    frame_i=len(frames)
                    
                    if epoch_seq[count-1][5] == 0 :
                        frame_f=len(frames)-1
                        meta_index.append((epoch_seq[count-1][0],frame_i,frame_f))
                        frame_i=len(frames)
#                        nrec = 0
#                        mmc.stopSequenceAcquisition()
#                        mmc.clearCircularBuffer() 
                    if (epoch_seq[count][5] == 1):
                        print "stop recording "+str(epoch_seq[count][4])+ "sec"
                        time.sleep(epoch_seq[count][4])
                        mmc.clearCircularBuffer()
                        #meta_index.pop()

#                        if epoch_seq[count-1][5] == 1:
#                            mmc.initializeCircularBuffer()   
#                            mmc.startContinuousSequenceAcquisition(10)
#                            nrec=1                    
                    count += 1
                if ser.inWaiting() :
                            text_file.write(str(time.clock()-start)+",  "+ser.readline()+"\n")
#                if cv2.waitKey(1) >= 0:
#                    break
            frame_f=len(frames)-1
            meta_index.append((epoch_seq[count-1][0],frame_i,frame_f))
            frame_i=len(frames)
            
            m_i_trial.append(meta_index)
            meta_index=[]
            
            window.progressBar.setValue(n_epoch+i*n_epoch)
            #reset
            device.setFIOState(green_lj, 0)
            device.setFIOState(red_lj, 1)
            #device.setFIOState(v1_lj, 0)
            #device.setFIOState(v2_lj, 0)
            DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
            device.getFeedback(u3.DAC0_8(DAC0_VALUE))        
            DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
            device.getFeedback(u3.DAC1_8(DAC1_VALUE))       
                      
            print str(total)+"s acquisition took", time.clock()-start, "seconds"
            
            mmc.stopSequenceAcquisition()
            mmc.clearCircularBuffer() 
            if i < rep: #end of an epoch
                    #time spent savong files
                    savingTime=time.time()
                    #print "saving tiff.."+str(len(frames))+" frames"
                    #namep=window.path.text()
                    framesnp = np.asarray(frames)
                    
                    #display avg green
                    print "save green"+str(i)+" in "+savepath
                    scipy.misc.imsave(savepath+'/green'+str(i)+'.jpg', np.mean(framesnp[0:20],axis=0))
                    #plt.imshow(np.mean(framesnp[0:20],axis=0))
                    
                    #saveAsMultipageTif(framesnp,savepath,namep,meta,k=512)#
                    #m = saveAsMultipageTif_inter(framesnp,savepath,namep,m)
                    #print "number of tiff files :"+str(m)
                    print "save trial hdf5"
                    print m_i_trial[-1]
                    print i+1
                    hdfs=saveTrialHdf5andClose(hdfpath,framesnp,m_i_trial[-1],i+1,epoch_seq) #modified added epoch
                    print "closing hdfs"
                    hdfs.close()
                    print "closed"
                    #print "save respiratory signal"
                    # text_file = open("respi"+".txt", 'w')
                    print "empty frames list.."
                    frames[:]=[]
                    text_file.close()
                    timeSpentSaving=time.time()-savingTime
                    newDelay=window.dur_2.value()-timeSpentSaving
                    print "in total wating for "+str(window.dur_2.value())+" s"
                    print "after time spent saving wating for "+str(newDelay)+" s"
                    if (newDelay>0):
                        time.sleep(newDelay)
            count =1
    
    #cv2.destroyAllWindows()
                                

    
    #print "nOfFrames :"+str(len(frames)) 
    print "exposure was", mmc.getProperty('Zyla','Exposure')                                               
    print "Framerate was", mmc.getProperty('Zyla','FrameRate')             
    print "saving metadata, timestamps and comments"
    
    if not os.path.exists(savepath+"/"+namep):
        os.makedirs(savepath+"/"+namep)
    
    with open(savepath+"/"+namep+"/epoch.p", "wb") as fp:   #Pickling
        pickle.dump(epoch_seq, fp)
    with open(savepath+"/"+namep+"/comment.p", "wb") as fpp:   #Pickling
        pickle.dump(window.comment.toPlainText(), fpp)
    with open(savepath+"/"+namep+"/timestamps.p", "wb") as fppp:   #Pickling
        pickle.dump(timestamps, fppp)
    with open(savepath+"/"+namep+"/meta.p", "wb") as fppp:   #Pickling
        pickle.dump(meta, fppp)
    with open(savepath+"/"+namep+"/m_i_trial.p", "wb") as fppp:   #Pickling
        pickle.dump(m_i_trial, fppp)

    print "restart kernel to be safe"
    
    text_file_globalTime.close()
    print "global time txt closed"

    
    window.progressBar.setValue(0)       
    a12_averages_NGENon = []
    a13_averages_NGENon = []
    a14_averages_NGENon = []
    a15_averages_NGENon = []
    save_values = []

    times = []
    ################################################################################
    ## Measurement Loop
    ################################################################################
    #LabJack and SA
    dig_offset = 0.024
    DAC1_VALUE = lj.voltageToDACBits(
        3 + dig_offset, dacNumber=1,
        is16Bits=False)  #CAL voltage value is set to 3V
    lj.getFeedback(u3.DAC1_8(DAC1_VALUE))

    # Get data when NGEN is OFF

    DAC0_VALUE = lj.voltageToDACBits(0 - dig_offset + 0.010,
                                     dacNumber=1,
                                     is16Bits=False)  #NGEN is OFF
    lj.getFeedback(u3.DAC0_8(DAC0_VALUE))
    k = 0
    with GracefulInterruptHandler() as h:
        while True:
            for i in range(n_avg):
                a0 = lj.getAIN(0)
                a1 = lj.getAIN(1)
                a2 = lj.getAIN(2)
                a3 = lj.getAIN(3)
Exemple #9
0
 def set_DAC1(self, voltage):
     voltage_in_bits = self._device.voltageToDACBits(volts=voltage,
                                                     dacNumber=1)
     self._try_command('getFeedback', u3.DAC1_8(voltage_in_bits))