コード例 #1
0
 def run_mda(self,scope):
     """ Run an MDA.
     
     args: initialized scope object
     """
     ## check for MDA setup
     if self.path_set_up is False: self.setup_path()
     if self.verbose: scope.verbose=True
     if self.pos_order==[]: print "Provide positions"; return None
     if self.channels=={}: print "Provide channels"; return None
     if self.channel_type=="": print "Provide channel format"; return None
     
     ## dependencies must have been met, continue with MDA
     try:        
         for t in range(0,self.n_timepoints):
             try:
                 t=t ## for easier reporting on progress
                 acquisition_start=time.time()
                 for pos_label in self.pos_order:
                     try: ## try positions
                         pos_label=pos_label ## for easier reporting on progress
                         print "####################%s###################" % pos_label
                         if self.verbose is True: t1=time.time()
                         pos=self.positions[pos_label]
                         x_target,y_target,z_target=pos.get_xyz()
                         afc_target=pos.get_afc_offset()
                         if self.verbose is True: move_t0=time.time()
                         scope.mmc.enableContinuousFocus(True) ## to enable afc hold during move
                         scope.move_XYstage(x_target,y_target,blocking=True)
                         time.sleep(1) ## wait a while once you are there to make sure it focus
                         ## To do , maybe add applying the first settings here
                         if self.verbose is True:
                             deltaT=time.time()-move_t0
                             print "Arrived at location %s in %.2f seconds" % (pos_label,deltaT)
                             x_final,y_final,z_final,afc_final=scope.get_stage_loc()
                             print " Now at %sx %sy %sz %s afc" % (x_final,
                                                                 y_final,
                                                                 z_final,
                                                                 afc_final)
                             focus_t0=time.time()
                         scope.mmc.enableContinuousFocus(False)
                         time.sleep(1) ## wait a bit for focus to be turned off
                         scope.move_to_offset(afc_target,post_focus_delay=.1,attempt_delay=.1)
                         if self.verbose is True:
                             deltaT=time.time()-focus_t0
                             x_final,y_final,z_final,afc_final=scope.get_stage_loc()
                             print "Focus after %s seconds" % deltaT
                             print "Now at afc: %s (target: %s)" % (afc_final,afc_target)
                         
                         ## set up channels
                         print "Channels to image at this position (%s)t%s: %s" % (pos_label,t,self.channels[pos_label][t])
                         c_to_image = self.channels[pos_label][t]
                         ## set up z stack
                         z_params = self.z_params
                         if t in self.specialized_z_params:
                             if pos_label in self.specialized_z_params[t]:
                                 for c in self.specialized_z_params[t][pos_label]:
                                         z_params[c]=self.specialized_z_params[t][pos_label][c]
                                         if self.verbose:
                                             print "Z stack updated; taking %s at c %s %s t%s" % (z_params[c],c,pos_label,t)
                         ## take images
                         if self.verbose:image_t0=time.time()
                         images,metadata=scope.multichannel_zstack(c_to_image,
                                                                 z_params=z_params,
                                                                 md_format="list")
                         if self.verbose:
                             deltaT=time.time()-image_t0
                             print "Finished images %s: %s seconds" % (pos_label,deltaT)
                         
                         ## save the images
                         for c in range(0,len(c_to_image)):
                             stack=images[c]
                             md_stack=metadata[c]
                             if self.channel_type == "MM Config": 
                                 channel_name = c_to_image[c][1]
                             elif self.channel_type == "Illuminations":
                                 channel_name = c_to_image[c].name
                             full_save_dir=os.path.join(self.save_dir,
                                                     pos_label,
                                                     channel_name)
                             full_file_name="%s_%s_c%s%s_t%s" % (self.file_name_prefix,
                                                                 pos_label,
                                                                 c,
                                                                 channel_name,
                                                                 t)
                             md=md_stack[len(md_stack)/2]
                             md.append("ImagingLoop:Timepoint=%s" % t)
                             md.append("ImagingLoop:Position=%s" % pos_label)
                             md.append("ImagingLoop:Channel=%s" % channel_name)
                             try:
                                 channel_description = self.experiment_metadata["ChannelMapping"]["ChannelDescription"][channel_name]
                             except:
                                 channel_description = "Error"
                             md.append("ImagingLoop:ChannelDescription=%s" % channel_description)
                             try: 
                                 channel_purpose = self.experiment_metadata["ChannelMapping"]["ChannelPurpose"][channel_name]
                             except:
                                 channel_purpose = "Error"
                             md.append("ImagingLoop:ChannelPurpose=%s" % channel_purpose)
                             try: 
                                 microfluidic_device_id = self.experiment_metadata["MicrofluidicDeviceId"]
                             except:
                                 microfluidic_device_id = "Error"
                             md.append("ExperimentData:MicrofluidicDeviceId=%s" % microfluidic_device_id)
                             try: 
                                 microfluidic_device_type = self.experiment_metadata["MicrofluidicDeviceType"]
                             except:
                                 microfluidic_device_type = "Error"
                             md.append("ExperimentData:MicrofluidicDeviceType=%s" % microfluidic_device_type)
                             try: 
                                 microfluidic_channel = self.experiment_metadata["MicrofluidicChannel"][pos_label]
                             except:
                                 microfluidic_channel = "Error"
                                 print "Unable to map position to 'microfluidic_channel'"
                                 print pos_label
                             md.append("ExperimentData:MicrofluidicChannel=%s" % microfluidic_channel)
                             try: 
                                 strain = self.experiment_metadata["Strain"][microfluidic_channel]
                             except:
                                 strain = "Error"
                             md.append("ExperimentData:Strain=%s" % strain)
                             try: 
                                 media = self.experiment_metadata["Media"][microfluidic_channel]
                                 if isinstance(media,dict):
                                     media=media[t]
                             except:
                                 media = "Error"
                             md.append("ExperimentData:Media=%s" % media)
                             try: 
                                 flow_rate = self.experiment_metadata["FlowRate"][microfluidic_channel]
                                 if isinstance(flow_rate,dict):
                                     flow_rate=flow_rate[t]
                             except:
                                 flow_rate = "Error"
                             md.append("ExperimentData:FlowRate=%s" % flow_rate)
                             try:
                                 file_out.stack_writer(full_save_dir,
                                                 full_file_name,
                                                 stack,
                                                 md)
                             except:
                                 try:
                                     if self.backup_save_path:
                                         if  os.path.isdir(self.backup_save_path) is False:
                                             os.mkdir(self.backup_save_path)
                                         file_out.stack_writer(self.backup_save_path,
                                                             full_file_name,
                                                             stack,
                                                             md)
                                         print "Warning! Your first save path failed"
                                         print "%s written to backup save path" % full_file_name
                                     else:
                                         print "You did not have the backup path set"
                                         print "%s was not saved" % full_file_name
                                 except:
                                     print "Warning! Your backup save failed."
                                         
                         deltaT_acquistion=time.time()-acquisition_start
                         print "%s completed after %s seconds from start of timeloop" % (pos_label,deltaT_acquistion)
                         if self.verbose:
                             deltaT=time.time()-t1
                             print "Position took a total of % seconds" % deltaT
                         #scope.mmc.enableContinuousFocus(True)
                     except KeyboardInterrupt:
                         raise
                     except: 
                         print "Position aborted because of error!!!"
                         print "Attempting to reconnect to the scope"
                         scope.disconnect()
                         time.sleep(10)
                         scope.reconnect()    
             except KeyboardInterrupt:
                 raise
             except:
                 print "Timepoint aborted because of error!!!"
             scope.move_to_offset(afc_target)
             x_next=self.positions[self.pos_order[0]].get_x()
             y_next=self.positions[self.pos_order[0]].get_y()
             scope.mmc.enableContinuousFocus(True)
             time.sleep(2)
             scope.move_XYstage(x_next,y_next,blocking=True)
             #scope.mmc.enableContinuousFocus(False)
             acquisition_finish_time=time.time()
             until_next=self.frequency-(acquisition_finish_time-acquisition_start)
             scope.mmc.setProperty("TL-Shutter","State",0)
             verbose_sleeper(until_next)
         print "Timelapse completed"
         results="Scan Completed" 
         return results
     except KeyboardInterrupt:
         print "print Timelapse was interupted after %s timepoints" % t
