iPython notebook not uploading my .txt file - ipython

I converted some excel files to .txt files (Text (tab delimited)), carefully saved them in the same folder as my notebook, made sure there were no copies anywhere on my computer, checked the .txt files could all be opened in Notebook, and after using the command '%pylab inline' to load all the functions I usually use, triple checked that mt .txt files were in the same folder as the notebook by using the 'ls' command. I then used loadtxt('filename.txt') to load my data and it gave me errno 2: no such file or directory: 'filename.txt'. I triple checked that I had spelled everything correctly and I can't understand why it's not working. I need these data analysing today for a lab report due in tomorrow - help! Here's my code:
%pylab inline
ls
loadtxt('Physical_lab_experiment_2_25_degrees_with_times.txt')
and here is the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-9-c8d929572f25> in <module>()
----> 1 loadtxt('Physical_lab_experiment_2_25_degrees_with_times.txt')
C:\Users\Daisy\Anaconda3\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
738 fh = iter(open(fname, 'U'))
739 else:
--> 740 fh = iter(open(fname))
741 else:
742 fh = iter(fname)
FileNotFoundError: [Errno 2] No such file or directory: 'Physical_lab_experiment_2_25_degrees_with_times.txt'

loadtxt is a numpy function. Import numpy like:
import numpy as np
data= np.loadtxt('Physical_lab_experiment_2_25_degrees_with_times.txt')
print data

Related

ValueError: can't read cfg files (sense2vec, reddit vectors)

I am relatively new to NLP and mostly use Jupyter, please let me know what I'm doing wrong:
I followed all the instructions provided here:
https://github.com/explosion/sense2vec
but when I try to use the reddit_vectors as described here:
s2v = Sense2VecComponent(nlp.vocab).from_disk("/path/to/s2v_reddit_2015_md")
I get a ValueError as shown below:
ValueError Traceback (most recent call last)
<ipython-input-36-0d396d0145de> in <module>
----> 1 s2v=Sense2Vec().from_disk('reddit_vectors-1.1.0/vectors.bin/')
~/.conda/envs/NewEnv6/lib/python3.7/site-packages/sense2vec/sense2vec.py in from_disk(self, path,
exclude)
343 cache_path = path / "cache"
344 self.vectors = Vectors().from_disk(path)
--> 345 self.cfg.update(srsly.read_json(path / "cfg"))
346 if freqs_path.exists():
347 self.freqs = dict(srsly.read_json(freqs_path))
~/.conda/envs/NewEnv6/lib/python3.7/site-packages/srsly/_json_api.py in read_json(location)
48 data = sys.stdin.read()
49 return ujson.loads(data)
---> 50 file_path = force_path(location)
51 with file_path.open("r", encoding="utf8") as f:
52 return ujson.load(f)
~/.conda/envs/NewEnv6/lib/python3.7/site-packages/srsly/util.py in force_path(location,
require_exists)
19 location = Path(location)
20 if require_exists and not location.exists():
---> 21 raise ValueError("Can't read file: {}".format(location))
22 return location
23
ValueError: Can't read file: reddit_vectors-1.1.0/vectors.bin/cfg
*I installed all the appropriate versions of libraries/packages required in the requirements.txt
This is what worked for me:
If you are not using a virtual environment check if all the libraries from libraries/packages required in the requirements.txt actually got installed properly, in my case one of them was not properly installed.
The path should lead to the folder containing the cfg file. (After the last update I recommend using the entire computer path instead of navigating inside the same project)
Check your path to reddit_vectors-x.x.x folder.
Put your reddit_vectors-x.x.x folder in the same folder where your .py file is.
Use pathlib to be sure your path is correct.
from pathlib import Path
path = Path(__file__).parent.joinpath('reddit_vectors-1.1.0')
s2v.from_disk(path)
If you still get the error, delete your reddit_vectors-x.x.x folder and re untar/unzip the original reddit_vectors-x.x.x.tar.gz or reddit_vectors-x.x.x.zip

Changing directory with os in python

I'm trying to change the directory to the folder that contains the folder I'm in.
That is, I'm in /Users/ethanfuerst/Documents/Coding/mpgdata and I'm trying to go back to /Users/ethanfuerst/Documents/Coding and access a text file in the Coding folder.
When I run this code:
import os
print(os.getcwd())
os.chdir('⁨Users/ethanfuerst/Documents/Coding')
I get this output and error
/Users/ethanfuerst/Documents/Coding/mpgdata
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-19-6c6ee01785f6> in <module>
1 import os
2 print(os.getcwd())
----> 3 os.chdir('⁨Users/ethanfuerst/Documents/Coding')
FileNotFoundError: [Errno 2] No such file or directory: '\u2068Users/ethanfuerst/Documents/Coding'
Does anyone know why this could be the case?
Edit: forgot to mention I am on a Mac
Try using C:\ in front of Users. If that does not work, try using .. in the directory specification to go back one directory.
Use double quotes os.chdir("⁨Users/ethanfuerst/Documents/Coding") instead of os.chdir('⁨Users/ethanfuerst/Documents/Coding')

PyPDF2 giving me an invalid argument error

I'm trying to parse text from pdf file. while I was doing tutorial of how to PyPDF2 I got the following error. I did the search for an answer but ended up finding none. Any Help will be greatly appreciated.
Traceback (most recent call last):
File "D:/text_recognizer/main.py", line 4, in <module>
inputStream = PyPDF2.PdfFileReader(input)
File "D:\KimKanna's Class\python27\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
self.read(stream)
File "D:\KimKanna's Class\python27\lib\site-packages\PyPDF2\pdf.py", line 1689, in read
stream.seek(-1, 2)
IOError: [Errno 22] Invalid argument
here is fullcode
import PyPDF2
with open(".\\pdf\\test_sample.pdf","rb") as input:
inputStream = PyPDF2.PdfFileReader(input)
In my case the .pdf I wanted to open is empty and not closed from previous python code in powershell(cmd prompt). So, when I tried to delete those files it says 'Close the file and try again'. (that was my "AHaa" moment)
So I stopped the py.exe from my Windows task manager and deleted those empty, not closed files. Then I run the same code with another files, It worked fine.. :)

Unable to run pykalma example code that is reading MAT file with scipy

Below is the line of the code that pykalman example is using to load the matlab file robot.mat. However, it is throwing an error with message ValueError: Unknown mat file type, version 100, 110.
I am new to reading matlab files in Python. Can anyone help me understand exactly what version it is expecting and how to get around the issue?
data = io.loadmat(join(module_path, 'data', 'robot.mat'))
Traceback (most recent call last):
File "C:\ProgramData\Anaconda2\lib\site-
packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-0439d709929f>", line 1, in <module>
data = load_robot()
File "C:\ProgramData\Anaconda2\lib\site-packages\pykalman\datasets\base.py",
line 45, in load_robot
data = io.loadmat(join(module_path, 'data', 'robot.mat'))
File "C:\ProgramData\Anaconda2\lib\site-packages\scipy\io\matlab\mio.py",
line 135, in loadmat
MR = mat_reader_factory(file_name, appendmat, **kwargs)
File "C:\ProgramData\Anaconda2\lib\site-packages\scipy\io\matlab\mio.py",
line 59, in mat_reader_factory
mjv, mnv = get_matfile_version(byte_stream)
File "C:\ProgramData\Anaconda2\lib\site-
packages\scipy\io\matlab\miobase.py",
line 241, in get_matfile_version
raise ValueError('Unknown mat file type, version %s, %s' % ret)
ValueError: Unknown mat file type, version 100, 110
I'm guessing here (and adding one more idea to hpaulj's good overview):
First i checked the file manually and opening in an editor reads as:
MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Tue Jun 26 00:59:04 2012
...
Therefore there should be no problem in regards to scipy (if the mat-file is actually following this "header").
Now it seems there are some open issues with that project with the most relevant beeing:
Unable to load robot.mat #59
(Don't be surprised too much with the different eror-values as i would expect possibly indeterministic stuff like that when bad IO is beeing done)
and: pykalman/datasets/ not copied when pykalman is installed with pip #66
So it seems this dataset does not come with pip-install automatically, which looks like your problem and should be easily handled (manually obtaining the data!). It is also compatible with hpaulj's wondering about that strange error (one could argue if there is no better error-handling possible).

Using Gurobi solver in the Enthought Canopy Editor on Linux

I am currently working with Gurobi solver using python. Previously, I have just been using a mix of a text editor and the terminal to write and run my models but I am currently experimenting with using Enthought Canopy Editor. I have a model that is works when I run in in the terminal using python model.py but when I try doing%run model.py in Canopy's iPython shell I get:
%run /home/cdhagmann/Copy/Code_Env/Farmer/model.py
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/cdhagmann/Canopy/appdata/canopy-1.0.1.1189.rh5-x86_64/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
181 else:
182 filename = fname
--> 183 __builtin__.execfile(filename, *where)
/home/cdhagmann/Copy/Code_Env/Farmer/model.py in <module>()
----> 1 from gurobipy import *
2 from data_IO import read_data,currency
3 import time
4
5 # Import model data
ImportError: No module named gurobipy
My guess is that the terminal and Canopy look for modules in different places but I don't know how to rectify it. I found this reference on the Gurobi group site about Enthought Canopy and Gurobi on OS X 10.8 but didn't know how much those instruction would differ from doing it on Linux and even what he meant about installing Gurobi in Canopy's site-packages.
UPDATE: Based on information found the first link I clicked on, I did in fact leave the boxed clicked making Canopy my default directory. You have to manually prepend the PATH variable, which I missed them saying that I had to do. So I did that. I now have Canopy's version of iPython running in my terminal (check by using sys.path); however, I get a huge list of errors when I try run python.
cdhagmann#Crispin ~ $ python
Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 563, in <module>
main()
File "/usr/lib/python2.7/site.py", line 545, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python2.7/site.py", line 272, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python2.7/site.py", line 247, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python2.7/site.py", line 237, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python2.7/sysconfig.py", line 578, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python2.7/sysconfig.py", line 505, in get_config_vars
import re
File "/usr/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Suggestions?
And to answer #Jonathan questions:
sys.prefix = '/usr'
sys.path:
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/pymodules/python2.7
/usr/lib/python2.7/dist-packages/ubuntu-sso-client
gurobipy.__file__ = '/usr/local/lib/python2.7/dist-packages/gurobipy/gurobipy.so'
If the terminal is correctly configured for Canopy Python, then python in terminal and Canopy Python should be looking for modules in the same places. If during installation, you disabled "make Canopy be default Python", or if you have not restarted terminal since installation, then it won't automatically be configured correctly.
These articles should help you understand and configure:
http://docs.enthought.com/canopy/configure/faq.html#where-are-all-of-the-python-packages-in-my-user-python-environment
https://support.enthought.com/entries/23646538-Make-Canopy-s-Python-be-your-default-Python-i-e-on-the-PATH-
https://support.enthought.com/entries/23389761-Installing-packages-into-Canopy-Python-from-the-command-line
If these articles do not suffice for you to solve this, then please report -- when you run python from terminal, what are these?
sys.prefix
sys.path
gurobipy.__file__