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

GriddedDataset: torch warn fix #145

Merged
merged 3 commits into from
Feb 9, 2023
Merged

GriddedDataset: torch warn fix #145

merged 3 commits into from
Feb 9, 2023

Conversation

jeffjennings
Copy link
Contributor

@jeffjennings jeffjennings commented Feb 9, 2023

Resolves a warning raised when passing a tensor to GriddedDataset.add_mask

@jeffjennings jeffjennings requested a review from iancze February 9, 2023 08:04
@jeffjennings
Copy link
Contributor Author

I don't know why MyPy is failing in the test, unless it's searching through

        if torch.is_tensor(mask):
            new_2D_mask = mask.clone().detach().to(device)

with a numpy array.

Copy link
Contributor

@kadri-nizam kadri-nizam left a comment

Choose a reason for hiding this comment

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

@jeffjennings

This is a known issue when typing with PyTorch. The recommended way for checking is to check the type explicitly: if isinstance(mask, torch.Tensor). This is the recommended approach by the devs at PyTorch as can be seen from the is_tensor documentation

That being said, my suggestion is to simply skip the check since we have mask.clone().detach() in the proceeding line. Doing new_2D_mask = torch.Tensor(mask).detach().to(device) will perform the same thing for both Tensors and ArrayLike objects.

src/mpol/datasets.py Outdated Show resolved Hide resolved
@kadri-nizam kadri-nizam dismissed their stale review February 9, 2023 20:51

Made the recommended changes in 30ad09b

@iancze iancze merged commit 7c9667f into main Feb 9, 2023
@iancze
Copy link
Collaborator

iancze commented Feb 9, 2023

Thanks!

@iancze iancze deleted the pytorch_warn_fix branch February 9, 2023 21:22
@iancze iancze removed their request for review February 9, 2023 21:23
@jeffjennings
Copy link
Contributor Author

jeffjennings commented Feb 9, 2023

Ah ok I see. Thanks for explaining that, @kadri-nizam!

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.

3 participants