<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>engram</title>
<link>https://bkowshik.github.io/engram/</link>
<atom:link href="https://bkowshik.github.io/engram/index.xml" rel="self" type="application/rss+xml"/>
<description>Notebooks and experiments from my work in NeuroAI and brain–computer interfaces.</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Fri, 19 Jun 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>FmriCleaner — nilearn.signal.clean, wrapped</title>
  <link>https://bkowshik.github.io/engram/neuroai/fmricleaner-walkthrough.html</link>
  <description><![CDATA[ 




<div id="0bb94dbf" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:34.445123Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:34.444891Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:35.124227Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:35.123732Z&quot;}}">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>pip install nilearn pydantic rich <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>progress<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>bar off</span></code></pre></div></div>
</div>
<div id="89016b42" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:35.126287Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:35.126124Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:35.558043Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:35.557609Z&quot;}}" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb2-2">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb2-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pydantic</span>
<span id="cb2-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Literal</span>
<span id="cb2-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>config InlineBackend.figure_format <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"retina"</span></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rich <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pretty</span>
<span id="cb2-11">pretty.install()</span></code></pre></div></div>
</div>
<p><a href="https://github.com/facebookresearch/neuroai"><code>neuralset</code></a> wraps <code>nilearn.signal.clean</code> in a tiny pydantic config object called <code>FmriCleaner</code>. If the five knobs (<code>detrend</code>, <code>standardize</code>, <code>high_pass</code> / <code>low_pass</code>, <code>ensure_finite</code>) are new to you, read <a href="../nilearn-nilearn/signal-clean.ipynb"><strong><code>signal-clean</code></strong></a> first — it builds the intuition from a synthetic signal. This notebook assumes that and focuses on what the <em>wrapper</em> adds:</p>
<ul>
<li>it’s a <strong>validated config object</strong>, not a bare function call;</li>
<li>it flips the data contract to <strong><code>(n_features, time)</code></strong> (nilearn wants <code>(time, n_features)</code>);</li>
<li>and it guards the <code>clean()</code> call in a way that — per <a href="https://github.com/facebookresearch/neuroai/issues/130"><strong>neuroai#130</strong></a> — silently drops <code>ensure_finite</code> when the signal-processing knobs are off.</li>
</ul>
<p>We reproduce the class inline so the guard is visible and the notebook runs without the full (torch-heavy) <code>neuralset</code> install.</p>
<div id="01e2bc9a" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:35.559401Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:35.559265Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:35.593669Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:35.593225Z&quot;}}" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reproduced verbatim from neuralset.extractors.neuro.FmriCleaner (neuralset 0.2.2)</span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># so this notebook is self-contained. Source: github.com/facebookresearch/neuroai</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> FmriCleaner(pydantic.BaseModel):</span>
<span id="cb3-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Configuration for fMRI signal cleaning."""</span></span>
<span id="cb3-6"></span>
<span id="cb3-7">    model_config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pydantic.ConfigDict(extra<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forbid"</span>)</span>
<span id="cb3-8">    standardize: Literal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zscore_sample"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zscore"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psc"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zscore_sample"</span></span>
<span id="cb3-9">    detrend: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb3-10">    high_pass: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb3-11">    low_pass: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb3-12">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>: Literal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"butterworth"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cosine"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb3-13">    ensure_finite: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb3-14"></span>
<span id="cb3-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> clean(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, data: np.ndarray, t_r: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> np.ndarray:</span>
<span id="cb3-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (</span>
<span id="cb3-17">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.detrend</span>
<span id="cb3-18">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.standardize</span>
<span id="cb3-19">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.high_pass <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)</span>
<span id="cb3-20">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.low_pass <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)</span>
<span id="cb3-21">        ):</span>
<span id="cb3-22">            <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> nilearn.signal</span>
<span id="cb3-23"></span>
<span id="cb3-24">            data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.T  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set time as first dim</span></span>
<span id="cb3-25">            shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.shape</span>
<span id="cb3-26">            data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nilearn.signal.clean(</span>
<span id="cb3-27">                data.reshape(shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb3-28">                t_r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t_r,</span>
<span id="cb3-29">                standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.standardize,</span>
<span id="cb3-30">                high_pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.high_pass,</span>
<span id="cb3-31">                low_pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.low_pass,</span>
<span id="cb3-32">                <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>,</span>
<span id="cb3-33">                detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.detrend,</span>
<span id="cb3-34">                ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.ensure_finite,</span>
<span id="cb3-35">            )</span>
<span id="cb3-36">            data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.reshape(shape).T</span>
<span id="cb3-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data</span></code></pre></div></div>
</div>
<section id="a-config-object-not-a-function" class="level2">
<h2 class="anchored" data-anchor-id="a-config-object-not-a-function">A config object, not a function</h2>
<p><code>FmriCleaner</code> is a <code>pydantic.BaseModel</code>: every field is typed and validated, defaults are declared once, and <code>extra="forbid"</code> means a misspelled option is rejected at construction instead of silently ignored. This is the pattern <code>neuralset</code> uses for all its configs.</p>
<div id="dac08dfb" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:35.594845Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:35.594768Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:35.598125Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:35.597649Z&quot;}}" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fields &amp; defaults:"</span>)</span>
<span id="cb4-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, field <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> FmriCleaner.model_fields.items():</span>
<span id="cb4-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&lt;14}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> default=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>field<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>default<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!r}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">default config:"</span>, FmriCleaner().model_dump())</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extra="forbid" -&gt; a typo is a hard error, not a silent no-op</span></span>
<span id="cb4-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb4-9">    FmriCleaner(detrendd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># note the typo</span></span>
<span id="cb4-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> pydantic.ValidationError <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb4-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rejected unknown field 'detrendd':"</span>, e.errors()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>fields &amp; defaults:
  standardize    default='zscore_sample'
  detrend        default=True
  high_pass      default=None
  low_pass       default=None
  filter         default=None
  ensure_finite  default=True

default config: {'standardize': 'zscore_sample', 'detrend': True, 'high_pass': None, 'low_pass': None, 'filter': None, 'ensure_finite': True}

rejected unknown field 'detrendd': extra_forbidden</code></pre>
</div>
</div>
</section>
<section id="the-data-contract-n_features-time" class="level2">
<h2 class="anchored" data-anchor-id="the-data-contract-n_features-time">The data contract: <code>(n_features, time)</code></h2>
<p><code>nilearn.signal.clean</code> expects <strong>time first</strong> — <code>(time, n_features)</code>. <code>FmriCleaner.clean(data, t_r)</code> flips that: it takes <strong><code>(n_features, time)</code></strong> (the layout the rest of <code>neuralset</code> uses), transposes internally, cleans, then transposes back — so shape in equals shape out, for any number of feature dimensions. <code>t_r</code> is the repetition time in seconds (<code>1 / frequency</code>).</p>
<p>We build the same synthetic voxel as the <code>signal-clean</code> notebook — <code>baseline + drift + slow wave + fast noise</code> — and plant one <code>NaN</code>, because non-finite values are normal in fMRI (masks, registration, surface projection all leave them behind).</p>
<div id="4ceae731" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:35.599177Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:35.599086Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:55.143481Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:55.142977Z&quot;}}" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">t_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># seconds per sample (a typical fMRI TR)</span></span>
<span id="cb6-2">n   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb6-3">t   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t_r</span>
<span id="cb6-4"></span>
<span id="cb6-5">truth  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.sin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.pi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.sin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.pi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t)</span>
<span id="cb6-6">signal <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> truth.copy().reshape(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).astype(np.float32)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (n_features=1, time)</span></span>
<span id="cb6-7">signal[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.nan                                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one planted NaN</span></span>
<span id="cb6-8"></span>
<span id="cb6-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"signal shape (features, time):"</span>, signal.shape, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" finite:"</span>, np.isfinite(signal).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>())</span>
<span id="cb6-10"></span>
<span id="cb6-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># shape is preserved for any number of feature dims:</span></span>
<span id="cb6-12">multi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.randn(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, n).astype(np.float32)</span>
<span id="cb6-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multi-voxel in:"</span>, multi.shape, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-&gt; out:"</span>, FmriCleaner().clean(multi, t_r).shape)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>signal shape (features, time): (1, 200)  finite: False
multi-voxel in: (5, 200) -&gt; out: (5, 200)</code></pre>
</div>
</div>
</section>
<section id="the-guard-and-the-bug-130" class="level2">
<h2 class="anchored" data-anchor-id="the-guard-and-the-bug-130">The guard — and the bug (#130)</h2>
<p>The whole issue lives in <code>clean()</code>’s opening <code>if</code>: it calls <code>nilearn.signal.clean</code> <strong>only</strong> when a signal-processing knob (<code>detrend</code> / <code>standardize</code> / a filter) is on. But <code>ensure_finite</code> is forwarded <em>inside</em> that call — so when every signal-processing knob is off, the call is skipped and <code>ensure_finite</code> goes with it, <strong>even when you set it to <code>True</code></strong>.</p>
<p>Watch the same planted <code>NaN</code> (sample 50) under three configs:</p>
<div id="df2f2943" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:55.146955Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:55.146684Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:55.151323Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:55.150627Z&quot;}}" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> describe(name, x):</span>
<span id="cb8-2">    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.asarray(x).ravel()</span>
<span id="cb8-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&lt;48}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> finite=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(np.isfinite(x).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&lt;5}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> sample50=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:8.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb8-4"></span>
<span id="cb8-5">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"default (detrend+zscore)"</span>, FmriCleaner().clean(signal.copy(), t_r))</span>
<span id="cb8-6">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"detrend=F, standardize=F"</span>, FmriCleaner(detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>).clean(signal.copy(), t_r))</span>
<span id="cb8-7">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"detrend=F, standardize=F, ensure_finite=True"</span>, FmriCleaner(detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).clean(signal.copy(), t_r))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>default (detrend+zscore)                         finite=True  sample50= -13.451
detrend=F, standardize=F                         finite=False sample50=     nan
detrend=F, standardize=F, ensure_finite=True     finite=False sample50=     nan</code></pre>
</div>
</div>
<div id="e90207b2" class="cell" data-quarto-private-1="{&quot;key&quot;:&quot;execution&quot;,&quot;value&quot;:{&quot;iopub.execute_input&quot;:&quot;2026-06-19T04:43:55.152553Z&quot;,&quot;iopub.status.busy&quot;:&quot;2026-06-19T04:43:55.152461Z&quot;,&quot;iopub.status.idle&quot;:&quot;2026-06-19T04:43:55.441294Z&quot;,&quot;shell.execute_reply&quot;:&quot;2026-06-19T04:43:55.440809Z&quot;}}" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">default_out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FmriCleaner().clean(signal.copy(), t_r)</span>
<span id="cb10-2">leaked_out  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FmriCleaner(detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).clean(signal.copy(), t_r)</span>
<span id="cb10-3"></span>
<span id="cb10-4">fig, (ax1, ax2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb10-5">ax1.plot(t, default_out.ravel())</span>
<span id="cb10-6">ax1.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"default — NaN scrubbed (finite)"</span>)</span>
<span id="cb10-7">ax1.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> ax1.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> ax1.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb10-8"></span>
<span id="cb10-9">ax2.plot(t, leaked_out.ravel())</span>
<span id="cb10-10">ax2.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ensure_finite=True ignored — gap at the NaN"</span>)</span>
<span id="cb10-11">ax2.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> ax2.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> ax2.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb10-12"></span>
<span id="cb10-13">plt.tight_layout()</span>
<span id="cb10-14">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bkowshik.github.io/engram/neuroai/fmricleaner-walkthrough_files/figure-html/cell-8-output-2.png" width="1189" height="389" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="the-takeaway" class="level2">
<h2 class="anchored" data-anchor-id="the-takeaway">The takeaway</h2>
<p><code>ensure_finite</code> is <strong>data hygiene</strong> — a different category from the signal-processing knobs (see <a href="../nilearn-nilearn/signal-clean.ipynb"><code>signal-clean</code></a> for why). The wrapper accidentally couples the two: by nesting <code>ensure_finite</code> inside a guard that only fires for <code>detrend</code> / <code>standardize</code> / filters, it turns “raw signal, just keep it finite” (<code>detrend=False, standardize=False, ensure_finite=True</code>) into “raw signal, <code>NaN</code>s and all” — and those <code>NaN</code>s detonate the moment a downstream model or fit touches them.</p>
<p>The fix is to honour <code>ensure_finite</code> independently of the other knobs — either by adding it to the guard, or by handling finite-replacement explicitly when the guard is otherwise false. That design choice is the next step.</p>


</section>

 ]]></description>
  <category>fmri</category>
  <category>nilearn</category>
  <category>neuralset</category>
  <category>neuroai</category>
  <guid>https://bkowshik.github.io/engram/neuroai/fmricleaner-walkthrough.html</guid>
  <pubDate>Fri, 19 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>nilearn.signal.clean — the five knobs</title>
  <link>https://bkowshik.github.io/engram/nilearn/signal-clean.html</link>
  <description><![CDATA[ 




<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://colab.research.google.com/github/bkowshik/engram/blob/main/repositories/nilearn-nilearn/signal-clean.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" class="img-fluid figure-img"></a></p>
<figcaption>Open In Colab</figcaption>
</figure>
</div>
<div id="098a4781" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>pip install <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>upgrade nilearn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>progress<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>bar off</span></code></pre></div></div>
</div>
<div id="a7a8c902" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb2-2">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb2-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> nilearn.signal</span>
<span id="cb2-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>config InlineBackend.figure_format <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'retina'</span></span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rich <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pretty</span>
<span id="cb2-10">pretty.install()</span></code></pre></div></div>
</div>
<p><code>nilearn.signal.clean(signals, ...)</code> is the workhorse fMRI preprocessing call. It bundles five operations behind one function, and they split into <strong>two categories</strong>:| Category | Knob | What it does || —————– | ———————— | ——————————- || Signal processing | <code>detrend</code> | remove slow drift || Signal processing | <code>standardize</code> | rescale to mean 0, std 1 || Signal processing | <code>high_pass</code> / <code>low_pass</code> | temporal frequency filters || <strong>Data hygiene</strong> | <code>ensure_finite</code> | replace <code>NaN</code> / <code>±Inf</code> with <code>0</code> |The way to build intuition: construct a synthetic signal out of <strong>known</strong> parts, plant one bad value, then turn a single knob at a time and watch what each one removes.This notebook grew out of <a href="https://github.com/facebookresearch/neuroai/issues/130">facebookresearch/neuroai#130</a> — a wrapper that skipped <code>clean()</code> entirely when the signal-processing knobs were off, silently disabling <code>ensure_finite</code> along with them. The cells below are <em>why that matters</em>.</p>
<div id="64c6d564" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># nilearn.signal.clean expects shape (time, n_features). One feature (one voxel).</span></span>
<span id="cb3-2">t_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># repetition time: 2 s per sample (a typical fMRI TR)</span></span>
<span id="cb3-3">n   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb3-4">t   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t_r       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># time axis in seconds</span></span>
<span id="cb3-5"></span>
<span id="cb3-6">baseline   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># BOLD has a big DC offset</span></span>
<span id="cb3-7">drift      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># slow linear drift (scanner warm-up etc.)</span></span>
<span id="cb3-8">slow_signal<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.pi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the "neural" signal we want to KEEP (0.02 Hz)</span></span>
<span id="cb3-9">fast_noise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.pi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># high-frequency nuisance (0.20 Hz)</span></span></code></pre></div></div>
</div>
<div id="59b01a71" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">clean_truth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> baseline <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> drift <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> slow_signal <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> fast_noise</span>
<span id="cb4-2">signal <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> clean_truth.copy().reshape(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (time, 1)</span></span>
<span id="cb4-3">signal[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.nan                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop in one non-finite value</span></span></code></pre></div></div>
</div>
<section id="the-signal-and-the-planted-nan" class="level2">
<h2 class="anchored" data-anchor-id="the-signal-and-the-planted-nan">The signal and the planted <code>NaN</code></h2>
<p>Our synthetic voxel is <code>baseline (100) + linear drift + slow 0.02 Hz wave + fast 0.20 Hz noise</code>. The slow wave is the “neural” signal we want to keep; drift and fast noise are the nuisances.</p>
<p>We then set one sample to <code>NaN</code>. That isn’t artificial paranoia — non-finite values are <strong>normal</strong> in fMRI: brain masks, registration/resampling, and surface projection all leave <code>NaN</code> where there is no valid data. Downstream models choke on a single <code>NaN</code>, so scrubbing it is routine hygiene, not an edge case.</p>
<div id="97cff454" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> describe(name, x):</span>
<span id="cb5-2">    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.asarray(x).ravel()</span>
<span id="cb5-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&lt;28}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> mean=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>nanmean(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:7.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  std=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>nanstd(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:6.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  "</span></span>
<span id="cb5-4">          <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"finite=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>np<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>isfinite(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb5-5"></span>
<span id="cb5-6">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raw input"</span>, signal)</span>
<span id="cb5-7"></span>
<span id="cb5-8">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raw"</span>)</span>
<span id="cb5-9">plt.plot(t, signal)</span>
<span id="cb5-10">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)</span>
<span id="cb5-11">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb5-12">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb5-13">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>raw input                    mean=109.975  std= 5.999  finite=False</code></pre>
</div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bkowshik.github.io/engram/nilearn/signal-clean_files/figure-html/cell-6-output-3.png" width="571" height="454" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="knob-1-ensure_finite-data-hygiene" class="level2">
<h2 class="anchored" data-anchor-id="knob-1-ensure_finite-data-hygiene">Knob 1 — <code>ensure_finite</code>: data hygiene</h2>
<p>The odd one out: it’s about data <em>validity</em>, not signal <em>shape</em>. It replaces <code>NaN</code> / <code>+Inf</code> / <code>-Inf</code> with <code>0</code> and touches nothing else — watch sample 50 go from <code>NaN</code> → <code>0</code>.</p>
<p><strong>Note:</strong> in <code>nilearn</code>, <code>ensure_finite</code> <strong>defaults to <code>False</code></strong> — you have to ask for it explicitly.</p>
<div id="a7fba116" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">only_finite <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nilearn.signal.clean(</span>
<span id="cb7-2">    signal,</span>
<span id="cb7-3">    detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb7-4">    standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb7-5">    ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb7-6">)</span>
<span id="cb7-7">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ensure_finite only"</span>, only_finite)</span>
<span id="cb7-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  -&gt; sample 50 was NaN, now:"</span>, only_finite[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span>
<span id="cb7-9"></span>
<span id="cb7-10">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Only Finite"</span>)</span>
<span id="cb7-11">plt.plot(t, only_finite)</span>
<span id="cb7-12">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)</span>
<span id="cb7-13">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb7-14">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb7-15">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>ensure_finite only           mean=109.425  std= 9.797  finite=True
  -&gt; sample 50 was NaN, now: 0.0</code></pre>
</div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bkowshik.github.io/engram/nilearn/signal-clean_files/figure-html/cell-7-output-3.png" width="571" height="454" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="knob-2-detrend-removes-drift-and-amplifies-a-stray-nan" class="level2">
<h2 class="anchored" data-anchor-id="knob-2-detrend-removes-drift-and-amplifies-a-stray-nan">Knob 2 — <code>detrend</code>: removes drift, and amplifies a stray <code>NaN</code></h2>
<p><code>detrend</code> fits one straight line across the <strong>whole</strong> time series and subtracts it. So if you <em>don’t</em> set <code>ensure_finite=True</code>, the planted <code>NaN</code> poisons that least-squares fit and <strong>every</strong> sample comes back <code>NaN</code> — one bad value contaminates all 200. Try it: drop <code>ensure_finite=True</code> and re-run.</p>
<p>Lesson: a single non-finite value does not stay local once a real operation touches it. With <code>ensure_finite=True</code> (below), the drift is cleanly removed and the signal recenters near 0.</p>
<div id="fe410ace" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">detrended <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nilearn.signal.clean(</span>
<span id="cb9-2">    signal,</span>
<span id="cb9-3">    detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb9-4">    standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb9-5">    ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb9-6">)</span>
<span id="cb9-7">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"detrend"</span>, detrended)</span>
<span id="cb9-8"></span>
<span id="cb9-9">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Detrended"</span>)</span>
<span id="cb9-10">plt.plot(t, detrended)</span>
<span id="cb9-11">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)</span>
<span id="cb9-12">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb9-13">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb9-14">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>detrend                      mean=  0.000  std= 7.732  finite=True</code></pre>
</div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bkowshik.github.io/engram/nilearn/signal-clean_files/figure-html/cell-8-output-3.png" width="583" height="454" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="knob-3-standardize-rescales-doesnt-reshape" class="level2">
<h2 class="anchored" data-anchor-id="knob-3-standardize-rescales-doesnt-reshape">Knob 3 — <code>standardize</code>: rescales, doesn’t reshape</h2>
<p><code>standardize</code> is an <strong>affine</strong> transform: <code>z = (x − mean) / std</code>. Subtracting and dividing by constants changes the <strong>y-axis scale</strong>, not the <strong>shape</strong> — so plotted on its own it looks like nothing happened. The proof is in <code>describe</code>: <code>mean</code> goes <code>~100 → 0</code> and <code>std → ~1</code>. To <em>see</em> it, overlay raw vs standardized on twin y-axes: identical curves, different scales.</p>
<p>The spike at t≈100 is the <code>NaN→0</code> value, now ~11 std below the baseline — a reminder that <code>0</code>-fill is only “neutral” once the data is mean-centered.</p>
<div id="a289dfee" class="cell" data-execution_count="8">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># standardize=True/False is deprecated in recent nilearn; "zscore_sample" is the modern spelling.</span></span>
<span id="cb11-2">standardized <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nilearn.signal.clean(</span>
<span id="cb11-3">    signal,</span>
<span id="cb11-4">    detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb11-5">    ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb11-6">    standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zscore_sample"</span></span>
<span id="cb11-7">)</span>
<span id="cb11-8">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"standardize"</span>, standardized)</span>
<span id="cb11-9"></span>
<span id="cb11-10">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Standardized"</span>)</span>
<span id="cb11-11">plt.plot(t, standardized)</span>
<span id="cb11-12">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)</span>
<span id="cb11-13">plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb11-14">plt.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb11-15">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>standardize                  mean=  0.000  std= 0.997  finite=True</code></pre>
</div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bkowshik.github.io/engram/nilearn/signal-clean_files/figure-html/cell-9-output-3.png" width="574" height="454" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="knob-4-high_pass-low_pass-temporal-filters" class="level2">
<h2 class="anchored" data-anchor-id="knob-4-high_pass-low_pass-temporal-filters">Knob 4 — <code>high_pass</code> / <code>low_pass</code>: temporal filters</h2>
<p>Mirror images. <strong>High-pass</strong> keeps fast content and strips the slow drift/baseline, so the signal recenters on 0. <strong>Low-pass</strong> keeps the slow wave and smooths away the fast noise, leaving the baseline intact.</p>
<p>Watch the <code>NaN→0</code> sample behave like an <strong>impulse</strong>: high-pass exposes it as a spike to ≈ −100 (the ~100 baseline got subtracted), while low-pass smears it into a dip with <strong>ringing</strong> on either side. Same theme as detrend, now in the frequency domain — one bad sample bleeds into its neighbours.</p>
<div id="b3e35b48" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">fig, (ax1, ax2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb13-2"></span>
<span id="cb13-3">high_passed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nilearn.signal.clean(</span>
<span id="cb13-4">    signal,</span>
<span id="cb13-5">    t_r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t_r,                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sampling interval — required for filtering</span></span>
<span id="cb13-6">    detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb13-7">    standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb13-8">    ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb13-9">    high_pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb13-10">)</span>
<span id="cb13-11">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"high_pass 0.01 Hz"</span>, high_passed)</span>
<span id="cb13-12"></span>
<span id="cb13-13">ax1.plot(t, high_passed)</span>
<span id="cb13-14">ax1.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High Passed"</span>)</span>
<span id="cb13-15">ax1.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)</span>
<span id="cb13-16">ax1.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb13-17">ax1.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb13-18"></span>
<span id="cb13-19"></span>
<span id="cb13-20">low_passed  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nilearn.signal.clean(</span>
<span id="cb13-21">    signal,</span>
<span id="cb13-22">    t_r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t_r,</span>
<span id="cb13-23">    detrend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb13-24">    standardize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb13-25">    ensure_finite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb13-26">    low_pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span></span>
<span id="cb13-27">)</span>
<span id="cb13-28">describe(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"low_pass 0.10 Hz"</span>,  low_passed)</span>
<span id="cb13-29"></span>
<span id="cb13-30">ax2.plot(t, low_passed)</span>
<span id="cb13-31">ax2.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low Passed"</span>)</span>
<span id="cb13-32">ax2.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)</span>
<span id="cb13-33">ax2.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb13-34">ax2.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb13-35"></span>
<span id="cb13-36">plt.tight_layout()</span>
<span id="cb13-37">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>high_pass 0.01 Hz            mean=  0.026  std= 7.590  finite=True
low_pass 0.10 Hz             mean=109.422  std= 7.798  finite=True</code></pre>
</div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bkowshik.github.io/engram/nilearn/signal-clean_files/figure-html/cell-10-output-3.png" width="1190" height="388" class="figure-img"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>fmri</category>
  <category>nilearn</category>
  <category>preprocessing</category>
  <guid>https://bkowshik.github.io/engram/nilearn/signal-clean.html</guid>
  <pubDate>Fri, 19 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>engram Cookbook</title>
  <link>https://bkowshik.github.io/engram/cookbook.html</link>
  <description><![CDATA[ 




<section id="how-to-use-this-cookbook" class="level1">
<h1>How to use this cookbook</h1>
<p>This is the <strong>living handbook for engram</strong> - a reference I keep coming back to and extend as I learn. It covers two things:</p>
<ul>
<li><strong>How the repo works</strong> - structure, conventions, and the render/publish workflow.</li>
<li><strong>How to author a notebook</strong> - the Quarto features worth using.</li>
</ul>
<p>For every visual feature in Part 2 you get two blocks: a <strong>Source</strong> block (the markup that produces it) and a <strong>Renders as</strong> block (what Quarto produces from it). Render locally with <code>quarto preview</code> to see the rendered halves live.</p>
<p>When you adopt something new, add a section and append a line to the Update log.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>A <code>.ipynb</code> carries Quarto front matter in a <strong>raw cell</strong> at the very top (the cell above this one). In a <code>.qmd</code> file the same YAML sits between <code>---</code> fences.</p>
</div>
</div>
</section>
<section id="part-1---the-repo" class="level1">
<h1>Part 1 - The repo</h1>
<section id="adding-a-new-notebook" class="level2">
<h2 class="anchored" data-anchor-id="adding-a-new-notebook">Adding a new notebook</h2>
<ol type="1">
<li>Drop the <code>.ipynb</code> into the relevant project folder (or create a new folder).</li>
<li>Add a front-matter raw cell at the top (see the starter).</li>
<li>Add a Colab badge as the first markdown cell.</li>
<li>Cite any papers with <code>@key</code> and add the entries to <code>references.bib</code>.</li>
<li>Run the notebook and save it <strong>with its outputs</strong>, then commit and push.</li>
</ol>
<p>The notebook then appears automatically on the homepage listing - no manual index edit.</p>
</section>
<section id="render-publish-workflow" class="level2">
<h2 class="anchored" data-anchor-id="render-publish-workflow">Render &amp; publish workflow</h2>
<p>The build <strong>never executes notebooks</strong> — it renders the outputs already saved in each <code>.ipynb</code> (<code>execute: enabled: false</code> in <code>_quarto.yml</code>). The runner needs no Python, no dependencies, and never downloads data.</p>
<p>So the rule is: <strong>run a notebook (locally or in Colab), save it with its outputs, then commit.</strong> A notebook committed without outputs renders with no figures.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">quarto</span> preview        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># live preview while editing (renders saved outputs)</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> add . <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> commit <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Add notebook"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> push</span></code></pre></div></div>
<p>Pushing to <code>main</code> triggers the Action, which publishes to the <code>gh-pages</code> branch -&gt; GitHub Pages at <code>bkowshik.github.io/engram</code>.</p>
</section>
</section>
<section id="part-2---authoring-with-quarto" class="level1">
<h1>Part 2 - Authoring with Quarto</h1>
<p>Each section below pairs the <strong>Source</strong> (what you write) with <strong>Renders as</strong> (what Quarto produces).</p>
<section id="front-matter-metadata" class="level2">
<h2 class="anchored" data-anchor-id="front-matter-metadata">Front matter &amp; metadata</h2>
<p>Every notebook opens with a raw cell of YAML. This drives the title, the listing entry, categories, and per-document options. (The “rendered” effect here is the page’s title block and listing entry, not inline output.)</p>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb2-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">title</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Short, specific title'</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">description</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'One line - shown in the listing and the social card.'</span></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2026-06-16'</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date-modified</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> last-modified</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # auto-updates from file mtime - good for living docs</span></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">categories</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eeg</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> pytorch</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> bci</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # power the tag filter + listing</span></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bibliography</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> references.bib</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # enables @citations</span></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toc</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">code-tools</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # adds the "view source / copy" menu</span></span>
<span id="cb2-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> thumbnail.png</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # optional - used in the social preview card</span></span>
<span id="cb2-11"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
</section>
<section id="callouts" class="level2">
<h2 class="anchored" data-anchor-id="callouts">Callouts</h2>
<p>Coloured boxes for asides, insights, and gotchas. Flavours: <code>note</code>, <code>tip</code>, <code>warning</code>, <code>important</code>, <code>caution</code>.</p>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb3-1">::: {.callout-tip}</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Key insight</span></span>
<span id="cb3-4"></span>
<span id="cb3-5">The takeaway in one or two sentences.</span>
<span id="cb3-6">:::</span>
<span id="cb3-7"></span>
<span id="cb3-8">::: {.callout-warning collapse="true"}</span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Gotcha (collapsed by default)</span></span>
<span id="cb3-11"></span>
<span id="cb3-12">Add <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`collapse="true"`</span> to fold long asides.</span>
<span id="cb3-13">:::</span></code></pre></div></div>
<p><strong>Renders as</strong></p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Key insight
</div>
</div>
<div class="callout-body-container callout-body">
<p>The takeaway in one or two sentences.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Gotcha (collapsed by default)
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Add <code>collapse="true"</code> to fold long asides.</p>
</div>
</div>
</div>
</section>
<section id="math" class="level2">
<h2 class="anchored" data-anchor-id="math">Math</h2>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">Inline:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> $h_t = \sigma(W x_t + b)$</span></span>
<span id="cb4-2"></span>
<span id="cb4-3">$$</span>
<span id="cb4-4">\mathcal{L} = \frac{1}{N}\sum_{i=1}^{N} -\,y_i \log \hat{y}_i</span>
<span id="cb4-5">$$ {#eq-nll}</span>
<span id="cb4-6"></span>
<span id="cb4-7">Reference it with <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`@eq-nll`</span>.</span>
<span id="cb4-8">$$</span></code></pre></div></div>
<p><strong>Renders as</strong></p>
<p>Inline: <img src="https://latex.codecogs.com/png.latex?h_t%20=%20%5Csigma(W%20x_t%20+%20b)"></p>
<p><span id="eq-nll"><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BL%7D%20=%20%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi=1%7D%5E%7BN%7D%20-%5C,y_i%20%5Clog%20%5Chat%7By%7D_i%0A%5Ctag%7B1%7D"></span></p>
<p>The negative log-likelihood in Equation&nbsp;1 is the loss the Shallow ConvNet trains with. $$</p>
</section>
<section id="citations-bibliography" class="level2">
<h2 class="anchored" data-anchor-id="citations-bibliography">Citations &amp; bibliography</h2>
<p>With <code>bibliography: references.bib</code> in the front matter, cite inline and Quarto builds the reference list automatically.</p>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb5-1">The Shallow ConvNet comes from @schirrmeister2017.</span>
<span id="cb5-2">A parenthetical citation looks like <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">@schirrmeister2017</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>.</span></code></pre></div></div>
<p>…resolving against this <code>.bib</code> entry:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bibtex code-with-copy"><code class="sourceCode bibtex"><span id="cb6-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">@article</span>{<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">schirrmeister2017</span>,</span>
<span id="cb6-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">title</span>   = {Deep learning with convolutional neural networks for EEG decoding and visualization},</span>
<span id="cb6-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">author</span>  = {Schirrmeister, Robin Tibor and others},</span>
<span id="cb6-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">journal</span> = {Human Brain Mapping}, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">year</span> = {2017}, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">volume</span> = {38}, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">pages</span> = {5391--5420}</span>
<span id="cb6-5">}</span></code></pre></div></div>
<p><strong>Renders as</strong></p>
<p>The Shallow ConvNet comes from <span class="citation" data-cites="schirrmeister2017">Schirrmeister et al. (2017)</span>. A parenthetical citation looks like <span class="citation" data-cites="schirrmeister2017">(Schirrmeister et al. 2017)</span>. (The full reference appears in the auto-generated list at the end of the page.)</p>
</section>
<section id="diagrams-mermaid-graphviz" class="level2">
<h2 class="anchored" data-anchor-id="diagrams-mermaid-graphviz">Diagrams (Mermaid &amp; Graphviz)</h2>
<p>Diagram-as-code - perfect for an architecture sketch instead of an ASCII block.</p>
<p><strong>Source</strong> (4 outer backticks so the inner block shows literally)</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb7-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{mermaid}</span></span>
<span id="cb7-2">flowchart LR</span>
<span id="cb7-3">  X[<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">EEG (22 ch x 1125)</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>] --&gt; T[<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Temporal conv</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>]</span>
<span id="cb7-4">  T --&gt; S[<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Spatial conv</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>] --&gt; P[<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Square -&gt; pool -&gt; log</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>] --&gt; C[<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Classifier</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>]</span>
<span id="cb7-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<p><strong>Renders as</strong></p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
  X["EEG (22 ch x 1125)"] --&gt; T["Temporal conv"]
  T --&gt; S["Spatial conv"] --&gt; P["Square -&gt; pool -&gt; log"] --&gt; C["Classifier"]
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
</section>
<section id="code-cells-source-output-together" class="level2">
<h2 class="anchored" data-anchor-id="code-cells-source-output-together">Code cells: source + output together</h2>
<p>A code cell is the clearest case: by default Quarto shows <strong>both</strong> the source and the output it produces. The cell below is labelled and captioned so the figure can be cross-referenced - and its code stays visible above the plot.</p>
<p>Set <code>%config InlineBackend.figure_format = "retina"</code> once near the top so the saved plots are crisp on hi-DPI screens.</p>
<div id="cell-fig-signal" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb8-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb8-3"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>config InlineBackend.figure_format <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"retina"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># crisp hi-DPI plots</span></span>
<span id="cb8-4"></span>
<span id="cb8-5">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb8-6">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.pi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t)</span>
<span id="cb8-7"></span>
<span id="cb8-8">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>))</span>
<span id="cb8-9">plt.plot(t, x)</span>
<span id="cb8-10">plt.xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (s)"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> plt.ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amplitude"</span>)</span>
<span id="cb8-11">plt.tight_layout()</span>
<span id="cb8-12">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="fig-signal" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-signal-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://bkowshik.github.io/engram/cookbook_files/figure-html/fig-signal-output-1.png" width="589" height="229" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-signal-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: A 10 Hz sine wave - a stand-in for a band-limited neural signal.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Figure&nbsp;1 shows the rendered output above, directly under the code that produced it. Control what each cell reveals with cell options:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 6%">
<col style="width: 23%">
<col style="width: 69%">
</colgroup>
<thead>
<tr class="header">
<th>Option</th>
<th>Effect</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>#     | echo: false</code></td>
<td>hide the code, keep the output</td>
<td></td>
</tr>
<tr class="even">
<td><code>#     | output: false</code></td>
<td>run the code, hide the output</td>
<td></td>
</tr>
<tr class="odd">
<td><code>#     | code-fold: true</code></td>
<td>keep the code but collapse it behind a toggle</td>
<td></td>
</tr>
<tr class="even">
<td><code>#     | label: fig-x</code>/<code>tbl-x</code></td>
<td>make it cross-referenceable (the <code>fig-</code>/<code>tbl-</code> prefix matters)</td>
<td></td>
</tr>
<tr class="odd">
<td><code>#     | fig-cap: "..."</code></td>
<td>figure caption</td>
<td></td>
</tr>
<tr class="even">
<td><code>#     | warning: false</code></td>
<td>suppress warnings in the render</td>
<td></td>
</tr>
</tbody>
</table>
<p>The default (<code>echo: true</code>) is exactly the “show both” behaviour - use the options above only when you deliberately want to hide one half.</p>
</section>
<section id="cross-references" class="level2">
<h2 class="anchored" data-anchor-id="cross-references">Cross-references</h2>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb9-1">See @fig-signal and @eq-nll. Sections work too: tag a heading <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`{#sec-setup}`</span>, then <span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">`@sec-setup`</span>.</span></code></pre></div></div>
<p><strong>Renders as</strong></p>
<p>See Figure&nbsp;1 and Equation&nbsp;1. Quarto numbers and links them automatically, so references never go stale when you reorder.</p>
</section>
<section id="tabsets" class="level2">
<h2 class="anchored" data-anchor-id="tabsets">Tabsets</h2>
<p>Stack alternatives behind tabs - handy for “same idea, two frameworks”.</p>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb10-1">::: {.panel-tabset}</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## NumPy</span></span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```python</span></span>
<span id="cb10-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb10-7">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1125</span>))</span>
<span id="cb10-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb10-9"></span>
<span id="cb10-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## PyTorch</span></span>
<span id="cb10-11"></span>
<span id="cb10-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```python</span></span>
<span id="cb10-13"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb10-14">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.zeros(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1125</span>)</span>
<span id="cb10-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb10-16"></span>
<span id="cb10-17">:::</span></code></pre></div></div>
<p><strong>Renders as</strong></p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">NumPy</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">PyTorch</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb11-2">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1125</span>))</span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb12-2">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.zeros(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1125</span>)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="figures-captions-lightbox" class="level2">
<h2 class="anchored" data-anchor-id="figures-captions-lightbox">Figures, captions &amp; lightbox</h2>
<p><strong>Source</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb13-1"><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">![Shallow ConvNet, Figure 2 from the paper.](architecture.png)</span>{#fig-arch}</span></code></pre></div></div>
<p>Enable click-to-zoom for all images via <code>_quarto.yml</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb14-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lightbox</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span></code></pre></div></div>
<p><strong>Renders as:</strong> a captioned, numbered figure (<code>Figure 1: ...</code>) that is cross-referenceable with <code>@fig-arch</code> and opens in a zoom overlay on click.</p>
</section>
<section id="listings-categories-search-site-level" class="level2">
<h2 class="anchored" data-anchor-id="listings-categories-search-site-level">Listings, categories &amp; search (site-level)</h2>
<p>These live in <code>_quarto.yml</code> / <code>index.qmd</code>, but every notebook feeds them:</p>
<ul>
<li><strong>Listing</strong> - <code>index.qmd</code> auto-indexes every <code>**/*.ipynb</code>, sorted by <code>date</code>.</li>
<li><strong>Categories</strong> - the <code>categories:</code> in each notebook become clickable filters.</li>
<li><strong>Full-text search</strong> - on by default; searches every notebook, client-side, free on GitHub Pages.</li>
</ul>
</section>
<section id="social-cards-comments-formats-site-level-config" class="level2">
<h2 class="anchored" data-anchor-id="social-cards-comments-formats-site-level-config">Social cards, comments &amp; formats (site-level config)</h2>
<p>These are <code>_quarto.yml</code> settings rather than inline output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">website</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb15-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">open-graph</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # rich link previews when shared</span></span>
<span id="cb15-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">twitter-card</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb15-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comments</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb15-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">giscus</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # discussion thread per post, backed by GitHub Discussions</span></span>
<span id="cb15-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repo</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> bkowshik/engram</span></span>
<span id="cb15-7"></span>
<span id="cb15-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb15-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> default</span></span>
<span id="cb15-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">revealjs</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # a speaker deck from the same notebook cells</span></span>
<span id="cb15-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slide-level</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span></code></pre></div></div>
<p>Render a slide deck from any notebook with <code>quarto render notebook.ipynb --to revealjs</code>.</p>
</section>
<section id="no-execution-on-build" class="level2">
<h2 class="anchored" data-anchor-id="no-execution-on-build">No execution on build</h2>
<p>engram sets this in <code>_quarto.yml</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">execute</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb16-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enabled</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">false</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # render saved notebook outputs; never launch a kernel</span></span></code></pre></div></div>
<p>This keeps publishing trivial: do the heavy compute wherever suits you (Colab, a GPU box, locally), save the notebook <strong>with its outputs</strong>, and the build just renders those - no kernel, no dependencies, no dataset downloads on CI.</p>
<p>If you would rather have the build execute notebooks and cache the results, the alternative is <code>execute: freeze: auto</code> plus a committed <code>_freeze/</code> directory - but that needs the full environment available at render time.</p>
</section>
<section id="colab-badge" class="level2">
<h2 class="anchored" data-anchor-id="colab-badge">Colab badge</h2>
<p><strong>Source</strong> (first markdown cell of a notebook)</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">](https://colab.research.google.com/github/bkowshik/engram/blob/main/FOLDER/NOTEBOOK.ipynb)</span></span></code></pre></div></div>
<p><strong>Renders as:</strong> a clickable “Open In Colab” badge that launches the notebook in Google Colab.</p>
</section>
</section>
<section id="copy-paste-starter" class="level1">
<h1>Copy-paste starter</h1>
<p>Front matter for a new notebook (raw cell at the top):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb18-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">title</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span>
<span id="cb18-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">description</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span>
<span id="cb18-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'YYYY-MM-DD'</span></span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date-modified</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> last-modified</span></span>
<span id="cb18-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">categories</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[]</span></span>
<span id="cb18-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bibliography</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> references.bib</span></span>
<span id="cb18-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toc</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb18-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">code-tools</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb18-10"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
<p><strong>Before you push:</strong></p>
<ul class="task-list">
<li><label><input type="checkbox">Front matter filled in (title, description, date, categories)</label></li>
<li><label><input type="checkbox">Colab badge points at the right folder/filename</label></li>
<li><label><input type="checkbox">Papers cited with <code>@key</code>, entries added to <code>references.bib</code></label></li>
<li><label><input type="checkbox">Cells reveal what you intend (<code>echo</code>/<code>output</code>/<code>code-fold</code>)</label></li>
<li><label><input type="checkbox">Notebook saved <strong>with its outputs</strong> (the build never executes it)</label></li>
<li><label><input type="checkbox">Plots are retina (<code>%config InlineBackend.figure_format = "retina"</code>)</label></li>
<li><label><input type="checkbox">No private references (paths, personal notes)</label></li>
</ul>
</section>
<section id="update-log" class="level1">
<h1>Update log</h1>
<p>Append a line whenever you learn or adopt something new.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 8%">
<col style="width: 91%">
</colgroup>
<thead>
<tr class="header">
<th>Date</th>
<th>Change</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>2026-06-16</td>
<td>Created. Each Part 2 feature now shows Source + Renders-as; example plot cell shows code and output together.</td>
</tr>
</tbody>
</table>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-schirrmeister2017" class="csl-entry">
Schirrmeister, Robin Tibor, Jost Tobias Springenberg, Lukas Dominique Josef Fiederer, et al. 2017. <span>“Deep Learning with Convolutional Neural Networks for <span>EEG</span> Decoding and Visualization.”</span> <em>Human Brain Mapping</em> 38 (11): 5391–420. <a href="https://doi.org/10.1002/hbm.23730">https://doi.org/10.1002/hbm.23730</a>.
</div>
</div></section></div> ]]></description>
  <category>reference</category>
  <category>quarto</category>
  <guid>https://bkowshik.github.io/engram/cookbook.html</guid>
  <pubDate>Tue, 16 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Events</title>
  <link>https://bkowshik.github.io/engram/neuroai/neuralset/01_tutorial.html</link>
  <description><![CDATA[ 




<div id="933e3114" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello, brain!'</span>)</span></code></pre></div></div>
</div>
<div id="d370bd15" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"git+https://github.com/bkowshik/neuroai.git@main"</span></span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>uv pip install <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>system <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"moabb"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neuralset[all] @ </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{B}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">#subdirectory=neuralset-repo"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{B}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">#subdirectory=neuralfetch-repo"</span></span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>uv pip install <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>system globus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sdk</span></code></pre></div></div>
</div>
<div id="e5bfcb2b" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>uv pip freeze <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> grep neural</span></code></pre></div></div>
</div>
<div id="83bc2b3c" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os, getpass</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> neuralset <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ns</span>
<span id="cb4-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> neuralset <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> events</span>
<span id="cb4-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> neuralset.events <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> etypes</span>
<span id="cb4-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> neuralfetch.utils <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> compute_study_info</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> globus_sdk</span></code></pre></div></div>
</div>
<div id="eea85221" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># To download data from https://www.globus.org/ Ex: Brennan2019Hierarchical</span></span>
<span id="cb5-2">os.environ[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NEURALFETCH_GLOBUS_CLIENT_ID"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> getpass.getpass(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Globus Client UUID: "</span>)</span>
<span id="cb5-3">os.environ[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NEURALFETCH_GLOBUS_CLIENT_SECRET"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> getpass.getpass(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Globus Client Secret: "</span>)</span></code></pre></div></div>
</div>
<div id="3320954a" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(etypes.Event._CLASSES), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(etypes.Event._CLASSES.keys())[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span></code></pre></div></div>
</div>
<div id="53c45b1d" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create event from a dictionary</span></span>
<span id="cb7-2">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb7-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Word"</span>,</span>
<span id="cb7-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"start"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb7-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"timeline"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"run-01"</span>,</span>
<span id="cb7-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span>,</span>
<span id="cb7-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"surprisal"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>,</span>
<span id="cb7-8">}</span>
<span id="cb7-9">event <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> etypes.Event.from_dict(data)</span>
<span id="cb7-10">event</span></code></pre></div></div>
</div>
<div id="64ecdd31" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create event using the class</span></span>
<span id="cb8-2">word <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> etypes.Word(</span>
<span id="cb8-3">    start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,</span>
<span id="cb8-4">    duration<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>,</span>
<span id="cb8-5">    timeline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sub-01_run-01"</span>,</span>
<span id="cb8-6">    text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span></span>
<span id="cb8-7">)</span>
<span id="cb8-8">word</span></code></pre></div></div>
</div>
<div id="f3549ccd" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a timeline using a Pandas DataFrame</span></span>
<span id="cb9-2">tl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sub-01_run-01"</span></span>
<span id="cb9-3">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb9-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Word"</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.5</span>, duration<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, timeline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tl, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The"</span>),</span>
<span id="cb9-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Word"</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.0</span>, duration<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, timeline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tl, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cat"</span>),</span>
<span id="cb9-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Word"</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.6</span>, duration<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, timeline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tl, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sat"</span>),</span>
<span id="cb9-7">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sentence"</span>, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.5</span>, duration<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, timeline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tl, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The cat sat"</span>, language<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'en'</span>, subject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bkowshik'</span>, session<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, run<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb9-8">]</span>
<span id="cb9-9">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> events.standardize_events(pd.DataFrame(rows))</span>
<span id="cb9-10">df</span></code></pre></div></div>
</div>
<section id="studies" class="level1">
<h1>Studies</h1>
<div id="5876291f" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">catalog <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ns.Study.catalog()</span>
<span id="cb10-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(catalog), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(catalog.keys())[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span></code></pre></div></div>
</div>
<div id="391770fa" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">study <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ns.Study(name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fake2025Meg"</span>, path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ns.CACHE_FOLDER)</span>
<span id="cb11-2">events <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> study.run()</span>
<span id="cb11-3"></span>
<span id="cb11-4">columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subject"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"start"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duration"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"timeline"</span>]</span>
<span id="cb11-5">events[columns]</span></code></pre></div></div>
</div>


</section>

 ]]></description>
  <guid>https://bkowshik.github.io/engram/neuroai/neuralset/01_tutorial.html</guid>
  <pubDate>Thu, 25 Jun 2026 03:25:29 GMT</pubDate>
</item>
<item>
  <title></title>
  <link>https://bkowshik.github.io/engram/pytorch/01_tutorial.html</link>
  <description><![CDATA[ 




<p>Ref: https://docs.pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html</p>
<div id="7130e576" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> optim</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> torch <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> nn</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch.nn.functional <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span></code></pre></div></div>
</div>
<div id="4792dfed" class="cell" data-execution_count="11">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]]</span>
<span id="cb2-2">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.tensor(data)</span>
<span id="cb2-3">t</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="11">
<pre><code>tensor([[1, 2],
        [3, 4],
        [5, 6]])</code></pre>
</div>
</div>
<div id="cb3a67f6" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">torch.rand_like(t, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>torch.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="12">
<pre><code>tensor([[0.6160, 0.9337],
        [0.7736, 0.9430],
        [0.5630, 0.0227]])</code></pre>
</div>
</div>
<div id="7be33f84" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.rand(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb6-2">t.shape, t.dtype, t.device</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="13">
<pre><code>(torch.Size([3, 4]), torch.float32, device(type='cpu'))</code></pre>
</div>
</div>
<div id="d0a702df" class="cell" data-execution_count="14">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">device <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.accelerator.current_accelerator() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> torch.accelerator.is_available() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cpu'</span></span>
<span id="cb8-2">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.to(device)</span>
<span id="cb8-3">t.shape, t.dtype, t.device</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<pre><code>(torch.Size([3, 4]), torch.float32, device(type='cuda', index=0))</code></pre>
</div>
</div>
<div id="e27a9c51" class="cell" data-execution_count="15">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.ones(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb10-2">t[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb10-3">t</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="15">
<pre><code>tensor([[1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1]])</code></pre>
</div>
</div>
<div id="ffe61a35" class="cell" data-execution_count="16">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">torch.cat([t, t, t], dim<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="16">
<pre><code>tensor([[1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
        [1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1]])</code></pre>
</div>
</div>
<section id="minimal-pytorch-neural-network" class="level3">
<h3 class="anchored" data-anchor-id="minimal-pytorch-neural-network">Minimal PyTorch neural network</h3>
<div id="a8a1c960" class="cell" data-execution_count="17">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.randn(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data: y = 2x</span></span>
<span id="cb14-2">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nn.Linear(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)                                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># model</span></span>
<span id="cb14-3">opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.optim.SGD(model.parameters(), lr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># optimizer</span></span>
<span id="cb14-4">loss_fn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nn.MSELoss()                                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># loss</span></span>
<span id="cb14-5"></span>
<span id="cb14-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> epoch <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>):</span>
<span id="cb14-7">    opt.zero_grad()                                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. reset grads</span></span>
<span id="cb14-8">    loss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loss_fn(model(X), y)                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. forward + loss</span></span>
<span id="cb14-9">    loss.backward()                                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. backprop  ← grads now exist</span></span>
<span id="cb14-10">    parts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"loss </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>item()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print after backward</span></span>
<span id="cb14-11">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>param<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>item()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> (grad </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>param<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>grad<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>item()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span></span>
<span id="cb14-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, param <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> model.named_parameters()</span>
<span id="cb14-13">    ]</span>
<span id="cb14-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" | "</span>.join(parts))</span>
<span id="cb14-15">    opt.step()                                           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. update weights</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>loss 7.2326 | weight -0.8592 (grad -5.0265) | bias -0.3763 (grad -0.2476)
loss 6.0221 | weight -0.6079 (grad -4.5810) | bias -0.3639 (grad -0.2673)
loss 5.0156 | weight -0.3789 (grad -4.1754) | bias -0.3506 (grad -0.2810)
loss 4.1784 | weight -0.1701 (grad -3.8060) | bias -0.3365 (grad -0.2898)
loss 3.4819 | weight 0.0202 (grad -3.4696) | bias -0.3220 (grad -0.2944)
loss 2.9023 | weight 0.1937 (grad -3.1632) | bias -0.3073 (grad -0.2956)
loss 2.4197 | weight 0.3519 (grad -2.8841) | bias -0.2925 (grad -0.2940)
loss 2.0178 | weight 0.4961 (grad -2.6298) | bias -0.2778 (grad -0.2900)
loss 1.6831 | weight 0.6275 (grad -2.3982) | bias -0.2633 (grad -0.2843)
loss 1.4042 | weight 0.7475 (grad -2.1871) | bias -0.2491 (grad -0.2770)
loss 1.1718 | weight 0.8568 (grad -1.9947) | bias -0.2353 (grad -0.2686)
loss 0.9780 | weight 0.9565 (grad -1.8195) | bias -0.2218 (grad -0.2594)
loss 0.8165 | weight 1.0475 (grad -1.6597) | bias -0.2089 (grad -0.2495)
loss 0.6817 | weight 1.1305 (grad -1.5141) | bias -0.1964 (grad -0.2392)
loss 0.5693 | weight 1.2062 (grad -1.3814) | bias -0.1844 (grad -0.2287)
loss 0.4756 | weight 1.2753 (grad -1.2604) | bias -0.1730 (grad -0.2180)
loss 0.3973 | weight 1.3383 (grad -1.1500) | bias -0.1621 (grad -0.2073)
loss 0.3320 | weight 1.3958 (grad -1.0494) | bias -0.1517 (grad -0.1967)
loss 0.2774 | weight 1.4483 (grad -0.9577) | bias -0.1419 (grad -0.1863)
loss 0.2319 | weight 1.4962 (grad -0.8741) | bias -0.1326 (grad -0.1762)
loss 0.1938 | weight 1.5399 (grad -0.7978) | bias -0.1238 (grad -0.1663)
loss 0.1621 | weight 1.5797 (grad -0.7282) | bias -0.1155 (grad -0.1567)
loss 0.1355 | weight 1.6162 (grad -0.6647) | bias -0.1076 (grad -0.1474)
loss 0.1133 | weight 1.6494 (grad -0.6068) | bias -0.1002 (grad -0.1386)
loss 0.0948 | weight 1.6797 (grad -0.5540) | bias -0.0933 (grad -0.1301)</code></pre>
</div>
</div>
<div id="df2fc729" class="cell" data-execution_count="18">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Input, output and prediction</span></span>
<span id="cb16-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> np.printoptions(precision<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, suppress<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb16-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(torch.cat([X, y, model(X)], dim<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).detach().numpy())</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[-0.9295 -1.859  -1.6739]
 [-0.384  -0.768  -0.7425]
 [ 1.663   3.3259  2.7526]
 [ 1.0355  2.071   1.6813]
 [ 0.1104  0.2209  0.1017]
 [-1.2856 -2.5712 -2.2819]
 [-0.3015 -0.603  -0.6016]
 [-1.3607 -2.7215 -2.4102]
 [ 0.6638  1.3276  1.0466]
 [-0.0945 -0.189  -0.2482]]</code></pre>
</div>
</div>


</section>

 ]]></description>
  <guid>https://bkowshik.github.io/engram/pytorch/01_tutorial.html</guid>
  <pubDate>Thu, 25 Jun 2026 03:25:29 GMT</pubDate>
</item>
</channel>
</rss>
