How do I fix syntax error in Matplot Lib? - matplotlib

I was attempting to create a graph of frequencies in audio data using Matplotlib with the following code (not yet completed) in Google Colaboratory. I imported the numpy, matplotlib, and wave libraries (not shown).
obj = wave.open("(name).wav", "rb")
sample_freq = obj.getframerate()
n_samples = obj.getnframes()
signal_wave = obj.readframes(-1)
obj.close()
time_audio = int(n_samples / sample_freq)
print(time_audio) # time audio
print(n_samples)
signal_array = np.frombuffer(signal_wave, dtype=np.int16)
times = np.linspace(0, time_audio, num=n_samples)
print(times)
plt.figure(figsize=(15,5))
plt.plot(times, signal_array)
and then some labeling of the axes etc. However, when I ran this code, I kept getting a syntax error. I'm not sure why, and I spent quite a bit of time trying to debug but couldn't find the obvious place of the syntax error. What am I doing wrong?

Related

Using SymPy for plotting 3d functions on a discord bot

I am not very good with programming, but I have been developing this discord bot on python that basically aims to fulfill some of the basic math functions one can do on Wolfram Alpha. So I figured I would use SymPy to plot implicit, parametric and 3d curves using plot_implicit,plot_parametric andplot3d respectively. Firstly, say I want to plot a parametric function (4t, t^2), the code fails to recognise 4t as 4*t. How do I fix that? Secondly, the 3d plotter shows me the same graph despite the fact I put plt.clear() in my code (Which shows an error, because it keeps saying 'clear' isn't defined). For reference, here is my code :
x, y = symbols('x y')
plt = plot3d(x * y, (x, -10, 10), (y, -10, 10), show=false)
img = BytesIO()
plt.save(img)
img.seek(0)
await ctx.send(file=discord.File(img, "graph.png"))
plt.clear()
So, how do I fix this?
Implicit multiplication
This issue was apparently solved here.
Repeated plots
I cannot help you with this since I couldn't replicate the problem and I do not use the discord library but here are a few debugging ideas:
Make absolutely sure it is not sending a file in the current working directory but rather from the BytesIO object.
Send file names that are different each time like graph1.png, graph2.png, ...
Maybe reimport plot3d inside the function for a completely fresh instance?
If these don't work, maybe write it in a comment so that others know that it did not solve the problem. Maybe they could help.
SymPy's plotting library is kind of buggy and fairly old. If it says that .clear is not an attribute then it might be best to not add that in.
Code
Here is my code with my advice without the discord library stuff.
from sympy import Expr
from io import BytesIO
def send_bio(bio: BytesIO):
# I don't use the discord library so I can't help with that
# this function stands in for the send file line
# let's try to make the file name unique
with open(f"output{hash(bio)}.png", "wb") as f:
f.write(bio.getbuffer())
def my_plot(expr: Expr):
# get the x and y variables with their specific traits
# like rational=True or something like that
x = [atom for atom in expr.expr_free_symbols if str(atom) == "x"][0]
y = [atom for atom in expr.expr_free_symbols if str(atom) == "y"][0]
from sympy.plotting import plot3d
plt = plot3d(expr, (x, -10, 10), (y, -10, 10), show=False)
img = BytesIO()
plt.save(img)
img.seek(0)
send_bio(img)
def string_to_expr(s: str) -> Expr:
# https://stackoverflow.com/questions/64311324/convert-xy-to-xy
from sympy.parsing.sympy_parser import parse_expr
from sympy.parsing.sympy_parser import standard_transformations, \
implicit_multiplication_application
transformations = (standard_transformations +
(implicit_multiplication_application,))
expr = parse_expr(s, transformations=transformations)
return expr
if __name__ == '__main__':
expr = string_to_expr("2xy")
my_plot(expr)
# next plot should be different
expr = string_to_expr("x+y")
my_plot(expr)

Using ggplot2 with Julia pluto notebook

I am working with a Pluto.jl notebook. I would like to use the ggplot2 R library to make some plots.
Following this example, if I run the following code in the Julia REPL then I can get a ggplot2 graph output.
using RCall
#rlibrary ggplot2
using DataFrames
df = DataFrame(v = [3,4,5], w = [5,6,7], x = [1,2,3], y = [4,5,6], z = [1,1,2])
ggplot(df, aes(x=:x,y=:y)) + geom_line()
Now, when I use the same code in a pluto.jl notebook (with each line being a separate cell), then I get the following error message:
Is there a way to get the ggplot2 image to appear inside the pluto notebook?
Similarly, if I just enter ggplot() into a cell, I get the same error, but ggplot not defined.
With #library Pluto.jl seems to be unable to find the R package.
However Pluto can handle this format:
#rimport ggplot2 as ggplot2
I managed to see the picture after clicking the "play" button 3 or 4 times. That is the end of good news - the Plut-RCall integration is kind of unstable. The graph shows in a separate Window that seems to hang - this is perhaps a story for opening an issue.
However what you can try to do is to save the image to a file and than visualize it:
begin
ggplot2.ggplot(df, ggplot2.aes(x=:x, y=:y)) + ggplot2.geom_line()
ggplot2.ggsave("myplot.png")
im1 = Images.load("myplot.png")
end
As a workaround, it is possible to override Base.show manually (see Pluto.jl/sample/test1.jl) with
function Base.show(io::IO, ::MIME"image/png", p::RObject{VecSxp})
(path, _) = mktemp()
R"ggsave($path, plot=$p, device = 'png')"
im = read(path)
rm(path)
write(io, im)
end
After that, cells which output anything of the type RObject{VecSxp} will show a PNG image:

TensorFlow example, MemoryError while run text_classification_character_cnn.py

I'm trying to run https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/text_classification_character_cnn.py for learning, but I get an error message:
File "C:\Users\natlun\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py", line 72, in load_csv_without_header
data = np.array(data)
MemoryError
I use CPU installation of TensorFlow and Python 3.5. Any ideas how to solve the problem?? Other scripts using a csv-file for input work fine.
I was having the same issue. And after many hours of reading and googling (and seeing your unanswered question), and just comparing the example with other examples that do run, I noticed that
dbpedia = tf.contrib.learn.datasets.load_dataset(
'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data, size='large')
should just be
dbpedia = tf.contrib.learn.datasets.load_dataset(
'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data)
Based off of what I've read about numpy, I'd bet the "size='large'" parameter causes an over allocation to a numpy array (which throws the memory error).
Or, when you don't set that parameter perhaps the input data is truncated.
Or some other thing. Anyway, I hope this helps others attempting to run this useful example!
--- Update ---
Without "size='large'" the load_dataset functions appears to create smaller training and test data sets (like 1/1000 the size).
After playing around with the example I realized I could manually load and use the whole data set without getting the memory error (assume it is saving the whole data set as it appears).
# Prepare training and testing data
##This was the provided method for setting up the data.
# dbpedia = tf.contrib.learn.datasets.load_dataset(
# 'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data)
# x_trainz = pandas.DataFrame(dbpedia.train.data)[1]
# y_trainz = pandas.Series(dbpedia.train.target)
# x_testz = pandas.DataFrame(dbpedia.test.data)[1]
# y_testz = pandas.Series(dbpedia.test.target)
##And this is my replacement.
x_train = []
y_train = []
x_test = []
y_test = []
with open("dbpedia_data/dbpedia_csv/train.csv", encoding='utf-8') as filex:
reader = csv.reader(filex)
for row in reader:
x_train.append(row[2])
y_train.append(int(row[0]))
with open("dbpedia_data/dbpedia_csv/test.csv", encoding='utf-8') as filex:
reader = csv.reader(filex)
for row in reader:
x_test.append(row[2])
y_test.append(int(row[0]))
x_train = pandas.Series(x_train)
y_train = pandas.Series(y_train)
x_test = pandas.Series(x_test)
y_test = pandas.Series(y_test)
The example seems to now be evaluating the whole training data set. But, the original code will probably need to be run once to get/put the data in the correct sub-folders. Also, even while evaluating the whole data set little memory is used (just a few hundred MB). Which, makes me think that the load_dataset function is broken in some way.

Code order influences the final result

I encountered a problem that the code order influences the final result. At first, the code works. After I move one line, tensorflow generates an error.
For example,
working version:
probs = net.get_output()
label_node = tf.placeholder(tf.int32, name='label_node')
top_1_op = tf.nn.in_top_k(probs, label_node, 1)
top_5_op = tf.nn.in_top_k(probs, label_node, 5)
threads = image_producer.start(session=sess, coordinator=coordinator)
for (labels, images) in image_producer.batches(sess):
top_1_result, top_5_result = sess.run([top_1_op, top_5_op],
feed_dict={input_node: images, label_node: labels})
Non-working version:
threads = image_producer.start(session=sess, coordinator=coordinator) # move here
probs = net.get_output()
label_node = tf.placeholder(tf.int32, name='label_node')
top_1_op = tf.nn.in_top_k(probs, label_node, 1)
top_5_op = tf.nn.in_top_k(probs, label_node, 5)
for (labels, images) in image_producer.batches(sess):
top_1_result, top_5_result = sess.run([top_1_op, top_5_op],
feed_dict={input_node: images, label_node: labels})
Tensorflow generates an error
"tensorflow.python.framework.errors.NotFoundError: FeedInputs: unable to find feed output label_node:0".
As you can see, tensorflow should be able to find "label_node:0". Actually, tensorflow cannot find top_1_op and top_5_op either.
The content of image_producer.start is something similar to:
op_A = ...
queue_runner = tf.train.QueueRunner(queue_B, [op_B] * num_concurrent)
session.run(op_A)
t = queue_runner.create_threads(session, coord=coordinator, start=True)
A more strange thing is that in the non-workable version, after I add two lines in image_producer.start, the code works again. For example, image_producer.start becomes
op_C = ... # new
session.run(op_C) # new
op_A = ...
queue_runner = tf.train.QueueRunner(queue_B, [op_B] * num_concurrent)
session.run(op_A)
t = queue_runner.create_threads(session, coord=coordinator, start=True)
Does anyone have an idea about possible causes of this problem? Or any idea about how to debug this?
It sounds like you are suffering from a bug that was fixed after TensorFlow 0.9.0 was released. In that version (and earlier) TensorFlow suffered from a race condition that could lead to unrecoverable errors if you modified the graph after queue runners (or other threads calling sess.run()) had started. The only workaround in version 0.9.0 is to start
the queue runners (i.e. the image_producer in your code) after the graph has been completely constructed.

Aliasing when saving matplotlib filled contour plot to .pdf or .eps

I'm generating a filed contour plot with the matplotlib.pyplot.contourf() function. The arguments in the call to the function are:
contourf(xvec,xvec,w,levels,cmap=matplotlib.cm.jet)
where
xvec = numpy.linspace(-3.,3.,50)
levels = numpy.linspace(-0.01,0.25,100)
and w is my data.
The resulting plot looks pretty good on screen, but when I save to pdf using a call to matplotlib.pyplot.savefig(), the resulting pdf has a lot of aliasing (I think that is what it is) going on. The call to savefig is simply savefig('filename.pdf'). I have tried using the dpi argument, but without luck. A call to matplotlib.get_backend() spits out 'TkAgg'.
I will attach a figure saved as pdf, compared to a figure saved as png (similar to what it looks like on screen) to demonstrate the problem:
png wihtout aliasing: https://dl.dropbox.com/u/6042643/wigner_g0.17.png
pdf with aliasing: https://dl.dropbox.com/u/6042643/wigner_g0.17.pdf
Please let me know if there are any other details I could give to help you give an answer. I should mention that saving as .eps gives similar bad results as saving to pdf. But the pdf shows the problem even clearer. My goal is to end up with a production quality .eps that I can attach to a latex document to be published as a scientific paper. I would be happy with some kind of work around where I save in one format, then convert it, if I can find a way that gives satisfying results.
Best,
Arne
After using the useful answer by #pelson for a while, I finally found a proper solution to this long-standing problem (currently in Matplotlib 3), which does not require multiple calls to contour or rasterizing the figure.
I refer to my original answer here for a more extensive explanation and examples.
In summary, the solution consists of the following lines:
cnt = plt.contourf(x, y, z)
for c in cnt.collections:
c.set_edgecolor("face")
plt.savefig('test.pdf')
I had no idea that contouring in pdf was so bad. You're right, I think the contours are being anti-aliased by the PDF renderers outside of matplotlib. It is for this reason I think you need to be particularly careful which application you use to view the resulting PDF - the best behaviour I have seen is with GIMP, but I'm sure there are plenty of other viewers which perform well.
To fix this problem (when viewing the PDF with GIMP), I was able to "rasterize" the contours produced with matplotlib to avoid the ugly white line problem:
import matplotlib.pyplot as plt
import numpy as np
xs, ys = np.mgrid[0:30, 0:40]
data = (xs - 15) ** 2 + (ys - 20) ** 2 + (np.sin(ys) + 10) ** 2
cs = plt.contourf(xs, ys, data, 60, cmap='jet')
# Rasterize the contour collections
for c in cs.collections:
c.set_rasterized(True)
plt.savefig('test.pdf')
This produced a contour plot which did not exhibit the problems you've shown.
Another alternative, perhaps better, approach, would be to fool the anti-aliasing by putting coloured lines below the contourf.
import matplotlib.pyplot as plt
import numpy as np
xs, ys = np.mgrid[0:30, 0:40]
data = (xs - 15) ** 2 + (ys - 20) ** 2 + (np.sin(ys) + 10) ** 2
# contour the plot first to remove any AA artifacts
plt.contour(xs, ys, data, 60, cmap='jet', lw=0.1)
cs = plt.contourf(xs, ys, data, 60, cmap='jet')
plt.savefig('test.pdf')
I should note that I don't see these problems if I save the figure as a ".ps" rather than a ".pdf" - perhaps that is a third alternative.
Hope this helps you get the paper looking exactly how you want it.