Пример #1
0
 def testreadSimulationHIVGraph(self): 
     
     hivReader = HIVGraphReader()
     graph = hivReader.readSimulationHIVGraph()
     
     
     self.assertEquals(graph.getNumVertices(), 5389)
     self.assertEquals(graph.getNumEdges(), 4097)
     #Check the edges of the first few vertices 
     nptst.assert_array_equal(graph.neighbours(0), numpy.array([50]))
     nptst.assert_array_equal(graph.neighbours(1), numpy.array([1380]))
     nptst.assert_array_equal(graph.neighbours(2), numpy.array([]))
     nptst.assert_array_equal(graph.neighbours(3), numpy.array([]))
     nptst.assert_array_equal(graph.neighbours(30), numpy.array([43, 51, 57, 151, 304, 386,913]))
     
     #Check vertex values 
     v = graph.getVertex(0)
     self.assertEquals(v[HIVVertices.dobIndex], 19602)
     self.assertEquals(v[HIVVertices.genderIndex], HIVVertices.male)
     self.assertEquals(v[HIVVertices.orientationIndex], HIVVertices.hetero)
     self.assertEquals(v[HIVVertices.stateIndex], HIVVertices.removed)
     self.assertEquals(v[HIVVertices.infectionTimeIndex], -1)
     self.assertEquals(v[HIVVertices.detectionTimeIndex], 31563)
     self.assertEquals(v[HIVVertices.detectionTypeIndex], HIVVertices.randomDetect)
     self.assertEquals(v[HIVVertices.hiddenDegreeIndex], 3)
     
     v = graph.getVertex(1)
     self.assertEquals(v[HIVVertices.dobIndex], 21508)
     self.assertEquals(v[HIVVertices.genderIndex], HIVVertices.male)
     self.assertEquals(v[HIVVertices.orientationIndex], HIVVertices.hetero)
     self.assertEquals(v[HIVVertices.stateIndex], HIVVertices.removed)
     self.assertEquals(v[HIVVertices.infectionTimeIndex], -1)
     self.assertEquals(v[HIVVertices.detectionTimeIndex], 31563)
     self.assertEquals(v[HIVVertices.detectionTypeIndex], HIVVertices.randomDetect)
     self.assertEquals(v[HIVVertices.hiddenDegreeIndex], 4)
     
     v = graph.getVertex(6)
     self.assertEquals(v[HIVVertices.dobIndex], 21676)
     self.assertEquals(v[HIVVertices.genderIndex], HIVVertices.female)
     self.assertEquals(v[HIVVertices.orientationIndex], HIVVertices.hetero)
     self.assertEquals(v[HIVVertices.stateIndex], HIVVertices.removed)
     self.assertEquals(v[HIVVertices.infectionTimeIndex], -1)
     self.assertEquals(v[HIVVertices.detectionTimeIndex], 31472)
     self.assertEquals(v[HIVVertices.detectionTypeIndex], HIVVertices.contactTrace)
     self.assertEquals(v[HIVVertices.hiddenDegreeIndex], 1)
     
     v = graph.getVertex(5381)
     self.assertEquals(v[HIVVertices.dobIndex], 25307)
     self.assertEquals(v[HIVVertices.genderIndex], HIVVertices.male)
     self.assertEquals(v[HIVVertices.orientationIndex], HIVVertices.bi)
     self.assertEquals(v[HIVVertices.stateIndex], HIVVertices.removed)
     self.assertEquals(v[HIVVertices.infectionTimeIndex], -1)
     self.assertEquals(v[HIVVertices.detectionTimeIndex], 38231)
     self.assertEquals(v[HIVVertices.detectionTypeIndex], HIVVertices.randomDetect)
     self.assertEquals(v[HIVVertices.hiddenDegreeIndex], 0)
