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

Delete unused code & add tests #18

Merged
merged 11 commits into from
Dec 15, 2023
Merged
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
10 changes: 0 additions & 10 deletions brainglobe_utils/IO/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ def get_cells(
cells_only: bool = False,
cell_type: Optional[int] = None,
):
# TODO: implement csv read
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we... actually address this? The function isn't changed

Copy link
Member Author

Choose a reason for hiding this comment

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

No, but there's no need to.

if cells_file_path.endswith(".xml"):
return get_cells_xml(cells_file_path, cells_only=cells_only)
elif cells_file_path.endswith(".yml"):
# Not general
return get_cells_yml(cells_file_path, ignore_type=True)
elif os.path.isdir(cells_file_path):
try:
Expand Down Expand Up @@ -211,14 +209,6 @@ def pretty_xml(elem, indentation_str=" "):
return md_parsed.toprettyxml(indent=indentation_str, encoding="UTF-8")


# TRANSCODE
def transform_xml_file(xml_file_path, output_file_path, transform_params):
cells = get_cells(xml_file_path) # TODO: check if cells_only
for cell in cells:
cell.transform(*transform_params)
cells_to_xml(cells, output_file_path)


def find_relevant_tiffs(tiffs, cell_def):
cells = [UntypedCell(tiff) for tiff in tiffs]
if os.path.isdir(cell_def):
Expand Down
19 changes: 0 additions & 19 deletions brainglobe_utils/array/filter.py

This file was deleted.

38 changes: 0 additions & 38 deletions brainglobe_utils/array/fit.py

This file was deleted.

29 changes: 0 additions & 29 deletions brainglobe_utils/array/locate.py

This file was deleted.

23 changes: 0 additions & 23 deletions brainglobe_utils/array/math.py

This file was deleted.

100 changes: 0 additions & 100 deletions brainglobe_utils/array/misc.py

This file was deleted.

21 changes: 0 additions & 21 deletions brainglobe_utils/array/size.py

This file was deleted.

111 changes: 1 addition & 110 deletions brainglobe_utils/cells/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
Based on https://github.com/SainsburyWellcomeCentre/niftynet_cell_count by
Christian Niedworok (https://github.com/cniedwor).
"""
import logging
import math
import os
import re
from collections import defaultdict
from functools import total_ordering
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union
from typing import Any, DefaultDict, Dict, List, Tuple, Union
from xml.etree import ElementTree
from xml.etree.ElementTree import Element as EtElement

import numpy.typing as npt


@total_ordering
class Cell:
Expand Down Expand Up @@ -250,112 +247,6 @@ def pos_from_file_name(file_name: str) -> List[float]:
return [int(p) for p in (x[-1][1:], y[-1][1:], z[-1][1:])]


def transform(
cell: Cell,
deformation_field: npt.NDArray[Any],
field_scales: Tuple[float, float, float],
scales: Tuple[float, float, float],
) -> Optional[Cell]:
"""
Transforms cell position from one space, to another (defined by a
deformation field)
:param cell: Cells in original space
:param deformation_field: Deformation field
(shape (len(x), len(y), len(z), 3). For each spatial position, there is a
vector mapping onto a new coordinate space.
:param field_scales: Scaling of the deformation field values (in mm) into
voxel space (e.g. 100,100,100)
:param scales: Scale of cell x, y and z positions onto deformation
field (e.g. 0.2, 0.2, 0.5)
:return: Cell in the new space
"""
scaled_x = int(round(cell.x * scales[0]))
scaled_y = int(round(cell.y * scales[1]))
scaled_z = int(round(cell.z * scales[2]))

try:
new_x = int(
round(
field_scales[0]
* deformation_field[scaled_x, scaled_y, scaled_z, 0, 0]
)
)
new_y = int(
round(
field_scales[1]
* deformation_field[scaled_x, scaled_y, scaled_z, 0, 1]
)
)
new_z = int(
round(
field_scales[2]
* deformation_field[scaled_x, scaled_y, scaled_z, 0, 2]
)
)

# if any new coordinates are negative
if any(position < 0 for position in [new_x, new_y, new_z]):
warn_outside_target_space(cell)

else:
cell.x = new_x
cell.y = new_y
cell.z = new_z
return cell

except IndexError:
warn_outside_target_space(cell)
return None


def warn_outside_target_space(cell: Cell) -> None:
logging.warning(
"Position x:{}, y:{}, z{} is outside the target "
"coordinate space, skipping. If this happens for many "
"cells, something may be up.".format(cell.x, cell.y, cell.z)
)


def transform_cell_positions(
cells: List[Cell],
deformation_field: npt.NDArray[Any],
field_scales: Tuple[float, float, float] = (100, 100, 100),
scales: Tuple[float, float, float] = (1, 1, 1),
) -> List[Cell]:
"""
Transforms cell positions from one space, to another (defined by a
deformation field)
:param cells: List of cells in original space
:param deformation_field: Deformation field
(shape (len(x), len(y), len(z), 3). For each spatial position, there is a
vector mapping onto a new coordinate space.
:param field_scales: Scaling of the deformation field values (in mm) into
voxel space (e.g. 100,100,100)
:param scales: Scale of cell x, y and z positions onto deformation
field (e.g. 0.2, 0.2, 0.5)
:return: list of cells in the new space
"""
# TODO: parallelise (maybe not needed, very quick anyway)
# TODO: clarify this transformation, and the existing transformed_x
# property of the cells used for other things (e.g. summaries)
transformed_cells = [
transform(cell, deformation_field, field_scales, scales)
for cell in cells
]

# Remove None's from list (where cell couldn't be transformed)
transformed_cells_no_none = [
cell for cell in transformed_cells if cell is not None
]
cells_not_transformed = len(cells) - len(transformed_cells_no_none)
logging.warning(
"{} cells were not transformed to standard space".format(
cells_not_transformed
)
)
return transformed_cells_no_none


def group_cells_by_z(cells: List[Cell]) -> DefaultDict[float, List[Cell]]:
"""
For a list of Cells return a dict of lists of cells, grouped by plane.
Expand Down
Loading