mirror of
				https://github.com/fjosw/pyerrors.git
				synced 2025-11-04 09:35:45 +01:00 
			
		
		
		
	Impr/refactor find files (#153)
* wrote small test for ms5_xsf read method * small bug in ms5_xsf found * first version of refactoring with test * built _find_files into openQCD module * postfix can now be used as before * altered test data to be smaller * read_rwms throws better Exception now * typo corrected * better tests for postfixes, also added similar solution for ext variable * added method to sort names of files and replica better
This commit is contained in:
		
					parent
					
						
							
								ee74ec4ed7
							
						
					
				
			
			
				commit
				
					
						0bc08be634
					
				
			
		
					 2 changed files with 44 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -48,10 +48,39 @@ def _find_files(path, prefix, postfix, ext, known_files=[]):
 | 
			
		|||
    if files == []:
 | 
			
		||||
        raise Exception("No files found after pattern filter!")
 | 
			
		||||
 | 
			
		||||
    files.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 | 
			
		||||
    files = _sort_names(files)
 | 
			
		||||
    return files
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _sort_names(ll):
 | 
			
		||||
    r_pattern = r'r(\d+)'
 | 
			
		||||
    id_pattern = r'id(\d+)'
 | 
			
		||||
 | 
			
		||||
    # sort list by id first
 | 
			
		||||
    if all([re.search(id_pattern, entry) for entry in ll]):
 | 
			
		||||
        ll.sort(key=lambda x: int(re.findall(id_pattern, x)[0]))
 | 
			
		||||
    # then by replikum
 | 
			
		||||
    if all([re.search(r_pattern, entry) for entry in ll]):
 | 
			
		||||
        ll.sort(key=lambda x: int(re.findall(r_pattern, x)[0]))
 | 
			
		||||
    # as the rearrangements by one key let the other key untouched, the list is sorted now
 | 
			
		||||
 | 
			
		||||
    else:
 | 
			
		||||
        # fallback
 | 
			
		||||
        sames = ''
 | 
			
		||||
        if len(ll) > 1:
 | 
			
		||||
            for i in range(len(ll[0])):
 | 
			
		||||
                checking = ll[0][i]
 | 
			
		||||
                for rn in ll[1:]:
 | 
			
		||||
                    is_same = (rn[i] == checking)
 | 
			
		||||
                if is_same:
 | 
			
		||||
                    sames += checking
 | 
			
		||||
                else:
 | 
			
		||||
                    break
 | 
			
		||||
            print(ll[0][len(sames):])
 | 
			
		||||
        ll.sort(key=lambda x: int(re.findall(r'\d+', x[len(sames):])[0]))
 | 
			
		||||
    return ll
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
 | 
			
		||||
    """Read rwms format from given folder structure. Returns a list of length nrw
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -142,6 +171,8 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
 | 
			
		|||
    else:
 | 
			
		||||
        rep_names = names
 | 
			
		||||
 | 
			
		||||
    rep_names = _sort_names(rep_names)
 | 
			
		||||
 | 
			
		||||
    print_err = 0
 | 
			
		||||
    if 'print_err' in kwargs:
 | 
			
		||||
        print_err = 1
 | 
			
		||||
| 
						 | 
				
			
			@ -936,12 +967,15 @@ def _read_flow_obs(path, prefix, c, dtr_cnfg=1, version="openQCD", obspos=0, sum
 | 
			
		|||
                if "names" not in kwargs:
 | 
			
		||||
                    raise Exception("Automatic recognition of replicum failed, please enter the key word 'names'.")
 | 
			
		||||
            ens_name = truncated_file[:idx]
 | 
			
		||||
            rep_names.append(ens_name + '|' + truncated_file[idx:])
 | 
			
		||||
            rep_names.append(ens_name + '|' + truncated_file[idx:].split(".")[0])
 | 
			
		||||
        else:
 | 
			
		||||
            names = kwargs.get("names")
 | 
			
		||||
            rep_names = names
 | 
			
		||||
 | 
			
		||||
        deltas.append(Q_top)
 | 
			
		||||
 | 
			
		||||
    rep_names = _sort_names(rep_names)
 | 
			
		||||
 | 
			
		||||
    idl = [range(int(configlist[rep][r_start_index[rep]]), int(configlist[rep][r_stop_index[rep]]) + 1, 1) for rep in range(len(deltas))]
 | 
			
		||||
    deltas = [deltas[nrep][r_start_index[nrep]:r_stop_index[nrep] + 1] for nrep in range(len(deltas))]
 | 
			
		||||
    result = Obs(deltas, rep_names, idl=idl)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -165,3 +165,11 @@ def test_find_files():
 | 
			
		|||
    fpre = "tune62"
 | 
			
		||||
    with pytest.raises(Exception):
 | 
			
		||||
        pe.input.openQCD._find_files(path, fpre, "ms5_xsf_" + qc, "dat")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_sort_names():
 | 
			
		||||
    my_list = ['sfqcd_r1_id5', 'sfqcd_r10_id5', 'sfqcd_r7_id5', 'sfqcd_r2_id5', 'sfqcd_r2_id9', 'sfqcd_r10_id4']
 | 
			
		||||
    presorted_list = ['sfqcd_r1_id5', 'sfqcd_r2_id5', 'sfqcd_r2_id9', 'sfqcd_r7_id5', 'sfqcd_r10_id4', 'sfqcd_r10_id5']
 | 
			
		||||
 | 
			
		||||
    sorted_list = pe.input.openQCD._sort_names(my_list)
 | 
			
		||||
    assert (all([sorted_list[i] == presorted_list[i] for i in range(len(sorted_list))]))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue