コード例 #1
0
def overlay(title, results, inference_time, inference_rate, layout):
    x0, y0, width, height = layout.window
    font_size = 0.03 * height

    defs = svg.Defs()
    defs += CSS_STYLES

    doc = svg.Svg(width=width,
                  height=height,
                  viewBox='%s %s %s %s' % layout.window,
                  font_size=font_size,
                  font_family='monospace',
                  font_weight=500)
    doc += defs

    ox1, ox2 = x0 + 20, x0 + width - 20
    oy1, oy2 = y0 + 20 + font_size, y0 + height - 20

    # Classes
    lines = ['%s (%.2f)' % pair for pair in results]
    for i, line in enumerate(lines):
        y = oy2 - i * 1.7 * font_size

        doc += svg.Rect(x=0,
                        y=0,
                        width=size_em(len(line)),
                        height='1em',
                        transform='translate(%s, %s) scale(-1,-1)' % (ox2, y),
                        _class='back')

        doc += svg.Text(line, text_anchor='end', x=ox2, y=y, fill='white')

    # Title
    if title:
        doc += svg.Rect(x=0,
                        y=0,
                        width=size_em(len(title)),
                        height='1em',
                        transform='translate(%s, %s) scale(1,-1)' % (ox1, oy1),
                        _class='back')
        doc += svg.Text(title, x=ox1, y=oy1, fill='white')

    # Info
    lines = [
        'Inference time: %.2f ms (%.2f fps)' %
        (inference_time * 1000, 1.0 / inference_time)
    ]

    for i, line in enumerate(reversed(lines)):
        y = oy2 - i * 1.7 * font_size
        doc += svg.Rect(x=0,
                        y=0,
                        width=size_em(len(line)),
                        height='1em',
                        transform='translate(%s, %s) scale(1,-1)' % (ox1, y),
                        _class='back')
        doc += svg.Text(line, x=ox1, y=y, fill='white')

    return str(doc)
コード例 #2
0
ファイル: reactor.py プロジェクト: shawn282/bio-pathfinder
    def reaction2svg(self, G_old, G_new, rid):
        font_size = 10
        scene = svg.Scene(800, 300)
        scene.add(G_old.svg(svg.Scene(300, 300)))
        scene.add(svg.Text((30, font_size), graph2compound(G_old, self.ignore_chirality), font_size, fill_color=magenta))
        scene.add(svg.Text((325, 120), self.reaction_templates[rid]['NAME'], font_size=font_size, fill_color=red))
        scene.add(svg.ChemicalArrow((380, 150), (420, 150), stroke_width=2))
        scene.add(G_new.svg(svg.Scene(300, 300)), (500, 0))
        scene.add(svg.Text((530, font_size), graph2compound(G_new, self.ignore_chirality), font_size, fill_color=magenta))
#        scene.justify()
        scene.border(True)
        return scene
コード例 #3
0
def overlay(title, objs, get_color, inference_time, inference_rate, layout):
    x0, y0, width, height = layout.window
    font_size = 0.03 * height

    defs = svg.Defs()
    defs += CSS_STYLES

    doc = svg.Svg(width=width, height=height,
                  viewBox='%s %s %s %s' % layout.window,
                  font_size=font_size, font_family='monospace', font_weight=500)
    doc += defs

    for obj in objs:
        percent = int(100 * obj.score)
        if obj.label:
            caption = '%d%% %s' % (percent, obj.label)
        else:
            caption = '%d%%' % percent

        x, y, w, h = obj.bbox.scale(*layout.size)
        color = get_color(obj.id)

        doc += svg.Rect(x=x, y=y, width=w, height=h,
                        style='stroke:%s' % color, _class='bbox')
        doc += svg.Rect(x=x, y=y+h ,
                        width=size_em(len(caption)), height='1.2em', fill=color)
        t = svg.Text(x=x, y=y+h, fill='black')
        t += svg.TSpan(caption, dy='1em')
        doc += t

    ox = x0 + 20
    oy1, oy2 = y0 + 20 + font_size, y0 + height - 20

    # Title
    if title:
        doc += svg.Rect(x=0, y=0, width=size_em(len(title)), height='1em',
                        transform='translate(%s, %s) scale(1,-1)' % (ox, oy1), _class='back')
        doc += svg.Text(title, x=ox, y=oy1, fill='white')

    # Info
    lines = [
        'Objects: %d' % len(objs),
        'Inference time: %.2f ms (%.2f fps)' % (inference_time * 1000, 1.0 / inference_time)
    ]

    for i, line in enumerate(reversed(lines)):
        y = oy2 - i * 1.7 * font_size
        doc += svg.Rect(x=0, y=0, width=size_em(len(line)), height='1em',
                       transform='translate(%s, %s) scale(1,-1)' % (ox, y), _class='back')
        doc += svg.Text(line, x=ox, y=y, fill='white')

    return str(doc)
