# Course: Learn Laser Interferometry

1.Introduction > 1.Getting Started

1.IPython Notebooks

Author: Andreas Freise

# 1. Introduction¶

IPython Notebook is an interactive environment, in which you can combine code execution, text, mathematics, and images. It provides a platform that facilitates transparent and reproducible research as input data; code; analytical expression; explaining text and figures; computations; and results such as output data and figures, can be gathered and performed in the same notebook document. Because of this we believe that we and other researchers will use IPython notebook more and more frequently to present scientific results.

For the same reasons IPython Notebook also provides an excellent platform for teaching and learning numerical modelling, which we utilise in this course. In this notebook we go through the notebook basics you need to follow this course.

Prerequisites

This is the first notebook in this course, hence you don't need to have done anything but followed the installing instructions in the document Simulation_preparation.pdf.

After this session you will be able to...

• Use the basic features of an IPython notebook
• Plot results

# 2. IPython Notebook¶

## 2.1. Python¶

Python is a powerful programming language which is particularly useful for fast development and scripting. We will use only a very small subset of Python's functionality. In particular, we will do simple mathematical operations (using the package Numpy), some simple string operations and we will plot output data.

I recommend that you do not try to learn Python or IPython in general right now, but search the web for a solution for each particular task. A good starting point, for example, is the A Crash Course in Python for Scientists.

## 2.2. The IPython Notebook¶

### 2.2.1. Text Cells¶

These cells are just text, formatted using *Markdown*. You change the cells into markdown format by clicking Cell-->Cell Type-->Markdown in the toolbar above, or by using the keyboard shortcut esc+m.

### 2.2.2. Code Cells¶

Code cells are the main element in the IPython notebook. When you press 'ctrl+enter' the content of the cell will be evaluated as Python code and an output cell is created to display the result, see the simple examples below.

In [2]:
# Adding numbers
a=5
b=10
print(a+b)

15

In [3]:
# Joining strings together
one='red '
two='car'
print(one+two)

red car

In [4]:
import numpy as np
# Outputting sentences
r = 2
area = np.pi * r*r
print("This is a good way to present results: The area is {0:.1f} km^2".format(area))

This is a good way to present results: The area is 12.6 km^2


Note that we needed to import the package numpy above to use $\pi$. Once imported, i.e. once the cell above is evaluated, this package can be used everywhere within this notebook.

### 2.2.3. Latex and HTML¶

You can use HTML commands and Latex commands (for example: $\Delta L = L_X - L_Y$) in the markdown cells. You can also generate HTML text with code, for example to highlight certain data:

In [6]:
from IPython.display import display, HTML
a=5.512;
h=HTML('<b>This is bold: {0}</b>'.format(a));
display(h)

This is bold: 5.512

Note that we needed to import the packages display and HTML from IPython.display.

### 2.2.4. Displaying Graphics (SVG Files)¶

For any optical simulation it is very useful to prepare a sketch of the optical layout. We typically use Inkscape and the ComponentLibrary to prepare such sketches as Scalable Vector Graphics (svg files). Such files can be loaded into notebooks, however, at them moment this fails when converting to html which is used at the web page to show the notebooks. So we convert the images into png-format at the moment, which can be done in inkscape as well.

### 2.2.5. Plotting¶

Python provides a number of powerful plotting packages. We will be using inline plots based on the 'matplotlib' package. Examples and documentation can be found online, for example a notebook with good plotting examples, or this extensive lecture on plotting. A simple example is shown below.

In [10]:
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

x=np.linspace(1, 100, 10) # this creates a data vector with 10 elements

print("x = {0}".format(x))

y = x*x

fig = plt.plot(x, y)
plt.title('A simple function')
plt.xlabel('x [some units]')
plt.ylabel('y [some other units]')
plt.show(fig)

x = [   1.   12.   23.   34.   45.   56.   67.   78.   89.  100.]


Note that we imported matplotlib and added the line

%matplotlib inline

to make the plots appear in the notebook instead of popping out as new windows.

# 3. Summary¶

In this notebook we have seen how to...

• write code
• write text
• write math
• display figures
• plot results

by using IPython noteobok. In the next session you will learn about PyKat, which is a python wrapper for our simulation software FINESSE.

© Andreas Freise 2009 onwards. The css design is based on a free template from Free CSS Templates.