I am working on the file "5/5.ipynb".
The first task is:
For a = 0.5, the curve is flatter; for a = 2, the curve is steeper.
3.
Picking $\mu$ = 0.1
Can you write Python code for it?

mu = 0.1

reaction_times_up = []
reaction_times_down = []

for trial in range(2000):
    x = np.zeros_like(time)
    for i, t in enumerate(time[:-1]):
        x[i+1] = x[i] + mu * dt + sigma * np.sqrt(dt) * np.random.normal()
        if x[i+1] >= a:  # up
            reaction_times_up.append(t)
            break
        elif x[i+1] <= -a:  # down
            reaction_times_down.append(t)
            break


I am working on the file "5/5.ipynb".
The first task is:
Picking $m_{\mu}$ = 4 and $s_{\mu}$ = 7
Can you write Python code for it?

m_mu = 4
s_mu = 7

reaction_times_up = []
reaction_times_down = []

for trial in range(2000):
    mu = np.random.normal(m_mu, s_mu)
    x = np.zeros_like(time)
    for i, t in enumerate(time[:-1]):
        x[i+1] = x[i] + mu * dt + sigma * np.sqrt(dt) * np.random.normal()
        if x[i+1] >= a:  # up
            reaction_times_up.append(t)
            break
        elif x[i+1] <= -a:  # down
            reaction_times_down.append(t)
            break

fig, axes = plt.subplots(1, 2, figsize=(10, 5))

for ax, reaction_times, title in zip(axes, [reaction_times_up, reaction_times_down], ['"Up" Response', '"Down" Response']):
    plt.sca(ax)
    plt.hist(reaction_times, 30)
    plt.xlabel('Reaction time [s]')
    plt.ylabel('# of trials')
    plt.title(title)
    plt.xlim(0, 10)
    plt.ylim(0, 1200)

np.mean(reaction_times_up), np.mean(reaction_times_down)


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
CelebA
Run your GANs on CelebA.  It will take around 20 minutes on the average GPU to run one epoch.  You can run the whole epoch or stop when it starts to generate realistic faces.
Can you write Python code for it?

batch_size = 64
z_dim = 128
learning_rate = 0.001
beta1 = 0.5

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
epochs = 1

celeba_dataset = helper.Dataset('celeba', glob(os.path.join(data_dir, 'img_align_celeba/*.jpg')))
with tf.Graph().as_default():
    train(epochs, batch_size, z_dim, learning_rate, beta1, celeba_dataset.get_batches,
          celeba_dataset.shape, celeba_dataset.image_mode)


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
CelebA
The CelebFaces Attributes Dataset (CelebA) dataset contains over 200,000 celebrity images with annotations.  Since you're going to be generating faces, you won't need the annotations.  You can view the first number of examples by changing show_n_images.
Can you write Python code for it?

show_n_images = 25

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB')
pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB'))


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
Discriminator
Implement discriminator to create a discriminator neural network that discriminates on images.  This function should be able to reuse the variabes in the neural network.  Use tf.variable_scope with a scope name of "discriminator" to allow the variables to be reused.  The function should return a tuple of (tensor output of the generator, tensor logits of the generator).
Can you write Python code for it?

def discriminator(images, reuse=False):
    """
    Create the discriminator network
    :param image: Tensor of input image(s)
    :param reuse: Boolean if the weights should be reused
    :return: Tuple of (tensor output of the discriminator, tensor logits of the discriminator)
    """
    # TODO: Implement Function
    alpha=0.2
    with tf.variable_scope('discriminator', reuse=reuse):
        conv1 = tf.layers.conv2d(images, 32, 5, 2, 'same')
        lrelu1 = tf.maximum(alpha * conv1, conv1)
        #print(lrelu1.shape)

        conv2 = tf.layers.conv2d(lrelu1, 64, 5, 2, 'same')
        batch_norm1 = tf.layers.batch_normalization(conv2, training=True)
        lrelu2 = tf.maximum(alpha * batch_norm1, batch_norm1)
        #print(lrelu2.shape)

        conv3 = tf.layers.conv2d(lrelu2, 128, 5, 2, 'same')
        batch_norm2 = tf.layers.batch_normalization(conv3, training=True)
        lrelu3 = tf.maximum(alpha * batch_norm2, batch_norm2)
        #print(lrelu3.shape)

        flat = tf.reshape(lrelu3, (-1, 4*4*128))

        logits = tf.layers.dense(flat, 1)
        output = tf.sigmoid(logits)

        return output, logits

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_discriminator(discriminator, tf)


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
Generator
Implement generator to generate an image using z. This function should be able to reuse the variabes in the neural network.  Use tf.variable_scope with a scope name of "generator" to allow the variables to be reused. The function should return the generated 28 x 28 x out_channel_dim images.
Can you write Python code for it?

def generator(z, out_channel_dim, is_train=True):
    """
    Create the generator network
    :param z: Input z
    :param out_channel_dim: The number of channels in the output image
    :param is_train: Boolean if generator is being used for training
    :return: The tensor output of the generator
    """
    # TODO: Implement Function

    alpha=0.2
    reuse=not is_train
    with tf.variable_scope('generator', reuse=reuse):

        fc1 = tf.layers.dense(z, 7*7*128)
        reshape = tf.reshape(fc1, (-1, 7, 7, 128))
        batch_norm1 = tf.layers.batch_normalization(reshape, training=is_train)
        lrelu1 = tf.maximum(alpha * batch_norm1, batch_norm1)
        #print(lrelu1.shape)

        deconv1 = tf.layers.conv2d_transpose(lrelu1, 64, 5, 2, 'same')
        batch_norm2 = tf.layers.batch_normalization(deconv1, training=is_train)
        lrelu2 = tf.maximum(alpha * batch_norm2, batch_norm2)
        #print(lrelu2.shape)

        deconv2 = tf.layers.conv2d_transpose(lrelu2, 32, 5, 2, 'same')
        batch_norm3 = tf.layers.batch_normalization(deconv2, training=is_train)
        lrelu3 = tf.maximum(alpha * batch_norm3, batch_norm3)
        #print(lrelu3.shape)

        logits = tf.layers.conv2d_transpose(lrelu3, out_channel_dim, 3, 1, 'same')
        output = tf.tanh(logits)

        return output

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_generator(generator, tf)


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
Loss
Implement model_loss to build the GANs for training and calculate the loss.  The function should return a tuple of (discriminator loss, generator loss).  Use the following functions you implemented:
- discriminator(images, reuse=False)
- generator(z, out_channel_dim, is_train=True)
Can you write Python code for it?

def model_loss(input_real, input_z, out_channel_dim):
    """
    Get the loss for the discriminator and generator
    :param input_real: Images from the real dataset
    :param input_z: Z input
    :param out_channel_dim: The number of channels in the output image
    :return: A tuple of (discriminator loss, generator loss)
    """
    # TODO: Implement Function

    smooth = 0.1
    g_model = generator(input_z, out_channel_dim)
    d_model_real, d_logits_real = discriminator(input_real)
    d_model_fake, d_logits_fake = discriminator(g_model, reuse=True)

    d_loss_real = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_real, labels=tf.ones_like(d_model_real) * (1 - smooth)))
    d_loss_fake = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.zeros_like(d_model_fake)))
    g_loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.ones_like(d_model_fake)))

    d_loss = d_loss_real + d_loss_fake
    return d_loss, g_loss

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_loss(model_loss)


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
MNIST
Test your GANs architecture on MNIST.  After 2 epochs, the GANs should be able to generate images that look like handwritten digits.  Make sure the loss of the generator is lower than the loss of the discriminator or close to 0.
Can you write Python code for it?

batch_size = 64
z_dim = 128
learning_rate = 0.001
beta1 = 0.5

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
epochs = 2

mnist_dataset = helper.Dataset('mnist', glob(os.path.join(data_dir, 'mnist/*.jpg')))
with tf.Graph().as_default():
    train(epochs, batch_size, z_dim, learning_rate, beta1, mnist_dataset.get_batches,
          mnist_dataset.shape, mnist_dataset.image_mode)


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
Neural Network Training
Show Output
Use this function to show the current output of the generator during training. It will help you determine how well the GANs is training.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

def show_generator_output(sess, n_images, input_z, out_channel_dim, image_mode):
    """
    Show example output for the generator
    :param sess: TensorFlow session
    :param n_images: Number of Images to display
    :param input_z: Input Z Tensor
    :param out_channel_dim: The number of channels in the output image
    :param image_mode: The mode to use for images ("RGB" or "L")
    """
    cmap = None if image_mode == 'RGB' else 'gray'
    z_dim = input_z.get_shape().as_list()[-1]
    example_z = np.random.uniform(-1, 1, size=[n_images, z_dim])

    samples = sess.run(
        generator(input_z, out_channel_dim, False),
        feed_dict={input_z: example_z})

    images_grid = helper.images_square_grid(samples, image_mode)
    pyplot.imshow(images_grid, cmap=cmap)
    pyplot.show()


I am working on the file "5/dlnd_face_generation.ipynb".
The first task is:
Preprocess the Data
Since the project's main focus is on building the GANs, we'll preprocess the data for you.  The values of the MNIST and CelebA dataset will be in the range of -0.5 to 0.5 of 28x28 dimensional images.  The CelebA images will be cropped to remove parts of the image that don't include a face, then resized down to 28x28.
The MNIST images are black and white images with a single [color channel](https://en.wikipedia.org/wiki/Channel_(digital_image%29) while the CelebA images have [3 color channels (RGB color channel)](https://en.wikipedia.org/wiki/Channel_(digital_image%29#RGB_Images).
Build the Neural Network
You'll build the components necessary to build a GANs by implementing the following functions below:
- model_inputs
- discriminator
- generator
- model_loss
- model_opt
- train
Check the Version of TensorFlow and Access to GPU
This will check to make sure you have the correct version of TensorFlow and access to a GPU
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer.  You are using {}'.format(tf.__version__)
print('TensorFlow Version: {}'.format(tf.__version__))

# Check for a GPU
if not tf.test.gpu_device_name():
    warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))


I am working on the file "5/spotify-homework-hon-june6_Graded.ipynb".
The first task is:
2) What genres are most represented in the search results?
Edit your previous printout to also display a list of their genres in the format
GENRE_1, GENRE_2, GENRE_3". If there are no genres, print "No genres listed".
Tip: "how to join a list Python" might be a helpful search
Can you write Python code for it?

for names in lil_names:
    if not names['genres']:
        print(names['name'], names['popularity'], "there are no genres listed")
    else:
        print(names['name'], names['popularity'], names["genres"])

#Join all the lists of genres in the dictionary and then count the number of elements in it


I am working on the file "5/spotify-homework-hon-june6_Graded.ipynb".
The first task is:
8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
Can you write Python code for it?

response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50')

b_data = response.json()

b_artists = b_data['artists']
b_names = b_artists['items']
b_pop_count = 0
b_number = 0

for names in b_names:
    print(names['name'], names['popularity'])
    b_number = b_number + 1
    b_pop_count = b_pop_count + int(names['popularity'])
    avg_b_pop = b_pop_count / int(b_number)
print("The Biggies' average popularity is", avg_b_pop)

lil_pop_count = 0
lil_number = 0

for names in lil_names:
    print(names['name'], names['popularity'])
    lil_number = lil_number + 1
    lil_pop_count = lil_pop_count + int(names['popularity'])
    avg_lil_pop = lil_pop_count / int(lil_number)
print("The Lils average popularity is", avg_lil_pop)

print("The Lils are far more popular")


I am working on the file "5/spotify-homework-hon-june6_Graded.ipynb".
The first task is:
ANSWER:
    #jonathansoma.com/site/lede/foundations/python-patterns/looping-problems/
second_most_popular_name = ""
second_most_popular_score = 0
for artist in artists:
    print("Looking at", artist['name]', "who has a popularity score of", artist['popularity'])
    #THE CONDITIONAL aka what you are testing
    print("Comparing", artist['popularity'], "to", most_popular_score)
    if artist['popularity'] > most_popular_score and artist['name'] != 'Lil Wayne':
                           OR
if artist['popularity'] > most_popular_score:
    if artist['name'] == "Lil Wayne":
        print("Nice try, Lil Wayne, we don't care")
    else:
        print("Not Lil Wayne, updating our notebook")

    #The change, aka what you are keeping track of
    most_popular_name = artist['name']
    most_popular_score = artist['popularity']

print(most_popular_name, most_popular_score)
But what if more than one person has the highest score?
Aggregation Problem. When you're looping through a series of objects and sometimes you
want to add one of those objects to a different list.
target score = 72
initial condition
second_best_artist = []
for artist in artists:
    print("Looking at", artist['name'], "who has a popularity of", )
    #2: Conditional. When we want to add someone to our list
                               if artist['popularity'] == 72:
                               print("!!! The artist's popularity is 72")
                               #The Change
                               #Add that artist to our list
                               #.append(newthing) is how we do that in Python
                               second_best_artists.append(artist['name'])
print("Our second best artists are:")
                           for artist in second_best_artist:
                           print(artist)

Print a list of Lil's that are more popular than Lil' Kim.
Can you write Python code for it?

for kim in lil_names:
    print(kim['name'], kim['id'])

response = requests.get("https://api.spotify.com/v1/artists/5tth2a3v0sWwV1C7bApBdX/")
kim_data = response.json()

#print(kim_data)
kim_followers = kim_data['followers']
total_kim_followers = kim_followers['total']
#print(total_kim_followers)

for artists in lil_names:
    if artists["followers"]["total"] > total_kim_followers:
        print(artists['name'], artists['popularity'])

#ANSWER:

for artist in artists:
    #print("Looking at", artist['name'])
    if artist['name'] == "Lil' Kim":
        print("Found Lil Kim")
        print(artist['popularity'])
    else:
        pass
        #print("Not Lil Kim")

lil_kim_popularity = 62

for artist in artists:
    if artist['popularity'] > lil_kim_popularity:
        print(artist['name'], "is more popular with a score of", artist['popularity'])
        more_popular_than_lil_kim.append(artist['name'])
    else:
        print(artist['name'], "is less popular with a score of", artist['popularity'])
print("#### More popular than Lil Kim ####"):
    print(artist_name)

    more_popular_string = ", ".join(more_popular_than_lil_kim)
    print("Artists mroe popular than Lil Kim are:", more_popular_string)


I am working on the file "5/spotify-homework-hon-june6_Graded.ipynb".
The first task is:
Graded = 7/8
Can you write Python code for it?

import requests

response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&market=US&limit=50')


I am working on the file "5/spotify-homework-hon-june6_Graded.ipynb".
The first task is:
QUESTION: Why does this return both true and not true statements for non-explicit statements?
for track in top_mama_tracks:
    print(track['name'], track['explicit'])
    if True:
        print(track['name'], "is explicit and has a popularity of", track['popularity'])
    if not track['explicit']:
        print(track['name'], "is not explicit and has a popularity of", track['popularity'])
Can you write Python code for it?

mama_exp_count = 0
mama_exp_pop_count = 0
mama_ok_count = 0
mama_ok_pop_count = 0
mama_exp_len = 0
mama_ok_len = 0

for track in top_mama_tracks:
    print(track['name'], track['explicit'], track['popularity'], track["duration_ms"])
    if True:
        mama_exp_count = mama_exp_count + 1
        mama_exp_pop_count = mama_exp_pop_count + int(track['popularity'])
        mama_avg_pop = int(mama_exp_pop_count) / int(mama_exp_count)
        mama_exp_len = mama_exp_len + int(track["duration_ms"])
    if not track['explicit']:
        mama_ok_count = mama_ok_count + 1
        mama_ok_pop_count = mama_ok_pop_count + int(track['popularity'])
        mama_ok_avg_pop = int(mama_ok_pop_count) / int(mama_ok_count)
        mama_ok_len = mama_ok_len + int(track["duration_ms"])

if mama_exp_count > 0:
       #1 minute is 60000 milliseconds, who knew?
    print("The average popularity for Lil Mama's xplicit songs is", mama_avg_pop)
    mama_exp_mins = int(mama_exp_len) / 60000
    print("Lil Mama has", mama_exp_mins, "minutes of explicit songs")
if mama_ok_count > 0:
    print("The average popularity for Lil Mama's non-explicit songs is", mama_ok_avg_pop)
    mama_ok_mins = int(mama_ok_len) / 60000
    print("Lil Mama has", mama_ok_mins, "minutes of non-explicit songs")


I am working on the file "5/spotify-homework-hon-june6_Graded.ipynb".
The first task is:
Will the world explode if a musician swears?
Get an average popularity for their explicit songs vs. their non-explicit songs.
How many minutes of explicit songs do they have? Non-explicit?
Can you write Python code for it?

wayne_explicit_count = 0
wayne_exp_popularity_count = 0
wayne_ok_count = 0
wayne_ok_popularity_count = 0
wayne_explicit_len = 0
wayne_ok_len = 0
for track in top_wayne_tracks:
    print(track['name'], track['explicit'], track['popularity'], track["duration_ms"])
    if True:
        wayne_explicit_count = wayne_explicit_count + 1
        wayne_exp_popularity_count = wayne_exp_popularity_count + int(track['popularity'])
        wayne_avg_pop = wayne_exp_popularity_count / wayne_explicit_count
        wayne_explicit_len = wayne_explicit_len + int(track["duration_ms"])
    if not track['explicit']:
        wayne_ok_count = wayne_ok_count + 1
        wayne_ok_popularity_count = wayne_ok_popularity_count + track['popularity']
        wayne_ok_avg_pop = wayne_ok_popularity_count / wayne_ok_count
        wayne_ok_len = wayne_ok_len + track["duration_ms"]

if wayne_explicit_count > 0:
    print("The average popularity for Lil Wayne's explicit songs is", wayne_avg_pop)
    #1 minute is 60000 milliseconds, who knew?
    wayne_explicit_mins = int(wayne_explicit_len) / 60000
    print("Lil Wayne has", wayne_explicit_mins, "minutes of explicit songs")
if wayne_ok_count > 0:
    print("The average popularity for Lil Wayne's non-explicit songs is", wayne_ok_avg_pop)
    wayne_ok_mins = int(wayne_ok_len) / 60000
    print("Lil Wayne has", wayne_ok_mins, "minutes of explicit songs")


I am working on the file "55_VH_Resample.ipynb".
The first task is:
Because we are resampling the moving image using the physical location of the fixed image without any transformation (identity), most of the resulting volume is empty. The image content appears in slice 57 and below.
Can you write Python code for it?

myshow(out)

  #combine the two images using a checkerboard pattern:
  #because the moving image is single channel with a high dynamic range we rescale it to [0,255] and repeat
  #the channel 3 times
vis = sitk.CheckerBoard(fixed,sitk.Compose([sitk.Cast(sitk.RescaleIntensity(out),sitk.sitkUInt8)]*3), checkerPattern=[15,10,1])

myshow(vis)


I am working on the file "55_VH_Resample.ipynb".
The first task is:
Write the image to the Output directory: (1) original as a single image volume and (2) as a series of smaller JPEG images which can be constructed into an animated GIF.
Can you write Python code for it?

import os

sitk.WriteImage(vis, os.path.join(OUTPUT_DIR, "example_resample_vis.mha"))

temp = sitk.Shrink(vis,[3,3,2])
sitk.WriteImage(temp, [os.path.join(OUTPUT_DIR,"r{0:03d}.jpg".format(i)) for i in range(temp.GetSize()[2])])


I am working on the file "56_nalu/nalu_problem.ipynb".
The first task is:
Generate random training data (in the range [0,1])
Can you write Python code for it?


I am working on the file "56_nalu/nalu_problem.ipynb".
The first task is:
Initialize second model and optimizer (closer to the paper)
Can you write Python code for it?

class MyModel2(nn.Module):
    def __init__(self):
        super().__init__()
        self.h1 = nn.Linear(1, 8)
        self.h2 = nn.Linear(8, 8)
        self.h3 = nn.Linear(8, 8)
        self.h4 = nn.Linear(8, 1)

    def forward(self, xb):
        res = torch.tanh(self.h1(xb))
        res = torch.tanh(self.h2(res))
        res = torch.tanh(self.h3(res))
        res = self.h4(res)
        return res

mod2 = MyModel2()
opt2 = optim.SGD(mod2.parameters(), lr=.01)

for epoch in tqdm(range(10)):
    for i in x:
        pred = mod2(i.unsqueeze(0))
        loss = loss_func(pred, i.unsqueeze(0))

        loss.backward()
        opt2.step()
        opt2.zero_grad()

mod2(Tensor([.3]))

mod2(Tensor([-1]))

points = [[x,mod2(Tensor([x])).item()] for x in np.linspace(-2,2,1000)]

plt.scatter([x[0] for x in points], [x[1] for x in points])
plt.scatter(np.linspace(-1,1,100),np.linspace(-1,1,100))


I am working on the file "56_nalu/nalu_problem.ipynb".
The first task is:
Results of trained network
Can you write Python code for it?

mod(Tensor([5]))

points = [[x,mod(Tensor([x])).item()] for x in np.linspace(-10,100,1000)]

plt.scatter([x[0] for x in points], [x[1] for x in points])


I am working on the file "56_nalu/nalu_problem.ipynb".
The first task is:
Train network for 10 epochs
Can you write Python code for it?

for epoch in tqdm(range(10)):
    for i in x:
        pred = mod(i.unsqueeze(0))
        loss = loss_func(pred, i.unsqueeze(0))

        loss.backward()
        opt.step()
        opt.zero_grad()


I am working on the file "56_nalu/nalu_problem.ipynb".
The first task is:
Training and extrapolation work perfectly if we do not add any non-linearity. But adding any activation function makes extrapolation fail at some points. The first model uses RELU which is very close to linear.
Initialize model and optimizer
Can you write Python code for it?

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.h1 = nn.Linear(1, 20)
        self.h2 = nn.Linear(20, 1)

    def forward(self, xb):
        return self.h2(F.relu(self.h1(xb)))

mod = MyModel()
opt = optim.SGD(mod.parameters(), lr=.05)

mod(Tensor([7]))


I am working on the file "5_Anomaly_Detection.ipynb".
The first task is:
Finally, we can start an AnmalyDetection() object, use train and validation data to fit the Gaussian Distribution paramters and find the proper value for $\epsilon$. With the fitted model, we can predict the anomaly cases on the test dataset and check the performance.
Can you write Python code for it?

ad = AnomalyDetection(multi_variate=False)
ad.fit(X_train, X_val, y_val)
y_predict = ad.predict(X_test)
print "True Values:      {}".format(y_test)
print "Predicted Values: {}".format(y_predict)
print "Prediction Accuracy: {:.2%}".format(np.mean((y_predict == y_test).astype(float)))


I am working on the file "5_Anomaly_Detection.ipynb".
The first task is:
Now let's load the Iris Dataset for a demo.
We can see that the dataset has 150 examples, with three different classes 0, 1, 2.
Can you write Python code for it?

from sklearn.datasets import load_iris
iris = load_iris()

X = iris['data']
y = iris['target']

print X.shape
print y.shape

print "Number of Classes 0: {}".format(np.sum(y == 0))
print "Number of Classes 1: {}".format(np.sum(y == 1))
print "Number of Classes 2: {}".format(np.sum(y == 2))


I am working on the file "5_Anomaly_Detection.ipynb".
The first task is:
Now we have 100 normal examples, and 50 abnormal cases.
Can you write Python code for it?

print "Number of Classes 0: {}".format(np.sum(y_new == 0))
print "Number of Classes 1: {}".format(np.sum(y_new == 1))


I am working on the file "5_Anomaly_Detection.ipynb".
The first task is:
Then we can split the dataset into train (with all normal examples), validation (normal & abnormal examples), and test (the rest of normal & abnormal examples).
Can you write Python code for it?

X_normal = X[y_new == 0]
y_normal = y_new[y_new == 0]
X_abnormal = X[y_new == 1]
y_abnormal = y_new[y_new == 1]

from sklearn.cross_validation import train_test_split

X_normal_train_val, X_normal_test, y_normal_train_val, y_normal_test = \
    train_test_split(X_normal, y_normal, test_size=0.2, random_state=26)

X_normal_train, X_normal_val, y_normal_train, y_normal_val = \
    train_test_split(X_normal_train_val, y_normal_train_val, test_size=0.25, random_state=26)

X_abnormal_val, X_abnormal_test, y_abnormal_val, y_abnormal_test = \
    train_test_split(X_abnormal, y_abnormal, test_size=0.5, random_state=26)

X_train = X_normal_train
y_train = y_normal_train
X_val = np.r_[X_normal_val, X_abnormal_val]
y_val = np.r_[y_normal_val, y_abnormal_val]
X_test = np.r_[X_normal_test, X_abnormal_test]
y_test = np.r_[y_normal_test, y_abnormal_test]

print X_train.shape
print X_val.shape
print X_test.shape


I am working on the file "5_Genesis_preprocessor.ipynb".
The first task is:
Match beam file
Can you write Python code for it?

beta_av = 20.0
beam=get_beam_peak(beamf)
beam.E=E_beam
rematch(beta_av, l_fodo, qdh, sase3, extra_fodo, beam, qf, qd)
beamf = transform_beam_file(beamf ,transform = [ [beam.beta_x,beam.alpha_x], [beam.beta_y,beam.alpha_y] ], energy_new = beam.E, emit_scale = 1.0)
beamf = cut_beam(beamf,[-2e-6, 2e-6])

fig=plt.figure()
fig.set_size_inches((20,15))
plot_beam(fig, beamf)


I am working on the file "5_Genesis_preprocessor.ipynb".
The first task is:
Plot beamfile
Can you write Python code for it?

fig=plt.figure()
fig.set_size_inches((20,15))
plot_beam(fig, beamf)


I am working on the file "5_Genesis_preprocessor.ipynb".
The first task is:
Setting input parameters
electron beam energy and expected radiation photon energy
Can you write Python code for it?


I am working on the file "5_Genesis_preprocessor.ipynb".
The first task is:
Tapering the undulator
linear, quadratic,power-law...
Can you write Python code for it?

def f1(n, n0, a0, a1, a2):
    '''
    piecewise-quadratic tapering function
    '''
    for i in xrange(1,len(n0)):
        if n < n0[i]:
            return a0 + (n-n0[i-1])*a1[i-1] + (n-n0[i-1])**2 * a2[i-1]
        a0 += (n0[i]-n0[i-1])*a1[i-1] + (n0[i]-n0[i-1])**2 * a2[i-1]

    return 1.0

tap_start=3 #number of undulators
lin_tap=0.01 #taper step
quad_tap=0.0

n = 60
n0 = [0,tap_start,60]
a0 = und.Kx
a1 = [0,lin_tap*a0]
a2 = [0,quad_tap]

taper_func1 = lambda n : f1(n, n0, a0, a1, a2)

sase3= taper(sase3, taper_func1)


I am working on the file "5_Genesis_preprocessor.ipynb".
The first task is:
generate Genesis input object
Can you write Python code for it?

inp = generate_input(up, beam, itdp=False)
inp.lattice_str = generate_lattice(sase3, unit = up.lw, energy = beam.E) #generate Genesis lattice based on Ocelot lattice object
inp.beam_file_str = beam_file_str(beamf)
#inp.beamfile = 'tmp.beam'

inp.runid = run_id
inp.run_dir = run_dir
inp.ipseed = 17111+7*run_id # defines shot-noise, changes automatically
# below other Genesis parameters may be specified, like prad0, dgrid, etc.....


I am working on the file "5_Genesis_preprocessor.ipynb".
The first task is:
specify the run_dir - directory into which the experimental results will be saved
Can you write Python code for it?

run_dir = 'gen_stst' #directory to dump data
run_id=0 # run number (subdirectory 'run_#') for statistical studies
try:
    os.makedirs(run_dir)
except:
    pass
launcher = get_genesis_launcher('genesis') # launcher object to start genesis


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
5.1 Spatial Frequencies
To first approximation, the observed sky and sampled visibilites have a Fourier pair relationship via van Cittert–Zernike theorem ➞, that is the Fourier Transform of the sampled visibilites is an image of the observed sky, and the Fourier Transform of the observed sky image is the sampled visibilities. Before we dive into specifics of radio interferometric imaging using aperture synthesis, we will cover the ideas which relate the spatial domain (i.e. an image) to the spatial frequency domain (i.e. the visibilities). To do this we will start with the Fourier transform which is the mathematics that relate the two domains. Using that framework we will transform simple signals between the two domains to understand how the a signal in one domain appears in the other. This is the groundwork we need to understand before we can start constructing sky images from the observed visibilities of an interferometric array.
5.1.1 The Fourier Transform in Two Dimensions
We will start with a bit of mathematics, but will soon move to some visual examples which are often more useful to get started to learn about Fourier transforms.
As described in $\S$ 2.4 ➞ the Fourier Transform of a continuous, 1-dimensional function $f(x)$ is
\begin{equation}
g(t) = \int_{-\infty}^{\infty} f(x) e^{-2\pi i xt} dx
\end{equation}
The discrete Fourier transform (DFT) $\S$ 2.8 ➞ of a discrete, 1-dimensional function $f(x)$ is
$$g(t) = \sum_{-\infty}^{\infty} f(x) e^{-2\pi i xt} dx$$
The notation $g \rightleftharpoons f$ denotes that the function $f$ and $g$ are Fourier pairs, that is if $g$ is the Fourier transform of $f$, then $f$ is the inverse Fourier transform of $g$.
A 2-dimensional Fourier transform is simply the product of the Fourier Transform in each dimension (as each dimension is orthogonal).
$$ I(l,m) = \int_{-\infty}^{\infty} V_u(u) e^{-2\pi iul} \,du \int_{-\infty}^{\infty} V_v(v) e^{-2\pi ivm} \, dv = \int_{-\infty}^{\infty}...
Can you write Python code for it?

#soccer = mpimg.imread('figures/WLA_moma_Umberto_Boccioni_Dynamism_of_a_Soccer_Player_1913_512.png')
cyclist = mpimg.imread('figures/Umberto_Boccioni_Dynamism_of_a_Cyclist_512.png')
duck = mpimg.imread('figures/Anas_platyrhynchos_male_female_quadrat_512.png')


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: 'Dynamism of a Duck (Redux)', reconstructed hybrid image using the spatial frequency amplitudes of the duck and the phases of 'Dynamism of a Cyclist'.
Though with both hybrid images the image in which the phase is extracted from dominates they are not perfect reconstructions of only one image. In both there is now a sheen of noise across the images. The amplitude information plays a role. This noisiness of the hybrid image is due to the fact that the two original images are effectly uncorrelated, i.e. they are not related. So, one would not expect the amtplitude and phase information from one to match the other.
What would happen if instead of swapping the amplitude information of the duck for the painting we just set all the amplitude of all the pixels to one? That is, we effectively throw out any amplitude information and make a phase information-only image?
Can you write Python code for it?

fig = plt.figure(figsize=(8,8))
plt.title('Duck (phase-only)')
phs = np.angle(fftDuck)
amp = 1.*np.ones_like(fftDuck) #set all the amplitude values to 1
fftPhsImg0 = amp * (np.cos(phs) + 1j * np.sin(phs))
phsImg0 = np.abs(np.fft.ifft2(np.fft.fftshift(fftPhsImg0)))
phsImg0Plot = plt.imshow(phsImg0)
phsImg0Plot.set_cmap('gray')
#fig.savefig("phs_only_duck.png", bbox_inches='tight', pad_inches=0)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: A Duck
With our eyes and visual learning to understand signals in the spatial domian these are clearly two different images. This may seem like an obvious point, of course they are very different images, but that is because we have learned to understand signals in the spatial domain very well.
The spatial frequency domain represents the decomposition of a spatial domain signal into complex sinusoidal waves. In the next section we will look at some simple examples which hopefully provide some insight into this domain. A useful metaphor here is to that of painting an image with a number different-sized paint brushes at different stroke angles. The painting is the spatial-domain image, and one can think of a point in the spatial-frequency domain as representing the amount of paint (in our case, a complex power) applied using a specific brush size at a specific stroke angle. In the spatial frequency domain, all points at the same radius represent the same sized paint brush but at all possible stroke angles. And, points further out represent smaller paint brushes (that is the brushes which can do fine details) compared to points closer to the centre which are for the large structures such as flat backgrounds. Additionally, not as much stroke angle control is required for the large paint brushes compared to the smaller brushes, so only a few points are need in the spatial frequency domain to fully represent a large brush.
Now, let us take a step in the spatial frequency domain via a 2-dimensional Fourier Transform. This results in a complex 2-d array where the pixels closest to the centre of the array represent the low-frequency, or 'large-scale', structure of the original image. At the edge of the array are the high-frequency, or 'fine-scale', structure of the image. The high-frequency components represent the details of the image.
Can you write Python code for it?

fftCyclist = np.fft.fftshift(np.fft.fft2(gCyclist))
fig, axes = plt.subplots(figsize=(16,8))
plt.suptitle('The Fourier Transform of \'Dynamism of a Cyclist\'')
plt.subplot(1,2,1)
plt.imshow( 10. * np.log10(np.abs(fftCyclist))) #amplitude (decibels)
plt.subplot(1,2,2)
plt.imshow( np.angle(fftCyclist)) #phase
#fig.savefig("soccer_fft_gray.png", bbox_inches='tight', pad_inches=0)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: Amplitude, in decibels, (left) and the phase (right) of the Fourier transform of 'Dynamism of a Cyclist'
Can you write Python code for it?

fftDuck = np.fft.fftshift(np.fft.fft2(gDuck))
fig, axes = plt.subplots(figsize=(16,8))
plt.suptitle('The Fourier Transform of a Duck')
plt.subplot(1,2,1)
plt.imshow( 10. * np.log10(np.abs(fftDuck))) #amplitude (decibels)
plt.subplot(1,2,2)
plt.imshow(np.angle(fftDuck)) #phase
#fig.savefig("duck_fft_gray.png", bbox_inches='tight', pad_inches=0)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: Amplitude, in decibels, (left) and the phase (right) of the Fourier transform of a duck
As can be seen in the amplitude figures on the left the majority of the power is in the central region, i.e. the majority of the signal can be represented with large scale, or low-frequency sinusoidal waves. Try plotting the amplitudes without first rescaling them logarithmically and you should see only a few bright pixels in the middle of the figure. In the phase figures the values range from $-2\pi$ to $2\pi$ and are fairly random. There is some coherent structure that goes left to right and top to bottom passing through the centre of the figure. The main insight to gain from the phase figure is that given a complex image, of say a duck for example, leads to very complex phase structure.
An interesting question to consider is 'which is more important, amplitude or phase?' This seems like an odd question, because intuition would seem to indicate that both amplitude and phase information is needed. It turns out that phase information contains most of the structural information. Let us start by taking a look at what this means, and then get into why this is the case. Start by switching the phase information of the two images.
Can you write Python code for it?

fig = plt.figure(figsize=(8,8))
plt.title('Hybrid image: amplitude (painting), phase (duck)')
phs = np.angle(fftDuck) #phase of the duck
amp = np.abs(fftCyclist) #amplitude of the painting
fftHybrid = amp * (np.cos(phs) + 1j * np.sin(phs)) #construct an image with the phase of a duck and amplitude of the painting
hybrid = np.abs(np.fft.ifft2(np.fft.fftshift(fftHybrid))) #compute the inverse Fourier Transform
hybridPlot = plt.imshow(hybrid)
hybridPlot.set_cmap('gray')
#fig.savefig("hybrid_phs_duck_amp_cyclist.png", bbox_inches='tight', pad_inches=0)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: Dynamism of a Cyclist by Umberto Boccioni
Can you write Python code for it?

fig = plt.figure(figsize=(8,8))
plt.title('A Duck')
img1plot = plt.imshow(gDuck)
img1plot.set_cmap('gray')
#fig.savefig("duck_gray.png", bbox_inches='tight', pad_inches=0)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: reconstructed image of a duck using only the phase information by setting all the amplitudes to unity.
The duck is still visibale when we only use the phase information, but now we see something interesting. Only the fine details (i.e. the high-frequency structure) like the little feathers and the outline (edges) are visible. We will get to this later in the chapter but by setting every ampltiude pixel to 1 we have created a weighting function which will favour the fine-scale or high-frequency structure in the image over the large-scale structure such as the background water.
Can we do the same trick with using only amplitude information, i.e. reconstruct the image after setting all the phases to zero?
Can you write Python code for it?

fig, axes = plt.subplots(figsize=(16,8))
plt.title('Duck (amplitude-only)')
phs = np.zeros_like(fftDuck) #set the phase information to 0
amp = np.abs(fftDuck)
fftAmpImg0 = amp

plt.subplot(1,2,1)
plt.title('Duck (amp-only)')
ampImg0 = np.abs(np.fft.fftshift(np.fft.ifft2(fftAmpImg0)))
ampImg0Plot = plt.imshow(ampImg0)
ampImg0Plot.set_cmap('gray')

plt.subplot(1,2,2)
plt.title('Duck (amp-only (dB))')
ampImg0deci = 10.*np.log10(np.abs(np.fft.fftshift(np.fft.ifft2(fftAmpImg0))))
ampImg0deciPlot = plt.imshow(ampImg0deci)
ampImg0deciPlot.set_cmap('gray')
#fig.savefig("amp_only_duck.png", bbox_inches='tight', pad_inches=0)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Figure: reconstructed image of a duck using only the real components (left) and imaginary components (right).
There seems to be a lot going on here, but the core of this effect is that the original image is strictly real-valued. When the image is Fourier transformed the resulting signal is now 'twice' the information because it is complex (there is a real and imaginary component at each point) but no new information has been created as it is a linear transform. The Fourier transformed signal is redundant along a diagonal axis, each value above the diagonal has an equivalent (conjugated) value below the diagonal. Now if only the real-component is used to inverse Fourier transform back to the spatial domain then this results in an image which is the sum of the original image and a $180^{\circ}$ rotated image, this is an addition because conjugation does not effect the sign of the real components. The imaginary-component image is the difference image of the original and a $180^{\circ}$ rotated image because the conjugation flips the imaginary sign.
The idea with this section has been to start with an image with a significant amount of structure (or information) which we can understand and then to transform that image into a new domain where our intution is lost. And, you may still be lost as we need to create some intution about the spatial frequency domain. In the next section we will start with simple structures to see how we can build up images out of basic components.
5.1.3 The Fourier Transform of a Point Source
Instead of looking at the Fourier Transform of an image with a large amount of structure let us examine a few simple images and the effect of applying a Fourier transform. The simpliest image one can make is an empty image with a single pixel, or what it is also called a point source as the source is not resolved (in this case the sizeof a single pixel or smaller), with unity intensity in the centre pixel.
Can you write Python code for it?

def pointSourceFFT(imgSize, ypos, xpos, amp=1.):
    img = np.zeros((imgSize+1, imgSize+1)) #odd sized array such that there is a central pixel
    img[ypos, xpos] = amp #make the central pixel have an intensity of 1
    fftImg = np.fft.fft2(np.fft.fftshift(img)) #compute the Fourier transform of the image

    fig, axes = plt.subplots(figsize=(16,8))
    plt.subplot(1,2,1)
    plt.title('Image')
    plt.imshow(img, interpolation='nearest')
    plt.set_cmap('gray')
    plt.colorbar(shrink=0.5)

    plt.subplot(1,2,2)
    plt.title('Fourier Transform (phase)')
    plt.imshow(np.angle(fftImg))
    plt.set_cmap('hsv')
    plt.colorbar(shrink=0.5)

    #amplitudes are all 1.
    print 'FFT Max:', np.max(np.abs(fftImg)), 'Min:', np.min(np.abs(fftImg))

imgSize = 128
pointSourceFFT(imgSize, (imgSize/2)+1, (imgSize/2)+1)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
For simplicity convert the RGB-color images to grayscale:
Can you write Python code for it?

def rgb2gray(rgb):
    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b #standard grayscale conversion
    return gray

gCyclist = rgb2gray(cyclist)
gDuck = rgb2gray(duck)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Import section specific modules:
Can you write Python code for it?

import matplotlib.image as mpimg

from IPython.display import HTML
HTML('../style/code_toggle.html')


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Left: a point source image, a simple image of a pixel with intensity 1, offset from the centre by one pixel to the left. Right: the spatial frequency phase of the point source image.
From the phase plot we see that the phase is constant in $y$ with respect to the a fixed $x$ position, and rotates around the unit circle once in the $x$ direction.
Instead, moving the position one pixel up from the centre the fringe pattern is now top to bottom.
Can you write Python code for it?

imgSize = 128
pointSourceFFT(imgSize, imgSize/2, (imgSize/2)+1)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Left: a point source image, a simple image of a pixel with intensity 1, rotated around a 10 pixel radius circle. The blue circle is plotted for reference. Right: the spatial frequency phase of the point source image.
In the top plot where the point source is 10 pixels above the centre has a similar phase plot to that of the last figure where the source is 10 pixels below the centre, but looking carefully the phase is rotating in the opposite direction. For every point in the image there is another point which makes up a conjugated pair.
Now from these simple image examples we can see something about how sparsity works in different domains. In the image domain a single point source can be described by only a position and intensity value, i.e. we would say the image is sparse. But, transforming that image in the spatial frequency domain means that we now need to know the phase and amplitude of every pixel. That is, the information has been spread out over every pixel, in the spatial frequency domain the point source signal is now dense. This comes about because a true point-source, i.e. a delta function, can only be reconstructed by an infinite set of complex sinusoidal waves. We have been cheating a bit here by using a point source which actually has a size in these examples. That is, the point source has a size which is a pixel in length in the $x$ and $y$ direction.
As a hint about what lays in store, if we know that the signal we are interested in is mostly point source-like then we know that the signal will be spread across the spatial frequency domain and we don't need to know exactly where that signal is. We don't even need to fully measure the spatial frequency domain if we make assumptions about what the source is to reconstruct the the image of the sky we are interested in. But, this is for later.
5.1.4 The Fourier Transform of Two Point Sources
Now that we have looked at the simple case of a single point source image and how moving that source around the image changes...
Can you write Python code for it?

def multipleSourcesFFT(imgSize, pos, amp):
    img = np.zeros((imgSize+1, imgSize+1)) #odd sized array such that there is a central pixel
    for p,a in zip(pos, amp):
        img[p[0], p[1]] = a #make the central pixel have an intensity of 1

    fftImg = np.fft.fft2(np.fft.fftshift(img)) #compute the Fourier transform of the image

    fig, axes = plt.subplots(figsize=(16,8))
    plt.subplot(1,2,1)
    plt.title('Image')
    plt.imshow(img, interpolation='nearest')
    plt.set_cmap('gray')
    plt.colorbar(shrink=0.5)

    plt.subplot(1,2,2)
    plt.title('Fourier Transform (phase)')
    plt.imshow(np.angle(fftImg))
    plt.set_cmap('hsv')
    plt.colorbar(shrink=0.5)

    #amplitudes are all 1.
    print 'FFT Max:', np.max(np.abs(fftImg)), 'Min:', np.min(np.abs(fftImg))

imgSize = 128

multipleSourcesFFT(imgSize, [[64, 65], [90,65]], [1., 1.])

multipleSourcesFFT(imgSize, [[65, 64], [65,80]], [1., 1.])

multipleSourcesFFT(imgSize, [[64, 65], [90,80]], [1., 1.])


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Left: a point source image, a simple image of a the centre pixel with unity intensity. Right: the spatial frequency phase of the point source image, all the phases are zero because the source is exactly in the middle of the image.
An image with a single intensity pixel in the centre is equivalent to a 2-dimensional boxcar function with a width the size of the pixel, as the number of pixels in the image increases this approaches a delta function. The Fourier Transform of a delta function ($\S$ 2.4 ➞) is a flat field of constant value as can be seen by printing the maximum and minimum amplitudes, they are both 1. When the pixel is at the centre of the image the phase is also flat, but as the position of the pixel is changed the phase will show a fringe pattern related to the position. Moving the source position by a single pixel to the left by one pixel the amplitude remains the same but now a fringe pattern appears left to right across the phase.
Can you write Python code for it?

imgSize = 128
pointSourceFFT(imgSize, (imgSize/2)+1, (imgSize/2))


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Left: five point sources at fixed positions with the central source having an amplitude of $0.2$ and the further out sources having an amplitude of $0.3$, $0.5$, $0.1$, and $0.7$. Right: the resulting phases which are an amplitude weighted average of the phases of each individual source.
5.1.5 Building up an Image from Sampled Visibilities
Now, since we stated at the beginning of this section that the spatial domain and spatial frequency domain are Fourier pairs we can think about these point source examples as a sampling in the spatial frequency (visibility) domain and the resulting Fourier transformed signal as an image using this sampling. This may sound odd, but let us use an example to show what this means.
Starting from the Fourier transformed duck image shown at the beginning of the section, we can construct a new image by selecting a few sample points with what is called a sampling function (more on this in the next in $\S$ 5.2 ➞) in the spatial frequency domain and creating a new image.
Can you write Python code for it?

fftDuck = np.fft.fftshift(np.fft.fft2(gDuck))

def reconstructImage(vis, nsamples):
    """randomly select a few values from the spatial frequency (visibility) domain
    and reconstruct the image with those samples. To do a full reconstruction of the
    image, nsamples needs to be larger than the total number of pixels in the image
    because the np.random.randint() chooses with replacement so positions will be
    doubles counted"""
    subVis = np.zeros_like(vis)
    ypos = np.random.randint(0, vis.shape[0]-1, size=nsamples)
    xpos = np.random.randint(0, vis.shape[1]-1, size=nsamples)
    subVis[ypos, xpos] = vis[ypos, xpos] #insert a subset of visibilities
    newImg = np.abs(np.fft.ifft2(np.fft.fftshift(subVis)))

    fig, axes = plt.subplots(figsize=(16,8))
    plt.subplot(1,2,1)
    plt.title('Sampled Visibilities')
    plt.imshow(np.abs(subVis).astype(bool), interpolation='nearest')
    plt.set_cmap('gray')

    plt.subplot(1,2,2)
    plt.title('Reconstructed Image')
    plt.imshow(newImg)
    plt.set_cmap('gray')

reconstructImage(fftDuck, 1e1)

reconstructImage(fftDuck, 1e3)

reconstructImage(fftDuck, 1e5)

reconstructImage(fftDuck, 1e6)


I am working on the file "5_Imaging/5_1_spatial_frequencies.ipynb".
The first task is:
Left: two point sources at fixed positions with the central source having unity amplitude and the further out source having an amplitude of 0.1, 1, 10 for each row. Right: the resulting phases which are an amplitude weighted average of the phases of each individual source.
Once we start including more than two sources in the image we can see the resulting spatial frequency plots can become very complex.
Can you write Python code for it?


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
5.2 Sampling and Point Spread Functions
For a moment let us discuss radio interferometry and the sky. We can think of the sky as being a 'continuous' image. What I mean by 'continuous' here is that the sky is spatial continuous, as opposed to a digital image which consists of spatially discrete pixels. The Fourier transform of a continuous function is another continous function. Thus, there is some continuous function in the visibility domain which is the Fourier transform of the continuous sky. As a hint of what is to come, we will see that an interferometric array measures, i.e. samples, the continuous visibility domain function. These measurements, colloquially called visibilities, can be used to reconstruct, at least partially, an image of the sky. The process of this reconstruction will be presented throughout this chapter. With that motivation let us return to the discussion at hand.
At the end of Section 5.1 we saw how the random sampling of the spatial frequency (visibility) domain reconstructs the original image to varying degrees of quality dependent on the number of samples used. Even without complete information, much of the original image can be recovered. In this section we will expand on that idea to show how different samplings of the visibility space will result in different characteristics in the image domain.
5.2.1 Regular Sampling
A standard method to convert a continuous signal into a discrete signal to apply a regularly-spaced function called a Shah (or Dirac comb, or bed of nails) function to the continous signal. This is equivalent to the concept of digitization. The 1-D Shah function ➞ is
$$\operatorname{III}a(x) = \sum{n=-\infty}^{+\infty} \delta(x+an)$$
where $\delta(x)$ is the Dirac Delta function and $a$ is a scaling factor to set the width between samples. Thus, a continuous function $f(x)$ can be sampled with the Shah function to produce a discrete function $f_n(x)$
$$f_n(x) = \operatorname{III}a(x)\,\,f(x) = \sum{n=-\infty}^{+\infty}...
Can you write Python code for it?

xpos = np.linspace(-5, 5, num=200+1)
shah = np.where(xpos.astype(int)==xpos, 1, 0)

fig, axes = plt.subplots(figsize=(8,16))

plt.subplot(3,1,1)
plt.vlines(xpos[np.argwhere(shah > 0)], 0, 1)
plt.ylim(0, 1.1)
plt.title('Shah Function')

plt.subplot(3,1,2)
cont0 = np.sin(1.2 * xpos + 0.15)
cont1 = 0.5 * np.sin(5 * xpos + 0.5)
plt.plot(xpos, cont0, 'b-')
plt.plot(xpos, cont1, 'r-')
plt.title('Continuous Functions $f(x)$')

plt.subplot(3,1,3)
discrete0 = shah * cont0
discrete1 = shah * cont1
plt.plot(xpos[np.argwhere(np.abs(shah) > 0)], discrete0[np.argwhere(np.abs(shah) > 0)], 'b-')
plt.plot(xpos[np.argwhere(np.abs(shah) > 0)], discrete1[np.argwhere(np.abs(shah) > 0)], 'r-')
plt.title('Discrete Functions $f_n(X)$')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Arial photo of WSRT. WSRT has 14 antennae layed out East to West. Image courtesy of ASTRON
Below Westerbork in its 92m configuration is plotted, as well as observations at several declinations. As you can see such an East-West array does not have good coverage at low declinations, nor does it have good instantanious coverage.
Can you write Python code for it?

#Globals file with Westerbork 92m-configuration antenna ENU coordinates
%run westerbork_92m_constants.py
plt.figure(figsize=(8, 4))
plt.title("ENU ANTENNA COORDINATES")
plt.scatter(ENU[:,0],
         ENU[:,1],
         c=ENU[:,2],
         marker="+",
         cmap="winter")
plt.colorbar()
plt.xlabel("E (m)")
plt.ylabel("N (m)")
plt.xlim([-1000,3000])
plt.ylim([-30,30])
plt.show()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Carina Nebula image from the Hubble Telescope, the cross pattern and airy pattern from the stars in the foreground is the point spread function of the telecsope due to the optics.
Credit: NASA, ESA, and M. Livio and the Hubble 20th Anniversary Team (STScI)
In radio astronomy there are two types of PSFs, the first is the same as in optical astronomy where the shape of the optics imposes a response to a point source. This is PSF relates to the antenna beam and can be ignored for now. When discussing radio interferometry the term PSF relates to the geometry of the array configuration, that is how the array samples the visibility domain. For the moment we will simply state that the PSF of an interferometric array (in fact any telescope) is the Fourier transform of the visibility domain sampling function.
$$\text{PSF}(l,m) \rightleftharpoons S(u,v)$$
To add some interferometry specific notation we describe positions in the visibility domain in terms of $u$ and $v$, the term $uv$-space is often used as a synonym for the visibility domain. Positions in the image domain are denoted with $l$ and $m$, both of which range $-1 \le l,m \le 1$ ($\S$ 3.4 ➞). These terms are more fully described in the  Visibility Space ➞ chapter. For both the image and visibility domain $(0,0)$ is at the centre like a Cartesian coordinate system. In historical literature the term synthesized beam is often used, this is equivalent to the PSF.
The resulting obsvered signal from a measurement system is the convolution of the true signal with the PSF of the system
$$I_{obs} = \text{PSF} \circ I_{true} $$
For the moment, we will skip the mathematical complexities of why the PSF and sampling functions are Fourier pairs, and the relations between the observed and true images. Instead, we will graphically look at some examples of simple sampling functions, what type of PSF is produced and how it affects an image.
We will continue to use the same example images from the previous section, and for simplicity convert the RGB-color...
Can you write Python code for it?

#soccer = mpimg.imread('figures/WLA_moma_Umberto_Boccioni_Dynamism_of_a_Soccer_Player_1913_512.png')
cyclist = mpimg.imread('figures/Umberto_Boccioni_Dynamism_of_a_Cyclist_512.png')
duck = mpimg.imread('figures/Anas_platyrhynchos_male_female_quadrat_512.png')

def rgb2gray(rgb):
    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b #standard grayscale conversion
    return gray

gCyclist = rgb2gray(cyclist)
gDuck = rgb2gray(duck)


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Inner 5 dishes of the KAT-7 telescope array (pointed at zenith) located in the Northern Cape of South Africa. Credit: SKA-SA ⤴
The seven elements of KAT-7 can be paired into 21 possible interferometric baselines. A typical observation with the array might be performed over 6 hours, covering 100 MHz of bandwidth, pointing at a southern declination. This would result in a visibility space sampling, i.e. 'uv coverage', similar to the figure below.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_6h60s_dec-30_10MHz_10chans.ms -s KAT-7_6h60s_dec-30_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_6h60s_dec-30_10MHz_10chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 12 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -30 degrees.
As we can see in the uv coverage plots, there is a width to the tracks due to the amount of frequency bandwidth sampled during an observation. If we decrease the bandwidth to 10 MHz for the 6 hour observation we end up with a sparser sampling of the visibility space compared to the 100 MHz bandwidth observation.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_6h60s_dec-30_10MHz_1chans.ms -s KAT-7_6h60s_dec-30_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_6h60s_dec-30_10MHz_1chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 2 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -30 degrees.
We can see in the 2 hour PSF that many of the sidelobes, particularly those near the edge, have been 'washed' out and the sidelobes near the centre are slightly reduced in flux compared to the snapshot PSF. We can see that with increasing the observation to 6 hours that most of the sidelobes are washed out. Increasing the observation length further to 12 hours we can see the effect of near-completely filling in the visibility space is a strong central peak with small sidelobes.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_12h60s_dec-30_10MHz_10chans.ms -s KAT-7_12h60s_dec-30_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_12h60s_dec-30_10MHz_10chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 6 hour, 1 GHz observation (1440 MHz - 2440 MHz) of a field at declination -30 degrees.
Some structure in the PSF sidelobes still exist due to the remaining holes in the coverage, but are lower compared to all other examples we have looked at so far. Increasing the amount of bandwidth sampled during an observation will result in a better sampled visibility space, and hence a better initial image. This is an important idea which over the years has led to a push towards increasing the sampling bandwidth of new telescope arrays. There is a limit to taking advantage of increased bandwidth depending on the science case, but that is an advanced topic not covered in this work.
Before this section concludes there is one more parameter which effects a radio interferometric array PSF which should be discussed. Unlike most other telescopes interferometric arrays have a declination-dependent PSF. The short reason for the declination dependence is that the array is distributed in 3-D space, usually on the surface of the Earth, and the baselines are projected onto a 2-D surface. Depending on the pointing direction the 2-D projection will result in different baseline tracks. This is one of the great advantages of interferometric arrays, as the Earth rotates the baseline tracks are drawn out to fill in regions of the visibility space. But, it adds a complication to observations as the PSF changes with every observation.
Observing at the north and sourth celestial poles results in circular baseline tracks which results in a compact PSF primary lobe and low sidelobes. Additionally, one of the poles (depending on if the array is in the northern or southern hemisphere) is observable for the full extent of the sidereal day.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_6h60s_dec-90_10MHz_10chans.ms -s KAT-7_6h60s_dec-90_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_6h60s_dec-90_10MHz_10chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 6 hour, 10 MHz observation (1440 MHz - 1450 MHz) of a field at declination -30 degrees.
The resulting PSF of the reduced bandwidth case has a similar sidelobe layout, but are larger compared to the original 100 MHz bandwidth case.
Conversely, if we increase the bandwidth during an observation by a factor of 10 to 1 GHz the visibility space will be well sampled.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_6h60s_dec-30_10MHz_100chans.ms -s KAT-7_6h60s_dec-30_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_6h60s_dec-30_10MHz_100chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 6 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination +30 degrees.
The effect of an observation-dependent PSF will play an important role again and again as we progress through the imaging and particularly when we come to deconvolution in the next chapter.
It is worth bringing up the idea of the 'ideal' sampling function and resulting PSF for a moment. Obviously the ideal sampling function would sample all spatial frequencies out to infinite resolution. But, of course this is not realistic, instrumentational limits do not make this possible, and the resulting amount of data would be infinite. This is a bit of a strawman argument, a more realistic ideal PSF would be to fully sample all spatial frequencies out to the Nyquist frequency based on the desired resolution. Effectively this would be an Airy disc sampling function. But, the sharp edge of the Airy function results in strong PSF sidelobes. If instead, a Gaussian is used, this has a nice feature in which the Fourier transform of a Gaussian is another Gaussian. The scale of this Gaussian is with a full width at half maximum (FWHM) on the scale of the Airy disc. This idea will arise again when the restored image is introduced in the deconvolution chapter.
5.2.5 A comparison between East-West and two-dimensional arrays
Now that you have seen some properties of the KAT-7 sampling function it is important to point out the differences between one dimensional and two dimensional layouts like that of the KAT-7 array. To draw this comparison we look at the Westerbork Synthesis Radio Telescope (WSRT) and the Jansky Very Large Array (JVLA).
The WSRT has 14 antennae along the East-West direction, 4 of which can be moved along rales. The JVLA has 27 reconfigurable antennae layed out in a Y-shape.
Can you write Python code for it?


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 6 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -60 degrees.
We can see that the baseline sampling changes smoothly as a function of declination, with the tracks being circular at the north and sotuh poles with the tracks becoming elliptical as the observation moves away from the poles. This leads the particularly odd case of observations at 0 degrees declination. The tracks follow elliptical paths, at declination 0 degrees the minor semiaxis is of size 0, this results in tracks which are straight lines.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_6h60s_dec0_10MHz_10chans.ms -s KAT-7_6h60s_dec0_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_6h60s_dec0_10MHz_10chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a 6 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -90 degrees (South Celestial Pole).
The resulting PSF for the south celestial pole looks similar to the Bessel function from an Airy disc sampling function. As the observation length and bandwidth is increased the PSF will approach as Bessel function response.
Observing at an intermediate declination, such as -60 degrees, results in tracks which are slightly elliptical and over-sampling occurs in some regions of the visibility space.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_6h60s_dec-60_10MHz_10chans.ms -s KAT-7_6h60s_dec-60_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_6h60s_dec-60_10MHz_10chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: Point Spread Function from KAT-7 during a snapshot 10 minute, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -30 degrees. Many of the sidelobes have nearly equivalent flux as compared to the central primary lobe.
Increasing the observation lenght to 2 hours begins to fill in the visibility space, with some of the baseline tracks beginning to overlap and the resulting PSF sidelobes begin to be reduced relative to the primary lobe.
Can you write Python code for it?

#plotUVcoverage.py -l 1000 -f KAT-7_2h60s_dec-30_10MHz_10chans.ms -s KAT-7_2h60s_dec-30_10MHz_10chans.png
Image(filename='figures/uvcoverage/KAT-7_2h60s_dec-30_10MHz_10chans.png')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: The 27 antenna JVLA near Sorroco, New Mexico. Image courtesy of Wikipedia
Below the JVLA in its compact D-configuration is plotted at various declinations and observation lengths. As is shown the VLA has reasonably good instantanious uv-coverage compared to the WSRT.
Can you write Python code for it?

#Globals file with JVLA D-configuration antenna ENU coordinates
%run jvla_d_constants.py
plt.figure(figsize=(8, 8))
plt.title("ENU ANTENNA COORDINATES")
plt.scatter(ENU[:,0],
         ENU[:,1],
         c=ENU[:,2],
         marker="+",
         cmap="winter")
plt.colorbar()
plt.xlabel("E (m)")
plt.ylabel("N (m)")
plt.show()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: The compact D-configuration of the VLA. The colourbar indicates difference in height from the reference antenna.
Can you write Python code for it?

uv_5min = track_simulator.sim_uv(0.0, 90.0, 5/60.0, 60/3600.0, ENU, ARRAY_LATITUDE, True, False, CENTRE_CHANNEL)/CENTRE_CHANNEL/1e4

uv_6hrs_90_dec = track_simulator.sim_uv(0.0, 90.0, 6, 60/3600.0, ENU, ARRAY_LATITUDE, True, False, CENTRE_CHANNEL)/CENTRE_CHANNEL/1e4

uv_6hrs_45_dec = track_simulator.sim_uv(0.0, 45.0, 6, 60/3600.0, ENU, ARRAY_LATITUDE, True, False, CENTRE_CHANNEL)/CENTRE_CHANNEL/1e4

uv_6hrs_20_dec = track_simulator.sim_uv(0.0, 20.0, 6, 60/3600.0, ENU, ARRAY_LATITUDE, True, False, CENTRE_CHANNEL)/CENTRE_CHANNEL/1e4

uv_6hrs_0_dec = track_simulator.sim_uv(0.0, 0.0, 6, 60/3600.0, ENU, ARRAY_LATITUDE, True, False, CENTRE_CHANNEL)/CENTRE_CHANNEL/1e4


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: The effect of sampling a limited set of spatial frequency domain components to produce an observed image. Top left: ring sampling function, Top right: resulting PSF of the sampling function, Bottom left: the amplitude of the sampled visibilities, Bottom right: the Fourier transform of the sampled visibilities, the observed image.
A ring sampling function can be thought of as being made up of two sampling functions, a circular 'positive' sampling function which results in an Airy disk, and small 'negative' circular sampling function, the inner section, which results in a larger Airy disk which is subtracted from the 'positive' sampling function. The resulting PSF can be seen to have sidelobes that dim but periodically brighten in radius. The observed image has a duck-like shape but is different from the first observed image we produced when the sampling function was a complete circle out to a radius of 20 pixels. We have dropped the inner 5 pixel radius visibility samples. The sum of the observed image is effectively zero, this is because the inner components represent the large scale structure of the image. For example, the sample at $(0,0)$ in the spatial frequency domain is often called the DC component (this comes from 'direct current' in signal processing) and represents the average of the image. By not sampling that component, the remaining image visibility components, when summed should be approximately zero.
Can you write Python code for it?

np.sum(np.fft.ifft2(np.fft.fftshift(sampling * fftDuck))) #The sum of the observed image components is approximately zero


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: The effect of sampling a limited set of spatial frequency domain components to produce an observed image. Top left: ring sampling function, Top right: resulting PSF of the sampling function, shown in decibels, Bottom left: the amplitude of the sampled visibilities, Bottom right: the Fourier transform of the sampled visibilities, the observed image.
Going to an even larger radius the finest details show up in the observed image, but is fairly faint because most the power is represented in the lower spatial frequencies, thus we have also increased the total sampling area. The intuition here is that in most images the the lower spatial freqeuncy components represent most of the information in the image, and at each successively larger ring in visibility space that are more possible orientations, i.e. higher resolution, for the power to be spread over.
We can generate the resulting PSFs for a collection of different shapes.
Can you write Python code for it?

imgSize = 512

#Square
sampling = np.zeros((imgSize,imgSize), dtype='float')
sampling[int(imgSize/4.)-5:int(3.*imgSize/4.)+5, int(imgSize/4.)-5:int(3.*imgSize/4.)+5] = 1.
fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
samplingPlot = plt.imshow(sampling)
plt.title('Square Sampling Function')
samplingPlot.set_cmap('gray')

plt.subplot(1,2,2)
psfPlot = plt.imshow(10. * np.log10(np.abs( np.fft.fftshift( np.fft.fft2(sampling)))))
plt.title('PSF (dB)')

#Cross
sampling = np.zeros((imgSize,imgSize), dtype='float')
xpos, ypos = np.mgrid[0:imgSize, 0:imgSize]
idx = np.argwhere(np.abs(xpos-ypos) < 5)
sampling[idx[:,0], idx[:,1]] = 1.
sampling[idx[:,0], (imgSize-1) - idx[:,1]] = 1.
fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
samplingPlot = plt.imshow(sampling)
plt.title('Cross Sampling Function')
samplingPlot.set_cmap('gray')

plt.subplot(1,2,2)
psfPlot = plt.imshow(10. * np.log10(np.abs( np.fft.fftshift( np.fft.fft2(sampling)))))
plt.title('PSF (dB)')

#Small Circles
sampling = np.zeros((imgSize,imgSize), dtype='float')
radius = 15
smallCirc = circularSamplingMap(2*radius+1, radius, 0)
sampling[60-radius:60+radius+1, 300-radius:300+radius+1] = smallCirc
sampling[300-radius:300+radius+1, 60-radius:60+radius+1] = smallCirc
sampling[400-radius:400+radius+1, 400-radius:400+radius+1] = smallCirc
fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
samplingPlot = plt.imshow(sampling)
plt.title('Circles Sampling Function')
samplingPlot.set_cmap('gray')

plt.subplot(1,2,2)
psfPlot = plt.imshow(10. * np.log10(np.abs( np.fft.fftshift( np.fft.fft2(sampling)))))
plt.title('PSF (dB)')

#Gaussian
xpos, ypos = np.mgrid[0:imgSize,0:imgSize]
sigma = 25.
sampling = np.exp(-1. * (((xpos - (imgSize/2.))**2. + (ypos - (imgSize/2.))**2.) / (2. * sigma**2.)))
fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
samplingPlot = plt.imshow(sampling)
plt.title('Gaussian Sampling...


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: The sampling functions and PSFs of the VLA in L-Band (1 GHz). To the left the fully extended JVLA A configuration is plotted and to the right the fully retracted D configuration is plotted for a 30min observation.
5.2.7 Conclusion
To summarize this section the following figure illustrates the most important properties of sampling function coverage.
Can you write Python code for it?


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: UVW coverage of an 3h30min observation at $\delta=90^\circ$ on JVLA D and Westerbork 92m configuration
The only downside to using arrays with non-East-West baselines is that it introduces artifacts when imaging wider fields of view. We will discuss this in detail in the chapter on the widefield effect.
5.2.6 The JVLA as spatial filter
To finish off this chapter we illustrate spatial filtering discussed in section 5.2.3 with the reconfigurable JVLA. Below the sampling functions for configurations A and D (fully extended and compact configurations) are plotted. Notice that the the 36 km baselines of the A configuration sample much higher spatial frequencies when compared to D configuration. The effect of which shrinks the instrument's PSF, which in turn means that the instrument can resolve much finer structure, as we saw previously.
Can you write Python code for it?

#Globals file with JVLA A-configuration antenna ENU coordinates
%run jvla_a_constants.py
DECLINATION = 30.0
RA = 0.0
fig = plt.figure(figsize=(16, 32))
ax=fig.add_subplot(321)
ax.set_title("JVLA A ENU ANTENNA COORDINATES")
ax.plot(ENU[:,0],
           ENU[:,1],
           'r+')
ax.set_xlabel("E (m)")
ax.set_ylabel("N (m)")
vla_a_uvw = track_simulator.sim_uv(RA, DECLINATION, 0.5, 60/3600.0, ENU, ARRAY_LATITUDE)
#Globals file with JVLA D-configuration antenna ENU coordinates
%run jvla_d_constants.py
ax=fig.add_subplot(322)
ax.set_title("JVLA D ENU ANTENNA COORDINATES")
ax.plot(ENU[:,0],
        ENU[:,1],
        'r+')
ax.set_xlabel("E (m)")
ax.set_ylabel("N (m)")
vla_d_uvw = track_simulator.sim_uv(RA, DECLINATION, 0.5, 60/3600.0, ENU, ARRAY_LATITUDE)
ax=fig.add_subplot(323)
ax.set_title("JVLA A uv-coverage")
ax.plot(vla_a_uvw[:,0]/CENTRE_CHANNEL,
           vla_a_uvw[:,1]/CENTRE_CHANNEL,
           'r+',markersize=1)
ax.plot(-vla_a_uvw[:,0]/CENTRE_CHANNEL,
           -vla_a_uvw[:,1]/CENTRE_CHANNEL,
           'b+',markersize=1)
ax.set_xlabel("u ($\lambda$)")
ax.set_ylabel("v ($\lambda$)")
ax=fig.add_subplot(324)
ax.set_title("JVLA D uv-coverage")
ax.plot(vla_d_uvw[:,0]/CENTRE_CHANNEL,
           vla_d_uvw[:,1]/CENTRE_CHANNEL,
           'r+',markersize=1)
ax.plot(-vla_d_uvw[:,0]/CENTRE_CHANNEL,
           -vla_d_uvw[:,1]/CENTRE_CHANNEL,
           'b+',markersize=1)
ax.set_xlabel("u ($\lambda$)")
ax.set_ylabel("v ($\lambda$)")

# more about the next bit in the gridding section:
IM_SIZE_DEGREES = 0.1
scale = np.deg2rad(IM_SIZE_DEGREES)

tabulated_filter = AA_filter(3,63,"sinc")
dirty_sky_vla_a, psf_vla_a = grid_ifft(np.ones([vla_a_uvw.shape[0],1,1], dtype=np.complex64),
                                       vla_a_uvw[:,0:2] * scale,
                                       np.array([CENTRE_CHANNEL]),
                                       2048,2048,
                                       tabulated_filter)
dirty_sky_vla_d, psf_vla_d = grid_ifft(np.ones([vla_d_uvw.shape[0],1,1], dtype=np.complex64),
               ...


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: VLA sampling function coverage at various declinations in D-configuration. The projection of the baselines onto the UV plane at declination $\delta=90^\circ$ are concentric circles. As the observation declination decreases the projections become straight lines. Here the baselines and their conjugates are plotted.
As illustrated both the instantanious and low declination coverage of the two-dimensional JVLA is far better than the east-west WSRT. The reason for this improved coverage is that not all baselines lie on the same plane when the array has non-east-west baselines. Note that this is true even if such a two dimensional layout is on a perfectly flat plane. This is easily visualized in three-dimensional uvw space.
Can you write Python code for it?

zslider=FloatSlider(description="Z-rotation",min=0,max=360,value=160, step=1, continuous_update=False)
xslider=FloatSlider(description="X-rotation",min=-90,max=90,value=10, step=1, continuous_update=False)
def interact_plot(key):
    clear_output(wait=True)
    DECLINATION = 90
    T_OBS = 3.5
    T_INT = 1/60.0
    %run jvla_d_constants.py
    uvw_2hr_jvla_d = track_simulator.sim_uv(0.0,DECLINATION,T_OBS,T_INT,ENU,ARRAY_LATITUDE,False)/CENTRE_CHANNEL/1e4
    %run westerbork_92m_constants.py
    uvw_2hr_westerbork = track_simulator.sim_uv(0.0,DECLINATION,T_OBS,T_INT,ENU,ARRAY_LATITUDE,False)/CENTRE_CHANNEL/1e4
    fig = plt.figure(figsize=(13, 5))
    ax=fig.add_subplot(121, projection='3d')
    ax.set_title("JVLA D")
    ax.plot(uvw_2hr_jvla_d[:,0],
            uvw_2hr_jvla_d[:,1],
            uvw_2hr_jvla_d[:,2],
            'r.',markersize=1)
    ax.plot(-uvw_2hr_jvla_d[:,0],
            -uvw_2hr_jvla_d[:,1],
            -uvw_2hr_jvla_d[:,2],
            'b.',markersize=1)
    ax.set_xlabel("u ($k\lambda$)")
    ax.set_ylabel("v ($k\lambda$)")
    ax.set_zlabel("w ($k\lambda$)")
    lims = max(np.abs(np.min(uvw_2hr_jvla_d)),
               np.abs(np.max(uvw_2hr_jvla_d)))
    ax.set_xlim([-lims, lims])
    ax.set_ylim([-lims, lims])
    ax.set_zlim([-lims, lims])

    ax.view_init(elev=xslider.value, azim=zslider.value)

    ax=fig.add_subplot(122, projection='3d')
    ax.set_title("Westerbork 92m")
    ax.plot(uvw_2hr_westerbork[:,0],
            uvw_2hr_westerbork[:,1],
            uvw_2hr_westerbork[:,2],
            'r.',markersize=1)
    ax.plot(-uvw_2hr_westerbork[:,0],
            -uvw_2hr_westerbork[:,1],
            -uvw_2hr_westerbork[:,2],
            'b.',markersize=1)
    ax.view_init(elev=xslider.value, azim=zslider.value)

    ax.set_xlabel("u ($k\lambda$)")
    ax.set_ylabel("v ($k\lambda$)")
    ax.set_zlabel("w ($k\lambda$)")
    lims = max(np.abs(np.min(uvw_2hr_westerbork)),
               np.abs(np.max(uvw_2hr_westerbork)))
    ax.set_xlim([-lims, lims])
    ax.set_ylim([-lims,...


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: sampling function of a simple refractor telescope.
We can then compute the PSF of this telescope using a Fourier transform.
Can you write Python code for it?

fig, axes = plt.subplots(figsize=(8,8))
psfPlot = plt.imshow(np.abs( np.fft.fftshift( np.fft.fft2(sampling))))
plt.title('PSF')
plt.colorbar(shrink=0.8)


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: sampling functions (top row), resulting PSFs (middle row), and observed images (bottom row). A larger radius sampling function results in a smaller PSF response, which can be interpreted as higher resolution.
From the left column we can see that a small disk results in a blurred out, or low resolution, image compared to the larger sampling disk in the right column. Because the right column sampling fuction does not fully sample the visibility domain a 'ringing' can be seen in the observed image, especially around the edge of the duck, due to the PSF sidelobes.
Modern telescopes are not as simple as the refactor telescope-Airy disk relation as most are some form of reflector mirror instead of lense. Most have structures which block part of the aperture, such as struts to hold up equipment at the focus, or holes due to engineering constraints. We can expand on our simple sampling function to more complex shapes. Lets take a small step towards complexity by making a ring sampling function, which is fairly common in astronomy.
Can you write Python code for it?

fig, axes = plt.subplots(figsize=(16,16))
fig.tight_layout()

plt.subplot(2,2,1)
sampling = circularSamplingMap(512, 30, 5)
samplingPlot = plt.imshow(sampling)
samplingPlot.set_cmap('gray')
plt.title('Sampling Function')

plt.subplot(2,2,2)
psfPlot = plt.imshow(np.abs( np.fft.fftshift( np.fft.fft2(sampling))))
plt.title('PSF')

plt.subplot(2,2,3)
samplingPlot = plt.imshow(np.abs(sampling * fftDuck))
plt.title('Sampled Visibilities')

plt.subplot(2,2,4)
obsDuck = np.abs(np.fft.ifft2(np.fft.fftshift(sampling * fftDuck)))
obsPlot = plt.imshow(obsDuck)
obsPlot.set_cmap('gray')
plt.title('Observed Image')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: snapshot uv coverage from KAT-7 during a 10 minute, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -30 degrees.
With a short 'snapshot' observation the visibility plane is sparsely sampled, and due to the random distribution of KAT-7 there is no overlap in baseline sampling for such a short observation. All 21 unique baselines can be counted. You may notice there are 42 short tracks in this image, but that is from the conjugated baselines since a baseline pair, say antenna A and antenna B, can be seen as an (A,B) baseline or a (B,A) baseline. The sparsity of the visibility space sampling results in a poor PSF response as there are many sidelobes which are nearly as bright as the primary PSF lobe.
Can you write Python code for it?

#wsclean -name KAT-7_0.167h60s_dec-30_10MHz_10chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_0.167h60s_dec-30_10MHz_10chans.ms
gc = aplpy.FITSFigure('../data/fits/psfs/KAT-7_0.167h60s_dec-30_10MHz_10chans_natural-psf.fits')
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: the PSF of a simple circular sampling function, this is also called an Airy disk.
Looking at the PSF there is large (in amplitude) central peak surrounding by rings which drop off in amplitude. This is known as an Airy disk (also called a Bessel function of the first kind, or radially symetric Sinc function), and is the Fourier transform of a radially symetric top-hat function which is what our simple sampling function is. The central peak is often called the PSF primary lobe and the secodary peaks, such as the rings, are the PSF sidelobes.
Remember, the PSF is the response of the measuring system to a point source, in this example we see that a point source will appear to be an Airy disk with a primary lobe of many pixels in size. Looking back at the Hubble image we can see that some of the bright sources have an AIry disk structure around them along with the cross patterns.
We can produce the observed image of the duck image from the previous section by taking advantage of the Fourier relation in the convolution theorem that a convolution of two function in one domain is a multiplication of those functions in the Fourier domain ($\S$ 2.5 ➞).
Can you write Python code for it?

fftDuck = np.fft.fftshift(np.fft.fft2(gDuck)) #Fourier transform the duck to the spatial frequency domain
obsDuck = np.abs(np.fft.ifft2(np.fft.fftshift(sampling * fftDuck))) #multiple by the sampling function, and
                                                                    #Fourier transfrom back to the image domain
fig, axes = plt.subplots(figsize=(8,8))
obsPlot = plt.imshow(obsDuck)
obsPlot.set_cmap('gray')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: the effect of applying a Shah or Dirac Comb to a continuous signal. Top: the Shah function with a scaling factor of $a=1$, thus the Nyquist limit is $2a = 2$. Middle: a continuous Sine function with a frequency of $1.2$ (blue) and $5$ (red). Bottom: The discrete Sine functions after sampling by the Shah function. Since the blue Sine wave rate $1.2 \le 2$, the function is sufficiently sampled, but the red Sine wave is $5 > 2$ the function is aliased to a lower frequency.
The effect of applying a Shah function to sample a continuous signal is that the now discretely sampled signal is bandlimited to a Nyquist rate of $2a$. Periodic signals with rates larger than $2a$ will be aliased to lower frequencies. See $\S$ 2.9 ➞ for a detailed discussion on sampling theory.
This formulation is easily extended to higher dimensions, for example the 2-D Shah function is
$$\operatorname{III}{a,b}(x, y) = \sum{n=-\infty}^{+\infty} \sum_{m=-\infty}^{+\infty} \delta(x+an, y+bm)$$
We can use this function to sample a continuous sky image into a digital image, such as is done with a telescope and CCD camera. But, in radio astronomy that is not possible due to a number of issues with scale, technology, and sensitivity. Instead, we use an irregular sampling method.
5.2.2 A Generalized Sampling Function
Regular sampling functions are a special case of the general sampling function which happen to be equally spaced in sampling postion. But, as we will see, regular sampling is a rare case in interferometry. Instead, the sampling functions will be a function of the interferometric array configuration, observation bandwidth, sky pointing direction, and data flagging among other issues. The general 2-D sampling function form an arbitrary set of $Q$ pairs $(x_i, y_i), i \le Q$
$$S(x, y) = \sum_{i=1}^{Q} \delta(x+x_i, y+y_i)$$
This equation can be further generalized to a weighted sampling function
$$S(x, y) = \sum_{i=1}^{Q} w_i \,\, \delta(x+x_i, y+y_i)$$
Which can be used to weight individual samples, for example the...
Can you write Python code for it?


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: the obsevred image of the duck using the sampling function presented above.
The observed image is quite blurry as the large PSF primary lobe affectively acts as a low-pass filter to remove the fine details. By increasing the radius of the sampling function, i.e. using a larger telescope, we see that the PSF primary lobe gets smaller, and thus we have a higher resolution telescope as expected.
Can you write Python code for it?

fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
sampling0 = circularSamplingMap(512, 10, 0)
samplingPlot = plt.imshow(sampling0)
samplingPlot.set_cmap('gray')
plt.title('Sampling Function (r=%i)'%10)

plt.subplot(1,2,2)
sampling1 = circularSamplingMap(512, 100, 0)
samplingPlot = plt.imshow(sampling1)
samplingPlot.set_cmap('gray')
plt.title('Sampling Function (r=%i)'%100)

fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
psfPlot = plt.imshow(np.abs( np.fft.fftshift( np.fft.fft2(sampling0))))
plt.title('PSF')

plt.subplot(1,2,2)
psfPlot = plt.imshow(np.abs( np.fft.fftshift( np.fft.fft2(sampling1))))
plt.title('PSF')

fig, axes = plt.subplots(figsize=(16,8))
fig.tight_layout()

plt.subplot(1,2,1)
obsDuck = np.abs(np.fft.ifft2(np.fft.fftshift(sampling0 * fftDuck)))
obsPlot = plt.imshow(obsDuck)
obsPlot.set_cmap('gray')
plt.title('Observed Image')

plt.subplot(1,2,2)
obsDuck = np.abs(np.fft.ifft2(np.fft.fftshift(sampling1 * fftDuck)))
obsPlot = plt.imshow(obsDuck)
obsPlot.set_cmap('gray')
plt.title('Observed Image')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: uv coverage from KAT-7 during a 12 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -30 degrees.
Can you write Python code for it?

#wsclean -name KAT-7_12h60s_dec-30_10MHz_10chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_12h60s_dec-30_10MHz_10chans.ms
gc = aplpy.FITSFigure('../data/fits/psfs/KAT-7_12h60s_dec-30_10MHz_10chans_natural-psf.fits')
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: uv coverage from KAT-7 during a 6 hour, 1 GHz observation (1440 MHz - 2440 MHz) of a field at declination -30 degrees.
The increase in bandwidth results in much of the visibility space being well sampled, and many of the regions oversampled multiple times. But, there do remain holes in the sampling, especially near the centre. There is a physical limit to how close the antennas can be, a KAT-7 dish is 12 metres in diameter, so no baseline can be shorter than 12 metres.
Can you write Python code for it?

#wsclean -name KAT-7_6h60s_dec-30_10MHz_100chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_6h60s_dec-30_10MHz_100chans.ms
gc = aplpy.FITSFigure('../data/fits/psfs/KAT-7_6h60s_dec-30_10MHz_100chans_natural-psf.fits')
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: uv coverage from KAT-7 during a 6 hour, 10 MHz observation (1440 MHz - 1450 MHz) of a field at declination -30 degrees.
Can you write Python code for it?

#wsclean -name KAT-7_6h60s_dec-30_10MHz_1chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_6h60s_dec-30_10MHz_1chans.ms
# Note: there is a odd issue here with single channel measurement set images, wsclean, and aplpy
# using astropy FITS interface to workaround the issue
fh = fits.open('../data/fits/psfs/KAT-7_6h60s_dec-30_10MHz_1chans_natural-psf.fits')
img = fh[0].data
gc = aplpy.FITSFigure(img[0,0])
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: uv coverage from KAT-7 during a 6 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination +30 degrees.
Comparing this coverage to that of the coverage for a field at declination -30 degrees one should notice that there is a similarity between the two. In fact, both sample different sections of the same complete uv ellipses, as they are effectively conjugates of each other, but the total possible coverage for the northern hemisphere field is limited compared to the sothern hemisphere due horizon limits.
Hopefully at this point we are starting to build up intuition on what the resulting PSF response will be based on the uv coverage. For example, in our declination +30 degrees case the coverage is sampled further out in the top-right and bottom-left quadrants compared to the top-left and bottom-right quandrants. This should indicate that the resulting PSF primary lobe will be elliptical, as we are better sampling the diagonal modes in one direction versus the other. IN the PSF response below we can see that the primary lobe is indeed elliptical.
Can you write Python code for it?

#wsclean -name KAT-7_6h60s_dec30_10MHz_10chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_6h60s_dec30_10MHz_10chans.ms
gc = aplpy.FITSFigure('../data/fits/psfs/KAT-7_6h60s_dec30_10MHz_10chans_natural-psf.fits')
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: uv coverage from KAT-7 during a 6 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -30 degrees.
The various arcs in the sampling figure are colloquially called visibility tracks and are from a single baseline. Each track has a width, which has been coloured, to represent the bandwidth of the observation. The lowest frequency of the observation is represented by the red side of the track, and the highest frequency is the blue side. As can be seen, some tracks overlap in regions of the visibility space. These regions are being sampled multiple times during and observation, the issue of how to deal with this multi-sampling will come up latter in the chapter when we discuss weighting schemes ➞. There are also empty regions of the sampling space, and one might talk about the 'holes in the uv coverage'. Notably is the central hole in the coverage, if the auto-correlations were included in an observation, this is the location in visibility space where these samples would be placed. As we created a PSF response from the Hubble sampling function, we can also create a PSF response for this KAT-7 uv coverage.
Can you write Python code for it?

#wsclean -name KAT-7_6h60s_dec-30_10MHz_10chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_6h60s_dec-30_10MHz_10chans.ms
gc = aplpy.FITSFigure('../data/fits/psfs/KAT-7_6h60s_dec-30_10MHz_10chans_natural-psf.fits')
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Figure: uv coverage from KAT-7 during a 6 hour, 100 MHz observation (1440 MHz - 1540 MHz) of a field at declination -60 degrees.
The PSF from this sampling has arced sidelobes which are brighter than the sidelobe rings at -90 declination, but not as strong as the prominent sidelobes at -30 degrees.
Can you write Python code for it?

#wsclean -name KAT-7_6h60s_dec-60_10MHz_10chans -size 512 512 -scale 0.004 -nosmallinversion -weight natural
# -makepsf KAT-7_6h60s_dec-60_10MHz_10chans.ms
gc = aplpy.FITSFigure('../data/fits/psfs/KAT-7_6h60s_dec-60_10MHz_10chans_natural-psf.fits')
gc.show_colorscale(cmap='viridis')
gc.add_colorbar()


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Import section specific modules:
Can you write Python code for it?

import matplotlib.image as mpimg
from IPython.display import Image, display, clear_output
from astropy.io import fits
import aplpy
import track_simulator
from mpl_toolkits.mplot3d import Axes3D
import track_simulator
from ipywidgets import FloatSlider
from scipy.interpolate import griddata
from convolutional_gridder import grid_ifft
from AA_filter import AA_filter
#Disable astropy/aplpy logging
import logging
logger0 = logging.getLogger('astropy')
logger0.setLevel(logging.CRITICAL)
logger1 = logging.getLogger('aplpy')
logger1.setLevel(logging.CRITICAL)

from IPython.display import HTML, Javascript
HTML('../style/code_toggle.html')


I am working on the file "5_Imaging/5_2_sampling_functions_and_psfs.ipynb".
The first task is:
Left: the approximate sampling pattern of the Hubble Telescope. Right: The resulting PSF from this sampling pattern which is apparent in many Hubble images.
Based on this approximate sampling pattern for the Hubble telescope we can now see where the PSF shape we see in the image we started the section with has come from. The cross structure comes from the cross in the sampling function, the compact primary lobe is due to the large ring sampling, and the extended, low-level Airy disc is from the small circle patterns.
From these various examples it should be clear that the choice of sampling greatly affects the resulting image. Ideally the sampling function completely covers the visibility domain, but that is never possible. First, the domain is infinite in extent, that is, by building a large telescope the resolution of the resulting image increases, this can go on ad infinitum. There is some practical engineering limit or scientific case to set the maximum resolution of an instrument. The second issue, which we have been hiding away in our examples is that we have assumed the sampling function is on a grid already. The array of pixels which make up the images are regularly spaced and all of the same size (i.e. the sample point covers the area of a pixel). In reality of course, telescope sampling functions are not made up of a grid of pixels, they are continuous. The sampling function must be transformed to a be placed on a regular array by some approximate interpolation method called gridding (along with an inverse method degridding). This will be covered in more detail in the next section ($\S$ 5.3 ➞). As we will see in interferometry, an assembly of baseline tracks will lead to complex sampling functions dependent on a number of factors such as array layout, oberserving frequency, sky direction, and integration time.
5.2.4 KAT-7 Visibility Sampling and Image PSFs
For the remainder of this chapter and the next we will use simulated observations of the KAT-7 array for exploring various aspects of...
Can you write Python code for it?