Mercurial > repos > iuc > biapy
comparison create_yaml.py @ 0:e434d9b9cd13 draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/biapy/ commit 66b393a7118c81d86d0fd80780d2bd551c18f3f0
| author | iuc |
|---|---|
| date | Thu, 09 Oct 2025 07:42:36 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e434d9b9cd13 |
|---|---|
| 1 import argparse | |
| 2 | |
| 3 import requests | |
| 4 import yaml | |
| 5 | |
| 6 | |
| 7 def download_yaml_template(workflow, dims, biapy_version=""): | |
| 8 """ | |
| 9 Download a YAML template for a specific workflow and dimensions. | |
| 10 | |
| 11 Parameters: | |
| 12 workflow (str): The workflow type. | |
| 13 dims (str): The dimensions (e.g., 2d, 3d). | |
| 14 biapy_version (str): The BiaPy version to use. | |
| 15 | |
| 16 Returns: | |
| 17 dict: The YAML template as a dictionary. | |
| 18 """ | |
| 19 template_dir_map = { | |
| 20 "SEMANTIC_SEG": "semantic_segmentation", | |
| 21 "INSTANCE_SEG": "instance_segmentation", | |
| 22 "DETECTION": "detection", | |
| 23 "DENOISING": "denoising", | |
| 24 "SUPER_RESOLUTION": "super-resolution", | |
| 25 "CLASSIFICATION": "classification", | |
| 26 "SELF_SUPERVISED": "self-supervised", | |
| 27 "IMAGE_TO_IMAGE": "image-to-image", | |
| 28 } | |
| 29 template_name = ( | |
| 30 template_dir_map[workflow] | |
| 31 + "/" | |
| 32 + dims.lower() + "_" + template_dir_map[workflow] + ".yaml" | |
| 33 ) | |
| 34 | |
| 35 url = ( | |
| 36 f"https://raw.githubusercontent.com/BiaPyX/BiaPy/" | |
| 37 f"refs/tags/v{biapy_version}/templates/{template_name}" | |
| 38 ) | |
| 39 print(f"Downloading YAML template from {url}") | |
| 40 response = requests.get(url) | |
| 41 if response.status_code != 200: | |
| 42 raise RuntimeError( | |
| 43 f"Failed to download YAML template: {response.status_code}" | |
| 44 ) | |
| 45 return yaml.safe_load(response.text) | |
| 46 | |
| 47 | |
| 48 def tuple_to_list(obj): | |
| 49 """Convert tuples to lists recursively.""" | |
| 50 if isinstance(obj, tuple): | |
| 51 return list(obj) | |
| 52 if isinstance(obj, dict): | |
| 53 return {k: tuple_to_list(v) for k, v in obj.items()} | |
| 54 if isinstance(obj, list): | |
| 55 return [tuple_to_list(v) for v in obj] | |
| 56 return obj | |
| 57 | |
| 58 | |
| 59 def main(): | |
| 60 parser = argparse.ArgumentParser( | |
| 61 description="Generate a YAML configuration from given arguments." | |
| 62 ) | |
| 63 parser.add_argument( | |
| 64 '--input_config_path', default='', type=str, | |
| 65 help="Input configuration file to reuse" | |
| 66 ) | |
| 67 parser.add_argument( | |
| 68 '--new_config', action='store_true', | |
| 69 help="Whether to create a new config or reuse an existing one." | |
| 70 ) | |
| 71 parser.add_argument( | |
| 72 '--out_config_path', required=True, type=str, | |
| 73 help="Path to save the generated YAML configuration." | |
| 74 ) | |
| 75 parser.add_argument( | |
| 76 '--workflow', default='semantic', type=str, | |
| 77 choices=['semantic', 'instance', 'detection', 'denoising', | |
| 78 'sr', 'cls', 'sr2', 'i2i'], | |
| 79 ) | |
| 80 parser.add_argument( | |
| 81 '--dims', default='2d', type=str, | |
| 82 choices=['2d_stack', '2d', '3d'], | |
| 83 help="Number of dimensions for the problem" | |
| 84 ) | |
| 85 parser.add_argument( | |
| 86 '--obj_slices', default='', type=str, | |
| 87 choices=['', '1-5', '5-10', '10-20', '20-60', '60+'], | |
| 88 help="Number of slices for the objects in the images" | |
| 89 ) | |
| 90 parser.add_argument( | |
| 91 '--obj_size', default='0-25', type=str, | |
| 92 choices=['0-25', '25-100', '100-200', '200-500', '500+'], | |
| 93 help="Size of the objects in the images" | |
| 94 ) | |
| 95 parser.add_argument( | |
| 96 '--img_channel', default=1, type=int, | |
| 97 help="Number of channels in the input images" | |
| 98 ) | |
| 99 parser.add_argument( | |
| 100 '--model_source', default='biapy', | |
| 101 choices=['biapy', 'bmz', 'torchvision'], | |
| 102 help="Source of the model." | |
| 103 ) | |
| 104 parser.add_argument( | |
| 105 '--model', default='', type=str, | |
| 106 help=("Path to the model file if using a pre-trained model " | |
| 107 "from BiaPy or name of the model within BioImage " | |
| 108 "Model Zoo or TorchVision.") | |
| 109 ) | |
| 110 parser.add_argument( | |
| 111 '--raw_train', default='', type=str, | |
| 112 help="Path to the training raw data." | |
| 113 ) | |
| 114 parser.add_argument( | |
| 115 '--gt_train', default='', type=str, | |
| 116 help="Path to the training ground truth data." | |
| 117 ) | |
| 118 parser.add_argument( | |
| 119 '--test_raw_path', default='', type=str, | |
| 120 help="Path to the testing raw data." | |
| 121 ) | |
| 122 parser.add_argument( | |
| 123 '--test_gt_path', default='', type=str, | |
| 124 help="Path to the testing ground truth data." | |
| 125 ) | |
| 126 parser.add_argument( | |
| 127 '--biapy_version', default='', type=str, | |
| 128 help="BiaPy version to use." | |
| 129 ) | |
| 130 | |
| 131 args = parser.parse_args() | |
| 132 | |
| 133 if args.new_config: | |
| 134 workflow_map = { | |
| 135 "semantic": "SEMANTIC_SEG", | |
| 136 "instance": "INSTANCE_SEG", | |
| 137 "detection": "DETECTION", | |
| 138 "denoising": "DENOISING", | |
| 139 "sr": "SUPER_RESOLUTION", | |
| 140 "cls": "CLASSIFICATION", | |
| 141 "sr2": "SELF_SUPERVISED", | |
| 142 "i2i": "IMAGE_TO_IMAGE", | |
| 143 } | |
| 144 workflow_type = workflow_map[args.workflow] | |
| 145 | |
| 146 if args.dims == "2d_stack": | |
| 147 ndim = "2D" | |
| 148 as_stack = True | |
| 149 elif args.dims == "2d": | |
| 150 ndim = "2D" | |
| 151 as_stack = True | |
| 152 elif args.dims == "3d": | |
| 153 ndim = "3D" | |
| 154 as_stack = False | |
| 155 | |
| 156 config = download_yaml_template( | |
| 157 workflow_type, ndim, biapy_version=args.biapy_version | |
| 158 ) | |
| 159 | |
| 160 config["PROBLEM"]["TYPE"] = workflow_type | |
| 161 config["PROBLEM"]["NDIM"] = ndim | |
| 162 config["TEST"]["ANALIZE_2D_IMGS_AS_3D_STACK"] = as_stack | |
| 163 | |
| 164 if args.model_source == "biapy": | |
| 165 config["MODEL"]["SOURCE"] = "biapy" | |
| 166 if args.model: | |
| 167 config["MODEL"]["LOAD_CHECKPOINT"] = True | |
| 168 config["MODEL"]["LOAD_MODEL_FROM_CHECKPOINT"] = True | |
| 169 config.setdefault("PATHS", {}) | |
| 170 config["PATHS"]["CHECKPOINT_FILE"] = args.model | |
| 171 else: | |
| 172 config["MODEL"]["LOAD_CHECKPOINT"] = False | |
| 173 config["MODEL"]["LOAD_MODEL_FROM_CHECKPOINT"] = False | |
| 174 elif args.model_source == "bmz": | |
| 175 config["MODEL"]["SOURCE"] = "bmz" | |
| 176 config["MODEL"]["LOAD_CHECKPOINT"] = False | |
| 177 config["MODEL"]["LOAD_MODEL_FROM_CHECKPOINT"] = False | |
| 178 config.setdefault("MODEL", {}).setdefault("BMZ", {}) | |
| 179 config["MODEL"]["BMZ"]["SOURCE_MODEL_ID"] = args.model | |
| 180 elif args.model_source == "torchvision": | |
| 181 config["MODEL"]["SOURCE"] = "torchvision" | |
| 182 config["MODEL"]["LOAD_CHECKPOINT"] = False | |
| 183 config["MODEL"]["LOAD_MODEL_FROM_CHECKPOINT"] = False | |
| 184 config["MODEL"]["TORCHVISION_MODEL_NAME"] = args.model | |
| 185 | |
| 186 obj_size_map = { | |
| 187 "0-25": (256, 256), | |
| 188 "25-100": (256, 256), | |
| 189 "100-200": (512, 512), | |
| 190 "200-500": (512, 512), | |
| 191 "500+": (1024, 1024), | |
| 192 } | |
| 193 obj_size = obj_size_map[args.obj_size] | |
| 194 | |
| 195 obj_slices_map = { | |
| 196 "": -1, | |
| 197 "1-5": 5, | |
| 198 "5-10": 10, | |
| 199 "10-20": 20, | |
| 200 "20-60": 40, | |
| 201 "60+": 80, | |
| 202 } | |
| 203 obj_slices = obj_slices_map[args.obj_slices] | |
| 204 if config["PROBLEM"]["NDIM"] == "2D": | |
| 205 config["DATA"]["PATCH_SIZE"] = obj_size + (args.img_channel,) | |
| 206 else: | |
| 207 assert obj_slices != -1, ( | |
| 208 "For 3D problems, obj_slices must be specified." | |
| 209 ) | |
| 210 config["DATA"]["PATCH_SIZE"] = ( | |
| 211 (obj_slices,) + obj_size + (args.img_channel,) | |
| 212 ) | |
| 213 config["DATA"]["PATCH_SIZE"] = str(config["DATA"]["PATCH_SIZE"]) | |
| 214 else: | |
| 215 assert args.input_config_path, ( | |
| 216 "Input configuration path must be specified when not " | |
| 217 "creating a new config." | |
| 218 ) | |
| 219 with open(args.input_config_path, 'r', encoding='utf-8') as f: | |
| 220 config = yaml.safe_load(f) | |
| 221 | |
| 222 if args.model: | |
| 223 config["MODEL"]["SOURCE"] = "biapy" | |
| 224 config["MODEL"]["LOAD_CHECKPOINT"] = True | |
| 225 config["MODEL"]["LOAD_MODEL_FROM_CHECKPOINT"] = True | |
| 226 config.setdefault("PATHS", {}) | |
| 227 config["PATHS"]["CHECKPOINT_FILE"] = args.model | |
| 228 else: | |
| 229 config["MODEL"]["LOAD_CHECKPOINT"] = False | |
| 230 config["MODEL"]["LOAD_MODEL_FROM_CHECKPOINT"] = False | |
| 231 | |
| 232 if args.raw_train: | |
| 233 config["TRAIN"]["ENABLE"] = True | |
| 234 config["DATA"]["TRAIN"]["PATH"] = args.raw_train | |
| 235 config["DATA"]["TRAIN"]["GT_PATH"] = args.gt_train | |
| 236 else: | |
| 237 config["TRAIN"]["ENABLE"] = False | |
| 238 | |
| 239 if args.test_raw_path: | |
| 240 config["TEST"]["ENABLE"] = True | |
| 241 config["DATA"]["TEST"]["PATH"] = args.test_raw_path | |
| 242 if args.test_gt_path: | |
| 243 config["DATA"]["TEST"]["LOAD_GT"] = True | |
| 244 config["DATA"]["TEST"]["GT_PATH"] = args.test_gt_path | |
| 245 else: | |
| 246 config["DATA"]["TEST"]["LOAD_GT"] = False | |
| 247 else: | |
| 248 config["TEST"]["ENABLE"] = False | |
| 249 | |
| 250 # Always use safetensors in Galaxy | |
| 251 config["MODEL"]["OUT_CHECKPOINT_FORMAT"] = "safetensors" | |
| 252 | |
| 253 config = tuple_to_list(config) | |
| 254 | |
| 255 with open(args.out_config_path, 'w', encoding='utf-8') as f: | |
| 256 yaml.dump(config, f, default_flow_style=False) | |
| 257 | |
| 258 print(f"YAML configuration written to {args.out_config_path}") | |
| 259 | |
| 260 | |
| 261 if __name__ == "__main__": | |
| 262 main() |
