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

Use dataframe index for TriMesh node indices #4936

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions holoviews/element/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ..core import Dimension, Dataset, Element2D
from ..core.accessors import Redim
from ..core.util import max_range, search_indices
from ..core.util import is_dataframe, max_range, search_indices
from ..core.operation import Operation
from .chart import Points
from .path import Path
Expand Down Expand Up @@ -545,15 +545,20 @@ def __init__(self, data, kdims=None, vdims=None, **params):
# Add index to make it a valid Nodes object
nodes = self.node_type(Dataset(nodes).add_dimension('index', 2, np.arange(len(nodes))))
elif not isinstance(nodes, Dataset) or nodes.ndims in [2, 3]:
# Try assuming data contains just coordinates (2 columns)
try:
points = self.point_type(nodes)
ds = Dataset(points).add_dimension('index', 2, np.arange(len(points)))
nodes = self.node_type(ds)
except:
raise ValueError("Nodes argument could not be interpreted, expected "
"data with two or three columns representing the "
"x/y positions and optionally the node indices.")
if is_dataframe(nodes):
coords = list(nodes.columns)[:2]
index = nodes.index.name or 'index'
nodes = self.node_type(nodes, coords+[index])
else:
try:
points = self.point_type(nodes)
ds = Dataset(points).add_dimension('index', 2, np.arange(len(points)))
nodes = self.node_type(ds)
except Exception:
raise ValueError(
"Nodes argument could not be interpreted, expected "
"data with two or three columns representing the "
"x/y positions and optionally the node indices.")
if edgepaths is not None and not isinstance(edgepaths, self.edge_type):
edgepaths = self.edge_type(edgepaths)

Expand Down
40 changes: 40 additions & 0 deletions holoviews/tests/operation/testdatashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,46 @@ def test_rasterize_trimesh(self):
image = Image(array, bounds=(0, 0, 1, 1))
self.assertEqual(img, image)

def test_rasterize_pandas_trimesh_implicit_nodes(self):
simplex_df = pd.DataFrame(self.simplexes, columns=['v0', 'v1', 'v2'])
vertex_df = pd.DataFrame(self.vertices_vdim, columns=['x', 'y', 'z'])

trimesh = TriMesh((simplex_df, vertex_df))
img = rasterize(trimesh, width=3, height=3, dynamic=False)

array = np.array([
[ 2., 3., np.NaN],
[ 1.5, 2.5, np.NaN],
[np.NaN, np.NaN, np.NaN]
])
image = Image(array, bounds=(0, 0, 1, 1))
self.assertEqual(img, image)

def test_rasterize_dask_trimesh_implicit_nodes(self):
simplex_df = pd.DataFrame(self.simplexes, columns=['v0', 'v1', 'v2'])
vertex_df = pd.DataFrame(self.vertices_vdim, columns=['x', 'y', 'z'])

simplex_ddf = dd.from_pandas(simplex_df, npartitions=2)
vertex_ddf = dd.from_pandas(vertex_df, npartitions=2)

trimesh = TriMesh((simplex_ddf, vertex_ddf))

ri = rasterize.instance()
img = ri(trimesh, width=3, height=3, dynamic=False, precompute=True)

cache = ri._precomputed
self.assertEqual(len(cache), 1)
self.assertIn(trimesh._plot_id, cache)
self.assertIsInstance(cache[trimesh._plot_id]['mesh'], dd.DataFrame)

array = np.array([
[ 2., 3., np.NaN],
[ 1.5, 2.5, np.NaN],
[np.NaN, np.NaN, np.NaN]
])
image = Image(array, bounds=(0, 0, 1, 1))
self.assertEqual(img, image)

def test_rasterize_dask_trimesh(self):
simplex_df = pd.DataFrame(self.simplexes_vdim, columns=['v0', 'v1', 'v2', 'z'])
vertex_df = pd.DataFrame(self.vertices, columns=['x', 'y'])
Expand Down