def run(self,*event): #I tried for WAY to long to get this to work with the high-level interface #but it usually resulted in the output having a very long label that #contained all the data in the vector we passed to it, still not sure #how to resolve it args = rdict() args['mu'] = self.parameters['Mu'].get() args['conf.level'] = float( self.parameters['Confidence Level'].get() ) #args['paired'] = self.parameters['Paired'].get() args['var.equal'] = self.parameters['Equal Variances'].get() #R takes the first character as an argument args['alternative'] = self.parameters['Alternative Hypothesis'].get().lower()[0] args = rsession.arguments_to_string(args) print args s1lab,s1data = self.custom_variables['samples'][0] if len(self.custom_variables['samples'])>1: s2lab,s2data = self.custom_variables['samples'][1] x = self.active_r_object[s1lab] y = self.active_r_object[s2lab] anova_output = rsession.r('t.test(x=%s,y=%s,data=%s,%s)' % (x, y, self.active_r_object.label, args)) else: x = self.active_r_object[s1lab] anova_output = rsession.r('t.test(x=%s,data=%s,%s)' % (x, self.active_r_object.label, args)) name = 'T-Test ' + str(self.active_r_object.label) self.parent.render_description(anova_output,label=name)
def run(self,*event): #I tried for WAY to long to get this to work with the high-level interface #but it usually resulted in the output having a very long label that #contained all the data in the vector we passed to it, still not sure #how to resolve it args = rdict() args['correct'] = self.parameters['Apply continuity correction'].get() args = rsession.arguments_to_string(args) s1lab,s1data = self.custom_variables['samples'][0] if len(self.custom_variables['samples'])==2: s2lab,s2data = self.custom_variables['samples'][1] x = self.active_r_object[s1lab] y = self.active_r_object[s2lab] anova_output = rsession.r('mcnemar.test(x=%s,y=%s%s)' % (x, y, args)) else: info('Please select two samples.') name = 'T-Test ' + str(self.active_r_object.label) self.parent.render_description(anova_output,label=name)
def plot_fit(self,event,image=None): ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() #These return the parent$column strings x = self.active_r_object[ilab] y = self.active_r_object[dlab] fit = rsession.r('lm(%s~poly(%s,%i))' % (y,x,self.degree.get_value())) self.result = fit data={'x':idata,'y':ddata} args={'xlab':ilab,'ylab':dlab} plotwindow = rsession.RPlotThread(data=data, args=args, par_mode=(1,1), export=image, type='scatter') plotwindow.start() seq=rsession.r['seq'] min=rsession.r['min'] max=rsession.r['max'] object = rsession.env[self.active_r_object.label] range = seq(min(object), max(object), length_out = 100) predicted = rsession.r['predict'](fit,data_frame=range) plotwindow.add_cmd(rsession.ro.r.lines,idata,predicted,col='red') self.active_threads.append(plotwindow)
def plot_fit(self,event,image=None): if not self.variables_are_set(): error('Required variables are not set.') return ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() x = self.active_r_object[ilab] y = self.active_r_object[dlab] fit = rsession.r('lm(%s ~ %s,data=%s)' % (y,x,self.active_r_object.label)) self.result = fit data={'x':idata,'y':ddata} args={'xlab':ilab,'ylab':dlab} plotwindow = rsession.RPlotThread(data=data, args=args, type='scatter', export=image, par_mode=(1,1)) plotwindow.add_cmd(rsession.r['abline'],fit,col='red') plotwindow.start() self.active_threads.append(plotwindow)
def freqtable(self,event): condition = variable_prompt.VariablePrompt(['Condition(s):']).get_inputs() #This is dirty but ro.RFormula will not parse try: filter = rsession.r('with(%s,table(%s))' % (self.active_robject.label,condition[0])) except: error('Formula is not valid. \n Enter conditions seperated by commas. Ex: V1>3,V4<1') return self.output(filter,title='Frequency Table: ' + condition[0]) self.active_output = rsession.description(filter,tabelize=False)
def export_latex(self,event): try: rsession.r('library(xtable)') rprint = rsession.r['print'] xtable = rsession.r['xtable'] table = rsession.r['table'] activetab = self.builder.get_object('main_tabview').get_current_page() #Data Tab if activetab == 0: if not self.active_robject: error('No active dataframe.') return object = self.active_robject.object label = self.active_robject.label text = str(xtable(object,caption=label)) TextOutput(text) #Output Tab elif activetab == 1: if not self.active_output: error('No active output.') return object = self.active_output.object label = self.active_output.label if not label: label = rsession.null if self.active_output.tabelize: text = str(xtable(table(object),caption=label)) else: text = str(xtable(object,caption=label)) TextOutput(text) #Console Tab elif activetab == 2: console = self.builder.get_object('consoleview').get_buffer() lower,upper = console.get_bounds() text = console.get_text(lower,upper) TextOutput('\\begin{verbatim}\n' + text + '\n\end{verbatim}') except: error("Could not load the xtable library.")
def plot_fit(self,event,image=None): if not self.variables_are_set(): error('Required variables are not set.') return rsession.r('par(mfrow=c(1,1))') ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() #fit = rsession.r['loess'](idata,ddata).object data={'x':idata,'y':ddata} args={'xlab':ilab,'ylab':dlab} plotwindow = rsession.RPlotThread(data=data, args=args, type='scatter.smooth') plotwindow.start() self.active_threads.append(plotwindow)
def plot_summary(self,event,image=None): if not self.variables_are_set(): error('Required variables are not set.') return rsession.r('par(mfrow=c(2,2))') ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() x = self.active_r_object[ilab] y = self.active_r_object[dlab] fit = rsession.r('lm(%s ~ %s,data=%s)' % (x,y,self.active_r_object.label)) self.result = fit plotwindow = rsession.RPlotThread(data=fit, type='general', par_mode=True) plotwindow.start() self.active_threads.append(plotwindow)
def transform(self,event): cols = self.active_robject.columns.keys() transforms = zip(cols,variable_prompt.VariablePrompt(cols).get_inputs(allow_empty=True)) try: args = rdict() argument_string = '' for col,transform in transforms: if col and transform: argument_string += ',%s=%s' % (col,transform) print argument_string label = self.active_robject.label obj = rsession.env[label] transformed = rsession.r('transform(%s %s)' % (label,argument_string)) #transformed = rsession.r['transform'](obj,args) except: error('Transformations were not well formed') return rsession.env[label] = transformed self.sync_with_r()
def plot_summary(self,event,image=None): ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() #These return the parent$column strings x = self.active_r_object[ilab] y = self.active_r_object[dlab] fit = rsession.r('lm(%s~poly(%s,%i))' % (y,x,self.degree.get_value())) self.result = fit plotwindow = rsession.RPlotThread(data=fit, type='general', par_mode=(2,2), export=image) plotwindow.start() self.active_threads.append(plotwindow)
def run(self,*event): ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() x = self.active_r_object[ilab] y = self.active_r_object[dlab] fit = rsession.r('lm(%s ~ %s,data=%s)' % (y,x,self.active_r_object.label)) if idata == ddata: error('Variables are identical, results may not be accurate.') anova_output = rsession.r['anova'](fit) name = 'ANOVA ' + str(self.active_r_object.label) #This isn't good if there are variables called x,y in the workspace rsession.rm('x') rsession.rm('y') self.parent.render_description(anova_output,label=name)
def plot(self,event,image=None): _xlab,_ylab,_title,_xmin,_ymin,_xmax,_ymax = self.fetch_labels() data = self.data args = rdict() if self.parameters['Lower Panels'].get(): args['lower.panel'] = rsession.r('panel.smooth') else: args['lower.panel'] = rsession.null args['gap'] = self.parameters['Plot Gap'].get() args['pch'] = self.get_point_style() #Advanced Parameters plotwindow = rsession.RPlotThread(data=data, args=args, type='matplot', export=image) plotwindow.start() self.active_threads.append(plotwindow)
def context_delcol(self,*args): #Its much easier to resolve this on the R side, rather than using numpy rsession.r('%s = transform(%s,%s)' % (self.active_robject.label, self.active_robject.label,self.active_column+'=NULL')) self.sync_with_r()
def subset(self,event): condition = variable_prompt.VariablePrompt(['Condition(s):']).get_inputs()[0] rsession.env['subset %s' % (self.active_robject.label)] = rsession.r('subset(%s,%s)' % (self.active_robject.label,condition)) self.sync_with_r()
def shutdown_cleanly(self): '''If the user CTRL-Cs, make sure the threads die and graphics get turned off''' #iterate over the shared state of ThreadHandler to fetch all threads and kill them for thread in self.active_threads.iter_shared(): thread.halt = True rsession.r('graphics.off()')
def plot_fit(self,event,image=None): model = self.get_model().lower() dependent = model.partition('~')[0] independent = model.partition('~')[2] coefs = set() vars = set() #We're iterating over the variables, so that later it will be easier to do this in more than 2D for symbol in model: if symbol is 'x' or symbol is 'y': vars.add(symbol) elif symbol.isalpha(): coefs.add(symbol) ilab,idata = self.custom_variables['independent'].get_first() dlab,ddata = self.custom_variables['dependent'].get_first() try: fmla = rsession.ro.RFormula(model) except: error('The model you specified is not valid.') return env = fmla.getenvironment() env['x'] = idata env['y'] = ddata #I love R, this would take like 30 hours to implement in python #this fetches all coefficents but ignores functions like sin,cos... coefs = list ( rsession.r['all.vars'](fmla) ) #Don't consider x and y as coefficents coefs.remove('y') coefs.remove('x') data={'x':idata,'y':ddata} args={'xlab':ilab,'ylab':dlab} object = rsession.r(self.active_r_object.label) startdict = {} for i in coefs: startdict[i]= 1 startlist=rsession.r['list'](**startdict) seq=rsession.r['seq'] rmin=rsession.r['min'] rmax=rsession.r['max'] # There is nothing truly beautiful but that which can never be of any use whatsoever; everything # useful is ugly; keep that in mine while reading the next 40 lines. #Generate a range of points so our predicted curve is smooth lenout = {'length.out':500} df = rsession.r['data.frame'] smooth_range = rsession.r['seq']( rsession.r['min'](idata) , rsession.r['max'](idata),**lenout) fit_found = False predicted = None #Try and find the fit, loop using the user provided starting values until we find a fit while(fit_found == False): try: #Try to find a fit fit = rsession.r['nls'](formula=fmla.r_repr(),data=object,start=startlist) #Interpolate the fit over the the range of the independent variable predicted = rsession.r['predict'](fit,df(x=smooth_range)) #If we fail to achieve convergance except: #Prompt the user for more information about coefficents info('Fit could not be found, please adjust starting values for coefficents.') startvalues = VariablePrompt(startdict.keys()).get_inputs() if not startvalues: return for coef,value in zip(startdict.keys(),startvalues): startdict[coef] = value startlist=rsession.r['list'](**startdict) if predicted: #info('Fit Found') fit_found = True self.fit = fit self.result = fit plotwindow = rsession.RPlotThread(data=data, args=args, type='scatter', export=image, par_mode=(1,1)) #print 'predicted',predicted #print 'smooth',smooth_range plotwindow.add_cmd(rsession.ro.r.lines,smooth_range,predicted,col='red') plotwindow.start() self.active_threads.append(plotwindow)