Beispiel #1
0
 def _cairo_matrix(self):
     """ returns the element's matrix in cairo form """
     m = self.matrix
     return cairo.Matrix(m[0, 0], m[1, 0], m[0, 1], m[1, 1], m[0, 2], m[1,
                                                                        2])
Beispiel #2
0
 def initial_transform(self):
     '''
     Return an identity matrix
     '''
     return cairo.Matrix()
Beispiel #3
0
 def set_width_height(self, width, height):
     self.width = width
     self.height = height
     self.matrix_flipy = cairo.Matrix(yy=-1, y0=self.height)
Beispiel #4
0
def write_pdf_metadata(fileobj, scale, url_fetcher, attachments,
                       attachment_links, pages):
    """Add PDF metadata that are not handled by cairo.

    Includes:
    - attachments
    - embedded files
    - trim box
    - bleed box

    """
    pdf = PDFFile(fileobj)

    # Add embedded files

    embedded_files_id = _write_pdf_embedded_files(pdf, attachments,
                                                  url_fetcher)
    if embedded_files_id is not None:
        params = b''
        if embedded_files_id is not None:
            params += pdf_format(' /Names << /EmbeddedFiles {0} 0 R >>',
                                 embedded_files_id)
        pdf.extend_dict(pdf.catalog, params)

    # Add attachments

    # A single link can be split in multiple regions. We don't want to embed
    # a file multiple times of course, so keep a reference to every embedded
    # URL and reuse the object number.
    # TODO: If we add support for descriptions this won't always be correct,
    # because two links might have the same href, but different titles.
    annot_files = {}
    for page_links in attachment_links:
        for link_type, target, rectangle in page_links:
            if link_type == 'attachment' and target not in annot_files:
                # TODO: use the title attribute as description
                annot_files[target] = _write_pdf_attachment(
                    pdf, (target, None), url_fetcher)

    for pdf_page, document_page, page_links in zip(pdf.pages, pages,
                                                   attachment_links):

        # Add bleed box

        media_box = pdf_page.get_value('MediaBox',
                                       '\\[(.+?)\\]').decode('ascii').strip()
        left, top, right, bottom = (float(value)
                                    for value in media_box.split(' '))
        # Convert pixels into points
        bleed = {
            key: value * 0.75
            for key, value in document_page.bleed.items()
        }

        trim_left = left + bleed['left']
        trim_top = top + bleed['top']
        trim_right = right - bleed['right']
        trim_bottom = bottom - bleed['bottom']

        # Arbitrarly set PDF BleedBox between CSS bleed box (PDF MediaBox) and
        # CSS page box (PDF TrimBox), at most 10 points from the TrimBox.
        bleed_left = trim_left - min(10, bleed['left'])
        bleed_top = trim_top - min(10, bleed['top'])
        bleed_right = trim_right + min(10, bleed['right'])
        bleed_bottom = trim_bottom + min(10, bleed['bottom'])

        pdf.extend_dict(
            pdf_page,
            pdf_format(
                '/TrimBox [ {} {} {} {} ] /BleedBox [ {} {} {} {} ]'.format(
                    trim_left, trim_top, trim_right, trim_bottom, bleed_left,
                    bleed_top, bleed_right, bleed_bottom)))

        # Add links to attachments

        # TODO: splitting a link into multiple independent rectangular
        # annotations works well for pure links, but rather mediocre for other
        # annotations and fails completely for transformed (CSS) or complex
        # link shapes (area). It would be better to use /AP for all links and
        # coalesce link shapes that originate from the same HTML link. This
        # would give a feeling similiar to what browsers do with links that
        # span multiple lines.
        annotations = []
        for link_type, target, rectangle in page_links:
            if link_type == 'attachment' and annot_files[target] is not None:
                matrix = cairo.Matrix(xx=scale,
                                      yy=-scale,
                                      y0=document_page.height * scale)
                rect_x, rect_y, width, height = rectangle
                rect_x, rect_y = matrix.transform_point(rect_x, rect_y)
                width, height = matrix.transform_distance(width, height)
                # x, y, w, h => x0, y0, x1, y1
                rectangle = rect_x, rect_y, rect_x + width, rect_y + height
                content = [
                    pdf_format(
                        '<< /Type /Annot '
                        '/Rect [{0:f} {1:f} {2:f} {3:f}] /Border [0 0 0]\n',
                        *rectangle)
                ]
                link_ap = pdf.write_new_object(
                    pdf_format(
                        '<< /Type /XObject /Subtype /Form '
                        '/BBox [{0:f} {1:f} {2:f} {3:f}] /Length 0 >>\n'
                        'stream\n'
                        'endstream', *rectangle))
                content.append(b'/Subtype /FileAttachment ')
                # evince needs /T or fails on an internal assertion. PDF
                # doesn't require it.
                content.append(
                    pdf_format('/T () /FS {0} 0 R /AP << /N {1} 0 R >>',
                               annot_files[target], link_ap))
                content.append(b'>>')
                annotations.append(pdf.write_new_object(b''.join(content)))

        if annotations:
            pdf.extend_dict(
                pdf_page,
                pdf_format('/Annots [{0}]',
                           ' '.join('{0} 0 R'.format(n) for n in annotations)))

    pdf.finish()
def seuquence_visualizer(npz_file):
    png_list = []
    with np.load(npz_file) as f:
        kernels = f["prediction/W_conv1:0"]

    kernel_shape = kernels.shape
    i = 0
    j = 0
    k = 0
    l = 0
    #line_num=10
    #DNA_len=1000
    #max_value=reconstruct.max()

    kernels = np.reshape(kernels,
                         (kernel_shape[0], kernel_shape[1], kernel_shape[3]))
    kernel_shape = kernels.shape
    # (9, 4, 320)
    #kernels=np.exp(kernels)
    #kernels=kernels/np.amax(kernels)
    #reconstruct=80*reconstruct/max_value
    #print norm
    #scale_factor=400/max_value
    width = kernel_shape[0] * 40 + 10
    hight = 150
    y_center = hight * 0.8

    prefix = os.path.splitext(npz_file)[0] + "_kernels/"
    if not os.path.isdir(prefix):
        try:
            os.mkdir(prefix)
        except:
            sys.exit()

    meme_fileout = open(prefix + 'motifs.meme', 'w')
    meme_def = "MEME version 4\n\nALPHABET= ACGT\n\nstrands: + -\n\n\Background letter frequencies (from uniform background):\nA 0.2500 C 0.2500 G 0.2500 T 0.2500\n\n"
    meme_fileout.write(meme_def)
    kernel_shape_ic_list = []
    for k in range(kernel_shape[2]):
        meme_def = "MOTIF kernel_" + str(
            k
        ) + "\n\nletter-probability matrix: alength= 4 w= 9 nsites= 9 E= 0\n"
        meme_fileout.write(meme_def)
        ims1 = cairo.PDFSurface(None, width, hight)
        cr = cairo.Context(ims1)
        cr.set_source_rgb(0.0, 0.0, 0)
        cr.move_to(width * 0.1, y_center)
        cr.line_to(width * 0.9, y_center)
        #cr.move_to(50, 100)
        #cr.line_to(50, 412)
        cr.set_line_width(2)
        cr.stroke()
        cr.move_to(width * 0.1, y_center)
        cr.line_to(width * 0.1, y_center - 120)
        cr.set_line_width(2)
        cr.stroke()
        cr.move_to(width * 0.1, y_center - 60)
        cr.line_to(width * 0.08, y_center - 60)
        cr.set_line_width(2)
        cr.stroke()
        cr.move_to(width * 0.075, y_center - 60 + 4 * 10)
        cr.rotate(-90 * math.pi / 180.0)
        #cr.set_line_width(2)
        cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                            cairo.FONT_WEIGHT_NORMAL)
        font_mat = cairo.Matrix(xx=32.0, yx=0.0, xy=0.0, yy=32, x0=0.0, y0=0.0)
        cr.set_font_matrix(font_mat)
        cr.show_text("2 bit")
        cr.rotate(90 * math.pi / 180.0)
        font_mat = cairo.Matrix(xx=12.0, yx=0.0, xy=0.0, yy=12, x0=0.0, y0=0.0)
        cr.move_to(width * 0.5, hight)
        cr.show_text("k" + str(k))
        #print y_center

        AGCT = {}
        #values=[]
        #print reconstruct[k]

        #reconstruct[k]=reconstruct[k]*50
        #mean=np.mean(reconstruct[k])
        #stdv=np.std(reconstruct[k])

        #reconstruct[k]=(reconstruct[k]-mean)/stdv
        #print kernels[:,:,k]
        xkernel = kernels[:, :, k]
        xkernel = np.exp(xkernel * 100.0)
        #print xkernel
        #sys.exit()
        probability = xkernel / np.nansum(xkernel, axis=1)[:, None]
        #probability/=np.nansum(probability)
        for p in probability:
            to_print = str(p[0]) + " " + str(p[2]) + " " + str(
                p[1]) + " " + str(p[3]) + "\n"
            meme_fileout.write(to_print)
        meme_fileout.write("\n\n")
        ic_sum = 0.0
        for pind, p in enumerate(probability):

            ic = np.nansum(p * np.log2(p * 4 + 0.0001)) * 80
            ic_sum += ic
            #print ic
            A = ["A", p[0] * ic]
            G = ["G", p[1] * ic]
            C = ["C", p[2] * ic]
            T = ["T", p[3] * ic]
            values = [A, G, C, T]
            pos = filter(lambda x: x[1] >= 0, values)
            #neg=filter(lambda x:x[1]<0,values)
            pos.sort(key=lambda x: x[1])
            #neg.sort(key=lambda x:x[1], reverse=True)
            Nucpos = 0.01
            #Nucneg=0
            x_pos = width * 0.1 + pind * 30

            for l in range(len(pos)):
                Nuc = pos[l][0]

                Nucsize = pos[l][1] + 0.01
                cr.move_to(x_pos, y_center - Nucpos * 0.75)
                cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                                    cairo.FONT_WEIGHT_NORMAL)
                _select_color(cr, Nuc)
                font_mat = cairo.Matrix(xx=40.0,
                                        yx=0.0,
                                        xy=0.0,
                                        yy=Nucsize,
                                        x0=0.0,
                                        y0=0.0)
                cr.set_font_matrix(font_mat)
                cr.show_text(str(Nuc))
                Nucpos += abs(pos[l][1])
        ims1.write_to_png(prefix + "kernel_" + str(k) + '.png')
        png_list.append(prefix + "kernel_" + str(k) + '.png')
        kernel_shape_ic_list.append(ic_sum)
        cr.show_page()
    meme_fileout.close()

    return png_list, kernel_shape_ic_list
Beispiel #6
0
 def skew(self, x=1, y=0):
     ### TODO bring back transform mixin
     t = self._canvas.transform
     t *= cairo.Matrix(1, 0, x, 1, 0, 0)
     t *= cairo.Matrix(1, y, 0, 1, 0, 0)
     self._canvas.transform = t
Beispiel #7
0
def convert_page(path, note_name, notebook_path, directory, pdf_file,
                 page_number):
    page = papyrus_pb2.Page()
    # Open and parse papyrus page using protobuf
    page.ParseFromString(open(path, 'rb').read())
    # Create a new pdf surface for drawing

    if page.background.width == 0 and page.background.height == 0:
        print("\tInfinite page!")

        max_x = 0
        max_y = 0

        for item in page.layer.item:
            bounds = None
            if item.type == papyrus_pb2.Item.Type.Value('Stroke'):
                bounds = item.stroke.bounds
            elif item.type == papyrus_pb2.Item.Type.Value('Shape'):
                if item.shape.type == 'Ellipse':
                    bounds = item.shape.ellipse.bounds
            elif item.type == papyrus_pb2.Item.Type.Value('Text'):
                bounds = item.text.bounds
            else:
                print(item)

            if bounds is not None:
                if bounds.right > max_x:
                    max_x = bounds.right

                if bounds.bottom > max_y:
                    max_y = bounds.bottom

        page.background.width = max_x + 1
        page.background.height = max_y + 1

    note_name = titlesafe(note_name)
    print("\t%s" % note_name)

    note_path = directory + '/' + notebook_path + '/' + dirsafe(note_name)
    new_note_path = note_path
    num = 1

    #while os.path.exists(new_note_path):
    #    new_note_path = note_path + '(' + str(num) + ')'
    #    num += 1

    makedir(note_path)

    note_path = new_note_path

    pdfpath = note_path + '/pdf'
    makedir(pdfpath)

    pdffile = pdfpath + '/page' + str(page_number) + '.pdf'
    print("\tSource: %s\n\tOutput: %s" % (path, pdffile))

    pdf_out = open(pdffile, 'w')
    surface = cairocffi.PDFSurface(pdf_out, cm_to_point(page.background.width),
                                   cm_to_point(page.background.height))
    context = cairocffi.Context(surface)

    # Paint the page white
    context.set_source_rgba(0, 0, 0, 0)
    context.paint()

    for item in page.layer.item:
        if item.type == papyrus_pb2.Item.Type.Value('Stroke'):
            context.save()
            # Translate to reference_point (stroke origin)
            context.translate(cm_to_point(item.stroke.reference_point.x),
                              cm_to_point(item.stroke.reference_point.y))
            # Set source color
            argb = u32_to_4f(item.stroke.color)
            context.set_source_rgba(argb[1], argb[2], argb[3], argb[0])
            # Set line width
            width = cm_to_point(item.stroke.weight)
            # Other parameter
            context.set_line_join(cairocffi.LINE_JOIN_ROUND)
            context.set_line_cap(cairocffi.LINE_CAP_ROUND)
            context.move_to(0, 0)

            if item.stroke.stroke_type == papyrus_pb2.Stroke.Highlight:
                context.push_group()
                context.set_source_rgba(argb[1], argb[2], argb[3], 1)
                context.fill_preserve()
                context.set_line_cap(cairocffi.LINE_CAP_SQUARE)

            for point in item.stroke.point:
                context.line_to(cm_to_point(point.x), cm_to_point(point.y))
                if item.stroke.stroke_type == papyrus_pb2.Stroke.Highlight:
                    context.set_line_width(width)
                    #context.
                elif point.HasField('pressure'):
                    context.set_line_width(width * point.pressure)
                else:
                    context.set_line_width(width)
                context.stroke()
                context.move_to(cm_to_point(point.x), cm_to_point(point.y))
            if item.stroke.stroke_type == papyrus_pb2.Stroke.Highlight:
                context.pop_group_to_source()
                context.paint_with_alpha(argb[0])
            context.restore()
        elif item.type == papyrus_pb2.Item.Type.Value(
                'Shape') and item.shape.ellipse is not None:
            width = item.shape.ellipse.weight * 0.3

            context.save()
            context.new_sub_path()
            context.translate(cm_to_point(item.shape.ellipse.center_x),
                              cm_to_point(item.shape.ellipse.center_y))
            context.set_line_width(item.shape.ellipse.weight)
            argb = u32_to_4f(item.shape.ellipse.color)
            context.set_line_width(width)
            context.set_source_rgba(argb[1], argb[2], argb[3], argb[0])
            context.scale(cm_to_point(item.shape.ellipse.radius_x),
                          cm_to_point(item.shape.ellipse.radius_y))
            context.arc(0, 0, 1,
                        (item.shape.ellipse.start_angle / 360) * 2 * math.pi,
                        (item.shape.ellipse.sweep_angle / 360) * 2 * math.pi)
            context.close_path()
            context.stroke()
            context.restore()
        elif item.type == papyrus_pb2.Item.Type.Value('Text'):
            context.save()
            context.set_font_size(item.text.weight)

            # Color
            argb = u32_to_4f(item.text.color)
            context.set_source_rgba(argb[1], argb[2], argb[3], argb[0])

            context.move_to(cm_to_point(item.text.bounds.left),
                            cm_to_point(item.text.bounds.top))
            tw = int(item.text.weight)
            size_m = cairocffi.Matrix(tw, 0, 0, tw, 0, 0)
            scaledFont = cairocffi.ScaledFont(
                cairocffi.ToyFontFace("sans-serif"), size_m)
            glyphs = scaledFont.text_to_glyphs(
                cm_to_point(item.text.bounds.left),
                cm_to_point(item.text.bounds.bottom), item.text.text, False)
            context.show_glyphs(glyphs)
            context.restore()

        elif item.type == papyrus_pb2.Item.Type.Value('Image'):
            if (DEBUG):
                print("Got an image!")
                print(item.image.image_hash)

            # Convert JPEG image to PNG
            im = Image.open(base_directory + "data/imgs/" +
                            item.image.image_hash)
            im = im.crop(
                (item.image.crop_bounds.left, item.image.crop_bounds.top,
                 item.image.crop_bounds.right, item.image.crop_bounds.bottom))
            im.save(
                base_directory + "data/imgs/" + item.image.image_hash + ".png",
                "PNG")
            im.close()

            matrix = cairocffi.Matrix()

            scale_x = cm_to_point(item.image.bounds.right -
                                  item.image.bounds.left) / (
                                      item.image.crop_bounds.right -
                                      item.image.crop_bounds.left)
            scale_y = cm_to_point(item.image.bounds.bottom -
                                  item.image.bounds.top) / (
                                      item.image.crop_bounds.bottom -
                                      item.image.crop_bounds.top)

            if (DEBUG):
                print("Scale X: %d" % (1 / scale_x))
                print("Scale Y: %d" % (1 / scale_y))
                print("Translate: %d" % cm_to_point(item.image.bounds.left))
            matrix.scale(1 / scale_x, 1 / scale_y)
            matrix.translate(-cm_to_point(item.image.bounds.left),
                             -cm_to_point(item.image.bounds.top))

            im_surface = cairocffi.ImageSurface.create_from_png(
                base_directory + "./data/imgs/" + item.image.image_hash +
                ".png")
            im_surface_pattern = cairocffi.SurfacePattern(im_surface)

            im_surface_pattern.set_filter(cairocffi.FILTER_GOOD)
            im_surface_pattern.set_matrix(matrix)

            context.save()
            context.set_source(im_surface_pattern)
            context.rectangle(
                cm_to_point(item.image.bounds.left),
                cm_to_point(item.image.bounds.top),
                cm_to_point(item.image.bounds.right - item.image.bounds.left),
                cm_to_point(item.image.bounds.bottom - item.image.bounds.top))
            context.fill()
            context.restore()
        else:
            print(item)
            print("Item of type {} not supported".format(
                papyrus_pb2.Item.Type.Name(item.type)))
    surface.flush()
    surface.finish()
    pdf_out.close()

    if page.background.HasField("pdf_background"):

        try:
            output_file = PdfFileWriter()
            input_file = PdfFileReader(file(pdffile, "rb"))
            pdf_file = PdfFileReader(
                file(base_directory + "data/docs/" + pdf_file, "rb"))
            pdf_page = pdf_file.getPage(
                page.background.pdf_background.page_number)

            input_page = input_file.getPage(0)
            pdf_page.mergePage(input_page)

            output_file.addPage(pdf_page)

            with open(pdffile + ".tmp", "wb") as outputStream:
                output_file.write(outputStream)
            os.rename(pdffile + ".tmp", pdffile)
        except:
            print(
                "\t%sUnable to merge PDFs - maybe the PDF was malformed? Result was %s%s"
                % (color.RED, sys.exc_info()[0], color.END))
    print("")
    return pdffile
def seuquence_visualizer2(npz_file, output_file):

    if type(npz_file)==str:
        with np.load(npz_file) as f:
            reconstruct=f["recon"]
    else:
        reconstruct=npz_file

    i=0 
    j=0
    k=0
    l=0
    line_num=10
    DNA_len=1000
    #max_value=reconstruct.max()
    
    reconstruct=np.reshape(reconstruct, (DNA_len, 4))
    max_value=np.max(reconstruct)
    reconstruct=80*reconstruct/max_value
    #print norm
    """reconstruct_pos=reconstruct.clip(min=0)
    reconstruct_neg=reconstruct.clip(max=0)
    reconstruct_pos_sum=np.sum(reconstruct_pos, axis=1)
    reconstruct_neg_sum=np.sum(reconstruct_neg, axis=1)
    
    max_value=reconstruct_pos_sum.max()
    min_value=reconstruct_neg_sum.min()
    min_value_abs=-min_value
    
    if min_value_abs>max_value:
        max_value=min_value_abs"""
    
    #scale_factor=400/max_value
    
    width=DNA_len*30/line_num+200
    hight=1024*2*3
    y_center=300
    ims1 = cairo.PDFSurface(output_file, width, hight)       
    cr = cairo.Context(ims1)
    cr.move_to(100, y_center)
    cr.line_to(DNA_len/line_num*30+100, y_center)
    #cr.move_to(50, 100)
    #cr.line_to(50, 412)
    cr.set_line_width(2)
    cr.stroke()
    
    meme_fileout=open(output_file+'.meme','w')
    meme_fileout.write("MEME version 4\n\nALPHABET= ACGT\n\nstrands: + -\n\n\
Background letter frequencies (from uniform background):\nA 0.2500 C 0.2500 G 0.2500 T 0.2500\n\nMOTIF LONG_MOTIF\n\nletter-probability matrix: alength= 4 w= 1000 nsites= 20 E= 0\n")
        
    for k in range(1000):
        if not k==0 and k%(DNA_len/line_num)==0:
            cr.set_source_rgba(0.0,0.0,0,1.0)
            y_center+=300
            cr.move_to(100, y_center)
            cr.line_to(DNA_len/line_num*30+100, y_center)
            cr.stroke()
            print(y_center)
            
        AGCT={}
        values=[]
        #print reconstruct[k]
        
        #reconstruct[k]=reconstruct[k]*50
        #mean=np.mean(reconstruct[k])
        #stdv=np.std(reconstruct[k])
        
        #reconstruct[k]=(reconstruct[k]-mean)/stdv
        probability=np.round(np.true_divide(np.exp(reconstruct[k]),np.nansum(np.exp(reconstruct[k]))),6)
        probability/=np.nansum(probability)
        to_print=""
        for i in range(4):
            if np.isnan(probability[i]):
                print(k)
                probability[i]=0.0
        
        to_print=str(probability[0])+" "+str(probability[2])+" "+str(probability[1])+" "+str(probability[3])+"\n"
        #print(to_print)
        meme_fileout.write(to_print)
        
        ic=np.nansum(probability*np.log2(probability*4+0.0001))*120
        #print ic
        A=["A", probability[0]*ic]
        G=["G",probability[1]*ic]
        C=["C",probability[2]*ic]
        T=["T", probability[3]*ic]

        """
        A=["A", reconstruct[k][0]*scale_factor]
        G=["G",reconstruct[k][1]*scale_factor]
        C=["C",reconstruct[k][2]*scale_factor]
        T=["T", reconstruct[k][3]*scale_factor]"""
        values=[A,G,C,T]
        pos=filter(lambda x:x[1]>=0,values)
        #neg=filter(lambda x:x[1]<0,values)
        pos.sort(key=lambda x:x[1])
        #neg.sort(key=lambda x:x[1], reverse=True)
        Nucpos=0
        #Nucneg=0
        x_pos=k%(DNA_len/line_num)
        
        for l in range(len(pos)):
            Nuc=pos[l][0]
            
            Nucsize=abs(pos[l][1])+0.1
            cr.move_to(100+x_pos*40*0.75, y_center-Nucpos*0.75)               
            cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
            _select_color(cr, Nuc)
            font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=Nucsize,x0=0.0,y0=0.0)
            cr.set_font_matrix(font_mat)
            cr.show_text(str(Nuc))
            Nucpos+=abs(pos[l][1])
            
        """
        l=0
        for l in range(len(neg)):
            Nuc=neg[l][0]
            Nucsize=abs(neg[l][1])
            
            cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
            _select_color(cr, Nuc)
            font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=-Nucsize,x0=0.0,y0=0.0)
            cr.set_font_matrix(font_mat)
            cr.move_to(100+x_pos*40*0.75, y_center+(Nucneg)*0.75)
            cr.show_text(str(Nuc))
            Nucneg+=abs(neg[l][1])"""
        """     
        max_value=np.amax(reconstruct[k])
        sum_value=np.sum(reconstruct[k])
        print max_value
        if max_value>0.0:
            max_index=np.argmax(reconstruct[k])
        
            if max_index==0:
                Nuc="A"
            elif max_index==1:
                Nuc="G"
            elif max_index==2:
                Nuc="C"
            elif max_index==3:
                Nuc="T"
        else:
            Nuc="N"

        Nucpos=0
        Nucneg=0
        Nucsize=max_value*1000
        Nucsize2=sum_value*1000"""

        #cr.move_to(50+x_pos*40*0.75, y_center)               
        #cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
        #select_color(cr, Nuc)
        #font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=Nucsize+0.1,x0=0.0,y0=0.0)
        #cr.set_font_matrix(font_mat)
        #print Nuc
        #cr.show_text(str(Nuc))
        """cr.move_to(50+x_pos*40*0.75, y_center)               
        cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
        select_color(cr, Nuc2)
        font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=Nucsize2+20.0,x0=0.0,y0=0.0)
        cr.set_font_matrix(font_mat)
        cr.show_text(str(Nuc2))    """
    #cr.set_font_size(40)
    meme_fileout.close()
    cr.show_page()
Beispiel #9
0
 def _icon_scale_matrix(size, newsize):
     matrix = cairocffi.Matrix()
     sp = newsize / size
     matrix.scale(sp, sp)
     return matrix
Beispiel #10
0
 def _render_mask(self):
     self.ctx.set_operator(cairo.OPERATOR_OVER)
     ptn = cairo.SurfacePattern(self.mask)
     ptn.set_matrix(cairo.Matrix(xx=1.0, yy=-1.0, x0=-self.origin_in_pixels[0], y0=self.size_in_pixels[1] + self.origin_in_pixels[1]))
     self.ctx.set_source(ptn)
     self.ctx.paint()
Beispiel #11
0
def prepare_metadata(document, bookmark_root_id, scale):
    """Change metadata into data structures closer to the PDF objects.

    In particular, convert from WeasyPrint units (CSS pixels from
    the top-left corner) to PDF units (points from the bottom-left corner.)

    :param scale:
        PDF points per CSS pixels.
        Defaults to 0.75, but is affected by `zoom` in
        :meth:`weasyprint.document.Document.write_pdf`.

    """
    # X and width unchanged;  Y’ = page_height - Y;  height’ = -height
    matrices = [cairo.Matrix(xx=scale, yy=-scale, y0=page.height * scale)
                for page in document.pages]
    links = []
    for page_links, matrix in izip(document.resolve_links(), matrices):
        new_page_links = []
        for link_type, target, rectangle in page_links:
            if link_type == 'internal':
                target_page, target_x, target_y = target
                target = (
                    (target_page,) +
                    matrices[target_page].transform_point(target_x, target_y))
            rect_x, rect_y, width, height = rectangle
            rect_x, rect_y = matrix.transform_point(rect_x, rect_y)
            width, height = matrix.transform_distance(width, height)
            # x, y, w, h => x0, y0, x1, y1
            rectangle = rect_x, rect_y, rect_x + width, rect_y + height
            new_page_links.append((link_type, target, rectangle))
        links.append(new_page_links)

    bookmark_root = {'Count': 0}
    bookmark_list = []
    last_id_by_depth = [bookmark_root_id]
    last_by_depth = [bookmark_root]
    for bookmark_id, (label, target, depth) in enumerate(
            flatten_bookmarks(document.make_bookmark_tree()),
            bookmark_root_id + 1):
        target_page, target_x, target_y = target
        target = (target_page,) + matrices[target_page].transform_point(
            target_x, target_y)
        bookmark = {
            'Count': 0, 'First': None, 'Last': None, 'Prev': None,
            'Next': None, 'Parent': last_id_by_depth[depth - 1],
            'label': label, 'target': target}

        if depth > len(last_by_depth) - 1:
            last_by_depth[depth - 1]['First'] = bookmark_id
        else:
            # The bookmark is sibling of last_id_by_depth[depth]
            bookmark['Prev'] = last_id_by_depth[depth]
            last_by_depth[depth]['Next'] = bookmark_id

            # Remove the bookmarks with a depth higher than the current one
            del last_by_depth[depth:]
            del last_id_by_depth[depth:]

        for i in range(depth):
            last_by_depth[i]['Count'] += 1
        last_by_depth[depth - 1]['Last'] = bookmark_id

        last_by_depth.append(bookmark)
        last_id_by_depth.append(bookmark_id)
        bookmark_list.append(bookmark)
    return bookmark_root, bookmark_list, links
def main():
    f = gl.glob(
        "/home/fast/onimaru/data/reconstruction/network_constructor_deepsea_1d3_Sat_Jul__1_145520_2017.ckpt-6293_transpose_*.npz"
    )
    for npz in f:
        with np.load(npz, 'r') as f:

            reconstruct = f["conv2"]
            original_seq = f["original"]
        i = 0
        j = 0
        k = 0
        l = 0
        reconstruct = np.reshape(reconstruct, (1000, 4))
        original_seq = np.reshape(original_seq, (1000, 4))

        line_num = 10
        DNA_len = 1000
        width = DNA_len * 30 / line_num + 200
        hight = 1024 * 2 * 3
        y_center = 300
        ims1 = cairo.PDFSurface(npz + ".pdf", width, hight)
        cr = cairo.Context(ims1)
        cr.move_to(100, y_center)
        cr.line_to(DNA_len / line_num * 30 + 100, y_center)
        #cr.move_to(50, 100)
        #cr.line_to(50, 412)
        cr.set_line_width(2)
        cr.stroke()
        max_value = reconstruct.max()
        SCALE = 300 / max_value
        for k in range(1000):
            if not k == 0 and k % (DNA_len / line_num) == 0:
                cr.set_source_rgba(0.0, 0.0, 0, 1.0)
                y_center += 400
                cr.move_to(100, y_center)
                cr.line_to(DNA_len // line_num * 30 + 100, y_center)
                cr.stroke()
                print(y_center)
            max_value = np.amax(reconstruct[k])
            sum_value = np.sum(reconstruct[k])
            max_value2 = np.amax(original_seq[k])
            print(max_value)
            if max_value > 0.0:
                max_index = np.argmax(reconstruct[k])

                if max_index == 0:
                    Nuc = "A"
                elif max_index == 1:
                    Nuc = "G"
                elif max_index == 2:
                    Nuc = "C"
                elif max_index == 3:
                    Nuc = "T"
            else:
                Nuc = "N"

            if max_value2 > 0.0:
                max_index2 = np.argmax(original_seq[k])

                if max_index2 == 0:
                    Nuc2 = "A"
                elif max_index2 == 1:
                    Nuc2 = "G"
                elif max_index2 == 2:
                    Nuc2 = "C"
                elif max_index2 == 3:
                    Nuc2 = "T"
            else:
                Nuc2 = "N"

            Nucpos = 0
            Nucneg = 0
            Nucsize = max_value * SCALE
            Nucsize2 = sum_value * SCALE
            x_pos = k % (DNA_len / line_num)
            #cr.move_to(50+x_pos*40*0.75, y_center)
            #cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
            #select_color(cr, Nuc)
            #font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=Nucsize+0.1,x0=0.0,y0=0.0)
            #cr.set_font_matrix(font_mat)
            #print Nuc
            #cr.show_text(str(Nuc))
            cr.move_to(100 + x_pos * 40 * 0.75, y_center)
            cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                                cairo.FONT_WEIGHT_NORMAL)
            select_color(cr, Nuc2)
            font_mat = cairo.Matrix(xx=40.0,
                                    yx=0.0,
                                    xy=0.0,
                                    yy=Nucsize + 20.0,
                                    x0=0.0,
                                    y0=0.0)
            cr.set_font_matrix(font_mat)
            cr.show_text(str(Nuc2))
        #cr.set_font_size(40)
        cr.show_page()
Beispiel #13
0
 def __init__(self, bot):
     # Takes bot rather than canvas for compatibility with libraries - e.g. the colors library
     self._canvas = canvas = bot._canvas
     self._bot = bot
     self._set_mode(canvas.mode)
     self._transform = cairo.Matrix(*canvas.transform)
Beispiel #14
0
 def push_matrix(self):
     self.matrix_stack.append(self.transform)
     self.transform = cairo.Matrix(*self.transform)
Beispiel #15
0
 def ctm(self, mx):
     nctm = mmult(self._in_transform, mx)
     self.ctx.set_matrix(cairo.Matrix(*nctm))
Beispiel #16
0
def kernel_connector(npz_file, png_list, kernel_shape_ic_list):
    """
    variable_dict={"W_conv1": W_conv1, 
                   "W_conv2": W_conv2,
                   "W_conv21": W_conv21, 
                   "W_conv22": W_conv22, 
                   "W_fc1": W_fc1,
                   "W_fc4": W_fc4, 
                   "b_fc1": b_fc1, 
                   "b_fc4": b_fc4}
    """

    conv_kernels = []
    fc_kernels = []
    with np.load(npz_file) as f:
        #conv_kernels.append(f["prediction/W_conv1:0"])
        conv_kernels.append(f["prediction/W_conv2:0"])
        conv_kernels.append(f["prediction/W_conv21:0"])
        conv_kernels.append(f["prediction/W_conv22:0"])
        fc_kernels.append(f["prediction/W_fc1:0"])
        fc_kernels.append(f["prediction/W_fc4:0"])
    conv_kernels_sum = []
    for i in conv_kernels:
        i_shape = i.shape
        #print i_shape
        i_reshape = i.reshape([i_shape[0], i_shape[2], i_shape[3]])
        conv_kernels_sum.append(np.clip(np.sum(i_reshape, axis=0), 0.0, None))
    #print fc_kernels[0].shape, conv_kernels[-1].shape
    last_seq_length = fc_kernels[0].shape[0] / conv_kernels[-1].shape[-1]
    #print last_seq_length
    #sys.exit()
    pt_per_mm = 72 / 25.4
    width, height = 210 * pt_per_mm, 297 * pt_per_mm * 2
    upper_lim = height * 0.05
    lateral_lim = width * 0.4
    x_interval = width * 0.8 / 9.0
    nodew = width * 0.005
    nodeh = 0.0015 * height

    y_num = fc_kernels[-1].shape[1]
    for _y_num in range(y_num):
        out_dir = os.path.splitext(npz_file)[0] + "_" + str(
            _y_num) + "_network.pdf"
        ims1 = cairo.PDFSurface(out_dir, width, height)
        cr = cairo.Context(ims1)
        coordinates = {}
        """drawing nodes in convolution layers on a surface"""

        for i, conv in enumerate(conv_kernels_sum):
            x = lateral_lim + i * (x_interval)
            cr.move_to(x, upper_lim - 0.0018 * height)
            cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                                cairo.FONT_WEIGHT_NORMAL)
            font_mat = cairo.Matrix(xx=12.0,
                                    yx=0.0,
                                    xy=0.0,
                                    yy=12,
                                    x0=0.0,
                                    y0=0.0)
            cr.set_font_matrix(font_mat)
            cr.show_text("hidden" + str(i + 1))
            coordinates["hidden" + str(i + 1)] = []
            conv_shape = conv.shape
            print conv_shape
            for j in range(conv_shape[0]):
                y = upper_lim + 0.0018 * height * j
                #cr.move_to(width*0.1, height*0.1+0.0008*height*j)

                #cr.arc(x, y, 0.001*height, 0, 2*math.pi)
                cr.rectangle(x, y, nodew, nodeh)
                coordinates["hidden" + str(i + 1)].append([x, y])
                cr.fill()
            if i + 1 == len(conv_kernels_sum):
                x += x_interval
                cr.move_to(x, upper_lim - 0.0018 * height)
                cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                                    cairo.FONT_WEIGHT_NORMAL)
                font_mat = cairo.Matrix(xx=12.0,
                                        yx=0.0,
                                        xy=0.0,
                                        yy=12,
                                        x0=0.0,
                                        y0=0.0)
                cr.set_font_matrix(font_mat)
                cr.show_text("hidden" + str(i + 2))
                coordinates["hidden" + str(i + 2)] = []
                last_conv_y = 0.0
                for j in range(conv_shape[1]):

                    y = upper_lim + 0.0018 * height * j
                    #cr.move_to(width*0.1, height*0.1+0.0008*height*j)

                    #cr.arc(x, y, 0.001*height, 0, 2*math.pi)
                    cr.rectangle(x, y, nodew, nodeh)
                    coordinates["hidden" + str(i + 2)].append([x, y])
                    cr.fill()

                last_conv_y = y + 0.0018 * height - upper_lim
                last_conv_shape = conv_shape[1]
        """drawing nodes as bars in fully-connected layers"""

        for i, fc in enumerate(fc_kernels):
            x += x_interval
            cr.move_to(x, upper_lim - 0.0018 * height)
            cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)
            cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                                cairo.FONT_WEIGHT_NORMAL)
            font_mat = cairo.Matrix(xx=12.0,
                                    yx=0.0,
                                    xy=0.0,
                                    yy=12,
                                    x0=0.0,
                                    y0=0.0)
            cr.set_font_matrix(font_mat)
            cr.show_text("fc" + str(i + 1))
            coordinates["fc" + str(i + 1)] = []
            fc_shape = fc.shape
            print fc_shape
            y = upper_lim
            #cr.move_to(width*0.1, height*0.1+0.0008*height*j)

            #cr.arc(x, y, 0.001*height, 0, 2*math.pi)
            cr.set_source_rgba(0.5, 0.5, 0.5, 0.5)
            cr.rectangle(
                x, y, width * 0.015, fc_shape[0] * last_conv_y /
                (float(last_seq_length) * last_conv_shape))
            coordinates["fc" + str(i + 1)].append([x, y])
            cr.fill()

        fc2_hight = fc_shape[0] * last_conv_y / (float(last_seq_length) *
                                                 last_conv_shape)
        """drawing nodes in the last prediction layer"""
        x += x_interval
        cr.move_to(x, upper_lim - 0.0018 * height)
        cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)
        cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                            cairo.FONT_WEIGHT_NORMAL)
        font_mat = cairo.Matrix(xx=12.0, yx=0.0, xy=0.0, yy=12, x0=0.0, y0=0.0)
        cr.set_font_matrix(font_mat)
        cr.show_text("y")
        coordinates["y"] = []
        fc_shape = fc.shape

        for j in range(fc_shape[1]):

            y = upper_lim + fc2_hight / float(fc_shape[1]) * j
            cr.set_source_rgba(0.0, 0.0, 0.0, 1.0)
            cr.rectangle(x, y, nodew * 3, nodeh * 3)
            coordinates["y"].append([x, y])
            cr.fill()

        w_fc2 = fc_kernels[1]
        w_fc2_max = np.amax(w_fc2)
        w_fc2_shape = w_fc2.shape

        x2, y2 = coordinates["fc2"][0]
        new_fc2_coord = []
        for k in range(w_fc2_shape[0]):
            y2_ = y2 + (last_conv_y /
                        (float(last_seq_length) * last_conv_shape)) * k
            new_fc2_coord.append([x2, y2_])

        nodes_interest = _y_num

        linked = set()
        unfilterd_links = []
        for i, j in enumerate(w_fc2):
            #x1, y1=new_fc2_coord[i]
            for k in range(len(j)):
                if k == nodes_interest:
                    x2, y2 = coordinates["y"][k]
                    cr.set_source_rgba(1, 0, 0, 1)
                    cr.rectangle(x2, y2, nodew * 3, nodeh * 3)
                    cr.fill()
                    unfilterd_links.append([j[k], i, k])
        rev_sort = sorted(unfilterd_links, reverse=True)
        if len(rev_sort) < 10:
            _limit = len(rev_sort)
        else:
            _limit = 10
        for m, i, k in rev_sort[:_limit]:
            x1, y1 = new_fc2_coord[i]
            x2, y2 = coordinates["y"][k]
            cr.move_to(x1 + width * 0.005, y1 + 0.001 * height / 2.0)
            cr.line_to(x2, y2 + 0.001 * height / 2.0)
            cr.set_source_rgba(0.0, 0.0, 1.0, 0.5)
            cr.set_line_width(1)
            cr.stroke()
            linked.add(i)

        w_fc1 = fc_kernels[0]
        w_fc1_max = np.amax(w_fc1)
        w_fc1_shape = w_fc1.shape
        print w_fc1_shape

        linked2 = set()
        unfilterd_links = []
        for i, j in enumerate(w_fc1):

            for k in range(len(j)):
                if k in linked:
                    unfilterd_links.append([j[k], i / last_seq_length, k])
        rev_sort = sorted(unfilterd_links, reverse=True)
        print rev_sort[:5]
        if len(rev_sort) < conv_kernels[-1].shape[-1] / 10:
            _limit = len(rev_sort)
        else:
            _limit = conv_kernels[-1].shape[-1] / 10
        for m, i, k in rev_sort[:_limit]:
            x1, y1 = coordinates["hidden4"][i]

            cr.move_to(x1 + width * 0.005 + x_interval,
                       y1 + 0.001 * height / 2.0)
            cr.line_to(*new_fc2_coord[k])
            cr.set_source_rgba(0.0, 0.0, 1.0, 0.5)
            cr.set_line_width(1)
            cr.stroke()
            cr.move_to(x1 + width * 0.005 + x_interval,
                       y1 + 0.001 * height / 2.0)
            cr.line_to(x1 + width * 0.005, y1 + 0.001 * height / 2.0)
            cr.set_source_rgba(0.5, 0.5, 0.5, 0.5)
            cr.set_line_width(1)
            cr.stroke()
            linked2.add(i)

        linked_conv = []
        linked_conv.append(linked2)
        for i, conv in reversed(list(enumerate(conv_kernels_sum))):

            conv_shape = conv.shape
            print i, conv_shape
            convf = conv.flatten()
            convf = convf[(-convf).argsort()]
            # topN=convf[600]
            max_value = np.amax(conv)
            linked3 = set()
            unfilterd_links = []
            for j in range(conv_shape[1]):

                #if i==0:
                #k=kernel_shape_ic_list

                k = conv[:, j]
                #idx = (-k).argsort()[:10]
                #print k.shape
                #max_value=np.amax(k)

                for l, m in enumerate(k):
                    if j in linked_conv[-1]:
                        unfilterd_links.append([m, j, l])
            rev_sort = sorted(unfilterd_links, reverse=True)
            if len(rev_sort) < conv_shape[0] / 10:
                _limit = len(rev_sort)
            else:
                _limit = conv_shape[0] / 10
            for m, j, l in rev_sort[:_limit]:
                x1, y1 = coordinates["hidden" + str(i + 2)][j]
                cr.set_source_rgba(1, 0, 0, 1)
                cr.rectangle(x1, y1, nodew, nodeh)
                cr.fill()
                #if coordinates.has_key("hidden"+str(i)):
                x2, y2 = coordinates["hidden" + str(i + 1)][l]
                cr.set_source_rgba(1, 0, 0, 1)
                cr.rectangle(x2, y2, nodew, nodeh)
                cr.fill()
                cr.move_to(x1, y1 + 0.001 * height / 2.0)
                cr.line_to(x2 + width * 0.005, y2 + 0.001 * height / 2.0)
                cr.set_source_rgba(0, 0, 1.0, 0.5)
                cr.set_line_width(1)
                cr.stroke()
                linked3.add(l)
            if len(linked3) == 0:
                sys.exit("no link was found")
            linked_conv.append(linked3)
        prev_y1 = []
        for i in sorted(linked_conv[-1]):
            x1, y1 = coordinates["hidden1"][i]
            im = Image.open(png_list[i])
            xwidth = 128
            ywidth = int(im.size[1] * xwidth / float(im.size[0]))
            im = im.resize([xwidth, ywidth], Image.ANTIALIAS)
            _buffer = StringIO.StringIO()
            im.save(_buffer, format="PNG", quality=100)
            _buffer.seek(0)
            png_image = cairo.ImageSurface.create_from_png(_buffer)
            if not len(prev_y1) == 0 and (y1 - prev_y1[-1][1]) < (
                    ywidth - 10) and prev_y1[-1][0] == 0:
                a = xwidth - 20
            else:
                a = 0
            cr.set_source_surface(png_image, lateral_lim - xwidth - a + 10,
                                  y1 - ywidth / 2)
            cr.paint()
            prev_y1.append([a, y1])

        cr.show_page()
Beispiel #17
0
    def get_matrix_with_center(self,x,y,mode):
        m = cairo.Matrix()
        centerx =x
        centery = y
        m_archived = []
        
        for trans in self.stack: 
            if isinstance(trans, cairo.Matrix):
                # multiply matrix
                m *= trans
            elif isinstance(trans, tuple) and trans[0] in TRANSFORMS:
                # parse transform command
                cmd = trans[0]
                args = trans[1:]
                t = cairo.Matrix()
                
                if cmd == 'translate':                    
                    xt = args[0]
                    yt = args[1]
                    m.translate(xt,yt)
                elif cmd == 'rotate':
                    if mode == 'corner':                        
                        # apply existing transform to cornerpoint
                        deltax,deltay = m.transform_point(0,0)
                        a = args[0]
                        ct = cos(a)
                        st = sin(a)
                        m *= cairo.Matrix(ct, st, -st, ct,deltax-(ct*deltax)+(st*deltay),deltay-(st*deltax)-(ct*deltay)) 
                    elif mode == 'center':
                        # apply existing transform to centerpoint
                        deltax,deltay = m.transform_point(centerx,centery)
                        a = args[0]
                        ct = cos(a)
                        st = sin(a)
                        m *= cairo.Matrix(ct, st, -st, ct,deltax-(ct*deltax)+(st*deltay),deltay-(st*deltax)-(ct*deltay)) 
                elif cmd == 'scale':
                    if mode == 'corner':
                        t.scale(args[0], args[1])
                        m *= t
                    elif mode == 'center':
                        # apply existing transform to centerpoint
                        deltax,deltay = m.transform_point(centerx,centery)
                        x, y = args
                        m1 = cairo.Matrix()
                        m2 = cairo.Matrix()
                        m1.translate(-deltax, -deltay)
                        m2.translate(deltax, deltay)
                        m *= m1
                        m *= cairo.Matrix(x,0,0,y,0,0)
                        m *= m2

                elif cmd == 'skew':
                    if mode == 'corner':
                        x, y = args
                        ## TODO: x and y should be the tangent of an angle
                        t *= cairo.Matrix(1,0,x,1,0,0)
                        t *= cairo.Matrix(1,y,0,1,0,0)
                        m *= t
                    elif mode == 'center':
                        # apply existing transform to centerpoint
                        deltax,deltay = m.transform_point(centerx,centery)
                        x,y = args
                        m1 = cairo.Matrix()
                        m2 = cairo.Matrix()
                        m1.translate(-deltax, -deltay)
                        m2.translate(deltax, deltay)
                        t *= m
                        t *= m1
                        t *= cairo.Matrix(1,0,x,1,0,0)
                        t *= cairo.Matrix(1,y,0,1,0,0)
                        t *= m2
                        m = t
                elif cmd == 'push':
                    m_archived.append(m)
                elif cmd == 'pop':
                    m = m_archived.pop()

        return m