Error using matplotlib after updating iPython [duplicate] - python-2.7

This question already has answers here:
IPython Notebook locale error [duplicate]
(4 answers)
Closed 6 years ago.
After updating IPython I constantly have problems with matplotlib. At the beginning of my notebook I have
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import scipy
from qutip import *
import time
Which generate a screen full of issues but the final part is
/Users/murray/anaconda/lib/python2.7/locale.pyc in _parse_localename(localename)
473 elif code == 'C':
474 return None, None
--> 475 raise ValueError, 'unknown locale: %s' % localename
476
477 def _build_localename(localetuple):
ValueError: unknown locale: UTF-8
There were other issues before this which I managed to fix. Similar things have been reported here but no solution which works for me. One solution I found online suggested running
export LANG="it_IT.UTF-8"
in the terminal window (plus about 8 other similar commands). This worked but everytime I restart the notebook I have to reenter all of this. As you might guess I am not an expert - I would assume there is a more permanent fix for this problem

As a work around, you can put export LANG="it_IT.UTF-8" and the "8 other similar commands" into your .profile (assuming your are on Mac OS X).
At the end of this file /Users/murray/.profile write:
# Fix for matplotlib imports in IPython
export LANG="it_IT.UTF-8"
# your other 8 lines here without the # in front
You need to start a new terminal window. In there, start a new IPython session.

Related

TensorFlow scatter_nd function is not working with placeholder and complex input

I am using tf.scatter_nd to update complex values at some index.
It seems the real and imaginary parts are somehow getting added together by this function. My question is how to make it work with placeholders. Here is the minimal working example where the variables b and e should have same values.
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
update=np.asarray([1.+2j])
idx=tf.constant( [[0]])
shp=tf.constant([1])
# works with constants
a=tf.constant(update)
b=tf.scatter_nd(idx,a,shp)
with tf.Session() as sess:
print sess.run(b) # correct output: 1.+2j
#Does not work with placeholders
d=tf.placeholder(tf.complex128)
e=tf.scatter_nd(idx,d,shp)
with tf.Session() as sess:
print sess.run(e,feed_dict={d:update}) # WRONG output: 3.+0j
I am using Anaconda python 2.7 + TensorFlow 1.7 GPU version installed using the conda command.
Edit:
The issue occurs when running the code on GPU. CPU version works correctly.
Here is the updated code to reproduce the issue in TensorFlow-GPU 1.8 installed using Anaconda Python 2.7.
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
update=np.asarray([1.+2j])
idx=tf.constant( [[0]])
shp=tf.constant([1])
a=tf.placeholder(tf.complex128)
with tf.device("/cpu:0"):
b=tf.scatter_nd(idx,a,shp)
with tf.device("/gpu:0"):
c=tf.scatter_nd(idx,a,shp)
with tf.Session() as sess:
print 'Correct output on CPU', sess.run(b,feed_dict={a:update})
print 'Wrong output on GPU',sess.run(c,feed_dict={a:update})
I saw this thread and this thread but could not find how to resolve it. Is there any alternative to tf.scatter_nd that will run on GPU?

Python: sys prevents print of elements

In Python 2.7 I have the following and I debug through IDLE:
print 'Here'
import sys
reload(sys)
sys.setdefaultencoding('cp1252')
print 'There'
what I get in return is
Here
So after I have set the default encoding it does not print the desired output.
Could this be due by conflicts with the IDLE encoding?
Because it is unable to find reference to setdefaultencoding from sys. That is why it is not printing 'There'
setdefaultencoding is deprecated and one should never use it!
Have a look at the following link.
Why should we NOT use sys.setdefaultencoding(“utf-8”) in a py script?

Using pygame midi module [duplicate]

I'm trying to play a sound with the pygame.midi module. Here is the code I
use :
#!/usr/bin/env python
import pygame.midi
import time
pygame.midi.init()
print pygame.midi.get_default_output_id()
print pygame.midi.get_device_info(0)
player = pygame.midi.Output(0)
player.set_instrument(0)
print 'Playing...'
player.note_on(64)
time.sleep(1)
player.note_off(64)
print 'Played'
pygame.midi.quit()
I've found similar codes while searching for exemples, here is the output :
0
('ALSA', 'Midi Through Port-0', 0, 1, 0)
Playing...
Played
PortMidi call failed...
PortMidi: `Bad pointer'
type ENTER...
No sound is played, and I didn't find any info about the PortMidi error which
occurs surprisingly after pygame.midi quits.
Do you have any idea? I'm running an debian-based linux distribution if that
can help.
There are two small problems. The sound is not played because you don't set the velocity of the note. Try setting it to 127 (maximum) to hear the sound. The other problem is that you don't delete the midi output object at the end before quitting. This leads to the "PortMidi: `Bad pointer'" error at the end. So here is the corrected code that should work properly:
import pygame.midi
import time
pygame.midi.init()
player = pygame.midi.Output(0)
player.set_instrument(0)
player.note_on(64, 127)
time.sleep(1)
player.note_off(64, 127)
del player
pygame.midi.quit()
thanks for your code, helped me to start with midi and python.
It seems to me you forgot the velocity (sort of volume) information in the note_on, note_off events. The default value is 0, so the note would 'play', but would not be audible.
About the quit error message you get... I can't help, i dont know about Linux and ALSA. For reference, this worked fine for me in a Win Vista box using the default midi mapper. This simply plays either a note, an arpeggio or a chord, using a base note and a major chord structure.
import pygame
import time
import pygame.midi
pygame.midi.init()
player= pygame.midi.Output(0)
player.set_instrument(48,1)
major=[0,4,7,12]
def go(note):
player.note_on(note, 127,1)
time.sleep(1)
player.note_off(note,127,1)
def arp(base,ints):
for n in ints:
go(base+n)
def chord(base, ints):
player.note_on(base,127,1)
player.note_on(base+ints[1],127,1)
player.note_on(base+ints[2],127,1)
player.note_on(base+ints[3],127,1)
time.sleep(1)
player.note_off(base,127,1)
player.note_off(base+ints[1],127,1)
player.note_off(base+ints[2],127,1)
player.note_off(base+ints[3],127,1)
def end():
pygame.quit()
To use it, just import the module and, for example, type a command like go(60), chord (60, major) or arp(60, major)
The error message shows that your output device is a "MIDI Through Port" - which isn't capable of making sounds on it's own. You would have to connect it (e.g. using qjackctl or any other tool letting you connect ALSA MIDI ports) to a software synthesizer like qsynth.
Try importing the entire pygame module:
import pygame
not
import pygame.midi

Loading an image using Pyglet

I am playing around with pyglet 1.2alpha-1 and Python 3.3. I have the following (extremely simple) application and cannot figure out what my issue is:
import pyglet
window = pyglet.window.Window()
#image = pyglet.resource.image('img1.jpg')
image = pyglet.image.load('img1.jpg')
label = pyglet.text.Label('Hello, World!!',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
#window.event
def on_draw():
window.clear()
label.draw()
# image.blit(0,0)
pyglet.app.run()
With the above code, my text label will appear as long as image.blit(0, 0) is commented out. However, if I try to display the image, the program crashes with the following error:
File "C:\Python33\lib\site-packages\pyglet\gl\lib.py", line 105, in errcheck
raise GLException(msg)
pyglet.gl.lib.GLException: b'invalid value'
I also get the above error if I try to use pyglet.resource.image instead of pyglet.image.load (the image and py file are in the same directory).
Any one know how I can fix this issue?
I am using Python 3.3, pyglet 1.2alpha-1, and Windows 8.
The code -including the image.blit- runs fine for me. I'm using python 2.7.3, pyglet 1.1.4
There's nothing wrong with the code. You might consider trying other python and pyglet versions for the time being (until pyglet has a new stable release)
This isn't a "fix", but might at least determine if it's fixable or not (mine was not). (From the Pyglet mailing group.)
You can verify whether the system does not even support Textures greater than 1024, by running this code (Python 3+):
from ctypes import c_long
from pyglet.gl import glGetIntegerv, GL_MAX_TEXTURE_SIZE
i = c_long()
glGetIntegerv(GL_MAX_TEXTURE_SIZE, i)
print (i) # output: c_long(1024) (or higher)
That is the maximum texture size your system supports. If it's 1024, then any larger pictures will raise an Exception. (And the only fix is, get a better system).

IPython Notebook Sympy Math Rendering

I have just started with using IPython Notebook and have been fascinated by its power. I have been using a few examples available on the net to get started with. I was following this tutorial: http://nbviewer.ipython.org/url/finiterank.com/cuadernos/suavesylocas.ipynb but the maths output is not getting rendered as expected. Below is the my code and the output:
In [30]:
%load_ext sympyprinting
%pylab inline
from __future__ import division
import sympy as sym
from sympy import *
init_printing()
x,y,z=symbols("x y z")
k,m,n=symbols("k m n", integer=True)
The sympyprinting extension is already loaded. To reload it, use:
%reload_ext sympyprinting
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
In [31]:
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**5)) + (x**2) / (2*k)
t
Out[31]:
2 ⎛ 2 ⎞
x sin⎝2⋅π⋅k ⋅x⎠
─── + ─────────────
2⋅k 2 5
4⋅π ⋅k
I have tried other examples also, and they are also not getting rendered properly. Where am I going wrong?
I had the same problem. Try
from sympy.interactive import printing
printing.init_printing(use_latex=True)
instead of
%load_ext sympyprinting
I am using sympy 0.7.2
I recently had the same problem, and I'm using Linux Crunchbang, which is a derivative of Redhat I think. Originally I installed sympy using
pip install sympy
However, this led to the above problem as described. So then I went to the sympy webpage and cloned the git repository to a folder. Then it can be installed (once in the local folder) by using
python setup.py install
After that everything worked fine, so I think it had something to do with the version used. For the record, the commands I used to initialize the printing in python were
import sympy
sympy.init_printing()
Import:
from sympy import *
init_printing()
Example:
x = symbols('x')
a = Integral(cos(x)*exp(x), x)
Eq(a, a.doit())
Output: