Skip to content

Commit

Permalink
Resolve Type Conversion and Improve Documentation
Browse files Browse the repository at this point in the history
Resolve a type conversion bug prevent individual tiff files from being written correctly. Improve script usage documentation and log output directory location.
  • Loading branch information
JackEAllen committed May 8, 2024
1 parent aa73fc2 commit 10b0caf
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions scripts/tiff_unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# SPDX - License - Identifier: GPL-3.0-or-later
"""
This is a Utility script to unpack tiff files into a directory of images using lossless compression.
This script is temporary while larger work is undertaken to improve how datasets are stored and accessed within Mantid.
Paths used as arguments are relative to the location of the script.
If a destination path does not exist, the script will create it.
This script is temporary while larger work is undertaken to improve how datasets are loaded,
stored and accessed within Mantid.
Usage:
python tiff_unpacker.py -s <path to tiff file> -d <path to output directory>
Expand All @@ -11,6 +14,9 @@
-d <path to output directory>
-r <start> <stop>
-n <naming convention>
Example usage with all arguments to unpack images 10 to 20 with naming convention "tbin_<index>":
python tiff_unpacker.py -s /path/to/tiff/file.tiff -d /path/to/output/directory -r 10 20 -n tbin_
"""
import argparse
from tqdm import tqdm
Expand All @@ -36,12 +42,25 @@ def unpack_tiff(source: Path, destination: Path, img_range: tuple[int, int], nam
with TiffFile(source) as tif:
for i, img in enumerate(tqdm(tif.pages[img_range[0]:img_range[1]], desc="Unpacking tiff file")):
imwrite(destination / f"{naming_convention}{i}.tiff",
img.asarray(),
img.asarray().astype('float32'),
compression="zlib",
predictor=True,
metadata={
'axes': img.axes,
})
validate_written_file_count(destination, img_range)


def validate_written_file_count(destination: Path, img_range: tuple[int, int]) -> None:
"""
Return a printed statement of the number of files written to the output directory and the expected number of files
"""
expected_files = img_range[1] - img_range[0]
written_files = len(list(destination.glob("*.tiff")))
if expected_files != written_files:
logger.warning("Expected to write %s files but wrote %s files", expected_files, written_files)
else:
logger.info("Successfully wrote %s files to %s", expected_files, destination)


def validate_input_range(data_length: int, start: int, stop: int) -> tuple[int, int]:
Expand Down

0 comments on commit 10b0caf

Please sign in to comment.