Skip to content

Commit 324fb89

Browse files
committed
updated unit tests for test_scene.py and test_geometry.py. Removed the "swap_axes" argument from some functions where it was not needed.
1 parent d53fae0 commit 324fb89

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed

tests/test_components/test_geometry.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ def test_to_gds(geometry, tmp_path):
952952
assert len(cell.polygons) == 0
953953

954954

955-
def test_custom_surface_geometry(tmp_path):
955+
@pytest.mark.parametrize("swap_axes", [True, False])
956+
def test_custom_surface_geometry(swap_axes, tmp_path):
956957
# create tetrahedron STL
957958
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
958959
faces = np.array([[1, 2, 3], [0, 3, 2], [0, 1, 3], [0, 2, 1]])
@@ -985,9 +986,13 @@ def test_custom_surface_geometry(tmp_path):
985986
assert np.isclose(geom.volume(), 1 / 6)
986987

987988
# test intersections
988-
assert shapely.equals(geom.intersections_plane(x=0), shapely.Polygon([[0, 0], [0, 1], [1, 0]]))
989989
assert shapely.equals(
990-
geom.intersections_plane(z=0.5), shapely.Polygon([[0, 0], [0, 0.5], [0.5, 0]])
990+
geom.intersections_plane(x=0, swap_axes=swap_axes),
991+
shapely.Polygon([[0, 0], [0, 1], [1, 0]]),
992+
)
993+
assert shapely.equals(
994+
geom.intersections_plane(z=0.5, swap_axes=swap_axes),
995+
shapely.Polygon([[0, 0], [0, 0.5], [0.5, 0]]),
991996
)
992997

993998
# test inside
@@ -996,7 +1001,7 @@ def test_custom_surface_geometry(tmp_path):
9961001

9971002
# test plot
9981003
_, ax = plt.subplots()
999-
_ = geom.plot(z=0.1, ax=ax)
1004+
_ = geom.plot(z=0.1, ax=ax, swap_axes=swap_axes)
10001005
plt.close()
10011006

10021007
# test inconsistent winding
@@ -1046,7 +1051,7 @@ def test_custom_surface_geometry(tmp_path):
10461051
boundary_spec=td.BoundarySpec.all_sides(td.PML()),
10471052
)
10481053
_, ax = plt.subplots()
1049-
_ = sim.plot(y=0, ax=ax)
1054+
_ = sim.plot(y=0, ax=ax, swap_axes=swap_axes)
10501055
plt.close()
10511056

10521057
# allow small triangles

tests/test_components/test_scene.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ def test_validate_components_none():
5050
assert SCENE._validate_num_mediums(val=None) is None
5151

5252

53-
def test_plot_eps():
54-
ax = SCENE_FULL.plot_eps(x=0)
53+
@pytest.mark.parametrize("swap_axes", [True, False])
54+
def test_plot_eps(swap_axes):
55+
ax = SCENE_FULL.plot_eps(x=0, swap_axes=swap_axes)
5556
SCENE_FULL._add_cbar_eps(eps_min=1, eps_max=2, ax=ax)
5657
plt.close()
5758

5859

