Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added script for sourcing SWC rat images #46

Draft
wants to merge 5 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
45 changes: 44 additions & 1 deletion brainglobe_template_builder/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
from pathlib import Path
from typing import Literal

import nibabel as nib
import numpy as np
Expand All @@ -7,7 +9,11 @@
from brainglobe_utils.IO.image.save import to_tiff


def get_unique_folder_in_dir(search_dir: Path, search_str: str) -> Path:
def get_unique_folder_in_dir(
search_dir: Path,
search_str: str,
str_position: Literal["start", "end"] | None = None,
) -> Path:
"""
Find a folder in a directory that contains a unique string.

Expand All @@ -17,6 +23,10 @@ def get_unique_folder_in_dir(search_dir: Path, search_str: str) -> Path:
Directory to search in
search_str : str
String to search for in folder names
str_position: Literal["start", "end"] | None
If None (default), ``search_str`` can be anywhere in the folder name.
If "start", ``search_str`` must be at the start of the folder name.
If "end", ``search_str`` must be at the end of the folder name.

Returns
-------
Expand All @@ -25,6 +35,15 @@ def get_unique_folder_in_dir(search_dir: Path, search_str: str) -> Path:
"""
all_folders = [x for x in search_dir.iterdir() if x.is_dir()]
folders_with_str = [x for x in all_folders if search_str in x.name]
if str_position == "start":
folders_with_str = [
x for x in folders_with_str if x.name.startswith(search_str)
]
elif str_position == "end":
folders_with_str = [
x for x in folders_with_str if x.name.endswith(search_str)
]

if len(folders_with_str) == 0:
raise ValueError(f"No folders with {search_str} found")
if len(folders_with_str) > 1:
Expand Down Expand Up @@ -196,3 +215,27 @@ def nifti_to_tiff(nifti_path: Path, tiff_path: Path):
"""
stack = load_any(nifti_path.as_posix())
to_tiff(stack, tiff_path.as_posix())


def get_path_from_env_variable(env_var: str, default_path: str) -> Path:
"""
Get a path from an environment variable, with a default fall-back path.

This could be useful for debugging a script locally - i.e. by setting
and environment variable on your machine, you can override the default
path used in the script (which might point to a shared cluster directory).

Parameters
----------
env_var : str
The name of the environment variable to read, e.g. "ATLAS_FORGE_DIR"
default_path : str
The default path to use if the environment variable is not set,
e.g. "/ceph/neuroinformatics/neuroinformatics/atlas-forge"

Returns
-------
atlas_dir : Path
The path to the directory
"""
return Path(os.getenv(env_var, default_path))
Loading
Loading