LS180 — Hu63 vs Iso — 2026-09-04

LS180
MTT
Hu63 vs Iso
Published

September 4, 2026

IC50 & Growth Kinetics — LS180 Conjugates

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.

Show code
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
Show code
# ====================================================================================
#  USER INPUT — Navigate to experiment folder shortcut; copy relative path to DATA_DIR
# ====================================================================================
DATA_DIR  = '/content/drive/MyDrive/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data'
# =====================================================================
#  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
Show code
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})

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)
# Well / replicate QC tables (calculated dataframes) go to their own subfolder, away from
# the input CSVs that are auto-discovered in DATA_DIR.
WELL_QC_DIR = os.path.join(DATA_DIR, 'well_qc')
os.makedirs(WELL_QC_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/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data
Plates   : 2 layout(s) — ['Plate1_layout.csv', 'Plate2_layout.csv']
X-axis   : 0 – all retained concentrations
Excl ABs : [] (excluded from y-axis range only)

Load Confluency Data for all Plate(s)

Show code
# ===========================================================================
# 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 ===
4 CSV files found
Combined raw data: 120 rows  x  4 cols
Plates: [1, 2]   Days: [7]

Merge Plate Layouts

Show code
# ===========================================================================
# 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: ['63GE', 'IsoGE']
Plate 2 drugs: ['63GE6', '63VPE6']
Layout entries: 120  (2 plates)
All wells matched their layout.
Drugs: ['63GE', '63GE6', '63VPE6', 'IsoGE']
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

Raw Metric QC & Fragmentation Reversal Filter

Show code
# ===========================================================================
# 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.

Confluence stats by Day:

count mean std min 25% 50% 75% max
Day
7 120.0 0.877 0.496 0.262 0.418 0.72 1.409 1.863

Confluence stats by Plate and Day:

count mean std min 25% 50% 75% max
Plate Day
1 7 60.0 0.900 0.521 0.273 0.383 0.865 1.409 1.863
2 7 60.0 0.854 0.473 0.262 0.473 0.649 1.275 1.848

25% reversal filter metric: Confluence
Excluded drug/day/concentration groups: 0

Replicate Variance & Outlier Wells

Show code
# ===========================================================================
# 3b. Replicate variance & outlier well detection — per Drug × Concentration
#     Plate layout: replicates run DOWN a column. The three wells of one
#     triplicate share a column and differ only by plate ROW (e.g. E2/F2/G2),
#     while concentration varies ACROSS columns (col 2 = top dose ... col 11 =
#     untreated). So one replicate group is (Assay, Plate, Drug, Day, Conc) and
#     its members are distinguished by row.
#
#     The replicate axis is detected from the data rather than assumed, so this
#     still works if a plate is ever laid out the other way round.
#
#     Run on the RAW metric, before any normalization.
# ===========================================================================
# Assay label for this notebook (labelling only: MTT and WST-8 are both absorbance, stored in the same
# 'Confluence' column). None = read it from the CSV file names (WST-8 / WST8 anywhere in a name -> 'WST-8',
# otherwise 'MTT'); set 'MTT' or 'WST-8' here to force it.
REP_ASSAY = None
if REP_ASSAY is None:
    REP_ASSAY = ('WST-8' if re.search(r'(?:^|[-_ ])WST-?8(?:[-_ .]|$)', ' '.join(csv_data_files), re.IGNORECASE)
                 else 'MTT')
print(f'Assay label: {REP_ASSAY}')
_rep_metric = 'Confluence'      # the raw column

print('\n=== 3b. Replicate variance & outlier wells (raw metric) ===')

# --- USER INPUT: what counts as a suspect replicate ------------------------
REPLICATE_CV_THRESHOLD = 25.0   # % CV above which a triplicate is flagged
REPLICATE_ROW_BIAS_PCT = 15.0   # mean % deviation above which a whole replicate
                                # position (plate row) is called systematically off
# ---------------------------------------------------------------------------

_REP_KEY = ['Assay', 'Plate', 'Drug', 'Day', 'Conc']

_well_pat = re.compile(r'^\s*([A-Za-z]+)\s*(\d+)\s*$')


def _well_row(w):
    m = _well_pat.match(str(w))
    return m.group(1).upper() if m else np.nan


def _well_col(w):
    m = _well_pat.match(str(w))
    return int(m.group(2)) if m else np.nan


def _cv_pct(values):
    """Coefficient of variation (%) — scale-free, so Castor and MTT are comparable."""
    v = np.asarray(values, dtype=float)
    v = v[np.isfinite(v)]
    if len(v) < 2:
        return np.nan
    m = v.mean()
    if m == 0:
        return np.nan
    return 100.0 * v.std(ddof=1) / m


_rep = df.dropna(subset=[_rep_metric]).copy()
_rep['Assay'] = REP_ASSAY      # single-assay notebook: one constant Assay label
_rep['Row'] = _rep.Well.map(_well_row)
_rep['Col'] = _rep.Well.map(_well_col)

# Deviation of each well from the median of its OWN triplicate. The median is
# used rather than the mean because with n = 3 a single bad well drags the mean
# toward itself and masks its own deviation.
_grp_median = _rep.groupby(_REP_KEY, dropna=False)[_rep_metric].transform('median')
_rep['Rep_Median'] = _grp_median
_rep['Dev_pct'] = np.where(_grp_median != 0,
                           100.0 * (_rep[_rep_metric] - _grp_median) / _grp_median,
                           np.nan)

# Which plate axis separates the members of a replicate group?
_axis_var = _rep.groupby(_REP_KEY, dropna=False)[['Row', 'Col']].nunique()
_rep_axis = ('Row' if _axis_var['Row'].mean() >= _axis_var['Col'].mean() else 'Col')
_other_axis = 'Col' if _rep_axis == 'Row' else 'Row'
print(f'  Replicate axis detected: members of a triplicate differ by {_rep_axis} '
      f'(constant {_other_axis}); {_other_axis} carries concentration.')

# --- Per-group statistics ---------------------------------------------------
_rep_rows = []
for _key, _g in _rep.groupby(_REP_KEY, dropna=False):
    _v = _g[_rep_metric].to_numpy(dtype=float)
    _w = _g.Well.to_numpy()
    _pos = _g[_rep_axis].to_numpy()
    _med = float(np.median(_v))
    _cv = _cv_pct(_v)

    # The well furthest from the triplicate median is the candidate culprit.
    _i = int(np.argmax(np.abs(_v - _med)))
    _keep = [j for j in range(len(_v)) if j != _i]
    _cv_drop = _cv_pct(_v[_keep]) if len(_keep) >= 2 else np.nan

    # Three-way verdict. "one suspect well" means the remaining replicates agree
    # once that well is removed — i.e. a single well is to blame. "scattered"
    # means the whole group disagrees and no single well explains it.
    if not np.isfinite(_cv) or _cv <= REPLICATE_CV_THRESHOLD:
        _verdict, _suspect = 'OK', ''
    elif np.isfinite(_cv_drop) and _cv_drop <= REPLICATE_CV_THRESHOLD:
        _verdict, _suspect = 'one suspect well', _w[_i]
    else:
        _verdict, _suspect = 'scattered', ''

    _others = ' / '.join(f'{x:.4g}' for x in _v[_keep])

    # Plate-layout coordinates, so the table can be read against the physical
    # plate: each drug occupies a contiguous block of replicate positions
    # (rows B-D, E-G, ...) and each group sits in one concentration position
    # (column). Sorting on these reproduces plate reading order.
    _pos_sorted = sorted(str(p) for p in set(_pos))
    _conc_pos = _g[_other_axis].dropna()
    _conc_pos = _conc_pos.iloc[0] if len(_conc_pos) else np.nan

    _rep_rows.append(dict(zip(_REP_KEY, _key)) | {
        'Wells': '/'.join(sorted(map(str, _w))),
        'Block': _pos_sorted[0] if _pos_sorted else '',
        'Conc_Pos': _conc_pos,
        'N': len(_v), 'Mean': _v.mean(), 'SD': _v.std(ddof=1) if len(_v) > 1 else np.nan,
        'CV_pct': _cv, 'Min': _v.min(), 'Max': _v.max(),
        'Worst_Well': _w[_i], 'Worst_Pos': _pos[_i],
        'Worst_Value': _v[_i], 'Worst_Dev_pct': 100.0 * (_v[_i] - _med) / _med if _med else np.nan,
        'Other_Reps': _others, 'CV_pct_drop1': _cv_drop,
        'Verdict': _verdict, 'Suspect_Well': _suspect,
        'Untreated': bool((_g.Conc_orig == 0).all()),
        'Reversal_Excluded': bool(_g.Exclude_25pct_Reversal.any()),
    })

# Canonical order = plate reading order: assay, plate, drug's replicate block
# (top block first), then across the concentration axis (column 2 -> 11, i.e.
# top dose -> untreated). Views that need worst-first re-sort explicitly.
df_replicate_qc = pd.DataFrame(_rep_rows).sort_values(
    ['Assay', 'Plate', 'Block', 'Conc_Pos']).reset_index(drop=True)

# --- Systematic bias along the replicate axis -------------------------------
# Only the replicate axis is interpretable here: the other axis IS concentration,
# so a "bias" along it would just be the dose-response curve.
_pos_bias = (_rep.groupby(['Assay', 'Plate', _rep_axis])['Dev_pct']
             .mean().rename('Mean_Dev_pct').reset_index())
_pos_bias['Systematic'] = _pos_bias.Mean_Dev_pct.abs() >= REPLICATE_ROW_BIAS_PCT
_bias_lookup = {(a, p, r): m for a, p, r, m, _s in _pos_bias.itertuples(index=False)}


def _likely_cause(row):
    """Distinguish a one-off bad well from a whole replicate position being off."""
    if not row.Suspect_Well:
        return ''
    _b = _bias_lookup.get((row.Assay, row.Plate, row.Worst_Pos), np.nan)
    if np.isfinite(_b) and abs(_b) >= REPLICATE_ROW_BIAS_PCT:
        return f'{_rep_axis} {row.Worst_Pos} biased ({_b:+.0f}% plate-wide)'
    return 'isolated well'


df_replicate_qc['Likely_Cause'] = df_replicate_qc.apply(_likely_cause, axis=1)

_sig4 = lambda x: f'{x:.4g}'

# Plate banding: alternating black / grey down the Plate column, so consecutive
# plate blocks are separable at a glance when scanning the table.
# Keyed on the plate number itself (odd = black, even = grey) rather than on row
# position, so a given plate keeps the same shade in every table.
# NB: Plate is kept as a DATA column rather than an index level — pandas
# sparsifies repeated MultiIndex labels and `Styler.map_index` then emits CSS
# for only the first row of each run, so index-level banding does not render.
_PLATE_BAND = {0: 'background-color: #757575; color: #ffffff;',   # even plates: grey
               1: 'background-color: #1f1f1f; color: #ffffff;'}   # odd plates:  black


def _plate_band(v):
    try:
        return _PLATE_BAND[int(v) % 2]
    except (TypeError, ValueError):
        return ''

# --- Table 1: per-assay overview -------------------------------------------
_ov = (df_replicate_qc.groupby('Assay')
       .agg(Groups=('CV_pct', 'size'),
            Median_CV=('CV_pct', 'median'),
            P90_CV=('CV_pct', lambda s: s.quantile(0.90)),
            Max_CV=('CV_pct', 'max'),
            Flagged=('Verdict', lambda s: int((s != 'OK').sum())),
            Suspect_Well=('Verdict', lambda s: int((s == 'one suspect well').sum())),
            Scattered=('Verdict', lambda s: int((s == 'scattered').sum()))))
_ov.insert(0, 'Raw metric', _rep_metric)

display(Markdown('### Replicate variance overview — per assay'))
display(Markdown(
    f'Replicate group = one triplicate: `{"`, `".join(_REP_KEY)}`. '
    f'A group is flagged when its CV exceeds **{REPLICATE_CV_THRESHOLD:.0f}%**. '
    'Computed on the raw metric, before any normalization.'))
display(_ov.style.format({'Median_CV': '{:.1f}', 'P90_CV': '{:.1f}',
                          'Max_CV': '{:.1f}'}, na_rep='—'))

# --- Table 2: full per-replicate variance table, in plate order -------------
display(Markdown(f'### Replicate variance by Drug × Concentration (plate layout order)'))
display(Markdown(
    f'Rows follow the physical plate: assay, plate, each drug\'s '
    f'{_rep_axis.lower()} block top-first, then across the '
    f'{_other_axis.lower()}s (top dose → untreated) — so this table can be read '
    'side by side with the plate map. `Wells` lists the triplicate. '
    '`Worst_Well` is the well furthest from its triplicate median and '
    '`CV_pct_drop1` is what the CV would become without it; when dropping it '
    'brings the group under threshold, a single well is to blame.'))
_tbl2 = df_replicate_qc
display(
    _tbl2[['Assay', 'Drug', 'Day', 'Conc', 'Plate', 'Wells', 'N', 'Mean', 'SD', 'CV_pct', 'Worst_Well', 'Worst_Value',
           'Worst_Dev_pct', 'Other_Reps', 'CV_pct_drop1', 'Verdict', 'Untreated']].style
    .format({'Conc': '{:g}', 'Mean': _sig4, 'SD': _sig4, 'CV_pct': '{:.1f}',
             'Worst_Value': _sig4, 'Worst_Dev_pct': '{:+.1f}',
             'CV_pct_drop1': '{:.1f}'}, na_rep='—')
    .hide(axis='index')
    .map(_plate_band, subset=['Plate'])
    .map(lambda v: 'font-weight: bold' if v in ('one suspect well', 'scattered') else None,
         subset=['Verdict'])
)

# --- Table 3: the actionable list ------------------------------------------
# Grouped by drug, inheriting the plate-layout order of the table above, so a
# drug's flagged concentrations read together and in dose order rather than
# being split apart by severity.
_flagged = df_replicate_qc[df_replicate_qc.Verdict != 'OK'].copy()
display(Markdown('### Suspect wells — groups above the CV threshold (grouped by drug)'))
if _flagged.empty:
    display(Markdown(f'_No replicate group exceeds {REPLICATE_CV_THRESHOLD:.0f}% CV._'))
else:
    _t3 = _flagged
    display(
        _t3[['Assay', 'Drug', 'Day', 'Conc', 'Plate', 'CV_pct', 'Worst_Well', 'Worst_Value', 'Other_Reps',
             'Worst_Dev_pct', 'CV_pct_drop1', 'Verdict', 'Likely_Cause', 'Untreated',
             'Reversal_Excluded']].style
        .format({'Conc': '{:g}', 'CV_pct': '{:.1f}', 'Worst_Value': _sig4,
                 'Worst_Dev_pct': '{:+.1f}', 'CV_pct_drop1': '{:.1f}'}, na_rep='—')
        .hide(axis='index')
        .map(_plate_band, subset=['Plate'])
    )

# --- Table 4: is a whole replicate position off? ----------------------------
display(Markdown(f'### Systematic bias by replicate position (plate {_rep_axis.lower()})'))
display(Markdown(
    f'Mean % deviation of each plate {_rep_axis.lower()} from its own triplicate '
    f'median. Near 0 = that replicate position tracks the others. '
    f'Beyond ±{REPLICATE_ROW_BIAS_PCT:.0f}% the position is systematically off, '
    'and individual wells flagged there are symptoms of that, not independent '
    'bad wells. The other plate axis is not shown: it carries concentration, so '
    'a "bias" along it would just be the dose response.'))
_bias_pivot = _pos_bias.pivot_table(index=['Assay', 'Plate'], columns=_rep_axis,
                                    values='Mean_Dev_pct')
_bias_pivot = _bias_pivot.rename_axis(columns=None)
display(
    _bias_pivot.style
    .format('{:+.1f}', na_rep='—')
    .map(lambda v: 'font-weight: bold' if (pd.notna(v) and abs(v) >= REPLICATE_ROW_BIAS_PCT)
         else None)
)

# --- Printed summary --------------------------------------------------------
print(f'\n  Replicate groups: {len(df_replicate_qc)} '
      f'({df_replicate_qc.N.min()}–{df_replicate_qc.N.max()} wells each)')
print(f'  Above {REPLICATE_CV_THRESHOLD:.0f}% CV: {int((df_replicate_qc.Verdict != "OK").sum())}'
      f'  [{int((df_replicate_qc.Verdict == "one suspect well").sum())} with a single '
      f'suspect well, {int((df_replicate_qc.Verdict == "scattered").sum())} scattered]')

_sys = _pos_bias[_pos_bias.Systematic]
if not _sys.empty:
    print(f'\n  Systematically biased replicate positions '
          f'(|mean deviation| >= {REPLICATE_ROW_BIAS_PCT:.0f}%):')
    for _r in _sys.itertuples(index=False):
        print(f'    {_r.Assay} plate {_r.Plate}, {_rep_axis.lower()} '
              f'{getattr(_r, _rep_axis)}: {_r.Mean_Dev_pct:+.1f}%')
    print('    -> re-check seeding/edge effects for these positions before '
          'blaming individual wells.')

_untx_bad = df_replicate_qc[(df_replicate_qc.Verdict != 'OK') & df_replicate_qc.Untreated]
if not _untx_bad.empty:
    print(f'\n  WARNING: {len(_untx_bad)} flagged group(s) are UNTREATED wells, which set '
          'the 100% baseline —')
    print('  an outlier there shifts %Viable for every concentration of that drug:')
    for _r in _untx_bad.itertuples(index=False):
        print(f'    {_r.Assay} plate {_r.Plate} {_r.Drug}: CV {_r.CV_pct:.1f}%, '
              f'worst well {_r.Worst_Well} ({_r.Worst_Dev_pct:+.0f}%)'
              + (f' — {_r.Likely_Cause}' if _r.Likely_Cause else ''))

# --- Export (flat CSVs: ready to copy into an Excel "Replicate analysis" tab) ---
_rep_tag = f'{EXPORT_STEM}_ReplicateQC_{_rep_metric}'
df_replicate_qc.to_csv(os.path.join(WELL_QC_DIR, f'{_rep_tag}.csv'), index=False)
_ov.reset_index().to_csv(os.path.join(WELL_QC_DIR, f'{_rep_tag}_overview.csv'), index=False)
_pos_bias.to_csv(os.path.join(WELL_QC_DIR, f'{_rep_tag}_position_bias.csv'), index=False)
print(f'\n  Saved to {WELL_QC_DIR}: {_rep_tag}.csv (+ _overview, _position_bias)')
Assay label: MTT

=== 3b. Replicate variance & outlier wells (raw metric) ===
  Replicate axis detected: members of a triplicate differ by Row (constant Col); Col carries concentration.

Replicate variance overview — per assay

Replicate group = one triplicate: Assay, Plate, Drug, Day, Conc. A group is flagged when its CV exceeds 25%. Computed on the raw metric, before any normalization.

  Raw metric Groups Median_CV P90_CV Max_CV Flagged Suspect_Well Scattered
Assay                
MTT Confluence 40 9.9 19.2 26.2 2 2 0

Replicate variance by Drug × Concentration (plate layout order)

Rows follow the physical plate: assay, plate, each drug’s row block top-first, then across the cols (top dose → untreated) — so this table can be read side by side with the plate map. Wells lists the triplicate. Worst_Well is the well furthest from its triplicate median and CV_pct_drop1 is what the CV would become without it; when dropping it brings the group under threshold, a single well is to blame.

Assay Drug Day Conc Plate Wells N Mean SD CV_pct Worst_Well Worst_Value Worst_Dev_pct Other_Reps CV_pct_drop1 Verdict Untreated
MTT 63GE 7 1000 1 B2/C2/D2 3 0.2994 0.01192 4.0 C2 0.3128 +5.8 0.2899 / 0.2956 1.4 OK False
MTT 63GE 7 222.2 1 B3/C3/D3 3 0.3451 0.02336 6.8 B3 0.3703 +8.7 0.3407 / 0.3242 3.5 OK False
MTT 63GE 7 49.4 1 B4/C4/D4 3 0.3219 0.01756 5.5 D4 0.3019 -8.2 0.3349 / 0.3288 1.3 OK False
MTT 63GE 7 11 1 B5/C5/D5 3 0.3731 0.07097 19.0 B5 0.4488 +23.9 0.3623 / 0.3081 11.4 OK False
MTT 63GE 7 2.44 1 B6/C6/D6 3 0.3895 0.04232 10.9 D6 0.3464 -11.4 0.431 / 0.3911 6.9 OK False
MTT 63GE 7 0.54 1 B7/C7/D7 3 0.4078 0.06101 15.0 B7 0.4763 +22.8 0.3878 / 0.3593 5.4 OK False
MTT 63GE 7 0.12 1 B8/C8/D8 3 0.4533 0.02873 6.3 D8 0.4202 -10.1 0.4721 / 0.4675 0.7 OK False
MTT 63GE 7 0.026 1 B9/C9/D9 3 0.803 0.09584 11.9 D9 0.6925 -18.8 0.8635 / 0.853 0.9 OK False
MTT 63GE 7 0.006 1 B10/C10/D10 3 1.098 0.283 25.8 C10 1.414 +39.6 1.013 / 0.8674 11.0 one suspect well False
MTT 63GE 7 0.0006 1 B11/C11/D11 3 1.235 0.1091 8.8 D11 1.111 -13.4 1.282 / 1.313 1.7 OK True
MTT IsoGE 7 1000 1 E2/F2/G2 3 0.3331 0.07062 21.2 G2 0.4109 +30.4 0.2731 / 0.3152 10.1 OK False
MTT IsoGE 7 222.2 1 E3/F3/G3 3 0.5462 0.08543 15.6 G3 0.639 +20.8 0.5289 / 0.4708 8.2 OK False
MTT IsoGE 7 49.4 1 E4/F4/G4 3 1.079 0.1622 15.0 F4 0.893 -22.5 1.192 / 1.152 2.4 OK False
MTT IsoGE 7 11 1 E5/F5/G5 3 1.598 0.162 10.1 F5 1.77 +12.3 1.448 / 1.575 6.0 OK False
MTT IsoGE 7 2.44 1 E6/F6/G6 3 1.543 0.05314 3.4 E6 1.6 +4.2 1.535 / 1.495 1.9 OK False
MTT IsoGE 7 0.54 1 E7/F7/G7 3 1.675 0.1694 10.1 E7 1.863 +14.6 1.625 / 1.536 4.0 OK False
MTT IsoGE 7 0.12 1 E8/F8/G8 3 1.377 0.1602 11.6 F8 1.193 -17.8 1.487 / 1.451 1.7 OK False
MTT IsoGE 7 0.026 1 E9/F9/G9 3 1.41 0.3698 26.2 E9 0.984 -38.5 1.647 / 1.6 2.0 one suspect well False
MTT IsoGE 7 0.006 1 E10/F10/G10 3 1.323 0.2768 20.9 E10 1.015 -27.7 1.404 / 1.551 7.0 OK False
MTT IsoGE 7 0.0006 1 E11/F11/G11 3 1.393 0.01214 0.9 F11 1.407 +1.5 1.385 / 1.387 0.1 OK True
MTT 63GE6 7 1000 2 B2/C2/D2 3 0.4177 0.02432 5.8 B2 0.4457 +9.9 0.4056 / 0.4018 0.7 OK False
MTT 63GE6 7 222.2 2 B3/C3/D3 3 0.3996 0.01098 2.7 C3 0.3869 -4.6 0.4064 / 0.4054 0.2 OK False
MTT 63GE6 7 49.4 2 B4/C4/D4 3 0.4769 0.04395 9.2 C4 0.4284 -12.2 0.4882 / 0.5141 3.7 OK False
MTT 63GE6 7 11 2 B5/C5/D5 3 0.5414 0.001401 0.3 D5 0.5428 +0.3 0.5413 / 0.54 0.2 OK False
MTT 63GE6 7 2.44 2 B6/C6/D6 3 0.5736 0.06814 11.9 C6 0.6453 +14.1 0.5097 / 0.5657 7.4 OK False
MTT 63GE6 7 0.54 2 B7/C7/D7 3 0.6162 0.03725 6.0 B7 0.5788 -6.1 0.6166 / 0.6533 4.1 OK False
MTT 63GE6 7 0.12 2 B8/C8/D8 3 0.8896 0.06639 7.5 D8 0.9631 +10.5 0.834 / 0.8717 3.1 OK False
MTT 63GE6 7 0.026 2 B9/C9/D9 3 1.303 0.1706 13.1 D9 1.5 +23.1 1.192 / 1.218 1.6 OK False
MTT 63GE6 7 0.006 2 B10/C10/D10 3 1.599 0.02991 1.9 C10 1.565 -2.9 1.612 / 1.62 0.4 OK False
MTT 63GE6 7 0.0006 2 B11/C11/D11 3 1.645 0.1984 12.1 D11 1.848 +13.0 1.635 / 1.451 8.4 OK True
MTT 63VPE6 7 1000 2 E2/F2/G2 3 0.2938 0.0302 10.3 F2 0.2616 -12.3 0.3215 / 0.2982 5.3 OK False
MTT 63VPE6 7 222.2 2 E3/F3/G3 3 0.3613 0.0296 8.2 E3 0.3929 +10.1 0.3342 / 0.3569 4.6 OK False
MTT 63VPE6 7 49.4 2 E4/F4/G4 3 0.4356 0.04192 9.6 G4 0.482 +13.5 0.4245 / 0.4004 4.1 OK False
MTT 63VPE6 7 11 2 E5/F5/G5 3 0.591 0.03955 6.7 G5 0.635 +9.6 0.5584 / 0.5796 2.6 OK False
MTT 63VPE6 7 2.44 2 E6/F6/G6 3 0.6777 0.07402 10.9 F6 0.5925 -17.1 0.7262 / 0.7144 1.2 OK False
MTT 63VPE6 7 0.54 2 E7/F7/G7 3 0.7694 0.03789 4.9 E7 0.8119 +7.3 0.757 / 0.7392 1.7 OK False
MTT 63VPE6 7 0.12 2 E8/F8/G8 3 0.9771 0.1453 14.9 E8 1.125 +15.8 0.8347 / 0.9715 10.7 OK False
MTT 63VPE6 7 0.026 2 E9/F9/G9 3 1.397 0.1692 12.1 F9 1.202 -19.1 1.486 / 1.503 0.8 OK False
MTT 63VPE6 7 0.006 2 E10/F10/G10 3 1.607 0.1187 7.4 F10 1.472 -11.0 1.654 / 1.695 1.7 OK False
MTT 63VPE6 7 0.0006 2 E11/F11/G11 3 1.507 0.05492 3.6 G11 1.444 -6.1 1.538 / 1.54 0.1 OK True

Suspect wells — groups above the CV threshold (grouped by drug)

Assay Drug Day Conc Plate CV_pct Worst_Well Worst_Value Other_Reps Worst_Dev_pct CV_pct_drop1 Verdict Likely_Cause Untreated Reversal_Excluded
MTT 63GE 7 0.006 1 25.8 C10 1.414 1.013 / 0.8674 +39.6 11.0 one suspect well isolated well False False
MTT IsoGE 7 0.026 1 26.2 E9 0.984 1.647 / 1.6 -38.5 2.0 one suspect well isolated well False False

Systematic bias by replicate position (plate row)

Mean % deviation of each plate row from its own triplicate median. Near 0 = that replicate position tracks the others. Beyond ±15% the position is systematically off, and individual wells flagged there are symptoms of that, not independent bad wells. The other plate axis is not shown: it carries concentration, so a “bias” along it would just be the dose response.

    B C D E F G
Assay Plate            
MTT 1 +6.8 +4.8 -10.3 -6.3 -3.5 +5.3
2 -1.2 -1.7 +5.8 +3.9 -8.5 +1.8

  Replicate groups: 40 (3–3 wells each)
  Above 25% CV: 2  [2 with a single suspect well, 0 scattered]

  Saved to /content/drive/MyDrive/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data/well_qc: LS180_ReplicateQC_Confluence.csv (+ _overview, _position_bias)

Drug & Color Setup

Show code
# ===========================================================================
# 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}')

4 drugs: ['63GE', '63GE6', '63VPE6', 'IsoGE']
  AB prefixes  (4): ['63GE', '63GE6', '63VPE6', 'IsoGE']
  Linker-payloads (0): []

Growth Kinetics Plots

Show code
# ===========================================================================
# 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 ===

LL.4 Model & Interpolation Functions

Show code
# ===========================================================================
# 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.

Dose-Response Fitting & Plotting

Show code
from matplotlib.lines import Line2D
# ===========================================================================
# 7. Dose-response fitting & plotting — Confluence
# ===========================================================================
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


def _dual_legend(ax, fig, ic50_label=None, ic50_plotted=False):
    handles, labels = ax.get_legend_handles_labels()

    if ic50_plotted and ic50_label:
        # Smaller diamond for small legend
        ic50_handle_small = Line2D([0], [0], marker='D', linestyle='none',
                                    color='black', markeredgecolor='black',
                                    markersize=6)
        handles_small = [ic50_handle_small] + handles
        labels_small = [ic50_label] + labels

        # Larger diamond for large legend
        ic50_handle_large = Line2D([0], [0], marker='D', linestyle='none',
                                    color='black', markeredgecolor='black',
                                    markersize=10)
        handles_large = [ic50_handle_large] + handles
        labels_large = [ic50_label] + labels
    else:
        handles_small = handles
        labels_small = labels
        handles_large = handles
        labels_large = labels

    ax.legend(handles_small, labels_small, fontsize=8, loc='lower left')
    fig.legend(handles_large, labels_large, fontsize=13, loc='center left',
               bbox_to_anchor=(1.02, 0.5), bbox_transform=ax.transAxes,
               ncol=1, frameon=True)


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})')
    _dual_legend(ax, fig)
    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()
        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_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)
    _dual_legend(ax, fig, ic50_label='Absolute IC50 (50% untreated)',
                 ic50_plotted=_absolute_marker_labeled)
    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()
        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_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')
    _dual_legend(ax, fig, ic50_label='Fitted IC50 (LL.4 plateau midpoint)',
                 ic50_plotted=_fitted_marker_labeled)
    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): 4 reliable of 4

  Absolute IC50 at 50% untreated (Confluence): 4 of 4 crossed 50%

  Fitted-midpoint IC50 (%Viable Confluence): 4 reliable of 4

Normalized LL.4 — Relative IC50 on One Line

Show code
# Rescale each drug's fitted curve and data to its own fitted floor and top.
# Each curve then runs from 100% down to 0%, its own relative IC50 lands exactly at the 50% line.
# You can compare the IC50s horizontally along one line.
# ===========================================================================
# 7h. Normalized LL.4 — each curve rescaled to its own fitted floor & top
#     No refitting. The 7d %Viable LL.4 fits are replotted on
#         y_norm = 100 * (y - Bottom) / (Top - Bottom)
#     under which the LL.4 collapses to  100 / (1 + (x / EC50) ** slope),
#     i.e. every curve runs 100% -> 0% and crosses the 50% line exactly at
#     its own relative IC50 (= EC50 = popt[3]), so IC50s can be read off
#     horizontally along that one line. The curve is defined at any
#     concentration, so it is drawn past the highest tested dose (faded) —
#     that part is the visual extension down to the floor, not data.
# ===========================================================================
print('\n=== 7h. Normalized LL.4 curves (own floor -> own top) ===')

NORM_MIN_SPAN    = 5.0    # min fitted (Top - Bottom) in % points to trust the rescaling
NORM_FLOOR_PCT   = 2.0    # extend each curve until it decays to this normalized %
NORM_EXT_DECADES = 3.0    # cap that extension at N decades past the highest tested dose

