From d6bbf7b7d661931883b7343698159d30a7893e3b Mon Sep 17 00:00:00 2001 From: Tristan Kuehn Date: Wed, 11 Jan 2023 11:45:34 -0500 Subject: [PATCH 1/3] Add setting to allow labels to be dropped from input maps --- labelmerge/config/snakebids.yml | 8 +++- .../workflow/rules/label_harmonization.smk | 9 ++++- labelmerge/workflow/scripts/labelmerge.py | 39 +++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/labelmerge/config/snakebids.yml b/labelmerge/config/snakebids.yml index 3a4a74b..f8c230d 100644 --- a/labelmerge/config/snakebids.yml +++ b/labelmerge/config/snakebids.yml @@ -80,7 +80,13 @@ parse_args: help: Space separated integer labels from the base labelmap to keep over overlay labels at the same voxels. nargs: '*' --overlay_exceptions: - help: Space separated integer labels from the overlay image to discard. + help: Space separated integer labels from the overlay image to be overwritten by base labels at the same voxels. + nargs: '*' + --base_drops: + help: Space separated integer labels from the base image to drop from the output. + nargs: '*' + --overlay_drops: + help: Space separated integer labels from the overlay image to drop from the output. nargs: '*' # Workflow specific config diff --git a/labelmerge/workflow/rules/label_harmonization.smk b/labelmerge/workflow/rules/label_harmonization.smk index 5564f6f..fa7e2ff 100644 --- a/labelmerge/workflow/rules/label_harmonization.smk +++ b/labelmerge/workflow/rules/label_harmonization.smk @@ -83,10 +83,17 @@ rule merge_labels: overlay_exceptions=f"--overlay_exceptions {' '.join(config['overlay_exceptions'])}" if config.get("overlay_exceptions") else "", + base_drops=f"--base_drops {' '.join(config['base_drops'])}" + if config.get("base_drops") + else "", + overlay_drops=f"--overlay_drops {' '.join(config['overlay_drops'])}" + if config.get("overlay_drops") + else "", resources: script=str(Path(workflow.basedir) / "scripts" / "labelmerge.py"), shell: "python3 {resources.script} {input.base_map} {input.base_metadata} " "{input.overlay_map} {input.overlay_metadata} " "{output.merged_map} {output.merged_metadata} " - "{params.base_exceptions} {params.overlay_exceptions}" + "{params.base_exceptions} {params.overlay_exceptions} " + "{params.base_drops} {params.overlay_drops}" diff --git a/labelmerge/workflow/scripts/labelmerge.py b/labelmerge/workflow/scripts/labelmerge.py index 1e8d150..c146344 100644 --- a/labelmerge/workflow/scripts/labelmerge.py +++ b/labelmerge/workflow/scripts/labelmerge.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +"""Script to merge two labelmaps with their BIDS metadata.""" from __future__ import annotations from argparse import ArgumentParser @@ -49,15 +50,20 @@ def split_labels( atlas: np.ndarray, metadata: pd.DataFrame, prefix: str = "", - exceptions: list[str] = [], + exceptions: list[int] | None = None, + drops: list[int] | None = None, ) -> list[xr.Dataset]: + if exceptions is None: + exceptions = [] + if drops is None: + drops = [] unique_vals = np.unique(atlas[atlas > 0]) normal_ds = xr.Dataset( dict( [ assemble_mask(atlas, metadata, label, prefix) for label in unique_vals - if label not in exceptions + if label not in exceptions + drops ] ) ) @@ -133,7 +139,25 @@ def gen_parser() -> ArgumentParser: nargs="*", help=( "Space separated list of integer labels from the overlay image to " - "discard." + "be overwritten by labels from the base image." + ), + type=int, + ) + parser.add_argument( + "--base_drops", + nargs="*", + help=( + "Space separated list of integer labels from the base image to " + "drop from the output map." + ), + type=int, + ) + parser.add_argument( + "--overlay_drops", + nargs="*", + help=( + "Space separated list of integer labels from the overlay image to " + "drop from the output map." ), type=int, ) @@ -151,21 +175,28 @@ def main(): overlay_exceptions = ( args.overlay_exceptions if args.overlay_exceptions else [] ) + base_drops = args.base_drops if args.base_drops else [] + overlay_drops = args.overlay_drops if args.overlay_drops else [] base_datasets = split_labels( base_data, base_metadata, prefix="base ", exceptions=base_exceptions, + drops=base_drops, ) overlay_datasets = split_labels( overlay_data, overlay_metadata, prefix="overlay ", exceptions=overlay_exceptions, + drops=overlay_drops, ) # Note that overlay exceptions are ignored merged_map, merged_metadata = merge_labels( - base_datasets[0:1] + overlay_datasets[0:1] + base_datasets[1:] + overlay_datasets[1:] + + base_datasets[0:1] + + overlay_datasets[0:1] + + base_datasets[1:] ) merged_img = nib.Nifti1Image( dataobj=merged_map, affine=base_affine, header=base_header From 3a954de67729216beffb5be1360d529499e8ae4f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:34:08 +0000 Subject: [PATCH 2/3] Bump version to 0.3.0-pre.1 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 1e36fa4..fcf2aca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "labelmerge" -version = "0.2.1" +version = "0.3.0-pre.1" description = "Snakebids app for merging multiple label maps." authors = [ "Jason Kai ", From f71f737cbac26fc80003b8d6f9cd5ad5345ed404 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:38:40 +0000 Subject: [PATCH 3/3] Bump version to 0.3.0 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fcf2aca..21c657c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "labelmerge" -version = "0.3.0-pre.1" +version = "0.3.0" description = "Snakebids app for merging multiple label maps." authors = [ "Jason Kai ",