Skip to content

Commit df938d1

Browse files
committed
fix: adding post_init to ValuePoints/VectorPoints to validate as numpy arrays
1 parent 3f06403 commit df938d1

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

LoopStructural/datatypes/_point.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ class ValuePoints:
1414
values: np.ndarray = field(default_factory=lambda: np.array([0]))
1515
name: str = "unnamed"
1616
properties: Optional[dict] = None
17-
17+
def __post_init__(self):
18+
19+
self.values = np.asarray(self.values)
20+
self.locations = np.asarray(self.locations)
21+
if self.locations.shape[1] != 3:
22+
raise ValueError('locations must be of shape (n, 3)')
23+
if len(self.values) != len(self.locations):
24+
raise ValueError('values must be the same length as locations')
25+
for k, v in (self.properties or {}).items():
26+
if len(v) != len(self.locations):
27+
raise ValueError(f'Property {k} must be the same length as locations')
28+
self.properties[k] = np.asarray(v)
1829
def to_dict(self):
1930
return {
2031
"locations": self.locations,
@@ -112,7 +123,17 @@ class VectorPoints:
112123
vectors: np.ndarray = field(default_factory=lambda: np.array([[0, 0, 0]]))
113124
name: str = "unnamed"
114125
properties: Optional[dict] = None
115-
126+
def __post_init__(self):
127+
self.vectors = np.asarray(self.vectors)
128+
self.locations = np.asarray(self.locations)
129+
if self.locations.shape[1] != 3:
130+
raise ValueError('locations must be of shape (n, 3)')
131+
if len(self.vectors) != len(self.locations):
132+
raise ValueError('vectors must be the same length as locations')
133+
for k, v in (self.properties or {}).items():
134+
if len(v) != len(self.locations):
135+
raise ValueError(f'Property {k} must be the same length as locations')
136+
self.properties[k] = np.asarray(v)
116137
def to_dict(self):
117138
return {
118139
"locations": self.locations,

0 commit comments

Comments
 (0)