Skip to content

Commit 59de165

Browse files
committed
Refactor whitespace and improve code clarity in pipeline generator
- Removed unnecessary whitespace in the app_generator and bundle_downloader files to enhance readability. - Streamlined import statements in test files for better organization. - Ensured consistent formatting across various sections of the codebase. Signed-off-by: Victor Chang <[email protected]>
1 parent d0c1e33 commit 59de165

File tree

4 files changed

+69
-85
lines changed

4 files changed

+69
-85
lines changed

tools/pipeline-generator/pipeline_generator/generator/app_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def generate_app(
112112
# Download the bundle
113113
logger.info(f"Downloading bundle: {model_id}")
114114
bundle_path = self.downloader.download_bundle(model_id, output_dir)
115-
115+
116116
# Organize bundle into proper structure if needed
117117
self.downloader.organize_bundle_structure(bundle_path)
118118

@@ -278,19 +278,19 @@ def _prepare_context(
278278
# Collect dependency hints from metadata.json
279279
required_packages_version = metadata.get("required_packages_version", {}) if metadata else {}
280280
extra_dependencies = getattr(model_config, "dependencies", []) if model_config else []
281-
281+
282282
# Handle dependency conflicts between config and metadata
283283
config_deps = []
284284
if extra_dependencies:
285285
# Extract dependency names from config overrides
286286
config_deps = [dep.split(">=")[0].split("==")[0].split("<")[0] for dep in extra_dependencies]
287-
287+
288288
# Add metadata dependencies only if not overridden by config
289289
if metadata and "numpy_version" in metadata and "numpy" not in config_deps:
290290
extra_dependencies.append(f"numpy=={metadata['numpy_version']}")
291291
if metadata and "pytorch_version" in metadata and "torch" not in config_deps:
292292
extra_dependencies.append(f"torch=={metadata['pytorch_version']}")
293-
293+
294294
# Handle MONAI version - move logic from template to Python for better maintainability
295295
has_monai_config = any(dep.startswith("monai") for dep in extra_dependencies)
296296
if has_monai_config and metadata:

tools/pipeline-generator/pipeline_generator/generator/bundle_downloader.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def detect_model_file(self, bundle_path: Path) -> Optional[Path]:
147147

148148
def organize_bundle_structure(self, bundle_path: Path) -> None:
149149
"""Organize bundle files into the expected MONAI Bundle structure.
150-
150+
151151
Creates the standard structure if files are in the root directory:
152152
bundle_root/
153153
configs/
@@ -156,60 +156,54 @@ def organize_bundle_structure(self, bundle_path: Path) -> None:
156156
models/
157157
model.pt
158158
model.ts
159-
159+
160160
Args:
161161
bundle_path: Path to the downloaded bundle
162162
"""
163163
configs_dir = bundle_path / "configs"
164164
models_dir = bundle_path / "models"
165-
165+
166166
# Check if structure already exists
167-
has_configs_structure = (
168-
configs_dir.exists() and
169-
(configs_dir / "metadata.json").exists()
170-
)
171-
has_models_structure = (
172-
models_dir.exists() and
173-
any(models_dir.glob("model.*"))
174-
)
175-
167+
has_configs_structure = configs_dir.exists() and (configs_dir / "metadata.json").exists()
168+
has_models_structure = models_dir.exists() and any(models_dir.glob("model.*"))
169+
176170
if has_configs_structure and has_models_structure:
177171
logger.debug("Bundle already has proper structure")
178172
return
179-
173+
180174
logger.info("Organizing bundle into standard structure")
181-
175+
182176
# Create directories
183177
configs_dir.mkdir(exist_ok=True)
184178
models_dir.mkdir(exist_ok=True)
185-
179+
186180
# Move config files to configs/
187181
config_files = ["metadata.json", "inference.json"]
188182
for config_file in config_files:
189183
src_path = bundle_path / config_file
190184
if src_path.exists() and not (configs_dir / config_file).exists():
191185
src_path.rename(configs_dir / config_file)
192186
logger.debug(f"Moved {config_file} to configs/")
193-
187+
194188
# Move model files to models/
195189
# Prefer PyTorch (.pt) > ONNX (.onnx) > TorchScript (.ts) for better compatibility
196190
model_extensions = [".pt", ".onnx", ".ts"]
197-
191+
198192
# First move model files from root directory
199193
for ext in model_extensions:
200194
for model_file in bundle_path.glob(f"*{ext}"):
201195
if model_file.is_file() and not (models_dir / model_file.name).exists():
202196
model_file.rename(models_dir / model_file.name)
203197
logger.debug(f"Moved {model_file.name} to models/")
204-
198+
205199
# Check if we already have a suitable model in the main directory
206200
# Prefer .pt files, then .onnx, then .ts
207201
has_suitable_model = False
208202
for ext in model_extensions:
209203
if any(models_dir.glob(f"*{ext}")):
210204
has_suitable_model = True
211205
break
212-
206+
213207
# If no suitable model in main directory, move from subdirectories
214208
if not has_suitable_model:
215209
# Also move model files from subdirectories to the main models/ directory
@@ -219,33 +213,33 @@ def organize_bundle_structure(self, bundle_path: Path) -> None:
219213
model_files = list(models_dir.glob(f"**/*{ext}"))
220214
if not model_files:
221215
continue
222-
216+
223217
# Filter files that are not in the main models directory
224218
subdirectory_files = [f for f in model_files if f.parent != models_dir]
225219
if not subdirectory_files:
226220
continue
227-
221+
228222
target_name = f"model{ext}"
229223
target_path = models_dir / target_name
230224
if target_path.exists():
231225
continue # Target already exists
232-
226+
233227
# Prefer non-TensorRT models for better compatibility
234228
# TensorRT models often have "_trt" in their name
235229
preferred_file = None
236230
for model_file in subdirectory_files:
237231
if "_trt" not in model_file.name.lower():
238232
preferred_file = model_file
239233
break
240-
234+
241235
# If no non-TensorRT model found, use the first available
242236
if preferred_file is None:
243237
preferred_file = subdirectory_files[0]
244-
238+
245239
# Move the preferred model file
246240
preferred_file.rename(target_path)
247241
logger.debug(f"Moved {preferred_file.name} from {preferred_file.parent.name}/ to models/{target_name}")
248-
242+
249243
# Clean up empty subdirectory if it exists
250244
try:
251245
if preferred_file.parent.exists() and not any(preferred_file.parent.iterdir()):
@@ -254,7 +248,7 @@ def organize_bundle_structure(self, bundle_path: Path) -> None:
254248
except OSError:
255249
pass # Directory not empty or other issue
256250
break # Only move one model file total
257-
251+
258252
# Ensure we have model.pt or model.ts in the main directory for MONAI Deploy
259253
# Create symlinks with standard names if needed
260254
standard_model_path = models_dir / "model.pt"

tools/pipeline-generator/tests/test_bundle_downloader.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def test_organize_bundle_structure_flat_to_structured(self, tmp_path):
230230
inference_file = bundle_path / "inference.json"
231231
model_pt_file = bundle_path / "model.pt"
232232
model_ts_file = bundle_path / "model.ts"
233-
233+
234234
metadata_file.write_text('{"name": "Test"}')
235235
inference_file.write_text('{"config": "test"}')
236236
model_pt_file.touch()
@@ -244,7 +244,7 @@ def test_organize_bundle_structure_flat_to_structured(self, tmp_path):
244244
assert (bundle_path / "configs" / "inference.json").exists()
245245
assert (bundle_path / "models" / "model.pt").exists()
246246
assert (bundle_path / "models" / "model.ts").exists()
247-
247+
248248
# Check that original files were moved (not copied)
249249
assert not metadata_file.exists()
250250
assert not inference_file.exists()
@@ -437,7 +437,7 @@ def test_organize_bundle_structure_skips_when_suitable_model_exists(self, tmp_pa
437437
# Main model should be renamed to standard name
438438
assert (models_dir / "model.pt").exists()
439439
assert not main_model.exists()
440-
440+
441441
# Subdirectory model should remain untouched
442442
assert subdir_model.exists()
443443
assert subdir.exists()
@@ -453,9 +453,9 @@ def test_organize_bundle_structure_multiple_extensions_preference(self, tmp_path
453453
pt_model = subdir / "model.pt"
454454
onnx_model = subdir / "model.onnx"
455455
ts_model = subdir / "model.ts"
456-
456+
457457
pt_model.write_bytes(b"pytorch model")
458-
onnx_model.write_bytes(b"onnx model")
458+
onnx_model.write_bytes(b"onnx model")
459459
ts_model.write_text("torchscript model")
460460

461461
# Organize structure
@@ -466,7 +466,7 @@ def test_organize_bundle_structure_multiple_extensions_preference(self, tmp_path
466466
assert not (models_dir / "model.onnx").exists()
467467
assert not (models_dir / "model.ts").exists()
468468
assert not pt_model.exists()
469-
469+
470470
# Other models should remain in subdirectory
471471
assert onnx_model.exists()
472472
assert ts_model.exists()

0 commit comments

Comments
 (0)