zefancainlp/xiaofan_backup / local_reports /build_denoising_final_code_comparison.ps1
zefancainlp's picture
download
raw
46.2 kB
param( [string]$OutputPath = "C:\Users\xiaof\Documents\TTT-discover\reports\denoising_final_code_comparison.html" ) $ErrorActionPreference = "Stop" $reportDir = Split-Path -Parent $OutputPath $singleFullPath = Join-Path $reportDir "denoising_single_anchor_final_best_full.py" $singleActivePath = Join-Path $reportDir "denoising_single_anchor_final_best.py" $baselinePath = Join-Path $reportDir "denoising_baseline50_final_best.py" foreach ($path in @($singleFullPath, $singleActivePath, $baselinePath)) { if (-not (Test-Path -LiteralPath $path)) { throw "Missing source file: $path" } } function Convert-CodeToHtml { param( [Parameter(Mandatory = $true)][string]$Path, [Parameter(Mandatory = $true)][string]$Prefix ) $lines = Get-Content -LiteralPath $Path $rendered = for ($index = 0; $index -lt $lines.Count; $index++) { $number = $index + 1 $encoded = [System.Net.WebUtility]::HtmlEncode($lines[$index]) if ($encoded.Length -eq 0) { $encoded = "&nbsp;" } "<span class=`"code-line`" id=`"$Prefix-line-$number`" data-line=`"$number`"><span class=`"line-number`">$number</span><span class=`"line-source`">$encoded</span></span>" } return ($rendered -join "`n") } function Convert-FunctionsToOptions { param([Parameter(Mandatory = $true)][string]$Path) $lines = Get-Content -LiteralPath $Path $options = @("<option value=`"1`">文件顶部</option>") for ($index = 0; $index -lt $lines.Count; $index++) { if ($lines[$index] -match '^def\s+([A-Za-z_][A-Za-z0-9_]*)') { $name = [System.Net.WebUtility]::HtmlEncode($Matches[1]) $number = $index + 1 $options += "<option value=`"$number`">$name · L$number</option>" } } return ($options -join "`n") } $singleFullCode = Convert-CodeToHtml -Path $singleFullPath -Prefix "single-full" $singleActiveCode = Convert-CodeToHtml -Path $singleActivePath -Prefix "single-active" $baselineCode = Convert-CodeToHtml -Path $baselinePath -Prefix "baseline" $singleFullFunctions = Convert-FunctionsToOptions -Path $singleFullPath $singleActiveFunctions = Convert-FunctionsToOptions -Path $singleActivePath $baselineFunctions = Convert-FunctionsToOptions -Path $baselinePath $html = @" <!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Denoising 最终代码对照:Single-anchor vs Baseline 50-step</title> <style> :root { color-scheme: light dark; --bg: #f5f7fb; --surface: #ffffff; --surface-soft: #f8fafc; --text: #172033; --muted: #64748b; --border: #dbe3ee; --single: #1769aa; --single-soft: #e8f3fc; --baseline: #a55716; --baseline-soft: #fff2e5; --good: #16734a; --good-soft: #e8f7ef; --warn: #9a5b08; --warn-soft: #fff6dd; --code-bg: #101827; --code-text: #dce7f5; --code-muted: #718096; --code-hit: #5b4310; --shadow: 0 10px 32px rgba(23, 32, 51, 0.08); } @media (prefers-color-scheme: dark) { :root { --bg: #0c111b; --surface: #131b29; --surface-soft: #172131; --text: #e7edf6; --muted: #9eacc0; --border: #2c394d; --single: #72b7ed; --single-soft: #152f43; --baseline: #f0ad6f; --baseline-soft: #3c291b; --good: #69d7a4; --good-soft: #153629; --warn: #f4c469; --warn-soft: #3b3019; --code-bg: #090e17; --code-text: #dce7f5; --code-muted: #6f8199; --code-hit: #5c4512; --shadow: 0 12px 34px rgba(0, 0, 0, 0.28); } } * { box-sizing: border-box; } html { scroll-behavior: smooth; } body { margin: 0; background: var(--bg); color: var(--text); font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif; line-height: 1.65; } button, input, select { font: inherit; } a { color: var(--single); } .page { width: min(1500px, calc(100% - 32px)); margin: 0 auto; padding: 36px 0 72px; } .hero { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.55fr); gap: 24px; align-items: end; margin-bottom: 24px; } .eyebrow { margin: 0 0 8px; color: var(--muted); font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; } h1, h2, h3, p { margin-top: 0; } h1 { margin-bottom: 10px; font-size: clamp(28px, 4vw, 48px); line-height: 1.14; letter-spacing: -0.035em; font-weight: 650; } h2 { margin-bottom: 14px; font-size: 24px; line-height: 1.25; font-weight: 650; } h3 { margin-bottom: 8px; font-size: 17px; line-height: 1.35; font-weight: 650; } .lede { margin-bottom: 0; max-width: 920px; color: var(--muted); font-size: 17px; } .verdict { padding: 18px 20px; border-left: 4px solid var(--good); background: var(--good-soft); border-radius: 0 12px 12px 0; } .verdict strong { display: block; margin-bottom: 4px; color: var(--good); font-size: 18px; } .verdict p { margin: 0; } .metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 28px; } .metric { min-height: 122px; padding: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); } .metric-label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 13px; } .metric-value { display: block; font-size: clamp(23px, 3vw, 34px); font-weight: 650; line-height: 1.1; font-variant-numeric: tabular-nums; } .metric-note { display: block; margin-top: 7px; color: var(--muted); font-size: 12px; } .single-text { color: var(--single); } .baseline-text { color: var(--baseline); } .good-text { color: var(--good); } .section { margin-top: 32px; padding-top: 6px; } .section-intro { max-width: 980px; margin-bottom: 18px; color: var(--muted); } .flows { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; } .flow-panel { padding: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); } .flow-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; } .flow-title h3 { margin: 0; } .flow-title span { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; } .flow-track { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; } .flow-node { padding: 8px 10px; border-radius: 8px; background: var(--surface-soft); border: 1px solid var(--border); font-size: 13px; line-height: 1.3; } .flow-node.single { color: var(--single); background: var(--single-soft); border-color: color-mix(in srgb, var(--single), transparent 65%); } .flow-node.baseline { color: var(--baseline); background: var(--baseline-soft); border-color: color-mix(in srgb, var(--baseline), transparent 65%); } .arrow { color: var(--muted); font-size: 14px; } .compare-table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); } table { width: 100%; border-collapse: collapse; min-width: 760px; } th, td { padding: 13px 16px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); } th { background: var(--surface-soft); font-size: 13px; font-weight: 650; } tr:last-child td { border-bottom: 0; } td:first-child { width: 16%; color: var(--muted); font-weight: 600; } code, pre { font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace; } :not(pre) > code { padding: 1px 5px; border-radius: 5px; background: var(--surface-soft); color: var(--text); border: 1px solid var(--border); font-size: 0.92em; } .architecture { display: grid; grid-template-columns: 0.8fr 1.2fr; gap: 16px; } .cause-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; } .cause-card { position: relative; padding: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); } .cause-card.dominant { border-top: 4px solid var(--single); } .cause-card.secondary { border-top: 4px solid var(--good); } .cause-card.pressure { border-top: 4px solid var(--baseline); } .cause-rank { display: block; margin-bottom: 8px; color: var(--muted); font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; } .cause-card p { margin-bottom: 0; color: var(--muted); } .mechanism-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 16px; } .mechanism { padding: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; } .mechanism ol { display: grid; gap: 8px; margin: 12px 0 0; padding: 0; list-style: none; counter-reset: mechanism-step; } .mechanism li { position: relative; padding: 10px 12px 10px 42px; background: var(--surface-soft); border-radius: 8px; counter-increment: mechanism-step; } .mechanism li::before { content: counter(mechanism-step); position: absolute; left: 12px; top: 10px; width: 20px; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; } .mechanism li + li::after { content: "↓"; position: absolute; left: 17px; top: -13px; color: var(--muted); font-size: 13px; } .evidence-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 16px; } .evidence-panel { padding: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; } .evidence-panel p:last-child { margin-bottom: 0; } .delta-positive { color: var(--good); font-variant-numeric: tabular-nums; } .delta-negative { color: var(--baseline); font-variant-numeric: tabular-nums; } .efficiency-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 16px; } .efficiency-item { padding: 16px 18px; background: var(--surface-soft); border-left: 3px solid var(--border); } .efficiency-item strong { display: block; margin-bottom: 5px; } .efficiency-item p { margin: 0; color: var(--muted); } .analysis-panel { padding: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); } .function-list { display: grid; gap: 8px; margin: 12px 0 0; padding: 0; list-style: none; } .function-list li { display: grid; grid-template-columns: minmax(180px, 0.65fr) 1.35fr auto; gap: 12px; align-items: baseline; padding-bottom: 8px; border-bottom: 1px solid var(--border); } .function-list li:last-child { border-bottom: 0; padding-bottom: 0; } .function-list small { color: var(--muted); font-variant-numeric: tabular-nums; } .formula { margin: 14px 0 0; padding: 14px 16px; overflow-x: auto; background: var(--code-bg); color: var(--code-text); border-radius: 10px; font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace; font-size: 13px; line-height: 1.65; white-space: pre; } .findings { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; } .finding { padding: 17px 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; } .finding p { margin: 0; color: var(--muted); } .finding.warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn), transparent 65%); } .code-section { margin-top: 38px; } .code-guide { display: flex; flex-wrap: wrap; gap: 10px 20px; margin-bottom: 14px; color: var(--muted); font-size: 13px; } .code-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; align-items: start; } .code-panel { min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; } .code-panel-header { padding: 14px 16px; border-bottom: 1px solid var(--border); } .code-panel-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; } .code-panel-title h3 { margin: 0; } .code-panel-title span { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; } .controls { display: grid; grid-template-columns: minmax(130px, 0.65fr) minmax(160px, 1fr) auto; gap: 8px; } input, select, button { min-height: 38px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-soft); color: var(--text); padding: 7px 10px; } button { cursor: pointer; } button:hover { border-color: var(--single); } .code-viewport { height: min(68vh, 760px); min-height: 420px; overflow: auto; background: var(--code-bg); color: var(--code-text); scroll-behavior: smooth; tab-size: 4; } .code-block { display: block; min-width: max-content; margin: 0; padding: 12px 0 22px; font-size: 12px; line-height: 1.55; } .code-line { display: grid; grid-template-columns: 52px minmax(max-content, 1fr); min-height: 19px; } .code-line.is-match { background: var(--code-hit); } .code-line.is-target { background: color-mix(in srgb, var(--single), transparent 72%); } .line-number { position: sticky; left: 0; z-index: 1; padding-right: 12px; background: var(--code-bg); color: var(--code-muted); text-align: right; user-select: none; font-variant-numeric: tabular-nums; } .line-source { display: block; padding: 0 16px 0 10px; white-space: pre; } .search-status { min-height: 22px; margin-top: 7px; color: var(--muted); font-size: 12px; } .hidden { display: none !important; } .footer { margin-top: 30px; padding-top: 18px; border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; } @media (max-width: 980px) { .hero, .flows, .architecture, .code-grid, .cause-grid, .mechanism-pair, .evidence-grid { grid-template-columns: 1fr; } .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); } .efficiency-grid { grid-template-columns: 1fr; } .code-viewport { height: 620px; } } @media (max-width: 620px) { .page { width: min(100% - 20px, 1500px); padding-top: 22px; } .metrics, .findings { grid-template-columns: 1fr; } .controls { grid-template-columns: 1fr; } .function-list li { grid-template-columns: 1fr; gap: 2px; } .code-viewport { min-height: 380px; height: 70vh; } } </style> </head> <body> <main class="page"> <header class="hero"> <div> <p class="eyebrow">Denoising · final best code inspection</p> <h1>Single-anchor 与 Baseline 50-step<br>最终代码对照</h1> <p class="lede"> 比较对象是两个运行各自最终搜索池中 MSE 最低的真实可执行程序。 Single-anchor 不是从头重写 denoiser,而是在固定 MAGIC 产生的 <code>Z</code> 上学习一个后处理函数图。 </p> </div> <aside class="verdict"> <strong>更小的程序,得到更低的 MSE</strong> <p>Single-anchor 的可变代码只有 150 行、4 个函数;它主要优化 <code>X</code> 与 <code>Z</code> 的混合,而不是堆叠新的 denoising 模块。</p> </aside> </header> <section class="metrics" aria-label="核心指标"> <div class="metric"> <span class="metric-label">Single-anchor MSE</span> <span class="metric-value single-text">0.16264931</span> <span class="metric-note">state a5bab5fb · timestep 24</span> </div> <div class="metric"> <span class="metric-label">Baseline MSE</span> <span class="metric-value baseline-text">0.16403095</span> <span class="metric-note">state c5843e35 · timestep 49</span> </div> <div class="metric"> <span class="metric-label">MSE 差值</span> <span class="metric-value good-text">−0.00138164</span> <span class="metric-note">Single-anchor 相对低 0.842%</span> </div> <div class="metric"> <span class="metric-label">可变代码规模</span> <span class="metric-value">150 / 1043</span> <span class="metric-note">Single-anchor / Baseline 内容行</span> </div> </section> <section class="section" id="architecture"> <h2>一眼看清:两份代码改的不是同一层</h2> <p class="section-intro"> Baseline 直接生成完整 <code>magic_denoise(X)</code>;Single-anchor 保留固定 backbone,搜索对象只是接收 <code>X, Z</code> 的 active function graph。 </p> <div class="flows"> <article class="flow-panel"> <div class="flow-title"> <h3 class="single-text">Single-anchor</h3> <span>230 行完整展开 / 150 行可变</span> </div> <div class="flow-track" aria-label="Single-anchor 调用流程"> <span class="flow-node">X</span><span class="arrow">→</span> <span class="flow-node">固定 MAGIC</span><span class="arrow">→</span> <span class="flow-node">Z</span><span class="arrow">→</span> <span class="flow-node single">sqrt 自适应混合</span><span class="arrow">→</span> <span class="flow-node single">cell/gene 收缩</span><span class="arrow">→</span> <span class="flow-node single">gene 校准</span><span class="arrow">→</span> <span class="flow-node single">raw 回混</span> </div> </article> <article class="flow-panel"> <div class="flow-title"> <h3 class="baseline-text">Baseline 50-step</h3> <span>1043 行完整重写</span> </div> <div class="flow-track" aria-label="Baseline 调用流程"> <span class="flow-node">X</span><span class="arrow">→</span> <span class="flow-node baseline">预缩放 / winsorize</span><span class="arrow">→</span> <span class="flow-node baseline">Anscombe / HVG</span><span class="arrow">→</span> <span class="flow-node baseline">多图 diffusion</span><span class="arrow">→</span> <span class="flow-node baseline">双 variant</span><span class="arrow">→</span> <span class="flow-node baseline">NMF / PCA</span><span class="arrow">→</span> <span class="flow-node baseline">kNN / ALRA</span> </div> </article> </div> </section> <section class="section" id="why-short"> <h2>为什么 Single-anchor 会短而且有效:三层机制叠加</h2> <p class="section-intro"> 这不是“模型偶然写出了一段短代码”。最主要的原因是任务被改写成了 residual correction;其次是 PFG 编译器把搜索动作限制成局部、可自动回收的符号修改;最后,baseline 的目标函数没有任何代码长度或运行代价惩罚,天然鼓励保留旧模块并继续加新模块。 </p> <div class="cause-grid"> <article class="cause-card dominant"> <span class="cause-rank">原因 1 · 主因</span> <h3>它解决的是更窄的问题</h3> <p> Baseline 学的是完整映射 <code>X → denoised</code>。Single-anchor 免费获得固定 MAGIC 的 <code>Z</code>,只学 <code>(X, Z) → better Z</code>。图构建、diffusion 和 inverse transform 已经由 backbone 完成,因此最终最优解可以只是几组逐元素混合和统计校准。 </p> </article> <article class="cause-card secondary"> <span class="cause-rank">原因 2 · 结构性压缩</span> <h3>动作是“改一个符号”,不是“重写整份程序”</h3> <p> PFG 每次只允许替换一个现有 reachable symbol。没有提到的函数自动保留;新 helper 必须从该 anchor 可达;替换后不可达的旧 helper 自动删除。模型不需要复制 1000 行上下文,也更容易撤掉无用结构。 </p> </article> <article class="cause-card pressure"> <span class="cause-rank">原因 3 · Baseline 的增长压力</span> <h3>奖励只看 MSE,不惩罚复杂度</h3> <p> Baseline 每轮必须返回一份完整 <code>magic_denoise</code>。在 Poisson 硬约束和运行失败风险下,最安全的生成策略是保留已有代码,再以很小权重叠加一个 PCA、NMF、kNN 或 correction。即使模块贡献接近零,只要整份程序被选中,它也会进入下一轮代码。 </p> </article> </div> <div class="mechanism-pair"> <article class="mechanism"> <h3 class="single-text">Single-anchor 的因果链</h3> <ol> <li>固定 MAGIC 先把高维原始 counts 压成一个已有质量的 <code>Z</code>。</li> <li>候选 patch 只能改变一个 anchor,因此同胞候选间的主要差异被限制在一个函数或常量。</li> <li>得分变化更容易对应到当前 anchor,而不是同时变化的二十多个函数。</li> <li>好公式留在 graph 中继续调常数;失去调用关系的 helper 被 reachability pruning 自动回收。</li> <li>最终收敛为 4 个互补操作:sqrt mix、dropout shrink、gene scaling、raw mix。</li> </ol> </article> <article class="mechanism"> <h3 class="baseline-text">Baseline 的因果链</h3> <ol> <li>prompt 放入完整当前程序,并要求重新输出完整 <code>magic_denoise</code>。</li> <li>一次生成可能同时改入口、多个 helper、常量和处理顺序,评价只返回一个最终 MSE。</li> <li>无法从这一个分数判断究竟是哪一段代码有效,删除旧模块的风险高于保留它。</li> <li>模型倾向于复制旧程序,再增加一个低权重“保险模块”;没有长度惩罚阻止这种累积。</li> <li>上下文和执行越来越重,完整重写又制造更多格式、超时及依赖失败机会。</li> </ol> </article> </div> </section> <section class="section" id="mechanism-evidence"> <h2>这个解释能在运行链和代码里直接看到</h2> <div class="compare-table-wrap"> <table> <thead> <tr><th>运行位置</th><th class="single-text">Single-anchor active graph</th><th class="baseline-text">Baseline full program</th></tr> </thead> <tbody> <tr><td>初始</td><td>2 行、1 个 noop readout,backbone 另行固定</td><td>60 行、1 个 <code>magic_denoise</code></td></tr> <tr><td>step 10</td><td>187 行、5 个函数,MSE 0.165346</td><td>623 行、13 个函数,MSE 0.182433</td></tr> <tr><td>中后期</td><td>step 14 达到 195 行峰值,随后边改善边压缩</td><td>step 27 为 636 行,之后继续增长到约 1000 行</td></tr> <tr><td>最终</td><td>150 行、4 个函数,MSE 0.162649</td><td>1043 行、24 个函数,MSE 0.164031</td></tr> <tr><td>距离自身峰值</td><td>195 → 150 行,减少 23.1%;函数峰值 6 → 4</td><td>1066 → 1043 行,只减少 2.2%;函数仍为峰值 24</td></tr> </tbody> </table> </div> <div class="evidence-grid"> <article class="evidence-panel"> <h3 class="single-text">PFG 确实发生了自动“垃圾回收”</h3> <p><code>step 6</code> 修改 <code>_sqrt_weighted_mix</code> 后,<code>_compute_gamma</code> 失去可达性并被删除;MSE 同时从 0.168573 降到 0.167456。</p> <p><code>step 18</code> 重写 gene scaling helper 后,4 个旧常量 <code>_GENE_SCALE_*</code> 自动删除,MSE 不变。</p> <p><code>step 21</code> 替换 readout 时又删除 <code>_gene_dropout_factor</code> 和 <code>_scale_raw_counts_to_Z_libsize</code>,代码 167 → 153 行,MSE 反而继续下降。</p> </article> <article class="evidence-panel"> <h3 class="baseline-text">Baseline 的函数是按模块逐步累积的</h3> <p><code>step 0</code> 一次从 1 个函数变成 4 个;<code>step 10</code> 加入 full PCA;<code>step 22</code> 加入 NMF;<code>step 27–29</code> 加入双 variant、Seurat HVG、gene raw mix、low-count suppression 和 variance scaling。</p> <p><code>step 32–44</code> 又继续加入 gene mean/variance、HVG refinement、final log-kNN 和 ALRA。因为每轮的新状态就是整份 <code>parsed_code</code>,这些模块默认全部被复制进下一轮。</p> </article> </div> <h3 style="margin-top: 22px;">Baseline 消融:1043 行并不等于每个模块都在贡献</h3> <p class="section-intro"> 下表在同一份最终代码上关闭单个模块。“移除后 MSE 变化”为正表示该模块有帮助;为负表示移除它反而更好。不能把交互式消融简单相加,但它足以判断复杂度是否都被结果支持。 </p> <div class="compare-table-wrap"> <table> <thead><tr><th>关闭项</th><th>移除后 MSE</th><th>相对 full 的变化</th><th>解释</th></tr></thead> <tbody> <tr><td>NMF</td><td>0.16943413</td><td class="delta-positive">+0.00540319</td><td>Baseline 中最有实质作用的独立模块。</td></tr> <tr><td>gene raw mix</td><td>0.16422692</td><td class="delta-positive">+0.00019597</td><td>有小但明确的帮助。</td></tr> <tr><td>全部 final refinement</td><td>0.16421512</td><td class="delta-positive">+0.00018417</td><td>final PCA + log-kNN + ALRA 合计只有约 1.84e−4。</td></tr> <tr><td>单独 final PCA</td><td>0.16408239</td><td class="delta-positive">+0.00005144</td><td>贡献很小。</td></tr> <tr><td>单独 final log-kNN</td><td>0.16408707</td><td class="delta-positive">+0.00005612</td><td>贡献很小,但增加一次建图和 residual diffusion。</td></tr> <tr><td>multi-kNN</td><td>0.16402914</td><td class="delta-negative">−0.00000180</td><td>关闭后略好;第二套图没有得到独立支持。</td></tr> <tr><td>full diffusion</td><td>0.16402933</td><td class="delta-negative">−0.00000162</td><td>关闭后略好。</td></tr> <tr><td>low-count suppression</td><td>0.16400414</td><td class="delta-negative">−0.00002680</td><td>关闭后明显更好,说明该模块在最终组合中已是负贡献。</td></tr> </tbody> </table> </div> </section> <section class="section" id="efficiency-meaning"> <h2>“高效”需要拆成三件事</h2> <div class="efficiency-grid"> <article class="efficiency-item"> <strong>算法执行效率</strong> <p>Single-anchor 的可变部分只有逐元素运算与 axis reduction;完整流水线只需固定 MAGIC 的一次图 diffusion。Baseline 还包含多图、双 variant、PCA/NMF 和最终 kNN/ALRA。</p> </article> <article class="efficiency-item"> <strong>搜索效率</strong> <p>后 5 步日志中,Single-anchor 平均 prompt 约 4,002 tokens、有效率 95%;Baseline 约 10,991 tokens、有效率 45%。这支持“局部 patch 更容易生成合法候选”,但两次运行模型和资源配置不同,不能当严格因果实验。</p> </article> <article class="efficiency-item"> <strong>统计效率</strong> <p>Single-anchor 把容量集中到少数与评分直接相关的自由度:<code>beta=30</code>、<code>lam=0.15</code>、<code>raw_alpha=0.08</code>、阈值与 scaling exponent。Baseline 则把搜索预算分散在大量结构和交互参数上。</p> </article> </div> <div class="verdict" style="margin-top: 16px;"> <strong>准确结论</strong> <p> Single-anchor 的优势不是“150 行代码凭空完成了 1043 行的全部工作”。它把固定 MAGIC 当作已经完成的共享底座,再用 PFG 对 residual correction 做局部、可回收的结构搜索。短主要来自搜索表示和自动 pruning;高分主要来自问题分解后更容易找到有效的 X/Z 混合;Baseline 的长主要来自完整重写、粗粒度信用和无复杂度惩罚共同造成的模块累积。 </p> </div> </section> <section class="section"> <h2>结构与行为差异</h2> <div class="compare-table-wrap"> <table> <thead> <tr> <th>维度</th> <th class="single-text">Single-anchor final</th> <th class="baseline-text">Baseline final</th> </tr> </thead> <tbody> <tr> <td>入口语义</td> <td><code>improve_artifact(X, Z)</code>:优化已存在的 denoised estimate</td> <td><code>magic_denoise(X)</code>:从原始 counts 完整计算</td> </tr> <tr> <td>生成代码</td> <td>150 行、141 个非空行、4 个函数</td> <td>1043 行、915 个非空行、24 个函数</td> </tr> <tr> <td>完整执行口径</td> <td>固定 backbone 60 行 + active graph 150 行 + 通用验证/wrapper,共约 230 行</td> <td>全部逻辑都在生成程序内,共 1043 行</td> </tr> <tr> <td>主入口复杂度</td> <td>5 个显式参数加 <code>**kwargs</code></td> <td>39 个显式参数加 <code>**kwargs</code></td> </tr> <tr> <td>图操作</td> <td>postprocessor 不建图;只复用固定 MAGIC 的 <code>Z</code></td> <td>初始/二次 HVG 图、多 knn 图,以及最终 log-space kNN 图</td> </tr> <tr> <td>低秩模型</td> <td>没有 PCA、NMF、ALRA</td> <td>KL-NMF、多个 log-space PCA、ALRA-style refinement</td> </tr> <tr> <td>低值处理</td> <td>按 cell dropout 设置阈值,低于阈值直接归零</td> <td>小于 0.2 的值乘 0.9,属于软抑制</td> </tr> <tr> <td>gene scaling</td> <td><code>ratio**0.6</code>,限制在 <code>[0.8, 1.2]</code></td> <td>gene mean 比率限制在 <code>[0.97, 1.03]</code>;variance scaling 默认关闭</td> </tr> <tr> <td>raw mixing</td> <td>按每个 cell 的 dropout 调节,基础权重 0.08</td> <td>variant 内 cell-level 回混,再做一次 gene-level 回混</td> </tr> <tr> <td>数值类型</td> <td>后处理主要使用 <code>float64</code></td> <td>入口和最终输出使用 <code>float32</code>,中间模型混用 NumPy/sklearn 类型</td> </tr> </tbody> </table> </div> </section> <section class="section"> <h2>Single-anchor:四个函数形成一条紧凑校准链</h2> <div class="architecture"> <article class="analysis-panel"> <h3>函数构成</h3> <ul class="function-list"> <li><code>_sqrt_weighted_mix</code><span>在 sqrt 空间按 cell/gene dropout 混合 X 与 Z,并执行自适应硬阈值。</span><small>40 行</small></li> <li><code>_adaptive_shrinkage_factor</code><span>dropout 越高,越收缩回固定 backbone。</span><small>34 行</small></li> <li><code>_apply_gene_scaling_v3</code><span>按列总量做温和 power-law 校准。</span><small>22 行</small></li> <li><code>improve_artifact</code><span>组织三个 helper,最后按 cell dropout 混回少量 raw counts。</span><small>48 行</small></li> </ul> </article> <article class="analysis-panel"> <h3>实际数值路径</h3> <div class="formula">βᵢⱼ = 30 × (1 + 2·dropout_cellᵢ) × (1 + dropout_geneⱼ) γᵢⱼ = Xᵢⱼ / (Xᵢⱼ + βᵢⱼ) Mᵢⱼ = [(1 − γᵢⱼ)√Zᵢⱼ + γᵢⱼ√Xᵢⱼ]² Mᵢⱼ = 0, if Mᵢⱼ &lt; 0.12 + 0.15·dropout_cellᵢ λᵢⱼ = max( 0.15 × (0.5 + 0.5·dropout_cellᵢ), 0.15 × (0.2 + 0.8·dropout_geneⱼ) ) C = (1 − λ)M + λZ gene_factorⱼ = clip[(ΣᵢXᵢⱼ / ΣᵢCᵢⱼ)^0.6, 0.8, 1.2] output = adaptive_raw_mix(C × gene_factor, X)</div> </article> </div> </section> <section class="section"> <h2>Baseline:24 个函数覆盖完整重建</h2> <p class="section-intro"> 它不是某个单一算法,而是逐步累积出的组合系统。主要计算集中在 407 行的 <code>magic_denoise</code> 和 115 行的 <code>_denoise_variant</code>。 </p> <div class="compare-table-wrap"> <table> <thead><tr><th>模块</th><th>具体函数</th><th>作用</th></tr></thead> <tbody> <tr><td>变换与规模</td><td><code>_anscombe_transform</code>, <code>_inverse_anscombe_newton</code>, <code>_smooth_row_sums</code>, <code>_adjust_row_sums</code></td><td>VST 正反变换与 cell library-size 控制。</td></tr> <tr><td>HVG</td><td><code>_select_hvg_seurat</code>, <code>_standardize_hvg</code>, <code>_destandardize_hvg</code>, <code>_refine_hvg</code></td><td>选择、标准化并两次扩展 HVG 集合。</td></tr> <tr><td>Diffusion</td><td><code>_apply_diffusion</code>, <code>_impute_counts_std</code>, <code>_full_diffuse</code>, <code>_auto_diffusion_time</code></td><td>在多步、多权重和全矩阵路径上执行平滑。</td></tr> <tr><td>低秩模型</td><td><code>_nmf_impute</code>, <code>_full_pca_denoise_centered</code>, <code>_low_rank_refine_counts</code></td><td>KL-NMF、PCA 和 ALRA-style 重建。</td></tr> <tr><td>统计校准</td><td><code>_scale_hvg_variance</code>, <code>_gene_raw_weight_mix</code>, <code>_suppress_low_counts</code>, <code>_scale_gene_means</code>, <code>_scale_gene_variance_masked</code></td><td>variance、mean、raw mixing 和低值校正。</td></tr> <tr><td>最终修正</td><td><code>_final_log_knn_smooth_with_residual</code></td><td>在 log-normalized 空间重新建图,并对 residual diffusion。</td></tr> <tr><td>组织层</td><td><code>_denoise_variant</code>, <code>magic_denoise</code></td><td>运行两套 variant,以 0.96 / 0.04 合并,再执行多阶段 polishing。</td></tr> </tbody> </table> </div> </section> <section class="section"> <h2>从实际代码发现的冗余与迭代痕迹</h2> <div class="findings"> <article class="finding warn"> <h3>Baseline 的 <code>solver</code> 是死参数</h3> <p>主函数设置了 <code>solver = "approximate" / "exact"</code>,但后续没有读取或传给任何计算函数,改变它不会改变输出。</p> </article> <article class="finding warn"> <h3>Baseline 有三个未使用 import</h3> <p><code>anndata</code>、<code>scanpy as sc</code>、<code>sklearn.metrics</code> 只被导入,没有出现在计算路径中。</p> </article> <article class="finding warn"> <h3>“Newton inverse”大部分情况下是空迭代</h3> <p>对正常的非负 <code>y</code>,初值 <code>(y/2)^2 − 3/8</code> 已经是当前 Anscombe 变换的解析逆;后续 12 轮 residual 为零。</p> </article> <article class="finding warn"> <h3>Single-anchor 有一处过期注释</h3> <p><code>improve_artifact</code> docstring 仍写 exponent 0.7,但真实调用使用 <code>_apply_gene_scaling_v3(..., exponent=0.6)</code>。</p> </article> </div> </section> <section class="code-section" id="code"> <h2>完整代码浏览器</h2> <div class="code-guide"> <span>左右代码都是最终 best 的原文。</span> <span>搜索会标出匹配行。</span> <span>函数下拉框可直接跳转。</span> <span>左侧可在“完整执行版”和“仅学习部分”之间切换。</span> </div> <div class="code-grid"> <article class="code-panel" data-panel="single"> <header class="code-panel-header"> <div class="code-panel-title"> <h3 class="single-text">Single-anchor final</h3> <span id="single-meta">完整执行版 · 230 行</span> </div> <div class="controls"> <select id="single-source" aria-label="选择 Single-anchor 代码口径"> <option value="full">完整执行版</option> <option value="active">仅 active graph</option> </select> <input id="single-search" type="search" placeholder="搜索代码,例如 dropout" aria-label="搜索 Single-anchor 代码"> <select id="single-functions-full" class="function-select" aria-label="跳转到 Single-anchor 函数"> $singleFullFunctions </select> <select id="single-functions-active" class="function-select hidden" aria-label="跳转到 Single-anchor active graph 函数"> $singleActiveFunctions </select> </div> <div class="search-status" id="single-status" aria-live="polite">未搜索</div> </header> <div class="code-viewport" id="single-viewport"> <pre class="code-block" id="single-code-full"><code>$singleFullCode</code></pre> <pre class="code-block hidden" id="single-code-active"><code>$singleActiveCode</code></pre> </div> </article> <article class="code-panel" data-panel="baseline"> <header class="code-panel-header"> <div class="code-panel-title"> <h3 class="baseline-text">Baseline 50-step final</h3> <span>完整重写 · 1043 行</span> </div> <div class="controls"> <button type="button" id="baseline-top">回到顶部</button> <input id="baseline-search" type="search" placeholder="搜索代码,例如 _denoise_variant" aria-label="搜索 Baseline 代码"> <select id="baseline-functions" class="function-select" aria-label="跳转到 Baseline 函数"> $baselineFunctions </select> </div> <div class="search-status" id="baseline-status" aria-live="polite">未搜索</div> </header> <div class="code-viewport" id="baseline-viewport"> <pre class="code-block" id="baseline-code"><code>$baselineCode</code></pre> </div> </article> </div> </section> <footer class="footer"> 数据来源:Single-anchor sampler step 25 的最小 MSE 节点;Baseline sampler 的 timestep 49 最小 MSE 节点。代码长度不包含 Markdown code fence。 </footer> </main> <script> (() => { const singleSource = document.getElementById('single-source'); const singleMeta = document.getElementById('single-meta'); const singleFull = document.getElementById('single-code-full'); const singleActive = document.getElementById('single-code-active'); const singleFunctionsFull = document.getElementById('single-functions-full'); const singleFunctionsActive = document.getElementById('single-functions-active'); const singleViewport = document.getElementById('single-viewport'); function activeSingleBlock() { return singleSource.value === 'active' ? singleActive : singleFull; } function activeSingleFunctions() { return singleSource.value === 'active' ? singleFunctionsActive : singleFunctionsFull; } function clearTargets(block) { block.querySelectorAll('.code-line.is-target').forEach(line => line.classList.remove('is-target')); } function jumpToLine(block, viewport, lineNumber) { clearTargets(block); const line = block.querySelector('.code-line[data-line="' + lineNumber + '"]'); if (!line) return; line.classList.add('is-target'); viewport.scrollTo({ top: Math.max(0, line.offsetTop - 36), behavior: 'smooth' }); window.setTimeout(() => line.classList.remove('is-target'), 1800); } function searchBlock(input, blockGetter, viewport, status) { const block = blockGetter(); block.querySelectorAll('.code-line.is-match').forEach(line => line.classList.remove('is-match')); const query = input.value.trim().toLocaleLowerCase(); if (!query) { status.textContent = '未搜索'; return; } const matches = [...block.querySelectorAll('.code-line')].filter(line => line.querySelector('.line-source').textContent.toLocaleLowerCase().includes(query) ); matches.forEach(line => line.classList.add('is-match')); status.textContent = matches.length ? '找到 ' + matches.length + ' 行' : '没有匹配行'; if (matches.length) { viewport.scrollTo({ top: Math.max(0, matches[0].offsetTop - 36), behavior: 'smooth' }); } } singleSource.addEventListener('change', () => { const active = singleSource.value === 'active'; singleFull.classList.toggle('hidden', active); singleActive.classList.toggle('hidden', !active); singleFunctionsFull.classList.toggle('hidden', active); singleFunctionsActive.classList.toggle('hidden', !active); singleMeta.textContent = active ? '仅学习部分 · 150 行' : '完整执行版 · 230 行'; singleViewport.scrollTop = 0; document.getElementById('single-search').value = ''; document.getElementById('single-status').textContent = '未搜索'; }); document.getElementById('single-search').addEventListener('input', event => { searchBlock( event.target, activeSingleBlock, singleViewport, document.getElementById('single-status') ); }); singleFunctionsFull.addEventListener('change', event => { jumpToLine(singleFull, singleViewport, event.target.value); }); singleFunctionsActive.addEventListener('change', event => { jumpToLine(singleActive, singleViewport, event.target.value); }); const baseline = document.getElementById('baseline-code'); const baselineViewport = document.getElementById('baseline-viewport'); document.getElementById('baseline-search').addEventListener('input', event => { searchBlock( event.target, () => baseline, baselineViewport, document.getElementById('baseline-status') ); }); document.getElementById('baseline-functions').addEventListener('change', event => { jumpToLine(baseline, baselineViewport, event.target.value); }); document.getElementById('baseline-top').addEventListener('click', () => { baselineViewport.scrollTo({ top: 0, behavior: 'smooth' }); }); })(); </script> </body> </html> "@ Set-Content -LiteralPath $OutputPath -Value $html -Encoding utf8NoBOM $result = Get-Item -LiteralPath $OutputPath Write-Output "Wrote $($result.FullName)" Write-Output "Bytes: $($result.Length)"
param(
[string]$OutputPath = "C:\Users\xiaof\Documents\TTT-discover\reports\denoising_final_code_comparison.html"
)
$ErrorActionPreference = "Stop"
$reportDir = Split-Path -Parent $OutputPath
$singleFullPath = Join-Path $reportDir "denoising_single_anchor_final_best_full.py"
$singleActivePath = Join-Path $reportDir "denoising_single_anchor_final_best.py"
$baselinePath = Join-Path $reportDir "denoising_baseline50_final_best.py"
foreach ($path in @($singleFullPath, $singleActivePath, $baselinePath)) {
if (-not (Test-Path -LiteralPath $path)) {
throw "Missing source file: $path"
}
}
function Convert-CodeToHtml {
param(
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][string]$Prefix
)
$lines = Get-Content -LiteralPath $Path
$rendered = for ($index = 0; $index -lt $lines.Count; $index++) {
$number = $index + 1
$encoded = [System.Net.WebUtility]::HtmlEncode($lines[$index])
if ($encoded.Length -eq 0) {
$encoded = "&nbsp;"
}
"<span class=`"code-line`" id=`"$Prefix-line-$number`" data-line=`"$number`"><span class=`"line-number`">$number</span><span class=`"line-source`">$encoded</span></span>"
}
return ($rendered -join "`n")
}
function Convert-FunctionsToOptions {
param([Parameter(Mandatory = $true)][string]$Path)
$lines = Get-Content -LiteralPath $Path
$options = @("<option value=`"1`">文件顶部</option>")
for ($index = 0; $index -lt $lines.Count; $index++) {
if ($lines[$index] -match '^def\s+([A-Za-z_][A-Za-z0-9_]*)') {
$name = [System.Net.WebUtility]::HtmlEncode($Matches[1])
$number = $index + 1
$options += "<option value=`"$number`">$name · L$number</option>"
}
}
return ($options -join "`n")
}
$singleFullCode = Convert-CodeToHtml -Path $singleFullPath -Prefix "single-full"
$singleActiveCode = Convert-CodeToHtml -Path $singleActivePath -Prefix "single-active"
$baselineCode = Convert-CodeToHtml -Path $baselinePath -Prefix "baseline"
$singleFullFunctions = Convert-FunctionsToOptions -Path $singleFullPath
$singleActiveFunctions = Convert-FunctionsToOptions -Path $singleActivePath
$baselineFunctions = Convert-FunctionsToOptions -Path $baselinePath
$html = @"
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Denoising 最终代码对照:Single-anchor vs Baseline 50-step</title>
<style>
:root {
color-scheme: light dark;
--bg: #f5f7fb;
--surface: #ffffff;
--surface-soft: #f8fafc;
--text: #172033;
--muted: #64748b;
--border: #dbe3ee;
--single: #1769aa;
--single-soft: #e8f3fc;
--baseline: #a55716;
--baseline-soft: #fff2e5;
--good: #16734a;
--good-soft: #e8f7ef;
--warn: #9a5b08;
--warn-soft: #fff6dd;
--code-bg: #101827;
--code-text: #dce7f5;
--code-muted: #718096;
--code-hit: #5b4310;
--shadow: 0 10px 32px rgba(23, 32, 51, 0.08);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0c111b;
--surface: #131b29;
--surface-soft: #172131;
--text: #e7edf6;
--muted: #9eacc0;
--border: #2c394d;
--single: #72b7ed;
--single-soft: #152f43;
--baseline: #f0ad6f;
--baseline-soft: #3c291b;

Xet Storage Details

Size:
46.2 kB
·
Xet hash:
c3c765bf2e3c1c3290e6484bc96af415df4766833664357cc40dafcf86050b1c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.