Skip to content

Commit 3e6bdf5

Browse files
authored
Merge pull request #1265 from nschloe/separate-int-data
Separate int data
2 parents 36ddfc7 + 1986272 commit 3e6bdf5

File tree

3 files changed

+62
-70
lines changed

3 files changed

+62
-70
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = meshio
3-
version = 5.2.4
3+
version = 5.2.5
44
author = Nico Schlömer et al.
55
author_email = [email protected]
66
description = I/O for many mesh formats

src/meshio/_mesh.py

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def read(cls, path_or_buf, file_format=None):
313313
warn("meshio.Mesh.read is deprecated, use meshio.read instead")
314314
return read(path_or_buf, file_format)
315315

316-
def sets_to_int_data(self):
316+
def cell_sets_to_data(self):
317317
# If possible, convert cell sets to integer cell data. This is possible if all
318318
# cells appear exactly in one group.
319319
default_value = -1
@@ -341,8 +341,10 @@ def sets_to_int_data(self):
341341
self.cell_data[data_name] = intfun
342342
self.cell_sets = {}
343343

344+
def point_sets_to_data(self):
344345
# now for the point sets
345346
# Go for -1 as the default value. (NaN is not int.)
347+
default_value = -1
346348
if len(self.point_sets) > 0:
347349
intfun = np.full(len(self.points), default_value, dtype=int)
348350
for i, cc in enumerate(self.point_sets.values()):
@@ -358,73 +360,61 @@ def sets_to_int_data(self):
358360
self.point_data[data_name] = intfun
359361
self.point_sets = {}
360362

361-
def int_data_to_sets(self, keys: list[str] | None = None):
362-
"""Convert all int data to {point,cell}_sets, where possible."""
363-
rm_keys = []
364-
for key, data in self.cell_data.items():
365-
if keys is not None:
366-
if key not in keys:
367-
continue
368-
369-
# handle all int and uint data
370-
if not all(v.dtype.kind in ["i", "u"] for v in data):
371-
continue
372-
373-
rm_keys.append(key)
374-
375-
# this call can be rather expensive
376-
tags = np.unique(np.concatenate(data))
377-
378-
# try and get the names by splitting the key along "-" (this is how
379-
# sets_to_int_data() forms the key)
380-
names = key.split("-")
381-
# remove duplicates and preserve order
382-
# <https://stackoverflow.com/a/7961390/353337>:
383-
names = list(dict.fromkeys(names))
384-
if len(names) != len(tags):
385-
# alternative names
386-
names = [f"set-{key}-{tag}" for tag in tags]
387-
388-
# TODO there's probably a better way besides np.where, something from
389-
# np.unique or np.sort
390-
for name, tag in zip(names, tags):
391-
self.cell_sets[name] = [np.where(d == tag)[0] for d in data]
363+
# This used to be int_data_to_sets(), converting _all_ cell and point data.
364+
# This is not useful in many cases, as one usually only wants one
365+
# particular data array (e.g., "MaterialIDs") converted to sets.
366+
def cell_data_to_sets(self, key: str):
367+
"""Convert point_data to cell_sets."""
368+
data = self.cell_data[key]
369+
370+
# handle all int and uint data
371+
if not all(v.dtype.kind in ["i", "u"] for v in data):
372+
raise RuntimeError(f"cell_data['{key}'] is not int data.")
373+
374+
tags = np.unique(np.concatenate(data))
375+
376+
# try and get the names by splitting the key along "-" (this is how
377+
# sets_to_int_data() forms the key)
378+
names = key.split("-")
379+
# remove duplicates and preserve order
380+
# <https://stackoverflow.com/a/7961390/353337>:
381+
names = list(dict.fromkeys(names))
382+
if len(names) != len(tags):
383+
# alternative names
384+
names = [f"set-{key}-{tag}" for tag in tags]
385+
386+
# TODO there's probably a better way besides np.where, something from
387+
# np.unique or np.sort
388+
for name, tag in zip(names, tags):
389+
self.cell_sets[name] = [np.where(d == tag)[0] for d in data]
392390

393391
# remove the cell data
394-
for key in rm_keys:
395-
del self.cell_data[key]
396-
397-
# now point data
398-
rm_keys = []
399-
for key, data in self.point_data.items():
400-
if keys is not None:
401-
if key not in keys:
402-
continue
392+
del self.cell_data[key]
403393

404-
# handle all int and uint data
405-
if not all(v.dtype.kind in ["i", "u"] for v in data):
406-
continue
394+
def point_data_to_sets(self, key: str):
395+
"""Convert point_data to point_sets."""
396+
data = self.point_data[key]
407397

408-
rm_keys.append(key)
398+
# handle all int and uint data
399+
if not all(v.dtype.kind in ["i", "u"] for v in data):
400+
raise RuntimeError(f"point_data['{key}'] is not int data.")
409401

410-
# this call can be rather expensive
411-
tags = np.unique(data)
402+
tags = np.unique(data)
412403

413-
# try and get the names by splitting the key along "-" (this is how
414-
# sets_to_int_data() forms the key
415-
names = key.split("-")
416-
# remove duplicates and preserve order
417-
# <https://stackoverflow.com/a/7961390/353337>:
418-
names = list(dict.fromkeys(names))
419-
if len(names) != len(tags):
420-
# alternative names
421-
names = [f"set{tag}" for tag in tags]
404+
# try and get the names by splitting the key along "-" (this is how
405+
# sets_to_int_data() forms the key
406+
names = key.split("-")
407+
# remove duplicates and preserve order
408+
# <https://stackoverflow.com/a/7961390/353337>:
409+
names = list(dict.fromkeys(names))
410+
if len(names) != len(tags):
411+
# alternative names
412+
names = [f"set-key-{tag}" for tag in tags]
422413

423-
# TODO there's probably a better way besides np.where, something from
424-
# np.unique or np.sort
425-
for name, tag in zip(names, tags):
426-
self.point_sets[name] = np.where(data == tag)[0]
414+
# TODO there's probably a better way besides np.where, something from
415+
# np.unique or np.sort
416+
for name, tag in zip(names, tags):
417+
self.point_sets[name] = np.where(data == tag)[0]
427418

428419
# remove the cell data
429-
for key in rm_keys:
430-
del self.point_data[key]
420+
del self.point_data[key]

tests/test_mesh.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def test_sets_to_int_data():
3030
mesh = helpers.add_point_sets(mesh)
3131
mesh = helpers.add_cell_sets(mesh)
3232

33-
mesh.sets_to_int_data()
33+
mesh.point_sets_to_data()
34+
mesh.cell_sets_to_data()
3435

3536
assert mesh.cell_sets == {}
3637
assert_equal(mesh.cell_data, {"grain0-grain1": [[0, 0, 1, 1, 1]]})
@@ -39,7 +40,8 @@ def test_sets_to_int_data():
3940
assert_equal(mesh.point_data, {"fixed-loose": [0, 0, 0, 1, 1, 1, 1]})
4041

4142
# now back to set data
42-
mesh.int_data_to_sets()
43+
mesh.cell_data_to_sets("grain0-grain1")
44+
mesh.point_data_to_sets("fixed-loose")
4345

4446
assert mesh.cell_data == {}
4547
assert_equal(mesh.cell_sets, {"grain0": [[0, 1]], "grain1": [[2, 3, 4]]})
@@ -56,7 +58,7 @@ def test_sets_to_int_data_warning():
5658
cell_sets={"tag": [[0]]},
5759
)
5860
with pytest.warns(UserWarning):
59-
mesh.sets_to_int_data()
61+
mesh.cell_sets_to_data()
6062
assert np.all(mesh.cell_data["tag"] == np.array([[0, -1]]))
6163

6264
mesh = meshio.Mesh(
@@ -65,7 +67,7 @@ def test_sets_to_int_data_warning():
6567
point_sets={"tag": [[0, 1, 3]]},
6668
)
6769
with pytest.warns(UserWarning):
68-
mesh.sets_to_int_data()
70+
mesh.point_sets_to_data()
6971

7072
assert np.all(mesh.point_data["tag"] == np.array([[0, 0, -1, 0]]))
7173

@@ -74,7 +76,7 @@ def test_int_data_to_sets():
7476
mesh = helpers.tri_mesh
7577
mesh.cell_data = {"grain0-grain1": [np.array([0, 1])]}
7678

77-
mesh.int_data_to_sets()
79+
mesh.cell_data_to_sets("grain0-grain1")
7880

7981
assert_equal(mesh.cell_sets, {"grain0": [[0]], "grain1": [[1]]})
8082

@@ -92,8 +94,8 @@ def test_gh_1165():
9294
},
9395
)
9496

95-
mesh.sets_to_int_data()
96-
mesh.int_data_to_sets()
97+
mesh.cell_sets_to_data()
98+
mesh.cell_data_to_sets("test-sets")
9799

98100
assert_equal(mesh.cell_sets, {"test": [[], [1]], "sets": [[0, 1], [0, 2, 3]]})
99101

0 commit comments

Comments
 (0)