Skip to content

XY transposable plots V3 part 2: Updated all remaining plot functions (issue1072) #2544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: jewettaijfc/issue1072_v3_part1_geom
Choose a base branch
from

Conversation

jewettaijfc
Copy link
Contributor

@jewettaijfc jewettaijfc commented Jun 5, 2025

This PR addresses the feature request from issue 1072, adding the ability to swap the horizontal and vertical axes of all Tidy3D plots.

Reviewer instructions

This PR has been based on top of PR2687. You must land that PR before this one.

Automatic Tests

poetry run pytest tests

Manual Tests

  • Open any of the tidy3d-notebook examples with jupyter.
  • Evaluate the cells to verify that the plots using transpose=True are flipped (with respect to the plots that omit the transpose argument).

I have tested this PR with over 150 examples (out of 191). It's possible that some advanced plot types which are not covered in these examples may have visual glitches or don't work when transpose=True.

But all plots that display simulations, scenes, structures, sources, monitors work with transpose=True. (Including geometry transformations and clipping.)

@jewettaijfc

This comment was marked as resolved.

@tylerflex

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc jewettaijfc changed the title Jewettaijfc/issue1072 v2 XY transposable sim.plot() plots (issue1072) VERSION 2 Jun 6, 2025
@jewettaijfc

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc jewettaijfc self-assigned this Jun 8, 2025
@jewettaijfc

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc jewettaijfc force-pushed the jewettaijfc/issue1072_v2 branch from 2f38175 to de2d14d Compare June 9, 2025 15:29
@jewettaijfc

This comment was marked as resolved.

@jewettaijfc

This comment was marked as resolved.

@jewettaijfc jewettaijfc force-pushed the jewettaijfc/issue1072_v2 branch from 2510f60 to f0995ca Compare June 10, 2025 18:32
@jewettaijfc

This comment was marked as resolved.

@tylerflex

This comment was marked as resolved.

@jewettaijfc jewettaijfc marked this pull request as ready for review June 11, 2025 04:10
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Implements axis transposition functionality across Tidy3D's visualization system by adding a transpose parameter to plot-related functions.

  • Core implementation centers on new pop_axis_and_swap() and unpop_axis_and_swap() functions in geometry/base.py that handle coordinate transformations
  • Systematically propagates transpose parameter through all plotting functions that accept axis arguments (x,y,z)
  • Notable update to intersections_tilted_plane() in geometry/base.py contains potential bug where X,Y axis order ignores normal direction
  • Comprehensive test coverage added across components including parameterized tests for both transpose=True/False cases
  • Clean implementation using strategy of swapping geometry/limits pre-matplotlib rather than post-rendering axis manipulation

25 files reviewed, 3 comments
Edit PR Review Bot Settings | Greptile

This comment was marked as outdated.

Copy link
Collaborator

@tylerflex tylerflex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! Just minor comments for now. You can focus on clearing these up + those from the new automated review bot 😆 and then request me again for another review.

Let's have @yaugenst-flex also take a look when he's back.

One thing I think we should do also before merging is pick a handful of existing notebooks at random (maybe 5-10) and just throw a transpose=True in to test this a bit more in some real world situations.

Thanks @jewettaijfc !

@yaugenst-flex yaugenst-flex self-requested a review June 12, 2025 07:26
@yaugenst-flex yaugenst-flex linked an issue Jun 12, 2025 that may be closed by this pull request
12 tasks
@jewettaijfc jewettaijfc force-pushed the jewettaijfc/issue1072_v2 branch from 8e2434a to f9b57ba Compare June 12, 2025 18:46
@jewettaijfc
Copy link
Contributor Author

jewettaijfc commented Jul 23, 2025

So many changes have been made since Greptile evaluated this code that it's comments are no longer relevant. So I hid those comments.

# only then overlay the mesh plot
field_data.plot(ax=ax, cmap=False, field=False, grid=True)
Copy link

@jewettaij jewettaij Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is field_data is, or which function is being invoked by this line. And I could not find an example of a plot() function in Tidy3D which accepts cmap=False as an argument (usually cmap is a string). So I removed this argument when I invoked this function (on line 206).

Momchil wrote the original version of this function. Whoever ends up reviewing this code should reach out to him for clarification. He was on PTO at the time I submitted this PR (on my last day).

func_name="plot_mesh",
arg="transpose",
val="True",
)
Copy link

@jewettaij jewettaij Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of all the functions I modified for this PR, plot_mesh() is the function I have the most concerns about.

The changes I made to this file should be moved into a different PR which can be tested separately.

I did not find any tidy3d-notebook examples which invoke this plot_mesh() function. (However I think it is covered by the unit tests.)

(Also see my comment below on line 195.)

@jewettaij
Copy link

jewettaij commented Jul 25, 2025

SUMMARY

I mentioned this in my later comments, but here's a summary:

I have tested this PR by successfully running 150 of the examples from tidy3d-notebooks using transpose=True (when possible).

Unfortunately, the examples I tried DID NOT invoke following functions that I modified in this PR:

All of those functions are untested (visually with transpose=True).
Also:

  • Scene.plot_structure_property() function has been tested (in components/scene.py). But in some scenarios, it will invoke _pcolormesh_shape_custom_medium_structure_eps() (listed above). So those scenarios need to be tested.

You may want to remove those changes from this PR and move them into a followup PR.

@@ -244,7 +246,7 @@ def plot_sim_eps(
mode_source_0 = self.to_source(port=port_source, mode_index=0)
plot_sources.append(mode_source_0)
sim_plot = self.simulation.copy(update={"sources": plot_sources})
return sim_plot.plot_eps(x=x, y=y, z=z, ax=ax, **kwargs)
return sim_plot.plot_eps(x=x, y=y, z=z, ax=ax, transpose=transpose, **kwargs)

Copy link

@jewettaij jewettaij Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I visually verified that the plot_eps() function works with transpose=True using the VizSimulation.ipynb notebook example.

@@ -217,6 +217,7 @@ def plot_sim(
y: Optional[float] = None,
z: Optional[float] = None,
ax: Ax = None,
transpose: bool = False,
Copy link

@jewettaij jewettaij Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I visually verified that plot_sim() function works with transpose=True using the SMatrix.ipynb notebook example.

warn_untested_argument(
cls_name=type(self).__name__, func_name="plot", arg="transpose", val="True"
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has not yet been tested with transpose=True visually. I suggest moving changes to this function out of this PR.

@@ -989,6 +1005,7 @@ def plot_structures_eps(
cbar=cbar,
reverse=reverse,
ax=ax,
transpose=transpose,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my memory is correct, Scene.plot_structures_eps() (invoked by this function) was tested visually (with transpose=True) using the ThermallyTunedRingResonator.ipynb notebook example.
You might want to verify it again, however.

# Please remove this warning once someone has verified that `transpose=True` works.
warn_untested_argument(
cls_name=type(self).__name__, func_name="plot", arg="transpose", val="True"
)
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested code!
The changes I made to this file should be moved into a different PR which can be tested separately.

I added this warning because I honestly can't remember if I tested the plot functions in this plugin with transpose=True.

Try running the cells in the CharacteristicImpedanceCalculator.ipynb (with transpose=True) to see if they work as expected.

warn_untested_argument(
cls_name=type(self).__name__, func_name="plot", arg="transpose", val="True"
)

Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested code!
The changes I made to this file should be moved into a different PR which can be tested separately.

I added this warning because I honestly can't remember if I tested the plot functions in this plugin with transpose=True. (See previous comment.)

# Please remove this warning once someone has verified that `transpose=True` works.
warn_untested_argument(
cls_name=type(self).__name__, func_name="plot", arg="transpose", val="True"
)
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested code!
The changes I made to this file should be moved into a different PR which can be tested separately.

I added this warning because I honestly can't remember if I tested the plot functions in this plugin with transpose=True. (See previous comment.)

warn_untested_argument(
cls_name=type(self).__name__, func_name="plot", arg="transpose", val="True"
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

@@ -306,6 +307,7 @@ def plot_eme_ports(
ax: Ax = None,
hlim: Optional[tuple[float, float]] = None,
vlim: Optional[tuple[float, float]] = None,
transpose: bool = False,
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested!
I could not find an example from tidy3d-notebooks which tests this function.

The changes to this function should be moved to a different PR.

Incidentally, I did run the EMEBends.ipynb and EMESolver.ipynb examples with transpose=True, but I don't think it tests the functions in this file.

@@ -350,6 +363,7 @@ def plot_eme_subgrid_boundaries(
ax: Ax = None,
hlim: Optional[tuple[float, float]] = None,
vlim: Optional[tuple[float, float]] = None,
transpose: bool = False,
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested!
I could not find an example from tidy3d-notebooks which tests this function.

The changes to this file should be moved to a different PR.

@@ -402,22 +435,27 @@ def plot_eme_grid(
ax: Ax = None,
hlim: Optional[tuple[float, float]] = None,
vlim: Optional[tuple[float, float]] = None,
transpose: bool = False,
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested!
I could not find an example from tidy3d-notebooks which tests this function.

The changes to this file should be moved to a different PR.

@@ -445,6 +490,7 @@ def plot(
monitor_alpha: Optional[float] = None,
hlim: Optional[tuple[float, float]] = None,
vlim: Optional[tuple[float, float]] = None,
transpose: bool = False,
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested!
I could not find an example from tidy3d-notebooks which tests this function.

The changes to this file should be moved to a different PR.

arg="transpose",
val="True",
)

Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested code!

As mentioned earlier:

  • I could not find an example in tidy3d-notebooks which tests _pcolormesh_shape_custom_medium_structure_eps(), so I had no way to visually confirm that this function is working (when transpose=True).
  • For comparison, _pcolormesh_shape_doping_box() has been tested (visually) using one of the tidy3d-notebook examples.

Perhaps the changes I made to this function should be moved into a different PR which can be tested separately. (See comment above.)

ax,
property,
transpose=transpose,
transpose_shape=False, # We took care of transposing the shape earlier.
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that the function behaves correctly when it reaches this line and invokes _pcolormesh_shape_doping_box().

@@ -993,10 +1084,22 @@ def plot_structures_property(
shape=shape,
ax=ax,
property="doping",
# Note: I omitted `transpose` because we took care of that earlier.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that the function behaves correctly when it reaches this line and invokes _plot_shape_structure_heat_charge_property().

@@ -50,6 +50,7 @@ def plot_field(
vmin: Optional[float] = None,
vmax: Optional[float] = None,
ax: Ax = None,
transpose: bool = False,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic plot_field() function works and has been tested in at least 20 different examples from tidy3d-notebooks. (I cannot remember their names.)

shape_swap_xy,
unpop_axis_and_swap,
warn_untested_argument,
)
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains untested code

Almost all of the changes I made to scene.py have been tested visually, by running over 150 examples from tidy3d-notebooks, and adding the transpose=True argument to the various plot functions. All of them are working (when transpose=True), including:

However the Scene.plot_structures_property() function is huge and has a lot of conditional branching. One of those conditional branches was never reachable by any of the tidy3d-notebook examples I tried.

(See the comment I left below next to the _pcolormesh_shape_custom_medium_structure_eps() function for details.)

So you may wish to postpone this PR until that condition has been tested. (I am sorry I did not do this before I left.)

@@ -1031,6 +1135,8 @@ def plot_structures_property(
ax,
grid,
eps_component=eps_component,
transpose=transpose,
transpose_shape=False, # We took care of transposing the "shape" earlier.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested code!
As I mentioned earlier, I have not found an example which invokes plot_structures_property() in a way which tests this particular condition. (I tried 150 examples from tidy3d-notebooks.

The other conditions have tested and are working.

Perhaps the changes to this function could be postponed to another PR until this is tested).

Alternatively, perhaps this plot_structures_property() function needs to be cleaned up. It is really complicated and messy.

@@ -539,6 +539,7 @@ def plot(
monitor_alpha: Optional[float] = None,
hlim: Optional[tuple[float, float]] = None,
vlim: Optional[tuple[float, float]] = None,
transpose: bool = False,
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I visually verified transpose=True works in all of the Adjoint examples from tidy3d-notebooks.

@@ -81,6 +81,7 @@
plot_params_heat_source,
)
from tidy3d.components.types import TYPE_TAG_STR, Ax, Bound, ScalarSymmetry, Shapely, annotate_type
from tidy3d.components.utils import shape_swap_xy
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test the changes in these heat.py and heat_charge.py files by verifying that following tidy3d-notebook examples work with transpose=True:

I visually verified that these examples work with transpose=True.

@@ -74,7 +74,8 @@ def cell_sizes(self) -> SpatialDataArray:
@cached_property
def cell_size_meshgrid(self):
"""Returns an N-dimensional grid where N is the number of coordinate arrays that have more than one
element. Each grid element corresponds to the size of the mesh cell in N-dimensions and 1 for N=0."""
element. Each grid element corresponds to the size of the mesh cell in N-dimensions and 1 for N=0.
"""
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was accidental.

@@ -55,6 +55,7 @@
PlotScale,
Symmetry,
)
from tidy3d.components.utils import pop_axis_and_swap
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to the files in the mode/ folder were tested by visually confirming that transpose=True in the following tidy3d-notebook examples:

@@ -358,12 +358,13 @@ def plot(
y: Optional[float] = None,
z: Optional[float] = None,
ax: Ax = None,
transpose: bool = False,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that the changes in this file work by verifying transpose=True works in VizSimulation.ipynb (and many other tidy3d-notebook examples).

@@ -74,14 +74,15 @@ def plot(
y: Optional[float] = None,
z: Optional[float] = None,
ax: Ax = None,
transpose: bool = False,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that the changes in this file work by verifying transpose=True works in VizSimulation.ipynb (and many other tidy3d-notebook examples).

**patch_kwargs,
) -> Ax:
# call Source.plot but with the base of the arrow centered on the injection plane
patch_kwargs["arrow_base"] = self.injection_plane_center
ax = Source.plot(self, x=x, y=y, z=z, ax=ax, **patch_kwargs)
ax = Source.plot(self, x=x, y=y, z=z, ax=ax, transpose=transpose, **patch_kwargs)
Copy link

@jewettaij jewettaij Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested code!

After releasing this PR, I realize that I never got around to testing this function. But you can! Here are the relevant examples from tidy3d-notebooks:

Try running the plot functions in these examples with the transpose=True argument and check visually that the horizontal and vertical axes are swapped.

@@ -35,7 +35,8 @@ class SlotboomBandGapNarrowing(Tidy3dBaseModel):
... min_N=1e15,
... )

.. [1] 'UNIFIED APPARENT BANDGAP NARROWING IN n- AND p-TYPE SILICON' Solid-State Electronics Vol. 35, No. 2, pp. 125-129, 1992"""
.. [1] 'UNIFIED APPARENT BANDGAP NARROWING IN n- AND p-TYPE SILICON' Solid-State Electronics Vol. 35, No. 2, pp. 125-129, 1992

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was accidental.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to transpose plot
3 participants