59-
def test_plot_eps_multiphysics():
60+
@pytest.mark.parametrize("swap_axes", [True, False])
61+
def test_plot_eps_multiphysics(swap_axes):
6062
s = td.Scene(
6163
structures=[
6264
td.Structure(
@@ -70,20 +72,22 @@ def test_plot_eps_multiphysics():
7072
]
7173
)
7274
assert s.structures[0].medium.name == "SiO2"
73-
s.plot_eps(x=0)
75+
s.plot_eps(x=0, swap_axes=swap_axes)
7476

7577

76-
def test_plot_eps_bounds():
77-
_ = SCENE_FULL.plot_eps(x=0, hlim=[-0.45, 0.45])
78+
@pytest.mark.parametrize("swap_axes", [True, False])
79+
def test_plot_eps_bounds(swap_axes):
80+
_ = SCENE_FULL.plot_eps(x=0, hlim=[-0.45, 0.45], swap_axes=swap_axes)
7881
plt.close()
79-
_ = SCENE_FULL.plot_eps(x=0, vlim=[-0.45, 0.45])
82+
_ = SCENE_FULL.plot_eps(x=0, vlim=[-0.45, 0.45], swap_axes=swap_axes)
8083
plt.close()
81-
_ = SCENE_FULL.plot_eps(x=0, hlim=[-0.45, 0.45], vlim=[-0.45, 0.45])
84+
_ = SCENE_FULL.plot_eps(x=0, hlim=[-0.45, 0.45], vlim=[-0.45, 0.45], swap_axes=swap_axes)
8285
plt.close()
8386

8487

85-
def test_plot():
86-
SCENE_FULL.plot(x=0)
88+
@pytest.mark.parametrize("swap_axes", [True, False])
89+
def test_plot(swap_axes):
90+
SCENE_FULL.plot(x=0, swap_axes=swap_axes)
8791
plt.close()
8892

8993

@@ -93,44 +97,48 @@ def test_plot_1d_scene():
9397
plt.close()
9498

9599

96-
def test_plot_bounds():
97-
_ = SCENE_FULL.plot(x=0, hlim=[-0.45, 0.45])
100+
@pytest.mark.parametrize("swap_axes", [True, False])
101+
def test_plot_bounds(swap_axes):
102+
_ = SCENE_FULL.plot(x=0, hlim=[-0.45, 0.45], swap_axes=swap_axes)
98103
plt.close()
99-
_ = SCENE_FULL.plot(x=0, vlim=[-0.45, 0.45])
104+
_ = SCENE_FULL.plot(x=0, vlim=[-0.45, 0.45], swap_axes=swap_axes)
100105
plt.close()
101-
_ = SCENE_FULL.plot(x=0, hlim=[-0.45, 0.45], vlim=[-0.45, 0.45])
106+
_ = SCENE_FULL.plot(x=0, hlim=[-0.45, 0.45], vlim=[-0.45, 0.45], swap_axes=swap_axes)
102107
plt.close()
103108

104109

105-
def test_structure_alpha():
106-
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=None)
110+
@pytest.mark.parametrize("swap_axes", [True, False])
111+
def test_structure_alpha(swap_axes):
112+
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=None, swap_axes=swap_axes)
107113
plt.close()
108-
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=-1)
114+
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=-1, swap_axes=swap_axes)
109115
plt.close()
110-
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=1)
116+
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=1, swap_axes=swap_axes)
111117
plt.close()
112-
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=0.5)
118+
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=0.5, swap_axes=swap_axes)
113119
plt.close()
114-
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=0.5, cbar=True)
120+
_ = SCENE_FULL.plot_structures_eps(x=0, alpha=0.5, cbar=True, swap_axes=swap_axes)
115121
plt.close()
116122
new_structs = [
117123
td.Structure(geometry=s.geometry, medium=SCENE_FULL.medium) for s in SCENE_FULL.structures
118124
]
119125
S2 = SCENE_FULL.copy(update={"structures": new_structs})
120-
_ = S2.plot_structures_eps(x=0, alpha=0.5)
126+
_ = S2.plot_structures_eps(x=0, alpha=0.5, swap_axes=swap_axes)
121127
plt.close()
122128

123129

124-
def test_plot_with_units():
130+
@pytest.mark.parametrize("swap_axes", [True, False])
131+
def test_plot_with_units(swap_axes):
125132
scene_with_units = SCENE_FULL.updated_copy(plot_length_units="nm")
126-
scene_with_units.plot(x=-0.5)
133+
scene_with_units.plot(x=-0.5, swap_axes=swap_axes)
127134

128135

129-
def test_filter_structures():
136+
@pytest.mark.parametrize("swap_axes", [True, False])
137+
def test_filter_structures(swap_axes):
130138
s1 = td.Structure(geometry=td.Box(size=(1, 1, 1)), medium=SCENE.medium)
131139
s2 = td.Structure(geometry=td.Box(size=(1, 1, 1), center=(1, 1, 1)), medium=SCENE.medium)
132140
plane = td.Box(center=(0, 0, 1.5), size=(td.inf, td.inf, 0))
133-
SCENE._filter_structures_plane_medium(structures=[s1, s2], plane=plane)
141+
SCENE._filter_structures_plane_medium(structures=[s1, s2], plane=plane, swap_axes=swap_axes)
134142

135143

136144
def test_get_structure_plot_params():

tidy3d/components/geometry/polyslab.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -994,16 +994,11 @@ def _find_intersecting_ys_angle_slant(
994994
return ints_y_sort, ints_angle_sort
995995

996996
@cached_property
997-
def bounds(self, swap_axes: bool = False) -> Bound:
997+
def bounds(self) -> Bound:
998998
"""Returns bounding box min and max coordinates. The dilation and slant angle are not
999999
taken into account exactly for speed. Instead, the polygon may be slightly smaller than
10001000
the returned bounds, but it should always be fully contained.
10011001
1002-
Parameters
1003-
-------
1004-
swap_axes : bool = False
1005-
Optional: Swap the planar axes? (the axes perpendicular to self.axis)
1006-
10071002
Returns
10081003
-------
10091004
Tuple[float, float, float], Tuple[float, float float]
@@ -1036,8 +1031,8 @@ def bounds(self, swap_axes: bool = False) -> Bound:
10361031
zmin, zmax = self.slab_bounds
10371032

10381033
# rearrange axes
1039-
coords_min = self.unpop_axis_and_swap(zmin, (xmin, ymin), axis=self.axis, swap_axes=swap_axes)
1040-
coords_max = self.unpop_axis_and_swap(zmax, (xmax, ymax), axis=self.axis, swap_axes=swap_axes)
1034+
coords_min = self.unpop_axis(zmin, (xmin, ymin), axis=self.axis)
1035+
coords_max = self.unpop_axis(zmax, (xmax, ymax), axis=self.axis)
10411036
return (tuple(coords_min), tuple(coords_max))
10421037

10431038
def _extrusion_length_to_offset_distance(self, extrusion: float) -> float:

tidy3d/components/geometry/primitives.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ def _update_from_bounds(self, bounds: tuple[float, float], axis: Axis) -> Cylind
379379
@verify_packages_import(["trimesh"])
380380
def _do_intersections_tilted_plane(
381381
self, normal: Coordinate, origin: Coordinate, to_2D: MatrixReal4x4,
382-
swap_axes: bool = False,
383382
) -> list[Shapely]:
384383
"""Return a list of shapely geometries at the plane specified by normal and origin.
385384
@@ -391,8 +390,6 @@ def _do_intersections_tilted_plane(
391390
Vector defining the plane origin.
392391
to_2D : MatrixReal4x4
393392
Transformation matrix to apply to resulting shapes.
394-
swap_axes : bool = False
395-
Optional: Swap coordinates in the plane perpendicular to the normal vector?
396393
397394
Returns
398395
-------
@@ -403,7 +400,7 @@ def _do_intersections_tilted_plane(
403400
"""
404401
import trimesh
405402

406-
z0, (x0, y0) = self.pop_axis_and_swap(self.center, self.axis, swap_axes=swap_axes)
403+
z0, (x0, y0) = self.pop_axis(self.center, self.axis)
407404
half_length = self.finite_length_axis / 2
408405

409406
z_top = z0 + half_length
@@ -445,7 +442,7 @@ def _do_intersections_tilted_plane(
445442
x = np.hstack((x_bot, x_top))
446443
y = np.hstack((y_bot, y_top))
447444
z = np.hstack((np.full_like(x_bot, z_bot), np.full_like(x_top, z_top)))
448-
vertices = np.vstack(self.unpop_axis_and_swap(z, (x, y), self.axis, swap_axes=swap_axes)).T
445+
vertices = np.vstack(self.unpop_axis(z, (x, y), self.axis)).T
449446

450447
if x_bot.shape[0] == 1:
451448
m = 1

0 commit comments

Comments
 (0)