예제 #1
0
def writeBitArray2DImage(bitArray, filename):
	# copy BitArray2D to numpy array
	data = numpy.empty(shape = (bitArray.rows, bitArray.columns), dtype = "bool")
	for row in xrange(0, bitArray.rows):
		for col in xrange(0, bitArray.columns):
			data[row, col] = bitArray[BitArray2D.godel(row, col)]
	
	writeNumpyArrayImage(numpyArray = data, filename = filename)
예제 #2
0
    def _applyNeighborhoodChange(self, neighborhood, step):
        if step == "EVEN":
            aRow = neighborhood.topCoordinate
            aCol = neighborhood.leftCoordinate
            bRow = neighborhood.topCoordinate
            bCol = neighborhood.leftCoordinate + 1
            cRow = neighborhood.topCoordinate + 1
            cCol = neighborhood.leftCoordinate
            dRow = neighborhood.topCoordinate + 1
            dCol = neighborhood.leftCoordinate + 1
            aVal = str(self.matrix[BitArray2D.godel(aRow, aCol)])
            bVal = str(self.matrix[BitArray2D.godel(bRow, bCol)])
            cVal = str(self.matrix[BitArray2D.godel(cRow, cCol)])
            dVal = str(self.matrix[BitArray2D.godel(dRow, dCol)])

        else:
            aRow = neighborhood.topCoordinate + 1
            aCol = neighborhood.leftCoordinate + 1
            bRow = neighborhood.topCoordinate + 1
            bCol = neighborhood.leftCoordinate + 1 + 1
            cRow = neighborhood.topCoordinate + 1 + 1
            cCol = neighborhood.leftCoordinate + 1
            dRow = neighborhood.topCoordinate + 1 + 1
            dCol = neighborhood.leftCoordinate + 1 + 1

            aVal = str(self.matrix[BitArray2D.godel(aRow, aCol)])
            if bCol >= self.topLevelWidth:
                bCol = 0
            bVal = str(self.matrix[BitArray2D.godel(bRow, bCol)])

            if cRow >= self.topLevelWidth:
                cRow = 0
            cVal = str(self.matrix[BitArray2D.godel(cRow, cCol)])

            if dCol >= self.topLevelWidth:
                dCol = 0
            if dRow >= self.topLevelWidth:
                dRow = 0
            dVal = str(self.matrix[BitArray2D.godel(dRow, dCol)])

        currentState = aVal + bVal + cVal + dVal
        newState = updateRules[currentState]
        newStateArray = updateRules[currentState]
        newState = newStateArray[rnd.randint(0, len(newStateArray) - 1)]

        aVal = newState[0]
        bVal = newState[1]
        cVal = newState[2]
        dVal = newState[3]
        self.matrix[aRow, aCol] = int(aVal)
        self.matrix[bRow, bCol] = int(bVal)
        self.matrix[cRow, cCol] = int(cVal)
        self.matrix[dRow, dCol] = int(dVal)
예제 #3
0
    def _getQuadrantString(self, quadrant):
        topCoord = quadrant.topCoordinate
        leftCoord = quadrant.leftCoordinate

        quadString = ""
        for row in xrange(topCoord, topCoord + 2):
            for col in xrange(leftCoord, leftCoord + 2):
                quadString += str(self.matrix[BitArray2D.godel(row, col)])

        return quadString
예제 #4
0
    def _getQuadrantValue(self, quadrant):
        totalOnes = 0
        halfCount = (quadrant.quadrantWidth**2) / 2

        if halfCount < 1:
            return self.matrix[BitArray2D.godel(quadrant.topCoordinate,
                                                quadrant.leftCoordinate)]

        for row in xrange(0, quadrant.quadrantWidth):
            for column in xrange(0, quadrant.quadrantWidth):
                bitValue = self.matrix[BitArray2D.godel(
                    quadrant.topCoordinate + row,
                    quadrant.leftCoordinate + column)]
                if bitValue == 1:
                    totalOnes += 1
                if totalOnes == halfCount:
                    return 1

        return 0
예제 #5
0
    def _applyInstructions(self, instructions):
        applyOperation = instructions.applyOperation
        applyFromQuadrantTopCoord = instructions.applyFromQuadrant.topCoordinate
        applyFromQuadrantLeftCoord = instructions.applyFromQuadrant.leftCoordinate
        applyToQuadrantTopCoord = instructions.applyToQuadrant.topCoordinate
        applyToQuadrantLeftCoord = instructions.applyToQuadrant.leftCoordinate
        quadrantWidth = instructions.applyFromQuadrant.quadrantWidth

        for row in xrange(0, quadrantWidth):
            fromRowCoord = applyFromQuadrantTopCoord + row
            toRowCoord = applyToQuadrantTopCoord + row
            for col in xrange(0, quadrantWidth):
                fromColCoord = applyFromQuadrantLeftCoord + col
                toColCoord = applyToQuadrantLeftCoord + col
                fromValue = self.matrix[BitArray2D.godel(
                    fromRowCoord, fromColCoord)]
                toValue = self.matrix[BitArray2D.godel(toRowCoord, toColCoord)]
                self.matrix[toRowCoord,
                            toColCoord] = applyOperation(leftBit=fromValue,
                                                         rightBit=toValue)
예제 #6
0
def writeMatrixImage(matrix, filename):
    # copy BitArray2D matrix to numpy array
    data = numpy.empty(shape=(matrix.rows, matrix.columns), dtype="bool")
    for row in xrange(0, matrix.rows):
        for col in xrange(0, matrix.columns):
            data[row, col] = matrix[BitArray2D.godel(row, col)]

    fig = pyplot.figure(figsize=(18, 18), dpi=80)
    pyplot.cla()
    ms = matshow(data, fignum=False, cmap=cm.binary)
    fig.savefig(filename)
    pyplot.close()
예제 #7
0
    def _applyOperatorChange(self, operator, quadrant, rightOperand):
        applyToQuadrantTopCoord = quadrant.topCoordinate
        applyToQuadrantLeftCoord = quadrant.leftCoordinate

        rightOperandIndex = 0
        for row in xrange(applyToQuadrantTopCoord,
                          applyToQuadrantTopCoord + 2):
            for col in xrange(applyToQuadrantLeftCoord,
                              applyToQuadrantLeftCoord + 2):
                leftValue = self.matrix[BitArray2D.godel(row, col)]
                rightValue = rightOperand[rightOperandIndex]
                setValue = self._getBooleanOperationValue(
                    operator=operator,
                    leftOperand=leftValue,
                    rightOperand=rightValue)
                self.matrix[row, col] = setValue
                rightOperandIndex = rightOperandIndex + 1
예제 #8
0
    def _getNeighborhoodEntropy(self, neighborhood):
        topCoord = neighborhood.topCoordinate
        leftCoord = neighborhood.leftCoordinate
        neighborhoodWidth = neighborhood.neighborhoodWidth
        zeros = 0
        for row in xrange(topCoord, topCoord + neighborhoodWidth):
            for col in xrange(leftCoord, leftCoord + neighborhoodWidth):
                if self.matrix[BitArray2D.godel(row, col)] == 0:
                    zeros = zeros + 1

        length = neighborhoodWidth**2
        ones = length - zeros
        pctZeros = float(zeros) / length
        pctOnes = float(ones) / length
        if pctZeros == 1 or pctOnes == 1:
            entropy = 0
        else:
            entropy = -((math.log(pctZeros, 2) * pctZeros) +
                        (math.log(pctOnes, 2) * pctOnes))
        return entropy
예제 #9
0
    def _getQuadrantEntropy(self, quadrant):
        topCoord = quadrant.topCoordinate
        leftCoord = quadrant.leftCoordinate
        quadrantWidth = quadrant.quadrantWidth
        zeros = 0
        for row in xrange(topCoord, topCoord + quadrantWidth):
            for col in xrange(leftCoord, leftCoord + quadrantWidth):
                if self.matrix[BitArray2D.godel(row, col)] == 0:
                    zeros = zeros + 1

        length = quadrantWidth**2
        ones = length - zeros
        pctZeros = float(zeros) / length
        pctOnes = float(ones) / length
        if pctZeros == 1 or pctOnes == 1:
            entropy = 0
        else:
            entropy = -((math.log(pctZeros, 2) * pctZeros) +
                        (math.log(pctOnes, 2) * pctOnes))
        return entropy
예제 #10
0
def getBitArray2DFromBitmapFile(filename, width):
	array = numpy.fromfile(file = filename, dtype = "uint8")[62 : ] # stripping header
	bitArray = []
	for byte in array:
		backwardsByte = []
		for bit in xrange(8):
			invertedBit = (byte >> bit) & 1
			finalBit = 0
			if invertedBit == 0:
				finalBit = 1
			backwardsByte.append(finalBit)
			
		for bit in xrange(7, -1, -1):
			bitArray.append(backwardsByte[bit])

	data = BitArray2D.BitArray2D(rows = width, columns = width)
	for row in xrange(0, width):
		for col in xrange(0, width):
			data[(width - 1) - row, col] = bitArray[(width * row) + col]
	#		data[row, col] = bitArray[(width * row) + col]
			
	return data
