Table Of Contents

Previous topic

Installation without root access

Next topic

First steps with Python

This Page

Understanding packages

One of the key features of Python is that the actual core language is fairly small. This is an intentional design feature to maintain simplicity. Much of the powerful functionality comes through the standard library (batteries included) and external modules and packages.

Modules

A module is simply a file containing Python definitions, functions, and statements. Putting code into modules is useful because of the ability to import the module functionality into your script or IPython session, for instance:

import numpy
data = numpy.loadtxt('mydatafile.txt')

You’ll see a bunch of import statements in virtually every Python script and soon it will be second nature. It might seem like a lot of overhead in typing, but it keeps everything modular and separate. Different packages can contain functions with the same name (e.g. read()), and specifying the package where it comes from avoids clashes.

Tip

The import statement is a little bit more flexible than shown above.

First, there is a possibility to only load the functions you need from a package:

from numpy import loadtxt
data = loadtxt('mydatafile.txt')

You can also make all functions local:

from numpy import *

This can be handy when you work in an interactive environment (IPython), but it is strongly discouraged when writing scripts since it makes the scripts less readable to others. If you want to have access to functions such as sin and cos without typing numpy in front of it, you should import those functions explicitly (as in the previous example) or create a shorthand for the module:

import numpy as np
from matplotlib import pyplot as plt

You will see these import frequently in many scripts.

Packages

A package is just a way of collecting related modules together within a single tree-like hierarchy. Very complex packages like NumPy or SciPy have hundreds of individual modules so putting them into a directory-like structure keeps things organized and avoids name collisions. For example here is a partial list of sub-packages available within SciPy.

scipy.fftpack Discrete Fourier Transform algorithms
scipy.stats Statistical Functions
scipy.lib Python wrappers to external libraries
scipy.lib.blas Wrappers to BLAS library
scipy.lib.lapack Wrappers to LAPACK library
scipy.integrate Integration routines
scipy.linalg Linear algebra routines
scipy.sparse.linalg Sparse Linear Algebra
scipy.sparse.linalg.eigen Sparse Eigenvalue Solvers
scipy.sparse.linalg.eigen.arpack Eigenvalue solver using iterative methods.

Exercise: Import a package module and learn about it

Import the Linear algebra module from the SciPy package and find out what functions it provides.

Solution (Click to Show/Hide)

Start an interactive IPython session and import the linear algebra module. Use the dir and help functions to learn more:

In [1]: import scipy.linalg

In [2]: print(dir(scipy.linalg))
['LinAlgError', 'Tester', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_flinalg', 'all_mat', 'basic', 'bench', 'blas', 'block_diag', 'calc_lwork', 'cblas', 'cho_factor', 'cho_solve', 'cho_solve_banded', 'cholesky', 'cholesky_banded', 'circulant', 'clapack', 'companion', 'coshm', 'cosm', 'decomp', 'decomp_cholesky', 'decomp_lu', 'decomp_qr', 'decomp_schur', 'decomp_svd', 'det', 'diagsvd', 'eig', 'eig_banded', 'eigh', 'eigvals', 'eigvals_banded', 'eigvalsh', 'expm', 'expm2', 'expm3', 'fblas', 'flapack', 'flinalg', 'funm', 'get_blas_funcs', 'hadamard', 'hankel', 'hessenberg', 'hilbert', 'inv', 'invhilbert', 'kron', 'lapack', 'leslie', 'linalg_version', 'logm', 'lstsq', 'lu', 'lu_factor', 'lu_solve', 'matfuncs', 'misc', 'norm', 'orth', 'pinv', 'pinv2', 'qr', 'qr_old', 'rq', 'rsf2csf', 'schur', 'signm', 'sinhm', 'sinm', 'solve', 'solve_banded', 'solve_triangular', 'solveh_banded', 'special_matrices', 'sqrtm', 'svd', 'svdvals', 'tanhm', 'tanm', 'test', 'toeplitz', 'tri', 'tril', 'triu']

In [3]: help(scipy.linalg.eig)
Help on function eig in module scipy.linalg.decomp:

eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False)
    Solve an ordinary or generalized eigenvalue problem of a square matrix.
    
    Find eigenvalues w and right or left eigenvectors of a general matrix::
    
        a   vr[:,i] = w[i]        b   vr[:,i]
        a.H vl[:,i] = w[i].conj() b.H vl[:,i]
    
    where .H is the Hermitean conjugation.
    
    Parameters
    ----------
    a : array, shape (M, M)
        A complex or real matrix whose eigenvalues and eigenvectors
        will be computed.
    b : array, shape (M, M)
        Right-hand side matrix in a generalized eigenvalue problem.
        If omitted, identity matrix is assumed.
    left : boolean
        Whether to calculate and return left eigenvectors
    right : boolean
        Whether to calculate and return right eigenvectors
    
    overwrite_a : boolean
        Whether to overwrite data in a (may improve performance)
    overwrite_b : boolean
        Whether to overwrite data in b (may improve performance)
    
    Returns
    -------
    w : double or complex array, shape (M,)
        The eigenvalues, each repeated according to its multiplicity.
    
    (if left == True)
    vl : double or complex array, shape (M, M)
        The normalized left eigenvector corresponding to the eigenvalue w[i]
        is the column v[:,i].
    
    (if right == True)
    vr : double or complex array, shape (M, M)
        The normalized right eigenvector corresponding to the eigenvalue w[i]
        is the column vr[:,i].
    
    Raises LinAlgError if eigenvalue computation does not converge
    
    See Also
    --------
    eigh : eigenvalues and right eigenvectors for symmetric/Hermitian arrays

Finding and installing packages

If you’ve gotten this far you have a working scientific Python environment that has most of what you will ever need. Nevertheless it is almost certain that you will eventually find a need that is not met within your current installation. Here we learn where to find other useful packages and how to install them.

Package resources

Google

Google “python blah blah” or “python astronomy blah blah”

Tip

Good vs. bad resources

When you find some package on the web, look for a few things:

  • Good modern-looking documentation with examples
  • Installs easily without lots of dependencies (or has detailed installation instructions)
  • Actively developed

Resource lists

There are a number of sites specifically devoted to Python for astronomy with organized lists of useful resources and packages.

PyPI

The Python Package Index is the main repository for 3rd party Python packages (about 14000 packages and growing). An increasing number of astronomy related packages are available on PyPI, but this list misses a lot of available options.

The advantage of being on PyPI is the ease of installation/uninstallation using:

$:> pip install <package_name>
$:> pip uninstall <package_name>

These commands might require root access. If you don’t have root access, you can use either:

$:> pip install --user <package_name>
$:> pip uninstall --user <package_name>

Or better yet, use the virtualenv framework

Exercise: Find packages for coordinate manipulations

Find one or more Python packages that will transform coordinates from Galactic to FK5 ecliptic.

Hint: tags are helpful at astropython.org and don’t forget the “next” button at the bottom.

Copyright: Smithsonian Astrophysical Observatory under terms of CC Attribution 3.0 Creative Commons
 License