UnicodeDecodeError in pickle.load - windows-7

I'm having some problems with loading a pkl file using pickle. I'm using windows 7 and Python 3.5.1 64 bit. The pkl file was downloaded from here.
This is my code:
import pickle
# Load model weights and metadata
weightFile = open('vgg16.pkl', 'rb')
d = pickle.load(weightFile)
and when I run it I get the output
"C:\Program Files\Python35\python.exe" C:/work/lasagne/tutorial/lasagne-tutorial2.py
Traceback (most recent call last):
File "C:/work/lasagne/tutorial/lasagne-tutorial2.py", line 5, in <module>
d = pickle.load(weightFile)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbc in position 1: ordinal not in range(128)
Process finished with exit code 1
What does this error message mean? It says that there is a byte that the acsii codec can't decode, but isn't a pkl file supposed to be binary (hence not contain ascii characters)?
Am I doing something wrong when loading the file? What can I do to fix the error?

The solution was found in this answer. The pickle file was probably encoded with Python 2, and providing pickle.load with the optional argument encoding='latin1' solved the problem.
The code that works looks like this:
import pickle
# Load model weights and metadata
weightFile = open('vgg16.pkl', 'rb')
d = pickle.load(weightFile, encoding='latin1')

Related

How can I save an animation from matplotlib as a mp4 video file? [duplicate]

enter code here
# -*- coding: utf-8 -*-
import math
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig1=plt.figure()
ax=plt.axes(xlim=(-10,10), ylim=(-10,10))
line,=ax.plot([],[],lw=1)
"""def init ():
line.set_data([],[])
return line,"""
dt=0.001
X=[]
Y=[]
r=float(input("Enter the radius :: "))
w=float(input("Enter angular frequency :: "))
def run(data):
t=0
while w*t<=2*math.pi:
x=r*math.cos(w*t)
y=r*math.sin(w*t)
X.append(x)
Y.append(y)
t=t+dt
line.set_data(X,Y)
return line,
line,=ax.plot(X,Y,lw=2)
FFMpegWriter = animation.writers['ffmpeg']
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
anim=animation.FuncAnimation(fig1,run,frames=200,interval=20,blit=True)
anim.save('amim.mp4',writer=writer)
The error message shown is ::
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/home/tathagata/Documents/Python scripts/circleamim.py", line 35, in <module>
FFMpegWriter = animation.writers['ffmpeg']
File "/home/tathagata/anaconda3/lib/python3.4/site-packages/matplotlib/animation.py", line 81, in __getitem__
return self.avail[name]
KeyError: 'ffmpeg'
I use anacoda distribution and SPYDER as my IDE. I have seen the many solutions related to key errors. But the movie wont run. How can I make the movie to run? I hope there are no other logical errors.
First install ffmpeg and add path to ffmpeg
# on windows
plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'
# on linux
plt.rcParams['animation.ffmpeg_path'] = u'/home/username/anaconda/envs/env_name/bin/ffmpeg'
Note for linux users: The path for ffmpeg can be found by simply using which: which ffmpeg
Also instead of
FFMpegWriter = animation.writers['ffmpeg']
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
I just used writer = animation.FFMpegWriter()
It seems that ffmpegis not installed on your system. Try the following code:
import matplotlib.animation as animation
print(animation.writers.list())
It will print out a list of all available MovieWriters. If ffmpegis not among it, you need to install it first from the ffmpeg homepage.
If you have Homebrew, literally just run the command
brew install ffmpeg
And Homebrew will take care of the rest (dependencies, etc). If you don't, I would recommend getting Homebrew or something like it (apt-get on Linux is built in, or an alternative on OS X would be Macports)
I have also posed with same problem(keyError: 'ffmpeg') but instead of using anakonda, I used IDLE3. So, first i checked for 'ffmpeg' in terminal it wasn't installed so installed it.
Using: sudo apt install ffmpeg
and when I run my save_animation program, it worked generating animation files in '.mpeg' format.

'Error compiling Cython file error' from one day to another

I use a lot of special characters from Hun language, and there were no problems previously. Now they all give errors when running the whole script (F9). It still runs perfect when running locally (select + F5).
Fúú='bar'
Traceback:
C:\Users\my name\.ipython\cython\_cython_magic_21a3824690cdb52a9fe6a3fa1c63ee73.pyx:1:1: Unrecognized character
Traceback (most recent call last):
File "<ipython-input-6-dcfab52d0ff4>", line 1, in <module>
runfile('E:/Anyagok/Programozas/Python/projekts/elo/mindennap/untitled0.pyx', wdir='E:/Anyagok/Programozas/Python/projekts/elo/mindennap')
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\spyder\utils\site\sitecustomize.py", line 703, in runfile
ipython_shell.run_cell_magic('cython', '', f.read())
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\IPython\core\interactiveshell.py", line 2131, in run_cell_magic
result = fn(magic_arg_s, cell)
File "<decorator-gen-130>", line 2, in cython
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\IPython\core\magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "E:\Download\PROGIK\ANACONDA\lib\site-packages\Cython\Build\IpythonMagic.py", line 321, in cython
assert len(extensions) == 1
TypeError: object of type 'NoneType' has no len()
If I change to e.g.
Fuu='bar'
it works great. Why the sudden change of heart?
EDIT:
Have been messing around with FFMPEG and LIBAV yesterday, because wanted to download and convert Youtube videos to mp3. But I'm pretty sure I ran scripts with these characters succesfully after it.

AlignIO gives 'AssertionError' when reading emboss alignment files

I have been stuck on a problem for three days... searched everywhere, posted on Biostar, still waiting for EMBL to respond to emails... would make a bounty if I had more rep.
After aligning sequences with EMBOSSwin needle() (pairwise global alignments) I get alignment files in pair format, with a .needle file extension. I want to use Biopython to read these alignments for later analysis.
I use AlignIO.read(open('alignment.needle'),'emboss') following the instructions in Biopython's AlignIO wiki but I keep getting an AssertionError.
My code:
>>> from Bio import AlignIO
>>> alignment = AlignIO.read(open("data/all/out/pair1_alignment.needle"), "emboss")
My error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\Bio\AlignIO\__init__.py", line 423, in read
first = next(iterator)
File "C:\Python27\lib\Bio\AlignIO\__init__.py", line 370, in parse
for a in i:
File "C:\Python27\lib\Bio\AlignIO\EmbossIO.py", line 150, in __next__
assert seq.replace("-", "") != ""
AssertionError
Example Alignment File:
Download the alignment file here
Versions:
Windows 7
Python version 2.7.3
Biopython version 1.63
EMBOSS version 2.10.0-0.8
Clues:
I suspect this may be related to a warning message I kept getting when actually making the alignments, which was outputted by EMBOSS needle() function:
Warning: Sequence character string not found in ajSeqCvtKS
Duplicate post on BioStars, http://www.biostars.org/p/87226/#87399
This appears to be down to a subtle change in the EMBOSS output. You have an extremely old version, EMBOSS version 2.10.0 (February 2005), and your output file has lines like this:
gag 1288 -------------------------------------------------- 1287
Using a newer version of EMBOSS (e.g. 6.3.0), gives lines like this:
gag 1287 -------------------------------------------------- 1287
The Biopython parser is expecting the latter for alignment sections with no letters (e.g. when one sequence is much longer than the other), where the start and end coordinates agree. Please update your copy of EMBOSS, and then the parser should be happy. The current EMBOSS release is version 6.5.0.
The problem is that you're passing the wrong format file to Biopython. An explanation follows.
Formatting
The format of the file you've linked to is srspair (see the header of pair1_aligned.fasta). It's worth noting that this is not the FASTA format - that's an entirely different format.
Delving into the source of Biopython's EmbossIO, we can see that the EmbossIterator (which is called by AlignIO.read when the format is 'emboss') is only meant to handle the formats pair and simple (see Alignment formats for an explanation of the various formats).
Solution
If you export EMBOSS's output in the pair format (then call AlignIO.read as you have before), that should solve your problem.

python win32com FileSystemObject failed on getting huge folder

My test code is:
#!/usr/bin/env python
import win32com.client
def GetFolderSizeQuick(target_folder):
fso = win32com.client.Dispatch("Scripting.FileSystemObject")
fobj = fso.GetFolder(target_folder)
return fobj.size
print(GetFolderSizeQuick("d:/pytools"))
print(GetFolderSizeQuick("d:/cygwin"))
The result is:
D:\>python a.py
160659697
Traceback (most recent call last):
File "a.py", line 10, in <module>
print(GetFolderSizeQuick("d:/cygwin"))
File "a.py", line 7, in GetFolderSizeQuick
return fobj.size
File "D:\Applications\Python33\lib\site-packages\win32com\client\dynamic.py",
line 511, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2146828218), None)
The first call GetFolderSizeQuick on d:/pytools folder works. it's about 153MB. But the second call failed. The folder d:/cygwin is about 12.6GB.
I am working on windows 7 with python3.3.0 32bit version. So I think the problem happened on the 32bit or 64bit to store the result. 32bit int can not store 12.6GB size.
What is the real problem here, and how to fix it?
That's neither a directory size nor a 32/64-Bit problem.
It's even not a python2 or python3 problem.
Your Error translates to "No Access allowed!"
The simpliest way for testing would be to create a directory where only the owner is allowed to read and all others have NO rights at all. Then take this directory as input - you'll get the same error, even if the directory is empty. A good example would be the local "c:\system Volume Information".
Digging a little deeper:
The errorcodes given by python are signed, whereas for a reasonable lookup Microsoft describes and expects them as unsigned. Kudos to EB in this thread and Tim Peters in this thread, using the examples, you'll get reasonable error-Codes.
import win32com.client
import pywintypes
def get_folder_size(target_folder):
fso = win32com.client.Dispatch("Scripting.FileSystemObject")
fobj = fso.GetFolder(target_folder)
return fobj.size
if __name__ == '__main__':
try:
get_folder_size('c:/system volume information')
except pywintypes.com_error, e:
print e # debug, have to see which indices
print hex(e[0]+2**32), hex(e[2][5]+2**32)
Now search for both of the hex digits, the 2nd one should lead to a lot of "you are not allowed to..." queries and answers.

Downloading a zipfile in binary format using python's ftplib

I have gone through quite a few questions that have been posted which appear to be related, but not entirely the same issue that I am having:
I am using python's ftplib module along with zipfile to download a zip file from ftp in binary format. However, for some reason, the downloaded zip file appears to be in ascii.
I have ensured that a leading / does not exist in the path of the file I am downloading (to match the zip specifications).
outFile = zipfile.ZipFile(local_file_path, 'w')
myftp.retrbinary('RETR %s' %i, outFile.write(i)) #i - target file path on ftp server
This code fails giving me the following error:
st = os.stat(filename)
OSError: [Errno 2] No such file or directory: //$i
I tried adding the 'b' option for binary, but zipfile doesn't seem to like it:
outFile = zipfile.ZipFile(local_file_path, 'wb')
This raises error:
RuntimeError: ZipFile() requires mode "r", "w", or "a"
I am using python v2.6.
What am I doing wrong and how to fix it?
According to python doc (http://docs.python.org/2/library/ftplib.html) seems retrbinary takes a callback as second parameter:
>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
'226 Transfer complete.'
>>> ftp.quit()
Documentation says:
FTP.retrbinary(command, callback[, maxblocksize[, rest]])
Retrieve a file in binary transfer mode. command should be an
appropriate RETR command: 'RETR filename'. The callback function is
called for each block of data received, with a single string argument
giving the data block. [...]
In your example it should be outfile.write (instead of outfile.write(i)).
>>> ftp.retrbinary('RETR %s' % i, outFile.write)

Resources