|
2 | 2 | import hashlib |
3 | 3 | import os |
4 | 4 | from pathlib import Path |
5 | | -from typing import Any, Dict, List, Optional, Tuple, Union |
| 5 | +from typing import Any, Dict, List, Optional, Union |
6 | 6 | from warnings import warn |
7 | 7 |
|
8 | 8 | import imageio |
9 | 9 | import numpy as np |
10 | 10 | import requests |
| 11 | +import tifffile |
11 | 12 |
|
12 | 13 | import bioimageio.spec as spec |
13 | 14 | import bioimageio.spec.model as model_spec |
|
21 | 22 | except ImportError: |
22 | 23 | from typing_extensions import get_args # type: ignore |
23 | 24 |
|
24 | | -# need tifffile for writing the deepimagej config |
25 | | -# we probably always have this, but wrap into an ImportGuard just in case |
26 | | -try: |
27 | | - import tifffile |
28 | | -except ImportError: |
29 | | - tifffile = None |
30 | 25 |
|
31 | 26 | # |
32 | 27 | # utility functions to build the spec from python |
@@ -419,35 +414,40 @@ def get_size(fname, axes): |
419 | 414 |
|
420 | 415 | def _write_sample_data(input_paths, output_paths, input_axes, output_axes, pixel_sizes, export_folder: Path): |
421 | 416 | def write_im(path, im, axes, pixel_size=None): |
422 | | - assert tifffile is not None, "need tifffile for writing deepimagej config" |
423 | 417 | assert len(axes) == im.ndim, f"{len(axes), {im.ndim}}" |
424 | 418 | assert im.ndim in (4, 5), f"{im.ndim}" |
425 | 419 |
|
426 | 420 | # convert the image to expects (Z)CYX axis order |
427 | 421 | if im.ndim == 4: |
428 | 422 | assert set(axes) == {"b", "x", "y", "c"}, f"{axes}" |
429 | | - axes_ij = "cyxb" |
| 423 | + resolution_axes_ij = "cyxb" |
430 | 424 | else: |
431 | 425 | assert set(axes) == {"b", "x", "y", "z", "c"}, f"{axes}" |
432 | | - axes_ij = "zcyxb" |
| 426 | + resolution_axes_ij = "bzcyx" |
| 427 | + |
| 428 | + def addMissingAxes(im_axes): |
| 429 | + needed_axes = ["b", "c", "x", "y", "z", "s"] |
| 430 | + for ax in needed_axes: |
| 431 | + if ax not in im_axes: |
| 432 | + im_axes += ax |
| 433 | + return im_axes |
| 434 | + |
| 435 | + axes_ij = "bzcyxs" |
| 436 | + # Expand the image to ImageJ dimensions |
| 437 | + im = np.expand_dims(im, axis=tuple(range(len(axes), len(axes_ij)))) |
433 | 438 |
|
434 | | - axis_permutation = tuple(axes.index(ax) for ax in axes_ij) |
| 439 | + axis_permutation = tuple(addMissingAxes(axes).index(ax) for ax in axes_ij) |
435 | 440 | im = im.transpose(axis_permutation) |
436 | | - # expand to TZCYXS |
437 | | - if len(axes_ij) == 4: # add singleton t and z axis |
438 | | - im = im[None, None] |
439 | | - else: # add singeton z axis |
440 | | - im = im[None] |
441 | 441 |
|
442 | 442 | if pixel_size is None: |
443 | 443 | resolution = None |
444 | 444 | else: |
445 | | - spatial_axes = list(set(axes_ij) - set("bc")) |
446 | | - resolution = tuple(1.0 / pixel_size[ax] for ax in axes_ij if ax in spatial_axes) |
| 445 | + spatial_axes = list(set(resolution_axes_ij) - set("bc")) |
| 446 | + resolution = tuple(1.0 / pixel_size[ax] for ax in resolution_axes_ij if ax in spatial_axes) |
447 | 447 | # does not work for double |
448 | 448 | if np.dtype(im.dtype) == np.dtype("float64"): |
449 | 449 | im = im.astype("float32") |
450 | | - tifffile.imsave(path, im, imagej=True, resolution=resolution) |
| 450 | + tifffile.imwrite(path, im, imagej=True, resolution=resolution) |
451 | 451 |
|
452 | 452 | sample_in_paths = [] |
453 | 453 | for i, (in_path, axes) in enumerate(zip(input_paths, input_axes)): |
|
0 commit comments