Coverage for model_workflow/tools/xvg_parse.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-23 10:54 +0000

1# Read and parse a xvg files 

2# xvg file example: 

3# # This is a comment line 

4# @ This is a comment line 

5# 0.0000000 0.3644627 

6# 10.0000000 0.3536768 

7# 20.0000000 0.3509805 

8 

9# Columns is a list with the name of each column 

10# It returns a dict with as many entries as columns specifided 

11# Each entry has the column name as key and the mined data (list) as value 

12# WARNING: If the xvg file has less columns than specified then the leftover entries will have empty lists 

13# WARNING: If the xvg file has more columns than specified then it will fail 

14def xvg_parse (filename : str, columns : list) -> dict: 

15 data = [ [] for column in columns ] 

16 # Read the specified file line per line 

17 with open(filename, 'r') as file: 

18 lines = list(file) 

19 for line in lines: 

20 # Skip comment lines 

21 first_character = line[0] 

22 if first_character in ['#','@']: 

23 continue 

24 # Useful lines are splitted by spaces 

25 # Splits are saved in as many columns as specified 

26 splits = line.split() 

27 for s, split in enumerate(splits): 

28 data[s].append(float(split)) 

29 # Create the formatted dict to be returned 

30 results = {} 

31 for c, column in enumerate(columns): 

32 results[column] = data[c] 

33 return results