From 714c892fccb705e1a7d29ca0e4fcd96b2aac82d0 Mon Sep 17 00:00:00 2001 From: Delattre Emilie Date: Tue, 26 Sep 2023 11:13:36 +0200 Subject: [PATCH 1/2] Start grayscale by default --- pipeline/download_gene.py | 21 ++++++++++++++++++++- pipeline/full_pipeline.py | 18 +++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pipeline/download_gene.py b/pipeline/download_gene.py index 2508436..3d6c01a 100644 --- a/pipeline/download_gene.py +++ b/pipeline/download_gene.py @@ -20,6 +20,7 @@ from pathlib import Path from typing import Any, Generator, Optional, Tuple +import cv2 import numpy as np import PIL from atldld.base import DisplacementField @@ -67,6 +68,14 @@ def parse_args(): If True, download and apply deformation to threshold images too. """, ) + parser.add_argument( + "--rgb", + action="store_true", + help="""\ + If True, images will be saved under RGB format. + Otherwise, images will be saved under grayscale format. + """, + ) args = parser.parse_args() return args @@ -79,6 +88,7 @@ def postprocess_dataset( None, ], n_images: int, + rgb: bool = False, ) -> Tuple[np.ndarray, np.ndarray, dict[str, Any]]: """Post process given dataset. @@ -90,6 +100,9 @@ def postprocess_dataset( n_images The overall number of slices we are going to download. Needs to be passed separately since the `dataset` is a generator. + rgb + If True, images will be saved under RGB format. + Otherwise, images will be saved under grayscale format. Returns ------- @@ -119,6 +132,8 @@ def postprocess_dataset( section_numbers.append(section_coordinate // 25) image_ids.append(img_id) warped_img = 255 - df.warp(img, border_mode="constant", c=img[0, 0, :].tolist()) + if not rgb: + warped_img = cv2.cvtColor(warped_img, cv2.RGB2GRAY) dataset_np.append(warped_img) if img_expression is not None: @@ -142,6 +157,7 @@ def main( output_dir: Path | str, downsample_img: int, expression: bool = True, + rgb: bool = False, ) -> int: """Download gene expression dataset. @@ -156,6 +172,9 @@ def main( This factor is going to reduce the size. expression If True, threshold images are downloaded too. + rgb + If True, images will be saved under RGB format. + Otherwise, images will be saved under grayscale format. """ # Imports import json @@ -180,7 +199,7 @@ def main( dataset_gen = dataset.run() axis = CommonQueries.get_axis(experiment_id) dataset_np, expression_np, metadata_dict = postprocess_dataset( - dataset_gen, len(dataset) + dataset_gen, len(dataset), rgb ) metadata_dict["axis"] = axis diff --git a/pipeline/full_pipeline.py b/pipeline/full_pipeline.py index bffae7a..07865ad 100644 --- a/pipeline/full_pipeline.py +++ b/pipeline/full_pipeline.py @@ -132,6 +132,14 @@ def parse_args(): If True, force to recompute every steps. """, ) + parser.add_argument( + "--rgb", + action="store_true", + help="""\ + If True, images will be saved under RGB format. + Otherwise, images will be saved under grayscale format. + """, + ) return parser.parse_args() @@ -148,6 +156,7 @@ def main( saving_format: str, expression: bool = False, force: bool = False, + rgb: bool = False, ) -> int: """Implement the main function.""" from download_gene import main as download_gene_main @@ -180,7 +189,9 @@ def main( nissl_path = warped_nissl_path - gene_experiment_dir = output_dir / "download-gene" + color = "rgb" if rgb else "grayscale" + + gene_experiment_dir = output_dir / "download-gene" / color gene_experiment_path = gene_experiment_dir / f"{experiment_id}.npy" if not gene_experiment_path.exists() or force: logger.info("Downloading Gene Expression...") @@ -189,6 +200,7 @@ def main( output_dir=gene_experiment_dir, downsample_img=downsample_img, expression=expression, + rgb=rgb, ) else: logger.info( @@ -196,7 +208,7 @@ def main( f"{experiment_id} is already downloaded and saved under {gene_experiment_path}" ) - aligned_results_dir = output_dir / "gene-to-nissl" / coordinate_sys + aligned_results_dir = output_dir / "gene-to-nissl" / coordinate_sys / color aligned_gene_path = aligned_results_dir / f"{experiment_id}-warped-gene.npy" if not aligned_gene_path.exists() or force: @@ -215,7 +227,7 @@ def main( else: logger.info("Aligning downloaded Gene Expression to Nissl volume: Skipped") - interpolation_results_dir = output_dir / "interpolate-gene" / coordinate_sys + interpolation_results_dir = output_dir / "interpolate-gene" / coordinate_sys / color interpolated_gene_path = ( interpolation_results_dir / f"{experiment_id}-{interpolator_name}-interpolated-gene.npy" From 129f35321f5aa44e93dd62a2d83b26f0806772db Mon Sep 17 00:00:00 2001 From: Delattre Emilie Date: Wed, 27 Sep 2023 14:51:12 +0200 Subject: [PATCH 2/2] Change cv2 for skimage --- pipeline/download_gene.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipeline/download_gene.py b/pipeline/download_gene.py index 3d6c01a..2092524 100644 --- a/pipeline/download_gene.py +++ b/pipeline/download_gene.py @@ -20,10 +20,10 @@ from pathlib import Path from typing import Any, Generator, Optional, Tuple -import cv2 import numpy as np import PIL from atldld.base import DisplacementField +from skimage.color import rgb2gray from tqdm import tqdm logger = logging.getLogger("download-gene") @@ -133,7 +133,7 @@ def postprocess_dataset( image_ids.append(img_id) warped_img = 255 - df.warp(img, border_mode="constant", c=img[0, 0, :].tolist()) if not rgb: - warped_img = cv2.cvtColor(warped_img, cv2.RGB2GRAY) + warped_img = rgb2gray(warped_img) dataset_np.append(warped_img) if img_expression is not None: