Example #1
0
    def diagnostics(self, fn=None):
        """
        Plot diagnostics for the regression.  If `fn` is provided, then save
        the results to file.  The filetype to be saved is determined by the
        extension.

        Additional kwargs are passed to the saving function (e.g., width=10)
        """
        d = {
                '.pdf': grdevices.pdf,
                '.png': grdevices.png
            }

        if fn:
            ext = os.path.splitext(fn)[1]
            try:
                saver_func = d[ext]
            except KeyError:
                raise ValueError('extension "%s" not supported, '
                        'please use one of %s' % (ext, d.keys()))
            saver_func(file=fn)

        r.layout(r.matrix([1, 2, 3, 4], 2, 2))
        r.plot(self.lm)

        if fn:
            rclose()
        return
 def __call__(self, track, slice = None):
     s = [random.randint(0,20) for x in range(40)]
     random.shuffle( s )
     # do the plotting
     R.x11()
     R.plot( s, s )
     return odict( (("text", "#$rpl %i$#" % getCurrentRDevice()),) )
Example #3
0
def plot_matrix_as_image(mat,
                         x1=None,
                         y1=None,
                         x2=None,
                         y2=None,
                         maxval=None,
                         main="",
                         xlab="",
                         ylab=""):
    """ adds the image to the current plot if x1, x2, y1 and y2 are defined;
    otherwise, create a new image with the dimensions of the matrix """
    from rpy2.robjects import r

    r.plot(numpy.array([0]),
           xlim=numpy.array([x1, x2]),
           ylim=numpy.array([y1, y2]),
           type="n",
           bty="n",
           main=main,
           xlab=xlab,
           ylab=ylab)

    if maxval is None:
        maxval = mat.max()
    r.rasterImage(r["as.raster"](mat, max=maxval), x1, y1, x2, y2)
def do_km(name, time, censor, split, outdir):
    """Given three clean (pre-processed) lists, make a kmplot of the data, and save it to outdir"""
    data = {
        'time': robjects.IntVector(np.array(time)),
        'censor': robjects.IntVector(np.array(censor)),
        'split': robjects.IntVector(np.array(split))
    }
    df = robjects.DataFrame(data)

    surv = importr('survival')
    grdevices = importr('grDevices')
    km = surv.survfit(robjects.Formula('Surv(time, censor) ~ split'), data=df)
    grdevices.png(file=os.path.join(outdir, name + '_km.png'),
                  width=512,
                  height=512)

    r.plot(km,
           xlab='Time',
           ylab='Cumulative Hazard',
           col=robjects.StrVector(['Red', 'Blue']))
    r.legend(1000,
             1,
             robjects.StrVector(['<= Mean', '> Mean']),
             lty=robjects.IntVector([1, 1]),
             col=robjects.StrVector(['Red', 'Blue']))
    grdevices.dev_off()
Example #5
0
def plot_scatter(target, source, env):
    """
    """
    mat = npz_to_df(source[0].rstr())
    module["grDevices"].png(target[0].rstr())
    r.plot(mat)
    module["grDevices"].dev_off()
    return None
Example #6
0
def main():

    in_fname = sys.argv[1]
    out_fname = sys.argv[2]
    try:
        columns = int( sys.argv[3] ) - 1, int( sys.argv[4] ) - 1
    except:
        stop_err( "Columns not specified, your query does not contain a column of numerical data." )
    title = sys.argv[5]
    xlab = sys.argv[6]
    ylab = sys.argv[7]

    matrix = []
    skipped_lines = 0
    first_invalid_line = 0
    invalid_value = ''
    invalid_column = 0
    i = 0
    for i, line in enumerate( file( in_fname ) ):
        valid = True
        line = line.rstrip( '\r\n' )
        if line and not line.startswith( '#' ): 
            row = []
            fields = line.split( "\t" )
            for column in columns:
                try:
                    val = fields[column]
                    if val.lower() == "na": 
                        row.append( float( "nan" ) )
                    else:
                        row.append( float( fields[column] ) )
                except:
                    valid = False
                    skipped_lines += 1
                    if not first_invalid_line:
                        first_invalid_line = i + 1
                        try:
                            invalid_value = fields[column]
                        except:
                            invalid_value = ''
                        invalid_column = column + 1
                    break
        else:
            valid = False
            skipped_lines += 1
            if not first_invalid_line:
                first_invalid_line = i+1

        if valid:
            matrix.append( row )

    if skipped_lines < i:
        try:
            r.pdf( out_fname, 8, 8 )
            r.plot( array(matrix), type="p", main=title, xlab=xlab, ylab=ylab, col="blue", pch=19 )
            r['dev.off']()
        except Exception, exc:
            stop_err( "%s" %str( exc ) )
Example #7
0
def r_plot(obj, fn, w=800, h=800, xlabel="", ylabel="", label=""):
    from rpy2.robjects.packages import importr
    from rpy2.robjects import r
    grdevices = importr('grDevices')
    grdevices.png(file=fn, width=w, height=h)

    r.plot(obj)

    grdevices.dev_off()
    print ">> saved: " + fn
Example #8
0
def r_plot(obj,fn,w=800,h=800,xlabel="",ylabel="",label=""):
	from rpy2.robjects.packages import importr
	from rpy2.robjects import r
	grdevices = importr('grDevices')
	grdevices.png(file=fn, width=w, height=h)

	r.plot(obj)

	grdevices.dev_off()
	print ">> saved: "+fn
Example #9
0
    def __call__(self, track, slice=None):
        if not R:
            return

        s = [random.randint(0, 20) for x in range(40)]
        random.shuffle(s)
        # do the plotting
        R.x11()
        R.plot(s, s)

        return {"text": "#$rpl %i$#" % R["dev.cur"]()[0]}
Example #10
0
def plotASProfile(tcc, cName, directory = None, min = 0, extra = "0"):
	if not directory:
		fN = extra + '.' + tcc + '.png'
	else:
		fN = directory + '/' + extra + '.' + tcc + '.png'
	
	#Get S Profile
	tccStretch = cgPeaks.stretch(tcc, cName)
	highest = tccStretch.getHighestLevel()
	if highest < min:
		return 0
		
	sortedX = tccStretch.profile.keys()
	sortedX.sort()
	
	sortedY = []
	for X in sortedX:
		sortedY.append(tccStretch.profile[X])
	
	#Get AS Profile
	chr, strand, start, end = tcc.strip().split(':')
	if strand == '1':
		strand = '-1'
	else:
		strand = '1'
	tcc = cg.makeTcc(chr, strand, start, end)
	
	tccStretchAS = cgPeaks.stretch(tcc, cName)
	highest = tccStretchAS.getHighestLevel()
	if highest < min:
		return 0 #AS can have minimum I guess...
		
	sortedXAS = tccStretchAS.profile.keys()
	sortedXAS.sort()
	
	sortedYAS = []
	for X in sortedXAS:
		sortedYAS.append(tccStretchAS.profile[X])
	
	#Plot them
	gDevice = importr('grDevices')
	gDevice.png(file=fN, width=1680, height=1050)
	r('split.screen(c(2,1))')
	r('screen(1)')
	r.plot(sortedX, sortedY, xlab = "Coordinates", ylab = "(Syn) Expression Level" )
	r.lines(sortedX, sortedY, type = "b")
	r('screen(2)')
	r.plot(sortedXAS, sortedYAS, xlab = "Coordinates", ylab = "(Anti) Expression Level")
	r.lines(sortedXAS, sortedYAS, type = "b")
	gDevice.dev_off()
Example #11
0
 def plot_me(sub_f, label):
     if (get_vec_type(sub_f) == 'real') and (len(sub_f.unique()) > 10):
         sub_f = to_quants(sub_f, q=q, std=std)
         
     m = get_cox_ph(surv, sub_f, formula=fmla)
     r_data = m.rx2('call')[2]
     p = log_rank(sub_f, surv)['p']
     ls = r.c(*colors)
     
     r.plot(survival.survfit(fmla, r_data), lty=1, col=ls, lwd=4, cex=1.25,
                             xlab='Years to Event', ylab='Survival');
     r.title(label, cex=3.)
     if ann == 'p':
         r.text(.2, 0, labels='logrank p = {0:.1e}'.format(p), pos=4)
     elif ann != None:
         r.text(0, labels=ann, pos=4)
def clusterize_r_em(*args):
    """ Clustering and plotting with EM GMM"""
    try:
        from rpy2.robjects import r
        import rpy2.robjects.numpy2ri
        rpy2.robjects.numpy2ri.activate()
    except:
        print "You need rpy2"
        sys.exit(-1)

    r.library("mclust")
    for arg in args:
        model = r.Mclust(arg)
        print model
        print r.summary(model)
        r.quartz("plot")
        r.plot(model, arg)
        print raw_input("any key to pass")
def clusterize_r_em(*args):
    """ Clustering and plotting with EM GMM"""
    try:
        from rpy2.robjects import r
        import rpy2.robjects.numpy2ri
        rpy2.robjects.numpy2ri.activate()
    except:
        print "You need rpy2"
        sys.exit(-1)

    r.library("mclust")
    for arg in args:
        model = r.Mclust(arg)
        print model
        print r.summary(model)
        r.quartz("plot")
        r.plot(model, arg)
        print raw_input("any key to pass")
Example #14
0
    def render(self, work, path ):
        
        # R.graphics_off()
        self.legend = []
        self.xlabels = []

        nplotted = 0

        for line, data in work.iteritems():

            for label, coords in data.iteritems():
                
                # sanity check on data
                try: keys = coords.keys()
                except AttributeError:
                    self.warn("could not plot %s - coords is not a dict: %s" % (label, str(coords) ))
                    continue
                
                if len(keys) <= 1:
                    self.warn("could not plot %s: not enough columns: %s" % (label, str(coords) ))
                    continue

                xlabel, ylabels = self.initCoords( label, coords)
                
                xvals = coords[xlabel]
                for ylabel in ylabels:
                    yvals = coords[ylabel]
                    R.plot( xvals, yvals )
                    # self.addData( line, label, xlabel, ylabel, xvals, yvals, nplotted )
                    nplotted += 1

                    if len(ylabels) > 1:
                        self.legend.append( "/".join((line,label,ylabel) ))
                    else:
                        self.legend.append( "/".join((line,label) ))
                                
                self.xlabels.append(xlabel)

        figid = getCurrentRDevice()
        blocks = ResultBlocks( ResultBlock( "\n".join( ("#$rpl %i$#" % (figid), "")), 
                                            title = "/".join(path) ) )

        return blocks
Example #15
0
    def plot_me(sub_f, label):
        if (get_vec_type(sub_f) == 'real') and (len(sub_f.unique()) > 10):
            sub_f = to_quants(sub_f, q=q, std=std)

        m = get_cox_ph(surv, sub_f, formula=fmla)
        r_data = m.rx2('call')[2]
        p = log_rank(sub_f, surv)['p']
        ls = r.c(*colors)

        r.plot(survival.survfit(fmla, r_data),
               lty=1,
               col=ls,
               lwd=4,
               cex=1.25,
               xlab='Years to Event',
               ylab='Survival')
        r.title(label, cex=3.)
        if ann == 'p':
            r.text(.2, 0, labels='logrank p = {0:.1e}'.format(p), pos=4)
        elif ann != None:
            r.text(0, labels=ann, pos=4)
Example #16
0
    def render(self, dataframe, path):

        fig = self.startPlot()

        labels = dataframe.index.levels

        paths = list(itertools.product(*labels))

        self.initPlot(fig, dataframe, path)

        nplotted = 0

        for idx in range(0, len(paths), 2):

            self.initLine(path, dataframe)

            xpath = paths[idx]
            ypath = paths[idx + 1]

            xvalues, yvalues = dataframe.ix[xpath], dataframe.ix[ypath]

            if len(xvalues) != len(yvalues):
                raise ValueError(
                    "length of x,y tuples not consistent: %i != %i" %
                    len(xvalues), len(yvalues))

            R.plot(xvalues, yvalues)
            self.initCoords(xvalues, yvalues)

            nplotted += 1

        self.finishPlot(fig, dataframe, path)

        figid = getCurrentRDevice()
        blocks = ResultBlocks(
            ResultBlock("\n".join(("#$rpl %i$#" % (figid), "")),
                        path2str(path)))

        return blocks
Example #17
0
def clusterize_r_em(*args, **kwargs):
    """ Clustering and plotting with EM GMM"""
    try:
        from rpy2.robjects import r
        import rpy2.robjects.numpy2ri
        rpy2.robjects.numpy2ri.activate()
        from sklearn.decomposition import PCA
    except:
        print "You need rpy2"
        sys.exit(-1)

    r.library("mclust")
    for arg in args:
        if kwargs.get('clf_on_pca', False):
            pca = PCA(2)
            arg = pca.fit(arg).transform(arg)
        model = r.Mclust(arg)
        print model
        print r.summary(model)
        r.quartz("plot")
        r.plot(model, arg)
        print raw_input("press any key to pass")
Example #18
0
def plotSmallDeg(tcc, smallCName, degCName, outDir = None, description = "None", nameNum = "0"):
	
        if not outDir:
		fN = nameNum + "." + tcc + '.png'
	else:
		fN = outDir + '/' + nameNum + "." + tcc + '.png'
	
        #Get deg Profile
	tccStretch = cgPeaks.stretch(tcc, degCName)
		
	sortedX = tccStretch.profile.keys()                                                                                                     
	sortedX.sort()
	
	sortedY = []
	for X in sortedX:
		sortedY.append(tccStretch.profile[X])
	
	#Get small
	tccStretchSmall = cgPeaks.stretch(tcc, smallCName)
		
	sortedXAS = tccStretchSmall.profile.keys()
	sortedXAS.sort()
	
	sortedYAS = []
	for X in sortedXAS:
		sortedYAS.append(tccStretchSmall.profile[X])
	
	#Plot them
	gDevice = importr('grDevices')
	gDevice.png(file=fN, width=1680, height=1050)
	r('split.screen(c(2,1))')
	r('screen(1)')
	r.plot(sortedX, sortedY, xlab = "Coordinates", ylab = "Degradome Expression" )
	r.lines(sortedX, sortedY, type = "b")
	r('screen(2)')
	r.plot(sortedXAS, sortedYAS, xlab = description, ylab = "Small Expression")
	r.lines(sortedXAS, sortedYAS, type = "b")
	gDevice.dev_off()
Example #19
0
def plotProfile(tcc, cName, directory = None, min = 0):
	if not directory:
		fN = tcc + '.png'
	else:
		fN = directory + '/' + tcc + '.png'
		
	tccStretch = cgPeaks.stretch(tcc, cName)
	highest = tccStretch.getHighestLevel()
	if highest < min:
		return 0
		
	sortedX = tccStretch.profile.keys()
	sortedX.sort()
	
	sortedY = []
	for X in sortedX:
		sortedY.append(tccStretch.profile[X])
	
	gDevice = importr('grDevices')
	gDevice.png(file=fN, width=1680, height=1050)
	r.plot(sortedX, sortedY, xlab = "Coordinates", ylab = "Expression Level")
	r.lines(sortedX, sortedY, type = "b")
	gDevice.dev_off()
Example #20
0
    def val(self):
        """ Estimate value functions with b-splines and compare """
        new_data = pd.DataFrame({'OverallRank': np.linspace(1, 194, 1000)})
        fit_a = self.spline_est(self.policy_a['value'], new_data)
        fit_b = self.spline_est(self.policy_b['value'], new_data)

        r.pdf(os.path.join(os.path.dirname(self.out_dir), 'value.pdf'))
        r.plot(new_data['OverallRank'], fit_a, type='l', xlab='Rank_M',
               ylab='V(Rank)')
        r.lines(new_data['OverallRank'], fit_b, col='red')
        r.points(self.policy_a['value']['OverallRank'],
                 self.policy_a['value']['val'],
                 col='black')
        r.points(self.policy_b['value']['OverallRank'],
                 self.policy_b['value']['val'],
                 col='red')
        r.legend('topright', np.array(['No Info', 'Info']),
                 lty=np.array([1, 1]), col=np.array(['black', 'red']))
        r('dev.off()')

        diff = np.array(fit_b) - np.array(fit_a)
        r.pdf(os.path.join(os.path.dirname(self.out_dir), 'value_diff.pdf'))
        r.plot(new_data['OverallRank'], diff, type='l', xlab='Rank',
               ylab='V(Rank|info=1) - V(Rank|info=0)')
        r.abline(h=0, lty=2)
        r('dev.off()')

        diff = (np.array(fit_b) - np.array(fit_a)) / np.array(fit_a)
        r.pdf(os.path.join(os.path.dirname(self.out_dir),
                           'value_percent_diff.pdf'))
        r.plot(new_data['OverallRank'], diff, type='l', xlab='Rank',
               ylab='(V(Rank|info=1) - V(Rank|info=0)) / V(Rank|info=0)')
        r.abline(h=0, lty=2)
        r('dev.off()')

        data_path = dirname(dirname(__file__))
        data_path = join(data_path, 'data', 'lawData.csv')
        data = pd.read_csv(data_path)
        new_data = deepcopy(data.loc[data['year'] == 2013, 'OverallRank'])
        #new_data = np.concatenate((
        #    new_data, np.zeros(lc.N_SCHOOLS - len(new_data))
        #))
        new_data = pd.DataFrame({'OverallRank': np.array(new_data)})
        fit_a = self.spline_est(self.policy_a['value'], new_data)
        fit_b = self.spline_est(self.policy_b['value'], new_data)
        diff = np.sum(np.array(fit_b) - np.array(fit_a))
        pdiff = diff / np.sum(fit_a)
        print("      - Change in Producer Surplus: {0}".format(diff))
        print("      - Percent change in Producer Surplus: {0}".format(pdiff))
        return diff
Example #21
0
>>> import rpy2.robjects as robjects
>>> from rpy2.robjects import r
>>> x= np.range(1,30)

Traceback (most recent call last):
  File "<pyshell#65>", line 1, in <module>
    x= np.range(1,30)
AttributeError: 'module' object has no attribute 'range'
>>> x= np.arange(1,30)
>>> y=np.arange(3,33)
>>> len(x)
29
>>> len(y)
30
>>> y=np.arange(3,32)
>>> r.plot(x,y)

Traceback (most recent call last):
  File "<pyshell#71>", line 1, in <module>
    r.plot(x,y)
  File "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 178, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 102, in __call__
    new_args = [conversion.py2ri(a) for a in args]
  File "/usr/local/lib/python2.7/dist-packages/singledispatch.py", line 210, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/conversion.py", line 60, in _py2ri
    raise NotImplementedError("Conversion 'py2ri' not defined for objects of type '%s'" % str(type(obj)))
NotImplementedError: Conversion 'py2ri' not defined for objects of type '<type 'numpy.ndarray'>'
>>> pplot=r.plot(x,y)
# -*- coding: utf-8 -*-

from rpy2.robjects import r
from rpy2.robjects import IntVector

x = IntVector(range(9))
y = IntVector(range(9))

import ipdb
ipdb.set_trace()

r.png('figura1.png')
r.plot(x, y)
r['dev.off']()

r.png('figura2.jpeg')
r.plot(x, y, xlab='x', ylab='y', main='Minha plotagem', type='l')
r['dev.off']()

r.png('figura3.pdf')
normal = r.rnorm(500, 0, 1)
r.hist(normal)
r['dev.off']()
# -*- coding: utf-8 -*-


from rpy2.robjects import r
from rpy2.robjects import IntVector

x = IntVector(range(9))
y = IntVector(range(9))

import ipdb; ipdb.set_trace()

r.png('figura1.png')
r.plot(x, y)
r['dev.off']()

r.png('figura2.jpeg')
r.plot(x, y, xlab='x', ylab='y', main='Minha plotagem', type='l')
r['dev.off']()

r.png('figura3.pdf')
normal = r.rnorm(500, 0, 1)
r.hist(normal)
r['dev.off']()

