Skip to content

Commit

Permalink
Create CP5 special directory
Browse files Browse the repository at this point in the history
- will eventually (on release) move current active_plugins to CP4 and
  move contents of CP5 to active_plugins, ditto for tests; but for now
  everything goes in CP5 directory since most people are on CP4
  • Loading branch information
gnodar01 committed Aug 8, 2024
1 parent b68e0d0 commit a48cc02
Show file tree
Hide file tree
Showing 29 changed files with 8,067 additions and 0 deletions.
442 changes: 442 additions & 0 deletions CP5/active_plugins/calculatemoments.py

Large diffs are not rendered by default.

660 changes: 660 additions & 0 deletions CP5/active_plugins/callbarcodes.py

Large diffs are not rendered by default.

567 changes: 567 additions & 0 deletions CP5/active_plugins/compensatecolors.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions CP5/active_plugins/cpij/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-

__author__ = "Mark Hiner, Alice Lucas, Beth Cimini"
__all__ = ["bridge", "server"]
138 changes: 138 additions & 0 deletions CP5/active_plugins/cpij/bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from multiprocessing.managers import SyncManager
import multiprocessing as mp
import atexit, cpij.server as ijserver
from queue import Queue
from threading import Lock


class QueueManager(SyncManager):
pass


QueueManager.register("input_queue")
QueueManager.register("output_queue")
QueueManager.register("get_lock")

_init_method = None


def init_method():
global _init_method
if not _init_method:
if ijserver.is_server_running():
l = lock()
l.acquire()
to_imagej().put(
{ijserver.PYIMAGEJ_KEY_COMMAND: ijserver.PYIMAGEJ_CMD_GET_INIT_METHOD}
)
_init_method = from_imagej().get()[ijserver.PYIMAGEJ_KEY_OUTPUT]
l.release()

return _init_method


def lock() -> Lock:
"""
Helper method to synchronzie requests with the ImageJ server.
A lock should be acquired before sending data to the server, and released after
receiving the result.
Returns
---------
A Lock connected to the ImageJ server.
"""
return _manager().get_lock()


def to_imagej() -> Queue:
"""
Helper method to send data to the ImageJ server
Returns
---------
A Queue connected to the ImageJ server. Only its put method should be called.
"""
return _manager().input_queue()


def from_imagej() -> Queue:
"""
Helper method to retrieve data from the ImageJ server
Returns
---------
A Queue connected to the ImageJ server. Only its get method should be called.
"""
return _manager().output_queue()


def init_pyimagej(init_string):
"""
Start the pyimagej daemon thread if it isn't already running.
Parameters
----------
init_string : str, optional
This can be a path to a local ImageJ installation, or an initialization string per imagej.init(),
e.g. sc.fiji:fiji:2.1.0
"""
to_imagej().put(
{
ijserver.PYIMAGEJ_KEY_COMMAND: ijserver.PYIMAGEJ_CMD_START,
ijserver.PYIMAGEJ_KEY_INPUT: init_string,
}
)
result = from_imagej().get()
if result == ijserver.PYIMAGEJ_STATUS_STARTUP_FAILED:
_shutdown_imagej()
# Wait for the server to shut down
while ijserver.is_server_running():
pass
return False

global _init_method
_init_method = init_string
return True


def _manager() -> QueueManager:
"""
Helper method to return a QueueManager connected to the ImageJ server
"""
if not ijserver.is_server_running():
raise RuntimeError("No ImageJ server instance available")

manager = QueueManager(
address=("127.0.0.1", ijserver.SERVER_PORT), authkey=ijserver._SERVER_KEY
)
manager.connect()
return manager


def _shutdown_imagej():
"""
Helper method to send the shutdown signal to ImageJ. Intended to be called
at process exit.
"""
if ijserver.is_server_running():
to_imagej().put({ijserver.PYIMAGEJ_KEY_COMMAND: ijserver.PYIMAGEJ_CMD_EXIT})


def start_imagej_server():
"""
If the ImageJ server is not already running, spawns the server in a new
Process. Blocks until the server is up and running.
"""
if ijserver.is_server_running():
return

ctx = mp.get_context("spawn")
p = ctx.Process(target=ijserver.main)
p.start()

# wait for the server to start up
ijserver.wait_for_server_startup()

# Ensure server shuts down when main app closes
atexit.register(_shutdown_imagej)
Loading

0 comments on commit a48cc02

Please sign in to comment.