pyerrors.input.utils
1import re 2"""Utilities for the input""" 3 4 5def sort_names(ll): 6 """Sorts a list of names of replika with searches for `r` and `id` in the replikum string. 7 If this search fails, a fallback method is used, 8 where the strings are simply compared and the first diffeing numeral is used for differentiation. 9 10 Parameters 11 ---------- 12 ll: list 13 list to sort 14 15 Returns 16 ------- 17 ll: list 18 sorted list 19 """ 20 if len(ll) > 1: 21 r_pattern = r'r(\d+)' 22 id_pattern = r'id(\d+)' 23 24 # sort list by id first 25 if all([re.search(id_pattern, entry) for entry in ll]): 26 ll.sort(key=lambda x: int(re.findall(id_pattern, x)[0])) 27 # then by replikum 28 if all([re.search(r_pattern, entry) for entry in ll]): 29 ll.sort(key=lambda x: int(re.findall(r_pattern, x)[0])) 30 # as the rearrangements by one key let the other key untouched, the list is sorted now 31 32 else: 33 # fallback 34 sames = '' 35 if len(ll) > 1: 36 for i in range(len(ll[0])): 37 checking = ll[0][i] 38 for rn in ll[1:]: 39 is_same = (rn[i] == checking) 40 if is_same: 41 sames += checking 42 else: 43 break 44 print("Using prefix:", ll[0][len(sames):]) 45 ll.sort(key=lambda x: int(re.findall(r'\d+', x[len(sames):])[0])) 46 return ll 47 48 49def check_idl(idl, che): 50 """Checks if list of configurations is contained in an idl 51 52 Parameters 53 ---------- 54 idl : range or list 55 idl of the current replicum 56 che : list 57 list of configurations to be checked against 58 59 Returns 60 ------- 61 miss_str : str 62 string with integers of which idls are missing 63 """ 64 missing = [] 65 for c in che: 66 if c not in idl: 67 missing.append(c) 68 # print missing configurations such that it can directly be parsed to slurm terminal 69 if not (len(missing) == 0): 70 print(len(missing), "configs missing") 71 miss_str = str(missing[0]) 72 for i in missing[1:]: 73 miss_str += "," + str(i) 74 print(miss_str) 75 return miss_str
def
sort_names(ll):
6def sort_names(ll): 7 """Sorts a list of names of replika with searches for `r` and `id` in the replikum string. 8 If this search fails, a fallback method is used, 9 where the strings are simply compared and the first diffeing numeral is used for differentiation. 10 11 Parameters 12 ---------- 13 ll: list 14 list to sort 15 16 Returns 17 ------- 18 ll: list 19 sorted list 20 """ 21 if len(ll) > 1: 22 r_pattern = r'r(\d+)' 23 id_pattern = r'id(\d+)' 24 25 # sort list by id first 26 if all([re.search(id_pattern, entry) for entry in ll]): 27 ll.sort(key=lambda x: int(re.findall(id_pattern, x)[0])) 28 # then by replikum 29 if all([re.search(r_pattern, entry) for entry in ll]): 30 ll.sort(key=lambda x: int(re.findall(r_pattern, x)[0])) 31 # as the rearrangements by one key let the other key untouched, the list is sorted now 32 33 else: 34 # fallback 35 sames = '' 36 if len(ll) > 1: 37 for i in range(len(ll[0])): 38 checking = ll[0][i] 39 for rn in ll[1:]: 40 is_same = (rn[i] == checking) 41 if is_same: 42 sames += checking 43 else: 44 break 45 print("Using prefix:", ll[0][len(sames):]) 46 ll.sort(key=lambda x: int(re.findall(r'\d+', x[len(sames):])[0])) 47 return ll
Sorts a list of names of replika with searches for r
and id
in the replikum string.
If this search fails, a fallback method is used,
where the strings are simply compared and the first diffeing numeral is used for differentiation.
Parameters
- ll (list): list to sort
Returns
- ll (list): sorted list
def
check_idl(idl, che):
50def check_idl(idl, che): 51 """Checks if list of configurations is contained in an idl 52 53 Parameters 54 ---------- 55 idl : range or list 56 idl of the current replicum 57 che : list 58 list of configurations to be checked against 59 60 Returns 61 ------- 62 miss_str : str 63 string with integers of which idls are missing 64 """ 65 missing = [] 66 for c in che: 67 if c not in idl: 68 missing.append(c) 69 # print missing configurations such that it can directly be parsed to slurm terminal 70 if not (len(missing) == 0): 71 print(len(missing), "configs missing") 72 miss_str = str(missing[0]) 73 for i in missing[1:]: 74 miss_str += "," + str(i) 75 print(miss_str) 76 return miss_str
Checks if list of configurations is contained in an idl
Parameters
- idl (range or list): idl of the current replicum
- che (list): list of configurations to be checked against
Returns
- miss_str (str): string with integers of which idls are missing