Import .edf file directly from online archive in python - python

Using pyedflib to import edf files, is it possible to import datasets directly from their source? Or is it always necessary to download data and import locally?
for example, I would like to do this:
pyedflib.EdfReader("https://www.physionet.org/pn6/chbmit/chb02/chb02_02.edf")
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-64-d123ce671a2f> in <module>()
----> 1 pyedflib.EdfReader("https://www.physionet.org/pn6/chbmit/chb02/chb02_02.edf")
pyedflib/_extensions/_pyedflib.pyx in pyedflib._extensions._pyedflib.CyEdfReader.__init__()
pyedflib/_extensions/_pyedflib.pyx in pyedflib._extensions._pyedflib.CyEdfReader.open()
pyedflib/_extensions/_pyedflib.pyx in pyedflib._extensions._pyedflib.CyEdfReader.check_open_ok()
IOError: can not open file, no such file or directory

Answer received on GitHub page
import pyedflib
import os
url = "https://www.physionet.org/pn6/chbmit/chb01/chb01_01.edf"
filename = "./chb.edf"
try:
from urllib import urlretrieve # Python 2
except ImportError:
from urllib.request import urlretrieve # Python 3
urlretrieve(url,filename)
pyedflib.EdfReader(filename)
os.remove(filename)
https://github.com/holgern/pyedflib/issues/22#issuecomment-341649760

Related

module 'skipthoughts' has no attribute 'load_model'

I was implementing the following text encoder code but was facing this error:
from codecs import EncodedFile
import os
from os import listdir
from os.path import isfile, join
import skipthoughts
import os.path
import pickle
# from keras.models import load_model
annot_dir='annot/test_annotations'
encoded_vector_dir='annot/test_encoded'
garbage='annot/test_garbage'
model=skipthoughts.load_model()
ofiles=[f_ for f_ in listdir(annot_dir) if isfile(join(annot_dir,f_))]
ofiles_1=ofiles[0: len(ofiles)//5]
ofiles_2=ofiles[len(ofiles)//5: 2*len(ofiles)//5]
ofiles_3=ofiles[2*len(ofiles)//5: 3*len(ofiles)//5]
ofiles_4=ofiles[3*len(ofiles)//5: 4*len(ofiles)//5]
ofiles_5=ofiles[4*len(ofiles)//5: 5*len(ofiles)//5]
for files in ofiles:
if os.path.exists(join(encoded_vector_dir,files))==False :
with open(join(annot_dir,files)) as f:
captions=f.read().split(',')
captions=[cap for cap in captions if len(cap.strip())>0]
try:
caption_vectors=skipthoughts.encode(model,captions)
except:
with open(join(garbage,files),mode='wb') as myfile:
pickle.dump(caption_vectors,myfile)
else:
print('skipped')
and below is the error I'm facing, if anyone has a solution if might be very helpful. Thanks.
Traceback (most recent call last):
File "\encoder.py", line 14, in <module>
model=skipthoughts.load_model()
AttributeError: module 'skipthoughts' has no attribute 'load_model'

Displaying/getting Images from an URL in Python

I am new to python. But I got a task and I need to Displaying/getting Images from an URL.
I have been using Jupyter notebook with python to try to do this.
import sys
print(sys.version)
3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
I was trying to do it as in this post but none of the answers work.
With
import urllib, cStringIO
file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
I get:
ImportError Traceback (most recent call last)
<ipython-input-33-da63c9426dad> in <module>()
1 url='http://images.mid-day.com/images/2017/feb/15-Justin-Bieber.jpg'
2 print(url)
----> 3 import urllib, cStringIO
4
5 file = cStringIO.StringIO(urllib.urlopen(URL).read())
ImportError: No module named 'cStringIO'
With:
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
I get:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-168cd6221ea3> in <module>()
1 #response = requests.get("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg")
----> 2 response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8')
3 img = Image.open(StringIO(response.content))
AttributeError: 'Response' object has no attribute 'read'
With:
from PIL import Image
import requests
from StringIO import StringIO
response = requests.get(url)
img = Image.open(StringIO(response.content))
I get:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-37-5716207ad35f> in <module>()
3 from PIL import Image
4 import requests
----> 5 from StringIO import StringIO
6
7 response = requests.get(url)
ImportError: No module named 'StringIO'
Etc....
I thought it was going to be an easy task, but so far I haven't been able to find an answer.
I really hope someone can help me
This worked for me
from PIL import Image
import requests
from io import BytesIO
url = "https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.show()
You are getting an error because you used the line response.read("https://baobab-poseannotation-appfile.s3.amazonaws.com/media/project_5/images/images01/01418849d54b3005.o.1.jpg").decode('utf-8') instead. I'd switch back to using response = requests.get(url)
Additionally, for your error: ImportError: No module named 'cStringIO', you are using python3. StringIO and cStringIO from python 2 were removed in python 3. Use from io import StringIO instead. See StringIO in Python3 for more details.
This might be duplicated with https://stackoverflow.com/a/46954931/4010864.
For your third option with PIL you can try this:
from PIL import Image
import requests
import matplotlib.pyplot as plt
response = requests.get(url, stream=True)
img = Image.open(response.raw)
plt.imshow(img)
plt.show()

python opening .txt error file an integer is required (got type str)

I've never used from os import * So this duplicate suggestion is not valid. Please look at the code in the description.
I'm not sure why I am running into this error. I thought it would be a simple with open() method but it isn't working. I have a .txt file that I'm trying to open and I'm getting this error.
I'm not sure why it is asking for a integer vs a string to open a .txt file
import os
import spacy
import en_core_web_sm
import plac
from collections import Counter
from __future__ import unicode_literals
file = 'spacy_sample.txt'
with open(file, 'r' )as f:
'''Do something'''
TypeError Traceback (most recent call last)
<ipython-input-198-b6441a57e84e> in <module>()
----> 1 with open(file, 'r' )as f:
2 print(f)
TypeError: an integer is required (got type str)

NameError: name 'images' is not defined

I was trying to import some images to my local computer from amazon using their api for data analysis
This is my code I am getting this error.Don't know what to do Please suggest some solution
Code
from PIL import Image
import requests
from io import BytesIO
for index, row in images.iterrows():
url = row['large_image_url']
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.save('images/15k_images/'+row['asin']+'.jpeg')
Error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-36-ce8803db815e> in <module>()
9 from io import BytesIO
10
---> 11 for index, row in images.iterrows():
12 url = row['large_image_url']
13 response = requests.get(url)
NameError: name 'images' is not defined

urllib.request for python 3.3 not working to download file

I would like to download a large archive file with python and save it, but urllib is not working for me. This is my code:
import urllib
urllib.request("http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt")
Note that the link I used in this example is not a large archive. I am only using it as an example. It links directly to a .txt file, so it should work. I am getting this error:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
urllib.request("http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt")
AttributeError: 'module' object has no attribute 'request'
It seems to me like urllib is somehow broken and missing the "request" method. I am using Python 3.3. Should I be using another module or is it actually a Python problem?
No, it is not broken. The urllib.request documentation is pretty clear on how this works:
import urllib.request
req = urllib.request.urlopen('http://www.google.com')
data = req.read()
Edit: If you need to write the file directly to disk rather than process the data, use urlretrieve.
urllib.request.urlretrieve('http://example.com/big.zip', 'file/on/disk.zip')
To download an url into a file, you could use urlretrieve() function:
from urllib.request import urlretrieve
url = "http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt"
urlretrieve(url, "result.txt")
The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3
from urllib.request import urlopen
data = urlopen(r"http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt")
print(data)

Categories