Skip to content
Snippets Groups Projects
Commit 1182775e authored by Alexander Dielen's avatar Alexander Dielen
Browse files

preserve array shape

parent 746cac21
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -106,6 +106,7 @@ public:
py::array_t<double> tmp_arr;
try {
tmp_arr = tmp_obj.cast<py::array_t<double> >();
tmp_arr = make_c_style(tmp_arr);
}
catch (py::error_already_set& e) {
return py::array_t<double>();
......@@ -117,6 +118,12 @@ public:
return py::array_t<double>();
}
// preserve array shape and strides
std::vector<size_t> shape({n});
std::vector<size_t> strides({size * sizeof(double)});
shape.insert(shape.end(), tmp_arr.shape(), tmp_arr.shape() + tmp_arr.ndim());
strides.insert(strides.end(), tmp_arr.strides(), tmp_arr.strides() + tmp_arr.ndim());
// allocate memory
double *data = new double[size * n];
......@@ -138,8 +145,6 @@ public:
}
// make numpy array
const auto shape = {n, size};
const auto strides = {size * sizeof(double), sizeof(double)};
py::capsule base = free_when_done(data);
return py::array_t<double>(shape, strides, data, base);
}
......@@ -159,10 +164,11 @@ public:
for (size_t i = 0; i < n; ++i) {
double *data = new double[size];
std::copy(_arr.data(i), _arr.data(i) + size, data);
const auto shape = {size};
const auto strides = {sizeof(double)};
const std::vector<size_t> shape(_arr.shape() + 1, _arr.shape() + _arr.ndim());
const std::vector<size_t> strides(_arr.strides() + 1, _arr.strides() + _arr.ndim());
py::capsule base = free_when_done(data);
py::array_t<double> tmp(shape, strides, data, base);
Mesh::property(prop, Handle(i)) = tmp;
}
}
......
......@@ -27,7 +27,7 @@ class Python(unittest.TestCase):
for vh in self.mesh.vertices():
arr3 = self.mesh.vertex_property('random', vh)
self.assertTrue(np.allclose(arr1.T[vh.idx()], arr3))
# single columns
# slices
arr1 = np.random.rand(10, self.mesh.n_vertices())
for vh in self.mesh.vertices():
self.mesh.set_vertex_property('random', vh, arr1[:, vh.idx()])
......@@ -36,6 +36,28 @@ class Python(unittest.TestCase):
for vh in self.mesh.vertices():
arr3 = self.mesh.vertex_property('random', vh)
self.assertTrue(np.allclose(arr1.T[vh.idx()], arr3))
# multidimensional
arr1 = np.random.rand(self.mesh.n_vertices(), 2, 4)
self.mesh.set_vertex_property_array('random', arr1)
arr2 = self.mesh.vertex_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for vh in self.mesh.vertices():
arr3 = self.mesh.vertex_property('random', vh)
self.assertEqual(arr1[vh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[vh.idx()], arr3))
# multidimensional slices
arr1 = np.random.rand(4, 2, self.mesh.n_vertices())
for vh in self.mesh.vertices():
self.mesh.set_vertex_property('random', vh, arr1[:, :, vh.idx()])
arr1 = arr1.transpose(2, 0, 1)
arr2 = self.mesh.vertex_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for vh in self.mesh.vertices():
arr3 = self.mesh.vertex_property('random', vh)
self.assertEqual(arr1[vh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[vh.idx()], arr3))
def test_halfedge_property_array(self):
self.assertFalse(self.mesh.has_halfedge_property('random'))
......@@ -55,7 +77,7 @@ class Python(unittest.TestCase):
for hh in self.mesh.halfedges():
arr3 = self.mesh.halfedge_property('random', hh)
self.assertTrue(np.allclose(arr1.T[hh.idx()], arr3))
# single columns
# slices
arr1 = np.random.rand(10, self.mesh.n_halfedges())
for hh in self.mesh.halfedges():
self.mesh.set_halfedge_property('random', hh, arr1[:, hh.idx()])
......@@ -64,6 +86,28 @@ class Python(unittest.TestCase):
for hh in self.mesh.halfedges():
arr3 = self.mesh.halfedge_property('random', hh)
self.assertTrue(np.allclose(arr1.T[hh.idx()], arr3))
# multidimensional
arr1 = np.random.rand(self.mesh.n_halfedges(), 2, 4)
self.mesh.set_halfedge_property_array('random', arr1)
arr2 = self.mesh.halfedge_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for hh in self.mesh.halfedges():
arr3 = self.mesh.halfedge_property('random', hh)
self.assertEqual(arr1[hh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[hh.idx()], arr3))
# multidimensional slices
arr1 = np.random.rand(4, 2, self.mesh.n_halfedges())
for hh in self.mesh.halfedges():
self.mesh.set_halfedge_property('random', hh, arr1[:, :, hh.idx()])
arr1 = arr1.transpose(2, 0, 1)
arr2 = self.mesh.halfedge_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for hh in self.mesh.halfedges():
arr3 = self.mesh.halfedge_property('random', hh)
self.assertEqual(arr1[hh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[hh.idx()], arr3))
def test_edge_property_array(self):
self.assertFalse(self.mesh.has_edge_property('random'))
......@@ -83,7 +127,7 @@ class Python(unittest.TestCase):
for eh in self.mesh.edges():
arr3 = self.mesh.edge_property('random', eh)
self.assertTrue(np.allclose(arr1.T[eh.idx()], arr3))
# single columns
# slices
arr1 = np.random.rand(10, self.mesh.n_edges())
for eh in self.mesh.edges():
self.mesh.set_edge_property('random', eh, arr1[:, eh.idx()])
......@@ -92,6 +136,28 @@ class Python(unittest.TestCase):
for eh in self.mesh.edges():
arr3 = self.mesh.edge_property('random', eh)
self.assertTrue(np.allclose(arr1.T[eh.idx()], arr3))
# multidimensional
arr1 = np.random.rand(self.mesh.n_edges(), 2, 4)
self.mesh.set_edge_property_array('random', arr1)
arr2 = self.mesh.edge_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for eh in self.mesh.edges():
arr3 = self.mesh.edge_property('random', eh)
self.assertEqual(arr1[eh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[eh.idx()], arr3))
# multidimensional slices
arr1 = np.random.rand(4, 2, self.mesh.n_edges())
for eh in self.mesh.edges():
self.mesh.set_edge_property('random', eh, arr1[:, :, eh.idx()])
arr1 = arr1.transpose(2, 0, 1)
arr2 = self.mesh.edge_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for eh in self.mesh.edges():
arr3 = self.mesh.edge_property('random', eh)
self.assertEqual(arr1[eh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[eh.idx()], arr3))
def test_face_property_array(self):
self.assertFalse(self.mesh.has_face_property('random'))
......@@ -111,7 +177,7 @@ class Python(unittest.TestCase):
for fh in self.mesh.faces():
arr3 = self.mesh.face_property('random', fh)
self.assertTrue(np.allclose(arr1.T[fh.idx()], arr3))
# single columns
# slices
arr1 = np.random.rand(10, self.mesh.n_faces())
for fh in self.mesh.faces():
self.mesh.set_face_property('random', fh, arr1[:, fh.idx()])
......@@ -120,6 +186,28 @@ class Python(unittest.TestCase):
for fh in self.mesh.faces():
arr3 = self.mesh.face_property('random', fh)
self.assertTrue(np.allclose(arr1.T[fh.idx()], arr3))
# multidimensional
arr1 = np.random.rand(self.mesh.n_faces(), 2, 4)
self.mesh.set_face_property_array('random', arr1)
arr2 = self.mesh.face_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for fh in self.mesh.faces():
arr3 = self.mesh.face_property('random', fh)
self.assertEqual(arr1[fh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[fh.idx()], arr3))
# multidimensional slices
arr1 = np.random.rand(4, 2, self.mesh.n_faces())
for fh in self.mesh.faces():
self.mesh.set_face_property('random', fh, arr1[:, :, fh.idx()])
arr1 = arr1.transpose(2, 0, 1)
arr2 = self.mesh.face_property_array('random')
self.assertEqual(arr1.shape, arr2.shape)
self.assertTrue(np.allclose(arr1, arr2))
for fh in self.mesh.faces():
arr3 = self.mesh.face_property('random', fh)
self.assertEqual(arr1[fh.idx()].shape, arr3.shape)
self.assertTrue(np.allclose(arr1[fh.idx()], arr3))
if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment