Seminar 2

Welcome to the first practical seminar. Did you know, we can use $\LaTeX$ syntax in Jupyter notebooks? $A = x^2$ <- This is Latex. Please google it

Python Syntax Examples:

In [1]:
A = 2
B = 3
C = A + B 
print C
5

There are a lot of free Libraries available via the python installer "pip". Let`s take a look at the two most important libraries for science.

In [2]:
import numpy as np
from matplotlib import pylab as plt

Numpy Array operations

Let's look at some arrays. Numpy arrays can be very handy because we can get around using loops or lambda operations. First, we will demonstrate some simple math.

In [3]:
a = np.array([1,2,6,3,2])
In [4]:
b = a**2
print a
print b
[1 2 6 3 2]
[ 1  4 36  9  4]
In [5]:
c = np.array([[1,2,3],[4,5,6]])
c
Out[5]:
array([[1, 2, 3],
       [4, 5, 6]])
In [6]:
d = np.zeros((5,5))
d
Out[6]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [7]:
d[0,0] = 1
d
Out[7]:
array([[1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [8]:
d[2,2] = 1
d
Out[8]:
array([[1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [9]:
plt.imshow(d)
plt.colorbar()
plt.show()

Numpy Row and Column assignments

RGB-array

An RGB array consists of three dimensions ($N \times M \times C$)

  • N,M: spacial pixel values
  • C: 3 Color slices Red, Green, Blue

We create an array with 20x20 pixel in three colors RGB -> (20,20,3) array.

  • Why do we need to set the object type to "int"?
  • What is the meaning of the number 255?
In [10]:
ColorArray = np.ones((20,20,3)).astype(int)
In [11]:
ColorArray[15,:] = 255
ColorArray[:,10] = 150

plt.imshow(ColorArray)
plt.show()
In [ ]:
 
In [12]:
ColorArray = np.ones((20,20,3)).astype(int)
ColorArray[::2 , ::2,1]  = 100
ColorArray[1::4,1::4,0]  = 200

plt.imshow(ColorArray)
plt.show()

What does a astronomical CCD image look like ?

(in an ideal world, without quantum mechanics) -> 2D Minecraft world

  • CCD Noise + image
In [13]:
CCDnoise = np.random.rand(20,20)*255*0.1
CCDimage = np.zeros((20,20))
CCDimage[10,10] = 200 

CCD = CCDnoise + CCDimage
In [14]:
plt.imshow(CCD)
plt.colorbar()
plt.show()

We will continue from here next week.

Homework

  • What is a Bayer filter? -Can you create the pattern of the Bayer arrangement in three lines or less?

Solution

In [15]:
BayerArray = np.zeros((10,10,3)).astype(int)
BayerArray[1::2,1::2,0] = 255 ### Red
BayerArray[0::2,1::2,1] = 255 ### Green
BayerArray[1::2,0::2,1] = 255 ### Green
BayerArray[0::2,0::2,2] = 255 ### Blue

plt.imshow(BayerArray)
plt.show()