Mercurial > repos > imgteam > image_math
annotate image_math.py @ 0:33b2ca53a566 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
author | imgteam |
---|---|
date | Sat, 09 Mar 2024 22:04:19 +0000 |
parents | |
children | 48fa3ac55df2 |
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 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
5 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
|
6 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
|
7 |
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 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
|
10 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
|
11 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
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 } |
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 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
|
21 '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
|
22 '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
|
23 } |
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 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
|
27 """ |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
28 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
|
29 """ |
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 # 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
|
32 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
|
33 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
|
34 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
|
35 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
36 # 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
|
37 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
|
38 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
|
39 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
|
40 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
41 # 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
47 # 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
53 # 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
60 # 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
|
61 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
|
62 |
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 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
|
65 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
|
66 |
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 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
|
69 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 name, filepath = input.split(':') |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
80 im = skimage.io.imread(filepath) |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 else: |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
86 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
|
87 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
88 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
|
89 |
33b2ca53a566
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/image_math commit b356d76025941b691c156f8ff931cd759d35b107
imgteam
parents:
diff
changeset
|
90 skimage.io.imsave(args.output, result) |