Mercurial > repos > bgruening > cp_measure_image_quality
comparison image_math.py @ 6:d3890df07463 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
author | bgruening |
---|---|
date | Sun, 05 Nov 2023 09:29:39 +0000 |
parents | e972e07c7845 |
children |
comparison
equal
deleted
inserted
replaced
5:6f4715b8540a | 6:d3890df07463 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import argparse | 3 import argparse |
4 import json | 4 import json |
5 | 5 |
6 from cp_common_functions import build_header | 6 from cp_common_functions import (build_header, concat_conditional, |
7 from cp_common_functions import concat_conditional | 7 get_json_value, get_pipeline_lines, |
8 from cp_common_functions import get_json_value | 8 get_total_number_of_modules, |
9 from cp_common_functions import get_pipeline_lines | 9 INDENTATION, update_module_count, |
10 from cp_common_functions import get_total_number_of_modules | 10 write_pipeline) |
11 from cp_common_functions import INDENTATION | |
12 from cp_common_functions import update_module_count | |
13 from cp_common_functions import write_pipeline | |
14 | 11 |
15 MODULE_NAME = "ImageMath" | 12 MODULE_NAME = "ImageMath" |
16 OUTPUT_FILENAME = "output.cppipe" | 13 OUTPUT_FILENAME = "output.cppipe" |
17 | 14 |
18 operator_map = { | 15 operator_map = { |
27 "log_2": "Log transform (base 2)", | 24 "log_2": "Log transform (base 2)", |
28 "log_legacy": "Log transform (legacy)", | 25 "log_legacy": "Log transform (legacy)", |
29 "and": "And", | 26 "and": "And", |
30 "or": "Or", | 27 "or": "Or", |
31 "not": "Not", | 28 "not": "Not", |
32 "equals": "Equals" | 29 "equals": "Equals", |
33 } | 30 } |
34 | 31 |
35 | 32 |
36 def build_main_block(input_params): | 33 def build_main_block(input_params): |
37 """Creates the main block of the CP pipeline with the parameters that don't depend on conditional choices""" | 34 """Creates the main block of the CP pipeline with the parameters that don't depend on conditional choices""" |
38 operation = operator_map[get_json_value( | 35 operation = operator_map[get_json_value(input_params, "operation.operation")] |
39 input_params, 'operation.operation')] | 36 result = INDENTATION.join( |
40 result = INDENTATION.join([f"{INDENTATION}Operation:{operation}\n", | 37 [ |
41 f"Raise the power of the result by:{get_json_value(input_params,'operation.op_results.raise_the_power_of_the_result_by')}\n", | 38 f"{INDENTATION}Operation:{operation}\n", |
42 f"Multiply the result by:{get_json_value(input_params,'operation.op_results.multiply_the_result_by')}\n", | 39 f"Raise the power of the result by:{get_json_value(input_params,'operation.op_results.raise_the_power_of_the_result_by')}\n", |
43 f"Add to result:{get_json_value(input_params,'operation.op_results.add_to_result')}\n", | 40 f"Multiply the result by:{get_json_value(input_params,'operation.op_results.multiply_the_result_by')}\n", |
44 f"Set values less than 0 equal to 0?:{get_json_value(input_params,'operation.op_results.set_values_less_than_0_equal_to_0')}\n", | 41 f"Add to result:{get_json_value(input_params,'operation.op_results.add_to_result')}\n", |
45 f"Set values greater than 1 equal to 1?:{get_json_value(input_params,'operation.op_results.set_values_greater_than_1_equal_to_1')}\n", | 42 f"Set values less than 0 equal to 0?:{get_json_value(input_params,'operation.op_results.set_values_less_than_0_equal_to_0')}\n", |
46 f"Ignore the image masks?:{get_json_value(input_params,'ignore_the_image_masks')}\n", | 43 f"Set values greater than 1 equal to 1?:{get_json_value(input_params,'operation.op_results.set_values_greater_than_1_equal_to_1')}\n", |
47 f"Name the output image:{get_json_value(input_params,'name_output_image')}" | 44 f"Ignore the image masks?:{get_json_value(input_params,'ignore_the_image_masks')}\n", |
48 ]) | 45 f"Name the output image:{get_json_value(input_params,'name_output_image')}", |
46 ] | |
47 ) | |
49 return result | 48 return result |
50 | 49 |
51 | 50 |
52 def build_variable_block(inputs_galaxy): | 51 def build_variable_block(inputs_galaxy): |
53 result = "" | 52 result = "" |
54 first_image_block = build_first_image_block( | 53 first_image_block = build_first_image_block( |
55 get_json_value(inputs_galaxy, 'operation.first_image')) | 54 get_json_value(inputs_galaxy, "operation.first_image") |
55 ) | |
56 result += f"\n{first_image_block}" | 56 result += f"\n{first_image_block}" |
57 second_image_block = build_second_image_block( | 57 second_image_block = build_second_image_block( |
58 get_json_value(inputs_galaxy, 'operation.second_image')) | 58 get_json_value(inputs_galaxy, "operation.second_image") |
59 ) | |
59 result += f"\n{second_image_block}" | 60 result += f"\n{second_image_block}" |
60 return result | 61 return result |
61 | 62 |
62 | 63 |
63 def build_first_image_block(input_params): | 64 def build_first_image_block(input_params): |
64 """Creates the block of parameters for the first operator in operations""" | 65 """Creates the block of parameters for the first operator in operations""" |
65 | 66 |
66 value_select = get_json_value( | 67 value_select = get_json_value( |
67 input_params, 'image_or_measurement_first.image_or_measurement_first') | 68 input_params, "image_or_measurement_first.image_or_measurement_first" |
69 ) | |
68 image_name = get_json_value( | 70 image_name = get_json_value( |
69 input_params, 'image_or_measurement_first.select_the_first_image') | 71 input_params, "image_or_measurement_first.select_the_first_image" |
70 value_multiply = get_json_value( | 72 ) |
71 input_params, 'multiply_the_first_image_by') | 73 value_multiply = get_json_value(input_params, "multiply_the_first_image_by") |
72 category = get_json_value( | 74 category = get_json_value( |
73 input_params, 'image_or_measurement_first.category_first.category_first') | 75 input_params, "image_or_measurement_first.category_first.category_first" |
76 ) | |
74 measurement = get_json_value( | 77 measurement = get_json_value( |
75 input_params, 'image_or_measurement_first.category_first.measurement_first') | 78 input_params, "image_or_measurement_first.category_first.measurement_first" |
79 ) | |
76 | 80 |
77 result = INDENTATION.join( | 81 result = INDENTATION.join( |
78 [f"{INDENTATION}Image or measurement?:{value_select}\n", | 82 [ |
79 f"Select the first image:{image_name}\n", | 83 f"{INDENTATION}Image or measurement?:{value_select}\n", |
80 f"Multiply the first image by:{value_multiply}\n", | 84 f"Select the first image:{image_name}\n", |
81 f"Measurement:{concat_conditional(category, measurement)}" | 85 f"Multiply the first image by:{value_multiply}\n", |
82 ]) | 86 f"Measurement:{concat_conditional(category, measurement)}", |
87 ] | |
88 ) | |
83 return result | 89 return result |
84 | 90 |
85 | 91 |
86 def build_second_image_block(input_params): | 92 def build_second_image_block(input_params): |
87 """Creates the block of parameters for the second operator in binary operations""" | 93 """Creates the block of parameters for the second operator in binary operations""" |
88 | 94 |
89 value_select = get_json_value( | 95 value_select = get_json_value( |
90 input_params, 'image_or_measurement_second.image_or_measurement_second') | 96 input_params, "image_or_measurement_second.image_or_measurement_second" |
97 ) | |
91 image_name = get_json_value( | 98 image_name = get_json_value( |
92 input_params, 'image_or_measurement_second.select_the_second_image') | 99 input_params, "image_or_measurement_second.select_the_second_image" |
93 value_multiply = get_json_value( | 100 ) |
94 input_params, 'multiply_the_second_image_by') | 101 value_multiply = get_json_value(input_params, "multiply_the_second_image_by") |
95 category = get_json_value( | 102 category = get_json_value( |
96 input_params, 'image_or_measurement_second.category_second.category_second') | 103 input_params, "image_or_measurement_second.category_second.category_second" |
104 ) | |
97 measurement = get_json_value( | 105 measurement = get_json_value( |
98 input_params, 'image_or_measurement_second.category_second.measurement_second') | 106 input_params, "image_or_measurement_second.category_second.measurement_second" |
107 ) | |
99 | 108 |
100 result = INDENTATION.join( | 109 result = INDENTATION.join( |
101 [f"{INDENTATION}Image or measurement?:{value_select}\n", | 110 [ |
102 f"Select the second image:{image_name}\n", | 111 f"{INDENTATION}Image or measurement?:{value_select}\n", |
103 f"Multiply the second image by:{value_multiply}\n", | 112 f"Select the second image:{image_name}\n", |
104 f"Measurement:{concat_conditional(category, measurement)}" | 113 f"Multiply the second image by:{value_multiply}\n", |
105 ]) | 114 f"Measurement:{concat_conditional(category, measurement)}", |
115 ] | |
116 ) | |
106 return result | 117 return result |
107 | 118 |
108 | 119 |
109 if __name__ == "__main__": | 120 if __name__ == "__main__": |
110 parser = argparse.ArgumentParser() | 121 parser = argparse.ArgumentParser() |
111 parser.add_argument( | 122 parser.add_argument("-p", "--pipeline", help="CellProfiler pipeline") |
112 '-p', '--pipeline', | 123 parser.add_argument("-i", "--inputs", help="JSON inputs from Galaxy") |
113 help='CellProfiler pipeline' | |
114 ) | |
115 parser.add_argument( | |
116 '-i', '--inputs', | |
117 help='JSON inputs from Galaxy' | |
118 ) | |
119 args = parser.parse_args() | 124 args = parser.parse_args() |
120 | 125 |
121 pipeline_lines = get_pipeline_lines(args.pipeline) | 126 pipeline_lines = get_pipeline_lines(args.pipeline) |
122 inputs_galaxy = json.load(open(args.inputs, "r")) | 127 inputs_galaxy = json.load(open(args.inputs, "r")) |
123 | 128 |