Skip to content

Commit

Permalink
Merge pull request #40 from dbouget/skip_convert
Browse files Browse the repository at this point in the history
Added QoL options
  • Loading branch information
andreped authored Jan 30, 2025
2 parents 83b5a36 + 124744c commit 1fc4c42
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
*.vsi
*.zip
*bftools/
*.ets
*.ets
*.xml
*.iml
*.idea/
40 changes: 32 additions & 8 deletions vsi2tif/src/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ def cellsens2tif_single(
plane: int = 0,
quality: int = 85,
max_mem: int = 32,
skip_converted: bool = True,
verbose: int = 1,
) -> None:

if int(plane) == -1:
if os.path.exists(output_path) and skip_converted:
logging.info(f"Skipping already converted slide: {output_path}")
return

image_folder = os.path.join(os.path.dirname(output_path), os.path.basename(output_path).replace(".tif", ""))
for s in range(50):
try:
Expand All @@ -49,7 +54,10 @@ def cellsens2tif_single(
logging.error("Issue cleaning up after all planes conversion.")
logging.error(traceback.format_exc())
else:
cellsens2tif(input_path, output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose)
if os.path.exists(output_path) and not skip_converted:
cellsens2tif(input_path, output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose)
elif skip_converted:
logging.info(f"Skipping already converted slide: {output_path}")


@benchmark
Expand All @@ -62,34 +70,50 @@ def cellsens2tif_batch(
plane: int = 0,
quality: int = 85,
max_mem: int = 32,
verbose: int = 1,
remove_name_spaces: bool = False,
skip_converted: bool = True,
extension_type: str = ".vsi",
verbose: int = 1,
) -> None:
# create directory if it does not exist
os.makedirs(output_path, exist_ok=True)

# find path to all cellSens VSI images to convert
# find path to all cellSens images to convert
paths = []
for root, _, files in os.walk(input_path):
for file in files:
if file.lower().endswith("overview.vsi"):
if "overview" in file.lower():
logging.info("Skipping overview file: {}".format(file))
continue
if file.endswith(".vsi"):
if file.endswith(extension_type):
paths.append((root, file))

# perform conversion in separate processes
for root, file in tqdm(paths):
curr_input_path = os.path.join(root, file)
if remove_name_spaces:
curr_output_path = (
(output_path + "/" + curr_input_path.split(input_path)[-1]).replace(" ", "_").replace(".vsi", ".tif")
(output_path + "/" + curr_input_path.split(input_path)[-1])
.replace(" ", "_")
.replace(extension_type, ".tif")
)
else:
curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(".vsi", ".tif")
curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(
extension_type, ".tif"
)

try:
cellsens2tif_single(
curr_input_path, curr_output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose
curr_input_path,
curr_output_path,
bfconvert,
compression,
tz,
plane,
quality,
max_mem,
skip_converted,
verbose,
)
except Exception:
continue
16 changes: 14 additions & 2 deletions vsi2tif/vsi2tif.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def main():
parser.add_argument("-m", "--max-mem", help="set maximum memory in the java vm - default 32", default=32)
parser.add_argument("-v", "--verbose", help="set verbosity level - default 1", default=1, type=int)
parser.add_argument(
"--remove-name-spaces", help="replace spaces in filename with underscores in batch mode", action="store_true"
"--remove-name-spaces",
help="replace spaces in filename with underscores in batch mode",
action="store_true",
)
parser.add_argument(
"-p",
Expand All @@ -37,7 +39,14 @@ def main():
default=0,
type=int,
)
parser.add_argument(
"--noskip-converted",
help="To specifically request existing files to be converted again",
action="store_true",
)
parser.add_argument("-f", "--extension", help="extension type to consider (e.g., .vsi)", default=".vsi", type=str)
argv = parser.parse_args()
skip_converted = not argv.noskip_converted

if argv.verbose not in list(range(6)):
raise ValueError("Verbosity level must be an integer between 0 and 5")
Expand All @@ -60,8 +69,10 @@ def main():
argv.plane,
argv.quality,
argv.max_mem,
argv.verbose,
argv.remove_name_spaces,
skip_converted,
argv.extension,
argv.verbose,
)
else:
logging.info("Performing single conversion...")
Expand All @@ -74,6 +85,7 @@ def main():
argv.plane,
argv.quality,
argv.max_mem,
skip_converted,
argv.verbose,
)

Expand Down

0 comments on commit 1fc4c42

Please sign in to comment.