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

Nearest neighbor interpolator #12

Merged
merged 14 commits into from
Sep 16, 2024
Merged

Nearest neighbor interpolator #12

merged 14 commits into from
Sep 16, 2024

Conversation

nbren12
Copy link
Collaborator

@nbren12 nbren12 commented Sep 6, 2024

No description provided.

@nbren12
Copy link
Collaborator Author

nbren12 commented Sep 6, 2024

Nearest neighbor
image

@nbren12
Copy link
Collaborator Author

nbren12 commented Sep 6, 2024

Inverse distance
image

Copy link
Collaborator

@akshaysubr akshaysubr left a comment

Choose a reason for hiding this comment

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

Looks good! Had a couple of comments about testing saving the interpolator and one minor comment on sampling for the tests.

return output


class S2LinearBarycentricInterpolator(torch.nn.Module):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be good to add a test that saves a checkpoint of an initialized instance of this and loads that back in. Would be very useful to avoid repeated calls to spatial.KDTree especially in ETL jobs where a regridding script might be invoked multiple times on different inputs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll make an issue for this. I think it may require some thought and potentially backwards breaking changes to apply this consistently across the suite of re-gridders.

I'm not sure the most elegant way to allow ser/deser of "buffer-only" modules like this. Do you know of an example for this e.g. with normalization.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we don't necessarily need a proper serialization/deserialization pipeline here right? It might be sufficient to create a classmethod that takes in a state dict and returns an initialized instance from that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I refactored this so that the S2NearestNeighborInterpolator now returns a serializeable Regridder object. Let me know what you think.

earth2grid/_regrid.py Outdated Show resolved Hide resolved
n = 10000
torch.manual_seed(0)
lon = torch.rand(n) * 360
lat = torch.rand(n) * 180 - 90
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
lat = torch.rand(n) * 180 - 90
lat = torch.asin(2*torch.rand(n)-1) * 180. / np.pi

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what is the intent here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't a super important change, but thought it would be good to remove the polar bias when sampling on the sphere in these tests.

earth2grid/_regrid.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@akshaysubr akshaysubr left a comment

Choose a reason for hiding this comment

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

Changes look good and the unification of the two KDTree implementations is nice too.

Not for this PR, but might be nice to have the Regridder object be TorchScript compatible so you could serialize a regridder object that way.

tests/test_regrid.py Outdated Show resolved Hide resolved
@nbren12 nbren12 merged commit 6c6bba8 into main Sep 16, 2024
2 checks passed
@nbren12 nbren12 deleted the nearest branch September 16, 2024 19:51
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.

2 participants