Skip to content

[Bug]: call cmap object on torch.tensor will output first element all 0 #23132

Closed

Description

@yugeng-tju

Bug summary

When I create camp object and call on torch.tensor, it output result with first element be all 0. But if I call tensor.numpy() explicity, everything went well. I do not know this is a problem of matplotlib or problem of pytorch.

Code for reproduction

import torch
from matplotlib import cm

cmap = cm.get_cmap("plasma")
i_tensor = abs(torch.randn([3,5,5]))
i_tensor = i_tensor/i_tensor.max()
i_numpy = i_tensor.numpy()
c_ts = cmap(i_tensor)[...,:3]
c_np = cmap(i_numpy)[...,:3]
print(c_ts[0])
print(c_np[0])

Actual outcome

[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]]

Expected outcome

with specific value

Additional information

No response

Operating system

No response

Matplotlib Version

3.3.4

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

No response

Activity

  1. jklymak commented on May 25, 2022

    @jklymak
    Member

    Presumably PyTorch objects are not interchangeable with floats. We are not going to start accepting PyTorch objects to all our function calls, so I'm going to close this.

  2. jklymak commented on May 25, 2022

    @jklymak
    Member

    Actually, lets reconsider this. I don't want to install PyTorch, but does np.asarray(X) work on a PyTorch object? We could consider adding this at the beginning of the colormap call to try and catch more types...

  3. yugeng-tju commented on May 25, 2022

    @yugeng-tju
    Author

    Thanks @jklymak , np.asarray(X) works on a PyTorch object.
    I understand matplotlib dose not have to accept all kinds of objects. I just wondering why torch.tensor object meet this bug. Note that only first element of the tensor are all 0, no matter the shape is. This is quite annoying for some people like me has bad habbit that is too lazy to explicity transfrom the data type... (cus torch tensor object is compatible with nearly all numpy function..). Anyway, I've learned the lesson that I‘d best always explicitly deal with the right data type.

  4. greglucas commented on May 25, 2022

    @greglucas
    Contributor

    We do force to array very early on already.

    mask_bad = X.mask if np.ma.is_masked(X) else np.isnan(X) # Mask nan's.
    xa = np.array(X, copy=True)

    Maybe the np.isnan(torch_array) is the issue? That returns a uint8 dtype, whereas torch_array.isnan() returns bools. So I wonder if some indexing is going on because the lookup array is not bool, but uint8.

  5. jklymak commented on May 25, 2022

    @jklymak
    Member

    If np.asarray(X) doesn't destroy masked arrays, then I guess we should do mask_bad after this.

  6. tacaswell commented on May 25, 2022

    @tacaswell
    Member

    The problem is

    xa[mask_bad] = self._i_bad
    coupled with

    In [42]: mask_bad = X.mask if np.ma.is_masked(X) else np.isnan(X)  # Mask nan's.
    
    In [43]: mask_bad
    Out[43]: 
    tensor([[[0, 0, 0],
             [0, 0, 0]],
    
            [[0, 0, 0],
             [0, 0, 0]]], dtype=torch.uint8)

    We are expecting mask_bad to be an array of bools which goes through numpy's fancy bool slicing and setting only the elements identified. Instead is doing the numpy's fancy index based slicing and getting a view of the first slice (which) we are then setting to the bad value. I think we can either do what @jklymak suggests and move the casting to numpy earlier or add

    mask_bad = np.asanyarray(mask_bad).astype(bool)

    after L710 to ensure that we really have bools.

  7. QuLogic commented on May 25, 2022

    @QuLogic
    Member

    np.isnan(torch_array) is the issue? That returns a uint8 dtype,

    So that's a bug in pytorch, isn't it? It seems like we can work around it, but it's definitely their problem?

  8. oscargus commented on May 27, 2022

    @oscargus
    Member

    It should be possible to replace isnan(X) with X != X which also would solve issues with e.g. dtype=object inputs.

  9. added a commit that references this issue on Sep 25, 2022
    be849c5
  10. added a commit that references this issue on Oct 15, 2022
    07268f0
  11. added this to the v3.6.2 milestone on Oct 20, 2022
  12. added a commit that references this issue on Dec 19, 2022
    f42a961
  13. added a commit that references this issue on Apr 19, 2026
    9e2fac4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions