Skip to content
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

Validate that custom datasets can interpolate #1684

Merged
merged 1 commit into from
Jun 3, 2024

Conversation

marc-flex
Copy link
Contributor

This PR addresses issue #1668

@marc-flex marc-flex added the 2.7 will go into version 2.7.* label May 7, 2024
@marc-flex marc-flex self-assigned this May 7, 2024
@marc-flex
Copy link
Contributor Author

All I've done so far is to introduce a validator that checks that data can be interpolated in each of the dimensions.

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.

Thanks @marc-flex I think this is looking pretty good, just a few minor tweaks and also we should see if we want to apply this validator to all DataArray objects (as written currently) or rather as a Simulation post-init validator that loops over sources and does the interp check. I'm curious to see if the front end tests pass with these changes or if there are instances where we explicitly allow duplicate coords in some data array objects.

tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
@tylerflex
Copy link
Collaborator

Also, could you please add a few tests to tests/test_data/test_data_array.py? Basically try to replicate a few scenarios and edge cases for this behavior and ensure that the proper thing happens. Thanks

@marc-flex
Copy link
Contributor Author

Thanks @marc-flex I think this is looking pretty good, just a few minor tweaks and also we should see if we want to apply this validator to all DataArray objects (as written currently) or rather as a Simulation post-init validator that loops over sources and does the interp check. I'm curious to see if the front end tests pass with these changes or if there are instances where we explicitly allow duplicate coords in some data array objects.

You're right. This doesn't pass front-end test. I'll give it another go with all your comments and restricting the check to custom fields and sources.

@tylerflex
Copy link
Collaborator

It might just be failing because of direction coords. but the fix I suggested about isel-ing the first coordinate might fix them, so perhaps try that first,

@tylerflex
Copy link
Collaborator

Or it could just be that some of the front end test data arrays have extra coordinates (by accident)

@momchil-flex
Copy link
Collaborator

Thanks @marc-flex I think this is looking pretty good, just a few minor tweaks and also we should see if we want to apply this validator to all DataArray objects (as written currently) or rather as a Simulation post-init validator that loops over sources and does the interp check. I'm curious to see if the front end tests pass with these changes or if there are instances where we explicitly allow duplicate coords in some data array objects.

You're right. This doesn't pass front-end test. I'll give it another go with all your comments and restricting the check to custom fields and sources.

I guess also CustomMedium types with interp_method == "linear" (as opposed to the default "nearest"). Actually I don't know if "nearest" also fails in some cases, i.e. maybe the data array sel method also fails if there are repeated coordinates?

@marc-flex
Copy link
Contributor Author

marc-flex commented May 8, 2024

@tylerflex I have tried several things. With the solution you mentioned (val.interp_like(val.isel({val.dim: 0}))) tests kept failing. So I have modified it to use an array of values from within the coordinates for each dimension (not sure that's generic enough).

Also, since this made checks fail, I'm only checking for custom source and mediums. Basically, I call a check function (defined at the DataArray level) within the validators.

I have created tests for custom medium and custom field source.

@tylerflex
Copy link
Collaborator

Thanks @marc-flex just FYI, this PR #1681 changed some of the tests in pre/2.7 so it looks like you'll need to rebase against that branch and fix any conflicts that might come up.

I'm not sure which tests are failing but we can look into it more. One that probably fails is related to log_capture but you can see my comment above.

@marc-flex
Copy link
Contributor Author

marc-flex commented May 8, 2024

Thanks @marc-flex just FYI, this PR #1681 changed some of the tests in pre/2.7 so it looks like you'll need to rebase against that branch and fix any conflicts that might come up.

I have just rebased against pre/2.7 and force-pushed it

@tylerflex
Copy link
Collaborator

@marc-flex @momchil-flex is this something we want to get into 2.7.0rc2? and is it ready or still needing changes?

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.

Sorry @marc-flex I realized that this review was left as "pending" for the past week. here are some additional comments.

When you think it's getting ready, feel free to un-mark as a draft.

tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/source.py Outdated Show resolved Hide resolved
tests/test_components/test_medium.py Outdated Show resolved Hide resolved
@marc-flex marc-flex marked this pull request as ready for review May 14, 2024 11:10
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/source.py Show resolved Hide resolved
Copy link
Collaborator

@weiliangjin2021 weiliangjin2021 left a comment

Choose a reason for hiding this comment

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

Just one comment: custom datasets are validated in CustomMedium. Shall we validate all other types of custom materials as well, e.g. CustomPoleResidue, CustomLorentz, etc.?

@marc-flex
Copy link
Contributor Author

marc-flex commented May 15, 2024

Just one comment: custom datasets are validated in CustomMedium. Shall we validate all other types of custom materials as well, e.g. CustomPoleResidue, CustomLorentz, etc.?

You're opening Pandora's box here. Maybe we should do this for all custom-defined DataArrays fields? Or should we limit this PR to the fields defined in issue #1668 ? What do you think @momchil-flex @tylerflex ? I'm guessing this wouldn't just be the validator, it'd also be implementing a corresponding test

@momchil-flex
Copy link
Collaborator

Just one comment: custom datasets are validated in CustomMedium. Shall we validate all other types of custom materials as well, e.g. CustomPoleResidue, CustomLorentz, etc.?

You're opening Pandora's box here. Maybe we should do this for all custom-defined DataArrays fields? Or should we limit this PR to the fields defined in issue #1668 ? What do you think @momchil-flex @tylerflex ? I'm guessing this wouldn't just be the validator, it'd also be implementing a corresponding test

To me it kinda makes sense to test all custom-defined data array fields, and I can't think of currently existing objects where we wouldn't want that. However, a) I don't know if I'm forgetting something already and b) I'm not sure if in the future something will come up. But maybe let's leave b) for future devs to worry about if validating all custom data arrays passes all tests (so hopefully a) is ok)?

