Skip to content

Commit 9d47a73

Browse files
committed
replaced (un)pop_axis() with (un)pop_axis_and_swap() everwhere where "swap_axis" is used
1 parent 4010018 commit 9d47a73

File tree

7 files changed

+115
-55
lines changed

7 files changed

+115
-55
lines changed

tidy3d/components/eme/simulation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,15 @@ def plot_eme_ports(
316316
rmax = self.geometry.bounds[1][self.axis]
317317
ports = np.array([rmin + self.port_offsets[0], rmax - self.port_offsets[1]])
318318
axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
319-
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis, swap_axes=swap_axes)
319+
_, (axis_x, axis_y) = self.pop_axis_and_swap([0, 1, 2], axis=axis, swap_axes=swap_axes)
320320
boundaries_x = []
321321
boundaries_y = []
322322
if axis_x == self.axis:
323323
boundaries_x = ports
324324
if axis_y == self.axis:
325325
boundaries_y = ports
326-
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis, swap_axes=swap_axes)
327-
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis, swap_axes=swap_axes)
326+
_, (xmin, ymin) = self.pop_axis_and_swap(self.simulation_bounds[0], axis=axis, swap_axes=swap_axes)
327+
_, (xmax, ymax) = self.pop_axis_and_swap(self.simulation_bounds[1], axis=axis, swap_axes=swap_axes)
328328
segs_x = [((bound, ymin), (bound, ymax)) for bound in boundaries_x]
329329
line_segments_x = mpl.collections.LineCollection(segs_x, **kwargs)
330330
segs_y = [((xmin, bound), (xmax, bound)) for bound in boundaries_y]
@@ -365,15 +365,15 @@ def plot_eme_subgrid_boundaries(
365365
subgrid_boundaries = np.array(eme_grid_spec.subgrid_boundaries)
366366
subgrids = eme_grid_spec.subgrids
367367
axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
368-
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis, swap_axes=swap_axes)
368+
_, (axis_x, axis_y) = self.pop_axis_and_swap([0, 1, 2], axis=axis, swap_axes=swap_axes)
369369
boundaries_x = []
370370
boundaries_y = []
371371
if axis_x == self.axis:
372372
boundaries_x = subgrid_boundaries
373373
if axis_y == self.axis:
374374
boundaries_y = subgrid_boundaries
375-
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis, swap_axes=swap_axes)
376-
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis, swap_axes=swap_axes)
375+
_, (xmin, ymin) = self.pop_axis_and_swap(self.simulation_bounds[0], axis=axis, swap_axes=swap_axes)
376+
_, (xmax, ymax) = self.pop_axis_and_swap(self.simulation_bounds[1], axis=axis, swap_axes=swap_axes)
377377
segs_x = [((bound, ymin), (bound, ymax)) for bound in boundaries_x]
378378
line_segments_x = mpl.collections.LineCollection(segs_x, **kwargs)
379379
segs_y = [((xmin, bound), (xmax, bound)) for bound in boundaries_y]
@@ -412,15 +412,15 @@ def plot_eme_grid(
412412
kwargs.setdefault("colors", "black")
413413
cell_boundaries = self.eme_grid.boundaries
414414
axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
415-
_, (axis_x, axis_y) = self.pop_axis([0, 1, 2], axis=axis, swap_axes=swap_axes)
415+
_, (axis_x, axis_y) = self.pop_axis_and_swap([0, 1, 2], axis=axis, swap_axes=swap_axes)
416416
boundaries_x = []
417417
boundaries_y = []
418418
if axis_x == self.axis:
419419
boundaries_x = cell_boundaries
420420
if axis_y == self.axis:
421421
boundaries_y = cell_boundaries
422-
_, (xmin, ymin) = self.pop_axis(self.simulation_bounds[0], axis=axis, swap_axes=swap_axes)
423-
_, (xmax, ymax) = self.pop_axis(self.simulation_bounds[1], axis=axis, swap_axes=swap_axes)
422+
_, (xmin, ymin) = self.pop_axis_and_swap(self.simulation_bounds[0], axis=axis, swap_axes=swap_axes)
423+
_, (xmax, ymax) = self.pop_axis_and_swap(self.simulation_bounds[1], axis=axis, swap_axes=swap_axes)
424424
segs_x = [((bound, ymin), (bound, ymax)) for bound in boundaries_x]
425425
line_segments_x = mpl.collections.LineCollection(segs_x, **kwargs)
426426
segs_y = [((xmin, bound), (xmax, bound)) for bound in boundaries_y]

tidy3d/components/geometry/base.py

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ def _pop_bounds(
462462
Packed as ``(zmin, zmax), ((xmin, ymin), (xmax, ymax))``.
463463
"""
464464
b_min, b_max = self.bounds
465-
zmin, (xmin, ymin) = self.pop_axis(b_min, axis=axis, swap_axes=swap_axes)
466-
zmax, (xmax, ymax) = self.pop_axis(b_max, axis=axis, swap_axes=swap_axes)
465+
zmin, (xmin, ymin) = self.pop_axis_and_swap(b_min, axis=axis, swap_axes=swap_axes)
466+
zmax, (xmax, ymax) = self.pop_axis_and_swap(b_max, axis=axis, swap_axes=swap_axes)
467467
return (zmin, zmax), ((xmin, ymin), (xmax, ymax))
468468

469469
@staticmethod
@@ -611,7 +611,7 @@ def _get_plot_labels(axis: Axis, swap_axes: bool = False) -> tuple[str, str]:
611611
str, str
612612
Labels of plot, packaged as ``(xlabel, ylabel)``.
613613
"""
614-
_, (xlabel, ylabel) = Geometry.pop_axis("xyz", axis=axis, swap_axes=swap_axes)
614+
_, (xlabel, ylabel) = Geometry.pop_axis_and_swap("xyz", axis=axis, swap_axes=swap_axes)
615615
return xlabel, ylabel
616616

617617
def _get_plot_limits(
@@ -734,7 +734,6 @@ def evaluate_inf_shape(shape: Shapely) -> Shapely:
734734
def pop_axis(
735735
coord: tuple[Any, Any, Any],
736736
axis: int,
737-
swap_axes: bool = False,
738737
) -> tuple[Any, tuple[Any, Any]]:
739738
"""Separates coordinate at ``axis`` index from coordinates on the plane tangent to ``axis``.
740739
@@ -744,8 +743,7 @@ def pop_axis(
744743
Tuple of three values in original coordinate system.
745744
axis : int
746745
Integer index into 'xyz' (0,1,2).
747-
swap_axes: bool = False
748-
Optional: Swap the order of the remaining two axes?
746+
749747
750748
Returns
751749
-------
@@ -756,16 +754,47 @@ def pop_axis(
756754
"""
757755
plane_vals = list(coord)
758756
axis_val = plane_vals.pop(axis)
759-
if swap_axes:
760-
plane_vals = [plane_vals[1], plane_vals[0]]
761757
return axis_val, tuple(plane_vals)
762758

759+
@staticmethod
760+
def pop_axis_and_swap(
761+
coord: tuple[Any, Any, Any],
762+
axis: int,
763+
swap_axes: bool = False,
764+
) -> tuple[Any, tuple[Any, Any]]:
765+
"""
766+
pop_axis_and_swap() is identical to pop_axis(), except that accepts an
767+
additional "swap_axes" argument which reverses the output orer. Examples:
768+
769+
pop_axis_and_swap(("x", "y", "z"), 1, swap_axes=False) -> "y", ("x", "z")
770+
pop_axis_and_swap(("x", "y", "z"), 1, swap_axes=True) -> "y", ("z", "x")
771+
772+
Parameters
773+
----------
774+
coord : Tuple[Any, Any, Any]
775+
Tuple of three values in original coordinate system.
776+
axis : int
777+
Integer index into 'xyz' (0,1,2).
778+
swap_axes: bool = False
779+
Optional: Swap the order of the data from the two remaining axes in the output tuple?
780+
781+
Returns
782+
-------
783+
Any, Tuple[Any, Any]
784+
The input coordinates are separated into the one along the axis provided
785+
and the two on the planar coordinates,
786+
like ``axis_coord, (planar_coord1, planar_coord2)``.
787+
"""
788+
axis_val, plane_vals = Geometry.pop_axis(coord, axis)
789+
if swap_axes:
790+
return axis_val, (plane_vals[1], plane_vals[0])
791+
return axis_val, plane_vals
792+
763793
@staticmethod
764794
def unpop_axis(
765795
ax_coord: Any,
766796
plane_coords: tuple[Any, Any],
767797
axis: int,
768-
swap_axes: bool = False,
769798
) -> tuple[Any, Any, Any]:
770799
"""Combine coordinate along axis with coordinates on the plane tangent to the axis.
771800
@@ -777,23 +806,55 @@ def unpop_axis(
777806
Values along ordered planar directions.
778807
axis : int
779808
Integer index into 'xyz' (0,1,2).
780-
swap_axes: bool = False
781-
Optional: Swap the order of the entries in plane_coords[]?
782-
NOTE: If `axis, plane_coords = pop_axis(coords, axis, transpose)`,
783-
then, if you want to get back the original `coords`, you should use:
784-
`unpop_axis(ax_coord, plane_coords, axis, transpose)`
785809
786810
Returns
787811
-------
788812
Tuple[Any, Any, Any]
789813
The three values in the xyz coordinate system.
790814
"""
791815
coords = list(plane_coords)
792-
if swap_axes:
793-
coords = [coords[1], coords[0]]
794816
coords.insert(axis, ax_coord)
795817
return tuple(coords)
796818

819+
@staticmethod
820+
def unpop_axis_and_swap(
821+
ax_coord: Any,
822+
plane_coords: tuple[Any, Any],
823+
axis: int,
824+
swap_axes: bool = False,
825+
) -> tuple[Any, Any, Any]:
826+
"""
827+
unpop_axis_and_swap() is identical to unpop_axis(), except that accepts
828+
an additional "swap_axes" argument which reverses the order of
829+
plane_coords before sending them to unpop_axis(). For example:
830+
831+
unpop_axis_and_swap("y", ("x", "z"), 1, swap_axes=False) --> ("x", "y", "z")
832+
unpop_axis_and_swap("y", ("x", "z"), 1, swap_axes=True) --> ("z", "y", "x")
833+
834+
This function is the inverse of pop_axis_and_swap(). For example:
835+
unpop_axis_and_swap("y", ("z", "x"), 1, swap_axes=True) --> ("x", "y", "z")
836+
837+
Parameters
838+
----------
839+
ax_coord : Any
840+
Value along axis direction.
841+
plane_coords : Tuple[Any, Any]
842+
Values along ordered planar directions.
843+
axis : int
844+
Integer index into 'xyz' (0,1,2).
845+
swap_axes: bool = False
846+
Optional: Swap the order of the entries in plane_coords[]?
847+
848+
Returns
849+
-------
850+
Tuple[Any, Any, Any]
851+
The three values in the xyz coordinate system.
852+
"""
853+
coords = plane_coords
854+
if swap_axes:
855+
coords = (coords[1], coords[0])
856+
return Geometry.unpop_axis(ax_coord, coords, axis)
857+
797858
@staticmethod
798859
def parse_xyz_kwargs(**xyz) -> tuple[Axis, float]:
799860
"""Turns x,y,z kwargs into index of the normal axis and position along that axis.
@@ -1813,7 +1874,7 @@ def _order_by_axis(
18131874
"""
18141875
vals = 3 * [plane_val]
18151876
vals[self.axis] = axis_val
1816-
_, (val_x, val_y) = self.pop_axis(vals, axis=axis, swap_axes=swap_axes)
1877+
_, (val_x, val_y) = self.pop_axis_and_swap(vals, axis=axis, swap_axes=swap_axes)
18171878
return val_x, val_y
18181879

18191880
@cached_property
@@ -2107,8 +2168,8 @@ def intersections_plane(
21072168
axis, position = self.parse_xyz_kwargs(x=x, y=y, z=z)
21082169
if not self.intersects_axis_position(axis, position):
21092170
return []
2110-
z0, (x0, y0) = self.pop_axis(self.center, axis=axis, swap_axes=swap_axes)
2111-
Lz, (Lx, Ly) = self.pop_axis(self.size, axis=axis, swap_axes=swap_axes)
2171+
z0, (x0, y0) = self.pop_axis_and_swap(self.center, axis=axis, swap_axes=swap_axes)
2172+
Lz, (Lx, Ly) = self.pop_axis_and_swap(self.size, axis=axis, swap_axes=swap_axes)
21122173
dz = np.abs(z0 - position)
21132174
if dz > Lz / 2 + fp_eps:
21142175
return []
@@ -2183,7 +2244,7 @@ def intersections_with(self, other, swap_axes: bool = False):
21832244
shapes_plane = other.intersections_plane(**xyz_kwargs, swap_axes=swap_axes)
21842245

21852246
# intersect all shapes with the input self
2186-
bs_min, bs_max = (self.pop_axis(bounds, axis=normal_ind, swap_axes=swap_axes)[1] for bounds in self.bounds)
2247+
bs_min, bs_max = (self.pop_axis_and_swap(bounds, axis=normal_ind, swap_axes=swap_axes)[1] for bounds in self.bounds)
21872248

21882249
shapely_box = self.make_shapely_box(bs_min[0], bs_min[1], bs_max[0], bs_max[1])
21892250
shapely_box = Geometry.evaluate_inf_shape(shapely_box)
@@ -2285,7 +2346,7 @@ def _plot_arrow(
22852346
"""
22862347

22872348
plot_axis, _ = self.parse_xyz_kwargs(x=x, y=y, z=z)
2288-
_, (dx, dy) = self.pop_axis(direction, axis=plot_axis, swap_axes=swap_axes)
2349+
_, (dx, dy) = self.pop_axis_and_swap(direction, axis=plot_axis, swap_axes=swap_axes)
22892350

22902351
# conditions to check to determine whether to plot arrow, taking into account the
22912352
# possibility of a custom arrow base
@@ -2297,12 +2358,12 @@ def _plot_arrow(
22972358
)
22982359
center = arrow_base
22992360

2300-
_, (dx, dy) = self.pop_axis(direction, axis=plot_axis, swap_axes=swap_axes)
2361+
_, (dx, dy) = self.pop_axis_and_swap(direction, axis=plot_axis, swap_axes=swap_axes)
23012362
components_in_plane = any(not np.isclose(component, 0) for component in (dx, dy))
23022363

23032364
# plot if arrow in plotting plane and some non-zero component can be displayed.
23042365
if arrow_intersecting_plane and components_in_plane:
2305-
_, (x0, y0) = self.pop_axis(center, axis=plot_axis, swap_axes=swap_axes)
2366+
_, (x0, y0) = self.pop_axis_and_swap(center, axis=plot_axis, swap_axes=swap_axes)
23062367

23072368
# Reasonable value for temporary arrow size. The correct size and direction
23082369
# have to be calculated after all transforms have been set. That is why we

tidy3d/components/geometry/mesh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ def intersections_plane(
604604
# permute so normal is aligned with z axis
605605
# and (y, z), (x, z), resp. (x, y) are aligned with (x, y)
606606
identity = np.eye(3)
607-
permutation = self.unpop_axis(identity[2], identity[0:2], axis=axis, swap_axes=swap_axes)
607+
permutation = self.unpop_axis_and_swap(identity[2], identity[0:2], axis=axis, swap_axes=swap_axes)
608608
mapping[:3, :3] = np.array(permutation).T
609609

610610
section2d, _ = section.to_planar(to_2D=mapping)

tidy3d/components/geometry/polyslab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,8 @@ def bounds(self, swap_axes: bool = False) -> Bound:
10351035
zmin, zmax = self.slab_bounds
10361036

10371037
# rearrange axes
1038-
coords_min = self.unpop_axis(zmin, (xmin, ymin), axis=self.axis, swap_axes=swap_axes)
1039-
coords_max = self.unpop_axis(zmax, (xmax, ymax), axis=self.axis, swap_axes=swap_axes)
1038+
coords_min = self.unpop_axis_and_swap(zmin, (xmin, ymin), axis=self.axis, swap_axes=swap_axes)
1039+
coords_max = self.unpop_axis_and_swap(zmax, (xmax, ymax), axis=self.axis, swap_axes=swap_axes)
10401040
return (tuple(coords_min), tuple(coords_max))
10411041

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

tidy3d/components/geometry/primitives.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def intersections_plane(
141141
axis, position = self.parse_xyz_kwargs(x=x, y=y, z=z)
142142
if not self.intersects_axis_position(axis, position):
143143
return []
144-
z0, (x0, y0) = self.pop_axis(self.center, axis=axis, swap_axes=swap_axes)
144+
z0, (x0, y0) = self.pop_axis_and_swap(self.center, axis=axis, swap_axes=swap_axes)
145145
intersect_dist = self._intersect_dist(position, z0)
146146
if not intersect_dist:
147147
return []
@@ -403,7 +403,7 @@ def _do_intersections_tilted_plane(
403403
"""
404404
import trimesh
405405

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

409409
z_top = z0 + half_length
@@ -445,7 +445,7 @@ def _do_intersections_tilted_plane(
445445
x = np.hstack((x_bot, x_top))
446446
y = np.hstack((y_bot, y_top))
447447
z = np.hstack((np.full_like(x_bot, z_bot), np.full_like(x_top, z_top)))
448-
vertices = np.vstack(self.unpop_axis(z, (x, y), self.axis, swap_axes=swap_axes)).T
448+
vertices = np.vstack(self.unpop_axis_and_swap(z, (x, y), self.axis, swap_axes=swap_axes)).T
449449

450450
if x_bot.shape[0] == 1:
451451
m = 1
@@ -501,8 +501,7 @@ def _intersections_normal(self, z: float, swap_axes: bool = False):
501501

502502
if radius_offset <= 0:
503503
return []
504-
505-
_, (x0, y0) = self.pop_axis(static_self.center, axis=self.axis, swap_axes=swap_axes)
504+
_, (x0, y0) = self.pop_axis_and_swap(static_self.center, axis=self.axis, swap_axes=swap_axes)
506505
return [shapely.Point(x0, y0).buffer(radius_offset, quad_segs=_N_SHAPELY_QUAD_SEGS)]
507506

508507
def _intersections_side(self, position, axis, swap_axes: bool = False):
@@ -786,5 +785,5 @@ def _local_to_global_side_cross_section(
786785
axis=axis,
787786
swap_axes=swap_axes,
788787
)
789-
_, (x_center, y_center) = self.pop_axis(self.center, axis=axis, swap_axes=swap_axes)
788+
_, (x_center, y_center) = self.pop_axis_and_swap(self.center, axis=axis, swap_axes=swap_axes)
790789
return [x_center + lx_offset, y_center + ly_offset]

tidy3d/components/scene.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ def _get_plot_lims(
372372
) -> tuple[tuple[float, float], tuple[float, float]]:
373373
# if no hlim and/or vlim given, the bounds will then be the usual pml bounds
374374
axis, _ = Box.parse_xyz_kwargs(x=x, y=y, z=z)
375-
_, (hmin, vmin) = Box.pop_axis(bounds[0], axis=axis, swap_axes=swap_axes)
376-
_, (hmax, vmax) = Box.pop_axis(bounds[1], axis=axis, swap_axes=swap_axes)
375+
_, (hmin, vmin) = Box.pop_axis_and_swap(bounds[0], axis=axis, swap_axes=swap_axes)
376+
_, (hmax, vmax) = Box.pop_axis_and_swap(bounds[1], axis=axis, swap_axes=swap_axes)
377377

378378
# account for unordered limits
379379
if hlim is None:
@@ -647,8 +647,8 @@ def _get_structures_2dbox(
647647
"""
648648
# if no hlim and/or vlim given, the bounds will then be the usual pml bounds
649649
axis, _ = Box.parse_xyz_kwargs(x=x, y=y, z=z)
650-
_, (hmin, vmin) = Box.pop_axis(self.bounds[0], axis=axis, swap_axes=swap_axes)
651-
_, (hmax, vmax) = Box.pop_axis(self.bounds[1], axis=axis, swap_axes=swap_axes)
650+
_, (hmin, vmin) = Box.pop_axis_and_swap(self.bounds[0], axis=axis, swap_axes=swap_axes)
651+
_, (hmax, vmax) = Box.pop_axis_and_swap(self.bounds[1], axis=axis, swap_axes=swap_axes)
652652

653653
if hlim is not None:
654654
(hmin, hmax) = hlim
@@ -662,8 +662,8 @@ def _get_structures_2dbox(
662662
v_size = (vmax - vmin) or inf
663663

664664
axis, center_normal = Box.parse_xyz_kwargs(x=x, y=y, z=z)
665-
center = Box.unpop_axis(center_normal, (h_center, v_center), axis=axis, swap_axes=swap_axes)
666-
size = Box.unpop_axis(0.0, (h_size, v_size), axis=axis, swap_axes=swap_axes)
665+
center = Box.unpop_axis_and_swap(center_normal, (h_center, v_center), axis=axis, swap_axes=swap_axes)
666+
size = Box.unpop_axis_and_swap(0.0, (h_size, v_size), axis=axis, swap_axes=swap_axes)
667667
plane = Box(center=center, size=size)
668668

669669
medium_shapes = []

0 commit comments

Comments
 (0)