# ===========================================================================
# 7. Dose-response fitting & plotting — BOTH metrics
# ===========================================================================
METRICS = {
'FoldD0': {
'col': 'FoldD0',
'ylabel_dr': 'Fold D0 (Confluence)',
'ylabel_pv': '% Viable (Fold-D0 normalized)',
},
'TotalCellArea': {
'col': 'TotalCellArea',
'ylabel_dr': 'Total Cell Area (mm²)',
'ylabel_pv': '% Viable (TCA normalized)',
},
}
all_ic50_tables = {}
# x-axis window derived from user-defined scale bounds
_xlo = PSEUDO_CONC if SCALE_MIN_CONC == 0 else SCALE_MIN_CONC
_xhi = CONC_MAX if SCALE_MAX_CONC is None else SCALE_MAX_CONC
def _analysis_rows(sub_df):
"""Rows allowed to affect scaling, fitted curves, and IC50 calculations."""
return sub_df[~sub_df['Exclude_25pct_Reversal']].copy()
def _axis_limits(sub_df, col, xlo, xhi, pad=0.10):
"""Y-limits from data within [xlo, xhi], honouring SCALE_EXCL_ABS."""
vis = _analysis_rows(sub_df[(sub_df['Conc'] >= xlo) & (sub_df['Conc'] <= xhi)])
if SCALE_EXCL_ABS and 'Drug' in vis.columns:
vis = vis[~vis['Drug'].str.split('-').str[0].isin(SCALE_EXCL_ABS)]
if vis.empty:
return None
m = vis.groupby('Conc')[col].mean()
s = vis.groupby('Conc')[col].std().fillna(0)
lo, hi = (m - s).min(), (m + s).max()
mg = (hi - lo) * pad if hi > lo else max(abs(hi), abs(lo)) * pad + 0.1
return lo - mg, hi + mg
for metric_name, minfo in METRICS.items():
col = minfo['col']
print(f'\n{"="*60}')
print(f'=== METRIC: {metric_name} ===')
print(f'{"="*60}')
# --- 7a. Dose-response plots (raw metric) ---
print(f'\n--- Dose-response plots ({metric_name}) ---')
fit_params_dr = {}
ic50_dr_records = []
for drug in all_drugs:
for day in fit_days:
sub = df[(df.Drug == drug) & (df.Day == day)].copy()
sub = sub.dropna(subset=['Conc', col])
sub = sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)]
sub = _analysis_rows(sub)
if sub.empty:
continue
popt, r2 = fit_ll4(sub.Conc.values, sub[col].values)
if popt is not None:
fit_params_dr[(drug, day)] = popt
ic50_dr_records.append({
'Drug': drug, 'Day': day,
'IC50_nM': popt[3], 'Slope': popt[0],
'Lower': popt[1], 'Upper': popt[2], 'R2': r2,
})
else:
ic50_dr_records.append({
'Drug': drug, 'Day': day,
'IC50_nM': np.nan, 'Slope': np.nan,
'Lower': np.nan, 'Upper': np.nan, 'R2': np.nan,
})
for day in fit_days:
# Pre-scan: y-range across all drugs within the visible x-window
_all_vis = pd.concat([
df[(df.Drug == d) & (df.Day == day)].dropna(subset=[col])
for d in all_drugs
], ignore_index=True)
_ylim_dr = _axis_limits(_all_vis, col, _xlo, _xhi)
fig, ax = plt.subplots(figsize=(12, 7))
for drug in all_drugs:
sub = df[(df.Drug == drug) & (df.Day == day)].copy()
sub = sub.dropna(subset=[col])
if sub.empty:
continue
means = sub.groupby('Conc')[col].mean()
sds = sub.groupby('Conc')[col].std()
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, color=drug_colors[drug], label=drug,
linestyle='none', linewidth=1.0)
key = (drug, day)
if key in fit_params_dr:
popt = fit_params_dr[key]
curve_sub = _analysis_rows(sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)])
x_pred = np.geomspace(curve_sub.Conc.min(), curve_sub.Conc.max(), 200)
y_pred = ll4(x_pred, *popt)
ax.plot(x_pred, y_pred, color=drug_colors[drug], linewidth=1.5,
linestyle=drug_linestyles[drug])
ax.set_xscale('log')
if _ylim_dr:
ax.set_ylim(*_ylim_dr)
ax.set_xlabel('Concentration (nM, log scale)')
ax.set_ylabel(minfo['ylabel_dr'])
ax.set_title(f'Day {day} — Dose Response ({metric_name})')
ax.legend(fontsize=8, loc='best')
plt.tight_layout()
save_fig()
df_ic50_dr = pd.DataFrame(ic50_dr_records)
df_ic50_dr['Reliable'] = (
(df_ic50_dr['R2'] >= R2_THRESHOLD) &
(df_ic50_dr['IC50_nM'] >= CONC_MIN * 0.5) &
(df_ic50_dr['IC50_nM'] <= CONC_MAX * 2)
)
df_ic50_dr['Status'] = df_ic50_dr.apply(classify_ic50, axis=1)
print(f'\n IC50 ({metric_name} direct): {df_ic50_dr.Reliable.sum()} reliable '
f'of {len(df_ic50_dr)}')
# --- 7b. %Viable calculation ---
pv_col = f'PctViable_{metric_name}'
untx_mean = (
df[df.Conc_orig == 0]
.groupby(['Plate', 'Drug', 'Day'])[col]
.mean()
.reset_index()
.rename(columns={col: f'UntxMean_{metric_name}'})
)
untx_col = f'UntxMean_{metric_name}'
df = df.merge(untx_mean, on=['Plate', 'Drug', 'Day'], how='left')
df[pv_col] = 100.0 * df[col] / df[untx_col]
# --- 7c. Absolute IC50: interpolated crossing at 50% of untreated ---
interp_records = []
for drug in all_drugs:
for day in fit_days:
sub = df[(df.Drug == drug) & (df.Day == day)].copy()
sub = sub.dropna(subset=['Conc', pv_col])
sub = sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)]
sub = _analysis_rows(sub)
if sub.empty:
continue
ic50_interp = interpolate_ic50(sub.Conc, sub[pv_col], target=50.0)
interp_records.append({
'Drug': drug, 'Day': day,
'Absolute_IC50_nM': ic50_interp, 'IC50_interp': ic50_interp,
})
df_interp = pd.DataFrame(interp_records)
all_ic50_tables[f'{metric_name}_interpolated'] = df_interp
print(f'\n Absolute IC50 at 50% untreated ({metric_name}): '
f'{df_interp.IC50_interp.notna().sum()} of {len(df_interp)} crossed 50%')
# --- 7d. Fitted IC50: LL.4 midpoint between fitted upper/lower plateaus ---
fit_params_pv = {}
ic50_pv_records = []
for drug in all_drugs:
for day in fit_days:
sub = df[(df.Drug == drug) & (df.Day == day)].copy()
sub = sub.dropna(subset=['Conc', pv_col])
sub = sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)]
sub = _analysis_rows(sub)
if sub.empty:
continue
popt, r2 = fit_ll4_pct_viable(sub.Conc.values, sub[pv_col].values)
if popt is not None:
fit_params_pv[(drug, day)] = popt
ic50_pv_records.append({
'Drug': drug, 'Day': day,
'IC50_nM': popt[3], 'Fitted_IC50_nM': popt[3],
'Midpoint_Response': (popt[1] + popt[2]) / 2.0,
'Slope': popt[0], 'Lower': popt[1], 'Upper': popt[2], 'R2': r2,
})
else:
ic50_pv_records.append({
'Drug': drug, 'Day': day,
'IC50_nM': np.nan, 'Fitted_IC50_nM': np.nan,
'Midpoint_Response': np.nan, 'Slope': np.nan,
'Lower': np.nan, 'Upper': np.nan, 'R2': np.nan,
})
# --- 7e. %Viable dose-response plots with absolute IC50 markers ---
for day in fit_days:
# Pre-scan: y-range across all drugs within the visible x-window
_all_vis_pv = pd.concat([
df[(df.Drug == d) & (df.Day == day)].dropna(subset=[pv_col])
for d in all_drugs
], ignore_index=True)
_ylim_pv = _axis_limits(_all_vis_pv, pv_col, _xlo, _xhi)
if _ylim_pv:
_ylim_pv = (0, _ylim_pv[1])
fig, ax = plt.subplots(figsize=(12, 7))
_absolute_marker_labeled = False
for drug in all_drugs:
sub = df[(df.Drug == drug) & (df.Day == day)].copy()
sub = sub.dropna(subset=[pv_col])
if sub.empty:
continue
means = sub.groupby('Conc')[pv_col].mean()
sds = sub.groupby('Conc')[pv_col].std()
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, color=drug_colors[drug], label=drug,
linestyle='none', linewidth=1.0)
# Overlay LL.4 fitted curve if available
key = (drug, day)
if key in fit_params_pv:
popt = fit_params_pv[key]
curve_sub = _analysis_rows(sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)])
x_pred = np.geomspace(curve_sub.Conc.min(), curve_sub.Conc.max(), 200)
y_pred = ll4(x_pred, *popt)
ax.plot(x_pred, y_pred, color=drug_colors[drug], linewidth=1.5,
linestyle=drug_linestyles[drug])
# Mark absolute IC50: concentration crossing 50% of untreated
row = df_interp[(df_interp.Drug == drug) & (df_interp.Day == day)]
if not row.empty and pd.notna(row.IC50_interp.values[0]):
ic50_val = row.IC50_interp.values[0]
ax.plot(ic50_val, 50, 'D', color=drug_colors[drug],
markersize=10, markeredgecolor='black', markeredgewidth=1,
label='Absolute IC50 (50% untreated)'
if not _absolute_marker_labeled else None,
zorder=5)
_absolute_marker_labeled = True
ax.set_xscale('log')
if _ylim_pv:
ax.set_ylim(*_ylim_pv)
ax.set_xlabel('Concentration (nM, log scale)')
ax.set_ylabel(minfo['ylabel_pv'])
ax.set_title(f'Day {day} — %Viable ({metric_name}) — Absolute IC50 at 50% untreated')
ax.axhline(50, color='red', ls='--', alpha=0.7, label='50% viable')
ax.axhline(100, color='grey', ls='--', alpha=0.3)
ax.legend(fontsize=8, loc='best')
plt.tight_layout()
save_fig()
# --- 7f. %Viable dose-response plots with fitted-midpoint IC50 markers ---
for day in fit_days:
_all_vis_pv = pd.concat([
df[(df.Drug == d) & (df.Day == day)].dropna(subset=[pv_col])
for d in all_drugs
], ignore_index=True)
_ylim_pv = _axis_limits(_all_vis_pv, pv_col, _xlo, _xhi, pad=0.05)
fig, ax = plt.subplots(figsize=(12, 7))
_fitted_marker_labeled = False
for drug in all_drugs:
sub = df[(df.Drug == drug) & (df.Day == day)].copy()
sub = sub.dropna(subset=[pv_col])
if sub.empty:
continue
means = sub.groupby('Conc')[pv_col].mean()
sds = sub.groupby('Conc')[pv_col].std()
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, color=drug_colors[drug], label=drug,
linestyle='none', linewidth=1.0)
key = (drug, day)
if key in fit_params_pv:
popt = fit_params_pv[key]
curve_sub = _analysis_rows(sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)])
x_pred = np.geomspace(curve_sub.Conc.min(), curve_sub.Conc.max(), 200)
y_pred = ll4(x_pred, *popt)
ax.plot(x_pred, y_pred, color=drug_colors[drug], linewidth=1.5,
linestyle=drug_linestyles[drug])
fitted_ic50 = popt[3]
fitted_midpoint = (popt[1] + popt[2]) / 2.0
if _xlo <= fitted_ic50 <= _xhi:
ax.plot(fitted_ic50, fitted_midpoint, 'D', color=drug_colors[drug],
markersize=10, markeredgecolor='black', markeredgewidth=1,
label='Fitted IC50 (LL.4 plateau midpoint)'
if not _fitted_marker_labeled else None,
zorder=5)
_fitted_marker_labeled = True
ax.set_xscale('log')
if _ylim_pv:
ax.set_ylim(*_ylim_pv)
ax.set_xlabel('Concentration (nM, log scale)')
ax.set_ylabel(minfo['ylabel_pv'])
ax.set_title(f'Day {day} — %Viable ({metric_name}) — Fitted IC50 at LL.4 plateau midpoint')
ax.legend(fontsize=8, loc='best')
plt.tight_layout()
save_fig()
df_ic50_pv = pd.DataFrame(ic50_pv_records)
df_ic50_pv['Reliable'] = (
(df_ic50_pv['R2'] >= R2_THRESHOLD) &
(df_ic50_pv['IC50_nM'] >= CONC_MIN * 0.5) &
(df_ic50_pv['IC50_nM'] <= CONC_MAX * 2)
)
df_ic50_pv['Status'] = df_ic50_pv.apply(classify_ic50, axis=1)
print(f' Fitted-midpoint IC50 (%Viable {metric_name}): {df_ic50_pv.Reliable.sum()} reliable '
f'of {len(df_ic50_pv)}')
all_ic50_tables[f'{metric_name}_direct'] = df_ic50_dr
all_ic50_tables[f'{metric_name}_pctViable'] = df_ic50_pv