```python
import json
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sympy.solvers import solve
from sympy import Symbol
import scipy.stats as sstats

from os import path

from scripts.utils import SimulateData
from stopsignalmetrics import SSRTmodel, StopData

from scipy.stats import exponnorm, norm, norminvgauss
from scipy import stats

from batch_files.generate_remaining_sim_cmds import replace
```

```python
OVERWRITE_MUS = False
```

# __Preprocessing__

## 1. Prepare data for use by stopsignalmetrics

```python
abcd_data = pd.read_csv('abcd_data/abcd_w_finger_press.csv')
```

```python
len(abcd_data.NARGUID.unique())
```

    8464

```python
for rt_col in ['go_rt_adjusted', 'stop_rt_adjusted']:
    abcd_data.loc[abcd_data['finger_press'].isnull(), rt_col] = np.nan
```

```python
abcd_data['block'] = np.NaN
abcd_data.loc[abcd_data['TrialNum'] < 180, 'block'] = '1'
abcd_data.loc[abcd_data['TrialNum'] >= 180, 'block'] = '2'
```

```python
abcd_data['choice_accuracy'] = np.where(
    abcd_data['finger_press'].notnull(),
    np.where(
        abcd_data['finger_press']==abcd_data['correct_response'],
        1,
        0),
    np.nan
)
```

```python
len(abcd_data.NARGUID.unique())
```

    8464

## 1.5 replace IDs with ` (backticks) in them
#### shell scripts do not interpret them well

```python
def strip_backticks(string):
    return string.replace('`', '')
```

```python
original_unique_IDs = abcd_data.NARGUID.unique()
fix_id_map = {i:strip_backticks(i) for i in abcd_data.NARGUID.unique() if '`' in i}
abcd_data.NARGUID = abcd_data.NARGUID.replace(fix_id_map)
assert len(original_unique_IDs)==len(abcd_data.NARGUID.unique())
```

```python
abcd_data.to_csv('abcd_data/minimal_abcd_with_issue_3.csv', index=False)
```

## 2. Drop Issue 3 people

```python
abcd_data_w_issue_3 = pd.read_csv('abcd_data/minimal_abcd_with_issue_3.csv')
```

```python
abcd_data_w_issue_3['correct_go_response'].unique()
```

    array([ 1.,  0., nan])

```python
issue_3_people = abcd_data_w_issue_3.loc[(abcd_data_w_issue_3['stop_rt_adjusted'] < 50) & (abcd_data_w_issue_3['stop_rt_adjusted'] > 0) & (abcd_data_w_issue_3['SSDDur'] ==50), 'NARGUID'].unique()

print('n affected:', len(issue_3_people))
print('p affect:', len(issue_3_people)/ 7231)

abcd_data = abcd_data_w_issue_3[~abcd_data_w_issue_3.NARGUID.isin(issue_3_people)].copy()
print('n remaining:', abcd_data.NARGUID.nunique())
```

    n affected: 226
    p affect: 0.03125432167058498
    n remaining: 8238

```python
abcd_data.to_csv('abcd_data/minimal_abcd_clean.csv', index=False)
```

# __Metrics for Simulation__

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')
```

## 1. get a distribution of mean go RTs and SSRTs to sample from

```python
variable_dict = {
   "columns": {
      "ID": "NARGUID", #subject identifier
      "condition": "trial_type", #col with trial types
      "correct_response": "correct_response", #col with correct reponse codes
      "response": "finger_press", #col with actual response codes
      "SSD": "SSDDur", #col with stop signal delay
      "block": "block", #col with which block a trial is accuring during
      "goRT": "go_rt_adjusted", # col with go reaction time recording
      "stopRT": "stop_rt_adjusted", #col with stop failure reaction time recording
      "choice_accuracy": "choice_accuracy" #col with whether a response was correct
   },
   "key_codes": {
      "go": "GoTrial", # cell values for go trials
      "stop": "StopTrial",  #cell values for stop trials
      "correct": 1.0,
       "incorrect": 0.0,
       "noResponse": np.nan
   }
}
```

```python
abcd_ssrt = StopData(var_dict=variable_dict, compute_acc_col=False)

abcd_proc = abcd_ssrt.fit_transform(abcd_data)

ssrt_model = SSRTmodel(model='replacement')

ssrt_metrics = ssrt_model.fit_transform(abcd_proc, level='group')

problem_subs = ssrt_metrics[ssrt_metrics.SSRT.isnull()].index

print(f'dropping {len(problem_subs)} subs for having P(respond|signal) == 1 or 0')

ssrt_metrics = ssrt_metrics[ssrt_metrics.SSRT.notnull()].copy()
```

    dropping 31 subs for having P(respond|signal) == 1 or 0

```python
len(problem_subs)
```

    31

```python
problem_subs
```

    Index(['0E4CT74P', '0LA6MBBY', '1PN1MPWF', '2L2B6VXP', '3FJ6DHVU', '4DVGGJE9',
           '4HWVY2FD', '5DJB8FFX', '7YA52BU1', '7ZRUXUX9', '8VA4L6RD', 'AB3XR5P1',
           'AF517NF3', 'B3438R23', 'C2ZW147D', 'E9KNUZDH', 'EC1NEE93', 'F86XUHXD',
           'K9MXKUUB', 'KDZR86PA', 'KP8W0VHU', 'L3258K1X', 'L5W5CDJU', 'L9NJJJBZ',
           'LW0J7PJB', 'MR60CAWU', 'T5EXYEGX', 'TY02C986', 'U1THG28C', 'YK2J75TH',
           'vcahyykd'],
          dtype='object', name='ID')

```python
ssrt_metrics.to_csv('abcd_data/abcd_ssrt_metrics.csv')
```

```python
# def get_mus(sub_row):
#     sub_row = sub_row.copy()
#     mu_dict = {}
#     # init go and stop vars
#     g = Symbol('g')
#     s = Symbol('s')

#     # formulas (must be solved as "expression = 0")
#     # subject_{go/ss}rt = threshold/mu_go + nondecision_time
#     # threshold = 100
#     # nondecision_time = 50

#     go_sol = solve((sub_row['mean_go_RT'].values[0] - 50) * g - 100, g)
# #     go_sol = solve(100/g + 50 - sub_row['mean_go_RT'].values[0], g)
#     assert len(go_sol) == 1, f"{len(go_sol)} solutions found based on {sub_row['mean_go_RT']}: {go_sol}"
#     mu_dict['go'] = float(go_sol[0])

#     stop_sol = solve(100/s + 50 - sub_row['SSRT'].values[0], s)
#     assert len(stop_sol) == 1, f"{len(stop_sol)} solutions found based on {sub_row['SSRT']}: {stop_sol}"
#     mu_dict['stop'] = float(stop_sol[0])

#     return mu_dict

# mu_df = ssrt_metrics.groupby('ID').apply(get_mus)
# mu_dict = mu_df.to_dict()
# mu_dict['prob_subs'] = list(problem_subs)
# mu_df = mu_df.apply(pd.Series)

# json_mu_path = 'abcd_data/individual_mus.json'
# with open(json_mu_path, 'w') as jp:
#     json.dump(mu_dict, jp)
```

```python
fig = plt.figure(figsize=(12,6))
ax = sns.distplot(ssrt_metrics['SSRT'], kde=False, norm_hist=True, label='SSRT')
ssrt_loc,ssrt_var = norm.fit(ssrt_metrics['SSRT'])
xx = np.arange(-500, 3000, 1)
ax.plot(xx, norm.pdf(xx, loc=ssrt_loc, scale=ssrt_var), 'k', lw=2)
_ = sns.distplot(ssrt_metrics['mean_go_RT'], kde=False,norm_hist=True, ax=ax, label='goRT')
goRT_loc,goRT_var = norm.fit(ssrt_metrics['mean_go_RT'])
ax.plot(xx, norm.pdf(xx, loc=goRT_loc, scale=goRT_var), 'k', lw=2)
_ = plt.legend()
```

```python
# repeating but giving a reasonable scale for the SSRT distribution

fig = plt.figure(figsize=(12,6))
ax = sns.distplot(ssrt_metrics['SSRT'], kde=False, norm_hist=True, label='SSRT')
xx = np.arange(-500, 3000, 1)
ax.plot(xx, norm.pdf(xx, loc=ssrt_loc, scale=85), 'k', lw=2)
_ = sns.distplot(ssrt_metrics['mean_go_RT'], kde=False,norm_hist=True, ax=ax, label='goRT')
ax.plot(xx, norm.pdf(xx, loc=goRT_loc, scale=goRT_var), 'k', lw=2)
_ = plt.legend()
```

```python
fig = plt.figure(figsize=(12,6))
ax = sns.distplot(ssrt_metrics['SSRT'], kde=False, norm_hist=True, label='SSRT')
xx = np.arange(-500, 3000, 1)
ax.plot(xx, norm.pdf(xx, loc=ssrt_loc, scale=25), 'k', lw=2)
_ = sns.distplot(ssrt_metrics['mean_go_RT'], kde=False,norm_hist=True, ax=ax, label='goRT')
ax.plot(xx, norm.pdf(xx, loc=goRT_loc, scale=goRT_var), 'k', lw=2)
_ = plt.legend()
```

```python
ssrt_metrics.keys()
```

    Index(['SSRT', 'mean_SSD', 'p_respond', 'max_RT', 'mean_go_RT', 'sd_go_RT',
           'mean_stopfail_RT', 'sd_stopfail_RT', 'omission_count', 'omission_rate',
           'go_acc', 'stopfail_acc'],
          dtype='object')

## 1. get SSD distributions per subject

```python
MAX_SSD = 500 #for clipping distributions
```

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')
```

```python
SSD_dist = abcd_data.groupby('NARGUID')['SSDDur'].value_counts(normalize=True)
SSD_dist.name = 'proportion'
SSD_dist = SSD_dist.reset_index()
SSD_dist.to_csv(f'abcd_data/SSD_dist_by_subj_{MAX_SSD}Clip-False.csv', index=False)
```

```python
SSD_dist_clipped = abcd_data.query(f"(SSDDur <= {MAX_SSD}) | (trial_type=='GoTrial')").groupby('NARGUID')['SSDDur'].value_counts(normalize=True)
SSD_dist_clipped.name = 'proportion'
SSD_dist_clipped = SSD_dist_clipped.reset_index()
SSD_dist_clipped.to_csv(f'abcd_data/SSD_dist_by_subj_{MAX_SSD}Clip-True.csv', index=False)
```

```python
len(SSD_dist.query(f"SSDDur > {MAX_SSD}").NARGUID.unique())
```

    1892

# 1.5 Sample Mus if necessary

```python
# MAGIC NUMBERS
GORT_SCALE = goRT_var
SSRT_SCALES = [85, 25, 5, 0]

def convert_to_mu(rt, thresh=100, ndt=50):
    # example solve:
    # Threshold = mu * (rt - non-decision-time)
    # Threshold / (rt - ndt) = mu
    return thresh/(rt-ndt)

def sample(mean, scale, ndt=50):
    tmp = np.random.normal(loc=mean, scale=scale)
    # if it is less that the non-decision-time, you get negative mus
    # if it is close to the nondecision time, you get HUGE mus
    while tmp < ndt+10:
        tmp = np.random.normal(loc=mean, scale=scale)
    return tmp

def get_mu_suffix(SSRT_SCALE=ssrt_var, GORT_SCALE=goRT_var):
    stop_str_suffix = 'SSRTscale-%d' % SSRT_SCALE
    go_str_suffix = ''
    if GORT_SCALE != goRT_var:
        go_str_suffix='_RTscale-%d' % GORT_SCALE
    return '%s%s' % (stop_str_suffix, go_str_suffix)

def assign_mus(SSRT_SCALE=ssrt_var, GORT_SCALE=goRT_var):

    suffix = get_mu_suffix(SSRT_SCALE, GORT_SCALE)

    write_out_path = 'abcd_data/assigned_mus_%s.json' % suffix

    if not path.exists(write_out_path) or OVERWRITE_MUS:
        print('assigning mus: %s' % suffix)
        mus_dict = {}
        for sub in SSD_dist.NARGUID.unique():
            mus_dict[sub] = {}
            mus_dict[sub]['goRT'] = sample(mean=goRT_loc, scale=GORT_SCALE)
            mus_dict[sub]['go'] = convert_to_mu(mus_dict[sub]['goRT'])
            mus_dict[sub]['SSRT'] = sample(mean=ssrt_loc, scale=SSRT_SCALE)
            mus_dict[sub]['stop'] = convert_to_mu(mus_dict[sub]['SSRT'])
        with open(write_out_path, 'w') as jp:
            json.dump(mus_dict, jp)
    return write_out_path

