Skip to content

Commit

Permalink
latest demo changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollman committed May 23, 2017
1 parent 2ed6fa7 commit 1ddc2af
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 197 deletions.
2 changes: 1 addition & 1 deletion FocusCorrectionPlaneWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,5 @@ def handle_open_dialog(self):
win = FocusCorrectionPlaneWindow(plist,imgSrc)
win.show()
app.exec_()
imgSrc.mmc.reset()
imgSrc.shutdown()
sys.exit()
117 changes: 36 additions & 81 deletions LiveMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pyqtgraph.ptime as ptime
import time
from skimage import img_as_ubyte,exposure
import MMCorePy
import cv2
from pyqtgraph.widgets.RawImageWidget import RawImageWidget
import functools
Expand All @@ -21,13 +20,12 @@ def __init__(self,imgSrc,exposure_times=dict([]),channelGroup="Channels"):
self.channelGroup=channelGroup
self.exposure_times=exposure_times
#self.setContentsMargins(0,0,0,0)
self.mmc = imgSrc.mmc

self.imgSrc = imgSrc
self.channels=self.mmc.getAvailableConfigs(self.channelGroup)
self.channels=self.imgSrc.get_channels()
self.init_mmc()
self.initUI()


self.i = 0
self.updateTime = ptime.time()
self.fps = 0
Expand All @@ -36,23 +34,18 @@ def __init__(self,imgSrc,exposure_times=dict([]),channelGroup="Channels"):

def init_mmc(self):
#filename="C:\Users\Smithlab\Documents\ASI_LUM_RETIGA_CRISP.cfg"
#self.mmc.loadSystemConfiguration(filename)
#self.mmc.enableStderrLog(False)
#self.mmc.enableDebugLog(False)
# # mmc.setCircularBufferMemoryFootprint(100)
self.cam=self.mmc.getCameraDevice()
self.mmc.setExposure(50)
#self.mmc.setProperty(self.cam, 'Gain', 1)


self.imgSrc.set_exposure(50)

Nch=len(self.channels)
startChan=self.channels[Nch-1]
for ch in self.channels:
if 'dapi' in ch.lower():
self.mmc.setConfig(self.channelGroup,ch)
self.mmc.waitForConfig(self.channelGroup,ch)
camera = self.mmc.getCameraDevice()
self.mmc.setProperty(camera,'Binning','1x1')
print 'Binning is:', self.mmc.getProperty(camera,'Binning')
self.mmc.startContinuousSequenceAcquisition(1)
self.imgSrc.set_channel(ch)
self.imgSrc.set_binning(1)
print 'Binning is:', self.imgSrc.get_binning()
self.startContinuousSequenceAcquisition(1)

def initUI(self):

Expand Down Expand Up @@ -103,7 +96,7 @@ def initUI(self):
if ch in keys:
spnBox.setValue(self.exposure_times[ch])
else:
spnBox.setValue(self.mmc.getExposure())
spnBox.setValue(self.imgSrc.get_exposure())
spnBox.setSuffix(" ms")
btn.clicked.connect(self.make_channelButtonClicked(ch,spnBox))
self.expSpnBoxes.append(spnBox)
Expand Down Expand Up @@ -145,23 +138,22 @@ def getExposureTimes(self):
return exposure_times

def setExposureAuto(self,evt):

self.mmc.stopSequenceAcquisition()
self.imgSrc.stopSequenceAcquisition()
perc=95; #the goal is to make the X percentile value equal to Y percent of the maximum value
#perc is X
desired_frac=.7 #desired_frac is Y
max_exposure = 3000 #exposure times shall not end up more than this
close_frac = .2 #fractional change in exposure for which we will just trust the math
bit_depth=self.mmc.getImageBitDepth()
max_val=np.power(2,bit_depth)
max_val=self.imgSrc.get_max_pixel_value()

#loop over the channels
for i,ch in enumerate(self.channels):
img_counter =0 #counter to count how many snaps it takes us
if 'Dark' not in ch: #don't set the 'Dark' channel for obvious reasons
print ch
#setup to use the channel
self.mmc.setConfig(self.channelGroup,ch)
self.mmc.waitForConfig(self.channelGroup,ch)
self.imgSrc.set_channel(ch)


#get current exposure
Expand All @@ -172,10 +164,10 @@ def setExposureAuto(self,evt):
#follow loop till we get it right
while 1:

self.mmc.setExposure(curr_exposure)
self.mmc.snapImage()
self.imgSrc.set_exposure(curr_exposure)
img=self.imgSrc.snap_image()
img_counter+=1
img=self.mmc.getImage()

vec=img.flatten()

#the value which is at the perc percentile
Expand Down Expand Up @@ -234,20 +226,17 @@ def make_channelButtonClicked(self,ch,spnBox):
def channelButtonClicked():
#print ch
#print spnBox.value()
self.mmc.stopSequenceAcquisition()
self.mmc.clearCircularBuffer()
self.mmc.setConfig(self.channelGroup,ch)
self.imgSrc.stopSequenceAcquisition()
self.imgSrc.set_channel(ch)
expTime=spnBox.value()
self.mmc.setExposure(expTime)
self.mmc.waitForConfig(self.channelGroup,ch)
self.mmc.startContinuousSequenceAcquisition(expTime)
self.imgSrc.set_exposure(expTime)
self.imgSrc.startContinuousSequenceAcquisition(expTime)
return channelButtonClicked

def closeEvent(self,evt):
self.mmc.stopSequenceAcquisition()
camera = self.mmc.getCameraDevice()
self.mmc.setProperty(camera,'Binning','2x2')
print 'Binning is:', self.mmc.getProperty(camera,'Binning')
self.imgSrc.stopSequenceAcquisition()
self.imgSrc.set_binning(2)
print 'Binning is:', self.get_binning()
print "stopped acquisition"
#if self.timer is not None:
# print "cancelling timer if it exists"
Expand Down Expand Up @@ -276,59 +265,25 @@ def lut_convert16as8bit(self,image, display_min, display_max) :

def updateData(self):

remcount = self.mmc.getRemainingImageCount()
#print 'remcount',remcount
#remcount=0
if remcount > 0:
#rgb32 = self.mmc.popNextImage()
data = self.mmc.getLastImage()


if data.dtype == np.uint16:
maxval=self.imgSrc.get_max_pixel_value()
#print "max val is",maxval
#print 'max_before',np.max(data)
#data = exposure.rescale_intensity(data,in_range=(0,maxval))
#print 'max after rescale',np.max(data)
#data = img_as_ubyte(data)
#data=self.lut_convert16as8bit(data,0,5000)

data = self.imgSrc.get_image()
maxval=self.imgSrc.get_max_pixel_value()
self.img.setImage(data,autoLevels=True)

# "maxval",maxval,np.max(data),data.dtype

flipx,flipy,trans = self.imgSrc.get_image_flip()
if trans:
data = np.transpose(data)
if flipx:
data = np.fliplr(data)
if flipy:
data = np.flipud(data)
data = np.rot90(data,k=3)
#gray=cv2.equalizeHist(gray)
self.img.setImage(data,autoLevels=True)
#cv2.imshow('Video', gray)
#else:
#print('No frame')

if not self.ended:
self.timer = QtCore.QTimer.singleShot(self.mmc.getExposure(), self.updateData)
self.timer = QtCore.QTimer.singleShot(self.imgSrc.get_exposure(), self.updateData)
now = ptime.time()
fps1 = 1.0 / (now-self.updateTime)
self.updateTime = now
self.fps = self.fps * 0.6 + fps1 * 0.4
if self.i == 0:
print "%0.1f fps" % self.fps


#def myExitHandler(mmc):

#mmc.stopSequenceAcquisition()
#QtGui.QApplication.quit()
#mmc.reset()

def launchLive(mmc,exposure_times):
def launchLive(imgSrc,exposure_times):
import sys

vidview = VideoView(mmc,exposure_times)
vidview = VideoView(imgSrc,exposure_times)
vidview.setGeometry(250,50,1100,1000)
vidview.show()

Expand All @@ -347,18 +302,18 @@ def launchLive(mmc,exposure_times):
app = QtGui.QApplication(sys.argv)
faulthandler.enable()

#mmc = MMCorePy.CMMCore()

defaultMMpath = "C:\Program Files\Micro-Manager-1.4"
configFile = QtGui.QFileDialog.getOpenFileName(
None, "pick a uManager cfg file", defaultMMpath, "*.cfg")
configFile = str(configFile.replace("/", "\\"))
print configFile
imgSrc = imageSource(configFile)
#mmc.loadSystemConfiguration(configFile)

print "loaded configuration file"
launchLive(imgSrc,dict([]))
#app.exec_()
print "got out of the event loop"
imgSrc.mmc.reset()
imgSrc.shutdown()
print "reset micromanager core"
sys.exit()
7 changes: 6 additions & 1 deletion MMArduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def __init__(self,port = 'COM8',baudrate = '57600'):
self.ser.timeout =.5
self.ser.open()
time.sleep(2)
self.ser.readlines()
self.sendCmd(chr(27))
self.sendCmd(chr(27))
self.sendCmd(chr(27))
self.sendCmd(chr(27))
def sendMessage(self,message,debug=False):
cmd = bytearray(message)
answer = self.sendDirect(message,len(message))
Expand Down Expand Up @@ -88,7 +93,7 @@ def setTriggerPolarity(self,triggerHigh = True):
return self.sendMessage([23,0])
else:
return self.sendMessage([23,1])

def setupExposure(self,exposures,interframe=10,exp_pattern=[0,0,0,0,0,0,0,1]):
i =0
for exp in exposures:
Expand Down
48 changes: 16 additions & 32 deletions MosaicPlanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from MyLasso import MyLasso
from MosaicImage import MosaicImage
from Transform import Transform,ChangeTransform
from imageSourceMM import imageSource
from imageSourceDemo import imageSource
from MMPropertyBrowser import MMPropertyBrowser
from ASI_Control import ASI_AutoFocus
from FocusCorrectionPlaneWindow import FocusCorrectionPlaneWindow
Expand Down Expand Up @@ -510,7 +510,7 @@ def handle_close(self,evt=None):
print "handling close"
#if not self.mosaicImage == None:
# self.mosaicImage.cursor_timer.cancel()
self.imgSrc.mmc.unloadAllDevices()
self.imgSrc.shutdown()

def on_load(self,rootPath):
self.rootPath = rootPath
Expand Down Expand Up @@ -675,29 +675,8 @@ def ResetPiezo(self):

do_stage_reset=self.cfg['StageResetSettings']['enableStageReset']
if do_stage_reset:
z_label = self.cfg['StageResetSettings']['compensationStage']
piezo_label = self.cfg['StageResetSettings']['resetStage']
min_threshold = self.cfg['StageResetSettings']['minThreshold']
max_threshold = self.cfg['StageResetSettings']['maxThreshold']
reset_position = self.cfg['StageResetSettings']['resetPosition']
invert_compensation = self.cfg['StageResetSettings']['invertCompensation']

piezo = self.imgSrc.mmc.getPosition(piezo_label)
if (piezo<min_threshold) or (piezo>max_threshold):
z = self.imgSrc.mmc.getPosition(z_label)
islocked = self.imgSrc.mmc.isContinuousFocusEnabled()

if islocked:
self.imgSrc.mmc.enableContinuousFocus(False)

if invert_compensation:
self.imgSrc.mmc.setPosition(z_label,z+(piezo-reset_position))
else:
self.imgSrc.mmc.setPosition(z_label,z-(piezo-reset_position))
self.imgSrc.mmc.setPosition(piezo_label,reset_position)

if islocked:
self.imgSrc.mmc.enableContinuousFocus(True)
self.imgSrc.reset_piezo(self.cfg['StageResetSettings'])


