Skip to content

Commit

Permalink
Merge pull request #10 from andreped:verbose-support
Browse files Browse the repository at this point in the history
Added support to set CLI verbosity
  • Loading branch information
andreped authored Sep 2, 2024
2 parents dd44bb5 + 2ad48a7 commit 8d96995
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,30 @@ vsi2tif -i /path/to/olympus/wsis/ -o /path/to/converted/wsis/directory/ -b /path
Comprehensive CLI documentation can be seen below:

```
vsi2tif [-h] -i INPUT -o OUTPUT -b BFCONVERT [-c COMPRESSION] [-p PLANE] [-s TILESIZE] [-q QUALITY]
positional arguments:
INPUT folder with input files
OUTPUT folder for output files
BFCONVERT path to bfconvert tool
optional arguments:
-h, --help show this help message and exit
--compression COMPRESSION compression technique used for last conversion step - default 'jpeg'
--plane PLANE which image plane to convert image from - default 0
--tilesize TILESIZE tile size to use during both conversion steps - default 1024
--quality QUALITY compression quality used with JPEG compression - default 85
usage: vsi2tif [-h] -i INPUT -o OUTPUT -b BFCONVERT [-c COMPRESSION] [-s TILESIZE] [-p PLANE] [-q QUALITY] [-m MAX_MEM] [-v VERBOSE]
vsi2tif - simple tool for converting images from cellSens VSI to Generic TIFF
options:
-h, --help show this help message and exit
-i INPUT, --input INPUT
folder with input files
-o OUTPUT, --output OUTPUT
folder for output files
-b BFCONVERT, --bfconvert BFCONVERT
path to bfconvert tool
-c COMPRESSION, --compression COMPRESSION
compression technique for final image
-s TILESIZE, --tilesize TILESIZE
tile size to use during both conversion steps
-p PLANE, --plane PLANE
which image plane to convert image from
-q QUALITY, --quality QUALITY
compression quality used with JPEG compression
-m MAX_MEM, --max-mem MAX_MEM
set maximum memory in the java vm
-v VERBOSE, --verbose VERBOSE
set verbosity level
```

## [License](https://github.com/andreped/vsi2tif#license)
Expand Down
32 changes: 18 additions & 14 deletions vsi2tif/src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def cellsens2raw(
tz: int = 1024,
plane: int = 0,
max_mem: int = 32,
verbose: int = 1,
) -> None:
if not os.path.exists(bfconvert):
raise FileNotFoundError(f"bfconvert not found at: {bfconvert}")
Expand All @@ -21,10 +22,13 @@ def cellsens2raw(
f"{bfconvert} -tilex {tz} -tiley {tz} -nogroup -no-upgrade -overwrite -bigtiff -series {plane} "
f"-compression {compression} {input_path} {output_path}"
)
sp.check_call(cmd, shell=True, env={"BF_MAX_MEM": f"{max_mem}g"})
if verbose == 0:
sp.check_call(cmd, shell=True, env={"BF_MAX_MEM": f"{max_mem}g"}, stdout=sp.DEVNULL, stderr=sp.STDOUT)
else:
sp.check_call(cmd, shell=True, env={"BF_MAX_MEM": f"{max_mem}g"})


def raw2tif(input_path: str, output_path: str, compression: str = "jpeg", quality: int = 85) -> None:
def raw2tif(input_path: str, output_path: str, compression: str = "jpeg", quality: int = 85, verbose: int = 1) -> None:
if not os.path.exists(input_path):
raise FileNotFoundError(f"Input file not found at: {input_path}")
if not os.path.exists(os.path.dirname(output_path)):
Expand All @@ -33,7 +37,10 @@ def raw2tif(input_path: str, output_path: str, compression: str = "jpeg", qualit
cmd = (
f"vips tiffsave {input_path} {output_path} --bigtiff --tile --pyramid --compression={compression} --Q={quality}"
)
sp.check_call(cmd, shell=True)
if verbose == 0:
sp.check_call(cmd, shell=True, stdout=sp.DEVNULL, stderr=sp.STDOUT)
else:
sp.check_call(cmd, shell=True)


def cellsens2tif(
Expand All @@ -45,18 +52,15 @@ def cellsens2tif(
plane: int = 0,
quality: int = 85,
max_mem: int = 32,
verbose: int = 1,
) -> None:
# create temporary directory to store intermediate files
temp_dir = TemporaryDirectory()
with TemporaryDirectory() as temp_dir:
# create temporary path for raw TIFF
bigtiff_path = os.path.join(temp_dir, "temporary.btf")

# create temporary path for raw TIFF
bigtiff_path = os.path.join(temp_dir.name, "temporary.btf")
# first convert from Olympus format to raw TIFF
cellsens2raw(input_path, bigtiff_path, bfconvert, "LZW", tz, plane, max_mem, verbose)

# first convert from Olympus format to raw TIFF
cellsens2raw(input_path, bigtiff_path, bfconvert, "LZW", tz, plane, max_mem)

# construct tiled, pyramidal TIFF
raw2tif(bigtiff_path, output_path, compression, quality)

# clear temporary directory
temp_dir.cleanup()
# construct tiled, pyramidal TIFF
raw2tif(bigtiff_path, output_path, compression, quality, verbose)
6 changes: 4 additions & 2 deletions vsi2tif/src/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def cellsens2tif_single(
plane: int = 0,
quality: int = 85,
max_mem: int = 32,
verbose: int = 1,
) -> None:
cellsens2tif(input_path, output_path, bfconvert, compression, tz, plane, quality, max_mem)
cellsens2tif(input_path, output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose)


@benchmark
Expand All @@ -30,6 +31,7 @@ def cellsens2tif_batch(
plane: int = 0,
quality: int = 85,
max_mem: int = 32,
verbose: int = 1,
) -> None:
# create directory if it does not exist
os.makedirs(output_path, exist_ok=True)
Expand All @@ -42,4 +44,4 @@ def cellsens2tif_batch(
curr_input_path = os.path.join(root, file)
curr_output_path = os.path.join(output_path, output_path.split(root)[0], file).replace(".vsi", ".tif")

cellsens2tif(curr_input_path, curr_output_path, bfconvert, compression, tz, plane, quality, max_mem)
cellsens2tif(curr_input_path, curr_output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose)
32 changes: 29 additions & 3 deletions vsi2tif/vsi2tif.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from argparse import ArgumentParser

Expand All @@ -11,23 +12,48 @@ def main():
parser.add_argument("-o", "--output", help="folder for output files", required=True)
parser.add_argument("-b", "--bfconvert", help="path to bfconvert tool", required=True)
parser.add_argument("-c", "--compression", help="compression technique for final image", default="jpeg")
parser.add_argument("-p", "--plane", help="which image plane to convert image from", default=0)
parser.add_argument("-s", "--tilesize", help="tile size to use during both conversion steps", default=1024)
parser.add_argument("-p", "--plane", help="which image plane to convert image from", default=0)
parser.add_argument("-q", "--quality", help="compression quality used with JPEG compression", default=85)
parser.add_argument("-m", "--max-mem", help="set maximum memory in the java vm", default=32)
parser.add_argument("-v", "--verbose", help="set verbosity level", default=1, type=int)
argv = parser.parse_args()

if argv.verbose not in list(range(6)):
raise ValueError("Verbosity level must be an integer between 0 and 5")

logging.getLogger().setLevel(argv.verbose)

if not os.path.isfile(argv.bfconvert):
raise FileNotFoundError(f"bfconvert not found at: {argv.bfconvert}")
if not os.path.exists(argv.input):
raise FileNotFoundError(f"Input directory not found at: {argv.input}")

if os.path.isdir(argv.input):
logging.info("Performing batch conversion...")
cellsens2tif_batch(
argv.input, argv.output, argv.bfconvert, argv.compression, argv.tilesize, argv.plane, argv.quality
argv.input,
argv.output,
argv.bfconvert,
argv.compression,
argv.tilesize,
argv.plane,
argv.quality,
argv.max_mem,
argv.verbose,
)
else:
logging.info("Performing single conversion...")
cellsens2tif_single(
argv.input, argv.output, argv.bfconvert, argv.compression, argv.tilesize, argv.plane, argv.quality
argv.input,
argv.output,
argv.bfconvert,
argv.compression,
argv.tilesize,
argv.plane,
argv.quality,
argv.max_mem,
argv.verbose,
)


Expand Down

0 comments on commit 8d96995

Please sign in to comment.