how can I debug a python file in google jupyter notebook - python

I run the following command to make the path to the package available in colab's environment as well as the PYTHONPATH.
%set_env PYTHONPATH /content/needle/python:/env/python
import sys
sys.path.append("/content/needle/python")
The folder structure is as follows
needle/python/needle
__init__.py
auograd.py
ops.py
And I wrote down the following code in colab's notebook
x = ndl.Tensor([1, 2, 3], dtype="float32")
y = x + 1
These classes and methods are defined in the autograd.py and ops.py.
******
class Tensor(Value):
grad: "Tensor"
def __init__(
self,
array,
*,
device: Optional[Device] = None,
dtype=None,
requires_grad=True,
**kwargs
):
******
I want to debug to see which methods are called during the generation of x, y and how the parameters change.
I've tried the pdb method, but I find that I can't set breakpoints in the autograd.py and ops.py file to see the call in more detail.
import pdb
pdb.set_trace()
x = ndl.Tensor([1, 2, 3], dtype="float32")
y = x + 1

Related

Import "lab_utils_uni could not be resolved

I need help with this Problem: Import "lab_utils_uni" could not be resolved. I installed numpy and matplotlib but lab_utils_uni didnt work. I am working with Visual Studio Code btw.
import numpy as np
import matplotlib.pyplot as plt
from lab_utils_uni import plt_intuition, plt_stationary, plt_update_onclick, soup_bowl
x_train = np.array([1.0, 2.0])
y_train = np.array([300.0, 500.0])
def compute_cost(x, y, w, b):
# number of training examples
m = x.shape[0]
cost_sum = 0
for i in range(m):
f_wb = w * x[i] + b
cost = (f_wb - y[i]) ** 2
cost_sum = cost_sum + cost
total_cost = (1 / (2 * m)) * cost_sum
return total_cost
plt_intuition(x_train,y_train)
It's a "reportMissingImports" warning.
lab_utils_uni is local drawing routines.
You need a lab_utils_uni.py file in your workspace.
Thatʻs simple, copy lab_utils_uni.py file to the same directory your code is saved to. You can download that python files from the "files" tab on Coursera online tab.
This code is from Coursera - DeepLearning AI course.
lab_utils_uni is a common util library which is used to run the code in the Online Lab in Coursera but doesn't show up in the Lab files section.
To look at the contents of the file try this:
import inspect, os
path = os.path.abspath(inspect.getfile(plt_stationary))
print(path)
f = open(path, 'r')
content = f.read()
print(content)

ImportError: Failed to import any qt binding, Python - Tensorflow

I'm starting my adventure with Tensorflow. I think I installed everything correctly, but when running this code, PyCharm returns an error:
Traceback (most recent call last):
File "C:/Users/tymot/Desktop/myenv3/env/Tensorflow/all_good.py", line 15, in <module>
import matplotlib.pyplot as plt
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 15, in <module>
from .backend_qt5 import (
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\backends\backend_qt5.py", line 19, in <module>
import matplotlib.backends.qt_editor.figureoptions as figureoptions
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\backends\qt_editor\figureoptions.py", line 20, in <module>
import matplotlib.backends.qt_editor.formlayout as formlayout
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\backends\qt_editor\formlayout.py", line 54, in <module>
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
File "C:\Users\tymot\Anaconda1\lib\site-packages\matplotlib\backends\qt_compat.py", line 158, in <module>
raise ImportError("Failed to import any qt binding")
ImportError: Failed to import any qt binding
My code which I am trying to run:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
num_features = 2
num_iter = 10000
display_step = int(num_iter / 10)
learning_rate = 0.01
num_input = 2 # units in the input layer 28x28 images
num_hidden1 = 2 # units in the first hidden layer
num_output = 1 # units in the output, only one output 0 or 1
#%% mlp function
def multi_layer_perceptron_xor(x, weights, biases):
hidden_layer1 = tf.add(tf.matmul(x, weights['w_h1']), biases['b_h1'])
hidden_layer1 = tf.nn.sigmoid(hidden_layer1)
out_layer = tf.add(tf.matmul(hidden_layer1, weights['w_out']), biases['b_out'])
return out_layer
#%%
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], np.float32) # 4x2, input
y = np.array([0, 1, 1, 0], np.float32) # 4, correct output, AND operation
y = np.reshape(y, [4,1]) # convert to 4x1
# trainum_inputg data and labels
X = tf.placeholder('float', [None, num_input]) # training data
Y = tf.placeholder('float', [None, num_output]) # labels
# weights and biases
weights = {
'w_h1' : tf.Variable(tf.random_normal([num_input, num_hidden1])), # w1, from input layer to hidden layer 1
'w_out': tf.Variable(tf.random_normal([num_hidden1, num_output])) # w2, from hidden layer 1 to output layer
}
biases = {
'b_h1' : tf.Variable(tf.zeros([num_hidden1])),
'b_out': tf.Variable(tf.zeros([num_output]))
}
model = multi_layer_perceptron_xor(X, weights, biases)
'''
- cost function and optimization
- sigmoid cross entropy -- single output
- softmax cross entropy -- multiple output, normalized
'''
loss_func = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=model, labels=Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss_func)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for k in range(num_iter):
tmp_cost, _ = sess.run([loss_func, optimizer], feed_dict={X: x, Y: y})
if k % display_step == 0:
#print('output: ', sess.run(model, feed_dict={X:x}))
print('loss= ' + "{:.5f}".format(tmp_cost))
# separates the input space
W = np.squeeze(sess.run(weights['w_h1'])) # 2x2
b = np.squeeze(sess.run(biases['b_h1'])) # 2,
sess.close()
#%%
# Now plot the fitted line. We need only two points to plot the line
plot_x = np.array([np.min(x[:, 0] - 0.2), np.max(x[:, 1]+0.2)])
plot_y = -1 / W[1, 0] * (W[0, 0] * plot_x + b[0])
plot_y = np.reshape(plot_y, [2, -1])
plot_y = np.squeeze(plot_y)
plot_y2 = -1 / W[1, 1] * (W[0, 1] * plot_x + b[1])
plot_y2 = np.reshape(plot_y2, [2, -1])
plot_y2 = np.squeeze(plot_y2)
plt.scatter(x[:, 0], x[:, 1], c=y, s=100, cmap='viridis')
plt.plot(plot_x, plot_y, color='k', linewidth=2) # line 1
plt.plot(plot_x, plot_y2, color='k', linewidth=2) # line 2
plt.xlim([-0.2, 1.2]); plt.ylim([-0.2, 1.25]);
#plt.text(0.425, 1.05, 'XOR', fontsize=14)
plt.xticks([0.0, 0.5, 1.0]); plt.yticks([0.0, 0.5, 1.0])
plt.show()
#%%
I think it follows another version of python. How can I run the code without error.
I installed qt-binding and added tensorflow to my PyCharm.
Any help will be appreciated.
make sure you have PyQt5 installed. you may open a python shell and try:
import PyQt5
if it fails then you can install it via:
pip install PyQt5
If you are on macOS or Linux be careful that you might need to run
pip3 install PyQt5
It solved my problem.
pip uninstall matplotlib
python -m pip install --upgrade pip
pip install matplotlib
I met the same ImportError when using %matplotlib qt.
Following Foad's answer solved my problem.
I use Archlinux so I tried
sudo pacman -S python-pyqt5
and it worked.
This is also useful:
https://docs.github.com/en/repositories/working-with-files/managing-large-files/about-large-files-on-github
If the file was added with your most recent commit:
$ git rm --cached giant_file
# Stage our giant file for removal, but leave it on disk
$ git commit --amend -CHEAD
# Amend the previous commit with your change
# Simply making a new commit won't work, as you need
# to remove the file from the unpushed history as well
I had problem with .so libraries which only revealed with
import PyQt5.QtCore
(libQt5Core.so.5: version 'Qt_5.15' not found...). The solution I have found is to install PyQt 5.14.x (pip install 'PyQt5<5.15')
I had that error on windows-VScode after the following magic command:
%matplotlib qt
Solved this error by completing the following steps:
Uninstall Anaconda
Reinstall Anaconda
Restart, open Anaconda, start VScode from there.

(WindowsError 183) cv2.imread/write how to make these function in different locations for the running script

I am running my script from the dir path C:\Automation\OCR\images. The PNG that we are reading in is also in that path. The output path is: C:\Automation\OCR\Drop. What happens is that I get an error in my shell saying WindowsError: [Error 183] Cannot create a file when that file already exists: 'C:\Automation\OCR\Drop' I want to be able to have the script isolated, read a PNG file from a particular folder, then output the preprocessed PNG in a different folder.
Pics below.
http://imgur.com/a/AbWUA
import cv2
import numpy as np
import math
import os
from matplotlib import pyplot as plt
from cycler import cycler
from PIL import Image, ImageEnhance
# Read PNG
dirname = 'C:\Automation\OCR\Drop'
os.mkdir(dirname)
img = cv2.imread('teleCapture.png', 0)
def bilateral_adaptive_threshold(img, ksize=20, C=0, mode='floor', true_value=255, false_value=0):
mask = np.full(img.shape, false_value, dtype=np.uint8)
kernel_l = np.array([[1] * (ksize) + [-ksize]], dtype=np.int16)
kernel_r = np.array([[-ksize] + [1] * (ksize)], dtype=np.int16)
kernel_u = np.array([[1]] * (ksize) + [[-ksize]], dtype=np.int16)
kernel_d = np.array([[-ksize]] + [[1]] * (ksize), dtype=np.int16)
if mode == 'floor':
delta = C * ksize
elif mode == 'ceil':
delta = -C * ksize
else: raise ValueError("Unexpected mode value. Expected value is 'floor' or 'ceil'.")
left_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_l, anchor=(ksize,0), delta=delta, borderType=cv2.BORDER_CONSTANT)
right_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_r, anchor=(0,0), delta=delta, borderType=cv2.BORDER_CONSTANT)
up_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_u, anchor=(0,ksize), delta=delta, borderType=cv2.BORDER_CONSTANT)
down_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_d, anchor=(0,0), delta=delta, borderType=cv2.BORDER_CONSTANT)
if mode == 'floor':
mask[((0 > left_thresh) & (0 > right_thresh)) | ((0 > up_thresh) & (0 > down_thresh))] = true_value
elif mode == 'ceil':
mask[((0 < left_thresh) & (0 < right_thresh)) | ((0 < up_thresh) & (0 < down_thresh))] = true_value
return mask
# Write modified PNG to the path
os.chdir(dirname)
cv2.imwrite('enhancedThresholdTeleCapture.png', img)
This line
img = cv2.imwrite('enhancedThresholdTeleCapture.png',0)
should instead be
cv2.imwrite('enhancedThresholdTeleCapture.png', img)
If you're getting unexpected behavior, be sure to search the OpenCV docs so you know the proper way to call functions and so that you can get the expected return value. For example, the docs for cv2.imwrite() show:
Python: cv2.imwrite(filename, img[, params]) → retval
Parameters:
filename – Name of the file.
image – Image to be saved.
params – ...
Along with a description of the function and what it does. The syntax
cv2.imwrite(filename, img[, params]) → retval
tells us everything we need to know to call the function. The function has two required arguments, filename and img. The last argument shown, params, is optional (and is only used for special options for certain filetypes). The brackets inside the list signify that it's optional. Then the arrow → shows the return value. If there is no return value for the function, it is simply None. In this case, there is a return value; named retval for "return value", so it's not very descriptive. Most unnamed return values are unimportant or are used for debugging purposes; in cases where some action is performed with the file system, it's pretty common for such a return value to be a boolean letting you know whether the action was successfully completed or not.
In the future, you can also run help(cv2.imwrite) (or with any other OpenCV function) inside your interpreter and you should be able to get the syntax line shown above so you can at least see how the function should be called.

Error message installing "Gnuplot-py" with "python setup.py install" command in Mac OS Sierra

