@@ -313,7 +313,7 @@ def read(cls, path_or_buf, file_format=None):
313
313
warn ("meshio.Mesh.read is deprecated, use meshio.read instead" )
314
314
return read (path_or_buf , file_format )
315
315
316
- def sets_to_int_data (self ):
316
+ def cell_sets_to_data (self ):
317
317
# If possible, convert cell sets to integer cell data. This is possible if all
318
318
# cells appear exactly in one group.
319
319
default_value = - 1
@@ -341,8 +341,10 @@ def sets_to_int_data(self):
341
341
self .cell_data [data_name ] = intfun
342
342
self .cell_sets = {}
343
343
344
+ def point_sets_to_data (self ):
344
345
# now for the point sets
345
346
# Go for -1 as the default value. (NaN is not int.)
347
+ default_value = - 1
346
348
if len (self .point_sets ) > 0 :
347
349
intfun = np .full (len (self .points ), default_value , dtype = int )
348
350
for i , cc in enumerate (self .point_sets .values ()):
@@ -358,73 +360,61 @@ def sets_to_int_data(self):
358
360
self .point_data [data_name ] = intfun
359
361
self .point_sets = {}
360
362
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 ]
392
390
393
391
# 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 ]
403
393
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 ]
407
397
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." )
409
401
410
- # this call can be rather expensive
411
- tags = np .unique (data )
402
+ tags = np .unique (data )
412
403
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 ]
422
413
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 ]
427
418
428
419
# remove the cell data
429
- for key in rm_keys :
430
- del self .point_data [key ]
420
+ del self .point_data [key ]
0 commit comments