Show code
from google.colab import drive
drive.mount('/content/drive')Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Raw metric: Confluence (Day 7 only) — no Day-0 normalization. Responses are normalized to the untreated wells of the same plate/drug (%Viable). IC50 computed two ways: fitted LL.4 plateau midpoint and absolute 50% of untreated by direct interpolation.
from google.colab import drive
drive.mount('/content/drive')Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
# ====================================================================================
# USER INPUT — Navigate to experiment folder shortcut; copy relative path to DATA_DIR
# ====================================================================================
DATA_DIR = '/content/drive/MyDrive/20260910 Cytotox - 63VPE vs 64VPE6/Capan WST8-20260918/Data'from IPython.display import display, Markdown, Image as IPImage
import os
import re
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
sns.set_style('whitegrid')
plt.rcParams.update({'figure.figsize': (10, 6), 'font.size': 12})
# =====================================================================
# USER INPUT — change only this section to run a different experiment
# =====================================================================
CELL_LINE = 'LS180' # cell line name shown in plot titles
TEST_AB = 'Hu63' # test article antibody prefix
ISO_AB = 'Iso' # isotype control antibody prefix
R2_THRESHOLD = 0.5 # minimum R² for a "reliable" LL.4 fit
SCALE_MIN_CONC = 0 # dose-response x-axis lower bound (0 = untreated pseudo-conc)
SCALE_MAX_CONC = None # None uses all concentrations retained by the reversal filter
SCALE_EXCL_ABS = [] # all retained drugs contribute to y-axis scaling
REVERSAL_FILTER_METRIC = 'Confluence' # raw metric used to detect fragmentation artefacts
REVERSAL_INCREASE_THRESHOLD = 0.25 # exclude if >25% above the preceding concentration
# =====================================================================
PLOT_DIR = os.path.join(DATA_DIR, 'plots')
# Stem for exported CSVs — the experiment folder name (DATA_DIR is often '.').
# If DATA_DIR is a generic 'Data' wrapper, name exports after the experiment
# folder above it (e.g. .../Capan/Data -> 'Capan') rather than after 'Data'.
_stem_dir = os.path.abspath(DATA_DIR)
if os.path.basename(_stem_dir).lower() == 'data':
_stem_dir = os.path.dirname(_stem_dir)
EXPORT_STEM = os.path.basename(_stem_dir)
os.makedirs(PLOT_DIR, exist_ok=True)
plot_counter = [0]
def save_fig(title=''):
plot_counter[0] += 1
fname = os.path.join(PLOT_DIR, f'plot_{plot_counter[0]:02d}.png')
plt.savefig(fname, dpi=120, bbox_inches='tight')
plt.close()
display(IPImage(filename=fname))
# Auto-discover data: find confluence data CSVs in DATA_DIR
_fname_pat = re.compile(r'.*_P(?:late)?(\d+)_D(\d+)\.csv', re.IGNORECASE)
csv_data_files = sorted([f for f in os.listdir(DATA_DIR) if _fname_pat.match(f)])
# Plate layout CSVs live in DATA_DIR
PLATE_LAYOUTS = sorted([f for f in os.listdir(DATA_DIR)
if re.match(r'Plate\d+_layout\.csv', f)])
assert len(PLATE_LAYOUTS) >= 1, f'No Plate*_layout.csv found in {DATA_DIR}'
print(f'Data dir : {DATA_DIR}')
print(f'Plates : {len(PLATE_LAYOUTS)} layout(s) — {PLATE_LAYOUTS}')
print(f'X-axis : {SCALE_MIN_CONC} – {SCALE_MAX_CONC if SCALE_MAX_CONC is not None else "all retained concentrations"}')
print(f'Excl ABs : {SCALE_EXCL_ABS} (excluded from y-axis range only)')Data dir : /content/drive/MyDrive/20260910 Cytotox - 63VPE vs 64VPE6/Capan WST8-20260918/Data
Plates : 4 layout(s) — ['Plate1_layout.csv', 'Plate2_layout.csv', 'Plate3_layout.csv', 'Plate4_layout.csv']
X-axis : 0 – all retained concentrations
Excl ABs : [] (excluded from y-axis range only)
# ===========================================================================
# 1. Load confluency data and name match plate layout files
# ===========================================================================
print('\n=== 1. Load confluency data ===')
csv_files = sorted([f for f in os.listdir(DATA_DIR) if f.endswith('.csv')])
print(f'{len(csv_files)} CSV files found')
# Flexible pattern: matches _Plate1_D0, _P1_D0, _P1_D07, etc.
fname_pattern = re.compile(r'.*_P(?:late)?(\d+)_D(\d+)\.csv', re.IGNORECASE)
frames = []
seen = set()
for fname in csv_files:
m = fname_pattern.match(fname)
if not m:
continue
plate_num = int(m.group(1))
day = int(m.group(2))
key = (plate_num, day)
if key in seen:
continue
seen.add(key)
tmp = pd.read_csv(os.path.join(DATA_DIR, fname), index_col=0)
tmp.index.name = 'Well'
tmp = tmp.reset_index()
tmp['Plate'] = plate_num
tmp['Day'] = day
frames.append(tmp)
df_raw = pd.concat(frames, ignore_index=True)
df_raw = df_raw.drop(columns=['Total cell area'], errors='ignore')
print(f'Combined raw data: {df_raw.shape[0]} rows x {df_raw.shape[1]} cols')
print(f'Plates: {sorted(df_raw.Plate.unique().tolist())} Days: {sorted(df_raw.Day.unique().tolist())}')
=== 1. Load confluency data ===
12 CSV files found
Combined raw data: 252 rows x 4 cols
Plates: [1, 2, 3, 4] Days: [7]
# ===========================================================================
# 2. Load plate layouts and merge
# ===========================================================================
print('\n=== 2. Load plate layouts and merge ===')
layout_frames = []
for lf in PLATE_LAYOUTS:
plate_num = int(re.search(r'Plate(\d+)', lf).group(1))
tmp = pd.read_csv(os.path.join(DATA_DIR, lf))
tmp.rename(columns={'Cell': 'Well'}, inplace=True)
tmp['Plate'] = plate_num
layout_frames.append(tmp)
print(f'Plate {plate_num} drugs: {sorted(tmp.Drug.unique())}')
layouts = pd.concat(layout_frames, ignore_index=True)
print(f'Layout entries: {len(layouts)} ({len(PLATE_LAYOUTS)} plates)')
df = df_raw.merge(layouts[['Well', 'Drug', 'Conc', 'Plate']],
on=['Well', 'Plate'], how='left')
unmatched = df.Drug.isna().sum()
if unmatched > 0:
print(f'WARNING: {unmatched} wells did not match a layout entry')
else:
print('All wells matched their layout.')
print(f'Drugs: {sorted(df.Drug.dropna().unique())}')
print(f'Concentrations: {sorted(df.Conc.dropna().unique().tolist())}')
# Auto-derive concentration parameters from the data
nonzero_concs = df.loc[df.Conc > 0, 'Conc']
CONC_MIN = nonzero_concs.min()
CONC_MAX = nonzero_concs.max()
PSEUDO_CONC = CONC_MIN / 10.0
df['Conc_orig'] = df['Conc'].copy()
df.loc[df.Conc == 0, 'Conc'] = PSEUDO_CONC
print(f'Conc range: {CONC_MIN:.4g} – {CONC_MAX:.4g} nM')
print(f'Conc==0 replaced with pseudo-conc {PSEUDO_CONC:.4g} nM')
=== 2. Load plate layouts and merge ===
Plate 1 drugs: ['63VPE - 1.5 hour', '64VPE6 - 1.5 hour', 'Media+WST8 - 1.5 hour']
Plate 2 drugs: ['63VPE - 2 hour', '64VPE6 - 2 hour', 'Media+WST8 - 2 hour']
Plate 3 drugs: ['63VPE - 3 hour', '64VPE6 - 3 hour', 'Media+WST8 - 3 hour']
Plate 4 drugs: ['63VPE - 4 hour', '64VPE6 - 4 hour', 'Media+WST8 - 4 hour']
Layout entries: 252 (4 plates)
All wells matched their layout.
Drugs: ['63VPE - 1.5 hour', '63VPE - 2 hour', '63VPE - 3 hour', '63VPE - 4 hour', '64VPE6 - 1.5 hour', '64VPE6 - 2 hour', '64VPE6 - 3 hour', '64VPE6 - 4 hour', 'Media+WST8 - 1.5 hour', 'Media+WST8 - 2 hour', 'Media+WST8 - 3 hour', 'Media+WST8 - 4 hour']
Concentrations: [0.0, 0.006, 0.026, 0.12, 0.54, 2.44, 11.0, 49.4, 222.2, 1000.0]
Conc range: 0.006 – 1000 nM
Conc==0 replaced with pseudo-conc 0.0006 nM
# ===========================================================================
# 3. Raw metric QC (no Day-0 normalization)
# Confluence is used as measured. Day-0 confluence is NOT used as a
# per-well baseline; all dose-response normalization is done against the
# untreated wells of the same plate/drug/day (%Viable, sec. 7b).
# ===========================================================================
print('\n=== 3. Raw metric QC (no Day-0 normalization) ===')
if 0 in set(df.Day.unique()):
print('NOTE: a Day 0 scan is present but is NOT used as a baseline. '
'It is treated as an ordinary timepoint and excluded from curve fitting.')
else:
print('No Day 0 scan present — none required.')
display(Markdown('### Confluence stats by Day:'))
display(df.groupby('Day')['Confluence'].describe().round(3))
display(Markdown('### Confluence stats by Plate and Day:'))
display(df.groupby(['Plate', 'Day'])['Confluence'].describe().round(3))
# Drug-by-drug fragmentation filter. Compare mean raw cell area at each
# concentration with the immediately preceding concentration for that drug/day.
_filter_means = (
df.groupby(['Drug', 'Day', 'Conc'], dropna=False)[REVERSAL_FILTER_METRIC]
.mean().rename('Reversal_Filter_Mean').reset_index()
.sort_values(['Drug', 'Day', 'Conc'])
)
_filter_means['Reversal_Previous_Mean'] = (
_filter_means.groupby(['Drug', 'Day'])['Reversal_Filter_Mean'].shift(1)
)
_prev = _filter_means['Reversal_Previous_Mean']
_curr = _filter_means['Reversal_Filter_Mean']
_filter_means['Reversal_Pct_Increase'] = np.where(
_prev > 0, (_curr - _prev) / _prev,
np.where((_prev == 0) & (_curr > 0), np.inf, np.nan)
)
_filter_means['Exclude_25pct_Reversal'] = (
_filter_means['Reversal_Pct_Increase'] > REVERSAL_INCREASE_THRESHOLD
)
df = df.merge(
_filter_means[['Drug', 'Day', 'Conc', 'Reversal_Filter_Mean',
'Reversal_Previous_Mean', 'Reversal_Pct_Increase',
'Exclude_25pct_Reversal']],
on=['Drug', 'Day', 'Conc'], how='left'
)
df['Exclude_25pct_Reversal'] = df['Exclude_25pct_Reversal'].fillna(False).astype(bool)
excluded_reversals = _filter_means[_filter_means.Exclude_25pct_Reversal].copy()
print(f'\n25% reversal filter metric: {REVERSAL_FILTER_METRIC}')
print(f'Excluded drug/day/concentration groups: {len(excluded_reversals)}')
if not excluded_reversals.empty:
display(excluded_reversals[['Drug', 'Day', 'Conc', 'Reversal_Previous_Mean',
'Reversal_Filter_Mean', 'Reversal_Pct_Increase']].round(3))
=== 3. Raw metric QC (no Day-0 normalization) ===
No Day 0 scan present — none required.
| count | mean | std | min | 25% | 50% | 75% | max | |
|---|---|---|---|---|---|---|---|---|
| Day | ||||||||
| 7 | 252 | 0.322 | 0.095 | 0.194 | 0.258 | 0.279 | 0.376 | 0.627 |
| count | mean | std | min | 25% | 50% | 75% | max | ||
|---|---|---|---|---|---|---|---|---|---|
| Plate | Day | ||||||||
| 1 | 7 | 63 | 0.296 | 0.057 | 0.199 | 0.258 | 0.276 | 0.33 | 0.44 |
| 2 | 7 | 63 | 0.307 | 0.073 | 0.196 | 0.253 | 0.278 | 0.374 | 0.488 |
| 3 | 7 | 63 | 0.331 | 0.103 | 0.194 | 0.254 | 0.283 | 0.427 | 0.598 |
| 4 | 7 | 63 | 0.355 | 0.124 | 0.199 | 0.263 | 0.289 | 0.481 | 0.627 |
25% reversal filter metric: Confluence
Excluded drug/day/concentration groups: 0
# ===========================================================================
# 4. Drug/color mapping
# ===========================================================================
all_drugs = sorted(df.Drug.dropna().unique())
all_days = sorted(df.Day.unique())
# Day 0, if scanned, is a seeding-check timepoint and is not fitted.
fit_days = [d for d in all_days if d > 0] or all_days
def _split_ab_lp(name):
"""Split 'AB-LP' on the first dash. LP may contain further dashes."""
idx = name.find('-')
return (name[:idx], name[idx+1:]) if idx >= 0 else (name, None)
# Unique AB prefixes and LP suffixes
_all_abs_setup = sorted({_split_ab_lp(d)[0] for d in all_drugs})
_all_lps_setup = sorted({lp for d in all_drugs
for lp in [_split_ab_lp(d)[1]] if lp is not None})
# Color → AB prefix (same AB = same color across all LPs)
# Style → LP suffix (same LP = same linestyle across all ABs)
_ls_cycle = [
'-',
'--',
'-.',
':',
(0, (5, 1)),
(0, (3, 1, 1, 1)),
(0, (1, 1)),
(0, (5, 5)),
(0, (3, 5, 1, 5)),
(0, (3, 1, 1, 1, 1, 1)),
]
ab_colors = dict(zip(_all_abs_setup, sns.color_palette('tab10', len(_all_abs_setup))))
lp_linestyles = {lp: _ls_cycle[i % len(_ls_cycle)]
for i, lp in enumerate(_all_lps_setup)}
# Convenience dicts keyed by full drug name (used throughout plotting code)
drug_colors = {d: ab_colors[_split_ab_lp(d)[0]] for d in all_drugs}
drug_linestyles = {d: lp_linestyles.get(_split_ab_lp(d)[1], '-') for d in all_drugs}
print(f'\n{len(all_drugs)} drugs: {all_drugs}')
print(f' AB prefixes ({len(_all_abs_setup)}): {_all_abs_setup}')
print(f' Linker-payloads ({len(_all_lps_setup)}): {_all_lps_setup}')
12 drugs: ['63VPE - 1.5 hour', '63VPE - 2 hour', '63VPE - 3 hour', '63VPE - 4 hour', '64VPE6 - 1.5 hour', '64VPE6 - 2 hour', '64VPE6 - 3 hour', '64VPE6 - 4 hour', 'Media+WST8 - 1.5 hour', 'Media+WST8 - 2 hour', 'Media+WST8 - 3 hour', 'Media+WST8 - 4 hour']
AB prefixes (3): ['63VPE ', '64VPE6 ', 'Media+WST8 ']
Linker-payloads (4): [' 1.5 hour', ' 2 hour', ' 3 hour', ' 4 hour']
# ===========================================================================
# 5. Growth kinetics plots (per drug, combined plates) — Confluence
# ===========================================================================
print('\n=== 4. Growth kinetics plots ===')
for drug in all_drugs:
sub = df[df.Drug == drug].copy()
concs = sorted(sub.Conc.unique())
fig, ax = plt.subplots(figsize=(8, 5))
for conc in concs:
csub = sub[sub.Conc == conc]
means = csub.groupby('Day')['Confluence'].mean()
sds = csub.groupby('Day')['Confluence'].std()
lbl = 'untreated' if conc == PSEUDO_CONC else f'{conc} nM'
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, label=lbl)
ax.set_xlabel('Day')
ax.set_ylabel('Confluence')
ax.set_xticks(all_days)
ax.legend(title='Conc', fontsize=8, title_fontsize=9)
fig.suptitle(f'{drug} — Growth Kinetics', fontsize=14)
plt.tight_layout()
save_fig()
=== 4. Growth kinetics plots ===
# ===========================================================================
# 6. LL.4 fitting functions
# ===========================================================================
print('\n=== 5. LL.4 model ===')
def ll4(x, slope, lower, upper, ec50):
return lower + (upper - lower) / (1.0 + (x / ec50) ** slope)
def compute_r2(y_obs, y_pred):
ss_res = np.sum((y_obs - y_pred) ** 2)
ss_tot = np.sum((y_obs - np.mean(y_obs)) ** 2)
if ss_tot == 0:
return 0.0
return 1.0 - ss_res / ss_tot
def fit_ll4(conc, response, p0=None):
"""Fit LL.4 using the points supplied by the shared analysis filter."""
conc = np.asarray(conc, dtype=float)
response = np.asarray(response, dtype=float)
mask = (conc > 0) & np.isfinite(response)
conc, response = conc[mask], response[mask]
if len(conc) < 4 or len(np.unique(conc)) < 3:
return None, None
cmin, cmax = conc.min(), conc.max()
if p0 is None:
p0 = [1.0, np.min(response), np.max(response), np.median(conc)]
bounds = (
[0.01, -np.inf, -np.inf, cmin * 0.01],
[np.inf, np.inf, np.inf, cmax * 100 ]
)
try:
popt, _ = curve_fit(ll4, conc, response, p0=p0,
maxfev=10000, bounds=bounds)
except Exception:
return None, None
y_pred = ll4(conc, *popt)
r2 = compute_r2(response, y_pred)
return popt, r2
def fit_ll4_pct_viable(conc, response, upper=100.0):
"""Fit %Viable LL.4 with the untreated-normalized upper plateau fixed at 100%."""
conc = np.asarray(conc, dtype=float)
response = np.asarray(response, dtype=float)
mask = (conc > 0) & np.isfinite(response)
conc, response = conc[mask], response[mask]
if len(conc) < 4 or len(np.unique(conc)) < 3:
return None, None
cmin, cmax = conc.min(), conc.max()
def _ll4_fixed_upper(x, slope, lower, ec50):
return ll4(x, slope, lower, upper, ec50)
try:
popt_fixed, _ = curve_fit(
_ll4_fixed_upper, conc, response,
p0=[1.0, np.min(response), np.median(conc)],
bounds=([0.01, -np.inf, cmin * 0.01],
[np.inf, np.inf, cmax * 100]),
maxfev=10000,
)
except Exception:
return None, None
slope, lower, ec50 = popt_fixed
popt = np.array([slope, lower, upper, ec50])
r2 = compute_r2(response, ll4(conc, *popt))
return popt, r2
def classify_ic50(row):
if row['R2'] < R2_THRESHOLD:
return 'poor fit'
if row['IC50_nM'] < CONC_MIN * 0.5:
return f'<{CONC_MIN} nM'
if row['IC50_nM'] > CONC_MAX * 2:
return f'>{CONC_MAX} nM'
return f"{row['IC50_nM']:.3g}"
def interpolate_ic50(concs, pct_viable, target=50.0):
"""
Find concentration where mean %Viable crosses `target` (default 50%).
Linear interpolation on log10(conc).
Exclusions are applied before this function is called.
Returns NaN if the curve never crosses the target.
"""
means = pct_viable.groupby(concs).mean()
c = np.array(means.index, dtype=float)
v = np.array(means.values, dtype=float)
order = np.argsort(c)
c, v = c[order], v[order]
for i in range(len(v) - 1):
if (v[i] >= target and v[i+1] < target) or \
(v[i] <= target and v[i+1] > target):
log_c1, log_c2 = np.log10(c[i]), np.log10(c[i+1])
frac = (target - v[i]) / (v[i+1] - v[i])
return 10 ** (log_c1 + frac * (log_c2 - log_c1))
if v[-1] < target and v[0] >= target:
return c[-1]
return np.nan
print(f'LL.4 defined. R² threshold = {R2_THRESHOLD}')
print('Interpolation IC50 function defined.')
print('Drug/day-specific >25% reversal exclusions active for all IC50 calculations.')
=== 5. LL.4 model ===
LL.4 defined. R² threshold = 0.5
Interpolation IC50 function defined.
Drug/day-specific >25% reversal exclusions active for all IC50 calculations.
# ===========================================================================
# 7. Dose-response fitting & plotting — Confluence
# ===========================================================================
from matplotlib.lines import Line2D
metric_name = 'Confluence'
minfo = {
'col': 'Confluence',
'ylabel_dr': 'Confluence',
'ylabel_pv': '% Viable (Confluence, untreated-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
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()
key = (drug, day)
has_fit = key in fit_params_dr
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, color=drug_colors[drug],
linestyle='none', linewidth=1.0,
label=None if has_fit else drug)
if has_fit:
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], label=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='lower left')
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_plotted = 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()
key = (drug, day)
has_fit = key in fit_params_pv
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, color=drug_colors[drug],
linestyle='none', linewidth=1.0,
label=None if has_fit else drug)
# Overlay LL.4 fitted curve if available
if has_fit:
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], label=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,
zorder=5)
_absolute_marker_plotted = 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)
# Build legend with IC50 marker first if plotted
handles, labels = ax.get_legend_handles_labels()
if _absolute_marker_plotted:
ic50_handle = Line2D([0], [0], marker='D', linestyle='none',
color='black', markeredgecolor='black',
markersize=6)
handles = [ic50_handle] + handles
labels = ['Absolute IC50 (50% untreated)'] + labels
ax.legend(handles, labels, fontsize=8, loc='lower left')
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_plotted = 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()
key = (drug, day)
has_fit = key in fit_params_pv
ax.errorbar(means.index, means.values, yerr=sds.values,
marker='o', capsize=3, color=drug_colors[drug],
linestyle='none', linewidth=1.0,
label=None if has_fit else drug)
if has_fit:
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], label=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,
zorder=5)
_fitted_marker_plotted = 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')
# Build legend with IC50 marker first if plotted
handles, labels = ax.get_legend_handles_labels()
if _fitted_marker_plotted:
ic50_handle = Line2D([0], [0], marker='D', linestyle='none',
color='black', markeredgecolor='black',
markersize=6)
handles = [ic50_handle] + handles
labels = ['Fitted IC50 (LL.4 plateau midpoint)'] + labels
ax.legend(handles, labels, fontsize=8, loc='lower left')
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
============================================================
=== METRIC: Confluence ===
============================================================
--- Dose-response plots (Confluence) ---
IC50 (Confluence direct): 8 reliable of 12
Absolute IC50 at 50% untreated (Confluence): 4 of 12 crossed 50%
Fitted-midpoint IC50 (%Viable Confluence): 8 reliable of 12
# ===========================================================================
# 7g. Trellis: All Antibodies per Linker-Payload, one subplot per Day
# ===========================================================================
print('\n=== Trellis: dose-response by LP (all antibodies) ===')
# Build lp -> {ab: drug_name} from all drugs
_lp_ab_map = {}
for d in all_drugs:
ab, lp = _split_ab_lp(d)
if lp is not None:
_lp_ab_map.setdefault(lp, {})[ab] = d
_all_lps = sorted(_lp_ab_map.keys())
_markers = ['o', 's', '^', 'D', 'v', 'P', '*', 'X', 'h', '<']
print(f' Linker-payloads: {_all_lps}')
for lp, abmap in sorted(_lp_ab_map.items()):
print(f' {lp}: {sorted(abmap.keys())}')
# Derive TEST_AB / ISO_AB matched pairs for section 7g (TI trellis)
_test_drugs = {lp: ab_drug[TEST_AB] for lp, ab_drug in _lp_ab_map.items() if TEST_AB in ab_drug}
_iso_drugs = {lp: ab_drug[ISO_AB] for lp, ab_drug in _lp_ab_map.items() if ISO_AB in ab_drug}
_matched_lps = sorted(set(_test_drugs) & set(_iso_drugs))
metric_name = 'Confluence'
pv_col = f'PctViable_{metric_name}'
for lp in _all_lps:
ab_drug = _lp_ab_map[lp]
abs_sorted = sorted(ab_drug.keys())
# Pre-scan: y-range within SCALE_MIN_CONC–SCALE_MAX_CONC, excluding SCALE_EXCL_ABS
all_vals = []
for day in fit_days:
for ab, drug in ab_drug.items():
if ab in SCALE_EXCL_ABS:
continue
sub = df[(df.Drug == drug) & (df.Day == day)].dropna(subset=[pv_col])
vis = sub[(sub.Conc >= _xlo) & (sub.Conc <= _xhi)]
vis = _analysis_rows(vis)
if not vis.empty:
means = vis.groupby('Conc')[pv_col].mean()
sds = vis.groupby('Conc')[pv_col].std().fillna(0)
all_vals.extend((means + sds).tolist())
all_vals.extend((means - sds).tolist())
if not all_vals:
continue
y_lo = min(all_vals)
y_hi = max(all_vals)
margin = (y_hi - y_lo) * 0.08
ylim = (y_lo - margin, y_hi + margin)
ncols = len(fit_days)
fig, axes = plt.subplots(1, ncols, figsize=(4 * ncols, 5), sharey=True)
if ncols == 1:
axes = [axes]
for ax, day in zip(axes, fit_days):
for i, ab in enumerate(abs_sorted):
drug = ab_drug[ab]
sub = df[(df.Drug == drug) & (df.Day == day)].dropna(subset=[pv_col])
if sub.empty:
continue
means = sub.groupby('Conc')[pv_col].mean()
sds = sub.groupby('Conc')[pv_col].std()
mrkr = _markers[i % len(_markers)]
ax.errorbar(means.index, means.values, yerr=sds.values,
marker=mrkr, 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)])
if curve_sub.empty:
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')
ax.set_ylim(ylim)
ax.set_title(f'Day {day}', fontsize=11)
ax.set_xlabel('Conc (nM)')
ax.axhline(50, color='red', ls=':', alpha=0.5)
ax.axhline(100, color='grey', ls=':', alpha=0.3)
if ax is axes[0]:
ax.set_ylabel(minfo['ylabel_pv'])
ax.legend(fontsize=7, loc='lower left')
fig.suptitle(f'{lp} — %Viable ({metric_name})', fontsize=14, y=1.02)
plt.tight_layout()
save_fig()
=== Trellis: dose-response by LP (all antibodies) ===
Linker-payloads: [' 1.5 hour', ' 2 hour', ' 3 hour', ' 4 hour']
1.5 hour: ['63VPE ', '64VPE6 ', 'Media+WST8 ']
2 hour: ['63VPE ', '64VPE6 ', 'Media+WST8 ']
3 hour: ['63VPE ', '64VPE6 ', 'Media+WST8 ']
4 hour: ['63VPE ', '64VPE6 ', 'Media+WST8 ']
# ===========================================================================
# 7g. Trellis: Therapeutic Index per concentration, per LP x Day
# TI = %Viable(ISO_AB) / %Viable(each test AB)
# ===========================================================================
print('\n=== Trellis: TI by concentration (all test ABs vs ISO_AB) ===')
metric_name = 'Confluence'
pv_col = f'PctViable_{metric_name}'
_ti_data = {} # (lp, ab, day) -> (conc_array, ti_array)
_lp_yranges = {} # lp -> (ymin, ymax)
for lp in _all_lps:
ab_drug = _lp_ab_map[lp]
if ISO_AB not in ab_drug:
continue
iso_drug = ab_drug[ISO_AB]
test_abs = sorted(ab_drug.keys()) # includes ISO_AB so TI=1 is shown
if not test_abs:
continue
all_ti = []
for ab in test_abs:
drug = ab_drug[ab]
for day in fit_days:
sub_t = df[(df.Drug == drug) & (df.Day == day)].dropna(subset=[pv_col])
sub_i = df[(df.Drug == iso_drug) & (df.Day == day)].dropna(subset=[pv_col])
sub_t = _analysis_rows(sub_t)
sub_i = _analysis_rows(sub_i)
if sub_t.empty or sub_i.empty:
continue
mean_t = sub_t.groupby('Conc')[pv_col].mean()
mean_i = sub_i.groupby('Conc')[pv_col].mean()
shared = sorted(set(mean_t.index) & set(mean_i.index))
if not shared:
continue
concs = np.array(shared)
ti = np.array([mean_i[c] / mean_t[c] if mean_t[c] != 0 else np.nan
for c in shared])
_ti_data[(lp, ab, day)] = (concs, ti)
all_ti.extend(ti[np.isfinite(ti)].tolist())
if all_ti:
_lp_yranges[lp] = (min(all_ti), max(all_ti))
if not _lp_yranges:
print(f' No TI data for {metric_name}')
else:
lps_with_ti = [lp for lp in _all_lps if lp in _lp_yranges]
nrows = len(lps_with_ti)
ncols = len(fit_days)
fig, axes = plt.subplots(nrows, ncols,
figsize=(3.5 * ncols, 3.5 * nrows),
squeeze=False)
for row, lp in enumerate(lps_with_ti):
ab_drug = _lp_ab_map[lp]
test_abs = sorted(ab_drug.keys()) # includes ISO_AB so TI=1 is shown
y_lo, y_hi = _lp_yranges[lp]
margin = (y_hi - y_lo) * 0.1 if y_hi > y_lo else 0.5
ylim = (y_lo - margin, y_hi + margin)
for col_i, day in enumerate(fit_days):
ax = axes[row][col_i]
for i, ab in enumerate(test_abs):
drug = ab_drug[ab]
key = (lp, ab, day)
if key not in _ti_data:
continue
concs, ti = _ti_data[key]
mrkr = _markers[i % len(_markers)]
ax.plot(concs, ti,
marker=mrkr, linestyle=drug_linestyles[drug],
color=drug_colors[drug], markersize=5, linewidth=1.2,
label=drug)
ax.axhline(1, color='red', ls='--', alpha=0.6)
ax.set_xscale('log')
ax.set_ylim(ylim)
ax.legend(fontsize=6, loc='best')
if row == 0:
ax.set_title(f'Day {day}', fontsize=11)
if col_i == 0:
ax.set_ylabel(f'{lp}\nTI ({ISO_AB} / AB)', fontsize=9)
else:
ax.set_yticklabels([])
if row == nrows - 1:
ax.set_xlabel('Conc (nM)')
else:
ax.set_xticklabels([])
fig.suptitle(f'Therapeutic Index — %Viable ({metric_name})\n'
f'TI = {ISO_AB} / each test AB (red dashed = TI 1)',
fontsize=14, y=1.02)
plt.tight_layout()
save_fig()
=== Trellis: TI by concentration (all test ABs vs ISO_AB) ===
No TI data for Confluence
# ===========================================================================
# 8. Summary tables — side-by-side comparison (plain display version)
# ===========================================================================
pd.set_option('display.float_format', lambda x: f'{x:.3g}') # applies to all displayed tables
sig3 = lambda x: f'{x:.3g}' # same 3-sig-fig rule, for use inside .style (doesn't inherit the option above)
def _display_pivot(pivot):
"""Return a display-only copy with 'Day' columns prefixed, leaving the original pivot untouched."""
disp = pivot.copy()
disp = disp.rename(columns={c: f'Day {c}' for c in disp.columns if c != 'Drug'})
return disp
display(Markdown('## IC50 SUMMARY — COMPARISON'))
for label, df_tbl in all_ic50_tables.items():
if 'interpolated' in label:
continue # handled separately below
# Reliable IC50 pivot
pivot = df_tbl[df_tbl.Reliable].pivot_table(
index='Drug', columns='Day', values='IC50_nM')
pivot = pivot.rename_axis(columns=None).reset_index()
display(Markdown(f'### {label} — IC50 (nM), reliable only (R²≥{R2_THRESHOLD}, in range)'))
if pivot.empty:
display(Markdown('_(no reliable fits)_'))
else:
display(_display_pivot(pivot).style.hide(axis='index'))
# --- Absolute IC50 tables (direct crossing at 50% of untreated) ---
display(Markdown('## ABSOLUTE IC50 AT 50% OF UNTREATED (no curve fitting)'))
for metric_name in ['Confluence']:
label = f'{metric_name}_interpolated'
df_interp_tbl = all_ic50_tables[label]
pivot = df_interp_tbl.pivot_table(
index='Drug', columns='Day', values='IC50_interp')
pivot = pivot.rename_axis(columns=None).reset_index()
display(Markdown(f'### {metric_name} — Absolute IC50 (nM) at 50% of untreated'))
display(_display_pivot(pivot).style.hide(axis='index'))
# Side-by-side: absolute 50%-untreated crossing vs fitted midpoint (Confluence)
display(Markdown('### Confluence: Absolute 50%-untreated vs fitted-midpoint IC50'))
interp = all_ic50_tables['Confluence_interpolated'][['Drug', 'Day', 'IC50_interp']]
fitted = all_ic50_tables['Confluence_pctViable']
fitted_rel = fitted[fitted.Reliable][['Drug', 'Day', 'IC50_nM']].rename(
columns={'IC50_nM': 'IC50_LL4'})
compare = interp.merge(fitted_rel, on=['Drug', 'Day'], how='outer')
compare = compare.dropna(subset=['IC50_interp', 'IC50_LL4'], how='all')
compare['Diff_%'] = ((compare.IC50_interp - compare.IC50_LL4) /
compare.IC50_LL4 * 100).round(1)
compare = compare.set_index(['Drug', 'Day']).sort_index()
display(
compare.style
.format({'IC50_interp': sig3, 'IC50_LL4': sig3, 'Diff_%': '{:.1f}'}, na_rep='—')
)| Drug | Day 7 |
|---|---|
| 63VPE - 1.5 hour | 0.089326 |
| 63VPE - 2 hour | 0.082546 |
| 63VPE - 3 hour | 0.055090 |
| 63VPE - 4 hour | 0.070149 |
| 64VPE6 - 1.5 hour | 0.122011 |
| 64VPE6 - 2 hour | 0.104339 |
| 64VPE6 - 3 hour | 0.086411 |
| 64VPE6 - 4 hour | 0.080092 |
| Drug | Day 7 |
|---|---|
| 63VPE - 1.5 hour | 0.067435 |
| 63VPE - 2 hour | 0.071818 |
| 63VPE - 3 hour | 0.052650 |
| 63VPE - 4 hour | 0.062517 |
| 64VPE6 - 1.5 hour | 0.127893 |
| 64VPE6 - 2 hour | 0.104076 |
| 64VPE6 - 3 hour | 0.083531 |
| 64VPE6 - 4 hour | 0.075880 |
| Drug | Day 7 |
|---|---|
| 63VPE - 3 hour | 1.605816 |
| 63VPE - 4 hour | 1.178307 |
| 64VPE6 - 3 hour | 1.964151 |
| 64VPE6 - 4 hour | 0.889607 |
| IC50_interp | IC50_LL4 | Diff_% | ||
|---|---|---|---|---|
| Drug | Day | |||
| 63VPE - 1.5 hour | 7 | — | 0.0674 | — |
| 63VPE - 2 hour | 7 | — | 0.0718 | — |
| 63VPE - 3 hour | 7 | 1.61 | 0.0526 | 2950.0 |
| 63VPE - 4 hour | 7 | 1.18 | 0.0625 | 1784.8 |
| 64VPE6 - 1.5 hour | 7 | — | 0.128 | — |
| 64VPE6 - 2 hour | 7 | — | 0.104 | — |
| 64VPE6 - 3 hour | 7 | 1.96 | 0.0835 | 2251.4 |
| 64VPE6 - 4 hour | 7 | 0.89 | 0.0759 | 1072.4 |
# ===========================================================================
# 9. IC50 bar plot (reliable, Confluence %Viable)
# ===========================================================================
for label, df_tbl in [
('Confluence %Viable', all_ic50_tables['Confluence_pctViable']),
]:
plot_df = df_tbl[df_tbl.Reliable].dropna(subset=['IC50_nM']).copy()
if plot_df.empty:
continue
fig, ax = plt.subplots(figsize=(14, 6))
drug_order = sorted(plot_df.Drug.unique())
x = np.arange(len(drug_order))
day_vals = sorted(plot_df.Day.unique())
width = 0.8 / max(len(day_vals), 1)
cmap = sns.color_palette('tab10', len(day_vals))
for i, day in enumerate(day_vals):
day_data = plot_df[plot_df.Day == day]
vals = []
for drug in drug_order:
row = day_data[day_data.Drug == drug]
vals.append(row.IC50_nM.values[0] if len(row) > 0 else np.nan)
ax.bar(x + i * width, vals, width, label=f'Day {day}', color=cmap[i])
ax.set_xticks(x + width * (len(day_vals) - 1) / 2)
ax.set_xticklabels(drug_order, rotation=45, ha='right', fontsize=9)
ax.set_ylabel('IC50 (nM)')
ax.set_yscale('log')
ax.set_title(f'IC50 by Drug and Day — {label}')
ax.legend()
plt.tight_layout()
save_fig()# ===========================================================================
# 10. ExportDATA_SUBDIR
# ===========================================================================
print('\n=== 10. Export ===')
out_long = os.path.join(DATA_DIR, f'{EXPORT_STEM}_long.csv')
df.to_csv(out_long, index=False)
print(f'Saved long data: {out_long}')
for label, df_tbl in all_ic50_tables.items():
out = os.path.join(DATA_DIR, f'{EXPORT_STEM}_IC50_{label}.csv')
df_tbl.to_csv(out, index=False)
print(f'Saved: {out}')
print(f'\nTotal plots saved: {plot_counter[0]}')
print('Done.')
=== 10. Export ===
Saved long data: /content/drive/MyDrive/20260910 Cytotox - 63VPE vs 64VPE6/Capan WST8-20260918/Data/Capan WST8-20260918_long.csv
Saved: /content/drive/MyDrive/20260910 Cytotox - 63VPE vs 64VPE6/Capan WST8-20260918/Data/Capan WST8-20260918_IC50_Confluence_interpolated.csv
Saved: /content/drive/MyDrive/20260910 Cytotox - 63VPE vs 64VPE6/Capan WST8-20260918/Data/Capan WST8-20260918_IC50_Confluence_direct.csv
Saved: /content/drive/MyDrive/20260910 Cytotox - 63VPE vs 64VPE6/Capan WST8-20260918/Data/Capan WST8-20260918_IC50_Confluence_pctViable.csv
Total plots saved: 20
Done.
# ===========================================================================
# 11. Therapeutic Windows — Relative IC50 (midpoint between plateaus)
# ===========================================================================
print(f'\n=== 11. Therapeutic Windows ({TEST_AB} vs {ISO_AB}) ===')
print(f' IC50 = relative IC50: conc at midpoint between untreated & max-dose plateaus')
def parse_drug(name, test_ab, iso_ab):
"""Return (antibody, linker-payload) or (None, None) if not matching."""
for ab in [test_ab, iso_ab]:
prefix = ab + '-'
if name.startswith(prefix):
return ab, name[len(prefix):]
return None, None
def relative_ic50(concs, responses):
"""
Relative IC50: concentration where response = midpoint between
the untreated plateau (lowest conc) and the max-dose plateau (highest conc).
Interpolates on log10(conc). Returns (ic50, target_pct, confident).
confident=False if the curve never monotonically crosses the midpoint.
"""
means = responses.groupby(concs).mean()
c = np.array(means.index, dtype=float)
v = np.array(means.values, dtype=float)
order = np.argsort(c)
c, v = c[order], v[order]
untreated = v[0]
max_dose = v[-1]
target = (untreated + max_dose) / 2.0
spread = abs(untreated - max_dose)
if spread < 5.0:
return np.nan, target, False
for i in range(len(v) - 1):
if (v[i] >= target >= v[i+1]) or (v[i] <= target <= v[i+1]):
log_c1, log_c2 = np.log10(max(c[i], 1e-6)), np.log10(max(c[i+1], 1e-6))
frac = (target - v[i]) / (v[i+1] - v[i])
ic50 = 10 ** (log_c1 + frac * (log_c2 - log_c1))
confident = (spread >= 20.0) and (CONC_MIN <= ic50 <= CONC_MAX)
return ic50, target, confident
return np.nan, target, False
METRICS_FOR_TI = {
'Confluence': 'PctViable_Confluence',
}
all_summary_rows = []
for metric_label, pv_col in METRICS_FOR_TI.items():
if pv_col not in df.columns:
print(f'\n {metric_label}: column {pv_col} not found, skipping.')
continue
all_drugs_in_df = sorted(df.Drug.dropna().unique())
drug_ab = {d: parse_drug(d, TEST_AB, ISO_AB)[0] for d in all_drugs_in_df}
drug_lp = {d: parse_drug(d, TEST_AB, ISO_AB)[1] for d in all_drugs_in_df}
test_lps = {drug_lp[d] for d in all_drugs_in_df if drug_ab[d] == TEST_AB}
iso_lps = {drug_lp[d] for d in all_drugs_in_df if drug_ab[d] == ISO_AB}
matched_lps = sorted(test_lps & iso_lps)
if not matched_lps:
print(f'\n {metric_label}: no matched LPs between {TEST_AB} and {ISO_AB}.')
continue
days = sorted(df[df.Day.isin(fit_days)].Day.unique())
print(f'\n{"="*60}')
print(f' {metric_label} (%Viable)')
print(f' Matched LPs: {matched_lps}')
print(f'{"="*60}')
for day in days:
day_df = df[(df.Day == day)].copy()
rows = []
for lp in matched_lps:
test_drug = f'{TEST_AB}-{lp}'
iso_drug = f'{ISO_AB}-{lp}'
test_sub = day_df[(day_df.Drug == test_drug)].dropna(subset=['Conc', pv_col])
iso_sub = day_df[(day_df.Drug == iso_drug)].dropna(subset=['Conc', pv_col])
test_sub = _analysis_rows(test_sub)
iso_sub = _analysis_rows(iso_sub)
if test_sub.empty or iso_sub.empty:
continue
t_ic50, t_target, t_conf = relative_ic50(test_sub.Conc, test_sub[pv_col])
i_ic50, i_target, i_conf = relative_ic50(iso_sub.Conc, iso_sub[pv_col])
t_untx = test_sub.groupby('Conc')[pv_col].mean().sort_index().iloc[0]
t_maxd = test_sub.groupby('Conc')[pv_col].mean().sort_index().iloc[-1]
i_untx = iso_sub.groupby('Conc')[pv_col].mean().sort_index().iloc[0]
i_maxd = iso_sub.groupby('Conc')[pv_col].mean().sort_index().iloc[-1]
ti = i_ic50 / t_ic50 if (not np.isnan(t_ic50) and t_ic50 > 0
and not np.isnan(i_ic50)) else np.nan
ti_conf = t_conf and i_conf and not np.isnan(ti)
def fmt(val, conf):
if np.isnan(val):
return 'NA'
return f'{val:.3g}' if conf else f'{val:.3g}*'
rows.append({
'LP': lp,
f'{TEST_AB} untx→max': f'{t_untx:.0f}→{t_maxd:.0f}%',
f'{TEST_AB} IC50 (nM)': fmt(t_ic50, t_conf),
f'{ISO_AB} untx→max': f'{i_untx:.0f}→{i_maxd:.0f}%',
f'{ISO_AB} IC50 (nM)': fmt(i_ic50, i_conf),
f'TI ({ISO_AB}/{TEST_AB})': fmt(ti, ti_conf),
})
all_summary_rows.append({
'Method': metric_label, 'Day': day, 'LP': lp,
f'{TEST_AB}_IC50_nM': t_ic50, f'{TEST_AB}_confident': t_conf,
f'{TEST_AB}_untreated_%': t_untx, f'{TEST_AB}_maxdose_%': t_maxd,
f'{ISO_AB}_IC50_nM': i_ic50, f'{ISO_AB}_confident': i_conf,
f'{ISO_AB}_untreated_%': i_untx, f'{ISO_AB}_maxdose_%': i_maxd,
'TI': ti, 'TI_confident': ti_conf,
})
if not rows:
print(f'\n Day {day}: no matched LP pairs with data.')
continue
tbl = pd.DataFrame(rows)
print(f'\n--- {metric_label} — Day {day} ---')
print(f' IC50 = conc at midpoint of (untreated, max-dose)')
print(f' TI = IC50({ISO_AB}) / IC50({TEST_AB}) ; * = low confidence')
print(tbl.to_string(index=False))
# Grouped bar chart: IC50 by LP
plot_rows = [r for r in rows
if r[f'{TEST_AB} IC50 (nM)'] != 'NA' or r[f'{ISO_AB} IC50 (nM)'] != 'NA']
if not plot_rows:
continue
lps_plot = [r['LP'] for r in plot_rows]
fig, ax = plt.subplots(figsize=(max(6, len(lps_plot) * 2.5), 5))
x = np.arange(len(lps_plot))
width = 0.35
colors = {TEST_AB: '#2196F3', ISO_AB: '#FF9800'}
for j, ab in enumerate([TEST_AB, ISO_AB]):
col_name = f'{ab} IC50 (nM)'
vals, labels, hatches = [], [], []
for r in plot_rows:
raw = r[col_name]
if raw == 'NA':
vals.append(0); labels.append('NA'); hatches.append('//')
else:
v = float(raw.rstrip('*'))
vals.append(v)
labels.append(raw)
hatches.append('//' if raw.endswith('*') else '')
bars = ax.bar(x + (j - 0.5) * width, vals, width, label=ab,
color=colors[ab], alpha=0.85)
for bar, lbl, hatch in zip(bars, labels, hatches):
bar.set_hatch(hatch)
if lbl != 'NA' and bar.get_height() > 0:
ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(),
lbl, ha='center', va='bottom', fontsize=9)
ax.set_xticks(x)
ax.set_xticklabels(lps_plot, fontsize=11)
ax.set_ylabel('Relative IC50 (nM)')
ax.set_title(f'{metric_label} — Day {day}\nRelative IC50 by Linker-Payload (* = low confidence)',
fontsize=13)
ax.legend()
ax.set_yscale('log')
plt.tight_layout()
save_fig()
# TI bar chart
ti_rows = [r for r in plot_rows if r[f'TI ({ISO_AB}/{TEST_AB})'] != 'NA']
if ti_rows:
ti_vals = [float(r[f'TI ({ISO_AB}/{TEST_AB})'].rstrip('*')) for r in ti_rows]
ti_lps = [r['LP'] for r in ti_rows]
ti_conf = [not r[f'TI ({ISO_AB}/{TEST_AB})'].endswith('*') for r in ti_rows]
fig, ax = plt.subplots(figsize=(max(5, len(ti_vals) * 2), 4.5))
bar_colors = ['#4CAF50' if c else '#FFC107' for c in ti_conf]
bar_hatches = ['' if c else '//' for c in ti_conf]
bars = ax.bar(range(len(ti_vals)), ti_vals, color=bar_colors, alpha=0.85)
for bar, hatch, v, c in zip(bars, bar_hatches, ti_vals, ti_conf):
bar.set_hatch(hatch)
lbl = f'{v:.1f}' + ('' if c else '*')
ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(),
lbl, ha='center', va='bottom', fontsize=10, fontweight='bold')
ax.axhline(1.0, color='red', linestyle='--', linewidth=1, label='TI = 1')
ax.set_xticks(range(len(ti_vals)))
ax.set_xticklabels(ti_lps, fontsize=11)
ax.set_ylabel(f'Therapeutic Index\nIC50({ISO_AB}) / IC50({TEST_AB})')
ax.set_title(f'{metric_label} — Day {day}\nTherapeutic Index (* = low confidence)',
fontsize=13)
ax.legend()
plt.tight_layout()
save_fig()
# Export combined summary
if all_summary_rows:
df_ti_summary = pd.DataFrame(all_summary_rows)
out = os.path.join(DATA_DIR, f'{EXPORT_STEM}_therapeutic_index_{TEST_AB}_vs_{ISO_AB}.csv')
df_ti_summary.to_csv(out, index=False)
print(f'\nSaved combined summary: {out}')
print('\n' + '='*60)
print(' Combined Therapeutic Index Summary')
print('='*60)
print(df_ti_summary.to_string(index=False, float_format=lambda x: f'{x:.3g}'))
=== 11. Therapeutic Windows (Hu63 vs Iso) ===
IC50 = relative IC50: conc at midpoint between untreated & max-dose plateaus
Confluence: no matched LPs between Hu63 and Iso.