def visualize_mu_dists(write_out_path):
    with open(write_out_path, 'r') as jf:
        mus_dict = json.load(jf)

    suffix_str = write_out_path.split('assigned_mus_')[-1].replace('.json', '')
    mu_df = pd.DataFrame(mus_dict).T
    fig = plt.figure(figsize=(8,4))
    ax = sns.distplot(mu_df['go'], kde=False, norm_hist=True, label='go')
    _ = sns.distplot(mu_df['stop'], kde=False,norm_hist=True, ax=ax, label='stop')
    _ = plt.legend()
    _ = plt.title(suffix_str)
    plt.show()
    plt.close()
```

```python
# MAIN
for SSRT_SCALE in SSRT_SCALES:
    mu_path = assign_mus(SSRT_SCALE=SSRT_SCALE)
    visualize_mu_dists(mu_path)

# Single Individual Case
mu_path_no_var = assign_mus(SSRT_SCALE=0,GORT_SCALE=0)
visualize_mu_dists(mu_path_no_var)

```

# Exgauss metrics

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')
SSD0_RTs = abcd_data.query(
    "SSDDur == 0.0 and correct_stop==0.0"
    ).stop_rt_adjusted.values

FIT_K, FIT_LOC, FIT_SCALE = sstats.exponnorm.fit(SSD0_RTs)
FIT_LAMBDA = 1/(FIT_K*FIT_SCALE)
FIT_BETA = 1/FIT_LAMBDA
```

```python
exgaus_params = {
    'K': FIT_K,
    'loc': FIT_LOC,
    'scale': FIT_SCALE,
    'lambda': FIT_LAMBDA,
    'beta': FIT_BETA,
}

with open('abcd_data/exgauss_params.json', 'w') as f:
    json.dump(exgaus_params, f)
```

## 2. P(guess|SSD) for mixture distributions

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')
SSDs = abcd_data.SSDDur.unique()
SSDs = [i for i in SSDs if i == i]
SSDs.sort()
```

```python
acc_per_SSD = pd.DataFrame()
for ssd in SSDs:
    curr_means = abcd_data.query(
        "SSDDur == %s and correct_stop==0.0" % ssd
    ).groupby('NARGUID').mean()['choice_accuracy']
    curr_means.name = ssd
    acc_per_SSD = pd.concat([acc_per_SSD, curr_means], 1, sort=True)

go_accs = abcd_data.query(
        "trial_type == 'GoTrial' and correct_go_response in [1.0, 0.0]"
    ).groupby('NARGUID').mean()['choice_accuracy']
go_accs.name = -1
acc_per_SSD = pd.concat([acc_per_SSD, go_accs], 1, sort=True)
```

```python
acc_per_SSD.describe()
```

      0.0
      50.0
      100.0
      150.0
      200.0
      250.0
      300.0
      350.0
      400.0
      450.0
      500.0
      550.0
      600.0
      650.0
      700.0
      750.0
      800.0
      850.0
      900.0
      -1.0

      count
      2944.000000
      4146.000000
      4819.000000
      5759.000000
      6554.000000
      6817.000000
      6566.000000
      5842.000000
      4783.000000
      3651.000000
      2516.000000
      1661.000000
      1057.000000
      630.000000
      392.000000
      239.000000
      138.000000
      117.000000
      115.000000
      8210.000000

      mean
      0.499410
      0.561566
      0.656870
      0.723856
      0.795379
      0.830639
      0.860157
      0.884024
      0.895152
      0.906854
      0.912347
      0.919252
      0.917789
      0.914306
      0.894193
      0.904812
      0.852174
      0.831197
      0.817350
      0.919088

      std
      0.309872
      0.346036
      0.328498
      0.303200
      0.263413
      0.243262
      0.230942
      0.219983
      0.223044
      0.218917
      0.224541
      0.229288
      0.229932
      0.240663
      0.276771
      0.263033
      0.319575
      0.327071
      0.333918
      0.085973

      min
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000

      25%
      0.333333
      0.333333
      0.500000
      0.500000
      0.666667
      0.750000
      0.800000
      0.833333
      0.875000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      0.833333
      0.666667
      0.898613

      50%
      0.500000
      0.571429
      0.666667
      0.800000
      0.875000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      0.941708

      75%
      0.666667
      0.880515
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      0.969900

      max
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000
      1.000000

```python
acc_per_SSD.mean()
```

     0.0      0.499410
     50.0     0.561566
     100.0    0.656870
     150.0    0.723856
     200.0    0.795379
     250.0    0.830639
     300.0    0.860157
     350.0    0.884024
     400.0    0.895152
     450.0    0.906854
     500.0    0.912347
     550.0    0.919252
     600.0    0.917789
     650.0    0.914306
     700.0    0.894193
     750.0    0.904812
     800.0    0.852174
     850.0    0.831197
     900.0    0.817350
    -1.0      0.919088
    dtype: float64

```python
p = Symbol('p')
guess_mean = acc_per_SSD.mean()[0.0]
go_mean = acc_per_SSD.mean()[-1]
p_guess_per_SSD = {}
for ssd in SSDs:
    curr_mean = acc_per_SSD.mean()[ssd]
    solution = solve(p*guess_mean + (1-p)*go_mean - curr_mean, p)
    assert len(solution) == 1
    p_guess_per_SSD[ssd] = solution[0]
p_guess_df = pd.DataFrame(p_guess_per_SSD, index=['p_guess'])
p_guess_df.to_csv('abcd_data/p_guess_per_ssd.csv', index=False)
```

```python
{col: float(p_guess_df[col].values[0]) for col
                      in p_guess_df.columns}
```

    {0.0: 1.0,
     50.0: 0.8518966881510508,
     100.0: 0.6248081425758761,
     150.0: 0.46519470731379453,
     200.0: 0.2947710624963631,
     250.0: 0.21075535798552303,
     300.0: 0.1404209047269027,
     350.0: 0.08354920017624702,
     400.0: 0.05703409119007931,
     450.0: 0.029151024358994575,
     500.0: 0.016063618909064378,
     550.0: -0.0003910068585019575,
     600.0: 0.0030962512655911116,
     650.0: 0.01139533818314855,
     700.0: 0.059318925632538,
     750.0: 0.0340178091093458,
     800.0: 0.15944204679687593,
     850.0: 0.20942638758940402,
     900.0: 0.24241997384504005}

```python
p_guess_df2 = pd.read_csv('abcd_data/p_guess_per_ssd.csv')
```

```python
p_guess_df2
```

      0.0
      50.0
      100.0
      150.0
      200.0
      250.0
      300.0
      350.0
      400.0
      450.0
      500.0
      550.0
      600.0
      650.0
      700.0
      750.0
      800.0
      850.0
      900.0

      0
      1.0
      0.851897
      0.624808
      0.465195
      0.294771
      0.210755
      0.140421
      0.083549
      0.057034
      0.029151
      0.016064
      -0.000391
      0.003096
      0.011395
      0.059319
      0.034018
      0.159442
      0.209426
      0.24242

## Within subjects SD of RTs for stop-fail and go RTs

```python
abcd_data.describe()
```

      go_rt_adjusted
      stop_rt_adjusted
      SSDDur
      correct_go_response
      correct_stop
      SSD.RESP
      Fix.RESP
      StopSignal.RESP
      Go.RESP
      TrialNum
      correct_stimulus_mapping_1
      correct_stimulus_mapping_2
      block
      finger_press
      correct_response
      choice_accuracy

      count
      2.292144e+06
      238026.000000
      494283.000000
      2.471397e+06
      494283.000000
      19624.000000
      236334.000000
      171150.000000
      2.203096e+06
      2.965680e+06
      2965680.0
      2965680.0
      2.965680e+06
      2.530170e+06
      2965680.0
      2.530170e+06

      mean
      5.437762e+02
      458.165058
      236.154895
      8.411251e-01
      0.518442
      1.591215
      1.638694
      1.602530
      1.603930e+00
      1.805000e+02
      1.5
      3.5
      1.502778e+00
      1.493806e+00
      1.5
      9.096041e-01

      std
      2.135862e+02
      207.786635
      159.296667
      3.655595e-01
      0.499660
      0.662964
      0.685852
      0.682406
      6.827720e-01
      1.039227e+02
      0.5
      0.5
      4.999924e-01
      4.999617e-01
      0.5
      2.867482e-01

      min
      0.000000e+00
      0.000000
      0.000000
      0.000000e+00
      0.000000
      1.000000
      1.000000
      1.000000
      1.000000e+00
      1.000000e+00
      1.0
      3.0
      1.000000e+00
      1.000000e+00
      1.0
      0.000000e+00

      25%
      3.990000e+02
      336.000000
      100.000000
      1.000000e+00
      0.000000
      1.000000
      1.000000
      1.000000
      1.000000e+00
      9.075000e+01
      1.0
      3.0
      1.000000e+00
      1.000000e+00
      1.0
      1.000000e+00

      50%
      5.060000e+02
      426.000000
      200.000000
      1.000000e+00
      1.000000
      2.000000
      2.000000
      2.000000
      2.000000e+00
      1.805000e+02
      1.5
      3.5
      2.000000e+00
      1.000000e+00
      1.5
      1.000000e+00

      75%
      6.460000e+02
      530.000000
      350.000000
      1.000000e+00
      1.000000
      2.000000
      2.000000
      2.000000
      2.000000e+00
      2.702500e+02
      2.0
      4.0
      2.000000e+00
      2.000000e+00
      2.0
      1.000000e+00

      max
      2.793000e+03
      2835.000000
      900.000000
      1.000000e+00
      1.000000
      4.000000
      4.000000
      4.000000
      4.000000e+00
      3.600000e+02
      2.0
      4.0
      2.000000e+00
      2.000000e+00
      2.0
      1.000000e+00

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')

```

```python
abcd_data[['NARGUID', 'go_rt_adjusted', 'stop_rt_adjusted']].groupby('NARGUID').apply(lambda x: x.describe().loc['std', :]).mean()
```

    std
    go_rt_adjusted      190.272261
    stop_rt_adjusted    155.122888
    dtype: float64

```python
rt_desc = abcd_data[['NARGUID', 'go_rt_adjusted', 'stop_rt_adjusted']].groupby('NARGUID').describe()
rt_desc
```

      go_rt_adjusted
      stop_rt_adjusted

      count
      mean
      std
      min
      25%
      50%
      75%
      max
      count
      mean
      std
      min
      25%
      50%
      75%
      max

      NARGUID

      003RTV85
      285.0
      634.652632
      169.271353
      333.0
      515.00
      595.0
      728.00
      1336.0
      28.0
      546.107143
      96.794681
      395.0
      496.25
      525.0
      564.25
      877.0

      007W6H7B
      297.0
      653.491582
      178.164613
      318.0
      515.00
      609.0
      764.00
      1242.0
      26.0
      562.000000
      121.130673
      405.0
      474.75
      541.5
      645.25
      859.0

      00CY2MDM
      284.0
      467.901408
      200.308095
      4.0
      331.00
      414.0
      566.25
      1332.0
      36.0
      347.583333
      92.117743
      116.0
      272.50
      349.0
      405.50
      530.0

      00HEV6HB
      289.0
      450.200692
      228.866837
      6.0
      313.00
      440.0
      527.00
      1688.0
      38.0
      373.947368
      289.887298
      83.0
      256.00
      343.5
      403.00
      1884.0

      00J52GPG
      281.0
      405.978648
      157.648749
      104.0
      284.00
      350.0
      494.00
      1165.0
      36.0
      305.194444
      67.751993
      245.0
      265.00
      285.5
      319.25
      543.0

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      vcahyykd
      0.0
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      0.0
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      ve4u5
      283.0
      451.784452
      132.458823
      196.0
      367.00
      432.0
      515.50
      1053.0
      27.0
      387.851852
      101.128518
      264.0
      303.00
      370.0
      443.50
      595.0

      wzrf2ge6
      275.0
      591.981818
      186.033696
      311.0
      469.50
      547.0
      658.00
      1520.0
      27.0
      502.111111
      102.445232
      247.0
      429.50
      481.0
      573.00
      692.0

      x8k59
      289.0
      488.622837
      143.985096
      210.0
      380.00
      478.0
      565.00
      1081.0
      28.0
      385.892857
      87.299119
      239.0
      321.00
      369.5
      460.75
      561.0

      xxmy9wd8
      252.0
      535.813492
      242.239257
      39.0
      355.25
      483.0
      660.75
      1653.0
      32.0
      382.218750
      122.849694
      126.0
      314.75
      356.5
      450.25
      776.0

8238 rows × 16 columns

```python
rt_desc
```

      go_rt_adjusted
      stop_rt_adjusted

      count
      mean
      std
      min
      25%
      50%
      75%
      max
      count
      mean
      std
      min
      25%
      50%
      75%
      max

      NARGUID

      003RTV85
      285.0
      634.652632
      169.271353
      333.0
      515.00
      595.0
      728.00
      1336.0
      28.0
      546.107143
      96.794681
      395.0
      496.25
      525.0
      564.25
      877.0

      007W6H7B
      297.0
      653.491582
      178.164613
      318.0
      515.00
      609.0
      764.00
      1242.0
      26.0
      562.000000
      121.130673
      405.0
      474.75
      541.5
      645.25
      859.0

      00CY2MDM
      284.0
      467.901408
      200.308095
      4.0
      331.00
      414.0
      566.25
      1332.0
      36.0
      347.583333
      92.117743
      116.0
      272.50
      349.0
      405.50
      530.0

      00HEV6HB
      289.0
      450.200692
      228.866837
      6.0
      313.00
      440.0
      527.00
      1688.0
      38.0
      373.947368
      289.887298
      83.0
      256.00
      343.5
      403.00
      1884.0

      00J52GPG
      281.0
      405.978648
      157.648749
      104.0
      284.00
      350.0
      494.00
      1165.0
      36.0
      305.194444
      67.751993
      245.0
      265.00
      285.5
      319.25
      543.0

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      vcahyykd
      0.0
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      0.0
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      ve4u5
      283.0
      451.784452
      132.458823
      196.0
      367.00
      432.0
      515.50
      1053.0
      27.0
      387.851852
      101.128518
      264.0
      303.00
      370.0
      443.50
      595.0

      wzrf2ge6
      275.0
      591.981818
      186.033696
      311.0
      469.50
      547.0
      658.00
      1520.0
      27.0
      502.111111
      102.445232
      247.0
      429.50
      481.0
      573.00
      692.0

      x8k59
      289.0
      488.622837
      143.985096
      210.0
      380.00
      478.0
      565.00
      1081.0
      28.0
      385.892857
      87.299119
      239.0
      321.00
      369.5
      460.75
      561.0

      xxmy9wd8
      252.0
      535.813492
      242.239257
      39.0
      355.25
      483.0
      660.75
      1653.0
      32.0
      382.218750
      122.849694
      126.0
      314.75
      356.5
      450.25
      776.0

8238 rows × 16 columns

## 3. Inhibition function (p(respond|SSD))

```python
def get_p_resp_per_SSD(data, SSDs):
    data = data.copy()
    out_dict = {}
    for ssd in SSDs:
        curr_data = data.query(
            "SSDDur == %s" % ssd
        )
        if len(curr_data) == 0:
            out_dict[ssd] = np.nan
        else:
            out_dict[ssd] = len(curr_data.query("correct_stop == 0.0")) / len(curr_data)
    return out_dict
```

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')
SSDs = [i for i in abcd_data.SSDDur.unique() if i==i]
ssd_resp_dict = abcd_data.groupby('NARGUID').apply(get_p_resp_per_SSD, SSDs)
ssd_resp_df = ssd_resp_dict.apply(pd.Series)
```

```python
ssd_resp_df
```

      50.0
      100.0
      150.0
      0.0
      200.0
      250.0
      300.0
      350.0
      400.0
      450.0
      500.0
      550.0
      600.0
      650.0
      700.0
      750.0
      800.0
      850.0
      900.0

      NARGUID

      003RTV85
      0.000000
      0.000000
      0.000000
      NaN
      0.333333
      0.090909
      0.666667
      0.500000
      0.454545
      1.0
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      007W6H7B
      0.000000
      0.000000
      0.250000
      NaN
      0.666667
      0.000000
      0.666667
      0.000000
      0.250000
      0.2
      0.571429
      0.75
      0.666667
      0.5
      1.0
      NaN
      NaN
      NaN
      NaN

      00CY2MDM
      0.375000
      0.625000
      0.666667
      0.687500
      1.000000
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      00HEV6HB
      0.666667
      0.500000
      0.250000
      0.888889
      0.500000
      0.250000
      0.750000
      1.000000
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      00J52GPG
      0.631579
      0.700000
      0.750000
      0.500000
      0.500000
      1.000000
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      vcahyykd
      0.000000
      0.000000
      0.000000
      NaN
      0.000000
      0.000000
      0.000000
      0.000000
      0.000000
      0.0
      0.000000
      0.00
      0.000000
      0.0
      0.0
      0.0
      0.0
      0.0
      0.0

      ve4u5
      0.200000
      0.500000
      0.181818
      0.000000
      0.444444
      0.692308
      0.750000
      0.000000
      1.000000
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      wzrf2ge6
      0.000000
      0.000000
      0.000000
      NaN
      0.000000
      0.272727
      0.411765
      0.600000
      0.750000
      1.0
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      x8k59
      0.166667
      0.444444
      0.363636
      0.000000
      0.461538
      0.600000
      0.666667
      0.666667
      1.000000
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      xxmy9wd8
      0.500000
      0.454545
      0.625000
      0.571429
      0.285714
      0.666667
      1.000000
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

8238 rows × 19 columns

```python
ssd_resp_melt = ssd_resp_df.reset_index().melt(id_vars='NARGUID', value_vars=ssd_resp_df.columns, var_name='SSD', value_name='p_respond' )
ssd_resp_melt['underlying distribution'] = 'ABCD data'
ssd_resp_melt.to_csv('abcd_data/abcd_inhib_func_per_sub.csv', index=False)
```

```python
abcd_inhib_func = pd.DataFrame(ssd_resp_df.mean())
abcd_inhib_func.index.name = 'SSD'
abcd_inhib_func.columns = ['p_respond']
abcd_inhib_func = abcd_inhib_func.reset_index()
abcd_inhib_func['underlying distribution'] = 'ABCD data'
```

```python
abcd_inhib_func.to_csv('abcd_data/abcd_inhib_func.csv', index=False)
```

## 4. build run_sims.sh

```python
abcd_data = pd.read_csv('abcd_data/minimal_abcd_clean.csv')
```

```python
narguids = abcd_data.NARGUID.unique()
narguids = [sub for sub in narguids if sub not in problem_subs]
print(len(narguids))
```

    8207

```python
# TACC

nsubs_per_job = 48
njobs_per_node = 36
nlines = 0
with open('batch_files/TACC/run_sims.sh', 'w') as f:
    for start_idx in range(0, len(narguids), nsubs_per_job):
        end_idx = start_idx + nsubs_per_job
        if end_idx > len(narguids):
            end_idx = len(narguids)
        substr = ' '.join(narguids[start_idx:end_idx])
        f.write(f'python simulate_individuals.py --subjects {substr}\n')
        nlines += 1
print(nlines)

N_line_str = '#SBATCH -N %d # number of nodes requested - set to ceil(n rows in command script / 36)\n' % int(np.ceil(nlines/njobs_per_node))
n_line_str = '#SBATCH -n %s # total number of mpi tasks requested - set to n rows in command script\n' % nlines

replace(
    'batch_files/TACC/launch_sim_cmds.slurm',
    '#SBATCH -N',
    N_line_str)
replace(
    'batch_files/TACC/launch_sim_cmds.slurm',
    '#SBATCH -n',
    n_line_str)
# prints so you can compare the lines of the slurm file
print(N_line_str)
print(n_line_str)
```

    171
    #SBATCH -N 5 # number of nodes requested - set to ceil(n rows in command script / 36)

    #SBATCH -n 171 # total number of mpi tasks requested - set to n rows in command script

```python
# SHERLOCK

nsubs_per_job = 72
njobs_per_node = 24

ABCD_LOC = '/oak/stanford/groups/russpold/users/henrymj/ABCD_simulations/abcd_data'
OUT_LOC = '/oak/stanford/groups/russpold/users/henrymj/ABCD_simulations/simulated_data/individual_data'

sher_header = '''#!/bin/bash
#SBATCH --job-name=sims
#SBATCH --output=.out/sims%d.out
#SBATCH --error=.err/sims%d.err
#SBATCH --time=3:00:00
#SBATCH --mail-type=END
#SBATCH --mail-user=henrymj@stanford.edu
#SBATCH -N 1 # number of nodes requested - set to ceil(n rows in command script / 24)
#SBATCH -n %d # total number of mpi tasks requested - set to n rows in command script
#SBATCH -p russpold,normal
# Job Submission
#-----------
export PYTHONPATH=""

source ~/miniconda3/etc/profile.d/conda.sh
conda activate py3-env

'''
def make_sherlock_sim_batch_files(sher_sim_file, suffix):
    nlines = 0
    batch_counter = 0

    file_str = sher_header
    for start_idx in range(0, len(narguids), nsubs_per_job):
        end_idx = start_idx + nsubs_per_job
        if end_idx > len(narguids):
            end_idx = len(narguids)
        substr = ' '.join(narguids[start_idx:end_idx])
        file_str += (f'eval "python ../../scripts/simulate_individuals.py --mu_suffix {suffix} --abcd_dir {ABCD_LOC} --out_dir {OUT_LOC} --subjects {substr}" &\n')
        nlines += 1
        if nlines == 24:
            with open(sher_sim_file % batch_counter, 'w') as f:
                f.write(file_str % (batch_counter, batch_counter, nlines))
                f.write(f'wait\n')
            # reset for new batch file
            file_str = sher_header
            nlines = 0
            batch_counter += 1
    # at end, if things didn't split out evenly, write out the remaining subs
    if nlines > 0:
        with open(sher_sim_file % batch_counter, 'w') as f:
            f.write(file_str % (batch_counter, batch_counter, nlines))
            f.write(f'wait\n')

for SSRT_SCALE in SSRT_SCALES:
    suffix=get_mu_suffix(SSRT_SCALE)
    sher_file = 'batch_files/sherlock/%s/' % suffix + '/sherlock_run_sims_iter%d.batch'
    make_sherlock_sim_batch_files(sher_file, suffix)

# Single Individual Case
suffix_noVar=get_mu_suffix(0,0)
sher_file = 'batch_files/sherlock/%s/' % suffix_noVar + '/sherlock_run_sims_iter%d.batch'
make_sherlock_sim_batch_files(sher_file, suffix_noVar)
```

```python

```


```python
%matplotlib inline
```

PyTorch的简单介绍
***********************
翻译者：www.studyai.com/antares

Torch 的张量库的介绍
======================================

所有的深度学习都是关于张量的计算，它是矩阵的推广，可以在多个维度上索引。
我们将在以后深入了解这意味着什么。首先，让我们看看我们能用张量做些什么。

```python
# Author: Robert Guthrie

import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

torch.manual_seed(1)
```

创建张量
~~~~~~~~~~~~~~~~

可以使用 torch.Tensor() 函数 从Python的list创建张量

```python
# torch.tensor(data) 使用给定的数据创建一个 torch.Tensor 对象
V_data = [1., 2., 3.]
V = torch.tensor(V_data)
print(V)

# 创建一个矩阵
M_data = [[1., 2., 3.], [4., 5., 6]]
M = torch.tensor(M_data)
print(M)

# 创建一个尺寸为 2x2x2 的 3D tensor。
T_data = [[[1., 2.], [3., 4.]],
          [[5., 6.], [7., 8.]]]
T = torch.tensor(T_data)
print(T)
```

什么是三维张量？像这样想一想。如果你有一个向量，索引到向量会给你一个标量。
如果你有一个矩阵，索引到矩阵给你一个向量。
如果你有一个三维张量，那么索引到张量给你一个矩阵！

关于术语的注意事项：当我在本教程中说“张量”时，它指的是任何一个torch.Tensor对象。
矩阵和向量是torch.Tensor的特例，它们的维数分别为1和2。当我谈论三维张量时，我会明确地使用“三维张量(3D tensor)”这个词。

```python
# Index into V and get a scalar (0 dimensional tensor)
print(V[0])
# Get a Python number from it
print(V[0].item())

# Index into M and get a vector
print(M[0])

# Index into T and get a matrix
print(T[0])
```

您还可以创建其他数据类型的张量。如您所见，默认情况是Float。
若要创建整数类型的张量，请尝试 torch.LongTensor()。
查看文档以获得更多的数据类型，但Float和Long将是最常见的。

你可以用随机数据创建一个张量，提供维数,用 torch.randn() 创建。

```python
x = torch.randn((3, 4, 5))
print(x)
```

张量的操作
~~~~~~~~~~~~~~~~~~~~~~~

你可以按你期望的方式对张量进行操作。

```python
x = torch.tensor([1., 2., 3.])
y = torch.tensor([4., 5., 6.])
z = x + y
print(z)
```

有关可供您使用的大量操作的完整列表，请 `参阅文档 <https://pytorch.org/docs/torch.html>`__。
它们不仅仅包括数学运算。

我们稍后将使用的一个有用的操作是串接(concatenation)。

```python
# 默认情况下，它沿着第一个轴(axis)进行串接 (concatenates rows)
x_1 = torch.randn(2, 5)
y_1 = torch.randn(3, 5)
z_1 = torch.cat([x_1, y_1])
print(z_1)

# Concatenate columns:
x_2 = torch.randn(2, 3)
y_2 = torch.randn(2, 5)
# 第二个参数用于指定要沿着哪个轴(axis)进行串接
z_2 = torch.cat([x_2, y_2], 1)
print(z_2)

# 如果你的张量不兼容，torch会抱怨。取消注释以查看错误
# torch.cat([x_1, x_2])
```

重塑(reshape)张量
~~~~~~~~~~~~~~~~~

使用.view()方法重新塑造张量(reshape a tensor)。由于许多神经网络组件期望的输入具有一定的形状(shape)，
这个方法(reshape, .view()方法)得到了大量的应用。通常，在将数据传递给组件之前，您需要进行整形(reshape)。

```python
x = torch.randn(2, 3, 4)
print(x)
print(x.view(2, 12))  # Reshape to 2 rows, 12 columns
# Same as above.  如果其中一个维度是 -1, 它的size将会被推断出来
print(x.view(2, -1))
```

计算图和自动微分
================================================

计算图的概念对于高效的深度学习编程至关重要，因为它允许您不必自己编写反向传播梯度。
计算图只是对如何组合数据以给出输出的一种规范。由于计算图完全指定了涉及哪些操作的参数，
所以它包含了足够的信息来计算导数。这可能听起来很模糊，所以让我们看看使用 基础标志
``requires_grad`` 将会发生什么。

首先，从程序员的角度思考。我们在上面创建的 torch.Tensor 对象中存储的是什么？
很明显，数据和形状，也许还有其他一些东西。但是当我们把两个张量相加时，
我们得到了一个输出张量。这个输出张量只知道它的数据和形状。
它不知道它是另外两个张量的总和(它可能是从文件中读取的，也可能是其他操作的结果，等等)。

如果 ``requires_grad=True``, Tensor 对象就可以跟踪它自己是如何被创建出来的。让我们看看具体的代码吧。

```python
# Tensor factory methods have a ``requires_grad`` flag
x = torch.tensor([1., 2., 3], requires_grad=True)

# With requires_grad=True, you can still do all the operations you previously
# could
y = torch.tensor([4., 5., 6], requires_grad=True)
z = x + y
print(z)

# BUT z knows something extra.
print(z.grad_fn)
```

所以张量知道是什么创造了它们。Z 知道它不是从文件中读取的，它不是乘法或指数之类的结果。
如果你继续跟踪 z.grad_fn，你会在  x和 y 发现自己。

但这如何帮助我们计算梯度呢？

```python
# Lets sum up all the entries in z
s = z.sum()
print(s)
print(s.grad_fn)
```

那么，这个和(sum)相对于x的第一个分量的导数是什么呢？在数学方面，我们想:

\begin{align}\frac{\partial s}{\partial x_0}\end{align}

好吧, s 知道它是被作为张量 z 的和所创建的。z 知道它是 x + y 的和，因此：

\begin{align}s = \overbrace{x_0 + y_0}^\text{$z_0$} + \overbrace{x_1 + y_1}^\text{$z_1$} + \overbrace{x_2 + y_2}^\text{$z_2$}\end{align}

因此 s 包含了足够的信息去计算出我们想要的导数就是 1 !!

当然，这掩盖了如何实际计算导数的挑战。这里的要点是，s携带了足够多的信息，因此可以计算它。
在现实中，Pytorch 的开发人员对sum()和+操作进行编程，以了解如何计算它们的梯度，
并运行反向传播算法。对该算法的深入讨论超出了本教程的范围.

让Pytorch计算梯度，看看我们是对的：(注意，如果您多次运行这个块，梯度就会增加。
这是因为Pytorch将梯度 **累加** 到 .grad 属性中，因为对于许多模型来说，这非常方便。)

```python
# 调用任意变量的 .backward() 将会执行反向传播(backprop), 从该变量开始.
s.backward()
print(x.grad)
```

理解下面这个模块中正在发生的事情对于成为一名成功的深度学习的程序员来说是至关重要的。

```python
x = torch.randn(2, 2)
y = torch.randn(2, 2)
# 默认情况下，用户创建的 Tensors 的 ``requires_grad=False``
print(x.requires_grad, y.requires_grad)
z = x + y
# So you can't backprop through z
print(z.grad_fn)

# ``.requires_grad_( ... )`` 会原位改变一个已经存在的张量的 ``requires_grad`` 标记
# The input flag defaults to ``True`` if not given.
x = x.requires_grad_()
y = y.requires_grad_()
# z 包含了足够的信息来计算梯度, as we saw above
z = x + y
print(z.grad_fn)
# If any input to an operation has ``requires_grad=True``, so will the output
print(z.requires_grad)

# 现在 z 拥有它自己和x以及y相关联的计算历史，
# 我们可以只接受它的值, 而把它从它的历史中 **detach** 出来吗?
new_z = z.detach()

# ... 那么 new_z 有信息可以反向传播到 x 和 y 吗?  **没有**
print(new_z.grad_fn)
# 那么为啥会这样的呢? ``z.detach()`` 返回一个与 ``z`` 共享存储空间的张量，但是把 ``z`` 上的
# 计算历史全忘记了(扔了)。 new_z 根本不知道它是如何被计算出来的。
# 从本质上说，我们已经把张量从过去的历史中剥离出来了。
```

还可以通过将代码块包装在 ``with torch.no_grad():`` 中来阻止autograd在
``.requires_grad``=True 中跟踪张量上的历史记录：

```python
print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
	print((x ** 2).requires_grad)
```


# Jupyter (iPython) Notebookを使って技術ノート環境を構築する方法

myenigma.hatenablog.com

```python
from sympy import *
```

```python
x=Symbol('x')
```

```python
%matplotlib inline
```

```python
init_printing()
```

```python
expand((x - 3)**5)
```

```python

```

しかし、下記のJupyter Notebooksのextensionをインストールすることで、

様々な拡張機能が使えるようになり、

画像のドラッグアンドドロップもできるようになります。

インストールは、READMEにある通り、

下記のコマンドでOKです。

(下記のインストールをする場合は

Jupyter Notebookのプロセスはkillしておきましょう)

$ pip install https://github.com/ipython-contrib/IPython-notebook-extensions/archive/master.zip –user

続いて、Jupyter Notebookを起動し、

```python

```

```python

```

```python

```

URLに/nbextensions/を追加して、移動すると

下記のような拡張機能の追加画面に移動します。

あとは、Drag and DropのチェックボタンをONして下さい。

すると、Jupyter Notebookで画像をドラッグアンドドロップすると、

画像が挿入されるようになります。

ちなみに画像ファイルはD＆Dすると

.ipynbファイルと

同じ場所にコピーされます。

目次を見出し情報から自動生成する
上記のIPython-notebook-extensionsの一機能を使うことで、

Markdownの見出し情報から、

上記のように自動で目次を生成することができます。

使い方としては、

先ほどの/nbextensions/の設定画面で、

