Beispiel #1
0
def main(arg1, arg2, arg3, arg4, polyScript, filteredIDs,popSizes):
    
    print arg1, arg2, arg3, arg4
    
    if __name__ != '__main__':
        sys.argv = ['RollVac.py',arg1,arg2,arg3,arg4]
    
    if arg1 == "poly":
        del sys.argv[3:5]

 
    trigger = 1
    subnum = 0
    path = ""
    population = ""
    useExplicit = False
    explicitDirectory = ""
    subpopDirectory = ""
    useBase = False
    useRaw =  False
    baseFile = "" 
    pos = 0
    iCode = 0
    
    vacTotal =  0
    avTotal = 0
    socialTotal = 0
    workTotal = 0
    schoolTotal = 0
    sqTotal = 0
    avTreatments = 0
    try:
        toFilterIDs = len(filteredIDs) > 0
    except:
        toFilterIDS = False
    useNew = False
    subpopsNew = []
    actionsNew = []
    totalsNew =  {'vaccination':1000000000,'antiviral':1000000000}
    interventionsNew = []
    

# LOADS PUBLIC/ PRIVATE DATA FROM GDOC IF PASSED
    if len(sys.argv) < 3:
        print "Missing arguments, quitting now"
        quit()
        outName = "Default"
    
    elif len(sys.argv) > 3:
   
        if sys.argv[1] == "gdoc" or sys.argv[1] == "poly":
            arg = "gDoc"
            if len(sys.argv) == 4:
                sys.argv.insert(3,'null') 
            else:
                print "Ignoring", len(sys.argv) - 3, "excess arguments\n"
    elif len(sys.argv) == 3:
        if sys.argv[1] == "gdoc" or sys.argv[1] == "poly":
            arg = "gDoc" 
            sys.argv.insert(2,'null')
            sys.argv.insert(2,'null')
        else:
            outName = sys.argv[2]
            arg = sys.argv[1]


# LOADS AND WRITES AV/ DIAG SCRIPT FROM GOOGLE DOC, NOT IMPLEMENTED FOR USER/ LOCAL SCRIPT MODE
                                               
    print sys.argv
    if arg == "gDoc":
        
        if sys.argv[1] == "poly":
            isPoly = True
            sys.argv[3] = 'null'
            path = arg2
            if os.path.exists(path):
                shutil.rmtree(path)
            os.makedirs(path)
            os.makedirs(path + "/output")
        else:
            isPoly = False
        loadType = "intervention script"
        print sys.argv
        script = gDocsImport.getScript(sys.argv[2], sys.argv[3], sys.argv[4], startWord, stopWord, loadType, isPoly, polyScript)
        params = gDocsImport.getLine(sys.argv[2], sys.argv[3], sys.argv[4],paramsStart, isPoly, polyScript)
        
        emptyblock = script == 'null'
        
        explicitDirectory= params[2]
        if len(explicitDirectory) > 0:
            useExplicit  = True
            
        try:
            shiftDates =  int(params[8])
            print "Date shift set to", shiftDates
        except:
            shiftDates = 0
            print "Using default '0' date shift"
        
        if isPoly:
            outName = ""
            baseFile = params[4]
            subpopDirectory = params[6]
            if len(baseFile) > 0:
                if os.path.exists(baseFile):
                    print "Appending interventions to base file", baseFile
                    useBase = True
                else:
                    print "Error, base file", baseFile, "not found"
                    quit()
                     
            
        else:
            outName = params[0]

        if len(outName) == 0:
            print "No name prefix stored, using default intervention, antiviral, and diagnosis"
            outName = ""
            
        print "Will write to intervention file '%sIntervention'\n" % outName

        diag = params[1]
        diagnosis = isYes(diag, "diagnosis")
        useNew = isYes(params[7], "new style")
        
        try:
            appendScript =  gDocsImport.getScript(sys.argv[2], sys.argv[3], sys.argv[4], avStop, 0, loadType, isPoly, polyScript)
            appendScript = appendRaw(appendScript)
            useRaw = appendScript != 'null'
            if useRaw:
                print "\nRaw script footer loaded succesfully,", appendScript.count('\n') - 3, "lines to append"
        except:
            print "Error, raw script footer not loaded"
        
        if not diagnosis:
            sys.argv[3] = "null"
        else:
            loadType = "default"
            avScript = gDocsImport.getScript(sys.argv[2], sys.argv[3], sys.argv[4], avStart, avStop, loadType, isPoly, polyScript)
            diagParams = gDocsImport.getLine(sys.argv[2], sys.argv[3], sys.argv[4], stopWord, isPoly, polyScript)
            sys.argv[3] = "null"               
            
            if not useNew:
                if avScript == 'null':
                    print "No AV-Script found"
                    temp = open(path+'/Antiviral', 'w')
                    temp.write("#Null file: AV-Scripting enabled though no interventions found")
                    temp.close()
                    temp = open(path+'/Diagnosis', 'w')
                    temp.write("#Null file: AV-Scripting enabled though no interventions found")
                    temp.close()
                    avTreatments = 0
                else:
                    avTreatments = writeAvScript(avScript, diagParams, outName, path, subpopDirectory, shiftDates)
                
                
# PREP NEW FORMAT DICTS
 
            else:
                if avScript == 'null':
                    print "No AV-Script found"
                    avTreatments = 0
                else:
                    temp = prepNewAV(avScript, diagParams, outName, path, subpopDirectory, totalsNew, shiftDates)
                    subpopsNew += temp['subpops']
                    actionsNew += temp['actions']
                    interventionsNew += temp['interventions']
                    """print subpopsNew
                    print actionsNew
                    print interventionsNew"""
                    avTreatments = len(actionsNew)
        
    if arg != "user" and arg != "gDoc" and arg != "gdoc" and (not os.path.isfile(arg)):
        print arg
        print "Error, cannot open file or directory\n"
        quit()     
        
    done = False
    
    
# LOCAL SCRIPT FILE LOADING
      
    if arg != "user" and arg != "gDoc":
        scriptfile = open(arg)    
        script = []    
        line = 0
        gotPath = False
        while True:
                testline = scriptfile.readline()
                if len(testline) == 0:
                    break
                if not testline.startswith("#"):
                    if testline.startswith("Directory"):
                        gotPath =  True
                        temp = testline.split()
                        if len(temp) <2:
                            path = ""
                        else:
                            path = testline.split()[1]
                            if (path == "local" or path == "Local"):
                                path = ""
                    else:
                        script.append(testline)
                        line += 1
        if not gotPath:
            path = ""
        

# FLUSH INTERVENTION FILE & COPY POLYRUN BASEFILE IF NEEDED
    
    writePath = path + outName + 'Intervention'
    if useNew:
        writePath += 'New'
    if useBase:
        shutil.copy2(baseFile, writePath)
        outFile = open(writePath)
        temp = outFile.read()
        outFile.close()
        outFile = open(writePath, 'w')
        outFile.write("# ----- PolyRun.Py Appended Intervention File -----\n\n")
        outFile.write(temp)
        outFile.write("\n\n# ----- End of Appended Intervention File -----\n\n")
    else:
        outFile = open(writePath, 'w')
    if useNew:
        outFile.write("# ----- RollVac.Py Autogenerated Intervention File New Version-----\n\n")
    else:
        outFile.write("# ----- RollVac.Py Autogenerated Intervention File Old Version-----\n\n")
    outFile.close()

        
# GENERATING OUTPUT FILE LOOP
       
    while not done and emptyblock == False:
    
        subnum += 1
        enum = False
    
                                                                              
