Coverage for gamdpy/misc/extract_scalars.py: 77%

13 statements  

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

1def extract_scalars(data, column_list, first_block=0, D=3): 

2 """ Extracts scalar data from simulation output. 

3 

4 Parameters 

5 ---------- 

6 

7 data : dict 

8 Output from a Simulation object. 

9 

10 column_list : list of str 

11 

12 first_block : int 

13 Index of the first timeblock to extract data from. 

14 

15 D : int 

16 Dimension of the simulation. 

17 

18 Returns 

19 ------- 

20 

21 tuple 

22 Tuple of 1D numpy arrays containing the extracted scalar data. 

23 

24 

25 Example 

26 ------- 

27 

28 >>> import numpy as np 

29 >>> import gamdpy as gp 

30 >>> sim = gp.get_default_sim() # Replace with your simulation object 

31 >>> for block in sim.run_timeblocks(): pass 

32 >>> U, W = gp.extract_scalars(sim.output, ['U', 'W'], first_block=1) 

33 """ 

34 

35 # Indices hardcoded for now (see scalar_calculator above) 

36 column_indices = {} 

37 try: 

38 scalar_names = data['scalar_saver'].attrs['scalar_names'] 

39 except KeyError: 

40 # try the old label 

41 print("Data file uses old format (meta data labelled 'scalars_names' rather than 'scalar_names'); at some point suport for this format will be removed.") 

42 scalar_names = data.attrs['scalars_names'] 

43 

44 for index, name in enumerate(scalar_names): 

45 column_indices[name] = index 

46 

47 output_list = [] 

48 for column in column_list: 

49 output_list.append(data['scalar_saver/scalars'][first_block:,:,column_indices[column]].flatten()) 

50 return tuple(output_list) 

51