Skip to content

Commit

Permalink
pybio -> bioimageio
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed May 18, 2021
1 parent a6de4cc commit 44288d4
Show file tree
Hide file tree
Showing 25 changed files with 134 additions and 137 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ jobs:
. /opt/conda/etc/profile.d/conda.sh
conda activate tiktorch-server-env
./scripts/conda_build.sh conda-recipe-client
./scripts/conda_build.sh pybio-core-recipe
./scripts/conda_build.sh pybio-torch-recipe
./scripts/conda_build.sh bioimageio-core-recipe
./scripts/conda_build.sh bioimageio-torch-recipe
./scripts/conda_build.sh conda-recipe
./scripts/conda_build.sh conda-recipe-cpu
Expand Down
4 changes: 2 additions & 2 deletions pybio-core-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ requirements:

test:
imports:
- pybio.spec
- bioimageio.spec

about:
home: https://github.com/bioimage-io/python-bioimage-io
license: MIT
license_family: MIT
license_file: LICENSE
summary: 'Tools for parsing pybio model specification'
summary: 'Tools for parsing bioimageio model specification'

doc_url: https://github.com/bioimage-io/python-bioimage-io
dev_url: https://github.com/bioimage-io/python-bioimage-io
Expand Down
6 changes: 3 additions & 3 deletions pybio-torch-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ requirements:

test:
imports:
- pybio.torch
- pybio.torch.training
- bioimageio.torch
- bioimageio.torch.training

about:
home: https://github.com/bioimage-io/pytorch-bioimage-io
license: MIT
license_family: MIT
license_file: LICENSE
summary: 'Pytorch related components of pybio'
summary: 'Pytorch related components of bioimageio'

doc_url: https://github.com/bioimage-io/pytorch-bioimage-io
dev_url: https://github.com/bioimage-io/pytorch-bioimage-io
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"numpy",
"pyyaml",
"torch",
"pybio.core @ git+ssh://[email protected]/bioimage-io/python-bioimage-io#egg=pybio.core",
"pybio.torch @ git+ssh://[email protected]/bioimage-io/pytorch-bioimage-io#egg=pybio.torch",
"bioimageio.core @ git+ssh://[email protected]/bioimage-io/python-bioimage-io#egg=bioimageio.core",
"bioimageio.torch @ git+ssh://[email protected]/bioimage-io/pytorch-bioimage-io#egg=bioimageio.torch",
],
entry_points={"console_scripts": ["tiktorch=tiktorch.server.base:main"]},
# extras_require={"test": ["pytest"]},
Expand Down
69 changes: 32 additions & 37 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import pytest

TEST_DATA = "data"
TEST_PYBIO_ZIPFOLDER = "unet2d"
TEST_PYBIO_ONNX = "unet2d_onnx"
TEST_PYBIO_DUMMY = "dummy"
TEST_PYBIO_TENSORFLOW_DUMMY = "dummy_tensorflow"
TEST_PYBIO_TORCHSCRIPT = "unet2d_torchscript"
TEST_BIOIMAGEIO_ZIPFOLDER = "unet2d"
TEST_BIOIMAGEIO_ONNX = "unet2d_onnx"
TEST_BIOIMAGEIO_DUMMY = "dummy"
TEST_BIOIMAGEIO_TENSORFLOW_DUMMY = "dummy_tensorflow"
TEST_BIOIMAGEIO_TORCHSCRIPT = "unet2d_torchscript"

NNModel = namedtuple("NNModel", ["model", "state"])

Expand Down Expand Up @@ -82,8 +82,8 @@ def assert_threads_cleanup():


@pytest.fixture
def pybio_model_bytes(data_path):
zip_folder = data_path / TEST_PYBIO_ZIPFOLDER
def bioimageio_model_bytes(data_path):
zip_folder = data_path / TEST_BIOIMAGEIO_ZIPFOLDER
data = io.BytesIO()
with ZipFile(data, mode="w") as zip_model:
for f_path in zip_folder.iterdir():
Expand All @@ -97,18 +97,18 @@ def pybio_model_bytes(data_path):


@pytest.fixture
def pybio_model_zipfile(pybio_model_bytes):
with ZipFile(pybio_model_bytes, mode="r") as zf:
def bioimageio_model_zipfile(bioimageio_model_bytes):
with ZipFile(bioimageio_model_bytes, mode="r") as zf:
yield zf


@pytest.fixture
def pybio_dummy_model_filepath(data_path, tmpdir):
pybio_net_dir = Path(data_path) / TEST_PYBIO_DUMMY
def bioimageio_dummy_model_filepath(data_path, tmpdir):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_DUMMY
path = tmpdir / "dummy_model.zip"

with ZipFile(path, mode="w") as zip_model:
for f_path in pybio_net_dir.iterdir():
for f_path in bioimageio_net_dir.iterdir():
if str(f_path.name).startswith("__"):
continue

Expand All @@ -119,11 +119,11 @@ def pybio_dummy_model_filepath(data_path, tmpdir):


@pytest.fixture
def pybio_dummy_model_bytes(data_path):
pybio_net_dir = Path(data_path) / TEST_PYBIO_DUMMY
def bioimageio_dummy_model_bytes(data_path):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_DUMMY
data = io.BytesIO()
with ZipFile(data, mode="w") as zip_model:
for f_path in pybio_net_dir.iterdir():
for f_path in bioimageio_net_dir.iterdir():
if str(f_path.name).startswith("__"):
continue

Expand Down Expand Up @@ -156,22 +156,22 @@ def _archive(path_to_archive):


@pytest.fixture
def pybio_dummy_tensorflow_model_bytes(data_path):
pybio_net_dir = Path(data_path) / TEST_PYBIO_TENSORFLOW_DUMMY
return archive(pybio_net_dir)
def bioimageio_dummy_tensorflow_model_bytes(data_path):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_TENSORFLOW_DUMMY
return archive(bioimageio_net_dir)


@pytest.fixture
def pybio_unet2d_onnx_bytes(data_path):
pybio_net_dir = Path(data_path) / TEST_PYBIO_ONNX
return archive(pybio_net_dir)
def bioimageio_unet2d_onnx_bytes(data_path):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_ONNX
return archive(bioimageio_net_dir)


@pytest.fixture
def pybio_unet2d_onnx_test_data(data_path):
pybio_net_dir = Path(data_path) / TEST_PYBIO_ONNX
test_input = pybio_net_dir / "test_input.npy"
test_output = pybio_net_dir / "test_output.npy"
def bioimageio_unet2d_onnx_test_data(data_path):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_ONNX
test_input = bioimageio_net_dir / "test_input.npy"
test_output = bioimageio_net_dir / "test_output.npy"
return {"test_input": test_input, "test_output": test_output}


Expand All @@ -184,19 +184,14 @@ def npy_zeros_file(tmpdir):


@pytest.fixture
def pybio_unet2d_torchscript_bytes(data_path):
pybio_net_dir = Path(data_path) / TEST_PYBIO_TORCHSCRIPT
return archive(pybio_net_dir)
def bioimageio_unet2d_torchscript_bytes(data_path):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_TORCHSCRIPT
return archive(bioimageio_net_dir)


@pytest.fixture
def pybio_unet2d_torchscript_test_data(data_path):
pybio_net_dir = Path(data_path) / TEST_PYBIO_TORCHSCRIPT
test_input = pybio_net_dir / "test_input.npy"
test_output = pybio_net_dir / "test_output.npy"
def bioimageio_unet2d_torchscript_test_data(data_path):
bioimageio_net_dir = Path(data_path) / TEST_BIOIMAGEIO_TORCHSCRIPT
test_input = bioimageio_net_dir / "test_input.npy"
test_output = bioimageio_net_dir / "test_output.npy"
return {"test_input": test_input, "test_output": test_output}


@pytest.fixture
def cache_path(tmp_path):
return Path(getenv("PYBIO_CACHE_PATH", tmp_path))
2 changes: 1 addition & 1 deletion tests/data/unet2d_onnx/UNet2DNucleiBroad.model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ outputs:

language: python
framework: pytorch
source: pybio.torch.models.unet2d.UNet2d
source: bioimageio.torch.models.unet2d.UNet2d
kwargs: {input_channels: 1, output_channels: 1}
dependencies: conda:../environment.yaml

Expand Down
2 changes: 1 addition & 1 deletion tests/data/unet2d_torchscript/UNet2DNucleiBroad.model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ outputs:

language: python
framework: pytorch
source: pybio.torch.models.unet2d.UNet2d
source: bioimageio.torch.models.unet2d.UNet2d
kwargs: {input_channels: 1, output_channels: 1}
dependencies: conda:../environment.yaml

Expand Down
Binary file modified tests/data/unet2d_torchscript/weights.pt
Binary file not shown.
30 changes: 15 additions & 15 deletions tests/test_server/test_grpc/test_inference_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def method_requiring_session(self, request, grpc_stub):
method_name, req = request.param
return getattr(grpc_stub, method_name), req

def test_model_session_creation(self, grpc_stub, pybio_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(pybio_model_bytes))
def test_model_session_creation(self, grpc_stub, bioimageio_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_model_bytes))
assert model.id
grpc_stub.CloseModelSession(model)

def test_model_session_creation_using_upload_id(self, grpc_stub, data_store, pybio_dummy_model_bytes):
id_ = data_store.put(pybio_dummy_model_bytes.getvalue())
def test_model_session_creation_using_upload_id(self, grpc_stub, data_store, bioimageio_dummy_model_bytes):
id_ = data_store.put(bioimageio_dummy_model_bytes.getvalue())

rq = inference_pb2.CreateModelSessionRequest(model_uri=f"upload://{id_}", deviceIds=["cpu"])
model = grpc_stub.CreateModelSession(rq)
Expand Down Expand Up @@ -103,28 +103,28 @@ def test_if_model_create_fails_devices_are_released(self, grpc_stub):
if model:
grpc_stub.CloseModelSession(model)

def test_use_device(self, grpc_stub, pybio_model_bytes):
def test_use_device(self, grpc_stub, bioimageio_model_bytes):
device_by_id = self._query_devices(grpc_stub)
assert "cpu" in device_by_id
assert inference_pb2.Device.Status.AVAILABLE == device_by_id["cpu"].status

model = grpc_stub.CreateModelSession(valid_model_request(pybio_model_bytes, device_ids=["cpu"]))
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_model_bytes, device_ids=["cpu"]))

device_by_id = self._query_devices(grpc_stub)
assert "cpu" in device_by_id
assert inference_pb2.Device.Status.IN_USE == device_by_id["cpu"].status

grpc_stub.CloseModelSession(model)

def test_using_same_device_fails(self, grpc_stub, pybio_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(pybio_model_bytes, device_ids=["cpu"]))
def test_using_same_device_fails(self, grpc_stub, bioimageio_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_model_bytes, device_ids=["cpu"]))
with pytest.raises(grpc.RpcError):
model = grpc_stub.CreateModelSession(valid_model_request(pybio_model_bytes, device_ids=["cpu"]))
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_model_bytes, device_ids=["cpu"]))

grpc_stub.CloseModelSession(model)

def test_closing_session_releases_devices(self, grpc_stub, pybio_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(pybio_model_bytes, device_ids=["cpu"]))
def test_closing_session_releases_devices(self, grpc_stub, bioimageio_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_model_bytes, device_ids=["cpu"]))

device_by_id = self._query_devices(grpc_stub)
assert "cpu" in device_by_id
Expand All @@ -138,8 +138,8 @@ def test_closing_session_releases_devices(self, grpc_stub, pybio_model_bytes):


class TestGetLogs:
def test_returns_ack_message(self, pybio_model_bytes, grpc_stub):
model = grpc_stub.CreateModelSession(valid_model_request(pybio_model_bytes))
def test_returns_ack_message(self, bioimageio_model_bytes, grpc_stub):
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_model_bytes))
resp = grpc_stub.GetLogs(inference_pb2.Empty())
record = next(resp)
assert inference_pb2.LogEntry.Level.INFO == record.level
Expand All @@ -154,8 +154,8 @@ def test_call_fails_with_unknown_model_session_id(self, grpc_stub):
assert grpc.StatusCode.FAILED_PRECONDITION == e.value.code()
assert "model-session with id myid1 doesn't exist" in e.value.details()

def test_call_predict(self, grpc_stub, pybio_dummy_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(pybio_dummy_model_bytes))
def test_call_predict(self, grpc_stub, bioimageio_dummy_model_bytes):
model = grpc_stub.CreateModelSession(valid_model_request(bioimageio_dummy_model_bytes))

arr = xr.DataArray(np.arange(32 * 32).reshape(1, 32, 32), dims=("c", "x", "y"))
expected = arr + 1
Expand Down
16 changes: 8 additions & 8 deletions tests/test_server/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def npy_zeros_file(tmpdir):
return path


def test_running_predict_with_valid_arguments(testdir, pybio_dummy_model_filepath, npy_zeros_file, output_path):
def test_running_predict_with_valid_arguments(testdir, bioimageio_dummy_model_filepath, npy_zeros_file, output_path):
result = testdir.run(
"python",
"-m",
"tiktorch.server.predict",
"--model",
pybio_dummy_model_filepath,
bioimageio_dummy_model_filepath,
npy_zeros_file,
"--output",
output_path,
Expand All @@ -41,20 +41,20 @@ def test_running_predict_fails_when_model_unspecified(testdir, npy_zeros_file, o
assert result.ret != 0


def test_running_predict_fails_when_no_images_specified(testdir, pybio_dummy_model_filepath, output_path):
def test_running_predict_fails_when_no_images_specified(testdir, bioimageio_dummy_model_filepath, output_path):
result = testdir.run(
"python", "-m", "tiktorch.server.predict", "--model", pybio_dummy_model_filepath, "--output", output_path
"python", "-m", "tiktorch.server.predict", "--model", bioimageio_dummy_model_filepath, "--output", output_path
)
assert result.ret != 0


def test_running_predict_fails_when_invalid_image_specified(testdir, pybio_dummy_model_filepath, output_path):
def test_running_predict_fails_when_invalid_image_specified(testdir, bioimageio_dummy_model_filepath, output_path):
result = testdir.run(
"python",
"-m",
"tiktorch.server.predict",
"--model",
pybio_dummy_model_filepath,
bioimageio_dummy_model_filepath,
"nonexisting",
"--output",
output_path,
Expand All @@ -77,9 +77,9 @@ def test_running_predict_fails_without_when_model_file_does_not_exist(testdir, t


def test_running_predict_failes_when_output_is_unspecified(
testdir, pybio_dummy_model_filepath, npy_zeros_file, output_path
testdir, bioimageio_dummy_model_filepath, npy_zeros_file, output_path
):
result = testdir.run(
"python", "-m", "tiktorch.server.predict", "--model", pybio_dummy_model_filepath, npy_zeros_file
"python", "-m", "tiktorch.server.predict", "--model", bioimageio_dummy_model_filepath, npy_zeros_file
)
assert result.ret != 0
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@
from tiktorch.server.reader import eval_model_zip


def test_eval_onnx_model_zip_predict(pybio_unet2d_onnx_bytes, pybio_unet2d_onnx_test_data, cache_path):
with ZipFile(pybio_unet2d_onnx_bytes) as zf:
def test_eval_onnx_model_zip_predict(bioimageio_unet2d_onnx_bytes, bioimageio_unet2d_onnx_test_data):
with ZipFile(bioimageio_unet2d_onnx_bytes) as zf:
adapter = eval_model_zip(zf, devices=["cpu"], preserve_batch_dim=True)
test_input = xarray.DataArray(np.load(pybio_unet2d_onnx_test_data["test_input"]), dims=("b", "c", "x", "y"))
test_input = xarray.DataArray(
np.load(bioimageio_unet2d_onnx_test_data["test_input"]), dims=("b", "c", "x", "y")
)
# TODO: Figure out why test output doesn't match result
adapter.forward(test_input)


def test_eval_torchscript_model_zip_predict(
pybio_unet2d_torchscript_bytes, pybio_unet2d_torchscript_test_data, cache_path
bioimageio_unet2d_torchscript_bytes, bioimageio_unet2d_torchscript_test_data
):
with ZipFile(pybio_unet2d_torchscript_bytes) as zf:
with ZipFile(bioimageio_unet2d_torchscript_bytes) as zf:
pipeline = eval_model_zip(zf, devices=["cpu"], preserve_batch_dim=True)
test_input = xarray.DataArray(
np.load(pybio_unet2d_torchscript_test_data["test_input"]).astype(np.float32), dims=("b", "c", "x", "y")
np.load(bioimageio_unet2d_torchscript_test_data["test_input"]).astype(np.float32), dims=("b", "c", "x", "y")
)
test_output = np.load(pybio_unet2d_torchscript_test_data["test_output"])
test_output = np.load(bioimageio_unet2d_torchscript_test_data["test_output"])
result = pipeline.forward(test_input)
assert_array_almost_equal(result.data, test_output, decimal=4)


def test_eval_model_zip_metadata_no_batch_dim(
pybio_unet2d_torchscript_bytes, pybio_unet2d_torchscript_test_data, cache_path
bioimageio_unet2d_torchscript_bytes, bioimageio_unet2d_torchscript_test_data
):
with ZipFile(pybio_unet2d_torchscript_bytes) as zf:
with ZipFile(bioimageio_unet2d_torchscript_bytes) as zf:
pipeline = eval_model_zip(zf, devices=["cpu"], preserve_batch_dim=False)
assert pipeline.name == "UNet 2D Nuclei Broad"
assert pipeline.input_axes == "cyx"
Expand All @@ -40,8 +42,8 @@ def test_eval_model_zip_metadata_no_batch_dim(
assert pipeline.halo == [("c", 0), ("y", 32), ("x", 32)]


def test_eval_model_zip(pybio_model_bytes, cache_path):
with ZipFile(pybio_model_bytes) as zf:
def test_eval_model_zip(bioimageio_model_bytes):
with ZipFile(bioimageio_model_bytes) as zf:
pipeline = eval_model_zip(zf, devices=["cpu"], preserve_batch_dim=True)

assert pipeline.input_axes == "bcyx"
Expand All @@ -51,9 +53,9 @@ def test_eval_model_zip(pybio_model_bytes, cache_path):


def test_eval_model_zip_metadata_with_batch_dim(
pybio_unet2d_torchscript_bytes, pybio_unet2d_torchscript_test_data, cache_path
bioimageio_unet2d_torchscript_bytes, bioimageio_unet2d_torchscript_test_data
):
with ZipFile(pybio_unet2d_torchscript_bytes) as zf:
with ZipFile(bioimageio_unet2d_torchscript_bytes) as zf:
pipeline = eval_model_zip(zf, devices=["cpu"], preserve_batch_dim=True)
assert pipeline.input_axes == "bcyx"
assert pipeline.output_axes == "bcyx"
Expand Down
Loading

0 comments on commit 44288d4

Please sign in to comment.