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