Thanks to visit codestin.com
Credit goes to github.com

Skip to content

implementing interpolation option for plot_field method #1964

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

Merged
merged 1 commit into from
Oct 27, 2024

Conversation

FilipeFcp
Copy link
Contributor

@FilipeFcp FilipeFcp commented Sep 19, 2024

Hi @tylerflex,

I’ve implemented the interpolation option. Here's how I approached it:

The default is set to False, so it generates a regular plot.
If set to True, it interpolates with 200 x 200 points.
If a tuple (a, b) is provided, it interpolates with a x b points.
If an int a is provided, it interpolates with a x a points.
I added an assertion to prevent users from interpolating more than 1000 points in any dimension to avoid potential notebook crashes due to memory limitations.

Let me know if this approach is too complex; I can simplify it if needed, like just True or False.

Fixes #1952

@FilipeFcp FilipeFcp requested a review from tylerflex September 19, 2024 02:35
@tylerflex
Copy link
Collaborator

tylerflex commented Sep 19, 2024

Hi @FilipeFcp Thanks for this!
The interpolation approach makes some sense, but also introduces these new parameters (like number of points). I wonder if instead we should include a way to downsample the fields?

In xarray there is a convenient way to grab every n data point by using slice
https://docs.xarray.dev/en/latest/user-guide/indexing.html

>>> import xarray as xr
>>> da = xr.DataArray([1, 2, 3], [("x", [0, 1, 2])])
>>> da.isel(x=slice(None, None, 2))
<xarray.DataArray (x: 2)> Size: 16B
array([1, 3])
Coordinates:
  * x        (x) int64 16B 0 2

perhaps we could instead allow the user to input the n for slice(None, None, n) (with default of 1)? and use that to downsample in all directions?

Just a thought let me know how you think that would work or if you prefer the interpolation approach

@FilipeFcp
Copy link
Contributor Author

What I had in mind was a way to add more data points, making it easier to create smoother visualizations. For example, moving from this:

image

To this:

image

I understand that the slice option is useful for quickly plotting a large dataset, but could it be used to generate a smoother image? However, if users are most likely looking to downsample datasets before plotting rather than generating more points for visualization purposes, the slice option would make more sense.

@tylerflex
Copy link
Collaborator

tylerflex commented Sep 24, 2024

ah ok, I misunderstood. Maybe in the same sprit, it could be nice if the interpolation argument was a float = 1 that just specified the ratio of points in the interpolation coords to the number of points in the raw data? for example nterpolation=2 would mean we sample the points at 2x resolution of the supplied data. Would this make sense?

The only thing about the current approach I think is not ideal is that it requires the hardcoding of these values (200x200) whereas the data might have a different aspect ratio, so it could lead to weird results.

Are there other examples of interpolation in plotting and what do they do?

Also, could you include a changelog item under Added for the final PR? thanks

@FilipeFcp FilipeFcp force-pushed the filipe/plot_fields branch 2 times, most recently from 0352ec0 to f2b2141 Compare September 27, 2024 19:53
@FilipeFcp
Copy link
Contributor Author

Hi @tylerflex, your float approach is actually way better.

Ideally, the user could be able to pass the shading argument for plotting, but it seems to be bugged at the moment: pydata/xarray#3002.

@tylerflex
Copy link
Collaborator

looks like they could also try this?

pydata/xarray#3002 (comment)

@FilipeFcp
Copy link
Contributor Author

Oh... I saw this comment, but I tried with infer_interval = True not False. Yes, it works with False.

I think we can pass the shading argument through - plot_field -> plot_field_monitor_data -> plot_scalar_array -> field_data.plot. Or create a **args in plot_scalar_array and input it into field_data.plot.

@tylerflex
Copy link
Collaborator

Oh... I saw this comment, but I tried with infer_interval = True not False. Yes, it works with False.

Great

I think we can pass the shading argument through - plot_field -> plot_field_monitor_data -> plot_scalar_array -> field_data.plot. Or create a **args in plot_scalar_array and input it into field_data.plot.

yea I think either a shading argument or just exploring all of the **kwargs for xarray would work. I dont have a strong preference. what do you think is better? will this be used often you think?

@tylerflex
Copy link
Collaborator

Hi @FilipeFcp sorry I let this slide, what's the current status of it?

I would say that the current implementation looks pretty good, but we should also add one test, just to make sure calling this with a value of, say 2, will work? and also adding a CHANGLOG item mentioning the new feature

Thanks again!

@FilipeFcp
Copy link
Contributor Author

Hi @tylerflex,

It was my bad. I had a lot of meetings and other tasks when Tom was in China, and I forgot about this for a while.

If you don't mind, I’ve pushed a different version that passes the shading argument. It seems simpler, though we lost the option to reduce the data for plotting. It was ready a month ago, just pending a review and push.

Let me know which implementation you think is better, and I’ll add it to the changelog.

Thank you.

@tylerflex
Copy link
Collaborator

It was my bad. I had a lot of meetings and other tasks when Tom was in China, and I forgot about this for a while.

No worries at all. I also forgeot.

If you don't mind, I’ve pushed a different version that passes the shading argument. It seems simpler, though we lost the option to reduce the data for plotting. It was ready a month ago, just pending a review and push.

Yea this seems great I like it better since it's more simple and adds less complication.

Let me know which implementation you think is better, and I’ll add it to the changelog.

That would be great when you get around to it, thanks!

@FilipeFcp
Copy link
Contributor Author

Ok, I think is done.
Let me know if there is anything to improve.

Thank you!

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.

Please modify a test showing the new shading option passed to plot_field. You can see some examples here

https://github.com/flexcompute/tidy3d/blob/develop/tests/test_data/test_sim_data.py#L139-L145

CHANGELOG.md Outdated
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Users can pass the `shading` argument in the `SimulationData.plot_field` to `Xarray.plot` method. When shading='gouraud', the image is interpolated, producing a smoother visualization.
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you put shading='gouraud' into the backslash quotes?

@tylerflex tylerflex merged commit e924074 into develop Oct 27, 2024
15 checks passed
@tylerflex tylerflex deleted the filipe/plot_fields branch October 27, 2024 16:12
@tylerflex
Copy link
Collaborator

Thanks @FilipeFcp !

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 interpolation option for SimulationData.plot_field method.
2 participants