Skip to content

Commit

Permalink
allow for additional channels with same name (#185)
Browse files Browse the repository at this point in the history
* allow for additional channels with same name

must have different parent folder name
otherwise error is thrown
tests for this added

* clearer variable names

* clarify docstring

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* limit line lengths

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
alessandrofelder and pre-commit-ci[bot] authored Mar 15, 2024
1 parent 65388d0 commit 31e2111
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
66 changes: 60 additions & 6 deletions brainreg/core/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,70 @@ def preprocessing_parser(parser):


def prep_registration(args):
"""
Prepares the file system for registration.
Ensures the brainreg directory exists and
stores paths for additional channels
that should be registered (if any were passed)
into a dictionary.
Dictionary keys for additional channel paths are
- the file/folder name passed (if unique for all additional channels)
- the file/folder name prefixed with the parent folder name (otherwise)
Throws an error if multiple additional channels have
the same name and the same parent folder, because the dictionary
key is not unique in this situation.
Parameters
----------
args : argparse.Namespace
ArgParse object. Expected attributes here are "brainreg_directory"
and (optionally) "additional_images".
Returns
-------
tuple
A tuple containing the `args` object
and the `additional_images_to_downsample` dictionary.
"""
logging.debug("Making registration directory")
ensure_directory_exists(args.brainreg_directory)

additional_images_downsample = {}
if args.additional_images:
for idx, images in enumerate(args.additional_images):
name = Path(images).name
additional_images_downsample[name] = images
additional_images_to_downsample = {}

return args, additional_images_downsample
if args.additional_images:
path_names = [
Path(path).name for path in args.additional_images
] # could be files or folders containing e.g. lots of 2D tiffs
has_duplicate_names = any(
path_names.count(name) > 1 for name in path_names
)
for additional_channel_paths in args.additional_images:
channel_name = Path(additional_channel_paths).name
if not has_duplicate_names:
additional_images_to_downsample[channel_name] = (
additional_channel_paths
)
else:
channel_name = Path(additional_channel_paths).name
parent_folder = Path(additional_channel_paths).parent.name
channel_name = f"{parent_folder}_{channel_name}"
additional_images_to_downsample[channel_name] = (
additional_channel_paths
)

assert len(args.additional_images) == len(
additional_images_to_downsample
), (
"Something went wrong parsing additional channel names - "
+ "please ensure additional channels have a unique "
+ "combination of name and parent folder."
)

return args, additional_images_to_downsample


def main():
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dev = [
"pre-commit",
"pytest-cov",
"pytest-qt",
"pytest-mock",
"pytest",
"setuptools_scm",
"tox",
Expand Down
55 changes: 55 additions & 0 deletions tests/tests/test_unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pathlib import Path

import pytest

from brainreg.core.cli import prep_registration


def test_prep_registration_different_names(mocker):
"""Check that additional channel names are returned, when unique."""
args = mocker.Mock()
args.brainreg_directory = Path.home() # This just needs to exist
args.additional_images = [
Path.home() / "additional_channel_0",
Path.home() / "additional_channel_1",
]

_, additional_channel_outputs = prep_registration(args)
assert "additional_channel_0" in additional_channel_outputs
assert "additional_channel_1" in additional_channel_outputs


def test_additional_channels_same_name_different_parent_name(mocker):
"""
Check that parent folder name is part of keys returned
if additional channel names are not unique.
"""
args = mocker.Mock()
args.brainreg_directory = Path.home() # This just needs to exist
args.additional_images = [
Path.home() / "folder_0/duplicate_name",
Path.home() / "folder_1/duplicate_name",
]

_, additional_channel_outputs = prep_registration(args)
assert "folder_1_duplicate_name" in additional_channel_outputs.keys()
assert "folder_1_duplicate_name" in additional_channel_outputs.keys()


def test_prep_registration_same_name_same_parent_name(mocker):
"""Check that error is thrown if both
parent and additional channel name are non-unique.
"""
args = mocker.Mock()
args.brainreg_directory = Path.home() # This just needs to exist
args.additional_images = [
str(Path.home() / "duplicate_name"),
str(Path.home() / "duplicate_name"),
]

with pytest.raises(
AssertionError,
match=".*ensure additional channels have a unique "
+ "combination of name and parent folder.*",
):
prep_registration(args)

0 comments on commit 31e2111

Please sign in to comment.