Skip to content

Commit

Permalink
Merge pull request #198 from mehta-lab/mmc_reconnect
Browse files Browse the repository at this point in the history
fixing the issue to connect and reconnect to MM
  • Loading branch information
talonchandler authored Oct 1, 2022
2 parents d4c1024 + 58e2800 commit 546b291
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions recOrder/plugin/widget/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def __init__(self, napari_viewer: Viewer):

# Setup Connections between elements
# Connect to MicroManager
self.ui.qbutton_mm_connect.clicked[bool].connect(self.connect_to_mm)

# I'm disconnecting and hiding this button for 0.2.0, TODO: reinstate for 1.0.0
# In 1.0.0 we'll only have a single "Switch to Online/Offline" button in the "Calibration tab"

# self.ui.qbutton_mm_connect.clicked[bool].connect(self.connect_to_mm)
self.ui.qbutton_mm_connect.hide()

# Calibration Tab

# Remove QT creator calibration mode items
Expand Down Expand Up @@ -205,6 +209,7 @@ def __init__(self, napari_viewer: Viewer):

# Instantiate Attributes:
self.gui_mode = 'offline'
self.bridge = None
self.mm = None
self.mmc = None
self.calib = None
Expand Down Expand Up @@ -484,7 +489,7 @@ def _hide_acquisition_ui(self, val: bool):
self.ui.le_mm_status.setStyleSheet("border: 1px solid yellow;")
self.mmc = None
self.mm = None
self.ui.cb_config_group.clear()
# self.ui.cb_config_group.clear() # this might be the culprit because it clear the config
self.ui.tabWidget.setCurrentIndex(0)

def _hide_offline_ui(self, val: bool):
Expand Down Expand Up @@ -1251,13 +1256,19 @@ def change_gui_mode(self):
self._hide_acquisition_ui(False)
self.gui_mode = 'online'
self.connect_to_mm()

else:
self.ui.qbutton_gui_mode.setText('Switch to Online')
self.ui.le_gui_mode.setText('Offline')
self.ui.le_gui_mode.setStyleSheet("border: 1px solid rgb(200,0,0); color: rgb(200,0,0);")
self._hide_offline_ui(False)
self._hide_acquisition_ui(True)
self.gui_mode = 'offline'
self.ui.cb_config_group.clear()

#Make sure button is still visible
self.ui.qbutton_mm_connect.setEnabled(True)


@Slot(bool)
def connect_to_mm(self):
Expand All @@ -1276,9 +1287,9 @@ def connect_to_mm(self):
# Try to open Bridge. Requires micromanager to be open with server running.
# This does not fail gracefully, so I'm wrapping it in its own try-except block.
try:
bridge = Bridge(convert_camel_case=False)
self.mmc = bridge.get_core()
self.mm = bridge.get_studio()
self.bridge = Bridge(convert_camel_case=False)
self.mmc = self.bridge.get_core()
self.mm = self.bridge.get_studio()
except:
print(("Could not establish pycromanager bridge.\n"
"Is micromanager open?\n"
Expand All @@ -1287,8 +1298,8 @@ def connect_to_mm(self):
raise EnvironmentError

# Warn the use if there is a MicroManager/ZMQ version mismatch
bridge._main_socket.send({"command": "connect", "debug": False})
reply_json = bridge._main_socket.receive(timeout=500)
self.bridge._main_socket.send({"command": "connect", "debug": False})
reply_json = self.bridge._main_socket.receive(timeout=500)
zmq_mm_version = reply_json['version']
if zmq_mm_version != ZMQ_TARGET_VERSION:
upgrade_str = 'upgrade' if version.parse(zmq_mm_version) < version.parse(ZMQ_TARGET_VERSION) else 'downgrade'
Expand All @@ -1302,7 +1313,7 @@ def connect_to_mm(self):
# in this version of the code we correctly parse 'LF-State0', but these channels, for not cannot be used
# by the Calibration class.
# A valid config group contains all channels in calib_channels
self.ui.cb_config_group.clear()
# self.ui.cb_config_group.clear() # This triggers the enter config we will clear when switching off
groups = self.mmc.getAvailableConfigGroups()
config_group_found = False
for i in range(groups.size()):
Expand All @@ -1321,7 +1332,6 @@ def connect_to_mm(self):
for ch in config_list:
if ch not in self.calib_channels:
self.ui.cb_acq_channel.addItem(ch)

if not config_group_found:
msg = f'No config group contains channels {self.calib_channels}. ' \
'Please refer to the recOrder wiki on how to set up the config properly.'
Expand Down Expand Up @@ -1350,8 +1360,11 @@ def handle_mm_status_update(self, value):
if value:
self.ui.le_mm_status.setText('Success!')
self.ui.le_mm_status.setStyleSheet("background-color: green;")

#Disabling the button
self.ui.qbutton_mm_connect.setEnabled(False)
else:
#Make sure button is still visible if it fails
self.ui.qbutton_mm_connect.setEnabled(True)
self.ui.le_mm_status.setText('Failed.')
self.ui.le_mm_status.setStyleSheet("background-color: rgb(200,0,0);")

Expand Down Expand Up @@ -1705,32 +1718,36 @@ def enter_config_group(self):
-------
"""
#if/else takes care of the clearing of config
if self.ui.cb_config_group.count() != 0:
self.mmc = self.bridge.get_core()
self.mm = self.bridge.get_studio()

# Gather config groups and their children
self.config_group = self.ui.cb_config_group.currentText()
config = self.mmc.getAvailableConfigs(self.config_group)

channels = []
for i in range(config.size()):
channels.append(config.get(i))
# Gather config groups and their children
self.config_group = self.ui.cb_config_group.currentText()
config = self.mmc.getAvailableConfigs(self.config_group)

# Check to see if any states are missing
states = ['State0', 'State1', 'State2', 'State3', 'State4']
missing = []
for state in states:
if state not in channels:
missing.append(state)
channels = []
for i in range(config.size()):
channels.append(config.get(i))

# if states are missing, set the combo box red and alert the user
if len(missing) != 0:
msg = f'The chosen config group ({self.config_group}) is missing states: {missing}. '\
'Please refer to the recOrder wiki on how to set up the config properly.'
# Check to see if any states are missing
states = ['State0', 'State1', 'State2', 'State3', 'State4']
missing = []
for state in states:
if state not in channels:
missing.append(state)

self.ui.cb_config_group.setStyleSheet("border: 1px solid rgb(200,0,0);")
raise KeyError(msg)
else:
self.ui.cb_config_group.setStyleSheet("")
# if states are missing, set the combo box red and alert the user
if len(missing) != 0:
msg = f'The chosen config group ({self.config_group}) is missing states: {missing}. '\
'Please refer to the recOrder wiki on how to set up the config properly.'

self.ui.cb_config_group.setStyleSheet("border: 1px solid rgb(200,0,0);")
raise KeyError(msg)
else:
self.ui.cb_config_group.setStyleSheet("")

@Slot()
def enter_use_cropped_roi(self):
state = self.ui.chb_use_roi.checkState()
Expand Down

0 comments on commit 546b291

Please sign in to comment.