def plot_forecast(data, fcast):
    index = pd.date_range(start=data.index.max(),
                          periods=len(fcast[3]) + 1,
                          freq='W')[1:]
    forecast = pd.Series(fcast[3], index=index)
    lowerpi = pd.Series(fcast[4], index=index)
    upperpi = pd.Series(fcast[5], index=index)
    plt.plot(data.index, data.casos_est, color='b', alpha=0.5)
    plt.plot(forecast.index, forecast.values, color='red')
    plt.fill_between(forecast.index,
                     lowerpi.values,
                     upperpi.values,
                     alpha=0.2,
                     color='red')


if __name__ == "__main__":
    data = get_alerta_table(3304557)  # Nova Iguaçu: 3303609
    tscount = importr('tscount')
    tsglm = r('tsglm')

    model = build_model(data)
    print(r.summary(model))
    r.plot(model)
    # fcast = forecast.forecast(model, h=5, level=95.0)
    # print(fcast[3], fcast[4], fcast[5])
    # plot_forecast(data=data, fcast=fcast)
    # plt.show()
Example #25
0
import os
os.environ[
    'R_USER'] = '******'  #path depends on where you installed Python. Mine is the Anaconda distribution

from rpy2.robjects import r
print(r)

r('.libPaths("C:/Users/Uwe/Documents/R/win-library/3.3")')
r('.libPaths()')

from rpy2.robjects.packages import importr
gr = importr('grDevices')
#importr("ggplot2")

r.pi
r.plot(1, 1)
gr.dev_off()
r("plot(mtcars$cyl, mtcars$gear)")
gr.dev_off()

import rpy2.robjects.lib.ggplot2 as ggplot2
p = r("p=ggplot(mtcars) + aes(cyl,gear) + geom_line();print(p)")
p.plot()
r('dev.off()')

boxcore = importr("BoxCore")
#ggplot2 = importr('ggplot2')
from rpy2.robjects.vectors import IntVector, FloatVector

blub = boxcore.plot_distr(FloatVector(df.age.values),
                          FloatVector(df.fare.values))
Example #26
0
 def plot(self, names=None):
     r.plot(model._estimate, names=names)
Example #27
0
from rpy2.robjects import r
from rpy2.robjects.packages import importr
gDevice = importr('grDevices')

gDevice.png(file="file.png", width=512, height=512)
# plotting code here

x = [1,2,3]
y = [1,2,3]

r.plot(x,y, xlab = "X")

gDevice.dev_off()
Example #28
0
from rpy2.robjects import r
from rpy2.robjects.packages import importr
gDevice = importr('grDevices')

gDevice.png(file="file.png", width=512, height=512)
# plotting code here

x = [1, 2, 3]
y = [1, 2, 3]

r.plot(x, y, xlab="X")

gDevice.dev_off()
Example #29
0
import pandas as pd
from rpy2 import robjects as ro
from rpy2.robjects import r
from rpy2.robjects import pandas2ri
from rpy2.robjects import FloatVector as c
import psycopg2
pandas2ri.activate()
con=psycopg2.connect("dbname='stars' user='******' host='localhost'")
cur=con.cursor()
cur.execute("select Jmag, kmag from alphaperprevmembers")
cur.execute("select jmag, ksmag from sheikhi where pm=0")
df=pd.DataFrame(cur.fetchall(),columns=['j','k'])
r.plot(df['j']-df['k'],df['j'],xlim=c([min(df['j']-df['k']),max(df['j']-df['k'])]), ylim=c([max(df['j']), min(df['j'])]), xlab='color J-K', ylab='J_mag', main='')
rW=raw_input()
Example #30
0
import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects import r 
from rpy2.robjects import pandas2ri
pandas2ri.activate()
import psycopg2
con=psycopg2.connect("dbname='stars' user='******' host='localhost'")
cur=con.cursor()
cur.execute("select jmag, ksmag from sheikhi")
df=pd.DataFrame(cur.fetchall(),columns=['j','ks'])
r.png("isochroneofsheikhidata.png")
r.plot(df['j']-df['ks'],df['j'],xlim=ro.FloatVector([min(df['j']-df['ks']),max(df['j']-df['ks'])]),ylim=ro.FloatVector([max(df['j']),min(df['j'])]),xlab='color [J-Ks]', ylab='Jmag', main='cmd from Sheikhi data')
v=raw_input()
def buildCuffdiffPlots(infile, outfile):
    '''create summaries of cufflinks results (including some diagnostic plots)

    Plots are created in the <exportdir>/cuffdiff directory.

    Plots are:

    <geneset>_<method>_<level>_<track1>_vs_<track2>_significance.png
        fold change against expression level
    '''
    ###########################################
    ###########################################
    # create diagnostic plots
    ###########################################
    outdir = os.path.join(PARAMS["exportdir"], "cuffdiff")

    dbhandle = sqlite3.connect(PARAMS["database"])

    prefix = P.snip(infile, ".load")

    geneset, method = prefix.split("_")

    for level in CUFFDIFF_LEVELS:
        tablename_diff = prefix + "_%s_diff" % level
        tablename_levels = prefix + "_%s_levels" % level

        # note that the ordering of EXPERIMENTS and the _diff table
        # needs to be the same as only one triangle is stored of the
        # pairwise results.  do not plot "undefined" lfold values
        # (where treatment_mean or control_mean = 0) do not plot lfold
        # values where the confidence bounds contain 0.
        for track1, track2 in itertools.combinations(EXPERIMENTS, 2):
            statement = """
            SELECT CASE WHEN d.treatment_mean < d.control_mean
            THEN d.treatment_mean
            ELSE d.control_mean END,
            d.l2fold, d.significant
            FROM %(tablename_diff)s AS d
            WHERE treatment_name = '%(track1)s' AND
            control_name = '%(track2)s' AND
            status = 'OK' AND
            treatment_mean > 0 AND
            control_mean > 0
            """ % locals()

            data = zip(*Database.executewait(dbhandle, statement))

            pngfile = "%(outdir)s/%(geneset)s_%(method)s_%(level)s_%(track1)s_vs_%(track2)s_significance.png" % locals(
            )

            # ian: Bug fix: moved R.png to after data check so that no
            #     plot is started if there is no data this was leading
            #     to R falling over from too many open devices

            if len(data) == 0:
                E.warn("no plot for %s - %s -%s vs %s" %
                       (pngfile, level, track1, track2))
                continue

            R.png(pngfile)
            R.plot(ro.FloatVector(data[0]),
                   ro.FloatVector(data[1]),
                   xlab='min(FPKM)',
                   ylab='log2fold',
                   log="x",
                   pch=20,
                   cex=.1,
                   col=R.ifelse(ro.IntVector(data[2]), "red", "black"))

            R['dev.off']()

    P.touch(outfile)
Example #32
0
 def plot(self, names=None):
     r.plot(model._estimate, names=names)
Example #33
0
def main(argv=None, **kwargs):
    '''main function for test.py.

    Long-form of command line arguments can also be supplied as kwargs.

    If argv is not None, command line parsing will be performed.
    '''
    parser = optparse.OptionParser(version="%prog version: $Id$",
                                   usage=globals()["__doc__"])

    parser.add_option("-t", "--tracker", dest="tracker", type="string",
                      help="tracker to use [default=%default]")

    parser.add_option("-p", "--page", dest="page", type="string",
                      help="render an rst page [default=%default]")

    parser.add_option("-a", "--tracks", dest="tracks", type="string",
                      help="tracks to use [default=%default]")

    parser.add_option("-m", "--transformer", dest="transformers",
                      type="string", action="append",
                      help="add transformation [default=%default]")

    parser.add_option("-s", "--slices", dest="slices", type="string",
                      help="slices to use [default=%default]")

    parser.add_option("-r", "--renderer", dest="renderer", type="string",
                      help="renderer to use [default=%default]")

    parser.add_option("-w", "--path", "--trackerdir",
                      dest="trackerdir", type="string",
                      help="path to trackers [default=%default]")

    parser.add_option("-f", "--force", dest="force", action="store_true",
                      help="force recomputation of data by deleting cached "
                      "results [default=%default]")

    parser.add_option("-o", "--option", dest="options", type="string",
                      action="append",
                      help="renderer options - supply as key=value pairs "
                      "(without spaces). [default=%default]")

    parser.add_option("-l", "--language", dest="language", type="choice",
                      choices=("rst", "notebook"),
                      help="output language for snippet. Use ``rst`` "
                      "to create a snippet to paste "
                      "into a cgatreport document. Use ``notebook`` to "
                      "create a snippet to paste "
                      "into an ipython notebook [default=%default]")

    parser.add_option("--no-print", dest="do_print", action="store_false",
                      help="do not print an rst text element to create "
                      "the displayed plots [default=%default].")

    parser.add_option("--no-show", dest="do_show", action="store_false",
                      help="do not show a plot [default=%default].")

    parser.add_option("--layout", dest="layout", type="string",
                      help="output rst with layout [default=%default].")

    parser.add_option("-i", "--start-interpreter", dest="start_interpreter",
                      action="store_true",
                      help="do not render, but start python interpreter "
                      "[default=%default].")

    parser.add_option("-I", "--ii", "--start-ipython", dest="start_ipython",
                      action="store_true",
                      help="do not render, start ipython interpreter "
                      "[default=%default].")

    parser.add_option(
        "--workdir", dest="workdir", type="string",
        help="working directory - change to this directory "
        "before executing "
        "[default=%default]")

    parser.add_option(
        "--hardcopy", dest="hardcopy", type="string",
        help="output images of plots. The parameter should "
        "contain one or more %s "
        "The suffix determines the type of plot. "
        "[default=%default].")

    parser.set_defaults(
        loglevel=1,
        tracker=None,
        transformers=[],
        tracks=None,
        slices=None,
        options=[],
        renderer="table",
        do_show=True,
        do_print=True,
        force=False,
        trackerdir=TRACKERDIR,
        caption="add caption here",
        start_interpreter=False,
        start_ipython=False,
        language="rst",
        workdir=None,
        layout=None,
        dpi=100)

    if argv is None and len(kwargs) == 0:
        argv = sys.argv

    if argv:
        (options, args) = parser.parse_args(argv)

        if len(args) == 2:
            options.tracker, options.renderer = args

    else:
        (options, args) = parser.parse_args([])

        ######################################################
        # set keyword arguments as options
        for keyword, value in kwargs.items():
            if hasattr(options, keyword):
                setattr(options, keyword, value)
                del kwargs[keyword]

        # change some kwarguments
        if options.transformers:
            for keyword, value in kwargs.items():
                if keyword.startswith("tf"):
                    kwargs["tf-{}".format(keyword[2:])] = value

    if options.workdir is not None:
        savedir = os.getcwd()
        os.chdir(options.workdir)
        Utils.getParameters(sorted(glob.glob("*.ini")))
    else:
        savedir = None

    ######################################################
    # configure options
    options.trackerdir = os.path.abspath(
        os.path.expanduser(options.trackerdir))
    if os.path.exists(options.trackerdir):
        sys.path.insert(0, options.trackerdir)
    else:
        L.warn("directory %s does not exist" % options.trackerdir)

    ######################################################
    # test plugins
    for x in options.options:
        if "=" in x:
            data = x.split("=")
            key, val = [y.strip() for y in (data[0], "=".join(data[1:]))]
        else:
            key, val = x.strip(), None
        kwargs[key] = val

    if options.tracks:
        kwargs["tracks"] = options.tracks
    if options.slices:
        kwargs["slices"] = options.slices

    kwargs = Utils.updateOptions(kwargs)

    option_map = getOptionMap()
    renderer_options = Utils.selectAndDeleteOptions(
        kwargs, option_map["render"])
    transformer_options = Utils.selectAndDeleteOptions(
        kwargs, option_map["transform"])
    display_options = Utils.selectAndDeleteOptions(
        kwargs, option_map["display"])

    ######################################################
    # decide whether to render or not
    if options.renderer == "none" or options.start_interpreter or \
       options.start_ipython or options.language == "notebook":
        renderer = None
    else:
        renderer = Utils.getRenderer(options.renderer, renderer_options)

    transformers = Utils.getTransformers(
        options.transformers, transformer_options)

    exclude = set(("Tracker",
                   "TrackerSQL",
                   "returnLabeledData",
                   "returnMultipleColumnData",
                   "returnMultipleColumns",
                   "returnSingleColumn",
                   "returnSingleColumnData",
                   "SQLError",
                   "MultipleColumns",
                   "MultipleColumnData",
                   "LabeledData",
                   "DataSimple",
                   "Data"))

    ######################################################
    # build from tracker
    if options.tracker:

        if "." in options.tracker:
            parts = options.tracker.split(".")
            tracker_modulename = ".".join(parts[:-1])
            tracker_name = parts[-1]
        else:
            tracker_modulename = None
            tracker_name = options.tracker

        try:
            _code, tracker, tracker_path = Utils.makeTracker(
                options.tracker, (), kwargs)
        except ImportError:
            # try to find class in module
            trackers = []

            for filename in glob.glob(
                    os.path.join(options.trackerdir, "*.py")):
                modulename = os.path.basename(filename)
                trackers.extend(
                    [x for x in getTrackers(modulename)
                     if x[0] not in exclude])

            for name, tracker_class, modulename, is_derived in trackers:
                if name == tracker_name:
                    if tracker_modulename is not None:
                        if modulename == tracker_modulename:
                            break
                    else:
                        tracker_modulename = modulename
                        break
            else:
                available_trackers = set([x[0] for x in trackers if x[3]])
                print(
                    "unknown tracker '%s': possible trackers are\n  %s" %
                    (options.tracker, "\n  ".join(sorted(available_trackers))))
                print(
                    "(the list above does not contain functions).")
                sys.exit(1)

            # instantiate functors
            if is_derived:
                tracker = tracker_class(**kwargs)
            #  but not functions
            else:
                tracker = tracker_class

        # remove everything related to that tracker for a clean slate
        if options.force:
            removed = CGATReport.clean.removeTracker(tracker_name)
            print("removed all data for tracker %s: %i files" %
                  (tracker_name, len(removed)))

        dispatcher = Dispatcher(tracker, renderer, transformers)

        if renderer is None:
            # dispatcher.parseArguments(**kwargs)
            # result = dispatcher.collect()
            # result = dispatcher.transform()
            result = dispatcher(**kwargs)
            options.do_print = options.language == "notebook"
            options.do_show = False
            options.hardcopy = False
        else:
            # needs to be resolved between renderer and dispatcher options
            result = dispatcher(**kwargs)

        if options.do_print:

            sys.stdout.write(".. ---- TEMPLATE START --------\n\n")

            if options.language == "rst":
                writeRST(sys.stdout,
                         options,
                         kwargs,
                         renderer_options,
                         transformer_options,
                         display_options,
                         tracker_modulename,
                         tracker_name)
            elif options.language == "notebook":
                writeNotebook(sys.stdout,
                              options,
                              kwargs,
                              renderer_options,
                              transformer_options,
                              display_options,
                              tracker_modulename,
                              tracker_name)

            sys.stdout.write("\n.. ---- TEMPLATE END ----------\n")

        sys.stdout.write("\n.. ---- OUTPUT-----------------\n")

        if result and renderer is not None:
            if options.layout is not None:
                lines = Utils.layoutBlocks(result, layout=options.layout)
                print "\n".join(lines)
            else:
                for r in result:
                    if r.title:
                        print ("")
                        print ("title: %s" % r.title)
                        print ("")
                    for s in r:
                        print(str(s))

        if options.hardcopy:

            fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()
            # create all the images
            for figman in fig_managers:
                # create all images
                figid = figman.num
                outfile = re.sub("%s", str(figid), options.hardcopy)
                figman.canvas.figure.savefig(outfile, dpi=options.dpi)

        if result and options.do_show:
            if options.renderer.startswith("r-"):
                for rr in result:
                    for r in rr:
                        if hasattr(r, 'rggplot'):
                            from rpy2.robjects import r as R
                            import rpy2.rinterface
                            try:
                                R.plot(r.rggplot)
                            except rpy2.rinterface.RRuntimeError, msg:
                                if re.search("object.*not found", str(msg)):
                                    print '%s: available columns in dataframe=%s' % \
                                        (msg,
                                          R('''colnames(rframe)'''))

                print("press Ctrl-c to stop")
                while 1:
                    pass

            elif len(_pylab_helpers.Gcf.get_all_fig_managers()) > 0:
                plt.show()

            else:
                for rr in result:
                    for r in rr:
                        if hasattr(r, 'xls'):
                            tmpfile, outpath = tempfile.mkstemp(
                                dir='.', suffix='.xlsx')
                            os.close(tmpfile)
                            print ('saving xlsx to %s' % outpath)
                            r.xls.save(outpath)
                        elif hasattr(r, 'bokeh'):
                            import bokeh.plotting as bk
                            bk.show()
def buildCuffdiffPlots(infile, outfile):
    '''create summaries of cufflinks results (including some diagnostic plots)

    Plots are created in the <exportdir>/cuffdiff directory.

    Plots are:

    <geneset>_<method>_<level>_<track1>_vs_<track2>_significance.png
        fold change against expression level
    '''
    ###########################################
    ###########################################
    # create diagnostic plots
    ###########################################
    outdir = os.path.join(PARAMS["exportdir"], "cuffdiff")

    dbhandle = sqlite3.connect(PARAMS["database"])

    prefix = P.snip(infile, ".load")

    geneset, method = prefix.split("_")

    for level in CUFFDIFF_LEVELS:
        tablename_diff = prefix + "_%s_diff" % level
        tablename_levels = prefix + "_%s_levels" % level

        # note that the ordering of EXPERIMENTS and the _diff table
        # needs to be the same as only one triangle is stored of the
        # pairwise results.  do not plot "undefined" lfold values
        # (where treatment_mean or control_mean = 0) do not plot lfold
        # values where the confidence bounds contain 0.
        for track1, track2 in itertools.combinations(EXPERIMENTS, 2):
            statement = """
            SELECT CASE WHEN d.treatment_mean < d.control_mean
            THEN d.treatment_mean
            ELSE d.control_mean END,
            d.l2fold, d.significant
            FROM %(tablename_diff)s AS d
            WHERE treatment_name = '%(track1)s' AND
            control_name = '%(track2)s' AND
            status = 'OK' AND
            treatment_mean > 0 AND
            control_mean > 0
            """ % locals()

            data = zip(*Database.executewait(dbhandle, statement))

            pngfile = "%(outdir)s/%(geneset)s_%(method)s_%(level)s_%(track1)s_vs_%(track2)s_significance.png" % locals()

            # ian: Bug fix: moved R.png to after data check so that no
            #     plot is started if there is no data this was leading
            #     to R falling over from too many open devices

            if len(data) == 0:
                E.warn("no plot for %s - %s -%s vs %s" %
                       (pngfile, level, track1, track2))
                continue

            R.png(pngfile)
            R.plot(ro.FloatVector(data[0]),
                   ro.FloatVector(data[1]),
                   xlab='min(FPKM)',
                   ylab='log2fold',
                   log="x", pch=20, cex=.1,
                   col=R.ifelse(ro.IntVector(data[2]), "red", "black"))

            R['dev.off']()

    P.touch(outfile)
Example #35
0
from rpy2.robjects import Formula
from rpy2.robjects.packages import importr
from rpy2.robjects import r
grdevices=importr('grDevices')
#accessing pi in R:
pi=r['pi']
print pi
stats=importr('stats')
graphics=importr('graphics')
base=importr('base')
##
sum=r.sum
print sum(np.array([1,2,3,4]))
#######plot1
d=stats.rnorm(10000)

hist=graphics.hist
grdevices.png(file='/Users/behrouzmadahian/Dropbox/Research_and_Development/Python/DEVELOPMENTS/R_int_hist.png',width=512
              ,height=800)
hist(d,col='blue',ylab='Freg',xlab='rnorm',main='')
grdevices.dev_off()
#####plot2
lattice=importr('lattice')
xyplot=lattice.xyplot
x=range(10)
y=range(10,20)
r.plot(x,y)
raw_input()