示例#1
0
def resultsToTorc(resultList, colored=False):
    """Takes the result and returns an image showing a Torc indicating the
        similarity relations of the compared texts in the results.
        
        A Torc is a kind of overview which allows the user to recognize the similarity
        relations between different texts. Therefore all texts are arranged on a circle.
        For each relation of similarity between two texts a connecting line is drawn.
    """
    #check preconditions
    if type(resultList) != type([]):
        raise NoValidArgumentError, 'Input must be of type list'
    elif len(resultList) == 0:
        return None
    else:
        for result in resultList:
            if type(result) != type(PlagResult()):
                raise NoValidArgumentError, 'Input list should only contain values of type PlagResult.'
    #1. get all identifiers of the results
    idSet = set()
    for result in resultList:
        for id in result.getIdentifier():
            idSet.add(id)
    idSet = list(idSet)
    idSet.sort()

    #2. create a circle with a size depending on the number of identifier
    font = ImageFont.load_default()
    freespace = computeMaxIdLength(idSet, font)
    margin = 10
    radius = computeRadius(
        len(idSet))  # computes radius depending on number of ids
    xM = freespace + radius + margin  #middle x pos of circle
    yM = freespace + radius + margin  #middle y pos of circle
    img = Image.new('RGB', (2 * xM, 2 * yM), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    draw.arc((freespace + margin, freespace + margin, freespace + margin +
              (2 * radius), freespace + margin + (2 * radius)),
             0,
             360,
             fill=(150, 150, 150))

    #3. arrange the ids along the circle and save the coordinates for each id
    distToNextId = 360 / len(idSet)
    angles = range(0, 360, distToNextId)
    idPosDict = {}
    for idNr in xrange(0, len(idSet)):
        # x = xM + r * cos phi und y = yM + r * sin phi
        pos = (xM + (radius * cos(radians(angles[idNr]))),
               yM + (radius * sin(radians(angles[idNr]))))
        idPosDict.setdefault(idSet[idNr], pos)

    # use a truetype font and draw the id names
    for id in idPosDict:
        draw.text(computeFontPos(font, draw, str(id), idPosDict.get(id), xM,
                                 yM),
                  str(id),
                  font=font,
                  fill=(0, 0, 0))

    #4. walk through the results and plot the similarity relations as lines between the Ids
    if colored:
        #TODO: Params von aussen eingeben?
        clusters = getClusters(resultList,
                               onlyPositives=False,
                               onlyNonZeroSimilarities=False)

    for result in resultList:
        if result.isSuspectPlagiarism():
            ids = result.getIdentifier()
            if colored:
                color = getColorForScope(
                    getClusterNr(ids[0], ids[1], clusters),
                    range(len(clusters)))
            else:
                color = (0, 0, 0)
            draw.line([idPosDict.get(ids[0]),
                       idPosDict.get(ids[1])],
                      fill=color)

    del draw  #free draw instance

    #5. return the image
    return img
示例#2
0
def createHeatmapChart(matrix, xLabels, yLabels, scopes = [0.2, 0.4, 0.6, 0.8, 1], 
                       recSize = 20, colorBG = (255, 255, 255), colorFG = (0, 0, 0), 
                       colorChartBG = (230, 230, 230), colorGrid = (100, 100, 100),
					   font = ImageFont.load_default()):
    """Creates a heatmap chart for the given values in the matrix labeled with the labels
        given in xDict and yDict TODO:
        
        options:
            - scopes - list of values that describe the scopes for the matrix values,
                        e.g. scopes = [0.2, 0.4, 0.6, 0.8, 1] (default)
            - font - font used
            - recSize - size of a single indicator rectangle in the heatmap
            - colorBG - background color of the chart as rgb value (default: (255, 255, 255))
            - colorFG - foreground color of the chart as rgb value (default: (0, 0, 0))
            - colorChartBG - background color of the chart as rgb value (default: (230, 230, 230))
            - colorGrid - color of the grid as rgb value (dafault: (100, 100, 100)); if set to
                            "None" no grid will be drawn
    """
    #===init===
    #margin to left and bottom
    margin = 5
    #maximal identifier length 
    maxIdLength = computeMaxIdLength(set(xLabels+yLabels), font)
    #size of the img
    maxX = margin + maxIdLength + ((len(xLabels)+2) * recSize)
    maxY = margin + maxIdLength + (len(yLabels)+2) * recSize
    #lower left corner fo the chart
    offset = (margin+maxIdLength+4, maxY-maxIdLength-margin)
    
    #===create img===
    img = Image.new("RGB", (maxX, maxY), colorBG)
    draw = ImageDraw.Draw(img)
    
    #draw chart background
    draw.rectangle((offset, (offset[0]+((len(xLabels)+1)*recSize), offset[1]-((len(yLabels)+1)*recSize))), fill=colorChartBG)
    
    #draw heatmap
    for row in xrange(len(matrix)):
        for col in xrange(len(matrix[0])):
            pos = (offset[0]+col*recSize, offset[1]-row*recSize, offset[0]+(col+1)*recSize, offset[1]-(row+1)*recSize)
            fill = getColorForScope(matrix[row][col], scopes, colorChartBG)
            draw.rectangle(pos, fill=fill)
    
    #draw grid
    #x-axis
    for i in xrange(len(yLabels)+1):
        yPos = offset[1]-((i+1)*recSize)
        draw.line(((offset[0], yPos), (offset[0]+recSize*(len(xLabels)+1), yPos)), fill=colorGrid)
    #y-axis
    for i in xrange(len(xLabels)+1):
        draw.line(((offset[0]+(i+1)*recSize, offset[1]), (offset[0]+(i+1)*recSize, offset[1]-recSize*(len(yLabels)+1))), fill=colorGrid)
    
    #draw axes
    #x-axis
    draw.line((offset, (offset[0]+recSize*(len(xLabels)+1), offset[1])), fill=colorFG)
    for i in xrange(len(xLabels)):
        draw.line(((offset[0]+recSize/2+i*recSize, offset[1]-2),(offset[0]+recSize/2+i*recSize, offset[1]+2)), fill=colorFG)
    #y-axis
    draw.line((offset, (offset[0], offset[1]-recSize*(len(yLabels)+1))), fill=colorFG)
    for i in xrange(len(yLabels)):
        draw.line(((offset[0]-2, offset[1]-(recSize/2+i*recSize)),(offset[0]+2, offset[1]-(recSize/2+i*recSize))), fill=colorFG)
    
    #draw ids
    for i in xrange(len(yLabels)):
        name = yLabels[i]
        pos = i
        draw.text((margin, offset[1]-((pos+1)*recSize)), name, font=font, fill=colorFG)
    for i in xrange(len(xLabels)):
        name = xLabels[i]
        pos = i
        img.paste(rotText(name, font=font, colorBG=colorBG, colorFG=colorFG), (offset[0]+(recSize/5)+((pos)*recSize), offset[1]+2))

    #clean up
    del draw

    #return heatmap chart img
    return img
示例#3
0
文件: torc.py 项目: dtgit/dtedu
def resultsToTorc(resultList, colored=False):
    """Takes the result and returns an image showing a Torc indicating the
        similarity relations of the compared texts in the results.
        
        A Torc is a kind of overview which allows the user to recognize the similarity
        relations between different texts. Therefore all texts are arranged on a circle.
        For each relation of similarity between two texts a connecting line is drawn.
    """
    #check preconditions
    if type(resultList) != type([]):
        raise NoValidArgumentError, 'Input must be of type list'
    elif len(resultList) == 0:
        return None
    else:
        for result in resultList:
            if type(result) != type(PlagResult()):
                raise NoValidArgumentError, 'Input list should only contain values of type PlagResult.'
    #1. get all identifiers of the results
    idSet = set()
    for result in resultList:
        for id in result.getIdentifier():
            idSet.add(id)
    idSet = list(idSet)
    idSet.sort()
            
    #2. create a circle with a size depending on the number of identifier
    font = ImageFont.load_default()
    freespace = computeMaxIdLength(idSet, font)
    margin = 10
    radius = computeRadius(len(idSet)) # computes radius depending on number of ids
    xM = freespace + radius + margin    #middle x pos of circle
    yM = freespace + radius + margin   #middle y pos of circle
    img = Image.new('RGB', (2*xM, 2*yM), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    draw.arc((freespace+margin, freespace+margin, freespace+margin+(2*radius), freespace+margin+(2*radius)), 0, 360, fill = (150, 150, 150))
    
    #3. arrange the ids along the circle and save the coordinates for each id
    distToNextId = 360 / len(idSet)
    angles = range(0, 360, distToNextId)
    idPosDict = {}
    for idNr in xrange(0, len(idSet)):
        # x = xM + r * cos phi und y = yM + r * sin phi
        pos = (xM + (radius * cos(radians(angles[idNr]))), 
               yM + (radius * sin(radians(angles[idNr]))))
        idPosDict.setdefault(idSet[idNr], pos)
    
    # use a truetype font and draw the id names
    for id in idPosDict:
        draw.text(computeFontPos(font, draw, str(id), idPosDict.get(id), xM, yM), 
                  str(id), 
                  font=font,
                  fill = (0, 0, 0))
    
    #4. walk through the results and plot the similarity relations as lines between the Ids
    if colored:
        #TODO: Params von aussen eingeben?
        clusters = getClusters(resultList, onlyPositives=False, onlyNonZeroSimilarities=False)

    for result in resultList:
        if result.isSuspectPlagiarism():
            ids = result.getIdentifier()
            if colored:
                color = getColorForScope(getClusterNr(ids[0], ids[1], clusters), range(len(clusters)))
            else:
                color = (0,0,0)
            draw.line([idPosDict.get(ids[0]), idPosDict.get(ids[1])], fill = color)
        
    del draw #free draw instance
   
    #5. return the image
    return img