|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import List, Literal, Optional |
| 5 | +from pathlib import Path |
| 6 | +import numpy as np |
| 7 | +import json |
| 8 | +import pyvista as pv |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +from .. import geo, utils |
| 12 | +from ..vol import Volume |
| 13 | + |
| 14 | +log = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class FiducialList: |
| 18 | + # Can be treated like a list of Point3Ds |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + points: List[geo.Point3D], |
| 22 | + world_from_anatomical: Optional[geo.FrameTransform] = None, |
| 23 | + anatomical_coordinate_system: Literal["RAS", "LPS"] = "RAS", |
| 24 | + ): |
| 25 | + self.points = points |
| 26 | + self.world_from_anatomical = world_from_anatomical |
| 27 | + self.anatomical_coordinate_system = anatomical_coordinate_system |
| 28 | + |
| 29 | + def __getitem__(self, index): |
| 30 | + return self.points[index] |
| 31 | + |
| 32 | + def __len__(self): |
| 33 | + return len(self.points) |
| 34 | + |
| 35 | + def __iter__(self): |
| 36 | + return iter(self.points) |
| 37 | + |
| 38 | + def __repr__(self): |
| 39 | + return f"FiducialList({self.points})" |
| 40 | + |
| 41 | + def __str__(self): |
| 42 | + return str(self.points) |
| 43 | + |
| 44 | + def to_RAS(self) -> FiducialList: |
| 45 | + if self.anatomical_coordinate_system == "RAS": |
| 46 | + return self |
| 47 | + else: |
| 48 | + return FiducialList( |
| 49 | + [geo.RAS_from_LPS @ p for p in self.points], |
| 50 | + self.world_from_anatomical, |
| 51 | + "RAS", |
| 52 | + ) |
| 53 | + |
| 54 | + def to_LPS(self) -> FiducialList: |
| 55 | + if self.anatomical_coordinate_system == "LPS": |
| 56 | + return self |
| 57 | + else: |
| 58 | + return FiducialList( |
| 59 | + [geo.LPS_from_RAS @ p for p in self.points], |
| 60 | + self.world_from_anatomical, |
| 61 | + "LPS", |
| 62 | + ) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def from_fcsv( |
| 66 | + cls, path: Path, world_from_anatomical: Optional[geo.FrameTransform] = None |
| 67 | + ) -> FiducialList: |
| 68 | + """Load a FCSV file from Slicer3D |
| 69 | +
|
| 70 | + Args: |
| 71 | + path (Path): Path to the FCSV file |
| 72 | +
|
| 73 | + Returns: |
| 74 | + np.ndarray: Array of 3D points |
| 75 | + """ |
| 76 | + with open(path, "r") as f: |
| 77 | + lines = f.readlines() |
| 78 | + points = [] |
| 79 | + coordinate_system = None |
| 80 | + for line in lines: |
| 81 | + if line.startswith("# CoordinateSystem"): |
| 82 | + coordinate_system = line.split("=")[1].strip() |
| 83 | + elif line.startswith("#"): |
| 84 | + continue |
| 85 | + else: |
| 86 | + x, y, z = line.split(",")[1:4] |
| 87 | + points.append(geo.point(float(x), float(y), float(z))) |
| 88 | + |
| 89 | + if coordinate_system is None: |
| 90 | + log.warning("No coordinate system specified in FCSV file. Assuming LPS.") |
| 91 | + coordinate_system = "LPS" |
| 92 | + assert coordinate_system in ["RAS", "LPS"], "Unknown coordinate system" |
| 93 | + |
| 94 | + return cls( |
| 95 | + points, |
| 96 | + world_from_anatomical=world_from_anatomical, |
| 97 | + anatomical_coordinate_system=coordinate_system, |
| 98 | + ) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def from_json( |
| 102 | + cls, path: Path, world_from_anatomical: Optional[geo.FrameTransform] = None |
| 103 | + ): |
| 104 | + # TODO: add support for associated IDs of the fiducials. Should really be a list/dict. |
| 105 | + data = pd.read_json(path) |
| 106 | + control_points_table = pd.DataFrame.from_dict( |
| 107 | + data["markups"][0]["controlPoints"] |
| 108 | + ) |
| 109 | + coordinate_system = data["markups"][0]["coordinateSystem"] |
| 110 | + # TODO: not sure if this works. |
| 111 | + points = [ |
| 112 | + geo.point(*row[["x", "y", "z"]].values) |
| 113 | + for _, row in control_points_table.iterrows() |
| 114 | + ] |
| 115 | + |
| 116 | + return cls( |
| 117 | + points, |
| 118 | + world_from_anatomical=world_from_anatomical, |
| 119 | + anatomical_coordinate_system=coordinate_system, |
| 120 | + ) |
| 121 | + |
| 122 | + def save(self, path: Path): |
| 123 | + raise NotImplementedError() |
| 124 | + |
| 125 | + |
| 126 | +class Fiducial(geo.Point3D): |
| 127 | + @classmethod |
| 128 | + def from_fcsv( |
| 129 | + cls, |
| 130 | + path: Path, |
| 131 | + world_from_anatomical: Optional[geo.FrameTransform] = None, |
| 132 | + ): |
| 133 | + fiducial_list = FiducialList.from_fcsv(path) |
| 134 | + assert len(fiducial_list) == 1, "Expected a single fiducial" |
| 135 | + return cls( |
| 136 | + fiducial_list[0].data, |
| 137 | + world_from_anatomical=world_from_anatomical, |
| 138 | + anatomical_coordinate_system=fiducial_list.anatomical_coordinate_system, |
| 139 | + ) |
| 140 | + |
| 141 | + @classmethod |
| 142 | + def from_json( |
| 143 | + cls, path: Path, world_from_anatomical: Optional[geo.FrameTransform] = None |
| 144 | + ): |
| 145 | + raise NotImplementedError |
| 146 | + |
| 147 | + def save(self, path: Path): |
| 148 | + raise NotImplementedError |
0 commit comments