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

Side Effects (Array Mutations) when Accessing Dataset Attributes #3180

Open
adam-grant-hendry opened this issue Aug 18, 2022 · 1 comment
Open
Labels
bug Uh-oh! Something isn't working as expected.

Comments

@adam-grant-hendry
Copy link
Contributor

adam-grant-hendry commented Aug 18, 2022

Describe the bug, what's wrong, and what you expected.

As revealed in PR #3170, accessing certain dataset attributes in function scopes causes side effects: mutations of arrays. Specifically, we saw:

  1. Accessing volume.active_scalars in plotting.add_volume
2918:            scalars = volume.active_scalars

led to volume['Spatial Point Data'] from the examples.load_uniform() dataset getting mutated (it was rescaled to the 0-255 range)

img_diff

and attempting to loop through

for name in volume.array_names:
    ...

led name to maintain a reference to the last element in volume.array_names, causing the last item of test_plot_helper_two_volumes to not be plotted:

Scope_Leak

Steps to reproduce the bug.

Please see the problem description.

System Information

--------------------------------------------------------------------------------
  Date: Wed Aug 17 17:09:20 2022 Pacific Daylight Time

                OS : Windows
            CPU(s) : 96
           Machine : AMD64
      Architecture : 64bit
               RAM : 190.7 GiB
       Environment : Python
        GPU Vendor : NVIDIA Corporation
      GPU Renderer : Quadro RTX 8000/PCIe/SSE2
       GPU Version : 4.5.0 NVIDIA 516.94

  Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64    
  bit (AMD64)]

           pyvista : 0.36.1
               vtk : 9.1.0
             numpy : 1.23.2
           imageio : 2.21.1
           appdirs : 1.4.4
            scooby : 0.5.12
        matplotlib : 3.5.3
         pyvistaqt : 0.9.0
           IPython : 8.4.0
              tqdm : 4.64.0
            meshio : 5.3.4
--------------------------------------------------------------------------------

Screenshots

Please see the problem description.

NB: I highly recommend watching:

  1. Python Oddities Explained, by Trey Hunner (PyCon 2022)
  2. Facts and Myths about Python names and values, by Ned Batchelder (PyCon 2015)

to best understand what is going on here.

In essence though, Python variables are C-references (e.g. int& var) and they are "pass by assignment", whereas in C variables copy by default and are pass by copy (value) by default.

@adam-grant-hendry adam-grant-hendry added the bug Uh-oh! Something isn't working as expected. label Aug 18, 2022
@adam-grant-hendry
Copy link
Contributor Author

Adding this here so I don't forget: I wonder if we use weakref anywhere in the code base...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Uh-oh! Something isn't working as expected.
Projects
None yet
Development

No branches or pull requests

1 participant