For many standard scientific applications thr simple scipy
/numpy
functions loadtxt()
and savetxt()
will do the trick.
import scipy as sp
import urllib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib notebook
Here's a copy of test1.dat
# This line is a comment
#
#t x y
# ============
1.2 2.3 3.2
2.2 4.6 6.1
3.0 5.9 9.1
The file is in the format of a typical data file. Each line might be something like $(t,x,y)$.
Note that by default loadtxt
ignores any lines in the data file that begin
with the #
character. (This can be changed.)
a = sp.loadtxt("test1.dat")
a
It's often useful to break the data into single arrays for $t$, $x$, and $y$. One way to do this with
the transpose of a
:
a.T
t, x, y = a.T
t
or
t, x, y = sp.loadtxt("test1.dat", unpack=True)
Here's a copy of test2.csv
# This line is a comment
#
#t x y
# ============
, 1, 2, 3
comment in column 0, 2, 4, 6
, 3, 9, 27
b = sp.loadtxt("test2.csv", delimiter = ',', usecols = (1,2,3), unpack = True)
b
c = 2*a
c
sp.savetxt('testout1.dat', c, header='sample header (optional)' )
# Retrieve file from URL and create copy in local filesystem
urllib.request.urlretrieve(' http://www.atlasoftheuniverse.com/nebulae/m42.jpg','orion.jpg')
# Read in local file
img = mpimg.imread('orion.jpg')
plt.figure()
plt.imshow(img);
version_information
is from J.R. Johansson (jrjohansson at gmail.com)
See Introduction to scientific computing with Python:
http://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-0-Scientific-Computing-with-Python.ipynb
for more information and instructions for package installation.
If version_information
has been installed system wide (as it has been on linuxremotes), continue with next cell as written. If not, comment out top line in next cell and uncomment the second line.
%load_ext version_information
#%install_ext http://raw.github.com/jrjohansson/version_information/master/version_information.py
%version_information scipy, matplotlib, urllib