Common Third Party Libraries
----------------------------------------

- lxml
- PIL
- NumPy
- SciPy
- matplotlib


Python Packaging
----------------------------------------

Python packaging is a messy subject that is under going a bit of a
make over right now. By packaging I mean a system to manage Python
libraries on a given system.

- Installation/Upgrading/Removal
- Managing Dependencies
- Listing Currently Installed Packages
- Searching for New Packages


Packaging Solution - pip/virtualenv
----------------------------------------

Just use ``pip`` to install Python packages http://pip.openplans.org/.

For more complex environments use ``virtualenv`` to manage multiple
Python environments. http://virtualenv.openplans.org/


The Python Package Index (PyPi)
----------------------------------------

Also (or formerly known) as the Cheeseshop, it is a central repository of Python projects.
When you install a package by name via ``pip`` this is where it comes from. If you are looking
for a particular package this is where you can search to find a suitable one.

See them at http://pypi.python.org/pypi


lxml
----------------------------------------

lxml is a Python binding for the libxml2 and libxslt C libraries.
It provides a Pythonic  API for handling xml files and it is very fast.
Unlike the standard lib ``xml`` it supports XPath expressions and XSLT.
lxml can also handle HTML.


lxml Example (geonames.py)
----------------------------------------

.. literalinclude:: /code/geonames.py
    :lines: 1-9


PIL (Python Image Library)
----------------------------------------

PIL provides image processing for Python. This includes reading and writing
images, type conversions, and image manipulation. Along with this set of
operations it provides a clean interface for handling images.


PIL Example (mandel.py)
----------------------------------------

.. literalinclude:: /code/mandel.py
    :lines: 1-7


NumPy/SciPy
----------------------------------------

NumPy is a package for handling array based data. It adds vector and matrix
operations on top of standard Python lists.

SciPy is built on top of NumPy. It provides scientific computational tools
for statistics, optimization, numerical integration, linear algebra,
Fourier transforms, signal processing, image processing, ODE solving, and more.


NumPy For Matlab Users
----------------------------------------

If you use Matlab then I would recommend reading this page on comparing
NumPy to Matlab: 

http://www.scipy.org/NumPy_for_Matlab_Users


Creating NumPy Arrays
----------------------------------------

``ndarray`` is the basic array type provided by NumPy. There a number of ways of
creating them. 

.. code-block:: python

    import numpy
    a = numpy.array([1, 2, 3]) # 1-dimensional
    b = numpy.array([[1, 2, 3], [4, 5, 6]]) # 2-dimensional
    zero = numpy.zeros((3, 3))
    one = numpy.ones((2, 3, 4))
    even = numpy.arange(2, 20, 2)
    unit = numpy.linspace(0, 1, 10)


Using NumPy Arrays
----------------------------------------

Arithmetic operators on arrays apply elementwise and create a new array with the result.

.. code-block:: python

    import numpy
    x = numpy.arange(4)
    y = numpy.arange(4)
    x + y
    x*2
    x**2
    x*y
    numpy.dot(x, y)


Array Slicing
----------------------------------------

.. code-block:: python
    
    import numpy
    x = numpy.arange(20).reshape(4, 5)
    x[1, 1]
    x[:, 2]    
    y = x.reshape(2, 2, 5)
    y[:, :, 1]
    y[..., 1]


SciPy Modules
----------------------------------------

- Clustering package (scipy.cluster)
- Constants (scipy.constants)
- Fourier transforms (scipy.fftpack)
- Integration and ODEs (scipy.integrate)
- Interpolation (scipy.interpolate)
- Input and output (scipy.io)
- Linear algebra (scipy.linalg)
- Maximum entropy models (scipy.maxentropy)
- Miscellaneous routines (scipy.misc)
- Multi-dimensional image processing (scipy.ndimage)
- Orthogonal distance regression (scipy.odr)
- Optimization and root finding (scipy.optimize)


SciPy Modules (cont.)
----------------------------------------

- Signal processing (scipy.signal)
- Sparse matrices (scipy.sparse)
- Sparse linear algebra (scipy.sparse.linalg)
- Spatial algorithms and data structures (scipy.spatial)
- Distance computations (scipy.spatial.distance)
- Special functions (scipy.special)
- Statistical functions (scipy.stats)
- C/C++ integration (scipy.weave)


SciPy Cookbook
-------------------------------------------------

.. epigraph::

    This page hosts "recipes", or worked examples of commonly-done tasks. Some are introductory in nature, while others are quite advanced (these are at the bottom of the page). We encourage new users to post recipes even for simple tasks that are not yet represented here. Our goal is an easy learning experience for new users. Some of these recipes may be incorporated into tutorials in the future.

    -- http://www.scipy.org/Cookbook


SciPy Regression Example (regress.py)
----------------------------------------

.. literalinclude:: /code/regress.py
    :lines: 1-7


Matplotlib
----------------------------------------

Matplotlib is a 2D plotting library. It makes use of NumPy arrays for large data sets
so it plays well with NumPy and SciPy. It also supports using LaTex for figure titles and text.


Simple Plotting Example (regressplot.py)
----------------------------------------

.. literalinclude:: /code/regressplot.py
    :lines: 1-7


.. header:: 

   MA792K Spring 2011 Lecture 7

.. footer:: 

   © Mark Lavin 2011