#  LOCAL SCRIPT/ GDOC CONTROLLED CHOPPING                                                                                                                                                                                           
                                                                                                                                                                                                
        if True:
            if pos == len(script):
                done = True
                break
                
            cmd = script[pos]
            items = cmd.split()
            population =  (subpopDirectory + '/' + items[0]).replace('//','/')
            try:
                with open(population): pass
            except:
                print "Error: population file", population, "not found\n"
                quit()
            
            if str(items[1]) == 'enum':
                enum = True
                cmd = cmd.replace("enum","null null")
                items = cmd.split()
                pos += 1
                enumList = script[pos].replace(";"," ; ")
                if checkEnum(enumList):
                    enumList = parseEnum(enumList)
                else:
                    print "Error: misformatted enumeration list", enumList
                    quit()
            
            if not enum:
                try:
                    day = int(items[1]) + shiftDates
                    if day <= 0:
                        print "Error, day must be an integer value\n"
                        quit()
                except:
                    print "Error, day must be an integer greater than zero\n"
                    quit()
                try:
                    length = int(items[2])
                    if length <= 0:
                        print "Error, length must be an integer greater than zero\n"
                        quit()
                except:
                    print "Error, length must be an integer greater than zero\n"
                    quit()
            
            temp = cmd.split()
            print "Generating interventions with parameters:",cmd
            method = temp[3]
            arguments = len(temp)
            
            if method == "Vaccination":
                target = 7
                meth = "v"
                iCode = 0
                if arguments == 7:
                    temp[5],temp[6] = temp[6],temp[5]
            elif method == "Antiviral":
                target = 8
                meth = "av"
                iCode = 1000
                if arguments == 8:
                    temp[5],temp[6],temp[7] = temp[6],temp[7],temp[5]
            elif method == "SocialDistancing":
                target = 6
                meth = "sd"
                iCode = 2000
            elif method == "WorkClosure":
                target = 6
                meth = "cw"
                iCode = 3000
            elif method == "SchoolClosure":
                target = 6
                meth = "cs"
                iCode = 4000
            elif method == "Sequestion":
                target = 7
                meth = "sq"
                iCode = 6000
            else:
                print "Error:", method, "method not recognized\n"
                quit()
            if arguments != target:
                print "Error:",  len(temp), "parameters found,", target, "expected for intervention type", method, "\n"
                quit()
            
            
            interv = " ".join(temp[3:target])
            intervNew = temp[3:target]
            pos += 1
    
        suffix = str(subnum) + meth


# ALL MODE CHOPPING EXECUTION

        print population
        if not useNew:
            pos2 = 0
            limit = len(filteredIDs)
            found = False
            while pos2 < limit:
                if population in filteredIDs[pos2]['directory']:
                    found = True
                    runIDs = filteredIDs[pos2]['ids']
                    break
                pos2 += 1
            
            if not found:
                filteredIDs.append(filterIDs(population))
                runIDs = filteredIDs[-1]['ids']
            
            if enum:
                populationSize = checkSize(population,popSizes)
                enumList = cleanEnum(percentEnum(enumList,populationSize))
                holder = chopper.main(population,'e'," ".join(map(str, enumList)),suffix, path,runIDs)
                returnSize = holder['count']
                enumList = holder['enum']
                length = chopper.getEnumSize(enumList)
            else:
                holder = chopper.main(population,'b',str(length),suffix, path, runIDs)
                returnSize = holder['count']
        else:
            addSubpop(subpopsNew, population.split('/')[-1], population, 9000+ len(subpopsNew))

            if enum:
                populationSize = checkSize(population,popSizes)
                temp = countEnum(enumList,populationSize)
                enumList = chopper.trimEnum(cleanEnum(temp['enum']), populationSize)
                length = chopper.getEnumSize(enumList)
                returnSize = int(min(temp['total'],populationSize))
            else:
                populationSize = returnSize = checkSize(population,popSizes)
                


# NON TREATMENT BASED INTERVENTION TRACKING            
    
        if meth == "v":
            vacTotal += returnSize
        elif meth == "av":
            avTotal += returnSize
        elif meth == "sd":
            socialTotal += returnSize
        elif meth == "cw":
            workTotal += returnSize
        elif meth == "cs":
            schoolTotal += returnSize
        elif meth == "sq":
            sqTotal += returnSize
        
                
# WRITING INTERVENTION FILE (AV TREATMENT & DIAG HANDLED SEPERATELY)

        writePath = path + outName + 'Intervention'
               
        pos2 = 0
        popName = population.split('/')[-1]
        
        if not useNew:
            outFile = open(writePath, 'a+b')
            if enum:
                while pos2 < len(enumList):
                    subPopName = popName + 'd' + str(pos2/2) + 'i' + suffix
                    if ".txt" in subPopName:
                        subPopName = subPopName.replace('.txt','') + '.txt'
                    triggerOut = "* Trigger " + str(trigger+iCode) + " Date " + str(enumList[pos2] + shiftDates) + "\n" 
                    if useExplicit:
                        tempPath = path + "/subpops/" + subPopName
                    else:
                        tempPath = "subpops/" + subPopName
                    intervOut = "* Action " + str(trigger+iCode) + " " + interv + " " + tempPath + "\n"
                    intervOut =  intervOut.replace('//','/')
                    print triggerOut, intervOut.replace('\n','')
                    outFile.write(triggerOut)
                    outFile.write(intervOut)
                    trigger += 1
                    pos2 += 2
                
            else:
                while pos2 < length:
                    subPopName = popName + 'd' + str(pos2) + 'i' + suffix
                    if ".txt" in subPopName:
                        subPopName = subPopName.replace('.txt','') + '.txt'
                    triggerOut = "* Trigger " + str(trigger+iCode) + " Date " + str(day+pos2) + "\n" 
                    if useExplicit:
                        tempPath = path + "/subpops/" + subPopName
                    else:
                        tempPath = "subpops/" + subPopName
                    intervOut = "* Action " + str(trigger+iCode) + " " + interv + " " + tempPath + "\n"
                    intervOut =  intervOut.replace('//','/')
                    print triggerOut, intervOut.replace('\n','')
                    outFile.write(triggerOut)
                    outFile.write(intervOut)
                    trigger += 1
                    pos2 += 1
            
            outFile.close()
            print
        else:
            writePath += 'New'
            tempAction = prepNewAction()
            #tempInterv = [prepNewIntervention()]*len(enumList/2)
            tempAction['actionID'] = actionID = str(trigger + iCode)
            tempAction['actionType'] = method
            if method == 'Antiviral':
                method += "_Prophylaxis"
            tempAction['actionDescription'] = method
            tempAction['actionDelay'] = '0'
            tempAction['actionDuration'] = intervNew[1]
            conditionTotal = -1
            if method == "Vaccination":
                tempAction['actionEfficacy'] = intervNew[3]
                conditionTotal = '9100'
            elif method == "Antiviral":
                tempAction['actionEfficacyIn'] = intervNew[3]
                tempAction['actionEfficacyOut'] = intervNew[4]
                conditionTotal = '9101'
            elif method == "Sequestion":
                tempAction['actionGroupSize'] = intervNew[3]
            if enum:    
                tempInterv = []
                while pos2 < len(enumList):
                    tempInterv.append(prepNewIntervention())
                    tempInterv[pos2/2]['interventionID'] = str(9300 + len(interventionsNew) + pos2/2)
                    tempInterv[pos2/2]['interventionType'] = "Offline"
                    if conditionTotal != -1:
                        tempInterv[pos2/2]['conditionTotal'] = conditionTotal
                    tempInterv[pos2/2]['conditionDate'] = str(enumList[pos2] + shiftDates) + '~' + str(enumList[pos2] + shiftDates)
                    tempInterv[pos2/2]['conditionMembership'] = getSubpopID(subpopsNew,popName)
                    tempInterv[pos2/2]['conditionCompliance'] = str(float(intervNew[2]) * enumList[pos2+1])
                    tempInterv[pos2/2]['action'] = actionID
                    pos2 += 2
            else:
                tempInterv = []
                for pos2 in range(length):
                    tempInterv.append(prepNewIntervention())
                    tempInterv[pos2]['interventionID'] = str(9300 + len(interventionsNew) + pos2)
                    tempInterv[pos2]['interventionType'] = "Offline"
                    if conditionTotal != -1:
                        tempInterv[pos2]['conditionTotal'] = conditionTotal
                    tempInterv[pos2]['conditionDate'] = str(day+pos2+shiftDates) + '~' + str(day+pos2+shiftDates)
                    tempInterv[pos2]['conditionMembership'] = getSubpopID(subpopsNew,popName)
                    tempInterv[pos2]['conditionCompliance'] = str(float(intervNew[2]) / length)
                    tempInterv[pos2]['action'] = actionID
                    subPopName = popName + 'd' + str(pos2+1) + 'i' + suffix
                    if ".txt" in subPopName:
                        subPopName = subPopName.replace('.txt','') + '.txt'
            actionsNew.append(tempAction)
            interventionsNew += tempInterv


