# Notebook from greenelab/mpmp
Path: 06_predict_survival/plot_survival_mutations.ipynb

## Survival prediction using somatic mutation data_____no_output_____In this notebook, we'll compare the results of survival prediction using [elastic net Cox regression](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.linear_model.CoxnetSurvivalAnalysis.html) for expression and methylation data only._____no_output_____

from pathlib import Path

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import mpmp.config as cfg
import mpmp.utilities.analysis_utilities as au
import mpmp.utilities.survival_utilities as su
import mpmp.utilities.plot_utilities as plu

%load_ext autoreload
%autoreload 2_____no_output_____

### Parameters and data directories_____no_output_____

# set results directories
pancan_results_dir = Path(cfg.results_dirs['survival'], 'mutations_me_all')
cancer_type_results_dir = Path(cfg.results_dirs['survival'], 'mutations_me_cancer_type')

# set list of PCA component numbers to look for
n_pcs = 10_____no_output_____

### Pan-cancer survival prediction, expression vs. methylation

These are models trained and tested on all data types in TCGA, for all samples that have expression and DNA methylation data profiled. We compare against a clinical covariate-only baseline (grey dotted line)._____no_output_____

# order to plot data types in
training_data_map = {
    'expression': 'gene expression',
    'me_27k': '27k methylation',
    'me_450k': '450k methylation',
    'vogelstein_mutations': 'all Vogelstein mutations',
    'significant_mutations': 'significant mutations',
    'mutation_preds_expression': 'mutation scores, expression',
    'mutation_preds_me_27k': 'mutation scores, 27k',
    'mutation_preds_me_450k': 'mutation scores, 450k',
}_____no_output_____pancan_results_df = su.load_survival_results(pancan_results_dir)
pancan_results_df.rename(columns={'identifier': 'cancer_type',
                                  'fold_no': 'fold'}, inplace=True)
pancan_results_df = pancan_results_df[(pancan_results_df.cancer_type == 'pancancer') |
                                      (~pancan_results_df.training_data.str.startswith('mutation_preds'))]

pancan_results_df.training_data.replace(to_replace=training_data_map, inplace=True)

print(pancan_results_df.shape)
print(pancan_results_df.cancer_type.unique())
print(pancan_results_df.training_data.unique())
pancan_results_df.head()(9216, 7)
['THCA' 'PCPG' 'ACC' 'LIHC' 'SARC' 'SKCM' 'pancancer' 'UCEC' 'LUSC' 'COAD'
 'STAD' 'TGCT' 'LUAD' 'KIRP' 'BRCA' 'GBM' 'HNSC' 'THYM' 'READ' 'ESCA'
 'LGG' 'UCS' 'KIRC' 'UVM' 'BLCA' 'LAML' 'PAAD' 'KICH' 'MESO' 'CHOL' 'PRAD'
 'CESC' 'DLBC']
['baseline' 'gene expression' '450k methylation'
 'all Vogelstein mutations' '27k methylation' 'significant mutations'
 'mutation scores, 450k' 'mutation scores, 27k'
 'mutation scores, expression']
cancer_type_results_df = su.load_survival_results(cancer_type_results_dir)
cancer_type_results_df.rename(columns={'identifier': 'cancer_type',
                                       'fold_no': 'fold'}, inplace=True)
cancer_type_results_df.training_data.replace(to_replace=training_data_map, inplace=True)

print(cancer_type_results_df.shape)
print(cancer_type_results_df.cancer_type.unique())
print(len(cancer_type_results_df.cancer_type.unique()))
print(cancer_type_results_df.training_data.unique())
cancer_type_results_df.head()(4284, 7)
['HNSC' 'UCS' 'UVM' 'MESO' 'PCPG' 'STAD' 'TGCT' 'ACC' 'GBM' 'KIRP' 'LGG'
 'ESCA' 'KICH' 'READ' 'LIHC' 'LUAD' 'BRCA' 'THCA' 'BLCA' 'PAAD' 'COAD'
 'PRAD' 'SKCM' 'LUSC' 'THYM' 'CESC' 'SARC' 'UCEC' 'LAML' 'KIRC' 'DLBC']
31
['mutation scores, 450k' 'mutation scores, expression'
 'mutation scores, 27k']
results_df = pd.concat((pancan_results_df, cancer_type_results_df))

print(results_df.shape)
print(results_df.cancer_type.unique())
print(len(results_df.cancer_type.unique()))
print(results_df.training_data.unique())
results_df.head()(13500, 7)
['THCA' 'PCPG' 'ACC' 'LIHC' 'SARC' 'SKCM' 'pancancer' 'UCEC' 'LUSC' 'COAD'
 'STAD' 'TGCT' 'LUAD' 'KIRP' 'BRCA' 'GBM' 'HNSC' 'THYM' 'READ' 'ESCA'
 'LGG' 'UCS' 'KIRC' 'UVM' 'BLCA' 'LAML' 'PAAD' 'KICH' 'MESO' 'CHOL' 'PRAD'
 'CESC' 'DLBC']
33
['baseline' 'gene expression' '450k methylation'
 'all Vogelstein mutations' '27k methylation' 'significant mutations'
 'mutation scores, 450k' 'mutation scores, 27k'
 'mutation scores, expression']
sns.set({'figure.figsize': (22, 6)})
sns.set_style('whitegrid')

plot_df = results_df[(results_df.training_data != 'baseline') &
                     (results_df.cancer_type == 'pancancer') &
                     (results_df.data_type == 'test') &
                     (results_df.signal == 'signal')]
sns.boxplot(data=plot_df, x='training_data', y='cindex',
            order=training_data_map.values())
plt.xlabel('Training data', size=14)
plt.ylabel('cindex', size=14)
plt.title('Pan-cancer survival performance, expression/methylation', size=14)
plt.ylim(0.6, 0.8)
for tick in plt.gca().get_xticklabels():
    tick.set_fontsize(13)

# plot baseline mean/bootstrapped 95% CI
baseline_df = results_df[(results_df.training_data == 'baseline') &
                         (results_df.cancer_type == 'pancancer') &
                         (results_df.data_type == 'test') &
                         (results_df.signal == 'signal')]
baseline_vals = baseline_df.cindex.values

baseline_mean = np.mean(baseline_vals)
plt.gca().axhline(y=baseline_mean, linestyle='--', linewidth=3, color='gray')

baseline_ci = sns.utils.ci(
    sns.algorithms.bootstrap(baseline_vals,
                             func=np.mean,
                             n_boot=1000,
                             units=None,
                             seed=cfg.default_seed)
)
plt.gca().axhspan(baseline_ci[0], baseline_ci[1], facecolor='gray', alpha=0.3)_____no_output_____

### Individual cancer survival prediction, expression vs. methylation

These are models trained and tested on each individual data type in TCGA, for all samples that have expression and DNA methylation data profiled. Like before, grey dotted lines are clinical covariate-only baselines, for that specific cancer type.

We only ran these experiments using 10 principal components extracted from the -omics datasets, since using more PCs than that led to convergence issues on many cancer types._____no_output_____

group_cancer_types = results_df.groupby(['cancer_type']).count().seed
max_count = group_cancer_types.max()
valid_cancer_types = group_cancer_types[group_cancer_types == max_count].index
print(len(valid_cancer_types))
print(valid_cancer_types)
print(group_cancer_types)30
Index(['ACC', 'BLCA', 'BRCA', 'CESC', 'COAD', 'ESCA', 'GBM', 'HNSC', 'KIRC',
       'KIRP', 'LAML', 'LGG', 'LIHC', 'LUAD', 'LUSC', 'MESO', 'PAAD', 'PCPG',
       'PRAD', 'READ', 'SARC', 'SKCM', 'STAD', 'TGCT', 'THCA', 'THYM', 'UCEC',
       'UCS', 'UVM', 'pancancer'],
      dtype='object', name='cancer_type')
cancer_type
ACC          432
BLCA         432
BRCA         432
CESC         432
CHOL         216
COAD         432
DLBC         108
ESCA         432
GBM          432
HNSC         432
KICH         216
KIRC         432
KIRP         432
LAML         432
LGG          432
LIHC         432
LUAD         432
LUSC         432
MESO         432
PAAD         432
PCPG         432
PRAD         432
READ         432
SARC         432
SKCM         432
STAD         432
TGCT         432
THCA         432
THYM         432
UCEC         432
UCS          432
UVM          432
pancancer    432
Name: seed, dtype: int64
cancer_type_avg = (
    results_df[(results_df.cancer_type.isin(valid_cancer_types)) &
               (results_df.data_type == 'test') &
               (results_df.signal == 'signal')]
      .groupby('cancer_type')
      .mean()
).cindex
cancer_type_avg.sort_values(ascending=False).head(10)_____no_output_____cancer_type_sd = (
    results_df[(results_df.cancer_type.isin(valid_cancer_types)) &
               (results_df.data_type == 'test') &
               (results_df.signal == 'signal')]
      .groupby('cancer_type')
      .std()
).cindex
cancer_type_cv = cancer_type_avg / cancer_type_sd
cancer_type_cv.sort_values(ascending=False).head(10)_____no_output_____import mpmp.utilities.data_utilities as du
sample_info_df = du.load_sample_info('expression')
top_sampled_cancer_types = sample_info_df.groupby('cancer_type').count().sample_type.sort_values(ascending=False).index_____no_output_____sns.set({'figure.figsize': (28, 12)})
sns.set_style('whitegrid')
fig, axarr = plt.subplots(2, 5)

cancer_type_cv = cancer_type_cv[cancer_type_cv.index != 'pancancer']
for ix, cancer_type in enumerate(top_sampled_cancer_types[:10]):
    ax = axarr[ix // 5, ix % 5]
    plot_df = results_df[(results_df.training_data != 'baseline') &
                         (results_df.cancer_type == cancer_type) &
                         (results_df.data_type == 'test') &
                         (results_df.signal == 'signal')]
    sns.boxplot(data=plot_df, x='training_data', y='cindex',
                order=training_data_map.values(), ax=ax)
    ax.set_xlabel('Training data type')
    ax.set_ylabel('cindex')
    ax.set_title('{} survival performance'.format(cancer_type))
    ax.set_ylim(0.3, 1.0)
    if (ix // 5) == 1:
        for tick in ax.get_xticklabels():
            tick.set_rotation(75)
            tick.set_fontsize(13)
    else:
        ax.set_xticks([])

    # plot baseline mean/bootstrapped 95% CI
    baseline_vals = (results_df
        [(results_df.training_data == 'baseline') &
         (results_df.cancer_type == cancer_type) &
         (results_df.data_type == 'test') &
         (results_df.signal == 'signal')]
    ).cindex.values

    baseline_mean = np.mean(baseline_vals)
    ax.axhline(y=baseline_mean, linestyle='--', linewidth=3, color='gray')

    baseline_ci = sns.utils.ci(
        sns.algorithms.bootstrap(baseline_vals,
                                 func=np.mean,
                                 n_boot=1000,
                                 units=None,
                                 seed=cfg.default_seed)
    )
    ax.axhspan(baseline_ci[0], baseline_ci[1], facecolor='gray', alpha=0.3)

plt.tight_layout()_____no_output_____sns.set({'figure.figsize': (28, 20)})
sns.set_style('whitegrid')
fig, axarr = plt.subplots(6, 5)

cancer_type_cv = cancer_type_cv[cancer_type_cv.index != 'pancancer']
for ix, cancer_type in enumerate(valid_cancer_types):
    ax = axarr[ix // 5, ix % 5]
    plot_df = results_df[(results_df.training_data != 'baseline') &
                         (results_df.cancer_type == cancer_type) &
                         (results_df.data_type == 'test') &
                         (results_df.signal == 'signal')]
    sns.boxplot(data=plot_df, x='training_data', y='cindex',
                order=training_data_map.values(), ax=ax)
    ax.set_xlabel('Training data type')
    ax.set_ylabel('cindex')
    ax.set_title('{} survival performance'.format(cancer_type))
    ax.set_ylim(0.3, 1.0)
    if (ix // 5) == 5:
        for tick in ax.get_xticklabels():
            tick.set_rotation(75)
            tick.set_fontsize(13)
    else:
        ax.set_xticks([])

    # plot baseline mean/bootstrapped 95% CI
    baseline_vals = (results_df
        [(results_df.training_data == 'baseline') &
         (results_df.cancer_type == cancer_type) &
         (results_df.data_type == 'test') &
         (results_df.signal == 'signal')]
    ).cindex.values

    baseline_mean = np.mean(baseline_vals)
    ax.axhline(y=baseline_mean, linestyle='--', linewidth=3, color='gray')

    baseline_ci = sns.utils.ci(
        sns.algorithms.bootstrap(baseline_vals,
                                 func=np.mean,
                                 n_boot=1000,
                                 units=None,
                                 seed=cfg.default_seed)
    )
    ax.axhspan(baseline_ci[0], baseline_ci[1], facecolor='gray', alpha=0.3)

plt.tight_layout()_____no_output_____sns.set({'figure.figsize': (11, 6)})
sns.set_style('whitegrid')

plot_df = results_df[(results_df.training_data != 'baseline') &
                     (results_df.data_type == 'test') &
                     (results_df.signal == 'signal')]
sns.boxplot(data=plot_df, x='training_data', y='cindex',
            order=training_data_map.values())

# plot baseline mean/bootstrapped 95% CI
baseline_df = results_df[(results_df.training_data == 'baseline') &
                         (results_df.data_type == 'test') &
                         (results_df.signal == 'signal')]
baseline_vals = baseline_df.cindex.values

baseline_mean = np.mean(baseline_vals)
plt.gca().axhline(y=baseline_mean, linestyle='--', linewidth=3, color='gray')

baseline_ci = sns.utils.ci(
    sns.algorithms.bootstrap(baseline_vals,
                             func=np.mean,
                             n_boot=1000,
                             units=None,
                             seed=cfg.default_seed)
)
plt.gca().axhspan(baseline_ci[0], baseline_ci[1], facecolor='gray', alpha=0.3)

plt.xlabel('Training data type')
plt.ylabel('cindex')
plt.title('Performance for varying PC count, averaged over cancer types')_____no_output_____


# Notebook from khoadinh44/pbdl-book
Path: overview-burgers-forw.ipynb

# Simple Forward Simulation of Burgers Equation with phiflow

This chapter will give an introduction for how to run _forward_, i.e., regular simulations starting with a given initial state and approximating a later state numerically, and introduce the ΦFlow framework. ΦFlow provides a set of differentiable building blocks that directly interface with deep learning frameworks, and hence is a very good basis for the topics of this book. Before going for deeper and more complicated integrations, this notebook (and the next one), will show how regular simulations can be done with ΦFlow. Later on, we'll show that these simulations can be easily coupled with neural networks.

The main repository for ΦFlow (in the following "phiflow") is [https://github.com/tum-pbs/PhiFlow](https://github.com/tum-pbs/PhiFlow), and additional API documentation and examples can be found at [https://tum-pbs.github.io/PhiFlow/](https://tum-pbs.github.io/PhiFlow/).

For this jupyter notebook (and all following ones), you can find a _"[run in colab]"_ link at the end of the first paragraph (alternatively you can use the launch button at the top of the page). This will load the latest version from the PBDL github repo in a colab notebook that you can execute on the spot:
[[run in colab]](https://colab.research.google.com/github/tum-pbs/pbdl-book/blob/main/overview-burgers-forw.ipynb)

## Model

As physical model we'll use Burgers equation.
This equation is a very simple, yet non-linear and non-trivial, model equation that can lead to interesting shock formations. Hence, it's a very good starting point for experiments, and it's 1D version (from equation {eq}`model-burgers1d`) is given by:

$$
    \frac{\partial u}{\partial{t}} + u \nabla u =
    \nu \nabla\cdot \nabla u
$$

_____no_output_____## Importing and loading phiflow

Let's get some preliminaries out of the way: first we'll import the phiflow library, more specifically the `numpy` operators for fluid flow simulations: `phi.flow` (differentiable versions for a DL framework _X_ are loaded via `phi.X.flow` instead).

**Note:** Below, the first command with a "!" prefix will install the [phiflow python package from GitHub](https://github.com/tum-pbs/PhiFlow) via `pip` in your python environment once you uncomment it. We've assumed that phiflow isn't installed, but if you have already done so, just comment out the first line (the same will hold for all following notebooks)._____no_output_____

!pip install --upgrade --quiet phiflow
#!pip install --upgrade --quiet git+https://github.com/tum-pbs/PhiFlow@develop

from phi.flow import *

from phi import __version__
print("Using phiflow version: {}".format(phi.__version__))Using phiflow version: 2.0.0rc2

Next we can define and initialize the necessary constants (denoted by upper-case names):
our simulation domain will have `N=128` cells as discretization points for the 1D velocity $u$ in a periodic domain $\Omega$ for the interval $[-1,1]$. We'll use 32 time `STEPS` for a time interval of 1, giving us `DT=1/32`. Additionally, we'll use a viscosity `NU` of $\nu=0.01/\pi$.

We'll also define an initial state given by $-\text{sin}(\pi x)$ in the numpy array `INITIAL_NUMPY`, which we'll use to initialize the velocity $u$ in the simulation in the next cell. This initialization will produce a nice shock in the center of our domain.

Phiflow is object-oriented and centered around field data in the form of grids (internally represented by a tensor object). I.e. you assemble your simulation by constructing a number of grids, and updating them over the course of time steps.

Phiflow internally works with tensors that have named dimensions. This will be especially handy later on for 2D simulations with additional batch and channel dimensions, but for now we'll simply convert the 1D array into a phiflow tensor that has a single spatial dimension `'x'`.
_____no_output_____

N = 128
STEPS = 32
DT = 1./STEPS
NU = 0.01/(N*np.pi)

# initialization of velocities
INITIAL_NUMPY = np.asarray( [-np.sin(np.pi * x) * 1. for x in np.linspace(-1,1,N)] ) # 1D numpy array

INITIAL = math.tensor(INITIAL_NUMPY, spatial('x') ) # convert to phiflow tensor
_____no_output_____

Next, we initialize a 1D `velocity` grid from the `INITIAL` numpy array that was converted into a tensor.
The extent of our domain $\Omega$ is specifiied via the `bounds` parameter $[-1,1]$, and the grid uses periodic boundary conditions (`extrapolation.PERIODIC`). These two properties are the main difference between a tensor and a grid: the latter has boundary conditions and a physical extent.

Just to illustrate, we'll also print some info about the velocity object: it's a `phi.math` tensor with a size of 128. Note that the actual grid content is contained in the `values` of the grid. Below we're printing five entries by using the `numpy()` function to convert the content of the phiflow tensor into a numpy array. For tensors with more dimensions, we'd need to specify the order here, e.g., `'y,x,vector'` for a 2D velocity field. (If we'd call `numpy('x,vector')` below, this would convert the 1D array into one with an additional dimension for each 1D velocity component.)_____no_output_____

velocity = CenteredGrid(INITIAL, extrapolation.PERIODIC, x=N, bounds=Box[-1:1])
#velocity = CenteredGrid(Noise(), extrapolation.PERIODIC, x=N, bounds=Box[-1:1]) # random init

print("Velocity tensor shape: "   + format( velocity.shape )) # == velocity.values.shape
print("Velocity tensor type: "    + format( type(velocity.values) ))
print("Velocity tensor entries 10 to 14: " + format( velocity.values.numpy()[10:15] ))Velocity tensor shape: (xˢ=128)
Velocity tensor type: <class 'phi.math._tensors.CollapsedTensor'>
Velocity tensor entries 10 to 14: [0.47480196 0.51774486 0.55942075 0.59972764 0.6385669 ]

## Running the simulation

Now we're ready to run the simulation itself. To ccompute the diffusion and advection components of our model equation we can simply call the existing `diffusion` and `semi_lagrangian` operators in phiflow: `diffuse.explicit(u,...)` computes an explicit diffusion step via central differences for the term $\nu \nabla\cdot \nabla u$ of our model. Next, `advect.semi_lagrangian(f,u)` is used for a stable first-order approximation of the transport of an arbitrary field `f` by a velocity `u`. In our model we have $\partial u / \partial{t} + u \nabla f$, hence we use the `semi_lagrangian` function to transport the velocity with itself in the implementation:_____no_output_____

velocities = [velocity]
age = 0.
for i in range(STEPS):
    v1 = diffuse.explicit(velocities[-1], NU, DT)
    v2 = advect.semi_lagrangian(v1, v1, DT)
    age += DT
    velocities.append(v2)

print("New velocity content at t={}: {}".format( age, velocities[-1].values.numpy()[0:5] ))New velocity content at t=1.0: [0.00274862 0.01272991 0.02360343 0.03478042 0.0460869 ]

Here we're actually collecting all time steps in the list `velocities`. This is not necessary in general (and could consume lots of memory for long-running sims), but useful here to plot the evolution of the velocity states later on.

The print statements print a few of the velocity entries, and already show that something is happening in our simulation, but it's difficult to get an intuition for the behavior of the PDE just from these numbers. Hence, let's visualize the states over time to show what is happening.

## Visualization

We can visualize this 1D case easily in a graph: the following code shows the initial state in blue, and then times $10/32, 20/32, 1$ in green, cyan and purple. _____no_output_____

# get "velocity.values" from each phiflow state with a channel dimensions, i.e. "vector"
vels = [v.values.numpy('x,vector') for v in velocities] # gives a list of 2D arrays

import pylab

fig = pylab.figure().gca()
fig.plot(np.linspace(-1,1,len(vels[ 0].flatten())), vels[ 0].flatten(), lw=2, color='blue',  label="t=0")
fig.plot(np.linspace(-1,1,len(vels[10].flatten())), vels[10].flatten(), lw=2, color='green', label="t=0.3125")
fig.plot(np.linspace(-1,1,len(vels[20].flatten())), vels[20].flatten(), lw=2, color='cyan',  label="t=0.625")
fig.plot(np.linspace(-1,1,len(vels[32].flatten())), vels[32].flatten(), lw=2, color='purple',label="t=1")
pylab.xlabel('x'); pylab.ylabel('u'); pylab.legend()
_____no_output_____

This nicely shows the shock developing in the center of our domain, which forms from the collision of the two initial velocity "bumps", the positive one on left (moving right) and the negative one right of the center (moving left).

As these lines can overlap quite a bit we'll also use a different visualization in the following chapters that shows the evolution over the course of all time steps in a 2D image. Our 1D domain will be shown along the Y-axis, and each point along X will represent one time step.

The code below converts our collection of velocity states into a 2D array, repeating individual time steps 8 times to make the image a bit wider. This is purely optional, of course, but makes it easier to see what's happening in our Burgers simulation._____no_output_____

def show_state(a, title):
    # we only have 33 time steps, blow up by a factor of 2^4 to make it easier to see
    # (could also be done with more evaluations of network)
    a=np.expand_dims(a, axis=2)
    for i in range(4):
        a = np.concatenate( [a,a] , axis=2)

    a = np.reshape( a, [a.shape[0],a.shape[1]*a.shape[2]] )
    #print("Resulting image size" +format(a.shape))

    fig, axes = pylab.subplots(1, 1, figsize=(16, 5))
    im = axes.imshow(a, origin='upper', cmap='inferno')
    pylab.colorbar(im) ; pylab.xlabel('time'); pylab.ylabel('x'); pylab.title(title)

vels_img = np.asarray( np.concatenate(vels, axis=-1), dtype=np.float32 )

# save for comparison with reconstructions later on
import os; os.makedirs("./temp",exist_ok=True)
np.savez_compressed("./temp/burgers-groundtruth-solution.npz", np.reshape(vels_img,[N,STEPS+1])) # remove batch & channel dimension

show_state(vels_img, "Velocity")_____no_output_____

This concludes a first simulation in phiflow. It's not overly complex, but because of that it's a good starting point for evaluating and comparing different physics-based deep learning approaches in the next chapter. But before that, we'll target a more complex simulation type in the next section._____no_output_____## Next steps

Some things to try based on this simulation setup:

- Feel free to experiment - the setup above is very simple, you can change the simulation parameters, or the initialization. E.g., you can use a noise field via `Noise()` to get more chaotic results (cf. the comment in the `velocity` cell above).

- A bit more complicated: extend the simulation to 2D (or higher). This will require changes throughout, but all operators above support higher dimensions. Before trying this, you probably will want to check out the next example, which covers a 2D Navier-Stokes case._____no_output_____


# Notebook from drfeinberg/Open-Science-Contemporary-Problems
Path: 04-Registered-Reports.ipynb

# Registered Reports

_____no_output_____# Registered Reports address many issues in scientific publishing

- Lack of replication
- Low statistical power
- Exploiting researcher degrees of freedom (p-hacking)
    - collecting data until analyses return statistically significant effects
    - selectively reporting analyses that reveal desirable outcomes.
- HARKing, or hypothesizing after results are known
- Publication bias
- Lack of data sharing_____no_output_____# Drawbacks

- Takes a lot of time
    - Despite In-Principle Acceptance at Stage 1, these manuscripts are **not** often recognised on
        - job applications
        - the ROA/performance evaluations
        - grant applications
    - Your paper could still be rejected at stage 2
        - This only is supposed to occur if the authors strayed from protocols in ways that would
            - Change the nature of the study
            - Suggest Questionable Research Practices
    - This can be difficult to manage for people early in their careers
- Reviewer 2 gets two chances to mess your study up  _____no_output_____# Which journals offer registered reports?

Here are some examples that I thought might be relevant to the department.

- Psychological Science
- Psychonomic Bulliten and Review
- Evolution and Human Behavior
- Quarterly Journal of Experimental Psychology
- Perception
- Nature Human Behaviour
- Journal of Personality and Social Psychology
- Journal of Neuroscience Research
- Journal of Experimental Psychology: Learning, Memory, and Cognition
- Journal of Experimental Social Psychology
- Infancy
- Infant Behavior and Development
- Developmental Science
- Animal Behavior and Cognition
- Behavioral Neuroscience
- Attention, Perception, & Psychophysics
_____no_output_____# Registered Report Workflow

_____no_output_____# Registered Report Example

 Here  is an example of a preregistration of a registered report we have just published at Nature Human Beviour.  It also has a preprint, and an OSF page.

_____no_output_____# Questions and Discussion

-----------
Materials adapted from: https://cos.io/rr/_____no_output_____


# Notebook from cnmazz/cmazzoladaher.github.io
Path: documents/ABC_2020/Matplotlib-1/Matplotlib I.ipynb

# Matplotlib: Plotting using Python
The [matplotlib library](https://matplotlib.org/index.html) is one of the most useful and used tools within python. With its consistent and relatively simple framework, you can create complex publication-worthy figures with ease*. This session will cover the basics of plotting and formatting in the scripted framework, while Matplotlib II will introduce the object-oriented framework and a variety of plots. Various links are documented below, but I'll also include them here for ease:
1. the latest [matplotlib documentation](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html#module-matplotlib.pyplot)
2. a page with [examples](https://matplotlib.org/gallery/index.html) that include source code for each plot
3. a page with additional [tutorials](https://matplotlib.org/tutorials/index.html) for the studious matplotlib student
4. another [matplotlib function index](https://matplotlib.org/api/pyplot_api.html) for an older version of matplotlib, but each function is defined on that page instead of requiring you click a link

### With that, let's get started!

*_ Some Googling for StackOverflow discussions may be necessary.______no_output_____

import matplotlib.pyplot as plt_____no_output_____

## The ```plt.plot()``` command
Okay, now that we've loaded matplotlib.pyplot, let's make the most basic plot: we'll just supply a list of numbers._____no_output_____

plt.plot([5, 2, 4, 1]);_____no_output_____

And that's it! Because we only supplied a single list, it assumed those were the y points and provided a set of x points: the list [0, 1, 2, ...] up to however many points you supplied. It also used a set of default formatting options, which we can change later.

Next, let's supply both x and y points:_____no_output_____

x = [2, 4, 6, 8]
y = [5, 2, 4, 1]

plt.plot(x,y);_____no_output_____

## Plot Formatting
Now, let's add a few extra features to our plot. Here, we'll
1. make two calls to ```plt.plot()```
2. add a vertical line using ```plt.axvline()```
3. add a horizontal line with ```plt.axhline()```
4. label each line using the ```label="your-name-here"``` keyword
5. add a legend based on those plot labels with ```plt.legend()```_____no_output_____

# Another set of values
x2 = [1, 3, 5, 7]
y2 = [1, 7, 3, 8]

# Main plotting calls
plt.plot(x, y, label='foo')
plt.plot(x2, y2, label='Line 2')

# Adding a vertical line and a horizontal line
plt.axvline(5, ymin=0.25, ymax=0.75, label='Short line')
plt.axhline(5, xmin=0.0, xmax=1.0, label='Long line')

# Legend
plt.legend();_____no_output_____

So, telling apart the lines up above became very difficult, even with the legend! Thankfully, there are many keywords you can use to customize your plots. The default colors and styles are decent, but for publication-worthy plots, you'll want to have as much control as possible. We'll step through some of the many customizations in the next few cells.

### Markers and Line Styles
First, we'll look at the keyword arguments (```**kwargs```) that we can provide to make our lines look unique. We'll just look at a small selection that are available.
1. ```color```: color of the line, can be an HTML color name ('red', 'darkred', 'black'), a hex string ('#008000'), RGB/RGBA tuple ((0,1,0,1)), grayscale intensities as a string ('0.5'), and several other settings I won't detail here
2.  ```linestyle or ls```: style of the line that is plotted; can be 'None' or a variety of styles, like '-' for solid, '--' dashed, '-.' dashed dot, ':' dotted; default is '-', so you have to specify 'None' if you just want markers for points
3.  ```linewidth or lw```: thickness of the line
4.  ```alpha```: transparency of the line, with 1.0 => 100% => completely solid and so on
5.  ```marker```: point style for markers; a few examples include 'o': circle; '.': point; 's': square; '*': star; 'v'/'^'/'<'/'>': triangle up/down/left/right
6.  ```markersize or ms```: size of the markers
7.  ```markeredgecolor or mec```: color of the marker edge; defaults to whatever the ```color``` kwarg is set to, but can be any valid color as described above
8.  ```markeredgewidth or mew```: thickness of the marker edge
9.  ```zorder```: order in which the points are displayed wherever they overlap, with lower numbers being displayed underneath higher zordered plot calls

**Note**: If you provide a value to ```marker``` and ```ls``` is anything other than 'None', _including not specified_, then the marker will be plotted on top of a line of the same color as the marker. You can easily check this in the code below._____no_output_____

# Main plotting calls
plt.plot(x, y,
         color = 'blue',
         ls = '--',
         lw = 4,
         alpha=0.5,
         label='Line 1',
         zorder = 2)

plt.plot(x2, y2,
         color = 'gold',
         ls = 'None',      # Required for the line portion to go away
         marker = 'o',
         ms = 9,
         mec = 'black',
         mew = 2,
         label='Line 2',
         zorder = 1)

# Legend
plt.legend();_____no_output_____

### Legends
We can also customize the legend quite a bit! Here's a list of some of the most useful keywords for the ```plt.legend()``` command, in my experience (there are others, check out the docs for more details).
1. ```fontsize```: size of the text in the legend; can be numeric (12, 25, etc.) and will be the absolute font size in points, or a string ('small', 'medium', 'xx-large') where the point size is scaled based on the _default_ font size
2. ```ncol```: number of columns in the legend
3. ```numpoints```: number of points to show per entry for plot calls with ```marker = ...```
4. ```frameon```: whether to include a frame around the legend
5. ```fancybox```: whether to round the edges of the legend frame; ignorerd if ```frameon=False```
6. ```edgecolor```: color of the legend frame; as before, any valid color
7. ```loc```: location of the legend; can be a string or an integer that refer to a pre-set position ('upper right' and 5 indicate the same location, see the docs for the 11 options) or a pair of floats (where (0.3, 0.5) means the bottom left corner is 30% to the right of the origin, 50% up)_____no_output_____

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large',
           ncol=1,
           numpoints=1,
           fancybox=False,
           frameon=True,
           edgecolor='black',
           loc=(0.3,0.5));_____no_output_____

### Formatting Axes
So far, we've focused on formatting the actual lines, but clear, informative axes make the plot actually useful. There are a number of ways we can alter the axes shown above. Each of the commands listed below can be applied to the y-axes by changing 'x' to 'y'.
1. ```plt.xlabel()```: string that labels the x-axis; can include LaTex math with a raw string, i.e., r'$math stuff$'; text attributes like color and fontsize apply, and you can select the rotation and placement of the label
2. ```plt.xlim()```: the minimum and maximum limits of the x-axis; you can supply both without keywords, or you can include just one: ```plt.xlim(xmax=5)``` if you want to leave ```xmin``` unchanged, etc.
3. ```plt.xticks()```: allows for choosing placement and labels for major ticks; the first list or array of values determines the placement, and the second array determines the labels on those ticks; text attributes like color, fontsize, rotation, etc. also apply_____no_output_____

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large', ncol=1, numpoints=3, fancybox=False,
           frameon=True, edgecolor='black', loc=(0.3,0.5))

# Axes labels
plt.xlabel('Time  [s]', fontsize=10, rotation=25)
plt.ylabel(r'$\Omega_{m}$', fontsize=16, color='green')

# Axes limits
plt.xlim(xmin=-2, xmax=12)
plt.ylim(0, 10)

# Tick locators and labels
plt.xticks([-1, 1, 3, 5, 7, 9, 11], fontsize=14)
plt.yticks([0, 2.5, 5, 7.5, 10], ['bad', 'meh', 'okay', 'good', 'best'], color='purple');_____no_output_____

### Some Final Touches
There are still many other useful features we could add to our plot. We'll highlight just four here, but don't worry. If you can imagine it, you can probably find a way to make it in matplotlib!
1. ```plt.title()```: set the title for your plot; follows most text properties, but to change the font size and weight, you have to supply it in a dictionary format before any other keywords (see example below)
2. ```plt.text()```: add text to a plot; the first two arguments are the absolute x and y positions for the text, and the third argument is the actual text to display; LaTex math can be rendered with a raw string (r'$math$'); most other text properties will also apply
3. ```plt.grid()```: True or False for whether to include a grid; keywords include: ```which=``` determines the set of ticks that are gridded and can be 'major' or 'minor'*; ```axis=``` determines which axis gets gridded lines, and can be 'x', 'y', or 'both'
4. ```ax = plt.gca()``` => ```ax.set_facecolor()```: changes the color in the plot's main face to any valid color; this looks a little different than most of the others, because it's verging on the object-oriented approach (next session!) but it's easy enough to show here

***Note**: Since minor ticks are not currently enabled on our plot, setting ```plt.grid(which='minor')``` will not show any gridlines, because the minor tick marks don't exist._____no_output_____

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large', ncol=1, numpoints=3, fancybox=False,
           frameon=True, edgecolor='black', loc=(0.3,0.5))

# Axes labels
plt.xlabel('Time  [s]', fontsize=10, rotation=25)
plt.ylabel(r'$\Omega_{m}$', fontsize=16, color='green')

# Axes limits
plt.xlim(xmin=-2, xmax=12)
plt.ylim(0, 10)

# Tick locators and labels
plt.xticks([-1, 1, 3, 5, 7, 9, 11], fontsize=14)
plt.yticks([0, 2.5, 5, 7.5, 10], ['bad', 'meh', 'okay', 'good', 'best'], color='purple')

# Title
plt.title('Look how far our plot has come!', {'fontsize':10, 'fontweight':'bold'}, loc='right', color='red')

# Text
plt.text(-1, 9, r'$v\approx H_{0}d$ (completely unrelated)', fontsize=14, color='brown')

# Grid
plt.grid(True, which='major', axis='y', ls=':', lw=2, color='orange')

# Facecolor
ax = plt.gca()
ax.set_facecolor('lightgray');_____no_output_____

## Saving Plots
The plot's no good to your paper if it only exists in your Jupyter notebook. Saving is important, and thankfully it's pretty easy. The function is ```plt.savefig()```. The first arguemnt should be the filename as a string, including the file path, if it's not in the same directory as the notebook: 'your/file/path/goes/here/filename.extension', where the extension can be png, pdf, ps, and eps, amongst others.

There's a variety of other keywords for ```plt.savefig()```, some of which are more useful than others. For plots like ours where some parts extend beyond where they're expected (thanks to the unusual axes labels, in our case), including ```bbox_inches='tight'``` can keep it from cutting off parts of the plot when saving. The keyword ```facecolor``` changes whether there's color behind the portion where the axes labels are. Adding ```facecolor='None'``` can make for an interesting effect on posters, where you might have a colored background behind the plot. The central portion will be white (or whatever you set the facecolor to be) and will stand out prominantly, while the axes labels will still be visible but there won't be a jarring white box around your plot, as you can see in the image below.

![facecolor='white' vs. facecolor='None'](Slide1.png)_____no_output_____

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large', ncol=1, numpoints=3, fancybox=False,
           frameon=True, edgecolor='black', loc=(0.3,0.5))

# Axes labels
plt.xlabel('Time  [s]', fontsize=10, rotation=25)
plt.ylabel(r'$\Omega_{m}$', fontsize=16, color='green')

# Axes limits
plt.xlim(xmin=-2, xmax=12)
plt.ylim(0, 10)

# Tick locators and labels
plt.xticks([-1, 1, 3, 5, 7, 9, 11], fontsize=14)
plt.yticks([0, 2.5, 5, 7.5, 10], ['bad', 'meh', 'okay', 'good', 'best'], color='purple')

# Title
plt.title('Look how far our plot has come!', {'fontsize':10, 'fontweight':'bold'}, loc='right', color='red')

# Text
plt.text(-1, 9, r'$v\approx H_{0}d$ (completely unrelated)', fontsize=14, color='brown')

# Grid
plt.grid(True, which='major', axis='y', ls=':', lw=2, color='orange')

# Facecolor
ax = plt.gca()
ax.set_facecolor('lightgray')

plt.savefig('fig-1.pdf', bbox_inches='tight');_____no_output_____

## Formatting Fun!
For publications, you'll probably spend a long time tweaking your plots over and over. This is normal! The formattion functions and options listed above are some of the most common ones, but there's far more you can do. Check out the latest [matplotlib documentation](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html#module-matplotlib.pyplot) for a list of functions with links to detailed documentation for each one, and check out [this page](https://matplotlib.org/gallery/index.html) of examples with source code. Personally, I prefer this [matplotlib function index](https://matplotlib.org/api/pyplot_api.html); while it's technically for an older version of matplotlib, very little has changed, and it has all the information for each function listed out. I control-F to find the function I want and have all the information right there, without having to click on another link. It's a useful tab to always have open._____no_output_____# Practice Problems_____no_output_____## Exercise 1
The main code for our plot up above is copied below. Change the ```plt.plot()``` keywords for both datasets until you have:

* different colors than before
* different linestyles for both
* different markerstyles for one or both
* different alphas and zorders that make sense together

Check out the [docs](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot) for ```plt.plot``` to read more about the keywords discussed earlier._____no_output_____

# Main plotting calls -- alter this code!
plt.plot(x, y, color = 'black', ls = ':', lw = 2, alpha=1.0, label='Line 1', zorder = 1)
plt.plot(x2, y2, color = 'green', ls = 'None', marker = 's', ms = 9, mec = 'red',
         mew = 1, label='Line 2', alpha = 0.65, zorder = 2);_____no_output_____

## Exercise 2
Return the x/y-axis back to standard formatting--i.e., black text, no rotation, uniform fontsize, etc. Try to find a good value for the fontsize on the x/y labels. Then, let's focus on the axes limits and ticks.

  1. Set new x/y limits. What happens if you supply the limits backwards (ex: plt.xlim(10, 0))?
  2. Change the location of the tick marks on both axes so that you have
    * 5 equally spaced ticks: 2 at the ends, 3 in between, and
    * 8 equally spaced ticks: 2 at the ends and 6 in between.

  These don't have to be _perfectly_ evenly spaced, but they should be within a few decimals. Alter the fontsize based on the number of ticks you include. Compare to the default choice of limits and ticks for the plots earlier._____no_output_____

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large', ncol=1, numpoints=3, fancybox=False,
           frameon=True, edgecolor='black', loc=(0.3,0.5))

#  * Alter the code below to standard settings and then complete parts 1 and 2 *

# Axes labels
plt.xlabel('Time  [s]', fontsize=14)
plt.ylabel(r'$\Omega_{m}$', fontsize=14)

# Axes limits
plt.xlim(xmin=0, xmax=9)
plt.ylim(0, 10)

# Tick locators and labels
plt.xticks([1, 3, 5, 7, 9], fontsize=12)
plt.yticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], fontsize=12);_____no_output_____

## Exercise 3
Color is quite important for clarity and aesthetics. Change the two calls below to both be lines--i.e., there should be no ```marker``` keywords, and ```ls``` should not be set to 'None'.

1. Find a good color combination for your two lines, and then find a bad combination, whether it's eye-watering bad or just difficult to tell apart.
2. Check out this [website](https://colorbrewer2.org/#type=sequential&scheme=Blues&n=3). Play with the number of colors in the plot and see how the different shades are distributed. If you see color combos or just a specific color you like, it lists the hex representation for each shade on the left hand side.
3. Complex plots often make use of a variety of colors to distinguish lines, but that can cripple their effectiveness for color-blind audiences. This [website](https://www.color-blindness.com/coblis-color-blindness-simulator/) shows you what an image looks like to those with an assortment of color vision handicaps. You can look at default image or upload your own. If you have time now or later, I suggest uploading your plot (must be .png) and see how your color choices hold up._____no_output_____

# * Alter the keywords below until they're both lines, then change the colors *

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1);_____no_output_____

## Exercise 4
Add at least 4 more sets of lines to your plot; they should be a mix of horizontal, vertical, diagonal, and scatter points. Few scientific plots show just two sets of data, and they often include one or more horiziontal/vertical lines to indicate limits or interesting regions.

1. Use only two colors for your 6+ sets of lines. What keywords can you use to make the lines distinguisable? Toy with the other keywords until you have a plot that is still readable.
2. This time, use only 2 linestyles. What colors and markers can you use to make the sets of data distinguishable? Keep it mind exercise 3 when selecting colors.
3. Include at least 3 of your lines in the legend, the more the merrier. Check out the [documentation](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html#matplotlib.pyplot.legend) and play with the legend settings. You don't want it covering up data, but it should still be readable and in a place that both draws the eye and doesn't distract from the overall plot. This is hard to describe, but you'll know it when you see it--when you don't have to search for the legend, but it doesn't dominate what you see in the plot, either._____no_output_____

# * Add more lines and experiment with clarity *

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large', ncol=1, numpoints=3, fancybox=False,
           frameon=True, edgecolor='black', loc=(0.3,0.5));_____no_output_____

## Exercise 5
Let's face it: the plot we made in the lesson portion was not pretty, at all. The formatting was exaggerated for the sake of instruction. Using the concepts from the previous four exercises, alter the code below until you're happy with the results. Remove, alter, and add features until you're happy with the figure._____no_output_____

# * Make this less bad *

# Main plotting calls
plt.plot(x, y, color = 'blue', ls = '--', lw = 4, alpha=0.5, label='Line 1', zorder = 2)
plt.plot(x2, y2, color = 'gold', ls = 'None', marker = 'o', ms = 9, mec = 'black',
         mew = 2, label='Line 2', zorder = 1)

# Legend
plt.legend(fontsize='x-large', ncol=1, numpoints=3, fancybox=False,
           frameon=True, edgecolor='black', loc=(0.3,0.5))

# Axes labels
plt.xlabel('Time  [s]', fontsize=10, rotation=25)
plt.ylabel(r'$\Omega_{m}$', fontsize=16, color='green')

# Axes limits
plt.xlim(xmin=-2, xmax=12)
plt.ylim(0, 10)

# Tick locators and labels
plt.xticks([-1, 1, 3, 5, 7, 9, 11], fontsize=14)
plt.yticks([0, 2.5, 5, 7.5, 10], ['bad', 'meh', 'okay', 'good', 'best'], color='purple')

# Title
plt.title('Look how far our plot has come!', {'fontsize':10, 'fontweight':'bold'}, loc='right', color='red')

# Text
plt.text(-1, 9, r'$v\approx H_{0}d$ (completely unrelated)', fontsize=14, color='brown')

# Grid
plt.grid(True, which='major', axis='y', ls=':', lw=2, color='orange')

# Facecolor
ax = plt.gca()
ax.set_facecolor('lightgray');_____no_output_____


# Notebook from thirasit/Practical-ML-in-Petrol-with-Python
Path: Case Study 2/Evolutionary Optimization for Oil & Gas/Genetic Algorithm - Maximization.ipynb

# **Genetic Algorithm - Maximization**_____no_output_____

<code>
#Variables Initialization
import random
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
seed=11
random.seed(seed)
up_limit, low_limit=30, -30_____no_output_____#Population Initialization
# Define the Method to Generate a population by choosing samples
#uniformly at random from the range specified
def gen_sample_population(size, x_limits, y_limits):
    x_low, x_up=x_limits
    y_low, y_up=y_limits
    population=np.zeros((size,2))
    for i in range(size):
        population[i,0]=random.uniform(x_low,
         x_up)
        population[i,1]=random.uniform(y_low,
         y_up)
    return population
# Producing a population
population=gen_sample_population(size=50,
                x_limits=(low_limit, up_limit),
                y_limits=(low_limit, up_limit))
# Plot the individual pairs in a sample generation
fig, ax0=plt.subplots(1,1)
ax0.set_title('A Sample Generation')
ax0.set_xlabel('X')
ax0.set_ylabel('Y')
ax0.plot(population[:,0],population[:,1],'ro')_____no_output_____#Fitness Function
# defining the fitness function which is f(x,y)=5 -(x^2 + y^2)
def function(point):
    return (5-(point[0] ** 2+ point[1]** 2))
#plot the fitness function
from mpl_toolkits.mplot3d import Axes3D
n=50
# linspace returns n equally spaced values over the interval low_limit
#to up_limit
x_vals=np.linspace(low_limit, up_limit, n+1)
y_vals=np.linspace(low_limit, up_limit, n+1)
# meshgrid returns the coordinate matrices by combining all the
#combinations of x_vals and y_vals and creates an n*n x and y spaces
x, y=np.meshgrid(x_vals, y_vals)
z=np.zeros_like(x)
# two nested loops to iterate over all 50 * 50 space for x and y values
for i in range(n+1):
    for j in range(n+1):
        z[i,j]=function([x[i,j], y[i,j]])
#Plot the 3D plot
fig=plt.figure()
#adding 3d projection for the plot
ax0=fig.add_subplot(111,projection='3d')
surf=ax0.plot_surface(x, y, z, rstride=1, cstride=1, cmap='viridis',
linewidth=0, antialiased=False)
#r'' will allow for adding latex code in the title, $$ indicates a latex
#formula
ax0.set_title(r'$5-(x^2+y^2)$')
ax0.set_xlabel('X')
ax0.set_ylabel('Y')
plt.tight_layout()_____no_output_____#Parent Selection: Roulette
# select fit individuals as parents
def roul_choice(sorted_population):
    func_vals=np.array([function(x) for x in sorted_population])
    min_fitness=func_vals[0]
# Make all fitness values positive so probabilities can be calculated
#next
    if min_fitness < 0:
        func_vals += -min_fitness
    fitness_sum=sum(func_vals)

# Draw a random variable from uniform distribution
    rand=random.uniform(0, 1)
    acc=0
    for i in range(len(sorted_population)):
        fitness=func_vals[i]
# Probability of each individual being selected is calculated
        prob=fitness/fitness_sum
        acc += prob
        if rand <= acc:
            return sorted_population[i]_____no_output_____#Crossover
def crossover(point_1,point_2):
    return[(point_1[0]+ point_2[0])/2,(point_1[1]+point_2[1])/2]_____no_output_____#Mutation
def mutate(point):
    mute_x=point[0] + random.uniform(-0.5, 0.5)
    mute_y=point[1] + random.uniform(-0.5, 0.5)
# Guarantee to be kept inside boundaries
    mute_x=min(max(mute_x, low_limit), up_limit)
    mute_y=min(max(mute_y, low_limit), up_limit)
    return [mute_x,mute_y]_____no_output_____#Make Next Generation: steps (ii)-(v)
# (ii) Fitness Function used for (iii) Parents selection and then (iv)
#Crossover and (v) Mutation used to Generate next generation
def offspring_generation(curr_population):
    next_generation=np.zeros(np.shape(curr_population))
# Sort items in the previous_population array based on their function
#value
    sorted_population=sorted(curr_population,key=function)
    population_size=len(curr_population)
    for i in range(population_size):
        # (iii) Parent Selection
        first_parent=roul_choice(sorted_population)
        second_parent=roul_choice(sorted_population)
        # Crossover
        offspring=crossover(first_parent, second_parent)
        # Mutation
        offspring=mutate(offspring)
        next_generation[i,:]=offspring
    return next_generation_____no_output_____#Genetics Algorithm Workflow
from matplotlib.patches import ConnectionPatch
#Perform the Genetics Algorithm to generate generations and find the
#best individuals in each generation
generations=100
best_val=float('-inf')
best_pair=np.zeros((2))
prev_best_pair=np.zeros_like(best_pair)
best_population=-1
# Threshold for improvement in order to continue the search
threshold=10e-5
stop=False
coordsA="data"
coordsB="data"
fig, ax0=plt.subplots(1,1)
ax0.set_xlabel('X')
ax0.set_ylabel('Y')
ax0.set_title(r'$5-(x^2+y^2)$')
ax0.grid(True)
# Mark the optimum Individuals [0,0] as a star
ax0.plot([0], [0], 'g*', markersize=15, linewidth=0.15,
label='Optimum Individual')
ax0.legend(loc='lower left')
# (i) Population Initialization
population=gen_sample_population(size=50, x_limits=(low_limit,
up_limit), y_limits=(low_limit, up_limit))
# (ii) Fitness Function Calculation
func_vals=np.array([function(x) for x in population])
gen_max_val=max(func_vals)
best_population=1
# Update the best value obtained so far
best_val=gen_max_val
best_pair=population[np.argmax(func_vals),:]
print('Generation:' ,1, ' Best individual: ',best_pair)
# Loop through the number of generations
for i in range(1, generations):
    print("Iteration number: ", i + 1)
# (ii) Fitness Function used for (iii) Parents selection and then (iv)
#Crossover and (v) Mutation used to Generate next generation
    population=offspring_generation(population)
    func_vals=np.array([function(x) for x in population])
    gen_max_val=max(func_vals)
# check if this generation maximum value (gen_max_val) is better than
#our best value (best_val) found thus far.
    if (gen_max_val > best_val):
        diff=abs(gen_max_val - best_val)
# If the improvement amount (diff) is less than threshold set
#stop=True => Break the loop, otherwise continue the search
        stop=True if diff < threshold else False
        best_population=i + 1
# Update the best value obtained so far
        best_val=gen_max_val
        prev_best_pair=np.copy(best_pair)
        best_pair=population[np.argmax(func_vals),:]
        print('Generation:' ,i + 1, ' individual: ',best_pair,
        'Improves the optimal value by amount ', diff )
# If it is after iteration 1 plot the arrow to show the evolution to the
#new best_pair
        if(i > 1):
# plots the best_pair as points with 'o' marker on the plot
            ax0.plot([best_pair[0]],[best_pair[1]], 'o')
# Mark each best_pair point with its iteration number (i + 1)
            ax0.text(best_pair[0] + 0.001 ,
            best_pair[1]+ 0.015 ,'{}'.format(i + 1))
# Create the arrow from prev_best_point to best_point to show the
#evolution to the new individual (best_pair)
            con=ConnectionPatch(prev_best_pair,
            best_pair, coordsA=coordsA, coordsB=coordsB,
            arrowstyle="-|>", shrinkA=2, shrinkB=2,
            mutation_scale=15, fc="w")
            ax0.add_artist(con)
        if (stop):
            break
# Print the final best individuals
print('Final Result is from iteration: ',best_population,
' for coordinates: ', best_pair , ' best value: ', best_val)Generation: 1  Best individual:  [-7.6977572  -2.80751372]
Iteration number:  2
Generation: 2  individual:  [-4.81379627  3.45549699] Improves the optimal value by amount  32.024505256832434
Iteration number:  3
Generation: 3  individual:  [ 0.04756721 -0.93437909] Improves the optimal value by amount  34.237767071729294
Iteration number:  4
Generation: 4  individual:  [-0.278554   -0.29793807] Improves the optimal value by amount  0.7089674896053291
Iteration number:  5
Iteration number:  6
Iteration number:  7
Iteration number:  8
Iteration number:  9
Iteration number:  10
Iteration number:  11
Iteration number:  12
Generation: 12  individual:  [-0.23266337  0.25234095] Improves the optimal value by amount  0.048551231537377504
Iteration number:  13
Iteration number:  14
Iteration number:  15
Iteration number:  16
Generation: 16  individual:  [0.05150034 0.32476834] Improves the optimal value by amount  0.009681440029304156
Iteration number:  17
Iteration number:  18
Generation: 18  individual:  [-0.19525474 -0.04241036] Improves the optimal value by amount  0.06820370435121159
Iteration number:  19
Generation: 19  individual:  [-0.09342639 -0.11753705] Improves the optimal value by amount  0.017379602773999636
Iteration number:  20
Generation: 20  individual:  [-0.06418065 -0.06352526] Improves the optimal value by amount  0.014388835622400009
Iteration number:  21
Iteration number:  22
Generation: 22  individual:  [ 0.07656358 -0.00241396] Improves the optimal value by amount  0.002286805471415576
Iteration number:  23
Iteration number:  24
Iteration number:  25
Generation: 25  individual:  [-0.01540758  0.07367238] Improves the optimal value by amount  0.0002027951112362203
Iteration number:  26
Iteration number:  27
Generation: 27  individual:  [0.02752718 0.0327375 ] Improves the optimal value by amount  0.003835524149973679
Iteration number:  28
Iteration number:  29
Iteration number:  30
Iteration number:  31
Generation: 31  individual:  [-0.01418233 -0.00964115] Improves the optimal value by amount  0.0015353992919147075
Iteration number:  32
Iteration number:  33
Iteration number:  34
Iteration number:  35
Iteration number:  36
Iteration number:  37
Iteration number:  38
Iteration number:  39
Iteration number:  40
Iteration number:  41
Iteration number:  42
Iteration number:  43
Iteration number:  44
Iteration number:  45
Iteration number:  46
Iteration number:  47
Iteration number:  48
Iteration number:  49
Iteration number:  50
Iteration number:  51
Iteration number:  52
Iteration number:  53
Iteration number:  54
Iteration number:  55
Iteration number:  56
Iteration number:  57
Iteration number:  58
Iteration number:  59
Iteration number:  60
Iteration number:  61
Iteration number:  62
Generation: 62  individual:  [ 0.01223495 -0.00870979] Improves the optimal value by amount  6.85357726553093e-05
Final Result is from iteration:  62  for coordinates:  [ 0.01223495 -0.00870979]  best value:  4.999774445565906

</code>


# Notebook from LSSTDESC/ssi-tools
Path: examples/cosmoDC2_star_example.ipynb

<code>
from desc_dc2_dm_data import REPOS
from lsst.daf.persistence import Butler
import lsst.geom as geom

butler = Butler(REPOS['2.1i'])
skymap = butler.get("deepCoadd_skyMap")

# Near the center of DC2
ra = 65.0  # degrees
dec = -35.0 # degrees

point = geom.SpherePoint(ra, dec, geom.degrees)
tractInfo = skymap.findTract(point)
patchInfo = tractInfo.findPatch(point)

dataId = {
    'tract':tractInfo.getId(),
    'patch':f"%d,%d"%patchInfo.getIndex(),
    'filter':'r'
}
print(dataId)
{'tract': 4030, 'patch': '1,3', 'filter': 'r'}
from astropy.table import Table
import astropy.io.fits as fits
import numpy as np

# Not sure if all of these are needed.
table = Table()
table['raJ2000'] = np.array([np.deg2rad(65.0)])
table['decJ2000'] = np.array([np.deg2rad(-35.0)])
table['sourceType'] = np.array(["star"])
table['DiskHalfLightRadius'] = np.array([1.0])
table['BulgeHalfLightRadius'] = np.array([1.0])
table['umagVar'] = np.array([23.0])
table['gmagVar'] = np.array([23.0])
table['rmagVar'] = np.array([23.0])
table['imagVar'] = np.array([23.0])
table['zmagVar'] = np.array([23.0])
table['ymagVar'] = np.array([23.0])
table['disk_n'] = np.array([1.0])
table['bulge_n'] = np.array([1.0])
table['a_d'] = np.array([1.0])
table['a_b'] = np.array([1.0])
table['b_d'] = np.array([1.0])
table['b_b'] = np.array([1.0])
table['pa_disk'] = np.array([1.0])
table['pa_bulge'] = np.array([1.0])

table.write("fsi.fits", overwrite=True)_____no_output_____!insertFakes.py \
    /global/cscratch1/sd/desc/DC2/data/Run2.1i/rerun/coadd-dr4-v1/ \
    --output test/ \
    --id tract=4030 patch=1,3 \
    filter=r -c fakeType=fsi.fits \
    --clobber-config --no-versionsCameraMapper INFO: Loading exposure registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/registry.sqlite3
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
LsstCamMapper WARN: Unable to find valid calib root directory
LsstCamMapper WARN: Unable to find valid calib root directory
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
CameraMapper INFO: Loading exposure registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/registry.sqlite3
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
LsstCamMapper WARN: Unable to find valid calib root directory
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
CameraMapper INFO: Loading exposure registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/registry.sqlite3
CameraMapper INFO: Loading calib registry from /global/cscratch1/sd/desc/DC2/data/Run2.1i_globus_20200715/w_2019_19-v1/CALIB/calibRegistry.sqlite3
root INFO: Running: /global/homes/b/beckermr/.conda/envs/dmstack2020.40w/lsst_home/stack/miniconda/Linux64/pipe_tasks/20.0.0-28-g282f9e7e+feda6aebd8/bin/insertFakes.py /global/cscratch1/sd/desc/DC2/data/Run2.1i/rerun/coadd-dr4-v1/ --output test/ --id tract=4030 patch=1,3 filter=r -c fakeType=fsi.fits --clobber-config --no-versions
insertFakes INFO: Adding fakes to: tract: 4030, patch: 1,3, filter: r
insertFakes INFO: Adding mask plane with bitmask 131072
numexpr.utils INFO: Note: NumExpr detected 64 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
insertFakes INFO: Removing 0 rows with HLR = 0 for either the bulge or disk
insertFakes INFO: Removing 0 rows of galaxies with nBulge or nDisk outside of 0.30 <= n <= 6.20
insertFakes INFO: Making 0 fake galaxy images
insertFakes INFO: Making 1 fake star images
import os
from desc_dc2_dm_data import REPOS
from lsst.daf.persistence import Butler
import lsst.geom as geom

butler = Butler("./test/")

cutoutSize = geom.ExtentI(101, 101)
point = geom.SpherePoint(65.0, -35.0, geom.degrees)

skymap = butler.get("deepCoadd_skyMap")
tractInfo = skymap.findTract(point)
patchInfo = tractInfo.findPatch(point)
xy = geom.PointI(tractInfo.getWcs().skyToPixel(point))
bbox = geom.BoxI(xy - cutoutSize//2, cutoutSize)
coaddId = {
    'tract':tractInfo.getId(),
    'patch':f"%d,%d"%patchInfo.getIndex(),
    'filter':'r'
}
print(coaddId)

image = butler.get("deepCoadd_sub", bbox=bbox, immediate=True, dataId=coaddId)
fake_image = butler.get("fakes_deepCoadd_sub", bbox=bbox, immediate=True, dataId=coaddId)

import matplotlib.pyplot as plt
fig, axes = plt.subplots(ncols=2, figsize=(10, 5))
axes[0].imshow(image.maskedImage.image.array, vmin=0, vmax=1)
axes[1].imshow(fake_image.maskedImage.image.array, vmin=0, vmax=1)
plt.show(){'tract': 4030, 'patch': '1,3', 'filter': 'r'}

</code>