Skip to content

Commit 028e761

Browse files
Merge pull request #11 from StructuralPython/fixes/adding_robustness_to_singularities_to_polygon
Fixes/adding robustness to singularities to polygon
2 parents 7d6a3d2 + e6ddef8 commit 028e761

File tree

3 files changed

+338
-5
lines changed

3 files changed

+338
-5
lines changed

src/load_distribution/load_distribution.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,31 +215,36 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo
215215
# required shape, even if that means the exact x value is omitted (because we are
216216
# keeping the value immediately to the left and immediately to the right instead).
217217
x_acc = sorted(list(set(x_acc)))
218-
219218
x_ord_count = Counter([round(x, 6) for x in x_acc])
220219
to_filter = []
221220
for key, count in x_ord_count.items():
222221
if count == 3:
223222
to_filter.append(key)
224223

224+
filtered_indexes = []
225225
if to_filter:
226226
rounded_x_acc = [round(x, 6) for x in x_acc]
227227
for filter_val in to_filter:
228228
index = rounded_x_acc.index(filter_val)
229-
x_acc.pop(index)
229+
filtered_indexes.append(index + 1)
230+
231+
filtered_x = []
232+
for idx, x_val in enumerate(x_acc):
233+
if idx not in filtered_indexes:
234+
filtered_x.append(x_val)
230235

231236
# Now, for every x value, compute the corresponding y value
232-
y_acc = [sum([sing(x) for sing in sorted_sings]) for x in x_acc[:-1]]
237+
y_acc = [sum([sing(x) for sing in sorted_sings]) for x in filtered_x[:-1]]
233238
# Always ends on 0.0
234239
y_acc.append(0.0)
235240
if xy:
236241
return x_acc, y_acc
237242
else:
238243
precision = n if n else 2
239244
xy_acc = zip(
240-
[round(x, precision) for x in x_acc], [round(y, precision) for y in y_acc]
245+
[round(x, precision) for x in filtered_x],
246+
[round(y, precision) for y in y_acc],
241247
)
242-
243248
return Polygon(xy_acc)
244249

245250

tests/test_data.py

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
from load_distribution import Singularity
2+
import numpy as np
3+
4+
# An output from papermodels that caused an error in the singularities_to_polygon function due to lots of list popping
5+
6+
big_list_o_singularities = [
7+
Singularity(
8+
x0=6.628,
9+
x1=7.859,
10+
m=np.float64(0.0),
11+
y0=np.float64(-111.0726290117895),
12+
precision=6,
13+
eps=1e-12,
14+
),
15+
Singularity(
16+
x0=0.001,
17+
x1=0.413,
18+
m=np.float64(0.0),
19+
y0=np.float64(1684.3833962328304),
20+
precision=6,
21+
eps=1e-12,
22+
),
23+
Singularity(
24+
x0=3.172,
25+
x1=6.876,
26+
m=np.float64(0.0),
27+
y0=np.float64(-110.42669320082763),
28+
precision=6,
29+
eps=1e-12,
30+
),
31+
Singularity(
32+
x0=3.172,
33+
x1=6.876,
34+
m=np.float64(0.0),
35+
y0=np.float64(-110.42669320082763),
36+
precision=6,
37+
eps=1e-12,
38+
),
39+
Singularity(
40+
x0=0.001,
41+
x1=3.504,
42+
m=np.float64(0.0),
43+
y0=np.float64(-110.44792139070424),
44+
precision=6,
45+
eps=1e-12,
46+
),
47+
Singularity(
48+
x0=0.001,
49+
x1=3.504,
50+
m=np.float64(0.0),
51+
y0=np.float64(-110.44792139070424),
52+
precision=6,
53+
eps=1e-12,
54+
),
55+
Singularity(
56+
x0=3.172,
57+
x1=6.876,
58+
m=np.float64(0.0),
59+
y0=np.float64(-110.42669320082763),
60+
precision=6,
61+
eps=1e-12,
62+
),
63+
Singularity(
64+
x0=3.172,
65+
x1=6.876,
66+
m=np.float64(0.0),
67+
y0=np.float64(-110.42669320082763),
68+
precision=6,
69+
eps=1e-12,
70+
),
71+
Singularity(
72+
x0=0.001,
73+
x1=3.504,
74+
m=np.float64(0.0),
75+
y0=np.float64(-110.44792139070424),
76+
precision=6,
77+
eps=1e-12,
78+
),
79+
Singularity(
80+
x0=0.001,
81+
x1=3.504,
82+
m=np.float64(0.0),
83+
y0=np.float64(-110.44792139070424),
84+
precision=6,
85+
eps=1e-12,
86+
),
87+
Singularity(
88+
x0=0.001,
89+
x1=3.504,
90+
m=np.float64(0.0),
91+
y0=np.float64(-110.44792139070424),
92+
precision=6,
93+
eps=1e-12,
94+
),
95+
Singularity(
96+
x0=6.628,
97+
x1=7.859,
98+
m=np.float64(0.0),
99+
y0=np.float64(-111.0726290117895),
100+
precision=6,
101+
eps=1e-12,
102+
),
103+
Singularity(
104+
x0=7.57,
105+
x1=7.859,
106+
m=np.float64(0.0),
107+
y0=np.float64(1274.5157420687394),
108+
precision=6,
109+
eps=1e-12,
110+
),
111+
Singularity(
112+
x0=6.628,
113+
x1=7.859,
114+
m=np.float64(0.0),
115+
y0=np.float64(-111.0726290117895),
116+
precision=6,
117+
eps=1e-12,
118+
),
119+
Singularity(
120+
x0=0.001,
121+
x1=0.413,
122+
m=np.float64(0.0),
123+
y0=np.float64(1684.3833962328304),
124+
precision=6,
125+
eps=1e-12,
126+
),
127+
Singularity(
128+
x0=3.172,
129+
x1=6.876,
130+
m=np.float64(0.0),
131+
y0=np.float64(-110.42669320082763),
132+
precision=6,
133+
eps=1e-12,
134+
),
135+
Singularity(
136+
x0=3.172,
137+
x1=6.876,
138+
m=np.float64(0.0),
139+
y0=np.float64(-110.42669320082763),
140+
precision=6,
141+
eps=1e-12,
142+
),
143+
Singularity(
144+
x0=0.001,
145+
x1=3.504,
146+
m=np.float64(0.0),
147+
y0=np.float64(-110.44792139070424),
148+
precision=6,
149+
eps=1e-12,
150+
),
151+
Singularity(
152+
x0=0.001,
153+
x1=3.504,
154+
m=np.float64(0.0),
155+
y0=np.float64(-110.44792139070424),
156+
precision=6,
157+
eps=1e-12,
158+
),
159+
Singularity(
160+
x0=3.172,
161+
x1=6.876,
162+
m=np.float64(0.0),
163+
y0=np.float64(-110.42669320082763),
164+
precision=6,
165+
eps=1e-12,
166+
),
167+
Singularity(
168+
x0=3.172,
169+
x1=6.876,
170+
m=np.float64(0.0),
171+
y0=np.float64(-110.42669320082763),
172+
precision=6,
173+
eps=1e-12,
174+
),
175+
Singularity(
176+
x0=0.001,
177+
x1=3.504,
178+
m=np.float64(0.0),
179+
y0=np.float64(-110.44792139070424),
180+
precision=6,
181+
eps=1e-12,
182+
),
183+
Singularity(
184+
x0=0.001,
185+
x1=3.504,
186+
m=np.float64(0.0),
187+
y0=np.float64(-110.44792139070424),
188+
precision=6,
189+
eps=1e-12,
190+
),
191+
Singularity(
192+
x0=0.001,
193+
x1=3.504,
194+
m=np.float64(0.0),
195+
y0=np.float64(-110.44792139070424),
196+
precision=6,
197+
eps=1e-12,
198+
),
199+
Singularity(
200+
x0=6.628,
201+
x1=7.859,
202+
m=np.float64(0.0),
203+
y0=np.float64(-111.0726290117895),
204+
precision=6,
205+
eps=1e-12,
206+
),
207+
Singularity(
208+
x0=7.57,
209+
x1=7.859,
210+
m=np.float64(0.0),
211+
y0=np.float64(1274.5157420687394),
212+
precision=6,
213+
eps=1e-12,
214+
),
215+
Singularity(
216+
x0=6.628,
217+
x1=7.859,
218+
m=np.float64(0.0),
219+
y0=np.float64(-111.0726290117895),
220+
precision=6,
221+
eps=1e-12,
222+
),
223+
Singularity(
224+
x0=0.001,
225+
x1=0.413,
226+
m=np.float64(0.0),
227+
y0=np.float64(1684.3833962328304),
228+
precision=6,
229+
eps=1e-12,
230+
),
231+
Singularity(
232+
x0=3.172,
233+
x1=6.876,
234+
m=np.float64(0.0),
235+
y0=np.float64(-110.42669320082763),
236+
precision=6,
237+
eps=1e-12,
238+
),
239+
Singularity(
240+
x0=3.172,
241+
x1=6.876,
242+
m=np.float64(0.0),
243+
y0=np.float64(-110.42669320082763),
244+
precision=6,
245+
eps=1e-12,
246+
),
247+
Singularity(
248+
x0=0.001,
249+
x1=3.504,
250+
m=np.float64(0.0),
251+
y0=np.float64(-110.44792139070424),
252+
precision=6,
253+
eps=1e-12,
254+
),
255+
Singularity(
256+
x0=0.001,
257+
x1=3.504,
258+
m=np.float64(0.0),
259+
y0=np.float64(-110.44792139070424),
260+
precision=6,
261+
eps=1e-12,
262+
),
263+
Singularity(
264+
x0=3.172,
265+
x1=6.876,
266+
m=np.float64(0.0),
267+
y0=np.float64(-110.42669320082763),
268+
precision=6,
269+
eps=1e-12,
270+
),
271+
Singularity(
272+
x0=3.172,
273+
x1=6.876,
274+
m=np.float64(0.0),
275+
y0=np.float64(-110.42669320082763),
276+
precision=6,
277+
eps=1e-12,
278+
),
279+
Singularity(
280+
x0=0.001,
281+
x1=3.504,
282+
m=np.float64(0.0),
283+
y0=np.float64(-110.44792139070424),
284+
precision=6,
285+
eps=1e-12,
286+
),
287+
Singularity(
288+
x0=0.001,
289+
x1=3.504,
290+
m=np.float64(0.0),
291+
y0=np.float64(-110.44792139070424),
292+
precision=6,
293+
eps=1e-12,
294+
),
295+
Singularity(
296+
x0=0.001,
297+
x1=3.504,
298+
m=np.float64(0.0),
299+
y0=np.float64(-110.44792139070424),
300+
precision=6,
301+
eps=1e-12,
302+
),
303+
Singularity(
304+
x0=6.628,
305+
x1=7.859,
306+
m=np.float64(0.0),
307+
y0=np.float64(-111.0726290117895),
308+
precision=6,
309+
eps=1e-12,
310+
),
311+
Singularity(
312+
x0=7.57,
313+
x1=7.859,
314+
m=np.float64(0.0),
315+
y0=np.float64(1274.5157420687394),
316+
precision=6,
317+
eps=1e-12,
318+
),
319+
]

tests/test_load_distribution.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import load_distribution as ld
2+
import test_data as data
23
from shapely import Polygon, GeometryCollection
34

45
import pytest
@@ -236,6 +237,14 @@ def test_singularities_to_polygon():
236237
== "POLYGON ((0 0, 0.034368 0, 0.051573 0.5, 0.051573 0.5, 11.705 0.5, 11.705 0, 0 0))"
237238
)
238239

240+
# This test fails due to popping multiple items from a list where the indexes have changed
241+
# because of the popping
242+
243+
assert (
244+
(ld.singularities_to_polygon(data.big_list_o_singularities).wkt)
245+
== "POLYGON ((0 0, 0.001 0, 0.001 3396.431373, 0.413 3396.431373, 0.413 -1656.718815, 3.172 -1656.718815, 3.172 -2981.839131, 3.504 -2981.839131, 3.504 -1325.120316, 6.628 -1325.120316, 6.628 -1991.55609, 6.876 -1991.55609, 6.876 -666.435774, 7.57 -666.435774, 7.57 3157.111452, 7.859 3157.111452, 7.859 0, 0 0))"
246+
)
247+
239248

240249
def test_overlap_region_to_singularity():
241250
assert ld.overlap_region_to_singularity(

0 commit comments

Comments
 (0)