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

AbstractPlotRenderers return different array shapes when passing a single point to map_screen #550

Open
mdsmarte opened this issue Dec 21, 2020 · 5 comments

Comments

@mdsmarte
Copy link

Base2DPlot and BaseXYPlot both implement the AbstractPlotRenderer interface, in particular the map_screen method.

When a single (x, y) point is passed to Base2DPlot.map_screen, the returned array is of the form [[x, y]] (nested, shape = (1, 2)), as set by GridMapper.map_screen:

chaco/chaco/grid_mapper.py

Lines 114 to 123 in 4a66835

def map_screen(self, data_pts):
""" map_screen(data_pts) -> screen_array
Maps values from data space into screen space.
"""
xs, ys = transpose(data_pts)
screen_xs = self._xmapper.map_screen(xs)
screen_ys = self._ymapper.map_screen(ys)
screen_pts = column_stack([screen_xs, screen_ys])
return screen_pts

When a single (x, y) point is passed to BaseXYPlot.map_screen, the returned array is of the form [x, y] (flattened, shape = (2,)):

chaco/chaco/base_xy_plot.py

Lines 336 to 353 in 4a66835

def map_screen(self, data_array):
""" Maps an array of data points into screen space and returns it as
an array.
Implements the AbstractPlotRenderer interface.
"""
# data_array is Nx2 array
if len(data_array) == 0:
return []
x_ary, y_ary = transpose(data_array)
sx = self.index_mapper.map_screen(x_ary)
sy = self.value_mapper.map_screen(y_ary)
if self.orientation == "h":
return transpose(array((sx,sy)))
else:
return transpose(array((sy,sx)))

This difference forces objects that interact with AbstractPlotRenderers to make an assumption about the behavior of the map_screen method. For example, the DataLabelTool assumes the map_screen return shape matches that of BaseXYPlot for a single point ((2,)):

pointx, pointy = label.component.map_screen(label.data_point)
self._original_offset = (label.x - pointx, label.y - pointy)

And therefore this tool throws an exception when used with a Base2DPlot:

File "<REDACTED>/python3.6/site-packages/chaco/tools/data_label_tool.py", line 53, in drag_start
    pointx, pointy = label.component.map_screen(label.data_point)
ValueError: not enough values to unpack (expected 2, got 1)

even though it would otherwise be compatible.

This issue also looks like it applies to the map_data method although I have not tested it.

@aaronayres35
Copy link
Contributor

#802 and #804 have been merged. This PR can be closed after we go through and audit the code base to see if anything was missed

@rahulporuri
Copy link
Contributor

This PR can be closed after we go through and audit the code base to see if anything was missed

I'm guessing you meant "this issue can be closed". Can you do the audit @aaronayres35 ?

@aaronayres35
Copy link
Contributor

So I've gone through the code base and I think it looks like we've got everything.

The only thing that stood out was the map_screen method on DataView which I am not sure why it exists. DataView has a class hierarchy which traces back to enable Container and none of its base classes define / use a map_screen method. It also isn't used anywhere in DataView itself, or in Plot which subclasses DataView. In other words I think it is completely unused. It currently is similar to what BaseXYPlot.map_screen was previously. I was thinking to update it to match, but after looking closer I think it can maybe just be removed entirely? It is technically public api though so maybe we shouldn't do that. I don't know when one would be using the method on the DataView/Plot rather than the plot renderer though...

@rahulporuri what do you think?

@rahulporuri
Copy link
Contributor

Nice catch. I'll try to look into the history of why that method was added - but it looks like we can close this issue and open another one regarding deprecating and potentially removing that method.

@rahulporuri
Copy link
Contributor

so it looks like the map_data and map_screen methods were added to the DataView here - 1f5f5c4. It isn't clear why though.

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

No branches or pull requests

3 participants