<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 SETUP
Step1:  Autosipper
Step2:  Manifold
Step3:  Micromanager
Step4:  Preset
Step5:  ACQUISITION
Step6:  MM Get info
Step7:  Video
Step8:  SNAP CV2
Step9:  EXIT
<ASSISTANT_TASK:>
Python Code:
import time
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
Explanation: SETUP
End of explanation
# config directory must have "__init__.py" file
# from the 'config' directory, import the following classes:
from config import Motor, ASI_Controller, Autosipper
from config import utils as ut
autosipper = Autosipper(Motor('config/motor.yaml'), ASI_Controller('config/asi_controller.yaml'))
autosipper.coord_frames
from config import gui
gui.stage_control(autosipper.XY, autosipper.Z)
# add/determine deck info
autosipper.coord_frames.deck.position_table = ut.read_delim_pd('config/position_tables/deck')
# check deck alignment
# CLEAR DECK OF OBSTRUCTIONS!!
autosipper.go_to('deck', ['name'],'align')
# add plate
from config import utils as ut
platemap = ut.generate_position_table((8,8),(9,9),93.5)
platemap[]
ut.lookup(platemap)
Explanation: Autosipper
End of explanation
from config import Manifold
manifold = Manifold('192.168.1.3', 'config/valvemaps/valvemap.csv', 512)
manifold.valvemap[manifold.valvemap.name>0]
def valve_states():
    tmp = []
    for i in [2,0,14,8]:
        status = 'x'
        if manifold.read_valve(i):
            status = 'o'
        tmp.append([status, manifold.valvemap.name.iloc[i]])
    return pd.DataFrame(tmp)
tmp = []
for i in range(16):
    status = 'x'
    if manifold.read_valve(i):
        status = 'o'
    name = manifold.valvemap.name.iloc[i]
    tmp.append([status, name])
pd.DataFrame(tmp).replace(np.nan, '')
name = 'inlet_in'
v = manifold.valvemap['valve'][manifold.valvemap.name==name]
v=14
manifold.depressurize(v)
manifold.pressurize(v)
manifold.exit()
Explanation: Manifold
End of explanation
# !!!! Also must have MM folder on system PATH
# mm_version = 'C:\Micro-Manager-1.4'
# cfg = 'C:\Micro-Manager-1.4\SetupNumber2_05102016.cfg'
mm_version = 'C:\Program Files\Micro-Manager-2.0beta'
cfg = 'C:\Program Files\Micro-Manager-2.0beta\Setup2_20170413.cfg'
import sys
sys.path.insert(0, mm_version) # make it so python can find MMCorePy
import MMCorePy
from PIL import Image
core = MMCorePy.CMMCore()
core.loadSystemConfiguration(cfg)
core.setProperty("Spectra", "White_Enable", "1")
core.waitForDevice("Spectra")
core.setProperty("Cam Andor_Zyla4.2", "Sensitivity/DynamicRange", "16-bit (low noise & high well capacity)") # NEED TO SET CAMERA TO 16 BIT (ceiling 12 BIT = 4096)
core.setProperty("Spectra", "White_Enable", "0")
Explanation: Micromanager
End of explanation
log = []
autosipper.Z.move(93.5)
manifold.depressurize(2)
manifold.depressurize(0)
log.append([time.ctime(time.time()), 'open inlet_in, inlet_out'])
valve_states()
text = 'fluorescence observed'
log.append([time.ctime(time.time()), text])
text = 'CLOSE inlet_out'
manifold.pressurize(0)
log.append([time.ctime(time.time()), text])
text = 'OPEN chip_in, chip_out'
manifold.depressurize(14)
manifold.depressurize(8)
log.append([time.ctime(time.time()), text])
valve_states()
text = 'fill'
log.append([time.ctime(time.time()), text])
manifold.pressurize(8)
#closed all
autosipper.Z.move(93.5)
manifold.depressurize(2)
manifold.depressurize(0)
log.append([time.ctime(time.time()), 'open inlet_in, inlet_out'])
valve_states()
text = 'fluorescence removed'
log.append([time.ctime(time.time()), text])
text = 'CLOSE inlet_out'
manifold.pressurize(0)
log.append([time.ctime(time.time()), text])
text = 'OPEN chip_in, chip_out'
manifold.depressurize(14)
manifold.depressurize(8)
log.append([time.ctime(time.time()), text])
valve_states()
text = 'flush'
log.append([time.ctime(time.time()), text])
manifold.pressurize(8)
for i in [2,0,14,8]:
    manifold.pressurize(i)
Explanation: Preset: 1_PBP 
ConfigGroup,Channel,1_PBP,TIFilterBlock1,Label,1-PBP
Preset: 2_BF  
ConfigGroup,Channel,2_BF,TIFilterBlock1,Label,2-BF
Preset: 3_DAPI 
ConfigGroup,Channel,3_DAPI,TIFilterBlock1,Label,3-DAPI
Preset: 4_eGFP 
ConfigGroup,Channel,4_eGFP,TIFilterBlock1,Label,4-GFP
Preset: 5_Cy5 
ConfigGroup,Channel,5_Cy5,TIFilterBlock1,Label,5-Cy5
Preset: 6_AttoPhos 
ConfigGroup,Channel,6_AttoPhos,TIFilterBlock1,Label,6-AttoPhos
TEST
4.5 psi, 25 psi valves
End of explanation
log
core.setConfig('Channel','2_BF')
core.setProperty(core.getCameraDevice(), "Exposure", 20)
core.snapImage()
img = core.getImage()
plt.imshow(img,cmap='gray')
image = Image.fromarray(img)
# image.save('TESTIMAGE.tif')
position_list = ut.load_mm_positionlist("C:/Users/fordycelab/Desktop/D1_cjm.pos")
position_list
def acquire():
    for i in xrange(len(position_list)):
        si = str(i)
        x,y = position_list[['x','y']].iloc[i]
        core.setXYPosition(x,y)
        core.waitForDevice(core.getXYStageDevice())
        logadd(log, 'moved '+si)
        
        core.snapImage()
#         core.waitForDevice(core.getCameraDevice())
        logadd(log, 'snapped '+si)
        
        img = core.getImage()
        logadd(log, 'got image '+si)
        
        image = Image.fromarray(img)
        image.save('images/images_{}.tif'.format(i))
        logadd(log, 'saved image '+si)
    
    x,y = position_list[['x','y']].iloc[0]
    core.setXYPosition(x,y)
    core.waitForDevice(core.getXYStageDevice())
    logadd(log, 'moved '+ str(0))
        
def logadd(log,st):
    log.append([time.ctime(time.time()), st])
    print log[-1]
# Auto
core.setAutoShutter(True) # default
core.snapImage()
# Manual
core.setAutoShutter(False) # disable auto shutter
core.setProperty("Shutter", "State", "1")
core.waitForDevice("Shutter")
core.snapImage()
core.setProperty("Shutter", "State", "0")
Explanation: ACQUISITION
End of explanation
core.getFocusDevice()
core.getCameraDevice()
core.XYStageDevice()
core.getDevicePropertyNames(core.getCameraDevice())
Explanation: MM Get info
End of explanation
import cv2
from IPython import display
import numpy as np
from ipywidgets import widgets
import time
# core.initializeCircularBuffer()
# core.setCircularBufferMemoryFootprint(4096) # MiB
cv2.WND
# video with button (CV2)
live = widgets.Button(description='Live')
close = widgets.Button(description='Close')
display.display(widgets.HBox([live, close]))
 
def on_live_clicked(b):
    display.clear_output(wait=True)
    print 'LIVE'
    core.startContinuousSequenceAcquisition(1000) # time overridden by exposure
    time.sleep(.2)
    cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
    cv2.setWindowProperty('Video', cv2.WND_PROP_ASPECT_RATIO, cv2.WINDOW_KEEPRATIO)
    cv2.resizeWindow('Video', 500,500)
    img = np.zeros((500,500))
    print 'To stop, click window + press ESC'
    while(1):
        time.sleep(.015)
        if core.getRemainingImageCount() > 0:
            img = core.getLastImage()
        cv2.imshow('Video',img)
        k = cv2.waitKey(30)
        if k==27: # ESC key; may need 255 mask?
            break
    print 'STOPPED'
    core.stopSequenceAcquisition()
def on_close_clicked(b):
    if core.isSequenceRunning():
        core.stopSequenceAcquisition()
    cv2.destroyWindow('Video')
        
live.on_click(on_live_clicked)
close.on_click(on_close_clicked)
# video with button (CV2)
# serial snap image
live = widgets.Button(description='Live')
close = widgets.Button(description='Close')
display.display(widgets.HBox([live, close]))
 
def on_live_clicked(b):
    display.clear_output(wait=True)
    print 'LIVE'
    cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
    cv2.setWindowProperty('Video', cv2.WND_PROP_ASPECT_RATIO, cv2.WINDOW_KEEPRATIO)
    cv2.resizeWindow('Video', 500,500)
    img = np.zeros((500,500))
    print 'To stop, click window + press ESC'
    while(1):
        core.snapImage()
        time.sleep(.05)
        img = core.getImage()
        cv2.imshow('Video',img)
        k = cv2.waitKey(30)
        if k==27: # ESC key; may need 255 mask?
            break
    print 'STOPPED'
def on_close_clicked(b):
    if core.isSequenceRunning():
        core.stopSequenceAcquisition()
    cv2.destroyWindow('Video')
        
live.on_click(on_live_clicked)
close.on_click(on_close_clicked)
cv2.destroyAllWindows()
Explanation: Video
End of explanation
# snap (CV2)
snap = widgets.Button(description='Snap')
close2 = widgets.Button(description='Close')
display.display(widgets.HBox([snap, close2]))
                
def on_snap_clicked(b):
    cv2.destroyWindow('Snap')
    cv2.namedWindow('Snap',cv2.WINDOW_NORMAL)
    cv2.resizeWindow('Snap', 500,500)
    cv2.setWindowProperty('Snap', cv2.WND_PROP_ASPECT_RATIO, cv2.WINDOW_KEEPRATIO)
    core.snapImage()
    time.sleep(.1)
    img = core.getImage()
    cv2.imshow('Snap',img)
    k = cv2.waitKey(30)
    
def on_close2_clicked(b):
    cv2.destroyWindow('Snap')
        
snap.on_click(on_snap_clicked)
close2.on_click(on_close2_clicked)
Explanation: SNAP CV2
End of explanation
autosipper.exit()
manifold.exit()
core.unloadAllDevices()
core.reset()
print 'closed'
Explanation: EXIT
End of explanation
<END_TASK>