# AUTOGENERATE NEW FORMAT MUTEXES           
                                              
    if useNew:
        vMutex = []; avMutex = []
        sdMutex = []; cwMutex = []
        csMutex = []; sqMutex = []
        limit = len(actionsNew)
        print "Generating", limit, "new format actions"
        for pos in range(limit):
            if actionsNew[pos]['actionType'] == "Vaccination":
                if actionsNew[pos]['actionID'] not in vMutex:
                    vMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "Antiviral":
                if actionsNew[pos]['actionID'] not in avMutex:
                    avMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "SocialDistancing":
                if actionsNew[pos]['actionID'] not in sdMutex:
                    sdMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "CloseWork":
                if actionsNew[pos]['actionID'] not in cwMutex:
                    cwMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "CloseSchools":
                if actionsNew[pos]['actionID'] not in csMutex:
                    csMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "Sequestion":
                if actionsNew[pos]['actionID'] not in sqMutex:
                    sqMutex.append(actionsNew[pos]['actionID'])
        limit = len(interventionsNew)
        print "Generating", limit, "new format interventions"
        for pos in range(limit):
            temp = getActionType(actionsNew,interventionsNew[pos]['action'])
            if temp == "Vaccination":
                interventionsNew[pos]['conditionMutex'] = ';'.join(vMutex)
            elif temp == "Antiviral":
                interventionsNew[pos]['conditionMutex'] = ';'.join(avMutex)
            elif temp == "SocialDistancing":
                interventionsNew[pos]['conditionMutex'] = ';'.join(sdMutex)
            elif temp == "CloseWork":
                interventionsNew[pos]['conditionMutex'] = ';'.join(cwMutex)
            elif temp == "CloseSchools":
                interventionsNew[pos]['conditionMutex'] = ';'.join(csMutex)
            elif temp == "Sequestion":
                interventionsNew[pos]['conditionMutex'] = ';'.join(sqMutex)

            
# APPENDING NON TREATMENT INTERVENTION TOTALS    
   
    outFile = open(writePath, 'a+b')
    if useNew:
        outFile.write(getOutputNew(subpopsNew, totalsNew, actionsNew, interventionsNew))
    sumIntervs = vacTotal + avTotal + socialTotal + workTotal + schoolTotal + sqTotal + avTreatments
    if sumIntervs == 0:
        if useRaw:
            print "No scripted interventions found, using iterated manual script"
            outFile.write(appendScript)
        else:
            print "No scripted interventions or raw script found"
    else:
        if useRaw:
            print "Appending manually iterated script"
            outFile.write(appendScript)
    
        outFile.write("\n#----- End of Generated Intervention File -----\n\n")
        outFile.write("\n#Use only one intervention of each type per sub pop, using enumerated interventions for complex interventions.")
        outFile.write("\n# Vaccination: " + str(vacTotal))
        outFile.write("\n# Antiviral Prophylaxis: " + str(avTotal))
        outFile.write("\n# Social Distancing: " + str(socialTotal))
        outFile.write("\n# Close Work: " + str(workTotal))
        outFile.write("\n# Close Schools: " + str(schoolTotal))
        if useNew:
            outFile.write("\n# Sequestered: " + str(sqTotal))
        outFile.write("\n# AV Treatment Programs: " + str(avTreatments))
        outFile.close()
        print """RollVac.Py Pre Compliance Intervention Totals- calculated per output,
does not account for over-application to a given set of IDs. 
Please apply only one of each type per sub pop, using enumerated 
interventions for complex interventions."""
        print "\nVaccination: " + str(vacTotal)
        print "Antiviral Prophylaxis: " + str(avTotal)
        print "Social Distancing: " + str(socialTotal)
        print "Close Work: " + str(workTotal)
        print "Close Schools:" + str(schoolTotal)
        if useNew:
            print "Sequestered:" + str(sqTotal)   
        print "AV Treatment Programs: " + str(avTreatments)
        print
        if toFilterIDs:
            return filteredIDs
Beispiel #2
0
def giSpyGDILoad(gDocURL,directory):
    """Updates user config & lists for GDI seeker"""
    gdi = {}
    print "Loading user account info from local directory"
    account = loadGDIAccount(gDocURL,directory)
    public = account['public']
    
    print "Loading email lists from local directory"
    
    if not public:
        emails = gd.getLine(account['userName'], account['password'], account['fileName'], gdiEmail, False, [])
    else:
        emails = gd.getLine('null', 'null', gDocURL, gdiEmail, False, [])
    
    try:
        gdi['Email'] = emails[0].replace(' ',',')
    except:
        gdi['Email'] = ""
    try:
        gdi['CC'] = emails[1].replace(' ',',')
    except:
        gdi['CC'] = ""
        
    gdi['URL'] = gDocURL
    gdi['UserName'] = account['userName']
    gdi['Password'] = account['password']
    gdi['FileName'] = account['fileName']
    try:
        gdi['Frequency'] = int(emails[2])
    except:
        gdi['Frequency'] = account['frequency']
    
    print "Loading word lists from local directory"
    
    if not public:
        config = gd.getScript(account['userName'], account['password'], account['fileName'], gdiParams, gdiLists, "default", False, [])
    else:
        config = gd.getScript('null', 'null', gDocURL, gdiParams, gdiLists, "default", False, [])
        
    cfg = getConfig(config)
    cfg['OutDir'] = account['name'] + '/'
    cfg['FileName'] = account['name']
    cfg['Sanitize'] =  account['sanitize']
    
    if type(account['login']) is list:
        cfg['Logins'] = account['login']
    else:
        cfg['Logins'] = [account['login']]
    cfg['GDI'] = gdi
    cfg['UseGDI'] = True
    
    #if cfg['OnlyKeepNLP'] != False and cfg['OnlyKeepNLP'] != 'null':
    #    if type(cfg['OnlyKeepNLP']) != list:
    #        cfg['OnlyKeepNLP'] = [str(cfg['OnlyKeepNLP'])]
    
    if not public:
        uglyLists = gd.getScript(account['userName'], account['password'], account['fileName'], gdiLists, -1, "default", False, [])
    else:
        uglyLists = gd.getScript('null', 'null', gDocURL, gdiLists, -1, "default", False, [])

    conditions = []
    qualifiers = set()
    exclusions = set()
    for pos in range(len(uglyLists)):
        row = uglyLists[pos]
        if len(str(row[0])) != 0:
            conditions.append(row[0])
        if len(str(row[1])) != 0:
            qualifiers.add(row[1])
        if len(str(row[2])) != 0:
            exclusions.add(row[2])
    lists = {'conditions':conditions,'qualifiers':qualifiers,'exclusions':exclusions}
    return {'lists':lists,'config':cfg,'login':account['login']}
Beispiel #3
0
def main():
    

# USED TO DEFINE LOADING POSITIONS, MUST BE CONSERVED IN SOURCE AND GDOC
        
    paramsStart = "Study Name Prefix (optional),Diagnosis Based"
    startWord = "Subpopulation,Day/'enum',Length of Spread"
    
    vacsRolled = 0
    
    totalVars = 0
    varList = []
    varSets = []
    suffixes = []
    directorySuffix = ""
    filteredIDs = [{"directory":'null','ids':[]}]
    popSizes = dict()
#    positions = []

# PARSING COMMAND LINE ARGUMENTS FOR PUBLIC/ PRIVATE FILE ACCESS        
    
                    
    if len(sys.argv) > 2:
        if len(sys.argv) == 3:
            sys.argv.insert(2,'null') 
        else:
            print "Ignoring", len(sys.argv) - 2, "excess arguments\n"
    elif len(sys.argv) == 2:
            sys.argv.insert(1,'null')
            sys.argv.insert(1,'null')

    script = gDocsImport.getScript(sys.argv[1], sys.argv[2], sys.argv[3], 0, -1, "default", False, [])
    directoryLines = gDocsImport.getScript(sys.argv[1], sys.argv[2], sys.argv[3], paramsStart, startWord, "default", False,[])
    sys.argv = None
    
# ERASES DIRECTORY NAMES GIVEN BY GDOC
    
    directories = []
    for directory in directoryLines:
        dirToFlush =(directory[2] + '/' + directory[0]).replace('//','/')
        if not dirToFlush in directories: 
            directories.append(dirToFlush)
    
    directorySuffix = flushDirectories(directories)
    
    
# CREATES LISTS OF ALL EXPERIMENTAL VARIABLES ITERATED OVER
            
    length = len(script)
    
    print "Searching list for experimentally iterated variables"
    
    for item in script:
        temp = getPoly(item)
        if temp[0] != 'null':
            if temp[0] not in varSets:
                totalVars += 1
                varList.append(temp[0])
            varSets.append(temp[0]) 
            suffixes.append(temp[1])    
            
    varList.sort()
    length = len(varSets)
    varMatrix = [[] for x in xrange(totalVars)]
    suffixMatrix = [[] for x in xrange(totalVars)]
#    positionMatrix = [[] for x in xrange(totalVars)]
    
    
# CREATES MATRICES OF SUFFIXES, VARIABLE ID, AND LIST POSITION FOR ITERATION  
      
    for pos2 in range(totalVars):              
        for pos1 in range(length):
            if varSets[pos1] == varList[pos2] and (suffixes[pos1] not in suffixMatrix[pos2]):
                varMatrix[pos2].append(varSets[pos1])
                suffixMatrix[pos2].append(suffixes[pos1])
#                positionMatrix[pos2].append(positions[pos1])
    

# SETS UP RUN BOUNDS
        
    runTracker = [0] * totalVars
    done = False
    totalRuns = 1
    ends = []
    for pos in range(totalVars):
        totalRuns *= len(varMatrix[pos])
        ends.append(len(varMatrix[pos])-1)
        if pos >= 1:
            ends[pos] += 1
        
    
# ITERATED RUN GENERATION, IF SCRIPT LINE CONTAINS CURRENT ITERATION RUN MARKERS OR NONE, SENT TO RollVac TO PARSE    
    
    while not done:      
            
        toRun  =  []
        for pos in range(totalVars):
            toRun.append(suffixMatrix[pos][runTracker[pos]])      
        
        length = len(script)
        rollScript = []
        print "Loading Script for line:", runTracker
        for item in script:
            if getPoly(item)[0] == 'null' or getPoly(item)[1] in toRun:
                rollScript.append(filterPoly(item))
                #if len(item.replace('"','').replace(',','').replace('\n','')) != 0:
                    
                
# SUPPORT ADDED FOR MULTIPLE DIRECTORIES/ RUN         
                
        params = gDocsImport.getLine('null', 'null','null', paramsStart, True, rollScript)
        folder = params[2] + '/' + params[0]
        directory = appendSuffix(folder,directorySuffix) 

# OUT DIRECTORY GENERATED VIA SUFFIX MATRIX
                                
        for pos in range(totalVars):
            directory += suffixMatrix[pos][runTracker[pos]] + '/'
            
        params = gDocsImport.loadNClean(False, rollScript, paramsStart, startWord, "single line")
        print params
        
        homeDir = appendSuffix(params[2] + '/' + params[0], directorySuffix)
        explicit =  appendSuffix(params[2], directorySuffix)
        
        needsReplace = len(params[5]) > 0
        noAVDiag = not RollVac.isYes(params[1],'null')
        fileString =  params[3]
        filesToCopy = len(fileString) > 0
            
        filteredIDs = RollVac.main('poly', directory, 'null', 'null', rollScript, filteredIDs, popSizes)
        sleep(0.05)
        
        qsubs = open(homeDir + 'qsublist', 'a+b')
        qsubs.write(("qsub " + directory + 'qsub\n').replace('//','/'))   
        qsubs.close()
        
        if noAVDiag: 
            extraCommands = ["Find = DiagnosisFile =",
                "Replace = ",
                "Find =  AntiviralFile = ",
                "Replace = "]
        else:
            extraCommands = 'null'
                
        if needsReplace:
            replaceFile = params[5]
        else:
            replaceFile = 'null'
               
        if needsReplace or noAVDiag:
            replaceScript = loadReplaceScript(replaceFile, extraCommands)    
            findNReplace(fileString, replaceScript, directory, vacsRolled, homeDir, explicit)
        
        if filesToCopy and not needsReplace:
            print fileString, directory       
            fileCopy(fileString, directory)
            
        vacsRolled += 1


# POLYRUN MULTIDIMENSIONAL LOOP ITERATOR

        if totalVars == 0:
            print "No PolyRun iteration operators found, running in singular mode."
            done = True
            break
            
        pos = 0 
        justRolled = False
 
        while runTracker[pos] == ends[pos] and pos < totalVars - 1:
            justRolled = True
            runTracker[pos] = 0
            runTracker[pos+1] += 1
            pos += 1
            
        if pos == totalVars - 1 and runTracker[pos] == ends[pos]:
            print "Interventions Iterated =", vacsRolled
            done = True
            break
         
        if not justRolled:
            runTracker[0] += 1   
            
    print "Intervention iteration succesfully complete!"
def main():
    if len(sys.argv) > 2:
        if len(sys.argv) == 3:
            sys.argv.insert(2,'null') 
        else:
            print "Ignoring", len(sys.argv) - 2, "excess arguments\n"
    elif len(sys.argv) == 2:
            sys.argv.insert(1,'null')
            sys.argv.insert(1,'null')

    params = gd.getLine(sys.argv[1], sys.argv[2], sys.argv[3], paramsLine, False, [])
    script = gd.getScript(sys.argv[1], sys.argv[2], sys.argv[3], toFromLine, EFO6Line, "default", False, [])
    directories = gd.getScript(sys.argv[1], sys.argv[2], sys.argv[3], EFO6Line, -1, "default", False, [])
    sys.argv = None
    
    outDir = prepDir(params[0])
    subpopDir = prepDir(params[1])
    filesOut = []
    durations = {}
    fromPops = []
    
    for line in script:
        if len(line[0]) == 0 or len(line[1]) == 0:
            print "Error, missing to or from subpop name, line:", line
            quit()
        if line[1] not in fromPops:
            fromPops.append(line[1])
    for line in directories:
        if len(line[0]) == 0 or len(line[1]) == 0:
            print "Error, missing file or directory name, line:", line
            quit()
        if line[0] not in durations:
            durations[line[0]] = 0
        durations[line[0]] = max(durations[line[0]],getLength(line[1]))
    
    if not os.path.isdir(outDir):
        os.mkdir(outDir)
    
    for line in directories:
        if line[0] not in filesOut:
            filesOut.append(line[0])
            text = str(range(durations[line[0]])).replace('[','').replace(']',',\n').replace(' ','')
            flush = open(outDir + 'CrossTalk_' + line[0],'w')
            flush.write("directory,descriptor,toSubpop,toSize,fromSubpop,fromSize,iteration,isEpidemic," + text)
            flush.close()
            flush = open(outDir + 'RepNum_' + line[0],'w')
            flush.write("directory,fromSubpop,fromSize,iteration,isEpidemic," + text)
            flush.close()
    
    if len(outDir) == 0:
        print "Error, no output directory specified"
        quit()
    elif not os.path.isdir(subpopDir):
        print "Error, subpop diretory", subpopDir, "does not exist"
        quit()
    elif len(filesOut) == 0:
        print "Error, no analysis output directories specified"
        quit()
    
    print "Prepping experiment, parameters are:\n"
    print "Analysis Directory:\n\t", outDir, "\nSubpop Directory:\n\t", subpopDir
    print "Subpopulations to/ from:\n", printList(script)
    print "Analyses:\n", printList(directories)
    
    EFO6Files = getEFO6s(directories)
    print EFO6Files.keys()
    popSize = EFO6Files[directories[0][1]+'_size']
    subpopFiles = getSubpops(script, subpopDir, popSize)
    if "error" in subpopFiles:
        print "Error termination"
        quit()
    
    allCurves = []
    fromPopIsEpi = [0]*len(fromPops)
    
    for experiment in directories:
        for subpop in script:
            crossTalkEFO6 = {'EFO6':EFO6Files[experiment[1]],'iterations':EFO6Files[experiment[1] +'_iterations']} 
            crossTalkSubs = {'toPop':subpopFiles[subpop[0]],
                            'toType':subpopFiles[subpop[0]+'_type'],
                            'toName':subpop[0],
                            'fromPop':subpopFiles[subpop[1]],
                            'fromType':subpopFiles[subpop[1] + '_type'],
                            'fromName':subpop[1]}
            print "\nAnalizing crosstalk for", experiment[1], "\n\twith subpops", subpop[0:2]
            crossTalk = loadCrossTalk(crossTalkEFO6, crossTalkSubs, durations[experiment[0]])
            
            fromPopIsEpi[fromPops.index(subpop[1])] = crossTalk['isEpidemic']
            statsOut = open(outDir + 'CrossTalk_' + experiment[0],'a+b')
            statsOut.write(curvesToStringCT(crossTalk['meanCrossTalkCurve'],
                                            crossTalk['crossTalkCurves'],
                                            crossTalk['isEpidemic'],
                                            experiment[1].replace(outDir.replace('Analysis/',''),'').replace('/EFO6',''),
                                            subpop[0],
                                            subpopFiles[subpop[0]+'_popSize'],
                                            subpop[1],
                                            subpopFiles[subpop[1]+'_popSize']))
            statsOut.close()
            #allCurves.append(crossTalk)
            print printList(crossTalk['crossTalkCurves'])
            
    print "CrossTalk Analysis Complete, beginning reproductive number analysis"
    
    for experiment in directories:
        for pos in range(len(fromPops)):
            pop = fromPops[pos]
            repNumStats = loadRepNum(pop,
                            subpopFiles[pop],
                            subpopFiles[pop + '_type'],
                            EFO6Files[experiment[1]],
                            EFO6Files[experiment[1]+'_iterations'],
                            durations[experiment[0]],
                            fromPopIsEpi[pos])
            print '\n', printList(repNumStats['meanRepNumCurve'])
            statsOut = open(outDir + 'RepNum_' + experiment[0],'a+b')
            statsOut.write(curvesToStringRN(repNumStats['meanRepNumCurve'],
                                            repNumStats['repNumCurves'],
                                            fromPopIsEpi[pos],
                                            experiment[1].replace(outDir.replace('Analysis/',''),'').replace('/EFO6',''),
                                            pop,
                                            subpopFiles[pop+'_popSize']))
            statsOut.close()
    
    print "Analysis complete, quitting now..."
Beispiel #5
0
def main(arg1, arg2, arg3, arg4, polyScript, filteredIDs, popSizes):

    print arg1, arg2, arg3, arg4

    if __name__ != '__main__':
        sys.argv = ['RollVac.py', arg1, arg2, arg3, arg4]

    if arg1 == "poly":
        del sys.argv[3:5]

    trigger = 1
    subnum = 0
    path = ""
    population = ""
    useExplicit = False
    explicitDirectory = ""
    subpopDirectory = ""
    useBase = False
    useRaw = False
    baseFile = ""
    pos = 0
    iCode = 0

    vacTotal = 0
    avTotal = 0
    socialTotal = 0
    workTotal = 0
    schoolTotal = 0
    sqTotal = 0
    avTreatments = 0
    try:
        toFilterIDs = len(filteredIDs) > 0
    except:
        toFilterIDS = False
    useNew = False
    subpopsNew = []
    actionsNew = []
    totalsNew = {'vaccination': 1000000000, 'antiviral': 1000000000}
    interventionsNew = []

    # LOADS PUBLIC/ PRIVATE DATA FROM GDOC IF PASSED
    if len(sys.argv) < 3:
        print "Missing arguments, quitting now"
        quit()
        outName = "Default"

    elif len(sys.argv) > 3:

        if sys.argv[1] == "gdoc" or sys.argv[1] == "poly":
            arg = "gDoc"
            if len(sys.argv) == 4:
                sys.argv.insert(3, 'null')
            else:
                print "Ignoring", len(sys.argv) - 3, "excess arguments\n"
    elif len(sys.argv) == 3:
        if sys.argv[1] == "gdoc" or sys.argv[1] == "poly":
            arg = "gDoc"
            sys.argv.insert(2, 'null')
            sys.argv.insert(2, 'null')
        else:
            outName = sys.argv[2]
            arg = sys.argv[1]

# LOADS AND WRITES AV/ DIAG SCRIPT FROM GOOGLE DOC, NOT IMPLEMENTED FOR USER/ LOCAL SCRIPT MODE

    print sys.argv
    if arg == "gDoc":

        if sys.argv[1] == "poly":
            isPoly = True
            sys.argv[3] = 'null'
            path = arg2
            if os.path.exists(path):
                shutil.rmtree(path)
            os.makedirs(path)
            os.makedirs(path + "/output")
        else:
            isPoly = False
        loadType = "intervention script"
        print sys.argv
        script = gDocsImport.getScript(sys.argv[2], sys.argv[3], sys.argv[4],
                                       startWord, stopWord, loadType, isPoly,
                                       polyScript)
        params = gDocsImport.getLine(sys.argv[2], sys.argv[3], sys.argv[4],
                                     paramsStart, isPoly, polyScript)

        print "debooo script", script

        emptyblock = (script
                      == 'null') or len(''.join(''.join(script).split())) < 2

        explicitDirectory = params[2]
        if len(explicitDirectory) > 0:
            useExplicit = True

        try:
            shiftDates = int(params[8])
            print "Date shift set to", shiftDates
        except:
            shiftDates = 0
            print "Using default '0' date shift"

        if isPoly:
            outName = ""
            baseFile = params[4]
            subpopDirectory = params[6]
            if len(baseFile) > 0:
                if os.path.exists(baseFile):
                    print "Appending interventions to base file", baseFile
                    useBase = True
                else:
                    print "Error, base file", baseFile, "not found"
                    quit()

        else:
            outName = params[0]

        if len(outName) == 0:
            print "No name prefix stored, using default intervention, antiviral, and diagnosis"
            outName = ""

        print "Will write to intervention file '%sIntervention'\n" % outName

        diag = params[1]
        diagnosis = isYes(diag, "diagnosis")
        useNew = isYes(params[7], "new style")

        try:
            appendScript = gDocsImport.getScript(sys.argv[2], sys.argv[3],
                                                 sys.argv[4], avStop, 0,
                                                 loadType, isPoly, polyScript)
            appendScript = appendRaw(appendScript)
            useRaw = appendScript != 'null'
            if useRaw:
                print "\nRaw script footer loaded succesfully,", appendScript.count(
                    '\n') - 3, "lines to append"
        except:
            print "Error, raw script footer not loaded"

        if not diagnosis:
            sys.argv[3] = "null"
        else:
            loadType = "default"
            avScript = gDocsImport.getScript(sys.argv[2], sys.argv[3],
                                             sys.argv[4], avStart, avStop,
                                             loadType, isPoly, polyScript)
            diagParams = gDocsImport.getLine(sys.argv[2], sys.argv[3],
                                             sys.argv[4], stopWord, isPoly,
                                             polyScript)
            sys.argv[3] = "null"

            if not useNew:
                if avScript == 'null':
                    print "No AV-Script found"
                    temp = open(path + '/Antiviral', 'w')
                    temp.write(
                        "#Null file: AV-Scripting enabled though no interventions found"
                    )
                    temp.close()
                    temp = open(path + '/Diagnosis', 'w')
                    temp.write(
                        "#Null file: AV-Scripting enabled though no interventions found"
                    )
                    temp.close()
                    avTreatments = 0
                else:
                    avTreatments = writeAvScript(avScript, diagParams, outName,
                                                 path, subpopDirectory,
                                                 shiftDates)

# PREP NEW FORMAT DICTS

            else:
                if avScript == 'null':
                    print "No AV-Script found"
                    avTreatments = 0
                else:
                    temp = prepNewAV(avScript, diagParams, outName, path,
                                     subpopDirectory, totalsNew, shiftDates)
                    subpopsNew += temp['subpops']
                    actionsNew += temp['actions']
                    interventionsNew += temp['interventions']
                    """print subpopsNew
                    print actionsNew
                    print interventionsNew"""
                    avTreatments = len(actionsNew)

    if arg != "user" and arg != "gDoc" and arg != "gdoc" and (
            not os.path.isfile(arg)):
        print arg
        print "Error, cannot open file or directory\n"
        quit()

    done = False

    # LOCAL SCRIPT FILE LOADING

    if arg != "user" and arg != "gDoc":
        scriptfile = open(arg)
        script = []
        line = 0
        gotPath = False
        while True:
            testline = scriptfile.readline()
            if len(testline) == 0:
                break
            if not testline.startswith("#"):
                if testline.startswith("Directory"):
                    gotPath = True
                    temp = testline.split()
                    if len(temp) < 2:
                        path = ""
                    else:
                        path = testline.split()[1]
                        if (path == "local" or path == "Local"):
                            path = ""
                else:
                    script.append(testline)
                    line += 1
        if not gotPath:
            path = ""

