Skip to content

cosmotron_mcp.tools.bmode_rotation

run_bmode_rotation_ensemble_from_session

run_bmode_rotation_ensemble_from_session(session_dir: str, n_rot: int = 100, backend: str = 'auto', n_batches: int | None = None) -> dict

Launch the random-rotation shape-noise ensemble (L2) as K parallel batch jobs — the near-definitive empirical B-mode covariance.

Randomly rotates the galaxy ellipticities and re-measures the spin-2 BB/EB pseudo-Cℓ many times to build an EMPIRICAL noise power + covariance (the KiDS/DES/HSC method), rather than trusting the over-precise analytic covariance. NOT a GLASS re-simulation. Splits n_rot rotations into K batches and submits ONE background job per batch (never one per rotation); with backend="slurm" (and a site pinned in the session) the batches run on the cluster. Poll every returned job_id with check_job; once all are done call aggregate_bmode_ensemble_from_session. Only offer this when a B-mode diagnosis returns COLORED_EXCESS / BENIGN_FLAG.

Parameters:

Name Type Description Default
session_dir str

Session directory.

required
n_rot int

Total number of random rotations (default 100).

100
backend str

"auto"/"local" for local jobs, "slurm" for a pinned remote site.

'auto'
n_batches int | None

Override the batch count (default: core count).

None

Returns:

Type Description
dict

{n_rot, n_batches, batches: [{job_id, status, out_json, ...}], next}.

Source code in cosmotron_mcp/server.py
@mcp.tool()
@sync_budget_guard
def run_bmode_rotation_ensemble_from_session(
    session_dir: str, n_rot: int = 100, backend: str = "auto",
    n_batches: int | None = None,
) -> dict:
    """Launch the random-rotation shape-noise ensemble (L2) as K parallel batch
    jobs — the near-definitive empirical B-mode covariance.

    Randomly rotates the galaxy ellipticities and re-measures the spin-2 BB/EB
    pseudo-Cℓ many times to build an EMPIRICAL noise power + covariance (the
    KiDS/DES/HSC method), rather than trusting the over-precise analytic
    covariance. NOT a GLASS re-simulation. Splits ``n_rot`` rotations into K
    batches and submits ONE background job per batch (never one per rotation);
    with ``backend="slurm"`` (and a site pinned in the session) the batches run on
    the cluster. Poll every returned ``job_id`` with ``check_job``; once all are
    ``done`` call ``aggregate_bmode_ensemble_from_session``. Only offer this when a
    B-mode diagnosis returns ``COLORED_EXCESS`` / ``BENIGN_FLAG``.

    Args:
        session_dir: Session directory.
        n_rot: Total number of random rotations (default 100).
        backend: ``"auto"``/``"local"`` for local jobs, ``"slurm"`` for a pinned
            remote site.
        n_batches: Override the batch count (default: core count).

    Returns:
        ``{n_rot, n_batches, batches: [{job_id, status, out_json, ...}], next}``.
    """
    _require_human_gates(session_dir)
    result = _run_bmode_rotation_ensemble_from_session(
        session_dir, n_rot=n_rot, backend=backend, n_batches=n_batches)
    _log_tool_call(session_dir, "run_bmode_rotation_ensemble_from_session",
                   {"n_rot": n_rot, "backend": backend})
    return _compact_result(result)

aggregate_bmode_ensemble_from_session

aggregate_bmode_ensemble_from_session(session_dir: str) -> dict

Combine the finished rotation batches into an empirical B-mode noise power + covariance and re-run the null against it — the definitive (L2) verdict.

Call after every run_bmode_rotation_ensemble_from_session batch job is done. Reads results/systematics/rotation_ensemble/batch_*.json, builds the empirical mean (noise floor) + Hartlap-corrected covariance per spin-2 bin, subtracts the empirical noise from the DATA B-modes, and χ²-tests against zero. Writes results/covariance/covariance_ensemble_{BB,EB}.json and merges the empirical p-values into results/systematics/bmode_diagnosis.json.

Parameters:

Name Type Description Default
session_dir str

Session directory.

required

Returns:

Type Description
dict

``{n_tests, n_fail, tests: [{bin, component, chi2, p_value, n_rot,

dict

verdict}]}``.

Source code in cosmotron_mcp/server.py
@mcp.tool()
@sync_budget_guard
def aggregate_bmode_ensemble_from_session(session_dir: str) -> dict:
    """Combine the finished rotation batches into an empirical B-mode noise power +
    covariance and re-run the null against it — the definitive (L2) verdict.

    Call after every ``run_bmode_rotation_ensemble_from_session`` batch job is
    ``done``. Reads ``results/systematics/rotation_ensemble/batch_*.json``, builds
    the empirical mean (noise floor) + Hartlap-corrected covariance per spin-2 bin,
    subtracts the empirical noise from the DATA B-modes, and χ²-tests against zero.
    Writes ``results/covariance/covariance_ensemble_{BB,EB}.json`` and merges the
    empirical p-values into ``results/systematics/bmode_diagnosis.json``.

    Args:
        session_dir: Session directory.

    Returns:
        ``{n_tests, n_fail, tests: [{bin, component, chi2, p_value, n_rot,
        verdict}]}``.
    """
    _require_human_gates(session_dir)
    result = _aggregate_bmode_ensemble_from_session(session_dir)
    _log_tool_call(session_dir, "aggregate_bmode_ensemble_from_session", {})
    return _compact_result(result)