コード例 #2
0
 def image_position(self,
                    scope,
                    pos_label,
                    t,
                    x_target,
                    y_target,
                    afc_target,
                    c_to_image,
                    z_params,
                    delay_after_move):
     ##move stage
     if self.afc_on_during_move:
         scope.afc_on()
     try: ##moving XYstage
         scope.move_XYstage(x_target,
                            y_target,
                            blocking=True)
     except KeyboardInterrupt:
         raise CancelTimeLapse(pos_label=pos_label,
                               t=t)
     except:
         raise StageMovementError
     time.sleep(delay_after_move)
     try: ##focusing to offset
         if self.afc_on_during_move:
             scope.mmc.enableContinuousFocus(False)
             autofocus_device = scope.mmc.getAutoFocusDevice()
             scope.mmc.waitForDevice(autofocus_device)
             time.sleep(1)
         scope.move_to_offset(afc_target,
                              post_focus_delay=.1,
                              attempt_delay=.1)
     except UnableToFocusAFC :
         if self.force_focus:
             raise
         else:
             pass
     try:
         images,metadata=scope.multichannel_zstack(
             c_to_image,
             z_params=z_params)
     except ConfigError:
         print "#!#!"*10
         print "Error in MDA configuration. Please fix the configurations file."
         print "#!#!"*10
         raise
     except:
         print "This wasnt the exception that i wanted"
         raise ImageAcquisitionError()
     for c in range(0,len(c_to_image)):
         stack=images[c]
         md_stack=metadata[c]
         channel_name=c_to_image[c][1]
         full_save_dir=os.path.join(
             self.save_dir,
             pos_label,
             channel_name)
         full_file_name="%s_%s_c%s%s_t%s" % (
             self.file_name_prefix,
             pos_label,
             c,
             channel_name,
             t)
         md=md_stack[len(md_stack)/2]
         md=self.append_experimental_md(md,
                                        t,
                                        pos_label,
                                        channel_name)
         try:
             ## TODO handle saving of images as a child process?
              file_out.stack_writer(full_save_dir,
                 full_file_name,
                 stack,
                 md)
              self.file_log.append((full_file_name,full_save_dir))
         except:
             try:
                if self.backup_save_path:
                    if  os.path.isdir(self.backup_save_path) is False:
                        os.mkdir(self.backup_save_path)
                    file_out.stack_writer(self.backup_save_path,
                                        full_file_name,
                                        stack,
                                        md)
                    print "Warning! Your first save path failed"
                    print "%s written to backup save path" % full_file_name
                    self.file_log.append((full_file_name,self.backup_save_path))
                else:
                    print "You did not have the backup path set"
                    print "%s was not saved" % full_file_name
             except:
                 #TODO: Handle this case a little better?
                 print "Warning! Your backup save failed."
                 print "Proceeding with timelapse..."
     for setting in self.end_location_settings:
         try:
             device_label,prop,value=setting
             self.scope.property_changer(device_label,prop,value,blocking=False)
         except:
             print "Unable to apply setting: %s" % setting
             pass