Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion formats/ome-converter-tool/.bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.3.3
current_version = 0.3.4-dev2
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<dev>\d+))?
Expand Down
4 changes: 3 additions & 1 deletion formats/ome-converter-tool/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM polusai/bfio:2.4.4
FROM polusai/bfio:2.5.0

# environment variables defined in polusai/bfio
ENV EXEC_DIR="/opt/executables"
ENV POLUS_IMG_EXT=".ome.tif"
ENV POLUS_TAB_EXT=".csv"
ENV POLUS_LOG="INFO"
ENV NUM_THREADS=2
ENV NUM_WORKERS=4

# Work directory defined in the base container
WORKDIR ${EXEC_DIR}
Expand Down
12 changes: 10 additions & 2 deletions formats/ome-converter-tool/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OME Converter (v0.3.3)
# OME Converter (v0.3.4-dev2)

This WIPP plugin converts BioFormats supported data types to the
OME Zarr or OME TIF file format. This is not a complete implementation, rather it implements a file
Expand Down Expand Up @@ -35,4 +35,12 @@ This plugin takes 2 input arguments and 1 output argument:
## Docker Command

```bash
docker run -e POLUS_IMG_EXT=".ome.zarr" -v /Users/username/:/Users/username/ polusai/ome-converter-tool:0.3.3 --inpDir=/Users/path/to/Images/ --filePattern=".*.tif" --outDir=/Users/path/to/outputs
docker run \
-e POLUS_IMG_EXT=".ome.zarr" \
-e NUM_THREADS=4 \
-e NUM_WORKERS=2 \
-v /Users/username/:/Users/username/ \
polusai/ome-converter-tool:0.3.4-dev2 \
--inpDir=/Users/path/to/Images/ \
--filePattern=".*.tif" \
--outDir=/Users/path/to/outputs
2 changes: 1 addition & 1 deletion formats/ome-converter-tool/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.3
0.3.4-dev2
6 changes: 3 additions & 3 deletions formats/ome-converter-tool/ict.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ author:
- Nick Schaub
- Hamdah Shafqat
contact: nick.schaub@nih.gov
container: polusai/ome-converter-tool:0.3.3
container: polusai/ome-converter-tool:0.3.4-dev2
description: Convert Bioformats supported format to OME Zarr or OME TIF
entrypoint: python3 -m polus.images.formats.ome_converter
inputs:
- description: Input generic data collection to be processed by this plugin
format:
- genericData
- string
name: inpDir
required: true
type: path
Expand Down Expand Up @@ -38,4 +38,4 @@ ui:
key: inputs.filePattern
title: Filepattern
type: text
version: 0.3.3
version: 0.3.4-dev2
16 changes: 16 additions & 0 deletions formats/ome-converter-tool/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[mypy]
files = src/polus/images/formats/ome_converter, tests/
exclude = \.git/
disable_error_code = import-untyped

[mypy-bfio]
ignore_missing_imports = True

[mypy-filepattern]
ignore_missing_imports = True

[mypy-tqdm]
ignore_missing_imports = True

# [mypy-polus.images.formats.ome_converter.*]
# ignore_missing_imports = False
4 changes: 2 additions & 2 deletions formats/ome-converter-tool/omeconverter.cwl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inputs:
inpDir:
inputBinding:
prefix: --inpDir
type: Directory
type: string
outDir:
inputBinding:
prefix: --outDir
Expand All @@ -20,7 +20,7 @@ outputs:
type: Directory
requirements:
DockerRequirement:
dockerPull: polusai/ome-converter-tool:0.3.3
dockerPull: polusai/ome-converter-tool:0.3.4-dev2
InitialWorkDirRequirement:
listing:
- entry: $(inputs.outDir)
Expand Down
6 changes: 3 additions & 3 deletions formats/ome-converter-tool/plugin.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "OME Converter",
"version": "0.3.3",
"version": "0.3.4-dev2",
"title": "OME Converter",
"description": "Convert Bioformats supported format to OME Zarr or OME TIF",
"author": "Nick Schaub (nick.schaub@nih.gov), Hamdah Shafqat Abbasi (hamdahshafqat.abbasi@nih.gov)",
"institution": "National Center for Advancing Translational Sciences, National Institutes of Health",
"repository": "https://github.com/PolusAI/image-tools",
"website": "https://ncats.nih.gov/preclinical/core/informatics",
"citation": "",
"containerId": "polusai/ome-converter-tool:0.3.3",
"containerId": "polusai/ome-converter-tool:0.3.4-dev2",
"baseCommand": [
"python3",
"-m",
Expand All @@ -17,7 +17,7 @@
"inputs": [
{
"name": "inpDir",
"type": "genericData",
"type": "string",
"description": "Input generic data collection to be processed by this plugin",
"required": true
},
Expand Down
26 changes: 18 additions & 8 deletions formats/ome-converter-tool/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "polus-images-formats-ome-converter"
version = "0.3.3"
version = "0.3.4-dev2"
description = "Convert BioFormats datatypes to ome.tif or ome.zarr file format"
authors = [
"Nick Schaub <nick.schaub@nih.gov>",
Expand All @@ -11,21 +11,31 @@ readme = "README.md"
packages = [{include = "polus", from = "src"}]

[tool.poetry.dependencies]
python = ">=3.9,<3.12"
bfio = {version = "^2.4.4", extras = ["all"]}
filepattern = "^2.0.4"
typer = "^0.7.0"
python = ">=3.11,<3.13"
bfio = "2.5.0"
filepattern = "2.1.4"
typer = "0.24.1"
tqdm = "^4.64.1"
preadator = "0.4.0-dev2"
numpy = "<2.0.0"
numpy = ">2.0.0"
scikit-image = "^0.26.0"

[tool.poetry.group.dev.dependencies]
bump2version = "^1.0.1"
pre-commit = "^3.0.4"
pytest = "^7.2.1"
ipykernel = "^6.21.2"
requests = "^2.28.2"
scikit-image = "^0.19.3"
scikit-image = ">=0.26.0"
matplotlib = "3.9.4"

[tool.mypy]
files = "."

# Use strict defaults
strict = true
warn_unreachable = true
warn_no_return = true


[build-system]
requires = ["poetry-core"]
Expand Down
14 changes: 9 additions & 5 deletions formats/ome-converter-tool/run-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ outDir=/data/output
# docker run polusai/ome-converter-plugin:${version}

# Run the plugin
docker run -e POLUS_IMG_EXT=${fileExtension} --mount type=bind,source=${datapath},target=/data/ \
polusai/ome-converter-tool:${version} \
--inpDir ${inpDir} \
--filePattern ${filePattern} \
--outDir ${outDir}
docker run \
-e POLUS_IMG_EXT=${fileExtension} \
-e NUM_THREADS=2 \
-e NUM_WORKERS=4 \
--mount type=bind,source=${datapath},target=/data/ \
polusai/ome-converter-tool:${version} \
--inpDir ${inpDir} \
--filePattern ${filePattern} \
--outDir ${outDir}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Ome Converter."""

__version__ = "0.3.3"
__version__ = "0.3.4-dev2"

from .image_converter import batch_convert
from .image_converter import convert_image
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import logging
import os
import pathlib
from concurrent.futures import as_completed
from typing import Any
from typing import Optional

import filepattern as fp
import preadator
import typer
from polus.images.formats.ome_converter.image_converter import NUM_THREADS
from polus.images.formats.ome_converter.image_converter import convert_image
from tqdm import tqdm
from polus.images.formats.ome_converter.image_converter import POLUS_IMG_EXT
from polus.images.formats.ome_converter.image_converter import batch_convert

app = typer.Typer()

Expand All @@ -23,22 +20,71 @@
)
logger = logging.getLogger("polus.images.formats.ome_converter")
logger.setLevel(os.environ.get("POLUS_LOG", logging.INFO))
POLUS_IMG_EXT = os.environ.get("POLUS_IMG_EXT", ".ome.tif")


def parse_input_dir(value: str) -> pathlib.Path | list[pathlib.Path]:
"""Parse a directory or comma-separated file paths."""
if "," in value:
files = [
pathlib.Path(f.strip()).expanduser().resolve()
for f in value.split(",")
if f.strip()
]
if not files:
msg = "Empty file list provided"
raise typer.BadParameter(msg)
return files
path = pathlib.Path(value).expanduser().resolve()
if not path.is_dir():
msg = f"Directory {path} does not exist"
raise typer.BadParameter(msg)
return path


def _collect_files(
inp: str | pathlib.Path | list[pathlib.Path],
pattern: str,
) -> list[pathlib.Path]:
"""Normalize input into a list of files."""
if isinstance(inp, (str, pathlib.Path)):
dir_path = pathlib.Path(inp) if isinstance(inp, str) else inp
if not dir_path.is_dir():
msg = f"Input path is not a directory: {dir_path}"
raise ValueError(msg)
fps = fp.FilePattern(dir_path, pattern)
return [files[1][0] for files in fps()]

if isinstance(inp, list):
return [pathlib.Path(p) if isinstance(p, str) else p for p in inp]
msg = "Expected str, Path, or list[Path/str]"
raise TypeError(msg)


def write_preview(
out_dir: pathlib.Path,
file_pattern: str,
files: list[pathlib.Path],
) -> None:
"""Write a JSON preview of the files that would be converted."""
preview: dict[str, Any] = {
"filePattern": file_pattern,
"outDir": [f.stem + POLUS_IMG_EXT for f in files],
}
preview_path = out_dir / "preview.json"
with preview_path.open("w") as f:
json.dump(preview, f, indent=2)
logger.info(f"Preview written to {preview_path}")


@app.command()
def main(
inp_dir: pathlib.Path = typer.Option(
inp_dir: str = typer.Option(
...,
"--inpDir",
help="Input generic data collection to be processed by this plugin",
exists=True,
resolve_path=True,
readable=True,
file_okay=False,
dir_okay=True,
callback=parse_input_dir,
),
pattern: str = typer.Option(
file_pattern: str = typer.Option(
".+",
"--filePattern",
help="A filepattern defining the images to be converted",
Expand All @@ -62,44 +108,22 @@ def main(
"""Convert bioformat supported image datatypes conversion to ome.tif or ome.zarr."""
logger.info(f"inpDir = {inp_dir}")
logger.info(f"outDir = {out_dir}")
logger.info(f"filePattern = {pattern}")
logger.info(f"filePattern = {file_pattern}")
logger.info(f"preview = {preview}")

fps = fp.FilePattern(inp_dir, pattern)
files = _collect_files(inp_dir, file_pattern) # type: ignore
logger.info(f"Found {len(files)} file(s) to process.")

if preview:
with out_dir.joinpath("preview.json").open("w") as jfile:
out_json: dict[str, Any] = {
"filepattern": pattern,
"outDir": [],
}
for file in fps():
out_name = str(file[1][0].name.split(".")[0]) + POLUS_IMG_EXT
out_json["outDir"].append(out_name)
json.dump(out_json, jfile, indent=2)
write_preview(out_dir, file_pattern, files)
return

with preadator.ProcessManager(
name="ome_converter",
num_processes=NUM_THREADS,
threads_per_process=2,
) as executor:
threads = []
for files in fps():
file = files[1][0]
threads.append(
executor.submit_process(convert_image, file, POLUS_IMG_EXT, out_dir),
)

for f in tqdm(
as_completed(threads),
total=len(threads),
mininterval=5,
desc=f"converting images to {POLUS_IMG_EXT}",
initial=0,
unit_scale=True,
colour="cyan",
):
f.result()
batch_convert(
inp_dir=inp_dir,
out_dir=out_dir,
file_pattern=file_pattern,
file_extension=POLUS_IMG_EXT,
)


if __name__ == "__main__":
Expand Down
Loading
Loading