Skip to content
Open
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
22 changes: 17 additions & 5 deletions brainglobe_template_builder/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
)
from brainglobe_template_builder.utils.cropping import crop_to_mask
from brainglobe_template_builder.utils.masking import create_mask
from brainglobe_template_builder.utils.preproc_config import PreprocConfig
from brainglobe_template_builder.utils.preproc_config import (
MaskConfig,
PreprocConfig,
)
from brainglobe_template_builder.validate import validate_input_csv

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -158,7 +161,9 @@ def _process_subject(
}


def preprocess(standardised_csv: Path, config: Path | PreprocConfig) -> None:
def preprocess(
standardised_csv: Path, config: Path | PreprocConfig | None = None
) -> None:
"""Process nifti files in ASR orientation to create output images +
masks ready for template creation.

Expand Down Expand Up @@ -189,12 +194,19 @@ def preprocess(standardised_csv: Path, config: Path | PreprocConfig) -> None:
standardised_csv : Path
Standardised csv file path. One row per sample, each with a
unique 'subject_id' - this is created via `standardise`.
config : Path | PreprocConfig
config : Path | PreprocConfig | None
Config yaml file path, or PreprocConfig object. Contains settings for
pre-processing steps.
pre-processing steps. Defaults to None, which will use defaults.
"""

if isinstance(config, Path):
if not config:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes config is truthy. its fine but it would be safer to say if config is None

# use default mask and padding, and default to outputting
# into sibling folder of `standardised/`
preproc_config = PreprocConfig(

@PolarBean PolarBean May 15, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just define MaskConfig() as the default argument for PreprocConfig simplify.

output_dir=standardised_csv.parent.parent,
mask=MaskConfig(),
)
elif isinstance(config, Path):
with open(config) as f:
config_yaml = yaml.safe_load(f)
preproc_config = PreprocConfig.model_validate(config_yaml)
Expand Down
2 changes: 2 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
# during sphinx-build linkcheck
linkcheck_ignore = [
"https://opensource.org/license/*", # to avoid odd 403 error
"https://zenodo.org/badge/*", # avoid 403 error
"https://doi.org/*", # avoid 403 error
]

# -- Options for HTML output -------------------------------------------------
Expand Down
14 changes: 9 additions & 5 deletions tests/test_unit/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,27 @@ def test_preprocess_use_input(

@pytest.mark.parametrize(
"config_type",
["config_file", "PreprocConfig object"],
["config_file", "PreprocConfig object", "None"],
)
@pytest.mark.usefixtures("mock_fancylog_datetime")
def test_preprocess(
write_standardised_test_data: tuple[Path, Path], config_type: str
) -> None:
"""Test that preprocess creates expected directories and files - both
with a config yaml file path as input OR a PreprocConfig object."""
"""Test that preprocess creates expected directories and files - with
a config yaml file path as input, a PreprocConfig object OR None."""
csv_path, config_path = write_standardised_test_data

config: Path | PreprocConfig
config: Path | PreprocConfig | None
if config_type == "config_file":
config = config_path
else:
elif config_type == "PreprocConfig object":
with open(config_path) as f:
config_yaml = yaml.safe_load(f)
config = PreprocConfig.model_validate(config_yaml)
elif config_type == "None":
config = None
else:
raise ValueError("Testing with invalid config")

preprocess(csv_path, config)

Expand Down
Loading