Coverage for gamdpy/tools/print_h5.py: 15%

20 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-06-14 15:25 +0200

1import h5py 

2 

3def print_h5_structure(node, indent=0): 

4 """ Recursively print groups and datasets with metadata of an h5 file. 

5 

6 Example 

7 ------- 

8 

9 >>> import gamdpy as gp 

10 >>> sim = gp.get_default_sim() 

11 >>> for _ in sim.run_timeblocks(): pass 

12 >>> gp.tools.print_h5_structure(sim.output) 

13 initial_configuration/ (Group) 

14 ptype (Dataset, shape=(2048,), dtype=int32) 

15 r_im (Dataset, shape=(2048, 3), dtype=int32) 

16 scalars (Dataset, shape=(2048, 4), dtype=float32) 

17 topology/ (Group) 

18 angles (Dataset, shape=(0,), dtype=int32) 

19 bonds (Dataset, shape=(0,), dtype=int32) 

20 dihedrals (Dataset, shape=(0,), dtype=int32) 

21 molecules/ (Group) 

22 vectors (Dataset, shape=(3, 2048, 3), dtype=float32) 

23 scalar_saver/ (Group) 

24 scalars (Dataset, shape=(8, 64, 3), dtype=float32) 

25 trajectory_saver/ (Group) 

26 images (Dataset, shape=(8, 12, 2048, 3), dtype=int32) 

27 positions (Dataset, shape=(8, 12, 2048, 3), dtype=float32) 

28 

29 """ 

30 for key, item in node.items(): 

31 pad = " " * indent 

32 if isinstance(item, h5py.Dataset): 

33 print(f"{pad}{key} (Dataset, shape={item.shape}, dtype={item.dtype})") 

34 elif isinstance(item, h5py.Group): 

35 print(f"{pad}{key}/ (Group)") 

36 print_h5_structure(item, indent+1) 

37 else: # This should not be relevant 

38 print(f"{pad}{key} (Unknown type: {type(item)})") 

39 

40 

41def print_h5_attributes(obj, path="/"): 

42 """ Recursively print attrs of every group/dataset of an h5 file. 

43 

44 Example 

45 ------- 

46 

47 >>> import gamdpy as gp 

48 >>> sim = gp.get_default_sim() 

49 >>> for _ in sim.run_timeblocks(): pass 

50 >>> gp.tools.print_h5_attributes(sim.output) 

51 Attributes at /: 

52 - dt: 0.005 

53 - script_content: ... 

54 - script_name: ... 

55 Attributes at /initial_configuration/: 

56 - simbox_data: [12.815602 12.815602 12.815602] 

57 - simbox_name: Orthorhombic 

58 Attributes at /initial_configuration/scalars: 

59 - scalar_columns: ['U' 'W' 'K' 'm'] 

60 Attributes at /initial_configuration/topology/molecules/: 

61 - names: [] 

62 Attributes at /initial_configuration/vectors: 

63 - vector_columns: ['r' 'v' 'f'] 

64 Attributes at /scalar_saver/: 

65 - compression_info: gzip with opts 4 

66 - scalar_names: ['U' 'W' 'K'] 

67 - steps_between_output: 16 

68 Attributes at /trajectory_saver/: 

69 - compression_info: gzip with opts 4 

70 

71 """ 

72 # obj could be the File or a Group 

73 if obj.attrs: 

74 print(f"Attributes at {path}:") 

75 for name, val in obj.attrs.items(): 

76 if name == 'script_content' or name == 'script_name': # Exclude since output is unpredictable (and untestable) 

77 print(f' - {name}: ...') 

78 else: 

79 print(f" - {name}: {val}") 

80 # Recurse into sub‐groups/datasets 

81 if isinstance(obj, h5py.Group): 

82 for key, sub in obj.items(): 

83 print_h5_attributes(sub, path + key + ("/" if isinstance(sub, h5py.Group) else ""))