Пример #2
0
    def __init__(self, minGraphSize=500, monthStep=1): 

        startYear = 1900
        daysInMonth = 30      
        
        #Start off with the HIV data 
        hivReader = HIVGraphReader()
        graph = hivReader.readHIVGraph()
        fInds = hivReader.getIndicatorFeatureIndices()
        
        #The set of edges indexed by zeros is the contact graph
        #The ones indexed by 1 is the infection graph
        edgeTypeIndex1 = 0
        edgeTypeIndex2 = 1
        sGraphContact = graph.getSparseGraph(edgeTypeIndex1)
        sGraphInfect = graph.getSparseGraph(edgeTypeIndex2)
        sGraphContact = sGraphContact.union(sGraphInfect)
        graph = sGraphContact
        
        #Find max component
        #Create a graph starting from the oldest point in the largest component 
        components = graph.findConnectedComponents()
        graph = graph.subgraph(list(components[0]))
        logging.debug(graph)
        
        detectionIndex = fInds["detectDate"]
        vertexArray = graph.getVertexList().getVertices()
        detections = vertexArray[:, detectionIndex]
        
        firstVertex = numpy.argmin(detections)
        
        dayList = list(range(int(numpy.min(detections)), int(numpy.max(detections)), daysInMonth*monthStep))
        dayList.append(numpy.max(detections))
        
        subgraphIndicesList = []
        
        #Generate subgraph indices list 
        for i in dayList:
            logging.info("Date: " + str(DateUtils.getDateStrFromDay(i, startYear)))
            subgraphIndices = numpy.nonzero(detections <= i)[0]
            
            #Check subgraphIndices are sorted 
            subgraphIndices = numpy.sort(subgraphIndices)
            currentSubgraph = graph.subgraph(subgraphIndices)
            compIndices = currentSubgraph.depthFirstSearch(list(subgraphIndices).index(firstVertex))
            subgraphIndices =  subgraphIndices[compIndices]
            
            if subgraphIndices.shape[0] >= minGraphSize: 
                subgraphIndicesList.append(subgraphIndices)
        
        self.graph = graph
        self.subgraphIndicesList = subgraphIndicesList
        
        self.numGraphs = len(subgraphIndicesList)
Пример #3
0
 def realSimulationParams(): 
     hivReader = HIVGraphReader()
     targetGraph = hivReader.readSimulationHIVGraph()
     
     numRecordSteps = 10 
     #Note that 5% of the population is bi 
     M = targetGraph.size * 4
     #This needs to be from 1986 to 2004 
     startDate = CsvConverters.dateConv("01/01/1986")
     endDates = [CsvConverters.dateConv("01/01/1987"), CsvConverters.dateConv("01/01/1989"), CsvConverters.dateConv("01/01/1991"), CsvConverters.dateConv("01/01/1993"), CsvConverters.dateConv("01/01/1995"), CsvConverters.dateConv("01/01/1997")]
     endDates = [float(i) for i in endDates]
     
     return float(startDate), endDates, numRecordSteps, M, targetGraph
Пример #4
0
import numpy
from apgl.graph import *
from exp.viroscopy.HIVGraphReader import HIVGraphReader
from sandbox.util.PathDefaults import PathDefaults
from sandbox.util.DateUtils import DateUtils
from exp.clusterexp.ClusterExpHelper import ClusterExpHelper
from exp.sandbox.GraphIterators import IncreasingSubgraphListIterator, toDenseGraphListIterator
from exp.sandbox.IterativeSpectralClustering import IterativeSpectralClustering

numpy.random.seed(21)
#numpy.seterr("raise")
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
numpy.set_printoptions(suppress=True, linewidth=60)

#Start off with the HIV data 
hivReader = HIVGraphReader()
graph = hivReader.readHIVGraph()
fInds = hivReader.getIndicatorFeatureIndices()

#The set of edges indexed by zeros is the contact graph
#The ones indexed by 1 is the infection graph
edgeTypeIndex1 = 0
edgeTypeIndex2 = 1
sGraphContact = graph.getSparseGraph(edgeTypeIndex1)
sGraphInfect = graph.getSparseGraph(edgeTypeIndex2)
sGraphContact = sGraphContact.union(sGraphInfect)
graph = sGraphContact

#Find max component
components = graph.findConnectedComponents()
graph = graph.subgraph(list(components[0]))