예제 #11
0
def getRandomBitMatrix(width):
    data = BitArray2D.BitArray2D(rows=width, columns=width)
    for row in xrange(0, width):
        for col in xrange(0, width):
            data[row, col] = rnd.getrandbits(1)
    return data
예제 #12
0
    def _applyNeighborhoodChange(self, neighborhood, step):
        if step == "EVEN":
            aRow = neighborhood.topCoordinate
            aCol = neighborhood.leftCoordinate
            bRow = neighborhood.topCoordinate
            bCol = neighborhood.leftCoordinate + 1
            cRow = neighborhood.topCoordinate + 1
            cCol = neighborhood.leftCoordinate
            dRow = neighborhood.topCoordinate + 1
            dCol = neighborhood.leftCoordinate + 1
            aVal = str(self.matrix[BitArray2D.godel(aRow, aCol)])
            bVal = str(self.matrix[BitArray2D.godel(bRow, bCol)])
            cVal = str(self.matrix[BitArray2D.godel(cRow, cCol)])
            dVal = str(self.matrix[BitArray2D.godel(dRow, dCol)])
            currentState = aVal + bVal + cVal + dVal
            newState = fredkinRules[currentState]
            aVal = newState[0]
            bVal = newState[1]
            cVal = newState[2]
            dVal = newState[3]
            self.matrix[aRow, aCol] = int(aVal)
            self.matrix[bRow, bCol] = int(bVal)
            self.matrix[cRow, cCol] = int(cVal)
            self.matrix[dRow, dCol] = int(dVal)

        else:
            aRow = neighborhood.topCoordinate + 1
            aCol = neighborhood.leftCoordinate + 1
            bRow = neighborhood.topCoordinate + 1
            bCol = neighborhood.leftCoordinate + 1 + 1
            cRow = neighborhood.topCoordinate + 1 + 1
            cCol = neighborhood.leftCoordinate + 1
            dRow = neighborhood.topCoordinate + 1 + 1
            dCol = neighborhood.leftCoordinate + 1 + 1

            aVal = str(self.matrix[BitArray2D.godel(aRow, aCol)])
            if bCol >= self.topLevelWidth:
                bVal = "1"
            else:
                bVal = str(self.matrix[BitArray2D.godel(bRow, bCol)])

            if cRow >= self.topLevelWidth:
                cVal = "1"
            else:
                cVal = str(self.matrix[BitArray2D.godel(cRow, cCol)])

            if dRow >= self.topLevelWidth or dCol >= self.topLevelWidth:
                dVal = "1"
            else:
                dVal = str(self.matrix[BitArray2D.godel(dRow, dCol)])

            currentState = aVal + bVal + cVal + dVal
            newState = fredkinRules[currentState]
            aVal = newState[0]
            bVal = newState[1]
            cVal = newState[2]
            dVal = newState[3]

            self.matrix[aRow, aCol] = int(aVal)
            if bCol < self.topLevelWidth:
                self.matrix[bRow, bCol] = int(bVal)

            if cRow < self.topLevelWidth:
                self.matrix[cRow, cCol] = int(cVal)

            if dRow < self.topLevelWidth and dCol < self.topLevelWidth:
                self.matrix[dRow, dCol] = int(dVal)
예제 #13
0
#print "---------------"
#
#ba[1, 1] = 0
#print subArray
#

#  00 00 00 00
#  00 01 10 11
#  
#  01 01 01 01
#  00 01 10 11
#  
#  10 10 10 10
#  00 01 10 11
#  
#  11 11 11 11
#  00 01 10 11

# send top level entropy down the chain.  

inputMatrix = BitArray2D.BitArray2D(bitstring = "00000000\n00011011\n01010101\n00011011\n10101010\n00011011\n11111111\n00011011")
#inputMatrix = mutator.getRandomBitMatrix(8)
mutator.writeMatrixImage(matrix = inputMatrix, filename = "test_00000.png")
m = mutator.Mutator(matrix = inputMatrix)

for i in xrange(0, 100):
	finalInstructions = m.mutate()
	print "iteration", i, "complete"
	m.printOperationStats()
	mutator.writeMatrixImage(matrix = inputMatrix, filename = "test_" + str(i + 1).zfill(5) + ".png")