RuntimeWarning: invalid value encountered in true_divide - python-3.4

I have to make a program using NLMS method, to minimize the noise in ECG signal.
from __future__ import division
from numpy import *
import numpy as np
#import matplotlib as plt
import matplotlib.pyplot as plt
import os
clear = lambda: os.system('cls')
clear()
meu=1e-05
file=open('ecg.txt','r') #this file i attached at below
data=file.readlines()
x=data[:1000]
xx=data[1001:2001]
N=len(x)
NN=len(xx)
t=np.zeros((N,1),float)
X=np.zeros((4,1),float)
Z=np.array([0,0,0,0],float)
w=random.rand(4,1)
y=np.zeros((N,1),float)
yp=np.zeros((N,1),float)
e=np.zeros((N,1))
j=np.zeros((N,1),float)
for n in range(len(x)):
X[1:len(X)-1]=X[0:len(X)-2]
X[0]=x[n]
sum=0
for k in range(len(X)):
sum+=(w[k].T*X[k])
y[n]=sum
e[n]=subtract(X[0],y[n])
j[n]=e[n]*e[n]
for l in range(len(X)):
w[l] = w[l]+(meu*e[n]*X[l])/((X[l]*X[l])*(X[l]*X[l])); #when division takes place it give error
MSE=mean(j,1)
plt.plot(10 * log10(MSE))
plt.title('MSE')
plt.show()
#print(' output\n')
#print(y,'\n')
#print(' data\n')
#print(x,'\n')
print(w)
#y.astype(float)
#pp=np.int16(y)
plt.plot(x)
plt.yscale('log')
plt.title('x')
plt.show()
plt.plot(y)
plt.yscale('log')
plt.title('y')
plt.show()
plt.plot(w)
plt.yscale('log')
plt.title('w')
plt.show()
plt.plot(e)
#plt.yscale('log')
plt.title('e')
plt.show()
plt.plot(e)
plt.plot(x)
plt.plot(y)
plt.show()
plt.plot(yp)
plt.title('yp')
plt.show()
#plt.plot(t)
#plt.yscale('log')
#plt.show()
I keep getting this error:
Warning (from warnings module):
w[l] = w[l]+(meu*e[n]*X[l])/((X[l]*X[l])*(X[l]*X[l]));
RuntimeWarning: invalid value encountered in true_divide
Result:
>>w
array([[ 0.86035037],
[ 0.35119551],
[ 0.40570589],
[ nan]])
>>X
array([[ 0.19258605],
[ 0.19442064],
[ 0.19243968],
[ 0. ]])
>>w[l]
array([ nan])
>>X[l]
array([ 0.])
I can't figure it out, what is wrong with the code?
ECG text file:
click this link to view the file

Related

NetworkX: draw_networkx not drawing labels:, draw results in'_AxesStack' object is not callable

Trying to draw two nodes graph with latest version of networkx-3.0 and latest version of matplotlib-3.6.3. Drawing does not show labels on the plot:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node(1, text='foo')
G.add_node(2, text='bar')
G.add_edge(1,2)
print("Node labels: ", nx.get_node_attributes(G, 'text'))
nx.draw_networkx(G, with_labels=True)
>> Node labels: {1: 'foo', 2: 'bar'}
Shows graph without labels. Why?
And this results in error:
nx.draw(G)
plt.show()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_16652\3657615245.py in <module>
----> 1 nx.draw(G)
2 plt.show()
~\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py in draw(G, pos, ax, **kwds)
111 cf.set_facecolor("w")
112 if ax is None:
--> 113 if cf._axstack() is None:
114 ax = cf.add_axes((0, 0, 1, 1))
115 else:
TypeError: '_AxesStack' object is not callable
<Figure size 640x480 with 0 Axes>
To answer the first question: the nx graph doesn't "know" that you want to look at its text attribute for the label, so it just goes with the initial node name.
Instead, you could pass a relabeled graph into the draw function
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node(1, text='foo')
G.add_node(2, text='bar')
G.add_edge(1,2)
print("Node labels: ", nx.get_node_attributes(G, 'text'))
nx.draw_networkx(nx.relabel_nodes(G, nx.get_node_attributes(G, 'text')),
with_labels=True, node_color = 'orange')
plt.show()
The result:

I want to order the data through jupyter notebook of the graph

(https://i.stack.imgur.com/oyGBO.png)
(https://i.stack.imgur.com/nDmnh.png)
be able to sort the values ​​and have them relocated to their labels correctly.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import csv
import pandas as pd
data_path = "id.csv"
id_csv = pd.read_csv(data_path)
etiquetas=['10','1','5']
rating=id_csv.rating.value_counts()
rating_count=id_csv.rating_count.value_counts()
confirmation_count=id_csv.confirmation_count.value_counts()
n = len(rating)
x = np.arange(n)
width = 0.25
fig, ax = plt.subplots()
plt.bar(x - width,rating,width=width, label='Rating',color=['green'])
plt.bar(x,rating_count, width=width, label='Rating Count',color=['gold'])
plt.bar(x + width,confirmation_count, width=width, label='Confirmation Count', color=['red'])
for i,j in zip (x, rating):
ax.annotate(j,xy=(i-0.3, j+0.3))
for i,j in zip (x, rating_count):
ax.annotate(j,xy=(i+0.0, j+0.3))
for i,j in zip (x, confirmation_count):
ax.annotate(j,xy=(i+0.2, j+0.3))
ax.set_title("Ratings Graph")
ax.set_ylabel("Population")
ax.set_xlabel("assessment")
ax.set_xticks(x)
plt.legend(loc='best')
ax.set_xticklabels(etiquetas)

not able to load the image and pass it to preprocessing for the model prediction

I am trying to upload the image from the local system within the same directory. Post uploading, when I am passing through open cv split and merge for b,g, and r colors, i get the error ValueError: not enough values to unpack (expected 3, got 0)
Error :
this is the error that is showing is there any possibility to debug in the streamlit where I can track changes at different lines of code? (As in the image path,) when executed in a google collab as individual ipynb files run properly and I get by required classification
ValueError: not enough values to unpack (expected 3, got 0)
Traceback:
File "C:\Users\ADARSH\anaconda3\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 564, in _run_script
exec(code, module.__dict__)
File "C:\Users\ADARSH\streamlit\deploy_test.py", line 76, in <module>
main()
File "C:\Users\ADARSH\streamlit\deploy_test.py", line 68, in main
mask = imageToTensor('image')
File "C:\Users\ADARSH\streamlit\deploy_test.py", line 44, in imageToTensor
b,g,r = cv2.split(bgr_img)
My entire streamlit app code
from pathlib import Path
import cv2
import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import random
from sklearn.utils import shuffle
from tqdm import tqdm_notebook
import streamlit as st
from PIL import Image as impo
from fastai import *
from fastai.vision import *
from torchvision.models import *
class MyImageItemList(ImageList):
def open(self, fn:PathOrStr)->Image:
img = readCroppedImage(fn.replace('/./','').replace('//','/'))
# This ndarray image has to be converted to tensor before passing on as fastai Image, we can use pil2tensor
return vision.Image(px=pil2tensor(img, np.float32))
def read_image(name):
image = st.file_uploader("Upload an "+ name, type=["png", "jpg", "jpeg",'tif'])
if image is not None:
im = impo.open(image)
im.filename = image.name
return image
def imageToTensor(image):
sz = 68
bgr_img = cv2.imread(image)
b,g,r = cv2.split(bgr_img)
rgb_img = cv2.merge([r,g,b])
# crop to center to the correct size and convert from 0-255 range to 0-1 range
H,W,C = rgb_img.shape
rgb_img = rgb_img[(H-sz)//2:(sz +(H-sz)//2),(H-sz)//2:(sz +(H-sz)//2),:] / 256
return vision.Image(px=pil2tensor(rgb_img, np.float32))
def learn_infernce():
return load_learner('./')
def get_prediction(image):
if st.button('Classify'):
pred, pred_idx, probs = learn_inference.predict(image)
classes = ['negative', 'tumor']
st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
else:
st.write(f'Click the button to classify')
def main():
st.set_page_config(page_title='Cancer detection', page_icon=None, layout='centered', initial_sidebar_state='auto')
image = read_image('image')
mask = imageToTensor('image')
if mask is not None:
get_prediction('mask')
if __name__ == "__main__":
main()
In your main function you are passing 'str' instead of variables, and also I think your read_image is not well structured.
What you should do is to first save the uploaded file in a directory and fetch the file from that directory and pass it to imageToTensor() as a parameter. That's one work around which will give you a total control over the file. Otherwise you will get other error messages after the first error is fixed.
You can automate some few lines of code in a separate python file to delete the uploaded file from the directory with a given duration.
Note: Keep an eye on the imports because I skipped them to keep the code short
class MyImageItemList(ImageList):
def open(self, fn:PathOrStr)->Image:
img = readCroppedImage(fn.replace('/./','').replace('//','/'))
# This ndarray image has to be converted to tensor before passing on as fastai Image, we can use pil2tensor
return vision.Image(px=pil2tensor(img, np.float32))
# Refactured read_image()
def get_uploaded_image():
upload = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg",'tif'])
if upload is not None:
st.write(upload.name)
# Create a directory and save the image file before proceeding.
file_path = os.path.join("data/uploadedImages/", upload.name)
with open(file_path, "wb") as user_file:
user_file.write(upload.getbuffer())
return file_path # fixed indentation
def imageToTensor(image):
sz = 68
bgr_img = cv2.imread(image)
b,g,r = cv2.split(bgr_img)
rgb_img = cv2.merge([r,g,b])
# crop to center to the correct size and convert from 0-255 range to 0-1 range
H,W,C = rgb_img.shape
rgb_img = rgb_img[(H-sz)//2:(sz +(H-sz)//2),(H-sz)//2:(sz +(H-sz)//2),:] / 256
return vision.Image(px=pil2tensor(rgb_img, np.float32))
def learn_infernce():
return load_learner('./')
def get_prediction(image):
if st.button('Classify'):
pred, pred_idx, probs = learn_inference.predict(image)
classes = ['negative', 'tumor']
st.write(f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}')
else:
st.write(f'Click the button to classify')
def main():
st.set_page_config(page_title='Cancer detection', page_icon=None, layout='centered', initial_sidebar_state='auto')
# Holds the saved file path
user_image = get_uploaded_image()
if user_image is not None:
# Pass the path to imageToTensor() as a parameter.
mask = imageToTensor(user_image)
get_prediction(mask)
if __name__ == "__main__":
main()
output:

Check partial derivatives and finite differences error

In relation with my previous question Scaled paraboloid and derivatives checking, I see that you fixed related to running the problem once. I wanted to try but I still have a problem with the derivatives checking and finite differences showed in the following code:
""" Unconstrained optimization of the scaled paraboloid component."""
from __future__ import print_function
import sys
import numpy as np
from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer
class Paraboloid(Component):
def __init__(self):
super(Paraboloid, self).__init__()
self.add_param('X', val=np.array([0.0, 0.0]))
self.add_output('f_xy', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
X = params['X']
x = X[0]
y = X[1]
unknowns['f_xy'] = (1000.*x-3.)**2 + (1000.*x)*(0.01*y) + (0.01*y+4.)**2 - 3.
def linearize(self, params, unknowns, resids):
""" Jacobian for our paraboloid."""
X = params['X']
J = {}
x = X[0]
y = X[1]
J['f_xy', 'X'] = np.array([[ 2000000.0*x - 6000.0 + 10.0*y,
0.0002*y + 0.08 + 10.0*x]])
return J
if __name__ == "__main__":
top = Problem()
root = top.root = Group()
#root.fd_options['force_fd'] = True # Error if uncommented
root.add('p1', IndepVarComp('X', np.array([3.0, -4.0])))
root.add('p', Paraboloid())
root.connect('p1.X', 'p.X')
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.add_desvar('p1.X',
lower=np.array([-1000.0, -1000.0]),
upper=np.array([1000.0, 1000.0]),
scaler=np.array([1000., 0.001]))
top.driver.add_objective('p.f_xy')
top.setup()
top.check_partial_derivatives()
top.run()
top.check_partial_derivatives()
print('\n')
print('Minimum of %f found at (%s)' % (top['p.f_xy'], top['p.X']))
First check works fine but the second check_partial_derivatives gives weird results for FD :
[...]
Partial Derivatives Check
----------------
Component: 'p'
----------------
p: 'f_xy' wrt 'X'
Forward Magnitude : 1.771706e-04
Reverse Magnitude : 1.771706e-04
Fd Magnitude : 9.998228e-01
Absolute Error (Jfor - Jfd) : 1.000000e+00
Absolute Error (Jrev - Jfd) : 1.000000e+00
Absolute Error (Jfor - Jrev): 0.000000e+00
Relative Error (Jfor - Jfd) : 1.000177e+00
Relative Error (Jrev - Jfd) : 1.000177e+00
Relative Error (Jfor - Jrev): 0.000000e+00
Raw Forward Derivative (Jfor)
[[ -1.77170624e-04 -8.89040341e-10]]
Raw Reverse Derivative (Jrev)
[[ -1.77170624e-04 -8.89040341e-10]]
Raw FD Derivative (Jfd)
[[ 0.99982282 0. ]]
Minimum of -27.333333 found at ([ 6.66666658e-03 -7.33333333e+02])
And (may be not related) when I try to set root.fd_options['force_fd'] = True (just to see), I get an error during the first check :
Partial Derivatives Check
----------------
Component: 'p'
----------------
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 59, in
File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\problem.py", line 1827, in check_partial_derivatives
u_size = np.size(dunknowns[u_name])
File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\vec_wrapper.py", line 398, in __getitem__
return self._dat[name].get()
File "d:\rlafage\OpenMDAO\OpenMDAO\openmdao\core\vec_wrapper.py", line 223, in _get_scalar
return self.val[0]
IndexError: index 0 is out of bounds for axis 0 with size 0
I work with OpenMDAO HEAD (d1e12d4).
This is just a stepsize problem for that finite difference. The 2nd FD occurs at a different point (the optimum), and it must be more sensitive at that point.
I tried with central difference
top.root.p.fd_options['form'] = 'central'
And got much better results.
----------------
Component: 'p'
----------------
p: 'f_xy' wrt 'X'
Forward Magnitude : 1.771706e-04
Reverse Magnitude : 1.771706e-04
Fd Magnitude : 1.771738e-04
The exception when you set 'fd' is a real bug related to the scaler on the des_var being an array. Thanks for the report on that; we'll get a story up to fix it.

openmdao v1.4 optimization with metamodel

I which to perform an optimization with openmdao 1.4 on a metamodel. Using the tutorials I have build u p problem that i do not mange to solve: I think the problem is coming from a misuse of setup() and run() : I do not manage to train my metamodel and to optimize on it at the same time (perhpas I should use two differentes "groups" to do this ..)
Here is my code :
from __future__ import print_function
from openmdao.api import Component, Group, MetaModel ,IndepVarComp, ExecComp, NLGaussSeidel, KrigingSurrogate, FloatKrigingSurrogate
import numpy as np
class KrigMM(Group):
''' FloatKriging gives responses as floats '''
def __init__(self):
super(KrigMM, self).__init__()
# Create meta_model for f_x as the response
pmm = self.add("pmm", MetaModel())
pmm.add_param('x', val=0.)
pmm.add_output('f_x:float', val=0., surrogate=FloatKrigingSurrogate())
self.add('p1', IndepVarComp('x', 0.0))
self.connect('p1.x','pmm.x')
# mm.add_output('f_xy:norm_dist', val=(0.,0.), surrogate=KrigingSurrogate())
if __name__ == '__main__':
# Setup and run the model.
from openmdao.core.problem import Problem
from openmdao.drivers.scipy_optimizer import ScipyOptimizer
from openmdao.core.driver import Driver
import numpy as np
import doe_lhs
#prob = Problem(root=ParaboloidProblem())
###########################################################
prob = Problem(root=Group())
prob.root.add('meta',KrigMM(), promotes=['*'])
prob.driver = ScipyOptimizer()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.add_desvar('p1.x', lower=0, upper=10)
prob.driver.add_objective('pmm.f_x:float')
prob.setup()
prob['pmm.train:x'] = np.linspace(0,10,20)
prob['pmm.train:f_x:float'] = np.sin(prob['pmm.train:x'])
prob.run()
print('\n')
print('Minimum of %f found for meta at %f' % (prob['pmm.f_x:float'],prob['pmm.x'])) #predicted value
I believe your problem is actually working fine. Its just that the sinusiod you've picked has an local optimum at 0.0, which happens to be your initial condition.
If I change the initial condition as follows:
prob.setup()
prob['p1.x'] = 5
prob['pmm.train:x'] = np.linspace(0,10,20)
prob['pmm.train:f_x:float'] = np.sin(prob['pmm.train:x'])
prob.run()
I get:
Optimization terminated successfully. (Exit mode 0)
Current function value: [-1.00004544]
Iterations: 3
Function evaluations: 3
Gradient evaluations: 3
Optimization Complete
-----------------------------------
Minimum of -1.000045 found for meta at 4.710483

Resources