Exemplo n.º 1
0
def draw_r(regions, points, **kwargs):
    # initialize the environment
    from rpy2.interactive import process_revents
    from rpy2.robjects import r
    from rpy2.robjects.packages import importr
    NA = r("NA")[0]
    RGB = lambda rgb: r.rgb(*rgb, maxColorValue=256)
    C = lambda seq: r.c(*seq)
    OOB = 40
    graphics = importr("graphics")
    grDevices = importr("grDevices")
    process_revents.start()
    graphics.par(bg="white")
    graphics.split_screen(r.c(2, 1))
    graphics.split_screen(r.c(1, 2), screen=2)
    graphics.screen(1)
    # prepare the regions for plotting
    ul, lr = regions.box()
    xlim = r.c(ul[0], lr[0])
    ylim = r.c(lr[1], ul[1])
    # create the main plot window
    graphics.plot(r.c(), r.c(), main=regions.name(), type="p", pch="+",
            xlim=xlim, ylim=ylim, xlab="", ylab="",
            xaxp=r.c(0, lr[0], lr[0]/200), yaxp=r.c(0, lr[1], lr[1]/200),
            bg="white")
    # plot the polygons in the order given
    order = sorted(regions.polys(), key=lambda p: p.area, reverse=True)
    for poly in order:
        xs, ys = zip(*poly.boundary[0].coords)
        color = regions.color(poly.name(), default=NA)
        cr, cg, cb = r.col2rgb(color)
        rgb = r.rgb(cr, cg, cb, alpha=128, maxColorValue=255)
        graphics.polygon(C(xs), C(ys), col=rgb)
    # plot the grid
    graphics.abline(v=r.c(OOB, lr[0]-OOB), lty=2)
    graphics.abline(h=r.seq(0, lr[1], 200), col="lightgray", lty=2)
    graphics.abline(v=r.seq(0, lr[0], 200), col="lightgray", lty=2)
    # plot the points
    xs, ys, names = zip(*[(pt[0].x, pt[0].y, pt[1]) for pt in points])
    colors = [RGB(points.Color(name)) for name in names]
    graphics.points(C(xs), C(ys), xlab="", ylab="", pch="+", col=C(colors))
    # save as a png
    if "png" in kwargs and kwargs['png']:
        grDevices.dev_print(grDevices.png, file=kwargs['png'], width=lr[0],
                height=lr[1])
    # derive legend contents: colors, counts, names
    tid_counts = {}
    uniq_tids = []
    for n in names:
        if n not in uniq_tids:
            tid_counts[n] = 0
            uniq_tids.append(n)
        tid_counts[n] += 1
    uniq_colors = [RGB(points.Color(tid)) for tid in uniq_tids]
    uniq_names = [("%d\t%s" % (i, IDs.TileID[i])) for i in uniq_tids]
    name_counts = [("%d\t%s: %d" % (k, IDs.TileID[k], v)) for (k,v) in \
            tid_counts.items()]
    # display the colors legend
    legend_args = dict(y_intersp=0.7, cex=0.7)
    graphics.screen(3)
    graphics.legend("center", title="Tile Colors", legend=C(uniq_names),
            col=C(uniq_colors), pch="+", pt_cex=1, **legend_args)
    # display the counts legend
    graphics.screen(4)
    graphics.legend("center", title="Tile Counts", legend=C(name_counts),
            **legend_args)
    # sleep until the window is closed
    while grDevices.dev_list() != r("NULL"):
        time.sleep(0.1)
Exemplo n.º 2
0
Arquivo: test.py Projeto: hphp/Kaggle
import math, datetime
import time
import rpy2.robjects.lib.ggplot2 as ggplot2
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.interactive import process_revents
grdevices = importr('grDevices')
process_revents.start()

base = importr('base')
datasets = importr('datasets')

mtcars = datasets.__rdata__.fetch('mtcars')['mtcars']
pp = ggplot2.ggplot(mtcars) + ggplot2.aes_string(
    x='wt', y='mpg',
    col='factor(cyl)') + ggplot2.geom_point() + ggplot2.geom_smooth(
        ggplot2.aes_string(group='cyl'), method='lm')
#pp.plot()
#process_revents.start()
print(pp)
process_revents.process_revents()

while True:
    time.sleep(1)
process_revents.stop()
Exemplo n.º 3
0
Arquivo: test.py Projeto: hphp/Kaggle
import math, datetime
import time
import rpy2.robjects.lib.ggplot2 as ggplot2
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.interactive import process_revents
grdevices = importr('grDevices')
process_revents.start()

base = importr('base')
datasets= importr('datasets')

mtcars = datasets.__rdata__.fetch('mtcars')['mtcars']
pp = ggplot2.ggplot(mtcars) +  ggplot2.aes_string(x='wt', y='mpg', col='factor(cyl)') +  ggplot2.geom_point() +  ggplot2.geom_smooth(ggplot2.aes_string(group = 'cyl'), method = 'lm') 
#pp.plot()
#process_revents.start()
print(pp)
process_revents.process_revents()

while True:
    time.sleep(1)
process_revents.stop()
Exemplo n.º 4
0
 def initInteractive(self):
     # This allows graphics windows to be resized
     from rpy2.interactive import process_revents
     process_revents.start()