I'm trying to install the "Gnuplot-py" package on my Mac using this command "python setup.py install" within the gnuplot-py-1.8 directory but I get this error:
Traceback (most recent call last):
File "/Users/sebydc77/Downloads/gnuplot-py-1.8/PlotItems.py", line
20, in
from cStringIO import StringIO ModuleNotFoundError: No module named 'cStringIO'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "setup.py", line 15, in
from init import version File "/Users/sebydc77/Downloads/gnuplot-py-1.8/init.py", line 166, in
from PlotItems import PlotItem, Func, File, Data, GridData File "/Users/sebydc77/Downloads/gnuplot-py-1.8/PlotItems.py", line 22, in
from StringIO import StringIO ModuleNotFoundError: No module named 'StringIO'
I spent at least 3 hours trying to solve this problem. I also tried different alternatives such as "pip install Gnuplot-py", "pip install download link..." etc...
(NOTE: I already have gnuplot installed on my machine)
As mentioned, the module doesn't work with Python 3.
But it isn't all that hard to write a custom plot function using gnuplot in Python. Below is an example that I wrote to make a graph of the infusion of carbon fiber laminates with epoxy resin. During the infusion, the pail of resin is placed on a scale, so I can write down the remaining weight every couple of minutes.
The main input for the plot function is a numpy array where I have recorded the time and amount of resin still in the bucket. This amount goes down over time, so I use this data to calculate the total amount of injected resin at every point and the speed with which the resin flows. That's where numpy comes in handy!
Basically this function creates a list of lines containing gnuplot commands and inline data, which is then joined into a single string and fed to gnuplot running as a subprocess.
import subprocess
import numpy as np
def plot(rawdata, filename, maxy=None, labels=False, n=2):
"""Plot injection data with gnuplot.
Arguments:
data: numpy array of shape (N,2)
(Time, weight) pairs as written down during the injection. The time
in minutes increases and the weight in grams decreases.
filename: string
Name to write the output figure to.
maxy: Maximum y coordinate for the left axis (injected weight).
The right axis (injection speed) is 1/10th of the left axis.
When set to None, automatic scaling is used.
labels: Label every n-th data point when true.
n: Print every n-th value as a label.
"""
gtype = 'lines'
if labels:
gtype = 'linespoints'
delta = rawdata[1:] - rawdata[:-1]
totals = np.array([[0, 0]] + [[dt, -dm] for dt, dm in delta])
som = np.cumsum(totals, axis=0)
print('harshoeveelheid "{}": {} g'.format(filename, som[-1, 1]))
if maxy is None:
maxy = round(som[-1, 1], -2)
dm = np.array([[0]] + [[-dm/dt] for dt, dm in delta])
newdata = np.hstack((som, dm))
newdata[0, 2] = newdata[1, 2]
l1 = 'set label "{:.0f} g" at {},{} center offset 0,0.5'
l2 = 'set label "{:.0f} g/min" at {},second {} center offset 0,0.5'
p1 = 'plot "-" with {gt} ls 1 title "harshoeveelheid", ' \
'"-" with {gt} axes x1y2 ls 2 title "injectiesnelheid"'
lp1 = ', "-" using 1:2:2 with labels right offset character 0.4,0.7' \
'font "Alegreya, 8" tc ls 1 notitle'
lp2 = ', "-" using 1:2:2 axis x1y2 with labels left offset character ' \
'0.5,0.7 font "Alegreya, 8" tc ls 2 notitle'
text = ['set terminal pdfcairo enhanced color dashed font "Alegreya, 11" '
'rounded size 12.8 cm, 7.0 cm',
'set xlabel "tijd [min]"',
'set ylabel "harshoeveelheid [g]"',
'set y2label "injectiesnelheid [g/min]"',
'set y2tics',
'set yrange [0:{:.0f}]'.format(maxy),
'set y2range [0:{:.0f}]'.format(maxy/10),
'set key left bottom',
'set grid']
if not labels:
text.append(l1.format(newdata[-1, 1], newdata[-1, 0], newdata[-1, 1]))
text.append(l2.format(newdata[-1, 2], newdata[-1, 0], newdata[-1, 2]))
text.append('set output "{}"'.format(filename + '.pdf'))
text.append(p1.format(gt=gtype))
if labels:
text[-1] += lp1 + lp2
data1 = ['{:2.0f} {:4.0f}'.format(a, b) for a, b, _ in newdata]
data2 = ['{:2.0f} {:4.0f}'.format(a, b) for a, _, b in newdata]
text += data1
text.append('e')
text += data2
text.append('e')
if labels:
data1 = ['{:2.0f} {:4.0f}'.format(a, b) for a, b, _ in newdata[n-1::n]]
data2 = ['{:2.0f} {:4.0f}'.format(a, b) for a, _, b in newdata[n-1::n]]
text += data1
text.append('e')
text += data2
text.append('e')
p = subprocess.Popen(['gnuplot'], stdin=subprocess.PIPE)
_, _ = p.communicate(input='\n'.join(text).encode('utf-8'))
The output looks something like this:
Note that the style of the graph is determined by the settings in my gnuplotrc file.

Calling R functions in rpy2 error - "argument is missing"

I'm facing some issues in using rpy2 package in Python.
Actually, I am trying to call a function called upliftRF (of the library "uplift" in R) by passing some arguments.
As stated on page 27 of https://cran.r-project.org/web/packages/uplift/uplift.pdf, one of the arguments of the function can be x or a formula that describes the model to fit based on a dataframe ("data" parameter in arguments).
When executing the code of page 29 in R, everything is running without any problems. However, I have some issues in rpy2. Here is my code :
import pandas.rpy.common as com
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
uplift = importr('uplift')
kwargs = {'n': 1000, 'p' : 20, 'rho' : 0, 'sigma' : np.sqrt(2), 'beta.den': 4}
dd = uplift.sim_pte(**kwargs)
ddPD = pandas2ri.ri2py(dd)
ddPD['treat'] = [1 if x==1 else 0 for x in ddPD['treat']]
dd = com.convert_to_r_dataframe(ddPD)
kwargs2 = {'formula':'y ~ X1 + X2 + X3 + X4 + X5 + X6 + trt(treat)',
'mtry':3,'ntree':200,'split_method':'KL','minsplit':200,'data':dd}
fit1 = uplift.upliftRF(**kwargs2)
Then, I get this error :
RRuntimeError: Error in is.data.frame(x) : argument "x" is missing, with no default
However, "x" is not a mandatory parameter of the function.
I guess that the error will be the same for any other R function that has one argument which is not mandatory at all.
Thank you for your help !
import pandas.rpy.common as com
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
uplift = importr('uplift')
Next, you should be able to use the most-common way to call Python functions because importr is "translating" named parameters in the
definition of the R function into syntactically-valid Python names.
dd = uplift.sim_pte(n = 1000, p = 20, rho = 0,
sigma = np.sqrt(2), beta_den = 4)
At this point you appear to have an R data.frame. Going to pandas to add a column, then back to R, is definitely possible:
ddPD = pandas2ri.ri2py(dd)
ddPD['treat'] = [1 if x==1 else 0 for x in ddPD['treat']]
dd = com.convert_to_r_dataframe(ddPD)
However, unless there is a good reason I'd recommend to stick to one conversion scheme when shuttling between pandas and rpy2. The one
defined in pandas or the one defined in rpy2 as consistency across
is presumably less tested. The error RRuntimeError: Error: $ operator is invalid for atomic vectors might come from this.
The alternative to going to pandas is to use the eminently expressive R package dplyr. rpy2 is providing a tailored interface to it since version 2.7.0:
from rpy2.robjects.lib import dplyr
dd = (dplyr.DataFrame(dd)
.mutate(treat = 'ifelse(treat==1, 1, 0)')
It was already pointed out in your answer that the formula should
be declared as such (formulas are language objects in R, but there is
no equivalent at the language level in Python). When writing this
as a common Python call:
fit1 = uplift.upliftRF(formula = robjects.Formula('y ~ X1 + X2 + X3 + X4 + X5 + X6 + trt(treat)'),
mtry = 3,
ntree = 200,
split_method = 'KL',
minsplit = 200,
data = dd)

Categories

Resources