def makesaliencelegend(): out = svg.svg(width=2000, height=100, viewbox='0 0 2000 100') if c.swapsaliencemagnitude: te = 'Magnitude:' else: te = 'Salience:' out.add(svg.text(te, 70, 50, fontsize=c.font)) for i in range(0,2): if i==0: red,green,blue = c.colorsbenefit[0] elif c.colorsbenefit[0]==c.colorscost[0]: return out else: red,green,blue = c.colorscost[0] for j in range(0,3+c.includezerosalience): out.add(svg.text(j + (1-c.includezerosalience), (i*950+290+j*(200-60*c.includezerosalience)), 50, fontsize=c.font)) if(c.saliencebycolor): s = ((j+1)**c.saliencebycolor)/((3.0+c.includezerosalience)**c.saliencebycolor) else: s = 1 if(c.saliencebywidth): out.add(svg.rect((i*950+320+j*(200-60*c.includezerosalience)),11,80,50, stroke='black', fill='none')) if(c.saliencebywidth>1): w = 80.0/(c.saliencebywidth**3)*(c.saliencebywidth**(j+1-c.includezerosalience)) else: w = 80.0/(3+c.includezerosalience)*(j+1) out.add(svg.rect((i*950+360+j*(200-60*c.includezerosalience)-(w/2)),11,w,50, stroke='none', fill='rgb(%s,%s,%s)' % (red,green,blue), p='fill-opacity="%s"' % s)) else: out.add(svg.rect((i*950+320+j*(200-60*c.includezerosalience)),11,80,50, stroke='none', fill='rgb(%s,%s,%s)' % (red,green,blue), p='fill-opacity="%s"' % s)) return out
def render(self, handle=sys.stdout): handle.write( svg.header( "Helena", "scarf", width=self.width * self.pixel, height=self.length * self.pixel, )) height_offset = 0 for block in self.pieces: handle.write(svg.group_begin()) for r in range(block.height): for c in range(self.width): props = { "fill": "red" if block.data[r][c] else "white", "opacity": 1, "stroke": "none", } handle.write( svg.rect( x=c * self.pixel, y=(height_offset + r) * self.pixel, width=self.pixel, height=self.pixel, style=props, )) height_offset += block.height handle.write(svg.group_end()) handle.write(svg.footer())
def _generate_code(self): """ Generate the text for drawing this multislot at (0,0) """ canvas = svg.IndentingStringIO("") upperLeft = (0,0) x,y = upperLeft y += self.Padding x += self.Padding lowerRight = (x,y) # Draw each subslot, separated by some padding for slot in self.subslots: slot.drawAt(canvas, (x,y)) lowerRight = (x + slot.size()[0], y + slot.size()[1]) y = lowerRight[1] # No padding between level-0 subslots if self.level > 1: y += self.Padding if len(self.subslots) == 0: lowerRight = (x + 2*SvgSlot.Radius + self.Padding, y + self.Padding ) else: lowerRight = (lowerRight[0] + self.Padding, y + self.Padding ) # Draw our outer rectangle width = lowerRight[0] - upperLeft[0] height = lowerRight[1] - upperLeft[1] canvas += svg.rect(x=upperLeft[0], y=upperLeft[1], width=width, height=height, stroke='black', stroke_width=1, style='fill-opacity:0', id_=self.key() ) self._code = canvas return lowerRight
def print(self): for r in range(self.height): for c in range(self.width): props = { "fill": "red" if self.data[r][c] else "white", "opacity": 1, "stroke": "none", } print( svg.rect( c * self.pixel, r * self.pixel, self.pixel, self.pixel, style=props, ))
def _generate_code(self): """ Generate the text for drawing this multislot at (0,0) """ canvas = svg.IndentingStringIO("") upperLeft = (0, 0) x, y = upperLeft y += self.Padding x += self.Padding lowerRight = (x, y) # Draw each subslot, separated by some padding for slot in self.subslots: slot.drawAt(canvas, (x, y)) lowerRight = (x + slot.size()[0], y + slot.size()[1]) y = lowerRight[1] # No padding between level-0 subslots if self.level > 1: y += self.Padding if len(self.subslots) == 0: lowerRight = (x + 2 * SvgSlot.Radius + self.Padding, y + self.Padding) else: lowerRight = (lowerRight[0] + self.Padding, y + self.Padding) # Draw our outer rectangle width = lowerRight[0] - upperLeft[0] height = lowerRight[1] - upperLeft[1] canvas += svg.rect(x=upperLeft[0], y=upperLeft[1], width=width, height=height, stroke='black', stroke_width=1, style='fill-opacity:0', id_=self.key()) self._code = canvas return lowerRight
def plotXYSVG(drawSpace, dataX, dataY, rank=0, dataLabel=[], plotColor = "black", axesColor="black", labelColor="black", symbolColor="red", XLabel=None, YLabel=None, title=None, fitcurve=None, connectdot=1, displayR=None, loadingPlot = 0, offset= (80, 20, 40, 60), zoom = 1, specialCases=[], showLabel = 1): 'displayR : correlation scatter plot, loadings : loading plot' dataXRanked, dataYRanked = webqtlUtil.calRank(dataX, dataY, len(dataX)) # Switching Ranked and Unranked X and Y values if a Spearman Rank Correlation if rank == 0: dataXPrimary = dataX dataYPrimary = dataY dataXAlt = dataXRanked dataYAlt = dataYRanked else: dataXPrimary = dataXRanked dataYPrimary = dataYRanked dataXAlt = dataX dataYAlt = dataY xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset plotWidth = drawSpace.attributes['width'] - xLeftOffset - xRightOffset plotHeight = drawSpace.attributes['height'] - yTopOffset - yBottomOffset if plotHeight<=0 or plotWidth<=0: return if len(dataXPrimary) < 1 or len(dataXPrimary) != len(dataYPrimary) or (dataLabel and len(dataXPrimary) != len(dataLabel)): return max_X=max(dataXPrimary) min_X=min(dataXPrimary) max_Y=max(dataYPrimary) min_Y=min(dataYPrimary) #for some reason I forgot why I need to do this if loadingPlot: min_X = min(-0.1,min_X) max_X = max(0.1,max_X) min_Y = min(-0.1,min_Y) max_Y = max(0.1,max_Y) xLow, xTop, stepX=detScale(min_X,max_X) yLow, yTop, stepY=detScale(min_Y,max_Y) xScale = plotWidth/(xTop-xLow) yScale = plotHeight/(yTop-yLow) #draw drawing region r = svg.rect(xLeftOffset, yTopOffset, plotWidth, plotHeight, 'none', axesColor, 1) drawSpace.addElement(r) #calculate data points data = map(lambda X, Y: (X, Y), dataXPrimary, dataYPrimary) xCoord = map(lambda X, Y: ((X-xLow)*xScale + xLeftOffset, yTopOffset+plotHeight-(Y-yLow)*yScale), dataXPrimary, dataYPrimary) labelFontF = "verdana" labelFontS = 11 if loadingPlot: xZero = -xLow*xScale+xLeftOffset yZero = yTopOffset+plotHeight+yLow*yScale for point in xCoord: drawSpace.addElement(svg.line(xZero,yZero,point[0],point[1], "red", 1)) else: if connectdot: pass #drawSpace.drawPolygon(xCoord,edgeColor=plotColor,closed=0) else: pass for i, item in enumerate(xCoord): if dataLabel and dataLabel[i] in specialCases: drawSpace.addElement(svg.rect(item[0]-3, item[1]-3, 6, 6, "none", "green", 0.5)) #drawSpace.drawCross(item[0],item[1],color=pid.blue,size=5) else: drawSpace.addElement(svg.line(item[0],item[1]+5,item[0],item[1]-5,symbolColor,1)) drawSpace.addElement(svg.line(item[0]+5,item[1],item[0]-5,item[1],symbolColor,1)) if showLabel and dataLabel: pass drawSpace.addElement(svg.text(item[0], item[1]+14, dataLabel[i], labelFontS, labelFontF, text_anchor="middle", style="stroke:blue;stroke-width:0.5;")) #canvas.drawString(, item[0]- canvas.stringWidth(dataLabel[i], # font=labelFont)/2, item[1]+14, font=labelFont, color=pid.blue) #draw scale #scaleFont=pid.Font(ttf="cour",size=14,bold=1) x=xLow for i in range(stepX+1): xc=xLeftOffset+(x-xLow)*xScale drawSpace.addElement(svg.line(xc,yTopOffset+plotHeight,xc,yTopOffset+plotHeight+5, axesColor, 1)) strX = cformat(d=x, rank=rank) drawSpace.addElement(svg.text(xc,yTopOffset+plotHeight+20,strX,13, "courier", text_anchor="middle")) x+= (xTop - xLow)/stepX y=yLow for i in range(stepY+1): yc=yTopOffset+plotHeight-(y-yLow)*yScale drawSpace.addElement(svg.line(xLeftOffset,yc,xLeftOffset-5,yc, axesColor, 1)) strY = cformat(d=y, rank=rank) drawSpace.addElement(svg.text(xLeftOffset-10,yc+5,strY,13, "courier", text_anchor="end")) y+= (yTop - yLow)/stepY #draw label labelFontF = "verdana" labelFontS = 17 if XLabel: drawSpace.addElement(svg.text(xLeftOffset+plotWidth/2.0, yTopOffset+plotHeight+yBottomOffset-10,XLabel, labelFontS, labelFontF, text_anchor="middle")) if YLabel: drawSpace.addElement(svg.text(xLeftOffset-50, yTopOffset+plotHeight/2,YLabel, labelFontS, labelFontF, text_anchor="middle", style="writing-mode:tb-rl", transform="rotate(270 %d %d)" % (xLeftOffset-50, yTopOffset+plotHeight/2))) #drawSpace.drawString(YLabel, xLeftOffset-50, yTopOffset+plotHeight- (plotHeight-drawSpace.stringWidth(YLabel,font=labelFont))/2.0, # font=labelFont,color=labelColor,angle=90) if fitcurve: sys.argv = [ "mod_python" ] #from numarray import linear_algebra as la #from numarray import ones, array, dot, swapaxes fitYY = array(dataYPrimary) fitXX = array([ones(len(dataXPrimary)),dataXPrimary]) AA = dot(fitXX,swapaxes(fitXX,0,1)) BB = dot(fitXX,fitYY) bb = la.linear_least_squares(AA,BB)[0] xc1 = xLeftOffset yc1 = yTopOffset+plotHeight-(bb[0]+bb[1]*xLow-yLow)*yScale if yc1 > yTopOffset+plotHeight: yc1 = yTopOffset+plotHeight xc1 = (yLow-bb[0])/bb[1] xc1=(xc1-xLow)*xScale+xLeftOffset elif yc1 < yTopOffset: yc1 = yTopOffset xc1 = (yTop-bb[0])/bb[1] xc1=(xc1-xLow)*xScale+xLeftOffset else: pass xc2 = xLeftOffset + plotWidth yc2 = yTopOffset+plotHeight-(bb[0]+bb[1]*xTop-yLow)*yScale if yc2 > yTopOffset+plotHeight: yc2 = yTopOffset+plotHeight xc2 = (yLow-bb[0])/bb[1] xc2=(xc2-xLow)*xScale+xLeftOffset elif yc2 < yTopOffset: yc2 = yTopOffset xc2 = (yTop-bb[0])/bb[1] xc2=(xc2-xLow)*xScale+xLeftOffset else: pass drawSpace.addElement(svg.line(xc1,yc1,xc2,yc2,"green", 1)) if displayR: labelFontF = "trebuc" labelFontS = 14 NNN = len(dataX) corr = webqtlUtil.calCorrelation(dataXPrimary,dataYPrimary,NNN)[0] if NNN < 3: corrPValue = 1.0 else: if abs(corr) >= 1.0: corrPValue = 0.0 else: ZValue = 0.5*log((1.0+corr)/(1.0-corr)) ZValue = ZValue*sqrt(NNN-3) corrPValue = 2.0*(1.0 - reaper.normp(abs(ZValue))) NStr = "N of Cases=%d" % NNN if rank == 1: corrStr = "Spearman's r=%1.3f P=%3.2E" % (corr, corrPValue) else: corrStr = "Pearson's r=%1.3f P=%3.2E" % (corr, corrPValue) drawSpace.addElement(svg.text(xLeftOffset,yTopOffset-10,NStr, labelFontS, labelFontF, text_anchor="start")) drawSpace.addElement(svg.text(xLeftOffset+plotWidth,yTopOffset-25,corrStr, labelFontS, labelFontF, text_anchor="end")) """ """ return
def makebarsvert(magnitudebenefit,saliencebenefit,magnitudecost,saliencecost): if c.swapsaliencemagnitude: tm = saliencebenefit[:] saliencebenefit = magnitudebenefit[:] magnitudebenefit = tm tm = saliencecost[:] saliencecost = magnitudecost[:] magnitudecost = tm #general variables t = 0 if c.centerlabel is not None: t = 50 out = svg.svg(width = 1000, height = 2250+t, viewbox='0 0 1000 2150') barwidth = 2050/(21+c.spacebars*2) chart = svg.g('transform="translate(0 %s)"' % t) out.add(chart) for cb in range(0,2): if c.centerlabel is not None: out.add(svg.text(c.centerlabel[1-cb], 250+(cb)*500, 25, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) if(cb==1): colors=c.colorsbenefit magnitude=magnitudebenefit salience=saliencebenefit else: magnitude=magnitudecost salience=saliencecost colors=c.colorscost #draw bars for i in range(0,21): h = barheight(magnitude[i]+c.includezeromagnitude) if(c.saliencebywidth==1): w = barwidth/(3+c.includezerosalience)*(salience[i]+c.includezerosalience) elif(c.saliencebywidth>1): w = barwidth/(c.saliencebywidth**3)*(c.saliencebywidth**salience[i]) else: w = barwidth if(c.saliencebycolor): s = ((salience[i]+c.includezerosalience)**c.saliencebycolor)/((3.0+c.includezerosalience)**c.saliencebycolor) else: s = 1 red,green,blue = colors[i//7] if(salience[i]>0 or c.includezerosalience): barlinecolor=(int(red*c.barlineshade),int(green*c.barlineshade),int(blue*c.barlineshade)) chart.add(svg.rect(500+(cb-1)*h,(i+(i//7)*c.spacebars)*barwidth+(barwidth-w)/2,h,w, fill='rgb(%s,%s,%s)' % (red,green,blue), stroke='rgb(%s,%s,%s)' % (barlinecolor[0],barlinecolor[1],barlinecolor[2]), strokewidth=c.barline, p='fill-opacity="%s"' % s)) #invisible link and tooltip box newrect = svg.rect(500+(cb-1)*c.textradius,(i+(i//7)*c.spacebars)*barwidth,c.textradius,barwidth, fill='white', p='fill-opacity="0.0"') newrect.add(svg.title(' %s: %s\n Magnitude %s\tSalience %s' % (c.indicatornames[i], c.indicatorfullnames[i], magnitude[i], salience[i]))) chart.add(newrect) #draw ygrid for i in range(0,c.maxvalue+c.includezeromagnitude+1): if(i==c.includezeromagnitude): w=5 else: w=1 if (c.ygrid or i==0): x2 = 0 else: x2 = 2050-c.ticksize chart.add(svg.line(500+barheight(i)*(cb-.5)*2,2050+c.ticksize,500+barheight(i)*(cb-.5)*2,x2,'black',w)) #draw xgrid dividers chart.add(svg.line(500-c.edgeradius,2050,500+c.edgeradius,2050,'black',5)) for i in range(0,21+c.spacebars*2): if (c.xgrid or (c.saliencebywidth and c.drawsaliencedividers)): chart.add(svg.line(500-c.edgeradius,barwidth*i,500+c.edgeradius,barwidth*i, 'black', 1)) elif(ticksize): chart.add(svg.line(500-c.ticksize,barwidth*i,500+c.ticksize,barwidth*i, 'black', 1)) #draw salience dividers and column headings for i in range(0,21): if(c.saliencebywidth and c.drawsaliencedividers): for j in range(1-c.includezerosalience,3): if(c.saliencebywidth>1): w = barwidth/2/(c.saliencebywidth**3)*(c.saliencebywidth**j) else: w = barwidth/2/(3+c.includezerosalience)*(j+c.includezerosalience) for k in [1,-1]: chart.add(svg.line(500-c.edgeradius,barwidth*(i+(i//7)+.5)+k*w,500+c.edgeradius,barwidth*(i+(i//7)+.5)+k*w, 'black', .5)) x = (500 - c.textradius) y = ((i+(i//7)*c.spacebars+.5)*barwidth) chart.add(svg.text(c.indicatornames[i], x, y, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) #draw magnitude legend if c.magnitudelegend: if c.swapsaliencemagnitude: te = 'Salience' else: te = 'Magnitude' mag=svg.g(p='transform="translate(0 2050)"') mag.add(svg.text(te, 500, 100, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) lastnumber=1 for i in range(0,c.maxvalue+c.includezeromagnitude+1): if (i-c.includezeromagnitude==0): mag.add(svg.text(i-c.includezeromagnitude,502-barheight(i),40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) if (barheight(i)*2>c.font): mag.add(svg.text(i-c.includezeromagnitude,502+barheight(i), 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) elif (i-c.includezeromagnitude>0): # alternate top and bottom if axis lables too close mag.add(svg.text(i-c.includezeromagnitude, 502+barheight(i)*lastnumber,40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) if (barheight(i)-barheight(i-1)>c.font): mag.add(svg.text(i-c.includezeromagnitude, 502-barheight(i)*lastnumber,40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) else: lastnumber = lastnumber * -1 chart.add(mag) return out
def _generate_code(self): upperLeft = (0,0) canvas = svg.IndentingStringIO("") x, y = upperLeft title_text = self.op.name logger.debug( "generating code for operator: {}".format( title_text ) ) inputSize = self.getInputSize() outputSize = self.getOutputSize() child_ordering = {} for child in self.op.children: logger.debug( "child with name: {}".format( child.name ) ) col = get_column_within_parent(child) if col not in child_ordering: child_ordering[col] = [] child_ordering[col].append(child) r = self.TitleHeight - 2 rect_x = upperLeft[0] + inputSize[0] rect_y = upperLeft[1] child_x = rect_x + r + self.PaddingForSlotName + self.PaddingBetweenInternalOps child_y = rect_y + 2*r max_child_y = child_y max_child_x = child_x if len(self.op.children) > 0: if self.max_child_depth == 0: title_text += '*' # Asterisk in the title indicates that this operator has children that are not shown else: svgChildren = {} columnHeights = [] for col_index, col_children in sorted( child_ordering.items() ): columnHeights.append(0) svgChildren[col_index] = [] for child in col_children: svgChild = SvgOperator(child, self.max_child_depth-1) columnHeights[col_index] += svgChild.size()[1] svgChildren[col_index].append(svgChild) maxColumnHeight = max(columnHeights) columnExtraPadding = [] for col_index, col_svg_children in sorted( svgChildren.items() ): columnExtraPadding.append( maxColumnHeight ) for svgChild in col_svg_children: columnExtraPadding[col_index] -= svgChild.size()[1] for col_index, col_children in sorted( svgChildren.items() ): for svgChild in col_children: child_y += (self.PaddingBetweenInternalOps + columnExtraPadding[col_index]/len(col_children)) / 2 svgChild.drawAt( canvas, (child_x, child_y) ) lowerRight = (svgChild.size()[0] + child_x, svgChild.size()[1] + child_y) max_child_x = max(lowerRight[0], max_child_x) child_y = lowerRight[1] + self.PaddingBetweenInternalOps + columnExtraPadding[col_index]/len(col_children) max_child_x += self.PaddingBetweenInternalOps max_child_y = max(max_child_y, child_y) child_x = max_child_x child_y = rect_y + 2*r max_child_x += self.PaddingForSlotName + self.PaddingBetweenInternalOps def max_slot_name_length(slots): m = 0 for slot in slots: m = max(m, len(slot.name)) return m max_input_name = max_slot_name_length(self.op.inputs.values()) max_output_name = max_slot_name_length(self.op.outputs.values()) rect_width = max_child_x - rect_x rect_width = max( rect_width, 2*self.PaddingBetweenInternalOps ) rect_width = max( rect_width, 9*len(self.op.name) ) # Correct width depends on font... rect_width = max( rect_width, 9*(max_input_name + max_output_name) ) rect_width += r rect_height = max_child_y - rect_y rect_height = max(rect_height, outputSize[1]) rect_height = max(rect_height, inputSize[1]) rect_height += 2*r rect_height = max( rect_height, 50 ) # Draw outer rectangle canvas += svg.rect(x=rect_x, y=rect_y, width=rect_width, height=rect_height, rx=r, ry=r, inkscape__connector_avoid="false", stroke='black', stroke_width=2, style='fill-opacity:0' ) path_d = 'M {startx} {y} L {endx} {y} Z'.format(startx=rect_x, y=rect_y+r+2, endx=rect_x+rect_width) canvas += svg.path(d=path_d, stroke='black', stroke_width=1) block = partial(svg.tagblock, canvas) with block(svg.text, x=rect_x+rect_width/2, y=rect_y+r, text_anchor='middle'): canvas += title_text + '\n' if self.op.debug_text: block = partial(svg.tagblock, canvas) with block(svg.text, x=rect_x+rect_width/2, y=rect_y+2*r, text_anchor='middle'): canvas += self.op.debug_text + '\n' # Add extra padding between input slots if there's room (i.e. spread out the inputs to cover the entire left side) inputSlotPadding = (rect_height - self.getInputSize()[1]) / (len(self.inputs)+1) # Draw inputs y += 1.5*self.TitleHeight + inputSlotPadding for slot in self.inputs.values(): size = slot.size() slot_x, slot_y = (x+(inputSize[0]-size[0]),y) slot.drawAt( canvas, (slot_x, slot_y) ) y += size[1] + inputSlotPadding text_x, text_y = (slot_x + size[0] + 5, slot_y + size[1]/2) with block(svg.text, x=text_x, y=text_y, text_anchor='start'): canvas += slot.name + '\n' # Add extra padding between output slots if there's room (i.e. spread out the inputs to cover the entire right side) outputSlotPadding = (rect_height - self.getOutputSize()[1]) / (len(self.outputs)+1) # Draw outputs x, y = upperLeft x += rect_width + inputSize[0] y += 1.5*self.TitleHeight + outputSlotPadding for slot in self.outputs.values(): size = slot.size() text_x, text_y = (x - 5, y + size[1]/2) slot.drawAt( canvas, (x,y) ) y += size[1] + outputSlotPadding with block(svg.text, x=text_x, y=text_y, text_anchor='end'): canvas += slot.name + '\n' lowerRight_x = upperLeft[0] + rect_width + inputSize[0] + outputSize[0] lowerRight_y = upperLeft[1] + rect_height self._code = canvas return (lowerRight_x, lowerRight_y)
def makesaliencelegend(): out = svg.svg(width=2000, height=100, viewbox='0 0 2000 100') if c.swapsaliencemagnitude: te = 'Magnitude:' else: te = 'Salience:' out.add(svg.text(te, 70, 50, fontsize=c.font)) for i in range(0, 2): if i == 0: red, green, blue = c.colorsbenefit[0] elif c.colorsbenefit[0] == c.colorscost[0]: return out else: red, green, blue = c.colorscost[0] for j in range(0, 3 + c.includezerosalience): out.add( svg.text(j + (1 - c.includezerosalience), (i * 950 + 290 + j * (200 - 60 * c.includezerosalience)), 50, fontsize=c.font)) if (c.saliencebycolor): s = ((j + 1)**c.saliencebycolor) / ( (3.0 + c.includezerosalience)**c.saliencebycolor) else: s = 1 if (c.saliencebywidth): out.add( svg.rect((i * 950 + 320 + j * (200 - 60 * c.includezerosalience)), 11, 80, 50, stroke='black', fill='none')) if (c.saliencebywidth > 1): w = 80.0 / (c.saliencebywidth**3) * (c.saliencebywidth**( j + 1 - c.includezerosalience)) else: w = 80.0 / (3 + c.includezerosalience) * (j + 1) out.add( svg.rect((i * 950 + 360 + j * (200 - 60 * c.includezerosalience) - (w / 2)), 11, w, 50, stroke='none', fill='rgb(%s,%s,%s)' % (red, green, blue), p='fill-opacity="%s"' % s)) else: out.add( svg.rect((i * 950 + 320 + j * (200 - 60 * c.includezerosalience)), 11, 80, 50, stroke='none', fill='rgb(%s,%s,%s)' % (red, green, blue), p='fill-opacity="%s"' % s)) return out
def makebarsvert(magnitudebenefit, saliencebenefit, magnitudecost, saliencecost): if c.swapsaliencemagnitude: tm = saliencebenefit[:] saliencebenefit = magnitudebenefit[:] magnitudebenefit = tm tm = saliencecost[:] saliencecost = magnitudecost[:] magnitudecost = tm #general variables t = 0 if c.centerlabel is not None: t = 50 out = svg.svg(width=1000, height=2250 + t, viewbox='0 0 1000 2150') barwidth = 2050 / (21 + c.spacebars * 2) chart = svg.g('transform="translate(0 %s)"' % t) out.add(chart) for cb in range(0, 2): if c.centerlabel is not None: out.add( svg.text( c.centerlabel[1 - cb], 250 + (cb) * 500, 25, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"') ) if (cb == 1): colors = c.colorsbenefit magnitude = magnitudebenefit salience = saliencebenefit else: magnitude = magnitudecost salience = saliencecost colors = c.colorscost #draw bars for i in range(0, 21): h = barheight(magnitude[i] + c.includezeromagnitude) if (c.saliencebywidth == 1): w = barwidth / (3 + c.includezerosalience) * ( salience[i] + c.includezerosalience) elif (c.saliencebywidth > 1): w = barwidth / (c.saliencebywidth**3) * (c.saliencebywidth** salience[i]) else: w = barwidth if (c.saliencebycolor): s = ((salience[i] + c.includezerosalience)** c.saliencebycolor) / ( (3.0 + c.includezerosalience)**c.saliencebycolor) else: s = 1 red, green, blue = colors[i // 7] if (salience[i] > 0 or c.includezerosalience): barlinecolor = (int(red * c.barlineshade), int(green * c.barlineshade), int(blue * c.barlineshade)) chart.add( svg.rect( 500 + (cb - 1) * h, (i + (i // 7) * c.spacebars) * barwidth + (barwidth - w) / 2, h, w, fill='rgb(%s,%s,%s)' % (red, green, blue), stroke='rgb(%s,%s,%s)' % (barlinecolor[0], barlinecolor[1], barlinecolor[2]), strokewidth=c.barline, p='fill-opacity="%s"' % s)) #invisible link and tooltip box newrect = svg.rect(500 + (cb - 1) * c.textradius, (i + (i // 7) * c.spacebars) * barwidth, c.textradius, barwidth, fill='white', p='fill-opacity="0.0"') newrect.add( svg.title(' %s: %s\n Magnitude %s\tSalience %s' % (c.indicatornames[i], c.indicatorfullnames[i], magnitude[i], salience[i]))) chart.add(newrect) #draw ygrid for i in range(0, c.maxvalue + c.includezeromagnitude + 1): if (i == c.includezeromagnitude): w = 5 else: w = 1 if (c.ygrid or i == 0): x2 = 0 else: x2 = 2050 - c.ticksize chart.add( svg.line(500 + barheight(i) * (cb - .5) * 2, 2050 + c.ticksize, 500 + barheight(i) * (cb - .5) * 2, x2, 'black', w)) #draw xgrid dividers chart.add( svg.line(500 - c.edgeradius, 2050, 500 + c.edgeradius, 2050, 'black', 5)) for i in range(0, 21 + c.spacebars * 2): if (c.xgrid or (c.saliencebywidth and c.drawsaliencedividers)): chart.add( svg.line(500 - c.edgeradius, barwidth * i, 500 + c.edgeradius, barwidth * i, 'black', 1)) elif (ticksize): chart.add( svg.line(500 - c.ticksize, barwidth * i, 500 + c.ticksize, barwidth * i, 'black', 1)) #draw salience dividers and column headings for i in range(0, 21): if (c.saliencebywidth and c.drawsaliencedividers): for j in range(1 - c.includezerosalience, 3): if (c.saliencebywidth > 1): w = barwidth / 2 / (c.saliencebywidth** 3) * (c.saliencebywidth**j) else: w = barwidth / 2 / (3 + c.includezerosalience) * ( j + c.includezerosalience) for k in [1, -1]: chart.add( svg.line(500 - c.edgeradius, barwidth * (i + (i // 7) + .5) + k * w, 500 + c.edgeradius, barwidth * (i + (i // 7) + .5) + k * w, 'black', .5)) x = (500 - c.textradius) y = ((i + (i // 7) * c.spacebars + .5) * barwidth) chart.add( svg.text( c.indicatornames[i], x, y, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) #draw magnitude legend if c.magnitudelegend: if c.swapsaliencemagnitude: te = 'Salience' else: te = 'Magnitude' mag = svg.g(p='transform="translate(0 2050)"') mag.add( svg.text( te, 500, 100, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"')) lastnumber = 1 for i in range(0, c.maxvalue + c.includezeromagnitude + 1): if (i - c.includezeromagnitude == 0): mag.add( svg.text( i - c.includezeromagnitude, 502 - barheight(i), 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) if (barheight(i) * 2 > c.font): mag.add( svg.text( i - c.includezeromagnitude, 502 + barheight(i), 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) elif (i - c.includezeromagnitude > 0): # alternate top and bottom if axis lables too close mag.add( svg.text( i - c.includezeromagnitude, 502 + barheight(i) * lastnumber, 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) if (barheight(i) - barheight(i - 1) > c.font): mag.add( svg.text( i - c.includezeromagnitude, 502 - barheight(i) * lastnumber, 40, fontsize=c.font, p='style="text-anchor: middle; dominant-baseline: middle"' )) else: lastnumber = lastnumber * -1 chart.add(mag) return out
def _generate_code(self): upperLeft = (0, 0) canvas = svg.IndentingStringIO("") x, y = upperLeft title_text = self.op.name logger.debug("generating code for operator: {}".format(title_text)) inputSize = self.getInputSize() outputSize = self.getOutputSize() child_ordering = {} for child in self.op.children: logger.debug("child with name: {}".format(child.name)) col = get_column_within_parent(child) if col not in child_ordering: child_ordering[col] = [] child_ordering[col].append(child) r = self.TitleHeight - 2 rect_x = upperLeft[0] + inputSize[0] rect_y = upperLeft[1] child_x = rect_x + r + self.PaddingForSlotName + self.PaddingBetweenInternalOps child_y = rect_y + 2 * r max_child_y = child_y max_child_x = child_x if len(self.op.children) > 0: if self.max_child_depth == 0: title_text += '*' # Asterisk in the title indicates that this operator has children that are not shown else: svgChildren = {} columnHeights = [] for col_index, col_children in sorted(child_ordering.items()): columnHeights.append(0) svgChildren[col_index] = [] for child in col_children: svgChild = SvgOperator(child, self.max_child_depth - 1) columnHeights[col_index] += svgChild.size()[1] svgChildren[col_index].append(svgChild) maxColumnHeight = max(columnHeights) columnExtraPadding = [] for col_index, col_svg_children in sorted(svgChildren.items()): columnExtraPadding.append(maxColumnHeight) for svgChild in col_svg_children: columnExtraPadding[col_index] -= svgChild.size()[1] for col_index, col_children in sorted(svgChildren.items()): for svgChild in col_children: child_y += (self.PaddingBetweenInternalOps + columnExtraPadding[col_index] / len(col_children)) / 2 svgChild.drawAt(canvas, (child_x, child_y)) lowerRight = (svgChild.size()[0] + child_x, svgChild.size()[1] + child_y) max_child_x = max(lowerRight[0], max_child_x) child_y = lowerRight[ 1] + self.PaddingBetweenInternalOps + columnExtraPadding[ col_index] / len(col_children) max_child_x += self.PaddingBetweenInternalOps max_child_y = max(max_child_y, child_y) child_x = max_child_x child_y = rect_y + 2 * r max_child_x += self.PaddingForSlotName + self.PaddingBetweenInternalOps def max_slot_name_length(slots): m = 0 for slot in slots: m = max(m, len(slot.name)) return m max_input_name = max_slot_name_length(self.op.inputs.values()) max_output_name = max_slot_name_length(self.op.outputs.values()) rect_width = max_child_x - rect_x rect_width = max(rect_width, 2 * self.PaddingBetweenInternalOps) rect_width = max(rect_width, 9 * len(self.op.name)) # Correct width depends on font... rect_width = max(rect_width, 9 * (max_input_name + max_output_name)) rect_width += r rect_height = max_child_y - rect_y rect_height = max(rect_height, outputSize[1]) rect_height = max(rect_height, inputSize[1]) rect_height += 2 * r rect_height = max(rect_height, 50) # Draw outer rectangle canvas += svg.rect(x=rect_x, y=rect_y, width=rect_width, height=rect_height, rx=r, ry=r, inkscape__connector_avoid="false", stroke='black', stroke_width=2, style='fill-opacity:0') path_d = 'M {startx} {y} L {endx} {y} Z'.format(startx=rect_x, y=rect_y + r + 2, endx=rect_x + rect_width) canvas += svg.path(d=path_d, stroke='black', stroke_width=1) block = partial(svg.tagblock, canvas) with block(svg.text, x=rect_x + rect_width / 2, y=rect_y + r, text_anchor='middle'): canvas += title_text + '\n' if self.op.debug_text: block = partial(svg.tagblock, canvas) with block(svg.text, x=rect_x + rect_width / 2, y=rect_y + 2 * r, text_anchor='middle'): canvas += self.op.debug_text + '\n' # Add extra padding between input slots if there's room (i.e. spread out the inputs to cover the entire left side) inputSlotPadding = (rect_height - self.getInputSize()[1]) / (len(self.inputs) + 1) # Draw inputs y += 1.5 * self.TitleHeight + inputSlotPadding for slot in self.inputs.values(): size = slot.size() slot_x, slot_y = (x + (inputSize[0] - size[0]), y) slot.drawAt(canvas, (slot_x, slot_y)) y += size[1] + inputSlotPadding text_x, text_y = (slot_x + size[0] + 5, slot_y + size[1] / 2) with block(svg.text, x=text_x, y=text_y, text_anchor='start'): canvas += slot.name + '\n' # Add extra padding between output slots if there's room (i.e. spread out the inputs to cover the entire right side) outputSlotPadding = (rect_height - self.getOutputSize()[1]) / (len(self.outputs) + 1) # Draw outputs x, y = upperLeft x += rect_width + inputSize[0] y += 1.5 * self.TitleHeight + outputSlotPadding for slot in self.outputs.values(): size = slot.size() text_x, text_y = (x - 5, y + size[1] / 2) slot.drawAt(canvas, (x, y)) y += size[1] + outputSlotPadding with block(svg.text, x=text_x, y=text_y, text_anchor='end'): canvas += slot.name + '\n' lowerRight_x = upperLeft[0] + rect_width + inputSize[0] + outputSize[0] lowerRight_y = upperLeft[1] + rect_height self._code = canvas return (lowerRight_x, lowerRight_y)