Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix imagej #518

Merged
merged 4 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ jobs:
run: python -c "import sys; print(sys.version)"
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -qq libtiff5-dev
python -m pip install --upgrade pip
pip install flake8 pytest coverage
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install scikit-image libtiff
pip install scikit-image
- name: Test with pytest
run: |
coverage run --source=concert -m pytest -m "not skip_ci"
Expand Down
40 changes: 20 additions & 20 deletions concert/ext/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import asyncio
import collections
import multiprocessing as mp
import os
import tempfile
import time
import logging
from queue import Empty
from subprocess import Popen
from typing import Callable
import numpy as np
from concert.base import Parameterizable, Parameter
Expand All @@ -19,31 +16,34 @@
_MP_CTX = mp.get_context('spawn')


def _start_command(program, image):
"""
Create a tmp file for dumping the *image* and use *program*
to open that image. Use *writer* for writing the iamge to the disk.
"""
import os
import tempfile
from subprocess import Popen
from concert.storage import write_tiff

tmp_file = tempfile.mkstemp()[1]
try:
full_path = write_tiff(tmp_file, image)
with Popen([program, full_path]) as process:
process.wait()
finally:
os.remove(full_path)


def imagej(image, path="imagej"):
"""
imagej(image, path="imagej")

Open *image* in ImageJ found by *path*.
"""
def start_command(program, image):
"""
Create a tmp file for dumping the *image* and use *program*
to open that image. Use *writer* for writing the iamge to the disk.
"""
from concert.storage import write_tiff

tmp_file = tempfile.mkstemp()[1]
try:
full_path = write_tiff(tmp_file, image)
with Popen([program, full_path]) as process:
process.wait()
finally:
os.remove(full_path)
LOG.debug('Temporary file removed')

# Do not make a daemon process to make sure the interpreter waits for it to exit, i.e. the
# *finally* will be called and temp file deleted.
proc = _MP_CTX.Process(target=start_command, args=(path, image), daemon=False)
proc = _MP_CTX.Process(target=_start_command, args=(path, image), daemon=False)
proc.start()


Expand Down
15 changes: 15 additions & 0 deletions concert/tests/integration/test_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import multiprocessing as mp
import numpy as np
from concert.ext.viewers import _start_command
from concert.tests import suppressed_logging


_MP_CTX = mp.get_context('spawn')


@suppressed_logging
def test_start_command():
image = np.arange(25).reshape(5, 5).astype(np.float32)
proc = _MP_CTX.Process(target=_start_command, args=("echo", image), daemon=False)
proc.start()
proc.join()