lundi 29 juin 2020

Function to follow replica exchanges from file

I'm trying to write a python script that will follow a set of exchanges recorded in a file.

The file shows lines with "Repl ex 0 x 1 2 3 4 x 5 ......."

The "x" indicates that replica 0 exchanged with replica 1 and replica 4 exchanged with replica 5.

I want to go through the file and create a list for each starting replica that shows all the numbers that it travelled through. So a list for the replica that started at 0 and each replica that it exchanged into during the course of the simulation and the same thing for the replica that started at 1, 2, 3 and so on.

So far I've used regexes to generate a list of lists (called exchanges[i]) of the lines from the "md.log" file where exchanges occur between each pair (i.e. exchanges[0] = file lines where 0 x 1 only).

If we are following the replica that started at 2, this will look for the first exchange that occurs between 1 x 2 or 2 x 3 and add the number of the system that it moves into.

For the next round of searching, a master list (xch_lines) containing all the lines from the file where any exchange occurs is used to determine the line to start from when searching the new adjacent xch_lists.

The code returns a list that appears to follow the replica up and down but eventually it seems to decay into returning a list of 1's and 0's. The list is about twice as long as it should be and it's the second half of the list that seems to be the problem.

import analyze_exchanges

replica = 8

if replica != 0:
    xp = replica - 1
else:
    replica = 0

walker.append(replica)

On = True

while result[0] != 2:
    
    result = find_next_xch(xp, master_index)
    
    if result[0] == 2:
        On = False
        break
    elif result[0] == 0:
        if replica == 0:
            replica += 1
            master_index = result[1]
            walker.append(replica)
            continue
        else:
            replica -= 1
            walker.append(replica)
            master_index = result[1]
            if replica == 0:
                continue
            else:
                xp -= 1
                continue
            
    elif result[0] == 1:
        replica +=1
        walker.append(replica)
        master_index = result[1]
        xp += 1
        continue
    
print(len(walker))
print(walker)

Analyze exchanges module

import re

# regular expressions for each exchange pair
regexes = [r"Repl ex\s+0\s+x\s+1", r"Repl ex.*1\s+x\s+2", r"Repl ex.*2\s+x\s+3",
           r"Repl ex.*3\s+x\s+4", r"Repl ex.*4\s+x\s+5", r"Repl ex.*5\s+x\s+6",
           r"Repl ex.*6\s+x\s+7", r"Repl ex.*7\s+x\s+8", r"Repl ex.*8\s+x\s+9",
           r"Repl ex.*9\s+x\s+10", r"Repl ex.*10\s+x\s+11", r"Repl ex.*11\s+x\s+12",
           r"Repl ex.*12\s+x\s+13", r"Repl ex.*13\s+x\s+14", r"Repl ex.*14\s+x\s+15",
           r"Repl ex.*15\s+x\s+16", r"Repl ex.*16\s+x\s+17", r"Repl ex.*17\s+x\s+18",
           r"Repl ex.*18\s+x\s+19"]

# make lists for each exchange pair that will store the line numbers for each exchange that that pair makes
exchanges = [[] for i in range(19)]
# open the md.log file
f = open('/Users/danielburns/Desktop/md.log', 'r')


# go through the md.log file looking for every time a pair exchanges and add that line number to that exchange pair's list
for i, line in enumerate(f):
    for x in range(19):
        if re.compile(regexes[x]).match(line):
            exchanges[x].append(i)

# master list for all the exchange attempt lines 
xch_lines = []
# reset to beginning of log file
f.seek(0)
# append all the exchange attempt lines to the list
for j, line in enumerate(f):
    if re.compile(r"Repl ex\s+").match(line):
        xch_lines.append(j)

#number of replicas
nreps = 20

#number of exchange lists
maxpair = nreps - 2

# List the replicas that the walker goes through
walker = []

#Starting index for the master list of exchange lines
master_index = -1



def find_next_xch(xp, master_index):
    """
    returns 0 if the next exchange is down and (unless current replica = 1)
    returns 1 if the next exchange is up 

    """
    li = 1
    while True:
        
        
        if xch_lines[li + master_index] >= exchanges[xp][-1]:
            return (2,)
            break
        
        elif xch_lines[master_index + li] in exchanges[xp]:
            master_index = master_index +li
            return (0, master_index)
            break
        elif xp == maxpair:
            li += 1
            continue
        elif xch_lines[master_index + li] in exchanges[xp + 1]:
            master_index = master_index +li
            return (1, master_index)
            break
        
        else:
            li += 1
        


        

The md.log file to be analyzed can be found here https://github.com/DBurns-Lab/Analyze_Exchanges

Aucun commentaire:

Enregistrer un commentaire