_norm_metric = 'Confluence'
_norm_pv_col = f'PctViable_{_norm_metric}'


def norm_ll4(x, slope, ec50):
    """LL.4 rescaled to its own plateaus: runs 100 -> 0, equals 50 at x = ec50."""
    return 100.0 / (1.0 + (np.asarray(x, dtype=float) / ec50) ** slope)


_norm_rows = []

for day in fit_days:
    # --- gather the usable fits for this day ---
    day_fits = {}
    for drug in all_drugs:
        popt = fit_params_pv.get((drug, day))
        if popt is None:
            continue
        slope, lower, upper, ec50 = popt
        span = upper - lower
        _r2row = df_ic50_pv[(df_ic50_pv.Drug == drug) & (df_ic50_pv.Day == day)]
        r2 = float(_r2row.R2.values[0]) if not _r2row.empty else np.nan
        reliable = bool(_r2row.Reliable.values[0]) if not _r2row.empty else False
        if not np.isfinite(span) or span < NORM_MIN_SPAN:
            print(f'  Day {day} — {drug}: fitted span {span:.1f}% < {NORM_MIN_SPAN}%, '
                  f'not rescaled (flat fit).')
            continue
        day_fits[drug] = dict(slope=slope, lower=lower, upper=upper, ec50=ec50,
                              span=span, r2=r2, reliable=reliable)

    if not day_fits:
        print(f'  Day {day}: no fits with a usable plateau span, skipping.')
        continue

    # --- x window: tested range (solid) + extension to the floor (faded) ---
    _tested = _analysis_rows(
        df[(df.Day == day) & (df.Drug.isin(day_fits))]
          .dropna(subset=['Conc', _norm_pv_col])
    )
    _tested = _tested[(_tested.Conc >= _xlo) & (_tested.Conc <= _xhi)]
    x_lo_t, x_hi_t = _tested.Conc.min(), _tested.Conc.max()

    _decay = (100.0 / NORM_FLOOR_PCT) - 1.0
    x_ext = max(f['ec50'] * _decay ** (1.0 / f['slope']) for f in day_fits.values())
    x_ext = float(np.clip(x_ext, x_hi_t, x_hi_t * 10 ** NORM_EXT_DECADES))
    # only call it an extension if it is wide enough to actually see
    _has_ext = x_ext > x_hi_t * 1.5

    fig, ax = plt.subplots(figsize=(12, 7))
    _y_seen = [0.0, 100.0]

    for drug, f in day_fits.items():
        scale = 100.0 / f['span']

        # observed %Viable, rescaled by the same (Bottom, Top)
        sub = df[(df.Drug == drug) & (df.Day == day)].dropna(subset=[_norm_pv_col])
        if not sub.empty:
            means = sub.groupby('Conc')[_norm_pv_col].mean()
            sds = sub.groupby('Conc')[_norm_pv_col].std().fillna(0)
            y_pts = (means.values - f['lower']) * scale
            y_err = sds.values * scale
            ax.errorbar(means.index, y_pts, yerr=y_err,
                        marker='o', capsize=3, color=drug_colors[drug],
                        linestyle='none', linewidth=1.0)
            _y_seen += [float(np.nanmin(y_pts - y_err)), float(np.nanmax(y_pts + y_err))]

        # fitted curve over the tested range
        x_in = np.geomspace(x_lo_t, x_hi_t, 200)
        ax.plot(x_in, norm_ll4(x_in, f['slope'], f['ec50']),
                color=drug_colors[drug], linewidth=1.8,
                linestyle=drug_linestyles[drug],
                label=f"{drug} — IC50 {f['ec50']:.3g} nM" + ('' if f['reliable'] else ' *'))

        # extension past the highest tested dose (same style, faded)
        if _has_ext:
            x_out = np.geomspace(x_hi_t, x_ext, 200)
            ax.plot(x_out, norm_ll4(x_out, f['slope'], f['ec50']),
                    color=drug_colors[drug], linewidth=1.4,
                    linestyle=drug_linestyles[drug], alpha=0.35)

        # relative IC50 sits exactly on the 50% line by construction
        ax.plot(f['ec50'], 50.0, 'D', color=drug_colors[drug], markersize=10,
                markeredgecolor='black', markeredgewidth=1, zorder=5)

        _norm_rows.append({
            'Day': day, 'Drug': drug,
            'Relative_IC50_nM': f['ec50'], 'Slope': f['slope'],
            'Bottom_%Viable': f['lower'], 'Top_%Viable': f['upper'],
            'Span_%': f['span'], 'R2': f['r2'], 'Reliable': f['reliable'],
            'Extrapolated': bool(f['ec50'] > x_hi_t),
        })

    ax.axhline(50, color='red', ls='--', alpha=0.7, zorder=1)
    ax.axhline(100, color='grey', ls='--', alpha=0.3, zorder=1)
    ax.axhline(0, color='grey', ls='--', alpha=0.3, zorder=1)
    if _has_ext:
        ax.axvline(x_hi_t, color='grey', ls=':', alpha=0.6, zorder=1)

    ax.set_xscale('log')
    _lo, _hi = min(_y_seen), max(_y_seen)
    _mg = (_hi - _lo) * 0.05
    ax.set_ylim(_lo - _mg, _hi + _mg)
    ax.set_xlim(x_lo_t * 0.8, (x_ext if _has_ext else x_hi_t) * 1.2)
    ax.set_xlabel('Concentration (nM, log scale)')
    ax.set_ylabel(f'% of own fitted range\n(100% = fitted Top, 0% = fitted Bottom)')
    ax.set_title(f'Day {day} — Normalized LL.4 ({_norm_metric})\n'
                 f'each curve rescaled to its own floor & top; '
                 f'relative IC50 = crossing of the 50% line')

    handles, labels = ax.get_legend_handles_labels()
    extra_h = [Line2D([0], [0], marker='D', linestyle='none', color='black',
                      markeredgecolor='black', markersize=6)]
    extra_l = ['Relative IC50 (EC50, on the 50% line)']
    if _has_ext:
        extra_h.append(Line2D([0], [0], color='grey', linewidth=1.4, alpha=0.35))
        extra_l.append('Extension past highest tested dose')
    # Legend outside the right edge only (no inset legend on this plot)
    extra_h_large = [Line2D([0], [0], marker='D', linestyle='none', color='black',
                            markeredgecolor='black', markersize=10)] + extra_h[1:]
    fig.legend(extra_h_large + handles, extra_l + labels, fontsize=13, loc='center left',
               bbox_to_anchor=(1.02, 0.5), bbox_transform=ax.transAxes,
               ncol=1, frameon=True)
    plt.tight_layout()
    save_fig()

df_norm_ic50 = pd.DataFrame(_norm_rows)
if not df_norm_ic50.empty:
    display(Markdown('### Relative IC50 from the normalized LL.4 fits\n'
                     '_Reliable = False → fit not reliable; '
                     'Extrapolated = IC50 beyond highest tested dose_'))
    _sig3 = lambda x: f'{x:.3g}'
    display(
        df_norm_ic50.style
        .format({c: _sig3 for c in ['Relative_IC50_nM', 'Slope', 'Bottom_%Viable',
                                    'Top_%Viable', 'Span_%', 'R2']}, na_rep='—')
        .hide(axis='index')
    )
    _out_norm = os.path.join(DATA_DIR, f'{EXPORT_STEM}_IC50_{_norm_metric}_normalizedLL4.csv')
    df_norm_ic50.to_csv(_out_norm, index=False)
    print(f'\nSaved: {_out_norm}')

=== 7h. Normalized LL.4 curves (own floor -> own top) ===

Relative IC50 from the normalized LL.4 fits

Reliable = False → fit not reliable; Extrapolated = IC50 beyond highest tested dose

Day Drug Relative_IC50_nM Slope Bottom_%Viable Top_%Viable Span_% R2 Reliable Extrapolated
7 63GE 0.0263 1.18 28.2 100 71.8 0.928 True False
7 63GE6 0.0698 0.968 28.6 100 71.4 0.966 True False
7 63VPE6 0.279 0.679 25.7 100 74.3 0.936 True False
7 IsoGE 87.7 1.89 25.2 100 74.8 0.81 True False

Saved: /content/drive/MyDrive/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data/LS180_IC50_Confluence_normalizedLL4.csv

Trellis: Test vs Isotype by Linker-Payload

Show code
# ===========================================================================
# 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 not 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: []

Trellis: Therapeutic Index by Concentration

Show code
# ===========================================================================
# 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

IC50 Summary Tables (Curve Fit)

Show code
# ===========================================================================
# 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='—')
)

IC50 SUMMARY — COMPARISON

Confluence_direct — IC50 (nM), reliable only (R²≥0.5, in range)

Drug Day 7
63GE 0.025603
63GE6 0.063130
63VPE6 0.190005
IsoGE 76.095906

Confluence_pctViable — IC50 (nM), reliable only (R²≥0.5, in range)

Drug Day 7
63GE 0.026289
63GE6 0.069818
63VPE6 0.279319
IsoGE 87.699049

ABSOLUTE IC50 AT 50% OF UNTREATED (no curve fitting)

Confluence — Absolute IC50 (nM) at 50% of untreated

Drug Day 7
63GE 0.058490
63GE6 0.173762
63VPE6 0.699352
IsoGE 145.391072

Confluence: Absolute 50%-untreated vs fitted-midpoint IC50

    IC50_interp IC50_LL4 Diff_%
Drug Day      
63GE 7 0.0585 0.0263 122.5
63GE6 7 0.174 0.0698 148.9
63VPE6 7 0.699 0.279 150.4
IsoGE 7 145 87.7 65.8

Absolute IC50 Tables

Show code
# ===========================================================================
# 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()

IC50 Bar Plots

Show code
# ===========================================================================
# 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/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data/LS180_long.csv
Saved: /content/drive/MyDrive/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data/LS180_IC50_Confluence_interpolated.csv
Saved: /content/drive/MyDrive/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data/LS180_IC50_Confluence_direct.csv
Saved: /content/drive/MyDrive/2026-09-04 Cytotox 63GE8, IsoGE8, 64GE6, 64VPE6/IC50 analysis/LS180/Data/LS180_IC50_Confluence_pctViable.csv

Total plots saved: 9
Done.
Show code
# ===========================================================================
# 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.