annotate filter.py @ 4:5ab62693dca5 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
author imgteam
date Sat, 20 Dec 2025 18:28:21 +0000
parents b2d9c92bc431
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
1 import argparse
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
2 import json
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
3 from typing import (
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
4 Any,
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
5 Callable,
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
6 )
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
7
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
8 import giatools
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
9 import numpy as np
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
10 import scipy.ndimage as ndi
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
11
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
12
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
13 def image_astype(image: giatools.Image, dtype: np.dtype) -> giatools.Image:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
14 if np.issubdtype(image.data.dtype, dtype):
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
15 return image # no conversion needed
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
16 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
17 return giatools.Image(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
18 data=image.data.astype(dtype),
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
19 axes=image.axes,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
20 original_axes=image.original_axes,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
21 metadata=image.metadata,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
22 )
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
23
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
24
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
25 def get_anisotropy(image: giatools.Image, axes: str) -> tuple[float, ...] | None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
26 """
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
27 Get the anisotropy of the image pixels/voxels along the given `axes`.
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
28
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
29 TODO: Migrate to `giatools.Image.get_anisotropy` with `giatools >=0.7`
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
30 """
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
31
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
32 # Determine the pixel/voxel size
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
33 voxel_size = list()
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
34 for axis in axes:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
35 match axis:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
36 case 'X':
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
37 if image.metadata.pixel_size is None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
38 return None # unknown size
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
39 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
40 voxel_size.append(image.metadata.pixel_size[0])
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
41 case 'Y':
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
42 if image.metadata.pixel_size is None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
43 return None # unknown size
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
44 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
45 voxel_size.append(image.metadata.pixel_size[1])
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
46 case 'Z':
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
47 if image.metadata.z_spacing is None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
48 return None # unknown size
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
49 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
50 voxel_size.append(image.metadata.z_spacing)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
51
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
52 # Check for unknown size and compute anisotropy
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
53 if any(abs(s) < 1e-8 for s in voxel_size):
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
54 print('Unknown pixel/voxel size')
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
55 return None
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
56 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
57 denom = pow(np.prod(voxel_size), 1 / len(voxel_size)) # geometric mean
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
58 anisotropy = tuple(np.divide(voxel_size, denom).tolist())
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
59 print(f'Anisotropy of {axes} pixels/voxels:', anisotropy)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
60 return anisotropy
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
61
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
62
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
63 def get_anisotropic_size(image: giatools.Image, axes: str, size: int) -> tuple[int, ...] | int:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
64 if (anisotropy := get_anisotropy(image, axes)) is not None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
65 return tuple(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
66 np.divide(size, anisotropy).round().clip(1, np.inf).astype(int).tolist(),
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
67 )
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
68 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
69 return size
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
70
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
71
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
72 class Filters:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
73
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
74 @staticmethod
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
75 def gaussian(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
76 image: giatools.Image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
77 sigma: float,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
78 anisotropic: bool,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
79 axes: str,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
80 order: int = 0,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
81 direction: int | None = None,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
82 **kwargs: Any,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
83 ) -> giatools.Image:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
84 if direction is None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
85 _order = 0
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
86 elif order >= 1:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
87 _order = [0] * len(axes)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
88 _order[direction] = order
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
89 _order = tuple(_order)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
90 if anisotropic and (anisotropy := get_anisotropy(image, axes)) is not None:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
91 _sigma = tuple(np.divide(sigma, anisotropy).tolist())
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
92 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
93 _sigma = sigma
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
94 return apply_nd_filter(
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
95 ndi.gaussian_filter,
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
96 image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
97 sigma=_sigma,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
98 order=_order,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
99 axes=axes,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
100 **kwargs,
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
101 )
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
102
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
103 @staticmethod
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
104 def uniform(image: giatools.Image, size: int, anisotropic: bool, axes: str, **kwargs: Any) -> giatools.Image:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
105 _size = get_anisotropic_size(image, axes, size) if anisotropic else size
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
106 return apply_nd_filter(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
107 ndi.uniform_filter,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
108 image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
109 size=_size,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
110 axes=axes,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
111 **kwargs,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
112 )
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
113
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
114 @staticmethod
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
115 def median(image: giatools.Image, size: int, anisotropic: bool, axes: str, **kwargs: Any) -> giatools.Image:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
116 _size = get_anisotropic_size(image, axes, size) if anisotropic else size
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
117 return apply_nd_filter(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
118 ndi.median_filter,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
119 image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
120 size=_size,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
121 axes=axes,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
122 **kwargs,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
123 )
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
124
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
125 @staticmethod
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
126 def prewitt(image: giatools.Image, direction: int, **kwargs: Any) -> giatools.Image:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
127 return apply_nd_filter(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
128 ndi.prewitt,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
129 image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
130 axis=direction,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
131 **kwargs,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
132 )
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
133
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
134 @staticmethod
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
135 def sobel(image: giatools.Image, direction: int, **kwargs: Any) -> giatools.Image:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
136 return apply_nd_filter(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
137 ndi.sobel,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
138 image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
139 axis=direction,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
140 **kwargs,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
141 )
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
142
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
143
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
144 def apply_nd_filter(
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
145 filter_impl: Callable[[np.ndarray, Any, ...], np.ndarray],
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
146 image: giatools.Image,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
147 axes: str,
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
148 **kwargs: Any,
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
149 ) -> giatools.Image:
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
150 """
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
151 Apply the filter to the 2-D/3-D, potentially multi-frame and multi-channel image.
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
152 """
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
153 print(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
154 'Applying filter:',
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
155 filter_impl.__name__,
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
156 'with',
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
157 ', '.join(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
158 f'{key}={repr(value)}' for key, value in (kwargs | dict(axes=axes)).items()
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
159 if not isinstance(value, np.ndarray)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
160 ),
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
161 )
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
162 result_data = None
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
163 for section_sel, section_arr in image.iterate_jointly(axes):
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
164 assert len(axes) == section_arr.ndim and section_arr.ndim in (2, 3) # sanity check, always True
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
165
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
166 # Define the section using the requested axes layout (compatible with `kwargs`)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
167 joint_axes_original_order = ''.join(filter(lambda axis: axis in axes, image.axes))
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
168 section = giatools.Image(section_arr, joint_axes_original_order).reorder_axes_like(axes)
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
169
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
170 # Perform 2-D or 3-D filtering
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
171 section_result = giatools.Image(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
172 filter_impl(section.data, **kwargs),
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
173 axes, # axes layout compatible with `kwargs`
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
174 ).reorder_axes_like(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
175 joint_axes_original_order, # axes order compatible to the input `image`
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
176 )
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
177
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
178 # Update the result image for the current section
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
179 if result_data is None:
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
180 result_data = np.empty(image.data.shape, section_result.data.dtype)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
181 result_data[section_sel] = section_result.data
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
182
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
183 # Return results
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
184 return giatools.Image(result_data, image.axes, image.metadata)
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
185
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
186
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
187 if __name__ == "__main__":
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
188 parser = argparse.ArgumentParser()
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
189 parser.add_argument('input', type=str, help='Input image filepath')
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
190 parser.add_argument('output', type=str, help='Output image filepath (TIFF)')
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
191 parser.add_argument('params', type=str)
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
192 args = parser.parse_args()
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
193
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
194 # Read the config file
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
195 with open(args.params) as cfgf:
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
196 cfg = json.load(cfgf)
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
197 cfg.setdefault('axes', 'YX')
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
198
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
199 # Read the input image
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
200 image = giatools.Image.read(args.input)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
201 print('Input image shape:', image.data.shape)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
202 print('Input image axes:', image.axes)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
203 print('Input image dtype:', image.data.dtype)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
204
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
205 # Convert the image to the explicitly requested `dtype`, or the same as the input image
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
206 convert_to = getattr(np, cfg.pop('dtype', str(image.data.dtype)))
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
207 if np.issubdtype(image.data.dtype, convert_to):
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
208 convert_to = image.data.dtype # use the input image `dtype` if `convert_to` is a superset
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
209 elif convert_to == np.floating:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
210 convert_to = np.float64 # use `float64` if conversion to *any* float is *required*
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
211
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
212 # If the input image is `float16` or conversion to `float16` is requested, ...
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
213 if convert_to == np.float16:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
214 image = image_astype(image, np.float32) # ...convert to `float32` as an intermediate
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
215 else:
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
216 image = image_astype(image, convert_to) # ...otherwise, convert now
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
217
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
218 # Perform filtering
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
219 filter_type = cfg.pop('filter_type')
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
220 filter_impl = getattr(Filters, filter_type)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
221 result = filter_impl(image, **cfg)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
222
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
223 # Apply `dtype` conversion
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
224 result = image_astype(result, convert_to)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
225
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
226 # Write the result
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
227 result = result.normalize_axes_like(
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
228 image.original_axes,
2
b2d9c92bc431 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit a6fd77be465068f709a71d377900da99becf94d8
imgteam
parents:
diff changeset
229 )
4
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
230 print('Output image shape:', result.data.shape)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
231 print('Output image axes:', result.axes)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
232 print('Output image dtype:', result.data.dtype)
5ab62693dca5 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_simple_filter/ commit c9cc62c508da3804872d105800993746c36cca48
imgteam
parents: 2
diff changeset
233 result.write(args.output, backend='tifffile')