upstream/tools/cellprofiler/image_math.py
1#!/usr/bin/env python
2
3import argparse
4import json
5
6from cp_common_functions import (build_header, concat_conditional,
7 get_json_value, get_pipeline_lines,
8 get_total_number_of_modules,
9 INDENTATION, update_module_count,
10 write_pipeline)
11
12MODULE_NAME = "ImageMath"
13OUTPUT_FILENAME = "output.cppipe"
14
15operator_map = {
16 "add": "Add",
17 "subtract": "Subtract",
18 "multiply": "Multiply",
19 "divide": "Divide",
20 "average": "Average",
21 "minimum": "Minimum",
22 "maximum": "Maximum",
23 "invert": "Invert",
24 "log_2": "Log transform (base 2)",
25 "log_legacy": "Log transform (legacy)",
26 "and": "And",
27 "or": "Or",
28 "not": "Not",
29 "equals": "Equals",
30}
31
32
33def build_main_block(input_params):
34 """Creates the main block of the CP pipeline with the parameters that don't depend on conditional choices"""
35 operation = operator_map[get_json_value(input_params, "operation.operation")]
36 result = INDENTATION.join(
37 [
38 f"{INDENTATION}Operation:{operation}\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",
40 f"Multiply the result by:{get_json_value(input_params,'operation.op_results.multiply_the_result_by')}\n",
41 f"Add to result:{get_json_value(input_params,'operation.op_results.add_to_result')}\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",
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",
44 f"Ignore the image masks?:{get_json_value(input_params,'ignore_the_image_masks')}\n",
45 f"Name the output image:{get_json_value(input_params,'name_output_image')}",
46 ]
47 )
48 return result
49
50
51def build_variable_block(inputs_galaxy):
52 result = ""
53 first_image_block = build_first_image_block(
54 get_json_value(inputs_galaxy, "operation.first_image")
55 )
56 result += f"\n{first_image_block}"
57 second_image_block = build_second_image_block(
58 get_json_value(inputs_galaxy, "operation.second_image")
59 )
60 result += f"\n{second_image_block}"
61 return result
62
63
64def build_first_image_block(input_params):
65 """Creates the block of parameters for the first operator in operations"""
66
67 value_select = get_json_value(
68 input_params, "image_or_measurement_first.image_or_measurement_first"
69 )
70 image_name = get_json_value(
71 input_params, "image_or_measurement_first.select_the_first_image"
72 )
73 value_multiply = get_json_value(input_params, "multiply_the_first_image_by")
74 category = get_json_value(
75 input_params, "image_or_measurement_first.category_first.category_first"
76 )
77 measurement = get_json_value(
78 input_params, "image_or_measurement_first.category_first.measurement_first"
79 )
80
81 result = INDENTATION.join(
82 [
83 f"{INDENTATION}Image or measurement?:{value_select}\n",
84 f"Select the first image:{image_name}\n",
85 f"Multiply the first image by:{value_multiply}\n",
86 f"Measurement:{concat_conditional(category, measurement)}",
87 ]
88 )
89 return result
90
91
92def build_second_image_block(input_params):
93 """Creates the block of parameters for the second operator in binary operations"""
94
95 value_select = get_json_value(
96 input_params, "image_or_measurement_second.image_or_measurement_second"
97 )
98 image_name = get_json_value(
99 input_params, "image_or_measurement_second.select_the_second_image"
100 )
101 value_multiply = get_json_value(input_params, "multiply_the_second_image_by")
102 category = get_json_value(
103 input_params, "image_or_measurement_second.category_second.category_second"
104 )
105 measurement = get_json_value(
106 input_params, "image_or_measurement_second.category_second.measurement_second"
107 )
108
109 result = INDENTATION.join(
110 [
111 f"{INDENTATION}Image or measurement?:{value_select}\n",
112 f"Select the second image:{image_name}\n",
113 f"Multiply the second image by:{value_multiply}\n",
114 f"Measurement:{concat_conditional(category, measurement)}",
115 ]
116 )
117 return result
118
119
120if __name__ == "__main__":
121 parser = argparse.ArgumentParser()
122 parser.add_argument("-p", "--pipeline", help="CellProfiler pipeline")
123 parser.add_argument("-i", "--inputs", help="JSON inputs from Galaxy")
124 args = parser.parse_args()
125
126 pipeline_lines = get_pipeline_lines(args.pipeline)
127 inputs_galaxy = json.load(open(args.inputs, "r"))
128
129 current_module_num = get_total_number_of_modules(pipeline_lines)
130 current_module_num += 1
131 pipeline_lines = update_module_count(pipeline_lines, current_module_num)
132
133 header_block = build_header(MODULE_NAME, current_module_num)
134 main_block = build_main_block(inputs_galaxy)
135 variable_block = build_variable_block(inputs_galaxy)
136
137 module_pipeline = f"\n{header_block}{main_block}{variable_block}\n"
138 pipeline_lines.append(module_pipeline)
139
140 write_pipeline(OUTPUT_FILENAME, pipeline_lines)