Basic reading and visualization of radar data with Py-ART

Introduction to Jupyter

What you are looking at is a Jupyter Notebook, a web-based interactive computation enviroment well suited for creating and sharing examples of scientific Python computations.

One of the key features of Jupyter notebooks is that Python code can be written and executed in cells.

Click on the next cell and press shift+enter to execute the Python code.


In [131]:
# This is a Python comment
# the next line is a line of Python code
print("Hello World!")


Hello World!

Cells can also be used to create textual materials using the markup language Markdown.

Double click on this cell (or any others) to see the raw markdown which produces the nicely formatted text.


One of the reasons these Notebooks are so well suited for scientific Python work is that Juypter is well integrated into the Scientific Python ecosystem. For example, plots can be included in notebooks for visualizing data. Execute the next two cells (using ctrl+enter)


In [132]:
# These two lines turn on inline plotting 
%matplotlib inline
import matplotlib.pyplot as plt

In [133]:
plt.plot([1,2,3])


Out[133]:
[<matplotlib.lines.Line2D at 0x7f8ac1db52b0>]

Python

  • General-purpose.
  • Interpreted.
  • Focuses on readability.
  • Excellent for interfacing with C, C++ and Fortran code.
  • Comprehesive standard library.
  • Extended with a large number of third-party packages.
  • Widely used in scientific programming.

This presentation will give a brief into to some key features of Python and the Scientific Python ecosystem to help those not familar with the language with the remainder of the class. This is in no way a comprehensive introduction to either topic. Excellent tutorials on Python and Scientific Python can be found online.

We will be using IPython for this class which is a package which allows Python code to be run inside a browser. This is in no way the only way to run python, the Python/IPython shell, scripts and various IDEs can also be used but will not be covered.

The notebook for this materials is available if you wish to follow along on your own computer, but we will be moving fast...

Variables

  • ### Integers

In [134]:
a = 1

In [135]:
a + 1


Out[135]:
2
  • ### Floating point numbers

In [136]:
b = 2.1

In [137]:
b + 1


Out[137]:
3.1

In [138]:
a + b


Out[138]:
3.1

In [139]:
type(a + b)


Out[139]:
float

Variables

  • ### Complex numbers

In [140]:
c = 1.5 + 0.5j  # complex numbers

In [141]:
print(c.real)
print(c.imag)


1.5
0.5
  • ### Booleans

In [142]:
d = 3 > 4

In [143]:
print(d)


False

In [144]:
type(d)


Out[144]:
bool

Variables

  • ### Strings

In [145]:
s = "Hello everyone"
type(s)


Out[145]:
str

In [146]:
a = "Hello " 
b = "World"
print(a + b)


Hello World

Variables can be cast from one type to another


In [147]:
a = 1
print(a)
print(type(a))


1
<class 'int'>

In [148]:
b = float(a)
print(b)
print(type(b))


1.0
<class 'float'>

In [149]:
s = "1.23"
print(s)
print(type(s))


1.23
<class 'str'>

In [150]:
f = float(s)
print(f)
print(type(f))


1.23
<class 'float'>

Containers

  • ### Lists

In [151]:
l = ['red', 'blue', 'green', 'black', 'white']

In [152]:
len(l)


Out[152]:
5

Indexing


In [153]:
l


Out[153]:
['red', 'blue', 'green', 'black', 'white']

In [154]:
print(l[0])
print(l[1])
print(l[2])


red
blue
green

In [155]:
print(l[-1])   # last element
print(l[-2])


white
black

In [156]:
l[0] = 'orange'
print(l)


['orange', 'blue', 'green', 'black', 'white']

Slicing


In [157]:
print(l[2:5])


['green', 'black', 'white']

In [158]:
print(l[2:-1])


['green', 'black']

In [159]:
print(l[1:6:2])


['blue', 'black']

In [160]:
l[::-1]


Out[160]:
['white', 'black', 'green', 'blue', 'orange']

Lists can store different type of variable in each element


In [161]:
ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]]

In [162]:
ll


Out[162]:
[5, 22.9, (14.8+1j), 'hello', [1, 2, 3]]

In [163]:
print(ll[0])
print(ll[1])
print(ll[2])
print(ll[3])
print(ll[4])


5
22.9
(14.8+1j)
hello
[1, 2, 3]

Containers

  • ### Dictionaries

In [164]:
d = {'name': 'Bobby', 'id': 223984, 'location': 'USA'}

In [165]:
d.keys()


Out[165]:
dict_keys(['location', 'name', 'id'])

In [166]:
d.values()


Out[166]:
dict_values(['USA', 'Bobby', 223984])

In [167]:
d['name']


Out[167]:
'Bobby'

In [168]:
d['id']


Out[168]:
223984

In [169]:
d['id'] = 1234

In [170]:
d['id']


Out[170]:
1234

Containers

  • ### Tuples

In [171]:
t = ('red', 'blue', 'green')

In [172]:
t[0]


Out[172]:
'red'

In [173]:
t[1:3]


Out[173]:
('blue', 'green')

Flow control

  • ### conditional (if, else, elif)

In [176]:
a = 4
if a > 10:
    print("a is larger than 10")
elif a < 10:
    print("a is less than 10")
else:
    print("a is equal to 10")


a is less than 10

Flow control

  • ### Loops

In [177]:
for i in range(10):
    print(i)


0
1
2
3
4
5
6
7
8
9

Functions


In [179]:
def func():
    print("Hello world")

In [180]:
func()


Hello world

Classes


In [188]:
class Car(object):
    
    engine = 'V4'    # class attribute
    
    def start(self):  # class method
        print("Starting the car with a", self.engine, "engine")

In [189]:
mycar = Car()

In [190]:
type(mycar)


Out[190]:
__main__.Car

In [191]:
mycar.engine


Out[191]:
'V4'

In [192]:
mycar.start()


Starting the car with a V4 engine

In [193]:
mycar.engine = 'V6'

In [194]:
mycar.engine


Out[194]:
'V6'

In [195]:
mycar.start()


Starting the car with a V6 engine

The Scientific Python ecosystem

  • ### NumPy


In [196]:
import numpy as np

In [197]:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])

In [198]:
a


Out[198]:
array([0, 1, 2, 3, 4, 5, 6, 7])

In [199]:
a.shape


Out[199]:
(8,)

In [200]:
a.ndim


Out[200]:
1

In [201]:
a.dtype


Out[201]:
dtype('int64')

In [202]:
a[0::2]


Out[202]:
array([0, 2, 4, 6])

In [203]:
a[a>3]


Out[203]:
array([4, 5, 6, 7])

In [204]:
a * 2 + 100


Out[204]:
array([100, 102, 104, 106, 108, 110, 112, 114])

In [205]:
a.mean()


Out[205]:
3.5

Arrays can be multi-dimensional


In [206]:
b = np.arange(12).reshape(3,4)

In [207]:
b.shape


Out[207]:
(3, 4)

In [208]:
b


Out[208]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [209]:
b[1,2]


Out[209]:
6

In [210]:
b[0:2, ::-1]


Out[210]:
array([[3, 2, 1, 0],
       [7, 6, 5, 4]])

The Scientific Python ecosystem

  • ### SciPy


In [211]:
import scipy

In [213]:
print(scipy.__doc__)


SciPy: A scientific computing package for Python
================================================

Documentation is available in the docstrings and
online at https://docs.scipy.org.

Contents
--------
SciPy imports all the functions from the NumPy namespace, and in
addition provides:

Subpackages
-----------
Using any of these subpackages requires an explicit import.  For example,
``import scipy.cluster``.

::

 cluster                      --- Vector Quantization / Kmeans
 fftpack                      --- Discrete Fourier Transform algorithms
 integrate                    --- Integration routines
 interpolate                  --- Interpolation Tools
 io                           --- Data input and output
 linalg                       --- Linear algebra routines
 linalg.blas                  --- Wrappers to BLAS library
 linalg.lapack                --- Wrappers to LAPACK library
 misc                         --- Various utilities that don't have
                                  another home.
 ndimage                      --- n-dimensional image package
 odr                          --- Orthogonal Distance Regression
 optimize                     --- Optimization Tools
 signal                       --- Signal Processing Tools
 sparse                       --- Sparse Matrices
 sparse.linalg                --- Sparse Linear Algebra
 sparse.linalg.dsolve         --- Linear Solvers
 sparse.linalg.dsolve.umfpack --- :Interface to the UMFPACK library:
                                  Conjugate Gradient Method (LOBPCG)
 sparse.linalg.eigen          --- Sparse Eigenvalue Solvers
 sparse.linalg.eigen.lobpcg   --- Locally Optimal Block Preconditioned
                                  Conjugate Gradient Method (LOBPCG)
 spatial                      --- Spatial data structures and algorithms
 special                      --- Special functions
 stats                        --- Statistical Functions

Utility tools
-------------
::

 test              --- Run scipy unittests
 show_config       --- Show scipy build configuration
 show_numpy_config --- Show numpy build configuration
 __version__       --- Scipy version string
 __numpy_version__ --- Numpy version string


The Scientific Python ecosystem

  • ### matplotlib


In [214]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib
/home/rjackson/anaconda3/lib/python3.5/site-packages/IPython/core/magics/pylab.py:161: UserWarning: pylab import has clobbered these variables: ['f']
`%matplotlib` prevents importing * from pylab and numpy
  "\n`%matplotlib` prevents importing * from pylab and numpy"

In [215]:
plot([1,2,3])


Out[215]:
[<matplotlib.lines.Line2D at 0x7f8ac1b24588>]

In [216]:
a = np.random.rand(30, 30)
imshow(a)
colorbar()


Out[216]:
<matplotlib.colorbar.Colorbar at 0x7f8ac1ac1dd8>

In [ ]: