def plotSecurity(canvas, text="12345"): if not text: return plotWidth = canvas.size[0] plotHeight = canvas.size[1] if plotHeight<=0 or plotWidth<=0: return bgColor = pid.Color(0.6+0.4*random.random(), 0.6+0.4*random.random(), 0.6+0.4*random.random()) canvas.drawRect(0,0,plotWidth,plotHeight, edgeColor=bgColor, fillColor=bgColor) for i in range(30): randomColor = pid.Color(0.6+0.4*random.random(), 0.6+0.4*random.random(), 0.6+0.4*random.random()) scaleFont=pid.Font(ttf="cour",size=random.choice(range(20, 50))) canvas.drawString(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), int(random.random()*plotWidth), int(random.random()*plotHeight), font=scaleFont, color=randomColor, angle=random.choice(range(-45, 50))) step = (plotWidth-20)/len(text) startX = 20 for item in text: randomColor = pid.Color(0.6*random.random(),0.6*random.random(), 0.6*random.random()) scaleFont=pid.Font(ttf="verdana",size=random.choice(range(50, 60)),bold=1) canvas.drawString(item, startX, plotHeight/2-10, font=scaleFont, color=randomColor, angle=random.choice(range(-45, 50))) startX += step
def send_hor_rule(self): self.send_line_break() self.y = self.y + self.oldLineHeight border = self.fsizex self.pc.drawLine(border, self.y, self.rmargin - border, self.y, piddle.Color(0.0, 0.0, 200 / 255.0)) self.y = self.y + self.oldLineHeight
def __init__(self, darken=0): profile = [[103, 0, 31], [178, 24, 43], [214, 96, 77], [244, 165, 130], [253, 219, 199], [255, 255, 255], [209, 229, 240], [146, 197, 222], [67, 147, 195], [33, 102, 172], [5, 48, 97]] profile.append(profile[-1]) # terminator... profile.append(profile[-1]) # terminator... self.LUT = [] for i in range(255): a = i / 254.0 # keep it even so that it's white in the middle b = a * (len(profile) - 3) bi = int(b) # round down db = b - bi # difference assert (db >= 0.0) if darken: idb = (1.0 - db) / 270.0 db /= 270.0 else: idb = (1.0 - db) / 254.0 db /= 254.0 rgb = [ profile[bi][x] * idb + profile[bi + 1][x] * db for x in range(3) ] self.LUT.append(piddle.Color(rgb[0], rgb[1], rgb[2]))
def _colorize1(cc): bluefunc = lambda cc: 1.0 / (1.0 + math.exp(-10 * (cc - 0.6))) redfunc = lambda cc: 1.0 / (1.0 + math.exp(10 * (cc - 0.5))) cred = bluefunc(cc) cgre = redfunc(cc) cblu = 1 - pow(redfunc(cc + 0.1), 2) - bluefunc(cc - 0.15) return piddle.Color(cred, cgre, cblu)
def _colorize0(cc): #bluefunc = lambda cc:1.0 / (1.0 + math.exp(-10*(cc-0.6))) #redfunc = lambda cc:1.0 / (1.0 + math.exp(10*(cc-0.5))) cc = max(-50, cc) cc = min(50, cc) bluefunc = lambda cc: 1.0 / (1.0 + math.exp(-10 * (cc - 0.6))) redfunc = lambda cc: 1.0 / (1.0 + math.exp(10 * (cc - 0.5))) cblu = bluefunc(cc) cred = redfunc(cc) cgre = 1 - pow(redfunc(cc + 0.1), 2) - bluefunc(cc - 0.15) #cgre = 1 - pow(redfunc(cc+0.2),2) - bluefunc(cc-0.3) return piddle.Color(cred, cgre, cblu)
def BWSpectrum(n=100): multiple = 10 if n == 1: return [pid.Color(0,0,0)] elif n == 2: return [pid.Color(0,0,0),pid.Color(1,1,1)] elif n == 3: return [pid.Color(0,0,0),pid.Color(0.5,0.5,0.5),pid.Color(1,1,1)] step = 1.0/n x = 0.0 out = [] for i in range(n): out.append(pid.Color(x,x,x)); x += step return out
def colorSpectrumOld(n): if n == 1: return [pid.Color(1,0,0)] elif n == 2: return [pid.Color(1,0,0),pid.Color(0,0,1)] elif n == 3: return [pid.Color(1,0,0),pid.Color(0,1,0),pid.Color(0,0,1)] else: step = 2.0/(n-1) red = 1.0 green = 0.0 blue = 0.0 colors = [pid.Color(red,green,blue)] i = 1 greenpeak = 0 while i < n: if red >= step: red -= step green += step if green >= 1.0: greenpeak = 1 blue += green -1.0 green = 1.0 else: red = 0.0 if greenpeak: green -= step blue += step else: green += step if green >= 1.0: greenpeak = 1 blue += green -1.0 green = 2.0 -green elif green < 0.0: green = 0.0 else: pass colors.append(pid.Color(red,green,blue)) i += 1 return colors
def colorSpectrum(n=100): multiple = 10 if n == 1: return [pid.Color(1, 0, 0)] elif n == 2: return [pid.Color(1, 0, 0), pid.Color(0, 0, 1)] elif n == 3: return [pid.Color(1, 0, 0), pid.Color(0, 1, 0), pid.Color(0, 0, 1)] N = n * multiple out = [None] * N for i in range(N): x = float(i) / N out[i] = pid.Color(redfunc(x), greenfunc(x), bluefunc(x)) out2 = [out[0]] step = N / float(n - 1) j = 0 for i in range(n - 2): j += step out2.append(out[int(j)]) out2.append(out[-1]) return out2
def anchor_bgn(self, href, name, type): if href: self.oldcolor = self.color self.color = piddle.Color(0.0, 0.0, 200 / 255.0) self.anchor = (href, name, type)
def dendrogram(self, labels, width=500, height=None, margin=20, hook=40, line_size=2.0, cluster_colors=[], canvas=None, line_width=1, color_mode=0, incremental_height=1, matr=[], g_lines=0, additional_labels=[], additional_matr=[], add_tags=[], adwidth=1.0, plot_gains=0, gains=[], gain_width=70, plot_ints=0, right_align=0, im=None): # prevent divide-by-zero... if len(labels) < 2: return canvas if plot_ints or (plot_gains and len(gains) == 0): # the gains have been proposed but not calculated if len(gains) > 0: warnings.warn( 'Ignoring the given gains: need to refer to the interaction matrix.' ) gains = [] # discretized gains mv = 1.0 / max(1e-6, max(im.gains)) for v in im.gains: gains.append( v * mv ) # normalize with respect to the best attribute from correlation analysis. if len(gains) > 0: # fix the widths gain_l = [] for i in range(self.n): gain_l.append(gains[i]) max_intlen = 0.0 if plot_ints: # include the interactions between all pairs intlist = [] for i in range(self.n - 1): idx1 = self.order[i] - 1 idx2 = self.order[i + 1] - 1 ig = im.way3[(min(idx1, idx2), max(idx1, idx2), -1)].InteractionInformation() if gains[idx1] < gains[idx2]: idx1, idx2 = idx2, idx1 if ig > 0: max_intlen = max(max_intlen, gain_width * ig * mv) gain_l[idx1] += ig * mv # possibly new maximum width intlist.append((idx1, ig * mv, 0.0)) else: intlist.append((idx2, ig * mv, 1.0)) else: intlist = [] ## ADJUST DIMENSIONS ### if canvas == None: tcanvas = piddlePIL.PILCanvas() else: tcanvas = canvas normal = piddle.Font(face=_defaultfont) bold = piddle.Font(face=_defaultfont, bold=1) if height == None: # compute the height lineskip = int(line_size * tcanvas.fontHeight(normal) + 1) lines = len(labels) - 1 if len(additional_labels) > 0: lines += 1 + len(additional_labels) height = int(2.0 * margin + lineskip * (lines) + tcanvas.fontHeight(normal) + 1) else: # compute lineskip lineskip = (height - 2.0 * margin - tcanvas.fontHeight(normal)) / (len(labels) - 1) maxlabel = 0.0 spacew = tcanvas.stringWidth(" ", font=normal) swids = [] for i in range(len(labels)): swid = tcanvas.stringWidth(labels[i], font=normal) swids.append(swid + spacew) if len(gains) > 0: assert (len(gains) == len(labels)) swid += spacew + gain_l[i] * gain_width maxlabel = max(maxlabel, swid) maxswid = max(swids) if canvas == None: canvas = piddlePIL.PILCanvas(size=(width, height)) if len(matr) > 0: block = lineskip / 2 - 1 else: block = 0 _colorize = _color_picker(color_mode) ### EXTRACT THE DENDROGRAM ### vlines = [] # vertical lines (cluster) hlines = [] # horizontal lines (clusters) origins = [0.0] * self.n # text positions xpositions = [0.0] * self.n # cluster x positions (height) ypositions = [0] * self.n # cluster y positions (average element) attcolors = [0] * self.n y = margin for i in range(len(labels)): # self.order identifies the label at a particular row ypositions[self.n - self.order[i]] = y y += lineskip xpositions.append("sentry") ypositions.append("sentry") p = self.n # bottom-up construction height = 0.0 displacement = 0.0 for i in range(self.n - 1): if len(cluster_colors) == self.n - 1: coloV = _colorize(cluster_colors[i][0]) coloH = _colorize(cluster_colors[i][1]) else: # no color information coloH = coloV = piddle.black # obtain the height if incremental_height: nheight = min(xpositions[p + self.merging[i][0]], xpositions[p + self.merging[i][1]]) nheight -= self.height[i] displacement = min(displacement, nheight) else: height -= self.height[i] nheight = height vlines.append((nheight, ypositions[p + self.merging[i][0]], ypositions[p + self.merging[i][1]], coloV)) avg = 0.0 for j in self.merging[i]: # record text origins v = ypositions[p + j] if j < 0: origins[-1 - j] = nheight attcolors[-1 - j] = coloH else: # create the cluster lines hlines.append((v, xpositions[p + j], nheight, coloH)) avg += v # recompute the average height of new cluster ypositions.append(0.5 * avg) xpositions.append(nheight) #print displacement ### DRAWING ### offset = width - maxlabel - hook - 2 * margin if len(matr) > 0: offset -= 2 * (len( matr[0] )) * adwidth * block + 2 * block # correct the right-hand side hs = (offset - margin) / (height - displacement) # height scaling if incremental_height: hs = -hs halfline = canvas.fontAscent(font=normal) / 2.0 # print line-guides if g_lines and len(matr) == len(labels): colo = piddle.Color(0.9, 0.9, 0.9) y = margin s = len(matr[0]) sx1 = width - margin - block sx2 = width - margin - 2 * (len(matr[0])) * adwidth * block - block canvas.drawLine(sx1, y - block - 1, sx2, y - block - 1, colo, width=1) x2 = width - margin - block for i in range(len(labels)): idx = self.order[i] - 1 x1 = offset + hook - hs * (origins[idx]) + tcanvas.stringWidth( labels[idx], font=normal) + 4 canvas.drawLine(x1, y, sx2, y, colo, width=1) canvas.drawLine(sx1, y + block + 1, sx2, y + block + 1, colo, width=1) y += lineskip if len(additional_labels) > 0: y += lineskip canvas.drawLine(sx1, y - block - 1, sx2, y - block - 1, colo, width=1) for i in range(len(additional_labels)): x1 = offset + hook + 5 + tcanvas.stringWidth( additional_labels[i], font=normal) + 4 canvas.drawLine(x1, y, sx2, y, colo, width=1) canvas.drawLine(sx1, y + block + 1, sx2, y + block + 1, colo, width=1) y += lineskip # vertical guides for i in range(len(matr[0]) + 1): x = width - margin - (2 * (i) * adwidth * block) - block canvas.drawLine(x, margin - block, x, y - lineskip + block + 1, colo, width=1) # print names y = margin for i in range(len(labels)): # self.order identifies the label at a particular row idx = self.order[i] - 1 x = offset - hs * (origins[idx]) if labels[idx][0] == '*': canvas.drawString(labels[idx][1:], hook + x, y + halfline, font=bold) else: canvas.drawString(labels[idx], hook + x, y + halfline, font=normal) # draw the hook canvas.drawLine(x, y, x + hook * 0.8, y, attcolors[idx], width=line_width) GSERIF = 1.2 * line_width MULT = 1.2 * line_width XMULT = 0.6 * line_width XSERIF = 0.6 * line_width SWIDTH = 1 if len(gains) > 0: # draw the gain line if right_align: orig = width - margin - gain_width * gains[idx] - max_intlen else: orig = hook + x + swids[idx] if gain_width * gains[idx] >= 2.0 * MULT: canvas.drawLine(orig + MULT, y, orig + gain_width * gains[idx] - MULT, y, piddle.black, width=MULT) # actual line canvas.drawLine(orig, y, orig + gain_width * gains[idx], y, piddle.black, width=SWIDTH) # thin line canvas.drawLine(orig, y - GSERIF, orig, y + GSERIF, piddle.black, width=SWIDTH) #serif 1 canvas.drawLine(orig + gain_width * gains[idx], y - GSERIF, orig + gain_width * gains[idx], y + GSERIF, piddle.black, width=SWIDTH) #serif2 if len(intlist) > 0 and i > 0: (qidx, widt, cc) = intlist[i - 1] if right_align: nx = width - margin - max_intlen else: nx = offset - hs * (origins[qidx]) + hook + swids[ qidx] + gain_width * gains[qidx] ny = y - 0.5 * lineskip colo = _colorize(cc) if widt > 0: disp = XMULT seri = XSERIF else: disp = -XMULT seri = -XSERIF if abs(gain_width * widt) >= 2.0 * XMULT: canvas.drawLine(nx + disp, ny, nx + gain_width * widt - disp, ny, colo, width=XMULT) # actual line canvas.drawLine(nx + gain_width * widt - seri, ny - seri, nx + gain_width * widt, ny, colo, width=SWIDTH) # arrowpoint 1 canvas.drawLine(nx + gain_width * widt - seri, ny + seri, nx + gain_width * widt, ny, colo, width=SWIDTH) # arrowpoint 2 canvas.drawLine(nx, ny, nx + gain_width * widt, ny, colo, width=SWIDTH) # thin line canvas.drawLine(nx, ny - XSERIF, nx, ny + XSERIF, colo, width=SWIDTH) # serif 1 y += lineskip for i in range(len(additional_labels)): y += lineskip if additional_labels[i][0] == '*': canvas.drawString(additional_labels[i][1:], offset + hook + 5, y + halfline, font=bold) else: canvas.drawString(additional_labels[i], offset + hook + 5, y + halfline, font=normal) y += lineskip * 1.5 for i in range(len(add_tags)): wi = tcanvas.stringWidth(add_tags[i], font=normal) x = width - margin - 2 * (len(add_tags) - i - 0.5) * adwidth * block - block - wi / 2 canvas.drawString(add_tags[i], x, y + halfline, font=bold) # print lines for (y, x1, x2, colo) in hlines: canvas.drawLine(offset - (x1) * hs, y, offset - (x2) * hs, y, colo, width=line_width) vlines.reverse() # smaller clusters are more interesting for (x, y1, y2, colo) in vlines: canvas.drawLine(offset - (x) * hs, y1, offset - (x) * hs, y2, colo, width=line_width) ### MATRIX RENDERING ### if len(matr) == len(labels): y = margin for i in range(len(labels)): # print labels[i],matr[i] idx = self.order[i] - 1 mm = matr[idx] for j in range(len(mm)): # self.order identifies the label at a particular row x = width - margin - 2 * (len(mm) - j - 0.5) * adwidth * block - block v = 1 - mm[j] #if v < 254.0/255.0: colo = piddle.Color(v, v, v) canvas.drawRect(x - adwidth * block + 1, y - block, x + adwidth * block - 1, y + block, edgeColor=colo, fillColor=colo) y += lineskip for i in range(len(additional_matr)): y += lineskip mm = additional_matr[i] for j in range(len(mm)): x = width - margin - 2 * (len(mm) - j - 0.5) * adwidth * block - block v = 1 - mm[j] colo = piddle.Color(v, v, v) canvas.drawRect(x - adwidth * block + 1, y - block, x + adwidth * block - 1, y + block, edgeColor=colo, fillColor=colo) canvas.flush() return canvas
def _bw2(cc): v = abs(cc) * 1.8 return piddle.Color(v, v, v)
def _bw3(cc): v = cc return piddle.Color(v, v, v)
def _blackwhite(cc): v = 1.0 - (1.0 / max(1e-6, cc)) return piddle.Color(v, v, v)
def genColors(n=100): out = [None] * n for i in range(n): x = float(i) / n out[i] = piddle.Color(redfunc(x), greenfunc(x), bluefunc(x)) return out