Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/load_distribution/load_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,31 +215,36 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo
# required shape, even if that means the exact x value is omitted (because we are
# keeping the value immediately to the left and immediately to the right instead).
x_acc = sorted(list(set(x_acc)))

x_ord_count = Counter([round(x, 6) for x in x_acc])
to_filter = []
for key, count in x_ord_count.items():
if count == 3:
to_filter.append(key)

filtered_indexes = []
if to_filter:
rounded_x_acc = [round(x, 6) for x in x_acc]
for filter_val in to_filter:
index = rounded_x_acc.index(filter_val)
x_acc.pop(index)
filtered_indexes.append(index + 1)

filtered_x = []
for idx, x_val in enumerate(x_acc):
if idx not in filtered_indexes:
filtered_x.append(x_val)

# Now, for every x value, compute the corresponding y value
y_acc = [sum([sing(x) for sing in sorted_sings]) for x in x_acc[:-1]]
y_acc = [sum([sing(x) for sing in sorted_sings]) for x in filtered_x[:-1]]
# Always ends on 0.0
y_acc.append(0.0)
if xy:
return x_acc, y_acc
else:
precision = n if n else 2
xy_acc = zip(
[round(x, precision) for x in x_acc], [round(y, precision) for y in y_acc]
[round(x, precision) for x in filtered_x],
[round(y, precision) for y in y_acc],
)

return Polygon(xy_acc)


Expand Down
319 changes: 319 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
from load_distribution import Singularity
import numpy as np

# An output from papermodels that caused an error in the singularities_to_polygon function due to lots of list popping

big_list_o_singularities = [
Singularity(
x0=6.628,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(-111.0726290117895),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=0.413,
m=np.float64(0.0),
y0=np.float64(1684.3833962328304),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=6.628,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(-111.0726290117895),
precision=6,
eps=1e-12,
),
Singularity(
x0=7.57,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(1274.5157420687394),
precision=6,
eps=1e-12,
),
Singularity(
x0=6.628,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(-111.0726290117895),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=0.413,
m=np.float64(0.0),
y0=np.float64(1684.3833962328304),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=6.628,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(-111.0726290117895),
precision=6,
eps=1e-12,
),
Singularity(
x0=7.57,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(1274.5157420687394),
precision=6,
eps=1e-12,
),
Singularity(
x0=6.628,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(-111.0726290117895),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=0.413,
m=np.float64(0.0),
y0=np.float64(1684.3833962328304),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=3.172,
x1=6.876,
m=np.float64(0.0),
y0=np.float64(-110.42669320082763),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=0.001,
x1=3.504,
m=np.float64(0.0),
y0=np.float64(-110.44792139070424),
precision=6,
eps=1e-12,
),
Singularity(
x0=6.628,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(-111.0726290117895),
precision=6,
eps=1e-12,
),
Singularity(
x0=7.57,
x1=7.859,
m=np.float64(0.0),
y0=np.float64(1274.5157420687394),
precision=6,
eps=1e-12,
),
]
9 changes: 9 additions & 0 deletions tests/test_load_distribution.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import load_distribution as ld
import test_data as data
from shapely import Polygon, GeometryCollection

import pytest
Expand Down Expand Up @@ -236,6 +237,14 @@ def test_singularities_to_polygon():
== "POLYGON ((0 0, 0.034368 0, 0.051573 0.5, 0.051573 0.5, 11.705 0.5, 11.705 0, 0 0))"
)

# This test fails due to popping multiple items from a list where the indexes have changed
# because of the popping

assert (
(ld.singularities_to_polygon(data.big_list_o_singularities).wkt)
== "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))"
)


def test_overlap_region_to_singularity():
assert ld.overlap_region_to_singularity(
Expand Down