# Notebook from atharvanaphade/deeplearning.ai-courses
Path: Deep Learning/Course 5 - Sequence Models/Dinosaurus_Island_Character_level_language_model_final_v3a.ipynb

# Character level language model - Dinosaurus Island

Welcome to Dinosaurus Island! 65 million years ago, dinosaurs existed, and in this assignment they are back. You are in charge of a special task. Leading biology researchers are creating new breeds of dinosaurs and bringing them to life on earth, and your job is to give names to these dinosaurs. If a dinosaur does not like its name, it might go berserk, so choose wisely!

Luckily you have learned some deep learning and you will use it to save the day. Your assistant has collected a list of all the dinosaur names they could find, and compiled them into this [dataset](dinos.txt). (Feel free to take a look by clicking the previous link.) To create new dinosaur names, you will build a character level language model to generate new names. Your algorithm will learn the different name patterns, and randomly generate new names. Hopefully this algorithm will keep you and your team safe from the dinosaurs' wrath!

By completing this assignment you will learn:

- How to store text data for processing using an RNN
- How to synthesize data, by sampling predictions at each time step and passing it to the next RNN-cell unit
- How to build a character-level text generation recurrent neural network
- Why clipping the gradients is important

We will begin by loading in some functions that we have provided for you in `rnn_utils`. Specifically, you have access to functions such as `rnn_forward` and `rnn_backward` which are equivalent to those you've implemented in the previous assignment. _____no_output_____## Updates

#### If you were working on the notebook before this update...
* The current notebook is version "3a".
* You can find your original work saved in the notebook with the previous version name ("v3")
* To view the file directory, go to the menu "File->Open", and this will open a new tab that shows the file directory.

#### List of updates
* Sort and print `chars` list of characters.
* Import and use pretty print
* `clip`:
    - Additional details on why we need to use the "out" parameter.
    - Modified for loop to have students fill in the correct items to loop through.
    - Added a test case to check for hard-coding error.
* `sample`
    - additional hints added to steps 1,2,3,4.
    - "Using 2D arrays instead of 1D arrays".
    - explanation of numpy.ravel().
    - fixed expected output.
    - clarified comments in the code.
* "training the model"
    - Replaced the sample code with explanations for how to set the index, X and Y (for a better learning experience).
* Spelling, grammar and wording corrections._____no_output_____

import numpy as np
from utils import *
import random
import pprint_____no_output_____

## 1 - Problem Statement

### 1.1 - Dataset and Preprocessing

Run the following cell to read the dataset of dinosaur names, create a list of unique characters (such as a-z), and compute the dataset and vocabulary size. _____no_output_____

data = open('dinos.txt', 'r').read()
data= data.lower()
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
print('There are %d total characters and %d unique characters in your data.' % (data_size, vocab_size))There are 19909 total characters and 27 unique characters in your data.

* The characters are a-z (26 characters) plus the "\n" (or newline character).
* In this assignment, the newline character "\n" plays a role similar to the `<EOS>` (or "End of sentence") token we had discussed in lecture.
    - Here, "\n" indicates the end of the dinosaur name rather than the end of a sentence.
* `char_to_ix`: In the cell below, we create a python dictionary (i.e., a hash table) to map each character to an index from 0-26.
* `ix_to_char`: We also create a second python dictionary that maps each index back to the corresponding character.
    -  This will help you figure out what index corresponds to what character in the probability distribution output of the softmax layer. _____no_output_____

chars = sorted(chars)
print(chars)['\n', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
char_to_ix = { ch:i for i,ch in enumerate(chars) }
ix_to_char = { i:ch for i,ch in enumerate(chars) }
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(ix_to_char){   0: '\n',
    1: 'a',
    2: 'b',
    3: 'c',
    4: 'd',
    5: 'e',
    6: 'f',
    7: 'g',
    8: 'h',
    9: 'i',
    10: 'j',
    11: 'k',
    12: 'l',
    13: 'm',
    14: 'n',
    15: 'o',
    16: 'p',
    17: 'q',
    18: 'r',
    19: 's',
    20: 't',
    21: 'u',
    22: 'v',
    23: 'w',
    24: 'x',
    25: 'y',
    26: 'z'}

### 1.2 - Overview of the model

Your model will have the following structure:

- Initialize parameters
- Run the optimization loop
    - Forward propagation to compute the loss function
    - Backward propagation to compute the gradients with respect to the loss function
    - Clip the gradients to avoid exploding gradients
    - Using the gradients, update your parameters with the gradient descent update rule.
- Return the learned parameters

 **Figure 1**: Recurrent Neural Network, similar to what you had built in the previous notebook "Building a Recurrent Neural Network - Step by Step".

* At each time-step, the RNN tries to predict what is the next character given the previous characters.
* The dataset $\mathbf{X} = (x^{\langle 1 \rangle}, x^{\langle 2 \rangle}, ..., x^{\langle T_x \rangle})$ is a list of characters in the training set.
* $\mathbf{Y} = (y^{\langle 1 \rangle}, y^{\langle 2 \rangle}, ..., y^{\langle T_x \rangle})$ is the same list of characters but shifted one character forward.
* At every time-step $t$, $y^{\langle t \rangle} = x^{\langle t+1 \rangle}$.  The prediction at time $t$ is the same as the input at time $t + 1$._____no_output_____## 2 - Building blocks of the model

In this part, you will build two important blocks of the overall model:
- Gradient clipping: to avoid exploding gradients
- Sampling: a technique used to generate characters

You will then apply these two functions to build the model._____no_output_____### 2.1 - Clipping the gradients in the optimization loop

In this section you will implement the `clip` function that you will call inside of your optimization loop.

#### Exploding gradients
* When gradients are very large, they're called "exploding gradients."
* Exploding gradients make the training process more difficult, because the updates may be so large that they "overshoot" the optimal values during back propagation.

Recall that your overall loop structure usually consists of:
* forward pass,
* cost computation,
* backward pass,
* parameter update.

Before updating the parameters, you will perform gradient clipping to make sure that your gradients are not "exploding."

#### gradient clipping
In the exercise below, you will implement a function `clip` that takes in a dictionary of gradients and returns a clipped version of gradients if needed.
* There are different ways to clip gradients.
* We will use a simple element-wise clipping procedure, in which every element of the gradient vector is clipped to lie between some range [-N, N].
* For example, if the N=10
    - The range is [-10, 10]
    - If any component of the gradient vector is greater than 10, it is set to 10.
    - If any component of the gradient vector is less than -10, it is set to -10.
    - If any components are between -10 and 10, they keep their original values.

 **Figure 2**: Visualization of gradient descent with and without gradient clipping, in a case where the network is running into "exploding gradient" problems.

**Exercise**:
Implement the function below to return the clipped gradients of your dictionary `gradients`.
* Your function takes in a maximum threshold and returns the clipped versions of the gradients.
* You can check out [numpy.clip](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.clip.html).
    - You will need to use the argument "`out = ...`".
    - Using the "`out`" parameter allows you to update a variable "in-place".
    - If you don't use "`out`" argument, the clipped variable is stored in the variable "gradient" but does not update the gradient variables `dWax`, `dWaa`, `dWya`, `db`, `dby`._____no_output_____

### GRADED FUNCTION: clip

def clip(gradients, maxValue):
    '''
    Clips the gradients' values between minimum and maximum.

    Arguments:
    gradients -- a dictionary containing the gradients "dWaa", "dWax", "dWya", "db", "dby"
    maxValue -- everything above this number is set to this number, and everything less than -maxValue is set to -maxValue

    Returns:
    gradients -- a dictionary with the clipped gradients.
    '''

    dWaa, dWax, dWya, db, dby = gradients['dWaa'], gradients['dWax'], gradients['dWya'], gradients['db'], gradients['dby']

    ### START CODE HERE ###
    # clip to mitigate exploding gradients, loop over [dWax, dWaa, dWya, db, dby]. (≈2 lines)
    for gradient in [dWax, dWaa, dWya, db, dby]:
        np.clip(gradient, -maxValue, maxValue, out=gradient)
    ### END CODE HERE ###

    gradients = {"dWaa": dWaa, "dWax": dWax, "dWya": dWya, "db": db, "dby": dby}

    return gradients_____no_output_____# Test with a maxvalue of 10
maxValue = 10
np.random.seed(3)
dWax = np.random.randn(5,3)*10
dWaa = np.random.randn(5,5)*10
dWya = np.random.randn(2,5)*10
db = np.random.randn(5,1)*10
dby = np.random.randn(2,1)*10
gradients = {"dWax": dWax, "dWaa": dWaa, "dWya": dWya, "db": db, "dby": dby}
gradients = clip(gradients, maxValue)
print("gradients[\"dWaa\"][1][2] =", gradients["dWaa"][1][2])
print("gradients[\"dWax\"][3][1] =", gradients["dWax"][3][1])
print("gradients[\"dWya\"][1][2] =", gradients["dWya"][1][2])
print("gradients[\"db\"][4] =", gradients["db"][4])
print("gradients[\"dby\"][1] =", gradients["dby"][1])gradients["dWaa"][1][2] = 10.0
gradients["dWax"][3][1] = -10.0
gradients["dWya"][1][2] = 0.29713815361
gradients["db"][4] = [ 10.]
gradients["dby"][1] = [ 8.45833407]

** Expected output:**

```Python
gradients["dWaa"][1][2] = 10.0
gradients["dWax"][3][1] = -10.0
gradients["dWya"][1][2] = 0.29713815361
gradients["db"][4] = [ 10.]
gradients["dby"][1] = [ 8.45833407]
```_____no_output_____

# Test with a maxValue of 5
maxValue = 5
np.random.seed(3)
dWax = np.random.randn(5,3)*10
dWaa = np.random.randn(5,5)*10
dWya = np.random.randn(2,5)*10
db = np.random.randn(5,1)*10
dby = np.random.randn(2,1)*10
gradients = {"dWax": dWax, "dWaa": dWaa, "dWya": dWya, "db": db, "dby": dby}
gradients = clip(gradients, maxValue)
print("gradients[\"dWaa\"][1][2] =", gradients["dWaa"][1][2])
print("gradients[\"dWax\"][3][1] =", gradients["dWax"][3][1])
print("gradients[\"dWya\"][1][2] =", gradients["dWya"][1][2])
print("gradients[\"db\"][4] =", gradients["db"][4])
print("gradients[\"dby\"][1] =", gradients["dby"][1])gradients["dWaa"][1][2] = 5.0
gradients["dWax"][3][1] = -5.0
gradients["dWya"][1][2] = 0.29713815361
gradients["db"][4] = [ 5.]
gradients["dby"][1] = [ 5.]

** Expected Output: **
```Python
gradients["dWaa"][1][2] = 5.0
gradients["dWax"][3][1] = -5.0
gradients["dWya"][1][2] = 0.29713815361
gradients["db"][4] = [ 5.]
gradients["dby"][1] = [ 5.]
```_____no_output_____### 2.2 - Sampling

Now assume that your model is trained. You would like to generate new text (characters). The process of generation is explained in the picture below:

 **Figure 3**: In this picture, we assume the model is already trained. We pass in $x^{\langle 1\rangle} = \vec{0}$ at the first time step, and have the network sample one character at a time.
_____no_output_____**Exercise**: Implement the `sample` function below to sample characters. You need to carry out 4 steps:

- **Step 1**: Input the "dummy" vector of zeros $x^{\langle 1 \rangle} = \vec{0}$.
    - This is the default input before we've generated any characters.
    We also set $a^{\langle 0 \rangle} = \vec{0}$_____no_output_____- **Step 2**: Run one step of forward propagation to get $a^{\langle 1 \rangle}$ and $\hat{y}^{\langle 1 \rangle}$. Here are the equations:

hidden state:
$$ a^{\langle t+1 \rangle} = \tanh(W_{ax}  x^{\langle t+1 \rangle } + W_{aa} a^{\langle t \rangle } + b)\tag{1}$$

activation:
$$ z^{\langle t + 1 \rangle } = W_{ya}  a^{\langle t + 1 \rangle } + b_y \tag{2}$$

prediction:
$$ \hat{y}^{\langle t+1 \rangle } = softmax(z^{\langle t + 1 \rangle })\tag{3}$$

- Details about $\hat{y}^{\langle t+1 \rangle }$:
   - Note that $\hat{y}^{\langle t+1 \rangle }$ is a (softmax) probability vector (its entries are between 0 and 1 and sum to 1).
   - $\hat{y}^{\langle t+1 \rangle}_i$ represents the probability that the character indexed by "i" is the next character.
   - We have provided a `softmax()` function that you can use._____no_output_____#### Additional Hints

- $x^{\langle 1 \rangle}$ is `x` in the code. When creating the one-hot vector, make a numpy array of zeros, with the number of rows equal to the number of unique characters, and the number of columns equal to one.  It's a 2D and not a 1D array.
- $a^{\langle 0 \rangle}$ is `a_prev` in the code.  It is a numpy array of zeros, where the number of rows is $n_{a}$, and number of columns is 1.  It is a 2D array as well.  $n_{a}$ is retrieved by getting the number of columns in $W_{aa}$ (the numbers need to match in order for the matrix multiplication $W_{aa}a^{\langle t \rangle}$ to work.
- [numpy.dot](https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html)
- [numpy.tanh](https://docs.scipy.org/doc/numpy/reference/generated/numpy.tanh.html)_____no_output_____#### Using 2D arrays instead of 1D arrays
* You may be wondering why we emphasize that $x^{\langle 1 \rangle}$ and $a^{\langle 0 \rangle}$ are 2D arrays and not 1D vectors.
* For matrix multiplication in numpy, if we multiply a 2D matrix with a 1D vector, we end up with with a 1D array.
* This becomes a problem when we add two arrays where we expected them to have the same shape.
* When two arrays with  a different number of dimensions are added together, Python "broadcasts" one across the other.
* Here is some sample code that shows the difference between using a 1D and 2D array._____no_output_____

import numpy as np_____no_output_____matrix1 = np.array([[1,1],[2,2],[3,3]]) # (3,2)
matrix2 = np.array([[0],[0],[0]]) # (3,1)
vector1D = np.array([1,1]) # (2,)
vector2D = np.array([[1],[1]]) # (2,1)
print("matrix1 \n", matrix1,"\n")
print("matrix2 \n", matrix2,"\n")
print("vector1D \n", vector1D,"\n")
print("vector2D \n", vector2D)matrix1
 [[1 1]
 [2 2]
 [3 3]]

matrix2
 [[0]
 [0]
 [0]]

vector1D
 [1 1]

vector2D
 [[1]
 [1]]
print("Multiply 2D and 1D arrays: result is a 1D array\n",
      np.dot(matrix1,vector1D))
print("Multiply 2D and 2D arrays: result is a 2D array\n",
      np.dot(matrix1,vector2D))Multiply 2D and 1D arrays: result is a 1D array
 [2 4 6]
Multiply 2D and 2D arrays: result is a 2D array
 [[2]
 [4]
 [6]]
print("Adding (3 x 1) vector to a (3 x 1) vector is a (3 x 1) vector\n",
      "This is what we want here!\n",
      np.dot(matrix1,vector2D) + matrix2)Adding (3 x 1) vector to a (3 x 1) vector is a (3 x 1) vector
 This is what we want here!
 [[2]
 [4]
 [6]]
print("Adding a (3,) vector to a (3 x 1) vector\n",
      "broadcasts the 1D array across the second dimension\n",
      "Not what we want here!\n",
      np.dot(matrix1,vector1D) + matrix2
     )Adding a (3,) vector to a (3 x 1) vector
 broadcasts the 1D array across the second dimension
 Not what we want here!
 [[2 4 6]
 [2 4 6]
 [2 4 6]]

- **Step 3**: Sampling:
    - Now that we have $y^{\langle t+1 \rangle}$, we want to select the next letter in the dinosaur name. If we select the most probable, the model will always generate the same result given a starting letter.
        - To make the results more interesting, we will use np.random.choice to select a next letter that is likely, but not always the same.
    - Sampling is the selection of a value from a group of values, where each value has a probability of being picked.
    - Sampling allows us to generate random sequences of values.
    - Pick the next character's index according to the probability distribution specified by $\hat{y}^{\langle t+1 \rangle }$.
    - This means that if $\hat{y}^{\langle t+1 \rangle }_i = 0.16$, you will pick the index "i" with 16% probability.
    - You can use [np.random.choice](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.choice.html).

    Example of how to use `np.random.choice()`:
    ```python
    np.random.seed(0)
    probs = np.array([0.1, 0.0, 0.7, 0.2])
    idx = np.random.choice([0, 1, 2, 3] p = probs)
    ```
    - This means that you will pick the index (`idx`) according to the distribution:

    $P(index = 0) = 0.1, P(index = 1) = 0.0, P(index = 2) = 0.7, P(index = 3) = 0.2$.

    - Note that the value that's set to `p` should be set to a 1D vector.
    - Also notice that $\hat{y}^{\langle t+1 \rangle}$, which is `y` in the code, is a 2D array._____no_output_____##### Additional Hints
- [range](https://docs.python.org/3/library/functions.html#func-range)
- [numpy.ravel](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html) takes a multi-dimensional array and returns its contents inside of a 1D vector.
```Python
arr = np.array([[1,2],[3,4]])
print("arr")
print(arr)
print("arr.ravel()")
print(arr.ravel())
```
Output:
```Python
arr
[[1 2]
 [3 4]]
arr.ravel()
[1 2 3 4]
```

- Note that `append` is an "in-place" operation.  In other words, don't do this:
```Python
fun_hobbies = fun_hobbies.append('learning')  ## Doesn't give you what you want
```_____no_output_____- **Step 4**: Update to $x^{\langle t \rangle }$
    - The last step to implement in `sample()` is to update the variable `x`, which currently stores $x^{\langle t \rangle }$, with the value of $x^{\langle t + 1 \rangle }$.
    - You will represent $x^{\langle t + 1 \rangle }$ by creating a one-hot vector corresponding to the character that you have chosen as your prediction.
    - You will then forward propagate $x^{\langle t + 1 \rangle }$ in Step 1 and keep repeating the process until you get a "\n" character, indicating that you have reached the end of the dinosaur name. _____no_output_____##### Additional Hints
- In order to reset `x` before setting it to the new one-hot vector, you'll want to set all the values to zero.
    - You can either create a new numpy array: [numpy.zeros](https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html)
    - Or fill all values with a single number: [numpy.ndarray.fill](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.fill.html)_____no_output_____

# GRADED FUNCTION: sample

def sample(parameters, char_to_ix, seed):
    """
    Sample a sequence of characters according to a sequence of probability distributions output of the RNN

    Arguments:
    parameters -- python dictionary containing the parameters Waa, Wax, Wya, by, and b.
    char_to_ix -- python dictionary mapping each character to an index.
    seed -- used for grading purposes. Do not worry about it.

    Returns:
    indices -- a list of length n containing the indices of the sampled characters.
    """

    # Retrieve parameters and relevant shapes from "parameters" dictionary
    Waa, Wax, Wya, by, b = parameters['Waa'], parameters['Wax'], parameters['Wya'], parameters['by'], parameters['b']
    vocab_size = by.shape[0]
    n_a = Waa.shape[1]

    ### START CODE HERE ###
    # Step 1: Create the a zero vector x that can be used as the one-hot vector
    # representing the first character (initializing the sequence generation). (≈1 line)
    x = np.zeros(shape=(vocab_size, 1))
    # Step 1': Initialize a_prev as zeros (≈1 line)
    a_prev = np.zeros(shape=(n_a, 1))

    # Create an empty list of indices, this is the list which will contain the list of indices of the characters to generate (≈1 line)
    indices = []

    # idx is the index of the one-hot vector x that is set to 1
    # All other positions in x are zero.
    # We will initialize idx to -1
    idx = -1

    # Loop over time-steps t. At each time-step:
    # sample a character from a probability distribution
    # and append its index (`idx`) to the list "indices".
    # We'll stop if we reach 50 characters
    # (which should be very unlikely with a well trained model).
    # Setting the maximum number of characters helps with debugging and prevents infinite loops.
    counter = 0
    newline_character = char_to_ix['\n']

    while (idx != newline_character and counter != 50):

        # Step 2: Forward propagate x using the equations (1), (2) and (3)
        a = np.tanh(np.dot(Wax, x) + np.dot(Waa, a_prev) + b)
        z = np.dot(Wya, a) + by
        y = softmax(z)

        # for grading purposes
        np.random.seed(counter+seed)

        # Step 3: Sample the index of a character within the vocabulary from the probability distribution y
        # (see additional hints above)
        idx = np.random.choice(list(range(vocab_size)), p=y.ravel())

        # Append the index to "indices"
        indices.append(idx)

        # Step 4: Overwrite the input x with one that corresponds to the sampled index `idx`.
        # (see additional hints above)
        x = np.zeros((vocab_size, 1))
        x[idx] = 1

        # Update "a_prev" to be "a"
        a_prev = a

        # for grading purposes
        seed += 1
        counter +=1

    ### END CODE HERE ###

    if (counter == 50):
        indices.append(char_to_ix['\n'])

    return indices_____no_output_____np.random.seed(2)
_, n_a = 20, 100
Wax, Waa, Wya = np.random.randn(n_a, vocab_size), np.random.randn(n_a, n_a), np.random.randn(vocab_size, n_a)
b, by = np.random.randn(n_a, 1), np.random.randn(vocab_size, 1)
parameters = {"Wax": Wax, "Waa": Waa, "Wya": Wya, "b": b, "by": by}

indices = sample(parameters, char_to_ix, 0)
print("Sampling:")
print("list of sampled indices:\n", indices)
print("list of sampled characters:\n", [ix_to_char[i] for i in indices])Sampling:
list of sampled indices:
 [12, 17, 24, 14, 13, 9, 10, 22, 24, 6, 13, 11, 12, 6, 21, 15, 21, 14, 3, 2, 1, 21, 18, 24, 7, 25, 6, 25, 18, 10, 16, 2, 3, 8, 15, 12, 11, 7, 1, 12, 10, 2, 7, 7, 11, 17, 24, 12, 13, 24, 0]
list of sampled characters:
 ['l', 'q', 'x', 'n', 'm', 'i', 'j', 'v', 'x', 'f', 'm', 'k', 'l', 'f', 'u', 'o', 'u', 'n', 'c', 'b', 'a', 'u', 'r', 'x', 'g', 'y', 'f', 'y', 'r', 'j', 'p', 'b', 'c', 'h', 'o', 'l', 'k', 'g', 'a', 'l', 'j', 'b', 'g', 'g', 'k', 'q', 'x', 'l', 'm', 'x', '\n']

** Expected output:**

```Python
Sampling:
list of sampled indices:
 [12, 17, 24, 14, 13, 9, 10, 22, 24, 6, 13, 11, 12, 6, 21, 15, 21, 14, 3, 2, 1, 21, 18, 24, 7, 25, 6, 25, 18, 10, 16, 2, 3, 8, 15, 12, 11, 7, 1, 12, 10, 2, 7, 7, 11, 17, 24, 12, 13, 24, 0]
list of sampled characters:
 ['l', 'q', 'x', 'n', 'm', 'i', 'j', 'v', 'x', 'f', 'm', 'k', 'l', 'f', 'u', 'o', 'u', 'n', 'c', 'b', 'a', 'u', 'r', 'x', 'g', 'y', 'f', 'y', 'r', 'j', 'p', 'b', 'c', 'h', 'o', 'l', 'k', 'g', 'a', 'l', 'j', 'b', 'g', 'g', 'k', 'q', 'x', 'l', 'm', 'x', '\n']
```

* Please note that over time, if there are updates to the back-end of the Coursera platform (that may update the version of numpy), the actual list of sampled indices and sampled characters may change.
* If you follow the instructions given above and get an output without errors, it's possible the routine is correct even if your output doesn't match the expected output.  Submit your assignment to the grader to verify its correctness._____no_output_____## 3 - Building the language model

It is time to build the character-level language model for text generation.

### 3.1 - Gradient descent

* In this section you will implement a function performing one step of stochastic gradient descent (with clipped gradients).
* You will go through the training examples one at a time, so the optimization algorithm will be stochastic gradient descent.

As a reminder, here are the steps of a common optimization loop for an RNN:

- Forward propagate through the RNN to compute the loss
- Backward propagate through time to compute the gradients of the loss with respect to the parameters
- Clip the gradients
- Update the parameters using gradient descent

**Exercise**: Implement the optimization process (one step of stochastic gradient descent).

The following functions are provided:

```python
def rnn_forward(X, Y, a_prev, parameters):
    """ Performs the forward propagation through the RNN and computes the cross-entropy loss.
    It returns the loss' value as well as a "cache" storing values to be used in backpropagation."""
    ....
    return loss, cache

def rnn_backward(X, Y, parameters, cache):
    """ Performs the backward propagation through time to compute the gradients of the loss with respect
    to the parameters. It returns also all the hidden states."""
    ...
    return gradients, a

def update_parameters(parameters, gradients, learning_rate):
    """ Updates parameters using the Gradient Descent Update Rule."""
    ...
    return parameters
```

Recall that you previously implemented the `clip` function:

```Python
def clip(gradients, maxValue)
    """Clips the gradients' values between minimum and maximum."""
    ...
    return gradients
```_____no_output_____#### parameters

* Note that the weights and biases inside the `parameters` dictionary are being updated by the optimization, even though `parameters` is not one of the returned values of the `optimize` function. The `parameters` dictionary is passed by reference into the function, so changes to this dictionary are making changes to the `parameters` dictionary even when accessed outside of the function.
* Python dictionaries and lists are "pass by reference", which means that if you pass a dictionary into a function and modify the dictionary within the function, this changes that same dictionary (it's not a copy of the dictionary)._____no_output_____

# GRADED FUNCTION: optimize

def optimize(X, Y, a_prev, parameters, learning_rate = 0.01):
    """
    Execute one step of the optimization to train the model.

    Arguments:
    X -- list of integers, where each integer is a number that maps to a character in the vocabulary.
    Y -- list of integers, exactly the same as X but shifted one index to the left.
    a_prev -- previous hidden state.
    parameters -- python dictionary containing:
                        Wax -- Weight matrix multiplying the input, numpy array of shape (n_a, n_x)
                        Waa -- Weight matrix multiplying the hidden state, numpy array of shape (n_a, n_a)
                        Wya -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)
                        b --  Bias, numpy array of shape (n_a, 1)
                        by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)
    learning_rate -- learning rate for the model.

    Returns:
    loss -- value of the loss function (cross-entropy)
    gradients -- python dictionary containing:
                        dWax -- Gradients of input-to-hidden weights, of shape (n_a, n_x)
                        dWaa -- Gradients of hidden-to-hidden weights, of shape (n_a, n_a)
                        dWya -- Gradients of hidden-to-output weights, of shape (n_y, n_a)
                        db -- Gradients of bias vector, of shape (n_a, 1)
                        dby -- Gradients of output bias vector, of shape (n_y, 1)
    a[len(X)-1] -- the last hidden state, of shape (n_a, 1)
    """

    ### START CODE HERE ###

    # Forward propagate through time (≈1 line)
    loss, cache =  rnn_forward(X, Y, a_prev, parameters)

    # Backpropagate through time (≈1 line)
    gradients, a = rnn_backward(X, Y, parameters, cache)

    # Clip your gradients between -5 (min) and 5 (max) (≈1 line)
    gradients = clip(gradients, 5)

    # Update parameters (≈1 line)
    parameters = update_parameters(parameters, gradients, learning_rate)

    ### END CODE HERE ###

    return loss, gradients, a[len(X)-1]_____no_output_____np.random.seed(1)
vocab_size, n_a = 27, 100
a_prev = np.random.randn(n_a, 1)
Wax, Waa, Wya = np.random.randn(n_a, vocab_size), np.random.randn(n_a, n_a), np.random.randn(vocab_size, n_a)
b, by = np.random.randn(n_a, 1), np.random.randn(vocab_size, 1)
parameters = {"Wax": Wax, "Waa": Waa, "Wya": Wya, "b": b, "by": by}
X = [12,3,5,11,22,3]
Y = [4,14,11,22,25, 26]

loss, gradients, a_last = optimize(X, Y, a_prev, parameters, learning_rate = 0.01)
print("Loss =", loss)
print("gradients[\"dWaa\"][1][2] =", gradients["dWaa"][1][2])
print("np.argmax(gradients[\"dWax\"]) =", np.argmax(gradients["dWax"]))
print("gradients[\"dWya\"][1][2] =", gradients["dWya"][1][2])
print("gradients[\"db\"][4] =", gradients["db"][4])
print("gradients[\"dby\"][1] =", gradients["dby"][1])
print("a_last[4] =", a_last[4])Loss = 126.503975722
gradients["dWaa"][1][2] = 0.194709315347
np.argmax(gradients["dWax"]) = 93
gradients["dWya"][1][2] = -0.007773876032
gradients["db"][4] = [-0.06809825]
gradients["dby"][1] = [ 0.01538192]
a_last[4] = [-1.]

** Expected output:**

```Python
Loss = 126.503975722
gradients["dWaa"][1][2] = 0.194709315347
np.argmax(gradients["dWax"]) = 93
gradients["dWya"][1][2] = -0.007773876032
gradients["db"][4] = [-0.06809825]
gradients["dby"][1] = [ 0.01538192]
a_last[4] = [-1.]
```_____no_output_____### 3.2 - Training the model _____no_output_____* Given the dataset of dinosaur names, we use each line of the dataset (one name) as one training example.
* Every 100 steps of stochastic gradient descent, you will sample 10 randomly chosen names to see how the algorithm is doing.
* Remember to shuffle the dataset, so that stochastic gradient descent visits the examples in random order.

**Exercise**: Follow the instructions and implement `model()`. When `examples[index]` contains one dinosaur name (string), to create an example (X, Y), you can use this:

##### Set the index `idx` into the list of examples
* Using the for-loop, walk through the shuffled list of dinosaur names in the list "examples".
* If there are 100 examples, and the for-loop increments the index to 100 onwards, think of how you would make the index cycle back to 0, so that we can continue feeding the examples into the model when j is 100, 101, etc.
* Hint: 101 divided by 100 is zero with a remainder of 1.
* `%` is the modulus operator in python.

##### Extract a single example from the list of examples
* `single_example`: use the `idx` index that you set previously to get one word from the list of examples._____no_output_____##### Convert a string into a list of characters: `single_example_chars`
* `single_example_chars`: A string is a list of characters.
* You can use a list comprehension (recommended over for-loops) to generate a list of characters.
```Python
str = 'I love learning'
list_of_chars = [c for c in str]
print(list_of_chars)
```

```
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'l', 'e', 'a', 'r', 'n', 'i', 'n', 'g']
```_____no_output_____##### Convert list of characters to a list of integers: `single_example_ix`
* Create a list that contains the index numbers associated with each character.
* Use the dictionary `char_to_ix`
* You can combine this with the list comprehension that is used to get a list of characters from a string.
* This is a separate line of code below, to help learners clarify each step in the function._____no_output_____##### Create the list of input characters: `X`
* `rnn_forward` uses the `None` value as a flag to set the input vector as a zero-vector.
* Prepend the `None` value in front of the list of input characters.
* There is more than one way to prepend a value to a list.  One way is to add two lists together: `['a'] + ['b']`_____no_output_____##### Get the integer representation of the newline character `ix_newline`
* `ix_newline`: The newline character signals the end of the dinosaur name.
    - get the integer representation of the newline character `'\n'`.
    - Use `char_to_ix`_____no_output_____##### Set the list of labels (integer representation of the characters): `Y`
* The goal is to train the RNN to predict the next letter in the name, so the labels are the list of characters that are one time step ahead of the characters in the input `X`.
    - For example, `Y[0]` contains the same value as `X[1]`
* The RNN should predict a newline at the last letter so add ix_newline to the end of the labels.
    - Append the integer representation of the newline character to the end of `Y`.
    - Note that `append` is an in-place operation.
    - It might be easier for you to add two lists together._____no_output_____

# GRADED FUNCTION: model

def model(data, ix_to_char, char_to_ix, num_iterations = 35000, n_a = 50, dino_names = 7, vocab_size = 27):
    """
    Trains the model and generates dinosaur names.

    Arguments:
    data -- text corpus
    ix_to_char -- dictionary that maps the index to a character
    char_to_ix -- dictionary that maps a character to an index
    num_iterations -- number of iterations to train the model for
    n_a -- number of units of the RNN cell
    dino_names -- number of dinosaur names you want to sample at each iteration.
    vocab_size -- number of unique characters found in the text (size of the vocabulary)

    Returns:
    parameters -- learned parameters
    """

    # Retrieve n_x and n_y from vocab_size
    n_x, n_y = vocab_size, vocab_size

    # Initialize parameters
    parameters = initialize_parameters(n_a, n_x, n_y)

    # Initialize loss (this is required because we want to smooth our loss)
    loss = get_initial_loss(vocab_size, dino_names)

    # Build list of all dinosaur names (training examples).
    with open("dinos.txt") as f:
        examples = f.readlines()
    examples = [x.lower().strip() for x in examples]

    # Shuffle list of all dinosaur names
    np.random.seed(0)
    np.random.shuffle(examples)

    # Initialize the hidden state of your LSTM
    a_prev = np.zeros((n_a, 1))

    # Optimization loop
    for j in range(num_iterations):

        ### START CODE HERE ###

        # Set the index `idx` (see instructions above)
        idx = j % len(examples)

        # Set the input X (see instructions above)
        single_example = examples[idx]
        single_example_chars = [c for c in single_example]
        single_example_ix = [char_to_ix[ch] for ch in single_example_chars]
        X = [None] + single_example_ix

        # Set the labels Y (see instructions above)
        ix_newline = char_to_ix["\n"]
        Y = X[1:] + [ix_newline]

        # Perform one optimization step: Forward-prop -> Backward-prop -> Clip -> Update parameters
        # Choose a learning rate of 0.01
        curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters)

        ### END CODE HERE ###

        # Use a latency trick to keep the loss smooth. It happens here to accelerate the training.
        loss = smooth(loss, curr_loss)

        # Every 2000 Iteration, generate "n" characters thanks to sample() to check if the model is learning properly
        if j % 2000 == 0:

            print('Iteration: %d, Loss: %f' % (j, loss) + '\n')

            # The number of dinosaur names to print
            seed = 0
            for name in range(dino_names):

                # Sample indices and print them
                sampled_indices = sample(parameters, char_to_ix, seed)
                print_sample(sampled_indices, ix_to_char)

                seed += 1  # To get the same result (for grading purposes), increment the seed by one.

            print('\n')

    return parameters_____no_output_____

Run the following cell, you should observe your model outputting random-looking characters at the first iteration. After a few thousand iterations, your model should learn to generate reasonable-looking names. _____no_output_____

parameters = model(data, ix_to_char, char_to_ix)Iteration: 0, Loss: 23.087336

Nkzxwtdmfqoeyhsqwasjkjvu
Kneb
Kzxwtdmfqoeyhsqwasjkjvu
Neb
Zxwtdmfqoeyhsqwasjkjvu
Eb
Xwtdmfqoeyhsqwasjkjvu

Iteration: 2000, Loss: 27.884160

Liusskeomnolxeros
Hmdaairus
Hytroligoraurus
Lecalosapaus
Xusicikoraurus
Abalpsamantisaurus
Tpraneronxeros

Iteration: 4000, Loss: 25.901815

Mivrosaurus
Inee
Ivtroplisaurus
Mbaaisaurus
Wusichisaurus
Cabaselachus
Toraperlethosdarenitochusthiamamumamaon

Iteration: 6000, Loss: 24.608779

Onwusceomosaurus
Lieeaerosaurus
Lxussaurus
Oma
Xusteonosaurus
Eeahosaurus
Toreonosaurus

Iteration: 8000, Loss: 24.070350

Onxusichepriuon
Kilabersaurus
Lutrodon
Omaaerosaurus
Xutrcheps
Edaksoje
Trodiktonus

Iteration: 10000, Loss: 23.844446

Onyusaurus
Klecalosaurus
Lustodon
Ola
Xusodonia
Eeaeosaurus
Troceosaurus

Iteration: 12000, Loss: 23.291971

Onyxosaurus
Kica
Lustrepiosaurus
Olaagrraiansaurus
Yuspangosaurus
Eealosaurus
Trognesaurus

Iteration: 14000, Loss: 23.382338

Meutromodromurus
Inda
Iutroinatorsaurus
Maca
Yusteratoptititan
Ca
Troclosaurus

Iteration: 16000, Loss: 23.255630

Meustolkanolus
Indabestacarospceryradwalosaurus
Justolopinaveraterasauracoptelalenyden
Maca
Yusocles
Daahosaurus
Trodon

Iteration: 18000, Loss: 22.905483

Phytronn
Meicanstolanthus
Mustrisaurus
Pegalosaurus
Yuskercis
Egalosaurus
Tromelosaurus

Iteration: 20000, Loss: 22.873854

Nlyushanerohyisaurus
Loga
Lustrhigosaurus
Nedalosaurus
Yuslangosaurus
Elagosaurus
Trrangosaurus

Iteration: 22000, Loss: 22.710545

Onyxromicoraurospareiosatrus
Liga
Mustoffankeugoptardoros
Ola
Yusodogongterosaurus
Ehaerona
Trododongxernochenhus

Iteration: 24000, Loss: 22.604827

Meustognathiterhucoplithaloptha
Jigaadosaurus
Kurrodon
Mecaistheansaurus
Yuromelosaurus
Eiaeropeeton
Troenathiteritaus

Iteration: 26000, Loss: 22.714486

Nhyxosaurus
Kola
Lvrosaurus
Necalosaurus
Yurolonlus
Ejakosaurus
Troindronykus

Iteration: 28000, Loss: 22.647640

Onyxosaurus
Loceahosaurus
Lustleonlonx
Olabasicachudrakhurgawamosaurus
Ytrojianiisaurus
Eladon
Tromacimathoshargicitan

Iteration: 30000, Loss: 22.598485

Oryuton
Locaaesaurus
Lustoendosaurus
Olaahus
Yusaurus
Ehadopldarshuellus
Troia

Iteration: 32000, Loss: 22.211861

Meutronlapsaurus
Kracallthcaps
Lustrathus
Macairugeanosaurus
Yusidoneraverataus
Eialosaurus
Troimaniathonsaurus

Iteration: 34000, Loss: 22.447230

Onyxipaledisons
Kiabaeropa
Lussiamang
Pacaeptabalsaurus
Xosalong
Eiacoteg
Troia

** Expected Output**

The output of your model may look different, but it will look something like this:

```Python
Iteration: 34000, Loss: 22.447230

Onyxipaledisons
Kiabaeropa
Lussiamang
Pacaeptabalsaurus
Xosalong
Eiacoteg
Troia
```_____no_output_____## Conclusion

You can see that your algorithm has started to generate plausible dinosaur names towards the end of the training. At first, it was generating random characters, but towards the end you could see dinosaur names with cool endings. Feel free to run the algorithm even longer and play with hyperparameters to see if you can get even better results. Our implementation generated some really cool names like `maconucon`, `marloralus` and `macingsersaurus`. Your model hopefully also learned that dinosaur names tend to end in `saurus`, `don`, `aura`, `tor`, etc.

If your model generates some non-cool names, don't blame the model entirely--not all actual dinosaur names sound cool. (For example, `dromaeosauroides` is an actual dinosaur name and is in the training set.) But this model should give you a set of candidates from which you can pick the coolest!

This assignment had used a relatively small dataset, so that you could train an RNN quickly on a CPU. Training a model of the english language requires a much bigger dataset, and usually needs much more computation, and could run for many hours on GPUs. We ran our dinosaur name for quite some time, and so far our favorite name is the great, undefeatable, and fierce: Mangosaurus!

_____no_output_____## 4 - Writing like Shakespeare

The rest of this notebook is optional and is not graded, but we hope you'll do it anyway since it's quite fun and informative.

A similar (but more complicated) task is to generate Shakespeare poems. Instead of learning from a dataset of Dinosaur names you can use a collection of Shakespearian poems. Using LSTM cells, you can learn longer term dependencies that span many characters in the text--e.g., where a character appearing somewhere a sequence can influence what should be a different character much much later in the sequence. These long term dependencies were less important with dinosaur names, since the names were quite short.

 Let's become poets!

We have implemented a Shakespeare poem generator with Keras. Run the following cell to load the required packages and models. This may take a few minutes. _____no_output_____

from __future__ import print_function
from keras.callbacks import LambdaCallback
from keras.models import Model, load_model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, Masking
from keras.layers import LSTM
from keras.utils.data_utils import get_file
from keras.preprocessing.sequence import pad_sequences
from shakespeare_utils import *
import sys
import ioUsing TensorFlow backend.

To save you some time, we have already trained a model for ~1000 epochs on a collection of Shakespearian poems called [*"The Sonnets"*](shakespeare.txt). _____no_output_____Let's train the model for one more epoch. When it finishes training for an epoch---this will also take a few minutes---you can run `generate_output`, which will prompt asking you for an input (`<`40 characters). The poem will start with your sentence, and our RNN-Shakespeare will complete the rest of the poem for you! For example, try "Forsooth this maketh no sense " (don't enter the quotation marks). Depending on whether you include the space at the end, your results might also differ--try it both ways, and try other inputs as well.
_____no_output_____

print_callback = LambdaCallback(on_epoch_end=on_epoch_end)

model.fit(x, y, batch_size=128, epochs=1, callbacks=[print_callback])Epoch 1/1
31412/31412 [==============================] - 271s - loss: 2.0217
# Run this cell to try with different inputs without having to re-train the model
generate_output()_____no_output_____

The RNN-Shakespeare model is very similar to the one you have built for dinosaur names. The only major differences are:
- LSTMs instead of the basic RNN to capture longer-range dependencies
- The model is a deeper, stacked LSTM model (2 layer)
- Using Keras instead of python to simplify the code

If you want to learn more, you can also check out the Keras Team's text generation implementation on GitHub: https://github.com/keras-team/keras/blob/master/examples/lstm_text_generation.py.

Congratulations on finishing this notebook! _____no_output_____**References**:
- This exercise took inspiration from Andrej Karpathy's implementation: https://gist.github.com/karpathy/d4dee566867f8291f086. To learn more about text generation, also check out Karpathy's [blog post](http://karpathy.github.io/2015/05/21/rnn-effectiveness/).
- For the Shakespearian poem generator, our implementation was based on the implementation of an LSTM text generator by the Keras team: https://github.com/keras-team/keras/blob/master/examples/lstm_text_generation.py _____no_output_____


# Notebook from gifford-lab/skipguide-analysis
Path: src/datB_Characterize_Skipping.ipynb

# dat-B: Characterize Skipping
dat-B in this study refers to the set of repair genotypes observed in Spliced (Pre and PostCas9, where PreCas9 is control, and gives us wildtype $\Psi$), and their count of transcripts with or without Exon B. The code here extracts these information._____no_output_____

from config import *
from utils import *

import sys
import regex
import copy
import numpy as np
import collections
import multiprocessing
import pickle

import numpy as np
import scipy

# Suppress pandas future warning, which messes tqdm
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd

from tqdm.notebook import tqdm

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns_____no_output_____bc_spliced_precas_map = load_bc_seq(BC_SPLICED_PRECAS_MAP)
bc_spliced1_postcas_map = load_bc_seq(BC_SPLICED1_POSTCAS_MAP)
bc_spliced2_postcas_map = load_bc_seq(BC_SPLICED2_POSTCAS_MAP)

bc_tid_indel_postcas_map = load_bc_id_indel(BC_TID_INDEL_POSTCAS_MAP)
unnorm_gt_indel_dist_map = load_var(UNNORM_GT_INDEL_DIST_MAP)
gt_indel_dist_map = load_var(GT_INDEL_DIST_MAP)_____no_output_____K = 5
design_t_kmers_map = {design_t:kmers(design_t, K) for design_t in exp_target_tid_map}_____no_output_____

# Characterize Skipping
For a given spliced RNA transcript sequence, we need to figure out the original designed target (splice-acceptor) sequence. Similar to what we did in the construction of dat-A, for a query sequence Q, we find the most similar designed sequence D from lib-SA by computing the Dice Coefficient between Q and D, using 5-mers, and picking the D that is most similar to Q.

For a transcript with Exon B skipped, only the barcode information is known. To associate it with its repair genotype, its barcode is queried on the dat-A PostCas lookup table (variable bc_tid_indel_postcas_map) to find the repair genotype with the same barcode. If there are multiple repair genotypes with the same barcode, then the one with the largest read count is chosen for association. If the barcode is not observed in dat-A, then the transcript is discarded.

For a transcript with Exon B retained, naturally only the Exon B portion of the repair outcome is discernible. Similarly to identify its repair genotype, its barcode is queried on dat-A lookup table. If there is one repair genotype with the same barcode and that repair genotype agrees with the partial Exon B we observe in the transcript, then we're set. If there are multiple repair genotypes with the same barcode AND agrees with the partial Exon B we observe in the transcript, then one of them is picked randomly, sampled according to the frequency of observing each candidate (see code). How can there be multiple possible repair genotypes? Consider the following original and spliced sequences:

```
Original ('|' denote intron-exon boundary)
AGTCAAG|CGGTCA

Repair outcomes
AGTC---|CGGTCA
AGTCAA-|CGGTCA
AGTCA--|CGGTCA
AGTCA--|--GTCA

Observed transcript:
ExonA|CGGTCA
```

We see that the first three repair outcomes all can give rise to the observed transcript, and there's no way of knowing which one it is if all three happen to match to the same barcode (rare, but possible)._____no_output_____## Characterize PreCas Spliced_____no_output_____

def search_splice(ref, obs):
    prog = regex.compile(r'(?b)('+obs+'){e<=3}')
    return prog.search(ref)

def get_spliceidx(ref, obs):
    m = search_splice(ref, obs)
    if m:
        return m.start()
    return -1

def get_tid_spliced(obs_t):
    if len(obs_t) < 5:
        return None

    if len(obs_t) > 12 and hamming_distance('CACAACATCGAG', obs_t[0:12]) <= 2:
        obs_t = obs_t[12:]

    max_sim = -float('inf')
    best_design_t = None
    for design_t in exp_target_tid_map:
        if design_t in obs_t or obs_t in design_t:
            max_sim = 1
            best_design_t = design_t
            break

        sim = dice_coefficient(obs_t, design_t_kmers_map[design_t], K)
        if sim > max_sim:
            max_sim = sim
            best_design_t = design_t

    return best_design_t, obs_t, max_sim

def get_tid_spliceidx_precas(obs_t):
    t_spliced = get_tid_spliced(obs_t)
    if t_spliced is None:
        return None, -1

    best_design_t, obs_t, max_sim = t_spliced

    if max_sim < 0.3:
        return None, -1

    spliceidx = get_spliceidx(best_design_t, obs_t)
    return exp_target_tid_map[best_design_t], spliceidx

def generate_bc_tid_spliceidx_precas_map(bc_precas_splice_map):
    try:
        p = multiprocessing.Pool(NUM_PROCESSES)

        bc_tid_spliceidx_map = collections.defaultdict(lambda: collections.defaultdict(lambda: collections.defaultdict(int)))

        obs_ts = list({obs_t for bc in tqdm(bc_precas_splice_map) for obs_t in bc_precas_splice_map[bc]})
        tid_spliceidxs = []
        for tid_spliceidx in tqdm(p.imap(get_tid_spliceidx_precas, obs_ts, chunksize=1), total=len(obs_ts)):
            tid_spliceidxs.append(tid_spliceidx)

        obs_tidspliceidx_map = {obs_t:tid_spliceidxs[i] for i, obs_t in enumerate(obs_ts)}

        for bc in tqdm(bc_precas_splice_map):
            for obs_t in bc_precas_splice_map[bc]:
                if obs_t == 'SKIPPED':
                    tid, spliceidx = 'SKIPPED', -1
                else:
                    tid, spliceidx = obs_tidspliceidx_map[obs_t]
                    if tid is None or spliceidx == -1:
                        continue

                bc_tid_spliceidx_map[bc][tid][spliceidx] += bc_precas_splice_map[bc][obs_t]

        print("(Before) Num Unique BCs:", len(bc_precas_splice_map))
        print("(After) Num Unique BCs:", len(bc_tid_spliceidx_map))
    finally:
        p.close()
        p.join()

    return bc_tid_spliceidx_map_____no_output_____if not pickle_exists(BC_TID_SPLICEIDX_PRECAS_MAP):
    bc_tid_spliceidx_precas_map = generate_bc_tid_spliceidx_precas_map(bc_spliced_precas_map)
    save_bc_id_indel(bc_tid_spliceidx_precas_map, BC_TID_SPLICEIDX_PRECAS_MAP)
else:
    bc_tid_spliceidx_precas_map = load_bc_id_indel(BC_TID_SPLICEIDX_PRECAS_MAP)_____no_output_____

## Characterize PostCas Spliced_____no_output_____

def get_gid_from_indel(indel, tid):
    if indel[1] in DELETION_SIGNATURES:
        _, _, deletion_size, genotype_pos, cutsite = indel
        for gid in exp_tid_gids_map[tid]:
            design_t = exp_tid_target_map[tid]
            grna = exp_gid_grna_map[gid]
            g_orientation = exp_grna_gid_map[grna][1]
            if cutsite == get_cutsite(grna, design_t, g_orientation):
                return gid
    elif indel[1] in INSERTION_SIGNATURES and indel[2] == 1:
        _, _, _, inserted_base, cutsite =  indel
        for gid in exp_tid_gids_map[tid]:
            design_t = exp_tid_target_map[tid]
            grna = exp_gid_grna_map[gid]
            g_orientation = exp_grna_gid_map[grna][1]
            if cutsite == get_cutsite(grna, design_t, g_orientation):
                return gid
    return None

np.random.seed(42)
def get_indel_spliceidx(a):
    t_spliced, bc = a
    if t_spliced is None:
        return None

    best_design_t, obs_t, max_sim = t_spliced
    tid = exp_target_tid_map[best_design_t]

    if tid not in bc_tid_indel_postcas_map[bc]:
        return None

    m_wt = search_splice(best_design_t, obs_t)

    min_fuzzy_count = float('inf')
    best_indel_spliceidx = []
    for indel in bc_tid_indel_postcas_map[bc][tid]:
        if indel[1] in DELETION_SIGNATURES or (indel[1] in INSERTION_SIGNATURES and indel[2] == 1):
            prod = get_simulated_product(indel, best_design_t)
            m = search_splice(prod, obs_t)
            if m:
                fuzzy_count = sum(m.fuzzy_counts)
                if fuzzy_count == min_fuzzy_count:
                    best_indel_spliceidx.append((m.start(), indel, tid))
                elif fuzzy_count < min_fuzzy_count:
                    min_fuzzy_count = fuzzy_count
                    best_indel_spliceidx = [(m.start(), indel, tid)]

    # Unsure: Could be uncut WT, or indel only in the intron.
    # There's no way of knowing which, so just ignore them
    if m_wt and sum(m_wt.fuzzy_counts) < min_fuzzy_count:
        return None

    if len(best_indel_spliceidx) == 0:
        return None

    # Unique best match found
    if len(best_indel_spliceidx) == 1:
        return best_indel_spliceidx[0]

    # If reached here, there are multiple possible original repair genotypes
    # Rather than giving up here and discarding them,
    # we look at the observed frequencies for each of the possible repair genotypes in question
    # This information is available from dat-A. We then sample one of these repair genotypes
    # according to these frequencies, and choose that as the original repair genotype.
    p = []
    for (s, indel, tid) in best_indel_spliceidx:
        gid = get_gid_from_indel(indel, tid)
        grna_seq = exp_gid_grna_map[gid]
        grna_orientation = exp_grna_gid_map[grna_seq][1]

        dist = gt_indel_dist_map[(grna_seq, best_design_t, grna_orientation)]

        if indel[1] in DELETION_SIGNATURES:
            _, _, deletion_size, genotype_pos, cutsite = indel
            p.append(dist[-deletion_size][genotype_pos])
        elif indel[1] in INSERTION_SIGNATURES:
            _, _, _, inserted_base, cutsite = indel
            p.append(dist[1][inserted_base])
    p = np.array(p) / np.sum(p)
    return best_indel_spliceidx[np.random.choice(len(best_indel_spliceidx), 1, p=p)[0]]

def generate_bc_tid_spliceidx_postcas_map(bc_postcas_splice_map):
    try:
        p = multiprocessing.Pool(NUM_PROCESSES)

        bc_tid_spliceidx_map = collections.defaultdict(lambda: collections.defaultdict(lambda: collections.defaultdict(int)))

        obs_ts = list({obs_t for bc in tqdm(bc_postcas_splice_map) for obs_t in bc_postcas_splice_map[bc]})
        t_splices = []
        for t_spliced in tqdm(p.imap(get_tid_spliced, obs_ts, chunksize=1), total=len(obs_ts)):
            t_splices.append(t_spliced)

        obs_tspliced_map = {obs_t:t_splices[i] for i, obs_t in enumerate(obs_ts)}

        tsplice_bcs = set()
        for bc in tqdm(bc_postcas_splice_map):
            if bc in bc_tid_indel_postcas_map:
                for obs_t in bc_postcas_splice_map[bc]:
                    tsplice_bcs.add((obs_tspliced_map[obs_t], bc))
        tsplice_bcs = list(tsplice_bcs)

        indel_spliceidxs = []
        for indel_spliceidx in tqdm(p.imap(get_indel_spliceidx, tsplice_bcs, chunksize=1), total=len(tsplice_bcs)):
            indel_spliceidxs.append(indel_spliceidx)

        tsplicebc_spliceidx_map = {tsplicebc:indel_spliceidxs[i] for i, tsplicebc in enumerate(tsplice_bcs)}

        for bc in tqdm(bc_postcas_splice_map):
            if bc in bc_tid_indel_postcas_map:
                for obs_t in bc_postcas_splice_map[bc]:
                    if obs_t == 'SKIPPED':
                        bc_tid_spliceidx_map[bc]['SKIPPED'][-1] += bc_postcas_splice_map[bc][obs_t]
                        continue

                    spliceidx_indel_tid = tsplicebc_spliceidx_map[(obs_tspliced_map[obs_t], bc)]
                    if spliceidx_indel_tid:
                        spliceidx, indel, tid = spliceidx_indel_tid
                        bc_tid_spliceidx_map[bc][tid][(spliceidx, indel)] += bc_postcas_splice_map[bc][obs_t]

        print("(Before) Num Unique BCs:", len(bc_postcas_splice_map))
        print("(After) Num Unique BCs:", len(bc_tid_spliceidx_map))
    finally:
        p.close()
        p.join()

    return bc_tid_spliceidx_map_____no_output_____if not pickle_exists(BC_TID_SPLICEIDX_POSTCAS_MAP):
    bc_tid1_spliceidx_postcas_map = generate_bc_tid_spliceidx_postcas_map(bc_spliced1_postcas_map)_____no_output_____if not pickle_exists(BC_TID_SPLICEIDX_POSTCAS_MAP):
    bc_tid2_spliceidx_postcas_map = generate_bc_tid_spliceidx_postcas_map(bc_spliced2_postcas_map)_____no_output_____def merge_bc_tid_spliceidx_maps(bc_tid1_spliceidx_map, bc_tid2_spliceidx_map):
    bc_tid_spliceidx_map = collections.defaultdict(lambda: collections.defaultdict(lambda: collections.defaultdict(int)))

    merged = 0
    for bc in tqdm(bc_tid1_spliceidx_map.keys() & bc_tid2_spliceidx_map.keys()):
        for tid in (bc_tid1_spliceidx_map[bc].keys() & bc_tid2_spliceidx_map[bc].keys()):
            for spliceidx in (bc_tid1_spliceidx_map[bc][tid].keys() & bc_tid2_spliceidx_map[bc][tid].keys()):
                bc_tid_spliceidx_map[bc][tid][spliceidx] += np.mean([bc_tid1_spliceidx_map[bc][tid][spliceidx], bc_tid2_spliceidx_map[bc][tid][spliceidx]])
                merged += 1

    for bc in tqdm(bc_tid1_spliceidx_map):
        for tid in bc_tid1_spliceidx_map[bc]:
            for spliceidx in bc_tid1_spliceidx_map[bc][tid]:
                if not (bc in bc_tid_spliceidx_map and tid in bc_tid_spliceidx_map[bc] and spliceidx in bc_tid_spliceidx_map[bc][tid]):
                    bc_tid_spliceidx_map[bc][tid][spliceidx] += bc_tid1_spliceidx_map[bc][tid][spliceidx]
    for bc in tqdm(bc_tid2_spliceidx_map):
        for tid in bc_tid2_spliceidx_map[bc]:
            for spliceidx in bc_tid2_spliceidx_map[bc][tid]:
                if not (bc in bc_tid_spliceidx_map and tid in bc_tid_spliceidx_map[bc] and spliceidx in bc_tid_spliceidx_map[bc][tid]):
                    bc_tid_spliceidx_map[bc][tid][spliceidx] += bc_tid2_spliceidx_map[bc][tid][spliceidx]

    print("Num Common:", merged)
    return bc_tid_spliceidx_map_____no_output_____if not pickle_exists(BC_TID_SPLICEIDX_POSTCAS_MAP):
    bc_tid_spliceidx_postcas_map = merge_bc_tid_spliceidx_maps(bc_tid1_spliceidx_postcas_map, bc_tid2_spliceidx_postcas_map)
    save_bc_id_indel(bc_tid_spliceidx_postcas_map, BC_TID_SPLICEIDX_POSTCAS_MAP)
else:
    bc_tid_spliceidx_postcas_map = load_bc_id_indel(BC_TID_SPLICEIDX_POSTCAS_MAP)_____no_output_____def get_skipped_tid_precas(bc):
    if len(bc_tid_spliceidx_precas_map[bc]) > 1:
        max_count = 0
        best_tid = None
        for tid in bc_tid_spliceidx_precas_map[bc]:
            if tid != 'SKIPPED':
                count = sum(bc_tid_spliceidx_precas_map[bc][tid][spliceidx] for spliceidx in bc_tid_spliceidx_precas_map[bc][tid])
                if count > max_count:
                    max_count = count
                    best_tid = tid
        if best_tid is not None:
            best_spliceidx = max((bc_tid_spliceidx_precas_map[bc][best_tid][spliceidx] ,spliceidx)
                                 for spliceidx in bc_tid_spliceidx_precas_map[bc][best_tid])[1]
            return best_tid, best_spliceidx
    return None, None

def get_skipped_indel_postcas(bc):
    max_count = 0
    best_indel1 = None
    if len(bc_tid_spliceidx_postcas_map[bc]) > 1:
        for tid in bc_tid_spliceidx_postcas_map[bc]:
            if tid != 'SKIPPED':
                for indel in bc_tid_spliceidx_postcas_map[bc][tid]:
                    count = bc_tid_spliceidx_postcas_map[bc][tid][indel]
                    if count > max_count:
                        max_count = count
                        best_indel1 = (tid, indel)

    return best_indel1

def generate_gid_spliceidx_count_map(bc_tid_spliceidx_map, mode='precas'):
    gid_spliceidx_count_map = collections.defaultdict(lambda: collections.defaultdict(int))

    for bc in tqdm(bc_tid_spliceidx_map):
        for tid in bc_tid_spliceidx_map[bc]:
            if mode == 'precas':
                if tid == 'SKIPPED':
                    guessed_tid, spliceidx = get_skipped_tid_precas(bc)
                    if guessed_tid is not None:
                        payload = (spliceidx, (exp_tid_target_map[guessed_tid], 'N'))
                        gid_spliceidx_count_map[guessed_tid][(-1, payload)] += bc_tid_spliceidx_map[bc]['SKIPPED'][-1]
                else:
                    payload = (exp_tid_target_map[tid], 'N')
                    for spliceidx in bc_tid_spliceidx_map[bc][tid]:
                        gid_spliceidx_count_map[tid][(spliceidx, payload)] += bc_tid_spliceidx_map[bc][tid][spliceidx]
            else:
                if tid == 'SKIPPED':
                    guessed_p = get_skipped_indel_postcas(bc)
                    if guessed_p is not None:
                        gid, indel = guessed_p # indel is actually (spliceidx, indel)
                        gid_spliceidx_count_map[gid][(-1, indel)] += bc_tid_spliceidx_map[bc]['SKIPPED'][-1]
                else:
                    for payload in bc_tid_spliceidx_map[bc][tid]:
                        spliceidx, indel = payload
                        gid = get_gid_from_indel(indel, tid)
                        if gid is not None:
                            gid_spliceidx_count_map[gid][payload] += bc_tid_spliceidx_map[bc][tid][payload]

    return gid_spliceidx_count_map

def generate_indel_splice_count_map(gid_spliceidx_count_map):
#     {
#     indel: {
#         C: f,
#         B: f,
#         spliceidx: ,
#         gid:
#     }
# }
    indel_splice_count_map = collections.defaultdict(lambda: collections.defaultdict(int))

    for gid in gid_spliceidx_count_map:
        # Skip cases
        for p in gid_spliceidx_count_map[gid]:
            if p[0] == -1:
                spliceidx, indel = p[1]
                indel_splice_count_map[indel]['C'] += gid_spliceidx_count_map[gid][p]
                indel_splice_count_map[indel]['spliceidx'] = spliceidx
                indel_splice_count_map[indel]['gid'] = gid
        # Non skip cases
        indels = {p[1] for p in gid_spliceidx_count_map[gid] if p[0] != -1}
        for indel in indels:
            max_count = 0
            best_spliceidx = None
            total_counts = 0
            for p in gid_spliceidx_count_map[gid]:
                if p[1] == indel and p[0] != -1:
                    count = gid_spliceidx_count_map[gid][p]
                    total_counts += count
                    if count > max_count:
                        max_count = count
                        best_spliceidx = p[0]
            indel_splice_count_map[indel]['B'] += total_counts
            indel_splice_count_map[indel]['spliceidx'] = best_spliceidx
            indel_splice_count_map[indel]['gid'] = gid
    return indel_splice_count_map_____no_output_____if not pickle_exists(INDEL_SPLICE_PRECAS_COUNT_MAP):
    gid_spliceidx_precas_count_map = generate_gid_spliceidx_count_map(bc_tid_spliceidx_precas_map, 'precas')_____no_output_____# Dictionary output format
# {
#     indel: {
#         C: count,
#         B: count,
#         spliceidx: ,
#         gid:
#     }
# }

if not pickle_exists(INDEL_SPLICE_PRECAS_COUNT_MAP):
    indel_splice_precas_count_map = generate_indel_splice_count_map(gid_spliceidx_precas_count_map)
    save_bc_seq(indel_splice_precas_count_map, INDEL_SPLICE_PRECAS_COUNT_MAP)
else:
    indel_splice_precas_count_map = load_bc_seq(INDEL_SPLICE_PRECAS_COUNT_MAP)_____no_output_____if not pickle_exists(INDEL_SPLICE_POSTCAS_COUNT_MAP):
    gid_spliceidx_postcas_count_map = generate_gid_spliceidx_count_map(bc_tid_spliceidx_postcas_map, 'postcas')_____no_output_____# Dictionary output format
# {
#     indel: {
#         C: count,
#         B: count,
#         spliceidx: ,
#         gid:
#     }
# }

if not pickle_exists(INDEL_SPLICE_POSTCAS_COUNT_MAP):
    indel_splice_postcas_count_map = generate_indel_splice_count_map(gid_spliceidx_postcas_count_map)
    save_bc_seq(indel_splice_postcas_count_map, INDEL_SPLICE_POSTCAS_COUNT_MAP)
else:
    indel_splice_postcas_count_map = load_bc_seq(INDEL_SPLICE_POSTCAS_COUNT_MAP)_____no_output_____def generate_gt_splice_count_map(indel_splice_count_map):
    gt_splice_count_map = collections.defaultdict(lambda: collections.defaultdict(int))

    for indel, splice_count_map in indel_iterator(indel_splice_count_map):
        gid = splice_count_map['gid']
        grna = exp_gid_grna_map[gid]
        design_t = exp_tid_target_map[exp_gid_tid_map[gid]]
        g_orientation = exp_grna_gid_map[grna][1]
        pair = (grna, design_t, g_orientation)
        gt_splice_count_map[pair]['C'] += splice_count_map['C']
        gt_splice_count_map[pair]['B'] += splice_count_map['B']

    return gt_splice_count_map_____no_output_____if not pickle_exists(GT_SPLICE_COUNT_MAP):
    gt_splice_count_map = generate_gt_splice_count_map(indel_splice_postcas_count_map)
    save_bc_seq(gt_splice_count_map, GT_SPLICE_COUNT_MAP)
else:
    gt_splice_count_map = load_bc_seq(GT_SPLICE_COUNT_MAP)_____no_output_____if not pickle_exists(GT_PRECAS_SPLICE_COUNT_MAP):
    gt_precas_splice_count_map = generate_gt_splice_count_map(indel_splice_precas_count_map)
    save_bc_seq(gt_precas_splice_count_map, GT_PRECAS_SPLICE_COUNT_MAP)
else:
    gt_precas_splice_count_map = load_bc_seq(GT_PRECAS_SPLICE_COUNT_MAP)_____no_output_____

## Compute Empirical Exon B Retention Frequencies ($\Psi_R$, $\Psi_G$, and WT $\Psi$)_____no_output_____$\Psi = \frac{T_B + 1}{T_B + T_C + 2}$

The code below shows how these values are calculated. The same calculations will happen in subsequent notebooks for further analysis._____no_output_____

PSEUDO_COUNT = 1

def get_B_retention_f(indel_splice_count_map, count_thres):
    result = []
    gids = []
    B_counts = []
    C_counts = []
    indels = []
    total_counts = []
    for indel, splice_count_map in indel_iterator(indel_splice_count_map):
        if not (indel[1] == 'N' or indel[1] in DELETION_SIGNATURES or (indel[1] in INSERTION_SIGNATURES and indel[2] == 1)):
            continue

        total = splice_count_map['C'] + splice_count_map['B']
        if total <= count_thres:
            continue

        C_f = (splice_count_map['C'] + PSEUDO_COUNT) / (splice_count_map['C'] + splice_count_map['B'] + 2*PSEUDO_COUNT)
        result.append(1 - C_f)
        total_counts.append(splice_count_map['C'] + splice_count_map['B'])
        B_counts.append(splice_count_map['B'])
        C_counts.append(splice_count_map['C'])
        gids.append(splice_count_map['gid'])
        indels.append(indel)
    return result, gids, indels, B_counts, C_counts, total_counts

def get_aggBf_scores(gt_splice_count_map):
    aggBf_scores = []
    aggBf_counts = []
    B_counts = []
    C_counts = []
    gids = []
    for gt in gt_splice_count_map.keys():
        m = gt_splice_count_map[gt]

        gids.append(exp_grna_gid_map[gt[0]][0])
        C_f = (m['C'] + PSEUDO_COUNT) / (m['C'] + m['B'] + 2*PSEUDO_COUNT)
        aggBf_scores.append(1 - C_f)
        B_counts.append(m['B'])
        C_counts.append(m['C'])
        aggBf_counts.append(m['C'] + m['B'])
    return aggBf_scores, gids, B_counts, C_counts, aggBf_counts_____no_output_____

$\Psi_R$ ($\Psi$ from a repair genotype):_____no_output_____

count_thres = 50 # Consider only repair genotypes with at least COUNT_THRES observed transcripts for computing PSI values.
splice_precas_Bf, _, _, _, _, _ = get_B_retention_f(indel_splice_precas_count_map, 50)
(splice_postcas_Bf,
 splice_postcas_gids,
 splice_postcas_indels,
 splice_postcas_B,
 splice_postcas_C,
 splice_postcas_total) = get_B_retention_f(indel_splice_postcas_count_map, 50)_____no_output_____

$\Psi_G$ ($\Psi$ from a gRNA, considering all its repair genotypes observed) and

WT $\Psi$ ($\Psi$ from a gRNA, preCas9, so no repair genotypes):_____no_output_____

aggBf_postcas_scores, postcas_gids, aggBf_postcas_B, aggBf_postcas_C, aggBf_postcas_counts = get_aggBf_scores(gt_splice_count_map)
aggBf_precas_scores, precas_gids, aggBf_precas_B, aggBf_precas_C, aggBf_precas_counts = get_aggBf_scores(gt_precas_splice_count_map)_____no_output_____print('Mean splice transcript count per repair genotype:', np.mean(splice_postcas_total))Mean splice transcript count per repair genotype: 246.1883577851396
print("Num repair genotypes represented:", len(splice_postcas_Bf))Num repair genotypes represented: 2113
print("Num gRNAs represented:", len(set(splice_postcas_gids)))Num gRNAs represented: 1063

## S3 Table_____no_output_____

datB_postcas_df = pd.DataFrame({
    'gRNA ID': splice_postcas_gids,
    'Category': ['del' if indel[1] in DELETION_SIGNATURES else 'ins' for indel in splice_postcas_indels],
    'Genotype position': [indel[3] if indel[1] in DELETION_SIGNATURES else None for indel in splice_postcas_indels],
    'Inserted Bases': [indel[3] if indel[1] in INSERTION_SIGNATURES else None for indel in splice_postcas_indels],
    'Length': [indel[2] if indel[1] in DELETION_SIGNATURES else 1 for indel in splice_postcas_indels],
    'Exon B Retained Count': splice_postcas_B,
    'Exon B Skipped Count': splice_postcas_C
})_____no_output_____datB_postcas_df.head()_____no_output_____datB_postcas_df.shape[0]_____no_output_____datB_postcas_df['gRNA ID'].nunique()_____no_output_____datB_postcas_df.to_csv(os.path.join(TABLES_DIR, 'datB_postCas_table.csv.gz'), index=False, compression='gzip')_____no_output_____print("Total Reads:", datB_postcas_df['Exon B Retained Count'].sum() + datB_postcas_df['Exon B Skipped Count'].sum())Total Reads: 520196.0

## S4 Table_____no_output_____

datB_precas_df = pd.DataFrame({
    'gRNA ID': precas_gids,
    'Exon B Retained Count': aggBf_precas_B,
    'Exon B Skipped Count': aggBf_precas_C
})_____no_output_____datB_precas_df.head()_____no_output_____datB_precas_df.shape[0]_____no_output_____"Num Targets represented:", len(set(exp_gid_tid_map[g] for g in datB_precas_df['gRNA ID']))_____no_output_____print("(S3 Fig) Number of gRNAs in both PostCas9 and PreCas9 (WT) datasets:")
print(len(set(splice_postcas_gids) & set(precas_gids)))(S3 Fig) Number of gRNAs in both PostCas9 and PreCas9 (WT) datasets:
735
datB_precas_df.to_csv(os.path.join(TABLES_DIR, 'datB_preCas_table.csv.gz'), index=False, compression='gzip')_____no_output_____print("Total Reads:", datB_precas_df['Exon B Retained Count'].sum() + datB_precas_df['Exon B Skipped Count'].sum())Total Reads: 3443915