コード例 #4
0
    def draw_secondary_structure(self):
        # squares (r1, c1)-(r2,c2) => (a, b)-(c, d)
        for (a, b, c, d, width, height, txt, color, avg, dummy,
             dummy) in self.squares:
            origin = self.coords(a * SQR_SIDE + 1, (d + 1) * SQR_SIDE - 1)
            self.scene.add(
                svg.Rectangle(origin, (height * SQR_SIDE) - 2,
                              (width * SQR_SIDE) - 2,
                              stroke_color=color,
                              stroke_width=2), 5)

            origin = self.coords(a * SQR_SIDE + 2, d * SQR_SIDE)
            self.scene.add(svg.Text(origin, str(txt), size=8), 5)

            origin = self.coords(a * SQR_SIDE + 2, b * SQR_SIDE + 3)
            self.scene.add(svg.Text(origin, "%.2f" % avg, size=8), 5)
コード例 #5
0
    def draw_matrix(self):
        self.coords_set(self.matrix_start)

        # draw matrix and write sequence
        for r in X_(self.dp.matrix):
            (rname, cname) = self.dp.match.get_res_names(r)

            origin = self.coords(-SQR_SIDE, r * SQR_SIDE)
            self.scene.add(svg.Text(origin, rname))
            #self.scene.add( svg.Text( origin, r ) )

            origin = self.coords(r * SQR_SIDE, -SQR_SIDE)
            self.scene.add(svg.Text(origin, cname))
            #self.scene.add( svg.Text( origin, r ) )

            for c in X_(self.dp.matrix):
                origin = self.coords(c * SQR_SIDE, (r + 1) * SQR_SIDE)
                color = self.palette.get_colors(self.dp.matrix[r, c])
                self.scene.add(
                    svg.Rectangle(origin, SQR_SIDE, SQR_SIDE, color, color, 0))
コード例 #6
0
def render_scene(scene):
    for ped in crowd:
        # axis transform
        o = Point2(ped.x*amp, (ysize-ped.y)*amp) + Point2(offset, offset)
        scene.add(svg.Circle(o, radius = ped_ra, color=ped.c))
    robot_o = Point2(pos_rx*amp, (ysize-pos_ry)*amp) + Point2(offset, offset)
    scene.add(svg.Circle(robot_o, radius = ped_ra, color='black'))
        
    # write text
    text = 'Ped outflow  ' + str(ped_outflow)
    scene.add(svg.Text((400,500), text, 20))
    clear_output(wait=True)
    display(scene)
コード例 #7
0
    def draw_curves(self):
        self.coords_set(self.curve_start)

        x1 = len(self.dp.matrix)

        y_max_inc = max(self.dp.curve_row_mean.max(),
                        self.dp.curve_col_mean.max(),
                        self.dp.curve_local_rmsd.max()) / 10.0
        y_max_inc = ((int(y_max_inc) / 5) + 1) * 5

        start = self.coords(0, 0)

        # Draw the XX axis
        end = self.coords(x1 * SQR_SIDE, 0)
        self.scene.add(svg.Line(start, end))

        # draw both YY axis
        end = self.coords(0, 10 * SQR_SIDE)
        self.scene.add(svg.Line(start, end))

        # draw scales in 5 steps
        for i in xrange(0, 6):
            origin = self.coords(-SQR_SIDE, i * 2 * SQR_SIDE)
            self.scene.add(
                svg.Text(origin, "%2d" % round(y_max_inc * 2 * float(i))), 10)

        # draw horizontal scale
        for r in X_(self.dp.matrix):
            (rname, cname) = self.dp.match.get_res_names(r)
            origin = self.coords(r * SQR_SIDE, -SQR_SIDE)
            self.scene.add(svg.Text(origin, cname))

        # draw curves
        self.draw_curves_aux(self.dp.curve_row_mean, y_max_inc, x1,
                             COLOR_ROW_MEAN)
        self.draw_curves_aux(self.dp.curve_col_mean, y_max_inc, x1,
                             COLOR_COL_MEAN)
        self.draw_curves_aux(self.dp.curve_local_rmsd, y_max_inc, x1,
                             COLOR_LOCAL_RMSD)
コード例 #8
0
    def draw_scale(self):
        self.coords_set(self.scale_start)

        x0 = 0
        x1 = self.palette.steps_down
        x2 = self.palette.steps_down + self.palette.steps_up

        # Color scale
        for i in xrange(x2):
            coords = self.coords(i * SQR_SIDE, 2 * SQR_SIDE)
            self.scene.add(
                svg.Rectangle(coords, SQR_SIDE, SQR_SIDE,
                              self.palette.colors[i]))

        origin = self.coords(x0, 0)
        self.scene.add(svg.Text(origin, "0.0"))

        origin = self.coords(x1 * SQR_SIDE, 0)
        self.scene.add(svg.Text(origin, "%.2f" % self.palette.limit_down))

        origin = self.coords((x2 - 1) * SQR_SIDE, 0)
        self.scene.add(svg.Text(origin, "%.2f" % self.palette.limit_up))
コード例 #9
0
def makeglyph(word, x, y, theta, label=True):
    glyphs = [glyphdict[c] for c in word]
    g = svg.Group(ring(glyphs),
                  transform='translate(%.2f, %.2f) rotate(%.2f)' %
                  (x, y, theta))
    if label:
        g.append(
            svg.Group(svg.Text(word,
                               stroke='none',
                               font_size=9,
                               font_family='sans-serif',
                               fill='black',
                               text_anchor='middle',
                               alignment_baseline='middle'),
                      transform='rotate(%.2f)' % -theta))
    return g
コード例 #10
0
 def __init__(self, id, width, height, title, data):
     self.base_colors = [
         (100, 30, 22),
         (120, 40, 31),
         (81, 46, 95),
         (74, 35, 90),
         (21, 67, 96 ),
         (27, 79, 114),
         (14, 98, 81),
         (11, 83, 69 ),
         (20, 90, 50),
         (125, 102, 8),
         (126, 81, 9 ),
         (126, 81, 9),
         (120, 66, 18),
         (110, 44, 0),
     ]
     self.colors = [
        [
             "#{:02X}{:02X}{:02X}".format(
                 c[0] * (1 - x / 10) + (255 * x / 10),
                 c[1] * (1 - x / 10) + (255 * x / 10),
                 c[2] * (1 - x / 10) + (255 * x / 10)
             )
             for x in range(0, 10, 2)
        ] for c in self.base_colors
     ]
     self.data = data
     self.skin = Skin()
     super(Chart, self).__init__(
         "g",
         id,
         None,
         False,
         [
             svg.Text("{0}-title".format(id), 0, 12, title, self.skin.title),
             svg.Rectangle("{0}-outline".format(id), 0, 0, width, height, self.skin.outline)
         ]
     )
     self.children += self.create_chart()
コード例 #11
0
def test():
    util._mkdir("../results")
    f = open("../results/hash2compound.txt", "w")
    for (h, compound) in map_hash2compound.iteritems():
        f.write("compound = %s\n" % compound)
        f.write("hash = %s\n" % h)
        f.write("$$$$\n")
    f.close()

    import html_writer
    compound_list = sorted(map_compound2graph.keys())

    util._mkdir("../results/svg")
    html_writer = html_writer.HtmlWriter("../results/compounds.html")
    html_writer.write("<h1><center>Known Compounds</h1></center>")
    for i in range(len(compound_list)):
        G = map_compound2graph[compound_list[i].upper()]
        scene = G.svg(svg.Scene(300, 300, 10))
        scene.add(svg.Text((30, 10), compound_list[i], 10, fill_color=magenta))
        html_writer.write_svg(scene, "svg/%s" % G.hash())

    html_writer.display()
コード例 #12
0
def overlay(title, objs, get_color, inference_time, inference_rate, layout):
    x0, y0, width, height = layout.window

    defs = svg.Defs()
    defs += CSS_STYLES

    doc = svg.Svg(width=width,
                  height=height,
                  viewBox='%s %s %s %s' % layout.window,
                  font_size='1em',
                  font_family='monospace',
                  font_weight=500)
    doc += defs

    for obj in objs:
        percent = int(100 * obj.score)
        if obj.label:
            caption = '%d%% %s' % (percent, obj.label)
        else:
            caption = '%d%%' % percent

        x, y, w, h = obj.bbox.scale(*layout.size)
        color = get_color(obj.id)

        doc += svg.Rect(x=x,
                        y=y,
                        width=w,
                        height=h,
                        style='stroke:%s' % color,
                        _class='bbox')
        doc += svg.Rect(x=x,
                        y=y + h,
                        width=size_em(len(caption)),
                        height='1.2em',
                        fill=color)
        #center
        center = ((x + w / 2), (y + h / 2))
        doc += svg.Circle(cx=center[0],
                          cy=center[1],
                          r=5,
                          style='stroke:%s' % color)

        centerPts.append(center)

        for i in range(1, len(centerPts)):
            doc += svg.Line(x1=centerPts[i - 1][0],
                            y1=centerPts[i - 1][1],
                            x2=centerPts[i][0],
                            y2=centerPts[i][1],
                            style='stroke:%s' % color)
            #print(centerPts[i-1][0],centerPts[i-1][1])
        #print(centerPts)

        #corner
        #doc += svg.Circle(cx=x,cy=y,r=10, style='stroke:%s' % color)

        t = svg.Text(x=x, y=y + h, fill='black')
        t += svg.TSpan(caption, dy='1em')
        doc += t

    ox, oy1, oy2 = x0 + 20, y0 + 20, y0 + height - 20

    # Title
    if title:
        doc += svg.Rect(x=ox,
                        y=oy1,
                        width=size_em(len(title)),
                        height='1em',
                        _class='back')
        t = svg.Text(x=ox, y=oy1, fill='white')
        t += svg.TSpan(title, dy='1em')
        doc += t

    # Info
    lines = [
        'Objects: %d' % len(objs),
        'Inference time: %.2f ms (%.2f fps)' %
        (inference_time * 1000, 1.0 / inference_time)
    ]
    text_width = size_em(max(len(line) for line in lines))
    doc += svg.Rect(x=0,
                    y=0,
                    width=text_width,
                    height='2.2em',
                    transform='translate(%s, %s) scale(1,-1)' % (ox, oy2),
                    _class='back')
    t = svg.Text(y=oy2, fill='white')
    t += svg.TSpan(lines[0], x=ox)
    t += svg.TSpan(lines[1], x=ox, dy='-1.2em')
    doc += t

    return str(doc)
コード例 #13
0
        print >> sys.stderr, "File not found: " + mol_filename
        sys.exit(-2)

    #file = open(mol_filename)
    try:
        G = molfile2graph(mol_filename)
    except Exception, strerror:
        root.title("ERROR")
        Message(root, text=strerror, width=100).pack()
        Button(root, text="OK", command=root.destroy).pack()
        root.mainloop()
        sys.exit(-1)

    scene = svg.Scene(400, 400, 20)
    G.svg(scene)
    scene.set_attribute("height", 500)
    h = G.hash()
    scene.add(
        svg.Line((0, 400), (400, 400), stroke_width=3, stroke_color=black))
    scene.add(svg.Text((20, 450), h, font_size=20))
    print h
    #scene = mol2svg(file.read(), 500, 500, 20)

    if (svg_filename != None):
        scene.write_svg(svg_filename)
    else:
        scene.display()


if __name__ == '__main__':
    main()
コード例 #14
0
def main():
    init()
    subdir = "motifs"
    motif_fullpath = html_path + "/motifs"
    util._mkdir(motif_fullpath)
    main_html_file = open(html_path + "/motifs.html", "w")

    anti_motif_list = []

    for size in motif_sizes:
        motifs = verify_file(size)
        motifs_t = motifs_templates(motifs)

        motif_hist = normalize_motifs(motifs)
        motif_hist_t = normalize_motifs(motifs_t)

        print "Generating all graphs of size %d ..." % size,
        all_graphs = generate_all_graphs(motif_hist, size)
        print "[DONE]"

        results = {}
        for h in all_graphs:
            G = chemconvert.hash2graph(h)
            template = G.template()
            count = int(motifs.get(h, 0))
            likelihoods = [0, 0]
            for i in range(2, size + 1):
                likelihoods.append(get_likelihood(G, motif_hist, i))
            delta_l = likelihoods[-2] - likelihoods[-1]

            if (not results.has_key(template)):
                results[template] = []
            results[template].append((delta_l, likelihoods, count, h))

        main_html_file.write("<p>")
        size_html_file = util.embed_link(main_html_file, html_path,
                                         subdir + "/motifs%s" % size,
                                         "Motifs of size %s" % size)
        main_html_file.write("</p>")

        for (template, graph_list) in results.iteritems():
            size_html_file.write("<p>")
            template_svg = chemconvert.hash2svg(template,
                                                200,
                                                200,
                                                node_color=black,
                                                bond_color=grey)
            template_svg.embed_in_html(size_html_file, motif_fullpath,
                                       template)
            count_t = len(graph_list)
            if (count_t > 0):
                template_html_file = util.embed_link(
                    size_html_file, motif_fullpath, template,
                    "View all %d instances" % count_t)
                for (delta_l, likelihoods, count, h) in sorted(graph_list):
                    if (likelihoods[-1] <= log_zero
                            and likelihoods[-2] <= log_zero):
                        continue
                    elif (likelihoods[-1] <= log_zero):
                        bond_color = red
                        anti_motif_list.append(h)
                    else:
                        #bond_color = (0, 255 * math.exp(-delta_l), 255 * (1 - math.exp(-delta_l)))
                        bond_color = green

                    motif_svg = chemconvert.hash2svg(h, 150, 150, black,
                                                     bond_color)
                    motif_svg.set_attribute("height", 200)
                    if (True):  # add
                        #motif_svg.add(svg.Text((35, 25), h, 12, green))
                        l_string = ",".join(
                            ["%.1f" % l for l in likelihoods[2:]])
                        motif_svg.add(
                            svg.Text((10, 160),
                                     "&#916;L = %.1f" % delta_l,
                                     font_size=12))
                        motif_svg.add(
                            svg.Text((10, 175), l_string, font_size=12))
                        if (count > 0):
                            #motif_svg.add(svg.Text((35, 75), "diff = %.1f" % (l1-l0), 12, black))
                            motif_svg.add(
                                svg.Text((10, 190),
                                         "count = %d" % count,
                                         font_size=12))
                        motif_svg.embed_in_html(template_html_file,
                                                motif_fullpath, h)
                    else:
                        template_html_file.write("<p>")
                        motif_svg.embed_in_html(template_html_file,
                                                motif_fullpath, h)
                        template_html_file.write("<a href=\"" + h + ".svg\">" +
                                                 h + "</a>")
                        template_html_file.write(", count = %d" % count)
                        for i in range(2, size + 1):
                            template_html_file.write(", L(%d) = %.1f" %
                                                     (i, likelihoods[i]))
                        template_html_file.write("</p>")
                template_html_file.close()
            else:
                size_html_file.write("No instances")

            size_html_file.write("</p>")
        size_html_file.close()
    main_html_file.close()

    util.write_text_file(anti_motif_list, "../results/stat/anti_motifs.txt")
    return