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

Update Pathlib usage and docstrings #68

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 13 additions & 6 deletions brainglobe_utils/IO/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import logging
import os
import pathlib
from pathlib import Path
from typing import List, Optional, Union
from xml.dom import minidom
from xml.etree import ElementTree
Expand Down Expand Up @@ -88,7 +88,7 @@ def raise_cell_read_error(cells_file_path):
)


def get_cells_xml(xml_file_path, cells_only=False):
def get_cells_xml(xml_file_path: Path, cells_only: Optional[bool] = False):
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
"""
Read cells from an xml file.

Expand Down Expand Up @@ -121,7 +121,11 @@ def get_cells_xml(xml_file_path, cells_only=False):
return cells


def get_cells_yml(cells_file_path, ignore_type=False, marker="markers"):
def get_cells_yml(
cells_file_path: Path,
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
ignore_type: Optional[bool] = False,
marker: Optional[str] = "markers",
):
"""
Read cells from a yml file.

Expand Down Expand Up @@ -158,7 +162,7 @@ def get_cells_yml(cells_file_path, ignore_type=False, marker="markers"):
return cells


def get_cells_dir(cells_file_path, cell_type=None):
def get_cells_dir(cells_file_path: Path, cell_type: Optional[bool] = None):
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
"""
Read cells from a directory. Cells will be created based on the filenames
of files in the directory, one cell per file.
Expand Down Expand Up @@ -230,7 +234,10 @@ def save_cells(


def cells_to_xml(
cells, xml_file_path, indentation_str=" ", artifact_keep=True
cells: Cell,
xml_file_path: Path,
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
indentation_str: Optional[str] = " ",
artifact_keep: Optional[bool] = True,
):
"""
Save cells to an xml file.
Expand Down Expand Up @@ -263,7 +270,7 @@ def cells_to_dataframe(cells: List[Cell]) -> pd.DataFrame:
return pd.DataFrame([c.to_dict() for c in cells])


def cells_to_csv(cells: List[Cell], csv_file_path: Union[str, pathlib.Path]):
def cells_to_csv(cells: List[Cell], csv_file_path: Union[str, Path]):
"""Save cells to csv file"""
df = cells_to_dataframe(cells)
df.to_csv(csv_file_path)
Expand Down
12 changes: 11 additions & 1 deletion brainglobe_utils/IO/surfaces.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
def marching_cubes_to_obj(marching_cubes_out, output_file):
from typing import TYPE_CHECKING, Tuple, Union

if TYPE_CHECKING:
from pathlib import Path

Check warning on line 4 in brainglobe_utils/IO/surfaces.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_utils/IO/surfaces.py#L4

Added line #L4 was not covered by tests

from numpy.typing import NDArray

Check warning on line 6 in brainglobe_utils/IO/surfaces.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_utils/IO/surfaces.py#L6

Added line #L6 was not covered by tests


def marching_cubes_to_obj(
marching_cubes_out: Tuple["NDArray"], output_file: Union[str, "Path"]
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Saves the output of skimage.measure.marching_cubes as an .obj file

Expand Down
17 changes: 14 additions & 3 deletions brainglobe_utils/IO/yaml.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from typing import TYPE_CHECKING, Any, Dict, Optional, Union

import yaml

if TYPE_CHECKING:
from pathlib import Path

Check warning on line 6 in brainglobe_utils/IO/yaml.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_utils/IO/yaml.py#L6

Added line #L6 was not covered by tests


def read_yaml_section(yaml_file, section):
def read_yaml_section(
yaml_file: Union[str, "Path"], section: str
) -> Dict[str, Any]:
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
"""
Read section from yaml file.

Expand All @@ -21,7 +28,7 @@
return contents


def open_yaml(yaml_file):
def open_yaml(yaml_file: Union[str, "Path"]) -> Dict[str, Any]:
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
"""
Read the contents of a yaml file.

Expand All @@ -40,7 +47,11 @@
return yaml_contents


def save_yaml(yaml_contents, output_file, default_flow_style=False):
def save_yaml(
yaml_contents: Dict[str, Any],
output_file: Union[str, "Path"],
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
default_flow_style: Optional[bool] = False,
):
"""
Save contents to a yaml file.

Expand Down
8 changes: 7 additions & 1 deletion brainglobe_utils/general/pathlib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def append_to_pathlib_stem(path, string_to_append):
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path

Check warning on line 4 in brainglobe_utils/general/pathlib.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_utils/general/pathlib.py#L4

Added line #L4 was not covered by tests


def append_to_pathlib_stem(path: "Path", string_to_append):
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
"""
Appends a string to the stem of a pathlib object.

Expand Down
17 changes: 11 additions & 6 deletions brainglobe_utils/general/string.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from typing import TYPE_CHECKING, Optional

from natsort import natsorted

from brainglobe_utils.general import list

if TYPE_CHECKING:
from pathlib import Path

Check warning on line 8 in brainglobe_utils/general/string.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_utils/general/string.py#L8

Added line #L8 was not covered by tests


def get_text_lines(
file,
return_lines=None,
rstrip=True,
sort=False,
remove_empty_lines=True,
encoding="utf8",
file: "Path",
willGraham01 marked this conversation as resolved.
Show resolved Hide resolved
return_lines: Optional[int] = None,
rstrip: Optional[bool] = True,
sort: Optional[bool] = False,
remove_empty_lines: Optional[bool] = True,
encoding: Optional[str] = "utf8",
):
"""
Return only the nth line of a text file.
Expand Down
Loading