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 sorted = False 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 sorted = True 29 # then by replikum 30 if all([re.search(r_pattern, entry) for entry in ll]): 31 ll.sort(key=lambda x: int(re.findall(r_pattern, x)[0])) 32 sorted = True 33 # as the rearrangements by one key let the other key untouched, the list is sorted now 34 35 if not sorted: 36 # fallback 37 sames = '' 38 for i in range(len(ll[0])): 39 checking = ll[0][i] 40 for rn in ll[1:]: 41 is_same = (rn[i] == checking) 42 if is_same: 43 sames += checking 44 else: 45 break 46 print("Using prefix:", sames) 47 ll.sort(key=lambda x: int(re.findall(r'\d+', x[len(sames):])[0])) 48 return ll 49 50 51def check_idl(idl, che): 52 """Checks if list of configurations is contained in an idl 53 54 Parameters 55 ---------- 56 idl : range or list 57 idl of the current replicum 58 che : list 59 list of configurations to be checked against 60 61 Returns 62 ------- 63 miss_str : str 64 string with integers of which idls are missing 65 """ 66 missing = [] 67 for c in che: 68 if c not in idl: 69 missing.append(c) 70 # print missing configurations such that it can directly be parsed to slurm terminal 71 if not (len(missing) == 0): 72 print(len(missing), "configs missing") 73 miss_str = str(missing[0]) 74 for i in missing[1:]: 75 miss_str += "," + str(i) 76 print(miss_str) 77 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 sorted = False 23 r_pattern = r'r(\d+)' 24 id_pattern = r'id(\d+)' 25 26 # sort list by id first 27 if all([re.search(id_pattern, entry) for entry in ll]): 28 ll.sort(key=lambda x: int(re.findall(id_pattern, x)[0])) 29 sorted = True 30 # then by replikum 31 if all([re.search(r_pattern, entry) for entry in ll]): 32 ll.sort(key=lambda x: int(re.findall(r_pattern, x)[0])) 33 sorted = True 34 # as the rearrangements by one key let the other key untouched, the list is sorted now 35 36 if not sorted: 37 # fallback 38 sames = '' 39 for i in range(len(ll[0])): 40 checking = ll[0][i] 41 for rn in ll[1:]: 42 is_same = (rn[i] == checking) 43 if is_same: 44 sames += checking 45 else: 46 break 47 print("Using prefix:", sames) 48 ll.sort(key=lambda x: int(re.findall(r'\d+', x[len(sames):])[0])) 49 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):
52def check_idl(idl, che): 53 """Checks if list of configurations is contained in an idl 54 55 Parameters 56 ---------- 57 idl : range or list 58 idl of the current replicum 59 che : list 60 list of configurations to be checked against 61 62 Returns 63 ------- 64 miss_str : str 65 string with integers of which idls are missing 66 """ 67 missing = [] 68 for c in che: 69 if c not in idl: 70 missing.append(c) 71 # print missing configurations such that it can directly be parsed to slurm terminal 72 if not (len(missing) == 0): 73 print(len(missing), "configs missing") 74 miss_str = str(missing[0]) 75 for i in missing[1:]: 76 miss_str += "," + str(i) 77 print(miss_str) 78 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