1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
# ----------------------------------------------------------------------------
# - Open3D: www.open3d.org -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2024 www.open3d.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------
import open3d as o3d
import numpy as np
import pytest
import mltest
# skip all tests if the ml ops were not built
pytestmark = mltest.default_marks
# the supported dtypes for the attributes
value_dtypes = pytest.mark.parametrize(
'dtype',
[np.uint8, np.int8, np.int16, np.int32, np.int64, np.float32, np.float64])
attributes = pytest.mark.parametrize('attributes',
['scalar', 'none', 'multidim'])
@value_dtypes
@attributes
@mltest.parametrize.ml
def test_invert_neighbors_list(dtype, attributes, ml):
# yapf: disable
# define connectivity for 3 query points and 3 input points
num_points = 3
edges = np.array(
[
[0, 0], [0, 1], [0, 2], # 3 neighbors
[1, 2], # 1 neighbors
[2, 1], [2, 2], # 2 neighbors
],
dtype=np.int32)
# the neighbors_index is the second column
neighbors_index = edges[:, 1]
# exclusive prefix sum of the number of neighbors
neighbors_row_splits = np.array([0, 3, 4, edges.shape[0]], dtype=np.int64)
if attributes == 'scalar':
neighbors_attributes = np.array([
10, 20, 30,
40,
50, 60,
], dtype=dtype)
elif attributes == 'none':
neighbors_attributes = np.array([], dtype=dtype)
elif attributes == 'multidim':
neighbors_attributes = np.array([
[10, 1], [20, 2], [30, 3],
[40, 4],
[50, 5], [60, 6],
], dtype=dtype)
# yapf: enable
ans = mltest.run_op(ml,
ml.device,
True,
ml.ops.invert_neighbors_list,
num_points=num_points,
inp_neighbors_index=neighbors_index,
inp_neighbors_row_splits=neighbors_row_splits,
inp_neighbors_attributes=neighbors_attributes)
expected_neighbors_row_splits = [0, 1, 3, edges.shape[0]]
np.testing.assert_equal(ans.neighbors_row_splits,
expected_neighbors_row_splits)
# checking the neighbors_index is more complicated because the order
# of the neighbors for each query point is not defined.
expected_neighbors_index = [
set([0]),
set([0, 2]),
set([0, 1, 2]),
]
for i, expected_neighbors_i in enumerate(expected_neighbors_index):
start = ans.neighbors_row_splits[i]
end = ans.neighbors_row_splits[i + 1]
neighbors_i = set(ans.neighbors_index[start:end])
assert neighbors_i == expected_neighbors_i
if neighbors_attributes.shape == (0,):
# if the input is a zero length vector then the returned attributes
# vector also must be a zero length vector
assert ans.neighbors_attributes.shape == (0,)
else:
# check if the attributes are still associated with the same edge
edge_attr_map = {
tuple(k): v for k, v in zip(edges, neighbors_attributes)
}
for i, _ in enumerate(expected_neighbors_index):
start = ans.neighbors_row_splits[i]
end = ans.neighbors_row_splits[i + 1]
# neighbors and attributes for point i
neighbors_i = ans.neighbors_index[start:end]
attributes_i = ans.neighbors_attributes[start:end]
for j, attr in zip(neighbors_i, attributes_i):
key = (j, i)
np.testing.assert_equal(attr, edge_attr_map[key])
@mltest.parametrize.ml
def test_invert_neighbors_list_shape_checking(ml):
num_points = 3
inp_neighbors_index = np.array([0, 1, 2, 2, 1, 2], dtype=np.int32)
inp_neighbors_row_splits = np.array([0, 3, 4, 6], dtype=np.int64)
inp_neighbors_attributes = np.array([10, 20, 30, 40, 50, 60],
dtype=np.float32)
# test the shape checking by passing arrays with wrong rank and/or size
with pytest.raises(Exception) as einfo:
_ = mltest.run_op(ml,
ml.cpu_device,
False,
ml.ops.invert_neighbors_list,
num_points=num_points,
inp_neighbors_index=inp_neighbors_index[1:],
inp_neighbors_row_splits=inp_neighbors_row_splits,
inp_neighbors_attributes=inp_neighbors_attributes)
assert 'invalid shape' in str(einfo.value)
with pytest.raises(Exception) as einfo:
_ = mltest.run_op(ml,
ml.cpu_device,
False,
ml.ops.invert_neighbors_list,
num_points=num_points,
inp_neighbors_index=inp_neighbors_index[:,
np.newaxis],
inp_neighbors_row_splits=inp_neighbors_row_splits,
inp_neighbors_attributes=inp_neighbors_attributes)
assert 'invalid shape' in str(einfo.value)
with pytest.raises(Exception) as einfo:
_ = mltest.run_op(
ml,
ml.cpu_device,
False,
ml.ops.invert_neighbors_list,
num_points=num_points,
inp_neighbors_index=inp_neighbors_index,
inp_neighbors_row_splits=inp_neighbors_row_splits[:, np.newaxis],
inp_neighbors_attributes=inp_neighbors_attributes)
assert 'invalid shape' in str(einfo.value)
with pytest.raises(Exception) as einfo:
_ = mltest.run_op(ml,
ml.cpu_device,
False,
ml.ops.invert_neighbors_list,
num_points=num_points,
inp_neighbors_index=inp_neighbors_index,
inp_neighbors_row_splits=inp_neighbors_row_splits,
inp_neighbors_attributes=inp_neighbors_attributes[1:])
assert 'invalid shape' in str(einfo.value)
|