1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 | class EdgeAligner(Node):
"""
A class to represent an edge aligner with various attributes and methods for managing edge alignment.
Attributes:
alignment_anchors (List[Dict[str, Any]]): List of alignment anchors.
"""
def __init__(
self,
name: str = "Edge aligner",
edge_location: List[float] = [0.0, 0.0],
edge_orientation: float = 0.0,
center_stage: bool = True,
action_upon_failure: str = "abort",
laser_power: Union[float, int] = 0.5,
scan_area_res_factors: List[float] = [1.0, 1.0],
scan_z_sample_distance: Union[float, int] = 0.1,
scan_z_sample_count: int = 51,
outlier_threshold: float = 10.0,
):
"""
Initialize the edge aligner with the specified parameters.
"""
super().__init__(node_type="edge_alignment", name=name)
# Set attributes using setters
self.edge_location = edge_location
self.edge_orientation = edge_orientation
self.center_stage = center_stage
self.action_upon_failure = action_upon_failure
self.laser_power = laser_power
self.scan_area_res_factors = scan_area_res_factors
self.scan_z_sample_distance = scan_z_sample_distance
self.scan_z_sample_count = scan_z_sample_count
self.outlier_threshold = outlier_threshold
self.alignment_anchors = []
# Property setters with validation
@property
def edge_location(self) -> List[float]:
return self._edge_location
@edge_location.setter
def edge_location(self, value: List[float]):
if (
not isinstance(value, list)
or len(value) != 2
or not all(isinstance(val, (float, int)) for val in value)
):
raise TypeError("edge_location must be a list of two numbers.")
self._edge_location = value
@property
def edge_orientation(self) -> float:
return self._edge_orientation
@edge_orientation.setter
def edge_orientation(self, value: float):
if not isinstance(value, (float, int)):
raise TypeError("edge_orientation must be a float or an int.")
self._edge_orientation = value
@property
def center_stage(self) -> bool:
return self._center_stage
@center_stage.setter
def center_stage(self, value: bool):
if not isinstance(value, bool):
raise TypeError("center_stage must be a boolean.")
self._center_stage = value
@property
def action_upon_failure(self) -> str:
return self._action_upon_failure
@action_upon_failure.setter
def action_upon_failure(self, value: str):
if value not in ["abort", "ignore"]:
raise ValueError(
"action_upon_failure must be 'abort' or 'ignore'."
)
self._action_upon_failure = value
@property
def laser_power(self) -> Union[float, int]:
return self._laser_power
@laser_power.setter
def laser_power(self, value: Union[float, int]):
if not isinstance(value, (float, int)) or value < 0:
raise ValueError("laser_power must be a non-negative number.")
self._laser_power = value
@property
def scan_area_res_factors(self) -> List[float]:
return self._scan_area_res_factors
@scan_area_res_factors.setter
def scan_area_res_factors(self, value: List[float]):
if (
not isinstance(value, list)
or len(value) != 2
or not all(isinstance(val, (float, int)) for val in value)
):
raise TypeError(
"scan_area_res_factors must be a list of two numbers greater than zero."
)
if not all(factor > 0 for factor in value):
raise ValueError(
"All elements in scan_area_res_factors must be greater than 0."
)
self._scan_area_res_factors = value
@property
def scan_z_sample_distance(self) -> Union[float, int]:
return self._scan_z_sample_distance
@scan_z_sample_distance.setter
def scan_z_sample_distance(self, value: Union[float, int]):
if not isinstance(value, (float, int)) or value <= 0:
raise ValueError(
"scan_z_sample_distance must be a positive number."
)
self._scan_z_sample_distance = value
@property
def scan_z_sample_count(self) -> int:
return self._scan_z_sample_count
@scan_z_sample_count.setter
def scan_z_sample_count(self, value: int):
if not isinstance(value, int) or value < 1:
raise ValueError(
"scan_z_sample_count must be an integer greater than zero."
)
self._scan_z_sample_count = value
@property
def outlier_threshold(self) -> float:
return self._outlier_threshold
@outlier_threshold.setter
def outlier_threshold(self, value: float):
if not isinstance(value, (float, int)) or not (0 <= value <= 100):
raise ValueError(
"outlier_threshold must be a number between 0 and 100."
)
self._outlier_threshold = value
def add_measurement(
self,
offset: Union[float, int],
scan_area_size: List[Union[float, int]],
label: str,
):
"""
Add a measurement with a label, offset, and scan area size.
"""
if not isinstance(label, str):
raise TypeError("label must be a string.")
if not isinstance(offset, (float, int)):
raise TypeError("offset must be a float or an int.")
if (
not isinstance(scan_area_size, list)
or len(scan_area_size) != 2
or not all(isinstance(val, (float, int)) for val in scan_area_size)
):
raise TypeError("scan_area_size must be a list of two numbers.")
if scan_area_size[0] <= 0:
raise ValueError(
"The width (X) in scan_area_size must be greater than 0."
)
if scan_area_size[1] < 0:
raise ValueError(
"The height (Y) in scan_area_size must be greater than or equal to 0."
)
self.alignment_anchors.append(
{
"label": label,
"offset": offset,
"scan_area_size": scan_area_size,
}
)
return self
def set_measurements_at(
self,
offsets: List[Union[float, int]],
scan_area_sizes: List[List[Union[float, int]]] = None,
labels: List[str] = None,
):
"""
Set multiple measurements at specified positions.
"""
if scan_area_sizes is None:
scan_area_sizes = [[50.0, 10.0]] * len(offsets)
if labels is None:
labels = [f"marker_{i}" for i in range(len(offsets))]
if len(labels) != len(scan_area_sizes) or len(labels) != len(offsets):
raise ValueError(
"The number of labels, offsets, and scan_area_sizes must match."
)
for label in labels:
if not isinstance(label, str):
raise TypeError("All labels must be strings.")
for offset in offsets:
if not isinstance(offset, (float, int)):
raise TypeError("All offsets must be float or int.")
for scan_area_size in scan_area_sizes:
if (
not isinstance(scan_area_size, list)
or len(scan_area_size) != 2
or not all(
isinstance(val, (float, int)) for val in scan_area_size
)
):
raise TypeError(
"All scan_area_sizes must be lists of two numbers."
)
if scan_area_size[0] <= 0:
raise ValueError(
"The width (X) in scan_area_size must be greater than 0."
)
if scan_area_size[1] < 0:
raise ValueError(
"The height (Y) in scan_area_size must be greater than or equal to 0."
)
for label, offset, scan_area_size in zip(
labels, offsets, scan_area_sizes
):
self.add_measurement(offset, scan_area_size, label)
return self
def to_dict(self) -> Dict[str, Any]:
"""
Converts the current state of the object into a dictionary representation.
"""
node_dict = super().to_dict()
node_dict.update(
{
"xy_position_local_cos": self.edge_location,
"z_rotation_local_cos": self.edge_orientation,
"center_stage": self.center_stage,
"action_upon_failure": self.action_upon_failure,
"laser_power": self.laser_power,
"scan_area_res_factors": self.scan_area_res_factors,
"scan_z_sample_distance": self.scan_z_sample_distance,
"scan_z_sample_count": self.scan_z_sample_count,
"outlier_threshold": self.outlier_threshold,
"alignment_anchors": self.alignment_anchors,
}
)
return node_dict
|