annotate image_math.py @ 2:48fa3ac55df2 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit c86a1b93cb7732f7331a981d13465653cc1a2790
author imgteam
date Wed, 24 Apr 2024 08:12:29 +0000
parents 33b2ca53a566
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
1 import argparse
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
2 import ast
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
3 import operator
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
4
2
48fa3ac55df2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 0
diff changeset
5 import giatools.io
0
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
6 import numpy as np
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
7 import skimage.io
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
8
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
9
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
10 supported_operators = {
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
11 ast.Add: operator.add,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
12 ast.Sub: operator.sub,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
13 ast.Mult: operator.mul,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
14 ast.Div: operator.truediv,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
15 ast.FloorDiv: operator.floordiv,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
16 ast.Pow: operator.pow,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
17 ast.USub: operator.neg,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
18 }
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
19
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
20
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
21 supported_functions = {
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
22 'sqrt': np.sqrt,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
23 'abs': abs,
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
24 }
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
25
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
26
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
27 def eval_ast_node(node, inputs):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
28 """
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
29 Evaluates a node of the syntax tree.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
30 """
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
31
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
32 # Numeric constants evaluate to numeric values.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
33 if isinstance(node, ast.Constant):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
34 assert type(node.value) in (int, float)
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
35 return node.value
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
36
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
37 # Variables are looked up from the inputs and resolved.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
38 if isinstance(node, ast.Name):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
39 assert node.id in inputs.keys()
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
40 return inputs[node.id]
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
41
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
42 # Binary operators are evaluated based on the `supported_operators` dictionary.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
43 if isinstance(node, ast.BinOp):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
44 assert type(node.op) in supported_operators.keys(), node.op
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
45 op = supported_operators[type(node.op)]
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
46 return op(eval_ast_node(node.left, inputs), eval_ast_node(node.right, inputs))
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
47
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
48 # Unary operators are evaluated based on the `supported_operators` dictionary.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
49 if isinstance(node, ast.UnaryOp):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
50 assert type(node.op) in supported_operators.keys(), node.op
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
51 op = supported_operators[type(node.op)]
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
52 return op(eval_ast_node(node.operand, inputs))
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
53
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
54 # Function calls are evaluated based on the `supported_functions` dictionary.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
55 if isinstance(node, ast.Call):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
56 assert len(node.args) == 1 and len(node.keywords) == 0
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
57 assert node.func.id in supported_functions.keys(), node.func.id
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
58 func = supported_functions[node.func.id]
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
59 return func(eval_ast_node(node.args[0], inputs))
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
60
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
61 # The node is unsupported and could not be evaluated.
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
62 raise TypeError(f'Unsupported node type: "{node}"')
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
63
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
64
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
65 def eval_expression(expr, inputs):
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
66 return eval_ast_node(ast.parse(expr, mode='eval').body, inputs)
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
67
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
68
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
69 if __name__ == '__main__':
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
70
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
71 parser = argparse.ArgumentParser()
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
72 parser.add_argument('--expression', type=str, required=True)
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
73 parser.add_argument('--output', type=str, required=True)
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
74 parser.add_argument('--input', default=list(), action='append', required=True)
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
75 args = parser.parse_args()
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
76
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
77 inputs = dict()
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
78 im_shape = None
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
79 for input in args.input:
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
80 name, filepath = input.split(':')
2
48fa3ac55df2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 0
diff changeset
81 im = giatools.io.imread(filepath)
0
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
82 assert name not in inputs, 'Input name "{name}" is ambiguous.'
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
83 inputs[name] = im
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
84 if im_shape is None:
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
85 im_shape = im.shape
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
86 else:
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
87 assert im.shape == im_shape, 'Input images differ in size and/or number of channels.'
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
88
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
89 result = eval_expression(args.expression, inputs)
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
90
33b2ca53a566 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff changeset
91 skimage.io.imsave(args.output, result)