From c88aa7932853b76a8ead3d80e1ce69980211b8ad Mon Sep 17 00:00:00 2001
From: s204159 <s204159@student.dtu.dk>
Date: Fri, 28 Jun 2024 09:53:01 +0200
Subject: [PATCH] Zarr cli

---
 qim3d/io/convert.py |  3 ++-
 qim3d/utils/cli.py  | 40 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/qim3d/io/convert.py b/qim3d/io/convert.py
index f1981536..b2ab1e49 100644
--- a/qim3d/io/convert.py
+++ b/qim3d/io/convert.py
@@ -20,6 +20,7 @@ class Convert:
         """
         self.chunk_shape = kwargs.get("chunk_shape", (64, 64, 64))
 
+
     def convert(self, input_path, output_path):
         # Stringify path in case it is not already a string
         input_path = stringify_path(input_path)
@@ -30,7 +31,7 @@ class Convert:
         if os.path.isfile(input_path):  
             match input_ext, output_ext:
                 case (".tif", ".zarr")  | (".tiff", ".zarr"):
-                    return self.convert_tif_to_zarr(input_path, output_path)
+                    return self.convert_tif_to_zarr(input_path, output_path, chunks=self.chunk_shape)
                 case _:
                     raise ValueError("Unsupported file format")
         # Load a directory
diff --git a/qim3d/utils/cli.py b/qim3d/utils/cli.py
index 4b057142..ac8708c4 100644
--- a/qim3d/utils/cli.py
+++ b/qim3d/utils/cli.py
@@ -1,12 +1,12 @@
 import argparse
 import webbrowser
+import outputformat as ouf
 
 from qim3d.gui import annotation_tool, data_explorer, iso3d, local_thickness
 from qim3d.io.loading import DataLoader
 from qim3d.utils import image_preview
 from qim3d import __version__ as version
-import outputformat as ouf
-import qim3d
+import qim3d.io
 
 QIM_TITLE = ouf.rainbow(
     f"\n         _          _____     __ \n  ____ _(_)___ ___ |__  /____/ / \n / __ `/ / __ `__ \ /_ </ __  /  \n/ /_/ / / / / / / /__/ / /_/ /   \n\__, /_/_/ /_/ /_/____/\__,_/    \n  /_/                 v{version}\n\n",
@@ -14,6 +14,9 @@ QIM_TITLE = ouf.rainbow(
     cmap="hot",
 )
 
+def parse_tuple(arg):
+    # Remove parentheses if they are included and split by comma
+    return tuple(map(int, arg.strip('()').split(',')))
 
 def main():
     parser = argparse.ArgumentParser(description="Qim3d command-line interface.")
@@ -87,6 +90,30 @@ def main():
         help="By default set the maximum value to be 255 so the contrast is strong. This turns it off.",
     )
 
+    # File Convert
+    convert_parser = subparsers.add_parser(
+        "convert",
+        help="Convert files to different formats without loading the entire file into memory",
+    )
+    convert_parser.add_argument(
+        "input_path",
+        type=str,
+        metavar="Input path",
+        help="Path to image that will be converted",
+    )
+    convert_parser.add_argument(
+        "output_path",
+        type=str,
+        metavar="Output path",
+        help="Path to save converted image",
+    )
+    convert_parser.add_argument(
+        "--chunks",
+        type=parse_tuple,
+        metavar="Chunk shape",
+        default=(64,64,64),
+        help="Chunk size for the zarr file. Defaults to (64, 64, 64).",
+    )
     args = parser.parse_args()
 
     if args.subcommand == "gui":
@@ -104,13 +131,13 @@ def main():
             else:
                 interface = iso3d.Interface()
                 interface.launch(inbrowser=inbrowser, force_light_mode=False)
-        
+
         elif args.annotation_tool:
             if args.platform:
                 annotation_tool.run_interface(arghost)
             else:
                 interface = annotation_tool.Interface()
-                interface.launch(inbrowser=inbrowser, force_light_mode=False)        
+                interface.launch(inbrowser=inbrowser, force_light_mode=False)
         elif args.local_thickness:
             if args.platform:
                 local_thickness.run_interface(arghost)
@@ -118,7 +145,6 @@ def main():
                 interface = local_thickness.Interface()
                 interface.launch(inbrowser=inbrowser, force_light_mode=False)
 
-
     elif args.subcommand == "viz":
         if not args.source:
             print("Please specify a source file using the argument --source")
@@ -139,6 +165,7 @@ def main():
 
     elif args.subcommand == "preview":
         image = DataLoader().load(args.filename)
+
         image_preview(
             image,
             image_width=args.resolution,
@@ -147,6 +174,9 @@ def main():
             relative_intensity=args.absolute_values,
         )
 
+    elif args.subcommand == "convert":
+        qim3d.io.convert(args.input_path, args.output_path, chunk_shape=args.chunks)
+
     elif args.subcommand is None:
         print(QIM_TITLE)
         welcome_text = (
-- 
GitLab