# FLUSH INTERVENTION FILE & COPY POLYRUN BASEFILE IF NEEDED

    writePath = path + outName + 'Intervention'
    if useNew:
        writePath += 'New'
    if useBase:
        shutil.copy2(baseFile, writePath)
        outFile = open(writePath)
        temp = outFile.read()
        outFile.close()
        outFile = open(writePath, 'w')
        outFile.write(
            "# ----- PolyRun.Py Appended Intervention File -----\n\n")
        outFile.write(temp)
        outFile.write(
            "\n\n# ----- End of Appended Intervention File -----\n\n")
    else:
        outFile = open(writePath, 'w')
    if useNew:
        outFile.write(
            "# ----- RollVac.Py Autogenerated Intervention File New Version-----\n\n"
        )
    else:
        outFile.write(
            "# ----- RollVac.Py Autogenerated Intervention File Old Version-----\n\n"
        )
    outFile.close()

    # GENERATING OUTPUT FILE LOOP

    while not done and emptyblock == False:

        subnum += 1
        enum = False

        #  LOCAL SCRIPT/ GDOC CONTROLLED CHOPPING

        if True:
            if pos == len(script):
                done = True
                break

            cmd = script[pos]
            items = cmd.split()
            population = (subpopDirectory + '/' + items[0]).replace('//', '/')
            try:
                with open(population):
                    pass
            except:
                print "Error: population file", population, "not found\n"
                quit()

            if str(items[1]) == 'enum':
                enum = True
                cmd = cmd.replace("enum", "null null")
                items = cmd.split()
                pos += 1
                enumList = script[pos].replace(";", " ; ")
                if checkEnum(enumList):
                    enumList = parseEnum(enumList)
                else:
                    print "Error: misformatted enumeration list", enumList
                    quit()

            if not enum:
                try:
                    day = int(items[1]) + shiftDates
                    if day <= 0:
                        print "Error, day must be an integer value\n"
                        quit()
                except:
                    print "Error, day must be an integer greater than zero\n"
                    quit()
                try:
                    length = int(items[2])
                    if length <= 0:
                        print "Error, length must be an integer greater than zero\n"
                        quit()
                except:
                    print "Error, length must be an integer greater than zero\n"
                    quit()

            temp = cmd.split()
            print "Generating interventions with parameters:", cmd
            method = temp[3]
            arguments = len(temp)

            if method == "Vaccination":
                target = 7
                meth = "v"
                iCode = 0
                if arguments == 7:
                    temp[5], temp[6] = temp[6], temp[5]
            elif method == "Antiviral":
                target = 8
                meth = "av"
                iCode = 1000
                if arguments == 8:
                    temp[5], temp[6], temp[7] = temp[6], temp[7], temp[5]
            elif method == "SocialDistancing":
                target = 6
                meth = "sd"
                iCode = 2000
            elif method == "WorkClosure":
                target = 6
                meth = "cw"
                iCode = 3000
            elif method == "SchoolClosure":
                target = 6
                meth = "cs"
                iCode = 4000
            elif method == "Sequestion":
                target = 7
                meth = "sq"
                iCode = 6000
            else:
                print "Error:", method, "method not recognized\n"
                quit()
            if arguments != target:
                print "Error:", len(
                    temp
                ), "parameters found,", target, "expected for intervention type", method, "\n"
                quit()

            interv = " ".join(temp[3:target])
            intervNew = temp[3:target]
            pos += 1

        suffix = str(subnum) + meth

        # ALL MODE CHOPPING EXECUTION

        print population
        if not useNew:
            pos2 = 0
            limit = len(filteredIDs)
            found = False
            while pos2 < limit:
                if population in filteredIDs[pos2]['directory']:
                    found = True
                    runIDs = filteredIDs[pos2]['ids']
                    break
                pos2 += 1

            if not found:
                filteredIDs.append(filterIDs(population))
                runIDs = filteredIDs[-1]['ids']

            if enum:
                populationSize = checkSize(population, popSizes)
                enumList = cleanEnum(percentEnum(enumList, populationSize))
                holder = chopper.main(population, 'e',
                                      " ".join(map(str, enumList)), suffix,
                                      path, runIDs)
                returnSize = holder['count']
                enumList = holder['enum']
                length = chopper.getEnumSize(enumList)
            else:
                holder = chopper.main(population, 'b', str(length), suffix,
                                      path, runIDs)
                returnSize = holder['count']
        else:
            addSubpop(subpopsNew,
                      population.split('/')[-1], population,
                      9000 + len(subpopsNew))

            if enum:
                populationSize = checkSize(population, popSizes)
                temp = countEnum(enumList, populationSize)
                enumList = chopper.trimEnum(cleanEnum(temp['enum']),
                                            populationSize)
                length = chopper.getEnumSize(enumList)
                returnSize = int(min(temp['total'], populationSize))
            else:
                populationSize = returnSize = checkSize(population, popSizes)

# NON TREATMENT BASED INTERVENTION TRACKING

        if meth == "v":
            vacTotal += returnSize
        elif meth == "av":
            avTotal += returnSize
        elif meth == "sd":
            socialTotal += returnSize
        elif meth == "cw":
            workTotal += returnSize
        elif meth == "cs":
            schoolTotal += returnSize
        elif meth == "sq":
            sqTotal += returnSize

# WRITING INTERVENTION FILE (AV TREATMENT & DIAG HANDLED SEPERATELY)

        writePath = path + outName + 'Intervention'

        pos2 = 0
        popName = population.split('/')[-1]

        if not useNew:
            outFile = open(writePath, 'a+b')
            if enum:
                while pos2 < len(enumList):
                    subPopName = popName + 'd' + str(pos2 / 2) + 'i' + suffix
                    if ".txt" in subPopName:
                        subPopName = subPopName.replace('.txt', '') + '.txt'
                    triggerOut = "* Trigger " + str(
                        trigger + iCode) + " Date " + str(enumList[pos2] +
                                                          shiftDates) + "\n"
                    if useExplicit:
                        tempPath = path + "/subpops/" + subPopName
                    else:
                        tempPath = "subpops/" + subPopName
                    intervOut = "* Action " + str(
                        trigger + iCode) + " " + interv + " " + tempPath + "\n"
                    intervOut = intervOut.replace('//', '/')
                    print triggerOut, intervOut.replace('\n', '')
                    outFile.write(triggerOut)
                    outFile.write(intervOut)
                    trigger += 1
                    pos2 += 2

            else:
                while pos2 < length:
                    subPopName = popName + 'd' + str(pos2) + 'i' + suffix
                    if ".txt" in subPopName:
                        subPopName = subPopName.replace('.txt', '') + '.txt'
                    triggerOut = "* Trigger " + str(
                        trigger + iCode) + " Date " + str(day + pos2) + "\n"
                    if useExplicit:
                        tempPath = path + "/subpops/" + subPopName
                    else:
                        tempPath = "subpops/" + subPopName
                    intervOut = "* Action " + str(
                        trigger + iCode) + " " + interv + " " + tempPath + "\n"
                    intervOut = intervOut.replace('//', '/')
                    print triggerOut, intervOut.replace('\n', '')
                    outFile.write(triggerOut)
                    outFile.write(intervOut)
                    trigger += 1
                    pos2 += 1

            outFile.close()
            print
        else:
            writePath += 'New'
            tempAction = prepNewAction()
            #tempInterv = [prepNewIntervention()]*len(enumList/2)
            tempAction['actionID'] = actionID = str(trigger + iCode)
            tempAction['actionType'] = method
            if method == 'Antiviral':
                method += "_Prophylaxis"
            tempAction['actionDescription'] = method
            tempAction['actionDelay'] = '0'
            tempAction['actionDuration'] = intervNew[1]
            conditionTotal = -1
            if method == "Vaccination":
                tempAction['actionEfficacy'] = intervNew[3]
                conditionTotal = '9100'
            elif method == "Antiviral":
                tempAction['actionEfficacyIn'] = intervNew[3]
                tempAction['actionEfficacyOut'] = intervNew[4]
                conditionTotal = '9101'
            elif method == "Sequestion":
                tempAction['actionGroupSize'] = intervNew[3]
            if enum:
                tempInterv = []
                while pos2 < len(enumList):
                    tempInterv.append(prepNewIntervention())
                    tempInterv[pos2 / 2]['interventionID'] = str(
                        9300 + len(interventionsNew) + pos2 / 2)
                    tempInterv[pos2 / 2]['interventionType'] = "Offline"
                    if conditionTotal != -1:
                        tempInterv[pos2 / 2]['conditionTotal'] = conditionTotal
                    tempInterv[pos2 / 2]['conditionDate'] = str(
                        enumList[pos2] +
                        shiftDates) + '~' + str(enumList[pos2] + shiftDates)
                    tempInterv[pos2 / 2]['conditionMembership'] = getSubpopID(
                        subpopsNew, popName)
                    tempInterv[pos2 / 2]['conditionCompliance'] = str(
                        float(intervNew[2]) * enumList[pos2 + 1])
                    tempInterv[pos2 / 2]['action'] = actionID
                    pos2 += 2
            else:
                tempInterv = []
                for pos2 in range(length):
                    tempInterv.append(prepNewIntervention())
                    tempInterv[pos2]['interventionID'] = str(
                        9300 + len(interventionsNew) + pos2)
                    tempInterv[pos2]['interventionType'] = "Offline"
                    if conditionTotal != -1:
                        tempInterv[pos2]['conditionTotal'] = conditionTotal
                    tempInterv[pos2]['conditionDate'] = str(
                        day + pos2 + shiftDates) + '~' + str(day + pos2 +
                                                             shiftDates)
                    tempInterv[pos2]['conditionMembership'] = getSubpopID(
                        subpopsNew, popName)
                    tempInterv[pos2]['conditionCompliance'] = str(
                        float(intervNew[2]) / length)
                    tempInterv[pos2]['action'] = actionID
                    subPopName = popName + 'd' + str(pos2 + 1) + 'i' + suffix
                    if ".txt" in subPopName:
                        subPopName = subPopName.replace('.txt', '') + '.txt'
            actionsNew.append(tempAction)
            interventionsNew += tempInterv

# AUTOGENERATE NEW FORMAT MUTEXES

    if useNew:
        vMutex = []
        avMutex = []
        sdMutex = []
        cwMutex = []
        csMutex = []
        sqMutex = []
        limit = len(actionsNew)
        print "Generating", limit, "new format actions"
        for pos in range(limit):
            if actionsNew[pos]['actionType'] == "Vaccination":
                if actionsNew[pos]['actionID'] not in vMutex:
                    vMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "Antiviral":
                if actionsNew[pos]['actionID'] not in avMutex:
                    avMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "SocialDistancing":
                if actionsNew[pos]['actionID'] not in sdMutex:
                    sdMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "CloseWork":
                if actionsNew[pos]['actionID'] not in cwMutex:
                    cwMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "CloseSchools":
                if actionsNew[pos]['actionID'] not in csMutex:
                    csMutex.append(actionsNew[pos]['actionID'])
            elif actionsNew[pos]['actionType'] == "Sequestion":
                if actionsNew[pos]['actionID'] not in sqMutex:
                    sqMutex.append(actionsNew[pos]['actionID'])
        limit = len(interventionsNew)
        print "Generating", limit, "new format interventions"
        for pos in range(limit):
            temp = getActionType(actionsNew, interventionsNew[pos]['action'])
            if temp == "Vaccination":
                interventionsNew[pos]['conditionMutex'] = ';'.join(vMutex)
            elif temp == "Antiviral":
                interventionsNew[pos]['conditionMutex'] = ';'.join(avMutex)
            elif temp == "SocialDistancing":
                interventionsNew[pos]['conditionMutex'] = ';'.join(sdMutex)
            elif temp == "CloseWork":
                interventionsNew[pos]['conditionMutex'] = ';'.join(cwMutex)
            elif temp == "CloseSchools":
                interventionsNew[pos]['conditionMutex'] = ';'.join(csMutex)
            elif temp == "Sequestion":
                interventionsNew[pos]['conditionMutex'] = ';'.join(sqMutex)


# APPENDING NON TREATMENT INTERVENTION TOTALS

    outFile = open(writePath, 'a+b')
    if useNew:
        outFile.write(
            getOutputNew(subpopsNew, totalsNew, actionsNew, interventionsNew))
    sumIntervs = vacTotal + avTotal + socialTotal + workTotal + schoolTotal + sqTotal + avTreatments
    if sumIntervs == 0:
        if useRaw:
            print "No scripted interventions found, using iterated manual script"
            outFile.write(appendScript)
        else:
            print "No scripted interventions or raw script found"
    else:
        if useRaw:
            print "Appending manually iterated script"
            outFile.write(appendScript)

        outFile.write("\n#----- End of Generated Intervention File -----\n\n")
        outFile.write(
            "\n#Use only one intervention of each type per sub pop, using enumerated interventions for complex interventions."
        )
        outFile.write("\n# Vaccination: " + str(vacTotal))
        outFile.write("\n# Antiviral Prophylaxis: " + str(avTotal))
        outFile.write("\n# Social Distancing: " + str(socialTotal))
        outFile.write("\n# Close Work: " + str(workTotal))
        outFile.write("\n# Close Schools: " + str(schoolTotal))
        if useNew:
            outFile.write("\n# Sequestered: " + str(sqTotal))
        outFile.write("\n# AV Treatment Programs: " + str(avTreatments))
        outFile.close()
        print """RollVac.Py Pre Compliance Intervention Totals- calculated per output,
does not account for over-application to a given set of IDs. 
Please apply only one of each type per sub pop, using enumerated 
interventions for complex interventions."""
        print "\nVaccination: " + str(vacTotal)
        print "Antiviral Prophylaxis: " + str(avTotal)
        print "Social Distancing: " + str(socialTotal)
        print "Close Work: " + str(workTotal)
        print "Close Schools:" + str(schoolTotal)
        if useNew:
            print "Sequestered:" + str(sqTotal)
        print "AV Treatment Programs: " + str(avTreatments)
        print
        if toFilterIDs:
            return filteredIDs
Beispiel #6
0
def main():

    # USED TO DEFINE LOADING POSITIONS, MUST BE CONSERVED IN SOURCE AND GDOC

    paramsStart = "Study Name Prefix (optional),Diagnosis Based"
    startWord = "Subpopulation,Day/'enum',Length of Spread"

    vacsRolled = 0

    totalVars = 0
    varList = []
    varSets = []
    suffixes = []
    directorySuffix = ""
    filteredIDs = [{"directory": 'null', 'ids': []}]
    popSizes = dict()
    #    positions = []

    # PARSING COMMAND LINE ARGUMENTS FOR PUBLIC/ PRIVATE FILE ACCESS

    if len(sys.argv) > 2:
        if len(sys.argv) == 3:
            sys.argv.insert(2, 'null')
        else:
            print "Ignoring", len(sys.argv) - 2, "excess arguments\n"
    elif len(sys.argv) == 2:
        sys.argv.insert(1, 'null')
        sys.argv.insert(1, 'null')

    script = gDocsImport.getScript(sys.argv[1], sys.argv[2], sys.argv[3], 0,
                                   -1, "default", False, [])
    directoryLines = gDocsImport.getScript(sys.argv[1], sys.argv[2],
                                           sys.argv[3], paramsStart, startWord,
                                           "default", False, [])
    sys.argv = None

    # ERASES DIRECTORY NAMES GIVEN BY GDOC

    directories = []
    for directory in directoryLines:
        dirToFlush = (directory[2] + '/' + directory[0]).replace('//', '/')
        if not dirToFlush in directories:
            directories.append(dirToFlush)

    directorySuffix = flushDirectories(directories)

    # CREATES LISTS OF ALL EXPERIMENTAL VARIABLES ITERATED OVER

    length = len(script)

    print "Searching list for experimentally iterated variables"
    print 'DEBOOO', script
    for item in script:
        temp = getPoly(item)
        if temp[0] != 'null':
            if temp[0] not in varSets:
                totalVars += 1
                varList.append(temp[0])
            varSets.append(temp[0])
            suffixes.append(temp[1])

    varList.sort()
    length = len(varSets)
    varMatrix = [[] for x in xrange(totalVars)]
    suffixMatrix = [[] for x in xrange(totalVars)]
    #    positionMatrix = [[] for x in xrange(totalVars)]

    # CREATES MATRICES OF SUFFIXES, VARIABLE ID, AND LIST POSITION FOR ITERATION

    for pos2 in range(totalVars):
        for pos1 in range(length):
            if varSets[pos1] == varList[pos2] and (suffixes[pos1]
                                                   not in suffixMatrix[pos2]):
                varMatrix[pos2].append(varSets[pos1])
                suffixMatrix[pos2].append(suffixes[pos1])
#                positionMatrix[pos2].append(positions[pos1])

# SETS UP RUN BOUNDS

    runTracker = [0] * totalVars
    done = False
    totalRuns = 1
    ends = []
    for pos in range(totalVars):
        totalRuns *= len(varMatrix[pos])
        ends.append(len(varMatrix[pos]) - 1)
        if pos >= 1:
            ends[pos] += 1

# ITERATED RUN GENERATION, IF SCRIPT LINE CONTAINS CURRENT ITERATION RUN MARKERS OR NONE, SENT TO RollVac TO PARSE

    while not done:

        toRun = []
        for pos in range(totalVars):
            toRun.append(suffixMatrix[pos][runTracker[pos]])

        length = len(script)
        rollScript = []
        print "Loading Script for line:", runTracker
        for item in script:
            if getPoly(item)[0] == 'null' or getPoly(item)[1] in toRun:
                rollScript.append(filterPoly(item))
                #if len(item.replace('"','').replace(',','').replace('\n','')) != 0:


# SUPPORT ADDED FOR MULTIPLE DIRECTORIES/ RUN

        params = gDocsImport.getLine('null', 'null', 'null', paramsStart, True,
                                     rollScript)
        folder = params[2] + '/' + params[0]
        directory = appendSuffix(folder, directorySuffix)

        # OUT DIRECTORY GENERATED VIA SUFFIX MATRIX

        for pos in range(totalVars):
            directory += suffixMatrix[pos][runTracker[pos]] + '/'

        params = gDocsImport.loadNClean(False, rollScript, paramsStart,
                                        startWord, "single line")
        print params

        homeDir = appendSuffix(params[2] + '/' + params[0], directorySuffix)
        explicit = appendSuffix(params[2], directorySuffix)

        needsReplace = len(params[5]) > 0
        noAVDiag = not RollVac.isYes(params[1], 'null')
        fileString = params[3]
        filesToCopy = len(fileString) > 0

        filteredIDs = RollVac.main('poly', directory, 'null', 'null',
                                   rollScript, filteredIDs, popSizes)
        sleep(0.05)

        qsubs = open(homeDir + 'qsublist', 'a+b')
        qsubs.write(("qsub " + directory + 'qsub\n').replace('//', '/'))
        qsubs.close()

        if noAVDiag:
            extraCommands = [
                "Find = DiagnosisFile =", "Replace = ",
                "Find =  AntiviralFile = ", "Replace = "
            ]
        else:
            extraCommands = 'null'

        if needsReplace:
            replaceFile = params[5]
        else:
            replaceFile = 'null'

        if needsReplace or noAVDiag:
            replaceScript = loadReplaceScript(replaceFile, extraCommands)
            findNReplace(fileString, replaceScript, directory, vacsRolled,
                         homeDir, explicit)

        if filesToCopy and not needsReplace:
            print fileString, directory
            fileCopy(fileString, directory)

        vacsRolled += 1

        # POLYRUN MULTIDIMENSIONAL LOOP ITERATOR

        if totalVars == 0:
            print "No PolyRun iteration operators found, running in singular mode."
            done = True
            break

        pos = 0
        justRolled = False

        while runTracker[pos] == ends[pos] and pos < totalVars - 1:
            justRolled = True
            runTracker[pos] = 0
            runTracker[pos + 1] += 1
            pos += 1

        if pos == totalVars - 1 and runTracker[pos] == ends[pos]:
            print "Interventions Iterated =", vacsRolled
            done = True
            break

        if not justRolled:
            runTracker[0] += 1

    print "Intervention iteration succesfully complete!"
def main():
    if len(sys.argv) > 2:
        if len(sys.argv) == 3:
            sys.argv.insert(2, 'null')
        else:
            print "Ignoring", len(sys.argv) - 2, "excess arguments\n"
    elif len(sys.argv) == 2:
        sys.argv.insert(1, 'null')
        sys.argv.insert(1, 'null')

    params = gd.getLine(sys.argv[1], sys.argv[2], sys.argv[3], paramsLine,
                        False, [])
    script = gd.getScript(sys.argv[1], sys.argv[2], sys.argv[3], toFromLine,
                          EFO6Line, "default", False, [])
    directories = gd.getScript(sys.argv[1], sys.argv[2], sys.argv[3], EFO6Line,
                               -1, "default", False, [])
    sys.argv = None

    outDir = prepDir(params[0])
    subpopDir = prepDir(params[1])
    filesOut = []
    durations = {}
    fromPops = []

    for line in script:
        if len(line[0]) == 0 or len(line[1]) == 0:
            print "Error, missing to or from subpop name, line:", line
            quit()
        if line[1] not in fromPops:
            fromPops.append(line[1])
    for line in directories:
        if len(line[0]) == 0 or len(line[1]) == 0:
            print "Error, missing file or directory name, line:", line
            quit()
        if line[0] not in durations:
            durations[line[0]] = 0
        durations[line[0]] = max(durations[line[0]], getLength(line[1]))

    if not os.path.isdir(outDir):
        os.mkdir(outDir)

    for line in directories:
        if line[0] not in filesOut:
            filesOut.append(line[0])
            text = str(range(durations[line[0]])).replace('[', '').replace(
                ']', ',\n').replace(' ', '')
            flush = open(outDir + 'CrossTalk_' + line[0], 'w')
            flush.write(
                "directory,descriptor,toSubpop,toSize,fromSubpop,fromSize,iteration,isEpidemic,"
                + text)
            flush.close()
            flush = open(outDir + 'RepNum_' + line[0], 'w')
            flush.write("directory,fromSubpop,fromSize,iteration,isEpidemic," +
                        text)
            flush.close()

    if len(outDir) == 0:
        print "Error, no output directory specified"
        quit()
    elif not os.path.isdir(subpopDir):
        print "Error, subpop diretory", subpopDir, "does not exist"
        quit()
    elif len(filesOut) == 0:
        print "Error, no analysis output directories specified"
        quit()

    print "Prepping experiment, parameters are:\n"
    print "Analysis Directory:\n\t", outDir, "\nSubpop Directory:\n\t", subpopDir
    print "Subpopulations to/ from:\n", printList(script)
    print "Analyses:\n", printList(directories)

    EFO6Files = getEFO6s(directories)
    print EFO6Files.keys()
    popSize = EFO6Files[directories[0][1] + '_size']
    subpopFiles = getSubpops(script, subpopDir, popSize)
    if "error" in subpopFiles:
        print "Error termination"
        quit()

    allCurves = []
    fromPopIsEpi = [0] * len(fromPops)

    for experiment in directories:
        for subpop in script:
            crossTalkEFO6 = {
                'EFO6': EFO6Files[experiment[1]],
                'iterations': EFO6Files[experiment[1] + '_iterations']
            }
            crossTalkSubs = {
                'toPop': subpopFiles[subpop[0]],
                'toType': subpopFiles[subpop[0] + '_type'],
                'toName': subpop[0],
                'fromPop': subpopFiles[subpop[1]],
                'fromType': subpopFiles[subpop[1] + '_type'],
                'fromName': subpop[1]
            }
            print "\nAnalizing crosstalk for", experiment[
                1], "\n\twith subpops", subpop[0:2]
            crossTalk = loadCrossTalk(crossTalkEFO6, crossTalkSubs,
                                      durations[experiment[0]])

            fromPopIsEpi[fromPops.index(subpop[1])] = crossTalk['isEpidemic']
            statsOut = open(outDir + 'CrossTalk_' + experiment[0], 'a+b')
            statsOut.write(
                curvesToStringCT(
                    crossTalk['meanCrossTalkCurve'],
                    crossTalk['crossTalkCurves'], crossTalk['isEpidemic'],
                    experiment[1].replace(outDir.replace('Analysis/', ''),
                                          '').replace('/EFO6', ''), subpop[0],
                    subpopFiles[subpop[0] + '_popSize'], subpop[1],
                    subpopFiles[subpop[1] + '_popSize']))
            statsOut.close()
            #allCurves.append(crossTalk)
            print printList(crossTalk['crossTalkCurves'])

    print "CrossTalk Analysis Complete, beginning reproductive number analysis"

    for experiment in directories:
        for pos in range(len(fromPops)):
            pop = fromPops[pos]
            repNumStats = loadRepNum(pop, subpopFiles[pop],
                                     subpopFiles[pop + '_type'],
                                     EFO6Files[experiment[1]],
                                     EFO6Files[experiment[1] + '_iterations'],
                                     durations[experiment[0]],
                                     fromPopIsEpi[pos])
            print '\n', printList(repNumStats['meanRepNumCurve'])
            statsOut = open(outDir + 'RepNum_' + experiment[0], 'a+b')
            statsOut.write(
                curvesToStringRN(
                    repNumStats['meanRepNumCurve'],
                    repNumStats['repNumCurves'], fromPopIsEpi[pos],
                    experiment[1].replace(outDir.replace('Analysis/', ''),
                                          '').replace('/EFO6', ''), pop,
                    subpopFiles[pop + '_popSize']))
            statsOut.close()

    print "Analysis complete, quitting now..."