Call auto.arima from Python via pyRserve - python

I have been trying to use the pyRserve for time series forecast and intent to use the auto.arima function in R.
I used the following code to solve the problem of functions with a dot in their names such as auto.arima:
import pyRserve
import pandas as pd
import numpy
conn = pyRserve.connect()
df = pd.read_excel('D:/My Path/C9.xlsx', sheet_name='C9')
aList = df['Value'].tolist() # Cast the desired column into a python list
aList = numpy.array(aList)
conn.r.List = aList
auto_arima = getattr(conn.r, 'auto.arima')
conn.r.sapply(conn.ref.List, auto_arima)
but, it returned this error:
Traceback (most recent call last):
File "D:/Forecast/Python/R2Python/R2P_Practice.py", line 21, in <module>
auto_arima = getattr(conn.r, 'auto.arima')
File "C:\Python27\lib\site-packages\pyRserve\rconn.py", line 308, in __getattr__
'defined in Rserve' % realname)
NameError: no such variable or function "auto.arima" defined in Rserve
It seems the auto.arima is not defined in Rserve. Why isn't it there? How can I fix this?

Related

rpy2 Conflict when converting R symbols in the package to Python symbols

I simply tried the following:
import rpy2
import rpy2.robjects as RObjects
from rpy2.robjects.packages import importr
princurve = importr('princurve', robject_translations = {"plot_principal_curve": "plot.principal.curve"})
princurve = importr('princurve', robject_translations = {"points_principal_curve": "points.principal.curve"})
and got this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\rpy2\robjects\packages.py", line 498, in importr
symbol_resolve=symbol_resolve)
File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\rpy2\robjects\packages.py", line 202, in __init__
self.__fill_rpy2r__(on_conflict=on_conflict)
File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\rpy2\robjects\packages.py", line 328, in __fill_rpy2r__
.__fill_rpy2r__(on_conflict=on_conflict))
File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\rpy2\robjects\packages.py", line 238, in __fill_rpy2r__
exception)
File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\rpy2\robjects\packages_utils.py", line 120, in _fix_map_symbols
raise exception(msg)
rpy2.robjects.packages.LibraryError: Conflict when converting R symbols in the package "princurve" to Python symbols:
-lines_principal_curve -> lines.principal.curve, lines.principal_curve
- plot_principal_curve -> plot.principal.curve, plot.principal_curve
- points_principal_curve -> points.principal.curve, points.principal_curve
To turn this exception into a simple warning use the parameter `on_conflict="warn"`
can anyone help?
You were almost there! In robject_translations you need to provide R name -> Python name mapping, but your dictionary seemed to be the other way around. You also need to have all the mappings in a single dictionary. To make it super clear, you can resolve the conflicts like this:
princurve_example_1 = importr(
"princurve",
robject_translations={
"plot.principal.curve": "plot_dot_principal_dot_curve",
"lines.principal.curve": "lines_dot_principal_dot_curve",
"points.principal.curve": "points_dot_principal_dot_curve",
# optional (if omitted, you will get them under plot_principal_curve, etc.):
"plot.principal_curve": "plot_dot_principal_curve",
"lines.principal_curve": "lines_dot_principal_curve",
"points.principal_curve": "points_dot_principal_curve"
}
)
# then, after creating the curve and storing it in curve variable:
princurve_example_1.plot_dot_principal_dot_curve(curve)
# or
princurve_example_1.plot_dot_principal_curve(curve)
However, after consulting the pincurve documentation I see that the principal.curve is deprecated and you should use principal_curve instead (good to see more R packages finally moving to the convention of using underscores in function and variable names when possible!); therefore you can just do:
princurve = importr(
"princurve",
robject_translations={
"plot.principal.curve": "plot_principal_curve_deprecated",
"lines.principal.curve": "lines_principal_curve_deprecated",
"points.principal.curve": "points_principal_curve_deprecated",
}
)
# auto-generated from "plot.principal_curve"
princurve.plot_principal_curve(curve)
# manually mapped from "plot.principal.curve"
princurve.plot_principal_curve_deprecated(curve)

Python: _dop.error: failed in processing argument list for call-back fcn

After having cleared my last error, I ran into another error and this error is more fundamental to the ode solver. Below is my error.
Traceback (most recent call last):
File "cont_inside_f.py", line 36, in <module>
x,t,u=solver(0,1e-2,10,[0,0],[a,eta,k,lam])
File "cont_inside_f.py", line 24, in solver
r.integrate(r.t+dt)
File "/usr/local/lib/python2.7/dist-packages/scipy/integrate/_ode.py", line 408, in integrate
self.f_params, self.jac_params)
File "/usr/local/lib/python2.7/dist-packages/scipy/integrate/_ode.py", line 1032, in run
tuple(self.call_args) + (f_params,)))
_dop.error: failed in processing argument list for call-back fcn.
I created a function that returns a list of lists. I wish to pass first element of this list to the ode solver for integration and second element of this returned list is something that I wish to plot later down my code. Kindly help me solve this error, thanks in advance!
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
from numpy import tanh,array,sin,cos
def f(t,Y,param):
x1,x2=Y[0],Y[1]
a,eta,k,lam=param[0],param[1],param[2],param[3]
e=x1-2
de=-2*x1+a*x2+sin(x1)
s=de+lam*e
u=(1/(a*cos(2*x1)))*(-eta*tanh(s)-k*s-(-2*x1+a*x2+sin(x1))*cos(x1)+2*(-2*x1+a*x2+sin(x1))+a*x2*cos(x1))
x1dot=-2*x1+a*x2+sin(x1)
x2dot=-x2*cos(x1)+cos(2*x1)*u
x=[x1dot,x2dot]
return [x,u]
def solver(t0,dt,t1,y0,param):
x,u=[[] for i in range(2)],[]
#import pdb;pdb.set_trace()
r=ode(f(t0,y0,param)[0]).set_integrator('dopri5',method='bdf')
r.set_initial_value(y0,t0).set_f_params(param)
while r.successful() and r.t<t1:
r.integrate(r.t+dt)
for i in range(2):
x[i].append(r.y[i])
t.append(r.t)
#u.append(f(r.t,[r.y[0],r.y[1]],param)[1])
u.append(f(t0,y0,param)[1])
#print(t)
return x,t,u
if __name__=='__main__':
a,eta,k,lam=2,1.2,3,2
x,t,u=solver(0,1e-2,10,[0,0],[a,eta,k,lam])
for i in range(3):
if i!=2:
plt.subplot(3,1,i+1)
plt.plot(t,x[i])
else:
plt.subplot(3,1,i+1)
plt.plot(t,u)
plt.show()
Here
r=ode(f(t0,y0,param)[0]).set_integrator('dopri5',method='bdf')
you do not pass a function pointer but the value at the point t0,y0. You could use a lambda expression to correct this,
r=ode(lambda t,y: f(t,y,param)[0]).set_integrator('dopri5',method='bdf')
As the parameter passing is done here directly, you can not do it twice via the indirect set_parameter_f, as that messes up the argument stack. Just remove that part.
You also need to initialize the list for the t values, and the current value of u is obtained via
u.append(f(r.t,r.y,param)[1])

Poloniex Api Trouble

So I'm accessing the poloniex API with python and this is my code:
from poloniex import Poloniex
import krakenex
import threading
import pprint
import urllib.request
import json
####POLONIEX####
#FUNCTIONS
polo = Poloniex()
def BTC_USDT_LAST_POLONIEX():
polo = Poloniex()
threading.Timer(1.0, BTC_USDT_LAST_POLONIEX).start() # called every minute
print("BTC Last Price = " + (polo('returnTicker')['USDT_BTC']['last']))
def POLONIEX_ASSET_LIST():
pprint.pprint(sorted(list(polo('returnTicker'))))
Everything is working so far and I want to avoid using urllib as its a pain to turn a http request into a list. I'm trying to access the order book but get the following error:
>>> polo('returnOrderBook')
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
polo('returnOrderBook')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/retry.py", line 15, in wrapped
return function(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/__init__.py", line 183, in __call__
return self.parseJson(ret.text)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/__init__.py", line 197, in parseJson
raise PoloniexError(jsonout['error'])
poloniex.PoloniexError: Please specify a currency pair.
I've tried specifying the currency pair but have no idea how to plug it in.
Rewrite your code and use requests module instead of urllib:
import requests
ret = requests.get('http://poloniex.com/public?command=returnOrderBook&currencyPair=BTC_BCN').json()
print ret
>>> {u'bids': [[u'0.00000034', 20629605.566027], [u'0.00000033', 43382683.465305], [u'0.00000032', 70007976.087993], [u'0.00000031', 49571221.248027], [u'0.00000030', 77520227.415484], [u'0.00000029', 46037827.046996], [u'0.00000028', 26267440.401662], [u'0.00000027', 22511987.85933], [u'0.00000026', 18885378.040015], [u'0.00000025', 13313109.292994], [u'0.00000024', 6243527.5236432], [u'0.00000023', 7504850.7832509], [u'0.00000022', 8443683.7997507], [u'0.00000021', 8996262.9826951], [u'0.00000020', 24601532.006268], [u'0.00000019', 26853346.478659], [u'0.00000018', 6027262.24889 etc....

error using pandas in python

I would like to use the pandas package for python. Some functionalities work, but when I try to pass "include" argument into the describe() function I get an error:
train_df.describe(include=['O'])
Full code looks like thie following:
import numpy as np
import pandas as pd
import random as rnd
import matplotlib.pyplot as plt
# aquire data
train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('test.csv')
train_df.describe(include=['O'])
I get the following error:
>> python survival.py
Traceback (most recent call last):
File "survival.py", line 10, in <module>
train_df.describe(include=['O'])
TypeError: describe() got an unexpected keyword argument 'include'
Using the .describe() on its own seems to work. Any ideas? Thank you.

how to use TTreeReader in PyROOT

I'm trying to get up and running using the TTreeReader approach to reading TTrees in PyROOT. As a guide, I am using the ROOT 6 Analysis Workshop (http://root.cern.ch/drupal/content/7-using-ttreereader) and its associated ROOT file (http://root.cern.ch/root/files/tutorials/mockupx.root).
from ROOT import *
fileName = "mockupx.root"
file = TFile(fileName)
tree = file.Get("MyTree")
treeReader = TTreeReader("MyTree", file)
After this, I am a bit lost. I attempt to access variable information using the TTreeReader object and it doesn't quite work:
>>> rvMissingET = TTreeReaderValue(treeReader, "missingET")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/ROOT/v6-03-01/root/lib/ROOT.py", line 198, in __call__
result = _root.MakeRootTemplateClass( *newargs )
SystemError: error return without exception set
Where am I going wrong here?
TTreeReaderValue is a templated class, as shown in the example on the TTreeReader documentation, so you need to specify the template type.
You can do this with
rvMissingET = ROOT.TTreeReaderValue(ROOT.Double)(treeReader, "missingET")
The Python built-ins can be used for int and float types, e.g.
rvInt = ROOT.TTreeReaderValue(int)(treeReader, "intBranch")
rvFloat = ROOT.TTreeReaderValue(float)(treeReader, "floatBranch")
Also note that using TTreeReader in PyROOT is not recommended. (If you're looking for faster ntuple branch access in Python, you might look in to the Ntuple class I wrote.)

Categories