when running gekko for 1st time, got this error "cannot import name 'dump_csp_header' from werkzeug.http' - python-3.x

I'm using Spyder to run Python 3.7 where I installed gekko. However, I tried running a simple gekko code from Wikipedia and it gives me the following error:
ImportError: cannot import name 'dump_csp_header' from 'werkzeug.http' (C:\Users\zulfan.adiputra\AppData\Local\Continuum\anaconda3\envs\PythonNew\lib\site-packages\werkzeug\http.py)
When I check in the Anaconda prompt, the werkzeug installed is 1.0.0. What to do in this regard?
Thanks

You can resolve the error with:
Set GUI=False in m.solve()
Run the Python program from the command line with python myProgram.py. There are sometimes problems with the Flask server if you try to use the GUI by running from an IDE like Spyder or IDLE.
Instead of using the GUI option, it is relatively easy to plot the results with matplotlib. Here is an example script:
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
m = GEKKO() # initialize gekko
nt = 101
m.time = np.linspace(0, 2, nt)
# Variables
x1 = m.Var(value=1)
x2 = m.Var(value=0)
u = m.Var(value=0, lb=-1, ub=1)
p = np.zeros(nt) # mark final time point
p[-1] = 1.0
final = m.Param(value=p)
# Equations
m.Equation(x1.dt() == u)
m.Equation(x2.dt() == 0.5 * x1 ** 2)
m.Obj(x2 * final) # Objective function
m.options.IMODE = 6 # optimal control mode
m.solve() # solve
plt.figure(1) # plot results
plt.plot(m.time, x1.value, "k-", label=r"$x_1$")
plt.plot(m.time, x2.value, "b-", label=r"$x_2$")
plt.plot(m.time, u.value, "r--", label=r"$u$")
plt.legend(loc="best")
plt.xlabel("Time")
plt.ylabel("Value")
plt.show()

Related

Org-mode for emacs: python sessions

Emacs 27
Python 3.6
Windows 10
I have an issue working with sessions that causes org-mode to fail exporting.
Here is an illustration as an org-mode ecxerpt:
#+begin_src python :session one :results file
#return 'filename.png' --- DNU in session mode
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y = np.sin(x)
fig = plt.plot(x,y)
filename = 'filename.png'
plt.savefig(filename)
filename
#+end_src
Inside same session:
#+begin_src python :session one :results value
z = -1
z
#+end_src
Yet inside same session again:
#+begin_src python :session one :results value
z = z - 3
z
#+end_src
The issue is that if I run the first block code it works fine and I get the correct output, but then when running following 2 block code, it causes org-mode to stall for ever - no error message.
However if I run only the last 2 block codes, everything works as expected.
Any lead on what could be causing the problem would really be appreciated,
Thanks!
Sharing my finding and solving.
The issue seems to reside in matplotlib that needs to use the 'Agg' backend.
Org-mode code would look like:
#+begin_src python :session one :exports results :results file
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y = np.sin(x)
fig = plt.plot(x,y, color='red')
filename = 'filename.png'
plt.savefig(filename)
filename
#+end_src
Now, continuing on the same :session one:
#+begin_src python :session one :exports results
f'The last value of x is: {x[-1]}'
#+end_src
Produces the desired results. Hoping someone will find this helpful.
Thanks

Statsmodels.api.tsa.seasonal_decompose plot figsize

I am using statsmodels.api.tsa.seasonal_decompose to do some seasonal analysis of a time series.
I set it up using
decomp_viz = sm.tsa.seasonal_decompose(df_ts['NetConsumption'], period=48*180)
and then try and visualise it using
decomp_viz.plot()
The output was tiny so I tried to use the standard matplotlib command of
decomp_viz.plot(figsize=(20,20))
However, this got the warning:
TypeError: plot() got an unexpected keyword argument 'figsize'
The documentation says that a matplotlib.figure.Figure is returned from DecomposeResult.plot so I am unsure as to why this error is happening.
My version of statsmodels is 0.13.1 and I am aware that the documentation is for 0.14.0, but conda says that that version does not exist and that I cannot update to it.
Any thoughts would be appreciated.
DecomposeResult.plot doesn't pass keyword arguments. You can change the figure size after you create it:
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y += g.standard_normal(y.shape)
decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
fig = decomp_viz.plot()
fig.set_size_inches((16, 9))
# Tight layout to realign things
fig.tight_layout()
plt.show()
Alternatively, you can do the same by altering the MPL rc.
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
# Change default figsize
plt.rc("figure",figsize=(20,20))
PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y += g.standard_normal(y.shape)
decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
decomp_viz.plot()
plt.show()
which produces (cropped as too big for my screen)

diffeqpy slow in python with julia

I recently started working with diffeqpy to solve differential equations, after installation i used this example from the page main https://github.com/SciML/diffeqpy :
from diffeqpy import de
import time
import matplotlib.pyplot as plt
t0 = time.time()
def f(u,p,t):
return -u
u0 = 0.5
tspan = (0., 1.)
prob = de.ODEProblem(f, u0, tspan)
sol = de.solve(prob)
print(time.time()-t0)
plt.plot(sol.t,sol.u)
plt.show()
It takes me 8 seconds to compute the solution of this simple equation, is there something wrong?
Edit: I am using Julia 1.5.3, python 3.7.4, diffeqpy 1.1.0 and julia(python) 0.5.6

module '<file_name>' has no attribute '__path__'

I'm using commands on terminal to run this script (plot_test.py is the name of the file):
#python3
import pybullet as p
import pybullet_data as p_data
import time
import matplotlib.pyplot as plt
import numpy as np #to reshape for matplotlib
import os
import matplotlib.animation as animation
# os.environ['MESA_GL_VERSION_OVERRIDE'] = '3.3'
# os.environ['MESA_GLSL_VERSION_OVERRIDE'] = '330'
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
# plt.ion()
# GUI = 0
def animate(i):
graph_data = open('solved_states.bin','r').read()
lines = graph_data.split('\n')
time_stamp = [] #time
torque = [] #torque
for line in lines:
if len(line) > 1:
x, y = line.split(',')
time_stamp.append(float(y))
torque.append(float(x))
ax1.clear()
ax1.plot(time_stamp, torque,color='r',label='Torque')
ax1.set_title('Torque Vs Time')
ax1.set_xlabel('Time')
ax1.set_ylabel('Torque')
ax1.legend(loc="upper right")
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Altough it plots the graph and, I keep getting this error:
pybullet build time: Oct 8 2020 00:10:04
/usr/bin/python3: Error while finding module specification for 'plot_test.py' (AttributeError: module 'plot_test' has no attribute '__path__')
I'm new to python and I don't know how this works
I've seen similar questions like this before, but here, the file that I am working on is showing up the error.
Are you running the file with command
python -m plot_test.py
?
The flag -m runs the file as a module and then you need to omit the .py
If my assumption is true then you should be good with either:
python -m plot_test
or simply
python plot_test.py

How to apply Henze-Zirkler's Multivariate Normality Test in Jupyter notebook with rpy2

I am interested in Applying Henze-Zirkler's Multivariate Normality Test in python 3x and I was wondering if I may do so in python in Jupyter notebook.
I have fitted a VAR model with my data and the then I would like to test whether the residuals from this fitted VAR model are normally distributed.
How may I do so in Jupyter notebook using python?
This is another answer since I discover this method later. If you do not want to import the library of R into Python. One may call the output of R to python. i.e. one is capable of activating R function through python as follow:
import rpy2.robjects as robjects
from rpy2.robjects import r
from rpy2.robjects.numpy2ri import numpy2ri
from rpy2.robjects.packages import importr
import numpy as np
suppose that resi is a Dataframe in python say
# Create data
resi = pd.DataFrame(np.random.random((108, 2)), columns=['Number1','Number2'])
Then the code is as follow
#Converting the dataframe from python to R
# firt take the values of the dataframe to numpy
resi1=np.array(resi, dtype=float)
# Taking the variable from Python to R
r_resi = numpy2ri(resi1)
# Creating this variable in R (from python)
r.assign("resi", r_resi)
# Calling libraries in R
r('library("MVN")')
# Calling a function in R (from python)
r("res <- hzTest(resi, qqplot = F)")
# Retrieving information from R to Python
r_result = r("res")
# Printing the output in python
print(r_result)
This will generate the output:
Henze-Zirkler's Multivariate Normality Test
---------------------------------------------
data : resi
HZ : 2.841424
p-value : 1.032563e-06
Result : Data are not multivariate normal.
---------------------------------------------
Update per 2021-08-25
There has been some API changes both to the MVN package and ro rpy2. The following works with MVN version 5.9 and rpy2 version 3.4.
"""Interface file to access the R MVN package"""
import numpy as np
import rpy2.robjects.packages as rpackages
from rpy2.robjects import numpy2ri
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import StrVector
# Install packages, if they are not already installed
packages_to_install_if_needed = ("MVN",)
utils = rpackages.importr("utils")
utils.chooseCRANmirror(ind=1) # select the first mirror in the list
names_to_install = [x for x in packages_to_install_if_needed if not rpackages.isinstalled(x)]
if len(names_to_install) > 0:
utils.install_packages(StrVector(names_to_install))
# load the package
mvn = importr("MVN")
# Generate data
np_arr = np.random.multivariate_normal(np.ones(2), np.eye(2), size=100)
# activate automatic conversion from numpy to rpy2 interface objects
numpy2ri.activate()
# perform the work
res = mvn.mvn(np_arr)
print(res)
outputting
$multivariateNormality
Test HZ p value MVN
1 Henze-Zirkler 0.3885607 0.8343017 YES
$univariateNormality
Test Variable Statistic p value Normality
1 Anderson-Darling Column1 0.2443 0.7569 YES
2 Anderson-Darling Column2 0.3935 0.3692 YES
$Descriptives
n Mean Std.Dev Median Min Max 25th 75th
1 100 0.9619135 1.0353688 1.0222279 -1.994833 3.679615 0.2696537 1.758255
2 100 0.7664778 0.9134449 0.8121996 -1.568635 2.648268 0.2068718 1.418113
Skew Kurtosis
1 -0.2123274 -0.16171832
2 -0.3718904 -0.05279222
There is an open source Python package called Pingouin that provides Henze-Zirkler multivariate normality test and is tested against R's MVM.
https://pingouin-stats.org/generated/pingouin.multivariate_normality.html
Example extracted from the docs:
import pingouin as pg
data = pg.read_dataset('multivariate')
X = data[['Fever', 'Pressure', 'Aches']]
pg.multivariate_normality(X, alpha=.05)
>>> HZResults(hz=0.5400861018514641, pval=0.7173686509624891, normal=True)
There is a package in R that already does this test and it is called MVN
The first thing you have to do is to import MVN into python as described in here
Then go to your jupyter notebook and fit the VAR(1) model to your data as so
# Fit VAR(1) Model
results = Model.fit(1)
results.summary()
Store the residuals as resi
resi=results.resid
Then
# Call function from R
import os
os.environ['R_USER'] = '...\Lib\site-packages\rpy2'
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
pandas2ri.activate()
from rpy2.robjects.packages import importr
MVN = importr("MVN", lib_loc = "C:/.../R/win-library/3.3")
After importing MVN you can simply do the normality test as so
MVNresult =MVN.hzTest(resi, qqplot = 0)
If you press on
type(MVNresult)
you will find that it is an
rpy2.robjects.methods.RS4
Therefore, in this case you will find this link a very powerful in explaining the details
Then afterwards
tuple(MVNresult.slotnames())
This will show you the observations
('HZ', 'p.value', 'dname', 'dataframe')
Then you may get the values as so
np.array(MVNresult.slots[tuple(MVNresult.slotnames())[i]])[0]
where i stands for 0, 1, 2, 3 as 'HZ', 'p-value',...
So in case the p-value i.e. i=1 is less than 0.05 then residuals (resi) are not multivariate normal at 5% confidence level.

Resources