From dc257271fbeb882dd09baae6b8285c48ffc2f323 Mon Sep 17 00:00:00 2001 From: Emilie Delattre Date: Thu, 28 Jul 2022 14:31:28 +0200 Subject: [PATCH 1/5] Add format option on the entire pipeline --- pipeline/convert_npy_nrrd.py | 30 ++++++++++++++++++++++++++++-- pipeline/full_pipeline.py | 10 ++++++++++ pipeline/interpolate_gene.py | 26 +++++++++++++++++++++----- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/pipeline/convert_npy_nrrd.py b/pipeline/convert_npy_nrrd.py index 4ed418c..4051ab6 100644 --- a/pipeline/convert_npy_nrrd.py +++ b/pipeline/convert_npy_nrrd.py @@ -14,11 +14,25 @@ # limitations under the License. import argparse +from collections import OrderedDict from pathlib import Path import nrrd import numpy as np +# Match voxcell requirements: +HEADER = OrderedDict([('type', 'uint32'), + ('dimension', 3), + ('space dimension', 3), + # ('sizes', np.array([528, 320, 456])), + ('space directions', + np.array([[25., 0., 0.], + [ 0., 25., 0.], + [ 0., 0., 25.]])), + ('endian', 'little'), + ('encoding', 'gzip'), + ('space origin', np.array([0., 0., 0.]))]) + def parse_args(): """Parse command line arguments. @@ -44,13 +58,25 @@ def parse_args(): Path to the output volume. """, ) + parser.add_argument( + "-h", + "--header", + type=Path, + help=f"""\ + If specified, the header {HEADER} is saved. + """, + ) return parser.parse_args() -def main(input_path: Path, output_path: Path) -> int: +def main(input_path: Path, output_path: Path, header: bool) -> int: array = np.load(input_path) - nrrd.write(str(output_path), array) + if header: + HEADER["sizes"] = np.array(array.shape) + nrrd.write(str(output_path), array, header=HEADER) + else: + nrrd.write(str(output_path), array) return 0 diff --git a/pipeline/full_pipeline.py b/pipeline/full_pipeline.py index 17365ca..8ce9e70 100644 --- a/pipeline/full_pipeline.py +++ b/pipeline/full_pipeline.py @@ -106,6 +106,15 @@ def parse_args(): Path of the interpolator checkpoints. """, ) + parser.add_argument( + "--format", + type=str, + choices=("nrrd", "npy"), + default="npy", + help="""\ + Format to save the output volumes. + """, + ) parser.add_argument( "-e", "--expression", @@ -135,6 +144,7 @@ def main( interpolator_name: str, interpolator_checkpoint: Path | str | None, output_dir: Path | str, + format: str, expression: bool = False, force: bool = False, ) -> int: diff --git a/pipeline/interpolate_gene.py b/pipeline/interpolate_gene.py index 82a0ee9..c15f6c9 100644 --- a/pipeline/interpolate_gene.py +++ b/pipeline/interpolate_gene.py @@ -63,6 +63,15 @@ def parse_args(): Path of the interpolator checkpoints. """, ) + parser.add_argument( + "--format", + type=str, + choices=("nrrd", "npy"), + default="npy", + help="""\ + Format to save the output volumes. + """, + ) parser.add_argument( "--reference-path", type=Path, @@ -137,10 +146,12 @@ def main( metadata_path: Path | str, interpolator_name: str, interpolator_checkpoint: str | Path | None, + format: str, reference_path: str | Path, output_dir: Path | str | None = None, ) -> int: """Implement main function.""" + import nrrd import numpy as np from atlinter.data import GeneDataset from utils import check_and_load @@ -194,12 +205,17 @@ def main( output_dir = Path(output_dir) output_dir.mkdir(parents=True, exist_ok=True) + output_path = str(output_dir / f"{experiment_id}-{interpolator_name}-interpolated-{image_type}") - np.save( - output_dir - / f"{experiment_id}-{interpolator_name}-interpolated-{image_type}.npy", - predicted_volume, - ) + if format == "npy": + np.save( + output_path + ".npy", + predicted_volume, + ) + else: + from convert_npy_nrrd import HEADER + HEADER["sizes"] = np.array(predicted_volume.shape) + nrrd.write(output_path + ".nrrd", predicted_volume, header=HEADER) return 0 From e60529272bba72f4a0ff596084f5a2943c457b66 Mon Sep 17 00:00:00 2001 From: Emilie Delattre Date: Thu, 28 Jul 2022 14:38:05 +0200 Subject: [PATCH 2/5] Change variable name + specify it in the full pipeline --- pipeline/full_pipeline.py | 6 ++++-- pipeline/interpolate_gene.py | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pipeline/full_pipeline.py b/pipeline/full_pipeline.py index 8ce9e70..bffae7a 100644 --- a/pipeline/full_pipeline.py +++ b/pipeline/full_pipeline.py @@ -107,7 +107,8 @@ def parse_args(): """, ) parser.add_argument( - "--format", + "-s", + "--saving-format", type=str, choices=("nrrd", "npy"), default="npy", @@ -144,7 +145,7 @@ def main( interpolator_name: str, interpolator_checkpoint: Path | str | None, output_dir: Path | str, - format: str, + saving_format: str, expression: bool = False, force: bool = False, ) -> int: @@ -236,6 +237,7 @@ def main( metadata_path=aligned_results_dir / f"{experiment_id}-metadata.json", interpolator_name=interpolator_name, interpolator_checkpoint=interpolator_checkpoint, + saving_format=saving_format, reference_path=nissl_path, output_dir=interpolation_results_dir, ) diff --git a/pipeline/interpolate_gene.py b/pipeline/interpolate_gene.py index c15f6c9..5cd8f39 100644 --- a/pipeline/interpolate_gene.py +++ b/pipeline/interpolate_gene.py @@ -64,7 +64,7 @@ def parse_args(): """, ) parser.add_argument( - "--format", + "--saving_format", type=str, choices=("nrrd", "npy"), default="npy", @@ -146,7 +146,7 @@ def main( metadata_path: Path | str, interpolator_name: str, interpolator_checkpoint: str | Path | None, - format: str, + saving_format: str, reference_path: str | Path, output_dir: Path | str | None = None, ) -> int: @@ -207,7 +207,7 @@ def main( output_dir.mkdir(parents=True, exist_ok=True) output_path = str(output_dir / f"{experiment_id}-{interpolator_name}-interpolated-{image_type}") - if format == "npy": + if saving_format == "npy": np.save( output_path + ".npy", predicted_volume, From f3a08d1b2ba81f1814882db70d26520259a11ce0 Mon Sep 17 00:00:00 2001 From: Emilie Delattre Date: Thu, 28 Jul 2022 14:43:58 +0200 Subject: [PATCH 3/5] Black on the header --- pipeline/convert_npy_nrrd.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pipeline/convert_npy_nrrd.py b/pipeline/convert_npy_nrrd.py index 4051ab6..04bf6d5 100644 --- a/pipeline/convert_npy_nrrd.py +++ b/pipeline/convert_npy_nrrd.py @@ -21,17 +21,21 @@ import numpy as np # Match voxcell requirements: -HEADER = OrderedDict([('type', 'uint32'), - ('dimension', 3), - ('space dimension', 3), - # ('sizes', np.array([528, 320, 456])), - ('space directions', - np.array([[25., 0., 0.], - [ 0., 25., 0.], - [ 0., 0., 25.]])), - ('endian', 'little'), - ('encoding', 'gzip'), - ('space origin', np.array([0., 0., 0.]))]) +HEADER = OrderedDict( + [ + ("type", "uint32"), + ("dimension", 3), + ("space dimension", 3), + ("sizes", None), + ( + "space directions", + np.array([[25.0, 0.0, 0.0], [0.0, 25.0, 0.0], [0.0, 0.0, 25.0]]), + ), + ("endian", "little"), + ("encoding", "gzip"), + ("space origin", np.array([0.0, 0.0, 0.0])), + ] +) def parse_args(): From d53c77d999e553cb479e5a8e14f1dd7786ba4954 Mon Sep 17 00:00:00 2001 From: Emilie Delattre Date: Thu, 28 Jul 2022 15:46:42 +0200 Subject: [PATCH 4/5] Address Jan's comments --- pipeline/convert_npy_nrrd.py | 3 ++- pipeline/interpolate_gene.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pipeline/convert_npy_nrrd.py b/pipeline/convert_npy_nrrd.py index 04bf6d5..36b3ac9 100644 --- a/pipeline/convert_npy_nrrd.py +++ b/pipeline/convert_npy_nrrd.py @@ -25,7 +25,7 @@ [ ("type", "uint32"), ("dimension", 3), - ("space dimension", 3), + ("space dimension", None), ("sizes", None), ( "space directions", @@ -77,6 +77,7 @@ def main(input_path: Path, output_path: Path, header: bool) -> int: array = np.load(input_path) if header: + HEADER["space dimension"] = len(array.shape) HEADER["sizes"] = np.array(array.shape) nrrd.write(str(output_path), array, header=HEADER) else: diff --git a/pipeline/interpolate_gene.py b/pipeline/interpolate_gene.py index 5cd8f39..7efbdc8 100644 --- a/pipeline/interpolate_gene.py +++ b/pipeline/interpolate_gene.py @@ -64,7 +64,7 @@ def parse_args(): """, ) parser.add_argument( - "--saving_format", + "--saving-format", type=str, choices=("nrrd", "npy"), default="npy", @@ -214,6 +214,7 @@ def main( ) else: from convert_npy_nrrd import HEADER + HEADER["space dimension"] = len(predicted_volume.shape) HEADER["sizes"] = np.array(predicted_volume.shape) nrrd.write(output_path + ".nrrd", predicted_volume, header=HEADER) From 1f905bd6ecf1ad4ccbb69dcdef59d1c6f246e378 Mon Sep 17 00:00:00 2001 From: Emilie Delattre Date: Thu, 28 Jul 2022 16:52:28 +0200 Subject: [PATCH 5/5] Need to change dimension instead of space dimension --- pipeline/convert_npy_nrrd.py | 6 +++--- pipeline/interpolate_gene.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pipeline/convert_npy_nrrd.py b/pipeline/convert_npy_nrrd.py index 36b3ac9..c7f57f7 100644 --- a/pipeline/convert_npy_nrrd.py +++ b/pipeline/convert_npy_nrrd.py @@ -24,8 +24,8 @@ HEADER = OrderedDict( [ ("type", "uint32"), - ("dimension", 3), - ("space dimension", None), + ("dimension", None), + ("space dimension", 3), ("sizes", None), ( "space directions", @@ -77,7 +77,7 @@ def main(input_path: Path, output_path: Path, header: bool) -> int: array = np.load(input_path) if header: - HEADER["space dimension"] = len(array.shape) + HEADER["dimension"] = len(array.shape) HEADER["sizes"] = np.array(array.shape) nrrd.write(str(output_path), array, header=HEADER) else: diff --git a/pipeline/interpolate_gene.py b/pipeline/interpolate_gene.py index 7efbdc8..97f16fb 100644 --- a/pipeline/interpolate_gene.py +++ b/pipeline/interpolate_gene.py @@ -214,7 +214,7 @@ def main( ) else: from convert_npy_nrrd import HEADER - HEADER["space dimension"] = len(predicted_volume.shape) + HEADER["dimension"] = len(predicted_volume.shape) HEADER["sizes"] = np.array(predicted_volume.shape) nrrd.write(output_path + ".nrrd", predicted_volume, header=HEADER)