Size: 13051
Comment:
|
← Revision 5 as of 2008-04-12 09:07:05 ⇥
Size: 4056
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
SiaProgrammingPython This lesson motivates the use of Python. The Python programming language is compared with other systems for satellite image processing and analysis. |
|
Line 8: | Line 12: |
Line 77: | Line 82: |
{{{ipython ipython -pylab}}} |
{{{ ipython ipython -pylab }}} |
Line 85: | Line 92: |
help()} help modules}}} |
help() help modules }}} |
Line 88: | Line 96: |
\end{itemize}} | |
Line 90: | Line 97: |
Features of IPython | '''Features of IPython''': |
Line 99: | Line 107: |
Importing modules | === Importing modules === There are many ways to import modules. The resulting namespaces are different |
Line 101: | Line 110: |
\begin{itemize} \item \textbf{import scipy}\\ Import module SciPy \item \textbf{help(scipy)} or \textbf{scipy.info(scipy)}\\ Lists SciPy subpackages \item \textbf{import scipy.ndimage}\\ Imports SciPy subpackage n-dimensional image package \item \textbf{help(scipy.ndimage)}\\ Lists ndimage functions \item \textbf{help(scipy.ndimage.laplace)}\\ Help on ndimage function laplace \item \textbf{from scipy import *}\\ Import SciPy module into local namespace \item \textbf{import scipy.ndimage as ndi}\\ Import SciPy subpackage ndimage in namespace ndi \item \textbf{scipy.source(ndi.laplace)}\\ Write source for this object to output \end{itemize} \vfill} |
* Import module SciPy: {{{import scipy}}} * List SciPy subpackages {{{help(scipy)}}} or {{{scipy.info(scipy)}}} * Import SciPy subpackage n-dimensional image package {{{import scipy.ndimage}}} * List ndimage functions {{{help(scipy.ndimage)}}} * Help on ndimage function laplace {{{help(scipy.ndimage.laplace)}}} * Import SciPy module into local namespace {{{from scipy import *}}} * Import SciPy subpackage ndimage in namespace ndi {{{import scipy.ndimage as ndi}}} * Write source for this object to output {{{scipy.source(ndi.laplace)}}} |
Line 121: | Line 119: |
\begin{frame}[fragile]\frametitle{Getting started: defining functions and visualization} \vfill\small \begin{lstlisting} |
=== Defining functions and visualization === {{{ |
Line 131: | Line 128: |
\end{lstlisting} \vfill \includegraphics[height=0.4\textheight]{images/figure1.png} \end{frame} |
}}} |
Line 136: | Line 130: |
\begin{frame}[fragile]\frametitle{Getting started: basic plotting} \vfill\small \begin{lstlisting} |
{{{ |
Line 146: | Line 138: |
\end{lstlisting} \vfill \includegraphics[height=0.6\textheight]{images/sinx.png} \end{frame} \section{Basic Python} \begin{frame}[fragile]\frametitle{Basic Python} \vfill \begin{itemize} \item Keywords \item Built-in types \item Operators \item Conditions \item Variables \item Functions \end{itemize} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Basic Python} \vfill Python built-in keywords \small \begin{verbatim} help> keywords and else import raise assert except in return break exec is try class finally lambda while continue for not yield def from or del global pass elif if print \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Built-in types} \vfill \textbf{Python built-in numeric types}\vfill There are four distinct numeric types: \begin{itemize} \item plain integers \textbf{int} \item long integers \textbf{long} \item floating point numbers \textbf{float} \item complex numbers \textbf{complex} \end{itemize} In addition, Booleans are a subtype of plain integers \vfill \end{frame} \begin{frame}[fragile]\frametitle{Operations} \small \begin{verbatim} x + y sum of x and y x - y difference of x and y x * y product of x and y x / y quotient of x and y x // y (floored) quotient of x and y x % y remainder of x / y -x x negated +x x unchanged abs(x) absolute value or magnitude of x int(x) x converted to integer long(x) x converted to long integer float(x) x converted to floating point complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero c.conjugate() conjugate of the complex number c x ** y x to the power y \end{verbatim} \end{frame} \begin{frame}[fragile]\frametitle{Comparisons} \small \begin{verbatim} < strictly less than <= less than or equal > strictly greater than >= greater than or equal == equal != not equal is object identity \end{verbatim} \end{frame} \begin{frame}[fragile]\frametitle{Boolean} Boolean (logical) types \begin{itemize} \item \textbf{True} \item \textbf{False} \end{itemize} \vfill Boolean Operations: \textbf{and}, \textbf{or}, \textbf{not} \small \begin{verbatim} x or y if x is false, then y, else x x and y if x is false, then x, else y not x if x is false, then True, else False \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Sequence types} Sequence types \begin{itemize} \item \textbf{str} \item \textbf{list} \item \textbf{tuple} \end{itemize} Operations \small \begin{verbatim} x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal to x, else True s + t the concatenation of s and t s[i] i'th item of s s[i:j] slice of s from i to j s[i:j:k] slice of s from i to j with step k len(s) length of s min(s) smallest item of s max(s) largest item of s \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Variables, assignment} The \textbf{assignment statement =} creates new variables and gives them values \begin{verbatim} a=2 l=9**1000L S='Hello' f=1e5 \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Lists and tuples} \begin{itemize} \item Lists \textbf{[]} are mutable sequences of values \item Tuples \textbf{()} can not be changed (immutable) \end{itemize} \tiny \begin{verbatim} In [5]: a=[1,2,3] In [6]: b=(1,2,3) In [7]: a[0] Out[7]: 1 In [8]: b[0] Out[8]: 1 In [9]: a[0]=4 In [10]: a Out[10]: [4, 2, 3] In [11]: b[0]=4 --------------------------------------------------------------------------- exceptions.TypeError \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Indexing and slicing} \begin{itemize} \item Indices \textbf{[]} select elements of variables \item Indices start from zero \item \textbf{[a:b]} selects a slice of elements \end{itemize} \tiny \begin{verbatim} In [1]: a=range(10) In [2]: a Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [3]: a[0:4] Out[3]: [0, 1, 2, 3] In [4]: a[4:] Out[4]: [4, 5, 6, 7, 8, 9] In [5]: a[:4] Out[5]: [0, 1, 2, 3] In [6]: a[:-1] Out[6]: [0, 1, 2, 3, 4, 5, 6, 7, 8] In [7]: s='Hello world' In [8]: s[:5] Out[8]: 'Hello' In [9]: a=tuple(range(10)) In [10]: a[0:4] Out[10]: (0, 1, 2, 3) \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{List built-in functions (methods)} \begin{itemize} \item \textbf{L.append(object)} -- append object to end \item \textbf{L.count(value)} -> integer -- return number of occurrences of value \item \textbf{L.extend(iterable)} -- extend list by appending elements from the iterable \item \textbf{L.index(value, [start, [stop]])} -> integer -- return first index of value \item \textbf{L.insert(index, object)} -- insert object before index \item \textbf{L.pop([index])} -> item -- remove and return item at index (default last) \item \textbf{L.reverse()} -- reverse *IN PLACE* \item \textbf{L.sort()} -- stable sort *IN PLACE* \end{itemize} \vfill \end{frame} \begin{frame}[fragile]\frametitle{List built-in functions (methods)} \scriptsize \begin{verbatim} In [72]: L=['C','A','B'] In [73]: L.sort() In [74]: L Out[74]: ['A', 'B', 'C'] In [75]: L.pop() Out[75]: 'C' In [76]: L Out[76]: ['A', 'B'] In [77]: L.insert(1,'C') In [78]: L Out[78]: ['A', 'C', 'B'] \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Nested data structures} \scriptsize \begin{verbatim} In [84]: a=range(10) In [85]: b=range(0,10,2) In [86]: b Out[86]: [0, 2, 4, 6, 8] In [87]: c=[a,b] In [88]: c Out[88]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8]] In [91]: c[0] Out[91]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [92]: c[1] Out[92]: [0, 2, 4, 6, 8] In [93]: c[0][0:4] Out[93]: [0, 1, 2, 3] In [94]: c[1][0:4] Out[94]: [0, 2, 4, 6] \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Dictionary} \begin{itemize} \item A dictionary is like a list, but more general. \item In a list, the indices have to be integers; in a dictionary they can be (almost) any type. \item \textbf{d=\{\}} creates empty dictionary \item \textbf{d[key]=value} add a key, value pair to dictionary \item \textbf{d.keys()} returns list of keys \end{itemize} \scriptsize \begin{verbatim} In [95]: D={} In [96]: D['a']=range(10) In [97]: D['b']=range(0,10,2) In [98]: D Out[98]: {'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'b': [0, 2, 4, 6, 8]} In [99]: D['a'] Out[99]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [100]: D.keys() Out[100]: ['a', 'b'] \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Loops} \begin{itemize} \item Looping over a list is done for the \textbf{for} statement \item \textbf{enumerate()} returns an enumerated object \end{itemize} \scriptsize \begin{verbatim} In [106]: a=['Hund','Katze','Maus'] In [108]: for i in a: .....: print i .....: Hund Katze Maus In [111]: for i,j in enumerate(a): .....: print i,j .....: 0 Hund 1 Katze 2 Maus \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Control statements} \begin{itemize} \item Conditions can be checked with \textbf{if-else} statement \item \textbf{if} condition: indented block \item \textbf{else:} \item condition \end{itemize} \scriptsize \begin{verbatim} In [116]: if a==1: .....: print 'a=1' .....: else: .....: print 'a != 1' .....: a=1 \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Functions} Function with one argument \begin{verbatim} In [124]: def quadrat(x): .....: return x**2 .....: In [125]: quadrat(2) Out[125]: 4 \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Functions} Function with variable number of arguments \begin{verbatim} In [126]: def printargs(*args): .....: for arg in args: .....: print arg .....: In [127]: printargs(a) [1, 2, 3] In [128]: printargs(a,range(4)) [1, 2, 3] [0, 1, 2, 3] \end{verbatim} \vfill \end{frame} \begin{frame}[fragile]\frametitle{Functions} Function with variable number of keywords \begin{verbatim} In [131]: def printkeys(**kw): .....: for k in kw.keys(): .....: print k,kw[k] .....: In [132]: printkeys(key1=10,key2='Hallo') key2 Hallo key1 10 \end{verbatim} \vfill \end{frame} \end{document} |
}}} |
This lesson motivates the use of Python. The Python programming language is compared with other systems for satellite image processing and analysis.
Software for Satellite Image processing and analysis
Programming languages
Ideal programming language for satellite image processing and analysis
- Fast array operations
- Image processing and numeric/scientific routines
- Visualization
- Various data formats
- Processing of files and metadata
- Short development cycles
- Very high level of abstraction
- Interactive
Ideal programming language for satellite image processing and analysis?
- Assembler
- Fortran, C/C++, Java
- Perl, Python
- Matlab, IDL
- Visual and menu driven environments, ENVI, GIS
Visual environments
Examples
Programming versus visual environments
- Visual environments are very useful for specific tasks
- Closed commerical software
- Programming offers more flexibility
- ESRI's ArcGIS scripting with python
Scripting verus Traditional Programming
- Traditional programming refers to building usually large, monolithic systems
- Fortran, C/C++, Java
- Scripting means programming at a high and flexible abstraction level
- Perl, Python, Ruby, Scheme, Tcl
- Scientific computing environments
- IDL, Matlab/Octave, Maple, Mathematica, R
Why Python?
Scalability
- The ability to scale from easy to difficult problems and the ability for beginners and experts to be comfortable.
- Python is easy enough to be a first language and powerful enough to write complex applications
- Scientific computing is more than number crunching
- Converting data formats
- Extracting metadata from text
- Working with a large number of files and directories
- Object oriented programming possible, but not required
- Freely available and runs on Unix, Mac and Windows
- Simple interfacing of C,C++ and Fortran code
- Heterogeneous data structures are easy to use
- Readable and compact code
Scientific Python Environment
- Basic Python has limited instruction set
- Extensions (modules)
- Scientific modules (scipy/numpy)
- Interactive environment (ipython)
- Plots and visualization (pylab)
Getting started with IPython
Invoking the IPython shell
ipython ipython -pylab
loads pylab module and enables interactive plotting
Quit with CTRL-D
Getting help
help() help modules
list available modules
Features of IPython:
- Command history (up, down)
- Completion by typing TAB
- System commands through magic functions cd, ls, env, pwd
- Access to Unix shell by prefix ! {!ls -s $|$ sort -g}
- Debugging and profiling
Program control: run
Print interactive variables who, whos
Importing modules
There are many ways to import modules. The resulting namespaces are different
Import module SciPy: import scipy
List SciPy subpackages help(scipy) or scipy.info(scipy)
Import SciPy subpackage n-dimensional image package import scipy.ndimage
List ndimage functions help(scipy.ndimage)
Help on ndimage function laplace help(scipy.ndimage.laplace)
Import SciPy module into local namespace from scipy import *
Import SciPy subpackage ndimage in namespace ndi import scipy.ndimage as ndi
Write source for this object to output scipy.source(ndi.laplace)
Defining functions and visualization
def tv(a): imshow(a,interpolation='nearest') colorbar() a=rand(10,10) tv(a) savefig('figure1.png',dpi=100)
x=linspace(0,2*pi,100) plot(x,sin(x),x,cos(x)) grid() axis('tight') legend(('sin(x)','cos(x)'),'upper right') xlabel('x') ylabel('f(x)')