def classify_encapsulated(audio_summary, track_info, pickle_file): """Performs a similar operation to classify() and all the above functions in one step But retrieves fit from pickle, for the GUI """ f = open(pickle_file, 'r') fit = pickle.load(f) f.close() rv = {} #print track_info.keys() #print track_info[audio_summary]['title'].keys() rv["speechiness"] = [audio_summary['audio_summary']['speechiness']] rv["time_sig"] = [audio_summary['audio_summary']['time_signature']] rv["bpm"] = [audio_summary['audio_summary']['tempo']] rv["key"] = [audio_summary['audio_summary']['key']] rv["duration"] = [audio_summary['audio_summary']['duration']] rv["loudness"] = [audio_summary['audio_summary']['loudness']] rv["end_of_fade_in"] = [track_info['track']['end_of_fade_in']] rv["start_of_fade_out"] = [track_info['track']['start_of_fade_out']] rv["bpm_range"] = [proc.bpm_range(track_info['beats'])] rv["max_bpm_spike"] = [proc.max_bpm_spike(track_info['beats'])] try: rv["num_keys"] = [proc.num_keys(track_info['sections'])] except: rv["num_keys"] = [1] rv["sections"] = [proc.num_sections(track_info['sections'])] new_df = build_data_frame([rv],["Unknown"]) p = prob_category(new_df,fit) robjects.globalenv["pred"] = p edm_prob = robjects.default_ri2py(p.rx(1))[0] folk_prob = robjects.default_ri2py(p.rx(2))[0] rap_prob = robjects.default_ri2py(p.rx(3))[0] cls = classify(new_df,fit) return [(edm_prob,folk_prob,rap_prob),cls[0]]
def custom_ri2py(obj): res = robjects.default_ri2py(obj) if isinstance(res, robjects.Vector) and (len(res) == 1): res = super(robjects.vectors.Vector, res).__getitem__(0) if isinstance(res, rinterface.Sexp): res = robjects.default_ri2py(res) return res
def get_sd_ratios(what,orgn,dstn,fltnum,dfrom,dto,daysprior,cls): curr_dt = datetime.now() curr_date = date(curr_dt.year, curr_dt.month, curr_dt.day) if what == 'pb': [vals,obs,diff,dptdts] = get_fcsterr_by_dptdt_dflc_lvl('uncons',orgn,dstn,fltnum,dfrom,curr_date,daysprior,cls) elif what == 'fb': print "fp parameter what isn't implemented" assert 0 elif what == 'ns': vals = get_noshows_by_flc_lvl(orgn,dstn,fltnum,dfrom,dto,cls) else: print 'Unknown parameter what in get_sd_ratios' assert 0 print vals try: vals_r = robjects.FloatVector(vals) res_r = r.find_var_prop(vals_r) res = robjects.default_ri2py(res_r) except: res = [0,0] return res
def ks_p_value(l): r_l = robjects.IntVector(l) r_p_value = r.get_ks_p_value(r_l) p_value = robjects.default_ri2py(r_p_value)[0] return p_value
def cramer_von_mises_p_value(l): r_l = robjects.IntVector(l) r_p_value = r.get_cr_von_mis_p_value(r_l) p_value = robjects.default_ri2py(r_p_value)[0] return p_value
def as_dendropy_object(o, taxon_set=None): """ Returns a DendroPy object corresponding to the ape object ``o``. If ``o`` is a single tree (i.e., ``phylo``), then a DendroPy Tree is returned. If ``o`` is a list of trees (i.e., a ``multiPhylo`` object, or list of ``phylo`` objects), then a DendroPy TreeList is returned. """ if o.rclass[0] == "multiPhylo": f = tempfile.NamedTemporaryFile() _R["write.nexus"](o, file=f.name) return dendropy.TreeList.get_from_path(f.name, "nexus", taxon_set=taxon_set) elif o.rclass[0] == "phylo": f = tempfile.NamedTemporaryFile() _R["write.nexus"](o, file=f.name) return dendropy.Tree.get_from_path(f.name, "nexus", taxon_set=taxon_set) elif o.rclass[0] == "list": f = tempfile.NamedTemporaryFile() _R["write.nexus.data"](o, file=f.name) # print open(f.name, "r").read() d = dendropy.DataSet.get_from_path(f.name, "nexus", taxon_set=taxon_set) if len(d.char_matrices) == 0: raise ValueError("No character data found") elif len(d.char_matrices) == 1: return d.char_matrices[0] else: raise ValueError("Multiple character matrices returned") else: return robjects.default_ri2py(o)
def ri2pandas(o): if isinstance(o, DataFrame): raise NotImplementedError( "Conversion from rpy2 DataFrame to pandas' DataFrame") else: res = ro.default_ri2py(o) return res
def as_dendropy_object(o, taxon_set=None): """ Returns a DendroPy object corresponding to the ape object ``o``. If ``o`` is a single tree (i.e., ``phylo``), then a DendroPy Tree is returned. If ``o`` is a list of trees (i.e., a ``multiPhylo`` object, or list of ``phylo`` objects), then a DendroPy TreeList is returned. """ if o.rclass[0] == "multiPhylo": f = tempfile.NamedTemporaryFile() _R['write.nexus'](o, file=f.name) return dendropy.TreeList.get_from_path(f.name, "nexus", taxon_set=taxon_set) elif o.rclass[0] == "phylo": f = tempfile.NamedTemporaryFile() _R['write.nexus'](o, file=f.name) return dendropy.Tree.get_from_path(f.name, "nexus", taxon_set=taxon_set) elif o.rclass[0] == "list": f = tempfile.NamedTemporaryFile() _R['write.nexus.data'](o, file=f.name) # print open(f.name, "r").read() d = dendropy.DataSet.get_from_path(f.name, "nexus", taxon_set=taxon_set) if len(d.char_matrices) == 0: raise ValueError("No character data found") elif len(d.char_matrices) == 1: return d.char_matrices[0] else: raise ValueError("Multiple character matrices returned") else: return robjects.default_ri2py(o)
def get_fcsterr_mse(type, orgn, dstn, fltnum, dfrom, dto, daysprior, cls): [fcst, obs, diff, dptdt_l] = get_fcsterr_by_dptdt_dflc_lvl(type, orgn, dstn, fltnum, dfrom, dto, daysprior, cls) r_diff = robjects.FloatVector(diff) r_mse = r.get_mse(r_diff) mse = robjects.default_ri2py(r_mse)[0] return mse
def ri2numpy(o): if isinstance(o, ListVector): res = numpy.rec.fromarrays(o, names=tuple(o.names)) elif isinstance(o, Vector) and (type(o) != Vector): res = numpy.asarray(o) else: res = ro.default_ri2py(o) return res
def f(obj): pyobj = robjects.default_ri2py(obj) inherits = rinterface.baseenv["inherits"] classname = rinterface.SexpVector(["density", ], rinterface.STRSXP) if inherits(pyobj, classname)[0]: pyobj = Density(pyobj) return pyobj
def testMapperR2Python_s4(self): robjects.r('setClass("A", representation(x="integer"))') classname = rinterface.StrSexpVector(["A", ]) one = rinterface.IntSexpVector([1, ]) sexp = rinterface.globalenv.get("new")(classname, x=one) self.assertTrue(isinstance(robjects.default_ri2py(sexp), robjects.RS4))
def shapiro_wilk_p_value(l): # l = list(Set(l)) r_l = robjects.FloatVector(l) r_p_value = r.get_shapiro_wilk_p_value(r_l) p_value = robjects.default_ri2py(r_p_value)[0] return p_value
def f(obj): pyobj = robjects.default_ri2py(obj) inherits = rinterface.baseenv["inherits"] classname = rinterface.SexpVector([ "density", ], rinterface.STRSXP) if inherits(pyobj, classname)[0]: pyobj = Density(pyobj) return pyobj
def convert_xts_to_df(o): """ Will convert xts objects to DataFrame """ dates = o.do_slot('index') index = convert_posixct_to_index(dates) res = robjects.default_ri2py(o) df = _convert_Matrix(res) df.index = index return df
def convert_xts_to_df(o): """ Will convert xts objects to DataFrame """ dates = o.do_slot('index') dates = np.array(dates, dtype=np.dtype("M8[s]")) res = robjects.default_ri2py(o) df = rcom.convert_robj(res) df.index = dates return df
def get_ljung_box_pval(type, orgn, dstn, fltnum, dfrom, dto, daysprior, cls): [fcst, obs, diff, dptdt_l] = get_fcsterr_by_dptdt_dflc_lvl(type, orgn, dstn, fltnum, dfrom, dto, daysprior, cls) if len(diff) != 0: r_diff = robjects.FloatVector(diff) r_pval = r.get_ljung_box_pval(r_diff) pval = robjects.default_ri2py(r_pval)[0] else: pval = 0 return pval
def testMapperR2Python_s4(self): robjects.r('setClass("A", representation(x="integer"))') classname = rinterface.StrSexpVector([ "A", ]) one = rinterface.IntSexpVector([ 1, ]) sexp = rinterface.globalenv.get("new")(classname, x=one) self.assertTrue(isinstance(robjects.default_ri2py(sexp), robjects.RS4))
def my_ri2py(o): res = None try: rcls = o.do_slot("class") except LookupError: rcls = [None] if isinstance(o, ri.SexpVector): if 'xts' in rcls: res = convert_xts_to_df(o) if res is None: res = robjects.default_ri2py(o) return res
def get_esp_params(flights,dows,sh_id,pool_ids,clss): sizes = [] alphas = [] for flight in flights: orgn = flight[0] dstn = flight[1] fltnum = flight[2] sh = splitHistory(sh_id) for dow in dows: for pool_id in pool_ids: for cls in clss: for daysprior in get_dayspriors(): fdr = fcstDataReader(orgn,dstn,fltnum,\ cls,daysprior,dow,sh) vals = fdr.get_fcst_minus_booked_vals('uncons',pool_id) sample_size = get_sample_size(vals) if sample_size is not None: sizes.append(sample_size) vals_r = robjects.FloatVector(vals) vals_r_ts = r.ts(vals_r,frequency=len(vals)) hw_res = r.HoltWinters(vals_r,beta=False,gamma=False) alpha = robjects.default_ri2py(hw_res[2]) alphas.append(alpha) lag = int(ceil(mean(sizes))) alpha = float(mean(alphas)) return [lag, alpha]
def ri2py_list(obj): if 'data.frame' in obj.rclass: # R "factor" vectors will not convert well by default # (will become integers), so we build a temporary list o2 # with the factors as strings. o2 = list() # An added complication is that the conversion defined # in this module will make __getitem__ at the robjects # level return numpy arrays for column in rinterface.ListSexpVector(obj): if 'factor' in column.rclass: levels = tuple(column.do_slot("levels")) column = tuple(levels[x-1] for x in column) o2.append(column) names = obj.do_slot('names') if names is NULL: res = numpy.rec.fromarrays(o2) else: res = numpy.rec.fromarrays(o2, names=tuple(names)) else: # not a data.frame, yet is it still possible to convert it res = ro.default_ri2py(obj) return res
def run_test(self): ''' take the R globalEnv located HyperGParam obj and run HyperGTest, returns the HyperGResult obj ''' return rob.default_ri2py(rob.r.hyperGTest(rob.r[self.par_name]))
def pd_ri2py(o): res = None try: rcls = o.do_slot("class") except LookupError, le: rcls = [None] if isinstance(o, SexpVector): if 'xts' in rcls: print 'xts' res = convert_xts_to_df(o) if 'POSIXct' in rcls: res = convert_posixct_to_index(o) if res is None: res = robjects.default_ri2py(o) return res def convert_xts_to_df(o): """ Will convert xts objects to DataFrame """ dates = o.do_slot('index') dates = np.array(dates, dtype=np.dtype("M8[s]")) res = robjects.default_ri2py(o) df = rcom.convert_robj(res) df.index = dates return df def convert_posixct_to_index(o):
baseenv_ri = rinterface.baseenv globalenv_ri = rinterface.globalenv def my_ri2py(o): res = None try: rcls = o.do_slot("class") except LookupError, le: rcls = [None] if isinstance(o, SexpVector): if 'xts' in rcls: res = convert_xts_to_df(o) if res is None: res = robjects.default_ri2py(o) return res def convert_xts_to_df(o): """ Will convert xts objects to DataFrame """ dates = o.do_slot('index') dates = np.array(dates, dtype=np.dtype("M8[s]")) res = robjects.default_ri2py(o) df = rcom.convert_robj(res) df.index = dates return df robjects.conversion.ri2py = my_ri2py
def ri2py_sexp(obj): if (obj.typeof in _vectortypes) and (obj.typeof != VECSXP): res = numpy.asarray(obj) else: res = ro.default_ri2py(obj) return res
def testMapperR2Python_string(self): sexp = rinterface.globalEnv.get("letters") ob = robjects.default_ri2py(sexp) self.assertTrue(isinstance(ob, robjects.RVector))
def testMapperR2Python_environment(self): sexp = rinterface.globalenv.get(".GlobalEnv") self.assertTrue(isinstance(robjects.default_ri2py(sexp), robjects.Environment))
def testMapperR2Python_function(self): sexp = rinterface.globalenv.get("plot") ob = robjects.default_ri2py(sexp) self.assertTrue(isinstance(ob, robjects.Function))
def testMapperR2Python_boolean(self): sexp = rinterface.globalenv.get("T") ob = robjects.default_ri2py(sexp) self.assertTrue(isinstance(ob, robjects.Vector))
def ri2pandas(o): if isinstance(o, DataFrame): raise NotImplementedError("Conversion from rpy2 DataFrame to pandas' DataFrame") else: res = ro.default_ri2py(o) return res
def testMapperR2Python_environment(self): sexp = rinterface.globalenv.get(".GlobalEnv") self.assertTrue( isinstance(robjects.default_ri2py(sexp), robjects.Environment))