Table Contents(2)にチェックをいれます。

加えて、同じ設定画面で、

“Add a Table of Contents at the top of the notebook”

にチェックをいれます。

最後に下記のように

目次ウインドウを表示させて、

tボタンを押すと、

自動的に目次情報が先頭のセルに追加されます。

レポートタイトルを入力する方法
残念ながら、今のところ

レポートのタイトルのようなものを

入力するツールは見つかっていませんが、

JupyterのMarkdownのモードでは、

HTMLを入力するとマークアップされた文字を入力できます。

この機能を使うことで、レポートタイトルっぽい文字を表示できました。

例えば、下記のようなhtmlを

JupyterのMarkdownモードのセルに入力すると、

Jupyter Report

Atsushi Sakai

下記のように表示されます。

PDFに出力する
上記の方法である程度のレポート形式の技術ノートを作ることは

できるはずなので、あとはPDFなどに変換すれば、

プログラマ以外の他の人と資料を共有することができます。

Macの場合
Jupyter NotebookのデータをPDFに変換する方法は色々ありますが、

Macで一番シンプルなのは、Jupyter Notebookをブラウザで開いた状態で、

Ctrl-Pで、Macの標準機能でPDFに変換するのが一番簡単だと思います。

下記のように詳細設定の部分で、ヘッダーとフッターに

時刻や、ファイル名、ページなどを入力する設定ができます。

あとは普通にPDFを出力すると、

下記のようにそれなりのレポートがPDFで作成できます。

Jupyterでプレゼン資料を作る方法
下記を参照下さい。

myenigma.hatenablog.com

JupyterのサーバをHerokuの無料枠で構築する方法
下記を参照下さい。

myenigma.hatenablog.com

Jupyter Markdown数式の入力テンプレート
最近、数式の入力はすべてJupyterで実施していますが、

複雑でいつもググっている数式コマンドをメモとして残しておきます。

行列
\begin{equation*}
\begin{bmatrix}
1 & 0 & \cdots \\
0 & 1 & \cdots \\
\vdots & \vdots & \ddots \\
-1 & 0 & \cdots \\
0 & -1 & \cdots \\
\vdots & \vdots & \ddots \\
\end{bmatrix}
\end{equation*}
数式参考リンク
できれば改善してもらいたい部分
もう少しで完全にWordをおさらばできそうなので、

下記のような機能を実現してもらいたいですね。

参考文献管理

図のタイトル入力

自動図番号割り当て

上記のような機能を実現できる拡張などを知っている方は

コメントなどで教えて頂けると幸いです。

(Python使えるから、自分で作ればいいのか。。。)

vimユーザのためのJupyterプラグイン
下記を参照下さい

lambdalisue.hatenablog.com

なにかおかしいので云々。
bookmark

```python

```

```python

```

```python

```

```python

```

```python

```

上記のプラグインを使うことで、

Jupyterのエディタの部分でvimのキーバインドが使えます。

Jupyter Notebook上でコードの処理時間を計測する
Jupyter notebookでは、

%timeit

の後にコードを記述すると、そのコードの計算時間を計測してくれます。

コードやアルゴリズムのレポートに便利ですね。

moqada.hatenablog.com

Jupyter上でモジュールや関数のdocstringを確認する
Jupyter notebookでは、

モジュールや関数の後ろに?(はてな)をつけると、

docstringを表示してくれるので、

マニュアル確認も簡単です。

更にJupyterを学びたい人は
下記の資料がおすすめです。

また、どうしても解決できない問題などが

ある場合は下記のようなQ＆Aサイトで質問してみると、

かなりの確率で回答がもらえると思います。

参考資料
myenigma.hatenablog.com

myenigma.hatenablog.com

myenigma.hatenablog.com

myenigma.hatenablog.com

myenigma.hatenablog.com

MyEnigma Supporters
もしこの記事が参考になり、

ブログをサポートしたいと思われた方は、

こちらからよろしくお願いします。


```python
from sympy import *
x = var('X')

%matplotlib inline
```

```python
!pip install seaborn
import seaborn as sns
```

    Requirement already satisfied: seaborn in ./anaconda3/lib/python3.7/site-packages (0.9.0)
    Requirement already satisfied: numpy>=1.9.3 in ./anaconda3/lib/python3.7/site-packages (from seaborn) (1.15.4)
    Requirement already satisfied: matplotlib>=1.4.3 in ./anaconda3/lib/python3.7/site-packages (from seaborn) (3.0.2)
    Requirement already satisfied: scipy>=0.14.0 in ./anaconda3/lib/python3.7/site-packages (from seaborn) (1.1.0)
    Requirement already satisfied: pandas>=0.15.2 in ./anaconda3/lib/python3.7/site-packages (from seaborn) (0.23.4)
    Requirement already satisfied: cycler>=0.10 in ./anaconda3/lib/python3.7/site-packages (from matplotlib>=1.4.3->seaborn) (0.10.0)
    Requirement already satisfied: kiwisolver>=1.0.1 in ./anaconda3/lib/python3.7/site-packages (from matplotlib>=1.4.3->seaborn) (1.0.1)
    Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in ./anaconda3/lib/python3.7/site-packages (from matplotlib>=1.4.3->seaborn) (2.3.0)
    Requirement already satisfied: python-dateutil>=2.1 in ./anaconda3/lib/python3.7/site-packages (from matplotlib>=1.4.3->seaborn) (2.7.5)
    Requirement already satisfied: pytz>=2011k in ./anaconda3/lib/python3.7/site-packages (from pandas>=0.15.2->seaborn) (2018.7)
    Requirement already satisfied: six in ./anaconda3/lib/python3.7/site-packages (from cycler>=0.10->matplotlib>=1.4.3->seaborn) (1.12.0)
    Requirement already satisfied: setuptools in ./anaconda3/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib>=1.4.3->seaborn) (40.6.3)

```python
sns.set(style='ticks',palette='Set2')
```

```python
frecuencias = np.array([0.1,0.2,0.5,0.6])
plt.figure(figsize=(7,4))
for f in frecuencias:
    plt.plot(t,0.5 * np.cos(f*t) + 0.1 * np.sin(f*t),'o-',
             label = '$\omega_0 = %s$'%f)
plt.xlabel('$t$',fontsize=16)
plt.ylabel('$x(t)$',fontsize=16)
plt.title('oscilaciones',fontsize=16)
plt.legend(loc='center left',bbox_to_anchor=(1.05,0.5),prop={'size':14})
plt.show
```

```python
from ipywidgets import *
```

```python
def masa_resorte(t=0):
    fig=plt
```