def summarize_stage_settings(self):
do_stage_reset = self.cfg['StageResetSettings']['enableStageReset']
Expand Down Expand Up @@ -829,7 +808,7 @@ def execute_imaging(self,pos_list,numFrames,numSections,channel_settings,chrome_
if pos.activated:
if not goahead:
break
if not self.imgSrc.mmc.isContinuousFocusEnabled():
if not self.imgSrc.get_hardware_autofocus_state():
print "autofocus not enabled when moving between sections.. "
goahead=False
break
Expand All @@ -844,7 +823,7 @@ def execute_imaging(self,pos_list,numFrames,numSections,channel_settings,chrome_
if not goahead:
print "breaking out!"
break
if not self.imgSrc.mmc.isContinuousFocusEnabled():
if not self.imgSrc.get_hardware_autofocus_state():
print "autofocus no longer enabled while moving between frames.. quiting"
goahead = False
break
Expand Down Expand Up @@ -966,7 +945,8 @@ def on_run_acq(self,event="none"):


goahead = True
if not self.imgSrc.mmc.isContinuousFocusEnabled():

if not self.imgSrc.get_hardware_autofocus_state():
self.slack_notify('HELP! lost autofocus on way to first position',notify=True)
print 'HELP! lost autofocus on way to first position'
goahead=False
Expand All @@ -979,7 +959,7 @@ def on_run_acq(self,event="none"):
if pos.activated:
if not goahead:
break
if not self.imgSrc.mmc.isContinuousFocusEnabled():
if not self.imgSrc.get_hardware_autofocus_state():
self.slack_notify('HELP! lost autofocus between sections',notify=True)
goahead=False
break
Expand All @@ -999,7 +979,7 @@ def on_run_acq(self,event="none"):
if not goahead:
print "breaking out!"
break
if not self.imgSrc.mmc.isContinuousFocusEnabled():
if not self.imgSrc.get_hardware_autofocus_state():
self.slack_notify('HELP! lost autofocus between frames',notify=True)
print "autofocus no longer enabled while moving between frames.. quiting"
goahead = False
Expand Down Expand Up @@ -1132,6 +1112,10 @@ def launch_MManager_browser(self, event=None):
win = MMPropertyBrowser(self.imgSrc.mmc)
win.show()

def launch_retake(self,event=None):
if self.retakeView is NOne:
self.retakeView = Retake(self)

def launch_snap(self, event=None):
if self.snapView is None:
self.imgSrc.set_binning(1)
Expand Down Expand Up @@ -1663,7 +1647,7 @@ def on_run_multi_acq(self,event="none"): #MultiRibbons
if pos.activated:
if not goahead:
break
if not self.imgSrc.mmc.isContinuousFocusEnabled():
if not self.imgSrc.get_hardware_autofocus_state():
self.slack_notify('HELP! lost autofocus between frames',notify=True)
print "autofocus not enabled when moving between sections.. "
goahead=False
Expand All @@ -1682,7 +1666,7 @@ def on_run_multi_acq(self,event="none"): #MultiRibbons
if not goahead:
print "breaking out!"
break
if not self.imgSrc.mmc.isContinuousFocusEnabled():
if not self.imgSrc.get_hardware_autofocus_state():
self.slack_notify('HELP! lost autofocus between frames',notify=True)
print "autofocus no longer enabled while moving between frames.. quiting"
goahead = False
Expand Down
4 changes: 2 additions & 2 deletions PositionList.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class posListSchema(mm.Schema):
camera_settings = mm.Nested(CameraSettingsSchema,required=True)
slicePositions = mm.List(slicePositionSchema)
dosort = mm.fields.Bool(required=False,default=True)
numberDisplaySettings = mm.Nested(NumberDisplaySettings)
numberDisplaySettings = mm.Nested(NumberDisplaySettingsSchema)

class posList():
"""class for holding, altering, and plotting the position list"""
Expand Down Expand Up @@ -968,7 +968,7 @@ class slicePositionSchema(mm.Schema):
activated = mm.fields.Bool(required=False,default=True)
withpoint = mm.fields.Bool(required=False,default=True)
number = mm.fields.Int(required=True)
numberDisplaySettings = mm.Nested(NumberDisplaySettings)
numberDisplaySettings = mm.Nested(NumberDisplaySettingsSchema)
self.frameList = mm.Nested(posListSchema)


Expand Down
Loading

0 comments on commit 1ddc2af

Please sign in to comment.