示例#1
0
    def testGenerateNonReceivers(self):
        numEgos = 3
        numFeatures = 5

        p = 1
        eCsvReader = EgoCsvReader()
        eCsvReader.setP(p)

        numContactsIndices = [0, 1]
        homophileIndexPairs = [(2,3)]

        receiverCounts = numpy.zeros(numEgos)

        #First test a very simple example with 1 homophile pair
        egoArray = numpy.zeros((numEgos, numFeatures))
        egoArray[0, :] = [0, 1, 1, 5, 4]
        egoArray[1, :] = [0, 1, 1, 5, 8]
        egoArray[2, :] = [0, 0, 1, 3, 6]

        (contactsArray, egoIndices, alterIndices) = eCsvReader.generateNonReceivers(egoArray, numContactsIndices, homophileIndexPairs, receiverCounts)

        numContacts = 4
        contactsArray2 = numpy.zeros((numContacts, numFeatures))
        contactsArray2[0, :] = [0, 1, 1, 5, 8]
        contactsArray2[1, :] = [0, 0, 1, 3, 6]
        contactsArray2[2, :] = [0, 1, 1, 5, 4]
        contactsArray2[3, :] = [0, 0, 1, 3, 6]


        egoIndices2 = numpy.zeros(numContacts)
        egoIndices2[0] = 0
        egoIndices2[1] = 0
        egoIndices2[2] = 1
        egoIndices2[3] = 1

        self.assertTrue((contactsArray == contactsArray2).all())
        self.assertTrue((egoIndices == egoIndices2).all())

        #Test the case when there are some receivers already
        receiverCounts = numpy.array([1,1,1])
        (contactsArray, egoIndices, alterIndices) = eCsvReader.generateNonReceivers(egoArray, numContactsIndices, homophileIndexPairs, receiverCounts)


        numContacts = 2
        contactsArray2 = numpy.zeros((numContacts, numFeatures))
        contactsArray2[0, :] = [0, 1, 1, 5, 8]
        contactsArray2[1, :] = [0, 1, 1, 5, 4]


        egoIndices2 = numpy.zeros(numContacts)
        egoIndices2[0] = 0
        egoIndices2[1] = 1

        self.assertTrue((contactsArray == contactsArray2).all())
        self.assertTrue((egoIndices == egoIndices2).all())

        #A more complex example
        numEgos = 6
        egoArray = numpy.zeros((numEgos, numFeatures))
        egoArray[0, :] = [1, 1, 1, 5, 4]
        egoArray[1, :] = [0, 0, 1, 5, 8]
        egoArray[2, :] = [0, 0, 1, 3, 6]
        egoArray[3, :] = [0, 0, 1, 5, 1]
        egoArray[4, :] = [0, 0, 1, 5, 2]
        egoArray[5, :] = [0, 0, 1, 5, 3]

        receiverCounts = numpy.zeros(numEgos)
        (contactsArray, egoIndices, alterIndices) = eCsvReader.generateNonReceivers(egoArray, numContactsIndices, homophileIndexPairs, receiverCounts)

        numContacts = 4
        contactsArray2 = numpy.zeros((numContacts, numFeatures))
        contactsArray2[0, :] = [0, 0, 1, 5, 8]
        contactsArray2[1, :] = [0, 0, 1, 5, 1]
        contactsArray2[2, :] = [0, 0, 1, 5, 2]
        contactsArray2[3, :] = [0, 0, 1, 5, 3]

        egoIndices2 = numpy.zeros(numContacts)
        egoIndices2[0] = 0
        egoIndices2[1] = 0
        egoIndices2[2] = 0
        egoIndices2[3] = 0


        self.assertTrue((contactsArray == contactsArray2).all())
        self.assertTrue((egoIndices == egoIndices2).all())

        #Test picking non-homophiles
        egoArray[0, :] = [2, 1, 2, 5, 4]

        (contactsArray, egoIndices, alterIndices) = eCsvReader.generateNonReceivers(egoArray, numContactsIndices, homophileIndexPairs, receiverCounts)

        numContacts = 5
        contactsArray2 = numpy.zeros((numContacts, numFeatures))
        contactsArray2[0, :] = [0, 0, 1, 5, 8]
        contactsArray2[1, :] = [0, 0, 1, 3, 6]
        contactsArray2[2, :] = [0, 0, 1, 5, 1]
        contactsArray2[3, :] = [0, 0, 1, 5, 2]
        contactsArray2[4, :] = [0, 0, 1, 5, 3]

        egoIndices2 = numpy.zeros(numContacts)
        egoIndices2[0] = 0
        egoIndices2[1] = 0
        egoIndices2[2] = 0
        egoIndices2[3] = 0
        egoIndices2[4] = 0


        self.assertTrue((contactsArray == contactsArray2).all())
        self.assertTrue((egoIndices == egoIndices2).all())

        #Choose different p 
        p = 0.5
        eCsvReader.setP(p)

        egoArray[0, :] = [1, 1, 1, 5, 4]
        egoArray[1, :] = [0, 0, 1, 5, 8]
        egoArray[2, :] = [0, 0, 1, 3, 6]
        egoArray[3, :] = [0, 0, 1, 5, 8]
        egoArray[4, :] = [0, 0, 1, 5, 8]
        egoArray[5, :] = [0, 0, 1, 5, 8]

        (contactsArray, egoIndices, alterIndices) = eCsvReader.generateNonReceivers(egoArray, numContactsIndices, homophileIndexPairs, receiverCounts)

        numContacts = 3
        contactsArray2 = numpy.zeros((numContacts, numFeatures))
        contactsArray2[0, :] = [0, 0, 1, 5, 8]
        contactsArray2[1, :] = [0, 0, 1, 5, 8]
        contactsArray2[2, :] = [0, 0, 1, 3, 6]


        egoIndices2 = numpy.zeros(numContacts)
        egoIndices2[0] = 0
        egoIndices2[1] = 0
        egoIndices2[2] = 0

        self.assertTrue((contactsArray == contactsArray2).all())
        self.assertTrue((egoIndices == egoIndices2).all())

        #Test 2 different homophile fields
        p = 1
        eCsvReader.setP(p)
        numEgos = 6
        numFeatures = 7

        homophileIndexPairs = [(2,3), (4,5)]

        egoArray = numpy.zeros((numEgos, numFeatures))
        egoArray[0, :] = [0, 0, 1, 5, 1, 2, 1]
        egoArray[1, :] = [0, 0, 1, 5, 1, 3, 2]
        egoArray[2, :] = [0, 0, 1, 4, 1, 2, 3]
        egoArray[3, :] = [1, 0, 1, 5, 1, 2, 4]
        egoArray[4, :] = [0, 0, 1, 2, 1, 1, 5]
        egoArray[5, :] = [0, 0, 1, 5, 1, 2, 6]

        (contactsArray, egoIndices, alterIndices) = eCsvReader.generateNonReceivers(egoArray, numContactsIndices, homophileIndexPairs, receiverCounts)

        numContacts = 2
        contactsArray2 = numpy.zeros((numContacts, numFeatures))
        contactsArray2[0, :] = [0, 0, 1, 5, 1, 2, 1]
        contactsArray2[1, :] = [0, 0, 1, 5, 1, 2, 6]

        egoIndices2 = numpy.zeros(numContacts)
        egoIndices2[0] = 3
        egoIndices2[1] = 3

        self.assertTrue((contactsArray == contactsArray2).all())
        self.assertTrue((egoIndices == egoIndices2).all())