scripts/rank_islet1_stacks.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/rank_islet1_stacks.py · SHA-256: c05d5fe606219e88…
round(math.sqrt(float(max(gls_b_diag[1:7]))), 4),
],
"two_factor_interaction_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_b_diag[7:22]))), 4),
round(math.sqrt(float(max(gls_b_diag[7:22]))), 4),
],
"stage_block_B_gls_se_pp": round(math.sqrt(float(gls_b_diag[22])), 4),
"stage1_split_batch_s1a_vs_s1b_incidence_2batch_residual_gls_se_sol61_r3": {
"covariance_mode": "MODE2_75_SHARED_GENOTYPE_CLONES_ACROSS_25_GENOTYPES",
"main_effect_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_split_2b_diag[1:7]))), 4),
round(math.sqrt(float(max(gls_split_2b_diag[1:7]))), 4),
],
"two_factor_interaction_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_split_2b_diag[7:22]))), 4),
round(math.sqrt(float(max(gls_split_2b_diag[7:22]))), 4),
],
"stage_block_B_gls_se_pp": round(math.sqrt(float(gls_split_2b_diag[22])), 4),
},
"stage1_split_batch_s1a_vs_s1b_incidence_1batch_stage1_residual_gls_se_sol61_r3": {
"covariance_mode": "MODE2_75_SHARED_GENOTYPE_CLONES_ACROSS_25_GENOTYPES",
"main_effect_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_split_1b_diag[1:7]))), 4),
round(math.sqrt(float(max(gls_split_1b_diag[1:7]))), 4),
],
"two_factor_interaction_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_split_1b_diag[7:22]))), 4),
round(math.sqrt(float(max(gls_split_1b_diag[7:22]))), 4),
],
"stage_block_B_gls_se_pp": round(math.sqrt(float(gls_split_1b_diag[22])), 4),
},
"stage1_split_batch_s1a_vs_s1b_mode1_independent_run_nested_clones_2batch_residual_gls_se_kimi31_r3": {
"covariance_mode": "MODE1_108_INDEPENDENT_RUN_NESTED_CLONE_DIFFERENTIATIONS",
"main_effect_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_mode1_split_2b_diag[1:7]))), 4),
round(math.sqrt(float(max(gls_mode1_split_2b_diag[1:7]))), 4),
],
"two_factor_interaction_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_mode1_split_2b_diag[7:22]))), 4),
round(math.sqrt(float(max(gls_mode1_split_2b_diag[7:22]))), 4),
],
"stage_block_B_gls_se_pp": round(math.sqrt(float(gls_mode1_split_2b_diag[22])), 4),
},
"stage1_split_batch_s1a_vs_s1b_mode1_independent_run_nested_clones_1batch_stage1_residual_gls_se_kimi31_r3": {
"covariance_mode": "MODE1_108_INDEPENDENT_RUN_NESTED_CLONE_DIFFERENTIATIONS",
"main_effect_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_mode1_split_1b_diag[1:7]))), 4),
round(math.sqrt(float(max(gls_mode1_split_1b_diag[1:7]))), 4),
],
"two_factor_interaction_half_effect_beta_gls_se_range_pp": [
round(math.sqrt(float(min(gls_mode1_split_1b_diag[7:22]))), 4),
round(math.sqrt(float(max(gls_mode1_split_1b_diag[7:22]))), 4),
],
"stage_block_B_gls_se_pp": round(math.sqrt(float(gls_mode1_split_1b_diag[22])), 4),
},
}
power_and_covariance_summary = {
"stage_by_main_interaction_and_batch_covariance_qualification_ultracode_v80_12": {
"stage_by_main_29col_expansion": {
"n_rows": n,
"n_columns_including_6_stage_by_main_interactions": 29,
"exact_rational_rank": rank_29col_stage_by_main,
"rank_deficiency": 29 - rank_29col_stage_by_main,
"tier1_16run_rank": s["rank_tier1_22"],
"tier1_plus_unreplicated_tier2_26run_rank_23col": s["rank23"],
"active_tier1_main_effect_columns_note": (
"All 6 main effects (X1..X6) are active and clear of 2FIs in the 16-run Tier-1 screening block (rank 14/22); "
"adding the 8 Tier-2 de-aliasing genotypes + 2 bridge controls (26 runs) or the full replicated 36-run table "
"achieves full rank 22/22 (genetic) and 23/23 (with stage_block_B), whereas the 29-column expansion "
"[I, X1..X6, 15 2FIs, B, B*X1..B*X6] has rank 25/29 (deficiency 4) in the 36-run design, rank 26/29 (deficiency 3) "
"in the 38-run 4-bridge upgrade, and achieves full rank 29/29 with a minimum of 4 extra Stage-1 bridge runs (6 bridges total, 40 runs; e.g. Runs 01, 16, 02, 03, 04, 05) or in the 8-bridge 42-run upgrade (Findings 021, 075, 084)."
),
"identifiability_note": (
"The two inter-stage bridge genotypes (Runs 01/25 and Runs 16/26) together with the Stage-1 de-aliasing and lead runs (Runs 17-24, 27-36) jointly identify the additive stage shift B "
f"(where genetic columns X1..X6 and 15 2FIs use {{-1, +1}} coding [full High-minus-Low contrast = 2*beta_j] while stage_block_B uses 0/1 indicator coding [0 = Stage 0 Runs 01-16, 1 = Stage 1 Runs 17-36, so beta_B is already the full Stage-1 minus Stage-0 shift], giving rank([X | B]) = 23/23 and projection residual SSR_B = ||(I - H_22) B||_2^2 = {_hip_frac_plain(s['ssr'])} = {float(s['ssr']):.4f} in 0/1 stage indicator coding [Var(beta_B) = {_hip_frac_plain(s['diag23'][22])} * sigma^2 = 0.688119 * sigma^2, SE(beta_B) = 0.8295 * sigma, MDE(beta_B) = 2.514 * sigma at df=13]; "
f"note that the two matched inter-stage bridge pairs alone contribute 1.0000 of the {float(s['ssr']):.4f} SSR_B in 0/1 indicator coding [68.81% of precision, with the remaining 63/139 = 0.4532 from the 18 non-bridge Stage-1 runs (Runs 17-24, 27-36); in {{-1, +1}} stage coding where B_pm = 2*B_01 - 1, SSR_B_pm = 4*SSR_B = 808/139 = 5.8129 with bridge sum 4.0000]), "
"whereas adding all 6 stage x main-effect terms B*X1..B*X6 yields 29 columns "
"with exact rank 25/29 (identifiability of the 23-column model assumes genetic main effects and 2FIs are invariant "
"across Stage 0 and Stage 1 blocks, or requires concurrent single-stage blocking / >=6 bridge genotypes in a >=40-run design; AUDIT-REF & AUDIT-REF)."
),
},
"differentiation_batch_random_intercept_sensitivity_sweep": batch_sensitivity_sweep,
},
"variance_assumptions": {
"sigma_clone_pp": 8.0,
"sigma_eps_donor_averaged_batch_cell_pp": 7.0,
"n_clones_per_genotype_or_run": 3,
"n_batches": 2,
"n_donors": 3,
"sigma_run_donor_averaged_batch_cell_pp": round(sigma_run_pp, 2),
"sigma_run_if_7pp_is_per_donor_well_residual_divided_by_n_donors_pp": round(sigma_run_3donors_pp, 2),
"clone_level_df_75_clones": 52,
"clone_level_df_108_clones": 85,
"n_distinct_design_rows_with_stage_indicator": n_distinct_with_stage,
"conservative_run_table_df_23col": n - 23,
"pure_error_df_within_stage_replicates_23col": n - n_distinct_with_stage,
"lack_of_fit_df_higher_order_interactions_23col": n_distinct_with_stage - 23,
"conservative_run_table_df_22col": n - 22,
},
"mode1_108_independent_run_nested_clones_exact_iid_run_covariance": {
"allocation_description": "3 independently derived/edited hPSC clones per run across all 36 runs (108 independent founder clone units total), yielding strictly diagonal Cov(Y_bar) = sigma_run^2 * I_36.",
"single_stage_22col_genetic_model": {
"tier1_16run_rank": s["rank_tier1_22"],
"combined_36run_rank_22col": s["rank22"],
"main_effect_best_X1_to_X3": {**_se_mde_entry(min_main_22, pairs=["X5"]), "deprecated_key_alias_note": "Legacy key name retained for compatibility; actual min-variance main effect is X5 (see main_effect_best_X5)."},
"main_effect_best_X5": _se_mde_entry(min_main_22, pairs=["X5"]),
"main_effect_worst_X4_to_X6": {**_se_mde_entry(max_main_22, pairs=["X1", "X2", "X3"]), "deprecated_key_alias_note": "Legacy key name retained for compatibility; actual max-variance main effects are X1, X2, X3 (see main_effect_worst_X1_to_X3)."},
"main_effect_worst_X1_to_X3": _se_mde_entry(max_main_22, pairs=["X1", "X2", "X3"]),
"two_factor_interaction_best_6pairs": _se_mde_entry(min_2fi_22, pairs=best_2fi_pairs_22),
"two_factor_interaction_worst_2pairs_X1X5_X4X6": {**_se_mde_entry(max_2fi_22, pairs=worst_2fi_pairs_22), "deprecated_key_alias_note": "Legacy key name retained for compatibility; actual 22-column worst-case 2FI is X2:X5 (1295/4448; see two_factor_interaction_worst_1pair_1295_over_4448_X2X5)."},
"two_factor_interaction_worst_1pair_1295_over_4448_X2X5": _se_mde_entry(max_2fi_22, pairs=worst_2fi_pairs_22),
},
"two_stage_23col_model_with_additive_stage_block_B": {
"combined_36run_rank_23col": s["rank23"],
"rank_if_augmented_with_6_stage_by_main_29col": rank_29col_stage_by_main,
"main_effect_best_X1_to_X3": {**_se_mde_entry(min_main_23, pairs=["X5"]), "deprecated_key_alias_note": "Legacy key name retained for compatibility; actual min-variance main effect is X5 (see main_effect_best_X5)."},
"main_effect_best_X5": _se_mde_entry(min_main_23, pairs=["X5"]),
"main_effect_worst_X4_to_X6": {**_se_mde_entry(max_main_23, pairs=["X1", "X2", "X3"]), "deprecated_key_alias_note": "Legacy key name retained for compatibility; actual max-variance main effects are X1, X2, X3 (see main_effect_worst_X1_to_X3)."},
"main_effect_worst_X1_to_X3": _se_mde_entry(max_main_23, pairs=["X1", "X2", "X3"]),
"two_factor_interaction_best_2pairs_115_over_1616_X4X5_X5X6": _se_mde_entry(min_2fi_23, pairs=best_2fi_pairs_23),
"two_factor_interaction_worst_1pair_1191_over_1616_X1X3": _se_mde_entry(max_2fi_23, pairs=worst_2fi_pairs_23),
"all_15_two_factor_interactions_by_contrast_name": {
HIP_TERM_NAMES[i]: _se_mde_entry(s["diag23"][i], pairs=[HIP_TERM_NAMES[i]])
for i in range(7, 22)
},
"stage_block_B": {
**_se_mde_entry(s["diag23"][22]),
"full_stage_difference_beta_B_se_pp": round(sigma_run_pp * math.sqrt(float(s["diag23"][22])), 4),
"full_stage_difference_beta_B_mde_80pct_alpha05_two_sided_df13_central_approx_pp": round(
(2.160369 + 0.841621) * sigma_run_pp * math.sqrt(float(s["diag23"][22])), 2
),
"full_stage_difference_beta_B_mde_80pct_alpha05_two_sided_df13_pp": round(
0.5 * float(_se_mde_entry(s["diag23"][22])["mde_80pct_power_2beta_t13_alpha_0_05_pp"]), 2
),
"coding_convention_note": (
"stage_block_B (column 23) is coded as a 0/1 indicator (0 = Stage 0 Runs 01-16, 1 = Stage 1 Runs 17-36), "
"so beta_B is already the full Stage-1 minus Stage-0 block difference (Var(beta_B) = 139/202 * sigma^2, "
"SE(beta_B) = 0.8295 * sigma = 4.5055 pp, MDE(beta_B) = 2.514 * sigma = 13.66 pp at df=13); the 2x multiplier fields in _se_mde_entry "
"apply only if stage_block_B is rescaled to {-1, +1} half-effect coding (AUDIT-REF & AUDIT-REF)."
),
},
},
},
"mode2_75_shared_genotype_clones_across_25_distinct_genotypes_gls_and_ols_sandwich": {
"allocation_description": (
"75 independently edited hPSC clones (3 clones per each of the 25 distinct genotypes), where each run cell averages n_batches = 2 independent differentiation batches per run "
"(giving v_eps_run = sigma_eps^2 / (n_clones * n_batches) = 49/6 pp^2 and diagonal run variance 29.50 pp^2 -> sigma_run = 5.43 pp; "
"note that if each run were a single differentiation batch n_batches = 1 with bridged/replicated runs 25-26, 27-34, 36 serving as the second batch for those 11 genotypes, "
"single-batch unbridged runs would have v_eps_run = 49/3 pp^2 -> sigma_run = sqrt(113/3) = 6.14 pp). "
"Here Cov(Y_bar) = (sigma_clone^2 / n_clones) * Z Z^T + (sigma_eps^2 / (n_clones * n_batches)) * I_36: "
f"shared clone random effects cancel inside the inter-stage bridge contrasts (cutting stage_block_B SE from "
f"{round(sigma_run_pp * math.sqrt(float(s['diag23'][22])), 2):.2f} pp [Mode 1 i.i.d.] to "
f"{round(math.sqrt(float(gls23_shared_var[22])), 2):.2f} pp [Mode 2 GLS] / "
f"{round(math.sqrt(float(ols23_shared_var[22])), 2):.2f} pp [Mode 2 OLS sandwich]) "
"while slightly increasing genetic SEs relative to 108 independent clones."
),
"single_stage_22col_ols_sandwich_se_pp": {
"main_effect_half_effect_beta_se_range_pp": [
round(math.sqrt(float(min(ols22_shared_var[1:7]))), 2),
round(math.sqrt(float(max(ols22_shared_var[1:7]))), 2),
],
"main_effect_full_contrast_2beta_se_range_pp": [
round(2.0 * math.sqrt(float(min(ols22_shared_var[1:7]))), 2),
round(2.0 * math.sqrt(float(max(ols22_shared_var[1:7]))), 2),
],
"two_factor_interaction_half_effect_beta_se_range_pp": [
round(math.sqrt(float(min(ols22_shared_var[7:22]))), 2),
round(math.sqrt(float(max(ols22_shared_var[7:22]))), 2),
],
"two_factor_interaction_full_contrast_2beta_se_range_pp": [
round(2.0 * math.sqrt(float(min(ols22_shared_var[7:22]))), 2),
round(2.0 * math.sqrt(float(max(ols22_shared_var[7:22]))), 2),
],
},
"two_stage_23col_ols_sandwich_se_pp": {
"main_effect_half_effect_beta_se_range_pp": [
round(math.sqrt(float(min(ols23_shared_var[1:7]))), 2),
round(math.sqrt(float(max(ols23_shared_var[1:7]))), 2),
],
"main_effect_full_contrast_2beta_se_range_pp": [
round(2.0 * math.sqrt(float(min(ols23_shared_var[1:7]))), 2),
round(2.0 * math.sqrt(float(max(ols23_shared_var[1:7]))), 2),
],
"two_factor_interaction_half_effect_beta_se_range_pp": [
round(math.sqrt(float(min(ols23_shared_var[7:22]))), 2),
round(math.sqrt(float(max(ols23_shared_var[7:22]))), 2),
],
"two_factor_interaction_full_contrast_2beta_se_range_pp": [
round(2.0 * math.sqrt(float(min(ols23_shared_var[7:22]))), 2),
round(2.0 * math.sqrt(float(max(ols23_shared_var[7:22]))), 2),
],
"stage_block_B_se_pp": round(math.sqrt(float(ols23_shared_var[22])), 2),
},
"two_stage_23col_gls_lmm_se_pp": {
"main_effect_half_effect_beta_se_range_pp": [
round(math.sqrt(float(min(gls23_shared_var[1:7]))), 2),
round(math.sqrt(float(max(gls23_shared_var[1:7]))), 2),
],
"main_effect_full_contrast_2beta_se_range_pp": [
round(2.0 * math.sqrt(float(min(gls23_shared_var[1:7]))), 2),
round(2.0 * math.sqrt(float(max(gls23_shared_var[1:7]))), 2),
],
"two_factor_interaction_half_effect_beta_se_range_pp": [
round(math.sqrt(float(min(gls23_shared_var[7:22]))), 2),
round(math.sqrt(float(max(gls23_shared_var[7:22]))), 2),
],
"two_factor_interaction_full_contrast_2beta_se_range_pp": [
round(2.0 * math.sqrt(float(min(gls23_shared_var[7:22]))), 2),
round(2.0 * math.sqrt(float(max(gls23_shared_var[7:22]))), 2),
],
"stage_block_B_se_pp": round(math.sqrt(float(gls23_shared_var[22])), 2),
},
},
# Top-level summary fields retained for direct inspection (explicitly labeling 22-col vs 23-col and best-case vs worst-case 2FI; Audit R03):
"main_effect_22col_best_half_effect_beta_j_se_pp": round(sigma_run_pp * math.sqrt(float(min_main_22)), 2),
"main_effect_22col_best_full_contrast_2beta_j_se_pp": round(2.0 * sigma_run_pp * math.sqrt(float(min_main_22)), 2),
"main_effect_22col_best_full_contrast_mde_80pct_power_t52_alpha_0_05_pp": round(mult_t52_a05 * 2.0 * sigma_run_pp * math.sqrt(float(min_main_22)), 2),
"main_effect_23col_best_half_effect_beta_j_se_pp": round(sigma_run_pp * math.sqrt(float(min_main_23)), 2),
"main_effect_23col_best_full_contrast_2beta_j_se_pp": round(2.0 * sigma_run_pp * math.sqrt(float(min_main_23)), 2),
"main_effect_23col_worst_half_effect_beta_j_se_pp": round(sigma_run_pp * math.sqrt(float(max_main_23)), 2),
"main_effect_23col_worst_full_contrast_2beta_j_se_pp": round(2.0 * sigma_run_pp * math.sqrt(float(max_main_23)), 2),
"best_case_2fi_23col_115_over_1616_half_effect_beta_jk_se_pp": round(sigma_run_pp * math.sqrt(float(min_2fi_23)), 2),
"best_case_2fi_23col_115_over_1616_full_contrast_2beta_jk_se_pp": round(2.0 * sigma_run_pp * math.sqrt(float(min_2fi_23)), 2),
"best_case_2fi_23col_115_over_1616_full_contrast_mde_80pct_power_t52_alpha_0_05_pp": round(mult_t52_a05 * 2.0 * sigma_run_pp * math.sqrt(float(min_2fi_23)), 2),