示例#1
0
    def snap_fov(self):
        self.DMDWidget.interupt_projection()

        self.DMDWidget.project_full_white()

        self.cam = CamActuator()
        self.cam.initializeCamera()
        image = self.cam.SnapImage(0.04)
        self.cam.Exit()
        self.selection_view.setImage(image)
示例#2
0
    def run(self):
        # Make sure registration can only start when camera is connected
        try:
            self.cam = CamActuator()
            self.cam.initializeCamera()
        except:
            print(sys.exc_info())
            self.backend.ui_widget.normalOutputWritten(
                "Unable to connect Hamamatsu camera"
            )
            return

        self.cam.setROI(0, 0, 2048, 2048)

        if self.device_1 == "galvos":
            reference_coordinates = self.gather_reference_coordinates_galvos()
            self.dict_transformations["camera-galvos"] = findTransform(
                reference_coordinates[0], reference_coordinates[1]
            )
        elif self.device_1 == "dmd":
            reference_coordinates = self.gather_reference_coordinates_dmd()
            for laser in self.laser_list:
                self.dict_transformations["camera-dmd-" + laser] = findTransform(
                    reference_coordinates[0], reference_coordinates[1]
                )

        elif self.device_1 == "stage":
            reference_coordinates = self.gather_reference_coordinates_stage()
            self.dict_transformations["camera-stage"] = findTransform(
                reference_coordinates[0], reference_coordinates[1]
            )

        self.cam.Exit()

        ## Save transformation to file
        with open(
            "CoordinatesManager/Registration/transformation.txt", "w"
        ) as json_file:

            dict_transformations_list_format = {}
            for key, value in self.dict_transformations.items():
                dict_transformations_list_format[key] = value.tolist()

            json.dump(dict_transformations_list_format, json_file)

        self.sig_finished_registration.emit(self.dict_transformations)
示例#3
0
    def start_aqcuisition(self):
        global_pos = np.array(
            ((-5000, -5000), (-5000, 5000), (5000, -5000), (5000, 5000)))
        global_pos_name = ["A", "B", "C", "D"]

        delta = 200
        local_pos = np.transpose(
            np.reshape(
                np.meshgrid(np.array((-delta, 0, delta)),
                            np.array((-delta, 0, delta))),
                (2, -1),
            ))
        local_pos_name = [str(i) for i in range(9)]

        self.cam = CamActuator()
        self.cam.initializeCamera()

        # Offset variables are used to generate replicates for good statistics
        offset_y = offset_x = delta

        cnt = 0
        for i in range(global_pos.shape[0]):
            for j in range(local_pos.shape[0]):
                x = global_pos[i, 0] + local_pos[j, 0] + offset_x
                y = global_pos[i, 1] + local_pos[j, 1] + offset_y

                self.ludlStage.moveAbs(x=x, y=y)
                # stage_movement_thread = StagemovementAbsoluteThread(x, y)
                # stage_movement_thread.start()
                # time.sleep(2)
                # stage_movement_thread.quit()
                # stage_movement_thread.wait()

                image = self.cam.SnapImage(0.04)
                filename = global_pos_name[i] + local_pos_name[j]

                self.save_image(filename, image)

                cnt += 1
                print(
                    str(cnt) + "/" +
                    str(len(local_pos_name) * len(global_pos_name)))

        self.cam.Exit()
示例#4
0
 def __init__(self, camerahandle=None):
     self.exposuretime = 0.02    # seconds
     self.GUIframerate = 25      # frames per second
     self.sleeptime = np.max([1/self.GUIframerate, self.exposuretime])
     self.frame = np.random.rand(2048, 2048)
     self.mutex = QMutex()
     
     # Camera attributes
     if camerahandle == None:
         self.camera = CamActuator()
         self.camera.initializeCamera()
     else:
         self.camera = camerahandle
     self.camera.hcam.setPropertyValue("exposure_time", self.exposuretime)
     
     # QThread attributes
     super().__init__()
     self.isrunning = False
     self.moveToThread(self)
     self.started.connect(self.live)
示例#5
0
    def cam_snap(self):
        """Get a image from camera"""
        self.cam = CamActuator()
        self.cam.initializeCamera()

        exposure_time = self.CamExposureBox.value()
        self.Rawimage = self.cam.SnapImage(exposure_time)
        self.cam.Exit()
        print('Snap finished')

        self.MLtargetedImg_raw = self.Rawimage.copy()

        self.MLtargetedImg = self.convert_for_MaskRCNN(self.MLtargetedImg_raw)

        self.show_raw_image(self.MLtargetedImg)

        self.addedROIitemMask = np.zeros(
            (self.MLtargetedImg.shape[0], self.MLtargetedImg.shape[1]))
        self.MLmask = np.zeros(
            (self.MLtargetedImg.shape[0], self.MLtargetedImg.shape[1]))
示例#6
0
 def __init__(self, DMD, *args, **kwargs):
     self.DMD = DMD
     self.cam = CamActuator()
     self.cam.initializeCamera()
示例#7
0
 def __init__(self, parent):
     self.DMD = DMDActuator.DMDActuator()
     self.cam = CamActuator()
     self.cam.initializeCamera()
