Source code for camacq.plugins.leica.helper
"""Helper functions for Leica api."""
from pathlib import Path, PureWindowsPath
from leicaimage import experiment
[docs]
def find_image_path(relpath: str, root: str) -> str:
"""Parse the relpath from the server to find file path from root.
Convert from windows path to posix path.
Parameters
----------
relpath : str
A relative path to the image.
root : str
Path to directory where path should start.
Returns
-------
str
Return path to image.
"""
parts = PureWindowsPath(relpath).parts
return str(Path(root).joinpath(*parts))
[docs]
def get_field(path: str) -> str:
"""Get path to field from image path.
Parameters
----------
path : string
Path to image.
Returns
-------
str
Return path to field directory of image.
"""
return experiment.Experiment(path).dirname
[docs]
def get_well(path: str) -> str:
"""Get path to well from image path.
Parameters
----------
path : string
Path to image.
Returns
-------
str
Return path to well directory of image.
"""
return experiment.Experiment(get_field(path)).dirname
[docs]
def get_imgs(path: str, img_type: str = "tif", search: str = "") -> list[Path]:
"""Get all images below path.
Parameters
----------
path : string
Path to directory where to search for images.
img_type : string
A string representing the image file type extension.
path : string
A glob pattern string to use in the search.
Returns
-------
list
Return paths of all images found.
"""
root = Path(path)
_path = Path("")
if search:
search = f"{search}*"
patterns = ["slide", "chamber--", "field--", "image--"]
for pattern in patterns:
if pattern not in path:
_path = _path / f"{pattern}*"
return list(root.glob(f"{_path}{search}.{img_type}"))