Skip to content

npxpy.nodes.aligners.CoarseAligner

Bases: Node

Class for coarse alignment nodes.

Attributes:

Name Type Description
residual_threshold Union[float, int]

The residual threshold for alignment.

alignment_anchors List[Dict]

List of alignment anchors with label and position.

Source code in npxpy/nodes/aligners.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class CoarseAligner(Node):
    """
    Class for coarse alignment nodes.

    Attributes:
        residual_threshold (Union[float, int]): The residual threshold for alignment.
        alignment_anchors (List[Dict]): List of alignment anchors with label and position.
    """

    def __init__(
        self,
        name: str = "Coarse aligner",
        residual_threshold: Union[float, int] = 10.0,
    ):
        """
        Initialize the coarse aligner with a name and a residual threshold.

        Parameters:
            name (str): The name of the coarse aligner node.
            residual_threshold (Union[float, int]): The residual threshold for alignment. Must be greater than 0.

        Raises:
            ValueError: If residual_threshold is not greater than 0.
        """
        super().__init__("coarse_alignment", name)

        self._alignment_anchors = []
        self.residual_threshold = (
            residual_threshold  # Using setter for validation
        )

    @property
    def residual_threshold(self):
        """Get the residual threshold."""
        return self._residual_threshold

    @residual_threshold.setter
    def residual_threshold(self, value: Union[float, int]):
        """
        Set the residual threshold for alignment.

        Parameters:
            value (Union[float, int]): Residual threshold, must be greater than 0.

        Raises:
            ValueError: If residual_threshold is not greater than 0.
        """
        if not isinstance(value, (float, int)) or value <= 0:
            raise ValueError("residual_threshold must be a positive number.")
        self._residual_threshold = value

    @property
    def alignment_anchors(self):
        """Get the list of alignment anchors."""
        return self._alignment_anchors

    def add_coarse_anchor(self, position: List[Union[float, int]], label: str):
        """
        Add a single coarse anchor with a label and position.

        Parameters:
            label (str): The label for the anchor.
            position (List[Union[float, int]]): The position [x, y, z] for the anchor.

        Raises:
            ValueError: If position does not contain exactly three elements.
            TypeError: If any element in position is not a number.
        """
        if not isinstance(label, str):
            raise TypeError("label must be a string.")
        if len(position) != 3:
            raise ValueError("position must be a list of three elements.")
        if not all(isinstance(p, (float, int)) for p in position):
            raise TypeError("All position elements must be numbers.")

        self._alignment_anchors.append(
            {
                "label": label,
                "position": position,
            }
        )
        return self

    def set_coarse_anchors_at(
        self,
        positions: List[List[Union[float, int]]],
        labels: List[str] = None,
    ):
        """
        Create multiple coarse anchors at specified positions.

        Parameters:
            labels (List[str]): List of labels for the anchors.
            positions (List[List[Union[float, int]]]): List of positions for the anchors, each position is [x, y, z].

        Returns:
            self: The instance of the CoarseAligner class.

        Raises:
            ValueError: If the number of labels does not match the number of positions.
            TypeError: If any label is not a string or any position is not a list of numbers.
        """
        if labels is None:
            labels = [f"anchor_{i}" for i in range(len(positions))]
        if len(labels) != len(positions):
            raise ValueError(
                "The number of labels must match the number of positions."
            )

        for label in labels:
            if not isinstance(label, str):
                raise TypeError("All labels must be strings.")

        for position in positions:
            if len(position) != 3:
                raise ValueError(
                    "Each position must be a list of three elements."
                )
            if not all(isinstance(p, (float, int)) for p in position):
                raise TypeError("All position elements must be numbers.")

        for label, position in zip(labels, positions):
            self.add_coarse_anchor(position, label)

        return self

    def to_dict(self) -> Dict[str, Any]:
        """
        Convert the CoarseAligner object into a dictionary.

        Returns:
            Dict[str, Any]: Dictionary representation of the object.
        """
        node_dict = super().to_dict()  # Get basic attributes from Node
        node_dict.update(
            {
                "alignment_anchors": self.alignment_anchors,
                "residual_threshold": self.residual_threshold,
            }
        )
        return node_dict

alignment_anchors property

Get the list of alignment anchors.

residual_threshold property writable

Get the residual threshold.

__init__(name='Coarse aligner', residual_threshold=10.0)

Initialize the coarse aligner with a name and a residual threshold.

Parameters:

Name Type Description Default
name str

The name of the coarse aligner node.

'Coarse aligner'
residual_threshold Union[float, int]

The residual threshold for alignment. Must be greater than 0.

10.0

Raises:

Type Description
ValueError

If residual_threshold is not greater than 0.

Source code in npxpy/nodes/aligners.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(
    self,
    name: str = "Coarse aligner",
    residual_threshold: Union[float, int] = 10.0,
):
    """
    Initialize the coarse aligner with a name and a residual threshold.

    Parameters:
        name (str): The name of the coarse aligner node.
        residual_threshold (Union[float, int]): The residual threshold for alignment. Must be greater than 0.

    Raises:
        ValueError: If residual_threshold is not greater than 0.
    """
    super().__init__("coarse_alignment", name)

    self._alignment_anchors = []
    self.residual_threshold = (
        residual_threshold  # Using setter for validation
    )

add_coarse_anchor(position, label)

Add a single coarse anchor with a label and position.

Parameters:

Name Type Description Default
label str

The label for the anchor.

required
position List[Union[float, int]]

The position [x, y, z] for the anchor.

required

Raises:

Type Description
ValueError

If position does not contain exactly three elements.

TypeError

If any element in position is not a number.

Source code in npxpy/nodes/aligners.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def add_coarse_anchor(self, position: List[Union[float, int]], label: str):
    """
    Add a single coarse anchor with a label and position.

    Parameters:
        label (str): The label for the anchor.
        position (List[Union[float, int]]): The position [x, y, z] for the anchor.

    Raises:
        ValueError: If position does not contain exactly three elements.
        TypeError: If any element in position is not a number.
    """
    if not isinstance(label, str):
        raise TypeError("label must be a string.")
    if len(position) != 3:
        raise ValueError("position must be a list of three elements.")
    if not all(isinstance(p, (float, int)) for p in position):
        raise TypeError("All position elements must be numbers.")

    self._alignment_anchors.append(
        {
            "label": label,
            "position": position,
        }
    )
    return self

set_coarse_anchors_at(positions, labels=None)

Create multiple coarse anchors at specified positions.

Parameters:

Name Type Description Default
labels List[str]

List of labels for the anchors.

None
positions List[List[Union[float, int]]]

List of positions for the anchors, each position is [x, y, z].

required

Returns:

Name Type Description
self

The instance of the CoarseAligner class.

Raises:

Type Description
ValueError

If the number of labels does not match the number of positions.

TypeError

If any label is not a string or any position is not a list of numbers.

Source code in npxpy/nodes/aligners.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def set_coarse_anchors_at(
    self,
    positions: List[List[Union[float, int]]],
    labels: List[str] = None,
):
    """
    Create multiple coarse anchors at specified positions.

    Parameters:
        labels (List[str]): List of labels for the anchors.
        positions (List[List[Union[float, int]]]): List of positions for the anchors, each position is [x, y, z].

    Returns:
        self: The instance of the CoarseAligner class.

    Raises:
        ValueError: If the number of labels does not match the number of positions.
        TypeError: If any label is not a string or any position is not a list of numbers.
    """
    if labels is None:
        labels = [f"anchor_{i}" for i in range(len(positions))]
    if len(labels) != len(positions):
        raise ValueError(
            "The number of labels must match the number of positions."
        )

    for label in labels:
        if not isinstance(label, str):
            raise TypeError("All labels must be strings.")

    for position in positions:
        if len(position) != 3:
            raise ValueError(
                "Each position must be a list of three elements."
            )
        if not all(isinstance(p, (float, int)) for p in position):
            raise TypeError("All position elements must be numbers.")

    for label, position in zip(labels, positions):
        self.add_coarse_anchor(position, label)

    return self

to_dict()

Convert the CoarseAligner object into a dictionary.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Dictionary representation of the object.

Source code in npxpy/nodes/aligners.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def to_dict(self) -> Dict[str, Any]:
    """
    Convert the CoarseAligner object into a dictionary.

    Returns:
        Dict[str, Any]: Dictionary representation of the object.
    """
    node_dict = super().to_dict()  # Get basic attributes from Node
    node_dict.update(
        {
            "alignment_anchors": self.alignment_anchors,
            "residual_threshold": self.residual_threshold,
        }
    )
    return node_dict