示例#8
0
    def run(self):
        """
        # ==========================================================================================================================================================
        #                                                                       Initialization
        # ==========================================================================================================================================================
        """
        #%%
        """
        # =============================================================================
        #         connect the Objective motor
        # =============================================================================
        """
        print('----------------------Starting to connect the Objective motor-------------------------')
        self.pi_device_instance = PIMotor()
        print('Objective motor connected.')
        self.errornum = 0
        self.ObjCurrentPos = self.pi_device_instance.pidevice.qPOS(self.pi_device_instance.pidevice.axes)

        """
        # =============================================================================
        #         connect the Hmamatsu camera
        # =============================================================================
        """
        self._use_camera = False
        for key in self.RoundQueueDict:
            if "RoundPackage_" in key:
                for waveform_and_cam_key in self.RoundQueueDict[key][1]:
                    if "CameraPackage_" in waveform_and_cam_key:
                        if len(self.RoundQueueDict[key][1][waveform_and_cam_key]) != 0:
                            self._use_camera = True
        
        if self._use_camera == True:
            print('Connecting camera...')
            self.HamamatsuCam = CamActuator()
            self.HamamatsuCam.initializeCamera()
        else:
            print('No camera involved.')
        
        """
        # =============================================================================
        #         connect the Insight X3
        # =============================================================================
        """        
        if len(self.RoundQueueDict['InsightEvents']) != 0:
            self.Laserinstance = InsightX3('COM11')
            try:
                querygap = 1.1
                # self.Laserinstance.SetWatchdogTimer(0) # Disable the laser watchdog!
#                Status_watchdog_thread = threading.Thread(target = self.Status_watchdog, args=[querygap], daemon=True)
#                Status_watchdog_thread.start() 
                time.sleep(1)
                #-------------Initialize laser--------------
                self.watchdog_flag = False
                time.sleep(0.5)
        
                warmupstatus = 0
                while int(warmupstatus) != 100:
                    try:
                        warmupstatus = self.Laserinstance.QueryWarmupTime()
                        time.sleep(0.6)
                    except:
                        time.sleep(0.6)
                        
                # if int(warmupstatus) == 100:
                #     self.warmupstatus = True
                #     print('Laser fully warmed up.')
                #     if 'Laser state:Ready' in self.Status_list:
                #         self.Laserinstance.Turn_On_PumpLaser()
                #         self.laserRun = True
                #     elif 'Laser state:RUN' in self.Status_list:
                #         self.laserRun = True
                        
                self.watchdog_flag = True
                time.sleep(0.5)
            except:
                print('Laser not connected.')
                
            # If turn on the laser shutter in the beginning
            if 'Shutter_Open' in self.GeneralSettingDict['StartUpEvents']:
                time.sleep(0.5)
                while True:
                    try:
                        self.Laserinstance.Open_TunableBeamShutter()
                        break
                    except:
                        time.sleep(1)
                time.sleep(0.5)                
        #%%
        """
        # ==========================================================================================================================================================
        #                                                                       Execution
        # ==========================================================================================================================================================
        """
   
        GridSequence = 0
        TotalGridNumber = self.meshgridnumber**2
        ScanningMaxCoord = int(np.amax(self.RoundCoordsDict['CoordsPackage_1'])) # Get the largest coordinate
        for EachGrid in range(TotalGridNumber):
            """
            # =============================================================================
            #         For each small repeat unit in the scanning meshgrid
            # =============================================================================
            """
            ScanningGridOffset_Row = int(GridSequence % self.meshgridnumber) * (ScanningMaxCoord + self.GeneralSettingDict['Scanning step']) # Offset coordinate row value for each well.
            ScanningGridOffset_Col = int(GridSequence/self.meshgridnumber) * (ScanningMaxCoord + self.GeneralSettingDict['Scanning step']) # Offset coordinate colunm value for each well.
            GridSequence += 1
            time.sleep(0.5)
            
            for EachRound in range(int(len(self.RoundQueueDict)/2-1)): # EachRound is the round sequence number starting from 0, while the actual number used in dictionary is 1.
                print ('----------------------------------------------------------------------------')            
                print('Below is Grid {}, Round {}.'.format(EachGrid, EachRound+1)) # EachRound+1 is the corresponding round number when setting the dictionary starting from round 1.
                
                """
                # =============================================================================
                #         Unpack the settings for each round
                # =============================================================================
                """
                # Initialize variables
                CoordOrder = 0      # Counter for n th coordinates, for appending cell properties array.      
                CellPropertiesDict = {}
                ND_filter1_Pos = None
                ND_filter2_Pos = None
                EM_filter_Pos = None
                cp_end_index = -1
                self.IndexLookUpCellPropertiesDict = {} #look up dictionary for each cell properties
                
                #-------------Unpack the focus stack information.
                ZStackinfor = self.GeneralSettingDict['FocusStackInfoDict']['RoundPackage_{}'.format(EachRound+1)]
                self.ZStackNum = int(ZStackinfor[ZStackinfor.index('Focus')+5])
                self.ZStackStep = float(ZStackinfor[ZStackinfor.index('Being')+5:len(ZStackinfor)])
                
                #-------------Unpack infor for stage move.
                CoordsNum = int(len(self.RoundCoordsDict['CoordsPackage_{}'.format(EachRound+1)])/2) #Each pos has 2 coords
                
                #-------------Unpack infor for filter event. In the list, the first one is for ND filter and the second one is for emission filter.
                FilterEventIndexList = [i for i,x in enumerate(self.RoundQueueDict['FilterEvents']) if 'Round_{}'.format(EachRound+1) in x]
                
                if len(FilterEventIndexList) > 0:
                    NDposText = self.RoundQueueDict['FilterEvents'][FilterEventIndexList[0]]
                    NDnumber = NDposText[NDposText.index('ToPos_')+6:len(NDposText)]
                    
                    EMposText = self.RoundQueueDict['FilterEvents'][FilterEventIndexList[1]]
                    EMprotein = EMposText[EMposText.index('ToPos_')+6:len(EMposText)]
                    
                    # "COM9" for filter 1 port, which has ND values from 0 to 3.
                    # "COM7" for filter 2 port, which has ND values from 0 to 0.5.
                    if NDnumber == '0':
                        ND_filter1_Pos = 0
                        ND_filter2_Pos = 0
                    elif NDnumber == '1':
                        ND_filter1_Pos = 1
                        ND_filter2_Pos = 0
                    elif NDnumber == '2':
                        ND_filter1_Pos = 2
                        ND_filter2_Pos = 0
                    elif NDnumber == '2.3':
                        ND_filter1_Pos = 2
                        ND_filter2_Pos = 2
                    elif NDnumber == '2.5':
                        ND_filter1_Pos = 2
                        ND_filter2_Pos = 3
                    elif NDnumber == '0.5':
                        ND_filter1_Pos = 0
                        ND_filter2_Pos = 3        
                    elif NDnumber == '0.3':
                        ND_filter1_Pos = 0
                        ND_filter2_Pos = 2
                    
                    if EMprotein == 'Arch':
                        EM_filter_Pos = 0
                    elif EMprotein == 'eGFP' or EMprotein == 'Citrine':
                        EM_filter_Pos = 1
                    
                #-------------Unpack infor for Insight X3. In the list, the first one is for shutter event and the second one is for wavelength event. 
                InsightX3EventIndexList = [i for i,x in enumerate(self.RoundQueueDict['InsightEvents']) if 'Round_{}'.format(EachRound+1) in x]
                
                """
                # =============================================================================
                #         Execute Insight event at the beginning of each round
                # =============================================================================
                """            
                if len(InsightX3EventIndexList) == 1:
                    print(InsightX3EventIndexList)
                    InsightText = self.RoundQueueDict['InsightEvents'][InsightX3EventIndexList[0]]
                    if 'Shutter_Open' in InsightText:
                        self.watchdog_flag = False
                        time.sleep(0.5)
                        while True:
                            try:
                                self.Laserinstance.Open_TunableBeamShutter()
                                break
                            except:
                                time.sleep(1)
                        time.sleep(0.5)
                        print('Laser shutter open.')
                        self.watchdog_flag = True
                        time.sleep(0.5)
    
                    elif 'Shutter_Close' in InsightText:
                        self.watchdog_flag = False
                        time.sleep(0.5)
                        while True:
                            try:
                                self.Laserinstance.Close_TunableBeamShutter()
                                break
                            except:
                                time.sleep(1)
                        time.sleep(0.5)
                        print('Laser shutter closed.')
                        self.watchdog_flag = True
                        time.sleep(0.5)
                    elif 'WavelengthTo' in InsightText:
                        self.watchdog_flag = False
                        time.sleep(0.5)
                        TargetWavelen = int(InsightText[InsightText.index('To_')+3:len(InsightText)])
                        print(TargetWavelen)
                        while True:
                            try:
                                self.Laserinstance.SetWavelength(TargetWavelen)
                                break
                            except:
                                time.sleep(1)
                        time.sleep(5)
                        self.watchdog_flag = True
                        time.sleep(0.5)
                        
                elif len(InsightX3EventIndexList) == 2:
                    
                    InsightText_wl = self.RoundQueueDict['InsightEvents'][InsightX3EventIndexList[1]]
                    InsightText_st = self.RoundQueueDict['InsightEvents'][InsightX3EventIndexList[0]]
                    
                    if 'WavelengthTo' in InsightText_wl and 'Shutter_Open' in InsightText_st:
                        self.watchdog_flag = False
                        time.sleep(0.5)
                        TargetWavelen = int(InsightText_wl[InsightText_wl.index('To_')+3:len(InsightText_wl)])
                        while True:
                            try:
                                self.Laserinstance.SetWavelength(TargetWavelen)
                                break
                            except:
                                time.sleep(1)
                        time.sleep(5)
                        while True:
                            try:
                                self.Laserinstance.Open_TunableBeamShutter()
                                break
                            except:
                                time.sleep(1)
                        print('Laser shutter open.')
                        self.watchdog_flag = True
                        time.sleep(0.5)
                        
                    elif 'WavelengthTo' in InsightText_wl and 'Shutter_Close' in InsightText_st:
                        self.watchdog_flag = False
                        time.sleep(0.5)
                        TargetWavelen = int(InsightText_wl[InsightText_wl.index('To_')+3:len(InsightText_wl)])
                        while True:
                            try:                        
                                self.Laserinstance.SetWavelength(TargetWavelen)
                                break
                            except:
                                time.sleep(1)
                        time.sleep(5)
                        while True:
                            try:
                                self.Laserinstance.Close_TunableBeamShutter()
                                break
                            except:
                                time.sleep(1)
                        time.sleep(1)
                        print('Laser shutter closed.')
                        self.watchdog_flag = True
                        time.sleep(0.5)
                        
                    time.sleep(2)
                    
                """
                # =============================================================================
                #         Execute filter event at the beginning of each round
                # =============================================================================
                """            
                if ND_filter1_Pos != None and ND_filter2_Pos != None:
                    #Move filter 1
                    self.filter1 = ELL9Filter("COM9")
                    self.filter1.moveToPosition(ND_filter1_Pos)
                    time.sleep(1)
                    #Move filter 2
                    self.filter2 = ELL9Filter("COM7")
                    self.filter2.moveToPosition(ND_filter2_Pos)
                    time.sleep(1)
                if EM_filter_Pos != None:
                    self.filter3 = ELL9Filter("COM15")
                    self.filter3.moveToPosition(EM_filter_Pos)
                    time.sleep(1)
    
                    
                self.currentCoordsSeq = 0
                #%%
                for EachCoord in range(CoordsNum):

                    #-----------------------------------------------At each stage coordinate:--------------------------------------------

                    self.error_massage = None
                    
                    self.currentCoordsSeq += 1
                    
                    """
                    # =============================================================================
                    #         Stage movement
                    # =============================================================================
                    """
                    RowIndex = int(self.RoundCoordsDict['CoordsPackage_{}'.format(EachRound+1)][EachCoord*2:EachCoord*2+2][0]) + ScanningGridOffset_Row
                    ColumnIndex = int(self.RoundCoordsDict['CoordsPackage_{}'.format(EachRound+1)][EachCoord*2:EachCoord*2+2][1]) + ScanningGridOffset_Col
    
                    try:
                        self.ludlStage.moveAbs(RowIndex,ColumnIndex) # Row/Column indexs of np.array are opposite of stage row-col indexs.
                    except:
                        self.error_massage = 'Fail_MoveStage'
                        self.errornum += 1
                        print('Stage move failed! Error number: {}'.format(int(self.errornum)))
                    
                    print ('Round {}. Current index: {}.'.format(EachRound+1, [RowIndex,ColumnIndex]))

                    time.sleep(1)
                    
                    """
                    # =============================================================================
                    #         Get the z stack objective positions ready
                    # =============================================================================
                    """
                    #-------------------------------------------If focus correction applies----------------------------------------
                    if len(self.GeneralSettingDict['FocusCorrectionMatrixDict']) > 0:
                        FocusPosArray = self.GeneralSettingDict['FocusCorrectionMatrixDict']['RoundPackage_{}_Grid_{}'.format(EachRound+1, EachGrid)]
    #                    print(FocusPosArray)
                        FocusPosArray = FocusPosArray.flatten('F')
                        FocusPos_fromCorrection = FocusPosArray[EachCoord]
                        print('Target focus pos: '.format(FocusPos_fromCorrection))
                    
                    # Without focus correction
                    if len(self.GeneralSettingDict['FocusCorrectionMatrixDict']) == 0:
                        ZStacklinspaceStart = self.ObjCurrentPos['1'] - (math.floor(self.ZStackNum/2)-1)*self.ZStackStep
                        ZStacklinspaceEnd = self.ObjCurrentPos['1'] + (self.ZStackNum - math.floor(self.ZStackNum/2))*self.ZStackStep
                    # With focus correction
                    elif len(self.GeneralSettingDict['FocusCorrectionMatrixDict']) > 0:
                        ZStacklinspaceStart = FocusPos_fromCorrection - (math.floor(self.ZStackNum/2)-1)*self.ZStackStep
                        ZStacklinspaceEnd = FocusPos_fromCorrection + (self.ZStackNum - math.floor(self.ZStackNum/2))*self.ZStackStep                    
                        
                    ZStackPosList = np.linspace(ZStacklinspaceStart, ZStacklinspaceEnd, num = self.ZStackNum)       
                    print(ZStackPosList)
                    
                    """
                    # =============================================================================
                    #         Execute waveform packages
                    # =============================================================================
                    """
                    self.WaveforpackageNum = int(len(self.RoundQueueDict['RoundPackage_{}'.format(EachRound+1)][0]))
                    #Execute each individual waveform package
                    print('*******************************************Round {}. Current index: {}.**************************************************'.format(EachRound+1, [RowIndex,ColumnIndex]))
                    
                    #------------------Move to Z stack focus-------------------
                    for EachZStackPos in range(self.ZStackNum): 
                        print('--------------------------------------------Stack {}--------------------------------------------------'.format(EachZStackPos+1))
                        if self.ZStackNum > 1:
                            self.ZStackOrder = int(EachZStackPos +1) # Here the first one is 1, not starting from 0.
                            FocusPos = ZStackPosList[EachZStackPos]
                            print('Target focus pos: {}'.format(FocusPos))
    
                            pos = PIMotor.move(self.pi_device_instance.pidevice, FocusPos)
                            self.ObjCurrentPosInStack = self.pi_device_instance.pidevice.qPOS(self.pi_device_instance.pidevice.axes)
                            print("Current position: {:.4f}".format(self.ObjCurrentPosInStack['1']))
                            
                            time.sleep(0.3)
                        else:
                            self.ZStackOrder = 1
                        
                        #------------For waveforms in each coordinate----------
                        for EachWaveform in range(self.WaveforpackageNum):
                            # Extract information
                            WaveformPackageToBeExecute = self.RoundQueueDict['RoundPackage_{}'.format(EachRound+1)][0]['WaveformPackage_{}'.format(EachWaveform+1)]
                            CameraPackageToBeExecute = self.RoundQueueDict['RoundPackage_{}'.format(EachRound+1)][1]["CameraPackage_{}".format(EachWaveform+1)]                  
                            WaveformPackageGalvoInfor = self.RoundQueueDict['GalvoInforPackage_{}'.format(EachRound+1)]['GalvoInfor_{}'.format(EachWaveform+1)]

                            self.readinchan = WaveformPackageToBeExecute[3]
                            self.RoundWaveformIndex = [EachRound+1, EachWaveform+1] # first is current round number, second is current waveform package number.
                            self.CurrentPosIndex = [RowIndex, ColumnIndex]
                            
                            #----------------Camera operations-----------------
                            _camera_isUsed = False
                            if CameraPackageToBeExecute != {}: # if camera operations are configured
                                _camera_isUsed = True
                                CamSettigList = CameraPackageToBeExecute["Settings"]
                                self.HamamatsuCam.StartStreaming(BufferNumber = CameraPackageToBeExecute["Buffer_number"],
                                                                 trigger_source = CamSettigList[CamSettigList.index("trigger_source")+1],
                                                                 exposure_time = CamSettigList[CamSettigList.index("exposure_time")+1],
                                                                 trigger_active = CamSettigList[CamSettigList.index("trigger_active")+1])
                                # Make sure that the camera is prepared before waveform execution.
#                                while self.HamamatsuCam.isStreaming == False:
#                                    print('Waiting for camera...')
#                                    time.sleep(0.5)
                                time.sleep(1)
                            print('Now start waveforms')
                            #----------------Waveforms operations--------------
                            if WaveformPackageGalvoInfor != 'NoGalvo': # Unpack the information of galvo scanning.
                                self.readinchan = WaveformPackageGalvoInfor[0]
                                self.repeatnum = WaveformPackageGalvoInfor[1]
                                self.PMT_data_index_array = WaveformPackageGalvoInfor[2]
                                self.averagenum = WaveformPackageGalvoInfor[3]
                                self.lenSample_1 = WaveformPackageGalvoInfor[4]
                                self.ScanArrayXnum = WaveformPackageGalvoInfor[5]
        
                            if self.clock_source == 'Dev1 as clock source':
                                self.adcollector = execute_analog_readin_optional_digital_thread()
                                self.adcollector.set_waves(WaveformPackageToBeExecute[0], WaveformPackageToBeExecute[1], WaveformPackageToBeExecute[2], WaveformPackageToBeExecute[3]) #[0] = sampling rate, [1] = analogcontainer_array, [2] = digitalcontainer_array, [3] = readinchan
                                self.adcollector.collected_data.connect(self.ProcessData)
                                self.adcollector.run()
                                time.sleep(0.2)
                                #self.ai_dev_scaling_coeff = self.adcollector.get_ai_dev_scaling_coeff()
                            elif self.clock_source == 'Cam as clock source' :
                                self.adcollector = execute_analog_and_readin_digital_optional_camtrig_thread()
                                self.adcollector.set_waves(WaveformPackageToBeExecute[0], WaveformPackageToBeExecute[1], WaveformPackageToBeExecute[2], WaveformPackageToBeExecute[3])
                                self.adcollector.collected_data.connect(self.ProcessData)
                                self.adcollector.run()
                            
                            #------------------Camera saving-------------------
                            if _camera_isUsed == True:
                                self.HamamatsuCam.isSaving = True
                                tif_name = os.path.join(self.scansavedirectory, 'Round'+str(self.RoundWaveformIndex[0])+'_Coords'+str(self.currentCoordsSeq)+'_R'+str(self.CurrentPosIndex[0])+'C'+str(self.CurrentPosIndex[1])+'_Cam_'+'Zpos'+str(self.ZStackOrder)+'.tif')
                                self.HamamatsuCam.StopStreaming(saving_dir = tif_name)
                                # Make sure that the saving process is finished.
                                while self.HamamatsuCam.isSaving == True:
                                    print('Camera saving...')
                                    time.sleep(0.5)
                                time.sleep(1)                                
                                
                            
                        time.sleep(0.6) # Wait for receiving data to be done.
                    time.sleep(0.3)
                    print('*************************************************************************************************************************')
        #%%
        """
        # ==========================================================================================================================================================
        #                                                                       Finalizing
        # ==========================================================================================================================================================
        """                
        
        # Switch off laser
        if len(self.RoundQueueDict['InsightEvents']) != 0:
            self.watchdog_flag = False
            time.sleep(0.5)
            
            while True:
                try:
                    self.Laserinstance.Close_TunableBeamShutter()
                    break
                except:
                    time.sleep(1)
            time.sleep(0.5)
            
            self.Laserinstance.SaveVariables()
            while True:
                try:                        
                    self.Laserinstance.Turn_Off_PumpLaser()
                    break
                except:
                    time.sleep(1)
                    
        # Disconnect camera
        if self._use_camera == True:
            self.HamamatsuCam.Exit()
        
        # Disconnect focus motor
        try:
            PIMotor.CloseMotorConnection(self.pi_device_instance.pidevice)
            print('Objective motor disconnected.')
        except:
            pass
    def run(self):
        """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #                                                               Connect devices.
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
        """
        # =============================================================================
        #         connect the Objective motor
        # =============================================================================
        """
        print('----------------------Starting to connect the Objective motor-------------------------')
        self.pi_device_instance = PIMotor()
        print('Objective motor connected.')
        self.errornum = 0
        self.init_focus_position = self.pi_device_instance.pidevice.qPOS(self.pi_device_instance.pidevice.axes)['1']
        print("init_focus_position : {}".format(self.init_focus_position))

        """
        # =============================================================================
        #         connect the Hmamatsu camera
        # =============================================================================
        """
        self._use_camera = False
        # Check if camera is used.
        for key in self.RoundQueueDict:
            if "RoundPackage_" in key:
                for waveform_and_cam_key in self.RoundQueueDict[key][1]:
                    if "CameraPackage_" in waveform_and_cam_key:
                        if len(self.RoundQueueDict[key][1][waveform_and_cam_key]) != 0:
                            self._use_camera = True
        
        if self._use_camera:
            print('Connecting camera...')
            self.HamamatsuCam = CamActuator()
            self.HamamatsuCam.initializeCamera()
        else:
            print('No camera involved.')
        
        """
        # =============================================================================
        #         connect the Insight X3
        # =============================================================================
        """        
        if len(self.RoundQueueDict['InsightEvents']) != 0:
            self.Laserinstance = InsightX3('COM11')
            try:
                # querygap = 1.1
                # self.Laserinstance.SetWatchdogTimer(0) # Disable the laser watchdog!
#                Status_watchdog_thread = threading.Thread(target = self.Status_watchdog, args=[querygap], daemon=True)
#                Status_watchdog_thread.start() 
                # time.sleep(1)
                #-------------Initialize laser--------------
                self.watchdog_flag = False
                time.sleep(0.5)
        
                warmupstatus = 0
                while int(warmupstatus) != 100:
                    warmupstatus = self.Laserinstance.QueryWarmupTime()
                    time.sleep(0.6)
                        
                self.watchdog_flag = True
                time.sleep(0.5)
            except:
                print('Laser not connected.')
                
            # If turn on the laser shutter in the beginning
            if 'Shutter_Open' in self.GeneralSettingDict['StartUpEvents']:
                time.sleep(0.5)

                self.Laserinstance.Open_TunableBeamShutter()

                time.sleep(0.5)     
        """
        # =============================================================================
        #         Initialize ML
        # =============================================================================
        """        
        self._use_ML = False
        # Check if machine learning segmentation is used.
        for key in self.RoundQueueDict:
            if "RoundPackage_" in key:
                for photocycle_key in self.RoundQueueDict[key][2]:
                    if "PhotocyclePackage_" in photocycle_key:
                        if len(self.RoundQueueDict[key][2][photocycle_key]) != 0:
                            self._use_ML = True
        if self._use_ML:
            from ImageAnalysis.ImageProcessing_MaskRCNN import ProcessImageML
            self.Predictor = ProcessImageML()
            print("ML loaded.")
            
        #%%
        """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #                                                                  Execution
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
        GridSequence = 0
        TotalGridNumber = self.meshgridnumber**2
        
        for EachGrid in range(TotalGridNumber):
            """
            #------------------------------------------------------------------------------
            #:::::::::::::::::::::::::::::::: AT EACH GRID ::::::::::::::::::::::::::::::::
            #------------------------------------------------------------------------------
            """
            self.Grid_index = EachGrid
            """
            # =============================================================================
            #         For each small repeat unit in the scanning meshgrid
            # =============================================================================
            """

            time.sleep(0.5)
            
            for EachRound in range(int(len(self.RoundQueueDict)/2-1)): # EachRound is the round sequence number starting from 0, while the actual number used in dictionary is 1.
                """
                #-------------------------------------------------------------------------------
                #:::::::::::::::::::::::::::::::: AT EACH ROUND ::::::::::::::::::::::::::::::::
                #-------------------------------------------------------------------------------
                """
                print ('----------------------------------------------------------------------------')            
                print('Below is Grid {}, Round {}.'.format(EachGrid, EachRound+1)) # EachRound+1 is the corresponding round number when setting the dictionary starting from round 1.
                """
                # =============================================================================
                #         Execute Insight event at the beginning of each round
                # =============================================================================
                """            
                self.laser_init(EachRound)
                    
                """
                # =============================================================================
                #         Execute filter event at the beginning of each round
                # =============================================================================
                """
                self.filters_init(EachRound)

                """
                # =============================================================================
                #         Generate focus position list at the beginning of each round
                # =============================================================================
                """
                if EachRound == 0:
                    ZStackinfor = self.GeneralSettingDict['FocusStackInfoDict']['RoundPackage_{}'.format(EachRound+1)]        
                    ZStackNum = int(ZStackinfor[ZStackinfor.index('Focus')+5])
                    ZStackStep = float(ZStackinfor[ZStackinfor.index('Being')+5:len(ZStackinfor)])
            
                    # Generate position list.
                    ZStacklinspaceStart = self.init_focus_position - (math.floor(ZStackNum/2)) * ZStackStep
                    ZStacklinspaceEnd = self.init_focus_position + (ZStackNum - math.floor(ZStackNum/2)-1) * ZStackStep
    
                    self.ZStackPosList = np.linspace(ZStacklinspaceStart, ZStacklinspaceEnd, num = ZStackNum)
                
                self.currentCoordsSeq = 0
                #%%
                #-------------Unpack infor for stage move.
                CoordsNum = len(self.RoundCoordsDict['CoordsPackage_{}'.format(EachRound+1)])              
                for EachCoord in range(CoordsNum):
                    """
                    #------------------------------------------------------------------------------------
                    #:::::::::::::::::::::::::::::::: AT EACH COORDINATE ::::::::::::::::::::::::::::::::
                    #------------------------------------------------------------------------------------
                    """
                    self.error_massage = None
                    
                    self.currentCoordsSeq += 1
                    
                    """
                    # =============================================================================
                    #         Stage movement
                    # =============================================================================
                    """
                    self.coord_array = self.RoundCoordsDict['CoordsPackage_{}'.format(EachRound+1)][EachCoord]
                    
                    # Offset coordinate row value for each well.
                    ScanningGridOffset_Row = int(EachGrid % self.meshgridnumber) * (self.GeneralSettingDict['StageGridOffset']) 
                    # Offset coordinate colunm value for each well.
                    ScanningGridOffset_Col = int(EachGrid/self.meshgridnumber) * (self.GeneralSettingDict['StageGridOffset']) 

                    RowIndex = self.coord_array['row'] + ScanningGridOffset_Row
                    ColumnIndex = self.coord_array['col'] + ScanningGridOffset_Col
                                      
                    try:
                        # for _ in range(2): # Repeat twice
                        #     self.ludlStage.moveAbs(RowIndex,ColumnIndex) # Row/Column indexs of np.array are opposite of stage row-col indexs.
                        #     time.sleep(1)
                        self.ludlStage.moveAbs(RowIndex,ColumnIndex) # Row/Column indexs of np.array are opposite of stage row-col indexs.
                        time.sleep(1.2)
                    except:
                        self.error_massage = 'Fail_MoveStage'
                        self.errornum += 1
                        print('Stage move failed! Error number: {}'.format(int(self.errornum)))

                    time.sleep(0.2)
                    
                    """
                    # =============================================================================
                    #                               Focus position
                    #         Unpack the focus stack information, conduct auto-focusing if set.
                    # =============================================================================
                    """
                    # Here also generate the ZStackPosList.
                    self.ZStackNum = self.unpack_focus_stack(EachGrid, EachRound, EachCoord)
                    
                    self.stack_focus_degree_list = []
                    
                    print('*******************************************Round {}. Current index: {}.**************************************************'.format\
                          (EachRound+1, [RowIndex,ColumnIndex]))
                    
                    #------------------Move to Z stack focus-------------------
                    for EachZStackPos in range(self.ZStackNum): 
                        """
                        #------------------------------------------------------------------------------------
                        #:::::::::::::::::::::::::::::::: AT EACH ZSTACK ::::::::::::::::::::::::::::::::::::
                        #------------------------------------------------------------------------------------
                        """
                        print('--------------------------------------------Stack {}--------------------------------------------------'.format(EachZStackPos+1))
                        if self.ZStackNum > 1:
                            self.ZStackOrder = int(EachZStackPos +1) # Here the first one is 1, not starting from 0.
                            
                            self.FocusPos = self.ZStackPosList[EachZStackPos]# + self.wavelength_offset    
                            
                            # Add the focus degree of previous image to the list.
                            # For stack of 3 only.
                            if EachZStackPos > 0:
                                try:
                                    self.stack_focus_degree_list.append(self.FocusDegree_img_reconstructed)
                                    
                                except:
                                    # FocusDegree_img_reconstructed is not generated with camera imaging.
                                    pass
                            print('stack_focus_degree_list is {}'.format(self.stack_focus_degree_list))
                            #-----Suppose now it's the 3rd stack position-----
                            if len(self.stack_focus_degree_list) == 2:
                                if self.stack_focus_degree_list[-1] < self.stack_focus_degree_list[-2]:
                                    self.FocusPos = self.ZStackPosList[0] - (self.ZStackPosList[1] - self.ZStackPosList[0])/2
                                    print('Focus degree decreasing, run the other direction.')
                            #-------------------------------------------------
                            
                            print('Target focus pos: {}'.format(self.FocusPos))
    
                            self.pi_device_instance.move(self.FocusPos)
                            # self.auto_focus_positionInStack = self.pi_device_instance.pidevice.qPOS(self.pi_device_instance.pidevice.axes)
                            # print("Current position: {:.4f}".format(self.auto_focus_positionInStack['1']))
                            
                            time.sleep(0.3)
                        else:
                            self.ZStackOrder = 1
                            self.FocusPos = self.init_focus_position
                        """
                        # =============================================================================
                        #         Execute waveform packages
                        # =============================================================================
                        """
                        self.Waveform_sequence_Num = int(len(self.RoundQueueDict['RoundPackage_{}'.format(EachRound+1)][0]))                        
                        #------------For waveforms in each coordinate----------
                        for EachWaveform in range(self.Waveform_sequence_Num):
                            
                            """
                            # =============================================================================
                            #         For photo-cycle 
                            # =============================================================================
                            """
                            # Get the photo cycle information
                            PhotocyclePackageToBeExecute = self.RoundQueueDict['RoundPackage_{}'.format(EachRound+1)][2] \
                                                            ["PhotocyclePackage_{}".format(EachWaveform+1)]
                            
                            # See if in this waveform sequence photo cycle is involved.
                            # PhotocyclePackageToBeExecute is {} if not configured.
                            if len(PhotocyclePackageToBeExecute) > 0:
                                
                                
                                
                                # Load the previous acquired camera image
                                self.cam_tif_name = r"M:\tnw\ist\do\projects\Neurophotonics\Brinkslab\Data\Octoscope\2020-8-13 Screening Archon1 library V5 and 6\V6\Round2_Coords181_R19800C0_PMT_0Zmax.tif"
                                previous_cam_img = imread(self.cam_tif_name)
                                img_width = previous_cam_img.shape[1]

                                img_height = previous_cam_img.shape[0]

                                # Get the segmentation of full image.
                                fig, ax = plt.subplots()
                                MLresults = self.Predictor.DetectionOnImage(previous_cam_img, axis = ax)
                                
                                ROI_number = len(MLresults["scores"])
                                print('roi number: {}'.format(ROI_number))
                                for each_ROI in range(ROI_number):
                                    # if MLresults['class_ids'][each_ROI] == 3:
                                    ROIlist = MLresults['rois'][each_ROI]
                                    print(ROIlist)
                                    # np array's column([1]) is the width of image, and is the row in stage coordinates.
                                    ROI_center_width = int(ROIlist[1] + (ROIlist[3] - ROIlist[1])/2)
                                    print('ROI_center_width '.format(ROIlist))
                                    ROI_center_height = int(ROIlist[0] + (ROIlist[2] + ROIlist[0])/2)
                                    print('ROI_center_height '.format(ROIlist))
                                    cam_stage_transform_factor = 1.135
                                    
                                    stage_move_col = (int((img_width)/2) - ROI_center_width) * cam_stage_transform_factor
                                    print(stage_move_col)
                                    stage_move_row = (int((img_height)/2) - ROI_center_height) * cam_stage_transform_factor
                                    print(stage_move_row)
                                    # Move cell of interest to the center of field of view
                                    self.ludlStage.moveRel(xRel = stage_move_row, yRel= stage_move_col)
                                    
                                    time.sleep(1)
                                    
                                    # Move the cell back
                                    self.ludlStage.moveRel(xRel = -1*stage_move_row, yRel= -1*stage_move_col)
                                    time.sleep(1)
                            #--------------------------------------------------
                            
                            """
                            # =============================================================================
                            #         Execute pre-set operations at EACH COORDINATE.
                            # =============================================================================
                            """
                            self.inidividual_coordinate_operation(EachRound, EachWaveform, RowIndex, ColumnIndex)
                                
                            
                        time.sleep(0.6) # Wait for receiving data to be done.
                    time.sleep(0.5)
                    print('*************************************************************************************************************************')
                    
        #%%
        """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #                                                            Disconnect devices.
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""             
        
        # Switch off laser
        if len(self.RoundQueueDict['InsightEvents']) != 0:
            self.watchdog_flag = False
            time.sleep(0.5)

            self.Laserinstance.Close_TunableBeamShutter()

            time.sleep(0.5)
            
            self.Laserinstance.SaveVariables()
                     
            self.Laserinstance.Turn_Off_PumpLaser()
                    
        # Disconnect camera
        if self._use_camera == True:
            self.HamamatsuCam.Exit()
        
        # Disconnect focus motor
        try:
            self.pi_device_instance.CloseMotorConnection()
            print('Objective motor disconnected.')
        except:
            pass
示例#10
0
    def __init__(self, source_of_image = "PMT", init_search_range = 0.010, total_step_number = 5, imaging_conditions = {'edge_volt':5}, \
                 motor_handle = None, camera_handle = None, twophoton_handle = None, *args, **kwargs):
        """
        

        Parameters
        ----------
        source_of_image : string, optional
            The input source of image. The default is PMT.
        init_search_range : int, optional
            The step size when first doing coarse searching. The default is 0.010.
        total_step_number : int, optional
            Number of steps in total to find optimal focus. The default is 5.
        imaging_conditions : list
            Parameters for imaging.
            For PMT, it specifies the scanning voltage.
            For camera, it specifies the AOTF voltage and exposure time.
        motor_handle : TYPE, optional
            Handle to control PI motor. The default is None.
        twophoton_handle : TYPE, optional
            Handle to control Insight X3. The default is None.

        Returns
        -------
        None.

        """
        super().__init__(*args, **kwargs)
        
        # The step size when first doing coarse searching.
        self.init_search_range = init_search_range
        
        # Number of steps in total to find optimal focus.
        self.total_step_number = total_step_number
        
        # Parameters for imaging.
        self.imaging_conditions = imaging_conditions
        
        if motor_handle == None:
            # Connect the objective if the handle is not provided.
            self.pi_device_instance = PIMotor()
        else:
            self.pi_device_instance = motor_handle
        
        # Current position of the focus.
        self.current_pos = self.pi_device_instance.GetCurrentPos()

        # Number of steps already tried.
        self.steps_taken = 0
        # The focus degree of previous position.
        self.previous_degree_of_focus = 0
        # Number of going backwards.
        self.turning_point = 0
        # The input source of image.
        self.source_of_image = source_of_image
        if source_of_image == "PMT":
            self.galvo = RasterScan(Daq_sample_rate = 500000, edge_volt = self.imaging_conditions['edge_volt'])
        elif source_of_image == "Camera":
            if camera_handle == None:
                # If no camera instance fed in, initialize camera.
                self.HamamatsuCam_ins = CamActuator()
                self.HamamatsuCam_ins.initializeCamera()
            else:
                self.HamamatsuCam_ins = camera_handle