Skip to content

Dimension incompatibility of NRRD files with ITK-snap #77

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pipeline/download_gene.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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")
Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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
-------
Expand Down Expand Up @@ -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 = rgb2gray(warped_img)
dataset_np.append(warped_img)

if img_expression is not None:
Expand All @@ -142,6 +157,7 @@ def main(
output_dir: Path | str,
downsample_img: int,
expression: bool = True,
rgb: bool = False,
) -> int:
"""Download gene expression dataset.

Expand All @@ -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
Expand All @@ -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

Expand Down
18 changes: 15 additions & 3 deletions pipeline/full_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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
Expand Down Expand Up @@ -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...")
Expand All @@ -189,14 +200,15 @@ def main(
output_dir=gene_experiment_dir,
downsample_img=downsample_img,
expression=expression,
rgb=rgb,
)
else:
logger.info(
"Downloading Gene Expression: Skipped \n"
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:
Expand All @@ -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"
Expand Down