Skip to content

Commit

Permalink
update slope calculations to be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
divyaramamoorthy committed Jun 25, 2022
1 parent df255a2 commit 741bc17
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 77 deletions.
17 changes: 12 additions & 5 deletions analysis/analysis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,15 @@ def pred_single_gp(x_train, y_train, x_pred):
###########################################################################
####### MoGP Analysis Functions #######
###########################################################################
def calc_slope_mogp_data(data):
def calc_slope_mogp_data(data, include_anchor=False):
"""Calculate average slope of each patient from data dictionary (exclude onset anchor)"""
XA = data['XA'][:, 1:] # Exclude anchor oonset
# YA_nonorm = (data['YA'][:, 1:] * data['Y_std']) + data['Y_mean'] # Scale data to original
YA = data['YA'][:, 1:]
if include_anchor:
XA = data['XA']
YA = data['YA']
else:
XA = data['XA'][:, 1:] # Exclude anchor oonset
# YA_nonorm = (data['YA'][:, 1:] * data['Y_std']) + data['Y_mean'] # Scale data to original
YA = data['YA'][:, 1:]

df_slope = pd.DataFrame(columns=['SI', 'slope'])
for i, si in enumerate(data['SI']):
Expand Down Expand Up @@ -331,7 +335,7 @@ def format_panel_axs(ax, alph_lab, num_pat, k_alph_flag, fontsize_numpat=20, fon
ax.text(0.97, 0.95, alph_lab, transform=ax.transAxes, va='top', ha='right', fontsize=fontsize_alph)
return ax

def plot_mogp_panel(model, data, disp_clust=12, k_alph_flag=True, mogp_color='b', slope_color='r'):
def plot_mogp_panel(model, data, disp_clust=12, k_alph_flag=True, mogp_color='b', slope_color='r', add_full_slope=False):
"""Plot full panel, including calculating slope per cluster"""
fig, axs = plt.subplots(math.ceil(disp_clust/4), 4, figsize=(20, 3*(math.ceil(disp_clust/4))), sharex=True, sharey=True)

Expand All @@ -347,6 +351,9 @@ def plot_mogp_panel(model, data, disp_clust=12, k_alph_flag=True, mogp_color='b'

axs.flat[i], num_pat = plot_mogp_by_clust(axs.flat[i], model, data, k, data_col='k', model_col=mogp_color)
estim_diff = plot_slope_by_clust(axs.flat[i], model, k, slope_col=slope_color)
if add_full_slope:
max_x = model.obsmodel[k].X.max()
_ = plot_slope_by_clust(axs.flat[i], model, k, slope_col='g', upper_bound=max_x)
axs.flat[i] = format_panel_axs(axs.flat[i], k_alph, num_pat, k_alph_flag)

df_disp_clust = df_disp_clust.append({'k': k, 'k_alph': k_alph, 'estim_diff': estim_diff}, ignore_index=True)
Expand Down
17 changes: 8 additions & 9 deletions analysis/nonals_domains.ipynb

Large diffs are not rendered by default.

56 changes: 36 additions & 20 deletions analysis/plot_mogp_full_panel_figure.ipynb

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions analysis/summ_stats_mogp_table.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
"metadata": {},
"outputs": [],
"source": [
"def initial_slope(df):\n",
" # Calculate slope as: (48-score at baseline)/time between onset and baseline in months.) \n",
" xa_months = df['XA'][:,1]*12\n",
" slopes = -((48-df['YA'][:,1])/xa_months)\n",
"# def initial_slope(df):\n",
"# # Calculate slope as: (48-score at baseline)/time between onset and baseline in months.) \n",
"# xa_months = df['XA'][:,1]*12\n",
"# slopes = -((48-df['YA'][:,1])/xa_months)\n",
" \n",
" return(slopes)"
"# return(slopes)"
]
},
{
Expand Down Expand Up @@ -80,8 +80,8 @@
" months_follow = ((np.nanmax(XA, axis=1)-np.nanmin(XA, axis=1))*12)\n",
" med_months_follow = \"{:.2f} ({:.2f})\".format(np.median(months_follow), iqr(months_follow))\n",
"\n",
"# slope_df = calc_slope_mogp_data(cur_dat).dropna(how='any')['slope'] \n",
" slope_df = initial_slope(cur_dat)\n",
" slope_df = calc_slope_mogp_data(cur_dat, include_anchor=True).dropna(how='any')['slope'] \n",
"# slope_df = initial_slope(cur_dat)\n",
" med_slope = \"{:.2f} ({:.2f})\".format(np.median(slope_df), iqr(slope_df))\n",
"\n",
" df_proj_sum = df_proj_sum.append({'dataset': proj, 'study_type':study_type, 'total_pats': num_pats, 'med_num_vis': med_num_visits, 'med_month_follow':med_months_follow, 'med_slope':med_slope}, ignore_index=True)\n",
Expand Down Expand Up @@ -156,7 +156,7 @@
" <td>2923.0</td>\n",
" <td>9 (7)</td>\n",
" <td>11.95 (4.63)</td>\n",
" <td>-0.53 (0.52)</td>\n",
" <td>-0.67 (0.64)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
Expand All @@ -165,7 +165,7 @@
" <td>907.0</td>\n",
" <td>5 (3)</td>\n",
" <td>17.24 (19.56)</td>\n",
" <td>-0.55 (0.66)</td>\n",
" <td>-0.65 (0.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
Expand All @@ -174,7 +174,7 @@
" <td>476.0</td>\n",
" <td>9 (7)</td>\n",
" <td>16.80 (14.40)</td>\n",
" <td>-0.62 (0.57)</td>\n",
" <td>-0.84 (0.63)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
Expand All @@ -183,7 +183,7 @@
" <td>456.0</td>\n",
" <td>5 (3)</td>\n",
" <td>13.79 (10.55)</td>\n",
" <td>-0.49 (0.63)</td>\n",
" <td>-0.55 (0.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
Expand All @@ -192,7 +192,7 @@
" <td>399.0</td>\n",
" <td>4 (3)</td>\n",
" <td>15.04 (13.76)</td>\n",
" <td>-0.74 (0.79)</td>\n",
" <td>-0.89 (0.67)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
Expand All @@ -214,11 +214,11 @@
"2 4 (3) 15.04 (13.76) \n",
"\n",
" Median (IQR) ALSFRS-R Slope \n",
"3 -0.53 (0.52) \n",
"4 -0.55 (0.66) \n",
"1 -0.62 (0.57) \n",
"0 -0.49 (0.63) \n",
"2 -0.74 (0.79) "
"3 -0.67 (0.64) \n",
"4 -0.65 (0.62) \n",
"1 -0.84 (0.63) \n",
"0 -0.55 (0.62) \n",
"2 -0.89 (0.67) "
]
},
"execution_count": 6,
Expand Down Expand Up @@ -282,7 +282,7 @@
" <td>2814.0</td>\n",
" <td>9 (8)</td>\n",
" <td>11.95 (4.43)</td>\n",
" <td>-0.52 (0.51)</td>\n",
" <td>-0.66 (0.63)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
Expand All @@ -292,7 +292,7 @@
" <td>714.0</td>\n",
" <td>6 (3)</td>\n",
" <td>20.00 (20.66)</td>\n",
" <td>-0.51 (0.60)</td>\n",
" <td>-0.61 (0.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
Expand All @@ -302,7 +302,7 @@
" <td>453.0</td>\n",
" <td>10 (7)</td>\n",
" <td>18.00 (13.20)</td>\n",
" <td>-0.61 (0.55)</td>\n",
" <td>-0.81 (0.59)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
Expand All @@ -312,7 +312,7 @@
" <td>341.0</td>\n",
" <td>5 (2)</td>\n",
" <td>15.86 (11.49)</td>\n",
" <td>-0.42 (0.50)</td>\n",
" <td>-0.49 (0.57)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
Expand All @@ -322,7 +322,7 @@
" <td>283.0</td>\n",
" <td>5 (3)</td>\n",
" <td>18.95 (14.94)</td>\n",
" <td>-0.70 (0.72)</td>\n",
" <td>-0.86 (0.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
Expand All @@ -332,7 +332,7 @@
" <td>1327.0</td>\n",
" <td>14 (5)</td>\n",
" <td>13.46 (3.97)</td>\n",
" <td>-0.53 (0.49)</td>\n",
" <td>-0.66 (0.58)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
Expand All @@ -342,7 +342,7 @@
" <td>228.0</td>\n",
" <td>13 (5)</td>\n",
" <td>25.20 (10.80)</td>\n",
" <td>-0.51 (0.48)</td>\n",
" <td>-0.69 (0.46)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
Expand All @@ -352,7 +352,7 @@
" <td>132.0</td>\n",
" <td>12 (6)</td>\n",
" <td>44.76 (24.52)</td>\n",
" <td>-0.40 (0.42)</td>\n",
" <td>-0.42 (0.33)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
Expand Down Expand Up @@ -380,14 +380,14 @@
"7 132.0 12 (6) \n",
"\n",
" Median (IQR) Months Followed Median (IQR) ALSFRS-R Slope \n",
"0 11.95 (4.43) -0.52 (0.51) \n",
"1 20.00 (20.66) -0.51 (0.60) \n",
"2 18.00 (13.20) -0.61 (0.55) \n",
"3 15.86 (11.49) -0.42 (0.50) \n",
"4 18.95 (14.94) -0.70 (0.72) \n",
"5 13.46 (3.97) -0.53 (0.49) \n",
"6 25.20 (10.80) -0.51 (0.48) \n",
"7 44.76 (24.52) -0.40 (0.42) "
"0 11.95 (4.43) -0.66 (0.63) \n",
"1 20.00 (20.66) -0.61 (0.62) \n",
"2 18.00 (13.20) -0.81 (0.59) \n",
"3 15.86 (11.49) -0.49 (0.57) \n",
"4 18.95 (14.94) -0.86 (0.62) \n",
"5 13.46 (3.97) -0.66 (0.58) \n",
"6 25.20 (10.80) -0.69 (0.46) \n",
"7 44.76 (24.52) -0.42 (0.33) "
]
},
"execution_count": 7,
Expand Down Expand Up @@ -654,7 +654,7 @@
" <td>2923.0</td>\n",
" <td>9 (7)</td>\n",
" <td>11.95 (4.63)</td>\n",
" <td>-0.53 (0.52)</td>\n",
" <td>-0.67 (0.64)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
Expand All @@ -669,7 +669,7 @@
" <td>907.0</td>\n",
" <td>5 (3)</td>\n",
" <td>17.24 (19.56)</td>\n",
" <td>-0.55 (0.66)</td>\n",
" <td>-0.65 (0.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
Expand All @@ -684,7 +684,7 @@
" <td>476.0</td>\n",
" <td>9 (7)</td>\n",
" <td>16.80 (14.40)</td>\n",
" <td>-0.62 (0.57)</td>\n",
" <td>-0.84 (0.63)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
Expand All @@ -699,7 +699,7 @@
" <td>456.0</td>\n",
" <td>5 (3)</td>\n",
" <td>13.79 (10.55)</td>\n",
" <td>-0.49 (0.63)</td>\n",
" <td>-0.55 (0.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
Expand All @@ -714,7 +714,7 @@
" <td>399.0</td>\n",
" <td>4 (3)</td>\n",
" <td>15.04 (13.76)</td>\n",
" <td>-0.74 (0.79)</td>\n",
" <td>-0.89 (0.67)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
Expand Down Expand Up @@ -750,11 +750,11 @@
"2 399.0 4 (3) \n",
"\n",
" Median (IQR) Months Followed Median (IQR) ALSFRS-R Slope \n",
"3 11.95 (4.63) -0.53 (0.52) \n",
"4 17.24 (19.56) -0.55 (0.66) \n",
"1 16.80 (14.40) -0.62 (0.57) \n",
"0 13.79 (10.55) -0.49 (0.63) \n",
"2 15.04 (13.76) -0.74 (0.79) "
"3 11.95 (4.63) -0.67 (0.64) \n",
"4 17.24 (19.56) -0.65 (0.62) \n",
"1 16.80 (14.40) -0.84 (0.63) \n",
"0 13.79 (10.55) -0.55 (0.62) \n",
"2 15.04 (13.76) -0.89 (0.67) "
]
},
"execution_count": 14,
Expand Down

0 comments on commit 741bc17

Please sign in to comment.