Mercurial > repos > goeckslab > tiling_pyhist
annotate tiling_pyhist.py @ 0:c051e9688932 draft default tip
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
| author | goeckslab | 
|---|---|
| date | Thu, 03 Jul 2025 23:48:01 +0000 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 0 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 1 import argparse | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 2 import logging | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 3 import os | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 4 import subprocess | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 5 import sys | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 6 import tempfile | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 7 import zipfile | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 8 from concurrent.futures import ProcessPoolExecutor | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 9 from pathlib import Path | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 10 from typing import Tuple | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 11 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 12 import openslide | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 13 import psutil | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 14 from pyhist import PySlide, TileGenerator | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 15 from src import utility_functions | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 16 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 17 # Configure logging to stdout | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 18 logging.basicConfig( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 19 stream=sys.stdout, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 20 format="%(asctime)s - %(levelname)s - %(message)s", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 21 level=logging.INFO, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 22 ) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 23 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 24 # Constants | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 25 SEGMENT_BINARY_PATH = "/pyhist/src/graph_segmentation/segment" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 26 DEFAULT_PATCH_SIZE = 256 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 27 DEFAULT_DOWNSCALE_FACTOR = 8 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 28 TILE_FORMAT = "png" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 29 MEMORY_PER_WORKER = 1 # GB, estimated memory per worker process | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 30 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 31 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 32 def log_memory_usage() -> None: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 33 """Log the current memory usage of the process in megabytes.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 34 process = psutil.Process(os.getpid()) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 35 mem_info = process.memory_info() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 36 logging.info( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 37 "Memory usage: RSS=%.2f MB, VMS=%.2f MB", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 38 mem_info.rss / 1024 / 1024, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 39 mem_info.vms / 1024 / 1024 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 40 ) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 41 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 42 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 43 def validate_slide(image_path: Path) -> None: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 44 """Validate the input image using OpenSlide.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 45 try: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 46 with openslide.OpenSlide(str(image_path)): | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 47 logging.info("Validated input file with OpenSlide: %s", image_path) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 48 except openslide.OpenSlideError as error: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 49 raise RuntimeError("Invalid input file: %s", error) from error | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 50 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 51 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 52 def check_segmentation_binary() -> bool: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 53 """Check if the segmentation binary exists and is executable.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 54 if os.path.exists(SEGMENT_BINARY_PATH) and os.access(SEGMENT_BINARY_PATH, os.X_OK): | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 55 logging.info("Segmentation executable found: %s", SEGMENT_BINARY_PATH) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 56 return True | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 57 logging.warning("Segmentation executable missing, using Otsu method") | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 58 return False | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 59 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 60 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 61 def build_pyhist_config(image_path: Path, output_dir: Path) -> dict: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 62 """Build the configuration dictionary for PyHIST processing.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 63 return { | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 64 "svs": str(image_path), | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 65 "patch_size": DEFAULT_PATCH_SIZE, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 66 "method": "otsu", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 67 "thres": 0.1, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 68 "output_downsample": DEFAULT_DOWNSCALE_FACTOR, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 69 "mask_downsample": DEFAULT_DOWNSCALE_FACTOR, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 70 "borders": "0000", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 71 "corners": "1010", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 72 "pct_bc": 1, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 73 "k_const": 1000, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 74 "minimum_segmentsize": 1000, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 75 "save_patches": True, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 76 "save_blank": False, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 77 "save_nonsquare": False, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 78 "save_tilecrossed_image": False, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 79 "save_mask": True, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 80 "save_edges": False, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 81 "info": "verbose", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 82 "output": str(output_dir), | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 83 "format": TILE_FORMAT, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 84 } | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 85 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 86 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 87 def process_image_with_pyhist( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 88 image_path: Path, output_dir: Path, original_name: str | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 89 ) -> Path: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 90 """Process a single image with PyHIST and return the tile directory.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 91 logging.info("Processing image: %s", image_path) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 92 log_memory_usage() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 93 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 94 # Validate input | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 95 validate_slide(image_path) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 96 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 97 # Check segmentation method | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 98 check_segmentation_binary() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 99 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 100 # Prepare PyHIST configuration | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 101 config = build_pyhist_config(image_path, output_dir) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 102 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 103 # Set logging level based on config | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 104 log_levels = { | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 105 "default": logging.INFO, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 106 "verbose": logging.DEBUG, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 107 "silent": logging.CRITICAL, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 108 } | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 109 logging.getLogger().setLevel(log_levels[config["info"]]) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 110 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 111 # Process the slide | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 112 utility_functions.check_image(config["svs"]) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 113 slide = PySlide(config) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 114 logging.info("Slide loaded: %s", slide) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 115 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 116 tile_generator = TileGenerator(slide) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 117 logging.info("Tile generator initialized: %s", tile_generator) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 118 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 119 try: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 120 tile_generator.execute() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 121 except subprocess.CalledProcessError as error: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 122 raise RuntimeError("Tile extraction failed: %s", error) from error | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 123 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 124 tile_dir = Path(slide.tile_folder) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 125 tiles = list(tile_dir.glob(f"*.{TILE_FORMAT}")) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 126 logging.info("Found %d tiles in %s", len(tiles), tile_dir) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 127 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 128 utility_functions.clean(slide) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 129 return tile_dir | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 130 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 131 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 132 def append_tiles_to_zip( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 133 zip_file: zipfile.ZipFile, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 134 original_name: str, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 135 tile_dir: Path | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 136 ) -> None: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 137 """Append PNG tiles from the tile directory to the ZIP file.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 138 original_base = Path(original_name).stem | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 139 tiles = list(tile_dir.glob(f"*.{TILE_FORMAT}")) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 140 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 141 for tile in tiles: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 142 tile_number = tile.stem.split("_")[-1] | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 143 arcname = f"{original_base}/{original_base}_{tile_number}.{TILE_FORMAT}" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 144 zip_file.write(tile, arcname) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 145 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 146 logging.info("Appended %d tiles from %s", len(tiles), tile_dir) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 147 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 148 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 149 def process_single_image(task: Tuple[Path, str, Path]) -> Path: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 150 """Process a single image and return the tile directory.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 151 image_path, original_name, output_dir = task | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 152 try: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 153 tile_dir = process_image_with_pyhist( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 154 image_path, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 155 output_dir, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 156 original_name | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 157 ) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 158 return tile_dir | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 159 except Exception as error: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 160 logging.error("Error processing %s: %s", image_path, error) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 161 raise | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 162 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 163 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 164 def get_max_workers() -> int: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 165 """Determine the maximum number of worker processes based on available resources.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 166 cpu_cores = psutil.cpu_count(logical=False) # Physical CPU cores | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 167 available_memory = psutil.virtual_memory().available / (1024 ** 3) # in GB | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 168 max_workers_memory = available_memory // MEMORY_PER_WORKER | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 169 max_workers = min(cpu_cores, max_workers_memory) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 170 return max(1, int(max_workers)) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 171 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 172 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 173 def parse_arguments() -> argparse.Namespace: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 174 """Parse command-line arguments.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 175 parser = argparse.ArgumentParser(description="Tile extraction for Galaxy") | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 176 parser.add_argument( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 177 "--input", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 178 action="append", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 179 help="Input image paths", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 180 default=[] | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 181 ) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 182 parser.add_argument( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 183 "--original_name", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 184 action="append", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 185 help="Original file names", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 186 default=[] | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 187 ) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 188 parser.add_argument( | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 189 "--output_zip", | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 190 required=True, | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 191 help="Output ZIP file path" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 192 ) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 193 return parser.parse_args() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 194 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 195 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 196 def main() -> None: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 197 """Main function to orchestrate tile extraction and ZIP creation with dynamic multiprocessing.""" | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 198 # Removed os.chdir("/pyhist") to stay in Galaxy's working directory | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 199 logging.info("Working directory: %s", os.getcwd()) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 200 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 201 args = parse_arguments() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 202 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 203 if len(args.input) != len(args.original_name): | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 204 raise ValueError("Mismatch between input paths and original names") | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 205 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 206 # Create a temporary directory using tempfile | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 207 with tempfile.TemporaryDirectory(prefix="pyhist_tiles_", dir=os.getcwd()) as temp_dir_path: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 208 temp_dir = Path(temp_dir_path) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 209 logging.info("Created temporary directory: %s", temp_dir) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 210 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 211 # Prepare tasks with unique output directories | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 212 tasks = [ | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 213 (Path(image_path), original_name, temp_dir / Path(original_name).stem) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 214 for image_path, original_name in zip(args.input, args.original_name) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 215 ] | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 216 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 217 # Determine the number of worker processes based on available resources | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 218 max_workers = get_max_workers() | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 219 logging.info("Using %d worker processes", max_workers) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 220 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 221 # Process images in parallel | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 222 with ProcessPoolExecutor(max_workers=max_workers) as executor: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 223 tile_dirs = list(executor.map(process_single_image, tasks)) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 224 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 225 # Create the ZIP file and append all tiles | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 226 with zipfile.ZipFile(args.output_zip, "w", zipfile.ZIP_DEFLATED) as zip_file: | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 227 for (image_path, original_name, output_dir), tile_dir in zip(tasks, tile_dirs): | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 228 append_tiles_to_zip(zip_file, original_name, tile_dir) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 229 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 230 logging.info("Final ZIP size: %d bytes", Path(args.output_zip).stat().st_size) | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 231 # No need for shutil.rmtree as TemporaryDirectory cleans up automatically | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 232 logging.info("Temporary directory cleaned up") | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 233 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 234 | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 235 if __name__ == "__main__": | 
| 
c051e9688932
planemo upload for repository https://github.com/goeckslab/gleam.git commit 11356473f09dd54d86af28b74bd9ed097d07ca04
 goeckslab parents: diff
changeset | 236 main() | 