@marc-flex
Copy link
Contributor Author

Just one comment: custom datasets are validated in CustomMedium. Shall we validate all other types of custom materials as well, e.g. CustomPoleResidue, CustomLorentz, etc.?

You're opening Pandora's box here. Maybe we should do this for all custom-defined DataArrays fields? Or should we limit this PR to the fields defined in issue #1668 ? What do you think @momchil-flex @tylerflex ? I'm guessing this wouldn't just be the validator, it'd also be implementing a corresponding test

To me it kinda makes sense to test all custom-defined data array fields, and I can't think of currently existing objects where we wouldn't want that. However, a) I don't know if I'm forgetting something already and b) I'm not sure if in the future something will come up. But maybe let's leave b) for future devs to worry about if validating all custom data arrays passes all tests (so hopefully a) is ok)?

I can remove the specific tests for custom medium/source and have the validator at the DataArray level. The only issue is that it wouldn't then say what property made the validator fail which might be frustrating for the users. So I guess I'd suggest to keep the scope of this PR as is, and add another issue where we collect any other fields we'd like to validate/test?

@tylerflex
Copy link
Collaborator

I agree with @marc-flex . Maybe it's best to just limit the scope of these validators to the components that are causing issues as per #1684 . because I additionally worry that adding too strict validation DataArray may lead to some unintended consequences.

@momchil-flex
Copy link
Collaborator

Ok yeah sounds good to me.

tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/source.py Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
tidy3d/components/data/data_array.py Outdated Show resolved Hide resolved
@tylerflex
Copy link
Collaborator

@marc-flex I think this is basically ready to go. Last thing you'll need to do is update the docs/notebooks submodule to the latest version (pre/2.7 branch).

After you make that commit, we'll want to rebase this against tidy3d pre/2.7 and squash all the commits into one.

@marc-flex
Copy link
Contributor Author

@tylerflex @momchil-flex I have rebased and I thought everything would be OK from my side but it fails the "latest" test. Do let me know how to fix this.

@momchil-flex
Copy link
Collaborator

Actually the latest test is fine now, it's the formatting test that fails. :) You need to run bash scripts/test_local.sh (and you can abort after the formatting step), or

black tidy3d/
black tests/

@tylerflex
Copy link
Collaborator

(make sure your black is the same as the version defined in pyproject.toml, or it will format most of the files in tidy3d)

@marc-flex
Copy link
Contributor Author

Not sure why poetry didn't complain about that when I committed the squash+rebase. I think it's all good now

@momchil-flex momchil-flex merged commit 2cf9de0 into pre/2.7 Jun 3, 2024
16 checks passed
@momchil-flex momchil-flex deleted the marc/interp_validator branch June 3, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.7 will go into version 2.7.*
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants