コード例 #1
0
ファイル: info.py プロジェクト: JohanComparat/pyLPT
def getParamCovMat(prefix,dlogpower = 2, theoconstmult = 1.,dlogfilenames = ['dlogpnldloga.dat'],volume=256.**3,startki = 0, endki = 0, veff = [0.]):
    """
    Calculates parameter covariance matrix from the power spectrum covariance matrix and derivative term
    in the prefix directory
    """
    nparams = len(dlogfilenames)

    kpnl = M.load(prefix+'pnl.dat')
    k = kpnl[startki:,0]

    nk = len(k)
    if (endki == 0):
        endki = nk
        
    pnl = M.array(kpnl[startki:,1],M.Float64)
    covarwhole = M.load(prefix+'covar.dat')
    covar = covarwhole[startki:,startki:]
    if len(veff) > 1:
        sqrt_veff = M.sqrt(veff[startki:])
    else:
        sqrt_veff = M.sqrt(volume*M.ones(nk))

    dlogs = M.reshape(M.ones(nparams*nk,M.Float64),(nparams,nk))
    paramFishMat = M.reshape(M.zeros(nparams*nparams*(endki-startki),M.Float64),(nparams,nparams,endki-startki))
    paramCovMat = paramFishMat * 0.

    # Covariance matrices of dlog's
    for param in range(nparams):
        if len(dlogfilenames[param]) > 0:
            dlogs[param,:] = M.load(prefix+dlogfilenames[param])[startki:,1]

    normcovar = M.zeros(M.shape(covar),M.Float64)
    for i in range(nk):
        normcovar[i,:] = covar[i,:]/(pnl*pnl[i])

    M.save(prefix+'normcovar.dat',normcovar)

    f = k[1]/k[0]

    if (volume == -1.):
        volume = (M.pi/k[0])**3

    #theoconst = volume * k[1]**3 * f**(-1.5)/(12.*M.pi**2) #1 not 0 since we're starting at 1
    for ki in range(1,endki-startki):
        for p1 in range(nparams):
            for p2 in range(nparams):
                paramFishMat[p1,p2,ki] = M.sum(M.sum(\
                M.inverse(normcovar[:ki+1,:ki+1]) *
                M.outerproduct(dlogs[p1,:ki+1]*sqrt_veff[:ki+1],\
                               dlogs[p2,:ki+1]*sqrt_veff[:ki+1])))
                
                
        paramCovMat[:,:,ki] = M.inverse(paramFishMat[:,:,ki])

    return k[1:],paramCovMat[:,:,1:]
コード例 #2
0
ファイル: __init__.py プロジェクト: giserh/ascgrid
 def ListColorMaps(self):
     """Utility function to display matplotlib color maps as an image"""
     p.rc('text', usetex=False)
     a=p.outerproduct(numpy.arange(0,1,0.01),numpy.ones(10))
     p.figure(figsize=(10,5))
     p.subplots_adjust(top=0.8,bottom=0.05,left=0.01,right=0.99)
     maps=[m for m in p.cm.datad.keys() if not m.endswith("_r")]
     maps.sort()
     l=len(maps)+1
     i=1
     for m in maps:
         p.subplot(1,l,i)
         p.axis("off")
         p.imshow(a,aspect='auto',cmap=p.get_cmap(m),origin="lower")
         p.title(m,rotation=90,fontsize=10)
         i=i+1
     #savefig("colormaps.png",dpi=100,facecolor='gray')
     p.show()
コード例 #3
0
 def ListColorMaps(self):
     """Utility function to display matplotlib color maps as an image"""
     p.rc('text', usetex=False)
     a = p.outerproduct(numpy.arange(0, 1, 0.01), numpy.ones(10))
     p.figure(figsize=(10, 5))
     p.subplots_adjust(top=0.8, bottom=0.05, left=0.01, right=0.99)
     maps = [m for m in p.cm.datad.keys() if not m.endswith("_r")]
     maps.sort()
     l = len(maps) + 1
     i = 1
     for m in maps:
         p.subplot(1, l, i)
         p.axis("off")
         p.imshow(a, aspect='auto', cmap=p.get_cmap(m), origin="lower")
         p.title(m, rotation=90, fontsize=10)
         i = i + 1
     #savefig("colormaps.png",dpi=100,facecolor='gray')
     p.show()
コード例 #4
0
ファイル: graph_links2gdl.py プロジェクト: CGATOxford/Optic
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv is None:
        argv = sys.argv

    parser = E.OptionParser(
        version="%prog version: $Id$",
        usage=globals()["__doc__"])

    parser.add_option(
        "-c", "--components-tsv-file", dest="filename_components", type="string",
        help="filename with component information.")
    parser.add_option("-o", "--component", dest="component", type="string",
                      help="restrict output to component.")
    parser.add_option("-s", "--filename-node-colours", dest="filename_node_colours", type="string",
                      help="filename with node colours.")
    parser.add_option("-l", "--filename-node-labels", dest="filename_node_labels", type="string",
                      help="filename with node labels.")
    parser.add_option("-w", "--weights-tsv-file", dest="weights", action="store_true",
                      help="use weights.")
    parser.add_option("--edge-colour", dest="edge_colour", type="choice",
                      choices=("heat", "jet", ),
                      help="colour edges.")
    parser.add_option("--format-node", dest="filename_node_format", type="string",
                      help="filename with node formats.")
    parser.add_option("--format-edge", dest="filename_edge_format", type="string",
                      help="filename with edge formats.")
    parser.add_option("--format-graph", dest="filename_graph_format", type="string",
                      help="filename with graph format.")
    parser.add_option("--no-titles", dest="titles", action="store_false",
                      help="do not output titles.")
    parser.add_option("--weight-range", dest="weight_range", type="string",
                      help="weight range. Comma separated values or 'auto'")

    parser.add_option("--add-edge-labels", dest="add_edge_labels", action="store_true",
                      help="add edge labels to edges.")

    parser.set_defaults(
        min_weight=0.0,
        max_weight=1.0,
        num_colours=100,
        min_colour=16,
        filename_components=None,
        component=None,
        filename_subset=None,
        colours=None,
        filename_labels=None,
        weights=False,
        edge_colour=None,
        default_edge_colour="18",
        filename_format_graph=None,
        filename_format_edge=None,
        filename_format_node=None,
        filename_format_bipartite=None,
        titles=True,
        edge_labels=True,
        column_edge_weight=3,
        column_edge_colour=3,
        weight_range="auto",
        add_edge_labels=False,
    )

    (options, args) = E.Start(parser)

    options.column_edge_weight -= 1
    options.column_edge_colour -= 1

    components = None
    # read components
    if options.filename_components:
        lines = open(options.filename_components, "r").readlines()
        components = {}
        for line in lines:
            id, cid = string.split(line[:-1], "\t")
            components[id] = cid

    if options.filename_subset:
        lines = open(options.filename_subset, "r").readlines()
        subset = {}
        for line in lines:
            id = string.split(line[:-1], "\t")[0]
            subset[id] = 1

    colours = {}
    if options.filename_node_colours:
        infile = open(options.filename_node_colours, "r")
        while 1:
            line = infile.readline()
            if not line:
                break
            if line[0] == "#":
                continue
            id, colour = string.split(line[:-1], "\t")[:2]
            colours[id] = colour
        infile.close()

    labels = {}
    if options.filename_labels:
        infile = open(options.filename_labels, "r")
        while 1:
            line = infile.readline()
            if not line:
                break
            if line[0] == "#":
                continue
            id, label = string.split(line[:-1], "\t")[:2]
            if id in labels:
                labels[id] += "," + label
            else:
                labels[id] = label

        infile.close()

    if options.weight_range == "auto":
        lines = sys.stdin.readlines()
        mi, ma = None, None

        for line in lines:

            if line[0] == "#":
                continue
            try:
                v = float(line[:-1].split("\t")[2])
            except ValueError:
                continue
            except IndexError:
                continue

            if mi is None:
                mi = v
            else:
                mi = min(v, mi)
            if ma is None:
                ma = v
            else:
                ma = max(v, ma)

        options.min_weight = mi
        options.max_weight = ma
        if options.loglevel >= 1:
            options.stdlog.write("# using automatic weight range from %f to %f\n" % (
                options.min_weight, options.max_weight))
    else:
        lines = sys.stdin
        options.min_weight, options.max_weight = map(
            float, options.weight_range.split(","))

    options.stdout.write("graph: {\n")

    if options.filename_format_graph:
        options.stdout.write(
            string.join(open(options.filename_format_graph, "r").readlines()))
    else:
        options.stdout.write(FORMAT_GRAPH)

        if options.add_edge_labels:
            options.stdout.write("display_edge_labels: yes\n")

    left_nodes = {}
    right_nodes = {}
    touched = {}        # remove repeats (several links between the same nids)

    if options.edge_colour:
        import matplotlib
        import pylab

        colour_map = pylab.get_cmap(options.edge_colour)

        if options.edge_colour == "jet":

            # for some reason I had problems with aisee colour indices > 128. Thus,
            # I suggest staying below 128.
            step_size = (options.max_weight - options.min_weight) / \
                float(options.num_colours + 1)
            increment = 1.0 / float(options.num_colours + 1)
            v, vv = options.min_weight, 0.0
            for x in range(options.min_colour, options.min_colour + options.num_colours + 1):
                rgba = colour_map(vv)
                r, g, b = map(lambda x: int(x * 255.0), rgba[:3])
                options.stdout.write("         colorentry %i: %03i %03i %03i // weight = %f\n" %
                                     (x, r, g, b, v))
                v += step_size
                vv += increment

            colour_scale = options.num_colours / \
                (options.max_weight - options.min_weight)

            def calc_colour_index(x):
                v = min(options.max_weight, float(x))
                v = max(options.min_weight, v) - options.min_weight
                v = min(options.num_colours - 1, int(v * colour_scale)) + \
                    options.min_colour
                return str(v)

            colour_conversion = calc_colour_index

            # save legend
            a = pylab.outerproduct(
                pylab.arange(options.min_weight, options.max_weight, step_size), 1)
            pylab.figure(figsize=(10, 5))
            pylab.imshow(a, cmap=colour_map, origin="lower")
            pylab.colorbar()
            pylab.savefig("legend.png", dpi=100, facecolor='gray')
        else:
            raise "unknown colour scheme", options.colour_scheme

    if options.filename_format_edge:
        options.stdout.write(
            string.join(open(options.filename_format_edge, "r").readlines()))
    else:
        options.stdout.write(FORMAT_EDGE)

    first = True
    for line in lines:

        if line[0] == "#":
            continue

        if first:
            first = False
            if options.titles:
                continue

        try:
            weight = 1.0
            colour = options.default_edge_colour

            x = string.split(line[:-1], "\t")

            id1, id2 = x[:2]

            if options.edge_colour:
                colour = colour_conversion(x[options.column_edge_colour])
            if options.edge_labels:
                weight = x[options.column_edge_weight]

        except ValueError:
            continue

        # patch for PFAM domains
        # id1, id2 = map(string.atoi, string.split(line[:-1], "\t")[:2])

        if id1 == id2:
            continue

        if options.filename_subset:
            if id1 not in subset or id2 not in subset:
                continue

        if options.component is not None:
            if id1 not in components:
                continue
            if id2 not in components:
                continue
            if components[id1] != options.component or components[id2] != options.component:
                continue

        if options.filename_format_bipartite:
            left_nodes[id1] = 1
            right_nodes[id2] = 1
        else:
            left_nodes[id1] = 1
            left_nodes[id2] = 1

        if id1 < id2:
            key = "%s-%s" % (id1, id2)
        else:
            key = "%s-%s" % (id2, id1)

        if key in touched:
            continue
        touched[key] = 1

        edge_attributes = ['color: %s' % colour]
        if options.add_edge_labels:
            edge_attributes.append('label: "%s"' % weight)

        options.stdout.write('\tedge: { thickness: 3 sourcename: "%s" targetname: "%s" %s}\n' % (
            id1, id2, " ".join(edge_attributes)))

    # sort nodes according to key

    if options.filename_format_node:
        options.stdout.write(
            "".join(open(options.filename_format_node, "r").readlines()))
    else:
        options.stdout.write(FORMAT_NODE)

    PrintNodes(left_nodes, labels, colours)

    if options.filename_format_bipartite:
        if options.format_bipartite == "default":
            options.stdout.write(FORMAT_BIPARTITE)
        else:
            options.stdout.write(
                "".join(open(options.filename_format_bipartite, "r").readlines()))

        PrintNodes(right_nodes, labels, colours)

    options.stdout.write("}\n")

    E.Stop()
コード例 #5
0
ファイル: graph_links2gdl.py プロジェクト: yangjl/cgat
def main( argv = None ):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv == None: argv = sys.argv

    #--------------------------------------------------------
    # command line parsing options
    parser = E.OptionParser( version = "%prog version: $Id: graph_links2gdl.py 2782 2009-09-10 11:40:29Z andreas $", usage = globals()["__doc__"])

    parser.add_option("-c", "--filename-components", dest="filename_components", type="string",
                      help="filename with component information." )
    parser.add_option("-o", "--component", dest="component", type="string",
                      help="restrict output to component." )
    parser.add_option("-s", "--filename-node-colours", dest="filename_node_colours", type="string",
                      help="filename with node colours." )
    parser.add_option("-l", "--filename-node-labels", dest="filename_node_labels", type="string",
                      help="filename with node labels." )
    parser.add_option("-w", "--weights", dest="weights", action="store_true",
                      help="use weights." )
    parser.add_option( "--edge-colour", dest="edge_colour", type="choice",
                       choices=("heat", "jet", ),
                       help="colour edges." )
    parser.add_option( "--format-node", dest="filename_node_format", type="string",
                       help="filename with node formats." )
    parser.add_option( "--format-edge", dest="filename_edge_format", type="string",
                       help="filename with edge formats." )
    parser.add_option( "--format-graph", dest="filename_graph_format", type="string",
                       help="filename with graph format." )
    parser.add_option( "--no-titles", dest="titles", action="store_false",
                       help="do not output titles." )
    parser.add_option( "--weight-range", dest="weight_range", type="string",
                       help="weight range. Comma separated values or 'auto'")

    parser.add_option( "--add-edge-labels", dest="add_edge_labels", action="store_true",
                       help="add edge labels to edges." )
                       
    parser.set_defaults(
        min_weight = 0.0,
        max_weight = 1.0,
        num_colours = 100,
        min_colour = 16,
        filename_components = None,
        component = None,
        filename_subset = None,
        colours = None,
        filename_labels  = None,
        weights = False,
        edge_colour = None,
        default_edge_colour = "18",
        filename_format_graph = None,
        filename_format_edge = None,
        filename_format_node = None,
        filename_format_bipartite = None,
        titles = True,
        edge_labels = True,
        column_edge_weight = 3,
        column_edge_colour = 3,
        weight_range = "auto",
        add_edge_labels = False,
        )

    (options, args) = E.Start( parser )

    options.column_edge_weight -= 1
    options.column_edge_colour -= 1    

    components = None
    ## read components
    if options.filename_components:
        lines = open(options.filename_components,"r").readlines()
        components = {}
        for line in lines:
            id, cid = string.split(line[:-1], "\t")
            components[id] = cid

    if options.filename_subset:
        lines = open(options.filename_subset,"r").readlines()        
        subset= {}
        for line in lines:
            id = string.split(line[:-1], "\t")[0]
            subset[id] = 1
        
    colours = {}            
    if options.filename_node_colours:
        infile = open(options.filename_node_colours, "r")
        while 1:
            line = infile.readline()
            if not line: break
            if line[0] == "#": continue
            id, colour = string.split(line[:-1], "\t")[:2]
            colours[id] = colour
        infile.close()
    
    labels = {}
    if options.filename_labels:
        infile = open(options.filename_labels, "r")
        while 1:
            line = infile.readline()
            if not line: break
            if line[0] == "#": continue            
            id, label = string.split(line[:-1], "\t")[:2]
            if labels.has_key(id):
                labels[id] += "," + label
            else:
                labels[id] = label
                
        infile.close()

    if options.weight_range == "auto":
        lines = sys.stdin.readlines()
        mi, ma = None, None

        for line in lines:

            if line[0] == "#": continue
            try:
                v = float(line[:-1].split("\t")[2])
            except ValueError:
                continue
            except IndexError:
                continue
            
            if mi == None: 
                mi = v
            else:
                mi = min(v, mi)
            if ma == None:
                ma = v
            else:
                ma = max(v, ma)

        options.min_weight = mi
        options.max_weight = ma
        if options.loglevel >= 1:
            options.stdlog.write("# using automatic weight range from %f to %f\n" % (options.min_weight, options.max_weight) )
    else:
        lines = sys.stdin
        options.min_weight, options.max_weight = map(float, options.weight_range.split(","))

    options.stdout.write( "graph: {\n" )
    
    if options.filename_format_graph:
        options.stdout.write( string.join(open(options.filename_format_graph, "r").readlines() ) )
    else:
        options.stdout.write( FORMAT_GRAPH )

        if options.add_edge_labels:
            options.stdout.write( "display_edge_labels: yes\n" )
        
    left_nodes = {}
    right_nodes = {}
    touched = {}        # remove repeats (several links between the same nids)

    if options.edge_colour :
        import matplotlib
        import pylab

        colour_map = pylab.get_cmap( options.edge_colour )
        
        if options.edge_colour == "jet":
            
            # for some reason I had problems with aisee colour indices > 128. Thus,
            # I suggest staying below 128.
            step_size = (options.max_weight - options.min_weight) / float(options.num_colours+1)
            increment = 1.0 / float(options.num_colours+1)
            v, vv = options.min_weight, 0.0
            for x in range( options.min_colour, options.min_colour + options.num_colours + 1):
                rgba = colour_map( vv )
                r, g, b = map( lambda x: int(x * 255.0), rgba[:3] )
                options.stdout.write( "         colorentry %i: %03i %03i %03i // weight = %f\n" %\
                                          (x,r,g,b,v ) )
                v += step_size
                vv += increment

            colour_scale = options.num_colours / (options.max_weight - options.min_weight ) 
            def calc_colour_index( x ):
                v = min( options.max_weight, float(x))
                v = max( options.min_weight, v ) - options.min_weight
                v = min( options.num_colours - 1, int( v * colour_scale )) + options.min_colour
                return str( v )

            colour_conversion = calc_colour_index

            # save legend
            a=pylab.outerproduct(pylab.arange(options.min_weight,options.max_weight,step_size),1)
            pylab.figure(figsize=(10,5))
            pylab.imshow(a,cmap=colour_map,origin="lower")
            pylab.colorbar()
            pylab.savefig("legend.png",dpi=100,facecolor='gray')
        else:
            raise "unknown colour scheme", options.colour_scheme


    if options.filename_format_edge:
        options.stdout.write( string.join(open(options.filename_format_edge, "r").readlines()) )
    else:
        options.stdout.write( FORMAT_EDGE )

    first = True
    for line in lines:

        if line[0] == "#": continue

        if first:
            first = False
            if options.titles: continue

        try:
            weight = 1.0
            colour = options.default_edge_colour
            
            x = string.split(line[:-1], "\t")

            id1, id2 = x[:2]
            
            if options.edge_colour:
                colour = colour_conversion(x[options.column_edge_colour])
            if options.edge_labels:
                weight = x[options.column_edge_weight]

        except ValueError:
            continue

        ## patch for PFAM domains
        ## id1, id2 = map(string.atoi, string.split(line[:-1], "\t")[:2])

        if id1 == id2: continue
        
        if options.filename_subset:
            if not subset.has_key(id1) or not subset.has_key(id2):
                continue

        if options.component != None:
            if not components.has_key(id1):
                continue
            if not components.has_key(id2):
                continue
            if components[id1] != options.component or components[id2] != options.component:
                continue

        if options.filename_format_bipartite:
            left_nodes[id1] = 1
            right_nodes[id2] = 1
        else:
            left_nodes[id1] = 1
            left_nodes[id2] = 1
            
        if id1 < id2:
            key = "%s-%s" % (id1,id2)
        else:
            key = "%s-%s" % (id2,id1)          
  
        if touched.has_key(key):
            continue
        touched[key] = 1
            
        edge_attributes = [ 'color: %s' % colour ]
        if options.add_edge_labels:
            edge_attributes.append( 'label: "%s"' % weight )

        options.stdout.write('\tedge: { thickness: 3 sourcename: "%s" targetname: "%s" %s}\n' % (id1, id2, " ".join(edge_attributes)) )

    # sort nodes according to key

    if options.filename_format_node:
        options.stdout.write( "".join(open(options.filename_format_node, "r").readlines()))
    else:
        options.stdout.write( FORMAT_NODE )

    PrintNodes( left_nodes, labels, colours)

    if options.filename_format_bipartite:
        if options.format_bipartite == "default":
            options.stdout.write( FORMAT_BIPARTITE )
        else:
            options.stdout.write( "".join(open(options.filename_format_bipartite, "r").readlines()))
            
        PrintNodes( right_nodes, labels, colours)
            
    options.stdout.write ("}\n" )
    
    E.Stop()
コード例 #6
0
ファイル: graph_links2gdl.py プロジェクト: siping/cgat
                options.stdout.write( "         colorentry %i: %03i %03i %03i // weight = %f\n" %\
                                          (x,r,g,b,v ) )
                v += step_size
                vv += increment

            colour_scale = options.num_colours / (options.max_weight - options.min_weight ) 
            def calc_colour_index( x ):
                v = min( options.max_weight, float(x))
                v = max( options.min_weight, v ) - options.min_weight
                v = min( options.num_colours - 1, int( v * colour_scale )) + options.min_colour
                return str( v )

            colour_conversion = calc_colour_index

            # save legend
            a=pylab.outerproduct(pylab.arange(options.min_weight,options.max_weight,step_size),1)
            pylab.figure(figsize=(10,5))
            pylab.imshow(a,cmap=colour_map,origin="lower")
            pylab.colorbar()
            pylab.savefig("legend.png",dpi=100,facecolor='gray')
        else:
            raise "unknown colour scheme", options.colour_scheme


    if options.filename_format_edge:
        options.stdout.write( string.join(open(options.filename_format_edge, "r").readlines()) )
    else:
        options.stdout.write( FORMAT_EDGE )

    first = True
    for line in lines: