enthought / chaco

Chaco is a Python package for building interactive and custom 2-D plots.
http://docs.enthought.com/chaco/
Other
292 stars 99 forks source link

Follow numpy's advice on how to use the resize function. #823

Closed jdpatt closed 2 years ago

jdpatt commented 2 years ago

When working on an application that uses traitsui/chaco, I'm unable to use a visual debugger such as vscode or pycharm. Instead, chaco raises the following exception.

Exception occurred in traits notification handler for object: <chaco.plot_containers.GridPlotContainer object at 0x7ff52a9d04a0>, trait: shape, old value: (0, 0), new value: (2, 2)
Traceback (most recent call last):
  File "/usr/local/Caskroom/miniconda/base/envs/pybert/lib/python3.8/site-packages/traits/trait_notifiers.py", line 342, in __call__
    self.handler(*args)
  File "/usr/local/Caskroom/miniconda/base/envs/pybert/lib/python3.8/site-packages/chaco/plot_containers.py", line 539, in _shape_changed
    self._reflow_layout()
  File "/usr/local/Caskroom/miniconda/base/envs/pybert/lib/python3.8/site-packages/chaco/plot_containers.py", line 533, in _reflow_layout
    grid.resize(self.shape)
ValueError: cannot resize an array that references or is referenced
by another array in this way.
Use the np.resize function or refcheck=False

Following numpy's suggestion of changing to np.resize(grid, self.shape) was enough to start using a debugger with this application. Looking at the codebase for \.resize\(.+\) this was the only instance of using resize from the numpy library.

jdpatt commented 2 years ago

Note another developer found from the numpy docs that it fills differently which may make this a non-starter for chaco like it did our application.

If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.

https://numpy.org/doc/stable/reference/generated/numpy.resize.html

corranwebster commented 2 years ago

Thanks for raising this.

Yes, repeating the array won't work here. I think that the way this is being used to flow a list of components into a grid:

If the initial array repeated then there wouldn't be any empty cells.

The solution is probably not to be quite so clever: