# Notebook from simehaa/MachineLearning
Path: stk-in4300/oblig1/Mandatory Assignment 1.ipynb

**STK-IN4300, Simen Håpnes**

This assignment with code might also be found at my **[github](https://github.com/simehaa/stk-in4300/tree/master/oblig1)**.

# Exercise 1
Contrast the ability of a lasso and a ridge regression model to predict the Duke CAD index (CADi) from the gene expression.

## Algorithm
The data file which is to be analysed contains 110 patients **with** Coronary Artery Disease.

The X-data contains 22283 various gene expressions, $X\in \mathbb{R}^{110 \times 22,283}$.

The y-data contains the true CADi value , $y \in \mathbb{R}^{110 \times 1}$.

I have written a python code, which uses the packages from Scikit-learn to perform Ridge and LASSO regression. The script to solve the exercise is presented. I will provide explainatory information between the code blocks as well as comments in the actual code.

To begin, I will do the necessary imports:_____no_output_____

%matplotlib inline
import matplotlib.pyplot as plt # for plotting
import pandas as pd # for reading csv file
import numpy as np # array/linear algebra library
import os # convenient when reading/writing files to other directories
from sklearn.linear_model import LassoCV, RidgeCV # Lasso and ridge with cross validation
from sklearn.model_selection import train_test_split # simple split of train and test set
from sklearn.preprocessing import scale # scaling of the data with both mean and std_____no_output_____

### Definition of functions
The two following functions are meant to easily read a file in the **./data/** directory and to plot/save figures to the **./figures/** directory._____no_output_____

def read_file(filename):
    """Read a csv data file located in the ./data/ directory
    Return design matrix X and y-data"""
    DATA_DIR = "./data"
    if not os.path.exists(DATA_DIR):
        os.mkdir(DATA_DIR)
    DATA_FILE = os.path.join(DATA_DIR,filename)
    df = pd.read_csv(DATA_FILE).values
    # 0th column contains file names
    y = np.array(df[:,1]) # 1st column is y-data
    X = np.array(df[:,2:]) # remaining columns is the X-data
    return X, y

def save_fig(y_test, y_pred, score, fig_id):
    """Save a matplotlib figure to the ./figure/ directory.
    If/else statements ask the user if they wish to overwrite an
    already existing figure."""
    # Plot
    l = y_test.shape[0]
    x = np.linspace(1,l,l)
    # plot of y_pred vs. y_test:
    plt.plot(y_test, y_pred, 'ro', label=fr"prediction, $R^2 = ${score:2.2f}")
    # plot of the ideal linear case
    min = np.min(y_test)
    max = np.max(y_test)
    plt.plot([min,max],[min,max])
    # plot options
    plt.xlabel(r"$y$")
    plt.ylabel(r"$\hat{y}$")
    plt.legend()
    plt.grid()
    # Saving of figure
    FIGURE_DIR = "./figures"
    if not os.path.exists(FIGURE_DIR):
        os.mkdir(FIGURE_DIR)
    FIGURE_FILE = os.path.join(FIGURE_DIR, fig_id)
    if os.path.exists(FIGURE_FILE):
        overwrite = str(input("The file \"" + FIGURE_FILE + \
            "\" already exists, do you wish to overwrite it [y/n]?\n"))
        if overwrite == "y":
            plt.savefig(FIGURE_FILE, format="png")
            plt.close()
            print("Figure was overwritten.")
        else:
            print("Figure was not saved.")
    else:
        plt.savefig(FIGURE_FILE, format="png")
        plt.close()
    return None_____no_output_____

The purpose of the main function is to use scikit-learn to perform Ridge regression and LASSO regression.

Note that X and y are scaled so that both mean = 0 and std = 1._____no_output_____

def main():
    """
    Info:
    Perform Ridge and Lasso regression on gene data. Try to predict
    The output which is the Duke CAD index
    (Coronary Artery Disease index).

    Notes:
    Data file "data_E1.csv" contains the 110 pasients with case:
    * The first column contains file IDs (not necessary for analysis)
    * the second column contains the y data: true CADi
    * the remaining columns contains 22283 features
    """
    # Read data from file
    X, y = read_file("data_E1.csv")
    # Scale data with both mean and std: e.g. y = y/std(y) - mean(y)
    std = np.std(y)
    mean = np.mean(y)
    X = scale(X)
    y = scale(y)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

    # Ridge (fit_intercept=False assumes that data is already scaled)
    clf_r = RidgeCV(fit_intercept=False).fit(X_train, y_train)
    y_pred_r = clf_r.predict(X_test)
    score_r = clf_r.score(X_test, y_test)
    y_test_r = y_test*std + mean
    y_pred_r = y_pred_r*std + mean
    # Plot and save figure
    save_fig(y_test_r, y_pred_r, score_r, "ridge.png")

    # Lasso (fit_intercept=False assumes that data is already scaled
    # Note: cv=5 is the default of a future version of scikit-learn
    clf_l = LassoCV(fit_intercept=False, cv=5).fit(X_train, y_train)
    y_pred_l = clf_l.predict(X_test)
    score_l = clf_l.score(X_test, y_test)
    y_test_l = y_test*std + mean
    y_pred_l = y_pred_l*std + mean
    # Plot and save figure
    save_fig(y_test_l, y_pred_l, score_l, "lasso.png")_____no_output_____

Run the main function:_____no_output_____

if __name__ == "__main__":
    np.random.seed(1) # affect the random splitting of train/test set: same seed obtain the same results each run
    main() # call the main function where the regressions are performed

# Test run, information about versions:
# python 3.7.3
# scikit-learn 0.21.3
# numpy 1.17.2
# pandas 0.25.1
# matplotlib 3.1.1
"""
$ python main.py
The file "./figures/ridge.png" already exists, do you wish to overwrite it [y/n]?
y
Figure was overwritten.
The file "./figures/lasso.png" already exists, do you wish to overwrite it [y/n]?
y
Figure was overwritten.
"""_____no_output_____

## Results
The blue lines in the figures shows the desired output if the models were perfect $\hat{y} = y$. The red dots shows the model predictions: $\hat{y}$ vs. $y$.

### Ridge Regression

![Ridge Regression](figures/ridge.png)

### LASSO Regression

![LASSO Regression](figures/lasso.png)_____no_output_____## Discussion
The first notable feature which is shown is that Ridge regression provides a better $R^2$-score than the Lasso regression.

Both models fail to predict values in the same range as the true CADi values. The true values are in the range $[20,100]$, whereas Ridge regression only provides predictions in the range of roughly $[38,59]$ and LASSO only provides predictions in the range of roughly $[44,52]$.

Ridge regression has a higher variance of $\hat{y}$ than LASSO regression and this might also be the problem with this model and this data set: Ridge regression seems to suffer from a too high variance. If this is true, the model complexity is too high, and the *training set* is overfitted, hence do these parameters not fit the *test set* very well.

LASSO regression however has a much lower variance, and it seem to have a very small output range. In the figure with $\hat{y}$ vs. $y$, the correlation is near linear, but not in the right direction. If the model was perfect, all the predictions would be linear and on top on the blue line (which is $\hat{y}=y$). In this case the predictions appear almost at a flat line $\hat{y} = const. + \sigma$, and this is an indication that this model suffers from a high bias. If this is true, LASSO fails to find a correlation between the gene data and CADi by using this data set. It is also notable that the number of data points (110) might be too low compared to the number of features (22,283)._____no_output_____# Exercise 2
Consider projection pursuit. Derive the expressions for $w$ which minimizes the linearized expression for the object function
$$
S = \sum_{i=1}^N g'(w_{old}^T x_i)^2 \left( \frac{y_i - g(w_{old}^T x_i)}{g'(w_{old}^T x_i)} + w_{old}^T x_i - w^Tx_i \right)^2
$$
In other words, we wish to minimize $S$ wrt. $w$. First, let's try to rewrite this expression to linear equation by getting rid of the summation sign. It is convenient to identify:
* which of the parameters are scalars: $y_i$, $g$ and $g$.
* which of the parameters are vectors: $x_i$, $w_{old}$ and $w$ must be vectors of length p.

Now, lets rename some of the expressions in the sum, in order to simplify the problem:

$$
a_i = g'(w_{old}^T x_i)^2
$$

$$
b_i = \frac{y_i - g(w_{old}^T x_i)}{g'(w_{old}^T x_i)} + w_{old}^T x_i
$$

Hence the equation is now
$$
S = \sum_{i=1}^N  a_i \left( b_i - w^Tx_i \right)^2,
$$

The output must be a scalar, therefore

$$
S = \sum_{i=1}^N  a_i \left( b_i - w^Tx_i \right)^2 = (b^T - w^TX^T) A (b^T - w^T X^T)^T
$$

where A is the diagonal matrix with the $a$ along its diagonal. The matrix $X$ now contains N rows of $x_i$. Further:

$$
(b^T - w^TX^T) A (b - Xw) = b^TAb - b^TAXw - w^TX^TAb + w^TX^TAXw
$$

Now lets confirm that all these terms are indeed scalars:

$$
b^TAb \rightarrow (1\times N)(N\times N)(N \times 1)
$$
$$
b^TAXw \rightarrow (1 \times N)(N \times N)(N \times p)( p \times 1)
$$
$$
w^TX^TAb \rightarrow (1\times p)(p \times N)(N \times N)(N \times 1)
$$
$$
w^TX^TAXw \rightarrow (1 \times p)(p \times N)(N \times N)(N \times p) (p \times 1)
$$

Since all terms start and end with 1, the products are $(1\times 1)$ which are scalars.

Notice that the the second term, transposed is $(b^TAXw)^T = w^TX^TAb$ which is the third term. Since the transpose of a scalar yields the same scalar these two terms are identical. The expression is now

$$
S = b^TAb - 2b^TAXw + (Xw)^TAXw
$$

Now, to obtain the minimum, we wish to differentiate wrt. $w$ and set this equal to 0:

$$
\frac{\partial }{\partial w} \left(b^TAb - 2b^TAXw + (Xw)^TAXw\right) = 0
$$
$$
- 2b^TAX + 2X^T A X w = 0
$$

Now, the first term is $(1\times p)$ and the second term is $(p \times n)$, I therefore transpose the first term to get the expressions on the same form:

$$
b^TAX = X^T A X w
$$
$$
X^T A b = X^T A X w
$$
$$
w = (X^TAX)^{-1} X^T A b
$$

Hence the solution is

$$
\underset{w}{\mathrm{argmin}} (S) = \left(X^TAX\right)^{-1}X^TAb
$$_____no_output_____


# Notebook from ulupo/ColabFold
Path: AlphaFold2.ipynb

_____no_output_____#ColabFold: AlphaFold2 w/ MMseqs2

Easy to use AlphaFold2 protein structure [(Jumper et al. 2021)](https://www.nature.com/articles/s41586-021-03819-2) and complex [(Evans et al. 2021)](https://www.biorxiv.org/content/10.1101/2021.10.04.463034v1) prediction using multiple sequence alignments generated through MMseqs2. For details, refer to our manuscript:

[Mirdita M, Schütze K, Moriwaki Y, Heo L, Ovchinnikov S, Steinegger M. ColabFold - Making protein folding accessible to all.
*bioRxiv*, 2021](https://www.biorxiv.org/content/10.1101/2021.08.15.456425v2)

- This notebook provides basic functionality for **protein structure (Alphafold2)** and **complex prediction (Alphafold2-multimer)**. Advanced features such as recycles, sampling, ... ->[advanced notebook](https://colab.research.google.com/github/sokrypton/ColabFold/blob/main/beta/AlphaFold2_advanced.ipynb).

Old versions: [v1.0-alpha](https://colab.research.google.com/github/sokrypton/ColabFold/blob/v1.0-alpha/AlphaFold2.ipynb), [v1.1-permultimer](https://colab.research.google.com/github/sokrypton/ColabFold/blob/v1.1-premultimer/AlphaFold2.ipynb), [v1.2](https://colab.research.google.com/github/sokrypton/ColabFold/blob/v1.2.0/AlphaFold2.ipynb)

For more details, see **bottom** of the notebook and checkout the **[ColabFold GitHub](https://github.com/sokrypton/ColabFold)**._____no_output_____

#@title Input protein sequence, then hit `Runtime` -> `Run all`
from google.colab import files
import os.path
import re
import hashlib

def add_hash(x,y):
  return x+"_"+hashlib.sha1(y.encode()).hexdigest()[:5]

query_sequence = 'PIAQIHILEGRSDEQKETLIREVSEAISRSLDAPLTSVRVIITEMAKGHFGIGGELASK' #@param {type:"string"}
#@markdown  - Use `:` to specify inter-protein chainbreaks for **modeling complexes** (supports homo- and hetro-oligomers). For example **PI...SK:PI...SK** for a mono-dimer

# remove whitespaces
query_sequence = "".join(query_sequence.split())

jobname = 'test5' #@param {type:"string"}
# remove whitespaces
jobname = "".join(jobname.split())
jobname = re.sub(r'\W+', '', jobname)
jobname = add_hash(jobname, query_sequence)

with open(f"{jobname}.csv", "w") as text_file:
    text_file.write(f"id,sequence\n{jobname},{query_sequence}")

queries_path=f"{jobname}.csv"

# number of models to use
#@markdown ---
#@markdown ### Advanced settings
msa_mode = "MMseqs2 (UniRef+Environmental)" #@param ["MMseqs2 (UniRef+Environmental)", "MMseqs2 (UniRef only)","single_sequence","custom"]
model_type = "auto" #@param ["auto", "AlphaFold2-ptm", "AlphaFold2-multimer"]
#@markdown  - auto = protein structure prediction using "AlphaFold2-ptm" and complex prediction "AlphaFold-multimer". For complexes "AlphaFold-multimer" and "AlphaFold-ptm" can be used.

num_models = 5 #@param [1,2,3,4,5] {type:"raw"}
num_recycles = 3 #@param [1,3,6,12,24,48] {type:"raw"}
use_amber = False #@param {type:"boolean"}
use_templates = False #@param {type:"boolean"}

#@markdown ---
#@markdown ### Experimental options
save_to_google_drive = False #@param {type:"boolean"}
#@markdown ---
#@markdown Don't forget to hit `Runtime` -> `Run all` after updating the form.

# decide which a3m to use
if msa_mode.startswith("MMseqs2"):
  a3m_file = f"{jobname}.a3m"
elif msa_mode == "custom":
  a3m_file = f"{jobname}.custom.a3m"
  if not os.path.isfile(a3m_file):
    custom_msa_dict = files.upload()
    custom_msa = list(custom_msa_dict.keys())[0]
    header = 0
    import fileinput
    for line in fileinput.FileInput(custom_msa,inplace=1):
      if line.startswith(">"):
         header = header + 1
      if not line.rstrip():
        continue
      if line.startswith(">") == False and header == 1:
         query_sequence = line.rstrip()
      print(line, end='')

    os.rename(custom_msa, a3m_file)
    queries_path=a3m_file
    print(f"moving {custom_msa} to {a3m_file}")
else:
  a3m_file = f"{jobname}.single_sequence.a3m"
  with open(a3m_file, "w") as text_file:
    text_file.write(">1\n%s" % query_sequence)

if save_to_google_drive:
  from pydrive.drive import GoogleDrive
  from pydrive.auth import GoogleAuth
  from google.colab import auth
  from oauth2client.client import GoogleCredentials
  auth.authenticate_user()
  gauth = GoogleAuth()
  gauth.credentials = GoogleCredentials.get_application_default()
  drive = GoogleDrive(gauth)
  print("You are logged into Google Drive and are good to go!")_____no_output_____#@title Install dependencies
%%bash -s $use_amber $use_templates

set -e

USE_AMBER=$1
USE_TEMPLATES=$2

if [ ! -f COLABFOLD_READY ]; then
  # install dependencies
  # We have to use "--no-warn-conflicts" because colab already has a lot preinstalled with requirements different to ours
  pip install --no-warn-conflicts -q "colabfold[alphafold] @ git+https://github.com/sokrypton/ColabFold"
  touch COLABFOLD_READY
fi

# setup conda
if [ ${USE_AMBER} == "True" ] || [ ${USE_TEMPLATES} == "True" ]; then
  if [ ! -f CONDA_READY ]; then
    wget -qnc https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    bash Miniconda3-latest-Linux-x86_64.sh -bfp /usr/local 2>&1 1>/dev/null
    rm Miniconda3-latest-Linux-x86_64.sh
    touch CONDA_READY
  fi
fi
# setup template search
if [ ${USE_TEMPLATES} == "True" ] && [ ! -f HH_READY ]; then
  conda install -y -q -c conda-forge -c bioconda kalign3=3.2.2 hhsuite=3.3.0 python=3.7 2>&1 1>/dev/null
  touch HH_READY
fi
# setup openmm for amber refinement
if [ ${USE_AMBER} == "True" ] && [ ! -f AMBER_READY ]; then
  conda install -y -q -c conda-forge openmm=7.5.1 python=3.7 pdbfixer 2>&1 1>/dev/null
  wget -qnc https://raw.githubusercontent.com/deepmind/alphafold/main/docker/openmm.patch
  (cd /usr/local/lib/python3.7/site-packages; patch -s -p0 < /content/openmm.patch)
  wget -qnc https://git.scicore.unibas.ch/schwede/openstructure/-/raw/7102c63615b64735c4941278d92b554ec94415f8/modules/mol/alg/src/stereo_chemical_props.txt
  touch AMBER_READY
fi_____no_output_____#@title Run Prediction

import sys

from colabfold.download import download_alphafold_params, default_data_dir
from colabfold.utils import setup_logging
from colabfold.batch import get_queries, run, set_model_type

from pathlib import Path

# For some reason we need that to get pdbfixer to import
if use_amber and '/usr/local/lib/python3.7/site-packages/' not in sys.path:
    sys.path.insert(0, '/usr/local/lib/python3.7/site-packages/')

result_dir="."
setup_logging(Path(".").joinpath("log.txt"))
queries, is_complex = get_queries(queries_path)
model_type = set_model_type(is_complex, model_type)
download_alphafold_params(model_type, Path("."))
run(
    queries=queries,
    result_dir=result_dir,
    use_templates=use_templates,
    use_amber=use_amber,
    msa_mode=msa_mode,
    model_type=model_type,
    num_models=num_models,
    num_recycles=num_recycles,
    model_order=[1, 2, 3, 4, 5],
    is_complex=is_complex,
    data_dir=Path("."),
    keep_existing_results=False,
    recompile_padding=1.0,
    rank_mode="auto",
    pair_mode="unpaired+paired",
    stop_at_score=float(100),
)_____no_output_____#@title Display 3D structure {run: "auto"}
import py3Dmol
import glob
import matplotlib.pyplot as plt
rank_num = 1 #@param ["1", "2", "3", "4", "5"] {type:"raw"}
color = "lDDT" #@param ["chain", "lDDT", "rainbow"]
show_sidechains = False #@param {type:"boolean"}
show_mainchains = False #@param {type:"boolean"}

jobname_prefix = ".custom" if msa_mode == "custom" else ""
if use_amber:
  pdb_filename = f"{jobname}{jobname_prefix}_relaxed_model_*_rank_{rank_num}.pdb"
else:
  pdb_filename = f"{jobname}{jobname_prefix}_unrelaxed_model_*_rank_{rank_num}.pdb"

pdb_file = glob.glob(pdb_filename)

def show_pdb(rank_num=1, show_sidechains=False, show_mainchains=False, color="lDDT"):
  model_name = f"rank_{rank_num}"
  if use_amber:
    pdb_filename = f"{jobname}{jobname_prefix}_relaxed_{model_name}.pdb"
  else:
    pdb_filename = f"{jobname}{jobname_prefix}_unrelaxed_{model_name}.pdb"

  view = py3Dmol.view(js='https://3dmol.org/build/3Dmol.js',)
  view.addModel(open(pdb_file[0],'r').read(),'pdb')

  if color == "lDDT":
    view.setStyle({'cartoon': {'colorscheme': {'prop':'b','gradient': 'roygb','min':50,'max':90}}})
  elif color == "rainbow":
    view.setStyle({'cartoon': {'color':'spectrum'}})
  elif color == "chain":
    for n,chain,color in zip(range(homooligomer),list("ABCDEFGH"),
                     ["lime","cyan","magenta","yellow","salmon","white","blue","orange"]):
       view.setStyle({'chain':chain},{'cartoon': {'color':color}})
  if show_sidechains:
    BB = ['C','O','N']
    view.addStyle({'and':[{'resn':["GLY","PRO"],'invert':True},{'atom':BB,'invert':True}]},
                        {'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
    view.addStyle({'and':[{'resn':"GLY"},{'atom':'CA'}]},
                        {'sphere':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
    view.addStyle({'and':[{'resn':"PRO"},{'atom':['C','O'],'invert':True}]},
                        {'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})
  if show_mainchains:
    BB = ['C','O','N','CA']
    view.addStyle({'atom':BB},{'stick':{'colorscheme':f"WhiteCarbon",'radius':0.3}})

  view.zoomTo()
  return view

show_pdb(rank_num,show_sidechains, show_mainchains, color).show()

_____no_output_____#@title Plots {run: "auto"}
from IPython.display import display, HTML
import base64
from html import escape

# see: https://stackoverflow.com/a/53688522
def image_to_data_url(filename):
  ext = filename.split('.')[-1]
  prefix = f'data:image/{ext};base64,'
  with open(filename, 'rb') as f:
    img = f.read()
  return prefix + base64.b64encode(img).decode('utf-8')

pae = image_to_data_url(f"{jobname}{jobname_prefix}_PAE.png")
cov = image_to_data_url(f"{jobname}{jobname_prefix}_coverage.png")
plddt = image_to_data_url(f"{jobname}{jobname_prefix}_plddt.png")
display(HTML(f"""

Plots for {escape(jobname)}

"""))
_____no_output_____#@title Package and download results
#@markdown If you are having issues downloading the result archive, try disabling your adblocker and run this cell again. If that fails click on the little folder icon to the left, navigate to file: `jobname.result.zip`, right-click and select \"Download\" (see [screenshot](https://pbs.twimg.com/media/E6wRW2lWUAEOuoe?format=jpg&name=small)).

if msa_mode == "custom":
  print("Don't forget to cite your custom MSA generation method.")

!zip -FSr $jobname".result.zip" config.json $jobname".a3m" $jobname*"relaxed_model_"*".pdb" $jobname*"_coverage_lDDT.png" "cite.bibtex" $jobname*".png"
files.download(f"{jobname}.result.zip")

if save_to_google_drive == True and drive:
  uploaded = drive.CreateFile({'title': f"{jobname}.result.zip"})
  uploaded.SetContentFile(f"{jobname}.result.zip")
  uploaded.Upload()
  print(f"Uploaded {jobname}.result.zip to Google Drive with ID {uploaded.get('id')}")_____no_output_____

# Instructions
**Quick start**
1. Paste your protein sequence(s) in the input field.
2. Press "Runtime" -> "Run all".
3. The pipeline consists of 5 steps. The currently running steps is indicated by a circle with a stop sign next to it.

**Result zip file contents**

1. PDB formatted structures sorted by avg. pIDDT and complexes are sorted by pTMscore. (unrelaxed and relaxed if `use_amber` is enabled).
2. Plots of the model quality.
3. Plots of the MSA coverage.
4. Parameter log file.
5. A3M formatted input MSA.
6. BibTeX file with citations for all used tools and databases.

At the end of the job a download modal box will pop up with a `jobname.result.zip` file. Additionally, if the `save_to_google_drive` option was selected, the `jobname.result.zip` will be uploaded to your Google Drive.

**MSA generation for complexes**

For the complex prediction we use unpaired and paired MSAs. Unpaired MSA is generated the same way as for the protein structures prediction by searching the UniRef100 and environmental sequences three iterations each.

The paired MSA is generated by searching the UniRef100 database and pairing the best hits sharing the same NCBI taxonomical identifier (=species or sub-species). We only pair sequences if all of the query sequences are present for the respective taxonomical identifier.

**Using a custom MSA as input**

To predict the structure with a custom MSA (A3M formatted): (1) Change the msa_mode: to "custom", (2) Wait for an upload box to appear at the end of the "Input Protein ..." box. Upload your A3M. The first fasta entry of the A3M must be the query sequence without gaps.

As an alternative for MSA generation the [HHblits Toolkit server](https://toolkit.tuebingen.mpg.de/tools/hhblits) can be used. After submitting your query, click "Query Template MSA" -> "Download Full A3M". Download the A3M file and upload it in this notebook.

**Comparision to the full AlphaFold2 and Alphafold2 colab**

This notebook replaces the homology detection and MSA pairing of AlphaFold2 with MMseqs2. For a comparision against the [AlphaFold2 Colab](https://colab.research.google.com/github/deepmind/alphafold/blob/main/notebooks/AlphaFold.ipynb) and the full [AlphaFold2](https://github.com/deepmind/alphafold) system read our [preprint](https://www.biorxiv.org/content/10.1101/2021.08.15.456425v1).

**Troubleshooting**
* Check that the runtime type is set to GPU at "Runtime" -> "Change runtime type".
* Try to restart the session "Runtime" -> "Factory reset runtime".
* Check your input sequence.

**Known issues**
* Google Colab assigns different types of GPUs with varying amount of memory. Some might not have enough memory to predict the structure for a long sequence.
* Your browser can block the pop-up for downloading the result file. You can choose the `save_to_google_drive` option to upload to Google Drive instead or manually download the result file: Click on the little folder icon to the left, navigate to file: `jobname.result.zip`, right-click and select \"Download\" (see [screenshot](https://pbs.twimg.com/media/E6wRW2lWUAEOuoe?format=jpg&name=small)).

**Limitations**
* Computing resources: Our MMseqs2 API can handle ~20-50k requests per day.
* MSAs: MMseqs2 is very precise and sensitive but might find less hits compared to HHblits/HMMer searched against BFD or Mgnify.
* We recommend to additionally use the full [AlphaFold2 pipeline](https://github.com/deepmind/alphafold).

**Description of the plots**
*   **Number of sequences per position** - We want to see at least 30 sequences per position, for best performance, ideally 100 sequences.
*   **Predicted lDDT per position** - model confidence (out of 100) at each position. The higher the better.
*   **Predicted Alignment Error** - For homooligomers, this could be a useful metric to assess how confident the model is about the interface. The lower the better.

**Bugs**
- If you encounter any bugs, please report the issue to https://github.com/sokrypton/ColabFold/issues

**Acknowledgments**
- We thank the AlphaFold team for developing an excellent model and open sourcing the software.

- [Söding Lab](https://www.mpibpc.mpg.de/soeding) for providing the computational resources for the MMseqs2 server

- Richard Evans for helping to bechmark the ColabFold's Alphafold-multimer support

- [David Koes](https://github.com/dkoes) for his awesome [py3Dmol](https://3dmol.csb.pitt.edu/) plugin, without whom these notebooks would be quite boring!

- Do-Yoon Kim for creating the ColabFold logo.

- A colab by Sergey Ovchinnikov ([@sokrypton](https://twitter.com/sokrypton)), Milot Mirdita ([@milot_mirdita](https://twitter.com/milot_mirdita)) and Martin Steinegger ([@thesteinegger](https://twitter.com/thesteinegger)).
_____no_output_____


# Notebook from mporritt/ttvpack
Path: notebooks/Demo.ipynb

<code>
from retrieval import search_target, get_target
from fitting import fit_target_ttsWARNING (aesara.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
# Parameters for star k2-146
epic = 211924657
period = [2.64460, 4.00498] # Periods of known planets_____no_output_____search_target(epic=epic)_____no_output_____lightcurves = get_target(epic=epic)The following products were selected for download:
SearchResult containing 5 data products.

 #     mission     year  author exptime  target_name  distance
                                   s                   arcsec
--- -------------- ---- ------- ------- ------------- --------
  0 K2 Campaign 05 2015 EVEREST    1800 ktwo211924657      0.0
  1 K2 Campaign 16 2017 EVEREST    1800 ktwo211924657      0.0
  2 K2 Campaign 18 2018 EVEREST    1800 ktwo211924657      0.0
  3 TESS Sector 44 2021 TESScut     475     211924657      0.0
  4 TESS Sector 46 2021 TESScut     475     211924657      0.0

Most recent sector available =  46
This sector isn't available yet.
Tried to download TESS sectors [44, 46], but only sectors [] are available according to tess-point.
Resolving to download sectors [].
model = fit_target_tts(lightcurves, period=period)Fitting a timing model for data set 0...
BLS found signal with t0=2308.99, period=2.6454, num_transits=28
BLS found signal with t0=2308.70, period=3.9062, num_transits=19
model.plot_ttvs()_____no_output_____
</code>


# Notebook from chuktuk/Alzheimers_Disease_Analysis
Path: zz2-Data_Storytelling_Raw-Copy1.ipynb

# Data Storytelling

## ADNI Alzheimer's Data_____no_output_____

# import standard packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
from datetime import timedelta

import eda

# display matplotlib inline
%matplotlib inline

# import wrangle_adni to import/wrangle the data
from adnidatawrangling import wrangle_adni

# set defaults for sns
sns.set()_____no_output_____# import data, clean, and extract data
adni_comp, clin_data, scan_data = wrangle_adni()_____no_output_____adni_comp.shape_____no_output_____adni_comp.info()<class 'pandas.core.frame.DataFrame'>
Int64Index: 4672 entries, 3 to 5292
Data columns (total 30 columns):
PTGENDER              4672 non-null category
DX                    4672 non-null category
DX_bl                 4672 non-null category
EXAMDATE              4672 non-null datetime64[ns]
EXAMDATE_bl           4672 non-null datetime64[ns]
PTEDUCAT              4672 non-null int32
Month                 4672 non-null int32
AGE                   4672 non-null float64
CDRSB                 4672 non-null float64
CDRSB_bl              4672 non-null float64
ADAS11                4672 non-null float64
ADAS11_bl             4672 non-null float64
ADAS13                4672 non-null float64
ADAS13_bl             4672 non-null float64
MMSE                  4672 non-null float64
MMSE_bl               4672 non-null float64
RAVLT_immediate       4672 non-null float64
RAVLT_immediate_bl    4672 non-null float64
Hippocampus           4672 non-null float64
Hippocampus_bl        4672 non-null float64
Ventricles            4672 non-null float64
Ventricles_bl         4672 non-null float64
WholeBrain            4672 non-null float64
WholeBrain_bl         4672 non-null float64
Entorhinal            4672 non-null float64
Entorhinal_bl         4672 non-null float64
MidTemp               4672 non-null float64
MidTemp_bl            4672 non-null float64
AGE_dynamic           4672 non-null float64
DX_bl2                4672 non-null object
dtypes: category(3), datetime64[ns](2), float64(22), int32(2), object(1)
memory usage: 999.6+ KB
adni_comp.head()_____no_output_____

## Questions
1. Distribution of different diagnoses for baseline, subsequent exams, and change
2. Cognitive tests distributions for baseline, subsequent exams, and change
3. Scan data change over time
4. Correlations with cognitive tests and brain scans
5. Correlations with cognitive tests and diagnoses
6. Correlations with brain scans and diagnoses
7. Change in biomarkers with change in diagnosis
8. Differences by sex/education
9. Age as a factor
10. Split by diagnosis, sex, education vs. lumping together_____no_output_____

# subset only the basline data from the complete dataset

adni_bl = adni_comp[adni_comp.EXAMDATE == adni_comp.EXAMDATE_bl]
clin_bl = clin_data[clin_data.EXAMDATE == clin_data.EXAMDATE_bl]
scan_bl = scan_data[scan_data.EXAMDATE == scan_data.EXAMDATE_bl]_____no_output_____# distribution of baseline diagnoses for complete clinical data

adni_comp_DXbl_counts = adni_bl.groupby('DX_bl')['DX_bl'].count()_____no_output_____adni_comp_DXbl_counts_____no_output_____# combine SMC + CN, combine EMCI + LMCI
# subjective memory complaints were categorized as cognitively normal
# this was based on literature suggesting SMC is associate with mood vs impairment

#CN_bl = adni_comp_DXbl_counts['CN'] + adni_comp_DXbl_counts['SMC']
#MCI_bl = adni_comp_DXbl_counts['EMCI'] + adni_comp_DXbl_counts['LMCI']
#AD_bl = adni_comp_DXbl_counts['AD']
DX_bl = adni_comp.groupby('RID')['DX_bl'].min()
DX_bl_counts = DX_bl.value_counts()_____no_output_____DX_bl_counts_____no_output_____sns.barplot(x=DX_bl_counts.index, y=DX_bl_counts.values)
plt.xlabel('Diagnosis')
plt.ylabel('Number of Patients')
plt.title('Baseline Diagnosis')_____no_output_____# filter the most advanced diagnosis for each patient

dx_final = adni_comp.groupby('RID')['DX'].max()_____no_output_____dx_counts = dx_final.value_counts()_____no_output_____dx_counts_____no_output_____sns.barplot(x=dx_counts.index, y=dx_counts.values)
plt.xlabel('Diagnosis')
plt.ylabel('Number of Patients')
plt.title('Final Diagnosis')_____no_output_____DX_bl2 = adni_comp.groupby('RID')['DX_bl2'].min()
DX_bl2_counts = DX_bl2.value_counts()_____no_output_____DX_bl2_counts_____no_output_____# join baseline and final diagnosis data for comparison

counts = [DX_bl2_counts[1], DX_bl2_counts[0], DX_bl2_counts[2], dx_counts[2], dx_counts[1], dx_counts[0]]

dx_comp = {'visit': ['Baseline', 'Baseline', 'Baseline', 'Final', 'Final', 'Final'],
           'diagnosis': ['CN', 'MCI', 'AD', 'CN', 'MCI', 'AD'],
          'counts': counts}_____no_output_____dx_comp_____no_output_____diag = pd.DataFrame(dx_comp)_____no_output_____diag_____no_output_____sns.barplot(x='diagnosis', y='counts', hue='visit', data=diag)
plt.xlabel('Diagnosis')
plt.ylabel('Number of Patients')
plt.title('Diagnosis Counts: Baseline vs. Final')
plt.legend(loc='upper left')_____no_output_____# diagnosis change
bl = diag[diag['visit'] == 'Baseline']
final = diag[diag['visit'] == 'Final']_____no_output_____change = final['counts'].values - bl['counts'].values _____no_output_____sns.barplot(x=['CN', 'MCI', 'AD'], y=change)
plt.xlabel('Diagnosis')
plt.ylabel('Change in Diagnosis')
plt.title('Change in Diagnosis Counts: Baseline to Final')_____no_output_____adni_comp.head()_____no_output_____# get last visit info

adni_comp.reset_index(inplace=True)_____no_output_____adni_comp.set_index([adni_comp.RID, adni_comp.EXAMDATE], inplace=True)_____no_output_____RID_list = adni_comp.RID.unique()_____no_output_____RID_list_____no_output_____adni_comp.drop('RID', axis='columns', inplace=True)_____no_output_____last_exam = adni_comp.groupby('RID')['EXAMDATE'].max()_____no_output_____last_exam[3]_____no_output_____data_dict = {}_____no_output_____adni_comp.shape[0]

for i in range(len(RID_list)):
    #list_data.append(adni_comp.loc[(RID_list[i], str(last_exam[RID_list[i]]))])
    data_dict[RID_list[i]] = adni_comp.loc[(RID_list[i], str(last_exam[RID_list[i]]))]_____no_output_____final_exam = pd.DataFrame(data_dict)_____no_output_____final_exam = final_exam.transpose()_____no_output_____final_exam.head()_____no_output_____final_exam.shape_____no_output_____# compare initial diagnosis with final diagnosis

cn_cn = final_exam[(final_exam.DX == 'CN') & (final_exam.DX_bl2 == 'CN')]
cn_mci = final_exam[(final_exam.DX == 'MCI') & (final_exam.DX_bl2 == 'CN')]
cn_ad = final_exam[(final_exam.DX == 'AD') & (final_exam.DX_bl2 == 'CN')]
mci_cn = final_exam[(final_exam.DX == 'CN') & (final_exam.DX_bl2 == 'MCI')]
mci_mci = final_exam[(final_exam.DX == 'MCI') & (final_exam.DX_bl2 == 'MCI')]
mci_ad = final_exam[(final_exam.DX == 'AD') & (final_exam.DX_bl2 == 'MCI')]
ad_cn = final_exam[(final_exam.DX == 'CN') & (final_exam.DX_bl2 == 'AD')]
ad_mci = final_exam[(final_exam.DX == 'MCI') & (final_exam.DX_bl2 == 'AD')]
ad_ad = final_exam[(final_exam.DX == 'AD') & (final_exam.DX_bl2 == 'AD')]_____no_output_____# create a dictionary and convert to a dataframe

diag_dict = {'CN': [cn_cn.shape[0], cn_mci.shape[0], cn_ad.shape[0]],
             'MCI': [mci_cn.shape[0], mci_mci.shape[0], mci_ad.shape[0]],
             'AD': [ad_cn.shape[0], ad_mci.shape[0], ad_ad.shape[0]]}

diag_df = pd.DataFrame(diag_dict)_____no_output_____diag_df_____no_output_____# extract the bar heights

ad_final = [diag_df.loc[:,'CN'].sum(), diag_df.loc[:,'MCI'].sum(), diag_df.loc[:,'AD'].sum()]
mci_final = [diag_df.loc[0:1, 'CN'].sum(), diag_df.loc[0:1, 'MCI'].sum(), diag_df.loc[0:1, 'AD'].sum()]
cn_final = [diag_df.loc[0, 'CN'], diag_df.loc[0, 'MCI'], diag_df.loc[0, 'AD']]_____no_output_____# contruct the plot

sns.barplot(x=['CN', 'MCI', 'AD'], y=ad_final, color='indianred', label='AD')
sns.barplot(x=['CN', 'MCI', 'AD'], y=mci_final, color='limegreen', label='MCI')
sns.barplot(x=['CN', 'MCI', 'AD'], y=cn_final, color='royalblue', label='CN')
plt.xlabel('Baseline Diagnosis')
plt.ylabel('Number of Patients')
plt.title('Final Diagnosis from Baseline Diagnosis')
plt.legend(title='Final Diagnosis')_____no_output_____cn_stayed = final_exam[(final_exam.DX == 'CN') & (final_exam.DX_bl == 'CN')]_____no_output_____# 42 CN GOT WORSE AND 294 STAYED NORMAL

cn_stayed.shape_____no_output_____cn = final_exam[(final_exam['DX_bl'] == 'CN') | (final_exam['DX_bl'] == 'SMC')]_____no_output_____cn.shape_____no_output_____cn_gotworse = cn[cn['DX'] != 'CN']_____no_output_____cn_gotworse.shape_____no_output_____cn_nochange = cn.shape[0] - cn_gotworse.shape[0]_____no_output_____cn_to_mci = cn_gotworse[cn_gotworse['DX'] == 'MCI']
cn_to_ad = cn_gotworse[cn_gotworse['DX'] == 'AD']_____no_output_____sns.barplot(x=['No Change', '+MCI', '+AD'], y=[cn_nochange, cn_to_mci.shape[0], cn_to_ad.shape[0]])
plt.xlabel('Change in Diagnosis')
plt.ylabel('Number of Patients')
plt.title('Final Diagnosis from CN Baseline Patients')_____no_output_____mci = final_exam[(final_exam['DX_bl'] == 'LMCI') | (final_exam['DX_bl'] == 'EMCI')]_____no_output_____mci_gotworse = mci[mci['DX'] == 'AD']_____no_output_____mci_gotbetter = mci[mci['DX'] == 'CN']_____no_output_____mci.shape_____no_output_____mci_nochange = mci.shape[0] - mci_gotbetter.shape[0] - mci_gotworse.shape[0]_____no_output_____mci_gotbetter.shape_____no_output_____sns.barplot(x=['Got Better', 'No Change', '+AD'], y=[mci_gotbetter.shape[0], mci_nochange, mci_gotworse.shape[0]])
plt.xlabel('Change in Diagnosis')
plt.ylabel('Number of Patients')
plt.title('Final Diagnosis from MCI Baseline Patients')_____no_output_____#clin_bl
#clin_sub_____no_output_____clin_bl.columns_____no_output_____clin_bl['PTGENDER'].value_counts().plot(kind='bar', rot=0)
plt.xlabel('Gender')
plt.ylabel('Number of Patients')
plt.title('Clinical Data: Patient Gender')_____no_output_____scan_bl['PTGENDER'].value_counts().plot(kind='bar', rot=0)
plt.xlabel('Gender')
plt.ylabel('Number of Patients')
plt.title('Scan Data: Patient Gender')_____no_output_____# view clinical pairplot to find potential correlations
# figure copy/pasted into image editor to zoom and view more closely

sns.pairplot(clin_sub, hue='DX')_____no_output_____

#### Pairplot Notes: Clinical Data
- Education has very little/no correlation with any other factor
- Dynamic_age has very little/no correlation with other factors
- CSRSB correlations
    - ADAS11, ADAS13 +
    - MMSE, RAVLT & RAVLT_bl -
- ADAS11 & ADAS13 tightly + corr
- MMSE & ADAS11/13 - corr

#### Baseline data correlations with later exams
- Baseline measures correlate more strongly with other baseline measures than subsequent measures
    - ADAS13 negative corr with MMSE/MMSE_bl, RAVLT_immediate/RAVLT_immediate_bl
- Baseline MMSE low correlation with clinical signs in later exams
- Baseline RAVLT_immediate is a little better
- ADAS11 & 13 some correlation with later exam data_____no_output_____

# redundant, added to wrangling script

# map new column for DX_bl to categorize based on subsequent DX categories
#mapper = {'SMC': 'CN', 'LMCI': 'MCI', 'EMCI': 'MCI', 'CN': 'CN', 'AD': 'AD'}
#final_exam.loc[:, 'DX_bl2'] = final_exam.DX_bl.map(mapper)_____no_output_____# baseline ADAS11 by diagnosis

sns.boxplot(x='DX_bl2', y='ADAS11_bl', hue='DX_bl2', data=clin_bl)
plt.xlabel('Baseline Diagnosis')
plt.ylabel('ADAS11')
plt.title('Baseline ADAS11 by Diagnosis')
plt.legend(loc='upper left')_____no_output_____# baseline ADAS13 by diagnosis

sns.boxplot(x='DX_bl2', y='ADAS13_bl', hue='DX_bl2', data=clin_bl)
plt.xlabel('Baseline Diagnosis')
plt.ylabel('ADAS13')
plt.title('Baseline ADAS13 by Diagnosis')
plt.legend(loc='upper left')_____no_output_____# find changes in CDRSB, ADAS11, ADAS13, MMSE, RAVLT_immediate,
# Hippocampus, Ventricles, WholeBrain, Entorhinal, Midtemp

final_exam.loc[:, 'CDRSB_delta'] = final_exam.CDRSB - final_exam.CDRSB_bl
final_exam.loc[:, 'ADAS11_delta'] = final_exam.ADAS11 - final_exam.ADAS11_bl
final_exam.loc[:, 'ADAS13_delta'] = final_exam.ADAS13 - final_exam.ADAS13_bl
final_exam.loc[:, 'MMSE_delta'] = final_exam.MMSE - final_exam.MMSE_bl
final_exam.loc[:, 'RAVLT_delta'] = final_exam.RAVLT_immediate - final_exam.RAVLT_immediate_bl
final_exam.loc[:, 'Hippocampus_delta'] = final_exam.Hippocampus - final_exam.Hippocampus_bl
final_exam.loc[:, 'Ventricles_delta'] = final_exam.Ventricles - final_exam.Ventricles_bl
final_exam.loc[:, 'WholeBrain_delta'] = final_exam.WholeBrain - final_exam.WholeBrain_bl
final_exam.loc[:, 'Entorhinal_delta'] = final_exam.Entorhinal - final_exam.Entorhinal_bl
final_exam.loc[:, 'MidTemp_delta'] = final_exam.MidTemp - final_exam.MidTemp_bl_____no_output_____final_exam.head()_____no_output_____# ADAS11 change separated by change in diagnosis
no_change = final_exam[final_exam['DX'] == final_exam['DX_bl2']]
cn_ad = final_exam[(final_exam['DX'] == 'AD') & (final_exam['DX_bl2'] == 'CN')]
mci_ad = final_exam[(final_exam['DX'] == 'AD') & (final_exam['DX_bl2'] == 'MCI')]
cn_mci = final_exam[(final_exam['DX'] == 'MCI') & (final_exam['DX_bl2'] == 'CN')]_____no_output_____# calculate the number of observations to use in calcuting number of bins for histograms

no_change_bins = int(np.sqrt(no_change.shape[0]))
cn_ad_bins = int(np.sqrt(cn_ad.shape[0]))
mci_ad_bins = int(np.sqrt(mci_ad.shape[0]))
cn_mci_bins = int(np.sqrt(cn_mci.shape[0]))_____no_output_____# distribution plot for CDRSB change

sns.distplot(no_change.CDRSB_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.CDRSB_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.CDRSB_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.CDRSB_delta.values, hist=False, label='CN to AD')

plt.xlabel('CDRSB Change')
plt.ylabel('Kernel Density Estimate')
plt.title('CDRSB Change by Change in Diagnosis')
plt.legend(loc='upper right')_____no_output_____# CDRSB change histograms

plt.hist(no_change.CDRSB_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.CDRSB_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.CDRSB_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.5, color='green', label='MCI to AD')
plt.hist(cn_ad.CDRSB_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in CDRSB')
plt.ylabel('Probability Density')
plt.title('Change in CDRSB: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper right')
plt.show()_____no_output_____# distribution plot for ADAS11 change

sns.distplot(no_change.ADAS11_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.ADAS11_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.ADAS11_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.ADAS11_delta.values, hist=False, label='CN to AD')

plt.xlabel('ADAS11 Change')
plt.ylabel('Kernel Density Estimate')
plt.title('ADAS11 Change by Change in Diagnosis')
plt.legend(loc='upper right')_____no_output_____# ADAS11 change histograms

plt.hist(no_change.ADAS11_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.ADAS11_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.ADAS11_delta.values, bins=mci_ad_bins, density=True,
         alpha=0.5, color='green', label='MCI to AD')
plt.hist(cn_ad.ADAS11_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in ADAS11')
plt.ylabel('Probability Density')
plt.title('Change in ADAS11: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper right')
plt.show()_____no_output_____# distribution plot for ADAS13 change

sns.distplot(no_change.ADAS13_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.ADAS13_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.ADAS13_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.ADAS13_delta.values, hist=False, label='CN to AD')

plt.xlabel('ADAS13 Change')
plt.ylabel('Kernel Density Estimate')
plt.title('ADAS13 Change by Change in Diagnosis')
plt.legend(loc='upper right')_____no_output_____# ADAS13 change histograms

plt.hist(no_change.ADAS13_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.ADAS13_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.ADAS13_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.5, color='green', label='MCI to AD')
plt.hist(cn_ad.ADAS13_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in ADAS13')
plt.ylabel('Probability Density')
plt.title('Change in ADAS13: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper right')
plt.show()_____no_output_____# distribution plot for MMSE change

sns.distplot(no_change.MMSE_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.MMSE_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.MMSE_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.MMSE_delta.values, hist=False, label='CN to AD')

plt.xlabel('MMSE Change')
plt.ylabel('Kernel Density Estimate')
plt.title('MMSE Change by Change in Diagnosis')
plt.legend(loc='upper left')_____no_output_____# MMSE change histograms

plt.hist(no_change.MMSE_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.MMSE_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.MMSE_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.MMSE_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in MMSE')
plt.ylabel('Probability Density')
plt.title('Change in MMSE: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper left')
plt.show()_____no_output_____# distribution plot for RAVLT change

sns.distplot(no_change.RAVLT_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.RAVLT_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.RAVLT_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.RAVLT_delta.values, hist=False, label='CN to AD')

plt.xlabel('RAVLT Change')
plt.ylabel('Kernel Density Estimate')
plt.title('RAVLT Change by Change in Diagnosis')
plt.legend(loc='upper left')_____no_output_____# RAVLT change histograms

plt.hist(no_change.RAVLT_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.RAVLT_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.RAVLT_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.RAVLT_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in RAVLT')
plt.ylabel('Probability Density')
plt.title('Change in RAVLT: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper right')
plt.show()_____no_output_____# distribution plot for Hippocampus change

sns.distplot(no_change.Hippocampus_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.Hippocampus_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.Hippocampus_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.Hippocampus_delta.values, hist=False, label='CN to AD')

plt.xlabel('Hippocampus Change')
plt.ylabel('Kernel Density Estimate')
plt.title('Hippocampus Change by Change in Diagnosis')
plt.legend(loc='upper left')_____no_output_____# Hippocampus change histograms

plt.hist(no_change.Hippocampus_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.Hippocampus_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.Hippocampus_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.Hippocampus_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in Hippocampus')
plt.ylabel('Probability Density')
plt.title('Change in Hippocampus: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper left')
plt.show()_____no_output_____# distribution plot for Ventricles change

sns.distplot(no_change.Ventricles_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.Ventricles_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.Ventricles_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.Ventricles_delta.values, hist=False, label='CN to AD')

plt.xlabel('Ventricles Change')
plt.ylabel('Kernel Density Estimate')
plt.title('Ventricles Change by Change in Diagnosis')
plt.legend(loc='upper right')_____no_output_____# Ventricles change histograms

plt.hist(no_change.Ventricles_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.Ventricles_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.Ventricles_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.Ventricles_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in Ventricles')
plt.ylabel('Probability Density')
plt.title('Change in Ventricles: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper right')
plt.show()_____no_output_____# distribution plot for WholeBrain change

sns.distplot(no_change.WholeBrain_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.WholeBrain_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.WholeBrain_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.WholeBrain_delta.values, hist=False, label='CN to AD')

plt.xlabel('WholeBrain Change')
plt.ylabel('Kernel Density Estimate')
plt.title('WholeBrain Change by Change in Diagnosis')
plt.legend(loc='upper left')_____no_output_____# WholeBrain change histograms

plt.hist(no_change.WholeBrain_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.WholeBrain_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.WholeBrain_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.WholeBrain_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in WholeBrain')
plt.ylabel('Probability Density')
plt.title('Change in WholeBrain: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper left')
plt.show()_____no_output_____# distribution plot for Entorhinal change

sns.distplot(no_change.Entorhinal_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.Entorhinal_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.Entorhinal_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.Entorhinal_delta.values, hist=False, label='CN to AD')

plt.xlabel('Entorhinal Change')
plt.ylabel('Kernel Density Estimate')
plt.title('Entorhinal Change by Change in Diagnosis')
plt.legend(loc='upper left')_____no_output_____# Entorhinal change histograms

plt.hist(no_change.Entorhinal_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.Entorhinal_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.Entorhinal_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.Entorhinal_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in Entorhinal')
plt.ylabel('Probability Density')
plt.title('Change in Entorhinal: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper left')
plt.show()_____no_output_____# distribution plot for MidTemp change

sns.distplot(no_change.MidTemp_delta.values, hist=False, label='No Change')
sns.distplot(cn_mci.MidTemp_delta.values, hist=False, label='CN to MCI')
sns.distplot(mci_ad.MidTemp_delta.values, hist=False, label='MCI to AD')
sns.distplot(cn_ad.MidTemp_delta.values, hist=False, label='CN to AD')

plt.xlabel('MidTemp Change')
plt.ylabel('Kernel Density Estimate')
plt.title('MidTemp Change by Change in Diagnosis')
plt.legend(loc='upper left')_____no_output_____# MidTemp change histograms

plt.hist(no_change.MidTemp_delta.values, bins=no_change_bins, density=True,
         alpha=0.6, color='blue', label='No Change')
plt.hist(cn_mci.MidTemp_delta.values, bins=cn_mci_bins, density=True,
         alpha=0.5, color='yellow', label='CN to MCI')
plt.hist(mci_ad.MidTemp_delta.values, bins=mci_ad_bins,
         density=True, alpha=0.4, color='green', label='MCI to AD')
plt.hist(cn_ad.MidTemp_delta.values, bins=cn_ad_bins, density=True,
         alpha=0.2, color='red', label='CN to AD')

plt.xlabel('Change in MidTemp')
plt.ylabel('Probability Density')
plt.title('Change in MidTemp: Baseline to Final\nby Change in Diagnosis')
plt.legend(loc='upper left')
plt.show()_____no_output_____sns.pairplot(scan_sub, hue='DX')_____no_output_____

#### Scan Pairplot Notes

- Age has little correlation with any data

- Baseline correlations with later scans
    - Hippocampus_bl did correlate positively with Hippocampus, WholeBrain, Entorhinal, and MidTemp scans
    - Baseline ventricles had low correlation with later measures
    - WholeBrain_bl corr with Hippocampus, MidTemp, and Entorhinal
    - Entorhinal_bl corr with Hippocampus, WholeBrain, Entorhinal, and MidTemp
    - MidTemp_bl corr with WholeBrain, MidTemp

- Good self predictors: baseline measures correlated well with the same measure later
    - Hippocampus, Ventricles, WholeBrain, Entorhinal, MidTemp_____no_output_____

final_exam.loc[:, 'G_DX_bl'] = final_exam.PTGENDER + '-' + final_exam.DX_bl2_____no_output_____final_exam.info()_____no_output_____# violin plots to explore baseline data for correlations to final diagnosis_____no_output_____sns.boxplot(x='DX', y='CDRSB_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline CDRSB')
plt.title('Baseline CDRSB by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='ADAS11_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline ADAS11')
plt.title('Baseline ADAS11 by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='ADAS13_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline ADAS13')
plt.title('Baseline ADAS13 by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='MMSE_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline MMSE')
plt.title('Baseline MMSE by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='RAVLT_immediate_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline RAVLT Immediate')
plt.title('Baseline RAVLT Immediate by Final Diagnosis')
plt.legend(title='Gender', loc='lower right')_____no_output_____sns.boxplot(x='DX', y='Hippocampus_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline Hippocampus')
plt.title('Baseline Hippocampus by Final Diagnosis')
plt.legend(title='Gender', loc='lower right')_____no_output_____sns.boxplot(x='DX', y='Ventricles_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline Ventricles')
plt.title('Baseline Ventricles by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='WholeBrain_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline WholeBrain')
plt.title('Baseline WholeBrain by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='Entorhinal_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline Entorhinal')
plt.title('Baseline Entorhinal by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='MidTemp_bl', data=final_exam, hue='PTGENDER', orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline MidTemp')
plt.title('Baseline MidTemp by Final Diagnosis')
plt.legend(title='Gender')_____no_output_____sns.boxplot(x='DX', y='ADAS11_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline ADAS11')
plt.title('Baseline ADAS11 by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____ad = final_exam[final_exam.DX == 'AD']
mci = final_exam[final_exam.DX == 'MCI']
cn = final_exam[final_exam.DX == 'CN']

ad_f = ad[ad.PTGENDER == 'Female']
ad_m = ad[ad.PTGENDER == 'Male']
mci_f = mci[mci.PTGENDER == 'Female']
mci_m = mci[mci.PTGENDER == 'Male']
cn_f = cn[cn.PTGENDER == 'Female']
cn_m = cn[cn.PTGENDER == 'Male']_____no_output_____# histograms of ADAS11 baseline vs final diagnosis

plt.hist(cn.ADAS11_bl, bins=int(np.sqrt(cn.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci.ADAS11_bl, bins=int(np.sqrt(mci.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad.ADAS11_bl, bins=int(np.sqrt(ad.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS11')
plt.ylabel('Probability Density')
plt.title('Baseline ADAS11 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')
plt.show()_____no_output_____plt.subplot(1, 2, 1)
plt.hist(cn.ADAS11_bl, bins=int(np.sqrt(cn.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci.ADAS11_bl, bins=int(np.sqrt(mci.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad.ADAS11_bl, bins=int(np.sqrt(ad.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS11')
plt.ylabel('Probability Density')
plt.title('Baseline ADAS11 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(1, 2, 2)
plt.hist(cn.ADAS13_bl, bins=int(np.sqrt(cn.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci.ADAS13_bl, bins=int(np.sqrt(mci.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad.ADAS13_bl, bins=int(np.sqrt(ad.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS13')
plt.ylabel('Probability Density')
plt.title('Baseline ADAS13 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.rcParams["figure.figsize"] = (14,5)_____no_output_____# hist plots of baseline ADAS11 & ADAS13 by final diagnosis and gender

plt.subplot(2, 2, 1)
plt.hist(cn_f.ADAS11_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_f.ADAS11_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_f.ADAS11_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS11')
plt.ylabel('Probability Density')
plt.title('Female Baseline ADAS11 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(2, 2, 2)
plt.hist(cn_f.ADAS13_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_f.ADAS13_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_f.ADAS13_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS13')
plt.ylabel('Probability Density')
plt.title('Female Baseline ADAS13 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(2, 2, 3)
plt.hist(cn_m.ADAS11_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_m.ADAS11_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_m.ADAS11_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS11')
plt.ylabel('Probability Density')
plt.title('Male Baseline ADAS11 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(2, 2, 4)
plt.hist(cn_m.ADAS13_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_m.ADAS13_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_m.ADAS13_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline ADAS13')
plt.ylabel('Probability Density')
plt.title('Male Baseline ADAS13 Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.tight_layout()
plt.subplots_adjust(top=1.2)

plt.rcParams["figure.figsize"] = (14,8)_____no_output_____sns.boxplot(x='DX', y='ADAS13_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline ADAS13')
plt.title('Baseline ADAS13 by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____plt.subplot(1, 2, 1)
sns.boxplot(x='DX', y='ADAS11_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Final Diagnosis')
plt.ylabel('Baseline ADAS11')
plt.title('Baseline ADAS11 by Final Diagnosis')
plt.legend('')
#plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)

plt.subplot(1, 2, 2)
sns.boxplot(x='DX', y='ADAS13_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Final Diagnosis')
plt.ylabel('Baseline ADAS13')
plt.title('Baseline ADAS13 by Final Diagnosis')
#plt.legend(title='Gender')
plt.tight_layout()
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
plt.rcParams["figure.figsize"] = (10,5)_____no_output_____sns.boxplot(x='DX', y='Ventricles_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline Ventricles')
plt.title('Baseline Ventricles by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____sns.boxplot(x='DX', y='Hippocampus_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline Hippocampus')
plt.title('Baseline Hippocampus by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____sns.boxplot(x='DX', y='WholeBrain_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline WholeBrain')
plt.title('Baseline WholeBrain by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____sns.boxplot(x='DX', y='Entorhinal_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline Entorhinal')
plt.title('Baseline Entorhinal by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____sns.boxplot(x='DX', y='MidTemp_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Diagnosis')
plt.ylabel('Baseline MidTemp')
plt.title('Baseline MidTemp by Final Diagnosis')
#plt.legend(title='Gender')
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)_____no_output_____plt.subplot(1, 2, 1)
sns.boxplot(x='DX', y='Hippocampus_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Final Diagnosis')
plt.ylabel('Baseline Hippocampus')
plt.title('Baseline Hippocampus by Final Diagnosis')
plt.legend('')
#plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)

plt.subplot(1, 2, 2)
sns.boxplot(x='DX', y='MidTemp_bl', data=final_exam, hue='G_DX_bl',
            hue_order=['Female-CN', 'Female-MCI', 'Female-AD', 'Male-CN', 'Male-MCI', 'Male-AD'], orient='v')
plt.xlabel('Final Diagnosis')
plt.ylabel('Baseline MidTemp')
plt.title('Baseline MidTemp by Final Diagnosis')
#plt.legend(title='Gender')
plt.tight_layout()
plt.legend(title='Gender and Baseline Diagnosis', bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
plt.rcParams["figure.figsize"] = (10,5)_____no_output_____# hist plots of baseline Hippocampus & MidTemp by final diagnosis and gender

plt.subplot(2, 2, 1)
plt.hist(cn_f.Hippocampus_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_f.Hippocampus_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_f.Hippocampus_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline Hippocampus')
plt.ylabel('Probability Density')
plt.title('Female Baseline Hippocampus Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(2, 2, 2)
plt.hist(cn_f.MidTemp_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_f.MidTemp_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_f.MidTemp_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline MidTemp')
plt.ylabel('Probability Density')
plt.title('Female Baseline MidTemp Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(2, 2, 3)
plt.hist(cn_m.Hippocampus_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_m.Hippocampus_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_m.Hippocampus_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline Hippocampus')
plt.ylabel('Probability Density')
plt.title('Male Baseline Hippocampus Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.subplot(2, 2, 4)
plt.hist(cn_m.MidTemp_bl, bins=int(np.sqrt(cn_f.shape[0])), density=True, alpha=0.3, color='blue', label='CN')
plt.hist(mci_m.MidTemp_bl, bins=int(np.sqrt(mci_f.shape[0])), density=True, alpha=0.3, color='green', label='MCI')
plt.hist(ad_m.MidTemp_bl, bins=int(np.sqrt(ad_f.shape[0])), density=True, alpha=0.3, color='red', label='AD')
plt.xlabel('Baseline MidTemp')
plt.ylabel('Probability Density')
plt.title('Male Baseline MidTemp Score\nSeparated by Final Diagnosis')
plt.legend(title='Final Diagnosis', loc='upper right')

plt.tight_layout()
plt.subplots_adjust(top=1.2)

plt.rcParams["figure.figsize"] = (14,8)_____no_output_____# check out bl ventricles plot vs final diagnosis

adni_bl, clin_bl, scan_bl = eda.get_bl_data(adni_comp, clin_data, scan_data)_____no_output_____final_exam.head()_____no_output_____


# Notebook from kbonna/decidenet
Path: behavioral_analysis/dn_ba_03_single_subject_model_fit.ipynb

import matplotlib.pyplot as plt
import numpy as np
import json

import sys
sys.path.append('/home/kmb/Desktop/Neuroscience/Projects/BONNA_decide_net/code')
from dn_utils.behavioral_models_REF import *_____no_output_____

### Behavioral modelling

#### Assumptions:

1. experienced utility is some function of displayed reward magnitude:
    - simple model: $u(x) = x$
    - complex model: $u(x) = x^{\delta} \quad if \quad x>0$ or $u(x) = -\gamma |x|^{\delta} \quad if \quad x>0$
2. values (reflecting beliefs about probability) are learned with simple delta learning rule (TD model):
    - $V(a_t)=V(a_{t-1})+ \alpha [R-V(a_{t-i})]$
3. choice probabilites are probabilistic functions of expected utilities:
    - degenerate model: $p(a)=\frac{EV(a)}{EV(a)+EV(b)}$ (parsimoneous approach introduced by *Summerfield et al. 2011*)
    - full softmax model: $p(a)=\frac{\exp(\beta EV(a))}{\exp(\beta EV(a)) + \exp(\beta EV(b))}$

#### Free parameters:

- $\alpha \in [0, 1]$: learning rate (modelling learning rate above half would result in model selecting previously rewarded / not punished option which is not realistic
- $\gamma \in [0, \infty]$: loss aversion parameter
- $\delta \in [0, 1]$: risk aversion parameter
- $\beta \in [0, \infty]$: inverse temperature for softmax function

#### Model variations:

- model 1: (k=1 parameter) simple utility model, degenerate model for choice probabilities
- model 2: (k=2 parameters) simple utility model, full softmax model
- model 3: (k=4 parameters) complex utility model, full softmax model
_____no_output_____

beh_path = "/home/kmb/Desktop/Neuroscience/Projects/BONNA_decide_net/" \
           "data/main_fmri_study/sourcedata/behavioral"

from dn_utils.behavioral_models_REF import load_behavioral_data
beh, meta = load_behavioral_data(root=beh_path)
n_subjects = beh.shape[0]
n_conditions = beh.shape[1]
n_trials = beh.shape[2]Shape of beh array: (32, 2, 110, 23)
Conditions [(0, 'rew'), (1, 'pun')]
Columns: [(0, 'block'), (1, 'block_bci'), (2, 'side'), (3, 'side_bci'), (4, 'magn_left'), (5, 'magn_right'), (6, 'response'), (7, 'rt'), (8, 'won_bool'), (9, 'won_magn'), (10, 'acc_after_trial'), (11, 'onset_iti'), (12, 'onset_iti_plan'), (13, 'onset_iti_glob'), (14, 'onset_dec'), (15, 'onset_dec_plan'), (16, 'onset_dec_glob'), (17, 'onset_isi'), (18, 'onset_isi_plan'), (19, 'onset_isi_glob'), (20, 'onset_out'), (21, 'onset_out_plan'), (22, 'onset_out_glob')]

### Show example model fit for models 1 and 2
Models are fitted to subject responses using $G^2$ which is a measure of badness-of-fit derived from log likelyhood. In this section model 1 and 2 are fitted for all possible parameters sampled from parameter space. Both task conditions are fitted separately for single subject. Finally, $G^2$  function is visualised in the parameter space._____no_output_____

N_grid = 100
subject = 15

### Model 1 #################################################################
alpha = np.linspace(0, 1, N_grid)

fit1 = np.zeros((2, N_grid))
for i, a in enumerate(alpha):
    fit1[0, i] = g_square(beh, meta, subject, 0,
                          model1(beh, meta, subject, 0, a)[2])
    fit1[1, i] = g_square(beh, meta, subject, 1,
                          model1(beh, meta, subject, 1, a)[2])

# Figure 1
fig1, ax = plt.subplots(facecolor='w', figsize=(10, 5))
ax.plot(alpha, fit1[0,:], linewidth=2, color='g', label='reward')
ax.plot(alpha, fit1[1,:], linewidth=2, color='r', label='punishment')

ax.set_ylim([0, 500])
ax.set_ylabel('$G^2$')
ax.set_xlabel('alpha')
ax.legend()
ax.grid()

plt.tight_layout()_____no_output_____N_grid = 100
subject = 27

## Model 2 #################################################################
alpha = np.linspace(0, .5, N_grid)
theta = np.linspace(0, .5, N_grid)

av, tv = np.meshgrid(alpha, theta)

fit2 = np.zeros((2, N_grid, N_grid))
for i, a in enumerate(alpha):
    for j, t in enumerate(theta):
        fit2[0, i, j] = g_square(beh, meta, subject, 0,
                                 model2(beh, meta, subject, 0, a, t)[2])
        fit2[1, i, j] = g_square(beh, meta, subject, 1,
                                 model2(beh, meta, subject, 1, a, t)[2])

# Figure 2
fig2, (ax2r, ax2p) = plt.subplots(nrows=1, ncols=2,
                                  facecolor='w', figsize=(10, 5))
im2r = ax2r.contourf(alpha, theta, fit2[0].T, levels=50, cmap='Greens_r')
im2p = ax2p.contourf(alpha, theta, fit2[1].T, levels=50, cmap='Reds_r')

ax2r.set_xlabel('alpha')
ax2r.set_ylabel('theta')
ax2p.set_xlabel('alpha')
ax2p.set_ylabel('theta')

fig2.colorbar(im2r, ax=ax2r)
fig2.colorbar(im2p, ax=ax2p)

plt.tight_layout()_____no_output_____

### Model fitting for single subject responses
In this section model parameters are optimized for explaining subject responses. Behavioral responsed are pooled across both task conditions.  _____no_output_____

from scipy.optimize import minimize
from scipy.optimize import Bounds_____no_output_____bounds1 = Bounds([0], [1])
x0 = np.array([.5])

def cost_model1(x):
    '''Optimization function for model 1.'''
    global subject
    g_rew = g_square(beh, meta, subject, 0, model1(beh, meta, subject, 0, x)[2])
    g_pun = g_square(beh, meta, subject, 1, model1(beh, meta, subject, 1, x)[2])
    return g_rew + g_pun

params_1 = np.zeros((n_subjects, 2)) # store results

for subject in range(n_subjects):

    res1 = minimize(cost_model1, x0, method='SLSQP', bounds=bounds1)
    params_1[subject, 0] = res1['x']
    params_1[subject, 1] = res1['fun']_____no_output_____bounds2 = Bounds([0, 0], [1, np.inf])
x0 = np.array([.5, 1])

def cost_model2(x):
    '''Optimization function for model 2.'''
    g_rew = g_square(beh, meta, subject, 0,
                     model2(beh, meta, subject, 0, x[0], x[1])[2])
    g_pun = g_square(beh, meta, subject, 1,
                     model2(beh, meta, subject, 0, x[0], x[1])[2])
    return g_rew

params_2 = np.zeros((n_subjects, 3)) # store results

for subject in range(n_subjects):

    res2 = minimize(cost_model2, x0, method='SLSQP', bounds=bounds2)
    params_2[subject][0] = res2['x'][0] # alpha
    params_2[subject][1] = res2['x'][1] # beta
    params_2[subject][2] = res2['fun']_____no_output_____fig, ax = plt.subplots(nrows=1, ncols=1, facecolor='w')

sp = ax.scatter(params_2[:,0], params_2[:, 1], c=params_2[:, 2])
ax.set_xlabel('alpha')
ax.set_ylabel('beta')

plt.colorbar(sp)_____no_output_____

### Model comparison for single subject
In this section three models are compared with respect to their Akaike Information Criterion (AIC score). AIC enables comparision of models with different number of free parameters. AIC is defined as:
$$AIC = G^2 + 2k$$ _____no_output_____

subject = 2

results = {}
results['model1'] = {'k': 1, 'g_square': None, 'x': None}
results['model2'] = {'k': 2, 'g_square': None, 'x': None}
results['model3'] = {'k': 4, 'g_square': None, 'x': None}

### Model 1 #################################################################
bounds1 = Bounds([0], [1])

def cost_model1(x):
    '''Optimization function for model 1.'''
    g_rew = g_square(beh, meta, subject, 0, model1(beh, meta, subject, 0, x)[2])
    g_pun = g_square(beh, meta, subject, 1, model1(beh, meta, subject, 1, x)[2])
    return g_rew  + g_pun

x0 = np.array([.5])

res1 = minimize(cost_model1, x0, method='SLSQP', bounds=bounds1)
results['model1']['g_square'] = res1['fun']
results['model1']['x'] = res1['x']

### Model 2 #################################################################
bounds2 = Bounds([0, 0], [1, np.inf])

def cost_model2(x):
    '''Optimization function for model 2.'''
    g_rew = g_square(beh, meta, subject, 0,
                     model2(beh, meta, subject, 0, x[0], x[1])[2])
    g_pun = g_square(beh, meta, subject, 1,
                     model2(beh, meta, subject, 1, x[0], x[1])[2])
    return g_rew + g_pun

x0 = np.array([.5, 1])

res2 = minimize(cost_model2, x0, method='SLSQP', bounds=bounds2)
results['model2']['g_square'] = res2['fun']
results['model2']['x'] = res2['x']

# ### Model 3 #################################################################
bounds3 = Bounds([0, 0, 0, 0], [1, np.inf, np.inf, 1])

def cost_model3(x):
    '''Optimization function for model 3.'''
    g_rew = g_square(beh, meta, subject, 0,
                     model3(beh, meta, subject, 0, x[0], x[1], x[2], x[3])[2])
    g_pun = g_square(beh, meta, subject, 1,
                     model3(beh, meta, subject, 1, x[0], x[1], x[2], x[3])[2])
    return g_rew + g_pun

x0 = np.array([.5, .5, 1, 1])

res3 = minimize(cost_model3, x0, method='SLSQP', bounds=bounds3)
results['model3']['g_square'] = res3['fun']
results['model3']['x'] = res3['x']_____no_output_____aic = np.zeros(3)

for i, model in enumerate(results):
    aic[i] = results[model]['g_square'] + 2*results[model]['k']

plt.style.use('seaborn-ticks')

fig, ax = plt.subplots(facecolor='w', figsize=(10, 3))

color = ['#01579B' for _ in range(3)]
color[np.argmin(aic)] = '#A67C00'
ax.barh(range(3), aic, color=color, alpha=.75)
ax.set_xlabel('Akaike Information Criterion')
ax.set_yticks(range(3))
ax.set_yticklabels(['model1', 'model2', 'model3'])
ax.grid()_____no_output_____