diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 00000000..5fdfa5bd --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,10 @@ +# Commits to skip when assigning blame — mechanical, repo-wide reformats that +# touch many lines without changing behaviour. Listing them here keeps +# `git blame` pointing at the commit that actually wrote each line. +# +# Enable locally with: +# git config blame.ignoreRevsFile .git-blame-ignore-revs +# (GitHub's blame view honours this file automatically.) + +# style(ruff): repo-wide ruff format pass (mechanical, no semantic change) +34740e11893fb90766d2139ebbb9d10b8b5d4091 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..c2be73bd --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,244 @@ +name: Lint + +# Lint gate for `develop` — warn locally, account here. +# +# The model (settled with Cail + Sacha, 2026-06-23/30): +# * Locally, ruff auto-applies safe fixes and only WARNS on the rest (see +# .pre-commit-config.yaml) — you commit freely. +# * Getting into `develop` is gated. This job runs the FULL ruff policy +# (`ruff check .` + `ruff format --check .`, region-aware per pyproject.toml) +# and on any failure it (1) fails the job RED so the check blocks the merge, +# and (2) tells the author what to fix — in the surface that fits the event: +# - On a PR → posts/updates a COMMENT on the PR itself, with the full +# violation list (and ruff annotations in the run's Checks view). The +# author sees it where they already are; no disconnected issue. The +# comment turns green when ruff passes. +# - On a direct push to develop → there's no PR to comment on, so it opens +# (or updates) ONE lint-debt issue for the committer, @-mentioning and +# assigning them. Auto-closes when their next push is clean. +# * If ruff itself can't run (network, bad version), the job goes red but says +# nothing — that's infra, not the committer's lint debt. +# +# Triggers: pushes to `develop` and PRs targeting `develop` only — other +# branches stay quiet (too noisy otherwise). `workflow_dispatch` is for manual +# testing of the gate itself. +# +# Why `pull_request_target` for PRs: commenting on a PR needs a write token, and +# PRs from forks (e.g. sachaguer/) get a read-only token under the plain +# `pull_request` event. `pull_request_target` runs this workflow from the BASE +# branch (so the workflow definition is trusted) with a write token, while we +# check out the PR head ONLY to lint it. Two hardening measures make running +# tooling over untrusted PR code safe here: ruff is a static analyzer (it parses +# files, never imports/executes them), and `uvx --no-config` makes uv ignore any +# `uv.toml`/`[tool.uv]` in the PR tree, so a malicious PR can't redirect ruff's +# download to a trojaned index. The checkout also drops its git credentials. +# (A PR can still edit `[tool.ruff]` to weaken its own policy, but that's visible +# in the diff and reviewed like any other change.) + +on: + push: + branches: [develop] + pull_request_target: + branches: [develop] + workflow_dispatch: + +# One lint run per branch / PR; newer pushes cancel older in-flight runs. +concurrency: + group: lint-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +permissions: + contents: read + issues: write # develop-push lint-debt issue + pull-requests: write # PR lint comment + +jobs: + lint: + name: Ruff (check + format) + runs-on: ubuntu-latest + + steps: + - name: Checkout (PR head on pull_request_target, else the pushed ref) + uses: actions/checkout@v4 + with: + ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + + - name: Install uv + uses: astral-sh/setup-uv@v3 + + # Run the checks WITHOUT failing the step — we post feedback before turning + # the job red. `ruff@` matches the pre-commit version; `--no-config` + # neutralizes any uv config in the (untrusted) PR tree. + # ruff exit codes: 0 = clean, 1 = violations, >=2 = ruff/uvx error. + - name: Run ruff + id: ruff + shell: bash + run: | + set +e + # Inline annotations on the diff (ruff's GitHub format → step log). + uvx --no-config ruff@0.15.18 check . --output-format=github + # Readable output for the PR comment / issue body, plus exit codes. + uvx --no-config ruff@0.15.18 check . --output-format=concise > check.txt 2>&1; check_rc=$? + uvx --no-config ruff@0.15.18 format --check . > format.txt 2>&1; fmt_rc=$? + + { + echo "### \`ruff check .\`" + if [ "$check_rc" -eq 0 ]; then echo; echo "✅ clean"; else echo; echo '```'; cat check.txt; echo '```'; fi + echo + echo "### \`ruff format --check .\`" + if [ "$fmt_rc" -eq 0 ]; then echo; echo "✅ clean"; else echo; echo '```'; cat format.txt; echo '```'; fi + } > report.md + + # A tooling error (rc >= 2) is not lint debt — block, but say nothing. + if [ "$check_rc" -ge 2 ] || [ "$fmt_rc" -ge 2 ]; then + echo "tool_error=true" >> "$GITHUB_OUTPUT" + else + echo "tool_error=false" >> "$GITHUB_OUTPUT" + fi + if [ "$check_rc" -eq 0 ] && [ "$fmt_rc" -eq 0 ]; then + echo "passed=true" >> "$GITHUB_OUTPUT" + else + echo "passed=false" >> "$GITHUB_OUTPUT" + fi + + # Feedback is a side effect — never let it red a clean run. + - name: Tell the author (PR comment) or record it (develop-push issue) + if: steps.ruff.outputs.tool_error == 'false' + continue-on-error: true + uses: actions/github-script@v7 + env: + PASSED: ${{ steps.ruff.outputs.passed }} + with: + script: | + const fs = require('fs'); + const passed = process.env.PASSED === 'true'; + const { owner, repo } = context.repo; + const runUrl = `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}`; + const report = passed ? '' : fs.readFileSync('report.md', 'utf8'); + + // ---- PR: speak on the PR itself (comment, auto-updating) ---------- + if (context.eventName === 'pull_request_target') { + const pr = context.payload.pull_request; + const author = pr.user.login; + const MARKER = ''; + const comments = await github.paginate(github.rest.issues.listComments, { + owner, repo, issue_number: pr.number, per_page: 100, + }); + const mine = comments.find(c => c.body && c.body.includes(MARKER)); + + if (passed) { + // Only update an existing comment to green; don't post on a PR + // that was never dirty. + if (mine) { + await github.rest.issues.updateComment({ + owner, repo, comment_id: mine.id, + body: `✅ **ruff is clean** — nothing to fix here. ${MARKER}`, + }); + } + core.info('PR clean.'); + return; + } + + const body = [ + `### 🔴 ruff found lint / format issues`, + ``, + `@${author} — these block the merge into \`develop\`. Full list below (also surfaced as annotations in the CI run):`, + ``, + report, + ``, + `---`, + `[CI run](${runUrl}) · _Updates on every push and turns green when ruff passes — nothing else to do._`, + MARKER, + ].join('\n'); + + if (mine) { + await github.rest.issues.updateComment({ owner, repo, comment_id: mine.id, body }); + core.info(`Updated PR comment ${mine.id}.`); + } else { + await github.rest.issues.createComment({ owner, repo, issue_number: pr.number, body }); + core.info('Posted PR comment.'); + } + return; + } + + // ---- Direct push to develop: per-committer issue (no PR exists) --- + const login = context.actor; + const where = `push to \`${context.ref.replace('refs/heads/', '')}\` (\`${context.sha.slice(0, 7)}\`)`; + const title = `Lint debt: @${login}`; + const LABEL = 'lint-debt'; + + try { + await github.rest.issues.getLabel({ owner, repo, name: LABEL }); + } catch { + try { + await github.rest.issues.createLabel({ + owner, repo, name: LABEL, color: 'd93f0b', + description: 'Auto-filed lint failures from the develop gate', + }); + } catch (e) { core.info(`label create skipped: ${e.message}`); } + } + + const open = await github.paginate(github.rest.issues.listForRepo, { + owner, repo, state: 'open', labels: LABEL, per_page: 100, + }); + const existing = open.find(i => i.title === title && !i.pull_request); + + if (passed) { + if (existing) { + await github.rest.issues.createComment({ + owner, repo, issue_number: existing.number, + body: `✅ Lint is clean as of ${where}. Closing — thanks!`, + }); + await github.rest.issues.update({ owner, repo, issue_number: existing.number, state: 'closed' }); + core.info(`Closed #${existing.number} (clean).`); + } else { + core.info('Clean, no open lint-debt issue to close.'); + } + return; + } + + const body = [ + `@${login} — ruff flagged lint issues in **${where}** (pushed straight to \`develop\`).`, + ``, + `This doesn't block local work (pre-commit only warns), but \`develop\` stays red until it's clean. Fix and push again — **this issue auto-closes when CI goes green.**`, + ``, + report, + ``, + `---`, + `[CI run](${runUrl}) · _Auto-filed by the lint gate; updated in place, closed when clean._`, + ``, + ].join('\n'); + + let number; + if (existing) { + await github.rest.issues.update({ owner, repo, issue_number: existing.number, body }); + await github.rest.issues.createComment({ + owner, repo, issue_number: existing.number, + body: `🔴 Still failing as of ${where}. [run](${runUrl})`, + }); + number = existing.number; + core.info(`Updated #${number}.`); + } else { + const created = await github.rest.issues.create({ owner, repo, title, body, labels: [LABEL] }); + number = created.data.number; + core.info(`Opened #${number}.`); + } + + try { + await github.rest.issues.addAssignees({ owner, repo, issue_number: number, assignees: [login] }); + } catch (e) { + core.info(`assign skipped (${login} not assignable): ${e.message}`); + } + + # Red → blocks the merge. `always()` so a hiccup in the feedback step above + # can't suppress the red on genuine lint debt; a tooling error also reds. + - name: Fail the job if the gate didn't pass + if: always() && steps.ruff.outputs.passed == 'false' + run: | + if [ "${{ steps.ruff.outputs.tool_error }}" = "true" ]; then + echo "::error::ruff could not run (network / version) — gate inconclusive, blocking." + else + echo "::error::ruff found lint/format issues — see the PR comment (or lint-debt issue)." + fi + exit 1 diff --git a/.gitignore b/.gitignore index f8daae0a..f8eec899 100644 --- a/.gitignore +++ b/.gitignore @@ -134,6 +134,9 @@ uv.lock # mkdocs documentation /site +# ruff +.ruff_cache/ + # mypy .mypy_cache/ .dmypy.json @@ -192,3 +195,6 @@ papers/catalog/plots/*.pdf # SLURM run logs from cosmo_val validation runs papers/cosmo_val/logs/ + +# Ignore scratch notebooks +scratch/*/*.ipynb \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 95451ee3..2cee26de 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,46 @@ -# Bloat guards: notebook outputs are stripped on commit, and oversized files -# are rejected. Activate once per clone with `pre-commit install` -# (see CONTRIBUTING.md). +# Pre-commit hooks. The posture: auto-apply what ruff can fix safely, WARN +# (never block) on what it can't, and hard-block only on repo bloat. +# +# * ruff auto-applies everything that's automatically applicable — `ruff +# format` (layout) and `ruff check --fix`'s SAFE fixes (import sorting, +# unused imports, …). When it rewrites a staged file the commit stops once +# so you re-`git add` the result; that's pre-commit's modify→re-stage step, +# not a lint failure. +# * What ruff WON'T safely fix — undefined names (F821), unused variables +# (F841), and other judgement calls — is printed as a WARNING and never +# blocks the commit. Those get enforced on the `develop` gate +# (.github/workflows/lint.yml), not at your keyboard. +# * Bloat guards (notebook outputs, oversized files) DO block — heavy content +# is expensive to undo once in history. +# +# ruff is pinned to the SAME version CI uses — keep them in lockstep. +# Activate once per clone with `pre-commit install` (see CONTRIBUTING.md). repos: + # --- ruff: auto-apply the safe stuff, warn on the rest, never hard-block ---- + - repo: local + hooks: + # Linter: applies ruff's SAFE autofixes (and re-stages like a formatter). + # Everything it won't safely fix is printed as a warning; `|| true` keeps + # those from ever blocking the commit. Runs before the formatter so layout + # gets the final word. + - id: ruff-fix + name: ruff check --fix (safe fixes auto-applied; the rest only warn) + entry: bash -c 'ruff check --fix "$@" || true' -- + language: python + additional_dependencies: ["ruff==0.15.18"] + types_or: [python, pyi] + require_serial: true + verbose: true + # Formatter: auto-applies. If it reformats your staged files the commit + # stops once so you re-`git add` — not a lint failure, just a re-stage. + - id: ruff-format + name: ruff format (auto-applies) + entry: ruff format + language: python + additional_dependencies: ["ruff==0.15.18"] + types_or: [python, pyi] + + # --- Bloat guards: BLOCKING (intentional) ---------------------------------- - repo: https://github.com/kynan/nbstripout rev: 0.8.1 hooks: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index eff30592..35d5e659 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -48,24 +48,47 @@ Tests live in `src/sp_validation/tests/`. The default options (configured in inside the freshly-built container image *before* publishing it, so a failing test blocks the image push. -## Code style +## Code style and the lint gate -We use [`ruff`](https://docs.astral.sh/ruff/) for linting and import sorting, -with a line length of 88: +We use [`ruff`](https://docs.astral.sh/ruff/) for both formatting and linting +(line length 88). The policy lives in `pyproject.toml` and is region-aware: +`src/sp_validation/` is strict, while the analysis/workflow/script trees waive a +few intentional patterns (`sys.path` edits before imports, star-imports). ```bash -ruff check # report issues +ruff check # report lint issues ruff check --fix # auto-fix what it can +ruff format # format the tree ``` -Please run `ruff check` before opening a pull request. +**Local hooks auto-fix the safe stuff, warn on the rest.** The +[`pre-commit`](https://pre-commit.com/) hooks auto-apply everything ruff can fix +safely — `ruff format` plus `ruff check --fix`'s safe fixes (import sorting, +unused imports). When that rewrites a staged file the commit stops *once* so you +`git add` the result and commit again. Anything ruff *won't* safely fix — +undefined names, unused variables, other judgement calls — is printed as a +**warning** and never blocks the commit. Judgement-call lint stays out of your +way locally; the gate below is where it's enforced. + +**`develop` is the gate.** On every push to `develop` and every PR into it, CI +runs the full ruff policy. If it fails, the check goes **red and blocks the +merge**, and the bot tells you what to fix where you already are: + +- **On a PR** → it posts (and keeps updating) a **comment on the PR** listing the + violations (also surfaced as annotations in the CI run). Push a fix and the + comment turns green. +- **On a direct push to `develop`** (no PR) → it opens (or updates) a single + **lint-debt issue assigned to you**, which auto-closes when CI is green. + +So: warn while you work, clean before it lands. ## Commit hygiene (notebooks & large files) -The repository's history is heavy from committed notebook outputs; two -[`pre-commit`](https://pre-commit.com/) hooks guard against more of it: -`nbstripout` (strips notebook outputs on commit) and a large-file check -(2 MB). Activate them once per clone: +The repository's history is heavy from committed notebook outputs. Alongside the +warn-only ruff hooks above, two **blocking** `pre-commit` hooks guard against +more of it: `nbstripout` (strips notebook outputs on commit) and a large-file +check (2 MB). These block because heavy content is expensive to undo once it is +in history. Activate everything once per clone: ```bash pre-commit install diff --git a/cosmo_inference/get_chi2.ipynb b/cosmo_inference/get_chi2.ipynb index 5c8f3404..69d026a2 100644 --- a/cosmo_inference/get_chi2.ipynb +++ b/cosmo_inference/get_chi2.ipynb @@ -6,45 +6,45 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import configparser\n", + "import os\n", "import subprocess\n", "\n", - "from getdist import plots, loadMCSamples\n", - "from astropy.io import fits\n", - "import numpy as np\n", + "import healpy as hp\n", "import matplotlib.pyplot as plt\n", - "import seaborn as sns\n", - "from scipy.interpolate import interp1d\n", + "import numpy as np\n", "import scipy.stats as stats\n", + "import seaborn as sns\n", + "from astropy.io import fits\n", + "from getdist import plots\n", "from IPython.display import Markdown, display\n", - "import healpy as hp\n", + "from scipy.interpolate import interp1d\n", + "\n", "%matplotlib inline\n", "# import uncertainties\n", "\n", "# Use paper style and seaborn with husl palette\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "# Set default palette - will be updated per plot as needed\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", - "root_dir='/n09data/guerrini/output_chains/'\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "root_dir = \"/n09data/guerrini/output_chains/\"\n", "\n", - "catalog_version = 'SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1'\n", + "catalog_version = \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1\"\n", "\n", - "path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/'\n", + "path_ini_files = \"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/\"\n", "\n", "roots = [\n", - " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\" for i in [3, 5, 7, 10, 11]\n", + " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\"\n", + " for i in [3, 5, 7, 10, 11]\n", "]\n", "\n", "\"\"\" roots = [\n", @@ -72,19 +72,23 @@ "for root in roots:\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+f'/cosmosis_pipeline_{root}.ini')\n", + " config.read(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\")\n", "\n", " add_xi_sys = config[\"2pt_like\"][\"add_xi_sys\"]\n", - " add_xi_sys = add_xi_sys == 'T'\n", - " lower_bound_xi_plus, upper_bound_xi_plus = map(float, config[\"2pt_like\"][\"angle_range_XI_PLUS_1_1\"].split())\n", - " lower_bound_xi_minus, upper_bound_xi_minus = map(float, config[\"2pt_like\"][\"angle_range_XI_MINUS_1_1\"].split())\n", + " add_xi_sys = add_xi_sys == \"T\"\n", + " lower_bound_xi_plus, upper_bound_xi_plus = map(\n", + " float, config[\"2pt_like\"][\"angle_range_XI_PLUS_1_1\"].split()\n", + " )\n", + " lower_bound_xi_minus, upper_bound_xi_minus = map(\n", + " float, config[\"2pt_like\"][\"angle_range_XI_MINUS_1_1\"].split()\n", + " )\n", "\n", " properties[root] = {\n", - " 'add_xi_sys': add_xi_sys,\n", - " 'lower_bound_xi_plus': lower_bound_xi_plus,\n", - " 'upper_bound_xi_plus': upper_bound_xi_plus,\n", - " 'lower_bound_xi_minus': lower_bound_xi_minus,\n", - " 'upper_bound_xi_minus': upper_bound_xi_minus\n", + " \"add_xi_sys\": add_xi_sys,\n", + " \"lower_bound_xi_plus\": lower_bound_xi_plus,\n", + " \"upper_bound_xi_plus\": upper_bound_xi_plus,\n", + " \"lower_bound_xi_minus\": lower_bound_xi_minus,\n", + " \"upper_bound_xi_minus\": upper_bound_xi_minus,\n", " }\n", "\n", "\n", @@ -107,16 +111,18 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/samples_{}.txt'.format('/'+root ,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(root_dir + \"{}/samples_{}.txt\".format(\"/\" + root, root), \"r\") as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/getdist_{}.paramnames'.format('/'+root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/getdist_{}.paramnames\".format(\"/\" + root, root), \"w\"\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -126,25 +132,26 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root,root))\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.3,\n", - " 'smooth_scale_1D':0.3})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.3, \"smooth_scale_1D\": 0.3},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -155,14 +162,39 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1','alpha','beta']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1', '\\\\alpha_{PSF}', '\\\\beta_{PSF}']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + " \"\\\\alpha_{PSF}\",\n", + " \"\\\\beta_{PSF}\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " for name, label in zip(name_list, label_list):\n", - " param_names.parWithName(name).label = label\n", - " " + " param_names.parWithName(name).label = label" ] }, { @@ -186,12 +218,11 @@ " bestfit_idx = np.argmax(chain.loglikes)\n", " maxlike = chain.loglikes[bestfit_idx]\n", " print(f\"Maximum Likelihood: {maxlike:.5g}\")\n", - " best_fit[root] = {\n", - " 'likelihood': maxlike\n", - " }\n", + " best_fit[root] = {\"likelihood\": maxlike}\n", " for i, par in enumerate(likestats.names):\n", - " best_fit[root].update({par.name: np.average(chain.samples[:, i], weights=chain.weights)})\n", - " " + " best_fit[root].update(\n", + " {par.name: np.average(chain.samples[:, i], weights=chain.weights)}\n", + " )" ] }, { @@ -207,7 +238,7 @@ "metadata": {}, "outputs": [], "source": [ - "if not os.path.exists(path_ini_files+'/values_empty.ini'):\n", + "if not os.path.exists(path_ini_files + \"/values_empty.ini\"):\n", " content = \"\"\"[cosmological_parameters]\n", "\n", "tau = 0.0544\n", @@ -228,11 +259,11 @@ "[psf_leakage_parameters]\n", "\"\"\"\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as f:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as f:\n", " f.write(content)\n", " f.close()\n", "\n", - " print('File created successfully')" + " print(\"File created successfully\")" ] }, { @@ -242,17 +273,17 @@ "outputs": [], "source": [ "section_map = {\n", - " 'omch2': 'cosmological_parameters',\n", - " 'ombh2': 'cosmological_parameters',\n", - " 'h0': 'cosmological_parameters',\n", - " 'n_s': 'cosmological_parameters',\n", - " 's_8_input': 'cosmological_parameters',\n", - " 'logt_agn': 'halo_model_parameters',\n", - " 'a': 'intrinsic_alignment_parameters',\n", - " 'm1': 'shear_calibration_parameters',\n", - " 'bias_1': 'nofz_shifts',\n", - " 'alpha': 'psf_leakage_parameters',\n", - " 'beta': 'psf_leakage_parameters',\n", + " \"omch2\": \"cosmological_parameters\",\n", + " \"ombh2\": \"cosmological_parameters\",\n", + " \"h0\": \"cosmological_parameters\",\n", + " \"n_s\": \"cosmological_parameters\",\n", + " \"s_8_input\": \"cosmological_parameters\",\n", + " \"logt_agn\": \"halo_model_parameters\",\n", + " \"a\": \"intrinsic_alignment_parameters\",\n", + " \"m1\": \"shear_calibration_parameters\",\n", + " \"bias_1\": \"nofz_shifts\",\n", + " \"alpha\": \"psf_leakage_parameters\",\n", + " \"beta\": \"psf_leakage_parameters\",\n", "}" ] }, @@ -263,13 +294,16 @@ "outputs": [], "source": [ "env = os.environ.copy()\n", - "env[\"LD_LIBRARY_PATH\"] = \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\" + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + "env[\"LD_LIBRARY_PATH\"] = (\n", + " \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\"\n", + " + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + ")\n", "\n", "for root in roots:\n", " print(root)\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+'/values_empty.ini')\n", + " config.read(path_ini_files + \"/values_empty.ini\")\n", " for param, value in best_fit[root].items():\n", " section = section_map.get(param)\n", " if section is None:\n", @@ -278,37 +312,37 @@ " config.add_section(section)\n", " config[section][param] = str(value)\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as configfile:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Modify the ini file to run in test mode at the best fit\n", + " # Modify the ini file to run in test mode at the best fit\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+f'/cosmosis_pipeline_{root}.ini')\n", + " config.read(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\")\n", "\n", - " sampler = config['runtime']['sampler']\n", - " config['runtime']['sampler'] = 'test'\n", - " values = config['pipeline']['values']\n", - " config['pipeline']['values'] = path_ini_files + '/values_empty.ini'\n", + " sampler = config[\"runtime\"][\"sampler\"]\n", + " config[\"runtime\"][\"sampler\"] = \"test\"\n", + " values = config[\"pipeline\"][\"values\"]\n", + " config[\"pipeline\"][\"values\"] = path_ini_files + \"/values_empty.ini\"\n", "\n", - " with open(path_ini_files+f'/cosmosis_pipeline_{root}.ini', 'w') as configfile:\n", + " with open(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\", \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Run cosmosis\n", + " # Run cosmosis\n", " result = subprocess.run(\n", - " ['cosmosis', 'cosmosis_config/cosmosis_pipeline_{}.ini'.format(root)],\n", + " [\"cosmosis\", \"cosmosis_config/cosmosis_pipeline_{}.ini\".format(root)],\n", " env=env,\n", " capture_output=True,\n", - " text=True\n", + " text=True,\n", " )\n", " print(f\"STDOUT:\\n{result.stdout}\")\n", " print(f\"STDERR:\\n{result.stderr}\")\n", "\n", - " #Modify the ini file to the previous one\n", - " config['pipeline']['values'] = values\n", - " config['runtime']['sampler'] = sampler\n", + " # Modify the ini file to the previous one\n", + " config[\"pipeline\"][\"values\"] = values\n", + " config[\"runtime\"][\"sampler\"] = sampler\n", "\n", - " with open(path_ini_files+f'/cosmosis_pipeline_{root}.ini', 'w') as configfile:\n", + " with open(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\", \"w\") as configfile:\n", " config.write(configfile)" ] }, @@ -325,50 +359,70 @@ "metadata": {}, "outputs": [], "source": [ - "output_folder = '/n09data/guerrini/output_chains/'\n", + "output_folder = \"/n09data/guerrini/output_chains/\"\n", "\n", "metrics = {}\n", "\n", "for root in roots:\n", " print(root)\n", "\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", "\n", - " #Read model tau_stats\n", - " theta_tau = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/theta.txt'.format(root))\n", + " # Read model tau_stats\n", + " theta_tau = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/theta.txt\".format(root)\n", + " )\n", " theta_tau_arcmin = theta_tau * 180 * 60 / np.pi\n", - " tau_0_model = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/bin_1_1.txt'.format(root))\n", - " tau_2_model = np.loadtxt(output_folder + 'best_fit/{}/tau_2_plus/bin_1_1.txt'.format(root))\n", + " tau_0_model = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " tau_2_model = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_2_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", - " #Read the data\n", - " data = fits.open(f'data/{catalog_version}/cosmosis_{catalog_version}.fits')\n", + " # Read the data\n", + " data = fits.open(f\"data/{catalog_version}/cosmosis_{catalog_version}.fits\")\n", "\n", - " theta_data = data['XI_PLUS'].data['ANG']\n", - " xi_plus_data = data['XI_PLUS'].data['VALUE']\n", - " xi_minus_data = data['XI_MINUS'].data['VALUE']\n", - " tau_0_data = data['TAU_0_PLUS'].data['VALUE']\n", - " tau_2_data = data['TAU_2_PLUS'].data['VALUE']\n", + " theta_data = data[\"XI_PLUS\"].data[\"ANG\"]\n", + " xi_plus_data = data[\"XI_PLUS\"].data[\"VALUE\"]\n", + " xi_minus_data = data[\"XI_MINUS\"].data[\"VALUE\"]\n", + " tau_0_data = data[\"TAU_0_PLUS\"].data[\"VALUE\"]\n", + " tau_2_data = data[\"TAU_2_PLUS\"].data[\"VALUE\"]\n", "\n", - " #Load the covariance\n", - " cov = data['COVMAT'].data\n", - " cov_xi = cov[0:2*len(xi_plus_data), 0:2*len(xi_plus_data)]\n", - " cov_tau = cov[2*len(xi_plus_data):, 2*len(xi_plus_data):]\n", + " # Load the covariance\n", + " cov = data[\"COVMAT\"].data\n", + " cov_xi = cov[0 : 2 * len(xi_plus_data), 0 : 2 * len(xi_plus_data)]\n", + " cov_tau = cov[2 * len(xi_plus_data) :, 2 * len(xi_plus_data) :]\n", "\n", - " #interpolate the model\n", - " interp_xi_plus = interp1d(theta_arcmin, shear_xi_plus, kind='cubic', fill_value='extrapolate')\n", - " interp_xi_minus = interp1d(theta_arcmin, shear_xi_minus, kind='cubic', fill_value='extrapolate')\n", + " # interpolate the model\n", + " interp_xi_plus = interp1d(\n", + " theta_arcmin, shear_xi_plus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", + " interp_xi_minus = interp1d(\n", + " theta_arcmin, shear_xi_minus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", "\n", " xi_plus_model = interp_xi_plus(theta_data)\n", " if add_xi_sys:\n", @@ -377,25 +431,32 @@ " if add_xi_sys:\n", " xi_minus_model += xi_sys_minus\n", "\n", - " #Concatenate the data vector\n", + " # Concatenate the data vector\n", " xi_data = np.concatenate((xi_plus_data, xi_minus_data))\n", " xi_model = np.concatenate((xi_plus_model, xi_minus_model))\n", "\n", " tau_data = np.concatenate((tau_0_data, tau_2_data))\n", " tau_model = np.concatenate((tau_0_model, tau_2_model))\n", "\n", - " #Apply scale cuts\n", - " mask_xi_plus = (theta_data > lower_bound_xi_plus) & (theta_data < upper_bound_xi_plus)\n", - " mask_xi_minus = (theta_data > lower_bound_xi_minus) & (theta_data < upper_bound_xi_minus)\n", + " # Apply scale cuts\n", + " mask_xi_plus = (theta_data > lower_bound_xi_plus) & (\n", + " theta_data < upper_bound_xi_plus\n", + " )\n", + " mask_xi_minus = (theta_data > lower_bound_xi_minus) & (\n", + " theta_data < upper_bound_xi_minus\n", + " )\n", " mask = np.concatenate((mask_xi_plus, mask_xi_minus))\n", "\n", " xi_data = xi_data[mask]\n", " xi_model = xi_model[mask]\n", " cov_xi = cov_xi[mask][:, mask]\n", "\n", - "\n", - " xi_plus_chi2 = np.dot((xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data)))\n", - " tau_chi2 = np.dot((tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data)))\n", + " xi_plus_chi2 = np.dot(\n", + " (xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data))\n", + " )\n", + " tau_chi2 = np.dot(\n", + " (tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data))\n", + " )\n", " n_dof_xi = np.sum(mask)\n", " n_dof_tau = len(tau_0_data) + len(tau_2_data)\n", " p_value_xi = 1 - stats.chi2.cdf(xi_plus_chi2, n_dof_xi)\n", @@ -405,15 +466,15 @@ " p_value_tot = 1 - stats.chi2.cdf(chi2_tot, n_dof_tot)\n", "\n", " metrics[root] = {\n", - " 'chi2_xi': xi_plus_chi2,\n", - " 'n_dof_xi': n_dof_xi,\n", - " 'p_value_xi': p_value_xi,\n", - " 'chi2_tau': tau_chi2,\n", - " 'n_dof_tau': n_dof_tau,\n", - " 'p_value_tau': p_value_tau,\n", - " 'chi2_tot': chi2_tot,\n", - " 'n_dof_tot': n_dof_tot,\n", - " 'p_value_tot': p_value_tot\n", + " \"chi2_xi\": xi_plus_chi2,\n", + " \"n_dof_xi\": n_dof_xi,\n", + " \"p_value_xi\": p_value_xi,\n", + " \"chi2_tau\": tau_chi2,\n", + " \"n_dof_tau\": n_dof_tau,\n", + " \"p_value_tau\": p_value_tau,\n", + " \"chi2_tot\": chi2_tot,\n", + " \"n_dof_tot\": n_dof_tot,\n", + " \"p_value_tot\": p_value_tot,\n", " }\n", " print(\"Done!\")" ] @@ -430,7 +491,7 @@ " r\"\\hline\",\n", " r\"Root & $\\chi^2_{\\xi^+}$/dof & $p_{\\xi^+}$ & \"\n", " r\"$\\chi^2_\\tau$/dof & $p_\\tau$ & $\\chi^2_{\\text{tot}}$/dof & $p_{\\text{tot}}$ \\\\\",\n", - " r\"\\hline\"\n", + " r\"\\hline\",\n", " ]\n", "\n", " for root, vals in metrics.items():\n", @@ -468,7 +529,7 @@ "def display_markdown(metrics):\n", " # Build Markdown table\n", " header = (\n", - " \"| Root | $\\chi^2$ (ξ⁺) / dof | p-val (ξ⁺) | $\\chi^2$ (τ) / dof | p-val (τ) | $\\chi^2$ (tot) / dof | p-val (tot) |\\n\"\n", + " \"| Root | $\\\\chi^2$ (ξ⁺) / dof | p-val (ξ⁺) | $\\\\chi^2$ (τ) / dof | p-val (τ) | $\\\\chi^2$ (tot) / dof | p-val (tot) |\\n\"\n", " \"|------|----------------|------------|---------------|------------|------------------|--------------|\\n\"\n", " )\n", "\n", @@ -519,10 +580,12 @@ "metadata": {}, "outputs": [], "source": [ - "data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1.fits')\n", - "xi_plus = data['XI_PLUS'].data\n", - "xi_minus = data['XI_MINUS'].data\n", - "cov_mat = data['COVMAT'].data" + "data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1.fits\"\n", + ")\n", + "xi_plus = data[\"XI_PLUS\"].data\n", + "xi_minus = data[\"XI_MINUS\"].data\n", + "cov_mat = data[\"COVMAT\"].data" ] }, { @@ -535,20 +598,36 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black', markersize=2)\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + ")\n", "\n", - "plt.ylabel(r'$\\xi_{+}$', fontsize=26)\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", + "plt.ylabel(r\"$\\xi_{+}$\", fontsize=26)\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black', markersize=2)\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + ")\n", "\n", - "plt.xlabel(r'$\\theta$ [arcmin]', fontsize=26)\n", - "plt.ylabel(r'$\\xi_{-}$', fontsize=26)\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\", fontsize=26)\n", + "plt.ylabel(r\"$\\xi_{-}$\", fontsize=26)\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", "plt.legend(fontsize=15)\n", "\n", "plt.show()" @@ -560,237 +639,411 @@ "metadata": {}, "outputs": [], "source": [ - "def plot_best_fit(root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0, multiply_theta=False, plot_xi_sys=True):\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits')\n", - " xi_plus = data['XI_PLUS'].data\n", - " xi_minus = data['XI_MINUS'].data\n", - " cov_mat = data['COVMAT'].data\n", + "def plot_best_fit(\n", + " root_to_plot,\n", + " colours,\n", + " savefile,\n", + " theta_min=1.0,\n", + " theta_max=250.0,\n", + " multiply_theta=False,\n", + " plot_xi_sys=True,\n", + "):\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits\"\n", + " )\n", + " xi_plus = data[\"XI_PLUS\"].data\n", + " xi_minus = data[\"XI_MINUS\"].data\n", + " cov_mat = data[\"COVMAT\"].data\n", "\n", " plt.figure(figsize=(15, 15))\n", "\n", " plt.subplot(211)\n", "\n", - " y_plot_xi_plus = xi_plus['VALUE'] if not multiply_theta else xi_plus['ANG'] * xi_plus['VALUE']\n", - " y_errorbar = xi_plus['ANG'] * np.sqrt(np.diag(cov_mat))[:20] if multiply_theta else np.sqrt(np.diag(cov_mat))[:20]\n", - " plt.errorbar(xi_plus['ANG'], y_plot_xi_plus, yerr=y_errorbar, fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " y_plot_xi_plus = (\n", + " xi_plus[\"VALUE\"] if not multiply_theta else xi_plus[\"ANG\"] * xi_plus[\"VALUE\"]\n", + " )\n", + " y_errorbar = (\n", + " xi_plus[\"ANG\"] * np.sqrt(np.diag(cov_mat))[:20]\n", + " if multiply_theta\n", + " else np.sqrt(np.diag(cov_mat))[:20]\n", + " )\n", + " plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " y_plot_xi_plus,\n", + " yerr=y_errorbar,\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_plus_model = shear_xi_plus[mask]\n", " if add_xi_sys:\n", - " xi_plus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus)\n", + " xi_plus_model += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus\n", + " )\n", "\n", " y_plot = theta_arcmin[mask] * xi_plus_model if multiply_theta else xi_plus_model\n", " plt.plot(theta_arcmin[mask], y_plot, color=color, label=root, alpha=0.5)\n", " if plot_xi_sys and add_xi_sys:\n", - " y_plot_xi_sys = theta_xi_sys_arcmin * xi_sys_plus if multiply_theta else xi_sys_plus\n", - " plt.plot(theta_xi_sys_arcmin, y_plot_xi_sys, color=color, linestyle='-.', alpha=0.5)\n", - " plt.axvline(x=lower_bound_xi_plus, color=color, linestyle='--', alpha=0.3)\n", - " plt.axvline(x=upper_bound_xi_plus, color=color, linestyle='--', alpha=0.3)\n", - " \n", - "\n", - " y_label = r'$\\xi_{+}$' if not multiply_theta else r'$\\theta \\xi_{+}$'\n", + " y_plot_xi_sys = (\n", + " theta_xi_sys_arcmin * xi_sys_plus if multiply_theta else xi_sys_plus\n", + " )\n", + " plt.plot(\n", + " theta_xi_sys_arcmin,\n", + " y_plot_xi_sys,\n", + " color=color,\n", + " linestyle=\"-.\",\n", + " alpha=0.5,\n", + " )\n", + " plt.axvline(x=lower_bound_xi_plus, color=color, linestyle=\"--\", alpha=0.3)\n", + " plt.axvline(x=upper_bound_xi_plus, color=color, linestyle=\"--\", alpha=0.3)\n", + "\n", + " y_label = r\"$\\xi_{+}$\" if not multiply_theta else r\"$\\theta \\xi_{+}$\"\n", " plt.ylabel(y_label, fontsize=26)\n", - " plt.xscale('log')\n", - " plt.yscale('log')\n", + " plt.xscale(\"log\")\n", + " plt.yscale(\"log\")\n", " plt.legend(loc=\"lower left\", fontsize=8)\n", "\n", " plt.subplot(212)\n", "\n", - " y_plot_xi_minus = xi_minus['VALUE'] if not multiply_theta else xi_minus['ANG'] * xi_minus['VALUE']\n", - " y_errorbar = xi_minus['ANG'] * np.sqrt(np.diag(cov_mat))[20:40] if multiply_theta else np.sqrt(np.diag(cov_mat))[20:40]\n", - " plt.errorbar(xi_minus['ANG'], y_plot_xi_minus, yerr=y_errorbar, fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " y_plot_xi_minus = (\n", + " xi_minus[\"VALUE\"] if not multiply_theta else xi_minus[\"ANG\"] * xi_minus[\"VALUE\"]\n", + " )\n", + " y_errorbar = (\n", + " xi_minus[\"ANG\"] * np.sqrt(np.diag(cov_mat))[20:40]\n", + " if multiply_theta\n", + " else np.sqrt(np.diag(cov_mat))[20:40]\n", + " )\n", + " plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " y_plot_xi_minus,\n", + " yerr=y_errorbar,\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_minus_model = shear_xi_minus[mask]\n", " if add_xi_sys:\n", - " xi_minus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus)\n", + " xi_minus_model += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus\n", + " )\n", "\n", - " y_plot = theta_arcmin[mask] * xi_minus_model if multiply_theta else xi_minus_model\n", + " y_plot = (\n", + " theta_arcmin[mask] * xi_minus_model if multiply_theta else xi_minus_model\n", + " )\n", " plt.plot(theta_arcmin[mask], y_plot, color=color, label=root, alpha=0.5)\n", " if plot_xi_sys and add_xi_sys:\n", - " y_plot_xi_sys = theta_xi_sys_arcmin * xi_sys_minus if multiply_theta else xi_sys_minus\n", - " plt.plot(theta_xi_sys_arcmin, y_plot_xi_sys, color=color, linestyle='-.', alpha=0.5)\n", - " plt.axvline(x=lower_bound_xi_minus, color=color, linestyle='--', alpha=0.3)\n", - " plt.axvline(x=upper_bound_xi_minus, color=color, linestyle='--', alpha=0.3)\n", - "\n", - "\n", - " plt.xlabel(r'$\\theta$ [arcmin]', fontsize=26)\n", - " y_label = r'$\\xi_{-}$' if not multiply_theta else r'$\\theta \\xi_{-}$'\n", + " y_plot_xi_sys = (\n", + " theta_xi_sys_arcmin * xi_sys_minus if multiply_theta else xi_sys_minus\n", + " )\n", + " plt.plot(\n", + " theta_xi_sys_arcmin,\n", + " y_plot_xi_sys,\n", + " color=color,\n", + " linestyle=\"-.\",\n", + " alpha=0.5,\n", + " )\n", + " plt.axvline(x=lower_bound_xi_minus, color=color, linestyle=\"--\", alpha=0.3)\n", + " plt.axvline(x=upper_bound_xi_minus, color=color, linestyle=\"--\", alpha=0.3)\n", + "\n", + " plt.xlabel(r\"$\\theta$ [arcmin]\", fontsize=26)\n", + " y_label = r\"$\\xi_{-}$\" if not multiply_theta else r\"$\\theta \\xi_{-}$\"\n", " plt.ylabel(y_label, fontsize=26)\n", - " plt.xscale('log')\n", - " plt.yscale('log')\n", + " plt.xscale(\"log\")\n", + " plt.yscale(\"log\")\n", " plt.legend(loc=\"lower left\", fontsize=8)\n", "\n", " if savefile is not None:\n", - " plt.savefig(savefile, bbox_inches='tight')\n", + " plt.savefig(savefile, bbox_inches=\"tight\")\n", "\n", " plt.show()\n", "\n", - "def plot_best_fit_ratio(root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0):\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits')\n", - " xi_plus = data['XI_PLUS'].data\n", - " xi_minus = data['XI_MINUS'].data\n", - " cov_mat = data['COVMAT'].data\n", + "\n", + "def plot_best_fit_ratio(\n", + " root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0\n", + "):\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits\"\n", + " )\n", + " xi_plus = data[\"XI_PLUS\"].data\n", + " xi_minus = data[\"XI_MINUS\"].data\n", + " cov_mat = data[\"COVMAT\"].data\n", "\n", " plt.figure(figsize=(15, 15))\n", "\n", " plt.subplot(211)\n", "\n", " root = roots[0]\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_plus_model_fiducial = shear_xi_plus[mask]\n", " if add_xi_sys:\n", - " xi_plus_model_fiducial += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus)\n", + " xi_plus_model_fiducial += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus\n", + " )\n", "\n", - " plt.errorbar(xi_plus['ANG'], xi_plus['VALUE']/np.interp(xi_plus['ANG'], theta_arcmin[mask], xi_plus_model_fiducial), yerr=np.sqrt(np.diag(cov_mat))[:20]/np.abs(np.interp(xi_plus['ANG'], theta_arcmin[mask], xi_plus_model_fiducial)), fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"]\n", + " / np.interp(xi_plus[\"ANG\"], theta_arcmin[mask], xi_plus_model_fiducial),\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20]\n", + " / np.abs(np.interp(xi_plus[\"ANG\"], theta_arcmin[mask], xi_plus_model_fiducial)),\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_plus_model = shear_xi_plus[mask]\n", " if add_xi_sys:\n", - " xi_plus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus)\n", - " \n", - " alpha = 1.0 if root==roots[0] else 0.5\n", - " plt.plot(theta_arcmin[mask], xi_plus_model/xi_plus_model_fiducial, color=color, label=root, alpha=alpha)\n", - " plt.axvline(x=lower_bound_xi_plus, color=color, linestyle='--', alpha=0.3)\n", - " plt.axvline(x=upper_bound_xi_plus, color=color, linestyle='--', alpha=0.3)\n", - " \n", - "\n", - "\n", - " plt.ylabel(fr'$\\xi_{{+}}/\\xi_{{+, \\text{{fid}}}}$', fontsize=26)\n", - " plt.xscale('log')\n", - " #plt.yscale('log')\n", + " xi_plus_model += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus\n", + " )\n", + "\n", + " alpha = 1.0 if root == roots[0] else 0.5\n", + " plt.plot(\n", + " theta_arcmin[mask],\n", + " xi_plus_model / xi_plus_model_fiducial,\n", + " color=color,\n", + " label=root,\n", + " alpha=alpha,\n", + " )\n", + " plt.axvline(x=lower_bound_xi_plus, color=color, linestyle=\"--\", alpha=0.3)\n", + " plt.axvline(x=upper_bound_xi_plus, color=color, linestyle=\"--\", alpha=0.3)\n", + "\n", + " plt.ylabel(r\"$\\xi_{+}/\\xi_{+, \\text{fid}}$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"lower left\", fontsize=8)\n", "\n", " plt.subplot(212)\n", "\n", " root = roots[0]\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_minus_model_fiducial = shear_xi_minus[mask]\n", " if add_xi_sys:\n", - " xi_minus_model_fiducial += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus)\n", + " xi_minus_model_fiducial += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus\n", + " )\n", "\n", - " plt.errorbar(xi_minus['ANG'], xi_minus['VALUE']/np.interp(xi_minus['ANG'], theta_arcmin[mask], xi_minus_model_fiducial), yerr=np.sqrt(np.diag(cov_mat))[20:40]/np.abs(np.interp(xi_minus['ANG'], theta_arcmin[mask], xi_minus_model_fiducial)), fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"]\n", + " / np.interp(xi_minus[\"ANG\"], theta_arcmin[mask], xi_minus_model_fiducial),\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40]\n", + " / np.abs(\n", + " np.interp(xi_minus[\"ANG\"], theta_arcmin[mask], xi_minus_model_fiducial)\n", + " ),\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_minus_model = shear_xi_minus[mask]\n", " if add_xi_sys:\n", - " xi_minus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus)\n", - "\n", - " alpha = 1.0 if root==roots[0] else 0.5\n", - " plt.plot(theta_arcmin[mask], xi_minus_model/xi_minus_model_fiducial, color=color, label=root, alpha=alpha)\n", - " plt.axvline(x=lower_bound_xi_minus, color=color, linestyle='--', alpha=0.3)\n", - " plt.axvline(x=upper_bound_xi_minus, color=color, linestyle='--', alpha=0.3)\n", - "\n", + " xi_minus_model += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus\n", + " )\n", + "\n", + " alpha = 1.0 if root == roots[0] else 0.5\n", + " plt.plot(\n", + " theta_arcmin[mask],\n", + " xi_minus_model / xi_minus_model_fiducial,\n", + " color=color,\n", + " label=root,\n", + " alpha=alpha,\n", + " )\n", + " plt.axvline(x=lower_bound_xi_minus, color=color, linestyle=\"--\", alpha=0.3)\n", + " plt.axvline(x=upper_bound_xi_minus, color=color, linestyle=\"--\", alpha=0.3)\n", "\n", - " plt.xlabel(r'$\\theta$ [arcmin]', fontsize=26)\n", - " plt.ylabel(fr'$\\xi_{{-}}/\\xi_{{-, \\text{{fid}}}}$', fontsize=26)\n", - " plt.xscale('log')\n", + " plt.xlabel(r\"$\\theta$ [arcmin]\", fontsize=26)\n", + " plt.ylabel(r\"$\\xi_{-}/\\xi_{-, \\text{fid}}$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", " plt.ylim(0, 2)\n", - " #plt.yscale('log')\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"lower left\", fontsize=8)\n", "\n", " if savefile is not None:\n", - " plt.savefig(savefile, bbox_inches='tight')\n", + " plt.savefig(savefile, bbox_inches=\"tight\")\n", "\n", " plt.show()" ] @@ -803,20 +1056,21 @@ "source": [ "root_to_plot = [\n", " \"SP_v1.4.5_A\",\n", - " #\"SP_v1.4.5_A_no_IA\",\n", - " #\"SP_v1.4.5_A_no_dz\",\n", - " #\"SP_v1.4.5_A_no_m_bias\",\n", + " # \"SP_v1.4.5_A_no_IA\",\n", + " # \"SP_v1.4.5_A_no_dz\",\n", + " # \"SP_v1.4.5_A_no_m_bias\",\n", " \"SP_v1.4.5_A_sc_3_150\",\n", " \"SP_v1.4.5_A_sc_3_60\",\n", " \"SP_v1.4.5_A_sc_10_150\",\n", " \"SP_v1.4.5_A_sc_10_60\",\n", " \"SP_v1.4.5_A_sc_5_150\",\n", " \"SP_v1.4.5_A_sc_7_150\",\n", - " #\"SP_v1.4.5_A_no_leakage\"\n", + " # \"SP_v1.4.5_A_no_leakage\"\n", "]\n", "\n", "root_to_plot = [\n", - " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\" for i in [3, 5, 7, 10, 11]\n", + " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\"\n", + " for i in [3, 5, 7, 10, 11]\n", "]\n", "\n", "\"\"\" root_to_plot = [\n", @@ -826,7 +1080,22 @@ "\n", "\n", "colours = [\n", - " 'cornflowerblue', 'salmon', 'darkorange', 'forestgreen', 'turquoise', 'darkviolet', 'crimson', 'gold', 'lightcoral', 'mediumseagreen', 'lightsteelblue', 'black', 'silver', 'peru', 'maroon', 'olive'\n", + " \"cornflowerblue\",\n", + " \"salmon\",\n", + " \"darkorange\",\n", + " \"forestgreen\",\n", + " \"turquoise\",\n", + " \"darkviolet\",\n", + " \"crimson\",\n", + " \"gold\",\n", + " \"lightcoral\",\n", + " \"mediumseagreen\",\n", + " \"lightsteelblue\",\n", + " \"black\",\n", + " \"silver\",\n", + " \"peru\",\n", + " \"maroon\",\n", + " \"olive\",\n", "]\n", "\n", "savefile = None\n", @@ -842,16 +1111,16 @@ "source": [ "root_to_plot = [\n", " \"SP_v1.4.5_A\",\n", - " #\"SP_v1.4.5_A_no_IA\",\n", - " #\"SP_v1.4.5_A_no_dz\",\n", - " #\"SP_v1.4.5_A_no_m_bias\",\n", + " # \"SP_v1.4.5_A_no_IA\",\n", + " # \"SP_v1.4.5_A_no_dz\",\n", + " # \"SP_v1.4.5_A_no_m_bias\",\n", " \"SP_v1.4.5_A_sc_3_150\",\n", " \"SP_v1.4.5_A_sc_3_60\",\n", " \"SP_v1.4.5_A_sc_10_150\",\n", " \"SP_v1.4.5_A_sc_10_60\",\n", " \"SP_v1.4.5_A_sc_5_150\",\n", " \"SP_v1.4.5_A_sc_7_150\",\n", - " #\"SP_v1.4.5_A_no_leakage\"\n", + " # \"SP_v1.4.5_A_no_leakage\"\n", "]\n", "\n", "\"\"\" root_to_plot = [\n", @@ -860,15 +1129,30 @@ "\n", "root_to_plot = [\n", " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0\",\n", - " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0_no_alpha_beta\"\n", + " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0_no_alpha_beta\",\n", "]\n", "\n", "\n", "colours = [\n", - " 'red', 'salmon', 'darkorange', 'forestgreen', 'turquoise', 'darkviolet', 'crimson', 'gold', 'lightcoral', 'mediumseagreen', 'lightsteelblue', 'black', 'silver', 'peru', 'maroon', 'olive'\n", + " \"red\",\n", + " \"salmon\",\n", + " \"darkorange\",\n", + " \"forestgreen\",\n", + " \"turquoise\",\n", + " \"darkviolet\",\n", + " \"crimson\",\n", + " \"gold\",\n", + " \"lightcoral\",\n", + " \"mediumseagreen\",\n", + " \"lightsteelblue\",\n", + " \"black\",\n", + " \"silver\",\n", + " \"peru\",\n", + " \"maroon\",\n", + " \"olive\",\n", "]\n", "\n", - "savefile = 'best_fit_ratio_w_wo_leakage.png'\n", + "savefile = \"best_fit_ratio_w_wo_leakage.png\"\n", "\n", "plot_best_fit_ratio(root_to_plot, colours, savefile)" ] @@ -880,59 +1164,90 @@ "outputs": [], "source": [ "def plot_best_fit_tau(root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0):\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits')\n", - " tau_0 = data['TAU_0_PLUS'].data\n", - " tau_2 = data['TAU_2_PLUS'].data\n", - " cov_mat = data['COVMAT'].data\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits\"\n", + " )\n", + " tau_0 = data[\"TAU_0_PLUS\"].data\n", + " tau_2 = data[\"TAU_2_PLUS\"].data\n", + " cov_mat = data[\"COVMAT\"].data\n", "\n", " plt.figure(figsize=(15, 15))\n", "\n", " plt.subplot(211)\n", "\n", - " plt.errorbar(tau_0['ANG'], tau_0['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[40:60], fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " plt.errorbar(\n", + " tau_0[\"ANG\"],\n", + " tau_0[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[40:60],\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/theta.txt'.format(root))\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " tau_0_plus = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/bin_1_1.txt'.format(root))\n", - "\n", + " tau_0_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", - " \n", - " plt.plot(theta_arcmin[mask], tau_0_plus[mask], color=color, label=root, alpha=0.5)\n", - " \n", - " plt.ylabel(r'$\\tau_0$', fontsize=26)\n", - " plt.xscale('log')\n", - " #plt.yscale('log')\n", + "\n", + " plt.plot(\n", + " theta_arcmin[mask], tau_0_plus[mask], color=color, label=root, alpha=0.5\n", + " )\n", + "\n", + " plt.ylabel(r\"$\\tau_0$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"upper right\", fontsize=8)\n", "\n", " plt.subplot(212)\n", "\n", - " y_plot_tau_2 = tau_2['ANG'] * tau_2['VALUE']\n", - " y_errorbar = tau_2['ANG'] * np.sqrt(np.diag(cov_mat))[60:80]\n", - " plt.errorbar(tau_2['ANG'], y_plot_tau_2, yerr=y_errorbar, fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " y_plot_tau_2 = tau_2[\"ANG\"] * tau_2[\"VALUE\"]\n", + " y_errorbar = tau_2[\"ANG\"] * np.sqrt(np.diag(cov_mat))[60:80]\n", + " plt.errorbar(\n", + " tau_2[\"ANG\"],\n", + " y_plot_tau_2,\n", + " yerr=y_errorbar,\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/tau_2_plus/theta.txt'.format(root))\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_2_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " tau_2_plus = np.loadtxt(output_folder + 'best_fit/{}/tau_2_plus/bin_1_1.txt'.format(root))\n", + " tau_2_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_2_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", - " \n", - " plt.plot(theta_arcmin[mask], theta_arcmin[mask]*tau_2_plus[mask], color=color, label=root, alpha=0.5)\n", "\n", + " plt.plot(\n", + " theta_arcmin[mask],\n", + " theta_arcmin[mask] * tau_2_plus[mask],\n", + " color=color,\n", + " label=root,\n", + " alpha=0.5,\n", + " )\n", "\n", - " plt.xlabel(r'$\\theta$ [arcmin]', fontsize=26)\n", - " plt.ylabel(r'$\\theta \\tau_2$', fontsize=26)\n", - " plt.xscale('log')\n", - " #plt.yscale('log')\n", + " plt.xlabel(r\"$\\theta$ [arcmin]\", fontsize=26)\n", + " plt.ylabel(r\"$\\theta \\tau_2$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"upper left\", fontsize=8)\n", "\n", " if savefile is not None:\n", - " plt.savefig(savefile, bbox_inches='tight')\n", + " plt.savefig(savefile, bbox_inches=\"tight\")\n", "\n", " plt.show()" ] @@ -945,27 +1260,43 @@ "source": [ "root_to_plot = [\n", " \"SP_v1.4.5_A\",\n", - " #\"SP_v1.4.5_A_no_IA\",\n", - " #\"SP_v1.4.5_A_no_dz\",\n", - " #\"SP_v1.4.5_A_no_m_bias\",\n", + " # \"SP_v1.4.5_A_no_IA\",\n", + " # \"SP_v1.4.5_A_no_dz\",\n", + " # \"SP_v1.4.5_A_no_m_bias\",\n", " \"SP_v1.4.5_A_sc_3_150\",\n", " \"SP_v1.4.5_A_sc_3_60\",\n", " \"SP_v1.4.5_A_sc_10_150\",\n", " \"SP_v1.4.5_A_sc_10_60\",\n", " \"SP_v1.4.5_A_sc_5_150\",\n", " \"SP_v1.4.5_A_sc_7_150\",\n", - " #\"SP_v1.4.5_A_no_leakage\"\n", + " # \"SP_v1.4.5_A_no_leakage\"\n", "]\n", "\n", "root_to_plot = [\n", - " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\" for i in [3, 5, 7, 10, 11]\n", + " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\"\n", + " for i in [3, 5, 7, 10, 11]\n", "]\n", "\n", "colours = [\n", - " 'red', 'salmon', 'darkorange', 'forestgreen', 'turquoise', 'darkviolet', 'crimson', 'gold', 'lightcoral', 'mediumseagreen', 'lightsteelblue', 'black', 'silver', 'peru', 'maroon', 'olive'\n", + " \"red\",\n", + " \"salmon\",\n", + " \"darkorange\",\n", + " \"forestgreen\",\n", + " \"turquoise\",\n", + " \"darkviolet\",\n", + " \"crimson\",\n", + " \"gold\",\n", + " \"lightcoral\",\n", + " \"mediumseagreen\",\n", + " \"lightsteelblue\",\n", + " \"black\",\n", + " \"silver\",\n", + " \"peru\",\n", + " \"maroon\",\n", + " \"olive\",\n", "]\n", "\n", - "savefile = 'best_fit_tau_new_binning.png'\n", + "savefile = \"best_fit_tau_new_binning.png\"\n", "\n", "plot_best_fit_tau(root_to_plot, colours, savefile)" ] @@ -976,21 +1307,44 @@ "metadata": {}, "outputs": [], "source": [ - "pseudo_cell = fits.open('/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.5.fits')[1].data\n", - "cov_pseudo_cell = fits.open('/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.5.fits')\n", + "pseudo_cell = fits.open(\n", + " \"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.5.fits\"\n", + ")[1].data\n", + "cov_pseudo_cell = fits.open(\n", + " \"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.5.fits\"\n", + ")\n", "\n", - "theory_ell = np.loadtxt('/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/ell.txt')\n", - "theory_cell = np.loadtxt('/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/bin_1_1.txt')\n", + "theory_ell = np.loadtxt(\n", + " \"/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/ell.txt\"\n", + ")\n", + "theory_cell = np.loadtxt(\n", + " \"/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/bin_1_1.txt\"\n", + ")\n", "\n", "pw = hp.pixwin(1024, lmax=2048)\n", "\n", - "plt.errorbar(pseudo_cell['ELL'], pseudo_cell['ELL']*pseudo_cell['EE'], yerr=pseudo_cell['ELL']*np.sqrt(np.diag(cov_pseudo_cell['COVAR_EE_EE'].data)), capsize=2, c='k', fmt='o', markersize=2)\n", + "plt.errorbar(\n", + " pseudo_cell[\"ELL\"],\n", + " pseudo_cell[\"ELL\"] * pseudo_cell[\"EE\"],\n", + " yerr=pseudo_cell[\"ELL\"] * np.sqrt(np.diag(cov_pseudo_cell[\"COVAR_EE_EE\"].data)),\n", + " capsize=2,\n", + " c=\"k\",\n", + " fmt=\"o\",\n", + " markersize=2,\n", + ")\n", "\n", "mask = (theory_ell > 0.1) & (theory_ell < 2048)\n", - "plt.plot(theory_ell[mask], theory_ell[mask]*theory_cell[mask]*np.interp(theory_ell[mask], np.arange(0, 2049), pw)**2, c='r', label='best-fit $\\\\theta \\in [3-200]$')\n", + "plt.plot(\n", + " theory_ell[mask],\n", + " theory_ell[mask]\n", + " * theory_cell[mask]\n", + " * np.interp(theory_ell[mask], np.arange(0, 2049), pw) ** 2,\n", + " c=\"r\",\n", + " label=\"best-fit $\\\\theta \\\\in [3-200]$\",\n", + ")\n", "\n", - "plt.xlabel(r'$\\ell$', fontsize=26)\n", - "plt.ylabel(r'$\\ell C_\\ell^{EE}$', fontsize=26)\n", + "plt.xlabel(r\"$\\ell$\", fontsize=26)\n", + "plt.ylabel(r\"$\\ell C_\\ell^{EE}$\", fontsize=26)\n", "plt.legend()\n", "plt.savefig(\"SP_v1.4.5_A_cell.png\")\n", "plt.show()" diff --git a/cosmo_inference/get_chi2_cell.ipynb b/cosmo_inference/get_chi2_cell.ipynb index 65949079..c97748d1 100644 --- a/cosmo_inference/get_chi2_cell.ipynb +++ b/cosmo_inference/get_chi2_cell.ipynb @@ -15,22 +15,20 @@ "import configparser\n", "import subprocess\n", "\n", - "from getdist import plots, loadMCSamples\n", - "from astropy.io import fits\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "from scipy.interpolate import interp1d\n", - "import scipy.stats as stats\n", - "from IPython.display import Markdown, display\n", "import healpy as hp\n", + "import matplotlib.pyplot as plt\n", "import matplotlib.scale as mscale\n", "import matplotlib.ticker as ticker\n", "import matplotlib.transforms as mtransforms\n", + "import numpy as np\n", + "import scipy.stats as stats\n", "import seaborn as sns\n", + "from astropy.io import fits\n", + "from getdist import plots\n", + "from IPython.display import Markdown, display\n", + "from scipy.interpolate import interp1d\n", "\n", - "plt.style.use(\n", - " \"../papers/harmonic/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"../papers/harmonic/matplotlib_config/paper.mplstyle\")\n", "\n", "plt.rcParams[\"text.usetex\"] = True\n", "\n", @@ -45,7 +43,7 @@ "\n", " \"\"\"\n", "\n", - " name = 'squareroot'\n", + " name = \"squareroot\"\n", "\n", " def __init__(self, axis, **kwargs):\n", " mscale.ScaleBase.__init__(self, axis, **kwargs)\n", @@ -57,7 +55,7 @@ " axis.set_minor_formatter(ticker.NullFormatter())\n", "\n", " def limit_range_for_scale(self, vmin, vmax, minpos):\n", - " return max(0., vmin), vmax\n", + " return max(0.0, vmin), vmax\n", "\n", " class SquareRootTransform(mtransforms.Transform):\n", " input_dims = 1\n", @@ -65,7 +63,7 @@ " is_separable = True\n", "\n", " def transform_non_affine(self, a):\n", - " return np.array(a)**0.5\n", + " return np.array(a) ** 0.5\n", "\n", " def inverted(self):\n", " return SquareRootScale.InvertedSquareRootTransform()\n", @@ -76,7 +74,7 @@ " is_separable = True\n", "\n", " def transform(self, a):\n", - " return np.array(a)**2\n", + " return np.array(a) ** 2\n", "\n", " def inverted(self):\n", " return SquareRootScale.SquareRootTransform()\n", @@ -84,60 +82,61 @@ " def get_transform(self):\n", " return self.SquareRootTransform()\n", "\n", + "\n", "mscale.register_scale(SquareRootScale)\n", "%matplotlib inline\n", "# import uncertainties\n", "\n", - "plt.rc('mathtext', fontset='stix')\n", - "plt.rc('font', family='sans-serif')\n", + "plt.rc(\"mathtext\", fontset=\"stix\")\n", + "plt.rc(\"font\", family=\"sans-serif\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", - "root_dir='/n09data/guerrini/output_chains/'\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "root_dir = \"/n09data/guerrini/output_chains/\"\n", "\n", - "catalog_version = 'SP_v1.4.6_leak_corr_cell'\n", - "catalog_version_real_space = 'SP_v1.4.6_leak_corr_A_10_80'\n", + "catalog_version = \"SP_v1.4.6_leak_corr_cell\"\n", + "catalog_version_real_space = \"SP_v1.4.6_leak_corr_A_10_80\"\n", "\n", - "path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/'\n", + "path_ini_files = \"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/\"\n", "\n", "roots = [\n", - " f\"SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell\",\n", - " f\"SP_v1.4.6_leak_corr_B_lmin=300_lmax=1600_cell\",\n", - " f\"SP_v1.4.6_leak_corr_C_lmin=300_lmax=1600_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_10_80\",\n", - " #f\"SP_v1.4.6_leak_corr_A_kmax=5Mpc_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_kmax=3Mpc_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_kmax=1Mpc_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_include_large_scales_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_small_scales_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_large_scales_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_halofit_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_HMCode_nobar_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_OneCov_cell\",\n", - " f\"SP_v1.4.6_A_fid_cell\"\n", + " \"SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell\",\n", + " \"SP_v1.4.6_leak_corr_B_lmin=300_lmax=1600_cell\",\n", + " \"SP_v1.4.6_leak_corr_C_lmin=300_lmax=1600_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_10_80\",\n", + " # f\"SP_v1.4.6_leak_corr_A_kmax=5Mpc_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_kmax=3Mpc_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_kmax=1Mpc_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_include_large_scales_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_small_scales_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_large_scales_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_halofit_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_HMCode_nobar_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_OneCov_cell\",\n", + " \"SP_v1.4.6_A_fid_cell\",\n", "]\n", "\n", "labels = [\n", - " rf\"UNIONS $C_\\ell$, Blind A\",\n", - " rf\"UNIONS $C_\\ell$, Blind B\",\n", - " rf\"UNIONS $C_\\ell$, Blind C\",\n", + " r\"UNIONS $C_\\ell$, Blind A\",\n", + " r\"UNIONS $C_\\ell$, Blind B\",\n", + " r\"UNIONS $C_\\ell$, Blind C\",\n", " r\"UNIONS $\\xi_\\pm(\\vartheta)$, (Goh et al., 2026)\",\n", - " #rf\"$k_\\mathrm{{max}}=5 h$ Mpc$^{{-1}}$, $\\ell_\\mathrm{{max}}=2048$\",\n", - " rf\"$k_\\mathrm{{max}}=3 h$ Mpc$^{{-1}}$, $\\ell_\\mathrm{{max}}=1800$\",\n", - " rf\"$k_\\mathrm{{max}}=1 h$ Mpc$^{{-1}}$, $\\ell_\\mathrm{{max}}=500$\",\n", + " # rf\"$k_\\mathrm{{max}}=5 h$ Mpc$^{{-1}}$, $\\ell_\\mathrm{{max}}=2048$\",\n", + " r\"$k_\\mathrm{max}=3 h$ Mpc$^{-1}$, $\\ell_\\mathrm{max}=1800$\",\n", + " r\"$k_\\mathrm{max}=1 h$ Mpc$^{-1}$, $\\ell_\\mathrm{max}=500$\",\n", " r\"Include Large Scales, $\\ell_\\mathrm{max}=1600$\",\n", - " f\"Small Scales only\",\n", - " f\"Large Scales only\",\n", + " \"Small Scales only\",\n", + " \"Large Scales only\",\n", " r\"Halofit\",\n", " r\"HMCode no baryons\",\n", - " f\"OneCovariance only\",\n", - " f\"No leakage correction\"\n", + " \"OneCovariance only\",\n", + " \"No leakage correction\",\n", "]\n", "\n", "bases = [\n", @@ -154,7 +153,7 @@ " \"harmonic\",\n", " \"harmonic\",\n", " \"harmonic\",\n", - " \"harmonic\"\n", + " \"harmonic\",\n", "]\n", "\n", "\n", @@ -163,35 +162,40 @@ "for i, root in enumerate(roots):\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+f'/cosmosis_pipeline_{root}.ini')\n", + " config.read(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\")\n", "\n", " try:\n", - " lower_bound_cell_ee, upper_bound_cell_ee = map(float, config[\"2pt_like\"][\"angle_range_CELL_EE_1_1\"].split())\n", - " \n", + " lower_bound_cell_ee, upper_bound_cell_ee = map(\n", + " float, config[\"2pt_like\"][\"angle_range_CELL_EE_1_1\"].split()\n", + " )\n", + "\n", " properties[root] = {\n", - " 'lower_bound_cell_ee': lower_bound_cell_ee,\n", - " 'upper_bound_cell_ee': upper_bound_cell_ee,\n", + " \"lower_bound_cell_ee\": lower_bound_cell_ee,\n", + " \"upper_bound_cell_ee\": upper_bound_cell_ee,\n", " }\n", " except KeyError:\n", - " properties[root] = {\n", - " 'lower_bound_cell_ee': 0.0,\n", - " 'upper_bound_cell_ee': 2048.0\n", - " }\n", + " properties[root] = {\"lower_bound_cell_ee\": 0.0, \"upper_bound_cell_ee\": 2048.0}\n", "\n", - " if bases[i] == 'configuration':\n", + " if bases[i] == \"configuration\":\n", " # Also save the scale cuts in theta for xi\n", " add_xi_sys = config[\"2pt_like\"][\"add_xi_sys\"]\n", - " add_xi_sys = add_xi_sys == 'T'\n", - " lower_bound_xi_plus, upper_bound_xi_plus = map(float, config[\"2pt_like\"][\"angle_range_XI_PLUS_1_1\"].split())\n", - " lower_bound_xi_minus, upper_bound_xi_minus = map(float, config[\"2pt_like\"][\"angle_range_XI_MINUS_1_1\"].split())\n", + " add_xi_sys = add_xi_sys == \"T\"\n", + " lower_bound_xi_plus, upper_bound_xi_plus = map(\n", + " float, config[\"2pt_like\"][\"angle_range_XI_PLUS_1_1\"].split()\n", + " )\n", + " lower_bound_xi_minus, upper_bound_xi_minus = map(\n", + " float, config[\"2pt_like\"][\"angle_range_XI_MINUS_1_1\"].split()\n", + " )\n", "\n", - " properties[root].update({\n", - " 'add_xi_sys': add_xi_sys,\n", - " 'lower_bound_xi_plus': lower_bound_xi_plus,\n", - " 'upper_bound_xi_plus': upper_bound_xi_plus,\n", - " 'lower_bound_xi_minus': lower_bound_xi_minus,\n", - " 'upper_bound_xi_minus': upper_bound_xi_minus\n", - " })\n", + " properties[root].update(\n", + " {\n", + " \"add_xi_sys\": add_xi_sys,\n", + " \"lower_bound_xi_plus\": lower_bound_xi_plus,\n", + " \"upper_bound_xi_plus\": upper_bound_xi_plus,\n", + " \"lower_bound_xi_minus\": lower_bound_xi_minus,\n", + " \"upper_bound_xi_minus\": upper_bound_xi_minus,\n", + " }\n", + " )\n", "\n", "\n", "print(roots)" @@ -213,16 +217,18 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/samples_{}.txt'.format('/'+root ,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(root_dir + \"{}/samples_{}.txt\".format(\"/\" + root, root), \"r\") as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/getdist_{}.paramnames'.format('/'+root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/getdist_{}.paramnames\".format(\"/\" + root, root), \"w\"\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -232,25 +238,26 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root,root))\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.3,\n", - " 'smooth_scale_1D':0.3})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.3, \"smooth_scale_1D\": 0.3},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -261,14 +268,35 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " for name, label in zip(name_list, label_list):\n", - " param_names.parWithName(name).label = label\n", - " " + " param_names.parWithName(name).label = label" ] }, { @@ -292,41 +320,44 @@ " bestfit_idx = np.argmax(chain.loglikes)\n", " maxlike = chain.loglikes[bestfit_idx]\n", " print(f\"Maximum Likelihood: {maxlike:.5g}\")\n", - " best_fit[root] = {\n", - " 'likelihood': maxlike\n", - " }\n", + " best_fit[root] = {\"likelihood\": maxlike}\n", " margestats = chain.getMargeStats()\n", - " s8_stats = margestats.parWithName('S_8')\n", - " sigma8_stats = margestats.parWithName('SIGMA_8')\n", - " omegam_stats = margestats.parWithName('OMEGA_M')\n", - " a_ia_stats = margestats.parWithName('a')\n", - "\n", - " best_fit[root].update({\n", - " 'S_8_mean': s8_stats.mean,\n", - " 'S_8_lower': s8_stats.mean - s8_stats.limits[0].lower,\n", - " 'S_8_upper': s8_stats.limits[0].upper - s8_stats.mean,\n", - " 'sigma_8_mean': sigma8_stats.mean,\n", - " 'sigma_8_lower': sigma8_stats.mean - sigma8_stats.limits[0].lower,\n", - " 'sigma_8_upper': sigma8_stats.limits[0].upper - sigma8_stats.mean,\n", - " 'omega_m_mean': omegam_stats.mean,\n", - " 'omega_m_lower': omegam_stats.mean - omegam_stats.limits[0].lower,\n", - " 'omega_m_upper': omegam_stats.limits[0].upper - omegam_stats.mean,\n", - " 'A_IA_mean': a_ia_stats.mean,\n", - " 'A_IA_lower': a_ia_stats.mean - a_ia_stats.limits[0].lower,\n", - " 'A_IA_upper': a_ia_stats.limits[0].upper - a_ia_stats.mean\n", - " })\n", + " s8_stats = margestats.parWithName(\"S_8\")\n", + " sigma8_stats = margestats.parWithName(\"SIGMA_8\")\n", + " omegam_stats = margestats.parWithName(\"OMEGA_M\")\n", + " a_ia_stats = margestats.parWithName(\"a\")\n", + "\n", + " best_fit[root].update(\n", + " {\n", + " \"S_8_mean\": s8_stats.mean,\n", + " \"S_8_lower\": s8_stats.mean - s8_stats.limits[0].lower,\n", + " \"S_8_upper\": s8_stats.limits[0].upper - s8_stats.mean,\n", + " \"sigma_8_mean\": sigma8_stats.mean,\n", + " \"sigma_8_lower\": sigma8_stats.mean - sigma8_stats.limits[0].lower,\n", + " \"sigma_8_upper\": sigma8_stats.limits[0].upper - sigma8_stats.mean,\n", + " \"omega_m_mean\": omegam_stats.mean,\n", + " \"omega_m_lower\": omegam_stats.mean - omegam_stats.limits[0].lower,\n", + " \"omega_m_upper\": omegam_stats.limits[0].upper - omegam_stats.mean,\n", + " \"A_IA_mean\": a_ia_stats.mean,\n", + " \"A_IA_lower\": a_ia_stats.mean - a_ia_stats.limits[0].lower,\n", + " \"A_IA_upper\": a_ia_stats.limits[0].upper - a_ia_stats.mean,\n", + " }\n", + " )\n", " try:\n", - " t_agn_stats = margestats.parWithName('logt_agn')\n", - " best_fit[root].update({\n", - " 'logt_agn_mean': t_agn_stats.mean,\n", - " 'logt_agn_lower': t_agn_stats.mean - t_agn_stats.limits[0].lower,\n", - " 'logt_agn_upper': t_agn_stats.limits[0].upper - t_agn_stats.mean\n", - " })\n", - " except:\n", + " t_agn_stats = margestats.parWithName(\"logt_agn\")\n", + " best_fit[root].update(\n", + " {\n", + " \"logt_agn_mean\": t_agn_stats.mean,\n", + " \"logt_agn_lower\": t_agn_stats.mean - t_agn_stats.limits[0].lower,\n", + " \"logt_agn_upper\": t_agn_stats.limits[0].upper - t_agn_stats.mean,\n", + " }\n", + " )\n", + " except Exception:\n", " pass\n", " for i, par in enumerate(likestats.names):\n", - " best_fit[root].update({par.name: np.average(chain.samples[:, i], weights=chain.weights)})\n", - " " + " best_fit[root].update(\n", + " {par.name: np.average(chain.samples[:, i], weights=chain.weights)}\n", + " )" ] }, { @@ -342,7 +373,7 @@ "metadata": {}, "outputs": [], "source": [ - "if not os.path.exists(path_ini_files+'/values_empty.ini'):\n", + "if not os.path.exists(path_ini_files + \"/values_empty.ini\"):\n", " content = \"\"\"[cosmological_parameters]\n", "\n", "tau = 0.0544\n", @@ -362,11 +393,11 @@ "[psf_leakage_parameters]\n", "\"\"\"\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as f:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as f:\n", " f.write(content)\n", " f.close()\n", "\n", - " print('File created successfully')" + " print(\"File created successfully\")" ] }, { @@ -376,17 +407,17 @@ "outputs": [], "source": [ "section_map = {\n", - " 'omch2': 'cosmological_parameters',\n", - " 'ombh2': 'cosmological_parameters',\n", - " 'h0': 'cosmological_parameters',\n", - " 'n_s': 'cosmological_parameters',\n", - " 's_8_input': 'cosmological_parameters',\n", - " 'logt_agn': 'halo_model_parameters',\n", - " 'a': 'intrinsic_alignment_parameters',\n", - " 'm1': 'shear_calibration_parameters',\n", - " 'bias_1': 'nofz_shifts',\n", - " 'alpha': 'psf_leakage_parameters',\n", - " 'beta': 'psf_leakage_parameters',\n", + " \"omch2\": \"cosmological_parameters\",\n", + " \"ombh2\": \"cosmological_parameters\",\n", + " \"h0\": \"cosmological_parameters\",\n", + " \"n_s\": \"cosmological_parameters\",\n", + " \"s_8_input\": \"cosmological_parameters\",\n", + " \"logt_agn\": \"halo_model_parameters\",\n", + " \"a\": \"intrinsic_alignment_parameters\",\n", + " \"m1\": \"shear_calibration_parameters\",\n", + " \"bias_1\": \"nofz_shifts\",\n", + " \"alpha\": \"psf_leakage_parameters\",\n", + " \"beta\": \"psf_leakage_parameters\",\n", "}" ] }, @@ -397,13 +428,16 @@ "outputs": [], "source": [ "env = os.environ.copy()\n", - "env[\"LD_LIBRARY_PATH\"] = \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\" + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + "env[\"LD_LIBRARY_PATH\"] = (\n", + " \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\"\n", + " + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + ")\n", "\n", "for root in roots:\n", " print(root)\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+'/values_empty.ini')\n", + " config.read(path_ini_files + \"/values_empty.ini\")\n", " for param, value in best_fit[root].items():\n", " section = section_map.get(param)\n", " if section is None:\n", @@ -412,37 +446,37 @@ " config.add_section(section)\n", " config[section][param] = str(value)\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as configfile:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Modify the ini file to run in test mode at the best fit\n", + " # Modify the ini file to run in test mode at the best fit\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+f'/cosmosis_pipeline_{root}.ini')\n", + " config.read(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\")\n", "\n", - " sampler = config['runtime']['sampler']\n", - " config['runtime']['sampler'] = 'test'\n", - " values = config['pipeline']['values']\n", - " config['pipeline']['values'] = path_ini_files + '/values_empty.ini'\n", + " sampler = config[\"runtime\"][\"sampler\"]\n", + " config[\"runtime\"][\"sampler\"] = \"test\"\n", + " values = config[\"pipeline\"][\"values\"]\n", + " config[\"pipeline\"][\"values\"] = path_ini_files + \"/values_empty.ini\"\n", "\n", - " with open(path_ini_files+f'/cosmosis_pipeline_{root}.ini', 'w') as configfile:\n", + " with open(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\", \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Run cosmosis\n", + " # Run cosmosis\n", " result = subprocess.run(\n", - " ['cosmosis', 'cosmosis_config/cosmosis_pipeline_{}.ini'.format(root)],\n", + " [\"cosmosis\", \"cosmosis_config/cosmosis_pipeline_{}.ini\".format(root)],\n", " env=env,\n", " capture_output=True,\n", - " text=True\n", + " text=True,\n", " )\n", " print(f\"STDOUT:\\n{result.stdout}\")\n", " print(f\"STDERR:\\n{result.stderr}\")\n", "\n", - " #Modify the ini file to the previous one\n", - " config['pipeline']['values'] = values\n", - " config['runtime']['sampler'] = sampler\n", + " # Modify the ini file to the previous one\n", + " config[\"pipeline\"][\"values\"] = values\n", + " config[\"runtime\"][\"sampler\"] = sampler\n", "\n", - " with open(path_ini_files+f'/cosmosis_pipeline_{root}.ini', 'w') as configfile:\n", + " with open(path_ini_files + f\"/cosmosis_pipeline_{root}.ini\", \"w\") as configfile:\n", " config.write(configfile)" ] }, @@ -459,7 +493,7 @@ "metadata": {}, "outputs": [], "source": [ - "output_folder = '/n09data/guerrini/output_chains/'\n", + "output_folder = \"/n09data/guerrini/output_chains/\"\n", "\n", "metrics = {}\n", "\n", @@ -469,89 +503,115 @@ " base = bases[i]\n", "\n", " if base == \"harmonic\":\n", - " #Remove cell from the end of root\n", - " root_cell_removed = root.replace('_cell', '')\n", + " # Remove cell from the end of root\n", + " root_cell_removed = root.replace(\"_cell\", \"\")\n", "\n", - " lower_bound_cell_ee = properties[root]['lower_bound_cell_ee']\n", - " upper_bound_cell_ee = properties[root]['upper_bound_cell_ee']\n", + " lower_bound_cell_ee = properties[root][\"lower_bound_cell_ee\"]\n", + " upper_bound_cell_ee = properties[root][\"upper_bound_cell_ee\"]\n", " print(upper_bound_cell_ee)\n", "\n", - " #Read the results\n", - " ell = np.loadtxt(output_folder + 'best_fit/{}/shear_cl/ell.txt'.format(root))\n", - " shear_cl = np.loadtxt(output_folder + 'best_fit/{}/shear_cl/bin_1_1.txt'.format(root))\n", + " # Read the results\n", + " ell = np.loadtxt(output_folder + \"best_fit/{}/shear_cl/ell.txt\".format(root))\n", + " shear_cl = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_cl/bin_1_1.txt\".format(root)\n", + " )\n", "\n", - " #Read the data\n", - " data = fits.open(f'data/{root_cell_removed}/cosmosis_{root}.fits')\n", + " # Read the data\n", + " data = fits.open(f\"data/{root_cell_removed}/cosmosis_{root}.fits\")\n", "\n", - " ell_data = data['CELL_EE'].data['ANG']\n", - " cell_data = data['CELL_EE'].data['VALUE']\n", + " ell_data = data[\"CELL_EE\"].data[\"ANG\"]\n", + " cell_data = data[\"CELL_EE\"].data[\"VALUE\"]\n", "\n", - " #Load the covariance\n", - " cov = data['COVMAT'].data\n", + " # Load the covariance\n", + " cov = data[\"COVMAT\"].data\n", " cov_cell = cov\n", "\n", - " #interpolate the model\n", - " interp_cell_ee = interp1d(ell, shear_cl, kind='cubic', fill_value='extrapolate')\n", + " # interpolate the model\n", + " interp_cell_ee = interp1d(ell, shear_cl, kind=\"cubic\", fill_value=\"extrapolate\")\n", "\n", " cell_model = interp_cell_ee(ell_data)\n", "\n", - " #Apply scale cuts\n", + " # Apply scale cuts\n", " mask_cell = (ell_data > lower_bound_cell_ee) & (ell_data < upper_bound_cell_ee)\n", " cell_data = cell_data[mask_cell]\n", " cell_model = cell_model[mask_cell]\n", " cov_cell = cov_cell[mask_cell][:, mask_cell]\n", "\n", - " cell_chi2 = np.dot((cell_model - cell_data), np.dot(np.linalg.inv(cov_cell), (cell_model - cell_data)))\n", + " cell_chi2 = np.dot(\n", + " (cell_model - cell_data),\n", + " np.dot(np.linalg.inv(cov_cell), (cell_model - cell_data)),\n", + " )\n", " n_dof_cell = np.sum(mask_cell)\n", " print(n_dof_cell)\n", " n_dof_cell -= 9\n", " p_value_cell = 1 - stats.chi2.cdf(cell_chi2, n_dof_cell)\n", "\n", " metrics[root] = {\n", - " 'chi2': cell_chi2,\n", - " 'n_dof': n_dof_cell,\n", - " 'p_value': p_value_cell\n", + " \"chi2\": cell_chi2,\n", + " \"n_dof\": n_dof_cell,\n", + " \"p_value\": p_value_cell,\n", " }\n", "\n", " elif base == \"configuration\":\n", - "\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", "\n", - " #Read model tau_stats\n", - " theta_tau = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/theta.txt'.format(root))\n", + " # Read model tau_stats\n", + " theta_tau = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/theta.txt\".format(root)\n", + " )\n", " theta_tau_arcmin = theta_tau * 180 * 60 / np.pi\n", - " tau_0_model = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/bin_1_1.txt'.format(root))\n", - " tau_2_model = np.loadtxt(output_folder + 'best_fit/{}/tau_2_plus/bin_1_1.txt'.format(root))\n", + " tau_0_model = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " tau_2_model = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_2_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", - " #Read the data\n", - " data = fits.open(f'data/{catalog_version_real_space}/cosmosis_{catalog_version_real_space}.fits')\n", + " # Read the data\n", + " data = fits.open(\n", + " f\"data/{catalog_version_real_space}/cosmosis_{catalog_version_real_space}.fits\"\n", + " )\n", "\n", - " theta_data = data['XI_PLUS'].data['ANG']\n", - " xi_plus_data = data['XI_PLUS'].data['VALUE']\n", - " xi_minus_data = data['XI_MINUS'].data['VALUE']\n", - " tau_0_data = data['TAU_0_PLUS'].data['VALUE']\n", - " tau_2_data = data['TAU_2_PLUS'].data['VALUE']\n", + " theta_data = data[\"XI_PLUS\"].data[\"ANG\"]\n", + " xi_plus_data = data[\"XI_PLUS\"].data[\"VALUE\"]\n", + " xi_minus_data = data[\"XI_MINUS\"].data[\"VALUE\"]\n", + " tau_0_data = data[\"TAU_0_PLUS\"].data[\"VALUE\"]\n", + " tau_2_data = data[\"TAU_2_PLUS\"].data[\"VALUE\"]\n", "\n", - " #Load the covariance\n", - " cov = data['COVMAT'].data\n", - " cov_xi = cov[0:2*len(xi_plus_data), 0:2*len(xi_plus_data)]\n", - " cov_tau = cov[2*len(xi_plus_data):, 2*len(xi_plus_data):]\n", + " # Load the covariance\n", + " cov = data[\"COVMAT\"].data\n", + " cov_xi = cov[0 : 2 * len(xi_plus_data), 0 : 2 * len(xi_plus_data)]\n", + " cov_tau = cov[2 * len(xi_plus_data) :, 2 * len(xi_plus_data) :]\n", "\n", - " #interpolate the model\n", - " interp_xi_plus = interp1d(theta_arcmin, shear_xi_plus, kind='cubic', fill_value='extrapolate')\n", - " interp_xi_minus = interp1d(theta_arcmin, shear_xi_minus, kind='cubic', fill_value='extrapolate')\n", + " # interpolate the model\n", + " interp_xi_plus = interp1d(\n", + " theta_arcmin, shear_xi_plus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", + " interp_xi_minus = interp1d(\n", + " theta_arcmin, shear_xi_minus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", "\n", " xi_plus_model = interp_xi_plus(theta_data)\n", " if add_xi_sys:\n", @@ -560,25 +620,33 @@ " if add_xi_sys:\n", " xi_minus_model += xi_sys_minus\n", "\n", - " #Concatenate the data vector\n", + " # Concatenate the data vector\n", " xi_data = np.concatenate((xi_plus_data, xi_minus_data))\n", " xi_model = np.concatenate((xi_plus_model, xi_minus_model))\n", "\n", " tau_data = np.concatenate((tau_0_data, tau_2_data))\n", " tau_model = np.concatenate((tau_0_model, tau_2_model))\n", "\n", - " #Apply scale cuts\n", - " mask_xi_plus = (theta_data > lower_bound_xi_plus) & (theta_data < upper_bound_xi_plus)\n", - " mask_xi_minus = (theta_data > lower_bound_xi_minus) & (theta_data < upper_bound_xi_minus)\n", + " # Apply scale cuts\n", + " mask_xi_plus = (theta_data > lower_bound_xi_plus) & (\n", + " theta_data < upper_bound_xi_plus\n", + " )\n", + " mask_xi_minus = (theta_data > lower_bound_xi_minus) & (\n", + " theta_data < upper_bound_xi_minus\n", + " )\n", " mask = np.concatenate((mask_xi_plus, mask_xi_minus))\n", "\n", " xi_data = xi_data[mask]\n", " xi_model = xi_model[mask]\n", " cov_xi = cov_xi[mask][:, mask]\n", "\n", - "\n", - " xi_plus_chi2 = np.dot((xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data)))\n", - " tau_chi2 = np.dot((tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data)))\n", + " xi_plus_chi2 = np.dot(\n", + " (xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data))\n", + " )\n", + " tau_chi2 = np.dot(\n", + " (tau_model - tau_data),\n", + " np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data)),\n", + " )\n", " n_dof_xi = np.sum(mask)\n", " n_dof_xi -= 11\n", " n_dof_tau = len(tau_0_data) + len(tau_2_data)\n", @@ -588,13 +656,8 @@ " n_dof_tot = n_dof_xi + n_dof_tau\n", " p_value_tot = 1 - stats.chi2.cdf(chi2_tot, n_dof_tot)\n", "\n", - " metrics[root] = {\n", - " 'chi2': xi_plus_chi2,\n", - " 'n_dof': n_dof_xi,\n", - " 'p_value': p_value_xi\n", - " }\n", + " metrics[root] = {\"chi2\": xi_plus_chi2, \"n_dof\": n_dof_xi, \"p_value\": p_value_xi}\n", "\n", - " \n", " print(\"Done!\")" ] }, @@ -609,13 +672,13 @@ " r\"\\begin{tabular}{|l|c|c|c|c|c|c|c|}\",\n", " r\"\\hline\",\n", " r\"Experiment name & $S_8$ & $\\Omega_m$ & $\\sigma_8$ & $A_\\mathrm{IA}$ & $\\log T_\\mathrm{AGN}$ & $\\chi^2$/dof & PTE \\\\ \",\n", - " r\"\\hline\"\n", + " r\"\\hline\",\n", " ]\n", "\n", " for i, (root, vals) in enumerate(metrics.items()):\n", " label = labels[i]\n", " best_fit_vals = best_fit[root]\n", - " log_t_agn_mean = best_fit_vals.get('logt_agn_mean', None)\n", + " log_t_agn_mean = best_fit_vals.get(\"logt_agn_mean\", None)\n", "\n", " if log_t_agn_mean is None:\n", " logt_agn_mean_str = \"N/A\"\n", @@ -657,7 +720,7 @@ "def display_markdown(metrics):\n", " # Build Markdown table\n", " header = (\n", - " \"| Root | $\\chi^2$ ($C_\\ell$) / dof | p-val ($C_\\ell$) |\\n\"\n", + " \"| Root | $\\\\chi^2$ ($C_\\\\ell$) / dof | p-val ($C_\\\\ell$) |\\n\"\n", " \"|------|----------------|------------|\\n\"\n", " )\n", "\n", @@ -704,10 +767,12 @@ "metadata": {}, "outputs": [], "source": [ - "catalog_version = 'SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600'\n", - "data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}_cell.fits')\n", - "cell_ee = data['CELL_EE'].data\n", - "cov_mat = data['COVMAT'].data" + "catalog_version = \"SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600\"\n", + "data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}_cell.fits\"\n", + ")\n", + "cell_ee = data[\"CELL_EE\"].data\n", + "cov_mat = data[\"COVMAT\"].data" ] }, { @@ -718,18 +783,25 @@ "source": [ "fig, ax = plt.subplots(1, 1, figsize=(15, 8))\n", "\n", - "ell = cell_ee['ANG']\n", - "cell = cell_ee['VALUE']\n", + "ell = cell_ee[\"ANG\"]\n", + "cell = cell_ee[\"VALUE\"]\n", "\n", - "ax.errorbar(ell, ell*cell, yerr=ell*np.sqrt(np.diag(cov)), fmt='o', label=\"SP_v1.4.5 data\", color='black')\n", - "ax.set_xlabel('$\\ell$')\n", - "ax.set_ylabel('$\\ell C_\\ell$')\n", - "ax.set_xlim(ell.min()-10, ell.max()+100)\n", - "ax.set_xscale('squareroot')\n", + "ax.errorbar(\n", + " ell,\n", + " ell * cell,\n", + " yerr=ell * np.sqrt(np.diag(cov)),\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "ax.set_xlabel(r\"$\\ell$\")\n", + "ax.set_ylabel(r\"$\\ell C_\\ell$\")\n", + "ax.set_xlim(ell.min() - 10, ell.max() + 100)\n", + "ax.set_xscale(\"squareroot\")\n", "ax.set_xticks(np.array([100, 400, 900, 1600]))\n", "ax.minorticks_on()\n", "ax.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)]\n", + "minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)]\n", "ax.xaxis.set_ticks(minor_ticks, minor=True)\n", "\n", "plt.legend(fontsize=15)\n", @@ -743,208 +815,340 @@ "metadata": {}, "outputs": [], "source": [ - "def plot_best_fit(data_points, root_to_plot, line_args, savefile, ell_min=10.0, ell_max=2048.0, multiply_ell=True, loc_legend=\"best\", bbox_to_anchor=None, label_data=\"Fiducial data\", labels=None):\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{data_points}/cosmosis_{data_points}_cell.fits')\n", - " cell_ee = data['CELL_EE'].data\n", - " cov_mat = data['COVMAT'].data\n", + "def plot_best_fit(\n", + " data_points,\n", + " root_to_plot,\n", + " line_args,\n", + " savefile,\n", + " ell_min=10.0,\n", + " ell_max=2048.0,\n", + " multiply_ell=True,\n", + " loc_legend=\"best\",\n", + " bbox_to_anchor=None,\n", + " label_data=\"Fiducial data\",\n", + " labels=None,\n", + "):\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{data_points}/cosmosis_{data_points}_cell.fits\"\n", + " )\n", + " cell_ee = data[\"CELL_EE\"].data\n", + " cov_mat = data[\"COVMAT\"].data\n", "\n", " if labels is None:\n", " labels = root_to_plot\n", "\n", " fig, ax = plt.subplots(1, 1, figsize=(8, 5))\n", "\n", - " ell, cell = cell_ee['ANG'], cell_ee['VALUE']\n", - " ax.errorbar(ell, ell*cell, yerr=ell*np.sqrt(np.diag(cov_mat)), fmt='o', label=label_data, color='black', capsize=2)\n", - " \n", + " ell, cell = cell_ee[\"ANG\"], cell_ee[\"VALUE\"]\n", + " ax.errorbar(\n", + " ell,\n", + " ell * cell,\n", + " yerr=ell * np.sqrt(np.diag(cov_mat)),\n", + " fmt=\"o\",\n", + " label=label_data,\n", + " color=\"black\",\n", + " capsize=2,\n", + " )\n", + "\n", " for idx, (label, root) in enumerate(zip(labels, root_to_plot)):\n", - " lower_bound_cell_ee = properties[root]['lower_bound_cell_ee']\n", - " upper_bound_cell_ee = properties[root]['upper_bound_cell_ee']\n", - " \n", - " #Read the results\n", - " ell = np.loadtxt(output_folder + 'best_fit/{}/shear_cl/ell.txt'.format(root))\n", - " shear_cl = np.loadtxt(output_folder + 'best_fit/{}/shear_cl/bin_1_1.txt'.format(root))\n", + " # Read the results\n", + " ell = np.loadtxt(output_folder + \"best_fit/{}/shear_cl/ell.txt\".format(root))\n", + " shear_cl = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_cl/bin_1_1.txt\".format(root)\n", + " )\n", "\n", " mask = (ell > ell_min) & (ell < ell_max)\n", "\n", - " ax.plot(ell[mask], ell[mask]*shear_cl[mask] if multiply_ell else shear_cl[mask], label=label, **line_args[idx])\n", - " \n", - " # Plot the scale cuts for different k_max\n", - " ax.axvline(x=1800, color='black', linestyle='--', alpha=0.5)\n", - " ax.axvline(x=2048, color='black', linestyle='--', alpha=1.0)\n", - " ax.axvline(x=500, color='black', linestyle='--', alpha=0.3)\n", + " ax.plot(\n", + " ell[mask],\n", + " ell[mask] * shear_cl[mask] if multiply_ell else shear_cl[mask],\n", + " label=label,\n", + " **line_args[idx],\n", + " )\n", "\n", - " \n", - " \n", + " # Plot the scale cuts for different k_max\n", + " ax.axvline(x=1800, color=\"black\", linestyle=\"--\", alpha=0.5)\n", + " ax.axvline(x=2048, color=\"black\", linestyle=\"--\", alpha=1.0)\n", + " ax.axvline(x=500, color=\"black\", linestyle=\"--\", alpha=0.3)\n", "\n", " # Add labels directly under the tick\n", - " ax.text(1740, 0.90,\n", - " r\"$k_\\mathrm{max} = 3 h$ Mpc$^{-1}$\",\n", - " transform=ax.get_xaxis_transform(),\n", - " ha='center', va='top', fontsize=14, rotation=90)\n", - "\n", - " ax.text(1978, 0.90,\n", - " r\"$k_\\mathrm{max} = 5 h$ Mpc$^{-1}$\",\n", - " transform=ax.get_xaxis_transform(),\n", - " ha='center', va='top', fontsize=14, rotation=90)\n", - "\n", - " ax.text(470, 0.90,\n", - " r\"$k_\\mathrm{max} = 1 h$ Mpc$^{-1}$\",\n", - " transform=ax.get_xaxis_transform(),\n", - " ha='center', va='top', fontsize=14, rotation=90)\n", - "\n", - " ell, cell = cell_ee['ANG'], cell_ee['VALUE']\n", - " ax.set_ylabel(r'$\\ell C_\\ell \\times 10^{-7}$', fontsize=20)\n", - " ax.set_xlabel(r'Multipole $\\ell$', fontsize=20)\n", - " ax.set_xlim(ell.min()-10, ell.max()+100)\n", - " ax.set_xscale('squareroot')\n", + " ax.text(\n", + " 1740,\n", + " 0.90,\n", + " r\"$k_\\mathrm{max} = 3 h$ Mpc$^{-1}$\",\n", + " transform=ax.get_xaxis_transform(),\n", + " ha=\"center\",\n", + " va=\"top\",\n", + " fontsize=14,\n", + " rotation=90,\n", + " )\n", + "\n", + " ax.text(\n", + " 1978,\n", + " 0.90,\n", + " r\"$k_\\mathrm{max} = 5 h$ Mpc$^{-1}$\",\n", + " transform=ax.get_xaxis_transform(),\n", + " ha=\"center\",\n", + " va=\"top\",\n", + " fontsize=14,\n", + " rotation=90,\n", + " )\n", + "\n", + " ax.text(\n", + " 470,\n", + " 0.90,\n", + " r\"$k_\\mathrm{max} = 1 h$ Mpc$^{-1}$\",\n", + " transform=ax.get_xaxis_transform(),\n", + " ha=\"center\",\n", + " va=\"top\",\n", + " fontsize=14,\n", + " rotation=90,\n", + " )\n", + "\n", + " ell, cell = cell_ee[\"ANG\"], cell_ee[\"VALUE\"]\n", + " ax.set_ylabel(r\"$\\ell C_\\ell \\times 10^{-7}$\", fontsize=20)\n", + " ax.set_xlabel(r\"Multipole $\\ell$\", fontsize=20)\n", + " ax.set_xlim(ell.min() - 10, ell.max() + 100)\n", + " ax.set_xscale(\"squareroot\")\n", " ax.set_xticks(np.array([100, 400, 900, 1600]))\n", " ax.minorticks_on()\n", " ax.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - " minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)]\n", + " minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)]\n", " ax.xaxis.set_ticks(minor_ticks, minor=True)\n", - " ax.tick_params(axis='both', which='major', labelsize=14)\n", - " ax.tick_params(axis='both', which='minor', labelsize=10)\n", + " ax.tick_params(axis=\"both\", which=\"major\", labelsize=14)\n", + " ax.tick_params(axis=\"both\", which=\"minor\", labelsize=10)\n", " ax.yaxis.get_offset_text().set_visible(False)\n", "\n", - "\n", " plt.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor, fontsize=11)\n", "\n", " if savefile is not None:\n", - " plt.savefig(savefile, bbox_inches='tight')\n", + " plt.savefig(savefile, bbox_inches=\"tight\")\n", "\n", " plt.show()\n", "\n", - "def plot_best_fit_ratio(root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0):\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits')\n", - " xi_plus = data['XI_PLUS'].data\n", - " xi_minus = data['XI_MINUS'].data\n", - " cov_mat = data['COVMAT'].data\n", + "\n", + "def plot_best_fit_ratio(\n", + " root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0\n", + "):\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits\"\n", + " )\n", + " xi_plus = data[\"XI_PLUS\"].data\n", + " xi_minus = data[\"XI_MINUS\"].data\n", + " cov_mat = data[\"COVMAT\"].data\n", "\n", " plt.figure(figsize=(15, 15))\n", "\n", " plt.subplot(211)\n", "\n", " root = roots[0]\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_plus_model_fiducial = shear_xi_plus[mask]\n", " if add_xi_sys:\n", - " xi_plus_model_fiducial += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus)\n", + " xi_plus_model_fiducial += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus\n", + " )\n", "\n", - " plt.errorbar(xi_plus['ANG'], xi_plus['VALUE']/np.interp(xi_plus['ANG'], theta_arcmin[mask], xi_plus_model_fiducial), yerr=np.sqrt(np.diag(cov_mat))[:20]/np.abs(np.interp(xi_plus['ANG'], theta_arcmin[mask], xi_plus_model_fiducial)), fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"]\n", + " / np.interp(xi_plus[\"ANG\"], theta_arcmin[mask], xi_plus_model_fiducial),\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20]\n", + " / np.abs(np.interp(xi_plus[\"ANG\"], theta_arcmin[mask], xi_plus_model_fiducial)),\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_plus_model = shear_xi_plus[mask]\n", " if add_xi_sys:\n", - " xi_plus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus)\n", - " \n", - " alpha = 1.0 if root==roots[0] else 0.5\n", - " plt.plot(theta_arcmin[mask], xi_plus_model/xi_plus_model_fiducial, color=color, label=root, alpha=alpha)\n", - " plt.axvline(x=lower_bound_xi_plus, color=color, linestyle='--', alpha=0.3)\n", - " plt.axvline(x=upper_bound_xi_plus, color=color, linestyle='--', alpha=0.3)\n", - " \n", - "\n", - "\n", - " plt.ylabel(fr'$\\xi_{{+}}/\\xi_{{+, \\text{{fid}}}}$', fontsize=26)\n", - " plt.xscale('log')\n", - " #plt.yscale('log')\n", + " xi_plus_model += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus\n", + " )\n", + "\n", + " alpha = 1.0 if root == roots[0] else 0.5\n", + " plt.plot(\n", + " theta_arcmin[mask],\n", + " xi_plus_model / xi_plus_model_fiducial,\n", + " color=color,\n", + " label=root,\n", + " alpha=alpha,\n", + " )\n", + " plt.axvline(x=lower_bound_xi_plus, color=color, linestyle=\"--\", alpha=0.3)\n", + " plt.axvline(x=upper_bound_xi_plus, color=color, linestyle=\"--\", alpha=0.3)\n", + "\n", + " plt.ylabel(r\"$\\xi_{+}/\\xi_{+, \\text{fid}}$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"lower left\", fontsize=8)\n", "\n", " plt.subplot(212)\n", "\n", " root = roots[0]\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_minus_model_fiducial = shear_xi_minus[mask]\n", " if add_xi_sys:\n", - " xi_minus_model_fiducial += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus)\n", + " xi_minus_model_fiducial += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus\n", + " )\n", "\n", - " plt.errorbar(xi_minus['ANG'], xi_minus['VALUE']/np.interp(xi_minus['ANG'], theta_arcmin[mask], xi_minus_model_fiducial), yerr=np.sqrt(np.diag(cov_mat))[20:40]/np.abs(np.interp(xi_minus['ANG'], theta_arcmin[mask], xi_minus_model_fiducial)), fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"]\n", + " / np.interp(xi_minus[\"ANG\"], theta_arcmin[mask], xi_minus_model_fiducial),\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40]\n", + " / np.abs(\n", + " np.interp(xi_minus[\"ANG\"], theta_arcmin[mask], xi_minus_model_fiducial)\n", + " ),\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root))\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = np.loadtxt(\n", + " output_folder + \"best_fit/{}/xi_sys/theta.txt\".format(root)\n", + " )\n", " theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", " xi_minus_model = shear_xi_minus[mask]\n", " if add_xi_sys:\n", - " xi_minus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus)\n", - "\n", - " alpha = 1.0 if root==roots[0] else 0.5\n", - " plt.plot(theta_arcmin[mask], xi_minus_model/xi_minus_model_fiducial, color=color, label=root, alpha=alpha)\n", - " plt.axvline(x=lower_bound_xi_minus, color=color, linestyle='--', alpha=0.3)\n", - " plt.axvline(x=upper_bound_xi_minus, color=color, linestyle='--', alpha=0.3)\n", - "\n", + " xi_minus_model += np.interp(\n", + " theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus\n", + " )\n", + "\n", + " alpha = 1.0 if root == roots[0] else 0.5\n", + " plt.plot(\n", + " theta_arcmin[mask],\n", + " xi_minus_model / xi_minus_model_fiducial,\n", + " color=color,\n", + " label=root,\n", + " alpha=alpha,\n", + " )\n", + " plt.axvline(x=lower_bound_xi_minus, color=color, linestyle=\"--\", alpha=0.3)\n", + " plt.axvline(x=upper_bound_xi_minus, color=color, linestyle=\"--\", alpha=0.3)\n", "\n", - " plt.xlabel(r'$\\theta$ [arcmin]', fontsize=26)\n", - " plt.ylabel(fr'$\\xi_{{-}}/\\xi_{{-, \\text{{fid}}}}$', fontsize=26)\n", - " plt.xscale('log')\n", + " plt.xlabel(r\"$\\theta$ [arcmin]\", fontsize=26)\n", + " plt.ylabel(r\"$\\xi_{-}/\\xi_{-, \\text{fid}}$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", " plt.ylim(0, 2)\n", - " #plt.yscale('log')\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"lower left\", fontsize=8)\n", "\n", " if savefile is not None:\n", - " plt.savefig(savefile, bbox_inches='tight')\n", + " plt.savefig(savefile, bbox_inches=\"tight\")\n", "\n", " plt.show()" ] @@ -955,36 +1159,52 @@ "metadata": {}, "outputs": [], "source": [ - "plt.rcParams.update({'font.family': \"serif\"})\n", + "plt.rcParams.update({\"font.family\": \"serif\"})\n", "\n", "root_to_plot = [\n", - " f\"SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_halofit_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_10_80\"\n", + " \"SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_halofit_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_10_80\",\n", "]\n", "\n", "labels = [\n", " r\"UNIONS $C_\\ell$, Blind A\",\n", " r\"UNIONS $C_\\ell$, Halofit\",\n", - " r\"UNIONS $\\xi_\\pm(\\vartheta)$ (Goh et al., 2026)\"\n", + " r\"UNIONS $\\xi_\\pm(\\vartheta)$ (Goh et al., 2026)\",\n", "]\n", "\n", "line_args = [\n", - " {'color': 'royalblue', 'linestyle': '-'},\n", - " {'color': 'royalblue', 'linestyle': '--'},\n", - " {'color': 'orange', 'linestyle': '-'}\n", + " {\"color\": \"royalblue\", \"linestyle\": \"-\"},\n", + " {\"color\": \"royalblue\", \"linestyle\": \"--\"},\n", + " {\"color\": \"orange\", \"linestyle\": \"-\"},\n", "]\n", "\n", - "log_legend=\"lower center\"\n", - "bbox_to_anchor=(0.685, 0.70)\n", + "log_legend = \"lower center\"\n", + "bbox_to_anchor = (0.685, 0.70)\n", "\n", "savefile = \"../papers/harmonic/plots/paperplot_Cell_EE_and_best_fit.png\"\n", "\n", - "plot_best_fit(catalog_version, root_to_plot, line_args, savefile, labels=labels, loc_legend=log_legend, bbox_to_anchor=bbox_to_anchor)\n", + "plot_best_fit(\n", + " catalog_version,\n", + " root_to_plot,\n", + " line_args,\n", + " savefile,\n", + " labels=labels,\n", + " loc_legend=log_legend,\n", + " bbox_to_anchor=bbox_to_anchor,\n", + ")\n", "\n", "savefile = \"../papers/harmonic/plots/paperplot_Cell_EE_and_best_fit.pdf\"\n", "\n", - "plot_best_fit(catalog_version, root_to_plot, line_args, savefile, labels=labels, loc_legend=log_legend, bbox_to_anchor=bbox_to_anchor)" + "plot_best_fit(\n", + " catalog_version,\n", + " root_to_plot,\n", + " line_args,\n", + " savefile,\n", + " labels=labels,\n", + " loc_legend=log_legend,\n", + " bbox_to_anchor=bbox_to_anchor,\n", + ")" ] }, { @@ -1001,18 +1221,15 @@ "outputs": [], "source": [ "root_to_plot = [\n", - " f\"SP_v1.4.6_leak_corr_A_small_scales_cell\",\n", - " f\"SP_v1.4.6_leak_corr_A_large_scales_cell\"\n", + " \"SP_v1.4.6_leak_corr_A_small_scales_cell\",\n", + " \"SP_v1.4.6_leak_corr_A_large_scales_cell\",\n", "]\n", "\n", - "labels = [\n", - " r\"Small scales only\",\n", - " r\"Large scales only\"\n", - "]\n", + "labels = [r\"Small scales only\", r\"Large scales only\"]\n", "\n", "line_args = [\n", - " {'color': 'royalblue', 'linestyle': '-'},\n", - " {'color': 'royalblue', 'linestyle': '--'}\n", + " {\"color\": \"royalblue\", \"linestyle\": \"-\"},\n", + " {\"color\": \"royalblue\", \"linestyle\": \"--\"},\n", "]\n", "\n", "savefile = \"../papers/harmonic/plots/small_vs_large_scale.png\"\n", @@ -1030,16 +1247,16 @@ "\n", "root_to_plot = [\n", " \"SP_v1.4.5_A\",\n", - " #\"SP_v1.4.5_A_no_IA\",\n", - " #\"SP_v1.4.5_A_no_dz\",\n", - " #\"SP_v1.4.5_A_no_m_bias\",\n", + " # \"SP_v1.4.5_A_no_IA\",\n", + " # \"SP_v1.4.5_A_no_dz\",\n", + " # \"SP_v1.4.5_A_no_m_bias\",\n", " \"SP_v1.4.5_A_sc_3_150\",\n", " \"SP_v1.4.5_A_sc_3_60\",\n", " \"SP_v1.4.5_A_sc_10_150\",\n", " \"SP_v1.4.5_A_sc_10_60\",\n", " \"SP_v1.4.5_A_sc_5_150\",\n", " \"SP_v1.4.5_A_sc_7_150\",\n", - " #\"SP_v1.4.5_A_no_leakage\"\n", + " # \"SP_v1.4.5_A_no_leakage\"\n", "]\n", "\n", "\"\"\" root_to_plot = [\n", @@ -1048,15 +1265,30 @@ "\n", "root_to_plot = [\n", " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0\",\n", - " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0_no_alpha_beta\"\n", + " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0_no_alpha_beta\",\n", "]\n", "\n", "\n", "colours = [\n", - " 'red', 'salmon', 'darkorange', 'forestgreen', 'turquoise', 'darkviolet', 'crimson', 'gold', 'lightcoral', 'mediumseagreen', 'lightsteelblue', 'black', 'silver', 'peru', 'maroon', 'olive'\n", + " \"red\",\n", + " \"salmon\",\n", + " \"darkorange\",\n", + " \"forestgreen\",\n", + " \"turquoise\",\n", + " \"darkviolet\",\n", + " \"crimson\",\n", + " \"gold\",\n", + " \"lightcoral\",\n", + " \"mediumseagreen\",\n", + " \"lightsteelblue\",\n", + " \"black\",\n", + " \"silver\",\n", + " \"peru\",\n", + " \"maroon\",\n", + " \"olive\",\n", "]\n", "\n", - "savefile = 'best_fit_ratio_w_wo_leakage.png'\n", + "savefile = \"best_fit_ratio_w_wo_leakage.png\"\n", "\n", "plot_best_fit_ratio(root_to_plot, colours, savefile)" ] @@ -1068,59 +1300,90 @@ "outputs": [], "source": [ "def plot_best_fit_tau(root_to_plot, colours, savefile, theta_min=1.0, theta_max=250.0):\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits')\n", - " tau_0 = data['TAU_0_PLUS'].data\n", - " tau_2 = data['TAU_2_PLUS'].data\n", - " cov_mat = data['COVMAT'].data\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_version}/cosmosis_{catalog_version}.fits\"\n", + " )\n", + " tau_0 = data[\"TAU_0_PLUS\"].data\n", + " tau_2 = data[\"TAU_2_PLUS\"].data\n", + " cov_mat = data[\"COVMAT\"].data\n", "\n", " plt.figure(figsize=(15, 15))\n", "\n", " plt.subplot(211)\n", "\n", - " plt.errorbar(tau_0['ANG'], tau_0['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[40:60], fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " plt.errorbar(\n", + " tau_0[\"ANG\"],\n", + " tau_0[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[40:60],\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/theta.txt'.format(root))\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " tau_0_plus = np.loadtxt(output_folder + 'best_fit/{}/tau_0_plus/bin_1_1.txt'.format(root))\n", - "\n", + " tau_0_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_0_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", - " \n", - " plt.plot(theta_arcmin[mask], tau_0_plus[mask], color=color, label=root, alpha=0.5)\n", - " \n", - " plt.ylabel(r'$\\tau_0$', fontsize=26)\n", - " plt.xscale('log')\n", - " #plt.yscale('log')\n", + "\n", + " plt.plot(\n", + " theta_arcmin[mask], tau_0_plus[mask], color=color, label=root, alpha=0.5\n", + " )\n", + "\n", + " plt.ylabel(r\"$\\tau_0$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"upper right\", fontsize=8)\n", "\n", " plt.subplot(212)\n", "\n", - " y_plot_tau_2 = tau_2['ANG'] * tau_2['VALUE']\n", - " y_errorbar = tau_2['ANG'] * np.sqrt(np.diag(cov_mat))[60:80]\n", - " plt.errorbar(tau_2['ANG'], y_plot_tau_2, yerr=y_errorbar, fmt='o', label=f'{catalog_version} data', color='black', markersize=2)\n", + " y_plot_tau_2 = tau_2[\"ANG\"] * tau_2[\"VALUE\"]\n", + " y_errorbar = tau_2[\"ANG\"] * np.sqrt(np.diag(cov_mat))[60:80]\n", + " plt.errorbar(\n", + " tau_2[\"ANG\"],\n", + " y_plot_tau_2,\n", + " yerr=y_errorbar,\n", + " fmt=\"o\",\n", + " label=f\"{catalog_version} data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + " )\n", "\n", " for root, color in zip(root_to_plot, colours):\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + 'best_fit/{}/tau_2_plus/theta.txt'.format(root))\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_2_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " tau_2_plus = np.loadtxt(output_folder + 'best_fit/{}/tau_2_plus/bin_1_1.txt'.format(root))\n", + " tau_2_plus = np.loadtxt(\n", + " output_folder + \"best_fit/{}/tau_2_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", " mask = (theta_arcmin > theta_min) & (theta_arcmin < theta_max)\n", - " \n", - " plt.plot(theta_arcmin[mask], theta_arcmin[mask]*tau_2_plus[mask], color=color, label=root, alpha=0.5)\n", "\n", + " plt.plot(\n", + " theta_arcmin[mask],\n", + " theta_arcmin[mask] * tau_2_plus[mask],\n", + " color=color,\n", + " label=root,\n", + " alpha=0.5,\n", + " )\n", "\n", - " plt.xlabel(r'$\\theta$ [arcmin]', fontsize=26)\n", - " plt.ylabel(r'$\\theta \\tau_2$', fontsize=26)\n", - " plt.xscale('log')\n", - " #plt.yscale('log')\n", + " plt.xlabel(r\"$\\theta$ [arcmin]\", fontsize=26)\n", + " plt.ylabel(r\"$\\theta \\tau_2$\", fontsize=26)\n", + " plt.xscale(\"log\")\n", + " # plt.yscale('log')\n", " plt.legend(loc=\"upper left\", fontsize=8)\n", "\n", " if savefile is not None:\n", - " plt.savefig(savefile, bbox_inches='tight')\n", + " plt.savefig(savefile, bbox_inches=\"tight\")\n", "\n", " plt.show()" ] @@ -1133,27 +1396,43 @@ "source": [ "root_to_plot = [\n", " \"SP_v1.4.5_A\",\n", - " #\"SP_v1.4.5_A_no_IA\",\n", - " #\"SP_v1.4.5_A_no_dz\",\n", - " #\"SP_v1.4.5_A_no_m_bias\",\n", + " # \"SP_v1.4.5_A_no_IA\",\n", + " # \"SP_v1.4.5_A_no_dz\",\n", + " # \"SP_v1.4.5_A_no_m_bias\",\n", " \"SP_v1.4.5_A_sc_3_150\",\n", " \"SP_v1.4.5_A_sc_3_60\",\n", " \"SP_v1.4.5_A_sc_10_150\",\n", " \"SP_v1.4.5_A_sc_10_60\",\n", " \"SP_v1.4.5_A_sc_5_150\",\n", " \"SP_v1.4.5_A_sc_7_150\",\n", - " #\"SP_v1.4.5_A_no_leakage\"\n", + " # \"SP_v1.4.5_A_no_leakage\"\n", "]\n", "\n", "root_to_plot = [\n", - " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\" for i in [3, 5, 7, 10, 11]\n", + " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\"\n", + " for i in [3, 5, 7, 10, 11]\n", "]\n", "\n", "colours = [\n", - " 'red', 'salmon', 'darkorange', 'forestgreen', 'turquoise', 'darkviolet', 'crimson', 'gold', 'lightcoral', 'mediumseagreen', 'lightsteelblue', 'black', 'silver', 'peru', 'maroon', 'olive'\n", + " \"red\",\n", + " \"salmon\",\n", + " \"darkorange\",\n", + " \"forestgreen\",\n", + " \"turquoise\",\n", + " \"darkviolet\",\n", + " \"crimson\",\n", + " \"gold\",\n", + " \"lightcoral\",\n", + " \"mediumseagreen\",\n", + " \"lightsteelblue\",\n", + " \"black\",\n", + " \"silver\",\n", + " \"peru\",\n", + " \"maroon\",\n", + " \"olive\",\n", "]\n", "\n", - "savefile = 'best_fit_tau_new_binning.png'\n", + "savefile = \"best_fit_tau_new_binning.png\"\n", "\n", "plot_best_fit_tau(root_to_plot, colours, savefile)" ] @@ -1164,21 +1443,44 @@ "metadata": {}, "outputs": [], "source": [ - "pseudo_cell = fits.open('/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.5.fits')[1].data\n", - "cov_pseudo_cell = fits.open('/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.5.fits')\n", + "pseudo_cell = fits.open(\n", + " \"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.5.fits\"\n", + ")[1].data\n", + "cov_pseudo_cell = fits.open(\n", + " \"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.5.fits\"\n", + ")\n", "\n", - "theory_ell = np.loadtxt('/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/ell.txt')\n", - "theory_cell = np.loadtxt('/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/bin_1_1.txt')\n", + "theory_ell = np.loadtxt(\n", + " \"/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/ell.txt\"\n", + ")\n", + "theory_cell = np.loadtxt(\n", + " \"/n09data/guerrini/output_chains/best_fit/SP_v1.4.5_A/shear_cl/bin_1_1.txt\"\n", + ")\n", "\n", "pw = hp.pixwin(1024, lmax=2048)\n", "\n", - "plt.errorbar(pseudo_cell['ELL'], pseudo_cell['ELL']*pseudo_cell['EE'], yerr=pseudo_cell['ELL']*np.sqrt(np.diag(cov_pseudo_cell['COVAR_EE_EE'].data)), capsize=2, c='k', fmt='o', markersize=2)\n", + "plt.errorbar(\n", + " pseudo_cell[\"ELL\"],\n", + " pseudo_cell[\"ELL\"] * pseudo_cell[\"EE\"],\n", + " yerr=pseudo_cell[\"ELL\"] * np.sqrt(np.diag(cov_pseudo_cell[\"COVAR_EE_EE\"].data)),\n", + " capsize=2,\n", + " c=\"k\",\n", + " fmt=\"o\",\n", + " markersize=2,\n", + ")\n", "\n", "mask = (theory_ell > 0.1) & (theory_ell < 2048)\n", - "plt.plot(theory_ell[mask], theory_ell[mask]*theory_cell[mask]*np.interp(theory_ell[mask], np.arange(0, 2049), pw)**2, c='r', label='best-fit $\\\\theta \\in [3-200]$')\n", + "plt.plot(\n", + " theory_ell[mask],\n", + " theory_ell[mask]\n", + " * theory_cell[mask]\n", + " * np.interp(theory_ell[mask], np.arange(0, 2049), pw) ** 2,\n", + " c=\"r\",\n", + " label=\"best-fit $\\\\theta \\\\in [3-200]$\",\n", + ")\n", "\n", - "plt.xlabel(r'$\\ell$', fontsize=26)\n", - "plt.ylabel(r'$\\ell C_\\ell^{EE}$', fontsize=26)\n", + "plt.xlabel(r\"$\\ell$\", fontsize=26)\n", + "plt.ylabel(r\"$\\ell C_\\ell^{EE}$\", fontsize=26)\n", "plt.legend()\n", "plt.savefig(\"SP_v1.4.5_A_cell.png\")\n", "plt.show()" diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/S8_om_sigma8_whisker.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/S8_om_sigma8_whisker.ipynb index 696f9438..7a4fd5d2 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/S8_om_sigma8_whisker.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/S8_om_sigma8_whisker.ipynb @@ -24,83 +24,76 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "sys.path.append(\n", - " \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/\"\n", - ")\n", + "sys.path.append(\"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/\")\n", + "\n", + "import sys\n", + "import warnings\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", - "import warnings\n", - "import sys\n", + "from getdist import plots\n", "\n", "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts\")\n", "\n", "import chain_postprocessing as cp\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", - "plt.rc('text', usetex=True)\n", + "plt.rc(\"text\", usetex=True)\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "root_external = f\"{root_dir}/ext_data/\"\n", - "blind = 'B'\n", + "blind = \"B\"\n", "\n", "roots = [\n", - "f'SP_v1.4.6.3_{blind}_fiducial_config',\n", - "f'SP_v1.4.6.3_leak_corr_{blind}',\n", - "'Planck18',\n", - "'DES Y6',\n", - "'KiDS-Legacy_bandpowers',\n", - "'KiDS-Legacy_cosebis',\n", - "'KiDS-Legacy_xipm',\n", - "'HSC_Y3',\n", - "'HSC_Y3_cell',\n", - "f'SP_v1.4.6.3_{blind}_small_scales_config',\n", - "f'SP_v1.4.6.3_{blind}_flat_alpha_beta_config',\n", - "f'SP_v1.4.6.3_{blind}_no_xi_sys_config',\n", - "f'SP_v1.4.6.3_{blind}_no_leak_corr_config',\n", - "f'SP_v1.4.6.3_{blind}_flat_delta_z_config',\n", - "f'SP_v1.4.6.3_{blind}_no_delta_z_config',\n", - "f'SP_v1.4.6.3_{blind}_flat_ia_config',\n", - "f'SP_v1.4.6.3_{blind}_no_ia_config',\n", - "f'SP_v1.4.6.3_{blind}_no_m_bias_config',\n", - "f'SP_v1.4.6.3_{blind}_unmasked_covmat_config',\n", - "f'SP_v1.4.6.3_{blind}_halofit_config',\n", - "f'SP_v1.4.6.3_{blind}_no_baryons_config',\n", - "f'SP_v1.4.6.3_{blind}_nautilus_config',\n", - "f'SP_v1.4.6.3_{blind}_planck_config',\n", - "f'SP_v1.4.6.3_{blind}_planck_desi_config',\n", - "\n", + " f\"SP_v1.4.6.3_{blind}_fiducial_config\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}\",\n", + " \"Planck18\",\n", + " \"DES Y6\",\n", + " \"KiDS-Legacy_bandpowers\",\n", + " \"KiDS-Legacy_cosebis\",\n", + " \"KiDS-Legacy_xipm\",\n", + " \"HSC_Y3\",\n", + " \"HSC_Y3_cell\",\n", + " f\"SP_v1.4.6.3_{blind}_small_scales_config\",\n", + " f\"SP_v1.4.6.3_{blind}_flat_alpha_beta_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_xi_sys_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_leak_corr_config\",\n", + " f\"SP_v1.4.6.3_{blind}_flat_delta_z_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_delta_z_config\",\n", + " f\"SP_v1.4.6.3_{blind}_flat_ia_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_ia_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_m_bias_config\",\n", + " f\"SP_v1.4.6.3_{blind}_unmasked_covmat_config\",\n", + " f\"SP_v1.4.6.3_{blind}_halofit_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_baryons_config\",\n", + " f\"SP_v1.4.6.3_{blind}_nautilus_config\",\n", + " f\"SP_v1.4.6.3_{blind}_planck_config\",\n", + " f\"SP_v1.4.6.3_{blind}_planck_desi_config\",\n", "]\n", "\n", "legend_labels = [\n", " r\"UNIONS-3500 $\\xi_{\\pm}(\\theta)$ (This work)\",\n", - " \n", " r\"UNIONS-3500 $C_\\ell$ (Guerrini et al. 2026)\",\n", - " \n", " r\"$\\textit{Planck}$ 2018\",\n", - " r'DES Y6 $\\xi_{\\pm}$, NLA',\n", + " r\"DES Y6 $\\xi_{\\pm}$, NLA\",\n", " r\"KiDS-Legacy Bandpowers ($C_{\\rm E}$)\",\n", " r\"KiDS-Legacy COSEBIs ($E_n$)\",\n", " r\"KiDS-Legacy $\\xi_{\\pm}(\\theta)$\",\n", " r\"HSC-Y3 $\\xi_{\\pm}(\\theta)$\",\n", " r\"HSC-Y3 $C_\\ell$\",\n", - " \n", " r\"$\\xi_+$ small scales, $\\theta$=[5,83] arcmin\",\n", " r\"Flat $\\alpha_{\\rm{PSF}}$ and $\\beta_{\\rm{PSF}}$ priors\",\n", " r\"No $\\xi^{\\rm sys}_{\\pm}$\",\n", @@ -116,12 +109,11 @@ " r\"Nautilus sampler\",\n", " r\"UNIONS-3500 + $\\textit{Planck}$\",\n", " r\"UNIONS-3500 + $\\textit{Planck}$ + DESI BAO\",\n", - " ]\n", + "]\n", "\n", "categories = [\n", " \"configuration\",\n", - " \"harmonic\", \n", - " \n", + " \"harmonic\",\n", " \"external\",\n", " \"external\",\n", " \"external\",\n", @@ -129,7 +121,6 @@ " \"external\",\n", " \"external\",\n", " \"external\",\n", - " \n", " \"configuration\",\n", " \"configuration\",\n", " \"configuration\",\n", @@ -145,21 +136,17 @@ " \"configuration\",\n", " \"configuration\",\n", " \"configuration\",\n", - " \n", - "\n", "]\n", "colours = [\n", " \"darkorange\",\n", " \"royalblue\",\n", - "\n", - " \"violet\", \n", + " \"violet\",\n", " \"black\",\n", " \"black\",\n", " \"black\",\n", " \"black\",\n", " \"black\",\n", " \"black\",\n", - " \n", " \"forestgreen\",\n", " \"forestgreen\",\n", " \"forestgreen\",\n", @@ -175,7 +162,7 @@ " \"forestgreen\",\n", " \"forestgreen\",\n", " \"forestgreen\",\n", - "]\n" + "]" ] }, { @@ -188,53 +175,38 @@ "chains = []\n", "for i, root in enumerate(roots):\n", " category = categories[i]\n", - " if root =='DES Y6':\n", + " if root == \"DES Y6\":\n", " continue\n", " if category != \"external\":\n", - " if category == 'configuration':\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/getdist_{root}\"\n", - " )\n", - " elif category == 'harmonic':\n", + " if category == \"configuration\":\n", + " path_samples = os.path.join(root_dir, f\"{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"{root}/getdist_{root}\")\n", + " elif category == \"harmonic\":\n", " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/samples_{root}_cell.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/getdist_{root}\"\n", + " root_dir, f\"{root}/{root}/samples_{root}_cell.txt\"\n", " )\n", + " path_getdist = os.path.join(root_dir, f\"{root}/{root}/getdist_{root}\")\n", " elif category == \"external_compute_sample\":\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", - " )\n", + " path_samples = os.path.join(root_dir, f\"ext_data/{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", " else:\n", " raise ValueError(f\"The category, {category}, of {root} is not correct\")\n", - " if 'nautilus' not in root:\n", - " cp.load_samples_and_write_paramnames(path_samples, path_getdist+\".paramnames\")\n", - " cp.write_samples_getdist_format(path_samples, path_getdist+\".txt\")\n", + " if \"nautilus\" not in root:\n", + " cp.load_samples_and_write_paramnames(\n", + " path_samples, path_getdist + \".paramnames\"\n", + " )\n", + " cp.write_samples_getdist_format(path_samples, path_getdist + \".txt\")\n", " else:\n", - " cp.load_samples_and_write_paramnames(path_samples, path_getdist+\".paramnames\", chain_type='nautilus')\n", - " cp.write_samples_getdist_format(path_samples, path_getdist+\".txt\", chain_type='nautilus')\n", + " cp.load_samples_and_write_paramnames(\n", + " path_samples, path_getdist + \".paramnames\", chain_type=\"nautilus\"\n", + " )\n", + " cp.write_samples_getdist_format(\n", + " path_samples, path_getdist + \".txt\", chain_type=\"nautilus\"\n", + " )\n", " chains.append(cp.load_chain(path_getdist, smoothing_scale=0.5))\n", " else:\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", - " )\n", - " chains.append(\n", - " cp.load_chain(path_getdist)\n", - " )" + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", + " chains.append(cp.load_chain(path_getdist))" ] }, { @@ -244,8 +216,32 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','S_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_b h^2', r'h_0', r'n_s', r'\\sigma_8', r'S_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_b h^2\",\n", + " r\"h_0\",\n", + " r\"n_s\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for i, chain in enumerate(chains):\n", " print(legend_labels[i])\n", @@ -253,7 +249,7 @@ " for name, label in zip(name_list, label_list):\n", " try:\n", " param_names.parWithName(name).label = label\n", - " except:\n", + " except Exception:\n", " warnings.warn(f\"Parameter {name} not found in chain {roots[i]}.\")" ] }, @@ -268,18 +264,18 @@ "\n", "# Account for the missing parameter conventions\n", "\n", - "idx = roots.index('KiDS-Legacy_xipm')\n", + "idx = roots.index(\"KiDS-Legacy_xipm\")\n", "cp.derive_parameter_S8(chains[idx])\n", "\n", - "idx = roots.index('KiDS-Legacy_bandpowers')\n", + "idx = roots.index(\"KiDS-Legacy_bandpowers\")\n", "cp.derive_parameter_S8(chains[idx])\n", "\n", - "idx = roots.index('KiDS-Legacy_cosebis')\n", + "idx = roots.index(\"KiDS-Legacy_cosebis\")\n", "cp.derive_parameter_S8(chains[idx])\n", "\n", - "#OMEGA_M not in HSC_Y3_cell\n", - "idx = roots.index('HSC_Y3_cell')\n", - "cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')" + "# OMEGA_M not in HSC_Y3_cell\n", + "idx = roots.index(\"HSC_Y3_cell\")\n", + "cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")" ] }, { @@ -289,43 +285,77 @@ "metadata": {}, "outputs": [], "source": [ - "param_values = np.array([\"# Expt\", \"Colour\", \"S8_Mean\", \"S8_low\", \"S8_high\", \"sigma_8_Mean\", \"sigma_8_low\", \"sigma_8_high\", \"Omega_m_Mean\", \"Omega_m_low\", \"Omega_m_high\"])\n", - "escaped = np.char.replace(legend_labels, '\\\\', '\\\\\\\\')\n", + "param_values = np.array(\n", + " [\n", + " \"# Expt\",\n", + " \"Colour\",\n", + " \"S8_Mean\",\n", + " \"S8_low\",\n", + " \"S8_high\",\n", + " \"sigma_8_Mean\",\n", + " \"sigma_8_low\",\n", + " \"sigma_8_high\",\n", + " \"Omega_m_Mean\",\n", + " \"Omega_m_low\",\n", + " \"Omega_m_high\",\n", + " ]\n", + ")\n", + "escaped = np.char.replace(legend_labels, \"\\\\\", \"\\\\\\\\\")\n", "\n", "for i, root in enumerate(roots):\n", " chain = chains[i]\n", - " if root =='DES Y6':\n", - " param_values = np.vstack((\n", - " param_values,\n", - " [escaped[i],\n", - " colours[i],\n", - " 0.798, 0.015, 0.014, 0.763, 0.057, 0.050, 0.332, 0.040, 0.035]\n", - " ))\n", + " if root == \"DES Y6\":\n", + " param_values = np.vstack(\n", + " (\n", + " param_values,\n", + " [\n", + " escaped[i],\n", + " colours[i],\n", + " 0.798,\n", + " 0.015,\n", + " 0.014,\n", + " 0.763,\n", + " 0.057,\n", + " 0.050,\n", + " 0.332,\n", + " 0.040,\n", + " 0.035,\n", + " ],\n", + " )\n", + " )\n", " else:\n", - " best_fit_params = cp.extract_best_fit_params(chain, best_fit_method='2Dkde')\n", + " best_fit_params = cp.extract_best_fit_params(chain, best_fit_method=\"2Dkde\")\n", " margestats = chain.getMargeStats()\n", "\n", - " s8_stats = margestats.parWithName('S_8')\n", - " sigma8_stats = margestats.parWithName('SIGMA_8')\n", - " omegam_stats = margestats.parWithName('OMEGA_M')\n", - "\n", - " param_values = np.vstack((\n", - " param_values,\n", - " [escaped[i],\n", - " colours[i],\n", - " best_fit_params['S_8'],\n", - " best_fit_params['S_8'] - s8_stats.limits[0].lower,\n", - " s8_stats.limits[0].upper - best_fit_params['S_8'],\n", - " best_fit_params['SIGMA_8'],\n", - " best_fit_params['SIGMA_8'] - sigma8_stats.limits[0].lower,\n", - " sigma8_stats.limits[0].upper - best_fit_params['SIGMA_8'],\n", - " best_fit_params['OMEGA_M'],\n", - " best_fit_params['OMEGA_M'] - omegam_stats.limits[0].lower,\n", - " omegam_stats.limits[0].upper - best_fit_params['OMEGA_M'],\n", - " ]\n", - " ))\n", + " s8_stats = margestats.parWithName(\"S_8\")\n", + " sigma8_stats = margestats.parWithName(\"SIGMA_8\")\n", + " omegam_stats = margestats.parWithName(\"OMEGA_M\")\n", + "\n", + " param_values = np.vstack(\n", + " (\n", + " param_values,\n", + " [\n", + " escaped[i],\n", + " colours[i],\n", + " best_fit_params[\"S_8\"],\n", + " best_fit_params[\"S_8\"] - s8_stats.limits[0].lower,\n", + " s8_stats.limits[0].upper - best_fit_params[\"S_8\"],\n", + " best_fit_params[\"SIGMA_8\"],\n", + " best_fit_params[\"SIGMA_8\"] - sigma8_stats.limits[0].lower,\n", + " sigma8_stats.limits[0].upper - best_fit_params[\"SIGMA_8\"],\n", + " best_fit_params[\"OMEGA_M\"],\n", + " best_fit_params[\"OMEGA_M\"] - omegam_stats.limits[0].lower,\n", + " omegam_stats.limits[0].upper - best_fit_params[\"OMEGA_M\"],\n", + " ],\n", + " )\n", + " )\n", "print(param_values)\n", - "np.savetxt(f\"{root_dir}/param_values.txt\", param_values, fmt=['%s' for i in range(11)], delimiter=';')" + "np.savetxt(\n", + " f\"{root_dir}/param_values.txt\",\n", + " param_values,\n", + " fmt=[\"%s\" for i in range(11)],\n", + " delimiter=\";\",\n", + ")" ] }, { @@ -336,20 +366,50 @@ "outputs": [], "source": [ "# Load the value of the parameters\n", - "cosmo = np.loadtxt(f\"{root_dir}/param_values.txt\",\n", - " dtype={'names': ('Expt', 'colour', 's8_mean', 's8_low', 's8_high', 'sigma8_mean', 'sigma8_low', 'sigma8_high', 'omegam_mean', 'omegam_low', 'omegam_high'),\n", - " 'formats': ('U250', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20')}, skiprows=1, delimiter=';')\n", - "expt = np.char.replace(cosmo['Expt'], '\\\\\\\\', '\\\\')\n", - "colours = cosmo['colour']\n", - "s8_mean = cosmo['s8_mean'].astype(np.float64)\n", - "s8_low = cosmo['s8_low'].astype(np.float64)\n", - "s8_high = cosmo['s8_high'].astype(np.float64)\n", - "sigma8_mean = cosmo['sigma8_mean'].astype(np.float64)\n", - "sigma8_low = cosmo['sigma8_low'].astype(np.float64)\n", - "sigma8_high = cosmo['sigma8_high'].astype(np.float64)\n", - "omegam_mean = cosmo['omegam_mean'].astype(np.float64)\n", - "omegam_low = cosmo['omegam_low'].astype(np.float64)\n", - "omegam_high = cosmo['omegam_high'].astype(np.float64)" + "cosmo = np.loadtxt(\n", + " f\"{root_dir}/param_values.txt\",\n", + " dtype={\n", + " \"names\": (\n", + " \"Expt\",\n", + " \"colour\",\n", + " \"s8_mean\",\n", + " \"s8_low\",\n", + " \"s8_high\",\n", + " \"sigma8_mean\",\n", + " \"sigma8_low\",\n", + " \"sigma8_high\",\n", + " \"omegam_mean\",\n", + " \"omegam_low\",\n", + " \"omegam_high\",\n", + " ),\n", + " \"formats\": (\n", + " \"U250\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " ),\n", + " },\n", + " skiprows=1,\n", + " delimiter=\";\",\n", + ")\n", + "expt = np.char.replace(cosmo[\"Expt\"], \"\\\\\\\\\", \"\\\\\")\n", + "colours = cosmo[\"colour\"]\n", + "s8_mean = cosmo[\"s8_mean\"].astype(np.float64)\n", + "s8_low = cosmo[\"s8_low\"].astype(np.float64)\n", + "s8_high = cosmo[\"s8_high\"].astype(np.float64)\n", + "sigma8_mean = cosmo[\"sigma8_mean\"].astype(np.float64)\n", + "sigma8_low = cosmo[\"sigma8_low\"].astype(np.float64)\n", + "sigma8_high = cosmo[\"sigma8_high\"].astype(np.float64)\n", + "omegam_mean = cosmo[\"omegam_mean\"].astype(np.float64)\n", + "omegam_low = cosmo[\"omegam_low\"].astype(np.float64)\n", + "omegam_high = cosmo[\"omegam_high\"].astype(np.float64)" ] }, { @@ -380,18 +440,11 @@ " r\"UNIONS-3500 $C_\\ell$ (Guerrini et al. 2026)\",\n", " r\"HSC-Y3 $C_\\ell$\",\n", " r\"$\\xi_+$ small scales, $\\theta$=[5,83] arcmin\",\n", - " r\"Unmasked covmat\", \n", + " r\"Unmasked covmat\",\n", " r\"$\\texttt{HMCode}$ no baryons\",\n", " r\"Nautilus sampler\",\n", "]\n", - "list_section_index = [\n", - " r\"(ii)\",\n", - " r\"(iii)\",\n", - " r\"(iv)\",\n", - " r\"(v)\",\n", - " r\"(vi)\",\n", - " r\"(vii)\"\n", - "]\n", + "list_section_index = [r\"(ii)\", r\"(iii)\", r\"(iv)\", r\"(v)\", r\"(vi)\", r\"(vii)\"]\n", "\n", "preliminary_watermark = False\n", "blind_axes = False\n", @@ -403,73 +456,151 @@ "for ax, param in zip(axs, params):\n", " means, lows, highs, label = param\n", " for i, mean, low, high, color in zip(y, means, lows, highs, colours):\n", - " ax.errorbar(mean, 0.05+i*row_spacing, xerr=np.array([low, high])[:, None], fmt='o', color=color, ecolor=color, elinewidth=2, capsize=3)\n", + " ax.errorbar(\n", + " mean,\n", + " 0.05 + i * row_spacing,\n", + " xerr=np.array([low, high])[:, None],\n", + " fmt=\"o\",\n", + " color=color,\n", + " ecolor=color,\n", + " elinewidth=2,\n", + " capsize=3,\n", + " )\n", " ax.set_xlabel(label, fontsize=14)\n", - " \n", + "\n", " ax.grid(False)\n", - " ax.tick_params(axis='y', left=False, labelleft=False)\n", + " ax.tick_params(axis=\"y\", left=False, labelleft=False)\n", " if label == r\"$S_8$\":\n", - " ax.axvspan(s8_mean[index_ref] - s8_low[index_ref], s8_mean[index_ref] + s8_high[index_ref], color=colours[index_ref], alpha=0.2)\n", + " ax.axvspan(\n", + " s8_mean[index_ref] - s8_low[index_ref],\n", + " s8_mean[index_ref] + s8_high[index_ref],\n", + " color=colours[index_ref],\n", + " alpha=0.2,\n", + " )\n", " ax.set_xlim(0.6, 1.35)\n", " if blind_axes:\n", " ref_tick = np.mean(s8_mean[:4])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.1 for i in range(-5, 5)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.1 for i in range(-5, 5)], labels=[])\n", " elif label == r\"$\\sigma_8$\":\n", - " ax.axvspan(sigma8_mean[index_ref] - sigma8_low[index_ref], sigma8_mean[index_ref] + sigma8_high[index_ref], color=colours[index_ref], alpha=0.2)\n", + " ax.axvspan(\n", + " sigma8_mean[index_ref] - sigma8_low[index_ref],\n", + " sigma8_mean[index_ref] + sigma8_high[index_ref],\n", + " color=colours[index_ref],\n", + " alpha=0.2,\n", + " )\n", " ax.set_xlim(0.5, 1.35)\n", " if blind_axes:\n", " ref_tick = np.mean(sigma8_mean[:4])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.2 for i in range(-2, 2)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.2 for i in range(-2, 2)], labels=[])\n", " elif label == r\"$\\Omega_{\\rm m}$\":\n", - " ax.axvspan(omegam_mean[index_ref] - omegam_low[index_ref], omegam_mean[index_ref] + omegam_high[index_ref], color=colours[index_ref], alpha=0.2)\n", + " ax.axvspan(\n", + " omegam_mean[index_ref] - omegam_low[index_ref],\n", + " omegam_mean[index_ref] + omegam_high[index_ref],\n", + " color=colours[index_ref],\n", + " alpha=0.2,\n", + " )\n", " ax.set_xlim(0.1, 0.5)\n", " if blind_axes:\n", " ref_tick = np.mean(omegam_mean[:4])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.1 for i in range(-2, 3)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.1 for i in range(-2, 3)], labels=[])\n", "\n", "\n", - "ax1.set_yticks(0.01+y*row_spacing)\n", + "ax1.set_yticks(0.01 + y * row_spacing)\n", "ax1.set_yticklabels([])\n", "for label, color in zip(expt, colours):\n", - " if 'This work' in label:\n", - " label_bold = r\"$\\bf{UNIONS}$-$\\bf{3500}$ $\\xi_{\\pm}(\\theta)$ $\\bf{(This\\ work)}$\"\n", - " ax1.text(-0.6, 0.05 + row_spacing * np.where(expt == label)[0][0], label_bold, fontsize=12, ha='left', va='center', color=color)\n", - " else: \n", - " ax1.text(-0.6, 0.05 + row_spacing * np.where(expt == label)[0][0], label, fontsize=12, ha='left', va='center', color=color)\n", + " if \"This work\" in label:\n", + " label_bold = (\n", + " r\"$\\bf{UNIONS}$-$\\bf{3500}$ $\\xi_{\\pm}(\\theta)$ $\\bf{(This\\ work)}$\"\n", + " )\n", + " ax1.text(\n", + " -0.6,\n", + " 0.05 + row_spacing * np.where(expt == label)[0][0],\n", + " label_bold,\n", + " fontsize=12,\n", + " ha=\"left\",\n", + " va=\"center\",\n", + " color=color,\n", + " )\n", + " else:\n", + " ax1.text(\n", + " -0.6,\n", + " 0.05 + row_spacing * np.where(expt == label)[0][0],\n", + " label,\n", + " fontsize=12,\n", + " ha=\"left\",\n", + " va=\"center\",\n", + " color=color,\n", + " )\n", " if label != reference:\n", " index = np.where(expt == label)[0][0]\n", " s8_tension = cp.get_sigma_tension(\n", - " s8_mean[index], s8_low[index], s8_high[index],\n", - " s8_mean[index_ref], s8_low[index_ref], s8_high[index_ref]\n", + " s8_mean[index],\n", + " s8_low[index],\n", + " s8_high[index],\n", + " s8_mean[index_ref],\n", + " s8_low[index_ref],\n", + " s8_high[index_ref],\n", " )\n", " sign_str = \"+\" if s8_tension > 0 else \"-\"\n", - " ax1.text(1.32, 0.05 + row_spacing * index, rf\"${sign_str}{np.abs(s8_tension):.2f}\" + r\"\\, \\sigma$\", fontsize=10, ha='right', va='center', color=color)\n", + " ax1.text(\n", + " 1.32,\n", + " 0.05 + row_spacing * index,\n", + " rf\"${sign_str}{np.abs(s8_tension):.2f}\" + r\"\\, \\sigma$\",\n", + " fontsize=10,\n", + " ha=\"right\",\n", + " va=\"center\",\n", + " color=color,\n", + " )\n", "# Add separation lines\n", "for i, sep in enumerate(separation_after):\n", " print(sep)\n", " index_sep = np.where(expt == sep)[0][0]\n", - " ax2.axhline(row_spacing * (index_sep + 1) - 0.07, color='black', linestyle='dotted', linewidth=1)\n", - " ax3.axhline(row_spacing * (index_sep + 1) - 0.07, color='black', linestyle='dotted', linewidth=1)\n", - " ax1.axhline(row_spacing * (index_sep + 1) - 0.07, xmin=-1.8,color='black', linestyle='dotted', linewidth=1, clip_on=False)\n", - " ax1.text(-0.61, row_spacing * (index_sep + 1) + 0.05, \n", - " list_section_index[i], fontsize=12, fontweight='bold', va='center', ha='right')\n", + " ax2.axhline(\n", + " row_spacing * (index_sep + 1) - 0.07,\n", + " color=\"black\",\n", + " linestyle=\"dotted\",\n", + " linewidth=1,\n", + " )\n", + " ax3.axhline(\n", + " row_spacing * (index_sep + 1) - 0.07,\n", + " color=\"black\",\n", + " linestyle=\"dotted\",\n", + " linewidth=1,\n", + " )\n", + " ax1.axhline(\n", + " row_spacing * (index_sep + 1) - 0.07,\n", + " xmin=-1.8,\n", + " color=\"black\",\n", + " linestyle=\"dotted\",\n", + " linewidth=1,\n", + " clip_on=False,\n", + " )\n", + " ax1.text(\n", + " -0.61,\n", + " row_spacing * (index_sep + 1) + 0.05,\n", + " list_section_index[i],\n", + " fontsize=12,\n", + " fontweight=\"bold\",\n", + " va=\"center\",\n", + " ha=\"right\",\n", + " )\n", "\n", "\n", "# --- Add section label (i)) ---\n", - "ax1.text(-0.61, 0.05, \n", - " r\"(i)\", fontsize=12, fontweight='bold', va='center', ha='right')\n", + "ax1.text(-0.61, 0.05, r\"(i)\", fontsize=12, fontweight=\"bold\", va=\"center\", ha=\"right\")\n", "\n", "if preliminary_watermark:\n", - " plt.figtext(0.5, 0.5, 'PRELIMINARY',\n", - " fontsize=50, color='gray',\n", - " ha='center', va='center',\n", - " alpha=0.3, rotation=330)\n", + " plt.figtext(\n", + " 0.5,\n", + " 0.5,\n", + " \"PRELIMINARY\",\n", + " fontsize=50,\n", + " color=\"gray\",\n", + " ha=\"center\",\n", + " va=\"center\",\n", + " alpha=0.3,\n", + " rotation=330,\n", + " )\n", "\n", "plt.gca().invert_yaxis()\n", "\n", @@ -477,7 +608,7 @@ "\n", "# plt.savefig(\"./plots/whisker_plot.png\", dpi=300)\n", "# #Save pdf\n", - "plt.savefig(\"../Plots/S8_whisker_plot.pdf\", bbox_inches='tight')\n", + "plt.savefig(\"../Plots/S8_whisker_plot.pdf\", bbox_inches=\"tight\")\n", "plt.show()" ] }, diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/best_fit_xipm.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/best_fit_xipm.ipynb index 2ba72ab9..d6ed4c01 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/best_fit_xipm.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/best_fit_xipm.ipynb @@ -18,34 +18,22 @@ "outputs": [], "source": [ "import os\n", - "import configparser\n", - "import subprocess\n", "import sys\n", - "import warnings\n", "\n", - "import sys\n", "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts\")\n", "\n", "import chain_postprocessing as cp\n", - "\n", - "from getdist import plots, loadMCSamples\n", - "from astropy.io import fits\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "from scipy.interpolate import interp1d\n", - "import scipy.stats as stats\n", - "from IPython.display import Markdown, display\n", - "import healpy as hp\n", "import matplotlib.scale as mscale\n", - "import matplotlib.ticker as ticker\n", - "import matplotlib.transforms as mtransforms\n", + "import numpy as np\n", "import seaborn as sns\n", + "from astropy.io import fits\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "from sp_validation.rho_tau import SquareRootScale\n", + "\n", "mscale.register_scale(SquareRootScale)\n", "\n", "plt.rcParams[\"text.usetex\"] = True\n", @@ -53,8 +41,8 @@ "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=40\n", - "g.settings.axes_labelsize=40\n", + "g.settings.axes_fontsize = 40\n", + "g.settings.axes_labelsize = 40\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 50\n", "\n", @@ -71,22 +59,21 @@ "label_fiducial_xi = r\"UNIONS $\\xi_{\\pm}$\"\n", "\n", "# Path to the ini files used\n", - "path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config'\n", - "path_datavectors = '/home/guerrini/sp_validation/cosmo_inference/data/'\n", + "path_ini_files = \"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config\"\n", + "path_datavectors = \"/home/guerrini/sp_validation/cosmo_inference/data/\"\n", "path_output_chains = \"/n09data/guerrini/output_chains/\"\n", "\n", "\n", "data_cell = fits.open(\n", " os.path.join(\n", - " path_datavectors,\n", - " f\"{fiducial_root_cell}/cosmosis_{fiducial_root_cell}.fits\"\n", + " path_datavectors, f\"{fiducial_root_cell}/cosmosis_{fiducial_root_cell}.fits\"\n", " )\n", ")\n", "\n", "data_xi = fits.open(\n", " os.path.join(\n", " path_datavectors,\n", - " f\"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits\"\n", + " f\"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits\",\n", " )\n", ")\n", "\n", @@ -100,34 +87,39 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "# Perform the computation for the fiducial of Cell\n", "path_samples_fiducial_cell = os.path.join(\n", " path_output_chains,\n", " fiducial_root_cell,\n", " fiducial_root_cell,\n", - " f\"samples_{fiducial_root_cell}_cell.txt\"\n", + " f\"samples_{fiducial_root_cell}_cell.txt\",\n", ")\n", "path_gd_fiducial_cell = os.path.join(\n", " path_output_chains,\n", " fiducial_root_cell,\n", " fiducial_root_cell,\n", - " f\"getdist_{fiducial_root_cell}_cell\"\n", + " f\"getdist_{fiducial_root_cell}_cell\",\n", + ")\n", + "cp.load_samples_and_write_paramnames(\n", + " path_samples_fiducial_cell, path_gd_fiducial_cell + \".paramnames\"\n", + ")\n", + "cp.write_samples_getdist_format(\n", + " path_samples_fiducial_cell, path_gd_fiducial_cell + \".txt\", chain_type=\"polychord\"\n", ")\n", - "cp.load_samples_and_write_paramnames(path_samples_fiducial_cell, path_gd_fiducial_cell+\".paramnames\")\n", - "cp.write_samples_getdist_format(path_samples_fiducial_cell, path_gd_fiducial_cell+\".txt\", chain_type='polychord')\n", "\n", "chain_fiducial_cell = cp.load_chain(path_gd_fiducial_cell, smoothing_scale=0.5)\n", "\n", - "best_fit_params_fiducial_cell = cp.extract_best_fit_params(chain_fiducial_cell, best_fit_method='2Dkde')\n", + "best_fit_params_fiducial_cell = cp.extract_best_fit_params(\n", + " chain_fiducial_cell, best_fit_method=\"2Dkde\"\n", + ")\n", "\n", "cp.compute_best_fit(\n", " path_ini_files,\n", " best_fit_params_fiducial_cell,\n", " fiducial_root_cell,\n", " is_harmonic=True,\n", - " blind=blind\n", - ")\n" + " blind=blind,\n", + ")" ] }, { @@ -137,29 +129,32 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "# Perform the computation for the fiducial of xi\n", "path_samples_fiducial_xi = os.path.join(\n", " path_output_chains,\n", " fiducial_root_xi_chains,\n", - " f\"samples_{fiducial_root_xi_chains}.txt\"\n", + " f\"samples_{fiducial_root_xi_chains}.txt\",\n", ")\n", "\n", "path_gd_fiducial_xi = os.path.join(\n", - " path_output_chains,\n", - " fiducial_root_xi_chains,\n", - " f\"getdist_{fiducial_root_xi_chains}\"\n", + " path_output_chains, fiducial_root_xi_chains, f\"getdist_{fiducial_root_xi_chains}\"\n", + ")\n", + "cp.load_samples_and_write_paramnames(\n", + " path_samples_fiducial_xi, path_gd_fiducial_xi + \".paramnames\"\n", + ")\n", + "cp.write_samples_getdist_format(\n", + " path_samples_fiducial_xi, path_gd_fiducial_xi + \".txt\", chain_type=\"polychord\"\n", ")\n", - "cp.load_samples_and_write_paramnames(path_samples_fiducial_xi, path_gd_fiducial_xi+\".paramnames\")\n", - "cp.write_samples_getdist_format(path_samples_fiducial_xi, path_gd_fiducial_xi+\".txt\", chain_type='polychord')\n", "\n", "chain_fiducial_xi = cp.load_chain(path_gd_fiducial_xi, smoothing_scale=0.5)\n", "\n", - "best_fit_params_fiducial_xi = cp.extract_best_fit_params(chain_fiducial_xi, best_fit_method='2Dkde')\n", + "best_fit_params_fiducial_xi = cp.extract_best_fit_params(\n", + " chain_fiducial_xi, best_fit_method=\"2Dkde\"\n", + ")\n", "\n", "ini_file_root = os.path.join(\n", " path_ini_files,\n", - " f'config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini'\n", + " f\"config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini\",\n", ")\n", "cp.compute_best_fit(\n", " path_ini_files,\n", @@ -167,7 +162,7 @@ " fiducial_root_xi_chains,\n", " is_harmonic=False,\n", " blind=blind,\n", - " ini_file_root=ini_file_root\n", + " ini_file_root=ini_file_root,\n", ")" ] }, @@ -190,15 +185,22 @@ "]\n", "\n", "line_args = [\n", - " {'color': 'royalblue', 'linestyle': '-'},\n", - " {'color': 'orange', 'linestyle': '-'}\n", + " {\"color\": \"royalblue\", \"linestyle\": \"-\"},\n", + " {\"color\": \"orange\", \"linestyle\": \"-\"},\n", "]\n", "\n", "properties = {}\n", "\n", - "properties = cp.update_properties_w_roots(properties, fiducial_root_cell, path_ini_files, with_configuration=False)\n", - "properties = cp.update_properties_w_roots(properties, fiducial_root_xi_chains, path_ini_files, with_configuration=True, path_to_this_ini=ini_file_root)\n", - "\n" + "properties = cp.update_properties_w_roots(\n", + " properties, fiducial_root_cell, path_ini_files, with_configuration=False\n", + ")\n", + "properties = cp.update_properties_w_roots(\n", + " properties,\n", + " fiducial_root_xi_chains,\n", + " path_ini_files,\n", + " with_configuration=True,\n", + " path_to_this_ini=ini_file_root,\n", + ")" ] }, { @@ -208,53 +210,92 @@ "metadata": {}, "outputs": [], "source": [ - "root_to_plot = [\n", - " fiducial_root_cell,\n", - " fiducial_root_xi_chains\n", - "]\n", - "labels = [\n", - " r\"Best fit $C_\\ell$\",\n", - " r\"Best fit $\\xi_\\pm(\\theta)$\"\n", - "]\n", + "root_to_plot = [fiducial_root_cell, fiducial_root_xi_chains]\n", + "labels = [r\"Best fit $C_\\ell$\", r\"Best fit $\\xi_\\pm(\\theta)$\"]\n", "path_best_fit_xi_theta = os.path.join(\n", - " path_output_chains,\n", - " fiducial_root_xi_chains,\n", - " \"best_fit/shear_xi_plus/\"\n", - " f\"theta.txt\"\n", + " path_output_chains, fiducial_root_xi_chains, \"best_fit/shear_xi_plus/theta.txt\"\n", ")\n", "\n", "theta_rad = np.loadtxt(path_best_fit_xi_theta)\n", "theta_min = 1\n", "theta_max = 250\n", "\n", - "cp.compute_best_fit_xi_from_cell(path_output_chains, fiducial_root_cell, best_fit_params_fiducial_cell, theta_rad)\n", + "cp.compute_best_fit_xi_from_cell(\n", + " path_output_chains, fiducial_root_cell, best_fit_params_fiducial_cell, theta_rad\n", + ")\n", "\n", - "data = fits.open(os.path.join(path_datavectors,f\"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits\"))\n", + "data = fits.open(\n", + " os.path.join(\n", + " path_datavectors,\n", + " f\"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits\",\n", + " )\n", + ")\n", "bbox_to_anchor_xip = (0.685, 0.09)\n", "bbox_to_anchor_xim = (0.3, 0.65)\n", - "xi_p_data = data['XI_PLUS'].data\n", - "xi_m_data = data['XI_MINUS'].data\n", - "cov_mat = data['COVMAT'].data\n", + "xi_p_data = data[\"XI_PLUS\"].data\n", + "xi_m_data = data[\"XI_MINUS\"].data\n", + "cov_mat = data[\"COVMAT\"].data\n", "\n", "# Plot hyperparameter\n", "loc_legend = \"lower center\"\n", "\n", - "fig, [ax,ax2] = plt.subplots(1, 2, figsize=(20, 8))\n", - "\n", - "theta, xi_p, xi_m = xi_p_data['ANG'], xi_p_data['VALUE'], xi_m_data['VALUE']\n", - "ax.errorbar(theta, theta*xi_p, yerr=theta*np.sqrt(np.diag(cov_mat[:len(theta),:len(theta)])), fmt='o', label=r\"UNIONS $\\xi_+$ data\", color='black', capsize=2)\n", - "ax2.errorbar(theta, theta*xi_m, yerr=theta*np.sqrt(np.diag(cov_mat[len(theta):2*len(theta),len(theta):2*len(theta)])), fmt='o', label=r\"UNIONS $\\xi_-$ data\", color='black', capsize=2)\n", + "fig, [ax, ax2] = plt.subplots(1, 2, figsize=(20, 8))\n", + "\n", + "theta, xi_p, xi_m = xi_p_data[\"ANG\"], xi_p_data[\"VALUE\"], xi_m_data[\"VALUE\"]\n", + "ax.errorbar(\n", + " theta,\n", + " theta * xi_p,\n", + " yerr=theta * np.sqrt(np.diag(cov_mat[: len(theta), : len(theta)])),\n", + " fmt=\"o\",\n", + " label=r\"UNIONS $\\xi_+$ data\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", + "ax2.errorbar(\n", + " theta,\n", + " theta * xi_m,\n", + " yerr=theta\n", + " * np.sqrt(\n", + " np.diag(cov_mat[len(theta) : 2 * len(theta), len(theta) : 2 * len(theta)])\n", + " ),\n", + " fmt=\"o\",\n", + " label=r\"UNIONS $\\xi_-$ data\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", "\n", "for idx, (label, root) in enumerate(zip(labels, root_to_plot)):\n", - " #Read the results\n", - " theta = (np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_plus/theta.txt'.format(root))) * 180/np.pi * 60\n", - " xi_plus = np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " xi_minus = np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " if '$C_\\ell$' not in label:\n", - " xi_sys_plus = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60\n", - " \n", + " # Read the results\n", + " theta = (\n", + " (\n", + " np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", + " )\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + " xi_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_minus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " if r\"$C_\\ell$\" not in label:\n", + " xi_sys_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = (\n", + " np.loadtxt(path_output_chains + \"{}/best_fit/xi_sys/theta.txt\".format(root))\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + "\n", " xi_sys_plus = np.interp(theta, theta_xi_sys, xi_sys_plus)\n", " xi_sys_minus = np.interp(theta, theta_xi_sys, xi_sys_minus)\n", " xi_plus += xi_sys_plus\n", @@ -262,84 +303,120 @@ "\n", " mask = (theta > theta_min) & (theta < theta_max)\n", " theta = theta[mask]\n", - " ax.plot(theta, theta*xi_plus[mask], label=r\"Best fit $\\xi_+(\\theta)$\", **line_args[idx], lw=2.5)\n", - " ax.plot(theta, theta*xi_sys_plus[mask], label=r\"Best fit $\\xi^{\\rm sys}_{+}(\\theta)$\", c='r')\n", - " ax2.plot(theta, theta*xi_minus[mask], label=r\"Best fit $\\xi_-(\\theta)$\", **line_args[idx], lw=2.5)\n", - " ax2.plot(theta, theta*xi_sys_minus[mask], label=r\"Best fit $\\xi^{\\rm sys}_{-}(\\theta)$\", c='r')\n", - " \n", + " ax.plot(\n", + " theta,\n", + " theta * xi_plus[mask],\n", + " label=r\"Best fit $\\xi_+(\\theta)$\",\n", + " **line_args[idx],\n", + " lw=2.5,\n", + " )\n", + " ax.plot(\n", + " theta,\n", + " theta * xi_sys_plus[mask],\n", + " label=r\"Best fit $\\xi^{\\rm sys}_{+}(\\theta)$\",\n", + " c=\"r\",\n", + " )\n", + " ax2.plot(\n", + " theta,\n", + " theta * xi_minus[mask],\n", + " label=r\"Best fit $\\xi_-(\\theta)$\",\n", + " **line_args[idx],\n", + " lw=2.5,\n", + " )\n", + " ax2.plot(\n", + " theta,\n", + " theta * xi_sys_minus[mask],\n", + " label=r\"Best fit $\\xi^{\\rm sys}_{-}(\\theta)$\",\n", + " c=\"r\",\n", + " )\n", + "\n", " else:\n", " mask = (theta > theta_min) & (theta < theta_max)\n", " theta = theta[mask]\n", - " ax.plot(theta, theta*xi_plus[mask], label=label, **line_args[idx], lw=2.5)\n", - " ax2.plot(theta, theta*xi_minus[mask], label=label, **line_args[idx], lw=2.5)\n", + " ax.plot(theta, theta * xi_plus[mask], label=label, **line_args[idx], lw=2.5)\n", + " ax2.plot(theta, theta * xi_minus[mask], label=label, **line_args[idx], lw=2.5)\n", "# XI PLUS PLOT SETTINGS\n", "\n", "# Plot the scale cuts for different k_max\n", - "ax.axvline(x=5, color='gray', linestyle='--', alpha=0.7)\n", - "ax.axhline(y=0, color='black', linestyle='--', alpha=0.7)\n", + "ax.axvline(x=5, color=\"gray\", linestyle=\"--\", alpha=0.7)\n", + "ax.axhline(y=0, color=\"black\", linestyle=\"--\", alpha=0.7)\n", "\n", "ymin = ax.get_ylim()[0]\n", "ymax = ax.get_ylim()[1]\n", "# Shadowing cut scaled\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2)\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color=\"gray\", alpha=0.2)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color=\"gray\", alpha=0.2)\n", "\n", "ax.set_ylim(ymin, ymax)\n", "\n", "# Add labels directly under the tick\n", - "ax.text(4.5, 0.47e-4,\n", - " r\"$k_\\mathrm{max} = 1 h$ Mpc$^{-1}$\",\n", - " ha='center', va='top', fontsize=20, rotation=90)\n", - "\n", - "ax.set_ylabel(r'$\\theta \\xi_\\pm$', fontsize=26)\n", - "ax.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax.set_xlim([theta.min()-0.1, theta.max()+20])\n", - "ax.set_title(r'$\\xi_+(\\theta)$', fontsize=26)\n", - "ax.set_xscale('log')\n", + "ax.text(\n", + " 4.5,\n", + " 0.47e-4,\n", + " r\"$k_\\mathrm{max} = 1 h$ Mpc$^{-1}$\",\n", + " ha=\"center\",\n", + " va=\"top\",\n", + " fontsize=20,\n", + " rotation=90,\n", + ")\n", + "\n", + "ax.set_ylabel(r\"$\\theta \\xi_\\pm$\", fontsize=26)\n", + "ax.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax.set_xlim([theta.min() - 0.1, theta.max() + 20])\n", + "ax.set_title(r\"$\\xi_+(\\theta)$\", fontsize=26)\n", + "ax.set_xscale(\"log\")\n", "ax.set_xticks(np.array([1, 10, 100]))\n", "ax.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax.tick_params(axis='both', which='major', labelsize=24)\n", - "ax.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax.yaxis.get_offset_text().set_fontsize(24)\n", - "ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "ax.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xip, fontsize=20)\n", "\n", "# XI_MINUS PLOT SETTINGS\n", "\n", "# Plot the scale cuts for different k_max\n", - "ax2.axvline(x=50, color='gray', linestyle='--', alpha=0.7)\n", - "ax2.axhline(y=0, color='black', linestyle='--', alpha=0.7)\n", + "ax2.axvline(x=50, color=\"gray\", linestyle=\"--\", alpha=0.7)\n", + "ax2.axhline(y=0, color=\"black\", linestyle=\"--\", alpha=0.7)\n", "\n", "ymin = ax2.get_ylim()[0]\n", "ymax = ax2.get_ylim()[1]\n", "# Shadowing cut scaled\n", - "ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2)\n", - "ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2)\n", + "ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color=\"gray\", alpha=0.2)\n", + "ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color=\"gray\", alpha=0.2)\n", "\n", "ax2.set_ylim(ymin, ymax)\n", "\n", "# Add labels directly under the tick\n", - "ax2.text(45, 1.15e-4,\n", - " r\"$k_\\mathrm{max} = 1 h$ Mpc$^{-1}$\",\n", - " ha='center', va='top', fontsize=20, rotation=90)\n", + "ax2.text(\n", + " 45,\n", + " 1.15e-4,\n", + " r\"$k_\\mathrm{max} = 1 h$ Mpc$^{-1}$\",\n", + " ha=\"center\",\n", + " va=\"top\",\n", + " fontsize=20,\n", + " rotation=90,\n", + ")\n", "\n", "# ax2.set_ylabel(r'$\\theta \\xi_-$', fontsize=16)\n", - "ax2.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax2.set_xlim([theta.min()-0.1, theta.max()+20])\n", - "ax2.set_xscale('log')\n", - "ax2.set_title(r'$\\xi_-(\\theta)$', fontsize=26)\n", + "ax2.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax2.set_xlim([theta.min() - 0.1, theta.max() + 20])\n", + "ax2.set_xscale(\"log\")\n", + "ax2.set_title(r\"$\\xi_-(\\theta)$\", fontsize=26)\n", "ax2.set_xticks(np.array([1, 10, 100]))\n", "ax2.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax2.tick_params(axis='both', which='major', labelsize=24)\n", - "ax2.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax2.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax2.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax2.yaxis.get_offset_text().set_fontsize(24)\n", - "ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax2.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "ax2.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xim, fontsize=20)\n", "\n", - "plt.savefig(\"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/best_fit_xipm_SP_v1.4.6.3_B.pdf\", bbox_inches='tight')\n", + "plt.savefig(\n", + " \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/best_fit_xipm_SP_v1.4.6.3_B.pdf\",\n", + " bbox_inches=\"tight\",\n", + ")\n", "\n", - "plt.show()\n", - "\n" + "plt.show()" ] }, { @@ -349,91 +426,147 @@ "metadata": {}, "outputs": [], "source": [ - "root_to_plot = [\n", - " fiducial_root_xi_chains\n", - "]\n", - "labels = [\n", - " r\"Best fit $\\tau_{0,2}(\\theta)$\"\n", - "]\n", + "root_to_plot = [fiducial_root_xi_chains]\n", + "labels = [r\"Best fit $\\tau_{0,2}(\\theta)$\"]\n", "\n", "bbox_to_anchor_xip = (0.285, 0.7)\n", "bbox_to_anchor_xim = (0.3, 0.65)\n", - "tau0_data = data['TAU_0_PLUS'].data\n", - "tau2_data = data['TAU_2_PLUS'].data\n", - "cov_mat = data['COVMAT'].data\n", + "tau0_data = data[\"TAU_0_PLUS\"].data\n", + "tau2_data = data[\"TAU_2_PLUS\"].data\n", + "cov_mat = data[\"COVMAT\"].data\n", "\n", "# Plot hyperparameter\n", "\n", - "fig, [ax,ax2] = plt.subplots(1, 2, figsize=(20, 8))\n", - "\n", - "theta, tau0, tau2 = tau0_data['ANG'], tau0_data['VALUE'], tau2_data['VALUE']\n", - "ax.errorbar(theta, theta*tau0, yerr=theta*np.sqrt(np.diag(cov_mat[2*len(theta):3*len(theta),2*len(theta):3*len(theta)])), fmt='o', label=r\"UNIONS $\\tau_{0,+}$\", color='black', capsize=2)\n", - "ax2.errorbar(theta, theta*tau2, yerr=theta*np.sqrt(np.diag(cov_mat[3*len(theta):4*len(theta),3*len(theta):4*len(theta)])), fmt='o', label=r\"UNIONS $\\tau_{2,+}$\", color='black', capsize=2)\n", + "fig, [ax, ax2] = plt.subplots(1, 2, figsize=(20, 8))\n", + "\n", + "theta, tau0, tau2 = tau0_data[\"ANG\"], tau0_data[\"VALUE\"], tau2_data[\"VALUE\"]\n", + "ax.errorbar(\n", + " theta,\n", + " theta * tau0,\n", + " yerr=theta\n", + " * np.sqrt(\n", + " np.diag(\n", + " cov_mat[2 * len(theta) : 3 * len(theta), 2 * len(theta) : 3 * len(theta)]\n", + " )\n", + " ),\n", + " fmt=\"o\",\n", + " label=r\"UNIONS $\\tau_{0,+}$\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", + "ax2.errorbar(\n", + " theta,\n", + " theta * tau2,\n", + " yerr=theta\n", + " * np.sqrt(\n", + " np.diag(\n", + " cov_mat[3 * len(theta) : 4 * len(theta), 3 * len(theta) : 4 * len(theta)]\n", + " )\n", + " ),\n", + " fmt=\"o\",\n", + " label=r\"UNIONS $\\tau_{2,+}$\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", "\n", "for idx, (label, root) in enumerate(zip(labels, root_to_plot)):\n", - " #Read the results\n", - " theta = (np.loadtxt(path_output_chains + '{}/best_fit/tau_0_plus/theta.txt'.format(root))) * 180/np.pi * 60\n", - " tau0_plus = np.loadtxt(path_output_chains + '{}/best_fit/tau_0_plus/bin_1_1.txt'.format(root))\n", - " tau2_plus = np.loadtxt(path_output_chains + '{}/best_fit/tau_2_plus/bin_1_1.txt'.format(root))\n", - " \n", + " # Read the results\n", + " theta = (\n", + " (\n", + " np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/tau_0_plus/theta.txt\".format(root)\n", + " )\n", + " )\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + " tau0_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/tau_0_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " tau2_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/tau_2_plus/bin_1_1.txt\".format(root)\n", + " )\n", + "\n", " mask = (theta > theta_min) & (theta < theta_max)\n", " theta = theta[mask]\n", - " ax.plot(theta, theta*tau0_plus[mask], label=r\"Best fit $\\tau_{0,+}(\\theta)$\", c='orange', lw=2.5)\n", - " ax2.plot(theta, theta*tau2_plus[mask], label=r\"Best fit $\\tau_{2,+}(\\theta)$\", c='orange', lw=2.5)\n", + " ax.plot(\n", + " theta,\n", + " theta * tau0_plus[mask],\n", + " label=r\"Best fit $\\tau_{0,+}(\\theta)$\",\n", + " c=\"orange\",\n", + " lw=2.5,\n", + " )\n", + " ax2.plot(\n", + " theta,\n", + " theta * tau2_plus[mask],\n", + " label=r\"Best fit $\\tau_{2,+}(\\theta)$\",\n", + " c=\"orange\",\n", + " lw=2.5,\n", + " )\n", "\n", "# XI PLUS PLOT SETTINGS\n", "\n", "# Plot the scale cuts for different k_max\n", - "ax.axhline(y=0, color='black', linestyle='--', alpha=0.7)\n", + "ax.axhline(y=0, color=\"black\", linestyle=\"--\", alpha=0.7)\n", "\n", "ymin = ax.get_ylim()[0]\n", "ymax = ax.get_ylim()[1]\n", "\n", "ax.set_ylim(ymin, ymax)\n", "\n", - "ax.set_ylabel(r'$\\theta\\tau_{0,2}$', fontsize=26)\n", - "ax.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax.set_xlim([theta.min()-0.1, theta.max()+20])\n", - "ax.set_title(r'$\\tau_{0,+}(\\theta)$', fontsize=26)\n", - "ax.set_xscale('log')\n", + "ax.set_ylabel(r\"$\\theta\\tau_{0,2}$\", fontsize=26)\n", + "ax.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax.set_xlim([theta.min() - 0.1, theta.max() + 20])\n", + "ax.set_title(r\"$\\tau_{0,+}(\\theta)$\", fontsize=26)\n", + "ax.set_xscale(\"log\")\n", "ax.set_xticks(np.array([1, 10, 100]))\n", "ax.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax.tick_params(axis='both', which='major', labelsize=24)\n", - "ax.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax.yaxis.get_offset_text().set_fontsize(24)\n", - "ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "ax.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xip, fontsize=20)\n", "\n", "# XI_MINUS PLOT SETTINGS\n", "\n", "# Plot the scale cuts for different k_max\n", - "ax2.axhline(y=0, color='black', linestyle='--', alpha=0.7)\n", + "ax2.axhline(y=0, color=\"black\", linestyle=\"--\", alpha=0.7)\n", "\n", "ymin = ax2.get_ylim()[0]\n", "ymax = ax2.get_ylim()[1]\n", "# Shadowing cut scaled\n", - "ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut')\n", - "ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2)\n", + "ax2.fill_betweenx(\n", + " y=[ymin, ymax],\n", + " x1=0,\n", + " x2=12,\n", + " color=\"gray\",\n", + " alpha=0.2,\n", + " label=r\"$B$-mode informed scale cut\",\n", + ")\n", + "ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color=\"gray\", alpha=0.2)\n", "\n", "ax2.set_ylim(ymin, ymax)\n", "\n", "# ax2.set_ylabel(r'$\\theta \\xi_-$', fontsize=16)\n", - "ax2.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax2.set_xlim([theta.min()-0.1, theta.max()+20])\n", - "ax2.set_xscale('log')\n", - "ax2.set_title(r'$\\tau_{2,+}(\\theta)$', fontsize=26)\n", + "ax2.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax2.set_xlim([theta.min() - 0.1, theta.max() + 20])\n", + "ax2.set_xscale(\"log\")\n", + "ax2.set_title(r\"$\\tau_{2,+}(\\theta)$\", fontsize=26)\n", "ax2.set_xticks(np.array([1, 10, 100]))\n", "ax2.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax2.tick_params(axis='both', which='major', labelsize=24)\n", - "ax2.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax2.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax2.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax2.yaxis.get_offset_text().set_fontsize(24)\n", - "ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax2.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "ax2.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xim, fontsize=20)\n", "\n", - "plt.savefig(\"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/best_fit_tau_02_SP_v1.4.6.3_B.pdf\", bbox_inches='tight')\n", + "plt.savefig(\n", + " \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/best_fit_tau_02_SP_v1.4.6.3_B.pdf\",\n", + " bbox_inches=\"tight\",\n", + ")\n", "\n", - "plt.show()\n", - " \n" + "plt.show()" ] }, { diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/contours.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/contours.ipynb index 10371a67..e95be1b5 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/contours.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/contours.ipynb @@ -17,86 +17,88 @@ "metadata": {}, "outputs": [], "source": [ - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", "import os.path\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", "from astropy.io import fits\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "plt.rcParams[\"text.usetex\"] = True\n", "\n", "sns.set_palette(\"husl\")\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=70\n", - "g.settings.axes_labelsize=80\n", + "g.settings.axes_fontsize = 70\n", + "g.settings.axes_labelsize = 80\n", "g.settings.alpha_filled_add = 0.7\n", - "g.settings.legend_fontsize =70\n", + "g.settings.legend_fontsize = 70\n", "\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "\n", - "root_dir = '/n09data/guerrini/output_chains/'\n", - "path_datavectors = '/home/guerrini/sp_validation/cosmo_inference/data/'\n", + "root_dir = \"/n09data/guerrini/output_chains/\"\n", + "path_datavectors = \"/home/guerrini/sp_validation/cosmo_inference/data/\"\n", "path_output_chains = \"/n09data/guerrini/output_chains/\"\n", "\n", - "data = fits.open(os.path.join(path_datavectors,f\"SP_v1.4.6.3_config/SP_v1.4.6.3_B/cosmosis_SP_v1.4.6.3_leak_corr_B_masked.fits\"))\n", + "data = fits.open(\n", + " os.path.join(\n", + " path_datavectors,\n", + " \"SP_v1.4.6.3_config/SP_v1.4.6.3_B/cosmosis_SP_v1.4.6.3_leak_corr_B_masked.fits\",\n", + " )\n", + ")\n", "\n", "roots_fid = {\n", - "'SP_v1.4.6.3_leak_corr_B': r\"UNIONS-3500 $C_\\ell$\",\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"UNIONS-3500 $\\xi_\\pm$ (This work) \",\n", - "\n", - "'KiDS-Legacy_xipm': r\"KiDS-Legacy $\\xi_\\pm$\",\n", - "'HSC_Y3': r\"HSC-Y3 $\\xi_\\pm$\",\n", - "'Planck18': r\"$\\textit{Planck}$ 2018\",\n", + " \"SP_v1.4.6.3_leak_corr_B\": r\"UNIONS-3500 $C_\\ell$\",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"UNIONS-3500 $\\xi_\\pm$ (This work) \",\n", + " \"KiDS-Legacy_xipm\": r\"KiDS-Legacy $\\xi_\\pm$\",\n", + " \"HSC_Y3\": r\"HSC-Y3 $\\xi_\\pm$\",\n", + " \"Planck18\": r\"$\\textit{Planck}$ 2018\",\n", "}\n", "\n", "roots_full = {\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"UNIONS-3500 $\\xi_\\pm$ (This work) \",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"UNIONS-3500 $\\xi_\\pm$ (This work) \",\n", "}\n", "\n", "roots_ia = {\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"Gaussian $A_{\\rm{IA}}$ prior\",\n", - "'SP_v1.4.6.3_B_flat_ia_config': r\"Flat $A_{\\rm{IA}}$ prior\",\n", - "'SP_v1.4.6.3_B_no_ia_config': r\"No IA\",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"Gaussian $A_{\\rm{IA}}$ prior\",\n", + " \"SP_v1.4.6.3_B_flat_ia_config\": r\"Flat $A_{\\rm{IA}}$ prior\",\n", + " \"SP_v1.4.6.3_B_no_ia_config\": r\"No IA\",\n", "}\n", "\n", "roots_ext = {\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"UNIONS-3500 $\\xi_\\pm$\",\n", - "'SP_v1.4.6.3_B_planck_config': r\"UNIONS-3500 $\\xi_\\pm$ + CMB\",\n", - "'SP_v1.4.6.3_B_planck_desi_config': r\"UNIONS-3500 $\\xi_\\pm$ + CMB + BAO\",\n", - "'Planck18': r\"$\\textit{Planck}$ 2018\",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"UNIONS-3500 $\\xi_\\pm$\",\n", + " \"SP_v1.4.6.3_B_planck_config\": r\"UNIONS-3500 $\\xi_\\pm$ + CMB\",\n", + " \"SP_v1.4.6.3_B_planck_desi_config\": r\"UNIONS-3500 $\\xi_\\pm$ + CMB + BAO\",\n", + " \"Planck18\": r\"$\\textit{Planck}$ 2018\",\n", "}\n", "\n", "roots_dz = {\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"Gaussian $\\Delta z$ prior\",\n", - "'SP_v1.4.6.3_B_flat_delta_z_config': r\"Flat $\\Delta z$ prior\",\n", - "'SP_v1.4.6.3_B_no_delta_z_config': r\"No $\\Delta z$ modelling\",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"Gaussian $\\Delta z$ prior\",\n", + " \"SP_v1.4.6.3_B_flat_delta_z_config\": r\"Flat $\\Delta z$ prior\",\n", + " \"SP_v1.4.6.3_B_no_delta_z_config\": r\"No $\\Delta z$ modelling\",\n", "}\n", "\n", "roots_psf = {\n", - "'SP_v1.4.6.3_B_flat_alpha_beta_config': r\"Flat $\\alpha$ and $\\beta$ priors\",\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"Gaussian $\\alpha$ and $\\beta$ priors\",\n", - "'SP_v1.4.6.3_B_no_xi_sys_config': r\"No $\\xi^{\\rm sys}$ included\",\n", - "'SP_v1.4.6.3_B_no_leak_corr_config': r\"No object-wise leakage correction\",\n", + " \"SP_v1.4.6.3_B_flat_alpha_beta_config\": r\"Flat $\\alpha$ and $\\beta$ priors\",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"Gaussian $\\alpha$ and $\\beta$ priors\",\n", + " \"SP_v1.4.6.3_B_no_xi_sys_config\": r\"No $\\xi^{\\rm sys}$ included\",\n", + " \"SP_v1.4.6.3_B_no_leak_corr_config\": r\"No object-wise leakage correction\",\n", "}\n", "\n", "roots_scale = {\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"$\\xi_+$: $\\theta=[12,83]$\",\n", - "'SP_v1.4.6.3_B_small_scales_config': r\"$\\xi_+$: $\\theta=[5,83]$\"\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"$\\xi_+$: $\\theta=[12,83]$\",\n", + " \"SP_v1.4.6.3_B_small_scales_config\": r\"$\\xi_+$: $\\theta=[5,83]$\",\n", "}\n", "\n", "roots_nonlin = {\n", - "'SP_v1.4.6.3_B_fiducial_config': r\"Fiducial (\\texttt{HMCode2020}, $\\log(T_{\\rm AGN})$)\",\n", - "'SP_v1.4.6.3_B_no_baryons_config': r\"\\texttt{HMCode2020} no baryons\",\n", - "'SP_v1.4.6.3_B_halofit_config': r\"\\texttt{Halofit}\",\n", + " \"SP_v1.4.6.3_B_fiducial_config\": r\"Fiducial (\\texttt{HMCode2020}, $\\log(T_{\\rm AGN})$)\",\n", + " \"SP_v1.4.6.3_B_no_baryons_config\": r\"\\texttt{HMCode2020} no baryons\",\n", + " \"SP_v1.4.6.3_B_halofit_config\": r\"\\texttt{Halofit}\",\n", "}\n", - "roots = roots_ext\n", - " " + "roots = roots_ext" ] }, { @@ -114,82 +116,113 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for i, root in enumerate(list(roots.keys())):\n", - " \n", " burnin = 0\n", - " if 'SP' not in root:\n", - " chain = g.samples_for_root(root_dir + 'ext_data/{}/getdist_{}'.format(root, root),\n", - " cache=False,\n", - " settings={'ignore_rows': burnin,\n", - " # 'smooth_scale_2D':0.2,\n", - " # 'smooth_scale_1D':0.2\n", - " })\n", + " if \"SP\" not in root:\n", + " chain = g.samples_for_root(\n", + " root_dir + \"ext_data/{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\n", + " \"ignore_rows\": burnin,\n", + " # 'smooth_scale_2D':0.2,\n", + " # 'smooth_scale_1D':0.2\n", + " },\n", + " )\n", " p = chain.getParams()\n", - " if hasattr(p, 'S_8')==False:\n", + " if hasattr(p, \"S_8\") == False:\n", " omega_m = chain.getParams().OMEGA_M\n", " sigma_8 = chain.getParams().SIGMA_8\n", "\n", " s_8 = sigma_8 * (omega_m / 0.3) ** 0.5\n", "\n", - " chain.addDerived(s_8, name='S_8', label=r'S_8')\n", + " chain.addDerived(s_8, name=\"S_8\", label=r\"S_8\")\n", + "\n", + " p = chain.paramNames.parWithName(\"S_8\")\n", "\n", - " p = chain.paramNames.parWithName('S_8')\n", - " \n", - " elif 'config' in root:\n", - " if os.path.isfile(root_dir + '{}/getdist_{}.txt'.format(root, root))==False:\n", - " \n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root, root))\n", - " \n", - " if 'nautilus' in root:\n", + " elif \"config\" in root:\n", + " if os.path.isfile(root_dir + \"{}/getdist_{}.txt\".format(root, root)) == False:\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", + "\n", + " if \"nautilus\" in root:\n", " weights = np.exp(samples[:, -3])\n", " neglogL = samples[:, -2] - samples[:, -1]\n", "\n", " samples = np.column_stack((weights, neglogL, samples[:, 0:-3]))\n", - " elif 'mh' in root:\n", - " samples = np.column_stack((np.ones_like(samples[:,-1]),np.log(samples[:,-1])-np.log(samples[:,-2]), samples[:,0:-2]))\n", - " burnin=0.3\n", + " elif \"mh\" in root:\n", + " samples = np.column_stack(\n", + " (\n", + " np.ones_like(samples[:, -1]),\n", + " np.log(samples[:, -1]) - np.log(samples[:, -2]),\n", + " samples[:, 0:-2],\n", + " )\n", + " )\n", + " burnin = 0.3\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " \n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root, root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root, root),\n", - " cache=False,\n", - " settings={'ignore_rows': burnin,\n", - " # 'smooth_scale_2D':0.2,\n", - " # 'smooth_scale_1D':0.2\n", - " })\n", + " samples = np.column_stack(\n", + " (samples[:, -1], samples[:, -3], samples[:, 0:-4])\n", + " )\n", + "\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\n", + " \"ignore_rows\": burnin,\n", + " # 'smooth_scale_2D':0.2,\n", + " # 'smooth_scale_1D':0.2\n", + " },\n", + " )\n", " else:\n", - " if os.path.isfile(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root, root, root))==False:\n", - " \n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root, root, root))\n", - " \n", - " if 'nautilus' in root:\n", + " if (\n", + " os.path.isfile(\n", + " root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root)\n", + " )\n", + " == False\n", + " ):\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", + "\n", + " if \"nautilus\" in root:\n", " weights = np.exp(samples[:, -3])\n", " neglogL = samples[:, -2] - samples[:, -1]\n", "\n", " samples = np.column_stack((weights, neglogL, samples[:, 0:-3]))\n", - " elif 'mh' in root:\n", - " samples = np.column_stack((np.ones_like(samples[:,-1]),np.log(samples[:,-1])-np.log(samples[:,-2]), samples[:,0:-2]))\n", - " burnin=0.3\n", + " elif \"mh\" in root:\n", + " samples = np.column_stack(\n", + " (\n", + " np.ones_like(samples[:, -1]),\n", + " np.log(samples[:, -1]) - np.log(samples[:, -2]),\n", + " samples[:, 0:-2],\n", + " )\n", + " )\n", + " burnin = 0.3\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " \n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root, root, root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root, root, root),\n", - " cache=False,\n", - " settings={'ignore_rows': burnin,\n", - " # 'smooth_scale_2D':0.2,\n", - " # 'smooth_scale_1D':0.2\n", - " })\n", + " samples = np.column_stack(\n", + " (samples[:, -1], samples[:, -3], samples[:, 0:-4])\n", + " )\n", + "\n", + " np.savetxt(\n", + " root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples\n", + " )\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\n", + " \"ignore_rows\": burnin,\n", + " # 'smooth_scale_2D':0.2,\n", + " # 'smooth_scale_1D':0.2\n", + " },\n", + " )\n", " p = chain.getParams()\n", "\n", - " chains.append(chain)\n" + " chains.append(chain)" ] }, { @@ -199,17 +232,45 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','S_8','logt_agn','a','m1','bias_1', 'alpha', 'beta', 'omch2']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b}', r'h', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z', r'\\alpha_{\\rm PSF}', r'\\beta_{\\rm PSF}', r'\\omega_{\\rm c}']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + " \"omch2\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b}\",\n", + " r\"h\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z\",\n", + " r\"\\alpha_{\\rm PSF}\",\n", + " r\"\\beta_{\\rm PSF}\",\n", + " r\"\\omega_{\\rm c}\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " p = chain.getParams()\n", " for name, label in zip(name_list, label_list):\n", - " if hasattr(p,name):\n", + " if hasattr(p, name):\n", " param_names.parWithName(name).label = label\n", - " \n", - "legend_labels = list(roots.values())\n" + "\n", + "legend_labels = list(roots.values())" ] }, { @@ -245,26 +306,22 @@ " \"indigo\",\n", "]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"solid\", \"solid\", \"solid\"]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", "\n", "# FIDUCIAL PLOT\n", - "g.triangle_plot(chains,\n", - " ['SIGMA_8','S_8','OMEGA_M'], #\n", - " legend_labels=legend_labels,\n", - " line_args=line_args,\n", - " contour_colors=colours,\n", - " label_order=[1,0,2,3,4],\n", - " filled=[True,True,False,False,True])\n", - "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot.pdf')" + "g.triangle_plot(\n", + " chains,\n", + " [\"SIGMA_8\", \"S_8\", \"OMEGA_M\"], #\n", + " legend_labels=legend_labels,\n", + " line_args=line_args,\n", + " contour_colors=colours,\n", + " label_order=[1, 0, 2, 3, 4],\n", + " filled=[True, True, False, False, True],\n", + ")\n", + "\n", + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot.pdf\")" ] }, { @@ -284,28 +341,41 @@ "source": [ "%matplotlib inline\n", "\n", - "g.settings.axes_fontsize=40\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 40\n", + "g.settings.axes_labelsize = 50\n", "\n", "colours = [\n", " \"orange\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", "\n", "# FIDUCIAL PLOT\n", - "g.triangle_plot(chains,\n", - " ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','S_8','logt_agn','a','m1','bias_1'],\n", - " legend_labels=legend_labels,\n", - " line_args=line_args,\n", - " contour_colors=colours,\n", - " filled=True)\n", - "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_full.pdf')" + "g.triangle_plot(\n", + " chains,\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", + " legend_labels=legend_labels,\n", + " line_args=line_args,\n", + " contour_colors=colours,\n", + " filled=True,\n", + ")\n", + "\n", + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_full.pdf\")" ] }, { @@ -324,28 +394,30 @@ "outputs": [], "source": [ "colours = [\n", - " \"orange\", \n", + " \"orange\",\n", " \"royalblue\",\n", " \"forestgreen\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", "\n", - "g.triangle_plot(chains,\n", - " ['S_8','OMEGA_M','a'], #\n", - " legend_labels=legend_labels,\n", - " line_args=line_args,\n", - " contour_args={'alpha':0.6},\n", - " contour_colors=colours,\n", - " filled=[True, False, True])\n", + "g.triangle_plot(\n", + " chains,\n", + " [\"S_8\", \"OMEGA_M\", \"a\"], #\n", + " legend_labels=legend_labels,\n", + " line_args=line_args,\n", + " contour_args={\"alpha\": 0.6},\n", + " contour_colors=colours,\n", + " filled=[True, False, True],\n", + ")\n", "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_ia.pdf')" + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_ia.pdf\")" ] }, { @@ -365,34 +437,40 @@ "source": [ "colours = [\n", " \"royalblue\",\n", - " \"orange\", \n", + " \"orange\",\n", " \"hotpink\",\n", " \"slategray\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", "\n", - "g.triangle_plot(chains,\n", - " ['S_8','OMEGA_M', 'alpha','beta'], #\n", - " legend_labels=legend_labels,\n", - " line_args=line_args,\n", - " contour_args=[{'alpha':1},{'alpha':0.6},{'alpha':0.8},{'alpha':0.8}],\n", - " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " label_order=[1,0,2,3],\n", - " filled=[False, True, True, True])\n", + "g.triangle_plot(\n", + " chains,\n", + " [\"S_8\", \"OMEGA_M\", \"alpha\", \"beta\"], #\n", + " legend_labels=legend_labels,\n", + " line_args=line_args,\n", + " contour_args=[{\"alpha\": 1}, {\"alpha\": 0.6}, {\"alpha\": 0.8}, {\"alpha\": 0.8}],\n", + " contour_colors=colours,\n", + " legend_loc=\"upper right\",\n", + " label_order=[1, 0, 2, 3],\n", + " filled=[False, True, True, True],\n", + ")\n", "\n", - "g.subplots[3,2].scatter(0.005,0.81, color='k', marker='X', s=400, label='Fiducial config best-fit')\n", - "g.subplots[3,2].scatter(0.022,0.798, color='k', marker='P', s=400, label='Fiducial config best-fit')\n", + "g.subplots[3, 2].scatter(\n", + " 0.005, 0.81, color=\"k\", marker=\"X\", s=400, label=\"Fiducial config best-fit\"\n", + ")\n", + "g.subplots[3, 2].scatter(\n", + " 0.022, 0.798, color=\"k\", marker=\"P\", s=400, label=\"Fiducial config best-fit\"\n", + ")\n", "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_psf.pdf')" + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_psf.pdf\")" ] }, { @@ -411,27 +489,29 @@ "outputs": [], "source": [ "colours = [\n", - " \"orange\", \n", + " \"orange\",\n", " \"royalblue\",\n", " \"indigo\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", - "g.triangle_plot(chains,\n", - " ['S_8','OMEGA_M', 'bias_1'], #\n", - " legend_labels=legend_labels,\n", - " line_args=line_args,\n", - " contour_args=[{'alpha':1.0},{'alpha':0.9},{'alpha':0.5}],\n", - " contour_colors=colours,\n", - " filled=[True, False, True])\n", - "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_dz.pdf')" + "g.triangle_plot(\n", + " chains,\n", + " [\"S_8\", \"OMEGA_M\", \"bias_1\"], #\n", + " legend_labels=legend_labels,\n", + " line_args=line_args,\n", + " contour_args=[{\"alpha\": 1.0}, {\"alpha\": 0.9}, {\"alpha\": 0.5}],\n", + " contour_colors=colours,\n", + " filled=[True, False, True],\n", + ")\n", + "\n", + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_dz.pdf\")" ] }, { @@ -450,39 +530,41 @@ "outputs": [], "source": [ "colours = [\n", - " \"orange\", \n", + " \"orange\",\n", " \"royalblue\",\n", " \"crimson\",\n", " \"forestgreen\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", "g = plots.get_subplot_plotter(width_inch=10)\n", - "g.settings.axes_fontsize=25\n", - "g.settings.axes_labelsize=25\n", + "g.settings.axes_fontsize = 25\n", + "g.settings.axes_labelsize = 25\n", "g.settings.legend_fontsize = 22\n", "\n", - "g.plot_2d(chains,\n", - " ['S_8','OMEGA_M', 'SIGMA_8'], #\n", - " line_args=line_args,\n", - " contour_colors=colours,\n", - " legend_labels=legend_labels,\n", - " alphas=[0.7, 1.0, 1.0, 1.0],\n", - " filled=[True, True, True, False])\n", + "g.plot_2d(\n", + " chains,\n", + " [\"S_8\", \"OMEGA_M\", \"SIGMA_8\"], #\n", + " line_args=line_args,\n", + " contour_colors=colours,\n", + " legend_labels=legend_labels,\n", + " alphas=[0.7, 1.0, 1.0, 1.0],\n", + " filled=[True, True, True, False],\n", + ")\n", "\n", - "g.add_y_bands(0.2975, 0.0086, alpha2=0, color='k', label=\"BAO\")\n", - "g.add_legend(legend_labels, legend_loc='upper right')\n", + "g.add_y_bands(0.2975, 0.0086, alpha2=0, color=\"k\", label=\"BAO\")\n", + "g.add_legend(legend_labels, legend_loc=\"upper right\")\n", "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_ext.pdf')" + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_ext.pdf\")" ] }, { @@ -503,32 +585,34 @@ "%matplotlib inline\n", "\n", "colours = [\n", - " \"orange\", \n", - " \"dodgerblue\", \n", + " \"orange\",\n", + " \"dodgerblue\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", "g = plots.get_subplot_plotter(width_inch=9)\n", - "g.settings.axes_fontsize=25\n", - "g.settings.axes_labelsize=25\n", + "g.settings.axes_fontsize = 25\n", + "g.settings.axes_labelsize = 25\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 30\n", "\n", - "g.plot_2d(chains,\n", - " ['S_8','OMEGA_M'], #\n", - " line_args=line_args,\n", - " contour_args=[{'alpha':0.7},{'alpha':1.0}],\n", - " contour_colors=colours,\n", - " filled=[True, True])\n", - "g.add_legend(legend_labels, legend_loc='upper right')\n", + "g.plot_2d(\n", + " chains,\n", + " [\"S_8\", \"OMEGA_M\"], #\n", + " line_args=line_args,\n", + " contour_args=[{\"alpha\": 0.7}, {\"alpha\": 1.0}],\n", + " contour_colors=colours,\n", + " filled=[True, True],\n", + ")\n", + "g.add_legend(legend_labels, legend_loc=\"upper right\")\n", "\n", - "g.export('../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_scales.pdf')" + "g.export(\"../Plots/SP_v1.4.6.3_B_fiducial_config_contour_plot_scales.pdf\")" ] }, { @@ -551,29 +635,35 @@ "from getdist.gaussian_mixtures import Gaussian1D\n", "\n", "colours = [\n", - " \"orange\", \n", + " \"orange\",\n", " \"royalblue\",\n", "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", "\n", "# BBN PRIOR\n", - "bbn_prior = Gaussian1D(mean=0.02218, sigma=0.00055, name='ombh2', labels=[r'\\omega_{\\rm b}'],label='BBN prior')\n", - "bbn_chain = bbn_prior.MCSamples(3000,label='BBN prior')\n", - "\n", - "g.triangle_plot(chains\n", - " +[bbn_chain]\n", - " ,\n", - " name_list,\n", - " legend_labels=legend_labels,\n", - " line_args=line_args,\n", - " contour_colors=colours,\n", - " filled=[True, False])\n" + "bbn_prior = Gaussian1D(\n", + " mean=0.02218,\n", + " sigma=0.00055,\n", + " name=\"ombh2\",\n", + " labels=[r\"\\omega_{\\rm b}\"],\n", + " label=\"BBN prior\",\n", + ")\n", + "bbn_chain = bbn_prior.MCSamples(3000, label=\"BBN prior\")\n", + "\n", + "g.triangle_plot(\n", + " chains + [bbn_chain],\n", + " name_list,\n", + " legend_labels=legend_labels,\n", + " line_args=line_args,\n", + " contour_colors=colours,\n", + " filled=[True, False],\n", + ")" ] }, { @@ -591,9 +681,9 @@ "metadata": {}, "outputs": [], "source": [ - "xi_p_data = data['XI_PLUS'].data\n", - "xi_m_data = data['XI_MINUS'].data\n", - "cov_mat = data['COVMAT'].data\n", + "xi_p_data = data[\"XI_PLUS\"].data\n", + "xi_m_data = data[\"XI_MINUS\"].data\n", + "cov_mat = data[\"COVMAT\"].data\n", "\n", "labels = roots_scale.values()\n", "\n", @@ -617,8 +707,8 @@ "]\n", "\n", "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", + " \"solid\",\n", + " \"solid\",\n", "]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", @@ -627,18 +717,47 @@ "\n", "fig, ax = plt.subplots(1, 1, figsize=(11, 7))\n", "\n", - "theta, xi_p, xi_m = xi_p_data['ANG'], xi_p_data['VALUE'], xi_m_data['VALUE']\n", - "ax.errorbar(theta, theta*xi_p, yerr=theta*np.sqrt(np.diag(cov_mat[:len(theta),:len(theta)])), fmt='o', color='black', capsize=2)\n", + "theta, xi_p, xi_m = xi_p_data[\"ANG\"], xi_p_data[\"VALUE\"], xi_m_data[\"VALUE\"]\n", + "ax.errorbar(\n", + " theta,\n", + " theta * xi_p,\n", + " yerr=theta * np.sqrt(np.diag(cov_mat[: len(theta), : len(theta)])),\n", + " fmt=\"o\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", "\n", "for idx, (label, root) in enumerate(zip(labels, roots_scale)):\n", - " #Read the results\n", - " theta = (np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_plus/theta.txt'.format(root))) * 180/np.pi * 60\n", - " xi_plus = np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " xi_minus = np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60\n", - " \n", + " # Read the results\n", + " theta = (\n", + " (\n", + " np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", + " )\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + " xi_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_minus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = (\n", + " np.loadtxt(path_output_chains + \"{}/best_fit/xi_sys/theta.txt\".format(root))\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + "\n", " xi_sys_plus = np.interp(theta, theta_xi_sys, xi_sys_plus)\n", " xi_sys_minus = np.interp(theta, theta_xi_sys, xi_sys_minus)\n", " xi_plus += xi_sys_plus\n", @@ -646,35 +765,34 @@ "\n", " mask = (theta > theta_min) & (theta < theta_max)\n", " theta = theta[mask]\n", - " ax.plot(theta, theta*xi_plus[mask], label=label, **line_args[idx])\n", + " ax.plot(theta, theta * xi_plus[mask], label=label, **line_args[idx])\n", "\n", "ymin = ax.get_ylim()[0]\n", "ymax = ax.get_ylim()[1]\n", "\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2)\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=5, color='gray', alpha=0.7)\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=300, color='gray', alpha=0.2)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color=\"gray\", alpha=0.2)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=5, color=\"gray\", alpha=0.7)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=300, color=\"gray\", alpha=0.2)\n", "\n", "ax.set_ylim(ymin, ymax)\n", "\n", - "ax.set_ylabel(r'$\\theta \\xi_\\pm$', fontsize=26)\n", - "ax.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax.set_xlim([theta.min()-0.1, theta.max()+20])\n", - "ax.set_title(r'$\\xi_+(\\theta)$', fontsize=26)\n", - "ax.set_xscale('log')\n", + "ax.set_ylabel(r\"$\\theta \\xi_\\pm$\", fontsize=26)\n", + "ax.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax.set_xlim([theta.min() - 0.1, theta.max() + 20])\n", + "ax.set_title(r\"$\\xi_+(\\theta)$\", fontsize=26)\n", + "ax.set_xscale(\"log\")\n", "ax.set_xticks(np.array([1, 10, 100]))\n", "ax.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax.tick_params(axis='both', which='major', labelsize=24)\n", - "ax.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax.yaxis.get_offset_text().set_fontsize(24)\n", - "ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "ax.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xip, fontsize=20)\n", "\n", "\n", - "plt.savefig(\"./../Plots/scale_cut_xipm_SP_v1.4.6.3_B.pdf\", bbox_inches='tight')\n", + "plt.savefig(\"./../Plots/scale_cut_xipm_SP_v1.4.6.3_B.pdf\", bbox_inches=\"tight\")\n", "\n", - "plt.show()\n", - "\n" + "plt.show()" ] }, { @@ -684,38 +802,68 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "labels = roots_nonlin.values()\n", "\n", - "colours = [\n", - " \"orange\",\n", - " \"hotpink\",\n", - " 'teal'\n", - "]\n", + "colours = [\"orange\", \"hotpink\", \"teal\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'dashed'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"dashed\"]\n", "\n", "line_args = [dict(color=col, ls=ls, lw=2) for col, ls in zip(colours, linestyle)]\n", "\n", - "fig, [ax,ax2] = plt.subplots(2, 1, figsize=(11, 14))\n", + "fig, [ax, ax2] = plt.subplots(2, 1, figsize=(11, 14))\n", "\n", - "theta, xi_p, xi_m = xi_p_data['ANG'], xi_p_data['VALUE'], xi_m_data['VALUE']\n", - "ax.errorbar(theta, theta*xi_p, yerr=theta*np.sqrt(np.diag(cov_mat[:len(theta),:len(theta)])), fmt='o', color='black', capsize=2)\n", - "ax2.errorbar(theta, theta*xi_m, yerr=theta*np.sqrt(np.diag(cov_mat[len(theta):2*len(theta),len(theta):2*len(theta)])), fmt='o', color='black', capsize=2)\n", + "theta, xi_p, xi_m = xi_p_data[\"ANG\"], xi_p_data[\"VALUE\"], xi_m_data[\"VALUE\"]\n", + "ax.errorbar(\n", + " theta,\n", + " theta * xi_p,\n", + " yerr=theta * np.sqrt(np.diag(cov_mat[: len(theta), : len(theta)])),\n", + " fmt=\"o\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", + "ax2.errorbar(\n", + " theta,\n", + " theta * xi_m,\n", + " yerr=theta\n", + " * np.sqrt(\n", + " np.diag(cov_mat[len(theta) : 2 * len(theta), len(theta) : 2 * len(theta)])\n", + " ),\n", + " fmt=\"o\",\n", + " color=\"black\",\n", + " capsize=2,\n", + ")\n", "\n", "for idx, (label, root) in enumerate(zip(labels, roots_nonlin)):\n", - " #Read the results\n", - " theta = (np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_plus/theta.txt'.format(root))) * 180/np.pi * 60\n", - " xi_plus = np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " xi_minus = np.loadtxt(path_output_chains + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " xi_sys_plus = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root))\n", - " theta_xi_sys = np.loadtxt(path_output_chains + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60\n", - " \n", + " # Read the results\n", + " theta = (\n", + " (\n", + " np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", + " )\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + " xi_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_minus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + " xi_sys_plus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " path_output_chains + \"{}/best_fit/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + " theta_xi_sys = (\n", + " np.loadtxt(path_output_chains + \"{}/best_fit/xi_sys/theta.txt\".format(root))\n", + " * 180\n", + " / np.pi\n", + " * 60\n", + " )\n", + "\n", " xi_sys_plus = np.interp(theta, theta_xi_sys, xi_sys_plus)\n", " xi_sys_minus = np.interp(theta, theta_xi_sys, xi_sys_minus)\n", " xi_plus += xi_sys_plus\n", @@ -723,51 +871,50 @@ "\n", " mask = (theta > theta_min) & (theta < theta_max)\n", " theta = theta[mask]\n", - " ax.plot(theta, theta*xi_plus[mask], label=label, **line_args[idx])\n", - " ax2.plot(theta, theta*xi_minus[mask], label=label, **line_args[idx])\n", + " ax.plot(theta, theta * xi_plus[mask], label=label, **line_args[idx])\n", + " ax2.plot(theta, theta * xi_minus[mask], label=label, **line_args[idx])\n", "\n", "ymin = ax.get_ylim()[0]\n", "ymax = ax.get_ylim()[1]\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2)\n", - "ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=300, color='gray', alpha=0.2)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color=\"gray\", alpha=0.2)\n", + "ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=300, color=\"gray\", alpha=0.2)\n", "\n", "ax.set_ylim(ymin, ymax)\n", "\n", - "ax.set_ylabel(r'$\\theta \\xi_\\pm$', fontsize=26)\n", - "ax.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax.set_xlim([theta.min()-0.1, theta.max()+20])\n", - "ax.set_title(r'$\\xi_+(\\theta)$', fontsize=26)\n", - "ax.set_xscale('log')\n", + "ax.set_ylabel(r\"$\\theta \\xi_\\pm$\", fontsize=26)\n", + "ax.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax.set_xlim([theta.min() - 0.1, theta.max() + 20])\n", + "ax.set_title(r\"$\\xi_+(\\theta)$\", fontsize=26)\n", + "ax.set_xscale(\"log\")\n", "ax.set_xticks(np.array([1, 10, 100]))\n", "ax.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax.tick_params(axis='both', which='major', labelsize=24)\n", - "ax.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax.yaxis.get_offset_text().set_fontsize(24)\n", - "ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "\n", "\n", "ymin = ax2.get_ylim()[0]\n", "ymax = ax2.get_ylim()[1]\n", - "ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2)\n", - "ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=3000, color='gray', alpha=0.2)\n", + "ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color=\"gray\", alpha=0.2)\n", + "ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=3000, color=\"gray\", alpha=0.2)\n", "\n", "ax2.set_ylim(ymin, ymax)\n", - "ax2.set_xlabel(r'$\\theta$ (arcmin)', fontsize=26)\n", - "ax2.set_xlim([theta.min()-0.1, theta.max()])\n", - "ax2.set_xscale('log')\n", - "ax2.set_title(r'$\\xi_-(\\vartheta)$', fontsize=26)\n", + "ax2.set_xlabel(r\"$\\theta$ (arcmin)\", fontsize=26)\n", + "ax2.set_xlim([theta.min() - 0.1, theta.max()])\n", + "ax2.set_xscale(\"log\")\n", + "ax2.set_title(r\"$\\xi_-(\\vartheta)$\", fontsize=26)\n", "ax2.set_xticks(np.array([1, 10, 100]))\n", "ax2.tick_params(axis=\"x\", which=\"minor\", length=2, width=0.8)\n", - "ax2.tick_params(axis='both', which='major', labelsize=24)\n", - "ax2.tick_params(axis='both', which='minor', labelsize=20)\n", + "ax2.tick_params(axis=\"both\", which=\"major\", labelsize=24)\n", + "ax2.tick_params(axis=\"both\", which=\"minor\", labelsize=20)\n", "ax2.yaxis.get_offset_text().set_fontsize(24)\n", - "ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0))\n", + "ax2.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", "ax2.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xim, fontsize=20)\n", "\n", - "plt.savefig(\"./../Plots/nonlin_xipm_SP_v1.4.6.3_B.pdf\", bbox_inches='tight')\n", + "plt.savefig(\"./../Plots/nonlin_xipm_SP_v1.4.6.3_B.pdf\", bbox_inches=\"tight\")\n", "\n", - "plt.show()\n", - "\n" + "plt.show()" ] }, { diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2.ipynb index 8deb5514..f124e4cd 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2.ipynb @@ -6,122 +6,131 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import configparser\n", - "import subprocess\n", + "import os\n", "import re\n", - "from getdist import plots, loadMCSamples\n", - "from astropy.io import fits\n", - "import numpy as np\n", + "import subprocess\n", + "import sys\n", + "\n", "import matplotlib.pyplot as plt\n", - "from scipy.interpolate import interp1d\n", + "import numpy as np\n", "import scipy.stats as stats\n", + "from astropy.io import fits\n", + "from getdist import plots\n", "from IPython.display import Markdown, display\n", - "import healpy as hp\n", + "from scipy.interpolate import interp1d\n", "\n", - "import sys\n", "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts\")\n", "\n", "import chain_postprocessing\n", "\n", "%matplotlib inline\n", "\n", - "plt.rc('mathtext', fontset='stix')\n", - "plt.rc('font', family='sans-serif')\n", + "plt.rc(\"mathtext\", fontset=\"stix\")\n", + "plt.rc(\"font\", family=\"sans-serif\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "# #SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", - "root_dir = '/n09data/guerrini/output_chains/'\n", - "blind = 'B'\n", + "root_dir = \"/n09data/guerrini/output_chains/\"\n", + "blind = \"B\"\n", "\n", "roots = [\n", - "f'SP_v1.4.6.3_{blind}_fiducial_config',\n", - "f'SP_v1.4.6.3_{blind}_small_scales_config',\n", - "f'SP_v1.4.6.3_{blind}_flat_alpha_beta_config',\n", - "f'SP_v1.4.6.3_{blind}_no_xi_sys_config',\n", - "f'SP_v1.4.6.3_{blind}_no_leak_corr_config',\n", - "f'SP_v1.4.6.3_{blind}_flat_delta_z_config',\n", - "f'SP_v1.4.6.3_{blind}_no_delta_z_config',\n", - "f'SP_v1.4.6.3_{blind}_flat_ia_config',\n", - "f'SP_v1.4.6.3_{blind}_no_ia_config',\n", - "f'SP_v1.4.6.3_{blind}_no_m_bias_config',\n", - "f'SP_v1.4.6.3_{blind}_unmasked_covmat_config',\n", - "f'SP_v1.4.6.3_{blind}_halofit_config',\n", - "f'SP_v1.4.6.3_{blind}_no_baryons_config',\n", - "f'SP_v1.4.6.3_{blind}_nautilus_config',\n", - "f'SP_v1.4.6.3_{blind}_planck_config',\n", - "f'SP_v1.4.6.3_{blind}_planck_desi_config',\n", + " f\"SP_v1.4.6.3_{blind}_fiducial_config\",\n", + " f\"SP_v1.4.6.3_{blind}_small_scales_config\",\n", + " f\"SP_v1.4.6.3_{blind}_flat_alpha_beta_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_xi_sys_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_leak_corr_config\",\n", + " f\"SP_v1.4.6.3_{blind}_flat_delta_z_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_delta_z_config\",\n", + " f\"SP_v1.4.6.3_{blind}_flat_ia_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_ia_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_m_bias_config\",\n", + " f\"SP_v1.4.6.3_{blind}_unmasked_covmat_config\",\n", + " f\"SP_v1.4.6.3_{blind}_halofit_config\",\n", + " f\"SP_v1.4.6.3_{blind}_no_baryons_config\",\n", + " f\"SP_v1.4.6.3_{blind}_nautilus_config\",\n", + " f\"SP_v1.4.6.3_{blind}_planck_config\",\n", + " f\"SP_v1.4.6.3_{blind}_planck_desi_config\",\n", "]\n", "\n", "catalog_versions = [\n", - " f'SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}',\n", + " f\"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}\",\n", "]\n", "\n", "catalog_sub_versions = [\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", - " f'SP_v1.4.6.3_leak_corr_{blind}_masked',\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", + " f\"SP_v1.4.6.3_leak_corr_{blind}_masked\",\n", "]\n", - "output_folder = '/n09data/guerrini/output_chains/'\n", - "\n", - "path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/'\n", - "\n", - "\n", - "ini_roots =[\n", - " f'blind_{blind}/fiducial',\n", - " f'blind_{blind}/small_scales',\n", - " f'blind_{blind}/flat_alpha_beta',\n", - " f'blind_{blind}/no_xi_sys',\n", - " f'blind_{blind}/no_leak_corr',\n", - " f'blind_{blind}/flat_delta_z',\n", - " f'blind_{blind}/no_delta_z',\n", - " f'blind_{blind}/flat_ia',\n", - " f'blind_{blind}/no_ia',\n", - " f'blind_{blind}/no_m_bias',\n", - " f'blind_{blind}/unmasked_covmat',\n", - " f'blind_{blind}/halofit',\n", - " f'blind_{blind}/no_baryons',\n", - " f'blind_{blind}/nautilus',\n", - " f'blind_{blind}/planck',\n", - " f'blind_{blind}/planck_desi',\n", + "output_folder = \"/n09data/guerrini/output_chains/\"\n", + "\n", + "path_ini_files = \"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/\"\n", + "\n", + "\n", + "ini_roots = [\n", + " f\"blind_{blind}/fiducial\",\n", + " f\"blind_{blind}/small_scales\",\n", + " f\"blind_{blind}/flat_alpha_beta\",\n", + " f\"blind_{blind}/no_xi_sys\",\n", + " f\"blind_{blind}/no_leak_corr\",\n", + " f\"blind_{blind}/flat_delta_z\",\n", + " f\"blind_{blind}/no_delta_z\",\n", + " f\"blind_{blind}/flat_ia\",\n", + " f\"blind_{blind}/no_ia\",\n", + " f\"blind_{blind}/no_m_bias\",\n", + " f\"blind_{blind}/unmasked_covmat\",\n", + " f\"blind_{blind}/halofit\",\n", + " f\"blind_{blind}/no_baryons\",\n", + " f\"blind_{blind}/nautilus\",\n", + " f\"blind_{blind}/planck\",\n", + " f\"blind_{blind}/planck_desi\",\n", "]\n", "\n", "properties = {}\n", "\n", - "for i,root in enumerate(roots):\n", + "for i, root in enumerate(roots):\n", " print(root)\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " config.read(path_ini_files+'config_space_v1.4.6.3_fiducial/pipeline/'+ini_roots[i]+'.ini') \n", + " config.read(\n", + " path_ini_files\n", + " + \"config_space_v1.4.6.3_fiducial/pipeline/\"\n", + " + ini_roots[i]\n", + " + \".ini\"\n", + " )\n", " add_xi_sys = config[\"2pt_like\"][\"add_xi_sys\"]\n", - " lower_bound_xi_plus, upper_bound_xi_plus = map(float, config[\"2pt_like\"][\"angle_range_XI_PLUS_1_1\"].split())\n", - " lower_bound_xi_minus, upper_bound_xi_minus = map(float, config[\"2pt_like\"][\"angle_range_XI_MINUS_1_1\"].split())\n", + " lower_bound_xi_plus, upper_bound_xi_plus = map(\n", + " float, config[\"2pt_like\"][\"angle_range_XI_PLUS_1_1\"].split()\n", + " )\n", + " lower_bound_xi_minus, upper_bound_xi_minus = map(\n", + " float, config[\"2pt_like\"][\"angle_range_XI_MINUS_1_1\"].split()\n", + " )\n", "\n", " properties[root] = {\n", - " 'add_xi_sys': add_xi_sys,\n", - " 'lower_bound_xi_plus': lower_bound_xi_plus,\n", - " 'upper_bound_xi_plus': upper_bound_xi_plus,\n", - " 'lower_bound_xi_minus': lower_bound_xi_minus,\n", - " 'upper_bound_xi_minus': upper_bound_xi_minus\n", - " }\n" + " \"add_xi_sys\": add_xi_sys,\n", + " \"lower_bound_xi_plus\": lower_bound_xi_plus,\n", + " \"upper_bound_xi_plus\": upper_bound_xi_plus,\n", + " \"lower_bound_xi_minus\": lower_bound_xi_minus,\n", + " \"upper_bound_xi_minus\": upper_bound_xi_minus,\n", + " }" ] }, { @@ -137,33 +146,49 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for i, root in enumerate(roots):\n", " burnin = 0\n", - " \n", - " if os.path.isfile(root_dir + '{}/getdist_{}.txt'.format(root, root))==False:\n", - " \n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root, root))\n", - " \n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", - " elif 'mh' in root:\n", - " samples = np.column_stack((np.ones_like(samples[:,-1]),np.log(samples[:,-1])-np.log(samples[:,-2]), samples[:,0:-2]))\n", - " burnin=0.3\n", + "\n", + " if os.path.isfile(root_dir + \"{}/getdist_{}.txt\".format(root, root)) == False:\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", + "\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (\n", + " np.exp(samples[:, -3]),\n", + " samples[:, -1] - samples[:, -2],\n", + " samples[:, 0:-3],\n", + " )\n", + " )\n", + " elif \"mh\" in root:\n", + " samples = np.column_stack(\n", + " (\n", + " np.ones_like(samples[:, -1]),\n", + " np.log(samples[:, -1]) - np.log(samples[:, -2]),\n", + " samples[:, 0:-2],\n", + " )\n", + " )\n", + " burnin = 0.3\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " \n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root, root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root, root),\n", - " cache=False,\n", - " settings={'ignore_rows': burnin,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5\n", - " })\n", + " samples = np.column_stack(\n", + " (samples[:, -1], samples[:, -3], samples[:, 0:-4])\n", + " )\n", + "\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\n", + " \"ignore_rows\": burnin,\n", + " \"smooth_scale_2D\": 0.5,\n", + " \"smooth_scale_1D\": 0.5,\n", + " },\n", + " )\n", " p = chain.getParams()\n", "\n", " chains.append(chain)" @@ -175,15 +200,46 @@ "metadata": {}, "outputs": [], "source": [ - "param_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1','alpha','beta', 'omch2', 'm', 'a_planck']\n", - "label_list = ['\\Omega_m', '\\omega_b', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1', '\\\\alpha_{PSF}', '\\\\beta_{PSF}', '\\omega_c', 'M', 'A_{\\rm Planck}']\n", + "param_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + " \"omch2\",\n", + " \"m\",\n", + " \"a_planck\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + " \"\\\\alpha_{PSF}\",\n", + " \"\\\\beta_{PSF}\",\n", + " r\"\\omega_c\",\n", + " \"M\",\n", + " \"A_{\\rm Planck}\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " for name, label in zip(param_list, label_list):\n", " if param_names.parWithName(name) is not None:\n", - " param_names.parWithName(name).label = label\n", - " " + " param_names.parWithName(name).label = label" ] }, { @@ -203,15 +259,18 @@ "\n", "for root, chain in zip(roots, chains):\n", " print(root)\n", - " p=chain.getParams()\n", - " \n", - " best_fit[root] = chain_postprocessing.extract_best_fit_params(chain, best_fit_method='2Dkde')\n", - " \n", + " p = chain.getParams()\n", + "\n", + " best_fit[root] = chain_postprocessing.extract_best_fit_params(\n", + " chain, best_fit_method=\"2Dkde\"\n", + " )\n", + "\n", " for param_name in best_fit[root].keys():\n", - " high_68, low_68, high_95, low_95 = chain_postprocessing.compute_limits(chain, param_name)\n", - " if param_name=='S_8':\n", - " print(f\"{best_fit[root][param_name]}\")\n", - " " + " high_68, low_68, high_95, low_95 = chain_postprocessing.compute_limits(\n", + " chain, param_name\n", + " )\n", + " if param_name == \"S_8\":\n", + " print(f\"{best_fit[root][param_name]}\")" ] }, { @@ -227,7 +286,7 @@ "metadata": {}, "outputs": [], "source": [ - "if not os.path.exists(path_ini_files+'/values_empty.ini'):\n", + "if not os.path.exists(path_ini_files + \"/values_empty.ini\"):\n", " content = \"\"\"[cosmological_parameters]\n", "\n", "tau = 0.0544\n", @@ -247,11 +306,11 @@ "[psf_leakage_parameters]\n", "\"\"\"\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as f:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as f:\n", " f.write(content)\n", " f.close()\n", "\n", - " print('File created successfully')" + " print(\"File created successfully\")" ] }, { @@ -261,23 +320,23 @@ "outputs": [], "source": [ "section_map = {\n", - " 'omch2': 'cosmological_parameters',\n", - " 'ombh2': 'cosmological_parameters',\n", - " 'h0': 'cosmological_parameters',\n", - " 'n_s': 'cosmological_parameters',\n", - " 'tau': 'cosmological_parameters',\n", - " 's_8_input': 'cosmological_parameters',\n", - " 'logt_agn': 'halo_model_parameters',\n", - " 'a': 'intrinsic_alignment_parameters',\n", - " 'm1': 'shear_calibration_parameters',\n", - " 'bias_1': 'nofz_shifts',\n", - " 'alpha': 'psf_leakage_parameters',\n", - " 'beta': 'psf_leakage_parameters',\n", - " 'm': 'supernova_params',\n", - " 'a_planck' : 'planck'\n", + " \"omch2\": \"cosmological_parameters\",\n", + " \"ombh2\": \"cosmological_parameters\",\n", + " \"h0\": \"cosmological_parameters\",\n", + " \"n_s\": \"cosmological_parameters\",\n", + " \"tau\": \"cosmological_parameters\",\n", + " \"s_8_input\": \"cosmological_parameters\",\n", + " \"logt_agn\": \"halo_model_parameters\",\n", + " \"a\": \"intrinsic_alignment_parameters\",\n", + " \"m1\": \"shear_calibration_parameters\",\n", + " \"bias_1\": \"nofz_shifts\",\n", + " \"alpha\": \"psf_leakage_parameters\",\n", + " \"beta\": \"psf_leakage_parameters\",\n", + " \"m\": \"supernova_params\",\n", + " \"a_planck\": \"planck\",\n", "}\n", "\n", - "best_fit['SP_v1.4.6.3_B_no_ia_config']['a'] = 0" + "best_fit[\"SP_v1.4.6.3_B_no_ia_config\"][\"a\"] = 0" ] }, { @@ -287,9 +346,12 @@ "outputs": [], "source": [ "env = os.environ.copy()\n", - "env[\"LD_LIBRARY_PATH\"] = \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\" + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + "env[\"LD_LIBRARY_PATH\"] = (\n", + " \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\"\n", + " + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + ")\n", "\n", - "for i,root in enumerate(roots):\n", + "for i, root in enumerate(roots):\n", " print(root)\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", @@ -298,49 +360,49 @@ " # Check if this parameter exists for the current root\n", " if param in best_fit[root]:\n", " value = best_fit[root][param]\n", - " \n", + "\n", " if section not in config:\n", " config.add_section(section)\n", "\n", " config[section][param] = str(value)\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as configfile:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Modify the ini file to run in test mode at the best fit\n", + " # Modify the ini file to run in test mode at the best fit\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " \n", - " ini_file = path_ini_files+'config_space_v1.4.6.3_fiducial/pipeline/{}.ini'.format(ini_roots[i])\n", + "\n", + " ini_file = path_ini_files + \"config_space_v1.4.6.3_fiducial/pipeline/{}.ini\".format(\n", + " ini_roots[i]\n", + " )\n", " config.read(ini_file)\n", "\n", - " sampler = config['runtime']['sampler']\n", - " config['runtime']['sampler'] = 'test'\n", - " values = config['pipeline']['values']\n", - " config['pipeline']['values'] = path_ini_files + '/values_empty.ini'\n", - " config['DEFAULT']['FITS_FILE'] = f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_versions[0]}/cosmosis_{catalog_sub_versions[i]}.fits'\n", - " config['test']['save_dir'] = root_dir + '{}/best_fit'.format(root)\n", - " \n", - " with open(ini_file, 'w') as configfile:\n", + " sampler = config[\"runtime\"][\"sampler\"]\n", + " config[\"runtime\"][\"sampler\"] = \"test\"\n", + " values = config[\"pipeline\"][\"values\"]\n", + " config[\"pipeline\"][\"values\"] = path_ini_files + \"/values_empty.ini\"\n", + " config[\"DEFAULT\"][\"FITS_FILE\"] = (\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_versions[0]}/cosmosis_{catalog_sub_versions[i]}.fits\"\n", + " )\n", + " config[\"test\"][\"save_dir\"] = root_dir + \"{}/best_fit\".format(root)\n", + "\n", + " with open(ini_file, \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Run cosmosis\n", + " # Run cosmosis\n", " result = subprocess.run(\n", - " ['cosmosis', ini_file],\n", - " env=env,\n", - " capture_output=True,\n", - " text=True\n", + " [\"cosmosis\", ini_file], env=env, capture_output=True, text=True\n", " )\n", " print(f\"STDOUT:\\n{result.stdout}\")\n", " print(f\"STDERR:\\n{result.stderr}\")\n", "\n", - " #Modify the ini file to the previous one\n", - " config['pipeline']['values'] = values\n", - " config['runtime']['sampler'] = sampler\n", + " # Modify the ini file to the previous one\n", + " config[\"pipeline\"][\"values\"] = values\n", + " config[\"runtime\"][\"sampler\"] = sampler\n", "\n", - " with open(ini_file, 'w') as configfile:\n", - " config.write(configfile)\n", - " " + " with open(ini_file, \"w\") as configfile:\n", + " config.write(configfile)" ] }, { @@ -356,55 +418,75 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "metrics = {}\n", "\n", - "for idx,root in enumerate(roots):\n", + "for idx, root in enumerate(roots):\n", " print(root)\n", - " match = re.search(r'corr_([A-Za-z])', root)\n", + " match = re.search(r\"corr_([A-Za-z])\", root)\n", " if match:\n", " blind = match.group(1)\n", "\n", - " add_xi_sys = properties[root]['add_xi_sys']\n", + " add_xi_sys = properties[root][\"add_xi_sys\"]\n", " print(f\"add_xi_sys: {add_xi_sys}\")\n", - " lower_bound_xi_plus = properties[root]['lower_bound_xi_plus']\n", - " upper_bound_xi_plus = properties[root]['upper_bound_xi_plus']\n", - " lower_bound_xi_minus = properties[root]['lower_bound_xi_minus']\n", - " upper_bound_xi_minus = properties[root]['upper_bound_xi_minus']\n", - "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder + '{}/best_fit/shear_xi_plus/theta.txt'.format(root))\n", + " lower_bound_xi_plus = properties[root][\"lower_bound_xi_plus\"]\n", + " upper_bound_xi_plus = properties[root][\"upper_bound_xi_plus\"]\n", + " lower_bound_xi_minus = properties[root][\"lower_bound_xi_minus\"]\n", + " upper_bound_xi_minus = properties[root][\"upper_bound_xi_minus\"]\n", + "\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder + \"{}/best_fit/shear_xi_plus/theta.txt\".format(root)\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root))\n", - " shear_xi_minus = np.loadtxt(output_folder + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root))\n", - " \n", - " if add_xi_sys == 'T':\n", - " xi_sys_plus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root))\n", - " xi_sys_minus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root))\n", - "\n", - " theta_tau = np.loadtxt(output_folder + '{}/best_fit/tau_0_plus/theta.txt'.format(root))\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder + \"{}/best_fit/shear_xi_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder + \"{}/best_fit/shear_xi_minus/bin_1_1.txt\".format(root)\n", + " )\n", + "\n", + " if add_xi_sys == \"T\":\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder + \"{}/best_fit/xi_sys/shear_xi_plus.txt\".format(root)\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder + \"{}/best_fit/xi_sys/shear_xi_minus.txt\".format(root)\n", + " )\n", + "\n", + " theta_tau = np.loadtxt(\n", + " output_folder + \"{}/best_fit/tau_0_plus/theta.txt\".format(root)\n", + " )\n", " theta_tau_arcmin = theta_tau * 180 * 60 / np.pi\n", - " tau_0_model = np.loadtxt(output_folder + '{}/best_fit/tau_0_plus/bin_1_1.txt'.format(root))\n", - " tau_2_model = np.loadtxt(output_folder + '{}/best_fit/tau_2_plus/bin_1_1.txt'.format(root))\n", + " tau_0_model = np.loadtxt(\n", + " output_folder + \"{}/best_fit/tau_0_plus/bin_1_1.txt\".format(root)\n", + " )\n", + " tau_2_model = np.loadtxt(\n", + " output_folder + \"{}/best_fit/tau_2_plus/bin_1_1.txt\".format(root)\n", + " )\n", "\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{catalog_versions[0]}/cosmosis_{catalog_sub_versions[idx]}.fits')\n", - " \n", - " tau_0_data = data['TAU_0_PLUS'].data['VALUE']\n", - " tau_2_data = data['TAU_2_PLUS'].data['VALUE']\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{catalog_versions[0]}/cosmosis_{catalog_sub_versions[idx]}.fits\"\n", + " )\n", "\n", - " theta_data = data['XI_PLUS'].data['ANG']\n", - " xi_plus_data = data['XI_PLUS'].data['VALUE']\n", - " xi_minus_data = data['XI_MINUS'].data['VALUE']\n", + " tau_0_data = data[\"TAU_0_PLUS\"].data[\"VALUE\"]\n", + " tau_2_data = data[\"TAU_2_PLUS\"].data[\"VALUE\"]\n", "\n", + " theta_data = data[\"XI_PLUS\"].data[\"ANG\"]\n", + " xi_plus_data = data[\"XI_PLUS\"].data[\"VALUE\"]\n", + " xi_minus_data = data[\"XI_MINUS\"].data[\"VALUE\"]\n", "\n", - " #Load the covariance\n", - " cov = data['COVMAT'].data\n", - " cov_xi = cov[0:2*len(xi_plus_data), 0:2*len(xi_plus_data)]\n", - " cov_tau = cov[2*len(xi_plus_data):, 2*len(xi_plus_data):]\n", + " # Load the covariance\n", + " cov = data[\"COVMAT\"].data\n", + " cov_xi = cov[0 : 2 * len(xi_plus_data), 0 : 2 * len(xi_plus_data)]\n", + " cov_tau = cov[2 * len(xi_plus_data) :, 2 * len(xi_plus_data) :]\n", "\n", - " #interpolate the model\n", - " interp_xi_plus = interp1d(theta_arcmin, shear_xi_plus, kind='cubic', fill_value='extrapolate')\n", - " interp_xi_minus = interp1d(theta_arcmin, shear_xi_minus, kind='cubic', fill_value='extrapolate')\n", + " # interpolate the model\n", + " interp_xi_plus = interp1d(\n", + " theta_arcmin, shear_xi_plus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", + " interp_xi_minus = interp1d(\n", + " theta_arcmin, shear_xi_minus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", "\n", " xi_plus_model = interp_xi_plus(theta_data)\n", " if add_xi_sys:\n", @@ -413,31 +495,54 @@ " if add_xi_sys:\n", " xi_minus_model += xi_sys_minus\n", "\n", - " #Concatenate the data vector\n", + " # Concatenate the data vector\n", " xi_data = np.concatenate((xi_plus_data, xi_minus_data))\n", " xi_model = np.concatenate((xi_plus_model, xi_minus_model))\n", "\n", " tau_data = np.concatenate((tau_0_data, tau_2_data))\n", " tau_model = np.concatenate((tau_0_model, tau_2_model))\n", "\n", - " #Apply scale cuts\n", - " mask_xi_plus = (theta_data > lower_bound_xi_plus) & (theta_data < upper_bound_xi_plus)\n", - " mask_xi_minus = (theta_data > lower_bound_xi_minus) & (theta_data < upper_bound_xi_minus)\n", + " # Apply scale cuts\n", + " mask_xi_plus = (theta_data > lower_bound_xi_plus) & (\n", + " theta_data < upper_bound_xi_plus\n", + " )\n", + " mask_xi_minus = (theta_data > lower_bound_xi_minus) & (\n", + " theta_data < upper_bound_xi_minus\n", + " )\n", " mask = np.concatenate((mask_xi_plus, mask_xi_minus))\n", "\n", " xi_data = xi_data[mask]\n", " xi_model = xi_model[mask]\n", " cov_xi = cov_xi[mask][:, mask]\n", - " \n", - " cov_xi_plus = cov[0:len(xi_plus_data), 0:len(xi_plus_data)]\n", + "\n", + " cov_xi_plus = cov[0 : len(xi_plus_data), 0 : len(xi_plus_data)]\n", " cov_xi_plus = cov_xi_plus[mask_xi_plus][:, mask_xi_plus]\n", - " cov_xi_minus = cov[len(xi_plus_data):2*len(xi_minus_data), len(xi_plus_data):2*len(xi_minus_data)]\n", + " cov_xi_minus = cov[\n", + " len(xi_plus_data) : 2 * len(xi_minus_data),\n", + " len(xi_plus_data) : 2 * len(xi_minus_data),\n", + " ]\n", " cov_xi_minus = cov_xi_minus[mask_xi_minus][:, mask_xi_minus]\n", "\n", - " xi_plus_chi2 = np.dot((xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus]), np.dot(np.linalg.inv(cov_xi_plus), (xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus])))\n", - " xi_minus_chi2 = np.dot((xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus]), np.dot(np.linalg.inv(cov_xi_minus), (xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus])))\n", - " xi_chi2 = np.dot((xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data)))\n", - " tau_chi2 = np.dot((tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data)))\n", + " xi_plus_chi2 = np.dot(\n", + " (xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus]),\n", + " np.dot(\n", + " np.linalg.inv(cov_xi_plus),\n", + " (xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus]),\n", + " ),\n", + " )\n", + " xi_minus_chi2 = np.dot(\n", + " (xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus]),\n", + " np.dot(\n", + " np.linalg.inv(cov_xi_minus),\n", + " (xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus]),\n", + " ),\n", + " )\n", + " xi_chi2 = np.dot(\n", + " (xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data))\n", + " )\n", + " tau_chi2 = np.dot(\n", + " (tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data))\n", + " )\n", " n_dof_xi_plus = np.sum(mask_xi_plus)\n", " n_dof_xi_minus = np.sum(mask_xi_minus)\n", " n_dof_tau = len(tau_0_data) + len(tau_2_data)\n", @@ -450,20 +555,20 @@ " p_value_tot = 1 - stats.chi2.cdf(chi2_tot, n_dof_tot)\n", "\n", " metrics[root] = {\n", - " 'chi2_xi_plus': xi_plus_chi2,\n", - " 'n_dof_xi_plus': n_dof_xi_plus,\n", - " 'p_value_xi_plus': p_value_xi_plus,\n", - " 'chi2_xi_minus': xi_minus_chi2,\n", - " 'n_dof_xi_minus': n_dof_xi_minus,\n", - " 'p_value_xi_minus': p_value_xi_minus,\n", - " 'chi2_xi': xi_chi2,\n", - " 'p_value_xi': p_value_xi,\n", - " 'chi2_tau': tau_chi2,\n", - " 'n_dof_tau': n_dof_tau,\n", - " 'p_value_tau': p_value_tau,\n", - " 'chi2_tot': chi2_tot,\n", - " 'n_dof_tot': n_dof_tot,\n", - " 'p_value_tot': p_value_tot\n", + " \"chi2_xi_plus\": xi_plus_chi2,\n", + " \"n_dof_xi_plus\": n_dof_xi_plus,\n", + " \"p_value_xi_plus\": p_value_xi_plus,\n", + " \"chi2_xi_minus\": xi_minus_chi2,\n", + " \"n_dof_xi_minus\": n_dof_xi_minus,\n", + " \"p_value_xi_minus\": p_value_xi_minus,\n", + " \"chi2_xi\": xi_chi2,\n", + " \"p_value_xi\": p_value_xi,\n", + " \"chi2_tau\": tau_chi2,\n", + " \"n_dof_tau\": n_dof_tau,\n", + " \"p_value_tau\": p_value_tau,\n", + " \"chi2_tot\": chi2_tot,\n", + " \"n_dof_tot\": n_dof_tot,\n", + " \"p_value_tot\": p_value_tot,\n", " }\n", " print(\"Done!\")" ] @@ -480,7 +585,7 @@ " r\"\\hline\",\n", " r\"Root & $\\chi^2_{\\xi^+}$/dof & $p_{\\xi^+}$ & $\\chi^2_{\\xi^-}$/dof & $p_{\\xi^+}$ & $\\chi^2_{\\xi}$/dof & $p_{\\xi}$ &\"\n", " r\"$\\chi^2_\\tau$/dof & $p_\\tau$ & $\\chi^2_{\\text{tot}}$/dof & $p_{\\text{tot}}$ \\\\\",\n", - " r\"\\hline\"\n", + " r\"\\hline\",\n", " ]\n", "\n", " for root, vals in metrics.items():\n", @@ -489,7 +594,7 @@ " f\"{escaped} & \"\n", " f\"{vals['chi2_xi_plus']:.2f}/{vals['n_dof_xi_plus']} & {vals['p_value_xi_plus']:.3g} & \"\n", " f\"{vals['chi2_xi_minus']:.2f}/{vals['n_dof_xi_minus']} & {vals['p_value_xi_minus']:.3g} & \"\n", - " f\"{vals['chi2_xi']:.2f}/{vals['n_dof_xi_plus']+ vals['n_dof_xi_minus']} & {vals['p_value_xi']:.3g} &\"\n", + " f\"{vals['chi2_xi']:.2f}/{vals['n_dof_xi_plus'] + vals['n_dof_xi_minus']} & {vals['p_value_xi']:.3g} &\"\n", " f\"{vals['chi2_tau']:.2f}/{vals['n_dof_tau']} & {vals['p_value_tau']:.3g} & \"\n", " f\"{vals['chi2_tot']:.2f}/{vals['n_dof_tot']} & {vals['p_value_tot']:.3g} \\\\\\\\\"\n", " )\n", @@ -520,7 +625,7 @@ "def display_markdown(metrics):\n", " # Build Markdown table\n", " header = (\n", - " \"| Root | $\\chi^2$ (ξ⁺) / dof | p-val (ξ⁺) |$\\chi^2$ (ξ-) / dof | p-val (ξ-) | $\\chi^2$ (ξ) / dof | p-val (ξ) | $\\chi^2$ (τ) / dof | p-val (τ) | $\\chi^2$ (tot) / dof | p-val (tot) |\\n\"\n", + " \"| Root | $\\\\chi^2$ (ξ⁺) / dof | p-val (ξ⁺) |$\\\\chi^2$ (ξ-) / dof | p-val (ξ-) | $\\\\chi^2$ (ξ) / dof | p-val (ξ) | $\\\\chi^2$ (τ) / dof | p-val (τ) | $\\\\chi^2$ (tot) / dof | p-val (tot) |\\n\"\n", " \"|------|----------------|------------|----------------|------------|------------|---------------|------------|------------|------------------|--------------|\\n\"\n", " )\n", "\n", @@ -531,7 +636,7 @@ " row += f\"| {vals['p_value_xi_plus']:.5f} \"\n", " row += f\"| {vals['chi2_xi_minus']:.2f} / {vals['n_dof_xi_minus']} \"\n", " row += f\"| {vals['p_value_xi_minus']:.5f} \"\n", - " row += f\"| {vals['chi2_xi']:.2f} / {vals['n_dof_xi_minus']+vals['n_dof_xi_plus']} \"\n", + " row += f\"| {vals['chi2_xi']:.2f} / {vals['n_dof_xi_minus'] + vals['n_dof_xi_plus']} \"\n", " row += f\"| {vals['p_value_xi']:.5f} \"\n", " row += f\"| {vals['chi2_tau']:.2f} / {vals['n_dof_tau']} \"\n", " row += f\"| {vals['p_value_tau']:.5f} \"\n", diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2_glass_mock.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2_glass_mock.ipynb index 2a25f9d1..ddd66cd0 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2_glass_mock.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_chi2_glass_mock.ipynb @@ -6,43 +6,38 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import configparser\n", + "import os\n", "import subprocess\n", - "import re\n", - "from getdist import plots, loadMCSamples\n", - "from astropy.io import fits\n", - "import numpy as np\n", + "import sys\n", + "\n", "import matplotlib.pyplot as plt\n", - "from scipy.interpolate import interp1d\n", - "import scipy.stats as stats\n", - "from IPython.display import Markdown, display\n", - "import healpy as hp\n", + "import numpy as np\n", + "\n", "# Make the plot\n", "import seaborn as sns\n", + "from astropy.io import fits\n", + "from getdist import plots\n", + "from scipy.interpolate import interp1d\n", "from scipy.stats import chi2\n", "\n", - "\n", - "import sys\n", "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts\")\n", "\n", "import chain_postprocessing\n", "\n", "%matplotlib inline\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", - "plt.rcParams['axes.labelsize'] = 18\n", - "plt.rcParams['xtick.labelsize'] = 18\n", - "plt.rcParams['ytick.labelsize'] = 18\n", + "plt.rcParams[\"axes.labelsize\"] = 18\n", + "plt.rcParams[\"xtick.labelsize\"] = 18\n", + "plt.rcParams[\"ytick.labelsize\"] = 18\n", "\n", - "plt.rcParams['text.usetex'] = True\n", + "plt.rcParams[\"text.usetex\"] = True\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", @@ -54,7 +49,9 @@ "chain_version = \"v6\"\n", "\n", "# Path to the glass mock data vectors\n", - "root_glass_dv = f\"/home/guerrini/sp_validation/cosmo_inference/data/glass_mocks/{chain_version}/\"\n", + "root_glass_dv = (\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/glass_mocks/{chain_version}/\"\n", + ")\n", "\n", "# Choose the best-fit method\n", "best_fit_method = \"2Dkde\"\n", @@ -63,17 +60,19 @@ "max_sim = 350\n", "failed_simulations = [82, 83, 281, 282, 283, 284, 285, 286, 287]\n", "roots = [f\"glass_mock_{chain_version}_{str(i).zfill(5)}\" for i in range(1, max_sim + 1)]\n", - "roots = [root for root in roots if int(root.split('_')[-1]) not in failed_simulations]\n", + "roots = [root for root in roots if int(root.split(\"_\")[-1]) not in failed_simulations]\n", "\n", "catalog_versions = [\n", - " f'SP_v1.4.6.3_config/SP_v1.4.6.3_A',\n", + " \"SP_v1.4.6.3_config/SP_v1.4.6.3_A\",\n", "]\n", "\n", - "output_folder_chains = '/n23data1/n06data/lgoh/scratch/temp/' \n", - "path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/'\n", - "output_fig_path = f\"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/\"\n", + "output_folder_chains = \"/n23data1/n06data/lgoh/scratch/temp/\"\n", + "path_ini_files = \"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/\"\n", + "output_fig_path = (\n", + " \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/\"\n", + ")\n", "\n", - "ini_root =f'blind_A/fiducial'\n", + "ini_root = \"blind_A/fiducial\"\n", "\n", "lower_bound_xi = 12\n", "upper_bound_xi = 83" @@ -92,25 +91,29 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "best_fit = {}\n", "\n", "for i, root in enumerate(roots):\n", " burnin = 0\n", - " \n", - " if os.path.isfile(f'{root_dir}/{root}/{root}/getdist_{root}.txt')==True:\n", - " \n", - " chain = g.samples_for_root(f'{root_dir}/{root}/{root}/getdist_{root}',\n", - " cache=False,\n", - " settings={'ignore_rows': burnin,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5\n", - " })\n", + "\n", + " if os.path.isfile(f\"{root_dir}/{root}/{root}/getdist_{root}.txt\") == True:\n", + " chain = g.samples_for_root(\n", + " f\"{root_dir}/{root}/{root}/getdist_{root}\",\n", + " cache=False,\n", + " settings={\n", + " \"ignore_rows\": burnin,\n", + " \"smooth_scale_2D\": 0.5,\n", + " \"smooth_scale_1D\": 0.5,\n", + " },\n", + " )\n", " p = chain.getParams()\n", - " \n", - " best_fit[root] = chain_postprocessing.extract_best_fit_params(chain, best_fit_method='2Dkde')" + "\n", + " best_fit[root] = chain_postprocessing.extract_best_fit_params(\n", + " chain, best_fit_method=\"2Dkde\"\n", + " )" ] }, { @@ -119,9 +122,40 @@ "metadata": {}, "outputs": [], "source": [ - "param_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1','alpha','beta', 'omch2', 'm', 'a_planck']\n", - "label_list = ['\\Omega_m', '\\omega_b', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1', '\\\\alpha_{PSF}', '\\\\beta_{PSF}', '\\omega_c', 'M', 'A_{\\rm Planck}']\n", - " " + "param_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + " \"omch2\",\n", + " \"m\",\n", + " \"a_planck\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + " \"\\\\alpha_{PSF}\",\n", + " \"\\\\beta_{PSF}\",\n", + " r\"\\omega_c\",\n", + " \"M\",\n", + " \"A_{\\rm Planck}\",\n", + "]" ] }, { @@ -137,7 +171,7 @@ "metadata": {}, "outputs": [], "source": [ - "if not os.path.exists(path_ini_files+'/values_empty.ini'):\n", + "if not os.path.exists(path_ini_files + \"/values_empty.ini\"):\n", " content = \"\"\"[cosmological_parameters]\n", "\n", "tau = 0.0544\n", @@ -157,11 +191,11 @@ "[psf_leakage_parameters]\n", "\"\"\"\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as f:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as f:\n", " f.write(content)\n", " f.close()\n", "\n", - " print('File created successfully')" + " print(\"File created successfully\")" ] }, { @@ -171,17 +205,17 @@ "outputs": [], "source": [ "section_map = {\n", - " 'omch2': 'cosmological_parameters',\n", - " 'ombh2': 'cosmological_parameters',\n", - " 'h0': 'cosmological_parameters',\n", - " 'n_s': 'cosmological_parameters',\n", - " 's_8_input': 'cosmological_parameters',\n", - " 'logt_agn': 'halo_model_parameters',\n", - " 'a': 'intrinsic_alignment_parameters',\n", - " 'm1': 'shear_calibration_parameters',\n", - " 'bias_1': 'nofz_shifts',\n", - " 'alpha': 'psf_leakage_parameters',\n", - " 'beta': 'psf_leakage_parameters',\n", + " \"omch2\": \"cosmological_parameters\",\n", + " \"ombh2\": \"cosmological_parameters\",\n", + " \"h0\": \"cosmological_parameters\",\n", + " \"n_s\": \"cosmological_parameters\",\n", + " \"s_8_input\": \"cosmological_parameters\",\n", + " \"logt_agn\": \"halo_model_parameters\",\n", + " \"a\": \"intrinsic_alignment_parameters\",\n", + " \"m1\": \"shear_calibration_parameters\",\n", + " \"bias_1\": \"nofz_shifts\",\n", + " \"alpha\": \"psf_leakage_parameters\",\n", + " \"beta\": \"psf_leakage_parameters\",\n", "}" ] }, @@ -192,8 +226,11 @@ "outputs": [], "source": [ "env = os.environ.copy()\n", - "env[\"LD_LIBRARY_PATH\"] = \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\" + env.get(\"LD_LIBRARY_PATH\", \"\")\n", - "for i,root in enumerate(roots):\n", + "env[\"LD_LIBRARY_PATH\"] = (\n", + " \"/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:\"\n", + " + env.get(\"LD_LIBRARY_PATH\", \"\")\n", + ")\n", + "for i, root in enumerate(roots):\n", " print(root)\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", @@ -202,49 +239,49 @@ " # Check if this parameter exists for the current root\n", " if param in best_fit[root]:\n", " value = best_fit[root][param]\n", - " \n", + "\n", " if section not in config:\n", " config.add_section(section)\n", "\n", " config[section][param] = str(value)\n", "\n", - " with open(path_ini_files+'/values_empty.ini', 'w') as configfile:\n", + " with open(path_ini_files + \"/values_empty.ini\", \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Modify the ini file to run in test mode at the best fit\n", + " # Modify the ini file to run in test mode at the best fit\n", " config = configparser.ConfigParser()\n", " config.optionxform = str # Preserve case sensitivity of option names\n", - " \n", - " ini_file = path_ini_files+f'config_space_v1.4.6.3_fiducial/pipeline/{ini_root}.ini'\n", + "\n", + " ini_file = (\n", + " path_ini_files + f\"config_space_v1.4.6.3_fiducial/pipeline/{ini_root}.ini\"\n", + " )\n", " config.read(ini_file)\n", "\n", - " sampler = config['runtime']['sampler']\n", - " config['runtime']['sampler'] = 'test'\n", - " values = config['pipeline']['values']\n", - " config['pipeline']['values'] = path_ini_files + '/values_empty.ini'\n", - " config['DEFAULT']['FITS_FILE'] = f'{root_glass_dv}/glass_mock_{root[-5:]}/cosmosis_glass_mock_v6_{root[-5:]}.fits'\n", - " config['test']['save_dir'] = output_folder_chains + f'{root}/best_fit_config'\n", - " \n", - " with open(ini_file, 'w') as configfile:\n", + " sampler = config[\"runtime\"][\"sampler\"]\n", + " config[\"runtime\"][\"sampler\"] = \"test\"\n", + " values = config[\"pipeline\"][\"values\"]\n", + " config[\"pipeline\"][\"values\"] = path_ini_files + \"/values_empty.ini\"\n", + " config[\"DEFAULT\"][\"FITS_FILE\"] = (\n", + " f\"{root_glass_dv}/glass_mock_{root[-5:]}/cosmosis_glass_mock_v6_{root[-5:]}.fits\"\n", + " )\n", + " config[\"test\"][\"save_dir\"] = output_folder_chains + f\"{root}/best_fit_config\"\n", + "\n", + " with open(ini_file, \"w\") as configfile:\n", " config.write(configfile)\n", "\n", - " #Run cosmosis\n", + " # Run cosmosis\n", " result = subprocess.run(\n", - " ['cosmosis', ini_file],\n", - " env=env,\n", - " capture_output=True,\n", - " text=True\n", + " [\"cosmosis\", ini_file], env=env, capture_output=True, text=True\n", " )\n", " # print(f\"STDOUT:\\n{result.stdout}\")\n", " # print(f\"STDERR:\\n{result.stderr}\")\n", "\n", - " #Modify the ini file to the previous one\n", - " config['pipeline']['values'] = values\n", - " config['runtime']['sampler'] = sampler\n", + " # Modify the ini file to the previous one\n", + " config[\"pipeline\"][\"values\"] = values\n", + " config[\"runtime\"][\"sampler\"] = sampler\n", "\n", - " with open(ini_file, 'w') as configfile:\n", - " config.write(configfile)\n", - " \n" + " with open(ini_file, \"w\") as configfile:\n", + " config.write(configfile)" ] }, { @@ -253,7 +290,6 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "xi_plus_chi2s = np.array([])\n", "xi_minus_chi2s = np.array([])\n", "xi_chi2s = np.array([])\n", @@ -261,76 +297,119 @@ "chi2_tots = np.array([])\n", "\n", "\n", - "for idx,root in enumerate(roots):\n", + "for idx, root in enumerate(roots):\n", " print(root)\n", "\n", - " data = fits.open(f'{root_glass_dv}/glass_mock_{root[-5:]}/cosmosis_glass_mock_v6_{root[-5:]}.fits')\n", - " \n", - " tau_0_data = data['TAU_0_PLUS'].data['VALUE']\n", - " tau_2_data = data['TAU_2_PLUS'].data['VALUE']\n", + " data = fits.open(\n", + " f\"{root_glass_dv}/glass_mock_{root[-5:]}/cosmosis_glass_mock_v6_{root[-5:]}.fits\"\n", + " )\n", + "\n", + " tau_0_data = data[\"TAU_0_PLUS\"].data[\"VALUE\"]\n", + " tau_2_data = data[\"TAU_2_PLUS\"].data[\"VALUE\"]\n", "\n", - " theta_data = data['XI_PLUS'].data['ANG']\n", - " xi_plus_data = data['XI_PLUS'].data['VALUE']\n", - " xi_minus_data = data['XI_MINUS'].data['VALUE']\n", + " theta_data = data[\"XI_PLUS\"].data[\"ANG\"]\n", + " xi_plus_data = data[\"XI_PLUS\"].data[\"VALUE\"]\n", + " xi_minus_data = data[\"XI_MINUS\"].data[\"VALUE\"]\n", " xi_data = np.concatenate((xi_plus_data, xi_minus_data))\n", "\n", " tau_data = np.concatenate((tau_0_data, tau_2_data))\n", "\n", - " #Apply scale cuts\n", + " # Apply scale cuts\n", " mask_xi_plus = (theta_data > lower_bound_xi) & (theta_data < upper_bound_xi)\n", " mask_xi_minus = (theta_data > lower_bound_xi) & (theta_data < upper_bound_xi)\n", " mask = np.concatenate((mask_xi_plus, mask_xi_minus))\n", - " #Load the covariance\n", - " cov = data['COVMAT'].data\n", - " cov_xi = cov[0:2*len(xi_plus_data), 0:2*len(xi_plus_data)]\n", - " cov_tau = cov[2*len(xi_plus_data):4*len(xi_plus_data), 2*len(xi_plus_data):4*len(xi_plus_data)]\n", + " # Load the covariance\n", + " cov = data[\"COVMAT\"].data\n", + " cov_xi = cov[0 : 2 * len(xi_plus_data), 0 : 2 * len(xi_plus_data)]\n", + " cov_tau = cov[\n", + " 2 * len(xi_plus_data) : 4 * len(xi_plus_data),\n", + " 2 * len(xi_plus_data) : 4 * len(xi_plus_data),\n", + " ]\n", " xi_data = xi_data[mask]\n", " cov_xi = cov_xi[mask][:, mask]\n", "\n", - " cov_xi_plus = cov[0:len(xi_plus_data), 0:len(xi_plus_data)]\n", + " cov_xi_plus = cov[0 : len(xi_plus_data), 0 : len(xi_plus_data)]\n", " cov_xi_plus = cov_xi_plus[mask_xi_plus][:, mask_xi_plus]\n", - " cov_xi_minus = cov[len(xi_plus_data):2*len(xi_minus_data), len(xi_plus_data):2*len(xi_minus_data)]\n", + " cov_xi_minus = cov[\n", + " len(xi_plus_data) : 2 * len(xi_minus_data),\n", + " len(xi_plus_data) : 2 * len(xi_minus_data),\n", + " ]\n", " cov_xi_minus = cov_xi_minus[mask_xi_minus][:, mask_xi_minus]\n", "\n", - " #Read the results\n", - " theta = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/shear_xi_plus/theta.txt')\n", + " # Read the results\n", + " theta = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/shear_xi_plus/theta.txt\"\n", + " )\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/shear_xi_plus/bin_1_1.txt')\n", - " shear_xi_minus = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/shear_xi_minus/bin_1_1.txt')\n", + " shear_xi_plus = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/shear_xi_plus/bin_1_1.txt\"\n", + " )\n", + " shear_xi_minus = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/shear_xi_minus/bin_1_1.txt\"\n", + " )\n", "\n", - " xi_sys_plus = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/xi_sys/shear_xi_plus.txt')\n", - " xi_sys_minus = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/xi_sys/shear_xi_minus.txt')\n", + " xi_sys_plus = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/xi_sys/shear_xi_plus.txt\"\n", + " )\n", + " xi_sys_minus = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/xi_sys/shear_xi_minus.txt\"\n", + " )\n", "\n", - " theta_tau = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/tau_0_plus/theta.txt')\n", + " theta_tau = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/tau_0_plus/theta.txt\"\n", + " )\n", " theta_tau_arcmin = theta_tau * 180 * 60 / np.pi\n", - " tau_0_model = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/tau_0_plus/bin_1_1.txt')\n", - " tau_2_model = np.loadtxt(output_folder_chains + f'{root}/best_fit_config/tau_2_plus/bin_1_1.txt')\n", + " tau_0_model = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/tau_0_plus/bin_1_1.txt\"\n", + " )\n", + " tau_2_model = np.loadtxt(\n", + " output_folder_chains + f\"{root}/best_fit_config/tau_2_plus/bin_1_1.txt\"\n", + " )\n", "\n", - " #interpolate the model\n", - " interp_xi_plus = interp1d(theta_arcmin, shear_xi_plus, kind='cubic', fill_value='extrapolate')\n", - " interp_xi_minus = interp1d(theta_arcmin, shear_xi_minus, kind='cubic', fill_value='extrapolate')\n", + " # interpolate the model\n", + " interp_xi_plus = interp1d(\n", + " theta_arcmin, shear_xi_plus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", + " interp_xi_minus = interp1d(\n", + " theta_arcmin, shear_xi_minus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", "\n", " xi_plus_model = interp_xi_plus(theta_data)\n", " xi_plus_model += xi_sys_plus\n", " xi_minus_model = interp_xi_minus(theta_data)\n", " xi_minus_model += xi_sys_minus\n", - " \n", + "\n", " xi_model = np.concatenate((xi_plus_model, xi_minus_model))\n", " tau_model = np.concatenate((tau_0_model, tau_2_model))\n", " xi_model = xi_model[mask]\n", "\n", - "\n", - " xi_plus_chi2 = np.dot((xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus]), np.dot(np.linalg.inv(cov_xi_plus), (xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus])))\n", - " xi_minus_chi2 = np.dot((xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus]), np.dot(np.linalg.inv(cov_xi_minus), (xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus])))\n", - " xi_chi2 = np.dot((xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data)))\n", - " tau_chi2 = np.dot((tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data)))\n", + " xi_plus_chi2 = np.dot(\n", + " (xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus]),\n", + " np.dot(\n", + " np.linalg.inv(cov_xi_plus),\n", + " (xi_plus_model[mask_xi_plus] - xi_plus_data[mask_xi_plus]),\n", + " ),\n", + " )\n", + " xi_minus_chi2 = np.dot(\n", + " (xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus]),\n", + " np.dot(\n", + " np.linalg.inv(cov_xi_minus),\n", + " (xi_minus_model[mask_xi_minus] - xi_minus_data[mask_xi_minus]),\n", + " ),\n", + " )\n", + " xi_chi2 = np.dot(\n", + " (xi_model - xi_data), np.dot(np.linalg.inv(cov_xi), (xi_model - xi_data))\n", + " )\n", + " tau_chi2 = np.dot(\n", + " (tau_model - tau_data), np.dot(np.linalg.inv(cov_tau), (tau_model - tau_data))\n", + " )\n", " chi2_tot = xi_plus_chi2 + xi_minus_chi2 + tau_chi2\n", "\n", " xi_plus_chi2s = np.append(xi_plus_chi2s, xi_plus_chi2)\n", " xi_minus_chi2s = np.append(xi_minus_chi2s, xi_minus_chi2)\n", " xi_chi2s = np.append(xi_chi2s, xi_chi2)\n", " tau_chi2s = np.append(tau_chi2s, tau_chi2)\n", - " chi2_tots = np.append(chi2_tots, chi2_tot)\n" + " chi2_tots = np.append(chi2_tots, chi2_tot)" ] }, { @@ -340,25 +419,21 @@ "outputs": [], "source": [ "fig, [ax1, ax2] = plt.subplots(2, 1, figsize=(7, 10))\n", - "chi2_fiducial = -2*-37.560916821678894\n", + "chi2_fiducial = -2 * -37.560916821678894\n", "dof, loc, scale = chi2.fit(chi2_tots, floc=0)\n", "\n", "print(f\"Best-fit dof: {dof:.3e}\")\n", - "counts, bin_edges = np.histogram(\n", - " chi2_tots,\n", - " bins=25,\n", - " density=True\n", - ")\n", + "counts, bin_edges = np.histogram(chi2_tots, bins=25, density=True)\n", "\n", "sns.histplot(\n", " chi2_tots,\n", " ax=ax1,\n", " kde=False,\n", " bins=bin_edges,\n", - " stat='density',\n", + " stat=\"density\",\n", " label=r\"$\\chi^2$ for \\texttt{GLASS} mocks best-fits\",\n", - " color='green',\n", - " alpha=0.3\n", + " color=\"green\",\n", + " alpha=0.3,\n", ")\n", "\n", "# Compute the p-value\n", @@ -371,14 +446,20 @@ "\n", "print(f\"P-value: {p_value}\")\n", "\n", - "ax1.axvline(chi2_fiducial, color='red', label=r'$\\chi^2$ of the fiducial', lw=2)\n", + "ax1.axvline(chi2_fiducial, color=\"red\", label=r\"$\\chi^2$ of the fiducial\", lw=2)\n", "\n", "mantissa, exponent = np.frexp(p_value)\n", "pte_string = rf\"${{\\rm PTE}} = {p_value:.4f}$\"\n", "print(f\"mantissa: {mantissa}, exponent: {exponent}\")\n", "x_text = 78\n", "y_text = max(counts) * 0.95\n", - "ax1.text(x_text, y_text, pte_string, fontsize=15, bbox=dict(facecolor='wheat', alpha=0.8, edgecolor='black'))\n", + "ax1.text(\n", + " x_text,\n", + " y_text,\n", + " pte_string,\n", + " fontsize=15,\n", + " bbox=dict(facecolor=\"wheat\", alpha=0.8, edgecolor=\"black\"),\n", + ")\n", "\n", "chi2_string = rf\"${{\\rm Eff. dof}}= {dof:.1f}$\"\n", "y_text = max(counts) * 0.85\n", @@ -387,7 +468,7 @@ " y_text,\n", " chi2_string,\n", " fontsize=15,\n", - " bbox=dict(facecolor='wheat', alpha=0.8, edgecolor='black')\n", + " bbox=dict(facecolor=\"wheat\", alpha=0.8, edgecolor=\"black\"),\n", ")\n", "\n", "ax1.set_xlabel(r\"$\\chi^2_{\\rm tot}$\")\n", @@ -397,21 +478,17 @@ "dof, loc, scale = chi2.fit(xi_chi2s, floc=0)\n", "\n", "print(f\"Best-fit dof: {dof:.3e}\")\n", - "counts, bin_edges = np.histogram(\n", - " xi_chi2s,\n", - " bins=25,\n", - " density=True\n", - ")\n", + "counts, bin_edges = np.histogram(xi_chi2s, bins=25, density=True)\n", "\n", "sns.histplot(\n", " xi_chi2s,\n", " ax=ax2,\n", " kde=False,\n", " bins=bin_edges,\n", - " stat='density',\n", + " stat=\"density\",\n", " label=r\"$\\chi^2$ for \\texttt{GLASS} mocks best-fits\",\n", - " color='pink',\n", - " alpha=0.5\n", + " color=\"pink\",\n", + " alpha=0.5,\n", ")\n", "\n", "# Compute the p-value\n", @@ -424,15 +501,21 @@ "\n", "print(f\"P-value: {p_value}\")\n", "\n", - "ax2.axvline(chi2_fiducial, color='red', label=r'$\\chi^2$ of the fiducial', lw=2)\n", + "ax2.axvline(chi2_fiducial, color=\"red\", label=r\"$\\chi^2$ of the fiducial\", lw=2)\n", "\n", "mantissa, exponent = np.frexp(p_value)\n", "print(f\"mantissa: {mantissa}, exponent: {exponent}\")\n", "pte_string = rf\"${{\\rm PTE}} = {p_value:.4f}$\"\n", - "# rf\"${{\\rm PTE}} = {mantissa:.2f} \\times 10^{{{exponent}}}$\" if exponent != 0 else \n", + "# rf\"${{\\rm PTE}} = {mantissa:.2f} \\times 10^{{{exponent}}}$\" if exponent != 0 else\n", "x_text = 17.5\n", "y_text = max(counts) * 0.95\n", - "ax2.text(x_text, y_text, pte_string, fontsize=15, bbox=dict(facecolor='wheat', alpha=0.8, edgecolor='black'))\n", + "ax2.text(\n", + " x_text,\n", + " y_text,\n", + " pte_string,\n", + " fontsize=15,\n", + " bbox=dict(facecolor=\"wheat\", alpha=0.8, edgecolor=\"black\"),\n", + ")\n", "\n", "chi2_string = rf\"${{\\rm Eff. dof}}= {dof:.1f}$\"\n", "y_text = max(counts) * 0.85\n", @@ -441,7 +524,7 @@ " y_text,\n", " chi2_string,\n", " fontsize=15,\n", - " bbox=dict(facecolor='wheat', alpha=0.8, edgecolor='black')\n", + " bbox=dict(facecolor=\"wheat\", alpha=0.8, edgecolor=\"black\"),\n", ")\n", "\n", "ax2.set_xlabel(r\"$\\chi^2 (\\xi_\\pm)$\")\n", diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_prior_psf_leakage.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_prior_psf_leakage.ipynb index 93a4dd2a..ad6f5fd1 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_prior_psf_leakage.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/get_prior_psf_leakage.ipynb @@ -18,33 +18,31 @@ "outputs": [], "source": [ "import os\n", + "\n", "if not os.path.exists(\"./Plots\"):\n", " os.makedirs(\"./Plots\")\n", "\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "from astropy.io import fits\n", + "import numpy as np\n", "import seaborn as sns\n", - "from getdist import plots, loadMCSamples, MCSamples\n", - "\n", - "from shear_psf_leakage.rho_tau_stat import RhoStat, TauStat, PSFErrorFit\n", + "from astropy.io import fits\n", + "from getdist import MCSamples, plots\n", + "from shear_psf_leakage.rho_tau_stat import PSFErrorFit, RhoStat, TauStat\n", "\n", "# Use paper style and seaborn with husl palette\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "# Set default palette - will be updated per plot as needed\n", "sns.set_palette(\"husl\")\n", "%matplotlib inline\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 25\n", "\n", - "ver = 'v1.4.6.3'\n", - "blind = 'B'" + "ver = \"v1.4.6.3\"\n", + "blind = \"B\"" ] }, { @@ -58,15 +56,9 @@ "\n", "path_cosmo_val = \"/home/guerrini/sp_validation/cosmo_val/output/\"\n", "\n", - "roots = [\n", - " f\"SP_{ver}_{blind}\",\n", - " f\"SP_{ver}_leak_corr_{blind}\"\n", - "]\n", + "roots = [f\"SP_{ver}_{blind}\", f\"SP_{ver}_leak_corr_{blind}\"]\n", "\n", - "labels = [\n", - " f\"SP_{ver}_{blind}\",\n", - " f\"SP_{ver}_leak_corr_{blind}\"\n", - "]" + "labels = [f\"SP_{ver}_{blind}\", f\"SP_{ver}_leak_corr_{blind}\"]" ] }, { @@ -80,7 +72,7 @@ "\n", "for root in roots:\n", " data_vectors.append(\n", - " fits.open(data_path +f\"SP_{ver}_{blind}/cosmosis_{root}_masked.fits\")\n", + " fits.open(data_path + f\"SP_{ver}_{blind}/cosmosis_{root}_masked.fits\")\n", " )" ] }, @@ -106,7 +98,7 @@ "metadata": {}, "outputs": [], "source": [ - "#Print the covariance matrix for each root\n", + "# Print the covariance matrix for each root\n", "for i, root in enumerate(roots):\n", " print(f\"Covariance matrix for {labels[i]}:\")\n", " cov = data_vectors[i][\"COVMAT\"].data\n", @@ -116,14 +108,29 @@ " fig, ax = plt.subplots(figsize=(10, 8))\n", "\n", " im = ax.imshow(cov_to_corr(cov), vmin=-1, vmax=1, cmap=\"seismic\")\n", - " ax.set_aspect('equal')\n", + " ax.set_aspect(\"equal\")\n", " ax.set_yticks(np.array([10, 30, 50, 70]))\n", - " ax.set_yticklabels([r\"$\\xi_+(\\vartheta)$\", r\"$\\xi_-(\\vartheta)$\", r\"$\\tau_0(\\vartheta)$\", r\"$\\tau_2(\\vartheta)$\"])\n", + " ax.set_yticklabels(\n", + " [\n", + " r\"$\\xi_+(\\vartheta)$\",\n", + " r\"$\\xi_-(\\vartheta)$\",\n", + " r\"$\\tau_0(\\vartheta)$\",\n", + " r\"$\\tau_2(\\vartheta)$\",\n", + " ]\n", + " )\n", " ax.set_xticks(np.array([10, 30, 50, 70]))\n", - " ax.set_xticklabels([r\"$\\xi_+(\\vartheta)$\", r\"$\\xi_-(\\vartheta)$\", r\"$\\tau_0(\\vartheta)$\", r\"$\\tau_2(\\vartheta)$\"], rotation=45)\n", + " ax.set_xticklabels(\n", + " [\n", + " r\"$\\xi_+(\\vartheta)$\",\n", + " r\"$\\xi_-(\\vartheta)$\",\n", + " r\"$\\tau_0(\\vartheta)$\",\n", + " r\"$\\tau_2(\\vartheta)$\",\n", + " ],\n", + " rotation=45,\n", + " )\n", " fig.colorbar(im, ax=ax)\n", "\n", - " plt.savefig(f\"./Plots/cov_matrix_{root}.png\", bbox_inches='tight', dpi=300)\n", + " plt.savefig(f\"./Plots/cov_matrix_{root}.png\", bbox_inches=\"tight\", dpi=300)\n", " plt.show()\n", " print(\"\\n\")" ] @@ -135,38 +142,34 @@ "metadata": {}, "outputs": [], "source": [ - "#Create dummy rho and tau stat handler.\n", + "# Create dummy rho and tau stat handler.\n", "\n", - "#Inference of the xi_sys parameters\n", - "sep_units = 'arcmin'\n", - "coord_units = 'degrees'\n", + "# Inference of the xi_sys parameters\n", + "sep_units = \"arcmin\"\n", + "coord_units = \"degrees\"\n", "theta_min = 1.0\n", "theta_max = 250\n", "nbins = 20\n", "\n", "\n", "TreeCorrConfig_xi = {\n", - " 'ra_units': coord_units,\n", - " 'dec_units': coord_units,\n", - " 'min_sep': theta_min,\n", - " 'max_sep': theta_max,\n", - " 'sep_units': sep_units,\n", - " 'nbins': nbins,\n", - " 'var_method':'jackknife',\n", + " \"ra_units\": coord_units,\n", + " \"dec_units\": coord_units,\n", + " \"min_sep\": theta_min,\n", + " \"max_sep\": theta_max,\n", + " \"sep_units\": sep_units,\n", + " \"nbins\": nbins,\n", + " \"var_method\": \"jackknife\",\n", "}\n", "\n", - "rho_stats_handler = RhoStat(\n", - " output='.',\n", - " treecorr_config=TreeCorrConfig_xi,\n", - " verbose=True\n", - ")\n", + "rho_stats_handler = RhoStat(output=\".\", treecorr_config=TreeCorrConfig_xi, verbose=True)\n", "\n", "tau_stats_handler = TauStat(\n", " catalogs=rho_stats_handler.catalogs,\n", - " output='.',\n", + " output=\".\",\n", " treecorr_config=TreeCorrConfig_xi,\n", - " verbose=True\n", - " )" + " verbose=True,\n", + ")" ] }, { @@ -176,19 +179,24 @@ "metadata": {}, "outputs": [], "source": [ - "#Create a PSFErrorFit instance\n", - "psf_fitter = PSFErrorFit(rho_stats_handler, tau_stats_handler, path_cosmo_val+'rho_tau_stats/', use_eta=False)\n", + "# Create a PSFErrorFit instance\n", + "psf_fitter = PSFErrorFit(\n", + " rho_stats_handler,\n", + " tau_stats_handler,\n", + " path_cosmo_val + \"rho_tau_stats/\",\n", + " use_eta=False,\n", + ")\n", "\n", - "g = plots.get_subplot_plotter(width_inch=30)\n", + "g = plots.get_subplot_plotter(width_inch=30)\n", "\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "chains = []\n", "\n", - "#Load rho-, tau-statistics, and cov_tau from the data_vector\n", + "# Load rho-, tau-statistics, and cov_tau from the data_vector\n", "for i, root in enumerate(roots):\n", " print(\"Sampling PSF parameters for \", labels[i])\n", " path_rho = f\"rho_stats_{root}.fits\"\n", @@ -197,19 +205,23 @@ " path_cov_tau = f\"cov_tau_{root}_th.npy\"\n", " psf_fitter.load_rho_stat(path_rho)\n", " psf_fitter.load_tau_stat(path_tau)\n", - " psf_fitter.load_covariance(path_cov_rho, cov_type='rho')\n", - " psf_fitter.load_covariance(path_cov_tau, cov_type='tau')\n", - " samples_lq, _, _ = psf_fitter.get_least_squares_params_samples(npatch=None, apply_debias=False)\n", + " psf_fitter.load_covariance(path_cov_rho, cov_type=\"rho\")\n", + " psf_fitter.load_covariance(path_cov_tau, cov_type=\"tau\")\n", + " samples_lq, _, _ = psf_fitter.get_least_squares_params_samples(\n", + " npatch=None, apply_debias=False\n", + " )\n", "\n", - " samples_gd = MCSamples(samples=samples_lq, names=[r\"\\alpha\", r\"\\beta\"], labels=[r\"\\alpha\", r\"\\beta\"])\n", + " samples_gd = MCSamples(\n", + " samples=samples_lq, names=[r\"\\alpha\", r\"\\beta\"], labels=[r\"\\alpha\", r\"\\beta\"]\n", + " )\n", "\n", " chains.append(samples_gd)\n", "\n", - "g.triangle_plot(chains,\n", - " filled=True,\n", - " legend_labels=labels,\n", - " legend_loc='upper right',\n", - " \n", + "g.triangle_plot(\n", + " chains,\n", + " filled=True,\n", + " legend_labels=labels,\n", + " legend_loc=\"upper right\",\n", ")\n", "\n", "# plt.savefig(f\"./Plots/psf_leakage_params.png\", bbox_inches='tight', dpi=300)\n", diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/glass_mock_hist.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/glass_mock_hist.ipynb index 259824e2..32f89a18 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/glass_mock_hist.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/glass_mock_hist.ipynb @@ -14,38 +14,31 @@ "ipython = IPython.get_ipython()\n", "\n", "if ipython is not None:\n", - " ipython.run_line_magic('load_ext', 'autoreload')\n", - " ipython.run_line_magic('autoreload', '2')\n", + " ipython.run_line_magic(\"load_ext\", \"autoreload\")\n", + " ipython.run_line_magic(\"autoreload\", \"2\")\n", "\n", "import os\n", - "import copy\n", - "from tqdm import tqdm\n", "\n", "import matplotlib.pyplot as plt\n", - "from mpl_toolkits.axes_grid1 import make_axes_locatable\n", "import numpy as np\n", - "import healpy as hp\n", "import seaborn as sns\n", - "from astropy.io import fits\n", - "\n", - "from getdist import plots, MCSamples\n", + "from getdist import plots\n", + "from tqdm import tqdm\n", "\n", "g = plots.get_subplot_plotter(width_inch=7)\n", - "g.settings.axes_fontsize=15\n", - "g.settings.axes_labelsize=15\n", + "g.settings.axes_fontsize = 15\n", + "g.settings.axes_labelsize = 15\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 15\n", "\n", "if os.path.exists(\"/home/guerrini/matplotlib_config/paper.mplstyle\"):\n", - " plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - " )\n", + " plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "# Set default palette - will be updated per plot as needed\n", "sns.set_palette(\"husl\")\n", "\n", "if ipython is not None:\n", - " ipython.run_line_magic('matplotlib', 'inline')" + " ipython.run_line_magic(\"matplotlib\", \"inline\")" ] }, { @@ -57,71 +50,85 @@ }, "outputs": [], "source": [ - "root_dir = '/n09data/guerrini/glass_mock_chains/'\n", - "chain_version = 'v6'\n", + "root_dir = \"/n09data/guerrini/glass_mock_chains/\"\n", + "chain_version = \"v6\"\n", "num_sims = 350\n", "\n", - "roots = [\n", - " f\"glass_mock_{chain_version}_{i+1:05d}\" for i in range(num_sims)\n", - "]\n", + "roots = [f\"glass_mock_{chain_version}_{i + 1:05d}\" for i in range(num_sims)]\n", "\n", "\n", "# # %%\n", "def load_samples_and_write_paramames(root_dir, root, chain_type=\"configuration\"):\n", - " assert chain_type in [\"configuration\", \"harmonic\"], \"chain_type must be 'configuration' or 'harmonic'\"\n", + " assert chain_type in [\"configuration\", \"harmonic\"], (\n", + " \"chain_type must be 'configuration' or 'harmonic'\"\n", + " )\n", "\n", " if chain_type == \"configuration\":\n", - " path_samples = root_dir + '{}/{}/samples_{}.txt'.format('/'+root, root, root)\n", - " path_paramnames = root_dir + '{}/{}/getdist_{}.paramnames'.format('/'+root, root, root)\n", + " path_samples = root_dir + \"{}/{}/samples_{}.txt\".format(\"/\" + root, root, root)\n", + " path_paramnames = root_dir + \"{}/{}/getdist_{}.paramnames\".format(\n", + " \"/\" + root, root, root\n", + " )\n", " else:\n", - " path_samples = root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root, root, root)\n", - " path_paramnames = root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root)\n", - " \n", - " with open(path_samples, 'r') as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " path_samples = root_dir + \"{}/{}/samples_{}_cell.txt\".format(\n", + " \"/\" + root, root, root\n", + " )\n", + " path_paramnames = root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\n", + " \"/\" + root, root, root\n", + " )\n", + "\n", + " with open(path_samples, \"r\") as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", "\n", - " with open(path_paramnames, 'w') as file:\n", + " with open(path_paramnames, \"w\") as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()\n", "\n", + "\n", "def write_samples_getdist_format(root_dir, root, chain_type=\"configuration\"):\n", - " assert chain_type in [\"configuration\", \"harmonic\"], \"chain_type must be 'configuration' or 'harmonic'\"\n", + " assert chain_type in [\"configuration\", \"harmonic\"], (\n", + " \"chain_type must be 'configuration' or 'harmonic'\"\n", + " )\n", "\n", " if chain_type == \"configuration\":\n", - " path_samples = root_dir + '{}/{}/samples_{}.txt'.format('/'+root, root, root)\n", - " path_gd_samples = root_dir + '{}/{}/getdist_{}.txt'.format('/'+root, root, root)\n", - " path_gd = root_dir + '{}/{}/getdist_{}'.format(root,root,root)\n", + " path_samples = root_dir + \"{}/{}/samples_{}.txt\".format(\"/\" + root, root, root)\n", + " path_gd_samples = root_dir + \"{}/{}/getdist_{}.txt\".format(\n", + " \"/\" + root, root, root\n", + " )\n", + " path_gd = root_dir + \"{}/{}/getdist_{}\".format(root, root, root)\n", " else:\n", - " path_samples = root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root, root, root)\n", - " path_gd_samples = root_dir + '{}/{}/getdist_{}_cell.txt'.format('/'+root, root, root)\n", - " path_gd = root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root)\n", - " \n", + " path_samples = root_dir + \"{}/{}/samples_{}_cell.txt\".format(\n", + " \"/\" + root, root, root\n", + " )\n", + " path_gd_samples = root_dir + \"{}/{}/getdist_{}_cell.txt\".format(\n", + " \"/\" + root, root, root\n", + " )\n", + " path_gd = root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root)\n", + "\n", " samples = np.loadtxt(\n", " path_samples,\n", " )\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-2],samples[:,0:-4]))\n", + " samples = np.column_stack((samples[:, -1], samples[:, -2], samples[:, 0:-4]))\n", " np.savetxt(path_gd_samples, samples)\n", "\n", " chain = g.samples_for_root(\n", " path_gd,\n", " cache=False,\n", - " settings={\n", - " 'ignore_rows': 0.,\n", - " 'smooth_scale_2D': 0.5,\n", - " 'smooth_scale_1D': 0.5\n", - " }\n", + " settings={\"ignore_rows\": 0.0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", " )\n", "\n", " return chain\n", "\n", + "\n", "def extract_param_chain(chain, param_names):\n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", @@ -130,14 +137,14 @@ " for param_name in param_names:\n", " if param_name not in chain.getParamNames().list():\n", " raise ValueError(f\"Parameter {param_name} not found in chain.\")\n", - " \n", + "\n", " param_stats = margestats.parWithName(param_name)\n", " param_values[param_name] = {\n", - " 'mean': param_stats.mean,\n", - " '1sigma_minus': param_stats.mean - param_stats.limits[0].lower,\n", - " '1sigma_plus': param_stats.limits[0].upper - param_stats.mean,\n", - " '2sigma_minus': param_stats.mean - param_stats.limits[1].lower,\n", - " '2sigma_plus': param_stats.limits[1].upper - param_stats.mean,\n", + " \"mean\": param_stats.mean,\n", + " \"1sigma_minus\": param_stats.mean - param_stats.limits[0].lower,\n", + " \"1sigma_plus\": param_stats.limits[0].upper - param_stats.mean,\n", + " \"2sigma_minus\": param_stats.mean - param_stats.limits[1].lower,\n", + " \"2sigma_plus\": param_stats.limits[1].upper - param_stats.mean,\n", " }\n", "\n", " param_stats = likestats.parWithName(param_name)\n", @@ -145,83 +152,98 @@ " par = param_names_getdist.parWithName(param_name)\n", " kde = chain.get1DDensity(par, num_bins=1000)\n", " kde_map = kde.x[np.argmax(kde.P)]\n", - " param_values[param_name].update({\n", - " 'MAP': kde_map,\n", - " })\n", - " \n", + " param_values[param_name].update(\n", + " {\n", + " \"MAP\": kde_map,\n", + " }\n", + " )\n", + "\n", " par = chain.getParamNames().parWithName(\"S_8\")\n", " par_om = chain.getParamNames().parWithName(\"OMEGA_M\")\n", " kde = chain.get2DDensity(par, par_om, fine_bins_2D=1000)\n", " s8_kde_map = kde.x[np.unravel_index(np.argmax(kde.P), kde.P.shape)[1]]\n", " om_kde_map = kde.y[np.unravel_index(np.argmax(kde.P), kde.P.shape)[0]]\n", - " param_values[\"S_8\"].update({\n", - " 'MAP_2D': s8_kde_map,\n", - " })\n", - " param_values[\"OMEGA_M\"].update({\n", - " 'MAP_2D': om_kde_map,\n", - " })\n", + " param_values[\"S_8\"].update(\n", + " {\n", + " \"MAP_2D\": s8_kde_map,\n", + " }\n", + " )\n", + " param_values[\"OMEGA_M\"].update(\n", + " {\n", + " \"MAP_2D\": om_kde_map,\n", + " }\n", + " )\n", "\n", " return param_values\n", "\n", + "\n", "def concatenate_param_stats(name, param_values, verbose=False):\n", " output = [name]\n", " for key in param_values.keys():\n", " param_stat = param_values[key]\n", " if verbose:\n", - " print(f\"{name} - {key}: {param_stat['mean']:.4f} +{param_stat['1sigma_plus']:.4f}/-{param_stat['1sigma_minus']:.4f} (1σ), +{param_stat['2sigma_plus']:.4f}/-{param_stat['2sigma_minus']:.4f} (2σ)\")\n", + " print(\n", + " f\"{name} - {key}: {param_stat['mean']:.4f} +{param_stat['1sigma_plus']:.4f}/-{param_stat['1sigma_minus']:.4f} (1σ), +{param_stat['2sigma_plus']:.4f}/-{param_stat['2sigma_minus']:.4f} (2σ)\"\n", + " )\n", "\n", " param_list = [\n", - " param_stat['mean'],\n", - " param_stat['1sigma_minus'],\n", - " param_stat['1sigma_plus'],\n", - " param_stat['2sigma_minus'],\n", - " param_stat['2sigma_plus'],\n", - " param_stat['MAP']\n", + " param_stat[\"mean\"],\n", + " param_stat[\"1sigma_minus\"],\n", + " param_stat[\"1sigma_plus\"],\n", + " param_stat[\"2sigma_minus\"],\n", + " param_stat[\"2sigma_plus\"],\n", + " param_stat[\"MAP\"],\n", " ]\n", "\n", " if key == \"S_8\":\n", - " param_list.append(param_stat['MAP_2D'])\n", - " \n", + " param_list.append(param_stat[\"MAP_2D\"])\n", + "\n", " if key == \"OMEGA_M\":\n", - " param_list.append(param_stat['MAP_2D'])\n", + " param_list.append(param_stat[\"MAP_2D\"])\n", "\n", " output += param_list\n", "\n", " return output\n", "\n", + "\n", "def merge_param_stats(params_configuration, params_harmonic):\n", " merged_params = {}\n", " for key in params_configuration.keys():\n", " if key in params_harmonic:\n", " merged_params[key] = {\n", - " 'configuration': params_configuration[key],\n", - " 'harmonic': params_harmonic[key]\n", + " \"configuration\": params_configuration[key],\n", + " \"harmonic\": params_harmonic[key],\n", " }\n", " return merged_params\n", "\n", + "\n", "def concatenate_merge_params(name, merged_params, verbose=False):\n", " output = [name]\n", " for key in merged_params.keys():\n", - " param_config = merged_params[key]['configuration']\n", - " param_harm = merged_params[key]['harmonic']\n", + " param_config = merged_params[key][\"configuration\"]\n", + " param_harm = merged_params[key][\"harmonic\"]\n", "\n", " if verbose:\n", - " print(f\"{name} - {key} (Configuration): {param_config['mean']:.4f} +{param_config['1sigma_plus']:.4f}/-{param_config['1sigma_minus']:.4f} (1σ), +{param_config['2sigma_plus']:.4f}/-{param_config['2sigma_minus']:.4f} (2σ)\")\n", - " print(f\"{name} - {key} (Harmonic): {param_harm['mean']:.4f} +{param_harm['1sigma_plus']:.4f}/-{param_harm['1sigma_minus']:.4f} (1σ), +{param_harm['2sigma_plus']:.4f}/-{param_harm['2sigma_minus']:.4f} (2σ)\")\n", + " print(\n", + " f\"{name} - {key} (Configuration): {param_config['mean']:.4f} +{param_config['1sigma_plus']:.4f}/-{param_config['1sigma_minus']:.4f} (1σ), +{param_config['2sigma_plus']:.4f}/-{param_config['2sigma_minus']:.4f} (2σ)\"\n", + " )\n", + " print(\n", + " f\"{name} - {key} (Harmonic): {param_harm['mean']:.4f} +{param_harm['1sigma_plus']:.4f}/-{param_harm['1sigma_minus']:.4f} (1σ), +{param_harm['2sigma_plus']:.4f}/-{param_harm['2sigma_minus']:.4f} (2σ)\"\n", + " )\n", "\n", " param_list = [\n", - " param_config['mean'],\n", - " param_config['1sigma_minus'],\n", - " param_config['1sigma_plus'],\n", - " param_config['2sigma_minus'],\n", - " param_config['2sigma_plus'],\n", - " param_config['MAP'],\n", - " param_harm['mean'],\n", - " param_harm['1sigma_minus'],\n", - " param_harm['1sigma_plus'],\n", - " param_harm['2sigma_minus'],\n", - " param_harm['2sigma_plus'],\n", - " param_harm['MAP']\n", + " param_config[\"mean\"],\n", + " param_config[\"1sigma_minus\"],\n", + " param_config[\"1sigma_plus\"],\n", + " param_config[\"2sigma_minus\"],\n", + " param_config[\"2sigma_plus\"],\n", + " param_config[\"MAP\"],\n", + " param_harm[\"mean\"],\n", + " param_harm[\"1sigma_minus\"],\n", + " param_harm[\"1sigma_plus\"],\n", + " param_harm[\"2sigma_minus\"],\n", + " param_harm[\"2sigma_plus\"],\n", + " param_harm[\"MAP\"],\n", " ]\n", "\n", " output += param_list\n", @@ -242,70 +264,132 @@ "chain_config = []\n", "\n", "for i, root in enumerate(tqdm(roots)):\n", - " if os.path.isfile(f'{root_dir}/{root}/{root}/getdist_{root}.txt'):\n", + " if os.path.isfile(f\"{root_dir}/{root}/{root}/getdist_{root}.txt\"):\n", " # Load samples and write paramnames for harmonic space\n", " load_samples_and_write_paramames(root_dir, root, chain_type=\"harmonic\")\n", " write_samples_getdist_format(root_dir, root, chain_type=\"harmonic\")\n", " chain_harm = g.samples_for_root(\n", - " root_dir + f'/{root}/{root}/getdist_{root}_cell',\n", + " root_dir + f\"/{root}/{root}/getdist_{root}_cell\",\n", " cache=False,\n", " settings={\n", - " 'ignore_rows': 0.,\n", - " 'smooth_scale_2D': 0.5,\n", - " 'smooth_scale_1D': 0.5\n", - " }\n", + " \"ignore_rows\": 0.0,\n", + " \"smooth_scale_2D\": 0.5,\n", + " \"smooth_scale_1D\": 0.5,\n", + " },\n", " )\n", " chain_harmonic.append(chain_harm)\n", - " \n", - " # Load samples and write paramnames for harmonic space\n", + "\n", + " # Load samples and write paramnames for harmonic space\n", " load_samples_and_write_paramames(root_dir, root, chain_type=\"configuration\")\n", " write_samples_getdist_format(root_dir, root, chain_type=\"configuration\")\n", " chain_conf = g.samples_for_root(\n", - " root_dir + f'/{root}/{root}/getdist_{root}',\n", + " root_dir + f\"/{root}/{root}/getdist_{root}\",\n", " cache=False,\n", " settings={\n", - " 'ignore_rows': 0.,\n", - " 'smooth_scale_2D': 0.5,\n", - " 'smooth_scale_1D': 0.5\n", - " }\n", + " \"ignore_rows\": 0.0,\n", + " \"smooth_scale_2D\": 0.5,\n", + " \"smooth_scale_1D\": 0.5,\n", + " },\n", " )\n", " chain_config.append(chain_conf)\n", "# # %%\n", - "param_names = ['S_8', 'OMEGA_M', 'SIGMA_8', 'a']\n", - "\n", - "output_mocks_harm = np.array([\n", - " \"Name\",\n", - " \"S8_mean\", \"S8_1sigma_minus\", \"S8_1sigma_plus\", \"S8_2sigma_minus\", \"S8_2sigma_plus\", \"S8_MAP\", \"S8_MAP_2D\",\n", - " \"OMEGA_M_mean\", \"OMEGA_M_1sigma_minus\", \"OMEGA_M_1sigma_plus\", \"OMEGA_M_2sigma_minus\", \"OMEGA_M_2sigma_plus\", \"OMEGA_M_MAP\", \"OMEGA_M_MAP_2D\",\n", - " \"SIGMA_8_mean\", \"SIGMA_8_1sigma_minus\", \"SIGMA_8_1sigma_plus\", \"SIGMA_8_2sigma_minus\", \"SIGMA_8_2sigma_plus\", \"SIGMA_8_MAP\",\n", - " \"a_mean\", \"a_1sigma_minus\", \"a_1sigma_plus\", \"a_2sigma_minus\", \"a_2sigma_plus\", \"a_MAP\"\n", - "])\n", - "\n", - "output_mocks_config = np.array([\n", - " \"Name\",\n", - " \"S8_mean\", \"S8_1sigma_minus\", \"S8_1sigma_plus\", \"S8_2sigma_minus\", \"S8_2sigma_plus\", \"S8_MAP\", \"S8_MAP_2D\",\n", - " \"OMEGA_M_mean\", \"OMEGA_M_1sigma_minus\", \"OMEGA_M_1sigma_plus\", \"OMEGA_M_2sigma_minus\", \"OMEGA_M_2sigma_plus\", \"OMEGA_M_MAP\", \"OMEGA_M_MAP_2D\",\n", - " \"SIGMA_8_mean\", \"SIGMA_8_1sigma_minus\", \"SIGMA_8_1sigma_plus\", \"SIGMA_8_2sigma_minus\", \"SIGMA_8_2sigma_plus\", \"SIGMA_8_MAP\",\n", - " \"a_mean\", \"a_1sigma_minus\", \"a_1sigma_plus\", \"a_2sigma_minus\", \"a_2sigma_plus\", \"a_MAP\"\n", - "])\n", + "param_names = [\"S_8\", \"OMEGA_M\", \"SIGMA_8\", \"a\"]\n", + "\n", + "output_mocks_harm = np.array(\n", + " [\n", + " \"Name\",\n", + " \"S8_mean\",\n", + " \"S8_1sigma_minus\",\n", + " \"S8_1sigma_plus\",\n", + " \"S8_2sigma_minus\",\n", + " \"S8_2sigma_plus\",\n", + " \"S8_MAP\",\n", + " \"S8_MAP_2D\",\n", + " \"OMEGA_M_mean\",\n", + " \"OMEGA_M_1sigma_minus\",\n", + " \"OMEGA_M_1sigma_plus\",\n", + " \"OMEGA_M_2sigma_minus\",\n", + " \"OMEGA_M_2sigma_plus\",\n", + " \"OMEGA_M_MAP\",\n", + " \"OMEGA_M_MAP_2D\",\n", + " \"SIGMA_8_mean\",\n", + " \"SIGMA_8_1sigma_minus\",\n", + " \"SIGMA_8_1sigma_plus\",\n", + " \"SIGMA_8_2sigma_minus\",\n", + " \"SIGMA_8_2sigma_plus\",\n", + " \"SIGMA_8_MAP\",\n", + " \"a_mean\",\n", + " \"a_1sigma_minus\",\n", + " \"a_1sigma_plus\",\n", + " \"a_2sigma_minus\",\n", + " \"a_2sigma_plus\",\n", + " \"a_MAP\",\n", + " ]\n", + ")\n", + "\n", + "output_mocks_config = np.array(\n", + " [\n", + " \"Name\",\n", + " \"S8_mean\",\n", + " \"S8_1sigma_minus\",\n", + " \"S8_1sigma_plus\",\n", + " \"S8_2sigma_minus\",\n", + " \"S8_2sigma_plus\",\n", + " \"S8_MAP\",\n", + " \"S8_MAP_2D\",\n", + " \"OMEGA_M_mean\",\n", + " \"OMEGA_M_1sigma_minus\",\n", + " \"OMEGA_M_1sigma_plus\",\n", + " \"OMEGA_M_2sigma_minus\",\n", + " \"OMEGA_M_2sigma_plus\",\n", + " \"OMEGA_M_MAP\",\n", + " \"OMEGA_M_MAP_2D\",\n", + " \"SIGMA_8_mean\",\n", + " \"SIGMA_8_1sigma_minus\",\n", + " \"SIGMA_8_1sigma_plus\",\n", + " \"SIGMA_8_2sigma_minus\",\n", + " \"SIGMA_8_2sigma_plus\",\n", + " \"SIGMA_8_MAP\",\n", + " \"a_mean\",\n", + " \"a_1sigma_minus\",\n", + " \"a_1sigma_plus\",\n", + " \"a_2sigma_minus\",\n", + " \"a_2sigma_plus\",\n", + " \"a_MAP\",\n", + " ]\n", + ")\n", "\n", "for i, root in enumerate(tqdm(roots[:-1])):\n", " param_values_harm = extract_param_chain(chain_harmonic[i], param_names)\n", "\n", " param_harm = concatenate_param_stats(root, param_values_harm, verbose=False)\n", - " \n", + "\n", " output_mocks_harm = np.vstack((output_mocks_harm, param_harm))\n", "\n", " param_values_config = extract_param_chain(chain_config[i], param_names)\n", "\n", " param_config = concatenate_param_stats(root, param_values_config, verbose=False)\n", - " \n", + "\n", " output_mocks_config = np.vstack((output_mocks_config, param_config))\n", "\n", - "np.savetxt(f\"summary_parameter_constraints_harmonic_space_{chain_version}.txt\", output_mocks_harm, fmt='%s', delimiter=';')\n", - "np.savetxt(f\"summary_parameter_constraints_configuration_space_{chain_version}.txt\", output_mocks_config, fmt='%s', delimiter=';')\n", - "print(f\"Saved summary of parameter constraints for harmonic space in summary_parameter_constraints_harmonic_space_{chain_version}.txt\")\n", - "print(f\"Saved summary of parameter constraints for configuration space in summary_parameter_constraints_configuration_space_{chain_version}.txt\")" + "np.savetxt(\n", + " f\"summary_parameter_constraints_harmonic_space_{chain_version}.txt\",\n", + " output_mocks_harm,\n", + " fmt=\"%s\",\n", + " delimiter=\";\",\n", + ")\n", + "np.savetxt(\n", + " f\"summary_parameter_constraints_configuration_space_{chain_version}.txt\",\n", + " output_mocks_config,\n", + " fmt=\"%s\",\n", + " delimiter=\";\",\n", + ")\n", + "print(\n", + " f\"Saved summary of parameter constraints for harmonic space in summary_parameter_constraints_harmonic_space_{chain_version}.txt\"\n", + ")\n", + "print(\n", + " f\"Saved summary of parameter constraints for configuration space in summary_parameter_constraints_configuration_space_{chain_version}.txt\"\n", + ")" ] }, { @@ -321,16 +405,16 @@ "\n", "output_df_harm = pd.read_csv(\n", " f\"summary_parameter_constraints_harmonic_space_{chain_version}.txt\",\n", - " delimiter=';',\n", + " delimiter=\";\",\n", " skiprows=1,\n", - " names=output_mocks_harm[0]\n", + " names=output_mocks_harm[0],\n", ")\n", "\n", "output_df_config = pd.read_csv(\n", " f\"summary_parameter_constraints_configuration_space_{chain_version}.txt\",\n", - " delimiter=';',\n", + " delimiter=\";\",\n", " skiprows=1,\n", - " names=output_mocks_config[0]\n", + " names=output_mocks_config[0],\n", ")" ] }, @@ -346,11 +430,13 @@ "\n", "Omega_m_fid = planck.Om0\n", "sigma_8_fid = 0.8102\n", - "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3)**0.5\n", + "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3) ** 0.5\n", "h = planck.h\n", "Omega_b_fig = planck.Ob0\n", "n_s_fid = 0.9665\n", - "print(f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\")" + "print(\n", + " f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\"\n", + ")" ] }, { @@ -361,10 +447,10 @@ "outputs": [], "source": [ "sns.histplot(\n", - " output_df_harm[\"S8_mean\"]-output_df_config[\"S8_mean\"],\n", + " output_df_harm[\"S8_mean\"] - output_df_config[\"S8_mean\"],\n", " kde=True,\n", " bins=30,\n", - " label=\"Mean\"\n", + " label=\"Mean\",\n", ")\n", "# sns.histplot(\n", "# output_df_harm[\"S8_MAP\"]-output_df_config[\"S8_MAP\"],\n", @@ -373,17 +459,20 @@ "# label=\"MAP\",\n", "# )\n", "sns.histplot(\n", - " output_df_harm[\"S8_MAP_2D\"]-output_df_config[\"S8_MAP_2D\"],\n", + " output_df_harm[\"S8_MAP_2D\"] - output_df_config[\"S8_MAP_2D\"],\n", " kde=True,\n", " bins=30,\n", " label=\"2D Mode\",\n", - " alpha=0.5\n", + " alpha=0.5,\n", ")\n", "plt.axvline(0, color=\"black\", linestyle=\"--\")\n", "plt.legend(fontsize=12)\n", "\n", "plt.xlabel(r\"$\\Delta S_8$\")\n", - "plt.savefig(\"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/S8_comparison_harmonic_vs_configuration.pdf\", bbox_inches='tight')\n", + "plt.savefig(\n", + " \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/S8_comparison_harmonic_vs_configuration.pdf\",\n", + " bbox_inches=\"tight\",\n", + ")\n", "plt.show()" ] }, @@ -408,35 +497,46 @@ "outputs": [], "source": [ "# Create JointGrid\n", - "g = sns.JointGrid(x=output_df_config['OMEGA_M_MAP_2D'], y=output_df_config['S8_MAP_2D'], height=7, ratio=5, space=0)\n", + "g = sns.JointGrid(\n", + " x=output_df_config[\"OMEGA_M_MAP_2D\"],\n", + " y=output_df_config[\"S8_MAP_2D\"],\n", + " height=7,\n", + " ratio=5,\n", + " space=0,\n", + ")\n", "\n", "# Main 2D histogram\n", "sns.histplot(\n", - " x=output_df_config['OMEGA_M_MAP_2D'],\n", - " y=output_df_config['S8_MAP_2D'],\n", + " x=output_df_config[\"OMEGA_M_MAP_2D\"],\n", + " y=output_df_config[\"S8_MAP_2D\"],\n", " bins=25,\n", " cmap=\"Greens\",\n", " cbar=False,\n", - " ax=g.ax_joint\n", + " ax=g.ax_joint,\n", ")\n", "\n", "# Marginal histograms\n", - "sns.histplot(x=output_df_config['OMEGA_M_MAP_2D'], bins=25, color=\"#2ca25f\", ax=g.ax_marg_x)\n", - "sns.histplot(y=output_df_config['S8_MAP_2D'], bins=25, color=\"#2ca25f\", ax=g.ax_marg_y)\n", + "sns.histplot(\n", + " x=output_df_config[\"OMEGA_M_MAP_2D\"], bins=25, color=\"#2ca25f\", ax=g.ax_marg_x\n", + ")\n", + "sns.histplot(y=output_df_config[\"S8_MAP_2D\"], bins=25, color=\"#2ca25f\", ax=g.ax_marg_y)\n", "\n", "# Add dashed reference lines\n", - "g.ax_joint.axvline(Omega_m_fid, color='k', linestyle='--')\n", - "g.ax_joint.axhline(s8_fid, color='k', linestyle='--')\n", + "g.ax_joint.axvline(Omega_m_fid, color=\"k\", linestyle=\"--\")\n", + "g.ax_joint.axhline(s8_fid, color=\"k\", linestyle=\"--\")\n", "\n", "# Labels\n", "g.set_axis_labels(\n", - " r'$\\Omega_m$ estimated from mocks (Configuration space)',\n", - " r'$S_8$ estimated from mocks (Configuration space)'\n", + " r\"$\\Omega_m$ estimated from mocks (Configuration space)\",\n", + " r\"$S_8$ estimated from mocks (Configuration space)\",\n", ")\n", "\n", "# Optional styling tweaks\n", "g.ax_joint.tick_params(labelsize=12)\n", - "plt.savefig(\"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/S8_vs_OmegaM_configuration_space_mocks.pdf\", bbox_inches='tight')\n", + "plt.savefig(\n", + " \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/notebooks/Plots/S8_vs_OmegaM_configuration_space_mocks.pdf\",\n", + " bbox_inches=\"tight\",\n", + ")\n", "plt.show()" ] }, diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/masking.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/masking.ipynb index bb795760..b0dd4bbc 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/masking.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/masking.ipynb @@ -15,32 +15,30 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "import os\n", - "import numpy as np\n", + "\n", "import healpy as hp\n", - "from astropy.io import fits\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", "\n", - "plt.rcParams['axes.labelsize'] = 18\n", - "plt.rcParams['xtick.labelsize'] = 18\n", - "plt.rcParams['ytick.labelsize'] = 18\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", + "\n", + "plt.rcParams[\"axes.labelsize\"] = 18\n", + "plt.rcParams[\"xtick.labelsize\"] = 18\n", + "plt.rcParams[\"ytick.labelsize\"] = 18\n", "\n", - "plt.rcParams['text.usetex'] = True\n", + "plt.rcParams[\"text.usetex\"] = True\n", "sns.set_palette(\"husl\")\n", "\n", - "cat_dir = '/n17data/UNIONS/WL/v1.4.x/'\n", - "catalog_ver = 'v1.4.6.3'\n", - "blind = 'B'\n", + "cat_dir = \"/n17data/UNIONS/WL/v1.4.x/\"\n", + "catalog_ver = \"v1.4.6.3\"\n", + "blind = \"B\"\n", "\n", "nside = 8192\n", "npix = hp.nside2npix(nside)\n", "\n", - "data_dir = f'/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/data/'\n", + "data_dir = \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/data/\"\n", "curr_dir = os.getcwd()" ] }, @@ -51,40 +49,43 @@ "outputs": [], "source": [ "# PLOT 2D MAP OF COVMAT masked vs unmasked RATIOS\n", - "nbins=20\n", - "ndata = nbins * 2 \n", - "full_ratio = np.zeros((ndata,ndata))\n", + "nbins = 20\n", + "ndata = nbins * 2\n", + "full_ratio = np.zeros((ndata, ndata))\n", "\n", - "cov = np.loadtxt(data_dir + f'/covs/cov_SP_{catalog_ver}_{blind}.txt')\n", - "cov_masked = np.loadtxt(data_dir + f'/covs/cov_masked_SP_{catalog_ver}_{blind}.txt')\n", + "cov = np.loadtxt(data_dir + f\"/covs/cov_SP_{catalog_ver}_{blind}.txt\")\n", + "cov_masked = np.loadtxt(data_dir + f\"/covs/cov_masked_SP_{catalog_ver}_{blind}.txt\")\n", "\n", "for i in range(ndata):\n", " for j in range(ndata):\n", " full_ratio[i][j] = cov_masked[i][j] / cov[i][j]\n", "\n", "fig = plt.figure()\n", - "ax = fig.add_subplot(1, 1, 1) \n", + "ax = fig.add_subplot(1, 1, 1)\n", "extent = (0, ndata, ndata, 0)\n", "\n", "vmin, vmax = np.percentile(full_ratio, [1, 99])\n", "\n", - "im3 = ax.imshow(full_ratio, cmap='RdBu_r', vmin=vmin, vmax=vmax, extent=extent)\n", + "im3 = ax.imshow(full_ratio, cmap=\"RdBu_r\", vmin=vmin, vmax=vmax, extent=extent)\n", "\n", "cbar = fig.colorbar(im3, ax=ax, fraction=0.046, pad=0.04)\n", "\n", - "ax.text(int(ndata/4), ndata+5, r'$\\xi_+$', fontsize=15)\n", - "ax.text(3*int(ndata/4), ndata+5, r'$\\xi_-$', fontsize=15)\n", - "ax.text(-8, int(ndata/4), r'$\\xi_+$', fontsize=15, rotation=90)\n", - "ax.text(-8, 3*int(ndata/4), r'$\\xi_-$', fontsize=15,rotation=90)\n", - "ax.set_xticks([0,10,20,30,40])\n", - "ax.set_yticks([0,10,20,30,40])\n", + "ax.text(int(ndata / 4), ndata + 5, r\"$\\xi_+$\", fontsize=15)\n", + "ax.text(3 * int(ndata / 4), ndata + 5, r\"$\\xi_-$\", fontsize=15)\n", + "ax.text(-8, int(ndata / 4), r\"$\\xi_+$\", fontsize=15, rotation=90)\n", + "ax.text(-8, 3 * int(ndata / 4), r\"$\\xi_-$\", fontsize=15, rotation=90)\n", + "ax.set_xticks([0, 10, 20, 30, 40])\n", + "ax.set_yticks([0, 10, 20, 30, 40])\n", "ax.set_yticklabels([\"1'\", \"125'\", \"250'\", \"125'\", \"250'\"])\n", "ax.set_xticklabels([\"1'\", \"125'\", \"250'\", \"125'\", \"250'\"])\n", - "plt.axvline(x=int(ndata/2), color='white', linewidth=1.0)\n", - "plt.axhline(y=int(ndata/2), color='white', linewidth=1.0)\n", + "plt.axvline(x=int(ndata / 2), color=\"white\", linewidth=1.0)\n", + "plt.axhline(y=int(ndata / 2), color=\"white\", linewidth=1.0)\n", "\n", - "plt.savefig(f'{curr_dir}/../Plots/covmat_masked_unmasked_ratio_{catalog_ver}_{blind}.pdf', bbox_inches='tight')\n", - "plt.show()\n" + "plt.savefig(\n", + " f\"{curr_dir}/../Plots/covmat_masked_unmasked_ratio_{catalog_ver}_{blind}.pdf\",\n", + " bbox_inches=\"tight\",\n", + ")\n", + "plt.show()" ] }, { @@ -93,15 +94,17 @@ "metadata": {}, "outputs": [], "source": [ - "theta = np.linspace(1,250,20)\n", - "plt.axhline(y=1, color='k', ls='--')\n", - "plt.plot(theta,np.diag(cov_masked)[:20]/np.diag(cov)[:20], label=r'$\\xi_+$')\n", - "plt.plot(theta,np.diag(cov_masked)[20:]/np.diag(cov)[20:], label=r'$\\xi_-$')\n", + "theta = np.linspace(1, 250, 20)\n", + "plt.axhline(y=1, color=\"k\", ls=\"--\")\n", + "plt.plot(theta, np.diag(cov_masked)[:20] / np.diag(cov)[:20], label=r\"$\\xi_+$\")\n", + "plt.plot(theta, np.diag(cov_masked)[20:] / np.diag(cov)[20:], label=r\"$\\xi_-$\")\n", "\n", - "plt.xlabel(r'$\\theta$ (arcmin)')\n", - "plt.ylabel('Cov masked / Cov unmasked')\n", + "plt.xlabel(r\"$\\theta$ (arcmin)\")\n", + "plt.ylabel(\"Cov masked / Cov unmasked\")\n", "plt.legend(fontsize=20)\n", - "plt.savefig(f'{curr_dir}/../Plots/covmat_masked_unmasked_ratio_diag.pdf', bbox_inches='tight')" + "plt.savefig(\n", + " f\"{curr_dir}/../Plots/covmat_masked_unmasked_ratio_diag.pdf\", bbox_inches=\"tight\"\n", + ")" ] } ], diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/nonlin_k_analysis.ipynb b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/nonlin_k_analysis.ipynb index 5c047ec4..4ab9c3c5 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/nonlin_k_analysis.ipynb +++ b/cosmo_inference/notebooks/2D_cosmic_shear_configuration_plots/nonlin_k_analysis.ipynb @@ -15,33 +15,35 @@ "metadata": {}, "outputs": [], "source": [ - "import numpy as np\n", "import os\n", + "\n", "import matplotlib.pylab as plt\n", + "import numpy as np\n", "import seaborn as sns\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", "\n", - "plt.rcParams['text.usetex'] = True\n", - "\n", - "plt.rcParams.update({\n", - " \"font.size\": 20, \n", - " \"axes.titlesize\": 21,\n", - " \"axes.labelsize\": 20,\n", - " \"xtick.labelsize\": 20,\n", - " \"ytick.labelsize\": 20,\n", - " \"legend.fontsize\": 20,\n", - " \"figure.titlesize\": 21,\n", - "})\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", + "\n", + "plt.rcParams[\"text.usetex\"] = True\n", + "\n", + "plt.rcParams.update(\n", + " {\n", + " \"font.size\": 20,\n", + " \"axes.titlesize\": 21,\n", + " \"axes.labelsize\": 20,\n", + " \"xtick.labelsize\": 20,\n", + " \"ytick.labelsize\": 20,\n", + " \"legend.fontsize\": 20,\n", + " \"figure.titlesize\": 21,\n", + " }\n", + ")\n", "sns.set_palette(\"husl\")\n", "\n", - "blind = 'B'\n", - "ver = 'v1.4.6.3'\n", + "blind = \"B\"\n", + "ver = \"v1.4.6.3\"\n", "\n", "%matplotlib inline\n", "\n", - "data_dir = f'/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/data/'\n", + "data_dir = \"/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/data/\"\n", "curr_dir = os.getcwd()" ] }, @@ -60,15 +62,14 @@ "source": [ "# Read the 2D array from the text file\n", "\n", - "file_headers = [ \"xip_%s_%s\" % (ver, blind) , \"xim_%s_%s\" % (ver, blind) ]\n", + "file_headers = [\"xip_%s_%s\" % (ver, blind), \"xim_%s_%s\" % (ver, blind)]\n", "\n", "for f in file_headers:\n", - " \n", - " xis = np.loadtxt(data_dir + f'theta_k_{f}.txt')\n", - " xis_reshaped = xis.reshape(-1,201)\n", + " xis = np.loadtxt(data_dir + f\"theta_k_{f}.txt\")\n", + " xis_reshaped = xis.reshape(-1, 201)\n", " sorted_xis = xis_reshaped[np.argsort(xis_reshaped[:, 0])]\n", - " \n", - " np.savetxt(data_dir + f'theta_k_{f}_sorted.txt', sorted_xis)" + "\n", + " np.savetxt(data_dir + f\"theta_k_{f}_sorted.txt\", sorted_xis)" ] }, { @@ -77,7 +78,7 @@ "metadata": {}, "outputs": [], "source": [ - "fig, axs = plt.subplots(2, 1, figsize=(8,10))\n", + "fig, axs = plt.subplots(2, 1, figsize=(8, 10))\n", "\n", "# --- k grid ---\n", "h = 0.6766\n", @@ -85,50 +86,50 @@ "\n", "file_header = \"%s_%s\" % (ver, blind)\n", "\n", - "xi_thetas = np.loadtxt(data_dir + f'theta_k_xip_{file_header}_sorted.txt')\n", - "thetas = xi_thetas[:,0]\n", - "xis = xi_thetas[:,1:]\n", + "xi_thetas = np.loadtxt(data_dir + f\"theta_k_xip_{file_header}_sorted.txt\")\n", + "thetas = xi_thetas[:, 0]\n", + "xis = xi_thetas[:, 1:]\n", "\n", "# normalise\n", "xi_plot = xis / np.max(xis, axis=1, keepdims=True)\n", "\n", "T, K = np.meshgrid(thetas, k_plot)\n", "\n", - "axs[0].contour(T, K, xi_plot.T, levels=[0.9], colors='red', linewidths=1.7)\n", - "pcm = axs[0].pcolormesh(T, K, xi_plot.T, shading='auto', cmap='viridis')\n", + "axs[0].contour(T, K, xi_plot.T, levels=[0.9], colors=\"red\", linewidths=1.7)\n", + "pcm = axs[0].pcolormesh(T, K, xi_plot.T, shading=\"auto\", cmap=\"viridis\")\n", "pcm.set_rasterized(True)\n", "\n", - "axs[0].axvline(5, color='k', ls='dashed',lw=1.2)\n", - "axs[0].axvline(12, color='white', ls='dashed',lw=1.6)\n", - "axs[0].axhline(1, color='k', ls='dashed',lw=1.2) # converted to h/Mpc space if needed\n", - "axs[0].axhline(0.425, color='white', ls='dashed',lw=1.6)\n", + "axs[0].axvline(5, color=\"k\", ls=\"dashed\", lw=1.2)\n", + "axs[0].axvline(12, color=\"white\", ls=\"dashed\", lw=1.6)\n", + "axs[0].axhline(1, color=\"k\", ls=\"dashed\", lw=1.2) # converted to h/Mpc space if needed\n", + "axs[0].axhline(0.425, color=\"white\", ls=\"dashed\", lw=1.6)\n", "\n", - "axs[0].set_yscale('log')\n", - "axs[0].set_xlabel(r'$\\theta\\ \\mathrm{(arcmin)}$')\n", - "axs[0].set_ylabel(r'$k\\ (h$ Mpc$^{-1})$')\n", + "axs[0].set_yscale(\"log\")\n", + "axs[0].set_xlabel(r\"$\\theta\\ \\mathrm{(arcmin)}$\")\n", + "axs[0].set_ylabel(r\"$k\\ (h$ Mpc$^{-1})$\")\n", "\n", - "axs[0].set_title(r'$\\xi_+$')\n", + "axs[0].set_title(r\"$\\xi_+$\")\n", "\n", - "xi_thetas = np.loadtxt(data_dir + f'theta_k_xim_{file_header}_sorted.txt')\n", - "thetas = xi_thetas[:,0]\n", - "xis = xi_thetas[:,1:]\n", + "xi_thetas = np.loadtxt(data_dir + f\"theta_k_xim_{file_header}_sorted.txt\")\n", + "thetas = xi_thetas[:, 0]\n", + "xis = xi_thetas[:, 1:]\n", "\n", "xi_plot = xis / np.max(xis, axis=1, keepdims=True)\n", "\n", "T, K = np.meshgrid(thetas, k_plot)\n", "\n", - "axs[1].contour(T, K, xi_plot.T, levels=[0.9], colors='red', linewidths=1.7)\n", - "pcm = axs[1].pcolormesh(T, K, xi_plot.T, shading='nearest', cmap='viridis')\n", + "axs[1].contour(T, K, xi_plot.T, levels=[0.9], colors=\"red\", linewidths=1.7)\n", + "pcm = axs[1].pcolormesh(T, K, xi_plot.T, shading=\"nearest\", cmap=\"viridis\")\n", "pcm.set_rasterized(True)\n", "\n", - "axs[1].axvline(12, color='white', ls='dashed', lw=1.6)\n", - "axs[1].axhline(2.85, color='white', ls='dashed', lw=1.6)\n", + "axs[1].axvline(12, color=\"white\", ls=\"dashed\", lw=1.6)\n", + "axs[1].axhline(2.85, color=\"white\", ls=\"dashed\", lw=1.6)\n", "\n", "\n", - "axs[1].set_yscale('log')\n", - "axs[1].set_xlabel(r'$\\theta\\ \\mathrm{(arcmin)}$')\n", - "axs[1].set_ylabel(r'$k\\ (h$ Mpc$^{-1})$')\n", - "axs[1].set_title(r'$\\xi_-$')\n", + "axs[1].set_yscale(\"log\")\n", + "axs[1].set_xlabel(r\"$\\theta\\ \\mathrm{(arcmin)}$\")\n", + "axs[1].set_ylabel(r\"$k\\ (h$ Mpc$^{-1})$\")\n", + "axs[1].set_title(r\"$\\xi_-$\")\n", "\n", "\n", "fig.tight_layout()\n", @@ -136,7 +137,9 @@ "cbar_ax = fig.add_axes([0.99, 0.15, 0.02, 0.7])\n", "cbar = fig.colorbar(pcm, cax=cbar_ax)\n", "\n", - "fig.savefig(curr_dir + f'/../Plots/theta_k_xip_xim_{ver}_{blind}.pdf', bbox_inches='tight')" + "fig.savefig(\n", + " curr_dir + f\"/../Plots/theta_k_xip_xim_{ver}_{blind}.pdf\", bbox_inches=\"tight\"\n", + ")" ] }, { diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/unblinding_party_plots.py b/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/unblinding_party_plots.py index d69a4465..13af7ed2 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/unblinding_party_plots.py +++ b/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/unblinding_party_plots.py @@ -1,35 +1,28 @@ # %% import os -import configparser -import subprocess import sys import warnings # Append any useful folder in the path -sys.path.append( - "/home/guerrini/sp_validation/cosmo_inference/scripts/" -) +sys.path.append("/home/guerrini/sp_validation/cosmo_inference/scripts/") sys.path.append( "/home/guerrini/sp_validation/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/" ) -from getdist import plots, loadMCSamples -from astropy.io import fits -import numpy as np import matplotlib.pyplot as plt -from scipy.interpolate import interp1d -import scipy.stats as stats -from IPython.display import Markdown, display -import healpy as hp import matplotlib.scale as mscale -import matplotlib.ticker as ticker -import matplotlib.transforms as mtransforms +import numpy as np import seaborn as sns +from astropy.io import fits +from getdist import plots +from IPython.display import Markdown, display from sp_validation.rho_tau import SquareRootScale + mscale.register_scale(SquareRootScale) import IPython + ipython = IPython.get_ipython() if ipython is not None: @@ -51,8 +44,8 @@ sns.set_palette("husl") g = plots.get_subplot_plotter(width_inch=30) -g.settings.axes_fontsize=30 -g.settings.axes_labelsize=30 +g.settings.axes_fontsize = 30 +g.settings.axes_labelsize = 30 g.settings.alpha_filled_add = 0.7 g.settings.legend_fontsize = 40 @@ -60,7 +53,7 @@ root_dir = "/n09data/guerrini/output_chains" # THE BLIND TO USE FOR THE PLOTS -blind = "B" # Options are "A", "B", or "C" +blind = "B" # Options are "A", "B", or "C" catalog_version = "SP_v1.4.6.3" fiducial_root_cell = f"SP_v1.4.6.3_leak_corr_{blind}" label_fiducial_cell = r"UNIONS $C_{\ell}$" @@ -69,34 +62,25 @@ label_fiducial_xi = r"UNIONS $\xi_{\pm}$" # Path to the ini files used -path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config' -path_datavectors = '/home/guerrini/sp_validation/cosmo_inference/data/' +path_ini_files = "/home/guerrini/sp_validation/cosmo_inference/cosmosis_config" +path_datavectors = "/home/guerrini/sp_validation/cosmo_inference/data/" path_output_chains = "/n09data/guerrini/output_chains/" # %% # 0. Do a funny print with emojis for the unblinding -display( - Markdown( - "## 🎉 Let the Unblinding Party begin 🎉" - ) -) +display(Markdown("## 🎉 Let the Unblinding Party begin 🎉")) # %% # 1. Plot the datavectors without best-fit -display( - Markdown( - "### 1.a. Plot the datavectors without best-fit" - ) -) +display(Markdown("### 1.a. Plot the datavectors without best-fit")) # Plot Cells EE data = fits.open( os.path.join( - path_datavectors, - f"{fiducial_root_cell}/cosmosis_{fiducial_root_cell}.fits" + path_datavectors, f"{fiducial_root_cell}/cosmosis_{fiducial_root_cell}.fits" ) ) -cell_ee = data['CELL_EE'].data -cov_mat = data['COVMAT'].data +cell_ee = data["CELL_EE"].data +cov_mat = data["COVMAT"].data # Plot hyperparameter loc_legend = "lower center" @@ -104,50 +88,83 @@ fig, ax = plt.subplots(1, 1, figsize=(8, 5)) -ell, cell = cell_ee['ANG'], cell_ee['VALUE'] -ax.errorbar(ell, ell*cell, yerr=ell*np.sqrt(np.diag(cov_mat)), fmt='o', label=r"UNIONS $C_{\ell}$ data", color='black', capsize=2) +ell, cell = cell_ee["ANG"], cell_ee["VALUE"] +ax.errorbar( + ell, + ell * cell, + yerr=ell * np.sqrt(np.diag(cov_mat)), + fmt="o", + label=r"UNIONS $C_{\ell}$ data", + color="black", + capsize=2, +) # Plot the scale cuts for different k_max -ax.axvline(x=1800, color='black', linestyle='--', alpha=0.5) -ax.axvline(x=2048, color='black', linestyle='--', alpha=1.0) -ax.axvline(x=500, color='black', linestyle='--', alpha=0.3) +ax.axvline(x=1800, color="black", linestyle="--", alpha=0.5) +ax.axvline(x=2048, color="black", linestyle="--", alpha=1.0) +ax.axvline(x=500, color="black", linestyle="--", alpha=0.3) ymin = ax.get_ylim()[0] ymax = ax.get_ylim()[1] # Shadowing cut scaled -ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=300, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') -ax.fill_betweenx(y=[ymin, ymax], x1=1600, x2=2048, color='gray', alpha=0.2) +ax.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=300, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", +) +ax.fill_betweenx(y=[ymin, ymax], x1=1600, x2=2048, color="gray", alpha=0.2) ax.set_ylim(ymin, ymax) # Add labels directly under the tick -ax.text(1740, 0.90, - r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=10, rotation=90) - -ax.text(1978, 0.90, - r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=10, rotation=90) - -ax.text(470, 0.90, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=10, rotation=90) - -ell, cell = cell_ee['ANG'], cell_ee['VALUE'] -ax.set_ylabel('$\ell C_\ell$', fontsize=16) -ax.set_xlabel('$\ell$', fontsize=16) -ax.set_xlim(ell.min()-10, ell.max()+100) -ax.set_xscale('squareroot') +ax.text( + 1740, + 0.90, + r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=10, + rotation=90, +) + +ax.text( + 1978, + 0.90, + r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=10, + rotation=90, +) + +ax.text( + 470, + 0.90, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=10, + rotation=90, +) + +ell, cell = cell_ee["ANG"], cell_ee["VALUE"] +ax.set_ylabel(r"$\ell C_\ell$", fontsize=16) +ax.set_xlabel(r"$\ell$", fontsize=16) +ax.set_xlim(ell.min() - 10, ell.max() + 100) +ax.set_xscale("squareroot") ax.set_xticks(np.array([100, 400, 900, 1600])) ax.minorticks_on() ax.tick_params(axis="x", which="minor", length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax.xaxis.set_ticks(minor_ticks, minor=True) -ax.tick_params(axis='both', which='major', labelsize=14) -ax.tick_params(axis='both', which='minor', labelsize=10) +ax.tick_params(axis="both", which="major", labelsize=14) +ax.tick_params(axis="both", which="minor", labelsize=10) ax.yaxis.get_offset_text().set_fontsize(14) plt.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor, fontsize=12) @@ -156,120 +173,161 @@ # Plots xi_+ and xi_- -#TODO: add the plot for xi_+ and xi_- -display( - Markdown( - r"### 1.b. Plot the datavectors without best-fit ($\xi_\pm$)" - ) -) +# TODO: add the plot for xi_+ and xi_- +display(Markdown(r"### 1.b. Plot the datavectors without best-fit ($\xi_\pm$)")) # Plot xi_pm's data = fits.open( os.path.join( path_datavectors, - f"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits" + f"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits", ) ) -xi_p_data = data['XI_PLUS'].data -xi_m_data = data['XI_MINUS'].data -cov_mat = data['COVMAT'].data +xi_p_data = data["XI_PLUS"].data +xi_m_data = data["XI_MINUS"].data +cov_mat = data["COVMAT"].data # Plot hyperparameter loc_legend = "lower center" bbox_to_anchor_xip = (0.685, 0.03) bbox_to_anchor_xim = (0.3, 0.65) -fig, [ax,ax2] = plt.subplots(2, 1, figsize=(8, 9)) - -theta, xi_p = xi_p_data['ANG'], xi_p_data['VALUE'] -ax.errorbar(theta, theta*xi_p, yerr=theta*np.sqrt(np.diag(cov_mat[:len(theta),:len(theta)])), fmt='o', label=r"UNIONS $\xi_+$ data", color='black', capsize=2) +fig, [ax, ax2] = plt.subplots(2, 1, figsize=(8, 9)) + +theta, xi_p = xi_p_data["ANG"], xi_p_data["VALUE"] +ax.errorbar( + theta, + theta * xi_p, + yerr=theta * np.sqrt(np.diag(cov_mat[: len(theta), : len(theta)])), + fmt="o", + label=r"UNIONS $\xi_+$ data", + color="black", + capsize=2, +) # Plot the scale cuts for different k_max -ax.axvline(x=3.2, color='black', linestyle='--', alpha=0.3) +ax.axvline(x=3.2, color="black", linestyle="--", alpha=0.3) ymin = ax.get_ylim()[0] ymax = ax.get_ylim()[1] # Shadowing cut scaled -ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') -ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2) +ax.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=12, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", +) +ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color="gray", alpha=0.2) ax.set_ylim(ymin, ymax) # Add labels directly under the tick -ax.text(3, 1e-4, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - # transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=10, rotation=90) +ax.text( + 3, + 1e-4, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + # transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=10, + rotation=90, +) -ax.set_ylabel(r'$\theta \xi_+$', fontsize=16) +ax.set_ylabel(r"$\theta \xi_+$", fontsize=16) # ax.set_xlabel('$\theta$', fontsize=16) # ax.set_xlim([theta.min()-0.1, theta.max()+20]) -ax.set_xscale('log') +ax.set_xscale("log") ax.set_xticks(np.array([1, 10, 100])) ax.tick_params(axis="x", which="minor", length=2, width=0.8) -ax.tick_params(axis='both', which='major', labelsize=14) -ax.tick_params(axis='both', which='minor', labelsize=10) +ax.tick_params(axis="both", which="major", labelsize=14) +ax.tick_params(axis="both", which="minor", labelsize=10) ax.yaxis.get_offset_text().set_fontsize(14) -ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) +ax.ticklabel_format(axis="y", style="sci", scilimits=(0, 0)) ax.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xip, fontsize=12) -theta, xi_m = xi_p_data['ANG'], xi_m_data['VALUE'] -ax2.errorbar(theta, theta*xi_m, yerr=theta*np.sqrt(np.diag(cov_mat[len(theta):2*len(theta),len(theta):2*len(theta)])), fmt='o', label=r"UNIONS $\xi_-$ data", color='black', capsize=2) +theta, xi_m = xi_p_data["ANG"], xi_m_data["VALUE"] +ax2.errorbar( + theta, + theta * xi_m, + yerr=theta + * np.sqrt( + np.diag(cov_mat[len(theta) : 2 * len(theta), len(theta) : 2 * len(theta)]) + ), + fmt="o", + label=r"UNIONS $\xi_-$ data", + color="black", + capsize=2, +) # Plot the scale cuts for different k_max -ax2.axvline(x=24, color='black', linestyle='--', alpha=0.3) +ax2.axvline(x=24, color="black", linestyle="--", alpha=0.3) ymin = ax2.get_ylim()[0] ymax = ax2.get_ylim()[1] # Shadowing cut scaled -ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') -ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2) +ax2.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=12, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", +) +ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color="gray", alpha=0.2) ax2.set_ylim(ymin, ymax) # Add labels directly under the tick -ax2.text(22.3, 9e-5, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - # transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=10, rotation=90) - -ax2.set_ylabel(r'$\theta÷ \xi_-$', fontsize=16) -ax2.set_xlabel('$\theta$', fontsize=16) -ax2.set_xlim([theta.min()-0.1, theta.max()+20]) -ax2.set_xscale('log') +ax2.text( + 22.3, + 9e-5, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + # transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=10, + rotation=90, +) + +ax2.set_ylabel(r"$\theta÷ \xi_-$", fontsize=16) +ax2.set_xlabel("$\theta$", fontsize=16) +ax2.set_xlim([theta.min() - 0.1, theta.max() + 20]) +ax2.set_xscale("log") ax2.set_xticks(np.array([1, 10, 100])) ax2.tick_params(axis="x", which="minor", length=2, width=0.8) -ax2.tick_params(axis='both', which='major', labelsize=14) -ax2.tick_params(axis='both', which='minor', labelsize=10) +ax2.tick_params(axis="both", which="major", labelsize=14) +ax2.tick_params(axis="both", which="minor", labelsize=10) ax2.yaxis.get_offset_text().set_fontsize(14) -ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) +ax2.ticklabel_format(axis="y", style="sci", scilimits=(0, 0)) ax2.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xim, fontsize=12) plt.show() # %% # 2. Plot the best-fit datavectors -display( - Markdown( - "### 2. Plot the best-fit datavectors" - ) -) +display(Markdown("### 2. Plot the best-fit datavectors")) # Perform the computation for the fiducial of Cell path_samples_fiducial_cell = os.path.join( path_output_chains, fiducial_root_cell, fiducial_root_cell, - f"samples_{fiducial_root_cell}_cell.txt" + f"samples_{fiducial_root_cell}_cell.txt", ) path_gd_fiducial_cell = os.path.join( path_output_chains, fiducial_root_cell, fiducial_root_cell, - f"getdist_{fiducial_root_cell}_cell" + f"getdist_{fiducial_root_cell}_cell", +) +cp.load_samples_and_write_paramnames( + path_samples_fiducial_cell, path_gd_fiducial_cell + ".paramnames" +) +cp.write_samples_getdist_format( + path_samples_fiducial_cell, path_gd_fiducial_cell + ".txt", chain_type="polychord" ) -cp.load_samples_and_write_paramnames(path_samples_fiducial_cell, path_gd_fiducial_cell+".paramnames") -cp.write_samples_getdist_format(path_samples_fiducial_cell, path_gd_fiducial_cell+".txt", chain_type='polychord') chain_fiducial_cell = cp.load_chain(path_gd_fiducial_cell, smoothing_scale=0.3) @@ -280,22 +338,24 @@ best_fit_params_fiducial_cell, fiducial_root_cell, is_harmonic=True, - blind=blind + blind=blind, ) # Perform the computation for the fiducial of xi path_samples_fiducial_xi = os.path.join( path_output_chains, fiducial_root_xi_chains, - f"samples_{fiducial_root_xi_chains}.txt" + f"samples_{fiducial_root_xi_chains}.txt", ) path_gd_fiducial_xi = os.path.join( - path_output_chains, - fiducial_root_xi_chains, - f"getdist_{fiducial_root_xi_chains}" + path_output_chains, fiducial_root_xi_chains, f"getdist_{fiducial_root_xi_chains}" +) +cp.load_samples_and_write_paramnames( + path_samples_fiducial_xi, path_gd_fiducial_xi + ".paramnames" +) +cp.write_samples_getdist_format( + path_samples_fiducial_xi, path_gd_fiducial_xi + ".txt", chain_type="polychord" ) -cp.load_samples_and_write_paramnames(path_samples_fiducial_xi, path_gd_fiducial_xi+".paramnames") -cp.write_samples_getdist_format(path_samples_fiducial_xi, path_gd_fiducial_xi+".txt", chain_type='polychord') chain_fiducial_xi = cp.load_chain(path_gd_fiducial_xi, smoothing_scale=0.3) @@ -303,7 +363,7 @@ ini_file_root = os.path.join( path_ini_files, - f'config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini' + f"config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini", ) cp.compute_best_fit( path_ini_files, @@ -311,63 +371,80 @@ fiducial_root_xi_chains, is_harmonic=False, blind=blind, - ini_file_root=ini_file_root + ini_file_root=ini_file_root, ) # %% # Make the plot for the best-fit datavector for Cell EE -root_to_plot = [ - fiducial_root_cell, - fiducial_root_xi_chains -] +root_to_plot = [fiducial_root_cell, fiducial_root_xi_chains] -labels = [ - r"UNIONS $C_\ell$", - r"UNIONS $\xi_\pm(\vartheta)$" -] +labels = [r"UNIONS $C_\ell$", r"UNIONS $\xi_\pm(\vartheta)$"] line_args = [ - {'color': 'royalblue', 'linestyle': '-'}, - {'color': 'orange', 'linestyle': '-'} + {"color": "royalblue", "linestyle": "-"}, + {"color": "orange", "linestyle": "-"}, ] properties = {} -properties = utils.update_properties_w_roots(properties, fiducial_root_cell, path_ini_files, with_configuration=False) -properties = utils.update_properties_w_roots(properties, fiducial_root_xi_chains, path_ini_files, with_configuration=True, path_to_this_ini=ini_file_root) +properties = utils.update_properties_w_roots( + properties, fiducial_root_cell, path_ini_files, with_configuration=False +) +properties = utils.update_properties_w_roots( + properties, + fiducial_root_xi_chains, + path_ini_files, + with_configuration=True, + path_to_this_ini=ini_file_root, +) -utils.plot_best_fit(fiducial_root_cell, root_to_plot, path_output_chains, line_args, savefile=None, labels=labels, loc_legend=loc_legend, bbox_to_anchor=bbox_to_anchor, properties=properties) +utils.plot_best_fit( + fiducial_root_cell, + root_to_plot, + path_output_chains, + line_args, + savefile=None, + labels=labels, + loc_legend=loc_legend, + bbox_to_anchor=bbox_to_anchor, + properties=properties, +) # TODO: add the plot for xi # %% # Plot best-fit xi_+ and xi_- (also from C_ell's) path_best_fit_xi_theta = os.path.join( - path_output_chains, - fiducial_root_xi_chains, - "best_fit/shear_xi_plus/" - f"theta.txt" + path_output_chains, fiducial_root_xi_chains, "best_fit/shear_xi_plus/theta.txt" ) theta_rad = np.loadtxt(path_best_fit_xi_theta) -cp.compute_best_fit_xi_from_cell(path_output_chains, fiducial_root_cell, best_fit_params_fiducial_cell, theta_rad) +cp.compute_best_fit_xi_from_cell( + path_output_chains, fiducial_root_cell, best_fit_params_fiducial_cell, theta_rad +) -xi_data_path = os.path.join(path_datavectors,f"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits") -utils.plot_best_fit_config(xi_data_path, root_to_plot, path_output_chains, line_args, savefile=None, labels=labels, loc_legend=loc_legend, bbox_to_anchor_xip=bbox_to_anchor_xip, bbox_to_anchor_xim=bbox_to_anchor_xim, properties=properties) +xi_data_path = os.path.join( + path_datavectors, + f"SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_{fiducial_root_xi_data}.fits", +) +utils.plot_best_fit_config( + xi_data_path, + root_to_plot, + path_output_chains, + line_args, + savefile=None, + labels=labels, + loc_legend=loc_legend, + bbox_to_anchor_xip=bbox_to_anchor_xip, + bbox_to_anchor_xim=bbox_to_anchor_xim, + properties=properties, +) # %% # 3. Do a whisker plot with external experiments and our constraints -display( - Markdown( - "### 🥸 Time to look at the whisker plot 🥸" - ) -) -colour_blind = { - "A": "royalblue", - "B": "crimson", - "C": "forestgreen" -} +display(Markdown("### 🥸 Time to look at the whisker plot 🥸")) +colour_blind = {"A": "royalblue", "B": "crimson", "C": "forestgreen"} roots = [ f"SP_v1.4.6.3_leak_corr_{blind}", @@ -422,89 +499,83 @@ "external_compute_sample", "external", "external", - "external_compute_sample" - + "external_compute_sample", ] for bl in ["A", "B", "C"]: if bl != blind: - roots.append( - f"SP_v1.4.6.3_leak_corr_{bl}" - ) - roots.append( - f"SP_v1.4.6.3_{bl}_fiducial_config" - ) - legend_labels.append( - fr"UNIONS $C_\ell$, Blind {bl}" - ) - legend_labels.append( - fr"UNIONS $\xi_\pm(\vartheta)$, Blind {bl}" - ) - colours.append( - colour_blind[bl] - ) - colours.append( - colour_blind[bl] - ) - categories.append( - "harmonic" - ) - categories.append( - "configuration" - ) + roots.append(f"SP_v1.4.6.3_leak_corr_{bl}") + roots.append(f"SP_v1.4.6.3_{bl}_fiducial_config") + legend_labels.append(rf"UNIONS $C_\ell$, Blind {bl}") + legend_labels.append(rf"UNIONS $\xi_\pm(\vartheta)$, Blind {bl}") + colours.append(colour_blind[bl]) + colours.append(colour_blind[bl]) + categories.append("harmonic") + categories.append("configuration") # Loop on all versions to load the chain chains = [] for i, root in enumerate(roots): category = categories[i] if category != "external": - if category == 'configuration': + if category == "configuration": path_samples = os.path.join( - path_output_chains, - f"{root}/samples_{root}.txt" - ) - path_getdist = os.path.join( - path_output_chains, - f"{root}/getdist_{root}" + path_output_chains, f"{root}/samples_{root}.txt" ) - elif category == 'harmonic': + path_getdist = os.path.join(path_output_chains, f"{root}/getdist_{root}") + elif category == "harmonic": path_samples = os.path.join( - path_output_chains, - f"{root}/{root}/samples_{root}_cell.txt" + path_output_chains, f"{root}/{root}/samples_{root}_cell.txt" ) path_getdist = os.path.join( - path_output_chains, - f"{root}/{root}/getdist_{root}" + path_output_chains, f"{root}/{root}/getdist_{root}" ) elif category == "external_compute_sample": path_samples = os.path.join( - path_output_chains, - f"ext_data/{root}/samples_{root}.txt" + path_output_chains, f"ext_data/{root}/samples_{root}.txt" ) path_getdist = os.path.join( - path_output_chains, - f"ext_data/{root}/getdist_{root}" + path_output_chains, f"ext_data/{root}/getdist_{root}" ) else: raise ValueError(f"The category, {category}, of {root} is not correct") - - cp.load_samples_and_write_paramnames(path_samples, path_getdist+".paramnames") - cp.write_samples_getdist_format(path_samples, path_getdist+".txt") - chains.append( - cp.load_chain(path_getdist, smoothing_scale=0.5) - ) + + cp.load_samples_and_write_paramnames(path_samples, path_getdist + ".paramnames") + cp.write_samples_getdist_format(path_samples, path_getdist + ".txt") + chains.append(cp.load_chain(path_getdist, smoothing_scale=0.5)) else: path_getdist = os.path.join( - path_output_chains, - f"ext_data/{root}/getdist_{root}" - ) - chains.append( - cp.load_chain(path_getdist) + path_output_chains, f"ext_data/{root}/getdist_{root}" ) + chains.append(cp.load_chain(path_getdist)) # Give labels for the chains -name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','S_8','s_8_input', 'logt_agn','a','m1','bias_1'] -label_list = [r'\Omega_{\rm m}', r'\omega_b h^2', r'h_0', r'n_s', r'\sigma_8', r'S_8', r'S_8', r'\log T_{\rm AGN}', r'A_{\rm IA}', r'm_1', r'\Delta z_1'] +name_list = [ + "OMEGA_M", + "ombh2", + "h0", + "n_s", + "SIGMA_8", + "S_8", + "s_8_input", + "logt_agn", + "a", + "m1", + "bias_1", +] +label_list = [ + r"\Omega_{\rm m}", + r"\omega_b h^2", + r"h_0", + r"n_s", + r"\sigma_8", + r"S_8", + r"S_8", + r"\log T_{\rm AGN}", + r"A_{\rm IA}", + r"m_1", + r"\Delta z_1", +] for i, chain in enumerate(chains): print(legend_labels[i]) @@ -512,74 +583,122 @@ for name, label in zip(name_list, label_list): try: param_names.parWithName(name).label = label - except: + except Exception: warnings.warn(f"Parameter {name} not found in chain {roots[i]}.") # Account for the missing parameter conventions -#OMEGA_M not in DES_Y3_cell -idx = roots.index('DES_Y3_cell') -cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\Omega_{\rm m}') +# OMEGA_M not in DES_Y3_cell +idx = roots.index("DES_Y3_cell") +cp.adjust_paramname_chain(chains[idx], "omega_m", "OMEGA_M", r"\Omega_{\rm m}") cp.derive_parameter_S8(chains[idx]) -#OMEGA_M not in KiDS-1000 -idx = roots.index('KiDS-1000') -cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\Omega_{\rm m}') +# OMEGA_M not in KiDS-1000 +idx = roots.index("KiDS-1000") +cp.adjust_paramname_chain(chains[idx], "omega_m", "OMEGA_M", r"\Omega_{\rm m}") -#OMEGA_M not in DES+KiDS -idx = roots.index('DES+KiDS') -cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\Omega_{\rm m}') +# OMEGA_M not in DES+KiDS +idx = roots.index("DES+KiDS") +cp.adjust_paramname_chain(chains[idx], "omega_m", "OMEGA_M", r"\Omega_{\rm m}") -#OMEGA_M not in HSC_Y3_cell -idx = roots.index('HSC_Y3_cell') -cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\Omega_{\rm m}') +# OMEGA_M not in HSC_Y3_cell +idx = roots.index("HSC_Y3_cell") +cp.adjust_paramname_chain(chains[idx], "omega_m", "OMEGA_M", r"\Omega_{\rm m}") # Build an array containing the parameter values -param_values = np.array(["# Expt", "Colour", "S8_Mean", "S8_low", "S8_high", "sigma_8_Mean", "sigma_8_low", "sigma_8_high", "Omega_m_Mean", "Omega_m_low", "Omega_m_high"]) -escaped = np.char.replace(legend_labels, '\\', '\\\\') +param_values = np.array( + [ + "# Expt", + "Colour", + "S8_Mean", + "S8_low", + "S8_high", + "sigma_8_Mean", + "sigma_8_low", + "sigma_8_high", + "Omega_m_Mean", + "Omega_m_low", + "Omega_m_high", + ] +) +escaped = np.char.replace(legend_labels, "\\", "\\\\") for i, chain in enumerate(chains): print(chain.root) margestats = chain.getMargeStats() likestats = chain.getLikeStats() - s8_stats = margestats.parWithName('S_8') - sigma8_stats = margestats.parWithName('SIGMA_8') - omegam_stats = margestats.parWithName('OMEGA_M') - - param_values = np.vstack(( - param_values, - [ - escaped[i], - colours[i], - s8_stats.mean, - s8_stats.mean-s8_stats.limits[0].lower, - s8_stats.limits[0].upper-s8_stats.mean, - sigma8_stats.mean, - sigma8_stats.mean - sigma8_stats.limits[0].lower, - sigma8_stats.limits[0].upper - sigma8_stats.mean, - omegam_stats.mean, - omegam_stats.mean - omegam_stats.limits[0].lower, - omegam_stats.limits[0].upper - omegam_stats.mean, - ] - )) + s8_stats = margestats.parWithName("S_8") + sigma8_stats = margestats.parWithName("SIGMA_8") + omegam_stats = margestats.parWithName("OMEGA_M") + + param_values = np.vstack( + ( + param_values, + [ + escaped[i], + colours[i], + s8_stats.mean, + s8_stats.mean - s8_stats.limits[0].lower, + s8_stats.limits[0].upper - s8_stats.mean, + sigma8_stats.mean, + sigma8_stats.mean - sigma8_stats.limits[0].lower, + sigma8_stats.limits[0].upper - sigma8_stats.mean, + omegam_stats.mean, + omegam_stats.mean - omegam_stats.limits[0].lower, + omegam_stats.limits[0].upper - omegam_stats.mean, + ], + ) + ) print(param_values) -np.savetxt(f"./param_values.txt", param_values, fmt=['%s' for i in range(11)], delimiter=';') +np.savetxt( + "./param_values.txt", param_values, fmt=["%s" for i in range(11)], delimiter=";" +) # Reload the table # Load the value of the parameters -cosmo = np.loadtxt(f"./param_values.txt", - dtype={'names': ('Expt', 'colour', 's8_mean', 's8_low', 's8_high', 'sigma8_mean', 'sigma8_low', 'sigma8_high', 'omegam_mean', 'omegam_low', 'omegam_high'), - 'formats': ('U250', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20')}, skiprows=1, delimiter=';') -expt = np.char.replace(cosmo['Expt'], '\\\\', '\\') -colours = cosmo['colour'] -s8_mean = cosmo['s8_mean'].astype(np.float64) -s8_low = cosmo['s8_low'].astype(np.float64) -s8_high = cosmo['s8_high'].astype(np.float64) -sigma8_mean = cosmo['sigma8_mean'].astype(np.float64) -sigma8_low = cosmo['sigma8_low'].astype(np.float64) -sigma8_high = cosmo['sigma8_high'].astype(np.float64) -omegam_mean = cosmo['omegam_mean'].astype(np.float64) -omegam_low = cosmo['omegam_low'].astype(np.float64) -omegam_high = cosmo['omegam_high'].astype(np.float64) +cosmo = np.loadtxt( + "./param_values.txt", + dtype={ + "names": ( + "Expt", + "colour", + "s8_mean", + "s8_low", + "s8_high", + "sigma8_mean", + "sigma8_low", + "sigma8_high", + "omegam_mean", + "omegam_low", + "omegam_high", + ), + "formats": ( + "U250", + "U20", + "U20", + "U20", + "U20", + "U20", + "U20", + "U20", + "U20", + "U20", + "U20", + ), + }, + skiprows=1, + delimiter=";", +) +expt = np.char.replace(cosmo["Expt"], "\\\\", "\\") +colours = cosmo["colour"] +s8_mean = cosmo["s8_mean"].astype(np.float64) +s8_low = cosmo["s8_low"].astype(np.float64) +s8_high = cosmo["s8_high"].astype(np.float64) +sigma8_mean = cosmo["sigma8_mean"].astype(np.float64) +sigma8_low = cosmo["sigma8_low"].astype(np.float64) +sigma8_high = cosmo["sigma8_high"].astype(np.float64) +omegam_mean = cosmo["omegam_mean"].astype(np.float64) +omegam_low = cosmo["omegam_low"].astype(np.float64) +omegam_high = cosmo["omegam_high"].astype(np.float64) # %% # Perform the plot @@ -598,19 +717,12 @@ (sigma8_mean, sigma8_low, sigma8_high, r"$\sigma_8$"), (omegam_mean, omegam_low, omegam_high, r"$\Omega_{\rm m}$"), ] -reference = "UNIONS $C_\ell$, unblind" +reference = r"UNIONS $C_\ell$, unblind" separation_after = [ r"UNIONS $\xi_\pm(\vartheta)$, unblind", r"HSC Y3 $C_\ell$", ] -list_section_index = [ - r"(ii)", - r"(iii)", - r"(iv)", - r"(v)", - r"(vi)", - r"(vii)" -] +list_section_index = [r"(ii)", r"(iii)", r"(iv)", r"(v)", r"(vi)", r"(vii)"] preliminary_watermark = False blind_axes = False @@ -622,67 +734,123 @@ for ax, param in zip(axs, params): means, lows, highs, label = param for i, mean, low, high, color in zip(y, means, lows, highs, colours): - ax.errorbar(mean, 0.05+i*row_spacing, xerr=np.array([low, high])[:, None], fmt='o', color=color, ecolor=color, elinewidth=2, capsize=3) + ax.errorbar( + mean, + 0.05 + i * row_spacing, + xerr=np.array([low, high])[:, None], + fmt="o", + color=color, + ecolor=color, + elinewidth=2, + capsize=3, + ) ax.set_xlabel(label, fontsize=14) - + ax.grid(False) - ax.tick_params(axis='y', left=False, labelleft=False) + ax.tick_params(axis="y", left=False, labelleft=False) if label == r"$S_8$": - ax.axvspan(s8_mean[index_ref] - s8_low[index_ref], s8_mean[index_ref] + s8_high[index_ref], color=colours[index_ref], alpha=0.2) + ax.axvspan( + s8_mean[index_ref] - s8_low[index_ref], + s8_mean[index_ref] + s8_high[index_ref], + color=colours[index_ref], + alpha=0.2, + ) ax.set_xlim(0.25, 1.1) if blind_axes: ref_tick = np.mean(s8_mean[:4]) - ax.set_xticks( - [ref_tick + i*0.1 for i in range(-5, 5)], labels=[] - ) + ax.set_xticks([ref_tick + i * 0.1 for i in range(-5, 5)], labels=[]) elif label == r"$\sigma_8$": - ax.axvspan(sigma8_mean[index_ref] - sigma8_low[index_ref], sigma8_mean[index_ref] + sigma8_high[index_ref], color=colours[index_ref], alpha=0.2) + ax.axvspan( + sigma8_mean[index_ref] - sigma8_low[index_ref], + sigma8_mean[index_ref] + sigma8_high[index_ref], + color=colours[index_ref], + alpha=0.2, + ) ax.set_xlim(0.5, 1.2) if blind_axes: ref_tick = np.mean(sigma8_mean[:4]) - ax.set_xticks( - [ref_tick + i*0.2 for i in range(-2, 2)], labels=[] - ) + ax.set_xticks([ref_tick + i * 0.2 for i in range(-2, 2)], labels=[]) elif label == r"$\Omega_{\rm m}$": - ax.axvspan(omegam_mean[index_ref] - omegam_low[index_ref], omegam_mean[index_ref] + omegam_high[index_ref], color=colours[index_ref], alpha=0.2) + ax.axvspan( + omegam_mean[index_ref] - omegam_low[index_ref], + omegam_mean[index_ref] + omegam_high[index_ref], + color=colours[index_ref], + alpha=0.2, + ) ax.set_xlim(0.1, 0.5) if blind_axes: ref_tick = np.mean(omegam_mean[:4]) - ax.set_xticks( - [ref_tick + i*0.1 for i in range(-2, 3)], labels=[] - ) + ax.set_xticks([ref_tick + i * 0.1 for i in range(-2, 3)], labels=[]) -axs[0].set_yticks(0.05+y*row_spacing) +axs[0].set_yticks(0.05 + y * row_spacing) axs[0].set_yticklabels([]) for label, color in zip(expt, colours): - axs[0].text(0.26, 0.05 + row_spacing * np.where(expt == label)[0][0], label, fontsize=12, ha='left', va='center', color=color) + axs[0].text( + 0.26, + 0.05 + row_spacing * np.where(expt == label)[0][0], + label, + fontsize=12, + ha="left", + va="center", + color=color, + ) if label != reference: index = np.where(expt == label)[0][0] s8_tension = cp.get_sigma_tension( - s8_mean[index], s8_low[index], s8_high[index], - s8_mean[index_ref], s8_low[index_ref], s8_high[index_ref] + s8_mean[index], + s8_low[index], + s8_high[index], + s8_mean[index_ref], + s8_low[index_ref], + s8_high[index_ref], ) sign_str = "+" if s8_tension > 0 else "-" - axs[0].text(1.095, 0.05 + row_spacing * index, rf"${sign_str}{np.abs(s8_tension):.2f}" + r"\, \sigma$", fontsize=10, ha='right', va='center', color=color) + axs[0].text( + 1.095, + 0.05 + row_spacing * index, + rf"${sign_str}{np.abs(s8_tension):.2f}" + r"\, \sigma$", + fontsize=10, + ha="right", + va="center", + color=color, + ) # Add separation lines for i, sep in enumerate(separation_after): index_sep = np.where(expt == sep)[0][0] for ax in axs: - ax.axhline(row_spacing * (index_sep + 1), color='black', linestyle='dotted', linewidth=1) - axs[0].text(0.25, 0.05 + row_spacing * (index_sep + 1), - list_section_index[i], fontsize=14, fontweight='bold', va='center', ha='right') + ax.axhline( + row_spacing * (index_sep + 1), + color="black", + linestyle="dotted", + linewidth=1, + ) + axs[0].text( + 0.25, + 0.05 + row_spacing * (index_sep + 1), + list_section_index[i], + fontsize=14, + fontweight="bold", + va="center", + ha="right", + ) # --- Add section labels (i), (ii)) --- -axs[0].text(0.25, 0.05, - r"(i)", fontsize=14, fontweight='bold', va='center', ha='right') +axs[0].text(0.25, 0.05, r"(i)", fontsize=14, fontweight="bold", va="center", ha="right") if preliminary_watermark: - plt.figtext(0.5, 0.5, 'PRELIMINARY', - fontsize=50, color='gray', - ha='center', va='center', - alpha=0.3, rotation=330) + plt.figtext( + 0.5, + 0.5, + "PRELIMINARY", + fontsize=50, + color="gray", + ha="center", + va="center", + alpha=0.3, + rotation=330, + ) plt.gca().invert_yaxis() @@ -692,53 +860,34 @@ # %% # 4. Make a contour plots -display( - Markdown( - "### Here comes $S_8$ and $\Omega_m$" - ) -) -colours = [ - "royalblue", - "orange", - "violet" -] +display(Markdown(r"### Here comes $S_8$ and $\Omega_m$")) +colours = ["royalblue", "orange", "violet"] -filled = [ - True, - True, - False, - False, - False, - False, - False, - False, - False, - False, - False -] +filled = [True, True, False, False, False, False, False, False, False, False, False] -line_args = [ - dict(color=col, ls='solid') for col in colours -] +line_args = [dict(color=col, ls="solid") for col in colours] g = plots.get_single_plotter(width_inch=30) -g.settings.axes_fontsize=60 -g.settings.axes_labelsize=60 +g.settings.axes_fontsize = 60 +g.settings.axes_labelsize = 60 g.settings.alpha_filled_add = 0.7 g.settings.legend_fontsize = 45 g.settings.figure_legend_ncol = 3 g.settings.legend_frame = False -g.plot_2d(chains[:-4], - 'OMEGA_M', 'S_8', - filled=filled, - line_args=line_args, - contour_colors=colours,) +g.plot_2d( + chains[:-4], + "OMEGA_M", + "S_8", + filled=filled, + line_args=line_args, + contour_colors=colours, +) g.add_legend( legend_labels[:-4], - legend_loc='upper center', - bbox_to_anchor=(0.5, 1.20), # moves legend above the axes + legend_loc="upper center", + bbox_to_anchor=(0.5, 1.20), # moves legend above the axes ) plt.show() diff --git a/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/utils.py b/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/utils.py index 6ac23f53..8c2fb9e5 100644 --- a/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/utils.py +++ b/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/utils.py @@ -1,32 +1,24 @@ """ Useful scripts to perform the plots for the unblinding party. """ -import os + import configparser -import subprocess +import os import sys # Append any useful folder in the path -sys.path.append( - "/home/guerrini/sp_validation/cosmo_inference/scripts/" -) +sys.path.append("/home/guerrini/sp_validation/cosmo_inference/scripts/") -from getdist import plots, loadMCSamples -from astropy.io import fits -import numpy as np import matplotlib.pyplot as plt -from scipy.interpolate import interp1d -import scipy.stats as stats -from IPython.display import Markdown, display -import healpy as hp import matplotlib.scale as mscale -import matplotlib.ticker as ticker -import matplotlib.transforms as mtransforms -import seaborn as sns +import numpy as np +from astropy.io import fits from sp_validation.rho_tau import SquareRootScale + mscale.register_scale(SquareRootScale) + def read_config(path_ini_files, root, thisfile=None): config = configparser.ConfigParser() config.optionxform = str @@ -37,7 +29,10 @@ def read_config(path_ini_files, root, thisfile=None): config.read(read_path) return config -def update_properties_w_roots(properties, root, path_ini_files, path_to_this_ini=None, with_configuration=False): + +def update_properties_w_roots( + properties, root, path_ini_files, path_to_this_ini=None, with_configuration=False +): config = read_config(path_ini_files, root, thisfile=path_to_this_ini) try: @@ -46,219 +41,402 @@ def update_properties_w_roots(properties, root, path_ini_files, path_to_this_ini ) properties[root].update( { - 'lower_bound_cell_ee': lower_bound_cell_ee, - 'upper_bound_cell_ee': upper_bound_cell_ee + "lower_bound_cell_ee": lower_bound_cell_ee, + "upper_bound_cell_ee": upper_bound_cell_ee, } ) except KeyError: - properties[root] = { - 'lower_bound_cell_ee': 0.0, - 'upper_bound_cell_ee': 2048 - } + properties[root] = {"lower_bound_cell_ee": 0.0, "upper_bound_cell_ee": 2048} if with_configuration: # Also save the scale cuts in theta for xi add_xi_sys = config["2pt_like"]["add_xi_sys"] - add_xi_sys = add_xi_sys == 'T' - lower_bound_xi_plus, upper_bound_xi_plus = map(float, config["2pt_like"]["angle_range_XI_PLUS_1_1"].split()) - lower_bound_xi_minus, upper_bound_xi_minus = map(float, config["2pt_like"]["angle_range_XI_MINUS_1_1"].split()) - - properties[root].update({ - 'add_xi_sys': add_xi_sys, - 'lower_bound_xi_plus': lower_bound_xi_plus, - 'upper_bound_xi_plus': upper_bound_xi_plus, - 'lower_bound_xi_minus': lower_bound_xi_minus, - 'upper_bound_xi_minus': upper_bound_xi_minus - }) + add_xi_sys = add_xi_sys == "T" + lower_bound_xi_plus, upper_bound_xi_plus = map( + float, config["2pt_like"]["angle_range_XI_PLUS_1_1"].split() + ) + lower_bound_xi_minus, upper_bound_xi_minus = map( + float, config["2pt_like"]["angle_range_XI_MINUS_1_1"].split() + ) + + properties[root].update( + { + "add_xi_sys": add_xi_sys, + "lower_bound_xi_plus": lower_bound_xi_plus, + "upper_bound_xi_plus": upper_bound_xi_plus, + "lower_bound_xi_minus": lower_bound_xi_minus, + "upper_bound_xi_minus": upper_bound_xi_minus, + } + ) return properties -def plot_best_fit(data_points, root_to_plot, output_folder, line_args, savefile, ell_min=10.0, ell_max=2048.0, multiply_ell=True, loc_legend="best", bbox_to_anchor=None, label_data="Fiducial data", labels=None, properties=None, paths_to_bestfit=None): - data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{data_points}/cosmosis_{data_points}.fits') - cell_ee = data['CELL_EE'].data - cov_mat = data['COVMAT'].data +def plot_best_fit( + data_points, + root_to_plot, + output_folder, + line_args, + savefile, + ell_min=10.0, + ell_max=2048.0, + multiply_ell=True, + loc_legend="best", + bbox_to_anchor=None, + label_data="Fiducial data", + labels=None, + properties=None, + paths_to_bestfit=None, +): + data = fits.open( + f"/home/guerrini/sp_validation/cosmo_inference/data/{data_points}/cosmosis_{data_points}.fits" + ) + cell_ee = data["CELL_EE"].data + cov_mat = data["COVMAT"].data if labels is None: labels = root_to_plot fig, ax = plt.subplots(1, 1, figsize=(8, 5)) - ell, cell = cell_ee['ANG'], cell_ee['VALUE'] - ax.errorbar(ell, ell*cell, yerr=ell*np.sqrt(np.diag(cov_mat)), fmt='o', label=label_data, color='black', capsize=2) - + ell, cell = cell_ee["ANG"], cell_ee["VALUE"] + ax.errorbar( + ell, + ell * cell, + yerr=ell * np.sqrt(np.diag(cov_mat)), + fmt="o", + label=label_data, + color="black", + capsize=2, + ) + for idx, (label, root) in enumerate(zip(labels, root_to_plot)): - lower_bound_cell_ee = properties[root]['lower_bound_cell_ee'] - upper_bound_cell_ee = properties[root]['upper_bound_cell_ee'] - - #Read the results + # Read the results if paths_to_bestfit is None: - ell = np.loadtxt(output_folder + '{}/best_fit/shear_cl/ell.txt'.format(root, root)) - shear_cl = np.loadtxt(output_folder + '{}/best_fit/shear_cl/bin_1_1.txt'.format(root, root)) + ell = np.loadtxt( + output_folder + + "{}/best_fit/shear_cl/ell.txt".format( + root, + ) + ) + shear_cl = np.loadtxt( + output_folder + + "{}/best_fit/shear_cl/bin_1_1.txt".format( + root, + ) + ) else: - ell = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_cl/ell.txt') - shear_cl = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_cl/bin_1_1.txt') + ell = np.loadtxt(paths_to_bestfit[idx] + "best_fit/shear_cl/ell.txt") + shear_cl = np.loadtxt( + paths_to_bestfit[idx] + "best_fit/shear_cl/bin_1_1.txt" + ) mask = (ell > ell_min) & (ell < ell_max) - ax.plot(ell[mask], ell[mask]*shear_cl[mask] if multiply_ell else shear_cl[mask], label=label, **line_args[idx]) - + ax.plot( + ell[mask], + ell[mask] * shear_cl[mask] if multiply_ell else shear_cl[mask], + label=label, + **line_args[idx], + ) + # Plot the scale cuts for different k_max - ax.axvline(x=1800, color='black', linestyle='--', alpha=0.5) - ax.axvline(x=2048, color='black', linestyle='--', alpha=1.0) - ax.axvline(x=500, color='black', linestyle='--', alpha=0.3) + ax.axvline(x=1800, color="black", linestyle="--", alpha=0.5) + ax.axvline(x=2048, color="black", linestyle="--", alpha=1.0) + ax.axvline(x=500, color="black", linestyle="--", alpha=0.3) ymin = ax.get_ylim()[0] ymax = ax.get_ylim()[1] # Shadowing cut scaled - ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=300, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') - ax.fill_betweenx(y=[ymin, ymax], x1=1600, x2=2048, color='gray', alpha=0.2) + ax.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=300, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", + ) + ax.fill_betweenx(y=[ymin, ymax], x1=1600, x2=2048, color="gray", alpha=0.2) ax.set_ylim(ymin, ymax) # Add labels directly under the tick - ax.text(1740, 0.90, - r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=14, rotation=90) - - ax.text(1978, 0.90, - r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=14, rotation=90) - - ax.text(470, 0.90, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=14, rotation=90) - - ell, cell = cell_ee['ANG'], cell_ee['VALUE'] - ax.set_ylabel(r'$\ell C_\ell \times 10^{-7}$', fontsize=20) - ax.set_xlabel(r'Multipole $\ell$', fontsize=20) - ax.set_xlim(ell.min()-10, ell.max()+100) - ax.set_xscale('squareroot') + ax.text( + 1740, + 0.90, + r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ax.text( + 1978, + 0.90, + r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ax.text( + 470, + 0.90, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ell, cell = cell_ee["ANG"], cell_ee["VALUE"] + ax.set_ylabel(r"$\ell C_\ell \times 10^{-7}$", fontsize=20) + ax.set_xlabel(r"Multipole $\ell$", fontsize=20) + ax.set_xlim(ell.min() - 10, ell.max() + 100) + ax.set_xscale("squareroot") ax.set_xticks(np.array([100, 400, 900, 1600])) ax.minorticks_on() ax.tick_params(axis="x", which="minor", length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax.xaxis.set_ticks(minor_ticks, minor=True) - ax.tick_params(axis='both', which='major', labelsize=14) - ax.tick_params(axis='both', which='minor', labelsize=10) + ax.tick_params(axis="both", which="major", labelsize=14) + ax.tick_params(axis="both", which="minor", labelsize=10) ax.yaxis.get_offset_text().set_visible(False) - plt.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor, fontsize=11) if savefile is not None: - plt.savefig(savefile, bbox_inches='tight') + plt.savefig(savefile, bbox_inches="tight") plt.show() -def plot_best_fit_config(data, root_to_plot, output_folder, line_args, savefile, theta_min=1.0, theta_max=250.0, multiply_theta=True, loc_legend="best", bbox_to_anchor_xip=None, bbox_to_anchor_xim=None, label_data="Fiducial data", labels=None, properties=None, paths_to_bestfit=None): - + +def plot_best_fit_config( + data, + root_to_plot, + output_folder, + line_args, + savefile, + theta_min=1.0, + theta_max=250.0, + multiply_theta=True, + loc_legend="best", + bbox_to_anchor_xip=None, + bbox_to_anchor_xim=None, + label_data="Fiducial data", + labels=None, + properties=None, + paths_to_bestfit=None, +): + data = fits.open(data) - - xi_p_data = data['XI_PLUS'].data - xi_m_data = data['XI_MINUS'].data - cov_mat = data['COVMAT'].data + + xi_p_data = data["XI_PLUS"].data + xi_m_data = data["XI_MINUS"].data + cov_mat = data["COVMAT"].data # Plot hyperparameter loc_legend = "lower center" - fig, [ax,ax2] = plt.subplots(2, 1, figsize=(8, 9)) + fig, [ax, ax2] = plt.subplots(2, 1, figsize=(8, 9)) + + theta, xi_p, xi_m = xi_p_data["ANG"], xi_p_data["VALUE"], xi_m_data["VALUE"] + ax.errorbar( + theta, + theta * xi_p, + yerr=theta * np.sqrt(np.diag(cov_mat[: len(theta), : len(theta)])), + fmt="o", + label=r"UNIONS $\xi_+$ data", + color="black", + capsize=2, + ) + ax2.errorbar( + theta, + theta * xi_m, + yerr=theta + * np.sqrt( + np.diag(cov_mat[len(theta) : 2 * len(theta), len(theta) : 2 * len(theta)]) + ), + fmt="o", + label=r"UNIONS $\xi_-$ data", + color="black", + capsize=2, + ) - theta, xi_p, xi_m = xi_p_data['ANG'], xi_p_data['VALUE'], xi_m_data['VALUE'] - ax.errorbar(theta, theta*xi_p, yerr=theta*np.sqrt(np.diag(cov_mat[:len(theta),:len(theta)])), fmt='o', label=r"UNIONS $\xi_+$ data", color='black', capsize=2) - ax2.errorbar(theta, theta*xi_m, yerr=theta*np.sqrt(np.diag(cov_mat[len(theta):2*len(theta),len(theta):2*len(theta)])), fmt='o', label=r"UNIONS $\xi_-$ data", color='black', capsize=2) - for idx, (label, root) in enumerate(zip(labels, root_to_plot)): - #Read the results + # Read the results if paths_to_bestfit is None: - theta = (np.loadtxt(output_folder + '{}/best_fit/shear_xi_plus/theta.txt'.format(root))) * 180/np.pi * 60 - xi_plus = np.loadtxt(output_folder + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root)) - xi_minus = np.loadtxt(output_folder + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root)) - if '$C_\ell$' not in label: - xi_sys_plus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root)) - xi_sys_minus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root)) - theta_xi_sys = np.loadtxt(output_folder + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60 + theta = ( + ( + np.loadtxt( + output_folder + + "{}/best_fit/shear_xi_plus/theta.txt".format(root) + ) + ) + * 180 + / np.pi + * 60 + ) + xi_plus = np.loadtxt( + output_folder + "{}/best_fit/shear_xi_plus/bin_1_1.txt".format(root) + ) + xi_minus = np.loadtxt( + output_folder + "{}/best_fit/shear_xi_minus/bin_1_1.txt".format(root) + ) + if r"$C_\ell$" not in label: + xi_sys_plus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_plus.txt".format(root) + ) + xi_sys_minus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_minus.txt".format(root) + ) + theta_xi_sys = ( + np.loadtxt( + output_folder + "{}/best_fit/xi_sys/theta.txt".format(root) + ) + * 180 + / np.pi + * 60 + ) xi_plus += np.interp(theta, theta_xi_sys, xi_sys_plus) xi_minus += np.interp(theta, theta_xi_sys, xi_sys_minus) else: - theta = (np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_xi_plus/theta.txt')) * 180/np.pi * 60 - xi_plus = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_xi_plus/bin_1_1.txt') - xi_minus = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_xi_minus/bin_1_1.txt') - if '$C_\ell$' not in label: - xi_sys_plus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root)) - xi_sys_minus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root)) - theta_xi_sys = np.loadtxt(output_folder + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60 + theta = ( + (np.loadtxt(paths_to_bestfit[idx] + "best_fit/shear_xi_plus/theta.txt")) + * 180 + / np.pi + * 60 + ) + xi_plus = np.loadtxt( + paths_to_bestfit[idx] + "best_fit/shear_xi_plus/bin_1_1.txt" + ) + xi_minus = np.loadtxt( + paths_to_bestfit[idx] + "best_fit/shear_xi_minus/bin_1_1.txt" + ) + if r"$C_\ell$" not in label: + xi_sys_plus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_plus.txt".format(root) + ) + xi_sys_minus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_minus.txt".format(root) + ) + theta_xi_sys = ( + np.loadtxt( + output_folder + "{}/best_fit/xi_sys/theta.txt".format(root) + ) + * 180 + / np.pi + * 60 + ) xi_plus += np.interp(theta, theta_xi_sys, xi_sys_plus) xi_minus += np.interp(theta, theta_xi_sys, xi_sys_minus) mask = (theta > theta_min) & (theta < theta_max) theta = theta[mask] - ax.plot(theta, theta*xi_plus[mask] if multiply_theta else xi_plus[mask], label=label, **line_args[idx]) - ax2.plot(theta, theta*xi_minus[mask] if multiply_theta else xi_minus[mask], label=label, **line_args[idx]) - + ax.plot( + theta, + theta * xi_plus[mask] if multiply_theta else xi_plus[mask], + label=label, + **line_args[idx], + ) + ax2.plot( + theta, + theta * xi_minus[mask] if multiply_theta else xi_minus[mask], + label=label, + **line_args[idx], + ) + # XI PLUS PLOT SETTINGS - + # Plot the scale cuts for different k_max - ax.axvline(x=3.2, color='black', linestyle='--', alpha=0.7) + ax.axvline(x=3.2, color="black", linestyle="--", alpha=0.7) ymin = ax.get_ylim()[0] ymax = ax.get_ylim()[1] # Shadowing cut scaled - ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') - ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2) + ax.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=12, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", + ) + ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color="gray", alpha=0.2) ax.set_ylim(ymin, ymax) # Add labels directly under the tick - ax.text(2.9, 1.23e-4, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - ha='center', va='top', fontsize=14, rotation=90) + ax.text( + 2.9, + 1.23e-4, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + ha="center", + va="top", + fontsize=14, + rotation=90, + ) # ax.set_ylabel('$\theta \xi_+$', fontsize=16) # ax.set_xlabel('$\theta$', fontsize=16) - ax.set_xlim([theta.min()-0.1, theta.max()+20]) - ax.set_xscale('log') + ax.set_xlim([theta.min() - 0.1, theta.max() + 20]) + ax.set_xscale("log") ax.set_xticks(np.array([1, 10, 100])) ax.tick_params(axis="x", which="minor", length=2, width=0.8) - ax.tick_params(axis='both', which='major', labelsize=14) - ax.tick_params(axis='both', which='minor', labelsize=10) + ax.tick_params(axis="both", which="major", labelsize=14) + ax.tick_params(axis="both", which="minor", labelsize=10) ax.yaxis.get_offset_text().set_fontsize(14) - ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) + ax.ticklabel_format(axis="y", style="sci", scilimits=(0, 0)) ax.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xip, fontsize=12) # XI_MINUS PLOT SETTINGS - + # Plot the scale cuts for different k_max - ax2.axvline(x=24, color='black', linestyle='--', alpha=0.7) + ax2.axvline(x=24, color="black", linestyle="--", alpha=0.7) ymin = ax2.get_ylim()[0] ymax = ax2.get_ylim()[1] # Shadowing cut scaled - ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') - ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2) + ax2.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=12, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", + ) + ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color="gray", alpha=0.2) ax2.set_ylim(ymin, ymax) # Add labels directly under the tick - ax2.text(21.8, 1.15e-4, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - ha='center', va='top', fontsize=14, rotation=90) - - ax2.set_ylabel(r'$\theta \xi_-$', fontsize=16) - ax2.set_xlabel(r'$\theta$', fontsize=16) - ax2.set_xlim([theta.min()-0.1, theta.max()+20]) - ax2.set_xscale('log') + ax2.text( + 21.8, + 1.15e-4, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ax2.set_ylabel(r"$\theta \xi_-$", fontsize=16) + ax2.set_xlabel(r"$\theta$", fontsize=16) + ax2.set_xlim([theta.min() - 0.1, theta.max() + 20]) + ax2.set_xscale("log") ax2.set_xticks(np.array([1, 10, 100])) ax2.tick_params(axis="x", which="minor", length=2, width=0.8) - ax2.tick_params(axis='both', which='major', labelsize=14) - ax2.tick_params(axis='both', which='minor', labelsize=10) + ax2.tick_params(axis="both", which="major", labelsize=14) + ax2.tick_params(axis="both", which="minor", labelsize=10) ax2.yaxis.get_offset_text().set_fontsize(14) - ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) + ax2.ticklabel_format(axis="y", style="sci", scilimits=(0, 0)) ax2.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xim, fontsize=12) if savefile is not None: - plt.savefig(savefile, bbox_inches='tight') + plt.savefig(savefile, bbox_inches="tight") - plt.show() \ No newline at end of file + plt.show() diff --git a/cosmo_inference/notebooks/cfis_analysis.ipynb b/cosmo_inference/notebooks/cfis_analysis.ipynb index dc2186fb..74816206 100644 --- a/cosmo_inference/notebooks/cfis_analysis.ipynb +++ b/cosmo_inference/notebooks/cfis_analysis.ipynb @@ -18,96 +18,122 @@ }, "outputs": [], "source": [ - "import sys\n", - "import numpy as np\n", - "from astropy.io import fits\n", - "import matplotlib.pylab as plt\n", - "import treecorr\n", - "from ipywidgets import interact, interactive, fixed, interact_manual\n", "import ipywidgets as widgets\n", + "import matplotlib.pylab as plt\n", + "import numpy as np\n", "import pandas as pd\n", - "# import pyccl as ccl\n", + "import pyccl as ccl\n", + "import treecorr\n", + "from astropy.io import fits\n", + "from ipywidgets import interact\n", "\n", "%matplotlib inline\n", - "plt.rcParams.update({'font.size': 20,'figure.figsize':[12,10]})\n", - "plt.rc('mathtext', fontset='stix')\n", - "plt.rc('font', family='sans-serif')\n", - "\n", - "#SPECIFY DIRECTORIES AND CATALOGUE PATHS\n", - "work_dir = '/home/mkilbing/astro/data/UNIONS/v1.x/ShapePipe'\n", - "\n", - "cat_dict = {1:{'dir': work_dir + '/Lensfit/lensfit_goldshape_2022v1.fits',\n", - " 'label': 'LF_full',\n", - " 'e1_bias': 0,\n", - " 'e2_bias': 0,\n", - " 'ls': 'solid',\n", - " 'colour': 'g'},\n", - " 2:{'dir': work_dir + '/ShapePipe/unions_shapepipe_2022_v1.0.fits',\n", - " 'label': 'SP_full',\n", - " 'e1_bias':0,\n", - " 'e2_bias':0,\n", - " 'ls': 'solid',\n", - " 'colour': 'b'},\n", - " 3:{'dir': work_dir + '/matched_LF_SP/masked_matched_lensfit_goldshape_2022v1.fits',\n", - " 'label': 'LF_matched_SP',\n", - " 'e1_bias': 3.939e-4,\n", - " 'e2_bias': 6.482e-5,\n", - " 'ls': 'dotted',\n", - " 'colour': 'g'},\n", - " 4:{'dir': work_dir + '/matched_LF_SP/masked_matched_unions_shapepipe_extended_2022_v1.0.fits',\n", - " 'label': 'SP_matched_LF',\n", - " 'e1_bias':-5.6726e-5,\n", - " 'e2_bias':8.218e-4,\n", - " 'ls': 'dotted',\n", - " 'colour': 'b'},\n", - " 5:{'dir': work_dir + '/matched_LF_SP/matched_footprint_shapepipe.fits',\n", - " 'label': 'SP Match LF Footprint',\n", - " 'e1_bias':0,\n", - " 'e2_bias':0,\n", - " 'ls': 'dashed',\n", - " 'colour': 'b'},\n", - " 6:{'dir': work_dir + '/cfis-shapepipe.parquet',\n", - " 'label': 'SP Match MegaPipe',\n", - " 'e1_bias':0,\n", - " 'e2_bias':0,\n", - " 'ls': 'dashdot',\n", - " 'colour': 'b'},\n", - " 7:{'dir': work_dir + '/ShapePipe/shapepipe_1500_goldshape_v1.fits',\n", - " 'label': 'SP_1500',\n", - " 'e1_bias':7.156105098141909e-06,\n", - " 'e2_bias':-6.00816359759969e-06,\n", - " 'ls': 'dotted',\n", - " 'colour': 'b'},\n", - " 8:{'dir': work_dir + '/ShapePipe/unions_shapepipe_2022_v1.0.4.fits',\n", - " 'label':'SP_cut_Fabian',\n", - " 'e1_bias':0.0,\n", - " 'e2_bias':0.0,\n", - " 'ls': 'dashdot',\n", - " 'colour': 'pink'},\n", - " 9:{'dir': work_dir + '/ShapePipe/unions_shapepipe_psf_2022_v1.0.2.fits',\n", - " 'label':'SP_PSF',\n", - " 'e1_bias':0.0,\n", - " 'e2_bias':0.0,\n", - " 'ls': 'dashdot',\n", - " 'colour': 'b'},\n", - " 10:{'dir': work_dir + '/unions_shapepipe_2022_v1.3.fits',\n", - " 'label':'SP_v1.3',\n", - " 'e1_bias':0.0,\n", - " 'e2_bias':0.0,\n", - " 'ls': 'dashdot',\n", - " 'colour': 'r'},\n", - " 11:{'dir': work_dir + '/ShapePipe/unions_shapepipe_star_2022_v1.3.fits',\n", - " 'label':'SP_v1.3',\n", - " 'e1_bias':0.0,\n", - " 'e2_bias':0.0,\n", - " 'ls': 'dashdot',\n", - " 'colour': 'b'},\n", - " 12:{'dir': work_dir + '/unions_shapepipe_2024_v1.4.1.fits',\n", - " 'label':'SP_v1.4.1',\n", - " 'e1_bias':0.0,\n", - " 'e2_bias':0.0,\n", - " 'ls': 'dashdot',\n", - " 'colour': 'b'}}" + "plt.rcParams.update({\"font.size\": 20, \"figure.figsize\": [12, 10]})\n", + "plt.rc(\"mathtext\", fontset=\"stix\")\n", + "plt.rc(\"font\", family=\"sans-serif\")\n", + "\n", + "# SPECIFY DIRECTORIES AND CATALOGUE PATHS\n", + "work_dir = \"/home/mkilbing/astro/data/UNIONS/v1.x/ShapePipe\"\n", + "\n", + "cat_dict = {\n", + " 1: {\n", + " \"dir\": work_dir + \"/Lensfit/lensfit_goldshape_2022v1.fits\",\n", + " \"label\": \"LF_full\",\n", + " \"e1_bias\": 0,\n", + " \"e2_bias\": 0,\n", + " \"ls\": \"solid\",\n", + " \"colour\": \"g\",\n", + " },\n", + " 2: {\n", + " \"dir\": work_dir + \"/ShapePipe/unions_shapepipe_2022_v1.0.fits\",\n", + " \"label\": \"SP_full\",\n", + " \"e1_bias\": 0,\n", + " \"e2_bias\": 0,\n", + " \"ls\": \"solid\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 3: {\n", + " \"dir\": work_dir + \"/matched_LF_SP/masked_matched_lensfit_goldshape_2022v1.fits\",\n", + " \"label\": \"LF_matched_SP\",\n", + " \"e1_bias\": 3.939e-4,\n", + " \"e2_bias\": 6.482e-5,\n", + " \"ls\": \"dotted\",\n", + " \"colour\": \"g\",\n", + " },\n", + " 4: {\n", + " \"dir\": work_dir\n", + " + \"/matched_LF_SP/masked_matched_unions_shapepipe_extended_2022_v1.0.fits\",\n", + " \"label\": \"SP_matched_LF\",\n", + " \"e1_bias\": -5.6726e-5,\n", + " \"e2_bias\": 8.218e-4,\n", + " \"ls\": \"dotted\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 5: {\n", + " \"dir\": work_dir + \"/matched_LF_SP/matched_footprint_shapepipe.fits\",\n", + " \"label\": \"SP Match LF Footprint\",\n", + " \"e1_bias\": 0,\n", + " \"e2_bias\": 0,\n", + " \"ls\": \"dashed\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 6: {\n", + " \"dir\": work_dir + \"/cfis-shapepipe.parquet\",\n", + " \"label\": \"SP Match MegaPipe\",\n", + " \"e1_bias\": 0,\n", + " \"e2_bias\": 0,\n", + " \"ls\": \"dashdot\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 7: {\n", + " \"dir\": work_dir + \"/ShapePipe/shapepipe_1500_goldshape_v1.fits\",\n", + " \"label\": \"SP_1500\",\n", + " \"e1_bias\": 7.156105098141909e-06,\n", + " \"e2_bias\": -6.00816359759969e-06,\n", + " \"ls\": \"dotted\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 8: {\n", + " \"dir\": work_dir + \"/ShapePipe/unions_shapepipe_2022_v1.0.4.fits\",\n", + " \"label\": \"SP_cut_Fabian\",\n", + " \"e1_bias\": 0.0,\n", + " \"e2_bias\": 0.0,\n", + " \"ls\": \"dashdot\",\n", + " \"colour\": \"pink\",\n", + " },\n", + " 9: {\n", + " \"dir\": work_dir + \"/ShapePipe/unions_shapepipe_psf_2022_v1.0.2.fits\",\n", + " \"label\": \"SP_PSF\",\n", + " \"e1_bias\": 0.0,\n", + " \"e2_bias\": 0.0,\n", + " \"ls\": \"dashdot\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 10: {\n", + " \"dir\": work_dir + \"/unions_shapepipe_2022_v1.3.fits\",\n", + " \"label\": \"SP_v1.3\",\n", + " \"e1_bias\": 0.0,\n", + " \"e2_bias\": 0.0,\n", + " \"ls\": \"dashdot\",\n", + " \"colour\": \"r\",\n", + " },\n", + " 11: {\n", + " \"dir\": work_dir + \"/ShapePipe/unions_shapepipe_star_2022_v1.3.fits\",\n", + " \"label\": \"SP_v1.3\",\n", + " \"e1_bias\": 0.0,\n", + " \"e2_bias\": 0.0,\n", + " \"ls\": \"dashdot\",\n", + " \"colour\": \"b\",\n", + " },\n", + " 12: {\n", + " \"dir\": work_dir + \"/unions_shapepipe_2024_v1.4.1.fits\",\n", + " \"label\": \"SP_v1.4.1\",\n", + " \"e1_bias\": 0.0,\n", + " \"e2_bias\": 0.0,\n", + " \"ls\": \"dashdot\",\n", + " \"colour\": \"b\",\n", + " },\n", + "}" ] }, { @@ -117,7 +143,7 @@ "metadata": {}, "outputs": [], "source": [ - "# CATALOGUE OPTIONS: \n", + "# CATALOGUE OPTIONS:\n", "# 1: LensFit Full\n", "# 2: ShapePipe Full\n", "# 3: LF Match SP\n", @@ -128,24 +154,26 @@ "# 8: SP cut on large gals\n", "# 12: SP psfex v1.4.1\n", "\n", - "cat_options=[10,12]\n", + "cat_options = [10, 12]\n", "\n", "dfs = []\n", "\n", "for cat_option in cat_options:\n", - " if cat_option==6:\n", - " df = pd.read_parquet(cat_dict[cat_option]['dir'], engine='pyarrow')\n", + " if cat_option == 6:\n", + " df = pd.read_parquet(cat_dict[cat_option][\"dir\"], engine=\"pyarrow\")\n", " df = df.replace([np.inf, -np.inf], np.nan).dropna(axis=0)\n", " else:\n", - " with fits.open(cat_dict[cat_option]['dir']) as data:\n", + " with fits.open(cat_dict[cat_option][\"dir\"]) as data:\n", " df = pd.DataFrame(data[1].data)\n", - " if cat_option==7:\n", - " df = df.rename(columns={'g1':'e1','g2':'e2'})\n", - " if cat_option==8 or cat_option==10:\n", - " df = df.rename(columns={'RA':'ra','Dec':'dec'})\n", - " if cat_option==12:\n", - " df = df.rename(columns={'RA':'ra','Dec':'dec','e1':'e1_prev','e2':'e2_prev'})\n", - " df = df.rename(columns={'e1_noleakage':'e1','e2_noleakage':'e2'})\n", + " if cat_option == 7:\n", + " df = df.rename(columns={\"g1\": \"e1\", \"g2\": \"e2\"})\n", + " if cat_option == 8 or cat_option == 10:\n", + " df = df.rename(columns={\"RA\": \"ra\", \"Dec\": \"dec\"})\n", + " if cat_option == 12:\n", + " df = df.rename(\n", + " columns={\"RA\": \"ra\", \"Dec\": \"dec\", \"e1\": \"e1_prev\", \"e2\": \"e2_prev\"}\n", + " )\n", + " df = df.rename(columns={\"e1_noleakage\": \"e1\", \"e2_noleakage\": \"e2\"})\n", " dfs.append(df)" ] }, @@ -166,10 +194,10 @@ "outputs": [], "source": [ "for cat in cat_options:\n", - " plt.plot(df['ra'],df['dec'], '.',label=cat_dict[cat]['label'])\n", - "plt.xlabel('RA [deg]')\n", - "plt.ylabel('DEC [deg]')\n", - "plt.legend(loc='upper right')\n", + " plt.plot(df[\"ra\"], df[\"dec\"], \".\", label=cat_dict[cat][\"label\"])\n", + "plt.xlabel(\"RA [deg]\")\n", + "plt.ylabel(\"DEC [deg]\")\n", + "plt.legend(loc=\"upper right\")\n", "# plt.savefig('plots/3500deg^2_plot.pdf',dpi=100)\n", "plt.show()" ] @@ -181,32 +209,66 @@ "metadata": {}, "outputs": [], "source": [ - "#Ellipticity histograms\n", - "plt.rcParams.update({'font.size': 20,'figure.figsize':[22,7]})\n", + "# Ellipticity histograms\n", + "plt.rcParams.update({\"font.size\": 20, \"figure.figsize\": [22, 7]})\n", "\n", "fig, axs = plt.subplots(1, 2)\n", "nbins = 200\n", "\n", - "for idx,cat in enumerate(cat_options):\n", - " (n,bins,_)= axs[0].hist(dfs[idx]['e1'], bins=nbins, density=True, histtype='step',\n", - " weights=dfs[idx]['w'],label='e1 %s' %cat_dict[cat]['label'])\n", - "axs[0].set_xlabel(r'$e_1$')\n", + "for idx, cat in enumerate(cat_options):\n", + " (n, bins, _) = axs[0].hist(\n", + " dfs[idx][\"e1\"],\n", + " bins=nbins,\n", + " density=True,\n", + " histtype=\"step\",\n", + " weights=dfs[idx][\"w\"],\n", + " label=\"e1 %s\" % cat_dict[cat][\"label\"],\n", + " )\n", + "axs[0].set_xlabel(r\"$e_1$\")\n", "axs[0].legend()\n", - "axs[0].set_xlim([-1.5,1.5])\n", + "axs[0].set_xlim([-1.5, 1.5])\n", "\n", "# axs[0].set_ylim([0,2e4])\n", "\n", - "for idx,cat in enumerate(cat_options):\n", - " (n,bins,_)= axs[1].hist(dfs[idx]['e2'], bins=nbins, density=True, histtype='step',\n", - " weights=dfs[idx]['w'],label='e2 {}'.format(cat_dict[cat]['label']))\n", - " print('e1 sigma {}: {}'.format(cat_dict[cat]['label'],np.std(dfs[idx]['e1_noleakage'])))\n", - " print('e2 sigma {}: {}'.format(cat_dict[cat]['label'],np.std(dfs[idx]['e2_noleakage'])))\n", - " print('e1 bias {}: {}'.format(cat_dict[cat]['label'],np.average(np.array(dfs[idx]['e1_noleakage']), weights=np.array(dfs[idx]['w']))))\n", - " print('e2 bias {}: {}'.format(cat_dict[cat]['label'],np.average(np.array(dfs[idx]['e2_noleakage']), weights=np.array(dfs[idx]['w']))))\n", - "axs[1].set_xlabel(r'$e_2$')\n", + "for idx, cat in enumerate(cat_options):\n", + " (n, bins, _) = axs[1].hist(\n", + " dfs[idx][\"e2\"],\n", + " bins=nbins,\n", + " density=True,\n", + " histtype=\"step\",\n", + " weights=dfs[idx][\"w\"],\n", + " label=\"e2 {}\".format(cat_dict[cat][\"label\"]),\n", + " )\n", + " print(\n", + " \"e1 sigma {}: {}\".format(\n", + " cat_dict[cat][\"label\"], np.std(dfs[idx][\"e1_noleakage\"])\n", + " )\n", + " )\n", + " print(\n", + " \"e2 sigma {}: {}\".format(\n", + " cat_dict[cat][\"label\"], np.std(dfs[idx][\"e2_noleakage\"])\n", + " )\n", + " )\n", + " print(\n", + " \"e1 bias {}: {}\".format(\n", + " cat_dict[cat][\"label\"],\n", + " np.average(\n", + " np.array(dfs[idx][\"e1_noleakage\"]), weights=np.array(dfs[idx][\"w\"])\n", + " ),\n", + " )\n", + " )\n", + " print(\n", + " \"e2 bias {}: {}\".format(\n", + " cat_dict[cat][\"label\"],\n", + " np.average(\n", + " np.array(dfs[idx][\"e2_noleakage\"]), weights=np.array(dfs[idx][\"w\"])\n", + " ),\n", + " )\n", + " )\n", + "axs[1].set_xlabel(r\"$e_2$\")\n", "axs[1].legend()\n", - "axs[1].set_xlim([-1.5,1.5])\n", - "# axs[1].set_ylim([0,2e4])\n" + "axs[1].set_xlim([-1.5, 1.5])\n", + "# axs[1].set_ylim([0,2e4])" ] }, { @@ -216,17 +278,23 @@ "metadata": {}, "outputs": [], "source": [ - "#Mag histograms\n", - "\n", - "plt.rcParams.update({'font.size': 20,'figure.figsize':[15,10]})\n", + "# Mag histograms\n", + "\n", + "plt.rcParams.update({\"font.size\": 20, \"figure.figsize\": [15, 10]})\n", + "\n", + "for idx, cat in enumerate(cat_options):\n", + " (n, bins, _) = plt.hist(\n", + " dfs[idx][\"mag\"],\n", + " bins=200,\n", + " density=False,\n", + " histtype=\"step\",\n", + " weights=dfs[idx][\"w\"],\n", + " label=\"Mag %s\" % cat_dict[cat][\"label\"],\n", + " )\n", "\n", - "for idx,cat in enumerate(cat_options):\n", - " (n,bins,_)= plt.hist(dfs[idx]['mag'], bins=200, density=False, histtype='step', \n", - " weights=dfs[idx]['w'],label='Mag %s' %cat_dict[cat]['label'])\n", - " \n", - "plt.xlim([19,26])\n", - "plt.xlabel('Mag')\n", - "plt.legend(loc='upper left')\n" + "plt.xlim([19, 26])\n", + "plt.xlabel(\"Mag\")\n", + "plt.legend(loc=\"upper left\")" ] }, { @@ -247,11 +315,10 @@ "outputs": [], "source": [ "# nz_lf = fits.open(work_dir + '/nz/blind_nz_cfis_lensfit_goldshape_2022v1.fits')[1].data\n", - "nz = fits.open(work_dir + '/nz/blind_nz_cfis_shapepipe_2022v1.fits')[1].data\n", + "nz = fits.open(work_dir + \"/nz/blind_nz_cfis_shapepipe_2022v1.fits\")[1].data\n", "\n", "# nz_lf_matched = fits.open(work_dir + '/nz/nz_masked_matched_lensfit_goldshape_2022v1.fits')[1].data\n", - "# nz_sp_matched = fits.open(work_dir + '/nz/nz_masked_matched_unions_shapepipe_extended_2022_v1.0.fits')[1].data\n", - "\n" + "# nz_sp_matched = fits.open(work_dir + '/nz/nz_masked_matched_unions_shapepipe_extended_2022_v1.0.fits')[1].data" ] }, { @@ -263,10 +330,10 @@ }, "outputs": [], "source": [ - "#FULL CATALOGUE NZ'S\n", + "# FULL CATALOGUE NZ'S\n", "from matplotlib.ticker import StrMethodFormatter\n", "\n", - "blinds = ['A','B','C']\n", + "blinds = [\"A\", \"B\", \"C\"]\n", "\n", "# for blind in blinds:\n", "# z1 = nz_lf['Z_%s' %blind]\n", @@ -282,31 +349,30 @@ "# plt.legend(fontsize=20)\n", "# # plt.savefig('plots/Lensfit_nz_all_blinds.pdf' )\n", "# plt.show()\n", - " #####################################################################################################\n", + "#####################################################################################################\n", "for blind in blinds:\n", - " z = nz['Z_%s' %blind]\n", - " bins=np.linspace(0,5,100)\n", - " \n", - " y,edges = np.histogram(z, bins,density=True,weights=nz['som_w'])\n", - " centers = 0.5*(edges[1:]+ edges[:-1])\n", - " plt.plot(centers,y,'-o',markersize=4,label='Blind %s' %blind,alpha=0.7)\n", + " z = nz[\"Z_%s\" % blind]\n", + " bins = np.linspace(0, 5, 100)\n", + "\n", + " y, edges = np.histogram(z, bins, density=True, weights=nz[\"som_w\"])\n", + " centers = 0.5 * (edges[1:] + edges[:-1])\n", + " plt.plot(centers, y, \"-o\", markersize=4, label=\"Blind %s\" % blind, alpha=0.7)\n", "\n", " # (n,bins,_)= plt.hist(z2, bins=50, range=(0,5.0), density=True, histtype='step',weights=nz['som_w'],label='Blind %s' %blind,alpha=0.5)\n", " # n_sp.append(list(n))\n", " # bins_sp.append(list(bins))\n", "\n", - " plt.xlabel(r'$z$')\n", - " plt.ylabel(r'$n(z)$')\n", - " plt.ylim([0,1.7])\n", - " plt.xlim([0,5])\n", + " plt.xlabel(r\"$z$\")\n", + " plt.ylabel(r\"$n(z)$\")\n", + " plt.ylim([0, 1.7])\n", + " plt.xlim([0, 5])\n", " plt.grid(True)\n", - " plt.gca().xaxis.set_major_formatter(StrMethodFormatter('{x:,.1f}'))\n", + " plt.gca().xaxis.set_major_formatter(StrMethodFormatter(\"{x:,.1f}\"))\n", " # print(\"zmin = \",min(z))\n", " # print(\"zmax = \",max(z))\n", " plt.legend(fontsize=20)\n", - " plt.savefig('../plots/unions_nz.pdf',bbox_inches='tight')\n", - "plt.show()\n", - "\n" + " plt.savefig(\"../plots/unions_nz.pdf\", bbox_inches=\"tight\")\n", + "plt.show()" ] }, { @@ -329,36 +395,36 @@ "# Create TreeCorr catalogue\n", "treecorr.set_omp_threads(8)\n", "\n", - "sep_units = 'arcmin'\n", + "sep_units = \"arcmin\"\n", "theta_min = 1\n", "theta_max = 200\n", "\n", "TreeCorrConfig = {\n", - " 'ra_units': 'degrees',\n", - " 'dec_units': 'degrees',\n", - " 'max_sep': str(theta_max),\n", - " 'min_sep': str(theta_min),\n", - " 'sep_units': sep_units,\n", - " 'nbins': 20,\n", - " 'var_method':'jackknife',\n", - " }\n", + " \"ra_units\": \"degrees\",\n", + " \"dec_units\": \"degrees\",\n", + " \"max_sep\": str(theta_max),\n", + " \"min_sep\": str(theta_min),\n", + " \"sep_units\": sep_units,\n", + " \"nbins\": 20,\n", + " \"var_method\": \"jackknife\",\n", + "}\n", "\n", "cat_ggs = []\n", - "for idx,cat in enumerate(cat_options):\n", + "for idx, cat in enumerate(cat_options):\n", " cat_gal = treecorr.Catalog(\n", - " ra=dfs[idx]['ra'],\n", - " dec=dfs[idx]['dec'],\n", - " g1=dfs[idx]['e1']-cat_dict[cat]['e1_bias'],\n", - " g2=dfs[idx]['e2']-cat_dict[cat]['e2_bias'],\n", - " w=dfs[idx]['w'],\n", - " ra_units='degrees',\n", - " dec_units='degrees',\n", - " npatch=50\n", + " ra=dfs[idx][\"ra\"],\n", + " dec=dfs[idx][\"dec\"],\n", + " g1=dfs[idx][\"e1\"] - cat_dict[cat][\"e1_bias\"],\n", + " g2=dfs[idx][\"e2\"] - cat_dict[cat][\"e2_bias\"],\n", + " w=dfs[idx][\"w\"],\n", + " ra_units=\"degrees\",\n", + " dec_units=\"degrees\",\n", + " npatch=50,\n", " )\n", " gg = treecorr.GGCorrelation(TreeCorrConfig)\n", " gg.process(cat_gal)\n", " cat_ggs.append(gg)\n", - " print(\"done for cat %s\" %cat)" + " print(\"done for cat %s\" % cat)" ] }, { @@ -368,52 +434,76 @@ "metadata": {}, "outputs": [], "source": [ - "plt.rcParams.update({'font.size': 25,'figure.figsize':[10,7]})\n", + "plt.rcParams.update({\"font.size\": 25, \"figure.figsize\": [10, 7]})\n", "\n", "ax1 = plt.subplot(111)\n", - "for idx,cat in enumerate(cat_options):\n", - " ax1.plot(cat_ggs[idx].meanr, cat_ggs[idx].npairs, \n", - " label=r'$n_{pairs}$ %s' %(cat_dict[cat]['label']),\n", - " ls=cat_dict[cat]['ls'],color=cat_dict[cat]['colour'])\n", - "ax1.set_xlabel(rf'$\\theta$ [{sep_units}]')\n", - "ax1.set_ylabel(r'$n_{pairs}$')\n", + "for idx, cat in enumerate(cat_options):\n", + " ax1.plot(\n", + " cat_ggs[idx].meanr,\n", + " cat_ggs[idx].npairs,\n", + " label=r\"$n_{pairs}$ %s\" % (cat_dict[cat][\"label\"]),\n", + " ls=cat_dict[cat][\"ls\"],\n", + " color=cat_dict[cat][\"colour\"],\n", + " )\n", + "ax1.set_xlabel(rf\"$\\theta$ [{sep_units}]\")\n", + "ax1.set_ylabel(r\"$n_{pairs}$\")\n", "plt.show()\n", "\n", "ax2 = plt.subplot(111)\n", - "for idx,cat in enumerate(cat_options):\n", - " ax2.errorbar(cat_ggs[idx].meanr, cat_ggs[idx].xip, yerr=np.sqrt(cat_ggs[idx].varxip), \n", - " label=r'$\\xi_+$ %s' %(cat_dict[cat]['label']),\n", - " ls=cat_dict[cat]['ls'],color=cat_dict[cat]['colour'])\n", - " ax2.axvspan(0,10,color='gray', alpha=0.3)\n", + "for idx, cat in enumerate(cat_options):\n", + " ax2.errorbar(\n", + " cat_ggs[idx].meanr,\n", + " cat_ggs[idx].xip,\n", + " yerr=np.sqrt(cat_ggs[idx].varxip),\n", + " label=r\"$\\xi_+$ %s\" % (cat_dict[cat][\"label\"]),\n", + " ls=cat_dict[cat][\"ls\"],\n", + " color=cat_dict[cat][\"colour\"],\n", + " )\n", + " ax2.axvspan(0, 10, color=\"gray\", alpha=0.3)\n", " # ax2.axvspan(100,200,color='gray', alpha=0.3)\n", - " \n", - "ax2.text(0.85, 0.88, '1,1', transform=ax2.transAxes,\n", - " bbox=dict(facecolor='white', edgecolor='black', boxstyle='round', pad=0.5))\n", - "ax2.set_xscale('log')\n", - "ax2.set_yscale('log')\n", - "ax2.set_xlabel(rf'$\\theta$ [{sep_units}]')\n", - "ax2.set_xlim([0,200])\n", - "_ = ax2.set_ylabel(r'$\\xi_+(\\theta)$')\n", - "ax2.legend(loc='lower left')\n", + "\n", + "ax2.text(\n", + " 0.85,\n", + " 0.88,\n", + " \"1,1\",\n", + " transform=ax2.transAxes,\n", + " bbox=dict(facecolor=\"white\", edgecolor=\"black\", boxstyle=\"round\", pad=0.5),\n", + ")\n", + "ax2.set_xscale(\"log\")\n", + "ax2.set_yscale(\"log\")\n", + "ax2.set_xlabel(rf\"$\\theta$ [{sep_units}]\")\n", + "ax2.set_xlim([0, 200])\n", + "_ = ax2.set_ylabel(r\"$\\xi_+(\\theta)$\")\n", + "ax2.legend(loc=\"lower left\")\n", "# plt.savefig('../plots/xi_plus_%s.pdf' %cat_dict[cat]['label'],bbox_inches='tight')\n", "plt.show()\n", "\n", "ax3 = plt.subplot(111)\n", - "for idx,cat in enumerate(cat_options):\n", - " ax3.errorbar(cat_ggs[idx].meanr, cat_ggs[idx].xim, yerr=np.sqrt(cat_ggs[idx].varxim), \n", - " label=r'$\\xi_-$ %s' %(cat_dict[cat]['label']),\n", - " ls='dotted',color=cat_dict[cat]['colour'])\n", - " ax3.axvspan(0,20,color='gray', alpha=0.3)\n", + "for idx, cat in enumerate(cat_options):\n", + " ax3.errorbar(\n", + " cat_ggs[idx].meanr,\n", + " cat_ggs[idx].xim,\n", + " yerr=np.sqrt(cat_ggs[idx].varxim),\n", + " label=r\"$\\xi_-$ %s\" % (cat_dict[cat][\"label\"]),\n", + " ls=\"dotted\",\n", + " color=cat_dict[cat][\"colour\"],\n", + " )\n", + " ax3.axvspan(0, 20, color=\"gray\", alpha=0.3)\n", " # ax3.axvspan(100,200,color='gray', alpha=0.3)\n", - " \n", - "ax3.text(0.85, 0.88, '1,1', transform=ax3.transAxes,\n", - " bbox=dict(facecolor='white', edgecolor='black', boxstyle='round', pad=0.5))\n", - "ax3.set_xscale('log')\n", - "ax3.set_yscale('log')\n", - "ax3.set_xlabel(rf'$\\theta$ [{sep_units}]')\n", - "ax3.set_xlim([0,200])\n", - "ax3.legend(loc='lower left')\n", - "_ = ax3.set_ylabel(r'$\\xi_-(\\theta)$')\n", + "\n", + "ax3.text(\n", + " 0.85,\n", + " 0.88,\n", + " \"1,1\",\n", + " transform=ax3.transAxes,\n", + " bbox=dict(facecolor=\"white\", edgecolor=\"black\", boxstyle=\"round\", pad=0.5),\n", + ")\n", + "ax3.set_xscale(\"log\")\n", + "ax3.set_yscale(\"log\")\n", + "ax3.set_xlabel(rf\"$\\theta$ [{sep_units}]\")\n", + "ax3.set_xlim([0, 200])\n", + "ax3.legend(loc=\"lower left\")\n", + "_ = ax3.set_ylabel(r\"$\\xi_-(\\theta)$\")\n", "# plt.savefig('../plots/xi_minus_%s.pdf' %cat_dict[cat]['label'],bbox_inches='tight')\n", "plt.show()" ] @@ -435,76 +525,113 @@ "metadata": {}, "outputs": [], "source": [ - "nz = np.loadtxt('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/v1.0/nz/dndz_SP_v1.0_A.txt', usecols=1)\n", - "bins = np.loadtxt('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/v1.0/nz/dndz_SP_v1.0_A.txt', usecols=0)\n", + "nz = np.loadtxt(\n", + " \"/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/v1.0/nz/dndz_SP_v1.0_A.txt\",\n", + " usecols=1,\n", + ")\n", + "bins = np.loadtxt(\n", + " \"/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/v1.0/nz/dndz_SP_v1.0_A.txt\",\n", + " usecols=0,\n", + ")\n", "\n", - "def theory_cls(Omega_c, Omega_b, h,n_s,sigma_8):\n", - " #Set cosmology\n", - " cosmo = ccl.Cosmology(Omega_c, Omega_b, h, n_s,sigma_8)\n", + "\n", + "def theory_cls(Omega_c, Omega_b, h, n_s, sigma_8):\n", + " # Set cosmology\n", + " cosmo = ccl.Cosmology(Omega_c, Omega_b, h, n_s, sigma_8)\n", "\n", " ell = np.arange(2, 2000)\n", - " theta_deg = np.logspace(np.log10(theta_min/60), np.log10(theta_max/60), num=20) # Theta is in degrees\n", - " #CALCULATION OF THEORY XI_PM\n", + " theta_deg = np.logspace(\n", + " np.log10(theta_min / 60), np.log10(theta_max / 60), num=20\n", + " ) # Theta is in degrees\n", + " # CALCULATION OF THEORY XI_PM\n", " xi_plus_lf = []\n", " xi_minus_lf = []\n", "\n", " for i in range(len(nz)):\n", - " bias_ia = 0*np.ones_like(bins[i][:-1])\n", - " lens_ia = ccl.WeakLensingTracer(cosmo, dndz =(np.array(bins[i][:-1]), np.array(nz[i])), ia_bias = (np.array(bins[i][:-1]),bias_ia))\n", + " bias_ia = 0 * np.ones_like(bins[i][:-1])\n", + " lens_ia = ccl.WeakLensingTracer(\n", + " cosmo,\n", + " dndz=(np.array(bins[i][:-1]), np.array(nz[i])),\n", + " ia_bias=(np.array(bins[i][:-1]), bias_ia),\n", + " )\n", " cl = ccl.angular_cl(cosmo, lens_ia, lens_ia, ell)\n", "\n", - " xi_plus_lf.append(list(ccl.correlation(cosmo, ell, cl, theta_deg, type='GG+', method='FFTLog')))\n", - " xi_minus_lf.append(list(ccl.correlation(cosmo, ell, cl, theta_deg, type='GG-', method='FFTLog')))\n", - "\n", - "\n", - " style = [':','--','-.']\n", - " plt.errorbar(gg.meanr, gg.xip, yerr=np.sqrt(gg.varxip), ls='', label=r'$\\xi_+$ TreeCorr (LF)',capsize=5,marker='o',color='b')\n", - " plt.errorbar(gg.meanr, gg.xim, yerr=np.sqrt(gg.varxim), ls='', label=r'$\\xi_-$ TreeCorr (LF)',capsize=5,marker='o',color='g')\n", - "\n", + " xi_plus_lf.append(\n", + " list(\n", + " ccl.correlation(cosmo, ell, cl, theta_deg, type=\"GG+\", method=\"FFTLog\")\n", + " )\n", + " )\n", + " xi_minus_lf.append(\n", + " list(\n", + " ccl.correlation(cosmo, ell, cl, theta_deg, type=\"GG-\", method=\"FFTLog\")\n", + " )\n", + " )\n", + "\n", + " style = [\":\", \"--\", \"-.\"]\n", + " plt.errorbar(\n", + " gg.meanr,\n", + " gg.xip,\n", + " yerr=np.sqrt(gg.varxip),\n", + " ls=\"\",\n", + " label=r\"$\\xi_+$ TreeCorr (LF)\",\n", + " capsize=5,\n", + " marker=\"o\",\n", + " color=\"b\",\n", + " )\n", + " plt.errorbar(\n", + " gg.meanr,\n", + " gg.xim,\n", + " yerr=np.sqrt(gg.varxim),\n", + " ls=\"\",\n", + " label=r\"$\\xi_-$ TreeCorr (LF)\",\n", + " capsize=5,\n", + " marker=\"o\",\n", + " color=\"g\",\n", + " )\n", "\n", " for i in range(len(blinds)):\n", - " plt.plot(theta_deg*60,xi_plus_lf[i],color='b', ls=style[i], label=r'$\\xi_+$ PyCCL (LF) blind %s' %blinds[i])\n", - " plt.plot(theta_deg*60,xi_minus_lf[i],color='g', ls=style[i], label=r'$\\xi_-$ PyCCL (LF) blind %s' %blinds[i])\n", - "\n", - " plt.xscale('log')\n", + " plt.plot(\n", + " theta_deg * 60,\n", + " xi_plus_lf[i],\n", + " color=\"b\",\n", + " ls=style[i],\n", + " label=r\"$\\xi_+$ PyCCL (LF) blind %s\" % blinds[i],\n", + " )\n", + " plt.plot(\n", + " theta_deg * 60,\n", + " xi_minus_lf[i],\n", + " color=\"g\",\n", + " ls=style[i],\n", + " label=r\"$\\xi_-$ PyCCL (LF) blind %s\" % blinds[i],\n", + " )\n", + "\n", + " plt.xscale(\"log\")\n", " # plt.yscale('log')\n", " plt.legend(fontsize=20)\n", - " plt.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0,0))\n", - " plt.xlim([1,200])\n", - " plt.ylim([0,10e-5])\n", - " plt.ylabel(r'$\\xi_\\pm(\\theta)$')\n", - " plt.xlabel(r'$\\theta$ [arcmin]')\n", + " plt.ticklabel_format(axis=\"y\", style=\"sci\", scilimits=(0, 0))\n", + " plt.xlim([1, 200])\n", + " plt.ylim([0, 10e-5])\n", + " plt.ylabel(r\"$\\xi_\\pm(\\theta)$\")\n", + " plt.xlabel(r\"$\\theta$ [arcmin]\")\n", " # plt.savefig('plots/pyccl_comparison_lensfit.pdf')\n", "\n", "\n", - "\n", - "interact(theory_cls,\n", - " Omega_c=widgets.FloatSlider(value=0.26,\n", - " min=0.01,\n", - " max=0.5,\n", - " step=0.01,\n", - " description=r'$\\Omega_c$'),\n", - " Omega_b=widgets.FloatSlider(value=0.04,\n", - " min=0.001,\n", - " max=0.07,\n", - " step=0.001,\n", - " description=r'$\\Omega_b$'),\n", - " h=widgets.FloatSlider(value=0.7,\n", - " min=0.3,\n", - " max=0.9,\n", - " step=0.01,\n", - " description=r'$h$'),\n", - "\n", - " n_s=widgets.FloatSlider(value=0.96,\n", - " min=0.6,\n", - " max=1.1,\n", - " step=0.01,\n", - " description=r'$n_s$'),\n", - " sigma_8=widgets.FloatSlider(value=0.8,\n", - " min=0.3,\n", - " max=1.2,\n", - " step=0.01,\n", - " description=r'$\\sigma_8$'))\n" + "interact(\n", + " theory_cls,\n", + " Omega_c=widgets.FloatSlider(\n", + " value=0.26, min=0.01, max=0.5, step=0.01, description=r\"$\\Omega_c$\"\n", + " ),\n", + " Omega_b=widgets.FloatSlider(\n", + " value=0.04, min=0.001, max=0.07, step=0.001, description=r\"$\\Omega_b$\"\n", + " ),\n", + " h=widgets.FloatSlider(value=0.7, min=0.3, max=0.9, step=0.01, description=r\"$h$\"),\n", + " n_s=widgets.FloatSlider(\n", + " value=0.96, min=0.6, max=1.1, step=0.01, description=r\"$n_s$\"\n", + " ),\n", + " sigma_8=widgets.FloatSlider(\n", + " value=0.8, min=0.3, max=1.2, step=0.01, description=r\"$\\sigma_8$\"\n", + " ),\n", + ")" ] }, { @@ -525,49 +652,77 @@ "metadata": {}, "outputs": [], "source": [ - "for idx,cat in enumerate(cat_options):\n", - " \n", - " blind = 'A'\n", - " label = 'SP_v1.4'\n", - " \n", - " cc = '/n23data1/n06data/lgoh/scratch/CFIS-UNIONS/CFIS-UNIONS_dev/cosmo_inference/data/{}/covs/cov_{}'.format(label+ '_{}'.format(blind),label)\n", - " \n", - " cc_var = np.diag(np.loadtxt(cc +'.txt'))\n", + "for idx, cat in enumerate(cat_options):\n", + " blind = \"A\"\n", + " label = \"SP_v1.4\"\n", + "\n", + " cc = \"/n23data1/n06data/lgoh/scratch/CFIS-UNIONS/CFIS-UNIONS_dev/cosmo_inference/data/{}/covs/cov_{}\".format(\n", + " label + \"_{}\".format(blind), label\n", + " )\n", + "\n", + " cc_var = np.diag(np.loadtxt(cc + \".txt\"))\n", " cc_varxip = cc_var[:20]\n", " cc_varxim = cc_var[20:]\n", "\n", - " cc_var = np.diag(np.loadtxt(cc+'_g.txt'))\n", + " cc_var = np.diag(np.loadtxt(cc + \"_g.txt\"))\n", " cc_varxip_g = cc_var[:20]\n", " cc_varxim_g = cc_var[20:]\n", "\n", - " plt.loglog(cat_ggs[idx].meanr,cat_ggs[idx].varxip,'-k', \n", - " label=r'$\\sigma(\\xi_+)$ TreeCorr jackknife %s' %cat_dict[cat]['label'])\n", - " plt.loglog(cat_ggs[idx].meanr,cc_varxip, ls='--', c='%s' %cat_dict[cat]['colour'], \n", - " label=r'$\\sigma(\\xi_+)$ CosmoCov %s' %cat_dict[cat]['label'])\n", - " plt.loglog(cat_ggs[idx].meanr,cc_varxip_g, ls=':', c='%s' %cat_dict[cat]['colour'], \n", - " label=r'$\\sigma(\\xi_+)$ CosmoCov Gaussian %s' %cat_dict[cat]['label'])\n", + " plt.loglog(\n", + " cat_ggs[idx].meanr,\n", + " cat_ggs[idx].varxip,\n", + " \"-k\",\n", + " label=r\"$\\sigma(\\xi_+)$ TreeCorr jackknife %s\" % cat_dict[cat][\"label\"],\n", + " )\n", + " plt.loglog(\n", + " cat_ggs[idx].meanr,\n", + " cc_varxip,\n", + " ls=\"--\",\n", + " c=\"%s\" % cat_dict[cat][\"colour\"],\n", + " label=r\"$\\sigma(\\xi_+)$ CosmoCov %s\" % cat_dict[cat][\"label\"],\n", + " )\n", + " plt.loglog(\n", + " cat_ggs[idx].meanr,\n", + " cc_varxip_g,\n", + " ls=\":\",\n", + " c=\"%s\" % cat_dict[cat][\"colour\"],\n", + " label=r\"$\\sigma(\\xi_+)$ CosmoCov Gaussian %s\" % cat_dict[cat][\"label\"],\n", + " )\n", " plt.grid()\n", - " plt.xlim([cat_ggs[idx].meanr[0],cat_ggs[idx].meanr[-1]])\n", + " plt.xlim([cat_ggs[idx].meanr[0], cat_ggs[idx].meanr[-1]])\n", " plt.legend(fontsize=15)\n", - " plt.xlabel(rf'$\\theta$ [{sep_units}]')\n", - " plt.ylabel(r'$\\sigma(\\xi_+)$')\n", + " plt.xlabel(rf\"$\\theta$ [{sep_units}]\")\n", + " plt.ylabel(r\"$\\sigma(\\xi_+)$\")\n", " plt.show()\n", " # plt.savefig()\n", "\n", - " plt.loglog(cat_ggs[idx].meanr,cat_ggs[idx].varxim,'-k', \n", - " label=r'$\\sigma(\\xi_-)$ TreeCorr jackknife %s' %cat_dict[cat]['label'])\n", - " plt.loglog(cat_ggs[idx].meanr,cc_varxim, ls='--', c='%s' %cat_dict[cat]['colour'], \n", - " label=r'$\\sigma(\\xi_-)$ CosmoCov (SP) %s' %cat_dict[cat]['label'])\n", - " plt.loglog(cat_ggs[idx].meanr,cc_varxim_g, ls=':', c='%s' %cat_dict[cat]['colour'], \n", - " label=r'$\\sigma(\\xi_-)$ CosmoCov (SP) Gaussian %s' %cat_dict[cat]['label'])\n", + " plt.loglog(\n", + " cat_ggs[idx].meanr,\n", + " cat_ggs[idx].varxim,\n", + " \"-k\",\n", + " label=r\"$\\sigma(\\xi_-)$ TreeCorr jackknife %s\" % cat_dict[cat][\"label\"],\n", + " )\n", + " plt.loglog(\n", + " cat_ggs[idx].meanr,\n", + " cc_varxim,\n", + " ls=\"--\",\n", + " c=\"%s\" % cat_dict[cat][\"colour\"],\n", + " label=r\"$\\sigma(\\xi_-)$ CosmoCov (SP) %s\" % cat_dict[cat][\"label\"],\n", + " )\n", + " plt.loglog(\n", + " cat_ggs[idx].meanr,\n", + " cc_varxim_g,\n", + " ls=\":\",\n", + " c=\"%s\" % cat_dict[cat][\"colour\"],\n", + " label=r\"$\\sigma(\\xi_-)$ CosmoCov (SP) Gaussian %s\" % cat_dict[cat][\"label\"],\n", + " )\n", " plt.grid()\n", - " plt.xlim([cat_ggs[idx].meanr[0],cat_ggs[idx].meanr[-1]])\n", + " plt.xlim([cat_ggs[idx].meanr[0], cat_ggs[idx].meanr[-1]])\n", " plt.legend(fontsize=15)\n", - " plt.xlabel(rf'$\\theta$ [{sep_units}]')\n", - " plt.ylabel(r'$\\sigma(\\xi_-)$')\n", + " plt.xlabel(rf\"$\\theta$ [{sep_units}]\")\n", + " plt.ylabel(r\"$\\sigma(\\xi_-)$\")\n", " plt.show()\n", - " # plt.savefig()\n", - "\n" + " # plt.savefig()" ] }, { @@ -599,55 +754,55 @@ "metadata": {}, "outputs": [], "source": [ - "#CALCULATE XI_SYS FOR SHAPEPIPE\n", + "# CALCULATE XI_SYS FOR SHAPEPIPE\n", "\n", - "sep_units = 'arcmin'\n", + "sep_units = \"arcmin\"\n", "theta_min = 1\n", "theta_max = 200\n", "\n", "TreeCorrConfig = {\n", - " 'ra_units': 'degrees',\n", - " 'dec_units': 'degrees',\n", - " 'max_sep': str(theta_max),\n", - " 'min_sep': str(theta_min),\n", - " 'sep_units': sep_units,\n", - " 'nbins': 20,\n", - " 'var_method':'jackknife',\n", - " }\n", - "\n", - "with fits.open(cat_dict[11]['dir']) as data:\n", + " \"ra_units\": \"degrees\",\n", + " \"dec_units\": \"degrees\",\n", + " \"max_sep\": str(theta_max),\n", + " \"min_sep\": str(theta_min),\n", + " \"sep_units\": sep_units,\n", + " \"nbins\": 20,\n", + " \"var_method\": \"jackknife\",\n", + "}\n", + "\n", + "with fits.open(cat_dict[11][\"dir\"]) as data:\n", " df_psf = pd.DataFrame(data[1].data)\n", - " \n", + "\n", "cat_psf = treecorr.Catalog(\n", - " ra=df_psf['RA'],\n", - " dec=df_psf['DEC'],\n", - " g1=df_psf['E1_PSF_HSM'],\n", - " g2=df_psf['E2_PSF_HSM'],\n", - " ra_units='degrees',\n", - " dec_units='degrees',\n", - " npatch=50\n", + " ra=df_psf[\"RA\"],\n", + " dec=df_psf[\"DEC\"],\n", + " g1=df_psf[\"E1_PSF_HSM\"],\n", + " g2=df_psf[\"E2_PSF_HSM\"],\n", + " ra_units=\"degrees\",\n", + " dec_units=\"degrees\",\n", + " npatch=50,\n", ")\n", "\n", "gg_psf = treecorr.GGCorrelation(TreeCorrConfig)\n", "gg_psf.process(cat_psf)\n", "\n", "ggs_psf_star = []\n", - "for idx,cat in enumerate(cat_options):\n", + "for idx, cat in enumerate(cat_options):\n", " cat_gal = treecorr.Catalog(\n", - " ra=dfs[idx]['ra'],\n", - " dec=dfs[idx]['dec'],\n", - " g1=dfs[idx]['e1']-cat_dict[cat]['e1_bias'],\n", - " g2=dfs[idx]['e2']-cat_dict[cat]['e2_bias'],\n", - " w=dfs[idx]['w'],\n", - " ra_units='degrees',\n", - " dec_units='degrees',\n", - " npatch=50\n", + " ra=dfs[idx][\"ra\"],\n", + " dec=dfs[idx][\"dec\"],\n", + " g1=dfs[idx][\"e1\"] - cat_dict[cat][\"e1_bias\"],\n", + " g2=dfs[idx][\"e2\"] - cat_dict[cat][\"e2_bias\"],\n", + " w=dfs[idx][\"w\"],\n", + " ra_units=\"degrees\",\n", + " dec_units=\"degrees\",\n", + " npatch=50,\n", " )\n", " gg_psf_star = treecorr.GGCorrelation(TreeCorrConfig)\n", - " gg_psf_star.process(cat_gal,cat_psf)\n", - " ggs_psf_star.append(gg_psf_star) \n", + " gg_psf_star.process(cat_gal, cat_psf)\n", + " ggs_psf_star.append(gg_psf_star)\n", "\n", - " print(\"done for cat %s\" %cat)" + " print(\"done for cat %s\" % cat)" ] }, { @@ -657,25 +812,29 @@ "metadata": {}, "outputs": [], "source": [ - "for idx,cat in enumerate(cat_options):\n", - " \n", + "for idx, cat in enumerate(cat_options):\n", " C_sys_xip = gg_psf.xip\n", " C_sys_xim = gg_psf.xim\n", "\n", " # delta_C_sys_xip = C_sys_xip*np.sqrt((2*np.sqrt(ggs_psf_star[idx].varxip)/ggs_psf_star[idx].xip)**2+(np.sqrt(gg_psf.varxip)/gg_psf.xip)**2)\n", " # delta_C_sys_xim = C_sys_xim*np.sqrt((2*np.sqrt(ggs_psf_star[idx].varxim)/ggs_psf_star[idx].xim)**2+(np.sqrt(gg_psf.varxim)/gg_psf.xim)**2)\n", - " \n", - " plt.errorbar(ggs_psf_star[idx].meanr, C_sys_xip, yerr=0, \n", - " label=r'$(\\xi^{sys}_+)$ Catalogue %s'%cat_dict[cat]['label'],\n", - " ls=cat_dict[cat]['ls'],color=cat_dict[cat]['colour'])\n", + "\n", + " plt.errorbar(\n", + " ggs_psf_star[idx].meanr,\n", + " C_sys_xip,\n", + " yerr=0,\n", + " label=r\"$(\\xi^{sys}_+)$ Catalogue %s\" % cat_dict[cat][\"label\"],\n", + " ls=cat_dict[cat][\"ls\"],\n", + " color=cat_dict[cat][\"colour\"],\n", + " )\n", " plt.legend()\n", - " plt.xlabel(r'$\\theta[arcmin]$')\n", - " plt.ylabel(r'$\\xi^{sys}_\\pm$')\n", + " plt.xlabel(r\"$\\theta[arcmin]$\")\n", + " plt.ylabel(r\"$\\xi^{sys}_\\pm$\")\n", " # plt.ylim([-2e-8,2e-8])\n", - " plt.xscale('log')\n", - " plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))\n", + " plt.xscale(\"log\")\n", + " plt.ticklabel_format(style=\"sci\", axis=\"y\", scilimits=(0, 0))\n", " plt.grid(True)\n", - " \n", + "\n", " # plt.errorbar(ggs_psf_star[idx].meanr, C_sys_xim, yerr=delta_C_sys_xim, label=r'$(\\xi^{sys}_-)$ Catalogue %s'%cat_dict[cat]['label'],color='g')\n", " # plt.legend()\n", " # plt.xlabel(r'$\\theta[arcmin]$')\n", @@ -704,31 +863,63 @@ "metadata": {}, "outputs": [], "source": [ - "for idx,cat in enumerate(cat_options):\n", + "for idx, cat in enumerate(cat_options):\n", " R = cat_ggs[idx].rnom\n", "\n", - " (Map_lf,mapsq_im_lf,Mx_lf,mxsq_im_lf,varMapsq_lf) = cat_ggs[idx].calculateMapSq(R=R,m2_uform='Schneider')\n", - " (Map_sp,mapsq_im_sp,Mx_sp,mxsq_im_sp,varMapsq_sp) = cat_ggs[idx].calculateMapSq(R=R,m2_uform='Schneider')\n", - " \n", - " plt.errorbar(R, Map_lf, yerr=np.sqrt(varMapsq_lf), label=r'$$ {}'.format(cat_dict[cat]['label']),ls=':',color='b')\n", - " plt.errorbar(R, Mx_lf, yerr=np.sqrt(varMapsq_lf), label=r'$$ {}'.format(cat_dict[cat]['label']),ls=':',color='r')\n", - " plt.axhline(y=0,xmin=0,xmax=200,color='k')\n", - " plt.xlabel(r'$\\theta[arcmin]$')\n", - " plt.ylabel(r'$$')\n", - " plt.xscale('log')\n", - " plt.ylim([-2e-5,1e-5])\n", + " (Map_lf, mapsq_im_lf, Mx_lf, mxsq_im_lf, varMapsq_lf) = cat_ggs[idx].calculateMapSq(\n", + " R=R, m2_uform=\"Schneider\"\n", + " )\n", + " (Map_sp, mapsq_im_sp, Mx_sp, mxsq_im_sp, varMapsq_sp) = cat_ggs[idx].calculateMapSq(\n", + " R=R, m2_uform=\"Schneider\"\n", + " )\n", + "\n", + " plt.errorbar(\n", + " R,\n", + " Map_lf,\n", + " yerr=np.sqrt(varMapsq_lf),\n", + " label=r\"$$ {}\".format(cat_dict[cat][\"label\"]),\n", + " ls=\":\",\n", + " color=\"b\",\n", + " )\n", + " plt.errorbar(\n", + " R,\n", + " Mx_lf,\n", + " yerr=np.sqrt(varMapsq_lf),\n", + " label=r\"$$ {}\".format(cat_dict[cat][\"label\"]),\n", + " ls=\":\",\n", + " color=\"r\",\n", + " )\n", + " plt.axhline(y=0, xmin=0, xmax=200, color=\"k\")\n", + " plt.xlabel(r\"$\\theta[arcmin]$\")\n", + " plt.ylabel(r\"$$\")\n", + " plt.xscale(\"log\")\n", + " plt.ylim([-2e-5, 1e-5])\n", " # plt.xlim([1,200])\n", " plt.grid(True)\n", " plt.legend()\n", - " \n", - " plt.errorbar(R, Map_sp, yerr=np.sqrt(varMapsq_sp), label=r'$$ {}'.format(cat_dict[cat]['label']),ls=':',color='b')\n", - " plt.errorbar(R, Mx_sp, yerr=np.sqrt(varMapsq_sp), label=r'$$ {}'.format(cat_dict[cat]['label']),ls=':',color='r')\n", - " plt.axhline(y=0,xmin=0,xmax=200,color='k')\n", - " plt.xscale('log')\n", - " plt.xlabel(r'$\\theta[arcmin]$')\n", - " plt.ylabel(r'$$')\n", - " plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))\n", - " plt.ylim([-2e-5,1e-5])\n", + "\n", + " plt.errorbar(\n", + " R,\n", + " Map_sp,\n", + " yerr=np.sqrt(varMapsq_sp),\n", + " label=r\"$$ {}\".format(cat_dict[cat][\"label\"]),\n", + " ls=\":\",\n", + " color=\"b\",\n", + " )\n", + " plt.errorbar(\n", + " R,\n", + " Mx_sp,\n", + " yerr=np.sqrt(varMapsq_sp),\n", + " label=r\"$$ {}\".format(cat_dict[cat][\"label\"]),\n", + " ls=\":\",\n", + " color=\"r\",\n", + " )\n", + " plt.axhline(y=0, xmin=0, xmax=200, color=\"k\")\n", + " plt.xscale(\"log\")\n", + " plt.xlabel(r\"$\\theta[arcmin]$\")\n", + " plt.ylabel(r\"$$\")\n", + " plt.ticklabel_format(style=\"sci\", axis=\"y\", scilimits=(0, 0))\n", + " plt.ylim([-2e-5, 1e-5])\n", " plt.grid(True)\n", " plt.legend()" ] @@ -760,34 +951,35 @@ "\n", "%matplotlib inline\n", "\n", + "\n", "def get_cov(filename):\n", "\n", - "\tdata = np.loadtxt(filename)\n", - "\tndata = int(np.max(data[:,0]))+1\n", + " data = np.loadtxt(filename)\n", + " ndata = int(np.max(data[:, 0])) + 1\n", "\n", - "\tprint(\"Dimension of cov: %dx%d\"%(ndata,ndata))\n", + " print(\"Dimension of cov: %dx%d\" % (ndata, ndata))\n", "\n", - "\t# ndata_min = int(np.min(data[:,0]))\n", - "\tcov_g = np.zeros((ndata,ndata))\n", - "\tcov_ng = np.zeros((ndata,ndata))\n", - "\tfor i in range(0,data.shape[0]):\n", - "\t\tcov_g[int(data[i,0]),int(data[i,1])] =data[i,8]\n", - "\t\tcov_g[int(data[i,1]),int(data[i,0])] =data[i,8]\n", - "\t\tcov_ng[int(data[i,0]),int(data[i,1])] =data[i,9]\n", - "\t\tcov_ng[int(data[i,1]),int(data[i,0])] =data[i,9]\n", + " # ndata_min = int(np.min(data[:,0]))\n", + " cov_g = np.zeros((ndata, ndata))\n", + " cov_ng = np.zeros((ndata, ndata))\n", + " for i in range(0, data.shape[0]):\n", + " cov_g[int(data[i, 0]), int(data[i, 1])] = data[i, 8]\n", + " cov_g[int(data[i, 1]), int(data[i, 0])] = data[i, 8]\n", + " cov_ng[int(data[i, 0]), int(data[i, 1])] = data[i, 9]\n", + " cov_ng[int(data[i, 1]), int(data[i, 0])] = data[i, 9]\n", "\n", - "\treturn cov_g, cov_ng, ndata\n", + " return cov_g, cov_ng, ndata\n", "\n", "\n", - "covfile = '/feynman/work/dap/lcs/lg268561/UNIONS/CFIS-UNIONS/CFIS-UNIONS_dev/cosmo_inference/data/SP_cut_Fabian/covs/out_cov_ssss_+-_cov_Ntheta20_Ntomo1_3'\n", + "covfile = \"/feynman/work/dap/lcs/lg268561/UNIONS/CFIS-UNIONS/CFIS-UNIONS_dev/cosmo_inference/data/SP_cut_Fabian/covs/out_cov_ssss_+-_cov_Ntheta20_Ntomo1_3\"\n", "\n", - "c_g, c_ng, ndata = get_cov(covfile)\t\n", - "cov = c_ng+c_g\n", + "c_g, c_ng, ndata = get_cov(covfile)\n", + "cov = c_ng + c_g\n", "cov_g = c_g\n", "\n", "b = np.sort(LA.eigvals(cov))\n", - "print(\"min+max eigenvalues cov: %e, %e\"%(np.min(b), np.max(b)))\n", - "if(np.min(b)<=0.):\n", + "print(\"min+max eigenvalues cov: %e, %e\" % (np.min(b), np.max(b)))\n", + "if np.min(b) <= 0.0:\n", " print(\"non-positive eigenvalue encountered! Covariance Invalid!\")\n", " exit()\n", "\n", @@ -795,16 +987,15 @@ "\n", "pp_var = []\n", "for i in range(ndata):\n", - "\n", " pp_var.append(cov[i][i])\n", - " \n", - " \n", - "cmap = 'seismic'\n", "\n", - "pp_norm = np.zeros((ndata,ndata))\n", + "\n", + "cmap = \"seismic\"\n", + "\n", + "pp_norm = np.zeros((ndata, ndata))\n", "for i in range(ndata):\n", " for j in range(ndata):\n", - " pp_norm[i][j] = cov[i][j]/ np.sqrt(cov[i][i]*cov[j][j])\n", + " pp_norm[i][j] = cov[i][j] / np.sqrt(cov[i][i] * cov[j][j])\n", "\n", "print(\"Plotting correlation matrix ...\")\n", "\n", @@ -817,29 +1008,28 @@ "ax.yaxis.set_ticks(np.arange(0, 41, 1))\n", "\n", "\n", - "plt.axvline(x=19.5,color='black',linewidth=1.5)\n", - "plt.axhline(y=19.5,color='black',linewidth=1.5)\n", + "plt.axvline(x=19.5, color=\"black\", linewidth=1.5)\n", + "plt.axhline(y=19.5, color=\"black\", linewidth=1.5)\n", "\n", "\n", "im3 = ax.imshow(pp_norm, cmap=cmap, vmin=-1, vmax=1)\n", "ax.get_xaxis().set_ticklabels([])\n", "ax.get_yaxis().set_ticklabels([])\n", - "cbar = fig.colorbar(im3, orientation='vertical',shrink=0.6,ticks=[-1, 0, 1])\n", + "cbar = fig.colorbar(im3, orientation=\"vertical\", shrink=0.6, ticks=[-1, 0, 1])\n", "cbar.ax.tick_params(labelsize=15)\n", - "cbar.ax.set_yticklabels([r'$-1$', r'$0$', r'$1$']) \n", + "cbar.ax.set_yticklabels([r\"$-1$\", r\"$0$\", r\"$1$\"])\n", "\n", - "ax.text(8, -2, r'$\\xi_+^{ij}(\\theta)$', fontsize=22)\n", - "ax.text(30, -2, r'$\\xi_-^{ij}(\\theta)$', fontsize=22)\n", - "ax.text(-6, 10, r'$\\xi_+^{ij}(\\theta)$', fontsize=22)\n", - "ax.text(-6, 30, r'$\\xi_-^{ij}(\\theta)$', fontsize=22)\n", + "ax.text(8, -2, r\"$\\xi_+^{ij}(\\theta)$\", fontsize=22)\n", + "ax.text(30, -2, r\"$\\xi_-^{ij}(\\theta)$\", fontsize=22)\n", + "ax.text(-6, 10, r\"$\\xi_+^{ij}(\\theta)$\", fontsize=22)\n", + "ax.text(-6, 30, r\"$\\xi_-^{ij}(\\theta)$\", fontsize=22)\n", "# ax.set_title('Blind A',fontsize=15)\n", "\n", - "plt.savefig('../plots/unions_covmat.pdf', bbox_inches='tight')\n", - "\n", + "plt.savefig(\"../plots/unions_covmat.pdf\", bbox_inches=\"tight\")\n", "\n", "\n", "plt.show()\n", - "# print(\"Plot saved as %s\"%(plot_path))\n" + "# print(\"Plot saved as %s\"%(plot_path))" ] }, { diff --git a/cosmo_inference/notebooks/cfis_mcmc.ipynb b/cosmo_inference/notebooks/cfis_mcmc.ipynb index 736d56fe..124eaf99 100644 --- a/cosmo_inference/notebooks/cfis_mcmc.ipynb +++ b/cosmo_inference/notebooks/cfis_mcmc.ipynb @@ -7,23 +7,24 @@ "metadata": {}, "outputs": [], "source": [ - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "from getdist import plots\n", + "\n", "# import uncertainties\n", "\n", - "plt.rc('mathtext', fontset='stix')\n", - "plt.rc('font', family='sans-serif')\n", + "plt.rc(\"mathtext\", fontset=\"stix\")\n", + "plt.rc(\"font\", family=\"sans-serif\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", - "root_dir='/n09data/guerrini/output_chains/'\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "root_dir = \"/n09data/guerrini/output_chains/\"\n", "\n", "\"\"\" lower_bound = ['3.0', '3.0', '3.0', '3.0', '3.0', '10.0', '10.0']\n", "upper_bound = ['200.0', '150.0', '100.0', '80.0', '60.0', '150.0', '60.0']\n", @@ -34,20 +35,20 @@ "roots = [\n", " \"SP_v1.4.5_glass_mock_1\",\n", " \"SP_v1.4.5_glass_mock_1_takahashi\",\n", - " \"SP_v1.4.5_glass_mock_1_HM_code\"\n", + " \"SP_v1.4.5_glass_mock_1_HM_code\",\n", "]\n", "\n", "roots = [\n", " \"SP_v1.4.5_A\",\n", - " #\"SP_v1.4.5_A_no_IA\",\n", - " #\"SP_v1.4.5_A_no_dz\",\n", - " #\"SP_v1.4.5_A_no_m_bias\",\n", + " # \"SP_v1.4.5_A_no_IA\",\n", + " # \"SP_v1.4.5_A_no_dz\",\n", + " # \"SP_v1.4.5_A_no_m_bias\",\n", " \"SP_v1.4.5_A_sc_3_150\",\n", " \"SP_v1.4.5_A_sc_3_60\",\n", - " #\"SP_v1.4.5_A_sc_10_150\",\n", - " #\"SP_v1.4.5_A_sc_10_60\",\n", - " #\"SP_v1.4.5_A_sc_5_150\",\n", - " #\"SP_v1.4.5_A_sc_7_150\",\n", + " # \"SP_v1.4.5_A_sc_10_150\",\n", + " # \"SP_v1.4.5_A_sc_10_60\",\n", + " # \"SP_v1.4.5_A_sc_5_150\",\n", + " # \"SP_v1.4.5_A_sc_7_150\",\n", " \"SP_v1.4.5_A_no_leakage\",\n", " \"SP_v1.4.5_A_no_leakage_150\",\n", " \"SP_v1.4.5_A_no_leakage_60\",\n", @@ -64,20 +65,21 @@ "\n", "\n", "roots = [\n", - " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\" for i in [3, 5, 7, 10, 11]\n", + " f\"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_{int(i)}.0_80.0_10.0_80.0\"\n", + " for i in [3, 5, 7, 10, 11]\n", "]\n", "\n", "roots = [\n", " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0\",\n", - " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0_no_alpha_beta\"\n", + " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0_no_alpha_beta\",\n", "]\n", "\n", "roots = [\n", " \"SP_v1.4.5_leak_corr_A_minsep=1_maxsep=250_nbins=20_npatch=1_sc_10.0_80.0_10.0_80.0\",\n", - " \"SP_v1.4.5_leak_corr_cell\"\n", + " \"SP_v1.4.5_leak_corr_cell\",\n", "]\n", "\n", - "print(roots)\n" + "print(roots)" ] }, { @@ -98,16 +100,18 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/samples_{}.txt'.format('/'+root ,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(root_dir + \"{}/samples_{}.txt\".format(\"/\" + root, root), \"r\") as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/getdist_{}.paramnames'.format('/'+root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/getdist_{}.paramnames\".format(\"/\" + root, root), \"w\"\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -118,25 +122,26 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root,root))\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -148,14 +153,35 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']# ,'alpha','beta']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1']#, '\\\\alpha_{PSF}', '\\\\beta_{PSF}']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "] # ,'alpha','beta']\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + "] # , '\\\\alpha_{PSF}', '\\\\beta_{PSF}']\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " for name, label in zip(name_list, label_list):\n", - " param_names.parWithName(name).label = label\n", - " " + " param_names.parWithName(name).label = label" ] }, { @@ -176,30 +202,24 @@ "%matplotlib inline\n", "\n", "\"\"\" legend_labels = [\n", - " rf'$\\theta \\in$ [{lc}-{hc}]' for lc, hc in zip(lower_bound, upper_bound)\n", + " rf'$\\theta \\\\in$ [{lc}-{hc}]' for lc, hc in zip(lower_bound, upper_bound)\n", "] \"\"\"\n", "\n", - "legend_labels = [\n", - " rf\"GLASS mock {i}\" for i in range(1, 17)\n", - "]\n", + "legend_labels = [rf\"GLASS mock {i}\" for i in range(1, 17)]\n", "\n", - "legend_labels = [\n", - " \"GLASS mock 1\",\n", - " \"GLASS mock 1 takahashi\",\n", - " \"GLASS mock 1 HM code\"\n", - "]\n", + "legend_labels = [\"GLASS mock 1\", \"GLASS mock 1 takahashi\", \"GLASS mock 1 HM code\"]\n", "\n", "legend_labels = [\n", " \"SP_v1.4.5 blind A\",\n", - " #\"SP_v1.4.5 blind A no IA\",\n", - " #r\"SP_v1.4.5 blind A no $\\Delta z$\",\n", - " #r\"SP_v1.4.5 blind A no $m_1$\",\n", + " # \"SP_v1.4.5 blind A no IA\",\n", + " # r\"SP_v1.4.5 blind A no $\\Delta z$\",\n", + " # r\"SP_v1.4.5 blind A no $m_1$\",\n", " r\"SP_v1.4.5 blind A, $\\theta \\in [3-150]$\",\n", " r\"SP_v1.4.5 blind A, $\\theta \\in [3-60]$\",\n", - " #r\"SP_v1.4.5 blind A, $\\theta \\in [10-150]$\",\n", - " #r\"SP_v1.4.5 blind A, $\\theta \\in [10-60]$\",\n", - " #r\"SP_v1.4.5 blind A, $\\theta \\in [5-150]$\",\n", - " #r\"SP_v1.4.5 blind A, $\\theta \\in [7-150]$\",\n", + " # r\"SP_v1.4.5 blind A, $\\theta \\in [10-150]$\",\n", + " # r\"SP_v1.4.5 blind A, $\\theta \\in [10-60]$\",\n", + " # r\"SP_v1.4.5 blind A, $\\theta \\in [5-150]$\",\n", + " # r\"SP_v1.4.5 blind A, $\\theta \\in [7-150]$\",\n", " r\"SP_v1.4.5 blind A no leakage\",\n", " r\"SP_v1.4.5 blind A no leakage, $\\theta \\in [3-150]$\",\n", " r\"SP_v1.4.5 blind A no leakage, $\\theta \\in [3-60]$\",\n", @@ -219,7 +239,22 @@ "]\n", "\n", "contour_colors = [\n", - " 'cornflowerblue', 'salmon', 'darkorange', 'forestgreen', 'turquoise', 'darkviolet', 'crimson', 'gold', 'lightcoral', 'mediumseagreen', 'lightsteelblue', 'black', 'silver', 'peru', 'maroon', 'olive'\n", + " \"cornflowerblue\",\n", + " \"salmon\",\n", + " \"darkorange\",\n", + " \"forestgreen\",\n", + " \"turquoise\",\n", + " \"darkviolet\",\n", + " \"crimson\",\n", + " \"gold\",\n", + " \"lightcoral\",\n", + " \"mediumseagreen\",\n", + " \"lightsteelblue\",\n", + " \"black\",\n", + " \"silver\",\n", + " \"peru\",\n", + " \"maroon\",\n", + " \"olive\",\n", "]\n", "\n", "\"\"\" legend_labels = [\n", @@ -229,42 +264,54 @@ "] \"\"\"\n", "\n", "marker = {\n", - " 'OMEGA_LAMBDA': 0.7013160542257656,\n", - " 'ombh2': 0.024499999999999997,\n", - " 'omch2': 0.12249999999999998,\n", - " 'h0': 0.70,\n", - " 'n_s': 0.96,\n", - " 'SIGMA_8': 0.793897,\n", - " 's_8_input': 0.79563645,\n", - " 'm1': 0.0,\n", - " 'bias_1': 0.0,\n", + " \"OMEGA_LAMBDA\": 0.7013160542257656,\n", + " \"ombh2\": 0.024499999999999997,\n", + " \"omch2\": 0.12249999999999998,\n", + " \"h0\": 0.70,\n", + " \"n_s\": 0.96,\n", + " \"SIGMA_8\": 0.793897,\n", + " \"s_8_input\": 0.79563645,\n", + " \"m1\": 0.0,\n", + " \"bias_1\": 0.0,\n", " #'alpha': -0.0005,\n", " #'beta': 0.0631,\n", - " 'a': 0.0\n", + " \"a\": 0.0,\n", "}\n", "\n", "marker = {\n", - " 'bias_1': -0.045,\n", - " 'm1': 0.0,\n", - " 'a': 0.5,\n", + " \"bias_1\": -0.045,\n", + " \"m1\": 0.0,\n", + " \"a\": 0.5,\n", " #'alpha': 0.0169,\n", " #'beta': 1.0789\n", "}\n", - "g.triangle_plot(chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1','alpha','beta'],\n", - " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", - " # param_limits={'bias_1':[-0.8,0.5]},\n", - " contour_colors=contour_colors,\n", - " line_args=[{\n", - " 'color': contour_colors[i],\n", - " 'ls': 'solid'\n", - " } for i in range(16)],\n", - " # title_limit=1,\n", - " filled=True,\n", - " markers=marker)\n", - "\n", - "g.export('contour_plot_unions_cell.png')" + "g.triangle_plot(\n", + " chains,\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + " ],\n", + " legend_labels=legend_labels,\n", + " legend_loc=\"upper right\",\n", + " # param_limits={'bias_1':[-0.8,0.5]},\n", + " contour_colors=contour_colors,\n", + " line_args=[{\"color\": contour_colors[i], \"ls\": \"solid\"} for i in range(16)],\n", + " # title_limit=1,\n", + " filled=True,\n", + " markers=marker,\n", + ")\n", + "\n", + "g.export(\"contour_plot_unions_cell.png\")" ] }, { @@ -275,23 +322,22 @@ "outputs": [], "source": [ "\"\"\" legend_labels = [\n", - " rf'$\\theta \\in$ [{lc}-{hc}]' for lc, hc in zip(lower_bound, upper_bound)\n", + " rf'$\\theta \\\\in$ [{lc}-{hc}]' for lc, hc in zip(lower_bound, upper_bound)\n", "] \"\"\"\n", - "g.triangle_plot(chains,\n", - " ['OMEGA_M', 's_8_input', 'SIGMA_8', 'a'],\n", - " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", - " # param_limits={'bias_1':[-0.8,0.5]},\n", - " contour_colors=contour_colors,\n", - " line_args=[{\n", - " 'color': contour_colors[i],\n", - " 'ls': 'solid'\n", - " } for i in range(16)],\n", - " title_limit=1,\n", - " filled=True,\n", - " markers=marker)\n", - "\n", - "g.export('contour_plot_s8_unions_cell.png')" + "g.triangle_plot(\n", + " chains,\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", + " legend_labels=legend_labels,\n", + " legend_loc=\"upper right\",\n", + " # param_limits={'bias_1':[-0.8,0.5]},\n", + " contour_colors=contour_colors,\n", + " line_args=[{\"color\": contour_colors[i], \"ls\": \"solid\"} for i in range(16)],\n", + " title_limit=1,\n", + " filled=True,\n", + " markers=marker,\n", + ")\n", + "\n", + "g.export(\"contour_plot_s8_unions_cell.png\")" ] }, { @@ -313,32 +359,67 @@ "outputs": [], "source": [ "#########BESTFIT AND SIGMA VALS##########\n", - "params = ['OMEGA_M','omega_b','h0','n_s','a_s','SIGMA_8','S_8','logt_agn','a','m1', 'bias_1', 'alpha', 'beta', 'omch2', 'ombh2']\n", - "latex_params = [r'$\\Omega_{\\rm m,0}$',r'$\\Omega_{\\rm b,0}$',r'$h$',r'$n_{\\rm s}$',r'$A_{\\rm s}$',r'$\\sigma_8$',r'$S_8$',\n", - " r'$\\log_{10}{T_{\\rm AGN}}$',r'$\\mathcal{A}_rm IA}$', r'$m_1$', r'$\\Delta z$', r'$\\alpha$', r'$\\beta$', r'$\\Omega_{\\rm c,0}$', r'$\\Omega_{\\rm b,0}$']\n", + "params = [\n", + " \"OMEGA_M\",\n", + " \"omega_b\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"a_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + " \"omch2\",\n", + " \"ombh2\",\n", + "]\n", + "latex_params = [\n", + " r\"$\\Omega_{\\rm m,0}$\",\n", + " r\"$\\Omega_{\\rm b,0}$\",\n", + " r\"$h$\",\n", + " r\"$n_{\\rm s}$\",\n", + " r\"$A_{\\rm s}$\",\n", + " r\"$\\sigma_8$\",\n", + " r\"$S_8$\",\n", + " r\"$\\log_{10}{T_{\\rm AGN}}$\",\n", + " r\"$\\mathcal{A}_rm IA}$\",\n", + " r\"$m_1$\",\n", + " r\"$\\Delta z$\",\n", + " r\"$\\alpha$\",\n", + " r\"$\\beta$\",\n", + " r\"$\\Omega_{\\rm c,0}$\",\n", + " r\"$\\Omega_{\\rm b,0}$\",\n", + "]\n", "\n", "for chain in chains:\n", - "\n", - " \n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", - " p=chain.getParams()\n", + " p = chain.getParams()\n", "\n", " for no in range(len(latex_params)):\n", - " if hasattr(p,params[no]):\n", + " if hasattr(p, params[no]):\n", " param_stats = margestats.parWithName(params[no])\n", - " a = np.array([param_stats.mean,param_stats.mean-param_stats.limits[0].lower, param_stats.limits[0].upper-param_stats.mean])\n", - " if '%.2g' %a[1] == '%.2g' %a[2]:\n", - " latex_params[no] += '&$%.3g\\pm%.2g$'%(a[0],a[1])\n", + " a = np.array(\n", + " [\n", + " param_stats.mean,\n", + " param_stats.mean - param_stats.limits[0].lower,\n", + " param_stats.limits[0].upper - param_stats.mean,\n", + " ]\n", + " )\n", + " if \"%.2g\" % a[1] == \"%.2g\" % a[2]:\n", + " latex_params[no] += r\"&$%.3g\\pm%.2g$\" % (a[0], a[1])\n", " else:\n", - " latex_params[no] += '&$%.3g_{-%.2g}^{+%.2g}$'%(a[0],a[1],a[2])\n", + " latex_params[no] += \"&$%.3g_{-%.2g}^{+%.2g}$\" % (a[0], a[1], a[2])\n", " else:\n", - " latex_params[no] += '&$-$'\n", + " latex_params[no] += \"&$-$\"\n", + "\n", "\n", - " \n", "for param in latex_params:\n", - " param += r'\\\\'\n", - " print(param) \n" + " param += r\"\\\\\"\n", + " print(param)" ] }, { @@ -352,13 +433,13 @@ "\n", "margestats = chain.getMargeStats()\n", "likestats = chain.getLikeStats()\n", - "p=chain.getParams()\n", + "p = chain.getParams()\n", "\n", "for no in range(len(latex_params)):\n", - " if hasattr(p,params[no]):\n", - " param_stats = margestats.parWithName(params[no])\n", - " a = np.array([param_stats.mean])\n", - " print(params[no], a[0])" + " if hasattr(p, params[no]):\n", + " param_stats = margestats.parWithName(params[no])\n", + " a = np.array([param_stats.mean])\n", + " print(params[no], a[0])" ] }, { @@ -369,28 +450,61 @@ "outputs": [], "source": [ "#########BESTFIT AND SIGMA VALS##########\n", - "params = ['OMEGA_M','omega_b','h0','n_s','a_s','SIGMA_8','S_8','logt_agn','a','m1', 'bias_1', 'alpha', 'beta', 'omch2', 'ombh2']\n", - "latex_params = [r'$\\Omega_{\\rm m,0}$',r'$\\Omega_{\\rm b,0}$',r'$h$',r'$n_{\\rm s}$',r'$A_{\\rm s}$',r'$\\sigma_8$',r'$S_8$',\n", - " r'$\\log_{10}{T_{\\rm AGN}}$',r'$\\mathcal{A}_rm IA}$', r'$m_1$', r'$\\Delta z$', r'$\\alpha$', r'$\\beta$', r'$\\Omega_{\\rm c,0}$', r'$\\Omega_{\\rm b,0}$']\n", + "params = [\n", + " \"OMEGA_M\",\n", + " \"omega_b\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"a_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " \"alpha\",\n", + " \"beta\",\n", + " \"omch2\",\n", + " \"ombh2\",\n", + "]\n", + "latex_params = [\n", + " r\"$\\Omega_{\\rm m,0}$\",\n", + " r\"$\\Omega_{\\rm b,0}$\",\n", + " r\"$h$\",\n", + " r\"$n_{\\rm s}$\",\n", + " r\"$A_{\\rm s}$\",\n", + " r\"$\\sigma_8$\",\n", + " r\"$S_8$\",\n", + " r\"$\\log_{10}{T_{\\rm AGN}}$\",\n", + " r\"$\\mathcal{A}_rm IA}$\",\n", + " r\"$m_1$\",\n", + " r\"$\\Delta z$\",\n", + " r\"$\\alpha$\",\n", + " r\"$\\beta$\",\n", + " r\"$\\Omega_{\\rm c,0}$\",\n", + " r\"$\\Omega_{\\rm b,0}$\",\n", + "]\n", "\n", - "values = {\n", - " param: [] for param in params\n", - "}\n", + "values = {param: [] for param in params}\n", "for i, chain in enumerate(chains):\n", - "\n", - " \n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", - " p=chain.getParams()\n", + " p = chain.getParams()\n", "\n", " print(legend_labels[i])\n", "\n", " for param in params:\n", - " if hasattr(p,param):\n", + " if hasattr(p, param):\n", " param_stats = margestats.parWithName(param)\n", - " a = np.array([param_stats.mean,param_stats.mean-param_stats.limits[0].lower, param_stats.limits[0].upper-param_stats.mean])\n", + " a = np.array(\n", + " [\n", + " param_stats.mean,\n", + " param_stats.mean - param_stats.limits[0].lower,\n", + " param_stats.limits[0].upper - param_stats.mean,\n", + " ]\n", + " )\n", " print(f\"{param}: {a[0]:.3g}_-{a[1]:.2g}^+{a[1]:.2g}\")\n", - " values[param].append(a[0])\n" + " values[param].append(a[0])" ] }, { @@ -436,26 +550,26 @@ "\n", "plt.subplot(131)\n", "\n", - "plt.hist(values['OMEGA_M'], bins=10, color='cornflowerblue', alpha=0.5)\n", - "plt.axvline(0.301316, color='black', linestyle='--', label='True value')\n", - "plt.xlabel(r'$\\Omega_{\\rm m,0}$')\n", - "plt.ylabel('Counts')\n", + "plt.hist(values[\"OMEGA_M\"], bins=10, color=\"cornflowerblue\", alpha=0.5)\n", + "plt.axvline(0.301316, color=\"black\", linestyle=\"--\", label=\"True value\")\n", + "plt.xlabel(r\"$\\Omega_{\\rm m,0}$\")\n", + "plt.ylabel(\"Counts\")\n", "plt.legend()\n", "\n", "plt.subplot(132)\n", "\n", - "plt.hist(values['SIGMA_8'], bins=10, color='cornflowerblue', alpha=0.5)\n", - "plt.axvline(0.793897, color='black', linestyle='--', label='True value')\n", - "plt.xlabel(r'$\\sigma_8$')\n", - "plt.ylabel('Counts')\n", + "plt.hist(values[\"SIGMA_8\"], bins=10, color=\"cornflowerblue\", alpha=0.5)\n", + "plt.axvline(0.793897, color=\"black\", linestyle=\"--\", label=\"True value\")\n", + "plt.xlabel(r\"$\\sigma_8$\")\n", + "plt.ylabel(\"Counts\")\n", "plt.legend()\n", "\n", "plt.subplot(133)\n", "\n", - "plt.hist(values['S_8'], bins=10, color='cornflowerblue', alpha=0.5)\n", - "plt.axvline(0.79563645, color='black', linestyle='--', label='True value')\n", - "plt.xlabel(r'$S_8$')\n", - "plt.ylabel('Counts')\n", + "plt.hist(values[\"S_8\"], bins=10, color=\"cornflowerblue\", alpha=0.5)\n", + "plt.axvline(0.79563645, color=\"black\", linestyle=\"--\", label=\"True value\")\n", + "plt.xlabel(r\"$S_8$\")\n", + "plt.ylabel(\"Counts\")\n", "plt.legend()\n", "plt.tight_layout()\n", "\n", @@ -469,7 +583,7 @@ "metadata": {}, "outputs": [], "source": [ - "np.sum(np.abs(np.array(values['S_8']) - 0.79563645) < 0.03)/len(values['S_8'])" + "np.sum(np.abs(np.array(values[\"S_8\"]) - 0.79563645) < 0.03) / len(values[\"S_8\"])" ] }, { @@ -489,12 +603,14 @@ "source": [ "from astropy.io import fits\n", "\n", - "version = 'SP_v1.4.5_glass_mock_1'\n", + "version = \"SP_v1.4.5_glass_mock_1\"\n", "\n", - "data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{version}/cosmosis_{version}.fits')\n", - "xi_plus = data['XI_PLUS'].data\n", - "xi_minus = data['XI_MINUS'].data\n", - "cov_mat = data['COVMAT'].data" + "data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{version}/cosmosis_{version}.fits\"\n", + ")\n", + "xi_plus = data[\"XI_PLUS\"].data\n", + "xi_minus = data[\"XI_MINUS\"].data\n", + "cov_mat = data[\"COVMAT\"].data" ] }, { @@ -508,28 +624,44 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black', markersize=2)\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + ")\n", "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(100.0, color='grey', linestyle='--', label='100 arcmin')\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(100.0, color=\"grey\", linestyle=\"--\", label=\"100 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black', markersize=2)\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + ")\n", "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", - "plt.savefig('xi_data.png')\n", + "plt.savefig(\"xi_data.png\")\n", "\n", "plt.show()" ] @@ -543,8 +675,8 @@ "source": [ "import pyccl as ccl\n", "\n", - "#Get theory correlation function from CCL\n", - "#Define the cosmology\n", + "# Get theory correlation function from CCL\n", + "# Define the cosmology\n", "theta_arcmin = np.logspace(np.log10(0.1), np.log10(250), 1000)\n", "h = 0.7\n", "Oc = 0.25\n", @@ -553,28 +685,35 @@ "n_s = 0.96\n", "cosmo = ccl.Cosmology(\n", " h=h,\n", - " Omega_c = Oc,\n", - " Omega_b = Ob,\n", - " sigma8 = sigma8,\n", - " n_s = n_s,\n", - " transfer_function='boltzmann_camb',\n", + " Omega_c=Oc,\n", + " Omega_b=Ob,\n", + " sigma8=sigma8,\n", + " n_s=n_s,\n", + " transfer_function=\"boltzmann_camb\",\n", ")\n", "\n", - "#Define the redshift distribution\n", - "z, dndz = np.loadtxt('/home/guerrini/sp_validation/cosmo_inference/cosmocov_config/dndz_test.txt', unpack=True)\n", + "# Define the redshift distribution\n", + "z, dndz = np.loadtxt(\n", + " \"/home/guerrini/sp_validation/cosmo_inference/cosmocov_config/dndz_test.txt\",\n", + " unpack=True,\n", + ")\n", "\n", "tracer = ccl.WeakLensingTracer(cosmo, dndz=(z, dndz), ia_bias=None)\n", "\n", - "#COmpute the angular power spectrum C_ell\n", + "# COmpute the angular power spectrum C_ell\n", "ell = np.logspace(0, np.log10(10000), 2000)\n", "cl_gg = ccl.angular_cl(cosmo, tracer, tracer, ell)\n", "\n", - "#Compute the 2PCF\n", + "# Compute the 2PCF\n", "theta_deg = theta_arcmin / 60\n", - "#xi+ fit\n", - "xi_p_theta_true = ccl.correlation(cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type='GG+')\n", - "#xi- fit\n", - "xi_m_theta_true = ccl.correlation(cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type='GG-')" + "# xi+ fit\n", + "xi_p_theta_true = ccl.correlation(\n", + " cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type=\"GG+\"\n", + ")\n", + "# xi- fit\n", + "xi_m_theta_true = ccl.correlation(\n", + " cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type=\"GG-\"\n", + ")" ] }, { @@ -584,42 +723,51 @@ "metadata": {}, "outputs": [], "source": [ - "#Get theory correlation function from CCL\n", - "#Define the cosmology\n", + "# Get theory correlation function from CCL\n", + "# Define the cosmology\n", "theta_arcmin = np.logspace(np.log10(0.1), np.log10(250), 1000)\n", "h = 0.6982064176424748\n", - "Oc = 0.21522128974860827/h**2\n", + "Oc = 0.21522128974860827 / h**2\n", "print(\"Omega_c:\", Oc)\n", - "Ob = 0.024410205304489712/h**2\n", + "Ob = 0.024410205304489712 / h**2\n", "print(\"Omega_b:\", Ob)\n", "sigma8 = 0.5330533925822226\n", - "print(\"S8:\", sigma8*np.sqrt((Oc+Ob)/0.3))\n", + "print(\"S8:\", sigma8 * np.sqrt((Oc + Ob) / 0.3))\n", "n_s = 0.9867762122563981\n", "a_ia = 0.0\n", "cosmo = ccl.Cosmology(\n", " h=h,\n", - " Omega_c = Oc,\n", - " Omega_b = Ob,\n", - " sigma8 = sigma8,\n", - " n_s = n_s,\n", - " transfer_function='boltzmann_camb',\n", + " Omega_c=Oc,\n", + " Omega_b=Ob,\n", + " sigma8=sigma8,\n", + " n_s=n_s,\n", + " transfer_function=\"boltzmann_camb\",\n", ")\n", "\n", - "#Define the redshift distribution\n", - "z, dndz = np.loadtxt('/home/guerrini/sp_validation/cosmo_inference/cosmocov_config/dndz_test.txt', unpack=True)\n", + "# Define the redshift distribution\n", + "z, dndz = np.loadtxt(\n", + " \"/home/guerrini/sp_validation/cosmo_inference/cosmocov_config/dndz_test.txt\",\n", + " unpack=True,\n", + ")\n", "\n", - "tracer = ccl.WeakLensingTracer(cosmo, dndz=(z, dndz), ia_bias=(z, np.ones_like(z)*a_ia))\n", + "tracer = ccl.WeakLensingTracer(\n", + " cosmo, dndz=(z, dndz), ia_bias=(z, np.ones_like(z) * a_ia)\n", + ")\n", "\n", - "#COmpute the angular power spectrum C_ell\n", + "# COmpute the angular power spectrum C_ell\n", "ell = np.logspace(0, np.log10(10000), 2000)\n", "cl_gg = ccl.angular_cl(cosmo, tracer, tracer, ell)\n", "\n", - "#Compute the 2PCF\n", + "# Compute the 2PCF\n", "theta_deg = theta_arcmin / 60\n", - "#xi+ fit\n", - "xi_p_theta_fit = ccl.correlation(cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type='GG+')\n", - "#xi- fit\n", - "xi_m_theta_fit = ccl.correlation(cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type='GG-')" + "# xi+ fit\n", + "xi_p_theta_fit = ccl.correlation(\n", + " cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type=\"GG+\"\n", + ")\n", + "# xi- fit\n", + "xi_m_theta_fit = ccl.correlation(\n", + " cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type=\"GG-\"\n", + ")" ] }, { @@ -629,42 +777,51 @@ "metadata": {}, "outputs": [], "source": [ - "#Get theory correlation function from CCL\n", - "#Define the cosmology\n", + "# Get theory correlation function from CCL\n", + "# Define the cosmology\n", "theta_arcmin = np.logspace(np.log10(0.1), np.log10(250), 1000)\n", "h = 0.6982064176424748\n", - "Oc = 0.21522128974860827/h**2\n", + "Oc = 0.21522128974860827 / h**2\n", "print(\"Omega_c:\", Oc)\n", - "Ob = 0.024410205304489712/h**2\n", + "Ob = 0.024410205304489712 / h**2\n", "print(\"Omega_b:\", Ob)\n", "sigma8 = 0.5330533925822226\n", - "print(\"S8:\", sigma8*np.sqrt((Oc+Ob)/0.3))\n", + "print(\"S8:\", sigma8 * np.sqrt((Oc + Ob) / 0.3))\n", "n_s = 0.9867762122563981\n", "a_ia = -0.4225120529551343\n", "cosmo = ccl.Cosmology(\n", " h=h,\n", - " Omega_c = Oc,\n", - " Omega_b = Ob,\n", - " sigma8 = sigma8,\n", - " n_s = n_s,\n", - " transfer_function='boltzmann_camb',\n", + " Omega_c=Oc,\n", + " Omega_b=Ob,\n", + " sigma8=sigma8,\n", + " n_s=n_s,\n", + " transfer_function=\"boltzmann_camb\",\n", ")\n", "\n", - "#Define the redshift distribution\n", - "z, dndz = np.loadtxt('/home/guerrini/sp_validation/cosmo_inference/cosmocov_config/dndz_test.txt', unpack=True)\n", + "# Define the redshift distribution\n", + "z, dndz = np.loadtxt(\n", + " \"/home/guerrini/sp_validation/cosmo_inference/cosmocov_config/dndz_test.txt\",\n", + " unpack=True,\n", + ")\n", "\n", - "tracer = ccl.WeakLensingTracer(cosmo, dndz=(z, dndz), ia_bias=(z, np.ones_like(z)*a_ia))\n", + "tracer = ccl.WeakLensingTracer(\n", + " cosmo, dndz=(z, dndz), ia_bias=(z, np.ones_like(z) * a_ia)\n", + ")\n", "\n", - "#COmpute the angular power spectrum C_ell\n", + "# COmpute the angular power spectrum C_ell\n", "ell = np.logspace(0, np.log10(10000), 2000)\n", "cl_gg = ccl.angular_cl(cosmo, tracer, tracer, ell)\n", "\n", - "#Compute the 2PCF\n", + "# Compute the 2PCF\n", "theta_deg = theta_arcmin / 60\n", - "#xi+ fit\n", - "xi_p_theta_fit_IA = ccl.correlation(cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type='GG+')\n", - "#xi- fit\n", - "xi_m_theta_fit_IA = ccl.correlation(cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type='GG-')" + "# xi+ fit\n", + "xi_p_theta_fit_IA = ccl.correlation(\n", + " cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type=\"GG+\"\n", + ")\n", + "# xi- fit\n", + "xi_m_theta_fit_IA = ccl.correlation(\n", + " cosmo, ell=ell, C_ell=cl_gg, theta=theta_deg, type=\"GG-\"\n", + ")" ] }, { @@ -678,31 +835,45 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(theta_arcmin, xi_p_theta_fit, label='SP_v1.4.5 fit', color='red')\n", - "plt.plot(theta_arcmin, xi_p_theta_true, label='SP_v1.4.5 true', color='blue')\n", - "plt.plot(theta_arcmin, xi_p_theta_fit_IA, label='SP_v1.4.5 fit IA', color='green')\n", - "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(100.0, color='grey', linestyle='--', label='100 arcmin')\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(theta_arcmin, xi_p_theta_fit, label=\"SP_v1.4.5 fit\", color=\"red\")\n", + "plt.plot(theta_arcmin, xi_p_theta_true, label=\"SP_v1.4.5 true\", color=\"blue\")\n", + "plt.plot(theta_arcmin, xi_p_theta_fit_IA, label=\"SP_v1.4.5 fit IA\", color=\"green\")\n", + "\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(100.0, color=\"grey\", linestyle=\"--\", label=\"100 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(theta_arcmin, xi_m_theta_fit, label='SP_v1.4.5 fit', color='red')\n", - "plt.plot(theta_arcmin, xi_m_theta_true, label='SP_v1.4.5 true', color='blue')\n", - "plt.plot(theta_arcmin, xi_m_theta_fit_IA, label='SP_v1.4.5 fit IA', color='green')\n", - "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(theta_arcmin, xi_m_theta_fit, label=\"SP_v1.4.5 fit\", color=\"red\")\n", + "plt.plot(theta_arcmin, xi_m_theta_true, label=\"SP_v1.4.5 true\", color=\"blue\")\n", + "plt.plot(theta_arcmin, xi_m_theta_fit_IA, label=\"SP_v1.4.5 fit IA\", color=\"green\")\n", + "\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", "plt.show()" @@ -715,34 +886,42 @@ "metadata": {}, "outputs": [], "source": [ - "#Add best-fit model\n", - "root_dir = '/n09data/guerrini/output_chains/output_result/glass_mock_1/'\n", - "xi_plus_bf_no_psf = np.loadtxt(root_dir+'best_fit/shear_xi_plus/bin_1_1.txt')\n", - "xi_minus_bf_no_psf = np.loadtxt(root_dir+'best_fit/shear_xi_minus/bin_1_1.txt')\n", - "xi_sys_p = np.loadtxt(root_dir+'best_fit/xi_sys/shear_xi_plus.txt')\n", - "xi_sys_m = np.loadtxt(root_dir+'best_fit/xi_sys/shear_xi_minus.txt')\n", - "theta_xi_sys = np.loadtxt(root_dir+'best_fit/xi_sys/theta.txt')\n", - "theta_xi_sys = theta_xi_sys * 180*60/np.pi\n", - "angle = np.loadtxt(root_dir+'best_fit/shear_xi_plus/theta.txt')\n", - "angle = angle * 180*60/np.pi\n", + "# Add best-fit model\n", + "root_dir = \"/n09data/guerrini/output_chains/output_result/glass_mock_1/\"\n", + "xi_plus_bf_no_psf = np.loadtxt(root_dir + \"best_fit/shear_xi_plus/bin_1_1.txt\")\n", + "xi_minus_bf_no_psf = np.loadtxt(root_dir + \"best_fit/shear_xi_minus/bin_1_1.txt\")\n", + "xi_sys_p = np.loadtxt(root_dir + \"best_fit/xi_sys/shear_xi_plus.txt\")\n", + "xi_sys_m = np.loadtxt(root_dir + \"best_fit/xi_sys/shear_xi_minus.txt\")\n", + "theta_xi_sys = np.loadtxt(root_dir + \"best_fit/xi_sys/theta.txt\")\n", + "theta_xi_sys = theta_xi_sys * 180 * 60 / np.pi\n", + "angle = np.loadtxt(root_dir + \"best_fit/shear_xi_plus/theta.txt\")\n", + "angle = angle * 180 * 60 / np.pi\n", "\n", "mask = (angle < 250) & (angle > 0.1)\n", "\n", "from scipy.interpolate import interp1d\n", "\n", - "xi_sys_p_interp = interp1d(theta_xi_sys, xi_sys_p, kind='linear', fill_value='extrapolate')\n", - "xi_sys_m_interp = interp1d(theta_xi_sys, xi_sys_m, kind='linear', fill_value='extrapolate')\n", + "xi_sys_p_interp = interp1d(\n", + " theta_xi_sys, xi_sys_p, kind=\"linear\", fill_value=\"extrapolate\"\n", + ")\n", + "xi_sys_m_interp = interp1d(\n", + " theta_xi_sys, xi_sys_m, kind=\"linear\", fill_value=\"extrapolate\"\n", + ")\n", "xi_plus_bf = xi_plus_bf_no_psf[mask] + xi_sys_p_interp(angle[mask])\n", "xi_minus_bf = xi_minus_bf_no_psf[mask] + xi_sys_m_interp(angle[mask])\n", "\n", - "xi_plus_bf_no_baryons = np.loadtxt(root_dir+'best_fit_no_feedback/shear_xi_plus/bin_1_1.txt')\n", - "xi_minus_bf_no_baryons = np.loadtxt(root_dir+'best_fit_no_feedback/shear_xi_minus/bin_1_1.txt')\n", + "xi_plus_bf_no_baryons = np.loadtxt(\n", + " root_dir + \"best_fit_no_feedback/shear_xi_plus/bin_1_1.txt\"\n", + ")\n", + "xi_minus_bf_no_baryons = np.loadtxt(\n", + " root_dir + \"best_fit_no_feedback/shear_xi_minus/bin_1_1.txt\"\n", + ")\n", "\n", - "xi_plus_bf_no_IA = np.loadtxt(root_dir+'best_fit_no_IA/shear_xi_plus/bin_1_1.txt')\n", - "xi_minus_bf_no_IA = np.loadtxt(root_dir+'best_fit_no_IA/shear_xi_minus/bin_1_1.txt')\n", + "xi_plus_bf_no_IA = np.loadtxt(root_dir + \"best_fit_no_IA/shear_xi_plus/bin_1_1.txt\")\n", + "xi_minus_bf_no_IA = np.loadtxt(root_dir + \"best_fit_no_IA/shear_xi_minus/bin_1_1.txt\")\n", "\n", - "xi_plus_truth_cosmosis = np.loadtxt(root_dir+'/truth/shear_xi_plus/bin_1_1.txt')\n", - "xi_minus_truth_cosmosis = np.loadtxt(root_dir+'/truth/shear_xi_minus/bin_1_1.txt')" + "xi_plus_truth_cosmosis = np.loadtxt(root_dir + \"/truth/shear_xi_plus/bin_1_1.txt\")\n", + "xi_minus_truth_cosmosis = np.loadtxt(root_dir + \"/truth/shear_xi_minus/bin_1_1.txt\")" ] }, { @@ -756,37 +935,95 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black', markersize=2)\n", - "plt.plot(theta_arcmin, xi_p_theta_true, label='SP_v1.4.5 true', color='blue')\n", - "plt.plot(angle[mask], xi_plus_bf, label='SP_v1.4.5 COSMOSIS', color='orange')\n", - "plt.plot(angle[mask], xi_plus_bf_no_psf[mask], label='SP_v1.4.5 COSMOSIS no PSF', color='green')\n", - "plt.plot(angle[mask], xi_plus_bf_no_baryons[mask], label='SP_v1.4.5 COSMOSIS no baryons', color='red')\n", - "plt.plot(angle[mask], xi_plus_bf_no_IA[mask], label='SP_v1.4.5 COSMOSIS no IA', color='purple')\n", - "plt.plot(angle[mask], xi_plus_truth_cosmosis[mask], label='SP_v1.4.5 COSMOSIS truth', color='black', linestyle='--')\n", - "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(150.0, color='grey', linestyle='--', label='150 arcmin')\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + ")\n", + "plt.plot(theta_arcmin, xi_p_theta_true, label=\"SP_v1.4.5 true\", color=\"blue\")\n", + "plt.plot(angle[mask], xi_plus_bf, label=\"SP_v1.4.5 COSMOSIS\", color=\"orange\")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_plus_bf_no_psf[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS no PSF\",\n", + " color=\"green\",\n", + ")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_plus_bf_no_baryons[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS no baryons\",\n", + " color=\"red\",\n", + ")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_plus_bf_no_IA[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS no IA\",\n", + " color=\"purple\",\n", + ")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_plus_truth_cosmosis[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS truth\",\n", + " color=\"black\",\n", + " linestyle=\"--\",\n", + ")\n", + "\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(150.0, color=\"grey\", linestyle=\"--\", label=\"150 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black', markersize=2)\n", - "plt.plot(theta_arcmin, xi_m_theta_true, label='SP_v1.4.5 true', color='blue')\n", - "plt.plot(angle[mask], xi_minus_bf, label='SP_v1.4.5 COSMOSIS', color='orange')\n", - "plt.plot(angle[mask], xi_minus_bf_no_psf[mask], label='SP_v1.4.5 COSMOSIS no PSF', color='green')\n", - "plt.plot(angle[mask], xi_minus_bf_no_baryons[mask], label='SP_v1.4.5 COSMOSIS no baryons', color='red')\n", - "plt.plot(angle[mask], xi_minus_bf_no_IA[mask], label='SP_v1.4.5 COSMOSIS no IA', color='purple')\n", - "plt.plot(angle[mask], xi_minus_truth_cosmosis[mask], label='SP_v1.4.5 COSMOSIS truth', color='black', linestyle='--')\n", - "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + " markersize=2,\n", + ")\n", + "plt.plot(theta_arcmin, xi_m_theta_true, label=\"SP_v1.4.5 true\", color=\"blue\")\n", + "plt.plot(angle[mask], xi_minus_bf, label=\"SP_v1.4.5 COSMOSIS\", color=\"orange\")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_minus_bf_no_psf[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS no PSF\",\n", + " color=\"green\",\n", + ")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_minus_bf_no_baryons[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS no baryons\",\n", + " color=\"red\",\n", + ")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_minus_bf_no_IA[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS no IA\",\n", + " color=\"purple\",\n", + ")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_minus_truth_cosmosis[mask],\n", + " label=\"SP_v1.4.5 COSMOSIS truth\",\n", + " color=\"black\",\n", + " linestyle=\"--\",\n", + ")\n", + "\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", "plt.show()" @@ -804,34 +1041,37 @@ "theta_min = 0.1\n", "theta_max = 250.0\n", "nbins = 20\n", - "var_method = 'jackknife'\n", + "var_method = \"jackknife\"\n", "\n", "treecorr_config = {\n", - " \"ra_units\": \"degrees\",\n", - " \"dec_units\": \"degrees\",\n", - " \"min_sep\": theta_min,\n", - " \"max_sep\": theta_max,\n", - " \"sep_units\": \"arcmin\",\n", - " \"nbins\": nbins,\n", - " \"var_method\": var_method,\n", - " }\n", + " \"ra_units\": \"degrees\",\n", + " \"dec_units\": \"degrees\",\n", + " \"min_sep\": theta_min,\n", + " \"max_sep\": theta_max,\n", + " \"sep_units\": \"arcmin\",\n", + " \"nbins\": nbins,\n", + " \"var_method\": var_method,\n", + "}\n", "\n", "gg = treecorr.GGCorrelation(treecorr_config)\n", "\n", - "#Load the measurement\n", - "cat = fits.getdata(f'/n09data/guerrini/glass_mock/results/unions_glass_sim_00001_4096.fits')\n", + "# Load the measurement\n", + "cat = fits.getdata(\n", + " \"/n09data/guerrini/glass_mock/results/unions_glass_sim_00001_4096.fits\"\n", + ")\n", "\n", - "e1 = cat['e1']\n", - "e2 = cat['e2']\n", - "ra = cat['ra']\n", - "dec = cat['dec']\n", + "e1 = cat[\"e1\"]\n", + "e2 = cat[\"e2\"]\n", + "ra = cat[\"ra\"]\n", + "dec = cat[\"dec\"]\n", "\n", - "#Create the catalog\n", - "cat = treecorr.Catalog(ra=ra, dec=dec, ra_units='degrees', dec_units='degrees', g1=e1, g2=e2, npatch=200)\n", + "# Create the catalog\n", + "cat = treecorr.Catalog(\n", + " ra=ra, dec=dec, ra_units=\"degrees\", dec_units=\"degrees\", g1=e1, g2=e2, npatch=200\n", + ")\n", "\n", - "#Process the catalog\n", - "gg.process(cat)\n", - "\n" + "# Process the catalog\n", + "gg.process(cat)" ] }, { @@ -841,7 +1081,7 @@ "metadata": {}, "outputs": [], "source": [ - "cov = treecorr.estimate_multi_cov([gg], method='jackknife')" + "cov = treecorr.estimate_multi_cov([gg], method=\"jackknife\")" ] }, { @@ -853,11 +1093,13 @@ "source": [ "data_vector_sim = []\n", "\n", - "for ver in [f'SP_v1.4.5_glass_mock_{i}' for i in range(1, 17)]:\n", - " data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{ver}/cosmosis_{ver}.fits')\n", - " xi_plus = data['XI_PLUS'].data\n", - " xi_minus = data['XI_MINUS'].data\n", - " data_vector_sim.append(np.concatenate((xi_plus['VALUE'], xi_minus['VALUE'])))\n", + "for ver in [f\"SP_v1.4.5_glass_mock_{i}\" for i in range(1, 17)]:\n", + " data = fits.open(\n", + " f\"/home/guerrini/sp_validation/cosmo_inference/data/{ver}/cosmosis_{ver}.fits\"\n", + " )\n", + " xi_plus = data[\"XI_PLUS\"].data\n", + " xi_minus = data[\"XI_MINUS\"].data\n", + " data_vector_sim.append(np.concatenate((xi_plus[\"VALUE\"], xi_minus[\"VALUE\"])))\n", "\n", "data_vector_sim = np.array(data_vector_sim)\n", "\n", @@ -881,9 +1123,13 @@ "metadata": {}, "outputs": [], "source": [ - "ver_sacha = 'SP_v1.4.5'\n", + "ver_sacha = \"SP_v1.4.5\"\n", "\n", - "cov_th_sacha = np.loadtxt('/home/guerrini/sp_validation/cosmo_inference/data/{}/covs/cov_{}.txt'.format(ver_sacha, ver_sacha))" + "cov_th_sacha = np.loadtxt(\n", + " \"/home/guerrini/sp_validation/cosmo_inference/data/{}/covs/cov_{}.txt\".format(\n", + " ver_sacha, ver_sacha\n", + " )\n", + ")" ] }, { @@ -895,17 +1141,19 @@ "source": [ "plt.figure()\n", "\n", - "plt.plot(xi_plus['ANG'], np.diag(cov)[:20])\n", - "plt.plot(xi_plus['ANG'], np.diag(cov_sim)[:20], label='SP_v1.4.5 data', color='red')\n", - "plt.plot(xi_plus['ANG'], np.diag(cov_th_sacha)[:20], label='SP_v1.4.5 data', color='green')\n", - "plt.plot(xi_plus['ANG'], np.diag(cov_mat)[:20], label='SP_v1.4.5 data', color='black')\n", + "plt.plot(xi_plus[\"ANG\"], np.diag(cov)[:20])\n", + "plt.plot(xi_plus[\"ANG\"], np.diag(cov_sim)[:20], label=\"SP_v1.4.5 data\", color=\"red\")\n", + "plt.plot(\n", + " xi_plus[\"ANG\"], np.diag(cov_th_sacha)[:20], label=\"SP_v1.4.5 data\", color=\"green\"\n", + ")\n", + "plt.plot(xi_plus[\"ANG\"], np.diag(cov_mat)[:20], label=\"SP_v1.4.5 data\", color=\"black\")\n", "\n", - "plt.ylabel('Diagonal of the covariance')\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", + "plt.ylabel(\"Diagonal of the covariance\")\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", "\n", - "plt.yscale('log')\n", - "plt.xscale('log')\n", - "plt.savefig('check_cov.png')\n", + "plt.yscale(\"log\")\n", + "plt.xscale(\"log\")\n", + "plt.savefig(\"check_cov.png\")\n", "plt.show()" ] }, @@ -950,30 +1198,44 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_plus_bf[mask], label='Best-fit model', color='red')\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_plus_bf[mask], label=\"Best-fit model\", color=\"red\")\n", "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(100.0, color='grey', linestyle='--', label='100 arcmin')\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(100.0, color=\"grey\", linestyle=\"--\", label=\"100 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_minus_bf[mask], label='Best-fit model', color='red')\n", - "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_minus_bf[mask], label=\"Best-fit model\", color=\"red\")\n", + "\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", - "plt.savefig('xi_data_bf.png')\n", + "plt.savefig(\"xi_data_bf.png\")\n", "plt.show()" ] }, @@ -984,11 +1246,11 @@ "metadata": {}, "outputs": [], "source": [ - "#Add PSF systematic\n", - "xi_sys_plus = np.loadtxt(root_dir+'/xi_sys/shear_xi_plus.txt')\n", - "xi_sys_minus = np.loadtxt(root_dir+'/xi_sys/shear_xi_minus.txt')\n", - "theta_sys = np.loadtxt(root_dir+'/xi_sys/theta.txt')\n", - "theta_sys = theta_sys * 180*60/np.pi" + "# Add PSF systematic\n", + "xi_sys_plus = np.loadtxt(root_dir + \"/xi_sys/shear_xi_plus.txt\")\n", + "xi_sys_minus = np.loadtxt(root_dir + \"/xi_sys/shear_xi_minus.txt\")\n", + "theta_sys = np.loadtxt(root_dir + \"/xi_sys/theta.txt\")\n", + "theta_sys = theta_sys * 180 * 60 / np.pi" ] }, { @@ -1002,32 +1264,46 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_plus_bf[mask], label='Best-fit model wo SYS', color='red')\n", - "plt.plot(theta_sys, xi_sys_plus, label=r'$\\xi_{\\rm sys}$', color='green')\n", - "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(100.0, color='grey', linestyle='--', label='100 arcmin')\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_plus_bf[mask], label=\"Best-fit model wo SYS\", color=\"red\")\n", + "plt.plot(theta_sys, xi_sys_plus, label=r\"$\\xi_{\\rm sys}$\", color=\"green\")\n", + "\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(100.0, color=\"grey\", linestyle=\"--\", label=\"100 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_minus_bf[mask], label='Best-fit model wo SYS', color='red')\n", - "plt.plot(theta_sys, xi_sys_minus, label=r'$\\xi_{\\rm sys}$', color='green')\n", - "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_minus_bf[mask], label=\"Best-fit model wo SYS\", color=\"red\")\n", + "plt.plot(theta_sys, xi_sys_minus, label=r\"$\\xi_{\\rm sys}$\", color=\"green\")\n", + "\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", - "plt.savefig('xi_data_bf_sys.png')\n", + "plt.savefig(\"xi_data_bf_sys.png\")\n", "plt.show()" ] }, @@ -1038,10 +1314,10 @@ "metadata": {}, "outputs": [], "source": [ - "shear_cl = np.loadtxt(root_dir+'/shear_cl/bin_1_1.txt')\n", - "shear_cl_gg = np.loadtxt(root_dir+'/shear_cl_gg/bin_1_1.txt')\n", - "shear_cl_gi = np.loadtxt(root_dir+'/shear_cl_gi/bin_1_1.txt')\n", - "shear_cl_ii = np.loadtxt(root_dir+'/shear_cl_ii/bin_1_1.txt')" + "shear_cl = np.loadtxt(root_dir + \"/shear_cl/bin_1_1.txt\")\n", + "shear_cl_gg = np.loadtxt(root_dir + \"/shear_cl_gg/bin_1_1.txt\")\n", + "shear_cl_gi = np.loadtxt(root_dir + \"/shear_cl_gi/bin_1_1.txt\")\n", + "shear_cl_ii = np.loadtxt(root_dir + \"/shear_cl_ii/bin_1_1.txt\")" ] }, { @@ -1052,7 +1328,7 @@ "outputs": [], "source": [ "A = 3.355083374185272\n", - "np.isclose(shear_cl, shear_cl_gg + 2*shear_cl_gi + shear_cl_ii)" + "np.isclose(shear_cl, shear_cl_gg + 2 * shear_cl_gi + shear_cl_ii)" ] }, { @@ -1061,9 +1337,7 @@ "id": "40", "metadata": {}, "outputs": [], - "source": [ - "\n" - ] + "source": [] }, { "cell_type": "code", @@ -1072,12 +1346,12 @@ "metadata": {}, "outputs": [], "source": [ - "#Add the lensing part without intrinsic alignment\n", - "root_dir = '/n09data/guerrini/output_chains/test_pipeline/'\n", - "xi_plus_wo_ia = np.loadtxt(root_dir+'/shear_xi_plus_wo_IA/bin_1_1.txt')\n", - "xi_minus_wo_ia = np.loadtxt(root_dir+'/shear_xi_minus_wo_IA/bin_1_1.txt')\n", - "angle = np.loadtxt(root_dir+'/shear_xi_plus_wo_IA/theta.txt')\n", - "angle = angle * 180*60/np.pi\n", + "# Add the lensing part without intrinsic alignment\n", + "root_dir = \"/n09data/guerrini/output_chains/test_pipeline/\"\n", + "xi_plus_wo_ia = np.loadtxt(root_dir + \"/shear_xi_plus_wo_IA/bin_1_1.txt\")\n", + "xi_minus_wo_ia = np.loadtxt(root_dir + \"/shear_xi_minus_wo_IA/bin_1_1.txt\")\n", + "angle = np.loadtxt(root_dir + \"/shear_xi_plus_wo_IA/theta.txt\")\n", + "angle = angle * 180 * 60 / np.pi\n", "\n", "mask = (angle < 250) & (angle > 0.1)" ] @@ -1093,34 +1367,55 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_plus_bf[mask], label='Best-fit model wo SYS', color='red')\n", - "plt.plot(theta_sys, xi_sys_plus, label=r'$\\xi_{\\rm sys}$', color='green')\n", - "plt.plot(angle[mask], xi_plus_wo_ia[mask], label='Best-fit model wo IA and SYS', color='blue')\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_plus_bf[mask], label=\"Best-fit model wo SYS\", color=\"red\")\n", + "plt.plot(theta_sys, xi_sys_plus, label=r\"$\\xi_{\\rm sys}$\", color=\"green\")\n", + "plt.plot(\n", + " angle[mask], xi_plus_wo_ia[mask], label=\"Best-fit model wo IA and SYS\", color=\"blue\"\n", + ")\n", "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(100.0, color='grey', linestyle='--', label='100 arcmin')\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(100.0, color=\"grey\", linestyle=\"--\", label=\"100 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_minus_bf[mask], label='Best-fit model wo SYS', color='red')\n", - "plt.plot(theta_sys, xi_sys_minus, label=r'$\\xi_{\\rm sys}$', color='green')\n", - "plt.plot(angle[mask], xi_minus_wo_ia[mask], label='Best-fit model wo IA and SYS', color='blue')\n", - "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_minus_bf[mask], label=\"Best-fit model wo SYS\", color=\"red\")\n", + "plt.plot(theta_sys, xi_sys_minus, label=r\"$\\xi_{\\rm sys}$\", color=\"green\")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_minus_wo_ia[mask],\n", + " label=\"Best-fit model wo IA and SYS\",\n", + " color=\"blue\",\n", + ")\n", + "\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", - "plt.savefig('xi_data_bf_sys_ia.png')\n", + "plt.savefig(\"xi_data_bf_sys_ia.png\")\n", "plt.show()" ] }, @@ -1131,12 +1426,12 @@ "metadata": {}, "outputs": [], "source": [ - "#Add the lensing part without intrinsic alignment\n", - "root_dir = '/n09data/guerrini/output_chains/test_pipeline/'\n", - "xi_plus_reas = np.loadtxt(root_dir+'/shear_xi_plus/bin_1_1.txt')\n", - "xi_minus_reas = np.loadtxt(root_dir+'/shear_xi_minus/bin_1_1.txt')\n", - "angle_reas = np.loadtxt(root_dir+'/shear_xi_plus/theta.txt')\n", - "angle_reas = angle_reas * 180*60/np.pi\n", + "# Add the lensing part without intrinsic alignment\n", + "root_dir = \"/n09data/guerrini/output_chains/test_pipeline/\"\n", + "xi_plus_reas = np.loadtxt(root_dir + \"/shear_xi_plus/bin_1_1.txt\")\n", + "xi_minus_reas = np.loadtxt(root_dir + \"/shear_xi_minus/bin_1_1.txt\")\n", + "angle_reas = np.loadtxt(root_dir + \"/shear_xi_plus/theta.txt\")\n", + "angle_reas = angle_reas * 180 * 60 / np.pi\n", "\n", "mask = (angle < 250) & (angle > 0.1)" ] @@ -1152,36 +1447,69 @@ "\n", "plt.subplot(211)\n", "\n", - "plt.errorbar(xi_plus['ANG'], xi_plus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[:20], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_plus_bf[mask], label='Best-fit model wo SYS', color='red')\n", - "plt.plot(theta_sys, xi_sys_plus, label=r'$\\xi_{\\rm sys}$', color='green')\n", - "plt.plot(angle[mask], xi_plus_wo_ia[mask], label='Best-fit model wo IA and SYS', color='blue')\n", - "plt.plot(angle_reas[mask], xi_plus_reas[mask], label='Lower IA', color='orange', linestyle='--')\n", - "\n", - "plt.ylabel(r'$\\xi_{+}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(3.0, color='grey', linestyle='--', label='3 arcmin')\n", - "plt.axvline(100.0, color='grey', linestyle='--', label='100 arcmin')\n", + "plt.errorbar(\n", + " xi_plus[\"ANG\"],\n", + " xi_plus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[:20],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_plus_bf[mask], label=\"Best-fit model wo SYS\", color=\"red\")\n", + "plt.plot(theta_sys, xi_sys_plus, label=r\"$\\xi_{\\rm sys}$\", color=\"green\")\n", + "plt.plot(\n", + " angle[mask], xi_plus_wo_ia[mask], label=\"Best-fit model wo IA and SYS\", color=\"blue\"\n", + ")\n", + "plt.plot(\n", + " angle_reas[mask],\n", + " xi_plus_reas[mask],\n", + " label=\"Lower IA\",\n", + " color=\"orange\",\n", + " linestyle=\"--\",\n", + ")\n", + "\n", + "plt.ylabel(r\"$\\xi_{+}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(3.0, color=\"grey\", linestyle=\"--\", label=\"3 arcmin\")\n", + "plt.axvline(100.0, color=\"grey\", linestyle=\"--\", label=\"100 arcmin\")\n", "plt.legend()\n", "\n", "plt.subplot(212)\n", "\n", - "plt.errorbar(xi_minus['ANG'], xi_minus['VALUE'], yerr=np.sqrt(np.diag(cov_mat))[20:40], fmt='o', label='SP_v1.4.5 data', color='black')\n", - "plt.plot(angle[mask], xi_minus_bf[mask], label='Best-fit model wo SYS', color='red')\n", - "plt.plot(theta_sys, xi_sys_minus, label=r'$\\xi_{\\rm sys}$', color='green')\n", - "plt.plot(angle[mask], xi_minus_wo_ia[mask], label='Best-fit model wo IA and SYS', color='blue')\n", - "plt.plot(angle_reas[mask], xi_minus_reas[mask], label='Lower IA', color='orange', linestyle='--')\n", - "\n", - "plt.xlabel(r'$\\theta$ [arcmin]')\n", - "plt.ylabel(r'$\\xi_{-}$')\n", - "plt.xscale('log')\n", - "plt.yscale('log')\n", - "plt.axvline(10.0, color='grey', linestyle='--', label='10 arcmin')\n", - "plt.axvline(200.0, color='grey', linestyle='--', label='200 arcmin')\n", + "plt.errorbar(\n", + " xi_minus[\"ANG\"],\n", + " xi_minus[\"VALUE\"],\n", + " yerr=np.sqrt(np.diag(cov_mat))[20:40],\n", + " fmt=\"o\",\n", + " label=\"SP_v1.4.5 data\",\n", + " color=\"black\",\n", + ")\n", + "plt.plot(angle[mask], xi_minus_bf[mask], label=\"Best-fit model wo SYS\", color=\"red\")\n", + "plt.plot(theta_sys, xi_sys_minus, label=r\"$\\xi_{\\rm sys}$\", color=\"green\")\n", + "plt.plot(\n", + " angle[mask],\n", + " xi_minus_wo_ia[mask],\n", + " label=\"Best-fit model wo IA and SYS\",\n", + " color=\"blue\",\n", + ")\n", + "plt.plot(\n", + " angle_reas[mask],\n", + " xi_minus_reas[mask],\n", + " label=\"Lower IA\",\n", + " color=\"orange\",\n", + " linestyle=\"--\",\n", + ")\n", + "\n", + "plt.xlabel(r\"$\\theta$ [arcmin]\")\n", + "plt.ylabel(r\"$\\xi_{-}$\")\n", + "plt.xscale(\"log\")\n", + "plt.yscale(\"log\")\n", + "plt.axvline(10.0, color=\"grey\", linestyle=\"--\", label=\"10 arcmin\")\n", + "plt.axvline(200.0, color=\"grey\", linestyle=\"--\", label=\"200 arcmin\")\n", "plt.legend()\n", "\n", - "plt.savefig('xi_data_bf_sys_ia_reas.png')\n", + "plt.savefig(\"xi_data_bf_sys_ia_reas.png\")\n", "plt.show()" ] }, diff --git a/cosmo_inference/notebooks/get_prior_psf_leakage.ipynb b/cosmo_inference/notebooks/get_prior_psf_leakage.ipynb index eb1a3e0c..1dae9a36 100644 --- a/cosmo_inference/notebooks/get_prior_psf_leakage.ipynb +++ b/cosmo_inference/notebooks/get_prior_psf_leakage.ipynb @@ -8,6 +8,7 @@ "outputs": [], "source": [ "import os\n", + "\n", "if not os.path.exists(\"./Plots\"):\n", " os.makedirs(\"./Plots\")\n", "\n", @@ -15,25 +16,22 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "from astropy.io import fits\n", + "import numpy as np\n", "import seaborn as sns\n", - "from getdist import plots, loadMCSamples, MCSamples\n", - "\n", - "from shear_psf_leakage.rho_tau_stat import RhoStat, TauStat, PSFErrorFit\n", + "from astropy.io import fits\n", + "from getdist import MCSamples, plots\n", + "from shear_psf_leakage.rho_tau_stat import PSFErrorFit, RhoStat, TauStat\n", "\n", "# Use paper style and seaborn with husl palette\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "# Set default palette - will be updated per plot as needed\n", "sns.set_palette(\"husl\")\n", "%matplotlib inline\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 25" ] @@ -49,20 +47,11 @@ "\n", "path_cosmo_val = \"/home/guerrini/sp_validation/cosmo_val/output/\"\n", "\n", - "roots_cosmo_val = [\n", - " \"SP_v1.4.6\",\n", - " \"SP_v1.4.6_leak_corr\"\n", - "]\n", + "roots_cosmo_val = [\"SP_v1.4.6\", \"SP_v1.4.6_leak_corr\"]\n", "\n", - "roots = [\n", - " \"SP_v1.4.6_no_leak_corr_A_masked\",\n", - " \"SP_v1.4.6_leak_corr_A_masked\"\n", - "]\n", + "roots = [\"SP_v1.4.6_no_leak_corr_A_masked\", \"SP_v1.4.6_leak_corr_A_masked\"]\n", "\n", - "labels = [\n", - " \"SP_v1.4.6_A\",\n", - " \"SP_v1.4.6_A leakage corrected\"\n", - "]" + "labels = [\"SP_v1.4.6_A\", \"SP_v1.4.6_A leakage corrected\"]" ] }, { @@ -75,9 +64,7 @@ "data_vectors = []\n", "\n", "for root in roots:\n", - " data_vectors.append(\n", - " fits.open(data_path + root +f\"/cosmosis_{root}.fits\")\n", - " )" + " data_vectors.append(fits.open(data_path + root + f\"/cosmosis_{root}.fits\"))" ] }, { @@ -102,7 +89,7 @@ "metadata": {}, "outputs": [], "source": [ - "#Print the covariance matrix for each root\n", + "# Print the covariance matrix for each root\n", "for i, root in enumerate(roots):\n", " print(f\"Covariance matrix for {labels[i]}:\")\n", " cov = data_vectors[i][\"COVMAT\"].data\n", @@ -112,14 +99,29 @@ " fig, ax = plt.subplots(figsize=(10, 8))\n", "\n", " im = ax.imshow(cov_to_corr(cov), vmin=-1, vmax=1, cmap=\"seismic\")\n", - " ax.set_aspect('equal')\n", + " ax.set_aspect(\"equal\")\n", " ax.set_yticks(np.array([10, 30, 50, 70]))\n", - " ax.set_yticklabels([r\"$\\xi_+(\\vartheta)$\", r\"$\\xi_-(\\vartheta)$\", r\"$\\tau_0(\\vartheta)$\", r\"$\\tau_2(\\vartheta)$\"])\n", + " ax.set_yticklabels(\n", + " [\n", + " r\"$\\xi_+(\\vartheta)$\",\n", + " r\"$\\xi_-(\\vartheta)$\",\n", + " r\"$\\tau_0(\\vartheta)$\",\n", + " r\"$\\tau_2(\\vartheta)$\",\n", + " ]\n", + " )\n", " ax.set_xticks(np.array([10, 30, 50, 70]))\n", - " ax.set_xticklabels([r\"$\\xi_+(\\vartheta)$\", r\"$\\xi_-(\\vartheta)$\", r\"$\\tau_0(\\vartheta)$\", r\"$\\tau_2(\\vartheta)$\"], rotation=45)\n", + " ax.set_xticklabels(\n", + " [\n", + " r\"$\\xi_+(\\vartheta)$\",\n", + " r\"$\\xi_-(\\vartheta)$\",\n", + " r\"$\\tau_0(\\vartheta)$\",\n", + " r\"$\\tau_2(\\vartheta)$\",\n", + " ],\n", + " rotation=45,\n", + " )\n", " fig.colorbar(im, ax=ax)\n", "\n", - " plt.savefig(f\"./Plots/cov_matrix_{root}.png\", bbox_inches='tight', dpi=300)\n", + " plt.savefig(f\"./Plots/cov_matrix_{root}.png\", bbox_inches=\"tight\", dpi=300)\n", " plt.show()\n", " print(\"\\n\")" ] @@ -131,38 +133,34 @@ "metadata": {}, "outputs": [], "source": [ - "#Create dummy rho and tau stat handler.\n", + "# Create dummy rho and tau stat handler.\n", "\n", - "#Inference of the xi_sys parameters\n", - "sep_units = 'arcmin'\n", - "coord_units = 'degrees'\n", + "# Inference of the xi_sys parameters\n", + "sep_units = \"arcmin\"\n", + "coord_units = \"degrees\"\n", "theta_min = 1.0\n", "theta_max = 250\n", "nbins = 20\n", "\n", "\n", "TreeCorrConfig_xi = {\n", - " 'ra_units': coord_units,\n", - " 'dec_units': coord_units,\n", - " 'min_sep': theta_min,\n", - " 'max_sep': theta_max,\n", - " 'sep_units': sep_units,\n", - " 'nbins': nbins,\n", - " 'var_method':'jackknife',\n", + " \"ra_units\": coord_units,\n", + " \"dec_units\": coord_units,\n", + " \"min_sep\": theta_min,\n", + " \"max_sep\": theta_max,\n", + " \"sep_units\": sep_units,\n", + " \"nbins\": nbins,\n", + " \"var_method\": \"jackknife\",\n", "}\n", "\n", - "rho_stats_handler = RhoStat(\n", - " output='.',\n", - " treecorr_config=TreeCorrConfig_xi,\n", - " verbose=True\n", - ")\n", + "rho_stats_handler = RhoStat(output=\".\", treecorr_config=TreeCorrConfig_xi, verbose=True)\n", "\n", "tau_stats_handler = TauStat(\n", " catalogs=rho_stats_handler.catalogs,\n", - " output='.',\n", + " output=\".\",\n", " treecorr_config=TreeCorrConfig_xi,\n", - " verbose=True\n", - " )" + " verbose=True,\n", + ")" ] }, { @@ -172,33 +170,40 @@ "metadata": {}, "outputs": [], "source": [ - "#Create a PSFErrorFit instance\n", - "psf_fitter = PSFErrorFit(rho_stats_handler, tau_stats_handler, path_cosmo_val+'rho_tau_stats/', use_eta=False)\n", + "# Create a PSFErrorFit instance\n", + "psf_fitter = PSFErrorFit(\n", + " rho_stats_handler,\n", + " tau_stats_handler,\n", + " path_cosmo_val + \"rho_tau_stats/\",\n", + " use_eta=False,\n", + ")\n", + "\n", "\n", - "def load_matrix_and_cut(root, root_cosmo_val, type='rho'):\n", - " if type=='rho':\n", + "def load_matrix_and_cut(root, root_cosmo_val, type=\"rho\"):\n", + " if type == \"rho\":\n", " cov = np.load(f\"{root}/cov_rho_{root_cosmo_val}.npy\")\n", " nbins = cov.shape[0] // 6\n", - " cov = cov[:nbins*3, :nbins*3]\n", + " cov = cov[: nbins * 3, : nbins * 3]\n", " np.save(f\"{root}/cov_rho_{root_cosmo_val}_cut.npy\", cov)\n", - " elif type=='tau':\n", + " elif type == \"tau\":\n", " cov = np.load(f\"{root}/cov_tau_{root_cosmo_val}_th.npy\")\n", " nbins = cov.shape[0] // 3\n", - " cov = cov[:nbins*2, :nbins*2]\n", + " cov = cov[: nbins * 2, : nbins * 2]\n", " np.save(f\"{root}/cov_tau_{root_cosmo_val}_th_cut.npy\", cov)\n", " else:\n", " raise ValueError(\"type must be 'rho' or 'tau'\")\n", "\n", - "g = plots.get_subplot_plotter(width_inch=30)\n", "\n", - "g.settings.axes_fontsize=30\n", - "g.settings.axes_labelsize=30\n", + "g = plots.get_subplot_plotter(width_inch=30)\n", + "\n", + "g.settings.axes_fontsize = 30\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "chains = []\n", "\n", - "#Load rho-, tau-statistics, and cov_tau from the data_vector\n", + "# Load rho-, tau-statistics, and cov_tau from the data_vector\n", "for i, root_cosmo_val in enumerate(roots_cosmo_val):\n", " print(\"Sampling PSF parameters for \", labels[i])\n", " path_rho = f\"rho_stats_{root_cosmo_val}.fits\"\n", @@ -206,28 +211,28 @@ " path_cov_rho = f\"cov_rho_{root_cosmo_val}.npy\"\n", " path_cov_tau = f\"cov_tau_{root_cosmo_val}_th.npy\"\n", "\n", - " load_matrix_and_cut(path_cosmo_val+'/rho_tau_stats/', root_cosmo_val, type='rho')\n", - " load_matrix_and_cut(path_cosmo_val+'/rho_tau_stats/', root_cosmo_val, type='tau')\n", + " load_matrix_and_cut(path_cosmo_val + \"/rho_tau_stats/\", root_cosmo_val, type=\"rho\")\n", + " load_matrix_and_cut(path_cosmo_val + \"/rho_tau_stats/\", root_cosmo_val, type=\"tau\")\n", " path_cov_rho = f\"cov_rho_{root_cosmo_val}_cut.npy\"\n", " path_cov_tau = f\"cov_tau_{root_cosmo_val}_th_cut.npy\"\n", "\n", " psf_fitter.load_rho_stat(path_rho)\n", " psf_fitter.load_tau_stat(path_tau)\n", - " psf_fitter.load_covariance(path_cov_rho, cov_type='rho')\n", - " psf_fitter.load_covariance(path_cov_tau, cov_type='tau')\n", - " samples_lq, _, _ = psf_fitter.get_least_squares_params_samples(npatch=None, apply_debias=False)\n", + " psf_fitter.load_covariance(path_cov_rho, cov_type=\"rho\")\n", + " psf_fitter.load_covariance(path_cov_tau, cov_type=\"tau\")\n", + " samples_lq, _, _ = psf_fitter.get_least_squares_params_samples(\n", + " npatch=None, apply_debias=False\n", + " )\n", "\n", - " samples_gd = MCSamples(samples=samples_lq, names=[r\"\\alpha\", r\"\\beta\"], labels=[r\"\\alpha\", r\"\\beta\"])\n", + " samples_gd = MCSamples(\n", + " samples=samples_lq, names=[r\"\\alpha\", r\"\\beta\"], labels=[r\"\\alpha\", r\"\\beta\"]\n", + " )\n", "\n", " chains.append(samples_gd)\n", "\n", - "g.triangle_plot(chains,\n", - " filled=True,\n", - " legend_labels=labels,\n", - " legend_loc='upper right'\n", - ")\n", + "g.triangle_plot(chains, filled=True, legend_labels=labels, legend_loc=\"upper right\")\n", "\n", - "plt.savefig(f\"./Plots/psf_leakage_params.png\", bbox_inches='tight', dpi=300)\n", + "plt.savefig(\"./Plots/psf_leakage_params.png\", bbox_inches=\"tight\", dpi=300)\n", "plt.show()" ] }, diff --git a/cosmo_inference/scripts/2pt_like_xi_sys.py b/cosmo_inference/scripts/2pt_like_xi_sys.py index 1b9f262a..93f11bd3 100644 --- a/cosmo_inference/scripts/2pt_like_xi_sys.py +++ b/cosmo_inference/scripts/2pt_like_xi_sys.py @@ -1,22 +1,23 @@ -from cosmosis.gaussian_likelihood import GaussianLikelihood -from cosmosis.datablock import names, SectionOptions -from twopoint_cosmosis import theory_names, type_table -from astropy.io import fits -from scipy.interpolate import interp1d +import gaussian_covariance import numpy as np import twopoint -import gaussian_covariance -import os +from astropy.io import fits +from cosmosis.datablock import SectionOptions, names +from cosmosis.gaussian_likelihood import GaussianLikelihood +from scipy.interpolate import interp1d from spec_tools import TheorySpectrum +from twopoint_cosmosis import theory_names, type_table + default_array = np.repeat(-1.0, 99) -#To copy in cosmosis-standard-library/likelihood + +# To copy in cosmosis-standard-library/likelihood def is_default(x): return len(x) == len(default_array) and (x == default_array).all() def convert_nz_steradian(n): - return n * (41253.0 * 60. * 60.) / (4 * np.pi) + return n * (41253.0 * 60.0 * 60.0) / (4 * np.pi) class TwoPointLikelihood(GaussianLikelihood): @@ -32,27 +33,32 @@ class TwoPointLikelihood(GaussianLikelihood): def __init__(self, options): # We may decide to use an analytic gaussian covariance # in that case we won't load the covmat. - self.gaussian_covariance = options.get_bool( - "gaussian_covariance", False) + self.gaussian_covariance = options.get_bool("gaussian_covariance", False) if self.gaussian_covariance: self.constant_covariance = False - self.moped = options.get_string('moped', default='') + self.moped = options.get_string("moped", default="") super(TwoPointLikelihood, self).__init__(options) self.raw_data_x, self.raw_data_y = self.build_data() if self.moped: - print("Using compressed data from MOPED algorithm: {} data points".format(len(self.moped_data))) + print( + "Using compressed data from MOPED algorithm: {} data points".format( + len(self.moped_data) + ) + ) if self.sellentin: - raise ValueError("Sellentin mode is incompatible with Moped mode in 2pt like") + raise ValueError( + "Sellentin mode is incompatible with Moped mode in 2pt like" + ) def build_data(self): - filename = self.options.get_string('data_file') + filename = self.options.get_string("data_file") # Suffixes to added on to two point data from e.g. different experiments - suffix_string = self.options.get_string('suffixes', default="") + suffix_string = self.options.get_string("suffixes", default="") if suffix_string == "": # If there are no suffixes provided, then we create a list of a single empty suffix suffixes = [""] @@ -60,8 +66,8 @@ def build_data(self): suffixes_temp = suffix_string.split() suffixes = [] for suffix in suffixes_temp: - if (suffix.lower() == 'none'): - suffixes.append('') + if suffix.lower() == "none": + suffixes.append("") else: suffixes.append("_" + suffix) self.suffixes = suffixes @@ -88,8 +94,7 @@ def get_arr(x): covmat_name = self.options.get_string("covmat_name", "COVMAT") # This is the main work - read data in from the file - self.two_point_data = twopoint.TwoPointFile.from_fits( - filename, covmat_name) + self.two_point_data = twopoint.TwoPointFile.from_fits(filename, covmat_name) # Potentially cut out lines. For some reason one version of # this file used zeros to mark masked values. @@ -113,8 +118,7 @@ def get_arr(x): self.two_point_data.choose_data_sets(data_sets) # The ones we actually used. - self.used_names = [ - spectrum.name for spectrum in self.two_point_data.spectra] + self.used_names = [spectrum.name for spectrum in self.two_point_data.spectra] # Check for scale cuts. In general, this is a minimum and maximum angle for # each spectrum, for each redshift bin combination. Which is clearly a massive pain... @@ -150,18 +154,25 @@ def get_arr(x): # Info on which likelihoods we do and do not use print("Found these data sets in the file:") total_data_points = 0 - final_names = [ - spectrum.name for spectrum in self.two_point_data.spectra] + final_names = [spectrum.name for spectrum in self.two_point_data.spectra] for name in all_names: if name in final_names: data_points = len(self.two_point_data.get_spectrum(name)) else: data_points = 0 if name in self.used_names: - print(" - {} {} data points after cuts {}".format(name, data_points, " [using in likelihood]")) + print( + " - {} {} data points after cuts {}".format( + name, data_points, " [using in likelihood]" + ) + ) total_data_points += data_points else: - print(" - {} {} data points after cuts {}".format(name, data_points, " [not using in likelihood]")) + print( + " - {} {} data points after cuts {}".format( + name, data_points, " [not using in likelihood]" + ) + ) print("Total data points used = {}".format(total_data_points)) # Convert all units to radians. The units in cosmosis are all @@ -170,24 +181,32 @@ def get_arr(x): if spectrum.is_real_space(): spectrum.convert_angular_units("rad") # if self.options.get_bool("print physical scale",False): - # section,_,_=theory_names(spectrum) - # chi_peak = - # for ang in spectrum.angle: + # section,_,_=theory_names(spectrum) + # chi_peak = + # for ang in spectrum.angle: # build up the data vector from all the separate vectors. # Just concatenation data_vector = np.concatenate( - [spectrum.value for spectrum in self.two_point_data.spectra]) + [spectrum.value for spectrum in self.two_point_data.spectra] + ) # Make sure if len(data_vector) == 0: raise ValueError( - "No data was chosen to be used from 2-point data file {0}. It was either not selectedin data_sets or cut out".format(filename)) + "No data was chosen to be used from 2-point data file {0}. It was either not selectedin data_sets or cut out".format( + filename + ) + ) if self.moped: data_file = fits.open(filename) - self.moped_data = data_file['MOPED-DATA-{}'.format(self.moped)].data['moped'] - self.moped_transform = data_file['MOPED-TRANSFORM-{}'.format(self.moped)].data + self.moped_data = data_file["MOPED-DATA-{}".format(self.moped)].data[ + "moped" + ] + self.moped_transform = data_file[ + "MOPED-TRANSFORM-{}".format(self.moped) + ].data data_file.close() return None, self.moped_data @@ -199,15 +218,13 @@ def get_arr(x): def build_covariance(self): - C = np.array(self.two_point_data.covmat) - r = self.options.get_int('covariance_realizations', default=-1) - self.sellentin = self.options.get_bool('sellentin', default=False) + r = self.options.get_int("covariance_realizations", default=-1) + self.sellentin = self.options.get_bool("sellentin", default=False) if self.moped: return np.identity(len(self.moped_data)) - if self.sellentin: if not self.constant_covariance: print() @@ -219,17 +236,24 @@ def build_covariance(self): if r < 0: print() print("ERROR: You asked for the Sellentin-Heavens corrections") - print("by setting sellentin=T, but you did not set covariance_realizations") + print( + "by setting sellentin=T, but you did not set covariance_realizations" + ) print("If you want covariance_realizations=infinity you can use 0") - print("(unlikely, but it's also possible you were super-perverse and set it negative?)") + print( + "(unlikely, but it's also possible you were super-perverse and set it negative?)" + ) print() raise ValueError( - "Please set covariance_realizations for 2pt like. See message above.") + "Please set covariance_realizations for 2pt like. See message above." + ) elif r == 0: print() print("NOTE: You asked for the Sellentin-Heavens corrections") print("but set covariance_realizations=0. I am assuming you want") - print("the limit of an infinite number of realizations, so we will just go back") + print( + "the limit of an infinite number of realizations, so we will just go back" + ) print("to the original Gaussian model") print() self.sellentin = False @@ -237,10 +261,14 @@ def build_covariance(self): # use proper correction self.covariance_realizations = r print() - print("You set sellentin=T so I will apply the Sellentin-Heavens correction") + print( + "You set sellentin=T so I will apply the Sellentin-Heavens correction" + ) print("for a covariance matrix estimated from Monte-Carlo simulations") print("(you told us it was {} simulations in the ini file)".format(r)) - print("This analytic marginalization converts the Gaussian distribution") + print( + "This analytic marginalization converts the Gaussian distribution" + ) print("to a multivariate student's t distribution instead.") print() @@ -252,10 +280,20 @@ def build_covariance(self): x = (r - 1.0) / (r - p - 2.0) C = C * x print() - print("You set covariance_realizations={} in the 2pt likelihood parameter file".format(r)) - print("So I will apply the Anderson-Hartlap correction to the covariance matrix") + print( + "You set covariance_realizations={} in the 2pt likelihood parameter file".format( + r + ) + ) + print( + "So I will apply the Anderson-Hartlap correction to the covariance matrix" + ) print("The covariance matrix is nxn = {}x{}".format(p, p)) - print("So the correction scales the covariance matrix by (r - 1) / (r - n - 2) = {}".format(x)) + print( + "So the correction scales the covariance matrix by (r - 1) / (r - n - 2) = {}".format( + x + ) + ) print() return C @@ -273,21 +311,25 @@ def extract_theory_points(self, block): angle = [] bin1 = [] bin2 = [] - dataset_name = [] # Get appropriate suffixes # If only a single suffix is provided, assume this applies to all data sets if len(self.suffixes) == 1: suffixes = np.tile(self.suffixes[0], len(self.two_point_data.spectra)) - elif len(self.suffixes) > 1 and len(self.suffixes) == len(self.two_point_data.spectra): + elif len(self.suffixes) > 1 and len(self.suffixes) == len( + self.two_point_data.spectra + ): suffixes = self.suffixes else: - raise ValueError("The number of suffixes supplied does not match the number of two point spectra.") - - # Now we actually loop through our data sets + raise ValueError( + "The number of suffixes supplied does not match the number of two point spectra." + ) + + # Now we actually loop through our data sets for ii, spectrum in enumerate(self.two_point_data.spectra): - theory_vector, angle_vector, bin1_vector, bin2_vector = self.extract_spectrum_prediction( - block, spectrum, suffixes[ii]) + theory_vector, angle_vector, bin1_vector, bin2_vector = ( + self.extract_spectrum_prediction(block, spectrum, suffixes[ii]) + ) theory.append(theory_vector) angle.append(angle_vector) bin1.append(bin1_vector) @@ -330,7 +372,8 @@ def do_likelihood(self, block): # least make clear there is a problem somewhere and not # yield misleading results. block[names.data_vector, self.like_name + "_simulation"] = ( - np.nan * block[names.data_vector, self.like_name + "_simulation"]) + np.nan * block[names.data_vector, self.like_name + "_simulation"] + ) # It changes the Likelihood from Gaussian to a multivariate # student's t distribution. Here we will have to do a little @@ -347,12 +390,12 @@ def do_likelihood(self, block): else: log_det = block[names.data_vector, self.like_name + "_LOG_DET"] - like = -0.5 * log_det - 0.5 * N * np.log(1 + chi2 / (N - 1.)) + like = -0.5 * log_det - 0.5 * N * np.log(1 + chi2 / (N - 1.0)) # overwrite the log-likelihood block[names.likelihoods, self.like_name + "_LIKE"] = like - #Should suffix be made into a keyword? + # Should suffix be made into a keyword? def extract_spectrum_prediction(self, block, spectrum, suffix): # We may need theory predictions for multiple different @@ -361,16 +404,15 @@ def extract_spectrum_prediction(self, block, spectrum, suffix): # block we expect to find these - mapping spectrum types # to block names section, x_name, y_name = theory_names(spectrum) - + # To handle multiple different data sets we allow a suffix # to be applied to the section names, so that we can look up # e.g. "shear_cl_des" instead of just "shear_cl". section += suffix - + # Initialize TheorySpectrum class from block bin_pairs = spectrum.get_bin_pairs() - theory_spec = TheorySpectrum.from_block(block, - section, bin_pairs=bin_pairs) + theory_spec = TheorySpectrum.from_block(block, section, bin_pairs=bin_pairs) # If the theory spectrum has been bin-averaged then we expect the # data to be so also. We check this by ensuring that angle_min is specified @@ -379,11 +421,13 @@ def extract_spectrum_prediction(self, block, spectrum, suffix): # whereas the interpolated version just wants a single angle. if theory_spec.is_bin_averaged: if spectrum.angle_min is None: - raise ValueError("Your theory pipeline produced angle-binnned values, but your data it not binned.") + raise ValueError( + "Your theory pipeline produced angle-binnned values, but your data it not binned." + ) angles = list(zip(spectrum.angle_min, spectrum.angle_max)) else: angles = spectrum.angle - + # We store the nominal mid-points for plotting later on, etc. angle_mids = spectrum.angle @@ -399,7 +443,9 @@ def extract_spectrum_prediction(self, block, spectrum, suffix): bin1_vector = [] bin2_vector = [] - for (b1, b2, angle, angle_mid) in zip(spectrum.bin1, spectrum.bin2, angles, angle_mids): + for b1, b2, angle, angle_mid in zip( + spectrum.bin1, spectrum.bin2, angles, angle_mids + ): # The extra object will either be a spline (for interpolated spectra) # or theta mid-point values (for bin-averaged ones, e.g. for plotting) theory, extra = theory_spec.get_spectrum_value(b1, b2, angle) @@ -427,9 +473,10 @@ def extract_spectrum_prediction(self, block, spectrum, suffix): return theory_vector, angle_vector, bin1_vector, bin2_vector - def extract_covariance(self, block): - assert self.gaussian_covariance, "Set constant_covariance=F but somehow not with Gaussian covariance. Internal error - please open an issue on the cosmosis site." + assert self.gaussian_covariance, ( + "Set constant_covariance=F but somehow not with Gaussian covariance. Internal error - please open an issue on the cosmosis site." + ) C = [] # s and t index the spectra that we have. e.g. s or t=1 might be the full set of @@ -437,15 +484,20 @@ def extract_covariance(self, block): for s, AB in enumerate(self.two_point_data.spectra[:]): M = [] for t, CD in enumerate(self.two_point_data.spectra[:]): - print("Looking at covariance between {} and {} (s={}, t={})".format(AB.name, CD.name, s, t)) + print( + "Looking at covariance between {} and {} (s={}, t={})".format( + AB.name, CD.name, s, t + ) + ) # We only calculate the upper triangular. # Get the lower triangular here. We have to # transpose it compared to the upper one. if s > t: MI = C[t][s].T else: - MI = gaussian_covariance.compute_gaussian_covariance(self.sky_area, - self._lookup_theory_cl, block, AB, CD) + MI = gaussian_covariance.compute_gaussian_covariance( + self.sky_area, self._lookup_theory_cl, block, AB, CD + ) M.append(MI) C.append(M) @@ -459,14 +511,16 @@ def extract_covariance(self, block): def _lookup_theory_cl(self, block, A, B, i, j, ell): """ This is a helper function for the compute_gaussian_covariance code. - It looks up the theory value of C^{ij}_{AB}(ell) in the + It looks up the theory value of C^{ij}_{AB}(ell) in the """ # We have already saved splines into the theory space earlier # when constructing the theory vector. # So now we just need to look those up again, using the same # code we use in the twopoint library. section, ell_name, value_name = type_table[A, B] - assert ell_name == "ell", "Gaussian covariances are currently only written for C_ell, not other 2pt functions" + assert ell_name == "ell", ( + "Gaussian covariances are currently only written for C_ell, not other 2pt functions" + ) d = self.theory_splines[section] # We save the splines with these names when we extract the theory vector @@ -492,8 +546,11 @@ def _lookup_theory_cl(self, block, A, B, i, j, ell): elif block.has_value(section, name_ji) and A == B: theory = block[section, name_ji] else: - raise ValueError("Could not find theory prediction {} in section {}".format( - value_name.format(i, j), section)) + raise ValueError( + "Could not find theory prediction {} in section {}".format( + value_name.format(i, j), section + ) + ) spline = interp1d(angle_theory, theory) # Finally cache this so we don't have to do this again. @@ -503,25 +560,34 @@ def _lookup_theory_cl(self, block, A, B, i, j, ell): # For shear-shear the noise component is sigma^2 / number_density_bin # and for position-position it is just 1/number_density_bin - if (A == B) and (A == twopoint.Types.galaxy_shear_emode_fourier.name) and (i == j): - if i > len(self.number_density_shear_bin) or i > len(self.sigma_e_bin) or is_default(self.sigma_e_bin) or is_default(self.number_density_shear_bin): - raise ValueError( - "Not enough number density bins for shear specified") - noise = self.sigma_e_bin[i - 1]**2 / \ - convert_nz_steradian(self.number_density_shear_bin[i - 1]) + if ( + (A == B) + and (A == twopoint.Types.galaxy_shear_emode_fourier.name) + and (i == j) + ): + if ( + i > len(self.number_density_shear_bin) + or i > len(self.sigma_e_bin) + or is_default(self.sigma_e_bin) + or is_default(self.number_density_shear_bin) + ): + raise ValueError("Not enough number density bins for shear specified") + noise = self.sigma_e_bin[i - 1] ** 2 / convert_nz_steradian( + self.number_density_shear_bin[i - 1] + ) obs_cl += noise if (A == B) and (A == twopoint.Types.galaxy_position_fourier.name) and (i == j): - if i > len(self.number_density_lss_bin) or is_default(self.number_density_lss_bin): - raise ValueError( - "Not enough number density bins for lss specified") - noise = 1.0 / \ - convert_nz_steradian(self.number_density_lss_bin[i - 1]) + if i > len(self.number_density_lss_bin) or is_default( + self.number_density_lss_bin + ): + raise ValueError("Not enough number density bins for lss specified") + noise = 1.0 / convert_nz_steradian(self.number_density_lss_bin[i - 1]) obs_cl += noise return obs_cl - + def update_xi_w_sys(self, block): - self.data_y = self.raw_data_y + block['xi_sys', 'xi_sys_vec'] + self.data_y = self.raw_data_y + block["xi_sys", "xi_sys_vec"] @classmethod def build_module(cls): @@ -534,7 +600,7 @@ def setup(options): def execute(block, config): likelihoodCalculator = config likelihoodCalculator.update_xi_w_sys(block) - #print(likelihoodCalculator.data_y) + # print(likelihoodCalculator.data_y) likelihoodCalculator.do_likelihood(block) return 0 diff --git a/cosmo_inference/scripts/chain_postprocessing.py b/cosmo_inference/scripts/chain_postprocessing.py index 2cc39029..83cddf4b 100644 --- a/cosmo_inference/scripts/chain_postprocessing.py +++ b/cosmo_inference/scripts/chain_postprocessing.py @@ -2,30 +2,32 @@ Scripts to postprocess the CosmoSIS chains Author: Sacha Guerrini """ -import os + import configparser +import os import subprocess +import matplotlib.pyplot as plt import numpy as np -from getdist import plots, MCSamples from astropy.io import fits -import matplotlib.pyplot as plt +from getdist import plots # Mapping for CosmoSIS ini files section section_map = { - 'omch2': 'cosmological_parameters', - 'ombh2': 'cosmological_parameters', - 'h0': 'cosmological_parameters', - 'n_s': 'cosmological_parameters', - 's_8_input': 'cosmological_parameters', - 'logt_agn': 'halo_model_parameters', - 'a': 'intrinsic_alignment_parameters', - 'm1': 'shear_calibration_parameters', - 'bias_1': 'nofz_shifts', - 'alpha': 'psf_leakage_parameters', - 'beta': 'psf_leakage_parameters', + "omch2": "cosmological_parameters", + "ombh2": "cosmological_parameters", + "h0": "cosmological_parameters", + "n_s": "cosmological_parameters", + "s_8_input": "cosmological_parameters", + "logt_agn": "halo_model_parameters", + "a": "intrinsic_alignment_parameters", + "m1": "shear_calibration_parameters", + "bias_1": "nofz_shifts", + "alpha": "psf_leakage_parameters", + "beta": "psf_leakage_parameters", } + # Utils functions def compute_average(chain, param_name): """ @@ -35,6 +37,7 @@ def compute_average(chain, param_name): param_stats = margestats.parWithName(param_name) return param_stats.mean + def compute_map_1D(chain, param_name, num_bins=1000): """ Compute the MAP value of a parameter from a CosmoSIS chain using 1D KDE @@ -45,6 +48,7 @@ def compute_map_1D(chain, param_name, num_bins=1000): kde_map = kde.x[np.argmax(kde.P)] return kde_map + def compute_map_2D(chain, param_name_x, param_name_y, num_bins=1000): """ Compute the MAP value of two parameters from a CosmoSIS chain using 2D KDE @@ -56,92 +60,134 @@ def compute_map_2D(chain, param_name_x, param_name_y, num_bins=1000): kde_map_index = np.unravel_index(np.argmax(kde.P), kde.P.shape) return kde.x[kde_map_index[1]], kde.y[kde_map_index[0]] + def compute_limits(chain, param_name): """ Compute the 68% and 95% confidence limits of a parameter from a CosmoSIS chain. """ margestats = chain.getMargeStats() param_stats = margestats.parWithName(param_name) - return param_stats.limits[0].upper, param_stats.limits[0].lower, param_stats.limits[1].upper, param_stats.limits[1].lower + return ( + param_stats.limits[0].upper, + param_stats.limits[0].lower, + param_stats.limits[1].upper, + param_stats.limits[1].lower, + ) + -def load_samples_and_write_paramnames(path_samples, path_paramnames, chain_type='polychord'): +def load_samples_and_write_paramnames( + path_samples, path_paramnames, chain_type="polychord" +): """ Load the samples from a CosmoSIS chain and write the parameter names to a file """ - with open(path_samples, 'r') as file: - if chain_type == 'nautilus': - params = file.readline()[1:].split('\t')[:-3] + with open(path_samples, "r") as file: + if chain_type == "nautilus": + params = file.readline()[1:].split("\t")[:-3] else: - params = file.readline()[1:].split('\t')[:-4] + params = file.readline()[1:].split("\t")[:-4] file.close() - with open(path_paramnames, 'w') as file: + with open(path_paramnames, "w") as file: for i in range(len(params)): - if len(params[i].split('--')) > 1: - param_name = params[i].split('--')[1] - if 'Legacy' in path_paramnames and param_name not in ['OMEGA_M', 'SIGMA_8']: + if len(params[i].split("--")) > 1: + param_name = params[i].split("--")[1] + if "Legacy" in path_paramnames and param_name not in [ + "OMEGA_M", + "SIGMA_8", + ]: continue - file.write(param_name + '\n') + file.write(param_name + "\n") else: - param_name = params[i].split('--')[0] - if 'Legacy' in path_paramnames and param_name not in ['OMEGA_M', 'SIGMA_8']: + param_name = params[i].split("--")[0] + if "Legacy" in path_paramnames and param_name not in [ + "OMEGA_M", + "SIGMA_8", + ]: continue - file.write(param_name + '\n') + file.write(param_name + "\n") file.close() return 0 -def write_samples_getdist_format(path_samples, path_gd, chain_type='polychord'): + +def write_samples_getdist_format(path_samples, path_gd, chain_type="polychord"): """ Load the samples from a CosmoSIS chain and write them in GetDist format """ - samples = np.loadtxt( - path_samples - ) - if chain_type == 'nautilus': - if 'Legacy' in path_gd: - samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-2]-samples[:,-1], samples[:, 21], samples[:, 23])) + samples = np.loadtxt(path_samples) + if chain_type == "nautilus": + if "Legacy" in path_gd: + samples = np.column_stack( + ( + np.exp(samples[:, -3]), + samples[:, -2] - samples[:, -1], + samples[:, 21], + samples[:, 23], + ) + ) else: - samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-2]-samples[:,-1],samples[:,0:-3])) + samples = np.column_stack( + ( + np.exp(samples[:, -3]), + samples[:, -2] - samples[:, -1], + samples[:, 0:-3], + ) + ) mask = np.isfinite(samples[:, 1]) samples = samples[mask] else: - samples = np.column_stack((samples[:,-1],samples[:,-2],samples[:,0:-4])) + samples = np.column_stack((samples[:, -1], samples[:, -2], samples[:, 0:-4])) np.savetxt(path_gd, samples) return 0 + def load_chain(path_gd, smoothing_scale=0.3): g = plots.get_single_plotter() - chain = g.samples_for_root(path_gd, cache=False, settings={'ignore_rows':0, 'smooth_scale_1D': smoothing_scale, 'smooth_scale_2D': smoothing_scale}) + chain = g.samples_for_root( + path_gd, + cache=False, + settings={ + "ignore_rows": 0, + "smooth_scale_1D": smoothing_scale, + "smooth_scale_2D": smoothing_scale, + }, + ) return chain -def extract_best_fit_params(chain, best_fit_method='weighted_mean'): + +def extract_best_fit_params(chain, best_fit_method="weighted_mean"): best_fit_params = {} - margestats = chain.getMargeStats() + chain.getMargeStats() likestats = chain.getLikeStats() for i, par in enumerate(likestats.names): - if best_fit_method == 'weighted_mean': + if best_fit_method == "weighted_mean": best_fit = compute_average(chain, par.name) - elif best_fit_method == '1Dkde': + elif best_fit_method == "1Dkde": best_fit = compute_map_1D(chain, par.name) - elif best_fit_method == '2Dkde': + elif best_fit_method == "2Dkde": # If the parameter is S8 or Omega_m, use the 2D KDE - if par.name == 'S_8' or par.name == "s_8_input" or par.name == 'OMEGA_M': - s8_map_2D, omega_m_map_2D = compute_map_2D(chain, 'S_8', 'OMEGA_M') - best_fit = s8_map_2D if par.name == 'S_8' or par.name == "s_8_input" else omega_m_map_2D + if par.name == "S_8" or par.name == "s_8_input" or par.name == "OMEGA_M": + s8_map_2D, omega_m_map_2D = compute_map_2D(chain, "S_8", "OMEGA_M") + best_fit = ( + s8_map_2D + if par.name == "S_8" or par.name == "s_8_input" + else omega_m_map_2D + ) else: best_fit = compute_map_1D(chain, par.name) else: - raise ValueError("Invalid best fit method. Choose one of: '2Dkde', '1Dkde', 'weighted_mean'") - best_fit_params.update( - { - par.name: best_fit - } - ) + raise ValueError( + "Invalid best fit method. Choose one of: '2Dkde', '1Dkde', 'weighted_mean'" + ) + best_fit_params.update({par.name: best_fit}) return best_fit_params -def compute_best_fit(path_ini_files, best_fit, root, is_harmonic, blind=None, ini_file_root=None): + +def compute_best_fit( + path_ini_files, best_fit, root, is_harmonic, blind=None, ini_file_root=None +): # Check if the values empty ini file exists - if not os.path.exists(path_ini_files+'/values_empty.ini'): + if not os.path.exists(path_ini_files + "/values_empty.ini"): content = """[cosmological_parameters] tau = 0.0544 @@ -161,20 +207,22 @@ def compute_best_fit(path_ini_files, best_fit, root, is_harmonic, blind=None, in [psf_leakage_parameters] """ - with open(path_ini_files+'/values_empty.ini', 'w') as f: + with open(path_ini_files + "/values_empty.ini", "w") as f: f.write(content) f.close() - print('File created successfully') - + print("File created successfully") # Load cosmosis in the library path env = os.environ.copy() - env["LD_LIBRARY_PATH"] = "/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:" + env.get("LD_LIBRARY_PATH", "") + env["LD_LIBRARY_PATH"] = ( + "/home/guerrini/.conda/envs/sp_validation/lib/python3.9/site-packages/cosmosis/datablock:" + + env.get("LD_LIBRARY_PATH", "") + ) config = configparser.ConfigParser() config.optionxform = str # Preserve case sensitivity of option names - config.read(path_ini_files+'/values_empty.ini') + config.read(path_ini_files + "/values_empty.ini") for param, value in best_fit.items(): section = section_map.get(param) if section is None: @@ -183,80 +231,99 @@ def compute_best_fit(path_ini_files, best_fit, root, is_harmonic, blind=None, in config.add_section(section) config[section][param] = str(value) - with open(path_ini_files+'/values_empty.ini', 'w') as configfile: + with open(path_ini_files + "/values_empty.ini", "w") as configfile: config.write(configfile) - #Modify the ini file to run in test mode at the best fit + # Modify the ini file to run in test mode at the best fit config = configparser.ConfigParser() config.optionxform = str # Preserve case sensitivity of option names if ini_file_root is None: # If the ini file root is not provided, we construct it based on the root and blind parameters if blind is not None: - subdir = f'harmonic_space_fiducial_{blind}' if is_harmonic else f'' #TODO: add real space subdir if needed + subdir = ( + f"harmonic_space_fiducial_{blind}" if is_harmonic else "" + ) # TODO: add real space subdir if needed else: - subdir='' + subdir = "" ini_file_root = os.path.join( - path_ini_files, - subdir, - f'cosmosis_pipeline_{root}_cell.ini' + path_ini_files, subdir, f"cosmosis_pipeline_{root}_cell.ini" ) config.read(ini_file_root) + sampler = config["runtime"]["sampler"] + config["runtime"]["sampler"] = "test" + values = config["pipeline"]["values"] + config["pipeline"]["values"] = path_ini_files + "/values_empty.ini" - sampler = config['runtime']['sampler'] - config['runtime']['sampler'] = 'test' - values = config['pipeline']['values'] - config['pipeline']['values'] = path_ini_files + '/values_empty.ini' - - with open(ini_file_root, 'w') as configfile: + with open(ini_file_root, "w") as configfile: config.write(configfile) - #Run cosmosis - os.chdir('/home/guerrini/sp_validation/cosmo_inference') + # Run cosmosis + os.chdir("/home/guerrini/sp_validation/cosmo_inference") result = subprocess.run( - ['cosmosis', ini_file_root], - env=env, - capture_output=True, - text=True + ["cosmosis", ini_file_root], env=env, capture_output=True, text=True ) print(f"STDOUT:\n{result.stdout}") print(f"STDERR:\n{result.stderr}") - #Modify the ini file to the previous one - config['pipeline']['values'] = values - config['runtime']['sampler'] = sampler + # Modify the ini file to the previous one + config["pipeline"]["values"] = values + config["runtime"]["sampler"] = sampler - with open(ini_file_root, 'w') as configfile: + with open(ini_file_root, "w") as configfile: config.write(configfile) + def compute_best_fit_xi_from_cell(output_folder, root, best_fit_params, theta_rad): - - ell = np.loadtxt(output_folder + '{}/best_fit/shear_cl/ell.txt'.format(root)) - shear_cl = np.loadtxt(output_folder + '{}/best_fit/shear_cl/bin_1_1.txt'.format(root)) - + + ell = np.loadtxt(output_folder + "{}/best_fit/shear_cl/ell.txt".format(root)) + shear_cl = np.loadtxt( + output_folder + "{}/best_fit/shear_cl/bin_1_1.txt".format(root) + ) + import pyccl as ccl - cosmo = ccl.Cosmology(Omega_c=best_fit_params['omch2']/(best_fit_params['h0']/100)**2, - Omega_b=best_fit_params['ombh2']/(best_fit_params['h0']/100)**2, - h=best_fit_params['h0']/100, - n_s=best_fit_params['n_s'], - sigma8=best_fit_params['SIGMA_8'], - baryonic_effects=None, - extra_parameters = {"camb": {"halofit_version": "mead2020_feedback", - "HMCode_logT_AGN": best_fit_params['logt_agn']}}) + + cosmo = ccl.Cosmology( + Omega_c=best_fit_params["omch2"] / (best_fit_params["h0"] / 100) ** 2, + Omega_b=best_fit_params["ombh2"] / (best_fit_params["h0"] / 100) ** 2, + h=best_fit_params["h0"] / 100, + n_s=best_fit_params["n_s"], + sigma8=best_fit_params["SIGMA_8"], + baryonic_effects=None, + extra_parameters={ + "camb": { + "halofit_version": "mead2020_feedback", + "HMCode_logT_AGN": best_fit_params["logt_agn"], + } + }, + ) theta_deg = np.rad2deg(theta_rad) - xi_p = ccl.correlation(cosmo, ell=ell, C_ell=shear_cl, theta=theta_deg, type='GG+') - xi_m = ccl.correlation(cosmo, ell=ell, C_ell=shear_cl, theta=theta_deg, type='GG-') - - os.makedirs(output_folder + '{}/best_fit/shear_xi_minus'.format(root), exist_ok=True) - os.makedirs(output_folder + '{}/best_fit/shear_xi_plus'.format(root), exist_ok=True) - - np.savetxt(output_folder + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root), xi_p) - np.savetxt(output_folder + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root), xi_m) - np.savetxt(output_folder + '{}/best_fit/shear_xi_plus/theta.txt'.format(root), theta_rad) - np.savetxt(output_folder + '{}/best_fit/shear_xi_minus/theta.txt'.format(root), theta_rad) - - print(f"Best fit xi+ and xi- from Cl's computed and saved in {output_folder + '{}/best_fit/shear_xi_plus'.format(root)} and {output_folder + '{}/best_fit/shear_xi_minus'.format(root)}") + xi_p = ccl.correlation(cosmo, ell=ell, C_ell=shear_cl, theta=theta_deg, type="GG+") + xi_m = ccl.correlation(cosmo, ell=ell, C_ell=shear_cl, theta=theta_deg, type="GG-") + + os.makedirs( + output_folder + "{}/best_fit/shear_xi_minus".format(root), exist_ok=True + ) + os.makedirs(output_folder + "{}/best_fit/shear_xi_plus".format(root), exist_ok=True) + + np.savetxt( + output_folder + "{}/best_fit/shear_xi_plus/bin_1_1.txt".format(root), xi_p + ) + np.savetxt( + output_folder + "{}/best_fit/shear_xi_minus/bin_1_1.txt".format(root), xi_m + ) + np.savetxt( + output_folder + "{}/best_fit/shear_xi_plus/theta.txt".format(root), theta_rad + ) + np.savetxt( + output_folder + "{}/best_fit/shear_xi_minus/theta.txt".format(root), theta_rad + ) + + print( + f"Best fit xi+ and xi- from Cl's computed and saved in {output_folder + '{}/best_fit/shear_xi_plus'.format(root)} and {output_folder + '{}/best_fit/shear_xi_minus'.format(root)}" + ) + def adjust_paramname_chain(chain, current_name, target_name, label): """ @@ -270,7 +337,6 @@ def adjust_paramname_chain(chain, current_name, target_name, label): chain.setParamNames(param_names) - p = chain.paramNames.parWithName(target_name) def derive_parameter_S8(chain): """ @@ -282,12 +348,11 @@ def derive_parameter_S8(chain): s_8 = sigma_8 * (omega_m / 0.3) ** 0.5 - chain.addDerived(s_8, name='S_8', label=r'S_8') - - p = chain.paramNames.parWithName('S_8') + chain.addDerived(s_8, name="S_8", label=r"S_8") return chain + def derive_parameter_Om(chain): """ Derives the Omega_m parameter from omch2 and h0 in a GetDist chain. @@ -297,13 +362,11 @@ def derive_parameter_Om(chain): omega_m = omch2 / (h0 / 100) ** 2 - chain.addDerived(omega_m, name='OMEGA_M', label=r'\Omega_{\rm m}') - - - p = chain.paramNames.parWithName('OMEGA_M') + chain.addDerived(omega_m, name="OMEGA_M", label=r"\Omega_{\rm m}") return chain + def get_sigma_tension(mean1, low1, high1, mean2, low2, high2): sigma1 = 0.5 * (high1 + low1) sigma2 = 0.5 * (high2 + low2) @@ -312,6 +375,7 @@ def get_sigma_tension(mean1, low1, high1, mean2, low2, high2): sign = 1 if mean1 > mean2 else -1 return sigma_tension * sign + def read_config(path_ini_files, root, thisfile=None): config = configparser.ConfigParser() config.optionxform = str @@ -322,7 +386,10 @@ def read_config(path_ini_files, root, thisfile=None): config.read(read_path) return config -def update_properties_w_roots(properties, root, path_ini_files, path_to_this_ini=None, with_configuration=False): + +def update_properties_w_roots( + properties, root, path_ini_files, path_to_this_ini=None, with_configuration=False +): config = read_config(path_ini_files, root, thisfile=path_to_this_ini) try: @@ -331,219 +398,402 @@ def update_properties_w_roots(properties, root, path_ini_files, path_to_this_ini ) properties[root].update( { - 'lower_bound_cell_ee': lower_bound_cell_ee, - 'upper_bound_cell_ee': upper_bound_cell_ee + "lower_bound_cell_ee": lower_bound_cell_ee, + "upper_bound_cell_ee": upper_bound_cell_ee, } ) except KeyError: - properties[root] = { - 'lower_bound_cell_ee': 0.0, - 'upper_bound_cell_ee': 2048 - } + properties[root] = {"lower_bound_cell_ee": 0.0, "upper_bound_cell_ee": 2048} if with_configuration: # Also save the scale cuts in theta for xi add_xi_sys = config["2pt_like"]["add_xi_sys"] - add_xi_sys = add_xi_sys == 'T' - lower_bound_xi_plus, upper_bound_xi_plus = map(float, config["2pt_like"]["angle_range_XI_PLUS_1_1"].split()) - lower_bound_xi_minus, upper_bound_xi_minus = map(float, config["2pt_like"]["angle_range_XI_MINUS_1_1"].split()) - - properties[root].update({ - 'add_xi_sys': add_xi_sys, - 'lower_bound_xi_plus': lower_bound_xi_plus, - 'upper_bound_xi_plus': upper_bound_xi_plus, - 'lower_bound_xi_minus': lower_bound_xi_minus, - 'upper_bound_xi_minus': upper_bound_xi_minus - }) + add_xi_sys = add_xi_sys == "T" + lower_bound_xi_plus, upper_bound_xi_plus = map( + float, config["2pt_like"]["angle_range_XI_PLUS_1_1"].split() + ) + lower_bound_xi_minus, upper_bound_xi_minus = map( + float, config["2pt_like"]["angle_range_XI_MINUS_1_1"].split() + ) + + properties[root].update( + { + "add_xi_sys": add_xi_sys, + "lower_bound_xi_plus": lower_bound_xi_plus, + "upper_bound_xi_plus": upper_bound_xi_plus, + "lower_bound_xi_minus": lower_bound_xi_minus, + "upper_bound_xi_minus": upper_bound_xi_minus, + } + ) return properties -def plot_best_fit(data_points, root_to_plot, output_folder, line_args, savefile, ell_min=10.0, ell_max=2048.0, multiply_ell=True, loc_legend="best", bbox_to_anchor=None, label_data="Fiducial data", labels=None, properties=None, paths_to_bestfit=None): - data = fits.open(f'/home/guerrini/sp_validation/cosmo_inference/data/{data_points}/cosmosis_{data_points}.fits') - cell_ee = data['CELL_EE'].data - cov_mat = data['COVMAT'].data +def plot_best_fit( + data_points, + root_to_plot, + output_folder, + line_args, + savefile, + ell_min=10.0, + ell_max=2048.0, + multiply_ell=True, + loc_legend="best", + bbox_to_anchor=None, + label_data="Fiducial data", + labels=None, + properties=None, + paths_to_bestfit=None, +): + data = fits.open( + f"/home/guerrini/sp_validation/cosmo_inference/data/{data_points}/cosmosis_{data_points}.fits" + ) + cell_ee = data["CELL_EE"].data + cov_mat = data["COVMAT"].data if labels is None: labels = root_to_plot fig, ax = plt.subplots(1, 1, figsize=(8, 5)) - ell, cell = cell_ee['ANG'], cell_ee['VALUE'] - ax.errorbar(ell, ell*cell, yerr=ell*np.sqrt(np.diag(cov_mat)), fmt='o', label=label_data, color='black', capsize=2) - + ell, cell = cell_ee["ANG"], cell_ee["VALUE"] + ax.errorbar( + ell, + ell * cell, + yerr=ell * np.sqrt(np.diag(cov_mat)), + fmt="o", + label=label_data, + color="black", + capsize=2, + ) + for idx, (label, root) in enumerate(zip(labels, root_to_plot)): - lower_bound_cell_ee = properties[root]['lower_bound_cell_ee'] - upper_bound_cell_ee = properties[root]['upper_bound_cell_ee'] - - #Read the results + # Read the results if paths_to_bestfit is None: - ell = np.loadtxt(output_folder + '{}/best_fit/shear_cl/ell.txt'.format(root, root)) - shear_cl = np.loadtxt(output_folder + '{}/best_fit/shear_cl/bin_1_1.txt'.format(root, root)) + ell = np.loadtxt( + output_folder + + "{}/best_fit/shear_cl/ell.txt".format( + root, + ) + ) + shear_cl = np.loadtxt( + output_folder + + "{}/best_fit/shear_cl/bin_1_1.txt".format( + root, + ) + ) else: - ell = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_cl/ell.txt') - shear_cl = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_cl/bin_1_1.txt') + ell = np.loadtxt(paths_to_bestfit[idx] + "best_fit/shear_cl/ell.txt") + shear_cl = np.loadtxt( + paths_to_bestfit[idx] + "best_fit/shear_cl/bin_1_1.txt" + ) mask = (ell > ell_min) & (ell < ell_max) - ax.plot(ell[mask], ell[mask]*shear_cl[mask] if multiply_ell else shear_cl[mask], label=label, **line_args[idx]) - + ax.plot( + ell[mask], + ell[mask] * shear_cl[mask] if multiply_ell else shear_cl[mask], + label=label, + **line_args[idx], + ) + # Plot the scale cuts for different k_max - ax.axvline(x=1800, color='black', linestyle='--', alpha=0.5) - ax.axvline(x=2048, color='black', linestyle='--', alpha=1.0) - ax.axvline(x=500, color='black', linestyle='--', alpha=0.3) + ax.axvline(x=1800, color="black", linestyle="--", alpha=0.5) + ax.axvline(x=2048, color="black", linestyle="--", alpha=1.0) + ax.axvline(x=500, color="black", linestyle="--", alpha=0.3) ymin = ax.get_ylim()[0] ymax = ax.get_ylim()[1] # Shadowing cut scaled - ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=300, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') - ax.fill_betweenx(y=[ymin, ymax], x1=1600, x2=2048, color='gray', alpha=0.2) + ax.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=300, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", + ) + ax.fill_betweenx(y=[ymin, ymax], x1=1600, x2=2048, color="gray", alpha=0.2) ax.set_ylim(ymin, ymax) # Add labels directly under the tick - ax.text(1740, 0.90, - r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=14, rotation=90) - - ax.text(1978, 0.90, - r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=14, rotation=90) - - ax.text(470, 0.90, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - transform=ax.get_xaxis_transform(), - ha='center', va='top', fontsize=14, rotation=90) - - ell, cell = cell_ee['ANG'], cell_ee['VALUE'] - ax.set_ylabel(r'$\ell C_\ell \times 10^{-7}$', fontsize=20) - ax.set_xlabel(r'Multipole $\ell$', fontsize=20) - ax.set_xlim(ell.min()-10, ell.max()+100) - ax.set_xscale('squareroot') + ax.text( + 1740, + 0.90, + r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ax.text( + 1978, + 0.90, + r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ax.text( + 470, + 0.90, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + transform=ax.get_xaxis_transform(), + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ell, cell = cell_ee["ANG"], cell_ee["VALUE"] + ax.set_ylabel(r"$\ell C_\ell \times 10^{-7}$", fontsize=20) + ax.set_xlabel(r"Multipole $\ell$", fontsize=20) + ax.set_xlim(ell.min() - 10, ell.max() + 100) + ax.set_xscale("squareroot") ax.set_xticks(np.array([100, 400, 900, 1600])) ax.minorticks_on() ax.tick_params(axis="x", which="minor", length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax.xaxis.set_ticks(minor_ticks, minor=True) - ax.tick_params(axis='both', which='major', labelsize=14) - ax.tick_params(axis='both', which='minor', labelsize=10) + ax.tick_params(axis="both", which="major", labelsize=14) + ax.tick_params(axis="both", which="minor", labelsize=10) ax.yaxis.get_offset_text().set_visible(False) - plt.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor, fontsize=11) if savefile is not None: - plt.savefig(savefile, bbox_inches='tight') + plt.savefig(savefile, bbox_inches="tight") plt.show() -def plot_best_fit_config(data, root_to_plot, output_folder, line_args, savefile, theta_min=1.0, theta_max=250.0, multiply_theta=True, loc_legend="best", bbox_to_anchor_xip=None, bbox_to_anchor_xim=None, label_data="Fiducial data", labels=None, properties=None, paths_to_bestfit=None): - + +def plot_best_fit_config( + data, + root_to_plot, + output_folder, + line_args, + savefile, + theta_min=1.0, + theta_max=250.0, + multiply_theta=True, + loc_legend="best", + bbox_to_anchor_xip=None, + bbox_to_anchor_xim=None, + label_data="Fiducial data", + labels=None, + properties=None, + paths_to_bestfit=None, +): + data = fits.open(data) - - xi_p_data = data['XI_PLUS'].data - xi_m_data = data['XI_MINUS'].data - cov_mat = data['COVMAT'].data + + xi_p_data = data["XI_PLUS"].data + xi_m_data = data["XI_MINUS"].data + cov_mat = data["COVMAT"].data # Plot hyperparameter loc_legend = "lower center" - fig, [ax,ax2] = plt.subplots(2, 1, figsize=(8, 9)) + fig, [ax, ax2] = plt.subplots(2, 1, figsize=(8, 9)) + + theta, xi_p, xi_m = xi_p_data["ANG"], xi_p_data["VALUE"], xi_m_data["VALUE"] + ax.errorbar( + theta, + theta * xi_p, + yerr=theta * np.sqrt(np.diag(cov_mat[: len(theta), : len(theta)])), + fmt="o", + label=r"UNIONS $\xi_+$ data", + color="black", + capsize=2, + ) + ax2.errorbar( + theta, + theta * xi_m, + yerr=theta + * np.sqrt( + np.diag(cov_mat[len(theta) : 2 * len(theta), len(theta) : 2 * len(theta)]) + ), + fmt="o", + label=r"UNIONS $\xi_-$ data", + color="black", + capsize=2, + ) - theta, xi_p, xi_m = xi_p_data['ANG'], xi_p_data['VALUE'], xi_m_data['VALUE'] - ax.errorbar(theta, theta*xi_p, yerr=theta*np.sqrt(np.diag(cov_mat[:len(theta),:len(theta)])), fmt='o', label=r"UNIONS $\xi_+$ data", color='black', capsize=2) - ax2.errorbar(theta, theta*xi_m, yerr=theta*np.sqrt(np.diag(cov_mat[len(theta):2*len(theta),len(theta):2*len(theta)])), fmt='o', label=r"UNIONS $\xi_-$ data", color='black', capsize=2) - for idx, (label, root) in enumerate(zip(labels, root_to_plot)): - #Read the results + # Read the results if paths_to_bestfit is None: - theta = (np.loadtxt(output_folder + '{}/best_fit/shear_xi_plus/theta.txt'.format(root))) * 180/np.pi * 60 - xi_plus = np.loadtxt(output_folder + '{}/best_fit/shear_xi_plus/bin_1_1.txt'.format(root)) - xi_minus = np.loadtxt(output_folder + '{}/best_fit/shear_xi_minus/bin_1_1.txt'.format(root)) - if '$C_\ell$' not in label: - xi_sys_plus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root)) - xi_sys_minus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root)) - theta_xi_sys = np.loadtxt(output_folder + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60 + theta = ( + ( + np.loadtxt( + output_folder + + "{}/best_fit/shear_xi_plus/theta.txt".format(root) + ) + ) + * 180 + / np.pi + * 60 + ) + xi_plus = np.loadtxt( + output_folder + "{}/best_fit/shear_xi_plus/bin_1_1.txt".format(root) + ) + xi_minus = np.loadtxt( + output_folder + "{}/best_fit/shear_xi_minus/bin_1_1.txt".format(root) + ) + if r"$C_\ell$" not in label: + xi_sys_plus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_plus.txt".format(root) + ) + xi_sys_minus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_minus.txt".format(root) + ) + theta_xi_sys = ( + np.loadtxt( + output_folder + "{}/best_fit/xi_sys/theta.txt".format(root) + ) + * 180 + / np.pi + * 60 + ) xi_plus += np.interp(theta, theta_xi_sys, xi_sys_plus) xi_minus += np.interp(theta, theta_xi_sys, xi_sys_minus) else: - theta = (np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_xi_plus/theta.txt')) * 180/np.pi * 60 - xi_plus = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_xi_plus/bin_1_1.txt') - xi_minus = np.loadtxt(paths_to_bestfit[idx] + 'best_fit/shear_xi_minus/bin_1_1.txt') - if '$C_\ell$' not in label: - xi_sys_plus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_plus.txt'.format(root)) - xi_sys_minus = np.loadtxt(output_folder + '{}/best_fit/xi_sys/shear_xi_minus.txt'.format(root)) - theta_xi_sys = np.loadtxt(output_folder + '{}/best_fit/xi_sys/theta.txt'.format(root)) * 180/np.pi * 60 + theta = ( + (np.loadtxt(paths_to_bestfit[idx] + "best_fit/shear_xi_plus/theta.txt")) + * 180 + / np.pi + * 60 + ) + xi_plus = np.loadtxt( + paths_to_bestfit[idx] + "best_fit/shear_xi_plus/bin_1_1.txt" + ) + xi_minus = np.loadtxt( + paths_to_bestfit[idx] + "best_fit/shear_xi_minus/bin_1_1.txt" + ) + if r"$C_\ell$" not in label: + xi_sys_plus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_plus.txt".format(root) + ) + xi_sys_minus = np.loadtxt( + output_folder + "{}/best_fit/xi_sys/shear_xi_minus.txt".format(root) + ) + theta_xi_sys = ( + np.loadtxt( + output_folder + "{}/best_fit/xi_sys/theta.txt".format(root) + ) + * 180 + / np.pi + * 60 + ) xi_plus += np.interp(theta, theta_xi_sys, xi_sys_plus) xi_minus += np.interp(theta, theta_xi_sys, xi_sys_minus) mask = (theta > theta_min) & (theta < theta_max) theta = theta[mask] - ax.plot(theta, theta*xi_plus[mask] if multiply_theta else xi_plus[mask], label=label, **line_args[idx]) - ax2.plot(theta, theta*xi_minus[mask] if multiply_theta else xi_minus[mask], label=label, **line_args[idx]) - + ax.plot( + theta, + theta * xi_plus[mask] if multiply_theta else xi_plus[mask], + label=label, + **line_args[idx], + ) + ax2.plot( + theta, + theta * xi_minus[mask] if multiply_theta else xi_minus[mask], + label=label, + **line_args[idx], + ) + # XI PLUS PLOT SETTINGS - + # Plot the scale cuts for different k_max - ax.axvline(x=3.2, color='black', linestyle='--', alpha=0.7) + ax.axvline(x=3.2, color="black", linestyle="--", alpha=0.7) ymin = ax.get_ylim()[0] ymax = ax.get_ylim()[1] # Shadowing cut scaled - ax.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') - ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2) + ax.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=12, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", + ) + ax.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color="gray", alpha=0.2) ax.set_ylim(ymin, ymax) # Add labels directly under the tick - ax.text(2.9, 1.23e-4, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - ha='center', va='top', fontsize=14, rotation=90) + ax.text( + 2.9, + 1.23e-4, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + ha="center", + va="top", + fontsize=14, + rotation=90, + ) # ax.set_ylabel('$\theta \xi_+$', fontsize=16) # ax.set_xlabel('$\theta$', fontsize=16) - ax.set_xlim([theta.min()-0.1, theta.max()+20]) - ax.set_xscale('log') + ax.set_xlim([theta.min() - 0.1, theta.max() + 20]) + ax.set_xscale("log") ax.set_xticks(np.array([1, 10, 100])) ax.tick_params(axis="x", which="minor", length=2, width=0.8) - ax.tick_params(axis='both', which='major', labelsize=14) - ax.tick_params(axis='both', which='minor', labelsize=10) + ax.tick_params(axis="both", which="major", labelsize=14) + ax.tick_params(axis="both", which="minor", labelsize=10) ax.yaxis.get_offset_text().set_fontsize(14) - ax.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) + ax.ticklabel_format(axis="y", style="sci", scilimits=(0, 0)) ax.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xip, fontsize=12) # XI_MINUS PLOT SETTINGS - + # Plot the scale cuts for different k_max - ax2.axvline(x=24, color='black', linestyle='--', alpha=0.7) + ax2.axvline(x=24, color="black", linestyle="--", alpha=0.7) ymin = ax2.get_ylim()[0] ymax = ax2.get_ylim()[1] # Shadowing cut scaled - ax2.fill_betweenx(y=[ymin, ymax], x1=0, x2=12, color='gray', alpha=0.2, label=r'$B$-mode informed scale cut') - ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color='gray', alpha=0.2) + ax2.fill_betweenx( + y=[ymin, ymax], + x1=0, + x2=12, + color="gray", + alpha=0.2, + label=r"$B$-mode informed scale cut", + ) + ax2.fill_betweenx(y=[ymin, ymax], x1=83, x2=250, color="gray", alpha=0.2) ax2.set_ylim(ymin, ymax) # Add labels directly under the tick - ax2.text(21.8, 1.15e-4, - r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", - ha='center', va='top', fontsize=14, rotation=90) - - ax2.set_ylabel(r'$\theta \xi_-$', fontsize=16) - ax2.set_xlabel(r'$\theta$', fontsize=16) - ax2.set_xlim([theta.min()-0.1, theta.max()+20]) - ax2.set_xscale('log') + ax2.text( + 21.8, + 1.15e-4, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + ha="center", + va="top", + fontsize=14, + rotation=90, + ) + + ax2.set_ylabel(r"$\theta \xi_-$", fontsize=16) + ax2.set_xlabel(r"$\theta$", fontsize=16) + ax2.set_xlim([theta.min() - 0.1, theta.max() + 20]) + ax2.set_xscale("log") ax2.set_xticks(np.array([1, 10, 100])) ax2.tick_params(axis="x", which="minor", length=2, width=0.8) - ax2.tick_params(axis='both', which='major', labelsize=14) - ax2.tick_params(axis='both', which='minor', labelsize=10) + ax2.tick_params(axis="both", which="major", labelsize=14) + ax2.tick_params(axis="both", which="minor", labelsize=10) ax2.yaxis.get_offset_text().set_fontsize(14) - ax2.ticklabel_format(axis='y', style='sci', scilimits=(0,0)) + ax2.ticklabel_format(axis="y", style="sci", scilimits=(0, 0)) ax2.legend(loc=loc_legend, bbox_to_anchor=bbox_to_anchor_xim, fontsize=12) if savefile is not None: - plt.savefig(savefile, bbox_inches='tight') + plt.savefig(savefile, bbox_inches="tight") - plt.show() \ No newline at end of file + plt.show() diff --git a/cosmo_inference/scripts/cosmocov_process.py b/cosmo_inference/scripts/cosmocov_process.py index eb10444b..c2c00996 100644 --- a/cosmo_inference/scripts/cosmocov_process.py +++ b/cosmo_inference/scripts/cosmocov_process.py @@ -1,80 +1,81 @@ #!/usr/bin/env python +import sys + import matplotlib.pyplot as plt import numpy as np -import sys + def get_cov(filename): - data = np.loadtxt(filename) - ndata = int(np.max(data[:,0]))+1 - - print("Dimension of cov: %dx%d"%(ndata,ndata)) - - cov_g = np.zeros((ndata,ndata)) - cov_ng = np.zeros((ndata,ndata)) - for i in range(0,data.shape[0]): - cov_g[int(data[i,0]),int(data[i,1])] =data[i,8] - cov_g[int(data[i,1]),int(data[i,0])] =data[i,8] - cov_ng[int(data[i,0]),int(data[i,1])] =data[i,9] - cov_ng[int(data[i,1]),int(data[i,0])] =data[i,9] - - return cov_g, cov_ng, ndata - - -if __name__ == '__main__': - - if len(sys.argv) != 3: - print("Usage: python cosmocov_process.py ") - sys.exit(1) - - covfile = sys.argv[1] - output_base = sys.argv[2] - - c_g, c_ng, ndata = get_cov(covfile) - - cov = c_ng+c_g - cov_g = c_g - - b = np.sort(np.linalg.eigvals(cov)) - print("min+max eigenvalues cov: %e, %e"%(np.min(b), np.max(b))) - if(np.min(b)<=0.): - print("non-positive eigenvalue encountered! Covariance Invalid!") - exit() - - print("Covariance is positive definite!") - - np.savetxt(str(output_base)+'.txt',cov) - print("covmat saved as %s" %(str(output_base)+'.txt')) - - np.savetxt(str(output_base)+'_g.txt',cov_g) - print("Gaussian covmat saved as %s" %(str(output_base)+'_g.txt')) - - cmap = 'seismic' - - pp_norm = np.zeros((ndata,ndata)) - for i in range(ndata): - for j in range(ndata): - pp_norm[i][j] = cov[i][j]/ np.sqrt(cov[i][i]*cov[j][j]) - - print("Plotting correlation matrix ...") - - plot_path = str(output_base)+'_plot.pdf' - fig = plt.figure() - ax = fig.add_subplot(1, 1, 1) - extent = (0, ndata, ndata, 0) - im3 = ax.imshow(pp_norm, cmap=cmap, vmin=-1, vmax=1, extent=extent) - - plt.axvline(x=int(ndata/2),color='black',linewidth=1.0) - plt.axhline(y=int(ndata/2),color='black',linewidth=1.0) - - fig.colorbar(im3, orientation='vertical') - - ax.text(int(ndata/4), ndata+5, r'$\xi_+^{ij}(\theta)$', fontsize=12) - ax.text(3*int(ndata/4), ndata+5, r'$\xi_-^{ij}(\theta)$', fontsize=12) - ax.text(-9, int(ndata/4), r'$\xi_+^{ij}(\theta)$', fontsize=12) - ax.text(-9, 3*int(ndata/4), r'$\xi_-^{ij}(\theta)$', fontsize=12) - - plt.savefig(plot_path,dpi=2000) - plt.close() - print("Plot saved as %s"%(plot_path)) \ No newline at end of file + data = np.loadtxt(filename) + ndata = int(np.max(data[:, 0])) + 1 + + print("Dimension of cov: %dx%d" % (ndata, ndata)) + + cov_g = np.zeros((ndata, ndata)) + cov_ng = np.zeros((ndata, ndata)) + for i in range(0, data.shape[0]): + cov_g[int(data[i, 0]), int(data[i, 1])] = data[i, 8] + cov_g[int(data[i, 1]), int(data[i, 0])] = data[i, 8] + cov_ng[int(data[i, 0]), int(data[i, 1])] = data[i, 9] + cov_ng[int(data[i, 1]), int(data[i, 0])] = data[i, 9] + + return cov_g, cov_ng, ndata + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python cosmocov_process.py ") + sys.exit(1) + + covfile = sys.argv[1] + output_base = sys.argv[2] + + c_g, c_ng, ndata = get_cov(covfile) + + cov = c_ng + c_g + cov_g = c_g + + b = np.sort(np.linalg.eigvals(cov)) + print("min+max eigenvalues cov: %e, %e" % (np.min(b), np.max(b))) + if np.min(b) <= 0.0: + print("non-positive eigenvalue encountered! Covariance Invalid!") + exit() + + print("Covariance is positive definite!") + + np.savetxt(str(output_base) + ".txt", cov) + print("covmat saved as %s" % (str(output_base) + ".txt")) + + np.savetxt(str(output_base) + "_g.txt", cov_g) + print("Gaussian covmat saved as %s" % (str(output_base) + "_g.txt")) + + cmap = "seismic" + + pp_norm = np.zeros((ndata, ndata)) + for i in range(ndata): + for j in range(ndata): + pp_norm[i][j] = cov[i][j] / np.sqrt(cov[i][i] * cov[j][j]) + + print("Plotting correlation matrix ...") + + plot_path = str(output_base) + "_plot.pdf" + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + extent = (0, ndata, ndata, 0) + im3 = ax.imshow(pp_norm, cmap=cmap, vmin=-1, vmax=1, extent=extent) + + plt.axvline(x=int(ndata / 2), color="black", linewidth=1.0) + plt.axhline(y=int(ndata / 2), color="black", linewidth=1.0) + + fig.colorbar(im3, orientation="vertical") + + ax.text(int(ndata / 4), ndata + 5, r"$\xi_+^{ij}(\theta)$", fontsize=12) + ax.text(3 * int(ndata / 4), ndata + 5, r"$\xi_-^{ij}(\theta)$", fontsize=12) + ax.text(-9, int(ndata / 4), r"$\xi_+^{ij}(\theta)$", fontsize=12) + ax.text(-9, 3 * int(ndata / 4), r"$\xi_-^{ij}(\theta)$", fontsize=12) + + plt.savefig(plot_path, dpi=2000) + plt.close() + print("Plot saved as %s" % (plot_path)) diff --git a/cosmo_inference/scripts/cosmosis_fitting.py b/cosmo_inference/scripts/cosmosis_fitting.py index edbfd126..6cfe8be8 100644 --- a/cosmo_inference/scripts/cosmosis_fitting.py +++ b/cosmo_inference/scripts/cosmosis_fitting.py @@ -183,7 +183,9 @@ def cov_cl_to_fits(cov_file, cov_hdu="COVAR_FULL"): elif cov_file.endswith(".npy"): cov_data = np.load(cov_file) else: - raise NotImplementedError(f"Unsupported pseudo-Cl covariance format: {cov_file}") + raise NotImplementedError( + f"Unsupported pseudo-Cl covariance format: {cov_file}" + ) if cov_data.shape[0] != cov_data.shape[1]: raise ValueError("Pseudo-Cl covariance matrix must be square") @@ -246,7 +248,9 @@ def rho_to_fits(filename, theta=None): return rho_stat_hdu -def covdat_to_fits(filename_cov_xi, filename_cov_tau=None, filename_cov_cl=None, cov_hdu=None): +def covdat_to_fits( + filename_cov_xi, filename_cov_tau=None, filename_cov_cl=None, cov_hdu=None +): """ Convert CosmoCov covariance matrix to FITS format. @@ -274,7 +278,9 @@ def covdat_to_fits(filename_cov_xi, filename_cov_tau=None, filename_cov_cl=None, elif filename_cov_cl.endswith(".npy"): cov_data = np.load(filename_cov_cl) else: - raise NotImplementedError(f"Unsupported pseudo-Cl covariance format: {filename_cov_cl}") + raise NotImplementedError( + f"Unsupported pseudo-Cl covariance format: {filename_cov_cl}" + ) covmat = np.block( [ [covmat, np.zeros((len(covmat), len(cov_data)))], @@ -297,17 +303,21 @@ def covdat_to_fits(filename_cov_xi, filename_cov_tau=None, filename_cov_cl=None, } if filename_cov_tau: - cov_dict.update({ - "NAME_2": "TAU_0_PLUS", - "STRT_2": len(covmat_xi), - "NAME_3": "TAU_2_PLUS", - "STRT_3": len(covmat_xi) + int(len(covmat_tau) / 2), - }) - - filename_cov_cl and cov_dict.update({ + cov_dict.update( + { + "NAME_2": "TAU_0_PLUS", + "STRT_2": len(covmat_xi), + "NAME_3": "TAU_2_PLUS", + "STRT_3": len(covmat_xi) + int(len(covmat_tau) / 2), + } + ) + + filename_cov_cl and cov_dict.update( + { "NAME_4": "CELL_EE", "STRT_4": len(covmat_xi) + (len(covmat_tau) if filename_cov_tau else 0), - }) + } + ) for key, value in cov_dict.items(): cov_hdu.header[key] = value @@ -366,7 +376,10 @@ def _generate_ini_file( ): """Generate a CosmoSIS INI configuration file from template with modifications.""" template_path = Path(args.template_dir) / template_base - output_path = Path(args.output_config_dir) / f"cosmosis_pipeline_{args.config_name_base}{suffix}.ini" + output_path = ( + Path(args.output_config_dir) + / f"cosmosis_pipeline_{args.config_name_base}{suffix}.ini" + ) with open(template_path, "r") as f: config_content = f.read() @@ -582,9 +595,7 @@ def parse_args(): required=False, help="Path to pseudo-C_ell covariance matrix (required if --cl-file)", ) - parser.add_argument( - "--mock", action="store_true", help="Mock data mode" - ) + parser.add_argument("--mock", action="store_true", help="Mock data mode") parser.add_argument( "--output-root", type=str, @@ -690,7 +701,9 @@ def parse_args(): else: # Data mode expects two TreeCorr files (xi_plus, xi_minus) if len(args.xi) != 2: - raise ValueError(f"Data mode requires exactly 2 xi files, got {len(args.xi)}") + raise ValueError( + f"Data mode requires exactly 2 xi files, got {len(args.xi)}" + ) xip_hdu, xim_hdu = treecorr_to_fits(args.xi[0], args.xi[1]) xi_theta = xip_hdu.data["ANG"] @@ -733,7 +746,12 @@ def parse_args(): print("Loaded rho/tau statistics") if args.cl_file: - cov_hdu = covdat_to_fits(args.cov_xi, filename_cov_tau=args.cov_tau, filename_cov_cl=args.cov_cl, cov_hdu="COVAR_FULL") + cov_hdu = covdat_to_fits( + args.cov_xi, + filename_cov_tau=args.cov_tau, + filename_cov_cl=args.cov_cl, + cov_hdu="COVAR_FULL", + ) else: cov_hdu = covdat_to_fits(args.cov_xi, filename_cov_tau=args.cov_tau) print("Loaded combined covariance with tau") @@ -746,7 +764,9 @@ def parse_args(): if cov_hdu is not None: hdu_list.append(cov_hdu) else: - cov_cl_hdu.header["EXTNAME"] = "COVMAT" # If no xi use covmat for the extname of the cl cov + cov_cl_hdu.header["EXTNAME"] = ( + "COVMAT" # If no xi use covmat for the extname of the cl cov + ) if cov_cl_hdu is not None: hdu_list.append(cov_cl_hdu) if args.xi: diff --git a/cosmo_inference/scripts/masking.py b/cosmo_inference/scripts/masking.py index 6d1652b0..08a04feb 100644 --- a/cosmo_inference/scripts/masking.py +++ b/cosmo_inference/scripts/masking.py @@ -1,11 +1,11 @@ import argparse +import os +from multiprocessing import Pool, cpu_count +from pathlib import Path + import h5py import healpy as hp import numpy as np -from multiprocessing import Pool, cpu_count -import os -import sys -from pathlib import Path import yaml # ------------------------- @@ -14,17 +14,25 @@ # relative size) are per-galaxy quality cuts that should NOT affect # the footprint definition. SPATIAL_CUTS = { - "overlap", "IMAFLAGS_ISO", "N_EPOCH", - "4_Stars", "8_Manual", "64_r", "1024_Maximask", "npoint3", - "1_Faint_star_halos", "2_Bright_star_halos", + "overlap", + "IMAFLAGS_ISO", + "N_EPOCH", + "4_Stars", + "8_Manual", + "64_r", + "1024_Maximask", + "npoint3", + "1_Faint_star_halos", + "2_Bright_star_halos", } # ------------------------- # Masking logic + def apply_condition(array, kind, value): """ - Apply a logical condition to a NumPy array and return a boolean mask, based + Apply a logical condition to a NumPy array and return a boolean mask, based on the "kind" key in the mask config YAML file. """ if kind == "equal": @@ -44,6 +52,7 @@ def apply_condition(array, kind, value): else: raise ValueError(f"Unknown kind: {kind}") + def apply_masks(data, data_ext, mask_config, footprint_only=False): """ Construct a boolean mask selecting galaxies that satisfy all @@ -111,7 +120,7 @@ def apply_masks(data, data_ext, mask_config, footprint_only=False): data["NGMIX_T_NOSHEAR"], data["NGMIX_Tpsf_NOSHEAR"], out=np.zeros_like(data["NGMIX_T_NOSHEAR"]), - where=(data["NGMIX_Tpsf_NOSHEAR"] > 0) + where=(data["NGMIX_Tpsf_NOSHEAR"] > 0), ) rel_min = mask_config["metacal"]["gal_rel_size_min"] @@ -121,6 +130,7 @@ def apply_masks(data, data_ext, mask_config, footprint_only=False): return mask + # ------------------------- # Process one chunk def process_chunk(args): @@ -165,17 +175,19 @@ def process_chunk(args): ra = data["RA"][mask] dec = data["Dec"][mask] - theta = np.radians(90.0 - dec) # colatitude - phi = np.radians(ra) # longitude + theta = np.radians(90.0 - dec) # colatitude + phi = np.radians(ra) # longitude pix = hp.ang2pix(nside, theta, phi) - + return np.unique(pix) + # ------------------------- # Build mask map in parallel -def build_mask_map_hdf5(filename, mask_config, nside, chunk_size=1_000_000, - footprint_only=False): +def build_mask_map_hdf5( + filename, mask_config, nside, chunk_size=1_000_000, footprint_only=False +): """ Build a binary HEALPix mask map from an HDF5 galaxy catalogue. @@ -209,9 +221,10 @@ def build_mask_map_hdf5(filename, mask_config, nside, chunk_size=1_000_000, with h5py.File(filename, "r") as f: nrows = f["data"].shape[0] - chunks = [(i, min(i+chunk_size, nrows), filename, nside, mask_config, - footprint_only) - for i in range(0, nrows, chunk_size)] + chunks = [ + (i, min(i + chunk_size, nrows), filename, nside, mask_config, footprint_only) + for i in range(0, nrows, chunk_size) + ] mask_map = np.zeros(hp.nside2npix(nside), dtype=np.uint8) @@ -221,18 +234,27 @@ def build_mask_map_hdf5(filename, mask_config, nside, chunk_size=1_000_000, return mask_map + ############################################################################################################ if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Build HEALPix mask from HDF5 catalog") parser.add_argument("nside", type=int, help="HEALPix NSIDE parameter") parser.add_argument("--config", required=True, help="Path to mask config YAML") - parser.add_argument("--output-prefix", required=True, - help="Output file prefix (e.g. 'footprint' or 'footprint_starhalo')") - parser.add_argument("--footprint-only", action="store_true", - help="Only apply spatially-structured cuts (for footprint definition)") - parser.add_argument("--output-dir", default=None, - help="Output directory (default: data/mask/ relative to script)") + parser.add_argument( + "--output-prefix", + required=True, + help="Output file prefix (e.g. 'footprint' or 'footprint_starhalo')", + ) + parser.add_argument( + "--footprint-only", + action="store_true", + help="Only apply spatially-structured cuts (for footprint definition)", + ) + parser.add_argument( + "--output-dir", + default=None, + help="Output directory (default: data/mask/ relative to script)", + ) args = parser.parse_args() nside = args.nside @@ -254,20 +276,25 @@ def build_mask_map_hdf5(filename, mask_config, nside, chunk_size=1_000_000, print(f"Footprint-only mode: applying only spatial cuts {SPATIAL_CUTS}") # Build mask map from comprehensive catalogue - mask_map = build_mask_map_hdf5(filename, mask_config, nside, chunk_size=500_000, - footprint_only=args.footprint_only) + mask_map = build_mask_map_hdf5( + filename, + mask_config, + nside, + chunk_size=500_000, + footprint_only=args.footprint_only, + ) # Get survey area after masking npix = hp.nside2npix(nside) - pix_area_sr = 4 * np.pi / npix - pix_area_deg2 = (180/np.pi)**2 * pix_area_sr + pix_area_sr = 4 * np.pi / npix + pix_area_deg2 = (180 / np.pi) ** 2 * pix_area_sr n_obs = mask_map.sum() f_sky_obs = n_obs / npix area_obs_deg2 = n_obs * pix_area_deg2 print(f"Kept area = {area_obs_deg2:.2f} deg^2\n") # Compute Cls of the mask map - cl_mask = hp.anafast(mask_map, lmax=3*nside-1) + cl_mask = hp.anafast(mask_map, lmax=3 * nside - 1) ells = np.arange(len(cl_mask)) # Save mask map and Cls @@ -280,7 +307,7 @@ def build_mask_map_hdf5(filename, mask_config, nside, chunk_size=1_000_000, print(f"Mask Cls saved to {cls_path}\n") # Compute normalising factor for the mask Cls - integral_w = np.sum((2*ells + 1) / (4 * np.pi) * cl_mask) / (np.pi/180)**2 + integral_w = np.sum((2 * ells + 1) / (4 * np.pi) * cl_mask) / (np.pi / 180) ** 2 norm_factor = area_obs_deg2 / integral_w norm_cls = cl_mask * norm_factor diff --git a/cosmo_inference/scripts/matching.py b/cosmo_inference/scripts/matching.py index 47b71fab..a465e449 100644 --- a/cosmo_inference/scripts/matching.py +++ b/cosmo_inference/scripts/matching.py @@ -3,33 +3,31 @@ Created on Wed Mar 1 17:37:27 2023 @author: fh272693 """ -import os -import numpy as np -import matplotlib.pyplot as plt -from astropy.io import fits -import treecorr -from astropy.coordinates import match_coordinates_sky + import astropy.units as u -from astropy.coordinates import SkyCoord +from astropy.coordinates import SkyCoord, match_coordinates_sky +from astropy.io import fits -Cat1=fits.open('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/unions_shapepipe_2022_v1.0.fits') -Cat2=fits.open('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/lensfit_goldshape_2022v1.fits') +Cat1 = fits.open( + "/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/unions_shapepipe_2022_v1.0.fits" +) +Cat2 = fits.open( + "/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/lensfit_goldshape_2022v1.fits" +) coord_units = u.degree Cat1_coord = SkyCoord( - ra=Cat1[1].data['ra'] * coord_units, - dec=Cat1[1].data['dec'] * coord_units + ra=Cat1[1].data["ra"] * coord_units, dec=Cat1[1].data["dec"] * coord_units ) Cat2_coord = SkyCoord( - ra=Cat2[1].data['ra'] * coord_units, - dec=Cat2[1].data['dec'] * coord_units + ra=Cat2[1].data["ra"] * coord_units, dec=Cat2[1].data["dec"] * coord_units ) -idx, d2d, d3d = match_coordinates_sky(Cat1_coord, Cat2_coord) -max_sep = 1. * u.arcsec +idx, d2d, d3d = match_coordinates_sky(Cat1_coord, Cat2_coord) +max_sep = 1.0 * u.arcsec sep_constraint = d2d < max_sep -#Important here is that the first catalogue of match_coordinates_sky has -#indices [sep_constraint] and the second[idx[sep_constraint]] +# Important here is that the first catalogue of match_coordinates_sky has +# indices [sep_constraint] and the second[idx[sep_constraint]] Cat1_matches = Cat1[1].data[sep_constraint] # np.save('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/shapepipe_unmatches_ra.npy',Cat1[1].data['ra'][sep_constraint]) @@ -38,5 +36,5 @@ # np.save('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/shapepipe_unmatches_e2.npy',Cat1[1].data['e2'][sep_constraint]) # np.save('/feynman/work/dap/lcs/lg268561/UNIONS/Catalogues/shapepipe_unmatches_w.npy',Cat1[1].data['w'][sep_constraint]) -print('there are ',len(Cat1_matches),' matching galaxies in catalogue', Cat1) -# print('there are ',len(Cat2_matches),' matching galaxies in catalogue', Cat2) \ No newline at end of file +print("there are ", len(Cat1_matches), " matching galaxies in catalogue", Cat1) +# print('there are ',len(Cat2_matches),' matching galaxies in catalogue', Cat2) diff --git a/cosmo_inference/scripts/nz_writeout.py b/cosmo_inference/scripts/nz_writeout.py index 8df4ead8..81994335 100644 --- a/cosmo_inference/scripts/nz_writeout.py +++ b/cosmo_inference/scripts/nz_writeout.py @@ -4,6 +4,7 @@ # In[ ]: import sys + import matplotlib.pylab as plt import numpy as np from astropy.io import fits @@ -13,13 +14,13 @@ blind = sys.argv[3] hdu = fits.open(nz_hdu) -z = hdu[1].data['Z_%s' %blind] +z = hdu[1].data["Z_%s" % blind] zmax = 5.0 -(n,bins,_)= plt.hist(z, bins=200, range=(0,zmax), density=True, weights=None) +(n, bins, _) = plt.hist(z, bins=200, range=(0, zmax), density=True, weights=None) -print("zmin = ",min(z)) -print("zmax = ",max(z)) +print("zmin = ", min(z)) +print("zmax = ", max(z)) -np.savetxt('data/'+root+'/nz_'+root+'.txt',np.column_stack((bins[:-1],n))) \ No newline at end of file +np.savetxt("data/" + root + "/nz_" + root + ".txt", np.column_stack((bins[:-1], n))) diff --git a/cosmo_inference/scripts/treecorr_calc.py b/cosmo_inference/scripts/treecorr_calc.py index 6ada7d00..38eb855e 100644 --- a/cosmo_inference/scripts/treecorr_calc.py +++ b/cosmo_inference/scripts/treecorr_calc.py @@ -2,13 +2,14 @@ # coding: utf-8 -import sys import os +import sys + import numpy as np -from astropy.io import fits import treecorr +from astropy.io import fits -script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) +script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) cat_name = sys.argv[1] root = sys.argv[2] @@ -20,29 +21,29 @@ n_thread = 8 treecorr.set_omp_threads(n_thread) -sep_units = 'arcmin' +sep_units = "arcmin" nbins = 20 TreeCorrConfig = { - 'ra_units': 'degrees', - 'dec_units': 'degrees', - 'max_sep': '200', - 'min_sep': '1', - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'jackknife', - } + "ra_units": "degrees", + "dec_units": "degrees", + "max_sep": "200", + "min_sep": "1", + "sep_units": sep_units, + "nbins": nbins, + "var_method": "jackknife", +} cat_gal = treecorr.Catalog( - ra=data['RA'], - dec=data['Dec'], - g1=data['e1_noleakage'], # for v1.4.1 - g2=data['e2_noleakage'], # for v1.4.1 - w=data['w'], - ra_units='degrees', - dec_units='degrees', - npatch=50 + ra=data["RA"], + dec=data["Dec"], + g1=data["e1_noleakage"], # for v1.4.1 + g2=data["e2_noleakage"], # for v1.4.1 + w=data["w"], + ra_units="degrees", + dec_units="degrees", + npatch=50, ) gg = treecorr.GGCorrelation(TreeCorrConfig) @@ -51,44 +52,56 @@ gg.process(cat_gal) -lst = np.arange(1,nbins+1) +lst = np.arange(1, nbins + 1) -#create fits HDU with xi_p and xi_m data -col1 = fits.Column(name ='BIN1', format ='K', array = np.ones(len(lst))) -col2 = fits.Column(name ='BIN2', format ='K', array = np.ones(len(lst))) -col3 = fits.Column(name ='ANGBIN', format ='K', array = lst) -col4 = fits.Column(name ='VALUE', format ='D', array = gg.xip) -col5 = fits.Column(name ='ANG', format ='D', unit ='arcmin', array = gg.meanr) +# create fits HDU with xi_p and xi_m data +col1 = fits.Column(name="BIN1", format="K", array=np.ones(len(lst))) +col2 = fits.Column(name="BIN2", format="K", array=np.ones(len(lst))) +col3 = fits.Column(name="ANGBIN", format="K", array=lst) +col4 = fits.Column(name="VALUE", format="D", array=gg.xip) +col5 = fits.Column(name="ANG", format="D", unit="arcmin", array=gg.meanr) coldefs = fits.ColDefs([col1, col2, col3, col4, col5]) -xiplus_hdu = fits.BinTableHDU.from_columns(coldefs,name ='XI_PLUS') +xiplus_hdu = fits.BinTableHDU.from_columns(coldefs, name="XI_PLUS") -col4 = fits.Column(name ='VALUE', format ='D', array = gg.xim) +col4 = fits.Column(name="VALUE", format="D", array=gg.xim) coldefs = fits.ColDefs([col1, col2, col3, col4, col5]) -ximinus_hdu = fits.BinTableHDU.from_columns(coldefs,name ='XI_MINUS') - -#append xi_p/xi_m header info -xip_dict = {'2PTDATA':'T', - 'QUANT1':'G+R', - 'QUANT2':'G+R', - 'KERNEL_1':'NZ_SOURCE', - 'KERNEL_2':'NZ_SOURCE', - 'WINDOWS':'SAMPLE'} +ximinus_hdu = fits.BinTableHDU.from_columns(coldefs, name="XI_MINUS") + +# append xi_p/xi_m header info +xip_dict = { + "2PTDATA": "T", + "QUANT1": "G+R", + "QUANT2": "G+R", + "KERNEL_1": "NZ_SOURCE", + "KERNEL_2": "NZ_SOURCE", + "WINDOWS": "SAMPLE", +} for key in xip_dict: xiplus_hdu.header[key] = xip_dict[key] -xim_dict = {'2PTDATA':'T', - 'QUANT1':'G-R', - 'QUANT2':'G-R', - 'KERNEL_1':'NZ_SOURCE', - 'KERNEL_2':'NZ_SOURCE', - 'WINDOWS':'SAMPLE'} +xim_dict = { + "2PTDATA": "T", + "QUANT1": "G-R", + "QUANT2": "G-R", + "KERNEL_1": "NZ_SOURCE", + "KERNEL_2": "NZ_SOURCE", + "WINDOWS": "SAMPLE", +} for key in xim_dict: ximinus_hdu.header[key] = xim_dict[key] -ximinus_hdu.writeto('%s/../data/' %script_dir+root+'/ximinus_'+root+'.fits',overwrite=True) -xiplus_hdu.writeto('%s/../data/' %script_dir+root+'/xiplus_'+root+'.fits',overwrite=True) +ximinus_hdu.writeto( + "%s/../data/" % script_dir + root + "/ximinus_" + root + ".fits", overwrite=True +) +xiplus_hdu.writeto( + "%s/../data/" % script_dir + root + "/xiplus_" + root + ".fits", overwrite=True +) -print('Correlation functions written to {}'.format('%s/../data/' %script_dir+root+'/xiplus_minus_'+root+'.fits')) \ No newline at end of file +print( + "Correlation functions written to {}".format( + "%s/../data/" % script_dir + root + "/xiplus_minus_" + root + ".fits" + ) +) diff --git a/cosmo_inference/scripts/xi_sys_psf.py b/cosmo_inference/scripts/xi_sys_psf.py index cc3a32ca..631b9464 100644 --- a/cosmo_inference/scripts/xi_sys_psf.py +++ b/cosmo_inference/scripts/xi_sys_psf.py @@ -1,14 +1,14 @@ -from cosmosis.datablock import names, option_section -from cosmosis.datablock.cosmosis_py import lib -from astropy.io import fits import numpy as np +from astropy.io import fits +from cosmosis.datablock import option_section + -#This file should be added to your cosmosis_standard_library following the path shear/xi_sys/xi_sys_psf.py +# This file should be added to your cosmosis_standard_library following the path shear/xi_sys/xi_sys_psf.py def setup(options): - filename = options.get_string(option_section, 'data_file') + filename = options.get_string(option_section, "data_file") data = fits.open(filename) - rho_stats_name = options.get_string(option_section, 'rho_stats_name') - samples_path = options.get_string(option_section,'samples') + rho_stats_name = options.get_string(option_section, "rho_stats_name") + samples_path = options.get_string(option_section, "samples") samples = np.load(samples_path) mean = np.mean(samples, axis=0) @@ -18,31 +18,36 @@ def setup(options): return mean, cov, rho_stats + def execute(block, config): mean, cov, rho_stats = config alpha, beta, eta = np.random.multivariate_normal(mean, cov) - block['xi_sys', 'alpha'], block['xi_sys', 'beta'], block['xi_sys', 'eta'] = alpha, beta, eta + block["xi_sys", "alpha"], block["xi_sys", "beta"], block["xi_sys", "eta"] = ( + alpha, + beta, + eta, + ) xi_sys_p = ( - alpha**2*rho_stats["rho_0_p"] - + beta**2*rho_stats["rho_1_p"] - + eta**2*rho_stats["rho_3_p"] - + 2*alpha*beta*rho_stats["rho_2_p"] - + 2*beta*eta*rho_stats["rho_4_p"] - + 2*alpha*eta*rho_stats["rho_5_p"] + alpha**2 * rho_stats["rho_0_p"] + + beta**2 * rho_stats["rho_1_p"] + + eta**2 * rho_stats["rho_3_p"] + + 2 * alpha * beta * rho_stats["rho_2_p"] + + 2 * beta * eta * rho_stats["rho_4_p"] + + 2 * alpha * eta * rho_stats["rho_5_p"] ) xi_sys_m = ( - alpha**2*rho_stats["rho_0_m"] - + beta**2*rho_stats["rho_1_m"] - + eta**2*rho_stats["rho_3_m"] - + 2*alpha*beta*rho_stats["rho_2_m"] - + 2*beta*eta*rho_stats["rho_4_m"] - + 2*alpha*eta*rho_stats["rho_5_m"] + alpha**2 * rho_stats["rho_0_m"] + + beta**2 * rho_stats["rho_1_m"] + + eta**2 * rho_stats["rho_3_m"] + + 2 * alpha * beta * rho_stats["rho_2_m"] + + 2 * beta * eta * rho_stats["rho_4_m"] + + 2 * alpha * eta * rho_stats["rho_5_m"] ) - block['xi_sys', 'xi_sys_vec'] = np.concatenate([xi_sys_p, xi_sys_m]) + block["xi_sys", "xi_sys_vec"] = np.concatenate([xi_sys_p, xi_sys_m]) - return 0 \ No newline at end of file + return 0 diff --git a/cosmo_val/compute_theory_cov.py b/cosmo_val/compute_theory_cov.py index d39b09ca..b65fadb2 100644 --- a/cosmo_val/compute_theory_cov.py +++ b/cosmo_val/compute_theory_cov.py @@ -1,97 +1,110 @@ -import yaml -import time -import numpy as np - -from astropy.io import fits +import time +import numpy as np +import yaml from shear_psf_leakage.rho_tau_cov import CovTauTh + def get_params_rho_tau(cat, survey="other"): # Set parameters params = {} # TODO to yaml file - params["ra_col"] = cat['psf']["ra_col"] - params["dec_col"] = cat['psf']["dec_col"] - params["e1_PSF_col"] = cat['psf']["e1_PSF_col"] - params["e2_PSF_col"] = cat['psf']["e2_PSF_col"] - params["e1_star_col"] = cat['psf']["e1_star_col"] - params["e2_star_col"] = cat['psf']["e2_star_col"] - params["PSF_size"] = cat['psf']["PSF_size"] - params["star_size"] = cat['psf']["star_size"] - params["square_size"] = cat['psf']["square_size"] + params["ra_col"] = cat["psf"]["ra_col"] + params["dec_col"] = cat["psf"]["dec_col"] + params["e1_PSF_col"] = cat["psf"]["e1_PSF_col"] + params["e2_PSF_col"] = cat["psf"]["e2_PSF_col"] + params["e1_star_col"] = cat["psf"]["e1_star_col"] + params["e2_star_col"] = cat["psf"]["e2_star_col"] + params["PSF_size"] = cat["psf"]["PSF_size"] + params["star_size"] = cat["psf"]["star_size"] + params["square_size"] = cat["psf"]["square_size"] params["R11"] = np.array([1]) params["R22"] = np.array([1]) - if survey != 'DES': - params["PSF_flag"] = cat['psf']["PSF_flag"] - params["star_flag"] = cat['psf']["star_flag"] - if survey == 'DES': - params["R11"] = cat['shear']["R11"] - params["R22"] = cat['shear']["R22"] + if survey != "DES": + params["PSF_flag"] = cat["psf"]["PSF_flag"] + params["star_flag"] = cat["psf"]["star_flag"] + if survey == "DES": + params["R11"] = cat["shear"]["R11"] + params["R22"] = cat["shear"]["R22"] params["ra_units"] = "deg" params["dec_units"] = "deg" - params["w_col"] = cat['shear']["w"] - params["e1_col"] = cat['shear']["e1_col"] - params["e2_col"] = cat['shear']["e2_col"] + params["w_col"] = cat["shear"]["w"] + params["e1_col"] = cat["shear"]["e1_col"] + params["e2_col"] = cat["shear"]["e2_col"] return params -if __name__ == "__main__": - base_dir = '/home/guerrini/data/' +if __name__ == "__main__": + base_dir = "/home/guerrini/data/" - versions = ['SP_v1.4-P3', 'SP_v1.4-P3_LFmask', 'SP_v1.3_LFmask_8k', 'SP_axel_v0.0', 'DES'] + versions = [ + "SP_v1.4-P3", + "SP_v1.4-P3_LFmask", + "SP_v1.3_LFmask_8k", + "SP_axel_v0.0", + "DES", + ] - path_config = '/home/guerrini/sp_validation/cosmo_val/cat_config.yaml' - output_dir = '/home/guerrini/sp_validation/cosmo_val/output/rho_tau_stats/' + path_config = "/home/guerrini/sp_validation/cosmo_val/cat_config.yaml" + output_dir = "/home/guerrini/sp_validation/cosmo_val/output/rho_tau_stats/" nbin_ang = 100 nbin_rad = 200 - sep_units = 'arcmin' - coord_units = 'degrees' + sep_units = "arcmin" + coord_units = "degrees" theta_min = 0.1 theta_max = 250 nbins = 20 TreeCorrConfig_xi = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, } - with open(path_config, 'r') as file: + with open(path_config, "r") as file: cat = yaml.load(file.read(), Loader=yaml.FullLoader) for ver in versions: - params = get_params_rho_tau(cat[ver], survey=ver) - + info = cat[ver] - A = info['cov_th']['A']*60*60 - n_e = info['cov_th']['n_e'] - n_psf = info['cov_th']['n_psf'] + A = info["cov_th"]["A"] * 60 * 60 + n_e = info["cov_th"]["n_e"] + n_psf = info["cov_th"]["n_psf"] - path_gal = base_dir+info['subdir']+'/'+info['shear']['path'] - path_psf = base_dir+info['subdir']+'/'+info['psf']['path'] - hdu_psf = info['psf']['hdu'] + path_gal = base_dir + info["subdir"] + "/" + info["shear"]["path"] + path_psf = base_dir + info["subdir"] + "/" + info["psf"]["path"] + hdu_psf = info["psf"]["hdu"] print("Computing the covariance matrix for the version: ", ver) start_time = time.time() cov_tau_th = CovTauTh( - path_gal=path_gal, path_psf=path_psf, hdu_psf=hdu_psf, treecorr_config=TreeCorrConfig_xi, - A=A, n_e=n_e, n_psf=n_psf, params=params + path_gal=path_gal, + path_psf=path_psf, + hdu_psf=hdu_psf, + treecorr_config=TreeCorrConfig_xi, + A=A, + n_e=n_e, + n_psf=n_psf, + params=params, + ) + print( + "--- Computation of the rho and tau statistics %s seconds ---" + % (time.time() - start_time) ) - print("--- Computation of the rho and tau statistics %s seconds ---" % (time.time() - start_time)) start_time = time.time() cov = cov_tau_th.build_cov(nbin_ang=nbin_ang, nbin_rad=nbin_rad) print("--- Covariance computation %s seconds ---" % (time.time() - start_time)) - np.save(output_dir+'/cov_tau_'+ver+'_th.npy', cov) + np.save(output_dir + "/cov_tau_" + ver + "_th.npy", cov) print("Saved covariance matrix of version: ", ver) diff --git a/cosmo_val/run_cosmo_val.py b/cosmo_val/run_cosmo_val.py index ae19561a..de19ebc3 100644 --- a/cosmo_val/run_cosmo_val.py +++ b/cosmo_val/run_cosmo_val.py @@ -15,23 +15,22 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") -import matplotlib.pyplot as plt -import numpy as np -from sp_validation.cosmo_val import CosmologyValidation # noqa: E402 -from sp_validation.cosmology import get_cosmo from astropy.cosmology import Planck18 # noqa: E402, F401 +from sp_validation.cosmo_val import CosmologyValidation # noqa: E402 + # Must follow sp_validation import (which sets agg backend) if ipython is not None: ipython.run_line_magic("matplotlib", "inline") +# Fiducial COSEBIs scale cut (theta_min, theta_max) in arcmin, used for the +# B-mode summary below. +FIDUCIAL_SCALE_CUT = (10, 250) + # %% # Specify version -versions = [ - "SP_v1.3.6", - "SP_v1.3.6_leak_corr" -] +versions = ["SP_v1.3.6", "SP_v1.3.6_leak_corr"] # Get the cosmology planck = Planck18 @@ -54,7 +53,7 @@ "halofit_version": halofit_version, "HMCode_log_T_AGN": log_T_AGN, "kmax": 20, - "kmax_extrapolate": 500 + "kmax_extrapolate": 500, } } @@ -62,10 +61,10 @@ "Omega_m": om, "Omega_b": ob, "h": h, - "sig8":sigma8, - "ns":ns, - "mnu":mnu, - "extra_params":extra_params + "sig8": sigma8, + "ns": ns, + "mnu": mnu, + "extra_params": extra_params, } cv = CosmologyValidation( @@ -81,13 +80,13 @@ cell_method="catalog", nside_mask=8192, path_onecovariance="/home/guerrini/OneCovariance/", - cosmo_params=cosmo_params + cosmo_params=cosmo_params, ) # %% -#cv.calculate_pseudo_cl_g_ng_cov() -#cv.calculate_pseudo_cl_g_ng_cov(gaussian_part="OneCovariance") +# cv.calculate_pseudo_cl_g_ng_cov() +# cv.calculate_pseudo_cl_g_ng_cov(gaussian_part="OneCovariance") # %% cv.plot_footprints() @@ -106,7 +105,7 @@ cv.plot_objectwise_leakage() # %% -#cv.plot_objectwise_leakage() +# cv.plot_objectwise_leakage() # %% # cv.plot_ellipticity() @@ -124,22 +123,22 @@ cv.plot_ratio_xi_sys_xi(offset=0.1) # %% -#cv.plot_aperture_mass_dispersion() +# cv.plot_aperture_mass_dispersion() # %% -#cv.plot_pseudo_cl() +# cv.plot_pseudo_cl() # %% -#cv.plot_pure_eb( +# cv.plot_pure_eb( # min_sep_int=0.08, # max_sep_int=300, # nbins_int=100, # npatch=256, # var_method="jackknife", -#) +# ) # %% -""" +""" scv.plot_cosebis( min_sep=0.9, max_sep=250, @@ -157,9 +156,8 @@ (20, 250), ], fiducial_scale_cut=(10, 250), -) +) """ # %% B-mode summary cv.summarize_bmodes(fiducial_scale_cut=FIDUCIAL_SCALE_CUT) - diff --git a/docs/source/conf.py b/docs/source/conf.py index 42daf2d3..6acc48a1 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -2,62 +2,65 @@ # Python Template sphinx config # Import relevant modules -import sys import os import re +import sys + from importlib_metadata import metadata # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath('../..')) -sys.path.insert(0, os.path.abspath('../../scripts')) +sys.path.insert(0, os.path.abspath("../..")) +sys.path.insert(0, os.path.abspath("../../scripts")) # -- General configuration ------------------------------------------------ # General information about the project. -project = 'sp_validation' +project = "sp_validation" mdata = metadata(project) # PEP 621 stores contributors in 'Author-email' ("Name , ...") rather # than the legacy 'Author' field; strip the addresses for a clean author list. -author = mdata.get('Author') or re.sub(r'\s*<[^>]*>', '', mdata.get('Author-email', 'CosmoStat')) -version = mdata['Version'] -copyright = '2021, {}'.format(author) +author = mdata.get("Author") or re.sub( + r"\s*<[^>]*>", "", mdata.get("Author-email", "CosmoStat") +) +version = mdata["Version"] +copyright = "2021, {}".format(author) # If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = '3.3' +needs_sphinx = "3.3" # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.autosummary', - 'sphinx.ext.coverage', - 'sphinx.ext.doctest', - 'sphinx.ext.ifconfig', - 'sphinx.ext.intersphinx', - 'sphinx.ext.mathjax', - 'sphinx.ext.napoleon', - 'sphinx.ext.todo', - 'sphinx.ext.viewcode', - 'sphinxcontrib.bibtex', - 'myst_parser', - 'numpydoc', + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", + "sphinx.ext.coverage", + "sphinx.ext.doctest", + "sphinx.ext.ifconfig", + "sphinx.ext.intersphinx", + "sphinx.ext.mathjax", + "sphinx.ext.napoleon", + "sphinx.ext.todo", + "sphinx.ext.viewcode", + "sphinxcontrib.bibtex", + "myst_parser", + "numpydoc", ] # Include module names for objects add_module_names = False # Set class documentation standard. -autoclass_content = 'class' +autoclass_content = "class" # Order docstrings as in the source -autodoc_member_order = 'bysource' +autodoc_member_order = "bysource" # Include private class methods -autodoc_default_options = {'members': True, 'private-members': True} +autodoc_default_options = {"members": True, "private-members": True} # Generate summaries autosummary_generate = True @@ -67,17 +70,17 @@ # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: -source_suffix = ['.rst', '.md'] +source_suffix = [".rst", ".md"] # The master toctree document. -master_doc = 'index' +master_doc = "index" # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. show_authors = True # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'default' +pygments_style = "default" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True @@ -86,7 +89,7 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'sphinxawesome_theme' +html_theme = "sphinxawesome_theme" # html_theme = 'sphinx_book_theme' # Theme options are theme-specific and customize the look and feel of a theme @@ -101,7 +104,7 @@ # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -html_title = '{0} v{1}'.format(project, version) +html_title = "{0} v{1}".format(project, version) # A shorter title for the navigation bar. Default is the same as html_title. # html_short_title = None @@ -117,7 +120,7 @@ # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -html_last_updated_fmt = '%d %b, %Y' +html_last_updated_fmt = "%d %b, %Y" # Convert quotes and dashes to typographically correct entities. smartquotes = True @@ -132,12 +135,12 @@ # Refer to the package libraries for type definitions intersphinx_mapping = { - 'python': ('http://docs.python.org/3', None), - 'numpy': ('https://numpy.org/doc/stable/', None), - 'scipy': ('https://docs.scipy.org/doc/scipy/reference', None), - 'matplotlib': ('https://matplotlib.org', None) + "python": ("http://docs.python.org/3", None), + "numpy": ("https://numpy.org/doc/stable/", None), + "scipy": ("https://docs.scipy.org/doc/scipy/reference", None), + "matplotlib": ("https://matplotlib.org", None), } # -- BibTeX Setting ---------------------------------------------- -bibtex_bibfiles = ['refs.bib'] +bibtex_bibfiles = ["refs.bib"] diff --git a/papers/bmodes/scripts/bb_covariance_blind_independence.py b/papers/bmodes/scripts/bb_covariance_blind_independence.py index 70e331cd..ac21a243 100644 --- a/papers/bmodes/scripts/bb_covariance_blind_independence.py +++ b/papers/bmodes/scripts/bb_covariance_blind_independence.py @@ -18,8 +18,8 @@ import numpy as np import treecorr from astropy.io import fits - from plotting_utils import PAPER_MPLSTYLE + from sp_validation.b_modes import calculate_cosebis plt.style.use(PAPER_MPLSTYLE) @@ -64,8 +64,14 @@ def load_harmonic_diagonals(path): def load_cosebis_diagonals( - xi_integration_path, cov_integration_path, nmodes, theta_min, theta_max, - min_sep_int, max_sep_int, nbins_int + xi_integration_path, + cov_integration_path, + nmodes, + theta_min, + theta_max, + min_sep_int, + max_sep_int, + nbins_int, ): """Compute COSEBIS covariance diagonals from config-space covariance. @@ -73,7 +79,9 @@ def load_cosebis_diagonals( Returns E_n and B_n covariance diagonals. """ # Load fine-binned 2PCF (need the binning info for COSEBIS calculation) - gg = treecorr.GGCorrelation(min_sep=min_sep_int, max_sep=max_sep_int, nbins=nbins_int, sep_units="arcmin") + gg = treecorr.GGCorrelation( + min_sep=min_sep_int, max_sep=max_sep_int, nbins=nbins_int, sep_units="arcmin" + ) gg.read(xi_integration_path) # Compute COSEBIS with this blind's covariance @@ -112,7 +120,15 @@ def compute_ratios(diag_ref, diag_test): } -def make_figure(theta, ell_eff, pure_eb_results, harmonic_results, cosebis_results, output_path, n_samples=2000): +def make_figure( + theta, + ell_eff, + pure_eb_results, + harmonic_results, + cosebis_results, + output_path, + n_samples=2000, +): """Four-panel figure comparing BB vs EE stability across blinds. Layout: 2x2 @@ -137,27 +153,69 @@ def setup_ratio_panel(ax, ylabel_x, title, show_mc_band=False): ax.axhline(1.0, color="gray", ls="-", lw=0.8, zorder=0) if show_mc_band: # Show expected 1σ MC scatter band only - ax.axhspan(1 - ratio_err, 1 + ratio_err, color="gray", alpha=0.25, - label=rf"$\pm\sqrt{{2/N}}$ ($N={n_samples}$)") + ax.axhspan( + 1 - ratio_err, + 1 + ratio_err, + color="gray", + alpha=0.25, + label=rf"$\pm\sqrt{{2/N}}$ ($N={n_samples}$)", + ) ax.set_xscale("log") ax.set_xlabel(ylabel_x) ax.set_title(title) # --- Panel 1: xi+^B vs xi+^E --- ax = axes[0, 0] - setup_ratio_panel(ax, r"$\theta$ [arcmin]", r"$\xi_+$: covariance ratio across blinds", show_mc_band=True) + setup_ratio_panel( + ax, + r"$\theta$ [arcmin]", + r"$\xi_+$: covariance ratio across blinds", + show_mc_band=True, + ) # B-mode (blue): square for B/A, triangle for C/A - ax.errorbar(theta, pure_eb_results["B"]["xip_B"]["ratio"], yerr=ratio_err, - fmt="s", color=color_B, label=r"B-mode B/A", markersize=5, alpha=0.8, capsize=0) - ax.errorbar(theta * 1.03, pure_eb_results["C"]["xip_B"]["ratio"], yerr=ratio_err, - fmt="^", color=color_B, label=r"B-mode C/A", markersize=5, alpha=0.8, capsize=0) + ax.errorbar( + theta, + pure_eb_results["B"]["xip_B"]["ratio"], + yerr=ratio_err, + fmt="s", + color=color_B, + label=r"B-mode B/A", + markersize=5, + alpha=0.8, + capsize=0, + ) + ax.errorbar( + theta * 1.03, + pure_eb_results["C"]["xip_B"]["ratio"], + yerr=ratio_err, + fmt="^", + color=color_B, + label=r"B-mode C/A", + markersize=5, + alpha=0.8, + capsize=0, + ) # E-mode (orange): square for B/A, triangle for C/A - ax.plot(theta, pure_eb_results["B"]["xip_E"]["ratio"], - "s", color=color_E, label=r"E-mode B/A", markersize=4, alpha=0.6) - ax.plot(theta * 1.03, pure_eb_results["C"]["xip_E"]["ratio"], - "^", color=color_E, label=r"E-mode C/A", markersize=4, alpha=0.6) + ax.plot( + theta, + pure_eb_results["B"]["xip_E"]["ratio"], + "s", + color=color_E, + label=r"E-mode B/A", + markersize=4, + alpha=0.6, + ) + ax.plot( + theta * 1.03, + pure_eb_results["C"]["xip_E"]["ratio"], + "^", + color=color_E, + label=r"E-mode C/A", + markersize=4, + alpha=0.6, + ) ax.set_ylabel("Diagonal ratio") ax.legend(loc="upper right", fontsize=7, ncol=2) @@ -166,17 +224,54 @@ def setup_ratio_panel(ax, ylabel_x, title, show_mc_band=False): # --- Panel 2: xi-^B vs xi-^E --- ax = axes[0, 1] - setup_ratio_panel(ax, r"$\theta$ [arcmin]", r"$\xi_-$: covariance ratio across blinds", show_mc_band=True) + setup_ratio_panel( + ax, + r"$\theta$ [arcmin]", + r"$\xi_-$: covariance ratio across blinds", + show_mc_band=True, + ) - ax.errorbar(theta, pure_eb_results["B"]["xim_B"]["ratio"], yerr=ratio_err, - fmt="s", color=color_B, label=r"B-mode B/A", markersize=5, alpha=0.8, capsize=0) - ax.errorbar(theta * 1.03, pure_eb_results["C"]["xim_B"]["ratio"], yerr=ratio_err, - fmt="^", color=color_B, label=r"B-mode C/A", markersize=5, alpha=0.8, capsize=0) + ax.errorbar( + theta, + pure_eb_results["B"]["xim_B"]["ratio"], + yerr=ratio_err, + fmt="s", + color=color_B, + label=r"B-mode B/A", + markersize=5, + alpha=0.8, + capsize=0, + ) + ax.errorbar( + theta * 1.03, + pure_eb_results["C"]["xim_B"]["ratio"], + yerr=ratio_err, + fmt="^", + color=color_B, + label=r"B-mode C/A", + markersize=5, + alpha=0.8, + capsize=0, + ) - ax.plot(theta, pure_eb_results["B"]["xim_E"]["ratio"], - "s", color=color_E, label=r"E-mode B/A", markersize=4, alpha=0.6) - ax.plot(theta * 1.03, pure_eb_results["C"]["xim_E"]["ratio"], - "^", color=color_E, label=r"E-mode C/A", markersize=4, alpha=0.6) + ax.plot( + theta, + pure_eb_results["B"]["xim_E"]["ratio"], + "s", + color=color_E, + label=r"E-mode B/A", + markersize=4, + alpha=0.6, + ) + ax.plot( + theta * 1.03, + pure_eb_results["C"]["xim_E"]["ratio"], + "^", + color=color_E, + label=r"E-mode C/A", + markersize=4, + alpha=0.6, + ) ax.legend(loc="upper right", fontsize=7, ncol=2) ax.set_xlim(1, 300) @@ -191,16 +286,44 @@ def setup_ratio_panel(ax, ylabel_x, title, show_mc_band=False): ax.set_title(r"COSEBIS: covariance ratio across blinds") # B-mode (blue): square for B/A, triangle for C/A - ax.plot(n_arr, cosebis_results["B"]["B"]["ratio"], - "s", color=color_B, label=r"B-mode B/A", markersize=5, alpha=0.8) - ax.plot(n_arr + 0.15, cosebis_results["C"]["B"]["ratio"], - "^", color=color_B, label=r"B-mode C/A", markersize=5, alpha=0.8) + ax.plot( + n_arr, + cosebis_results["B"]["B"]["ratio"], + "s", + color=color_B, + label=r"B-mode B/A", + markersize=5, + alpha=0.8, + ) + ax.plot( + n_arr + 0.15, + cosebis_results["C"]["B"]["ratio"], + "^", + color=color_B, + label=r"B-mode C/A", + markersize=5, + alpha=0.8, + ) # E-mode (orange): square for B/A, triangle for C/A - ax.plot(n_arr, cosebis_results["B"]["E"]["ratio"], - "s", color=color_E, label=r"E-mode B/A", markersize=4, alpha=0.6) - ax.plot(n_arr + 0.15, cosebis_results["C"]["E"]["ratio"], - "^", color=color_E, label=r"E-mode C/A", markersize=4, alpha=0.6) + ax.plot( + n_arr, + cosebis_results["B"]["E"]["ratio"], + "s", + color=color_E, + label=r"E-mode B/A", + markersize=4, + alpha=0.6, + ) + ax.plot( + n_arr + 0.15, + cosebis_results["C"]["E"]["ratio"], + "^", + color=color_E, + label=r"E-mode C/A", + markersize=4, + alpha=0.6, + ) ax.set_ylabel("Diagonal ratio") ax.legend(loc="upper right", fontsize=7, ncol=2) @@ -211,16 +334,44 @@ def setup_ratio_panel(ax, ylabel_x, title, show_mc_band=False): setup_ratio_panel(ax, r"$\ell$", r"$C_\ell$: covariance ratio across blinds") # B-mode (blue): square for B/A, triangle for C/A - ax.plot(ell_eff, harmonic_results["B"]["BB"]["ratio"], - "s", color=color_B, label=r"BB B/A", markersize=5, alpha=0.8) - ax.plot(ell_eff * 1.03, harmonic_results["C"]["BB"]["ratio"], - "^", color=color_B, label=r"BB C/A", markersize=5, alpha=0.8) + ax.plot( + ell_eff, + harmonic_results["B"]["BB"]["ratio"], + "s", + color=color_B, + label=r"BB B/A", + markersize=5, + alpha=0.8, + ) + ax.plot( + ell_eff * 1.03, + harmonic_results["C"]["BB"]["ratio"], + "^", + color=color_B, + label=r"BB C/A", + markersize=5, + alpha=0.8, + ) # E-mode (orange): square for B/A, triangle for C/A - ax.plot(ell_eff, harmonic_results["B"]["EE"]["ratio"], - "s", color=color_E, label=r"EE B/A", markersize=4, alpha=0.6) - ax.plot(ell_eff * 1.03, harmonic_results["C"]["EE"]["ratio"], - "^", color=color_E, label=r"EE C/A", markersize=4, alpha=0.6) + ax.plot( + ell_eff, + harmonic_results["B"]["EE"]["ratio"], + "s", + color=color_E, + label=r"EE B/A", + markersize=4, + alpha=0.6, + ) + ax.plot( + ell_eff * 1.03, + harmonic_results["C"]["EE"]["ratio"], + "^", + color=color_E, + label=r"EE C/A", + markersize=4, + alpha=0.6, + ) ax.set_ylabel("Diagonal ratio") ax.legend(loc="upper right", fontsize=7, ncol=2) @@ -281,8 +432,7 @@ def main(snakemake): pure_eb_results[blind] = {} for mode in ["xip_E", "xim_E", "xip_B", "xim_B"]: pure_eb_results[blind][mode] = compute_ratios( - pure_eb_data["A"][mode], - pure_eb_data[blind][mode] + pure_eb_data["A"][mode], pure_eb_data[blind][mode] ) harmonic_results = {} @@ -290,8 +440,7 @@ def main(snakemake): harmonic_results[blind] = {} for mode in ["EE", "BB"]: harmonic_results[blind][mode] = compute_ratios( - harmonic_data["A"][mode], - harmonic_data[blind][mode] + harmonic_data["A"][mode], harmonic_data[blind][mode] ) cosebis_results = {} @@ -301,13 +450,20 @@ def main(snakemake): } for mode in ["E", "B"]: cosebis_results[blind][mode] = compute_ratios( - cosebis_data["A"][mode], - cosebis_data[blind][mode] + cosebis_data["A"][mode], cosebis_data[blind][mode] ) # Generate figure n_samples = config["covariance"]["n_samples"] - make_figure(theta, ell_eff, pure_eb_results, harmonic_results, cosebis_results, snakemake.output.figure, n_samples=n_samples) + make_figure( + theta, + ell_eff, + pure_eb_results, + harmonic_results, + cosebis_results, + snakemake.output.figure, + n_samples=n_samples, + ) # Compute summary statistics bb_max_devs = [ @@ -438,13 +594,13 @@ def clean_ratios(d): # Print summary print("\nBB Covariance Blind Independence Summary:") - print(f" COSEBIS B_n max deviation: {cosebis_bb_max*100:.6f}%") - print(f" COSEBIS E_n max deviation: {cosebis_ee_max*100:.2f}%") + print(f" COSEBIS B_n max deviation: {cosebis_bb_max * 100:.6f}%") + print(f" COSEBIS E_n max deviation: {cosebis_ee_max * 100:.2f}%") print(f" COSEBIS B_n blind-independent (<0.1%): {cosebis_bb_blind_independent}") print(f" E-modes vary as expected (5-15%): {ee_varies_as_expected}") print("\n Pure E/B + Harmonic (MC methods):") - print(f" BB max deviation: {bb_max*100:.2f}%") - print(f" EE max deviation: {ee_max*100:.2f}%") + print(f" BB max deviation: {bb_max * 100:.2f}%") + print(f" EE max deviation: {ee_max * 100:.2f}%") print(" Note: MC sampling noise causes BB to vary similarly to EE") diff --git a/papers/bmodes/scripts/calculate_pure_eb_ptes.py b/papers/bmodes/scripts/calculate_pure_eb_ptes.py index a86f65e1..e7d95163 100644 --- a/papers/bmodes/scripts/calculate_pure_eb_ptes.py +++ b/papers/bmodes/scripts/calculate_pure_eb_ptes.py @@ -33,6 +33,7 @@ def _load_snakemake(): class FakeGG: """Minimal GGCorrelation-like object for calculate_eb_statistics.""" + def __init__(self, nbins, npatch): self.nbins = nbins self.npatch1 = npatch diff --git a/papers/bmodes/scripts/cl_data_vector.py b/papers/bmodes/scripts/cl_data_vector.py index 34935353..7bf98ae4 100644 --- a/papers/bmodes/scripts/cl_data_vector.py +++ b/papers/bmodes/scripts/cl_data_vector.py @@ -9,13 +9,11 @@ """ import json -import shutil from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt import numpy as np -import seaborn as sns from astropy.io import fits # Import shared utilities (also registers SquareRootScale) @@ -28,7 +26,6 @@ iter_version_figures, ) - plt.style.use(PAPER_MPLSTYLE) @@ -66,7 +63,9 @@ def _load_pseudo_cl_data(pseudo_cl_path, pseudo_cl_cov_path): return ell, cl_bb, cl_eb, cov_bb, cov_eb, sigma_bb, sigma_eb -def _create_cl_figure(ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_max_cut, title=None): +def _create_cl_figure( + ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_max_cut, title=None +): """Create two-panel Cl figure. Args: @@ -74,10 +73,12 @@ def _create_cl_figure(ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_ma ell_max_cut: Upper scale cut for shading excluded region title: Optional title for the figure (None for paper figure) """ - fig, (ax_bb, ax_eb) = plt.subplots(2, 1, figsize=(FIG_WIDTH_SINGLE, FIG_WIDTH_SINGLE * 0.75), sharex=True) + fig, (ax_bb, ax_eb) = plt.subplots( + 2, 1, figsize=(FIG_WIDTH_SINGLE, FIG_WIDTH_SINGLE * 0.75), sharex=True + ) - color_bb = "#2c5f8a" # dark blue (distinct harmonic-space scheme) - color_eb = "#c45a2c" # burnt orange + color_bb = "#2c5f8a" # dark blue (distinct harmonic-space scheme) + color_eb = "#c45a2c" # burnt orange minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] @@ -86,9 +87,17 @@ def _create_cl_figure(ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_ma if title: bb_label = rf"$C_\ell^{{BB}}$ ({title})" ax_bb.errorbar( - ell, cl_bb / sigma_bb, yerr=np.ones_like(cl_bb), - fmt="o", mfc=color_bb, mec=color_bb, color=color_bb, - capsize=2, markersize=4, linewidth=1.0, label=bb_label, + ell, + cl_bb / sigma_bb, + yerr=np.ones_like(cl_bb), + fmt="o", + mfc=color_bb, + mec=color_bb, + color=color_bb, + capsize=2, + markersize=4, + linewidth=1.0, + label=bb_label, ) ax_bb.axhline(0, color="black", linestyle="-", linewidth=1.0, alpha=0.8) ax_bb.set_xscale("squareroot") @@ -100,9 +109,17 @@ def _create_cl_figure(ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_ma if title: eb_label = rf"$C_\ell^{{EB}}$ ({title})" ax_eb.errorbar( - ell, cl_eb / sigma_eb, yerr=np.ones_like(cl_eb), - fmt="s", mfc="none", mec=color_eb, color=color_eb, - capsize=2, markersize=4, linewidth=1.0, label=eb_label, + ell, + cl_eb / sigma_eb, + yerr=np.ones_like(cl_eb), + fmt="s", + mfc="none", + mec=color_eb, + color=color_eb, + capsize=2, + markersize=4, + linewidth=1.0, + label=eb_label, ) ax_eb.axhline(0, color="black", linestyle="-", linewidth=1.0, alpha=0.8) ax_eb.set_xscale("squareroot") @@ -117,7 +134,7 @@ def _create_cl_figure(ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_ma ell_low, ell_high = get_powspace_bin_edges(ell) mask = ell_bin_mask(ell, ell_min_cut, ell_max_cut) included = np.where(mask)[0] - shade_low = ell_low[included[0]] # lower edge of first included bin + shade_low = ell_low[included[0]] # lower edge of first included bin shade_high = ell_high[included[-1]] # upper edge of last included bin for ax in [ax_bb, ax_eb]: @@ -147,8 +164,14 @@ def main(): ell_max_cut = int(snakemake.params.ell_max_cut) # Build input path lookup from snakemake inputs - cl_paths = {k: v for k, v in snakemake.input.items() if k.startswith("pseudo_cl_") and not k.startswith("pseudo_cl_cov")} - cov_paths = {k: v for k, v in snakemake.input.items() if k.startswith("pseudo_cl_cov_")} + cl_paths = { + k: v + for k, v in snakemake.input.items() + if k.startswith("pseudo_cl_") and not k.startswith("pseudo_cl_cov") + } + cov_paths = { + k: v for k, v in snakemake.input.items() if k.startswith("pseudo_cl_cov_") + } # Create output directory output_dir = Path(snakemake.output["evidence"]).parent @@ -174,8 +197,14 @@ def main(): # Create figure with appropriate title fig = _create_cl_figure( - ell, cl_bb, cl_eb, sigma_bb, sigma_eb, ell_min_cut, ell_max_cut, - title=fig_spec["title"] + ell, + cl_bb, + cl_eb, + sigma_bb, + sigma_eb, + ell_min_cut, + ell_max_cut, + title=fig_spec["title"], ) # Save figure diff --git a/papers/bmodes/scripts/cl_version_comparison.py b/papers/bmodes/scripts/cl_version_comparison.py index 5a075459..ea2637f2 100644 --- a/papers/bmodes/scripts/cl_version_comparison.py +++ b/papers/bmodes/scripts/cl_version_comparison.py @@ -6,15 +6,16 @@ """ import json -import shutil from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt import numpy as np + +# Import to register SquareRootScale +import plotting_utils # noqa: F401 import seaborn as sns from astropy.io import fits - from plotting_utils import ( ERRORBAR_DEFAULTS, FIG_WIDTH_FULL, @@ -27,18 +28,14 @@ get_version_alpha, version_label, ) -# Import to register SquareRootScale -import plotting_utils # noqa: F401 - plt.style.use(PAPER_MPLSTYLE) - - def main(): # Read config import yaml + with open(snakemake.input["config"]) as f: config = yaml.safe_load(f) @@ -55,7 +52,8 @@ def main(): # Which version gets the fiducial reference line in boxes fiducial_for_comparison = getattr( - snakemake.params, "fiducial_for_comparison", + snakemake.params, + "fiducial_for_comparison", plotting_config.get("fiducial_for_comparison", config["fiducial"]["version"]), ) box_style = plotting_config.get("version_box", {}) @@ -118,34 +116,40 @@ def main(): chi2_bb_cut, pte_bb_cut, dof_bb_cut = compute_chi2_pte(cl_bb_cut, cov_bb_cut) chi2_eb_cut, pte_eb_cut, dof_eb_cut = compute_chi2_pte(cl_eb_cut, cov_eb_cut) - datasets.append({ - "version": version, - "label": version_label(version, version_labels), - "color": color, - "marker": marker, - "fillstyle": fillstyle, - "alpha": get_version_alpha(version, fiducial_for_comparison, plotting_config), - "ell": ell, - "cl_bb": cl_bb, - "cl_eb": cl_eb, - "sigma_bb": sigma_bb, - "sigma_eb": sigma_eb, - "pte_bb": pte_bb, - "chi2_bb": chi2_bb, - "dof_bb": dof_bb, - "pte_eb": pte_eb, - "chi2_eb": chi2_eb, - "dof_eb": dof_eb, - "pte_bb_cut": pte_bb_cut, - "chi2_bb_cut": chi2_bb_cut, - "dof_bb_cut": dof_bb_cut, - "pte_eb_cut": pte_eb_cut, - "chi2_eb_cut": chi2_eb_cut, - "dof_eb_cut": dof_eb_cut, - }) + datasets.append( + { + "version": version, + "label": version_label(version, version_labels), + "color": color, + "marker": marker, + "fillstyle": fillstyle, + "alpha": get_version_alpha( + version, fiducial_for_comparison, plotting_config + ), + "ell": ell, + "cl_bb": cl_bb, + "cl_eb": cl_eb, + "sigma_bb": sigma_bb, + "sigma_eb": sigma_eb, + "pte_bb": pte_bb, + "chi2_bb": chi2_bb, + "dof_bb": dof_bb, + "pte_eb": pte_eb, + "chi2_eb": chi2_eb, + "dof_eb": dof_eb, + "pte_bb_cut": pte_bb_cut, + "chi2_bb_cut": chi2_bb_cut, + "dof_bb_cut": dof_bb_cut, + "pte_eb_cut": pte_eb_cut, + "chi2_eb_cut": chi2_eb_cut, + "dof_eb_cut": dof_eb_cut, + } + ) # Two-panel figure: BB (top) and EB (bottom) - fig, (ax_bb, ax_eb) = plt.subplots(2, 1, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.45), sharex=True) + fig, (ax_bb, ax_eb) = plt.subplots( + 2, 1, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.45), sharex=True + ) ell_ref = datasets[0]["ell"] ell_widths = np.diff(ell_ref) @@ -161,14 +165,26 @@ def main(): # Draw version spread boxes (before data points) draw_normalized_boxes_ell_scale( - ax_bb, ell_ref, ell_widths, datasets, - y_norm_key="cl_bb_normalized", fiducial_idx=fiducial_idx, - jitter_fraction=jitter_fraction, n_versions=len(datasets), box_style=box_style + ax_bb, + ell_ref, + ell_widths, + datasets, + y_norm_key="cl_bb_normalized", + fiducial_idx=fiducial_idx, + jitter_fraction=jitter_fraction, + n_versions=len(datasets), + box_style=box_style, ) draw_normalized_boxes_ell_scale( - ax_eb, ell_ref, ell_widths, datasets, - y_norm_key="cl_eb_normalized", fiducial_idx=fiducial_idx, - jitter_fraction=jitter_fraction, n_versions=len(datasets), box_style=box_style + ax_eb, + ell_ref, + ell_widths, + datasets, + y_norm_key="cl_eb_normalized", + fiducial_idx=fiducial_idx, + jitter_fraction=jitter_fraction, + n_versions=len(datasets), + box_style=box_style, ) legend_handles = [] @@ -189,17 +205,27 @@ def main(): cl_eb_normalized = data["cl_eb_normalized"] line_bb = ax_bb.errorbar( - ell_jittered, cl_bb_normalized, yerr=np.ones_like(cl_bb_normalized), - fmt=marker, color=color, alpha=alpha, - markerfacecolor=mfc, markeredgecolor=color, + ell_jittered, + cl_bb_normalized, + yerr=np.ones_like(cl_bb_normalized), + fmt=marker, + color=color, + alpha=alpha, + markerfacecolor=mfc, + markeredgecolor=color, **ERRORBAR_DEFAULTS, zorder=2, ) ax_eb.errorbar( - ell_jittered, cl_eb_normalized, yerr=np.ones_like(cl_eb_normalized), - fmt=marker, color=color, alpha=alpha, - markerfacecolor=mfc, markeredgecolor=color, + ell_jittered, + cl_eb_normalized, + yerr=np.ones_like(cl_eb_normalized), + fmt=marker, + color=color, + alpha=alpha, + markerfacecolor=mfc, + markeredgecolor=color, **ERRORBAR_DEFAULTS, zorder=2, ) @@ -214,7 +240,10 @@ def main(): major_ticks = np.array([100, 400, 900, 1600]) minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] - for ax, ylabel in [(ax_bb, r"$C_\ell^{BB} / \sigma$"), (ax_eb, r"$C_\ell^{EB} / \sigma$")]: + for ax, ylabel in [ + (ax_bb, r"$C_\ell^{BB} / \sigma$"), + (ax_eb, r"$C_\ell^{EB} / \sigma$"), + ]: ax.axhline(0, color="black", linewidth=0.8, alpha=0.6) ax.set_xscale("squareroot") ax.set_xlim(ell_min, ell_max) diff --git a/papers/bmodes/scripts/compute_cosebis_pte_single.py b/papers/bmodes/scripts/compute_cosebis_pte_single.py index f9780d83..548b6bd0 100644 --- a/papers/bmodes/scripts/compute_cosebis_pte_single.py +++ b/papers/bmodes/scripts/compute_cosebis_pte_single.py @@ -12,8 +12,8 @@ import numpy as np import treecorr - from plotting_utils import compute_chi2_pte + from sp_validation.b_modes import calculate_cosebis @@ -49,7 +49,9 @@ def main(): # Load precomputed fine-binned 2PCF t0 = time.time() - gg = treecorr.GGCorrelation(min_sep=min_sep_int, max_sep=max_sep_int, nbins=nbins_int, sep_units="arcmin") + gg = treecorr.GGCorrelation( + min_sep=min_sep_int, max_sep=max_sep_int, nbins=nbins_int, sep_units="arcmin" + ) gg.read(snakemake.input.xi_integration) print(f"[TIMING] Load 2PCF: {time.time() - t0:.2f}s") @@ -73,15 +75,22 @@ def main(): ) except Exception as e: if "NoConvergence" in str(type(e).__name__) or "NoConvergence" in str(e): - print(f"WARNING: polyroots convergence failure for ({i_min}, {i_max}), writing NaN PTEs") + print( + f"WARNING: polyroots convergence failure for ({i_min}, {i_max}), writing NaN PTEs" + ) nan = float("nan") output = { - "i_min": i_min, "i_max": i_max, - "theta_min": float(theta_min), "theta_max": float(theta_max), - "En": [], "Bn": [], + "i_min": i_min, + "i_max": i_max, + "theta_min": float(theta_min), + "theta_max": float(theta_max), + "En": [], + "Bn": [], "nmodes_6": {"chi2_E": nan, "chi2_B": nan, "pte_E": nan, "pte_B": nan}, "nmodes_20": {"chi2_E": nan, "chi2_B": nan, "pte_E": nan, "pte_B": nan}, - "pte_B": nan, "chi2_B": nan, "chi2_E": nan, + "pte_B": nan, + "chi2_B": nan, + "chi2_E": nan, "convergence_failure": True, } output_path = Path(snakemake.output.pte_json) @@ -102,7 +111,7 @@ def main(): # Compute PTEs for both 6 and 20 modes # 6 modes: use first 6 - cov_B_6 = cov_full[nmodes:nmodes+6, nmodes:nmodes+6] + cov_B_6 = cov_full[nmodes : nmodes + 6, nmodes : nmodes + 6] cov_E_6 = cov_full[:6, :6] chi2_B_6, pte_B_6, _ = compute_chi2_pte(Bn_full[:6], cov_B_6) chi2_E_6, pte_E_6, _ = compute_chi2_pte(En_full[:6], cov_E_6) diff --git a/papers/bmodes/scripts/config_space_pte_matrices.py b/papers/bmodes/scripts/config_space_pte_matrices.py index f13cc036..07c8bea5 100644 --- a/papers/bmodes/scripts/config_space_pte_matrices.py +++ b/papers/bmodes/scripts/config_space_pte_matrices.py @@ -8,16 +8,19 @@ """ import json -import shutil import sys from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt -from matplotlib.patches import Rectangle import numpy as np - -from plotting_utils import PAPER_MPLSTYLE, format_pte_colorbar, make_pte_colormap, make_pte_norm +from matplotlib.patches import Rectangle +from plotting_utils import ( + PAPER_MPLSTYLE, + format_pte_colorbar, + make_pte_colormap, + make_pte_norm, +) plt.style.use(PAPER_MPLSTYLE) @@ -176,8 +179,16 @@ def _load_version_pte_data(pure_eb_pte_files, cosebis_pte_files, version, config } -def plot_pte_panel(ax, pte_matrix, theta_grid, fid_start, fid_stop, title, - show_xticklabels=True, show_yticklabels=False): +def plot_pte_panel( + ax, + pte_matrix, + theta_grid, + fid_start, + fid_stop, + title, + show_xticklabels=True, + show_yticklabels=False, +): """Plot a single PTE heatmap panel. Parameters @@ -220,7 +231,8 @@ def plot_pte_panel(ax, pte_matrix, theta_grid, fid_start, fid_stop, title, ax.add_patch( Rectangle( (fid_start, fid_stop), - 1, 1, + 1, + 1, fill=False, edgecolor="black", linewidth=1.5, @@ -229,9 +241,16 @@ def plot_pte_panel(ax, pte_matrix, theta_grid, fid_start, fid_stop, title, # Title inside plot (bottom-right of empty upper triangle) if title: - ax.text(0.95, 0.05, title, transform=ax.transAxes, - ha="right", va="bottom", fontsize=9, - bbox=dict(boxstyle="round,pad=0.2", facecolor="white", alpha=0.8)) + ax.text( + 0.95, + 0.05, + title, + transform=ax.transAxes, + ha="right", + va="bottom", + fontsize=9, + bbox=dict(boxstyle="round,pad=0.2", facecolor="white", alpha=0.8), + ) # Tick positioning: x-axis at left edge of bins, y-axis at top edge tick_step = 2 @@ -245,7 +264,9 @@ def plot_pte_panel(ax, pte_matrix, theta_grid, fid_start, fid_stop, title, ax.set_yticks(tick_indices + 1) if show_xticklabels: - ax.set_xticklabels(x_tick_labels, rotation=45, ha="right", rotation_mode="anchor", fontsize=7) + ax.set_xticklabels( + x_tick_labels, rotation=45, ha="right", rotation_mode="anchor", fontsize=7 + ) else: ax.set_xticklabels([]) @@ -348,8 +369,9 @@ def extract_full_range_ptes(pure_eb_pte_files, cosebis_pte_files, version): return ptes -def create_3panel_composite(version, pure_eb_pte_files, cosebis_pte_files, - xip_fid, xim_fid, cosebis_fid, config): +def create_3panel_composite( + version, pure_eb_pte_files, cosebis_pte_files, xip_fid, xim_fid, cosebis_fid, config +): """Create a 1×3 composite figure for the fiducial version (main text). Parameters @@ -382,11 +404,15 @@ def create_3panel_composite(version, pure_eb_pte_files, cosebis_pte_files, fig = plt.figure(figsize=(fig_width, fig_height)) gs = fig.add_gridspec( - 1, 3, + 1, + 3, width_ratios=[1, 1, 1], - wspace=0.03, hspace=0.03, - left=0.08, right=0.92, - bottom=0.15, top=0.90 + wspace=0.03, + hspace=0.03, + left=0.08, + right=0.92, + bottom=0.15, + top=0.90, ) # Load all PTE data for this version @@ -417,25 +443,37 @@ def create_3panel_composite(version, pure_eb_pte_files, cosebis_pte_files, # Plot panels with titles plot_pte_panel( - ax_xip, pte_xip_B, theta_pure_eb, - xip_start, xip_stop, + ax_xip, + pte_xip_B, + theta_pure_eb, + xip_start, + xip_stop, r"$\xi_+^{\mathrm{B}}$", - show_xticklabels=True, show_yticklabels=True + show_xticklabels=True, + show_yticklabels=True, ) ax_xip.set_ylabel(r"$\theta_{\max}$ [arcmin]", labelpad=2) plot_pte_panel( - ax_xim, pte_xim_B, theta_pure_eb, - xim_start, xim_stop, + ax_xim, + pte_xim_B, + theta_pure_eb, + xim_start, + xim_stop, r"$\xi_-^{\mathrm{B}}$", - show_xticklabels=True, show_yticklabels=False + show_xticklabels=True, + show_yticklabels=False, ) im_cosebis = plot_pte_panel( - ax_cosebis, pte_cosebis, theta_cosebis, - cosebis_fid_start, cosebis_fid_stop, + ax_cosebis, + pte_cosebis, + theta_cosebis, + cosebis_fid_start, + cosebis_fid_stop, r"COSEBIS $B_n$", - show_xticklabels=True, show_yticklabels=False + show_xticklabels=True, + show_yticklabels=False, ) # Colorbar at exact height of rendered panels (accounts for aspect="equal") @@ -448,26 +486,39 @@ def create_3panel_composite(version, pure_eb_pte_files, cosebis_pte_files, cbar.ax.tick_params(labelsize=7) # Common x-axis label - fig.text(0.50, 0.02, r"$\theta_{\min}$ [arcmin]", - ha="center") + fig.text(0.50, 0.02, r"$\theta_{\min}$ [arcmin]", ha="center") # Compute statistics stats = { "xip": compute_stats(pte_xip_B, xip_start, xip_stop), "xim": compute_stats(pte_xim_B, xim_start, xim_stop), - "combined": compute_stats(pte_combined, xip_start, xip_stop) if pte_combined is not None else {"pte_at_fiducial": float("nan")}, + "combined": compute_stats(pte_combined, xip_start, xip_stop) + if pte_combined is not None + else {"pte_at_fiducial": float("nan")}, "cosebis": compute_stats(pte_cosebis, cosebis_fid_start, cosebis_fid_stop), - "cosebis_20": compute_stats(pte_cosebis_20, cosebis_fid_start, cosebis_fid_stop), + "cosebis_20": compute_stats( + pte_cosebis_20, cosebis_fid_start, cosebis_fid_stop + ), } # Extract full-range PTEs - full_range_ptes = extract_full_range_ptes(pure_eb_pte_files, cosebis_pte_files, version) + full_range_ptes = extract_full_range_ptes( + pure_eb_pte_files, cosebis_pte_files, version + ) return fig, stats, full_range_ptes -def create_9panel_composite(versions, pure_eb_pte_files, cosebis_pte_files, - xip_fid, xim_fid, cosebis_fid, version_labels, config): +def create_9panel_composite( + versions, + pure_eb_pte_files, + cosebis_pte_files, + xip_fid, + xim_fid, + cosebis_fid, + version_labels, + config, +): """Create a Nx3 composite figure for all versions (appendix). Parameters @@ -502,11 +553,15 @@ def create_9panel_composite(versions, pure_eb_pte_files, cosebis_pte_files, fig = plt.figure(figsize=(fig_width, fig_height)) gs = fig.add_gridspec( - n_versions, 3, + n_versions, + 3, width_ratios=[1, 1, 1], - wspace=0.03, hspace=0.04, - left=0.10, right=0.85, - bottom=0.08, top=0.94 + wspace=0.03, + hspace=0.04, + left=0.10, + right=0.85, + bottom=0.08, + top=0.94, ) all_stats = {} @@ -543,7 +598,7 @@ def create_9panel_composite(versions, pure_eb_pte_files, cosebis_pte_files, # Y-axis labels on each row (leftmost column only) # X-axis labels only on bottom row show_yticklabels = True - show_xticklabels = (row_idx == n_versions - 1) + show_xticklabels = row_idx == n_versions - 1 # Plot titles: version label + stat name on every row's panels version_label = version_labels.get(version, version) @@ -553,24 +608,36 @@ def create_9panel_composite(versions, pure_eb_pte_files, cosebis_pte_files, # Plot panels plot_pte_panel( - ax_xip, pte_xip_B, theta_pure_eb, - xip_start, xip_stop, + ax_xip, + pte_xip_B, + theta_pure_eb, + xip_start, + xip_stop, xip_title, - show_xticklabels=show_xticklabels, show_yticklabels=show_yticklabels + show_xticklabels=show_xticklabels, + show_yticklabels=show_yticklabels, ) plot_pte_panel( - ax_xim, pte_xim_B, theta_pure_eb, - xim_start, xim_stop, + ax_xim, + pte_xim_B, + theta_pure_eb, + xim_start, + xim_stop, xim_title, - show_xticklabels=show_xticklabels, show_yticklabels=False + show_xticklabels=show_xticklabels, + show_yticklabels=False, ) im_cosebis = plot_pte_panel( - ax_cosebis, pte_cosebis, theta_cosebis, - cosebis_fid_start, cosebis_fid_stop, + ax_cosebis, + pte_cosebis, + theta_cosebis, + cosebis_fid_start, + cosebis_fid_stop, cosebis_title, - show_xticklabels=show_xticklabels, show_yticklabels=False + show_xticklabels=show_xticklabels, + show_yticklabels=False, ) cosebis_axes.append(ax_cosebis) @@ -578,14 +645,20 @@ def create_9panel_composite(versions, pure_eb_pte_files, cosebis_pte_files, stats = { "xip": compute_stats(pte_xip_B, xip_start, xip_stop), "xim": compute_stats(pte_xim_B, xim_start, xim_stop), - "combined": compute_stats(pte_combined, xip_start, xip_stop) if pte_combined is not None else {"pte_at_fiducial": float("nan")}, + "combined": compute_stats(pte_combined, xip_start, xip_stop) + if pte_combined is not None + else {"pte_at_fiducial": float("nan")}, "cosebis": compute_stats(pte_cosebis, cosebis_fid_start, cosebis_fid_stop), - "cosebis_20": compute_stats(pte_cosebis_20, cosebis_fid_start, cosebis_fid_stop), + "cosebis_20": compute_stats( + pte_cosebis_20, cosebis_fid_start, cosebis_fid_stop + ), } all_stats[version] = stats # Extract full-range PTEs - full_range_ptes = extract_full_range_ptes(pure_eb_pte_files, cosebis_pte_files, version) + full_range_ptes = extract_full_range_ptes( + pure_eb_pte_files, cosebis_pte_files, version + ) all_full_range_ptes[version] = full_range_ptes # Colorbar: use actual rendered positions after aspect="equal" constraint @@ -672,6 +745,7 @@ def main(): except Exception: import traceback + print(" ERROR creating fiducial composite:", flush=True) traceback.print_exc() raise @@ -711,15 +785,21 @@ def main(): except Exception: import traceback + print(" ERROR creating appendix composite:", flush=True) traceback.print_exc() raise # Compute PTEs for uncorrected versions (no figures, evidence only) paper_version_labels = config["plotting"]["version_labels"] - uncorrected_versions = [v for v in versions if v not in paper_version_labels and v != fiducial_version] + uncorrected_versions = [ + v for v in versions if v not in paper_version_labels and v != fiducial_version + ] if uncorrected_versions: - print(f"\n--- Computing PTEs for uncorrected versions: {uncorrected_versions} ---", flush=True) + print( + f"\n--- Computing PTEs for uncorrected versions: {uncorrected_versions} ---", + flush=True, + ) for version in uncorrected_versions: try: matrices = _load_version_pte_data( @@ -737,19 +817,33 @@ def main(): stats = { "xip": compute_stats(matrices["pte_xip_B"], xip_start, xip_stop), "xim": compute_stats(matrices["pte_xim_B"], xim_start, xim_stop), - "combined": compute_stats(matrices["pte_combined"], xip_start, xip_stop) if matrices["pte_combined"] is not None else {"pte_at_fiducial": float("nan")}, - "cosebis": compute_stats(matrices["pte_cosebis"], cos_start, cos_stop), - "cosebis_20": compute_stats(matrices["pte_cosebis_20"], cos_start, cos_stop), + "combined": compute_stats( + matrices["pte_combined"], xip_start, xip_stop + ) + if matrices["pte_combined"] is not None + else {"pte_at_fiducial": float("nan")}, + "cosebis": compute_stats( + matrices["pte_cosebis"], cos_start, cos_stop + ), + "cosebis_20": compute_stats( + matrices["pte_cosebis_20"], cos_start, cos_stop + ), } - full_range_ptes = extract_full_range_ptes(pure_eb_pte_files, cosebis_pte_files, version) + full_range_ptes = extract_full_range_ptes( + pure_eb_pte_files, cosebis_pte_files, version + ) all_stats[version] = stats all_full_range_ptes[version] = full_range_ptes - print(f" {version}: xip_fid={stats['xip']['pte_at_fiducial']:.3f}, " - f"xim_fid={stats['xim']['pte_at_fiducial']:.3f}, " - f"cosebis_fid={stats['cosebis']['pte_at_fiducial']:.4f}", flush=True) + print( + f" {version}: xip_fid={stats['xip']['pte_at_fiducial']:.3f}, " + f"xim_fid={stats['xim']['pte_at_fiducial']:.3f}, " + f"cosebis_fid={stats['cosebis']['pte_at_fiducial']:.4f}", + flush=True, + ) except Exception: import traceback + print(f" WARNING: Could not compute PTEs for {version}:", flush=True) traceback.print_exc() @@ -799,8 +893,12 @@ def main(): }, } - evidence_data["output"]["figure_fiducial"] = Path(snakemake.output["figure_fiducial"]).name - evidence_data["output"]["figure_appendix"] = Path(snakemake.output["figure_appendix"]).name + evidence_data["output"]["figure_fiducial"] = Path( + snakemake.output["figure_fiducial"] + ).name + evidence_data["output"]["figure_appendix"] = Path( + snakemake.output["figure_appendix"] + ).name evidence_path = Path(snakemake.output["evidence"]) with open(evidence_path, "w") as f: diff --git a/papers/bmodes/scripts/cosebis_binning_comparison.py b/papers/bmodes/scripts/cosebis_binning_comparison.py index f5d1db2e..8249c155 100644 --- a/papers/bmodes/scripts/cosebis_binning_comparison.py +++ b/papers/bmodes/scripts/cosebis_binning_comparison.py @@ -17,17 +17,14 @@ import numpy as np import seaborn as sns import treecorr -from scipy import stats - from cosmo_numba.B_modes.cosebis import COSEBIS -from sp_validation.b_modes import calculate_cosebis, scale_cut_to_bins - from plotting_utils import ( FIG_WIDTH_SINGLE, PAPER_MPLSTYLE, compute_chi2_pte, ) +from sp_validation.b_modes import calculate_cosebis, scale_cut_to_bins plt.style.use(PAPER_MPLSTYLE) @@ -108,7 +105,9 @@ def main(): print("Loading 1,000-bin ξ±...") gg_1k = _load_gg(snakemake.input.xi_1k, min_sep_int, max_sep_int, nbins_1k) print("Loading 10,000-bin ξ±...") - gg_10k = _load_gg(snakemake.input.xi_10k, min_sep_int, max_sep_int, nbins_10k, columns_only=True) + gg_10k = _load_gg( + snakemake.input.xi_10k, min_sep_int, max_sep_int, nbins_10k, columns_only=True + ) # Compute COSEBIS from 1,000-bin ξ± (with covariance for PTE baseline) print("\nComputing COSEBIS from 1,000-bin ξ±...") diff --git a/papers/bmodes/scripts/cosebis_data_vector.py b/papers/bmodes/scripts/cosebis_data_vector.py index a46ae8d4..8d04c347 100644 --- a/papers/bmodes/scripts/cosebis_data_vector.py +++ b/papers/bmodes/scripts/cosebis_data_vector.py @@ -10,24 +10,20 @@ """ import json -import shutil from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt import numpy as np import seaborn as sns - import treecorr - -from sp_validation.b_modes import calculate_cosebis - from plotting_utils import ( FIG_WIDTH_SINGLE, PAPER_MPLSTYLE, iter_version_figures, ) +from sp_validation.b_modes import calculate_cosebis plt.style.use(PAPER_MPLSTYLE) @@ -110,7 +106,9 @@ def _create_single_panel_bmode_figure(datasets, nmodes, scale_cuts, title=None): ax.set_xticks(np.arange(1, nmodes + 1)) ax.set_xticklabels( [str(i) for i in range(1, nmodes + 1)], - rotation=45, ha="right", rotation_mode="anchor", + rotation=45, + ha="right", + rotation_mode="anchor", ) ax.tick_params(axis="both", width=0.5, length=3) @@ -192,9 +190,7 @@ def main(): gg.read(xi_paths[xi_key]) # Compute COSEBIS datasets - datasets = _compute_cosebis_datasets( - gg, cov_paths[cov_key], nmodes, scale_cuts - ) + datasets = _compute_cosebis_datasets(gg, cov_paths[cov_key], nmodes, scale_cuts) # Create figure with appropriate title fig = _create_single_panel_bmode_figure( diff --git a/papers/bmodes/scripts/cosebis_version_comparison.py b/papers/bmodes/scripts/cosebis_version_comparison.py index ef903feb..c13fab09 100644 --- a/papers/bmodes/scripts/cosebis_version_comparison.py +++ b/papers/bmodes/scripts/cosebis_version_comparison.py @@ -6,7 +6,6 @@ """ import json -import shutil from datetime import datetime from pathlib import Path @@ -14,8 +13,6 @@ import numpy as np import seaborn as sns import treecorr - -from sp_validation.b_modes import calculate_cosebis from plotting_utils import ( ERRORBAR_DEFAULTS, FIG_WIDTH_FULL, @@ -28,15 +25,21 @@ version_label, ) +from sp_validation.b_modes import calculate_cosebis plt.style.use(PAPER_MPLSTYLE) - - - -def _create_stacked_bmode_figure(fiducial_datasets, full_datasets, nmodes, scale_cuts, fiducial_idx, - y_limits=None, x_offsets=None, box_style=None): +def _create_stacked_bmode_figure( + fiducial_datasets, + full_datasets, + nmodes, + scale_cuts, + fiducial_idx, + y_limits=None, + x_offsets=None, + box_style=None, +): """Create a vertically stacked B-mode COSEBIS comparison figure. Plots B_n / sigma_n (dimensionless, in units of standard deviation). @@ -55,7 +58,9 @@ def _create_stacked_bmode_figure(fiducial_datasets, full_datasets, nmodes, scale x_offsets = np.array([-0.20, -0.07, 0.07, 0.20]) x_offsets = np.array(x_offsets) - fig, axes = plt.subplots(2, 1, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.4), sharex=True) + fig, axes = plt.subplots( + 2, 1, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.4), sharex=True + ) modes = np.arange(1, nmodes + 1) scale_labels = {"fiducial": "Fiducial", "full": "Full"} @@ -71,14 +76,20 @@ def _create_stacked_bmode_figure(fiducial_datasets, full_datasets, nmodes, scale for panel_idx, (scale_key, datasets, ax, scale_cut) in enumerate(panels): # Draw version spread boxes (before data points) draw_normalized_boxes_linear_scale( - ax, modes, datasets, - y_norm_key="Bn_normalized", fiducial_idx=fiducial_idx, - x_offsets=x_offsets, box_style=box_style + ax, + modes, + datasets, + y_norm_key="Bn_normalized", + fiducial_idx=fiducial_idx, + x_offsets=x_offsets, + box_style=box_style, ) for i, data in enumerate(datasets): offset = x_offsets[i] if i < len(x_offsets) else 0 - marker = data.get("marker", MARKER_STYLES[i] if i < len(MARKER_STYLES) else "o") + marker = data.get( + "marker", MARKER_STYLES[i] if i < len(MARKER_STYLES) else "o" + ) fillstyle = data.get("fillstyle", "full") mfc = data["color"] if fillstyle == "full" else "none" line = ax.errorbar( @@ -107,10 +118,12 @@ def _create_stacked_bmode_figure(fiducial_datasets, full_datasets, nmodes, scale label = scale_labels[scale_key] ax.text( - 0.98, 0.95, + 0.98, + 0.95, rf"{label} $\theta = {scale_cut[0]:.0f}$--${scale_cut[1]:.0f}'$", transform=ax.transAxes, - ha="right", va="top", + ha="right", + va="top", bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8), ) @@ -146,7 +159,8 @@ def main(): # Which version gets the fiducial reference line in boxes fiducial_for_comparison = getattr( - snakemake.params, "fiducial_for_comparison", + snakemake.params, + "fiducial_for_comparison", plotting_config.get("fiducial_for_comparison", config["fiducial"]["version"]), ) @@ -172,7 +186,6 @@ def main(): max_sep_int = float(config["fiducial"]["max_sep_int"]) nbins_int = int(config["fiducial"]["nbins_int"]) - # Color/marker assignment: pair by parent version if ecut versions present has_ecut = any("_ecut" in v for v in versions) if has_ecut: @@ -194,11 +207,13 @@ def main(): for scale_key, scale_cut in scale_cuts.items(): datasets = [] - for i, (version, xi_path, cov_path) in enumerate(zip( - versions, - snakemake.input["xi_integration"], - snakemake.input["cov_integration"], - )): + for i, (version, xi_path, cov_path) in enumerate( + zip( + versions, + snakemake.input["xi_integration"], + snakemake.input["cov_integration"], + ) + ): if has_ecut: parent = version.split("_ecut")[0].replace("_leak_corr", "") color = parent_color_map[parent] @@ -237,15 +252,19 @@ def main(): all_ptes[version][f"chi2_B_{scale_key}"] = float(chi2) all_ptes[version][f"dof_B_{scale_key}"] = int(dof) - datasets.append({ - "version": version, - "label": version_label(version, version_labels), - "color": color, - "marker": marker, - "fillstyle": fillstyle, - "alpha": get_version_alpha(version, fiducial_for_comparison, plotting_config), - "Bn_normalized": Bn / sigma_B, - }) + datasets.append( + { + "version": version, + "label": version_label(version, version_labels), + "color": color, + "marker": marker, + "fillstyle": fillstyle, + "alpha": get_version_alpha( + version, fiducial_for_comparison, plotting_config + ), + "Bn_normalized": Bn / sigma_B, + } + ) all_datasets[scale_key] = datasets @@ -259,7 +278,9 @@ def main(): y_limits = (min(all_y) - 0.1 * y_range, max(all_y) + 0.1 * y_range) # Find fiducial version index for box highlighting (use fiducial datasets) - fiducial_idx = find_fiducial_index(all_datasets["fiducial"], fiducial_for_comparison) + fiducial_idx = find_fiducial_index( + all_datasets["fiducial"], fiducial_for_comparison + ) # Create output output_dir = Path(snakemake.output["evidence"]).parent diff --git a/papers/bmodes/scripts/covariance_blind_consistency.py b/papers/bmodes/scripts/covariance_blind_consistency.py index 2e2dbdf2..99ff920c 100644 --- a/papers/bmodes/scripts/covariance_blind_consistency.py +++ b/papers/bmodes/scripts/covariance_blind_consistency.py @@ -55,10 +55,22 @@ def make_figure(theta, ratios_B, ratios_C, output_path): ax.axhspan(0.99, 1.01, color="teal", alpha=0.15, label=r"$\pm 1\%$") ax.axhspan(0.90, 1.10, color="orange", alpha=0.10, label=r"$\pm 10\%$") - ax.plot(theta, ratios_B[key]["ratio"], "o-", color=colors[0], - label="B / A", markersize=5) - ax.plot(theta, ratios_C[key]["ratio"], "s--", color=colors[1], - label="C / A", markersize=5) + ax.plot( + theta, + ratios_B[key]["ratio"], + "o-", + color=colors[0], + label="B / A", + markersize=5, + ) + ax.plot( + theta, + ratios_C[key]["ratio"], + "s--", + color=colors[1], + label="C / A", + markersize=5, + ) ax.set_xscale("log") ax.set_xlabel(r"$\theta$ [arcmin]") @@ -93,8 +105,10 @@ def main(snakemake): # Determine pass/fail all_max_devs = [ - ratios_B["xip"]["max_dev"], ratios_B["xim"]["max_dev"], - ratios_C["xip"]["max_dev"], ratios_C["xim"]["max_dev"], + ratios_B["xip"]["max_dev"], + ratios_B["xim"]["max_dev"], + ratios_C["xip"]["max_dev"], + ratios_C["xim"]["max_dev"], ] pass_1pct = all(d < 0.01 for d in all_max_devs) pass_10pct = all(d < 0.10 for d in all_max_devs) diff --git a/papers/bmodes/scripts/explorations/compare_mask_effect.py b/papers/bmodes/scripts/explorations/compare_mask_effect.py index 312d527e..0c5f8d6f 100644 --- a/papers/bmodes/scripts/explorations/compare_mask_effect.py +++ b/papers/bmodes/scripts/explorations/compare_mask_effect.py @@ -9,15 +9,14 @@ """ # %% -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns from pathlib import Path from typing import Tuple +import matplotlib.pyplot as plt +import numpy as np +import seaborn as sns from IPython import get_ipython - # %% ipython = get_ipython() if ipython is not None: @@ -30,7 +29,9 @@ # %% -def load_covariance_pair(mask_path: Path, nomask_path: Path) -> Tuple[np.ndarray, np.ndarray]: +def load_covariance_pair( + mask_path: Path, nomask_path: Path +) -> Tuple[np.ndarray, np.ndarray]: """Load covariance matrices for mask/no-mask comparison.""" if not mask_path.exists(): raise FileNotFoundError(f"Mask covariance not found: {mask_path}") @@ -97,7 +98,9 @@ def plot_comparison( ax_ratio.plot(indices, variance_ratio, linewidth=2, color="#ef8a62") ax_ratio.set_xlabel("Data vector index") - ax_ratio.set_ylabel("$\\sigma^2_{\\mathrm{mask}} / \\sigma^2_{\\mathrm{no\\ mask}}$") + ax_ratio.set_ylabel( + "$\\sigma^2_{\\mathrm{mask}} / \\sigma^2_{\\mathrm{no\\ mask}}$" + ) ax_ratio.set_title("Variance ratio") ax_ratio.grid(alpha=0.3) @@ -137,10 +140,14 @@ def main() -> None: nomask_path = Path(snakemake.input["nomask"]) # type: ignore[name-defined] output_path = Path(snakemake.output["plot"]) # type: ignore[name-defined] except NameError as exc: # pragma: no cover - executed only outside Snakemake - raise RuntimeError("This script is intended to be executed via Snakemake.") from exc + raise RuntimeError( + "This script is intended to be executed via Snakemake." + ) from exc mask_cov, nomask_cov = load_covariance_pair(mask_path, nomask_path) - mask_std, nomask_std, variance_ratio, covariance_delta = create_diagnostics(mask_cov, nomask_cov) + mask_std, nomask_std, variance_ratio, covariance_delta = create_diagnostics( + mask_cov, nomask_cov + ) output_path.parent.mkdir(parents=True, exist_ok=True) plot_comparison(mask_std, nomask_std, variance_ratio, covariance_delta, output_path) diff --git a/papers/bmodes/scripts/explorations/plot_mask_diagonal_ratio.py b/papers/bmodes/scripts/explorations/plot_mask_diagonal_ratio.py index 23bc55e2..32f930b5 100644 --- a/papers/bmodes/scripts/explorations/plot_mask_diagonal_ratio.py +++ b/papers/bmodes/scripts/explorations/plot_mask_diagonal_ratio.py @@ -5,25 +5,30 @@ import numpy as np import seaborn as sns - STYLE_PATH = Path( "/n17data/cdaley/unions/pure_eb/code/sp_validation/cosmo_inference/notebooks/2D_cosmic_shear_paper_plots/config/paper.mplstyle" ) -def compute_ratio(mask_path: Path, nomask_path: Path, data_out: Path, plot_out: Path) -> None: +def compute_ratio( + mask_path: Path, nomask_path: Path, data_out: Path, plot_out: Path +) -> None: mask_cov = np.loadtxt(mask_path) nomask_cov = np.loadtxt(nomask_path) if mask_cov.shape != nomask_cov.shape: - raise ValueError(f"Mask and no-mask covariances have mismatched shapes {mask_cov.shape} vs {nomask_cov.shape}") + raise ValueError( + f"Mask and no-mask covariances have mismatched shapes {mask_cov.shape} vs {nomask_cov.shape}" + ) nomask_diag = np.diag(nomask_cov) mask_diag = np.diag(mask_cov) if np.any(nomask_diag == 0): zero_indices = np.where(nomask_diag == 0)[0] - raise ValueError(f"Zero variance entries at indices {zero_indices.tolist()} prevent ratio evaluation.") + raise ValueError( + f"Zero variance entries at indices {zero_indices.tolist()} prevent ratio evaluation." + ) ratio = mask_diag / nomask_diag @@ -56,10 +61,22 @@ def run_from_snakemake() -> None: def run_from_cli() -> None: - parser = argparse.ArgumentParser(description="Plot ratio of covariance diagonals with and without mask.") - parser.add_argument("mask", type=Path, help="Path to Gaussian covariance matrix generated with mask.") - parser.add_argument("nomask", type=Path, help="Path to Gaussian covariance matrix generated without mask.") - parser.add_argument("data", type=Path, help="Output path for diagonal ratio text file.") + parser = argparse.ArgumentParser( + description="Plot ratio of covariance diagonals with and without mask." + ) + parser.add_argument( + "mask", + type=Path, + help="Path to Gaussian covariance matrix generated with mask.", + ) + parser.add_argument( + "nomask", + type=Path, + help="Path to Gaussian covariance matrix generated without mask.", + ) + parser.add_argument( + "data", type=Path, help="Output path for diagonal ratio text file." + ) parser.add_argument("plot", type=Path, help="Output path for diagonal ratio plot.") args = parser.parse_args() diff --git a/papers/bmodes/scripts/filter_catalog_ellipticity.py b/papers/bmodes/scripts/filter_catalog_ellipticity.py index 5e41eb44..ade5dd6d 100644 --- a/papers/bmodes/scripts/filter_catalog_ellipticity.py +++ b/papers/bmodes/scripts/filter_catalog_ellipticity.py @@ -11,8 +11,12 @@ import numpy as np from astropy.io import fits -sys.stdout = sys.stdout if hasattr(sys, "ps1") else open(sys.stdout.fileno(), "w", buffering=1) -sys.stderr = sys.stderr if hasattr(sys, "ps1") else open(sys.stderr.fileno(), "w", buffering=1) +sys.stdout = ( + sys.stdout if hasattr(sys, "ps1") else open(sys.stdout.fileno(), "w", buffering=1) +) +sys.stderr = ( + sys.stderr if hasattr(sys, "ps1") else open(sys.stderr.fileno(), "w", buffering=1) +) from snakemake.script import snakemake # noqa: E402 @@ -39,7 +43,9 @@ mask = e_mag < e_max del e_mag n_kept = int(np.sum(mask)) - print(f"Ellipticity cut |e| < {e_max}: {n_kept}/{n_total} galaxies kept ({100*n_kept/n_total:.1f}%)") + print( + f"Ellipticity cut |e| < {e_max}: {n_kept}/{n_total} galaxies kept ({100 * n_kept / n_total:.1f}%)" + ) # Compute survey properties from masked columns (memory-efficient) w = np.asarray(data[w_col][mask], dtype=np.float64) @@ -73,7 +79,7 @@ } print(f"Survey properties: n_eff={n_eff:.3f} gal/arcmin², sigma_e={sigma_e:.4f}") -print(f" (parent n_eff and sigma_e should be compared to assess impact)") +print(" (parent n_eff and sigma_e should be compared to assess impact)") with open(output_props, "w") as f: json.dump(props, f, indent=2) diff --git a/papers/bmodes/scripts/gather_pure_eb_chunks.py b/papers/bmodes/scripts/gather_pure_eb_chunks.py index 0d8931bc..d8736c72 100644 --- a/papers/bmodes/scripts/gather_pure_eb_chunks.py +++ b/papers/bmodes/scripts/gather_pure_eb_chunks.py @@ -62,9 +62,14 @@ def main(): # Compute data vectors from correlation functions eb_results = get_pure_EB_modes( - theta=gg["meanr"], xip=gg["xip"], xim=gg["xim"], - theta_int=gg_int["meanr"], xip_int=gg_int["xip"], xim_int=gg_int["xim"], - tmin=numeric_params["min_sep"], tmax=numeric_params["max_sep"] + theta=gg["meanr"], + xip=gg["xip"], + xim=gg["xim"], + theta_int=gg_int["meanr"], + xip_int=gg_int["xip"], + xim_int=gg_int["xim"], + tmin=numeric_params["min_sep"], + tmax=numeric_params["max_sep"], ) xip_E, xim_E, xip_B, xim_B, xip_amb, xim_amb = eb_results diff --git a/papers/bmodes/scripts/generate_paper_macros.py b/papers/bmodes/scripts/generate_paper_macros.py index dd24b0b5..c63cc35e 100644 --- a/papers/bmodes/scripts/generate_paper_macros.py +++ b/papers/bmodes/scripts/generate_paper_macros.py @@ -14,7 +14,14 @@ # Version number to word mapping for TeX-safe macro names # (avoids cleveref/siunitx conflict with numeric names) -VERSION_WORDS = {"5": "Five", "6": "Six", "6.3": "SixThree", "8": "Eight", "11.2": "ElevenTwo", "11.3": "ElevenThree"} +VERSION_WORDS = { + "5": "Five", + "6": "Six", + "6.3": "SixThree", + "8": "Eight", + "11.2": "ElevenTwo", + "11.3": "ElevenThree", +} def _parse_version_short(version: str) -> str: @@ -87,20 +94,28 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version fid_versions = fiducial.get("versions", {}) fid_data = fid_versions.get(fiducial_version, {}) if "pte_6_min" in fid_data: - macros.append(f"\\newcommand{{\\cosebisfiducialPte}}{{{_format_value(fid_data['pte_6_min'])}}}") + macros.append( + f"\\newcommand{{\\cosebisfiducialPte}}{{{_format_value(fid_data['pte_6_min'])}}}" + ) # Full range full = cosebis_ev.get("full", {}) full_versions = full.get("versions", {}) full_data = full_versions.get(fiducial_version, {}) if "pte_6_min" in full_data: - macros.append(f"\\newcommand{{\\cosebisfullPte}}{{{_format_value(full_data['pte_6_min'])}}}") + macros.append( + f"\\newcommand{{\\cosebisfullPte}}{{{_format_value(full_data['pte_6_min'])}}}" + ) # Scale cuts from fiducial if "scale_cut_arcmin" in fiducial: cuts = fiducial["scale_cut_arcmin"] - macros.append(f"\\newcommand{{\\cosebisthetaMin}}{{{_format_value(cuts[0])}}}") - macros.append(f"\\newcommand{{\\cosebisthetaMax}}{{{_format_value(cuts[1])}}}") + macros.append( + f"\\newcommand{{\\cosebisthetaMin}}{{{_format_value(cuts[0])}}}" + ) + macros.append( + f"\\newcommand{{\\cosebisthetaMax}}{{{_format_value(cuts[1])}}}" + ) macros.append("") @@ -116,12 +131,16 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version # Fiducial PTEs - use pte_joint_min (conservative across blinds) eb_fid = eb_ev.get("fiducial", {}) if "pte_joint_min" in eb_fid: - macros.append(f"\\newcommand{{\\ebfiducialPte}}{{{_format_value(eb_fid['pte_joint_min'])}}}") + macros.append( + f"\\newcommand{{\\ebfiducialPte}}{{{_format_value(eb_fid['pte_joint_min'])}}}" + ) # Full range PTEs full = eb_ev.get("full", {}) if "pte_joint_min" in full: - macros.append(f"\\newcommand{{\\ebfullPte}}{{{_format_value(full['pte_joint_min'])}}}") + macros.append( + f"\\newcommand{{\\ebfullPte}}{{{_format_value(full['pte_joint_min'])}}}" + ) # Scale cuts from fiducial if "scale_cut_xip" in eb_fid: @@ -158,7 +177,9 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version # Full matrix if "condition_number" in ev: - macros.append(f"\\newcommand{{\\ebcovCondFull}}{{\\num{{{ev['condition_number']:.1e}}}}}") + macros.append( + f"\\newcommand{{\\ebcovCondFull}}{{\\num{{{ev['condition_number']:.1e}}}}}" + ) if "n_bins" in ev: macros.append(f"\\newcommand{{\\ebcovNbins}}{{\\num{{{ev['n_bins']}}}}}") @@ -176,20 +197,36 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version # Max deviations across blinds xip = ev.get("xip", {}) xim = ev.get("xim", {}) - xip_max = max(xip.get("B_to_A", {}).get("max_dev", 0), xip.get("C_to_A", {}).get("max_dev", 0)) - xim_max = max(xim.get("B_to_A", {}).get("max_dev", 0), xim.get("C_to_A", {}).get("max_dev", 0)) - macros.append(f"\\newcommand{{\\covXipMaxDev}}{{{_format_value(xip_max * 100)}\\%}}") - macros.append(f"\\newcommand{{\\covXimMaxDev}}{{{_format_value(xim_max * 100)}\\%}}") + xip_max = max( + xip.get("B_to_A", {}).get("max_dev", 0), + xip.get("C_to_A", {}).get("max_dev", 0), + ) + xim_max = max( + xim.get("B_to_A", {}).get("max_dev", 0), + xim.get("C_to_A", {}).get("max_dev", 0), + ) + macros.append( + f"\\newcommand{{\\covXipMaxDev}}{{{_format_value(xip_max * 100)}\\%}}" + ) + macros.append( + f"\\newcommand{{\\covXimMaxDev}}{{{_format_value(xim_max * 100)}\\%}}" + ) macros.append("") # PTE variation across blinds (reuse eb_fid cached above) if eb_fid: - joint_ptes = [eb_fid.get(f"pte_joint_{b}") for b in ["A", "B", "C"] if f"pte_joint_{b}" in eb_fid] + joint_ptes = [ + eb_fid.get(f"pte_joint_{b}") + for b in ["A", "B", "C"] + if f"pte_joint_{b}" in eb_fid + ] if joint_ptes: macros.append("% PTE variation across blinds (fiducial scale cuts)") joint_delta = max(joint_ptes) - min(joint_ptes) - macros.append(f"\\newcommand{{\\ebJointPteDelta}}{{{_format_value(joint_delta)}}}") + macros.append( + f"\\newcommand{{\\ebJointPteDelta}}{{{_format_value(joint_delta)}}}" + ) macros.append("") # Config-space PTE matrices - generate table @@ -219,29 +256,48 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version cosebis_20 = ver_data.get("cosebis_20_stats", {}) # Fiducial PTEs - bold = 0.05 if "pte_at_fiducial" in xip: - macros.append(f"\\newcommand{{\\{prefix}Xip}}{{{_format_value(xip['pte_at_fiducial'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}Xip}}{{{_format_value(xip['pte_at_fiducial'])}}}" + ) if "pte_at_fiducial" in xim: - macros.append(f"\\newcommand{{\\{prefix}Xim}}{{{_format_value(xim['pte_at_fiducial'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}Xim}}{{{_format_value(xim['pte_at_fiducial'])}}}" + ) if "pte_at_fiducial" in combined: - macros.append(f"\\newcommand{{\\{prefix}Combined}}{{{_format_value(combined['pte_at_fiducial'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}Combined}}{{{_format_value(combined['pte_at_fiducial'])}}}" + ) if "pte_at_fiducial" in cosebis: - macros.append(f"\\newcommand{{\\{prefix}Cosebis}}{{{_format_value(cosebis['pte_at_fiducial'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}Cosebis}}{{{_format_value(cosebis['pte_at_fiducial'])}}}" + ) if "pte_at_fiducial" in cosebis_20: - macros.append(f"\\newcommand{{\\{prefix}CosebisTwenty}}{{{_format_value(cosebis_20['pte_at_fiducial'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}CosebisTwenty}}{{{_format_value(cosebis_20['pte_at_fiducial'])}}}" + ) # Full-range PTEs if "pte_at_full_range" in xip: - macros.append(f"\\newcommand{{\\{prefix}XipFull}}{{{_format_value(xip['pte_at_full_range'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}XipFull}}{{{_format_value(xip['pte_at_full_range'])}}}" + ) if "pte_at_full_range" in xim: - macros.append(f"\\newcommand{{\\{prefix}XimFull}}{{{_format_value(xim['pte_at_full_range'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}XimFull}}{{{_format_value(xim['pte_at_full_range'])}}}" + ) if "pte_at_full_range" in combined: - macros.append(f"\\newcommand{{\\{prefix}CombinedFull}}{{{_format_value(combined['pte_at_full_range'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}CombinedFull}}{{{_format_value(combined['pte_at_full_range'])}}}" + ) if "pte_at_full_range" in cosebis: - macros.append(f"\\newcommand{{\\{prefix}CosebisFull}}{{{_format_value(cosebis['pte_at_full_range'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}CosebisFull}}{{{_format_value(cosebis['pte_at_full_range'])}}}" + ) if "pte_at_full_range" in cosebis_20: - macros.append(f"\\newcommand{{\\{prefix}CosebisTwentyFull}}{{{_format_value(cosebis_20['pte_at_full_range'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}CosebisTwentyFull}}{{{_format_value(cosebis_20['pte_at_full_range'])}}}" + ) macros.append("") @@ -260,8 +316,7 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version # duplicate macro definitions — both SP_v1.4.6_ecut07_leak_corr and # SP_v1.4.6_leak_corr would map to the same clPteSix prefix. leak_corr_versions = { - k: v for k, v in versions.items() - if "leak_corr" in k and "ecut" not in k + k: v for k, v in versions.items() if "leak_corr" in k and "ecut" not in k } for ver, ver_data in leak_corr_versions.items(): @@ -270,17 +325,25 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version # Fiducial PTEs if "pte_at_fiducial" in ver_data: - macros.append(f"\\newcommand{{\\{prefix}Fid}}{{{_format_value(ver_data['pte_at_fiducial'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}Fid}}{{{_format_value(ver_data['pte_at_fiducial'])}}}" + ) # Full-range PTEs if "pte_at_full_range" in ver_data: - macros.append(f"\\newcommand{{\\{prefix}Full}}{{{_format_value(ver_data['pte_at_full_range'])}}}") + macros.append( + f"\\newcommand{{\\{prefix}Full}}{{{_format_value(ver_data['pte_at_full_range'])}}}" + ) macros.append("") # Harmonic-config COSEBIS comparison - B-mode PTEs (modes 1-N) per angular range for angular_range in ["full", "fiducial"]: - harm_cosebis_path = claims_dir / f"harmonic_config_cosebis_comparison_{angular_range}" / "evidence.json" + harm_cosebis_path = ( + claims_dir + / f"harmonic_config_cosebis_comparison_{angular_range}" + / "evidence.json" + ) if not harm_cosebis_path.exists(): continue with open(harm_cosebis_path) as f: @@ -288,10 +351,15 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version ev = data.get("evidence", {}) range_suffix = "Full" if angular_range == "full" else "Fid" - macros.append(f"% harmonic_config_cosebis_comparison_{angular_range} (B-mode COSEBIS PTEs)") + macros.append( + f"% harmonic_config_cosebis_comparison_{angular_range} (B-mode COSEBIS PTEs)" + ) macros.append("") - for method, method_prefix in [("harmonic_b_mode_ptes", "harmCosebis"), ("config_b_mode_ptes", "cfgCosebis")]: + for method, method_prefix in [ + ("harmonic_b_mode_ptes", "harmCosebis"), + ("config_b_mode_ptes", "cfgCosebis"), + ]: ptes = ev.get(method, {}) for ver, pte_data in ptes.items(): short_ver = _parse_version_short(ver) @@ -299,9 +367,13 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version pte = pte_data.get("pte") chi2 = pte_data.get("chi2") if pte is not None: - macros.append(f"\\newcommand{{\\{method_prefix}Pte{word}{range_suffix}}}{{{_format_value(pte)}}}") + macros.append( + f"\\newcommand{{\\{method_prefix}Pte{word}{range_suffix}}}{{{_format_value(pte)}}}" + ) if chi2 is not None: - macros.append(f"\\newcommand{{\\{method_prefix}Chisq{word}{range_suffix}}}{{{_format_value(chi2)}}}") + macros.append( + f"\\newcommand{{\\{method_prefix}Chisq{word}{range_suffix}}}{{{_format_value(chi2)}}}" + ) macros.append("") @@ -312,7 +384,13 @@ def generate_macros(claims_dir: Path, output_paths: list[Path], fiducial_version print(f" → {output_path}") -_CONFIG_STATS = ["cosebis_stats", "cosebis_20_stats", "xip_stats", "xim_stats", "combined_stats"] +_CONFIG_STATS = [ + "cosebis_stats", + "cosebis_20_stats", + "xip_stats", + "xim_stats", + "combined_stats", +] def _pte_row_cells(pte_key, cfg, harm, bold, italic=None): @@ -341,12 +419,23 @@ def _pte_row_cells(pte_key, cfg, harm, bold, italic=None): cells = [] for stat in _CONFIG_STATS: s = cfg.get(stat, {}) - cells.append(f"& {_format_value(s.get(pte_key, float('nan')), bold_threshold=bold, italic_threshold=italic)}") - cells.append(f"& {_format_value(harm.get(pte_key, float('nan')), bold_threshold=bold, italic_threshold=italic)}") + cells.append( + f"& {_format_value(s.get(pte_key, float('nan')), bold_threshold=bold, italic_threshold=italic)}" + ) + cells.append( + f"& {_format_value(harm.get(pte_key, float('nan')), bold_threshold=bold, italic_threshold=italic)}" + ) return cells -def generate_pte_tables(claims_dir: Path, output_dir: Path, fiducial_version: str, versions: list, version_labels: dict, config: dict): +def generate_pte_tables( + claims_dir: Path, + output_dir: Path, + fiducial_version: str, + versions: list, + version_labels: dict, + config: dict, +): """Generate LaTeX table files from PTE evidence. Creates: @@ -378,29 +467,37 @@ def generate_pte_tables(claims_dir: Path, output_dir: Path, fiducial_version: st with open(harmonic_pte_path) as f: harmonic_data = json.load(f).get("evidence", {}).get("versions", {}) - # Get table label for caption - fid_label = table_labels.get(fiducial_version, version_labels.get(fiducial_version, fiducial_version)) - # Results table (fiducial only) — grouped by statistic family if fiducial_version in config_data or fiducial_version in harmonic_data: results_table = [] results_table.append("% Auto-generated PTE summary tabular (Results section)") results_table.append("% Regenerate: snakemake paper_macros") - results_table.append(r"% Wrap in table*/table environment in main tex for float control") + results_table.append( + r"% Wrap in table*/table environment in main tex for float control" + ) # Column layout: Scale | COSEBIS n≤6 | COSEBIS n≤20 || ξ+^B | ξ-^B | ξ_tot^B ||| C_ℓ^BB results_table.append(r"\begin{tabular}{l cc @{\hskip 8pt} ccc @{\hskip 8pt} c}") results_table.append(r" \hline\hline") results_table.append(r" \noalign{\vskip 3pt}") - results_table.append(r" & \multicolumn{2}{c}{COSEBIs} & \multicolumn{3}{c}{Pure E/B} & $C_\ell$ \\") - results_table.append(r" \cmidrule(lr){2-3} \cmidrule(lr){4-6} \cmidrule(l){7-7}") - results_table.append(r" Scale cuts & $B_n$ ($n \leq 6$) & $B_n$ ($n \leq 20$) & $\xi_+^{\mathrm{B}}$ & $\xi_-^{\mathrm{B}}$ & $\xi_{\mathrm{tot}}^{\mathrm{B}}$ & $C_\ell^{BB}$ \\") + results_table.append( + r" & \multicolumn{2}{c}{COSEBIs} & \multicolumn{3}{c}{Pure E/B} & $C_\ell$ \\" + ) + results_table.append( + r" \cmidrule(lr){2-3} \cmidrule(lr){4-6} \cmidrule(l){7-7}" + ) + results_table.append( + r" Scale cuts & $B_n$ ($n \leq 6$) & $B_n$ ($n \leq 20$) & $\xi_+^{\mathrm{B}}$ & $\xi_-^{\mathrm{B}}$ & $\xi_{\mathrm{tot}}^{\mathrm{B}}$ & $C_\ell^{BB}$ \\" + ) results_table.append(r" \hline") results_table.append(r" \noalign{\vskip 3pt}") cfg = config_data.get(fiducial_version, {}) harm = harmonic_data.get(fiducial_version, {}) - for pte_key, cut_label in [("pte_at_fiducial", "Fiducial"), ("pte_at_full_range", "Full range")]: + for pte_key, cut_label in [ + ("pte_at_fiducial", "Fiducial"), + ("pte_at_full_range", "Full range"), + ]: row = [f" {cut_label}"] row.extend(_pte_row_cells(pte_key, cfg, harm, bold)) row.append(r" \\") @@ -419,13 +516,23 @@ def generate_pte_tables(claims_dir: Path, output_dir: Path, fiducial_version: st appendix_table = [] appendix_table.append("% Auto-generated PTE comparison tabular (Appendix)") appendix_table.append("% Regenerate: snakemake paper_macros") - appendix_table.append(r"% Wrap in table*/table environment in main tex for float control") - appendix_table.append(r"\begin{tabular}{ll cc @{\hskip 8pt} ccc @{\hskip 8pt} c}") + appendix_table.append( + r"% Wrap in table*/table environment in main tex for float control" + ) + appendix_table.append( + r"\begin{tabular}{ll cc @{\hskip 8pt} ccc @{\hskip 8pt} c}" + ) appendix_table.append(r" \hline\hline") appendix_table.append(r" \noalign{\vskip 3pt}") - appendix_table.append(r" & & \multicolumn{2}{c}{COSEBIs} & \multicolumn{3}{c}{Pure E/B} & $C_\ell$ \\") - appendix_table.append(r" \cmidrule(lr){3-4} \cmidrule(lr){5-7} \cmidrule(l){8-8}") - appendix_table.append(r" Version & Scale cuts & $B_n$ ($n \leq 6$) & $B_n$ ($n \leq 20$) & $\xi_+^{\mathrm{B}}$ & $\xi_-^{\mathrm{B}}$ & $\xi_{\mathrm{tot}}^{\mathrm{B}}$ & $C_\ell^{BB}$ \\") + appendix_table.append( + r" & & \multicolumn{2}{c}{COSEBIs} & \multicolumn{3}{c}{Pure E/B} & $C_\ell$ \\" + ) + appendix_table.append( + r" \cmidrule(lr){3-4} \cmidrule(lr){5-7} \cmidrule(l){8-8}" + ) + appendix_table.append( + r" Version & Scale cuts & $B_n$ ($n \leq 6$) & $B_n$ ($n \leq 20$) & $\xi_+^{\mathrm{B}}$ & $\xi_-^{\mathrm{B}}$ & $\xi_{\mathrm{tot}}^{\mathrm{B}}$ & $C_\ell^{BB}$ \\" + ) appendix_table.append(r" \hline") appendix_table.append(r" \noalign{\vskip 3pt}") @@ -436,7 +543,10 @@ def generate_pte_tables(claims_dir: Path, output_dir: Path, fiducial_version: st if ver == fiducial_version: label = f"{label} (fiducial)" - for pte_key, cut_label in [("pte_at_fiducial", "Fiducial"), ("pte_at_full_range", "Full range")]: + for pte_key, cut_label in [ + ("pte_at_fiducial", "Fiducial"), + ("pte_at_full_range", "Full range"), + ]: # Only show version label on first row of each pair row_label = label if pte_key == "pte_at_fiducial" else "" row = [f" {row_label} & {cut_label}"] @@ -511,7 +621,9 @@ def generate_evidence( # Separate macro file from PTE tables and evidence # Only claims_macros.tex gets macro content; PTE tables generated separately macro_file = [Path(p) for p in snakemake.output if p.endswith("claims_macros.tex")] # noqa: F821 - evidence_outputs = [Path(p) for p in snakemake.output if p.endswith("evidence.json")] # noqa: F821 + evidence_outputs = [ + Path(p) for p in snakemake.output if p.endswith("evidence.json") + ] # noqa: F821 print(f"Generating macros from {tapestry_dir}") generate_macros(tapestry_dir, macro_file, fiducial_version) @@ -520,12 +632,16 @@ def generate_evidence( if macro_file: paper_dir = macro_file[0].parent print(f"Generating PTE tables to {paper_dir}") - generate_pte_tables(tapestry_dir, paper_dir, fiducial_version, versions, version_labels, config) + generate_pte_tables( + tapestry_dir, paper_dir, fiducial_version, versions, version_labels, config + ) # Generate evidence.json if requested # Dependencies derived from snakemake inputs (rules.X.output declarations) rule_inputs = snakemake.input.keys() # noqa: F821 - input_deps = [k for k in rule_inputs if k.endswith("_evidence") or k == "covariance_evidence"] + input_deps = [ + k for k in rule_inputs if k.endswith("_evidence") or k == "covariance_evidence" + ] depends_on = [d.replace("_evidence", "") for d in input_deps] for evidence_path in evidence_outputs: diff --git a/papers/bmodes/scripts/harmonic_config_cosebis_comparison.py b/papers/bmodes/scripts/harmonic_config_cosebis_comparison.py index ce563950..58d1f06e 100644 --- a/papers/bmodes/scripts/harmonic_config_cosebis_comparison.py +++ b/papers/bmodes/scripts/harmonic_config_cosebis_comparison.py @@ -12,7 +12,6 @@ """ import json -import shutil from datetime import datetime from pathlib import Path @@ -21,9 +20,7 @@ import seaborn as sns import treecorr from astropy.io import fits - from cosmo_numba.B_modes.cosebis import COSEBIS -from sp_validation.b_modes import calculate_cosebis from plotting_utils import ( FIG_WIDTH_FULL, FIG_WIDTH_SINGLE, @@ -33,6 +30,8 @@ version_label, ) +from sp_validation.b_modes import calculate_cosebis + plt.style.use(PAPER_MPLSTYLE) # At 32 bins, modes > 6 are unreliable. At 96 bins, modes 1-6 are recovered @@ -83,10 +82,16 @@ def _build_transforms(cosebis_obj, ell): basis[idx] = 1.0 ce_basis, _ = cosebis_obj.cosebis_from_Cell( - ell=ell, Cell_E=basis, Cell_B=zeros, cache=True, + ell=ell, + Cell_E=basis, + Cell_B=zeros, + cache=True, ) _, cb_basis = cosebis_obj.cosebis_from_Cell( - ell=ell, Cell_E=zeros, Cell_B=basis, cache=True, + ell=ell, + Cell_E=zeros, + Cell_B=basis, + cache=True, ) T_E[:, idx] = ce_basis @@ -95,7 +100,9 @@ def _build_transforms(cosebis_obj, ell): return T_E, T_B -def _compute_harmonic_cosebis(pseudo_cl_path, pseudo_cov_path, nmodes, theta_min, theta_max): +def _compute_harmonic_cosebis( + pseudo_cl_path, pseudo_cov_path, nmodes, theta_min, theta_max +): """Compute COSEBIS from pseudo-C_ell and propagate covariance.""" with fits.open(pseudo_cl_path) as hdul: data = hdul["PSEUDO_CELL"].data @@ -106,7 +113,10 @@ def _compute_harmonic_cosebis(pseudo_cl_path, pseudo_cov_path, nmodes, theta_min cosebis_obj = COSEBIS(theta_min, theta_max, nmodes) ce_harm, cb_harm = cosebis_obj.cosebis_from_Cell( - ell=ell, Cell_E=cl_ee, Cell_B=cl_bb, cache=True, + ell=ell, + Cell_E=cl_ee, + Cell_B=cl_bb, + cache=True, ) T_E, T_B = _build_transforms(cosebis_obj, ell) @@ -131,11 +141,16 @@ def _compute_config_cosebis(xi_path, cov_path, nmodes, scale_cut, config): nbins_int = int(config["fiducial"]["nbins_int"]) gg = treecorr.GGCorrelation( - min_sep=min_sep_int, max_sep=max_sep_int, nbins=nbins_int, sep_units="arcmin", + min_sep=min_sep_int, + max_sep=max_sep_int, + nbins=nbins_int, + sep_units="arcmin", ) gg.read(xi_path) - results = calculate_cosebis(gg, nmodes=nmodes, scale_cuts=[scale_cut], cov_path=cov_path) + results = calculate_cosebis( + gg, nmodes=nmodes, scale_cuts=[scale_cut], cov_path=cov_path + ) r = results[scale_cut] return r["En"], r["Bn"], r["cov"] @@ -145,7 +160,9 @@ def _shade_reliable(ax, reliable_mode_max=6): ax.axvspan(0.5, reliable_mode_max + 0.5, color="0.95", alpha=0.5, zorder=0) -def _make_data_vector_figure(harm_data, config_data, nmodes, scale_cut, title=None, reliable_mode_max=6): +def _make_data_vector_figure( + harm_data, config_data, nmodes, scale_cut, title=None, reliable_mode_max=6 +): """Data vector figure: E and B from both methods (single scale cut). Two rows (E-modes, B-modes). @@ -163,19 +180,40 @@ def _make_data_vector_figure(harm_data, config_data, nmodes, scale_cut, title=No sigma_harm_E = harm_data["sigma_E"] sigma_harm_B = np.where(harm_data["sigma_B"] > 0, harm_data["sigma_B"], 1.0) - fig, (ax_e, ax_b) = plt.subplots(2, 1, figsize=(FIG_WIDTH_SINGLE, FIG_WIDTH_SINGLE * 0.85), sharex=True) + fig, (ax_e, ax_b) = plt.subplots( + 2, 1, figsize=(FIG_WIDTH_SINGLE, FIG_WIDTH_SINGLE * 0.85), sharex=True + ) # --- E-modes (factored by 10^10 for readability) --- e_scale = 1e10 ax_e.errorbar( - modes - 0.1, ce_config * e_scale, yerr=sigma_config_E * e_scale, - fmt="o", color=c_cfg, mfc=c_cfg, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, label="Config-space", + modes - 0.1, + ce_config * e_scale, + yerr=sigma_config_E * e_scale, + fmt="o", + color=c_cfg, + mfc=c_cfg, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, + label="Config-space", ) ax_e.errorbar( - modes + 0.1, ce_harm * e_scale, yerr=sigma_harm_E * e_scale, - fmt="s", color=c_harm, mfc="white", mew=0.8, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, label="Harmonic-space", + modes + 0.1, + ce_harm * e_scale, + yerr=sigma_harm_E * e_scale, + fmt="s", + color=c_harm, + mfc="white", + mew=0.8, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, + label="Harmonic-space", ) ax_e.axhline(0.0, color="black", lw=0.8, alpha=0.6) @@ -189,14 +227,31 @@ def _make_data_vector_figure(harm_data, config_data, nmodes, scale_cut, title=No # --- B-modes (B_n / sigma units) --- ax_b.errorbar( - modes - 0.1, cb_config / sigma_config_B, yerr=np.ones(nmodes), - fmt="o", color=c_cfg, mfc=c_cfg, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, + modes - 0.1, + cb_config / sigma_config_B, + yerr=np.ones(nmodes), + fmt="o", + color=c_cfg, + mfc=c_cfg, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, ) ax_b.errorbar( - modes + 0.1, cb_harm / sigma_harm_B, yerr=np.ones(nmodes), - fmt="s", color=c_harm, mfc="white", mew=0.8, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, + modes + 0.1, + cb_harm / sigma_harm_B, + yerr=np.ones(nmodes), + fmt="s", + color=c_harm, + mfc="white", + mew=0.8, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, ) ax_b.axhline(0.0, color="black", lw=0.8, alpha=0.6) @@ -208,9 +263,12 @@ def _make_data_vector_figure(harm_data, config_data, nmodes, scale_cut, title=No ax_b.tick_params(axis="both", width=0.5, length=3) ax_b.text( - 0.98, 0.95, + 0.98, + 0.95, rf"$\theta = {scale_cut[0]:.0f}$--${scale_cut[1]:.0f}'$", - transform=ax_b.transAxes, ha="right", va="top", + transform=ax_b.transAxes, + ha="right", + va="top", bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8), ) @@ -219,21 +277,29 @@ def _make_data_vector_figure(harm_data, config_data, nmodes, scale_cut, title=No return fig -def _make_combined_data_vector_figure(harm_data_full, config_data_full, - harm_data_fid, config_data_fid, - nmodes, scale_cut_full, scale_cut_fid, - title=None, reliable_mode_max=6): +def _make_combined_data_vector_figure( + harm_data_full, + config_data_full, + harm_data_fid, + config_data_fid, + nmodes, + scale_cut_full, + scale_cut_fid, + title=None, + reliable_mode_max=6, +): """Combined 2x2 data vector figure: rows=E/B, columns=full/fiducial. Column titles show the theta range. Gray band over reliable modes (1-6). Dark2 palette, filled circles for config-space, open squares for harmonic. """ modes = np.arange(1, nmodes + 1) - c_cfg = "#2c5f8a" # steel blue (config-space) + c_cfg = "#2c5f8a" # steel blue (config-space) c_harm = "#c45a2c" # burnt orange (harmonic-space) - fig, axes = plt.subplots(2, 2, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.4), - sharex=True) + fig, axes = plt.subplots( + 2, 2, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.4), sharex=True + ) datasets = [ (config_data_full, harm_data_full, scale_cut_full), @@ -257,15 +323,32 @@ def _make_combined_data_vector_figure(harm_data_full, config_data_full, # E-modes ax_e = axes[0, col] ax_e.errorbar( - modes - 0.1, ce_cfg * e_scale, yerr=sigma_cfg_E * e_scale, - fmt="o", color=c_cfg, mfc=c_cfg, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, + modes - 0.1, + ce_cfg * e_scale, + yerr=sigma_cfg_E * e_scale, + fmt="o", + color=c_cfg, + mfc=c_cfg, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, label="Config-space" if col == 0 else None, ) ax_e.errorbar( - modes + 0.1, ce_h * e_scale, yerr=sigma_h_E * e_scale, - fmt="s", color=c_harm, mfc="white", mew=0.8, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, + modes + 0.1, + ce_h * e_scale, + yerr=sigma_h_E * e_scale, + fmt="s", + color=c_harm, + mfc="white", + mew=0.8, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, label="Harmonic-space" if col == 0 else None, ) ax_e.axhline(0.0, color="black", lw=0.8, alpha=0.6) @@ -276,14 +359,31 @@ def _make_combined_data_vector_figure(harm_data_full, config_data_full, # B-modes ax_b = axes[1, col] ax_b.errorbar( - modes - 0.1, cb_cfg / sigma_cfg_B, yerr=np.ones(nmodes), - fmt="o", color=c_cfg, mfc=c_cfg, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, + modes - 0.1, + cb_cfg / sigma_cfg_B, + yerr=np.ones(nmodes), + fmt="o", + color=c_cfg, + mfc=c_cfg, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, ) ax_b.errorbar( - modes + 0.1, cb_h / sigma_h_B, yerr=np.ones(nmodes), - fmt="s", color=c_harm, mfc="white", mew=0.8, ms=4, alpha=0.8, - capsize=2, capthick=0.8, elinewidth=0.8, + modes + 0.1, + cb_h / sigma_h_B, + yerr=np.ones(nmodes), + fmt="s", + color=c_harm, + mfc="white", + mew=0.8, + ms=4, + alpha=0.8, + capsize=2, + capthick=0.8, + elinewidth=0.8, ) ax_b.axhline(0.0, color="black", lw=0.8, alpha=0.6) _shade_reliable(ax_b, reliable_mode_max) @@ -291,7 +391,9 @@ def _make_combined_data_vector_figure(harm_data_full, config_data_full, ax_b.set_xticks(np.arange(1, nmodes + 1)) ax_b.set_xticklabels( [str(i) for i in range(1, nmodes + 1)], - rotation=45, ha="right", rotation_mode="anchor", + rotation=45, + ha="right", + rotation_mode="anchor", ) ax_b.set_xlim(0.5, nmodes + 0.5) ax_b.tick_params(axis="both", width=0.5, length=3) @@ -316,7 +418,9 @@ def _make_combined_data_vector_figure(harm_data_full, config_data_full, return fig -def _make_version_comparison_figure(all_version_data, nmodes, scale_cut, version_labels_map, reliable_mode_max=6): +def _make_version_comparison_figure( + all_version_data, nmodes, scale_cut, version_labels_map, reliable_mode_max=6 +): """Version comparison: all versions, both methods, B-modes in B_n/sigma. Single panel, full angular range only. @@ -338,18 +442,37 @@ def _make_version_comparison_figure(all_version_data, nmodes, scale_cut, version sigma_config_B = _safe_sigma(np.diag(cov_config[nmodes:, nmodes:])) cb_harm = vdata["harm"]["Bn"] - sigma_harm_B = np.where(vdata["harm"]["sigma_B"] > 0, vdata["harm"]["sigma_B"], 1.0) + sigma_harm_B = np.where( + vdata["harm"]["sigma_B"] > 0, vdata["harm"]["sigma_B"], 1.0 + ) ax.errorbar( - modes + offset - 0.03, cb_config / sigma_config_B, yerr=np.ones(nmodes), - fmt="o", color=color, mfc=color, ms=3.5, alpha=0.85, - capsize=1.5, capthick=0.6, elinewidth=0.6, + modes + offset - 0.03, + cb_config / sigma_config_B, + yerr=np.ones(nmodes), + fmt="o", + color=color, + mfc=color, + ms=3.5, + alpha=0.85, + capsize=1.5, + capthick=0.6, + elinewidth=0.6, label=f"{label} (config)", ) ax.errorbar( - modes + offset + 0.03, cb_harm / sigma_harm_B, yerr=np.ones(nmodes), - fmt="s", color=color, mfc="white", ms=3.5, alpha=0.85, - capsize=1.5, capthick=0.6, elinewidth=0.6, mew=0.8, + modes + offset + 0.03, + cb_harm / sigma_harm_B, + yerr=np.ones(nmodes), + fmt="s", + color=color, + mfc="white", + ms=3.5, + alpha=0.85, + capsize=1.5, + capthick=0.6, + elinewidth=0.6, + mew=0.8, label=f"{label} (harmonic)", ) @@ -362,16 +485,22 @@ def _make_version_comparison_figure(all_version_data, nmodes, scale_cut, version ax.tick_params(axis="both", width=0.5, length=3) ax.text( - 0.98, 0.95, + 0.98, + 0.95, rf"$\theta = {scale_cut[0]:.0f}$--${scale_cut[1]:.0f}'$", - transform=ax.transAxes, ha="right", va="top", + transform=ax.transAxes, + ha="right", + va="top", bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8), ) ax.legend( - loc="upper center", ncol=4, - frameon=True, framealpha=0.9, - handletextpad=0.3, columnspacing=0.8, + loc="upper center", + ncol=4, + frameon=True, + framealpha=0.9, + handletextpad=0.3, + columnspacing=0.8, ) plt.tight_layout() @@ -388,15 +517,22 @@ def main(): cosebis_nbins = int(config["cl"].get("cosebis_nbins", 32)) reliable_mode_max = _RELIABLE_MODE_MAX_BY_NBINS.get( - cosebis_nbins, 8 if cosebis_nbins >= 64 else 6, + cosebis_nbins, + 8 if cosebis_nbins >= 64 else 6, ) print(f"Using {cosebis_nbins}-bin pseudo-Cl, reliable modes 1-{reliable_mode_max}") versions_leak_corr = [v for v in config["versions"] if "_leak_corr" in v] # Build input path lookups from snakemake inputs - pseudo_cl_paths = {k: v for k, v in snakemake.input.items() if k.startswith("pseudo_cl_") and not k.startswith("pseudo_cl_cov_")} - pseudo_cov_paths = {k: v for k, v in snakemake.input.items() if k.startswith("pseudo_cl_cov_")} + pseudo_cl_paths = { + k: v + for k, v in snakemake.input.items() + if k.startswith("pseudo_cl_") and not k.startswith("pseudo_cl_cov_") + } + pseudo_cov_paths = { + k: v for k, v in snakemake.input.items() if k.startswith("pseudo_cl_cov_") + } xi_paths = {k: v for k, v in snakemake.input.items() if k.startswith("xi_")} cov_paths = {k: v for k, v in snakemake.input.items() if k.startswith("cov_")} @@ -425,28 +561,42 @@ def main(): xi_key = f"xi_{ver}" cov_key = f"cov_{ver}" - print(f"Processing {ver} ({'corrected' if fig_spec['leak_corrected'] else 'uncorrected'})...") + print( + f"Processing {ver} ({'corrected' if fig_spec['leak_corrected'] else 'uncorrected'})..." + ) # Config-space ce_cfg, cb_cfg, cov_cfg = _compute_config_cosebis( - xi_paths[xi_key], cov_paths[cov_key], nmodes, scale_cut, config, + xi_paths[xi_key], + cov_paths[cov_key], + nmodes, + scale_cut, + config, ) # Harmonic-space ce_h, cb_h, cov_h = _compute_harmonic_cosebis( - pseudo_cl_paths[pseudo_cl_key], pseudo_cov_paths[pseudo_cov_key], - nmodes, scale_cut[0], scale_cut[1], + pseudo_cl_paths[pseudo_cl_key], + pseudo_cov_paths[pseudo_cov_key], + nmodes, + scale_cut[0], + scale_cut[1], ) harm = { - "En": ce_h, "Bn": cb_h, + "En": ce_h, + "Bn": cb_h, "sigma_E": np.sqrt(np.clip(np.diag(cov_h[:nmodes, :nmodes]), 0, None)), "sigma_B": np.sqrt(np.clip(np.diag(cov_h[nmodes:, nmodes:]), 0, None)), } # Create figure fig = _make_data_vector_figure( - harm, (ce_cfg, cb_cfg, cov_cfg), nmodes, scale_cut, - title=fig_spec["title"], reliable_mode_max=reliable_mode_max, + harm, + (ce_cfg, cb_cfg, cov_cfg), + nmodes, + scale_cut, + title=fig_spec["title"], + reliable_mode_max=reliable_mode_max, ) fig_path = output_dir / fig_spec["filename"] fig.savefig(fig_path, dpi=300, bbox_inches="tight") @@ -474,29 +624,52 @@ def main(): # Compute COSEBIS at the other scale cut for this version ce_cfg_other, cb_cfg_other, cov_cfg_other = _compute_config_cosebis( - xi_paths[xi_key], cov_paths[cov_key], nmodes, other_cut, config, + xi_paths[xi_key], + cov_paths[cov_key], + nmodes, + other_cut, + config, ) ce_h_other, cb_h_other, cov_h_other = _compute_harmonic_cosebis( - pseudo_cl_paths[pseudo_cl_key], pseudo_cov_paths[pseudo_cov_key], - nmodes, other_cut[0], other_cut[1], + pseudo_cl_paths[pseudo_cl_key], + pseudo_cov_paths[pseudo_cov_key], + nmodes, + other_cut[0], + other_cut[1], ) harm_other = { - "En": ce_h_other, "Bn": cb_h_other, - "sigma_E": np.sqrt(np.clip(np.diag(cov_h_other[:nmodes, :nmodes]), 0, None)), - "sigma_B": np.sqrt(np.clip(np.diag(cov_h_other[nmodes:, nmodes:]), 0, None)), + "En": ce_h_other, + "Bn": cb_h_other, + "sigma_E": np.sqrt( + np.clip(np.diag(cov_h_other[:nmodes, :nmodes]), 0, None) + ), + "sigma_B": np.sqrt( + np.clip(np.diag(cov_h_other[nmodes:, nmodes:]), 0, None) + ), } # Arrange: full on left, fiducial on right if scale_cut == scale_cut_full: harm_full, cfg_full = harm, (ce_cfg, cb_cfg, cov_cfg) - harm_fid, cfg_fid = harm_other, (ce_cfg_other, cb_cfg_other, cov_cfg_other) + harm_fid, cfg_fid = ( + harm_other, + (ce_cfg_other, cb_cfg_other, cov_cfg_other), + ) else: harm_fid, cfg_fid = harm, (ce_cfg, cb_cfg, cov_cfg) - harm_full, cfg_full = harm_other, (ce_cfg_other, cb_cfg_other, cov_cfg_other) + harm_full, cfg_full = ( + harm_other, + (ce_cfg_other, cb_cfg_other, cov_cfg_other), + ) fig_combined = _make_combined_data_vector_figure( - harm_full, cfg_full, harm_fid, cfg_fid, - nmodes, scale_cut_full, scale_cut_fid, + harm_full, + cfg_full, + harm_fid, + cfg_fid, + nmodes, + scale_cut_full, + scale_cut_fid, title=fig_spec["title"] if fig_spec["title"] else None, reliable_mode_max=reliable_mode_max, ) @@ -511,16 +684,20 @@ def main(): n_rel = reliable_mode_max # Harmonic-space PTE cb_h_rel = cb_h[:n_rel] - cov_h_B_rel = cov_h[nmodes:nmodes + n_rel, nmodes:nmodes + n_rel] + cov_h_B_rel = cov_h[nmodes : nmodes + n_rel, nmodes : nmodes + n_rel] chi2, pte, dof = compute_chi2_pte(cb_h_rel, cov_h_B_rel) harmonic_ptes[ver] = {"chi2": chi2, "pte": pte, "dof": dof} - print(f" Harmonic B-mode PTE (modes 1-{n_rel}): {pte:.4f} (chi2={chi2:.2f}, dof={dof})") + print( + f" Harmonic B-mode PTE (modes 1-{n_rel}): {pte:.4f} (chi2={chi2:.2f}, dof={dof})" + ) # Config-space PTE cb_cfg_rel = cb_cfg[:n_rel] - cov_cfg_B_rel = cov_cfg[nmodes:nmodes + n_rel, nmodes:nmodes + n_rel] + cov_cfg_B_rel = cov_cfg[nmodes : nmodes + n_rel, nmodes : nmodes + n_rel] chi2_c, pte_c, dof_c = compute_chi2_pte(cb_cfg_rel, cov_cfg_B_rel) config_ptes[ver] = {"chi2": chi2_c, "pte": pte_c, "dof": dof_c} - print(f" Config B-mode PTE (modes 1-{n_rel}): {pte_c:.4f} (chi2={chi2_c:.2f}, dof={dof_c})") + print( + f" Config B-mode PTE (modes 1-{n_rel}): {pte_c:.4f} (chi2={chi2_c:.2f}, dof={dof_c})" + ) # Cache leak-corrected results for version comparison figure if fig_spec["leak_corrected"] and ver not in all_version_data: @@ -530,7 +707,10 @@ def main(): } fig_vc = _make_version_comparison_figure( - all_version_data, nmodes, scale_cut, version_labels_map, + all_version_data, + nmodes, + scale_cut, + version_labels_map, reliable_mode_max=reliable_mode_max, ) fig_vc_path = Path(snakemake.output["figure_versions"]) diff --git a/papers/bmodes/scripts/harmonic_space_pte_matrices.py b/papers/bmodes/scripts/harmonic_space_pte_matrices.py index f87ba682..6232fdc0 100644 --- a/papers/bmodes/scripts/harmonic_space_pte_matrices.py +++ b/papers/bmodes/scripts/harmonic_space_pte_matrices.py @@ -8,17 +8,15 @@ """ import json -import shutil import sys from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt -from matplotlib.patches import Rectangle -from mpl_toolkits.axes_grid1 import make_axes_locatable import numpy as np from astropy.io import fits - +from matplotlib.patches import Rectangle +from mpl_toolkits.axes_grid1 import make_axes_locatable from plotting_utils import ( FIG_WIDTH_SINGLE, PAPER_MPLSTYLE, @@ -29,7 +27,6 @@ make_pte_norm, ) - plt.style.use(PAPER_MPLSTYLE) @@ -49,7 +46,9 @@ def _load_snakemake(): snakemake = _load_snakemake() -def compute_pte_matrix(pseudo_cl_path, pseudo_cl_cov_path, fiducial_ell_min=None, fiducial_ell_max=None): +def compute_pte_matrix( + pseudo_cl_path, pseudo_cl_cov_path, fiducial_ell_min=None, fiducial_ell_max=None +): """Compute PTE matrix for all ell-bin cut combinations. Parameters @@ -126,9 +125,17 @@ def compute_pte_matrix(pseudo_cl_path, pseudo_cl_cov_path, fiducial_ell_min=None return pte_matrix, ell, stats_out -def plot_cl_pte_panel(ax, pte_matrix, ell, title, show_colorbar=False, - show_xlabel=True, show_ylabel=True, - fid_i_min=None, fid_i_max=None): +def plot_cl_pte_panel( + ax, + pte_matrix, + ell, + title, + show_colorbar=False, + show_xlabel=True, + show_ylabel=True, + fid_i_min=None, + fid_i_max=None, +): """Plot a single Cl PTE heatmap panel. Parameters @@ -163,14 +170,25 @@ def plot_cl_pte_panel(ax, pte_matrix, ell, title, show_colorbar=False, pte_plot = pte_matrix.T im = ax.imshow( - pte_plot, origin="lower", aspect="equal", - cmap=pte_cmap, norm=pte_norm, extent=[0, n_ell, 0, n_ell], + pte_plot, + origin="lower", + aspect="equal", + cmap=pte_cmap, + norm=pte_norm, + extent=[0, n_ell, 0, n_ell], ) # Mark fiducial ell cut (black square) if fid_i_min is not None and fid_i_max is not None: ax.add_patch( - Rectangle((fid_i_min, fid_i_max), 1, 1, fill=False, edgecolor="black", linewidth=1.5) + Rectangle( + (fid_i_min, fid_i_max), + 1, + 1, + fill=False, + edgecolor="black", + linewidth=1.5, + ) ) # Tick positioning: x-axis at left edge of bins, y-axis at top edge @@ -217,8 +235,15 @@ def create_single_panel(pte_matrix, ell, fid_i_min=None, fid_i_max=None): """Create single-panel figure for fiducial version.""" fig, ax = plt.subplots(1, 1, figsize=(FIG_WIDTH_SINGLE, FIG_WIDTH_SINGLE)) - plot_cl_pte_panel(ax, pte_matrix, ell, "", show_colorbar=True, - fid_i_min=fid_i_min, fid_i_max=fid_i_max) + plot_cl_pte_panel( + ax, + pte_matrix, + ell, + "", + show_colorbar=True, + fid_i_min=fid_i_min, + fid_i_max=fid_i_max, + ) ax.set_xlabel(r"$\ell_{\mathrm{min}}$") ax.set_ylabel(r"$\ell_{\mathrm{max}}$") @@ -260,24 +285,37 @@ def create_npanel_composite(matrices, ells, panel_labels, fid_indices=None): fig = plt.figure(figsize=(fig_width, fig_height)) width_ratios = [1] * n_panels + [cbar_ratio] gs = fig.add_gridspec( - 1, n_panels + 1, + 1, + n_panels + 1, width_ratios=width_ratios, wspace=wspace_val, - left=gs_left, right=gs_right, - bottom=gs_bottom, top=gs_top, + left=gs_left, + right=gs_right, + bottom=gs_bottom, + top=gs_top, ) axes = [fig.add_subplot(gs[i]) for i in range(n_panels)] cax_placeholder = fig.add_subplot(gs[n_panels]) # position reference - for i, (ax, matrix, ell, label) in enumerate(zip(axes, matrices, ells, panel_labels)): + for i, (ax, matrix, ell, label) in enumerate( + zip(axes, matrices, ells, panel_labels) + ): fi = fid_indices[i] if fid_indices else (None, None) - im = plot_cl_pte_panel(ax, matrix, ell, "", show_ylabel=(i == 0), - fid_i_min=fi[0], fid_i_max=fi[1]) + im = plot_cl_pte_panel( + ax, matrix, ell, "", show_ylabel=(i == 0), fid_i_min=fi[0], fid_i_max=fi[1] + ) # Version label inside panel (bottom-right) - ax.text(0.95, 0.05, label, transform=ax.transAxes, - ha="right", va="bottom", fontsize=8, - bbox=dict(boxstyle="round,pad=0.2", facecolor="white", alpha=0.8)) + ax.text( + 0.95, + 0.05, + label, + transform=ax.transAxes, + ha="right", + va="bottom", + fontsize=8, + bbox=dict(boxstyle="round,pad=0.2", facecolor="white", alpha=0.8), + ) # Draw first to get actual panel positions after aspect="equal" constraint fig.canvas.draw() @@ -296,10 +334,21 @@ def create_npanel_composite(matrices, ells, panel_labels, fid_indices=None): cbar.ax.tick_params(labelsize=9) # Common axis labels - fig.text(0.5 * (gs_left + gs_right), 0.4 * gs_bottom, - r"$\ell_{\mathrm{min}}$", ha="center", va="center") - fig.text(0.25 * gs_left, 0.5 * (gs_bottom + gs_top), - r"$\ell_{\mathrm{max}}$", va="center", ha="center", rotation="vertical") + fig.text( + 0.5 * (gs_left + gs_right), + 0.4 * gs_bottom, + r"$\ell_{\mathrm{min}}$", + ha="center", + va="center", + ) + fig.text( + 0.25 * gs_left, + 0.5 * (gs_bottom + gs_top), + r"$\ell_{\mathrm{max}}$", + va="center", + ha="center", + rotation="vertical", + ) return fig @@ -383,7 +432,9 @@ def main(): # Compute fiducial ell indices per version using bin edges version_fid_indices = {} for version, ell in all_ells.items(): - version_fid_indices[version] = ell_bin_index(ell, fiducial_ell_min, fiducial_ell_max) + version_fid_indices[version] = ell_bin_index( + ell, fiducial_ell_min, fiducial_ell_max + ) # Create fiducial single-panel figure if fiducial_version in all_matrices: @@ -406,14 +457,20 @@ def main(): plt.close(fig_fiducial) # Create appendix N-panel composite (paper versions only, no ecut variants) - appendix_versions = [v for v in versions if v in all_matrices and v in version_labels] + appendix_versions = [ + v for v in versions if v in all_matrices and v in version_labels + ] if len(appendix_versions) >= 2: matrices = [all_matrices[v] for v in appendix_versions] ells = [all_ells[v] for v in appendix_versions] labels = [version_labels.get(v, v) for v in appendix_versions] - fid_indices = [version_fid_indices.get(v, (None, None)) for v in appendix_versions] - fig_appendix = create_npanel_composite(matrices, ells, labels, fid_indices=fid_indices) + fid_indices = [ + version_fid_indices.get(v, (None, None)) for v in appendix_versions + ] + fig_appendix = create_npanel_composite( + matrices, ells, labels, fid_indices=fid_indices + ) fig_path = Path(snakemake.output["figure_appendix"]) fig_appendix.savefig(fig_path, dpi=300, bbox_inches="tight", facecolor="white") diff --git a/papers/bmodes/scripts/plot_cosebis_filter_overlay.py b/papers/bmodes/scripts/plot_cosebis_filter_overlay.py index f1fb58ac..8de785b3 100644 --- a/papers/bmodes/scripts/plot_cosebis_filter_overlay.py +++ b/papers/bmodes/scripts/plot_cosebis_filter_overlay.py @@ -14,9 +14,9 @@ from datetime import datetime from pathlib import Path -import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as ticker +import numpy as np import seaborn as sns from astropy.io import fits from cosmo_numba.B_modes.cosebis import COSEBIS @@ -51,7 +51,10 @@ def make_figure(ell_32, bb, sigma_bb, ell_dense, Wn_full, Wn_fid, output_path): colors = sns.color_palette("husl", nmodes) fig, axes = plt.subplots( - 2, 1, figsize=(8.5, 7.5), sharex=True, + 2, + 1, + figsize=(8.5, 7.5), + sharex=True, gridspec_kw={"hspace": 0.12}, ) @@ -64,16 +67,26 @@ def make_figure(ell_32, bb, sigma_bb, ell_dense, Wn_full, Wn_fid, output_path): Dl_bb = ell_32 * (ell_32 + 1) * bb / (2 * np.pi) Dl_sig = ell_32 * (ell_32 + 1) * sigma_bb / (2 * np.pi) ax_bb.fill_between( - ell_32, Dl_bb - Dl_sig, Dl_bb + Dl_sig, - color="0.85", zorder=0, label=r"$C_\ell^{BB} \pm 1\sigma$", + ell_32, + Dl_bb - Dl_sig, + Dl_bb + Dl_sig, + color="0.85", + zorder=0, + label=r"$C_\ell^{BB} \pm 1\sigma$", ) ax_bb.scatter( - ell_32, Dl_bb, - s=12, color="0.55", zorder=1, marker="s", + ell_32, + Dl_bb, + s=12, + color="0.55", + zorder=1, + marker="s", ) ax_bb.set_ylabel( r"$\ell(\ell{+}1)\,C_\ell^{BB}/2\pi$", - fontsize=8, color="0.5", labelpad=8, + fontsize=8, + color="0.5", + labelpad=8, ) ax_bb.tick_params(axis="y", labelcolor="0.5", labelsize=7) @@ -82,9 +95,12 @@ def make_figure(ell_32, bb, sigma_bb, ell_dense, Wn_full, Wn_fid, output_path): peak = np.max(np.abs(Wn[n])) if peak > 0: ax.plot( - ell_dense, Wn[n] / peak, - color=colors[n], lw=1.4, alpha=0.9, - label=rf"$n = {n+1}$", + ell_dense, + Wn[n] / peak, + color=colors[n], + lw=1.4, + alpha=0.9, + label=rf"$n = {n + 1}$", ) ax.axhline(0, color="k", lw=0.4, zorder=0) @@ -94,25 +110,39 @@ def make_figure(ell_32, bb, sigma_bb, ell_dense, Wn_full, Wn_fid, output_path): # Bin-centre tick marks along the top for e in ell_32: ax.plot( - [e, e], [1.1, 1.2], color="0.35", lw=0.6, - clip_on=False, zorder=5, transform=ax.get_xaxis_transform(), + [e, e], + [1.1, 1.2], + color="0.35", + lw=0.6, + clip_on=False, + zorder=5, + transform=ax.get_xaxis_transform(), ) # Scale-cut annotation ax.text( - 0.02, 0.95, cut_label, - transform=ax.transAxes, fontsize=9, - va="top", ha="left", + 0.02, + 0.95, + cut_label, + transform=ax.transAxes, + fontsize=9, + va="top", + ha="left", bbox=dict(boxstyle="round,pad=0.3", fc="white", ec="0.7", alpha=0.9), ) # Shared legend for filter modes handles, labels = axes[0].get_legend_handles_labels() axes[0].legend( - handles, labels, - fontsize=7.5, ncol=nmodes, loc="upper right", - title=r"COSEBIS mode $n$", title_fontsize=8, - columnspacing=1.0, handlelength=1.5, + handles, + labels, + fontsize=7.5, + ncol=nmodes, + loc="upper right", + title=r"COSEBIS mode $n$", + title_fontsize=8, + columnspacing=1.0, + handlelength=1.5, ) axes[1].set_xlabel(r"$\ell$", fontsize=10) diff --git a/papers/bmodes/scripts/plot_presentation_blind_nz.py b/papers/bmodes/scripts/plot_presentation_blind_nz.py index da15a2e3..c71fbd4f 100644 --- a/papers/bmodes/scripts/plot_presentation_blind_nz.py +++ b/papers/bmodes/scripts/plot_presentation_blind_nz.py @@ -5,7 +5,6 @@ import matplotlib.pyplot as plt import numpy as np import seaborn as sns - from plotting_utils import PAPER_MPLSTYLE diff --git a/papers/bmodes/scripts/plot_pure_eb_covariance.py b/papers/bmodes/scripts/plot_pure_eb_covariance.py index 54047f0c..3fdba8db 100644 --- a/papers/bmodes/scripts/plot_pure_eb_covariance.py +++ b/papers/bmodes/scripts/plot_pure_eb_covariance.py @@ -9,7 +9,6 @@ import numpy as np import seaborn as sns from mpl_toolkits.axes_grid1 import make_axes_locatable - from plotting_utils import PAPER_MPLSTYLE # Unbuffered output for Snakemake log streaming @@ -61,8 +60,12 @@ def main(): # Configure tick labels for E/B modes tick_positions = np.arange(nbins / 2, nbins * 6, nbins) tick_labels = [ - r"$\xi_+^{\mathrm{E}}$", r"$\xi_-^{\mathrm{E}}$", r"$\xi_+^{\mathrm{B}}$", - r"$\xi_-^{\mathrm{B}}$", r"$\xi_+^{\mathrm{amb}}$", r"$\xi_-^{\mathrm{amb}}$" + r"$\xi_+^{\mathrm{E}}$", + r"$\xi_-^{\mathrm{E}}$", + r"$\xi_+^{\mathrm{B}}$", + r"$\xi_-^{\mathrm{B}}$", + r"$\xi_+^{\mathrm{amb}}$", + r"$\xi_-^{\mathrm{amb}}$", ] for ticks in (plt.xticks, plt.yticks): diff --git a/papers/bmodes/scripts/precompute_pure_eb_chunk.py b/papers/bmodes/scripts/precompute_pure_eb_chunk.py index fe242143..f25158aa 100644 --- a/papers/bmodes/scripts/precompute_pure_eb_chunk.py +++ b/papers/bmodes/scripts/precompute_pure_eb_chunk.py @@ -7,9 +7,8 @@ import numpy as np import pyccl as ccl import tqdm -from scipy import sparse - from IPython import get_ipython +from scipy import sparse ipython = get_ipython() @@ -70,6 +69,7 @@ def _load_xi(path, min_sep, max_sep, nbins): def main(): from cosmo_numba.B_modes.schneider2022 import get_pure_EB_modes + from sp_validation.cosmo_val import CosmologyValidation from sp_validation.cosmology import get_theo_xi @@ -80,10 +80,14 @@ def main(): # Compute this chunk's sample range samples_per_chunk = n_samples_total // n_chunks start_idx = chunk_id * samples_per_chunk - end_idx = start_idx + samples_per_chunk if chunk_id < n_chunks - 1 else n_samples_total + end_idx = ( + start_idx + samples_per_chunk if chunk_id < n_chunks - 1 else n_samples_total + ) n_samples_chunk = end_idx - start_idx - print(f"Chunk {chunk_id}/{n_chunks}: samples {start_idx}-{end_idx} ({n_samples_chunk} samples)") + print( + f"Chunk {chunk_id}/{n_chunks}: samples {start_idx}-{end_idx} ({n_samples_chunk} samples)" + ) numeric_params = { "min_sep": float(params["min_sep"]), @@ -137,16 +141,21 @@ def main(): binning_matrix = sparse.csr_matrix( (np.ones(len(row_indices)), (row_indices, col_indices)), - shape=(len(gg["meanr"]), nbins_int) + shape=(len(gg["meanr"]), nbins_int), ) row_sums = np.array(binning_matrix.sum(axis=1)).flatten() - binning_matrix = sparse.diags(1/row_sums) @ binning_matrix + binning_matrix = sparse.diags(1 / row_sums) @ binning_matrix # Get theoretical mean (same as b_modes.py) - mean_int = np.concatenate(get_theo_xi( - theta=theta_int, z=z_dist[:, 0], nz=z_dist[:, 1], - backend="ccl", cosmo=cosmo_cov - )) + mean_int = np.concatenate( + get_theo_xi( + theta=theta_int, + z=z_dist[:, 0], + nz=z_dist[:, 1], + backend="ccl", + cosmo=cosmo_cov, + ) + ) # Set seed based on chunk_id for reproducibility rng = np.random.default_rng(seed=42 + chunk_id) @@ -163,12 +172,18 @@ def main(): max_sep = numeric_params["max_sep"] transformed_samples = [ - np.concatenate(get_pure_EB_modes( - theta=gg["meanr"], theta_int=gg_int["meanr"], - xip=samples_rep_xip[i], xim=samples_rep_xim[i], - xip_int=samples_int_xip[i], xim_int=samples_int_xim[i], - tmin=min_sep, tmax=max_sep - )) + np.concatenate( + get_pure_EB_modes( + theta=gg["meanr"], + theta_int=gg_int["meanr"], + xip=samples_rep_xip[i], + xim=samples_rep_xim[i], + xip_int=samples_int_xip[i], + xim_int=samples_int_xim[i], + tmin=min_sep, + tmax=max_sep, + ) + ) for i in tqdm.tqdm(range(n_samples_chunk), desc=f"Chunk {chunk_id}") ] diff --git a/papers/bmodes/scripts/pure_eb_covariance.py b/papers/bmodes/scripts/pure_eb_covariance.py index 91f66b4e..fb1c4ac1 100644 --- a/papers/bmodes/scripts/pure_eb_covariance.py +++ b/papers/bmodes/scripts/pure_eb_covariance.py @@ -15,11 +15,8 @@ import matplotlib.pyplot as plt import numpy as np -import seaborn as sns # Registers seaborn colormaps (icefire, etc.) with matplotlib - from plotting_utils import FIG_WIDTH_SINGLE, PAPER_MPLSTYLE - plt.style.use(PAPER_MPLSTYLE) @@ -48,7 +45,7 @@ def _analyze_block(covariance, start_idx, end_idx, name): min_eig = float(eigenvalues[0]) max_eig = float(eigenvalues[-1]) is_positive_definite = bool(min_eig > 0) - condition_number = max_eig / min_eig if min_eig > 0 else float('inf') + condition_number = max_eig / min_eig if min_eig > 0 else float("inf") return { "condition_number": condition_number, @@ -93,25 +90,27 @@ def main(): # Matrix is 6*nbins x 6*nbins: # Block structure: [E+, E-, B+, B-, amb+, amb-] # Each block is nbins x nbins - assert cov_pure_eb.shape == (6*nbins, 6*nbins), f"Expected ({6*nbins}, {6*nbins}), got {cov_pure_eb.shape}" + assert cov_pure_eb.shape == (6 * nbins, 6 * nbins), ( + f"Expected ({6 * nbins}, {6 * nbins}), got {cov_pure_eb.shape}" + ) # Analyze full matrix eigenvalues = np.linalg.eigvalsh(cov_pure_eb) min_eig = float(eigenvalues[0]) max_eig = float(eigenvalues[-1]) is_positive_definite = bool(min_eig > 0) - condition_number = max_eig / min_eig if min_eig > 0 else float('inf') + condition_number = max_eig / min_eig if min_eig > 0 else float("inf") # Analyze blocks block_analysis = { "xi_E": _analyze_block( - cov_pure_eb, 0, 2*nbins, "xi_+^E and xi_-^E combined (40x40)" + cov_pure_eb, 0, 2 * nbins, "xi_+^E and xi_-^E combined (40x40)" ), "xi_B": _analyze_block( - cov_pure_eb, 2*nbins, 4*nbins, "xi_+^B and xi_-^B combined (40x40)" + cov_pure_eb, 2 * nbins, 4 * nbins, "xi_+^B and xi_-^B combined (40x40)" ), "xi_amb": _analyze_block( - cov_pure_eb, 4*nbins, 6*nbins, "xi_+^amb and xi_-^amb combined (40x40)" + cov_pure_eb, 4 * nbins, 6 * nbins, "xi_+^amb and xi_-^amb combined (40x40)" ), } @@ -143,7 +142,10 @@ def main(): # Compute block boundaries and centers block_sizes = [nbins] * 6 boundaries = np.cumsum(block_sizes) - centers = [0.5 * (start + end) - 0.5 for start, end in zip([0] + list(boundaries[:-1]), boundaries)] + centers = [ + 0.5 * (start + end) - 0.5 + for start, end in zip([0] + list(boundaries[:-1]), boundaries) + ] # Set ticks at block centers with labels (no axis labels, tick labels only) ax.set_xticks(centers) @@ -205,11 +207,11 @@ def main(): print(f"Saved evidence to {evidence_path}") # Print summary - print(f"\nCovariance Matrix Summary:") + print("\nCovariance Matrix Summary:") print(f" Shape: {cov_pure_eb.shape}") print(f" Full matrix condition number: {condition_number:.2e}") print(f" Positive definite: {is_positive_definite}") - print(f"\nBlock Analysis:") + print("\nBlock Analysis:") for block_name, stats in block_analysis.items(): print(f" {block_name}:") print(f" Condition number: {stats['condition_number']:.2e}") diff --git a/papers/bmodes/scripts/pure_eb_data_vector.py b/papers/bmodes/scripts/pure_eb_data_vector.py index a30b03da..a8a20edf 100644 --- a/papers/bmodes/scripts/pure_eb_data_vector.py +++ b/papers/bmodes/scripts/pure_eb_data_vector.py @@ -12,13 +12,11 @@ """ import json -import shutil from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt import numpy as np - from plotting_utils import ( FIG_WIDTH_FULL, PAPER_MPLSTYLE, @@ -26,7 +24,6 @@ iter_version_figures, ) - plt.style.use(PAPER_MPLSTYLE) @@ -74,7 +71,9 @@ def _load_pure_eb_data(pure_eb_path, cov_path): return data -def _create_pure_eb_figure(data, fiducial_xip_scale_cut, fiducial_xim_scale_cut, title_suffix=""): +def _create_pure_eb_figure( + data, fiducial_xip_scale_cut, fiducial_xim_scale_cut, title_suffix="" +): """Create pure E/B decomposition figure. Args: @@ -116,28 +115,61 @@ def setup_panel(ax, ylabel_text=None): ylabel_text and ax.set_ylabel(ylabel_text) # Create 1x2 figure - fig, axes = plt.subplots(1, 2, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.36), sharey=True) + fig, axes = plt.subplots( + 1, 2, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.36), sharey=True + ) # Left panel: xi+ decomposition ax = axes[0] plot_data = [ - (data["xip_total"], sigma_xip_total, "o", color_total, alpha_main, r"$\xi_\pm$ (total)"), + ( + data["xip_total"], + sigma_xip_total, + "o", + color_total, + alpha_main, + r"$\xi_\pm$ (total)", + ), (data["xip_E"], sigma_xip_E, "s", color_E, alpha_faint, r"$\xi_\pm^E$"), (data["xip_B"], sigma_xip_B, "X", color_B, alpha_main, r"$\xi_\pm^B$"), - (data["xip_amb"], sigma_xip_amb, "v", color_amb, alpha_faint, r"$\xi_\pm^\mathrm{amb}$"), + ( + data["xip_amb"], + sigma_xip_amb, + "v", + color_amb, + alpha_faint, + r"$\xi_\pm^\mathrm{amb}$", + ), ] for i, (d, sigma, marker, color, alpha, label) in enumerate(plot_data): ax.errorbar( - theta * offsets[i], theta * d / scale_factor, yerr=theta * sigma / scale_factor, - fmt=marker, color=color, markersize=ms, capsize=capsize, capthick=capthick, - elinewidth=elinewidth, alpha=alpha, label=label + theta * offsets[i], + theta * d / scale_factor, + yerr=theta * sigma / scale_factor, + fmt=marker, + color=color, + markersize=ms, + capsize=capsize, + capthick=capthick, + elinewidth=elinewidth, + alpha=alpha, + label=label, ) shade_excluded_regions(ax, fiducial_xip_scale_cut) setup_panel(ax, ylabel_text=r"$\theta \xi \times 10^4$") panel_label = rf"$\xi_+${title_suffix}" if title_suffix else r"$\xi_+$" - ax.text(0.05, 0.95, panel_label, transform=ax.transAxes, - ha="left", va="top", fontsize=10, - bbox=dict(boxstyle="round,pad=0.2", facecolor="white", alpha=0.8, edgecolor="none")) + ax.text( + 0.05, + 0.95, + panel_label, + transform=ax.transAxes, + ha="left", + va="top", + fontsize=10, + bbox=dict( + boxstyle="round,pad=0.2", facecolor="white", alpha=0.8, edgecolor="none" + ), + ) # Right panel: xi- decomposition ax = axes[1] @@ -149,16 +181,32 @@ def setup_panel(ax, ylabel_text=None): ] for i, (d, sigma, marker, color, alpha) in enumerate(xim_plot_data): ax.errorbar( - theta * offsets[i], theta * d / scale_factor, yerr=theta * sigma / scale_factor, - fmt=marker, color=color, markersize=ms, capsize=capsize, capthick=capthick, - elinewidth=elinewidth, alpha=alpha + theta * offsets[i], + theta * d / scale_factor, + yerr=theta * sigma / scale_factor, + fmt=marker, + color=color, + markersize=ms, + capsize=capsize, + capthick=capthick, + elinewidth=elinewidth, + alpha=alpha, ) shade_excluded_regions(ax, fiducial_xim_scale_cut) setup_panel(ax) panel_label = rf"$\xi_-${title_suffix}" if title_suffix else r"$\xi_-$" - ax.text(0.05, 0.95, panel_label, transform=ax.transAxes, - ha="left", va="top", fontsize=10, - bbox=dict(boxstyle="round,pad=0.2", facecolor="white", alpha=0.8, edgecolor="none")) + ax.text( + 0.05, + 0.95, + panel_label, + transform=ax.transAxes, + ha="left", + va="top", + fontsize=10, + bbox=dict( + boxstyle="round,pad=0.2", facecolor="white", alpha=0.8, edgecolor="none" + ), + ) axes[0].set_ylim(ylim) fig.tight_layout() handles, labels = axes[0].get_legend_handles_labels() @@ -175,7 +223,9 @@ def main(): fiducial_xim_scale_cut = tuple(config["fiducial"]["fiducial_xim_scale_cut"]) # Build input path lookup from snakemake inputs - pure_eb_paths = {k: v for k, v in snakemake.input.items() if k.startswith("pure_eb_")} + pure_eb_paths = { + k: v for k, v in snakemake.input.items() if k.startswith("pure_eb_") + } cov_paths = {k: v for k, v in snakemake.input.items() if k.startswith("cov_")} # Create output directory @@ -201,8 +251,10 @@ def main(): # Create figure with appropriate title title_suffix = f" ({fig_spec['title']})" if fig_spec["title"] else "" fig = _create_pure_eb_figure( - data, fiducial_xip_scale_cut, fiducial_xim_scale_cut, - title_suffix=title_suffix + data, + fiducial_xip_scale_cut, + fiducial_xim_scale_cut, + title_suffix=title_suffix, ) # Save figure @@ -223,8 +275,7 @@ def main(): # Compute PTEs for evidence (fiducial version, leak-corrected only) data = _load_pure_eb_data( - pure_eb_paths[f"pure_eb_{version}"], - cov_paths[f"cov_{version}"] + pure_eb_paths[f"pure_eb_{version}"], cov_paths[f"cov_{version}"] ) theta = data["theta"] nbins = data["nbins"] @@ -240,8 +291,12 @@ def main(): cov_cross_full = cov_pure_eb[2 * nbins : 3 * nbins, 3 * nbins : 4 * nbins] # Scale cut masks - mask_xip = (theta >= fiducial_xip_scale_cut[0]) & (theta <= fiducial_xip_scale_cut[1]) - mask_xim = (theta >= fiducial_xim_scale_cut[0]) & (theta <= fiducial_xim_scale_cut[1]) + mask_xip = (theta >= fiducial_xip_scale_cut[0]) & ( + theta <= fiducial_xip_scale_cut[1] + ) + mask_xim = (theta >= fiducial_xim_scale_cut[0]) & ( + theta <= fiducial_xim_scale_cut[1] + ) # Apply scale cuts to covariances cov_xip_B_cut = cov_xip_B_full[np.ix_(mask_xip, mask_xip)] @@ -249,20 +304,36 @@ def main(): cov_cross_cut = cov_cross_full[np.ix_(mask_xip, mask_xim)] # Compute PTEs at fiducial scale cuts - chi2_xip_fid, pte_xip_fid, dof_xip_fid = compute_chi2_pte(xip_B[mask_xip], cov_xip_B_cut, n_samples=n_samples) - chi2_xim_fid, pte_xim_fid, dof_xim_fid = compute_chi2_pte(xim_B[mask_xim], cov_xim_B_cut, n_samples=n_samples) + chi2_xip_fid, pte_xip_fid, dof_xip_fid = compute_chi2_pte( + xip_B[mask_xip], cov_xip_B_cut, n_samples=n_samples + ) + chi2_xim_fid, pte_xim_fid, dof_xim_fid = compute_chi2_pte( + xim_B[mask_xim], cov_xim_B_cut, n_samples=n_samples + ) pte_joint_fid, chi2_joint_fid, dof_joint_fid = _compute_joint_pte( - xip_B[mask_xip], xim_B[mask_xim], cov_xip_B_cut, cov_xim_B_cut, cov_cross_cut, n_samples=n_samples + xip_B[mask_xip], + xim_B[mask_xim], + cov_xip_B_cut, + cov_xim_B_cut, + cov_cross_cut, + n_samples=n_samples, ) # Compute PTEs at full range _, pte_xip_full, _ = compute_chi2_pte(xip_B, cov_xip_B_full, n_samples=n_samples) _, pte_xim_full, _ = compute_chi2_pte(xim_B, cov_xim_B_full, n_samples=n_samples) pte_joint_full, chi2_joint_full, dof_joint_full = _compute_joint_pte( - xip_B, xim_B, cov_xip_B_full, cov_xim_B_full, cov_cross_full, n_samples=n_samples + xip_B, + xim_B, + cov_xip_B_full, + cov_xim_B_full, + cov_cross_full, + n_samples=n_samples, ) - print(f"Blind {blind} PTEs (fiducial): xi+^B={pte_xip_fid:.3f}, xi-^B={pte_xim_fid:.3f}, joint={pte_joint_fid:.3f}") + print( + f"Blind {blind} PTEs (fiducial): xi+^B={pte_xip_fid:.3f}, xi-^B={pte_xim_fid:.3f}, joint={pte_joint_fid:.3f}" + ) # Write evidence.json (based on leak-corrected fiducial data only) evidence_data = { diff --git a/papers/bmodes/scripts/pure_eb_version_comparison.py b/papers/bmodes/scripts/pure_eb_version_comparison.py index 2b85bba1..a8d3daef 100644 --- a/papers/bmodes/scripts/pure_eb_version_comparison.py +++ b/papers/bmodes/scripts/pure_eb_version_comparison.py @@ -7,28 +7,23 @@ """ import json -import shutil from datetime import datetime from pathlib import Path import matplotlib.pyplot as plt import numpy as np import seaborn as sns - from plotting_utils import ( - ERRORBAR_DEFAULTS, FIG_WIDTH_FULL, MARKER_STYLES, PAPER_MPLSTYLE, compute_chi2_pte, draw_normalized_boxes_log_scale, find_fiducial_index, - get_box_style, get_version_alpha, version_label, ) - plt.style.use(PAPER_MPLSTYLE) @@ -39,9 +34,9 @@ def _extract_sigma(covariance, block_index, block_size): return np.sqrt(np.clip(np.diag(block), 0, None)) - -def _create_version_comparison_figure(datasets, scale_cuts, fiducial_version, - x_offset_factors=None, box_style=None): +def _create_version_comparison_figure( + datasets, scale_cuts, fiducial_version, x_offset_factors=None, box_style=None +): """Create figure comparing total and B-mode correlations across versions. Layout: @@ -68,7 +63,8 @@ def _create_version_comparison_figure(datasets, scale_cuts, fiducial_version, # Create figure with custom height ratios: B row is 2/3 of top row # Both rows share y-axis across columns fig, axes = plt.subplots( - 2, 2, + 2, + 2, figsize=(FIG_WIDTH_FULL, FIG_WIDTH_FULL * 0.55), sharex=True, gridspec_kw={"height_ratios": [3, 2]}, @@ -99,15 +95,19 @@ def _create_version_comparison_figure(datasets, scale_cuts, fiducial_version, for key in ("xip_total", "xim_total"): data[f"{key}_scaled"] = (theta * data[key]) / scale_factor - for col, (mode_key, title) in enumerate([ - ("xip_total", r"$\xi_+$"), - ("xim_total", r"$\xi_-$"), - ]): + for col, (mode_key, title) in enumerate( + [ + ("xip_total", r"$\xi_+$"), + ("xim_total", r"$\xi_-$"), + ] + ): ax = axes[0, col] # Draw version spread boxes (before data points so they're behind) draw_normalized_boxes_log_scale( - ax, theta, datasets, + ax, + theta, + datasets, y_norm_key=f"{mode_key}_scaled", fiducial_idx=fiducial_idx, x_offset_range=x_offset_range, @@ -117,7 +117,9 @@ def _create_version_comparison_figure(datasets, scale_cuts, fiducial_version, for i, data in enumerate(datasets): theta_i = data["theta"] offset = x_offset_factors[i] if i < len(x_offset_factors) else 1.0 - marker = data.get("marker", MARKER_STYLES[i] if i < len(MARKER_STYLES) else "o") + marker = data.get( + "marker", MARKER_STYLES[i] if i < len(MARKER_STYLES) else "o" + ) fillstyle = data.get("fillstyle", "full") mfc = data["color"] if fillstyle == "full" else "none" @@ -155,15 +157,19 @@ def _create_version_comparison_figure(datasets, scale_cuts, fiducial_version, ax.set_ylabel(r"$\theta \xi \times 10^4$") # Bottom row: xi_B +/- normalized - for col, (mode_key, title) in enumerate([ - ("xip_B", r"$\xi_+^{\mathrm{B}} / \sigma$"), - ("xim_B", r"$\xi_-^{\mathrm{B}} / \sigma$"), - ]): + for col, (mode_key, title) in enumerate( + [ + ("xip_B", r"$\xi_+^{\mathrm{B}} / \sigma$"), + ("xim_B", r"$\xi_-^{\mathrm{B}} / \sigma$"), + ] + ): ax = axes[1, col] # Draw version spread boxes (before data points) draw_normalized_boxes_log_scale( - ax, theta, datasets, + ax, + theta, + datasets, y_norm_key=f"{mode_key}_normalized", fiducial_idx=fiducial_idx, x_offset_range=x_offset_range, @@ -173,7 +179,9 @@ def _create_version_comparison_figure(datasets, scale_cuts, fiducial_version, for i, data in enumerate(datasets): theta_i = data["theta"] offset = x_offset_factors[i] if i < len(x_offset_factors) else 1.0 - marker = data.get("marker", MARKER_STYLES[i] if i < len(MARKER_STYLES) else "o") + marker = data.get( + "marker", MARKER_STYLES[i] if i < len(MARKER_STYLES) else "o" + ) fillstyle = data.get("fillstyle", "full") mfc = data["color"] if fillstyle == "full" else "none" @@ -245,7 +253,8 @@ def main(): # Which version gets the fiducial reference line in boxes fiducial_for_comparison = getattr( - snakemake.params, "fiducial_for_comparison", + snakemake.params, + "fiducial_for_comparison", plotting_config.get("fiducial_for_comparison", fiducial_version), ) @@ -282,10 +291,12 @@ def main(): # Load data for all versions datasets = [] - for i, (version, data_path) in enumerate(zip( - versions, - snakemake.input["pure_eb_data"], - )): + for i, (version, data_path) in enumerate( + zip( + versions, + snakemake.input["pure_eb_data"], + ) + ): if has_ecut: parent = version.split("_ecut")[0].replace("_leak_corr", "") color = parent_color_map[parent] @@ -317,8 +328,8 @@ def main(): sigma_xim_total = _extract_sigma(cov_pure_eb, 1, nbins) # Extract B-mode covariance blocks - block_xip_B = cov_pure_eb[2*nbins:3*nbins, 2*nbins:3*nbins] - block_xim_B = cov_pure_eb[3*nbins:4*nbins, 3*nbins:4*nbins] + block_xip_B = cov_pure_eb[2 * nbins : 3 * nbins, 2 * nbins : 3 * nbins] + block_xim_B = cov_pure_eb[3 * nbins : 4 * nbins, 3 * nbins : 4 * nbins] sigma_xip_B = np.sqrt(np.clip(np.diag(block_xip_B), 0, None)) sigma_xim_B = np.sqrt(np.clip(np.diag(block_xim_B), 0, None)) @@ -327,37 +338,55 @@ def main(): chi2_xim_B, pte_xim_B, dof_xim_B = compute_chi2_pte(xim_B, block_xim_B) # Scale-cut PTEs (fiducial xip and xim cuts) - xip_cut_mask = (theta >= fiducial_xip_scale_cut[0]) & (theta <= fiducial_xip_scale_cut[1]) - xim_cut_mask = (theta >= fiducial_xim_scale_cut[0]) & (theta <= fiducial_xim_scale_cut[1]) + xip_cut_mask = (theta >= fiducial_xip_scale_cut[0]) & ( + theta <= fiducial_xip_scale_cut[1] + ) + xim_cut_mask = (theta >= fiducial_xim_scale_cut[0]) & ( + theta <= fiducial_xim_scale_cut[1] + ) xip_idx = np.where(xip_cut_mask)[0] xim_idx = np.where(xim_cut_mask)[0] chi2_xip_B_cut, pte_xip_B_cut, dof_xip_B_cut = compute_chi2_pte( - xip_B[xip_idx], block_xip_B[np.ix_(xip_idx, xip_idx)]) + xip_B[xip_idx], block_xip_B[np.ix_(xip_idx, xip_idx)] + ) chi2_xim_B_cut, pte_xim_B_cut, dof_xim_B_cut = compute_chi2_pte( - xim_B[xim_idx], block_xim_B[np.ix_(xim_idx, xim_idx)]) - - datasets.append({ - "version": version, - "label": version_label(version, version_labels), - "color": color, - "marker": marker, - "fillstyle": fillstyle, - "alpha": get_version_alpha(version, fiducial_for_comparison, plotting_config), - "theta": theta, - # Total correlation functions - "xip_total": xip_total, - "xim_total": xim_total, - "sigma_xip_total": sigma_xip_total, - "sigma_xim_total": sigma_xim_total, - # B-mode (normalized) - "xip_B_normalized": xip_B / sigma_xip_B, - "xim_B_normalized": xim_B / sigma_xim_B, - # PTEs - "pte_xip_B": pte_xip_B, "chi2_xip_B": chi2_xip_B, "dof_xip_B": dof_xip_B, - "pte_xim_B": pte_xim_B, "chi2_xim_B": chi2_xim_B, "dof_xim_B": dof_xim_B, - "pte_xip_B_cut": pte_xip_B_cut, "chi2_xip_B_cut": chi2_xip_B_cut, "dof_xip_B_cut": dof_xip_B_cut, - "pte_xim_B_cut": pte_xim_B_cut, "chi2_xim_B_cut": chi2_xim_B_cut, "dof_xim_B_cut": dof_xim_B_cut, - }) + xim_B[xim_idx], block_xim_B[np.ix_(xim_idx, xim_idx)] + ) + + datasets.append( + { + "version": version, + "label": version_label(version, version_labels), + "color": color, + "marker": marker, + "fillstyle": fillstyle, + "alpha": get_version_alpha( + version, fiducial_for_comparison, plotting_config + ), + "theta": theta, + # Total correlation functions + "xip_total": xip_total, + "xim_total": xim_total, + "sigma_xip_total": sigma_xip_total, + "sigma_xim_total": sigma_xim_total, + # B-mode (normalized) + "xip_B_normalized": xip_B / sigma_xip_B, + "xim_B_normalized": xim_B / sigma_xim_B, + # PTEs + "pte_xip_B": pte_xip_B, + "chi2_xip_B": chi2_xip_B, + "dof_xip_B": dof_xip_B, + "pte_xim_B": pte_xim_B, + "chi2_xim_B": chi2_xim_B, + "dof_xim_B": dof_xim_B, + "pte_xip_B_cut": pte_xip_B_cut, + "chi2_xip_B_cut": chi2_xip_B_cut, + "dof_xip_B_cut": dof_xip_B_cut, + "pte_xim_B_cut": pte_xim_B_cut, + "chi2_xim_B_cut": chi2_xim_B_cut, + "dof_xim_B_cut": dof_xim_B_cut, + } + ) # Create output directory output_dir = Path(snakemake.output["evidence"]).parent @@ -365,8 +394,11 @@ def main(): # Generate figure fig = _create_version_comparison_figure( - datasets, scale_cuts, fiducial_for_comparison, - x_offset_factors=x_offset_factors, box_style=box_style + datasets, + scale_cuts, + fiducial_for_comparison, + x_offset_factors=x_offset_factors, + box_style=box_style, ) fig_name = "figure.png" @@ -390,10 +422,20 @@ def main(): evidence_versions = {} for data in datasets: v = data["version"] - for key in ("pte_xip_B", "chi2_xip_B", "dof_xip_B", - "pte_xim_B", "chi2_xim_B", "dof_xim_B", - "pte_xip_B_cut", "chi2_xip_B_cut", "dof_xip_B_cut", - "pte_xim_B_cut", "chi2_xim_B_cut", "dof_xim_B_cut"): + for key in ( + "pte_xip_B", + "chi2_xip_B", + "dof_xip_B", + "pte_xim_B", + "chi2_xim_B", + "dof_xim_B", + "pte_xip_B_cut", + "chi2_xip_B_cut", + "dof_xip_B_cut", + "pte_xim_B_cut", + "chi2_xim_B_cut", + "dof_xim_B_cut", + ): val = data[key] evidence_versions[f"{v}_{key}"] = int(val) if "dof" in key else float(val) diff --git a/papers/bmodes/scripts/test_bandpower_sampling.py b/papers/bmodes/scripts/test_bandpower_sampling.py index 98167ef7..dec8633a 100644 --- a/papers/bmodes/scripts/test_bandpower_sampling.py +++ b/papers/bmodes/scripts/test_bandpower_sampling.py @@ -17,17 +17,46 @@ """ import numpy as np -from scipy import special from cosmo_numba.B_modes.cosebis import COSEBIS - +from scipy import special # The exact 32 powspace ℓ values from the pipeline -ELL_32 = np.array([ - 12.5, 24., 38.5, 56.5, 78., 103., 131.5, 163.5, 199., 238., - 281., 327.5, 377., 430., 487., 547.5, 611., 678., 749., 823.5, - 901., 982., 1067., 1155.5, 1247.5, 1343., 1441.5, 1544., 1650., - 1759.5, 1872.5, 1988.5, -]) +ELL_32 = np.array( + [ + 12.5, + 24.0, + 38.5, + 56.5, + 78.0, + 103.0, + 131.5, + 163.5, + 199.0, + 238.0, + 281.0, + 327.5, + 377.0, + 430.0, + 487.0, + 547.5, + 611.0, + 678.0, + 749.0, + 823.5, + 901.0, + 982.0, + 1067.0, + 1155.5, + 1247.5, + 1343.0, + 1441.5, + 1544.0, + 1650.0, + 1759.5, + 1872.5, + 1988.5, + ] +) def theory_cl(ell, model="power_law"): @@ -64,7 +93,6 @@ def config_space_cosebis(theta_min, theta_max, nmodes, cl_model, n_theta=5_000): xip = np.zeros(n_theta) xim = np.zeros(n_theta) weight = ell_dense * cl / (2 * np.pi) # (n_ell,) - d_ell = np.gradient(ell_dense) # for trapezoid-like weighting for start in range(0, n_theta, chunk): end = min(start + chunk, n_theta) @@ -102,21 +130,28 @@ def main(): (1.0, 250.0, "full [1,250]'"), (12.0, 83.0, "fiducial [12,83]'"), ]: - print(f"\n{'='*75}") + print(f"\n{'=' * 75}") print(f"C(ℓ) model: {cl_model}, scale cut: {label}") - print(f"{'='*75}") + print(f"{'=' * 75}") # --- Ground truth: dense ℓ --- ell_dense = np.logspace(np.log10(2), np.log10(5000), 10_000) cl_dense = theory_cl(ell_dense, model=cl_model) ce_dense, _ = harmonic_cosebis( - ell_dense, cl_dense, theta_min, theta_max, nmodes, + ell_dense, + cl_dense, + theta_min, + theta_max, + nmodes, ) # --- Config-space reference --- print("Computing config-space reference (Hankel + xipm)...") ce_config, _ = config_space_cosebis( - theta_min, theta_max, nmodes, cl_model, + theta_min, + theta_max, + nmodes, + cl_model, ) # --- Binned paths --- @@ -136,11 +171,17 @@ def main(): print("-" * (14 + 13 * len(bin_counts))) for n in range(nmodes): - line = f" n={n+1:>2d}" + line = f" n={n + 1:>2d}" for nb in bin_counts: - r = ce_binned[nb][n] / ce_dense[n] if abs(ce_dense[n]) > 1e-30 else np.nan + r = ( + ce_binned[nb][n] / ce_dense[n] + if abs(ce_dense[n]) > 1e-30 + else np.nan + ) line += f" {r:>12.4f}" - r_cfg = ce_dense[n] / ce_config[n] if abs(ce_config[n]) > 1e-30 else np.nan + r_cfg = ( + ce_dense[n] / ce_config[n] if abs(ce_config[n]) > 1e-30 else np.nan + ) line += f" {r_cfg:>12.4f}" print(line) @@ -152,12 +193,12 @@ def main(): valid = np.isfinite(ratios) if valid.any(): err = np.max(np.abs(1 - ratios[valid])) - print(f" {nb:>4d} bins: {err:.4f} ({err*100:.1f}%)") + print(f" {nb:>4d} bins: {err:.4f} ({err * 100:.1f}%)") ratios_cfg = ce_dense[:n_summary] / ce_config[:n_summary] valid = np.isfinite(ratios_cfg) if valid.any(): err = np.max(np.abs(1 - ratios_cfg[valid])) - print(f" dense/config: {err:.4f} ({err*100:.1f}%)") + print(f" dense/config: {err:.4f} ({err * 100:.1f}%)") if __name__ == "__main__": diff --git a/papers/bmodes/scripts/unblinding_ceremony.py b/papers/bmodes/scripts/unblinding_ceremony.py index ccf2ace4..61ab3a95 100644 --- a/papers/bmodes/scripts/unblinding_ceremony.py +++ b/papers/bmodes/scripts/unblinding_ceremony.py @@ -29,9 +29,8 @@ from getdist import plots from matplotlib import scale as mscale from matplotlib.gridspec import GridSpec -from scipy.interpolate import interp1d - from plotting_utils import PAPER_MPLSTYLE, SquareRootScale +from scipy.interpolate import interp1d # ── Plotting environment ──────────────────────────────────────────────────── mscale.register_scale(SquareRootScale) @@ -41,6 +40,7 @@ # ── Data types ────────────────────────────────────────────────────────────── + @dataclass(frozen=True) class ChainSpec: root: str @@ -50,7 +50,18 @@ class ChainSpec: alpha: float = 1.0 -FULL_PARAMS = ["OMEGA_M", "ombh2", "h0", "n_s", "SIGMA_8", "s_8_input", "logt_agn", "a", "m1", "bias_1"] +FULL_PARAMS = [ + "OMEGA_M", + "ombh2", + "h0", + "n_s", + "SIGMA_8", + "s_8_input", + "logt_agn", + "a", + "m1", + "bias_1", +] COSMO_PARAMS = ["OMEGA_M", "s_8_input", "SIGMA_8", "a"] MUTED_ALPHA = 0.25 @@ -85,6 +96,7 @@ def _save_path(cfg: CeremonyConfig, index: int, slug: str) -> Path: # ── Chain loading (extracted from Sasha's notebooks) ──────────────────────── + def _load_xi_table(path: Path | str, nrows: int = 20) -> np.ndarray: rows: list[np.ndarray] = [] with Path(path).open("r", encoding="utf-8") as handle: @@ -99,7 +111,9 @@ def _load_xi_table(path: Path | str, nrows: int = 20) -> np.ndarray: if len(rows) >= nrows: break if len(rows) < nrows: - raise ValueError(f"Expected at least {nrows} xi rows in {path}, found {len(rows)}") + raise ValueError( + f"Expected at least {nrows} xi rows in {path}, found {len(rows)}" + ) return np.vstack(rows) @@ -125,7 +139,9 @@ def ensure_getdist_chain(base_dir: Path, root: str) -> Path: samples = np.loadtxt(samples_path) if "nautilus" in root: - samples = np.column_stack((np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])) + samples = np.column_stack( + (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3]) + ) else: samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4])) np.savetxt(gd_samples_path, samples) @@ -147,7 +163,19 @@ def _build_plotter( def _set_param_labels(chain) -> None: - name_list = ["OMEGA_M", "ombh2", "h0", "n_s", "SIGMA_8", "S_8", "s_8_input", "logt_agn", "a", "m1", "bias_1"] + name_list = [ + "OMEGA_M", + "ombh2", + "h0", + "n_s", + "SIGMA_8", + "S_8", + "s_8_input", + "logt_agn", + "a", + "m1", + "bias_1", + ] label_list = [ r"\Omega_{\rm m}", r"\omega_{\rm b} h^2", @@ -179,7 +207,9 @@ def _set_param_labels(chain) -> None: pass -def _adjust_paramname_chain(chain, current_name: str, target_name: str, label: str) -> None: +def _adjust_paramname_chain( + chain, current_name: str, target_name: str, label: str +) -> None: try: param_names = chain.getParamNames() par = param_names.parWithName(current_name) @@ -214,7 +244,12 @@ def load_getdist_chains( axes_labelsize: float, legend_fontsize: float, ): - g = _build_plotter(width_inch=width_inch, axes_fontsize=axes_fontsize, axes_labelsize=axes_labelsize, legend_fontsize=legend_fontsize) + g = _build_plotter( + width_inch=width_inch, + axes_fontsize=axes_fontsize, + axes_labelsize=axes_labelsize, + legend_fontsize=legend_fontsize, + ) chains = [] for spec in chain_specs: @@ -225,7 +260,14 @@ def load_getdist_chains( settings={"ignore_rows": 0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) _set_param_labels(chain) - if spec.base_dir.name == "ext_data" or spec.root in {"Planck18", "DES_Y3", "KiDS-1000", "DES+KiDS", "HSC_Y3", "HSC_Y3_cell"}: + if spec.base_dir.name == "ext_data" or spec.root in { + "Planck18", + "DES_Y3", + "KiDS-1000", + "DES+KiDS", + "HSC_Y3", + "HSC_Y3_cell", + }: _harmonize_external_chain(chain, spec.root) chains.append(chain) @@ -234,6 +276,7 @@ def load_getdist_chains( # ── Plot functions (extracted from Sasha's notebooks) ─────────────────────── + def plot_triangle( chain_specs: list[ChainSpec], param_names: list[str], @@ -245,7 +288,9 @@ def plot_triangle( legend_loc: str = "upper right", ) -> None: """Extracted triangle_plot pattern from contour notebooks.""" - g, chains = load_getdist_chains(chain_specs, width_inch, axes_fontsize, axes_labelsize, legend_fontsize) + g, chains = load_getdist_chains( + chain_specs, width_inch, axes_fontsize, axes_labelsize, legend_fontsize + ) colours = [spec.color for spec in chain_specs] linestyle = ["solid" for _ in chain_specs] @@ -277,22 +322,74 @@ def plot_xipm_data_vector(xipm_path: str, output_path: Path) -> None: fig, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(10, 4.5)) - ax1.tick_params(axis="both", which="both", direction="in", length=6, width=1, top=True, bottom=True, left=True, right=True) + ax1.tick_params( + axis="both", + which="both", + direction="in", + length=6, + width=1, + top=True, + bottom=True, + left=True, + right=True, + ) ax1.yaxis.minorticks_on() - ax1.plot(theta, xip * 1e4, marker="o", markersize=4, ls="solid", lw=1.8, color="royalblue") - ax1.fill_between(theta, (xip - varxip) * 1e4, (xip + varxip) * 1e4, color="powderblue", alpha=0.7) - ax1.text(0.85, 0.88, "1-1", transform=ax1.transAxes, bbox=dict(facecolor="white", edgecolor="black", boxstyle="round", pad=0.5)) + ax1.plot( + theta, + xip * 1e4, + marker="o", + markersize=4, + ls="solid", + lw=1.8, + color="royalblue", + ) + ax1.fill_between( + theta, (xip - varxip) * 1e4, (xip + varxip) * 1e4, color="powderblue", alpha=0.7 + ) + ax1.text( + 0.85, + 0.88, + "1-1", + transform=ax1.transAxes, + bbox=dict(facecolor="white", edgecolor="black", boxstyle="round", pad=0.5), + ) ax1.axvspan(0, 10, color="gray", alpha=0.3) ax1.axvspan(150, 200, color="gray", alpha=0.3) ax1.set_xscale("log") ax1.set_xlabel(r"$\theta$ [arcmin]") ax1.set_ylabel(r"$\xi_+\times 10^4$") - ax2.tick_params(axis="both", which="both", direction="in", length=6, width=1, top=True, bottom=True, left=True, right=True) + ax2.tick_params( + axis="both", + which="both", + direction="in", + length=6, + width=1, + top=True, + bottom=True, + left=True, + right=True, + ) ax2.yaxis.minorticks_on() - ax2.plot(theta, xim * 1e4, marker="o", markersize=4, ls="solid", lw=1.8, color="orangered") - ax2.fill_between(theta, (xim - varxim) * 1e4, (xim + varxim) * 1e4, color="pink", alpha=0.7) - ax2.text(0.85, 0.88, "1-1", transform=ax2.transAxes, bbox=dict(facecolor="white", edgecolor="black", boxstyle="round", pad=0.5)) + ax2.plot( + theta, + xim * 1e4, + marker="o", + markersize=4, + ls="solid", + lw=1.8, + color="orangered", + ) + ax2.fill_between( + theta, (xim - varxim) * 1e4, (xim + varxim) * 1e4, color="pink", alpha=0.7 + ) + ax2.text( + 0.85, + 0.88, + "1-1", + transform=ax2.transAxes, + bbox=dict(facecolor="white", edgecolor="black", boxstyle="round", pad=0.5), + ) ax2.axvspan(0, 10, color="gray", alpha=0.3) ax2.axvspan(150, 200, color="gray", alpha=0.3) ax2.set_xscale("log") @@ -305,7 +402,9 @@ def plot_xipm_data_vector(xipm_path: str, output_path: Path) -> None: plt.close(fig) -def plot_cell_ee_data_vector(pseudo_cl_path: str, pseudo_cl_cov_path: str, output_path: Path) -> None: +def plot_cell_ee_data_vector( + pseudo_cl_path: str, pseudo_cl_cov_path: str, output_path: Path +) -> None: """Extracted from 2025_10_08_plot_data_vectors.py (EE panel logic).""" cell = fits.getdata(pseudo_cl_path) cov_cell = fits.open(pseudo_cl_cov_path) @@ -385,8 +484,12 @@ def plot_xipm_bestfit_with_bmodes( cov_pure_eb = eb_data["cov_pure_eb"] nbins = len(theta_eb) - sigma_xip_B = np.sqrt(np.diag(cov_pure_eb[2 * nbins : 3 * nbins, 2 * nbins : 3 * nbins])) - sigma_xim_B = np.sqrt(np.diag(cov_pure_eb[3 * nbins : 4 * nbins, 3 * nbins : 4 * nbins])) + sigma_xip_B = np.sqrt( + np.diag(cov_pure_eb[2 * nbins : 3 * nbins, 2 * nbins : 3 * nbins]) + ) + sigma_xim_B = np.sqrt( + np.diag(cov_pure_eb[3 * nbins : 4 * nbins, 3 * nbins : 4 * nbins]) + ) min_sep, max_sep = 1.0, 250.0 bin_edges = np.geomspace(min_sep, max_sep, nbins + 1) @@ -403,22 +506,38 @@ def get_bin_edge_cuts(centers, edges, scale_cut): has_theory = bestfit_dir is not None if has_theory: - theta_theory_rad = np.loadtxt(bestfit_dir / "shear_xi_plus" / "theta.txt", comments="#") + theta_theory_rad = np.loadtxt( + bestfit_dir / "shear_xi_plus" / "theta.txt", comments="#" + ) theta_theory = np.rad2deg(theta_theory_rad) * 60 - xip_theory = np.loadtxt(bestfit_dir / "shear_xi_plus" / "bin_1_1.txt", comments="#") - xim_theory = np.loadtxt(bestfit_dir / "shear_xi_minus" / "bin_1_1.txt", comments="#") + xip_theory = np.loadtxt( + bestfit_dir / "shear_xi_plus" / "bin_1_1.txt", comments="#" + ) + xim_theory = np.loadtxt( + bestfit_dir / "shear_xi_minus" / "bin_1_1.txt", comments="#" + ) theta_sys_rad = np.loadtxt(bestfit_dir / "xi_sys" / "theta.txt", comments="#") theta_sys = np.rad2deg(theta_sys_rad) * 60 xip_sys = np.loadtxt(bestfit_dir / "xi_sys" / "shear_xi_plus.txt", comments="#") - xim_sys = np.loadtxt(bestfit_dir / "xi_sys" / "shear_xi_minus.txt", comments="#") + xim_sys = np.loadtxt( + bestfit_dir / "xi_sys" / "shear_xi_minus.txt", comments="#" + ) theta_fine = np.geomspace(0.5, 300, 500) if has_theory: - xip_th_interp = interp1d(theta_theory, xip_theory, kind="cubic", fill_value="extrapolate")(theta_fine) - xim_th_interp = interp1d(theta_theory, xim_theory, kind="cubic", fill_value="extrapolate")(theta_fine) - xip_sys_interp = interp1d(theta_sys, xip_sys, kind="cubic", fill_value="extrapolate")(theta_fine) - xim_sys_interp = interp1d(theta_sys, xim_sys, kind="cubic", fill_value="extrapolate")(theta_fine) + xip_th_interp = interp1d( + theta_theory, xip_theory, kind="cubic", fill_value="extrapolate" + )(theta_fine) + xim_th_interp = interp1d( + theta_theory, xim_theory, kind="cubic", fill_value="extrapolate" + )(theta_fine) + xip_sys_interp = interp1d( + theta_sys, xip_sys, kind="cubic", fill_value="extrapolate" + )(theta_fine) + xim_sys_interp = interp1d( + theta_sys, xim_sys, kind="cubic", fill_value="extrapolate" + )(theta_fine) scale_factor = 1e-4 xlim = [1, 250] @@ -432,15 +551,44 @@ def get_bin_edge_cuts(centers, edges, scale_cut): fig, axes = plt.subplots(1, 2, figsize=(10, 4.5), sharey=True) plot_configs = [ - (axes[0], xip_data, sigma_xip, xip_B, sigma_xip_B, - xip_th_interp if has_theory else None, xip_sys_interp if has_theory else None, - edge_cut_xip, r"$\xi_+$", "+"), - (axes[1], xim_data, sigma_xim, xim_B, sigma_xim_B, - xim_th_interp if has_theory else None, xim_sys_interp if has_theory else None, - edge_cut_xim, r"$\xi_-$", "-"), + ( + axes[0], + xip_data, + sigma_xip, + xip_B, + sigma_xip_B, + xip_th_interp if has_theory else None, + xip_sys_interp if has_theory else None, + edge_cut_xip, + r"$\xi_+$", + "+", + ), + ( + axes[1], + xim_data, + sigma_xim, + xim_B, + sigma_xim_B, + xim_th_interp if has_theory else None, + xim_sys_interp if has_theory else None, + edge_cut_xim, + r"$\xi_-$", + "-", + ), ] - for idx, (ax, xi_data_arr, sigma_xi, xi_B, sigma_B, xi_th, xi_sys_arr, edge_cut, label, _pm) in enumerate(plot_configs): + for idx, ( + ax, + xi_data_arr, + sigma_xi, + xi_B, + sigma_B, + xi_th, + xi_sys_arr, + edge_cut, + label, + _pm, + ) in enumerate(plot_configs): show_legend = idx == 1 ax.axvspan(xlim[0], edge_cut[0], color="0.90", zorder=0, alpha=0.7) @@ -453,7 +601,9 @@ def get_bin_edge_cuts(centers, edges, scale_cut): "-", color="k", lw=1.5, - label=r"Best-fit $\xi^{\mathrm{th}}_\pm + \xi^{\mathrm{sys}}_\pm$" if show_legend else None, + label=r"Best-fit $\xi^{\mathrm{th}}_\pm + \xi^{\mathrm{sys}}_\pm$" + if show_legend + else None, zorder=2, ) ax.plot( @@ -593,19 +743,34 @@ def plot_cell_ee_with_bestfit( ax.axvline(x=500, color="black", linestyle="--", alpha=0.3) ax.text( - 1740, 0.90, r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", + 1740, + 0.90, + r"$k_\mathrm{max} = 3 h$ Mpc$^{-1}$", transform=ax.get_xaxis_transform(), - ha="center", va="top", fontsize=10, rotation=90, + ha="center", + va="top", + fontsize=10, + rotation=90, ) ax.text( - 1978, 0.90, r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", + 1978, + 0.90, + r"$k_\mathrm{max} = 5 h$ Mpc$^{-1}$", transform=ax.get_xaxis_transform(), - ha="center", va="top", fontsize=10, rotation=90, + ha="center", + va="top", + fontsize=10, + rotation=90, ) ax.text( - 470, 0.90, r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", + 470, + 0.90, + r"$k_\mathrm{max} = 1 h$ Mpc$^{-1}$", transform=ax.get_xaxis_transform(), - ha="center", va="top", fontsize=10, rotation=90, + ha="center", + va="top", + fontsize=10, + rotation=90, ) ax.set_ylabel(r"$\ell C_\ell$", fontsize=16) @@ -646,7 +811,13 @@ def plot_s8_whisker( reference_labels = [reference_label] reference_colors = reference_colors or [None] - g, chains = load_getdist_chains(chain_specs, width_inch=30, axes_fontsize=60, axes_labelsize=60, legend_fontsize=60) + g, chains = load_getdist_chains( + chain_specs, + width_inch=30, + axes_fontsize=60, + axes_labelsize=60, + legend_fontsize=60, + ) plt.close(g.fig) labels = [spec.label for spec in chain_specs] @@ -654,7 +825,21 @@ def plot_s8_whisker( alphas = [spec.alpha for spec in chain_specs] param_values = np.array( - [["# Expt", "Colour", "S8_Mean", "S8_low", "S8_high", "sigma_8_Mean", "sigma_8_low", "sigma_8_high", "Omega_m_Mean", "Omega_m_low", "Omega_m_high"]], + [ + [ + "# Expt", + "Colour", + "S8_Mean", + "S8_low", + "S8_high", + "sigma_8_Mean", + "sigma_8_low", + "sigma_8_high", + "Omega_m_Mean", + "Omega_m_low", + "Omega_m_high", + ] + ], dtype=object, ) @@ -676,7 +861,7 @@ def plot_s8_whisker( omegam_high = param_values[1:, 10].astype(np.float64) ref_indices = [] - for rl in (reference_labels or []): + for rl in reference_labels or []: matches = np.where(expt == rl)[0] if len(matches): ref_indices.append(matches[0]) @@ -703,13 +888,35 @@ def plot_s8_whisker( for ax, param in zip(axs, params): means, lows, highs, label = param - for i, mean, low, high, color, alpha in zip(y, means, lows, highs, colours_arr, alphas): - ax.errorbar(mean, 0.05 + i * row_spacing, xerr=np.array([low, high])[:, None], fmt="o", color=color, ecolor=color, elinewidth=2, capsize=3, alpha=alpha) + for i, mean, low, high, color, alpha in zip( + y, means, lows, highs, colours_arr, alphas + ): + ax.errorbar( + mean, + 0.05 + i * row_spacing, + xerr=np.array([low, high])[:, None], + fmt="o", + color=color, + ecolor=color, + elinewidth=2, + capsize=3, + alpha=alpha, + ) ax.set_xlabel(label, fontsize=14) for ri, ref_idx in enumerate(ref_indices): - band_color = (reference_colors[ri] if reference_colors and ri < len(reference_colors) else colours_arr[ref_idx]) or colours_arr[ref_idx] - ax.axvspan(means[ref_idx] - lows[ref_idx], means[ref_idx] + highs[ref_idx], color=band_color, alpha=0.15, zorder=0) + band_color = ( + reference_colors[ri] + if reference_colors and ri < len(reference_colors) + else colours_arr[ref_idx] + ) or colours_arr[ref_idx] + ax.axvspan( + means[ref_idx] - lows[ref_idx], + means[ref_idx] + highs[ref_idx], + color=band_color, + alpha=0.15, + zorder=0, + ) ax.grid(False) ax.tick_params(axis="y", left=False, labelleft=False) @@ -725,12 +932,37 @@ def plot_s8_whisker( for label, color, alpha in zip(expt, colours_arr, alphas): idx = np.where(expt == label)[0][0] yloc = 0.05 + row_spacing * idx - axs[0].text(0.26, yloc, label, fontsize=12, ha="left", va="center", color=color, alpha=alpha) + axs[0].text( + 0.26, + yloc, + label, + fontsize=12, + ha="left", + va="center", + color=color, + alpha=alpha, + ) if label not in ref_label_set and ref_indices: ri0 = ref_indices[0] - s8_tension = get_sigma_tension(s8_mean[idx], s8_low[idx], s8_high[idx], s8_mean[ri0], s8_low[ri0], s8_high[ri0]) + s8_tension = get_sigma_tension( + s8_mean[idx], + s8_low[idx], + s8_high[idx], + s8_mean[ri0], + s8_low[ri0], + s8_high[ri0], + ) sign_str = "+" if s8_tension > 0 else "-" - axs[0].text(1.045, yloc, rf"${sign_str}{np.abs(s8_tension):.2f}" + r"\, \sigma$", fontsize=10, ha="right", va="center", color=color, alpha=alpha) + axs[0].text( + 1.045, + yloc, + rf"${sign_str}{np.abs(s8_tension):.2f}" + r"\, \sigma$", + fontsize=10, + ha="right", + va="center", + color=color, + alpha=alpha, + ) plt.gca().invert_yaxis() plt.tight_layout() @@ -742,6 +974,7 @@ def plot_s8_whisker( # ── Snakemake entry ────────────────────────────────────────────────────────── + def _config_from_snakemake(smk) -> CeremonyConfig: """Build config from snakemake.input / snakemake.output / snakemake.params.""" chain_root_dir = Path(smk.params.chain_root_dir) @@ -793,10 +1026,21 @@ def _config_from_cli() -> CeremonyConfig: """ _PROJECT_ROOT = _SCRIPT_DIR.parent.parent - parser = argparse.ArgumentParser(description="Run the UNIONS unblinding ceremony plot sequence.") + parser = argparse.ArgumentParser( + description="Run the UNIONS unblinding ceremony plot sequence." + ) parser.add_argument("blind", choices=["A", "B", "C"], help="Revealed blind letter") - parser.add_argument("--chain-version", default=_DEFAULT_CHAIN_VERSION, help="Chain version (default: %(default)s)") - parser.add_argument("--output-dir", type=Path, default=None, help="Output directory for results (default: /results/unblinding)") + parser.add_argument( + "--chain-version", + default=_DEFAULT_CHAIN_VERSION, + help="Chain version (default: %(default)s)", + ) + parser.add_argument( + "--output-dir", + type=Path, + default=None, + help="Output directory for results (default: /results/unblinding)", + ) args = parser.parse_args() blind = args.blind @@ -805,21 +1049,29 @@ def _config_from_cli() -> CeremonyConfig: output_dir = args.output_dir or (_PROJECT_ROOT / "results" / "unblinding") xi_data_path = _require_path( - _COSMOSIS_DATA_DIR / f"{chain_prefix}_{blind}" / f"cosmosis_{chain_prefix}_{blind}.fits", + _COSMOSIS_DATA_DIR + / f"{chain_prefix}_{blind}" + / f"cosmosis_{chain_prefix}_{blind}.fits", f"CosmoSIS xi FITS for blind {blind}", ) pseudo_cl_path = _require_path( - Path(f"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_{chain_prefix}.fits"), + Path( + f"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_{chain_prefix}.fits" + ), f"pseudo-Cl for {chain_prefix} (Guerrini)", ) pseudo_cl_cov_path = _require_path( - Path(f"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_{chain_prefix}.fits"), + Path( + f"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_{chain_prefix}.fits" + ), f"pseudo-Cl covariance for {chain_prefix} (Guerrini)", ) cosmosis_cell_fits = _require_path( - _COSMOSIS_DATA_DIR / f"{chain_prefix}_{blind}_fid" / f"cosmosis_{chain_prefix}_{blind}_fid_cell.fits", + _COSMOSIS_DATA_DIR + / f"{chain_prefix}_{blind}_fid" + / f"cosmosis_{chain_prefix}_{blind}_fid_cell.fits", f"CosmoSIS C_ell FITS for blind {blind}", ) @@ -839,21 +1091,30 @@ def _config_from_cli() -> CeremonyConfig: evidence_dir=output_dir / "claims" / "unblinding_ceremony", xi_data_path=xi_data_path, pure_eb_path=_require_path( - _PROJECT_ROOT / "results" / "paper_plots" / "intermediate" / f"{chain_prefix}_{blind}_pure_eb_semianalytic.npz", + _PROJECT_ROOT + / "results" + / "paper_plots" + / "intermediate" + / f"{chain_prefix}_{blind}_pure_eb_semianalytic.npz", f"Pure E/B file for blind {blind}", ), pseudo_cl_path=pseudo_cl_path, pseudo_cl_cov_path=pseudo_cl_cov_path, cosmosis_cell_fits=cosmosis_cell_fits, bestfit_dir=bestfit_dir, - bestfit_root_fid_cell=_require_bestfit_root(_CHAIN_ROOT_DIR, f"{chain_prefix}_{blind}_fid_cell"), - bestfit_root_halofit_cell=_require_bestfit_root(_CHAIN_ROOT_DIR, f"{chain_prefix}_{blind}_halofit_cell"), + bestfit_root_fid_cell=_require_bestfit_root( + _CHAIN_ROOT_DIR, f"{chain_prefix}_{blind}_fid_cell" + ), + bestfit_root_halofit_cell=_require_bestfit_root( + _CHAIN_ROOT_DIR, f"{chain_prefix}_{blind}_halofit_cell" + ), bestfit_root_config=bestfit_root_config, ) # ── Main ceremony ──────────────────────────────────────────────────────────── + def run_ceremony(cfg: CeremonyConfig) -> None: blind = cfg.blind cfg.results_dir.mkdir(parents=True, exist_ok=True) @@ -873,7 +1134,9 @@ def run_ceremony(cfg: CeremonyConfig) -> None: # 02: C_ell^EE data, no theory plot_cell_ee_data_vector( - str(cfg.pseudo_cl_path), str(cfg.pseudo_cl_cov_path), _save_path(cfg, 2, "cell_ee_data") + str(cfg.pseudo_cl_path), + str(cfg.pseudo_cl_cov_path), + _save_path(cfg, 2, "cell_ee_data"), ) # 03: xi+/- with config-space best-fit (Paper IV Fig 1) @@ -899,7 +1162,7 @@ def run_ceremony(cfg: CeremonyConfig) -> None: {"color": "royalblue", "linestyle": "--"}, ), ( - rf"UNIONS $\xi_\pm(\vartheta)$ (Goh et al., 2026)", + r"UNIONS $\xi_\pm(\vartheta)$ (Goh et al., 2026)", cfg.bestfit_root_config, {"color": "orange", "linestyle": "-"}, ), @@ -1011,12 +1274,34 @@ def run_ceremony(cfg: CeremonyConfig) -> None: ) ) - whisker_specs.extend([ - ChainSpec(root="Planck18", label=r"\textit{Planck} 2018", color="black", base_dir=cfg.external_root_dir), - ChainSpec(root="DES_Y3", label=r"DES Y3 $\xi_\pm$", color="black", base_dir=cfg.external_root_dir), - ChainSpec(root="KiDS-1000", label=r"KiDS-1000 $\xi_\pm$", color="black", base_dir=cfg.external_root_dir), - ChainSpec(root="HSC_Y3", label=r"HSC Y3 $\xi_\pm$", color="black", base_dir=cfg.external_root_dir), - ]) + whisker_specs.extend( + [ + ChainSpec( + root="Planck18", + label=r"\textit{Planck} 2018", + color="black", + base_dir=cfg.external_root_dir, + ), + ChainSpec( + root="DES_Y3", + label=r"DES Y3 $\xi_\pm$", + color="black", + base_dir=cfg.external_root_dir, + ), + ChainSpec( + root="KiDS-1000", + label=r"KiDS-1000 $\xi_\pm$", + color="black", + base_dir=cfg.external_root_dir, + ), + ChainSpec( + root="HSC_Y3", + label=r"HSC Y3 $\xi_\pm$", + color="black", + base_dir=cfg.external_root_dir, + ), + ] + ) plot_s8_whisker( whisker_specs, @@ -1056,9 +1341,16 @@ def run_ceremony(cfg: CeremonyConfig) -> None: "pseudo_cl": str(cfg.pseudo_cl_path), "pseudo_cl_cov": str(cfg.pseudo_cl_cov_path), "cosmosis_cell_fits": str(cfg.cosmosis_cell_fits), - "harmonic_chains": str(cfg.chain_root_dir / f"{cfg.chain_prefix}_{{A,B,C}}_lmin=300_lmax=1600_cell"), - "config_chains": str(cfg.chain_root_dir / f"{cfg.chain_prefix}_{{A,B,C}}_10_80"), - "external_chains": str(cfg.external_root_dir / "{Planck18,DES_Y3,KiDS-1000,HSC_Y3}"), + "harmonic_chains": str( + cfg.chain_root_dir + / f"{cfg.chain_prefix}_{{A,B,C}}_lmin=300_lmax=1600_cell" + ), + "config_chains": str( + cfg.chain_root_dir / f"{cfg.chain_prefix}_{{A,B,C}}_10_80" + ), + "external_chains": str( + cfg.external_root_dir / "{Planck18,DES_Y3,KiDS-1000,HSC_Y3}" + ), }, "output": output_dict, "params": { diff --git a/papers/bmodes/scripts/update_survey_stats.py b/papers/bmodes/scripts/update_survey_stats.py index 02038b8e..6713b3c0 100644 --- a/papers/bmodes/scripts/update_survey_stats.py +++ b/papers/bmodes/scripts/update_survey_stats.py @@ -8,16 +8,16 @@ python workflow/scripts/update_survey_stats.py \ --mask-standard --mask-starhalo """ + import argparse import gc -import sys import os +from pathlib import Path import healpy as hp import numpy as np -from astropy.io import fits -from pathlib import Path import yaml +from astropy.io import fits os.chdir("/n17data/cdaley/unions/pure_eb/code/sp_validation/cosmo_val") @@ -60,7 +60,7 @@ def compute_stats_chunked(catalog_path, w_col, e1_col, e2_col, chunk_size=1_000_ e2 = np.asarray(data[e2_col][start:stop], dtype=np.float64) sum_w += np.sum(w) - w2 = w ** 2 + w2 = w**2 sum_w2 += np.sum(w2) sum_w2_e2 += np.sum(w2 * (e1**2 + e2**2)) @@ -87,10 +87,13 @@ def main(): print(f"Standard footprint area: {area_standard:.2f} deg²") print(f"Star-halo footprint area: {area_starhalo:.2f} deg²\n") - all_versions = [(v, area_standard) for v in STANDARD_VERSIONS] + \ - [(v, area_starhalo) for v in STARHALO_VERSIONS] + all_versions = [(v, area_standard) for v in STANDARD_VERSIONS] + [ + (v, area_starhalo) for v in STARHALO_VERSIONS + ] - print(f"{'Version':<30} {'Area (deg²)':>12} {'n_eff':>10} {'sigma_e':>10} {'Old A':>10} {'Old n_e':>10} {'Old σ_e':>10}") + print( + f"{'Version':<30} {'Area (deg²)':>12} {'n_eff':>10} {'sigma_e':>10} {'Old A':>10} {'Old n_e':>10} {'Old σ_e':>10}" + ) print("-" * 105) for ver, area_deg2 in all_versions: @@ -126,7 +129,9 @@ def main(): n_eff = (sum_w**2) / (area_arcmin2 * sum_w2) if sum_w2 > 0 else 0.0 sigma_e = np.sqrt(sum_w2_e2 / sum_w2) if sum_w2 > 0 else 0.0 - print(f"{ver:<30} {area_deg2:>12.2f} {n_eff:>10.6f} {sigma_e:>10.6f} {old_A:>10.2f} {old_ne:>10.6f} {old_se:>10.6f}") + print( + f"{ver:<30} {area_deg2:>12.2f} {n_eff:>10.6f} {sigma_e:>10.6f} {old_A:>10.2f} {old_ne:>10.6f} {old_se:>10.6f}" + ) if not args.dry_run: if "cov_th" not in cc[ver]: diff --git a/papers/bmodes/scripts/validate_cosebis_filters.py b/papers/bmodes/scripts/validate_cosebis_filters.py index a36ca7a2..cddbf421 100644 --- a/papers/bmodes/scripts/validate_cosebis_filters.py +++ b/papers/bmodes/scripts/validate_cosebis_filters.py @@ -13,12 +13,10 @@ Author: Claude Code """ -import numpy as np -from scipy import special -from scipy import integrate import matplotlib.pyplot as plt - +import numpy as np from cosmo_numba.B_modes.cosebis import COSEBIS +from scipy import integrate, special def direct_Wn_integration(ell_val, theta_arcmin, Tp): @@ -40,6 +38,7 @@ def direct_Wn_quad(ell_val, theta_arcmin, Tp): Interpolates T_+^log and uses scipy.integrate.quad. """ from scipy.interpolate import CubicSpline + theta_rad = np.deg2rad(theta_arcmin / 60) cs = CubicSpline(theta_rad, Tp) @@ -47,8 +46,12 @@ def integrand(t): return t * cs(t) * special.j0(ell_val * t) result, _ = integrate.quad( - integrand, theta_rad[0], theta_rad[-1], - limit=500, epsrel=1e-10, epsabs=0, + integrand, + theta_rad[0], + theta_rad[-1], + limit=500, + epsrel=1e-10, + epsabs=0, ) return result @@ -58,9 +61,9 @@ def validate_scale_cut(theta_min, theta_max, nmodes, ell_test, n_theta=200_000): Returns dict with keys 'fftlog', 'direct', 'ratio' — each (nmodes, n_ell). """ - print(f"\n{'='*70}") + print(f"\n{'=' * 70}") print(f"Scale cut: [{theta_min}, {theta_max}] arcmin, modes 1-{nmodes}") - print(f"{'='*70}") + print(f"{'=' * 70}") cosebis = COSEBIS(theta_min, theta_max, nmodes) @@ -83,7 +86,7 @@ def validate_scale_cut(theta_min, theta_max, nmodes, ell_test, n_theta=200_000): Wn_direct[n, i] = direct_Wn_integration(ell, theta, Tp[n]) # Ratio - with np.errstate(divide='ignore', invalid='ignore'): + with np.errstate(divide="ignore", invalid="ignore"): ratio = np.where( np.abs(Wn_direct) > 1e-30, Wn_fftlog / Wn_direct, @@ -91,11 +94,11 @@ def validate_scale_cut(theta_min, theta_max, nmodes, ell_test, n_theta=200_000): ) return { - 'fftlog': Wn_fftlog, - 'direct': Wn_direct, - 'ratio': ratio, - 'Tp': Tp, - 'theta': theta, + "fftlog": Wn_fftlog, + "direct": Wn_direct, + "ratio": ratio, + "Tp": Tp, + "theta": theta, } @@ -107,12 +110,12 @@ def print_table(ell_test, result, nmodes, indices=None, spot_modes=None): indices = list(range(len(ell_test))) for n in spot_modes: - print(f"\n--- Mode n={n+1} ---") + print(f"\n--- Mode n={n + 1} ---") print(f"{'ell':>8} {'FFT-log':>14} {'Direct':>14} {'Ratio':>10}") print("-" * 50) for i in indices: - r = result['ratio'][n, i] + r = result["ratio"][n, i] print( f"{ell_test[i]:>8.0f} " f"{result['fftlog'][n, i]:>14.6e} " @@ -123,9 +126,9 @@ def print_table(ell_test, result, nmodes, indices=None, spot_modes=None): def quad_spot_check(theta_min, theta_max, nmodes, ell_spot): """Spot-check a few (mode, ℓ) values with adaptive quadrature.""" - print(f"\n{'='*70}") + print(f"\n{'=' * 70}") print(f"Adaptive quadrature spot check: [{theta_min}, {theta_max}]'") - print(f"{'='*70}") + print(f"{'=' * 70}") cosebis = COSEBIS(theta_min, theta_max, nmodes) theta_fine = np.logspace(np.log10(theta_min), np.log10(theta_max), 500_000) @@ -133,7 +136,9 @@ def quad_spot_check(theta_min, theta_max, nmodes, ell_spot): Wn_fftlog = cosebis.get_Wn_log(np.array(ell_spot, dtype=float)) - print(f"{'mode':>6} {'ell':>8} {'FFT-log':>14} {'quad':>14} {'trapz(500k)':>14} {'FFT/quad':>10} {'trapz/quad':>10}") + print( + f"{'mode':>6} {'ell':>8} {'FFT-log':>14} {'quad':>14} {'trapz(500k)':>14} {'FFT/quad':>10} {'trapz/quad':>10}" + ) print("-" * 80) for n in range(min(nmodes, 5)): @@ -144,7 +149,7 @@ def quad_spot_check(theta_min, theta_max, nmodes, ell_spot): r_fft = wn_fft / wn_quad if abs(wn_quad) > 1e-30 else np.nan r_trapz = wn_trapz / wn_quad if abs(wn_quad) > 1e-30 else np.nan print( - f"{n+1:>6d} {ell:>8.0f} {wn_fft:>14.6e} {wn_quad:>14.6e} " + f"{n + 1:>6d} {ell:>8.0f} {wn_fft:>14.6e} {wn_quad:>14.6e} " f"{wn_trapz:>14.6e} {r_fft:>10.6f} {r_trapz:>10.6f}" ) @@ -154,28 +159,31 @@ def plot_results(ell_test, results, output_path): fig, axes = plt.subplots(2, 1, figsize=(12, 10), sharex=True) for ax, (label, result) in zip(axes, results.items()): - nmodes = result['ratio'].shape[0] - cmap = plt.get_cmap('viridis', nmodes) + nmodes = result["ratio"].shape[0] + cmap = plt.get_cmap("viridis", nmodes) for n in range(nmodes): - valid = np.isfinite(result['ratio'][n]) + valid = np.isfinite(result["ratio"][n]) ax.plot( - ell_test[valid], result['ratio'][n, valid], - color=cmap(n), alpha=0.7, lw=1.2, - label=f"n={n+1}" if n < 10 else None, + ell_test[valid], + result["ratio"][n, valid], + color=cmap(n), + alpha=0.7, + lw=1.2, + label=f"n={n + 1}" if n < 10 else None, ) - ax.axhline(1.0, color='k', ls='--', lw=0.8) - ax.axhspan(0.99, 1.01, color='green', alpha=0.1) + ax.axhline(1.0, color="k", ls="--", lw=0.8) + ax.axhspan(0.99, 1.01, color="green", alpha=0.1) ax.set_ylabel("FFT-log / Direct") ax.set_title(rf"$W_n(\ell)$ accuracy: {label}") ax.set_ylim(0.9, 1.1) - ax.legend(ncol=5, fontsize=7, loc='lower left') + ax.legend(ncol=5, fontsize=7, loc="lower left") axes[1].set_xlabel(r"$\ell$") - axes[1].set_xscale('log') + axes[1].set_xscale("log") fig.tight_layout() - fig.savefig(output_path, dpi=150, bbox_inches='tight') + fig.savefig(output_path, dpi=150, bbox_inches="tight") print(f"\nFigure saved: {output_path}") plt.close(fig) @@ -200,17 +208,20 @@ def main(): print_table(ell_test, result_fid, nmodes, indices=spot_idx) # --- Summary statistics --- - for label, result in [("Full [1,250]'", result_full), ("Fiducial [12,83]'", result_fid)]: - print(f"\n{'='*70}") + for label, result in [ + ("Full [1,250]'", result_full), + ("Fiducial [12,83]'", result_fid), + ]: + print(f"\n{'=' * 70}") print(f"Summary: {label}") - print(f"{'='*70}") + print(f"{'=' * 70}") for n in range(nmodes): - valid = np.isfinite(result['ratio'][n]) + valid = np.isfinite(result["ratio"][n]) if valid.any(): - r = result['ratio'][n, valid] + r = result["ratio"][n, valid] print( - f" n={n+1:>2d}: ratio range [{r.min():.4f}, {r.max():.4f}], " - f"median={np.median(r):.4f}, |1-ratio| max={np.max(np.abs(1-r)):.4f}" + f" n={n + 1:>2d}: ratio range [{r.min():.4f}, {r.max():.4f}], " + f"median={np.median(r):.4f}, |1-ratio| max={np.max(np.abs(1 - r)):.4f}" ) # --- Adaptive quadrature spot checks --- @@ -227,14 +238,16 @@ def main(): plot_results(ell_test, results, output_path) # --- Verdict --- - all_ratios = np.concatenate([ - result_full['ratio'][np.isfinite(result_full['ratio'])], - result_fid['ratio'][np.isfinite(result_fid['ratio'])], - ]) + all_ratios = np.concatenate( + [ + result_full["ratio"][np.isfinite(result_full["ratio"])], + result_fid["ratio"][np.isfinite(result_fid["ratio"])], + ] + ) max_err = np.max(np.abs(1 - all_ratios)) - print(f"\n{'='*70}") - print(f"VERDICT") - print(f"{'='*70}") + print(f"\n{'=' * 70}") + print("VERDICT") + print(f"{'=' * 70}") print(f"Maximum |1 - ratio| across all modes, scale cuts, ℓ: {max_err:.6f}") if max_err < 0.01: print("PASS: FFT-log W_n(ℓ) accurate to <1%") diff --git a/papers/bmodes/scripts/validate_cosebis_theory.py b/papers/bmodes/scripts/validate_cosebis_theory.py index b2e39643..dda26f34 100644 --- a/papers/bmodes/scripts/validate_cosebis_theory.py +++ b/papers/bmodes/scripts/validate_cosebis_theory.py @@ -11,7 +11,6 @@ import numpy as np import pyccl as ccl - from cosmo_numba.B_modes.cosebis import COSEBIS @@ -23,8 +22,8 @@ def setup_cosmology_and_tracer(): h=0.67, sigma8=0.8, n_s=0.96, - transfer_function='boltzmann_camb', - matter_power_spectrum='halofit' + transfer_function="boltzmann_camb", + matter_power_spectrum="halofit", ) # Simple Gaussian n(z) centered at z=1.0 @@ -48,8 +47,8 @@ def get_theory_xipm(cosmo, ell, cl_ee, theta_arcmin): """Compute ξ±(θ) from C_ℓ using CCL's built-in correlation function.""" theta_rad = np.deg2rad(theta_arcmin / 60) - xip = ccl.correlation(cosmo, ell=ell, C_ell=cl_ee, theta=theta_rad, type='GG+') - xim = ccl.correlation(cosmo, ell=ell, C_ell=cl_ee, theta=theta_rad, type='GG-') + xip = ccl.correlation(cosmo, ell=ell, C_ell=cl_ee, theta=theta_rad, type="GG+") + xim = ccl.correlation(cosmo, ell=ell, C_ell=cl_ee, theta=theta_rad, type="GG-") return xip, xim @@ -70,7 +69,7 @@ def main(): n_theta = 1000 theta_integration = np.logspace(np.log10(theta_min), np.log10(theta_max), n_theta) - print(f"\nParameters:") + print("\nParameters:") print(f" theta range: [{theta_min}, {theta_max}] arcmin") print(f" COSEBIS modes: 1-{nmodes}") print(f" ℓ grid: {len(ell)} points in [{ell.min()}, {ell.max()}]") @@ -100,24 +99,15 @@ def main(): # Note: Using 20000 points for better high-ℓ accuracy (FFT-log issue) theta_grid = np.logspace(np.log10(theta_min), np.log10(theta_max), 20000) ce_harm, cb_harm = cosebis.cosebis_from_Cell( - ell=ell, - Cell_E=cl_ee, - Cell_B=cl_bb, - theta=theta_grid, - cache=True + ell=ell, Cell_E=cl_ee, Cell_B=cl_bb, theta=theta_grid, cache=True ) print("\nComputing COSEBIS from config-space (ξ±)...") # For config-space, need dtheta for integration dtheta = np.gradient(theta_integration) - xim_zero = np.zeros_like(xip) # B=0 for theory ce_config, cb_config = cosebis.cosebis_from_xipm( - theta=theta_integration, - dtheta=dtheta, - xi_plus=xip, - xi_minus=xim, - cache=True + theta=theta_integration, dtheta=dtheta, xi_plus=xip, xi_minus=xim, cache=True ) # Compare @@ -132,7 +122,9 @@ def main(): # For theory, expect exact agreement, so σ is not meaningful # Just report absolute difference diff = ce_harm[n] - ce_config[n] - print(f"E_{n+1:<4} {ce_harm[n]:>14.6e} {ce_config[n]:>14.6e} {ratio:>10.4f} {diff:>10.2e}") + print( + f"E_{n + 1:<4} {ce_harm[n]:>14.6e} {ce_config[n]:>14.6e} {ratio:>10.4f} {diff:>10.2e}" + ) print("\n" + "=" * 60) print("Results: B_n (should be ~0)") @@ -141,7 +133,7 @@ def main(): print("-" * 60) for n in range(nmodes): - print(f"B_{n+1:<4} {cb_harm[n]:>14.6e} {cb_config[n]:>14.6e}") + print(f"B_{n + 1:<4} {cb_harm[n]:>14.6e} {cb_config[n]:>14.6e}") # Summary statistics print("\n" + "=" * 60) @@ -156,20 +148,22 @@ def main(): # Verdict tolerance = 0.05 # 5% agreement expected for well-resolved integration if np.all(np.abs(ratios - 1) < tolerance): - print(f"\n✓ PASS: Harmonic and config-space agree within {tolerance*100:.0f}%") + print( + f"\n✓ PASS: Harmonic and config-space agree within {tolerance * 100:.0f}%" + ) else: - print(f"\n✗ FAIL: Discrepancy exceeds {tolerance*100:.0f}%") + print(f"\n✗ FAIL: Discrepancy exceeds {tolerance * 100:.0f}%") print(" This could indicate:") print(" 1. Insufficient integration resolution") print(" 2. Different ℓ/θ range coverage") print(" 3. Bug in one of the integration methods") return { - 'ce_harm': ce_harm, - 'cb_harm': cb_harm, - 'ce_config': ce_config, - 'cb_config': cb_config, - 'ratios': ratios + "ce_harm": ce_harm, + "cb_harm": cb_harm, + "ce_config": ce_config, + "cb_config": cb_config, + "ratios": ratios, } diff --git a/papers/catalog/2025_09_19_alpha_leakage_correction.py b/papers/catalog/2025_09_19_alpha_leakage_correction.py index 472b68ca..de411ed8 100644 --- a/papers/catalog/2025_09_19_alpha_leakage_correction.py +++ b/papers/catalog/2025_09_19_alpha_leakage_correction.py @@ -8,25 +8,25 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import matplotlib.pyplot as plt import numpy as np +import pandas as pd +import seaborn as sns import statsmodels.api as sm from astropy.io import fits -import matplotlib.pyplot as plt -import seaborn as sns -import pandas as pd -plt.style.use( - "/home/guerrini/matplotlib_config/paper.mplstyle" -) +plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("husl") -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") -path_cat = "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" +path_cat = ( + "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" +) num_bins = 20 # %% @@ -38,15 +38,18 @@ size_ratio = cat_gal[psf_size] / (cat_gal[gal_size] + cat_gal[psf_size]) df_gal = pd.DataFrame( - np.array([ - np.array(cat_gal['e1'], dtype=np.float64), - np.array(cat_gal['e2'], dtype=np.float64), - np.array(cat_gal['e1_PSF'], dtype=np.float64), - np.array(cat_gal['e2_PSF'], dtype=np.float64), - np.array(size_ratio, dtype=np.float64), - np.array(cat_gal['snr'], dtype=np.float64), - np.array(cat_gal['w_des'], dtype=np.float64), - ]).T, columns=['e1', 'e2', 'e1_PSF', 'e2_PSF', 'size_ratio', 'snr', 'w'] + np.array( + [ + np.array(cat_gal["e1"], dtype=np.float64), + np.array(cat_gal["e2"], dtype=np.float64), + np.array(cat_gal["e1_PSF"], dtype=np.float64), + np.array(cat_gal["e2_PSF"], dtype=np.float64), + np.array(size_ratio, dtype=np.float64), + np.array(cat_gal["snr"], dtype=np.float64), + np.array(cat_gal["w_des"], dtype=np.float64), + ] + ).T, + columns=["e1", "e2", "e1_PSF", "e2_PSF", "size_ratio", "snr", "w"], ) # %% @@ -56,65 +59,63 @@ n_bins_snr = num_bins n_bins_size = num_bins -#Create logarithmic bins in size and SNR -df_gal.loc[:, 'bin_R'] = pd.qcut(df_gal['size_ratio'], n_bins_size, labels=False, retbins=False) +# Create logarithmic bins in size and SNR +df_gal.loc[:, "bin_R"] = pd.qcut( + df_gal["size_ratio"], n_bins_size, labels=False, retbins=False +) -#initialize bin snr -df_gal.loc[:, 'bin_snr'] = -999 +# initialize bin snr +df_gal.loc[:, "bin_snr"] = -999 for ibin_r in range(n_bins_size): - mask_binr = df_gal['bin_R'].values == ibin_r + mask_binr = df_gal["bin_R"].values == ibin_r - df_gal.loc[mask_binr, 'bin_snr'] = pd.qcut( - df_gal.loc[mask_binr, 'snr'], - n_bins_snr, - labels=False, - retbins=False + df_gal.loc[mask_binr, "bin_snr"] = pd.qcut( + df_gal.loc[mask_binr, "snr"], n_bins_snr, labels=False, retbins=False ) -#Group by bin -df_gal_grouped = df_gal.groupby(['bin_R', 'bin_snr']) +# Group by bin +df_gal_grouped = df_gal.groupby(["bin_R", "bin_snr"]) ngroups = df_gal_grouped.ngroups print(f"Number of groups: {ngroups}") # %% # Measure leakage per bins alpha_df = pd.DataFrame( - 0., + 0.0, index=np.arange(ngroups), - columns=['R', 'SNR', 'alpha_1', 'alpha_2', 'alpha_1_err', 'alpha_2_err'] + columns=["R", "SNR", "alpha_1", "alpha_2", "alpha_1_err", "alpha_2_err"], ) i_group = 0 for name, group in df_gal_grouped: - - #Save weighted average - alpha_df.loc[i_group, 'R'] = np.average(group['size_ratio'], weights=group['w']) - alpha_df.loc[i_group, 'SNR'] = np.average(group['snr'], weights=group['w']) - - #Fit linear model to compute alpha - e1_out = np.array(group['e1']) - e2_out = np.array(group['e2']) - weight_out = np.array(group['w']) - e1_PSF = np.array(group['e1_PSF']) - e2_PSF = np.array(group['e2_PSF']) + # Save weighted average + alpha_df.loc[i_group, "R"] = np.average(group["size_ratio"], weights=group["w"]) + alpha_df.loc[i_group, "SNR"] = np.average(group["snr"], weights=group["w"]) + + # Fit linear model to compute alpha + e1_out = np.array(group["e1"]) + e2_out = np.array(group["e2"]) + weight_out = np.array(group["w"]) + e1_PSF = np.array(group["e1_PSF"]) + e2_PSF = np.array(group["e2_PSF"]) del group - #Fit e1 + # Fit e1 mod_wls = sm.WLS(e1_out, sm.add_constant(e1_PSF), weights=weight_out) try: res_wls = mod_wls.fit() - except: - raise RunTimeError("Linear regression fit for PSF leakage failed") - alpha_df.loc[i_group, 'alpha_1'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_1_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + except Exception: + raise RuntimeError("Linear regression fit for PSF leakage failed") + alpha_df.loc[i_group, "alpha_1"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_1_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del res_wls, mod_wls - #Fit e2 + # Fit e2 mod_wls = sm.WLS(e2_out, sm.add_constant(e2_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_2'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_2_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_2"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_2_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del weight_out, res_wls, mod_wls i_group += 1 @@ -125,15 +126,17 @@ SNR_bins_val = alpha_df["SNR"].values R_bins_val = alpha_df["R"].values -alpha = 0.5 * (alpha_df['alpha_1'].values + alpha_df['alpha_2'].values) +alpha = 0.5 * (alpha_df["alpha_1"].values + alpha_df["alpha_2"].values) vmin, vmax = np.min(alpha), np.max(alpha) vmin, vmax = -np.max([np.abs(vmin), np.abs(vmax)]), np.max([np.abs(vmin), np.abs(vmax)]) -cset1 = ax.scatter(SNR_bins_val, R_bins_val, c=alpha, cmap='coolwarm', vmin=vmin, vmax=vmax) +cset1 = ax.scatter( + SNR_bins_val, R_bins_val, c=alpha, cmap="coolwarm", vmin=vmin, vmax=vmax +) ax.set_xlabel("SNR", fontsize=15) ax.set_ylabel(r"$\mathcal{R}$", fontsize=15) -ax.set_xscale('log') -ax.set_ylim(max(R_bins_val)+0.01, min(R_bins_val)-0.01) +ax.set_xscale("log") +ax.set_ylim(max(R_bins_val) + 0.01, min(R_bins_val) - 0.01) cbar = fig.colorbar(cset1, ax=ax) cbar.set_label(r"$\alpha$", fontsize=15) diff --git a/papers/catalog/2025_09_19_rho_stats.py b/papers/catalog/2025_09_19_rho_stats.py index ec2ba9d5..9582f1f5 100644 --- a/papers/catalog/2025_09_19_rho_stats.py +++ b/papers/catalog/2025_09_19_rho_stats.py @@ -3,20 +3,18 @@ ipython = IPython.get_ipython() -import numpy as np -from astropy.io import fits import matplotlib.pyplot as plt +import numpy as np import seaborn as sns +from astropy.io import fits -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("colorblind") -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") @@ -29,13 +27,13 @@ rho_stats = fits.getdata(f"{base_dir}/rho_stats_{version}.fits") cov_rho = np.load(f"{base_dir}/cov_rho_{version}.npy") -n_bins = rho_stats['theta'].shape[0] +n_bins = rho_stats["theta"].shape[0] version_xi = "SP_v1.4.6_no_leak_corr_A" data_vector_path = f"/home/guerrini/sp_validation/cosmo_inference/data/{version_xi}/cosmosis_{version_xi}.fits" data_vector = fits.open(data_vector_path) -xi_plus = data_vector['XI_PLUS'].data['VALUE'] -cov_xi = data_vector['COVMAT'].data +xi_plus = data_vector["XI_PLUS"].data["VALUE"] +cov_xi = data_vector["COVMAT"].data snr = np.sqrt(xi_plus @ np.linalg.inv(cov_xi[:n_bins, :n_bins]) @ xi_plus) print(f"SNR of xi+: {snr:.2f}") @@ -48,7 +46,7 @@ plot_requirements = True # %% -e_psf = "e^\mathrm{PSF}" +e_psf = r"e^\mathrm{PSF}" delta_e_psf = r"\delta e^\mathrm{PSF}" delta_T_psf = r"\delta T^\mathrm{PSF}" @@ -67,36 +65,40 @@ for i in range(6): axs[i].errorbar( - rho_stats['theta'], - np.abs(rho_stats[f'rho_{i}_p']), - yerr=np.sqrt(cov_rho[i*n_bins:(i+1)*n_bins, i*n_bins:(i+1)*n_bins].diagonal()), - fmt='o', - linestyle='-', + rho_stats["theta"], + np.abs(rho_stats[f"rho_{i}_p"]), + yerr=np.sqrt( + cov_rho[ + i * n_bins : (i + 1) * n_bins, i * n_bins : (i + 1) * n_bins + ].diagonal() + ), + fmt="o", + linestyle="-", label=label, capsize=2, - color=color + color=color, ) if plot_requirements: if i in [1, 3, 4]: axs[i].fill_between( - rho_stats['theta'], + rho_stats["theta"], 0, y2=threshold_rho_134, - color='gray', + color="gray", alpha=0.3, ) elif i in [2, 5]: axs[i].fill_between( - rho_stats['theta'], + rho_stats["theta"], 0, y2=threshold_rho_25, - color='gray', + color="gray", alpha=0.3, ) - axs[i].set_xscale('log') - axs[i].set_yscale('log') + axs[i].set_xscale("log") + axs[i].set_yscale("log") axs[i].set_ylabel(rf"$|\rho_{i}(\vartheta)|$") if i in [3, 4, 5]: axs[i].set_xlabel(r"$\vartheta$ [arcmin]") @@ -105,19 +107,20 @@ handles, labels = axs[4].get_legend_handles_labels() fig.legend( - handles, labels, + handles, + labels, loc="upper center", - bbox_to_anchor=(0.5, 0.05), # centered under all axes + bbox_to_anchor=(0.5, 0.05), # centered under all axes ncol=2, frameon=False, - fontsize=16 + fontsize=16, ) plt.tight_layout() -plt.savefig("./plots/rho_stats.pdf", bbox_inches='tight') +plt.savefig("./plots/rho_stats.pdf", bbox_inches="tight") plt.show() # %% -rho_stats['theta'] +rho_stats["theta"] # %% diff --git a/papers/catalog/2025_09_19_tau_stats.py b/papers/catalog/2025_09_19_tau_stats.py index 6aa69112..b049d051 100644 --- a/papers/catalog/2025_09_19_tau_stats.py +++ b/papers/catalog/2025_09_19_tau_stats.py @@ -3,20 +3,18 @@ ipython = IPython.get_ipython() -import numpy as np -from astropy.io import fits import matplotlib.pyplot as plt +import numpy as np import seaborn as sns +from astropy.io import fits -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("colorblind") -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") @@ -35,13 +33,13 @@ cov_taus.append(np.load(f"{base_dir}/cov_tau_{ver}_th.npy")) samples.append(np.load(f"{base_dir}/samples_{ver}.npy")) -num_bins = tau_stats[0]['theta'].shape[0] +num_bins = tau_stats[0]["theta"].shape[0] # %% -e_obs = "e^\mathrm{obs}" -e_psf = "e^\mathrm{PSF}" -delta_e_psf = "\delta e^\mathrm{PSF}" -delta_T_psf = "\delta T^\mathrm{PSF}" +e_obs = r"e^\mathrm{obs}" +e_psf = r"e^\mathrm{PSF}" +delta_e_psf = r"\delta e^\mathrm{PSF}" +delta_T_psf = r"\delta T^\mathrm{PSF}" titles = [ rf"$\langle {e_obs} {e_psf} \rangle$", @@ -56,9 +54,9 @@ ] dict_index_tau = { - 0: '0', - 1: '2', - 2: '5', + 0: "0", + 1: "2", + 2: "5", } fig, axs = plt.subplots(1, 3, figsize=(12, 4), sharex=True) @@ -67,36 +65,43 @@ for i in range(3): for j in range(len(versions)): - y_plot = np.copy(tau_stats[j][f'tau_{dict_index_tau[i]}_p']) - yerr_plot = np.copy(np.sqrt(cov_taus[j][i*num_bins:(i+1)*num_bins, i*num_bins:(i+1)*num_bins].diagonal())) - if i>0: - y_plot *= tau_stats[j]['theta'] - yerr_plot *= tau_stats[j]['theta'] + y_plot = np.copy(tau_stats[j][f"tau_{dict_index_tau[i]}_p"]) + yerr_plot = np.copy( + np.sqrt( + cov_taus[j][ + i * num_bins : (i + 1) * num_bins, i * num_bins : (i + 1) * num_bins + ].diagonal() + ) + ) + if i > 0: + y_plot *= tau_stats[j]["theta"] + yerr_plot *= tau_stats[j]["theta"] axs[i].errorbar( - tau_stats[j]['theta'], + tau_stats[j]["theta"], y_plot, yerr=yerr_plot, - fmt='o', - linestyle='-', + fmt="o", + linestyle="-", label=labels[j], color=colors[j], capsize=2, ) axs[i].set_xlim(1.0, 250.0) - axs[i].set_xscale('log') + axs[i].set_xscale("log") axs[i].set_xlabel(r"$\vartheta$ [arcmin]") axs[i].set_ylabel(y_labels[i]) - axs[i].axhline(0, color='black', linestyle='--', linewidth=1) + axs[i].axhline(0, color="black", linestyle="--", linewidth=1) handles, labels = axs[1].get_legend_handles_labels() fig.legend( - handles, labels, + handles, + labels, loc="upper center", - bbox_to_anchor=(0.5, 0.05), # centered under all axes + bbox_to_anchor=(0.5, 0.05), # centered under all axes ncol=2, frameon=False, - fontsize=16 + fontsize=16, ) fig.canvas.draw() @@ -105,10 +110,10 @@ for i in range(3): text_offset_axis = axs[i].yaxis.get_offset_text().get_text() axs[i].yaxis.offsetText.set_visible(False) - axs[i].set_ylabel(y_labels[i]+text_offset_axis) + axs[i].set_ylabel(y_labels[i] + text_offset_axis) plt.tight_layout() -plt.savefig("./plots/tau_stats.pdf", bbox_inches='tight') +plt.savefig("./plots/tau_stats.pdf", bbox_inches="tight") plt.show() # %% diff --git a/papers/catalog/2025_10_02_xi_sys_rho_tau.py b/papers/catalog/2025_10_02_xi_sys_rho_tau.py index 7bd4e308..ab95d69c 100644 --- a/papers/catalog/2025_10_02_xi_sys_rho_tau.py +++ b/papers/catalog/2025_10_02_xi_sys_rho_tau.py @@ -4,25 +4,22 @@ ipython = IPython.get_ipython() import os -import numpy as np -from astropy.io import fits + import matplotlib.pyplot as plt import matplotlib.ticker as mticker - +import numpy as np import seaborn as sns -from getdist import plots, MCSamples - -from shear_psf_leakage.rho_tau_stat import RhoStat, TauStat, PSFErrorFit +from astropy.io import fits +from getdist import MCSamples, plots +from shear_psf_leakage.rho_tau_stat import PSFErrorFit, RhoStat, TauStat -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("colorblind") -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") @@ -42,17 +39,17 @@ cov_taus.append(np.load(f"{base_dir}/cov_tau_{ver}_th.npy")) samples.append(np.load(f"{base_dir}/samples_{ver}.npy")) -num_bins = tau_stats[0]['theta'].shape[0] +num_bins = tau_stats[0]["theta"].shape[0] # %% -#Create chains object for getdist +# Create chains object for getdist chains = [] for i, ver in enumerate(versions): chains.append( MCSamples( samples=samples[i], - names=[r'\alpha', r'\beta', r'\eta'], - labels=[r'\alpha', r'\beta', r'\eta'], + names=[r"\alpha", r"\beta", r"\eta"], + labels=[r"\alpha", r"\beta", r"\eta"], label=labels[i], ) ) @@ -65,56 +62,60 @@ r"\beta": 1.0, r"\eta": 1.0, } -line_args = [ - {'color': colors[i]} for i in range(len(versions)) -] -g.triangle_plot(chains, filled=True, legend_labels=labels, legend_loc="upper right", line_args=line_args, contour_colors=colors, markers=markers) +line_args = [{"color": colors[i]} for i in range(len(versions))] +g.triangle_plot( + chains, + filled=True, + legend_labels=labels, + legend_loc="upper right", + line_args=line_args, + contour_colors=colors, + markers=markers, +) g.export("./plots/psf_leakage_params.pdf") # %% -#Inference of the xi_sys parameters -sep_units = 'arcmin' -coord_units = 'degrees' +# Inference of the xi_sys parameters +sep_units = "arcmin" +coord_units = "degrees" theta_min = 1.0 theta_max = 250.0 nbins = 20 TreeCorrConfig_xi = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'jackknife', + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, + "var_method": "jackknife", } -rho_stats_handler = RhoStat( - output='.', - treecorr_config=TreeCorrConfig_xi, - verbose=True -) +rho_stats_handler = RhoStat(output=".", treecorr_config=TreeCorrConfig_xi, verbose=True) tau_stats_handler = TauStat( catalogs=rho_stats_handler.catalogs, - output='.', + output=".", treecorr_config=TreeCorrConfig_xi, - verbose=True - ) + verbose=True, +) psf_fitter = PSFErrorFit(rho_stats_handler, tau_stats_handler, base_dir) # %% -markers = ['o', 'h', 'x', 's', 'D', '^'] +markers = ["o", "h", "x", "s", "D", "^"] plt.figure() offset = 0.02 -for idx, (ver, color, marker, sample) in enumerate(zip(versions, colors, markers, samples)): +for idx, (ver, color, marker, sample) in enumerate( + zip(versions, colors, markers, samples) +): print("Plotting verion:", ver) - psf_fitter.load_rho_stat('rho_stats_' + ver + '.fits') + psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 quantiles = [1 - quant, quant] xi_psf_sys_samples = np.array([]).reshape(0, num_bins) @@ -124,15 +125,22 @@ xi_psf_sys_mean = np.mean(xi_psf_sys_samples, axis=0) xi_psf_sys_quant = np.quantile(xi_psf_sys_samples, quantiles, axis=0) - theta = psf_fitter.rho_stat_handler.rho_stats['theta'] - - if os.path.exists(base_dir+f'/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt'): - xip = np.loadtxt(base_dir+f'/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt', usecols=3)[:nbins] ## This will need modifications as the pipeline evolved + theta = psf_fitter.rho_stat_handler.rho_stats["theta"] + + if os.path.exists( + base_dir + + f"/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt" + ): + xip = np.loadtxt( + base_dir + + f"/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt", + usecols=3, + )[:nbins] ## This will need modifications as the pipeline evolved ratio_mean = xi_psf_sys_mean / xip ratio_quant = xi_psf_sys_quant / xip - jittered_theta = theta * (1+idx * offset) + jittered_theta = theta * (1 + idx * offset) plt.errorbar( jittered_theta, @@ -141,7 +149,7 @@ fmt=marker, color=color, label=labels[idx], - capsize=5 + capsize=5, ) threshold = 0.10 @@ -149,29 +157,19 @@ [theta_min, theta_max], -threshold, threshold, - color='black', + color="black", alpha=0.1, - label=fr"${threshold*100}\%$ threshold" -) -plt.plot( - [theta_min, theta_max], - [threshold, threshold], - color='black', - ls='--' -) -plt.plot( - [theta_min, theta_max], - [-threshold, -threshold], - color='black', - ls='--' + label=rf"${threshold * 100}\%$ threshold", ) +plt.plot([theta_min, theta_max], [threshold, threshold], color="black", ls="--") +plt.plot([theta_min, theta_max], [-threshold, -threshold], color="black", ls="--") -plt.xscale('log') -plt.xlabel(r'$\vartheta\ [\mathrm{arcmin}]$') -plt.ylabel(r'$[\xi_\mathrm{sys}/\xi_+](\vartheta)$') +plt.xscale("log") +plt.xlabel(r"$\vartheta\ [\mathrm{arcmin}]$") +plt.ylabel(r"$[\xi_\mathrm{sys}/\xi_+](\vartheta)$") plt.gca().yaxis.set_major_formatter(mticker.PercentFormatter(xmax=1)) plt.legend(fontsize=12) -plt.savefig('./plots/xi_sys_over_xi_plus.pdf') +plt.savefig("./plots/xi_sys_over_xi_plus.pdf") plt.show() @@ -189,17 +187,17 @@ cov_taus.append(np.load(f"{base_dir}/cov_tau_{ver}_th.npy")) samples.append(np.load(f"{base_dir}/samples_{ver}.npy")) -num_bins = tau_stats[0]['theta'].shape[0] +num_bins = tau_stats[0]["theta"].shape[0] # %% -#Create chains object for getdist +# Create chains object for getdist chains = [] for i, ver in enumerate(versions): chains.append( MCSamples( samples=samples[i], - names=[r'\alpha', r'\beta', r'\eta'], - labels=[r'\alpha', r'\beta', r'\eta'], + names=[r"\alpha", r"\beta", r"\eta"], + labels=[r"\alpha", r"\beta", r"\eta"], label=labels[i], ) ) @@ -212,56 +210,60 @@ r"\beta": 1.0, r"\eta": 1.0, } -line_args = [ - {'color': colors[i]} for i in range(len(versions)) -] -g.triangle_plot(chains, filled=True, legend_labels=labels, legend_loc="upper right", line_args=line_args, contour_colors=colors, markers=markers) +line_args = [{"color": colors[i]} for i in range(len(versions))] +g.triangle_plot( + chains, + filled=True, + legend_labels=labels, + legend_loc="upper right", + line_args=line_args, + contour_colors=colors, + markers=markers, +) g.export("./plots/psf_leakage_params.pdf") # %% -#Inference of the xi_sys parameters -sep_units = 'arcmin' -coord_units = 'degrees' +# Inference of the xi_sys parameters +sep_units = "arcmin" +coord_units = "degrees" theta_min = 1.0 theta_max = 250.0 nbins = 20 TreeCorrConfig_xi = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'jackknife', + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, + "var_method": "jackknife", } -rho_stats_handler = RhoStat( - output='.', - treecorr_config=TreeCorrConfig_xi, - verbose=True -) +rho_stats_handler = RhoStat(output=".", treecorr_config=TreeCorrConfig_xi, verbose=True) tau_stats_handler = TauStat( catalogs=rho_stats_handler.catalogs, - output='.', + output=".", treecorr_config=TreeCorrConfig_xi, - verbose=True - ) + verbose=True, +) psf_fitter = PSFErrorFit(rho_stats_handler, tau_stats_handler, base_dir) # %% -markers = ['o', 'h', 'x'] +markers = ["o", "h", "x"] plt.figure() offset = 0.02 -for idx, (ver, color, marker, sample) in enumerate(zip(versions, colors, markers, samples)): +for idx, (ver, color, marker, sample) in enumerate( + zip(versions, colors, markers, samples) +): print("Plotting verion:", ver) - psf_fitter.load_rho_stat('rho_stats_' + ver + '.fits') + psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 quantiles = [1 - quant, quant] xi_psf_sys_samples = np.array([]).reshape(0, num_bins) @@ -271,15 +273,22 @@ xi_psf_sys_mean = np.mean(xi_psf_sys_samples, axis=0) xi_psf_sys_quant = np.quantile(xi_psf_sys_samples, quantiles, axis=0) - theta = psf_fitter.rho_stat_handler.rho_stats['theta'] - - if os.path.exists(base_dir+f'/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt'): - xip = np.loadtxt(base_dir+f'/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt', usecols=3)[:nbins] ## This will need modifications as the pipeline evolved + theta = psf_fitter.rho_stat_handler.rho_stats["theta"] + + if os.path.exists( + base_dir + + f"/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt" + ): + xip = np.loadtxt( + base_dir + + f"/../{ver}_xi_minsep={theta_min}_maxsep={theta_max}_nbins={nbins}_npatch=1.txt", + usecols=3, + )[:nbins] ## This will need modifications as the pipeline evolved ratio_mean = xi_psf_sys_mean / xip ratio_quant = xi_psf_sys_quant / xip - jittered_theta = theta * (1+idx * offset) + jittered_theta = theta * (1 + idx * offset) plt.errorbar( jittered_theta, @@ -288,7 +297,7 @@ fmt=marker, color=color, label=labels[idx], - capsize=5 + capsize=5, ) threshold = 0.10 @@ -296,29 +305,19 @@ [theta_min, theta_max], -threshold, threshold, - color='black', + color="black", alpha=0.1, - label=r"$5\%$ threshold" -) -plt.plot( - [theta_min, theta_max], - [threshold, threshold], - color='black', - ls='--' -) -plt.plot( - [theta_min, theta_max], - [-threshold, -threshold], - color='black', - ls='--' + label=r"$5\%$ threshold", ) +plt.plot([theta_min, theta_max], [threshold, threshold], color="black", ls="--") +plt.plot([theta_min, theta_max], [-threshold, -threshold], color="black", ls="--") -plt.xscale('log') -plt.xlabel(r'$\vartheta\ [\mathrm{arcmin}]$') -plt.ylabel(r'$[\xi_\mathrm{sys}/\xi_+](\vartheta)$') +plt.xscale("log") +plt.xlabel(r"$\vartheta\ [\mathrm{arcmin}]$") +plt.ylabel(r"$[\xi_\mathrm{sys}/\xi_+](\vartheta)$") plt.gca().yaxis.set_major_formatter(mticker.PercentFormatter(xmax=1)) plt.legend() -plt.savefig('./plots/xi_sys_over_xi_plus_leak_corr.pdf') +plt.savefig("./plots/xi_sys_over_xi_plus_leak_corr.pdf") plt.show() diff --git a/papers/catalog/2025_10_03_scale_dependent_leakage.py b/papers/catalog/2025_10_03_scale_dependent_leakage.py index 82660ac8..4700ab4b 100644 --- a/papers/catalog/2025_10_03_scale_dependent_leakage.py +++ b/papers/catalog/2025_10_03_scale_dependent_leakage.py @@ -8,23 +8,18 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") -import numpy as np -import statsmodels.api as sm -from astropy.io import fits import matplotlib.pyplot as plt +import numpy as np import seaborn as sns -import pandas as pd - -from shear_psf_leakage.rho_tau_stat import RhoStat, TauStat, PSFErrorFit +from astropy.io import fits +from shear_psf_leakage.rho_tau_stat import PSFErrorFit, RhoStat, TauStat -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("colorblind") -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") @@ -55,48 +50,52 @@ alpha_leakages_err = [] for i, ver in enumerate(versions): - alpha = tau_stats[i]['tau_0_p'] / rho_stats[i]['rho_0_p'] + alpha = tau_stats[i]["tau_0_p"] / rho_stats[i]["rho_0_p"] alpha_leakages.append(alpha) - + var_tau = np.diag(cov_taus[i])[:num_bins] var_rho = np.diag(cov_rhos[i])[:num_bins] - var_alpha = (var_tau / rho_stats[i]['rho_0_p']**2) + (tau_stats[i]['tau_0_p']**2 * var_rho / rho_stats[i]['rho_0_p']**4) + var_alpha = (var_tau / rho_stats[i]["rho_0_p"] ** 2) + ( + tau_stats[i]["tau_0_p"] ** 2 * var_rho / rho_stats[i]["rho_0_p"] ** 4 + ) sigma_alpha = np.sqrt(var_alpha) alpha_leakages_err.append(sigma_alpha) # %% plt.figure() -for i, (ver, color, label, alpha, alpha_err) in enumerate(zip(versions, colors, labels, alpha_leakages, alpha_leakages_err)): +for i, (ver, color, label, alpha, alpha_err) in enumerate( + zip(versions, colors, labels, alpha_leakages, alpha_leakages_err) +): plt.errorbar( - tau_stats[i]['theta'], + tau_stats[i]["theta"], alpha, yerr=alpha_err, - fmt='o', + fmt="o", color=color, label=label, - capsize= 5 + capsize=5, ) plt.xlabel(r"$\vartheta$ [arcmin]") plt.ylabel(r"$\alpha(\vartheta)$") -plt.xscale('log') +plt.xscale("log") plt.legend() -plt.savefig('./plots/scale_dependent_alpha.pdf') +plt.savefig("./plots/scale_dependent_alpha.pdf") plt.show() # %% -#Check the computation of error bars are correct. +# Check the computation of error bars are correct. tau_stats_sample = np.random.normal( - loc=tau_stats[0]['tau_0_p'], + loc=tau_stats[0]["tau_0_p"], scale=np.sqrt(np.diag(cov_taus[0])[:num_bins]), - size=(10_000, num_bins) + size=(10_000, num_bins), ) rho_stats_sample = np.random.normal( - loc=rho_stats[0]['rho_0_p'], + loc=rho_stats[0]["rho_0_p"], scale=np.sqrt(np.diag(cov_rhos[0])[:num_bins]), - size=(10_000, num_bins) + size=(10_000, num_bins), ) alpha_samples = tau_stats_sample / rho_stats_sample @@ -106,30 +105,29 @@ plt.figure() plt.plot(sigma_alpha) -plt.plot(alpha_leakages_err[0], linestyle='--') +plt.plot(alpha_leakages_err[0], linestyle="--") plt.show() # %% -#Plot scale dependent xi_sys +# Plot scale dependent xi_sys xi_sys = [] xi_sys_err = [] for i, ver in enumerate(versions): - - xi_sys_i = alpha_leakages[i]**2 * rho_stats[i]['rho_0_p'] + xi_sys_i = alpha_leakages[i] ** 2 * rho_stats[i]["rho_0_p"] xi_sys.append(xi_sys_i) rho_stats_sample = np.random.normal( - loc=rho_stats[i]['rho_0_p'], + loc=rho_stats[i]["rho_0_p"], scale=np.sqrt(np.diag(cov_rhos[i])[:num_bins]), - size=(10_000, num_bins) + size=(10_000, num_bins), ) tau_stats_sample = np.random.normal( - loc=tau_stats[i]['tau_0_p'], + loc=tau_stats[i]["tau_0_p"], scale=np.sqrt(np.diag(cov_taus[i])[:num_bins]), - size=(10_000, num_bins) + size=(10_000, num_bins), ) xi_sys_samples = tau_stats_sample**2 / rho_stats_sample @@ -140,60 +138,58 @@ # %% plt.figure() -for i, (ver, color, label, xi_sys_i, xi_sys_err_i) in enumerate(zip(versions, colors, labels, xi_sys, xi_sys_err)): +for i, (ver, color, label, xi_sys_i, xi_sys_err_i) in enumerate( + zip(versions, colors, labels, xi_sys, xi_sys_err) +): plt.errorbar( - tau_stats[i]['theta'], + tau_stats[i]["theta"], xi_sys_i, yerr=xi_sys_err_i, - fmt='o', + fmt="o", color=color, label=label, - capsize= 5 + capsize=5, ) plt.xlabel(r"$\vartheta$ [arcmin]") plt.ylabel(r"$\xi^\mathrm{sys}_+(\vartheta)$") -plt.xscale('log') -plt.yscale('log') +plt.xscale("log") +plt.yscale("log") plt.legend() -plt.savefig('./plots/scale_dependent_xi_sys.pdf') +plt.savefig("./plots/scale_dependent_xi_sys.pdf") plt.show() # %% -#Comparison with rho/tau predictions +# Comparison with rho/tau predictions -#Inference of the xi_sys parameters -sep_units = 'arcmin' -coord_units = 'degrees' +# Inference of the xi_sys parameters +sep_units = "arcmin" +coord_units = "degrees" theta_min = 0.1 theta_max = 250 nbins = 20 TreeCorrConfig_xi = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'jackknife', + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, + "var_method": "jackknife", } -rho_stats_handler = RhoStat( - output='.', - treecorr_config=TreeCorrConfig_xi, - verbose=True -) +rho_stats_handler = RhoStat(output=".", treecorr_config=TreeCorrConfig_xi, verbose=True) tau_stats_handler = TauStat( catalogs=rho_stats_handler.catalogs, - output='.', + output=".", treecorr_config=TreeCorrConfig_xi, - verbose=True - ) + verbose=True, +) psf_fitter = PSFErrorFit(rho_stats_handler, tau_stats_handler, base_dir) @@ -206,7 +202,7 @@ sample = np.load(f"{base_dir}/samples_{ver}.npy") print("Plotting verion:", ver) -psf_fitter.load_rho_stat('rho_stats_' + ver + '.fits') +psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 quantiles = [1 - quant, quant] xi_psf_sys_samples = np.array([]).reshape(0, num_bins) @@ -218,35 +214,35 @@ xi_psf_sys_std = np.std(xi_psf_sys_samples, axis=0) plt.errorbar( - psf_fitter.rho_stat_handler.rho_stats['theta'], + psf_fitter.rho_stat_handler.rho_stats["theta"], xi_psf_sys_mean, yerr=xi_psf_sys_std, - fmt='o', + fmt="o", color=color, - ls='-', - label=labels[idx]+r' $\rho/\tau$ model', - capsize=5 + ls="-", + label=labels[idx] + r" $\rho/\tau$ model", + capsize=5, ) plt.errorbar( - tau_stats[idx]['theta'], + tau_stats[idx]["theta"], xi_sys[idx], yerr=xi_sys_err[idx], - fmt='o', - ls='--', - color='C3', - label=labels[idx]+r' direct $\xi^\mathrm{sys}_+$', - capsize=5 + fmt="o", + ls="--", + color="C3", + label=labels[idx] + r" direct $\xi^\mathrm{sys}_+$", + capsize=5, ) plt.xlabel(r"$\vartheta$ [arcmin]") plt.ylabel(r"$\xi^\mathrm{sys}_+(\vartheta)$") -plt.xscale('log') -plt.yscale('log') +plt.xscale("log") +plt.yscale("log") plt.legend() -plt.savefig('./plots/scale_dependent_xi_sys_comparison.pdf') +plt.savefig("./plots/scale_dependent_xi_sys_comparison.pdf") plt.show() - + # %% diff --git a/papers/catalog/2025_12_09_residual_star_plot.py b/papers/catalog/2025_12_09_residual_star_plot.py index 4b53e872..30085eef 100644 --- a/papers/catalog/2025_12_09_residual_star_plot.py +++ b/papers/catalog/2025_12_09_residual_star_plot.py @@ -3,21 +3,19 @@ ipython = IPython.get_ipython() +import matplotlib.pyplot as plt import numpy as np +import seaborn as sns from astropy.io import fits -import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec -import seaborn as sns -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("husl", 18) -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") @@ -29,19 +27,19 @@ cat_star = fits.getdata(path_star) # To be checked -e1_star = cat_star['E1_STAR_HSM'] -e2_star = cat_star['E2_STAR_HSM'] -T_star = cat_star['SIGMA_STAR_HSM']**2 -e1_psf = cat_star['E1_PSF_HSM'] -e2_psf = cat_star['E2_PSF_HSM'] -T_psf = cat_star['SIGMA_PSF_HSM']**2 -M_4_1_star = cat_star['M_4_STAR_1'] -M_4_2_star = cat_star['M_4_STAR_2'] -M_4_1_psf = cat_star['M_4_PSF_1'] -M_4_2_psf = cat_star['M_4_PSF_2'] +e1_star = cat_star["E1_STAR_HSM"] +e2_star = cat_star["E2_STAR_HSM"] +T_star = cat_star["SIGMA_STAR_HSM"] ** 2 +e1_psf = cat_star["E1_PSF_HSM"] +e2_psf = cat_star["E2_PSF_HSM"] +T_psf = cat_star["SIGMA_PSF_HSM"] ** 2 +M_4_1_star = cat_star["M_4_STAR_1"] +M_4_2_star = cat_star["M_4_STAR_2"] +M_4_1_psf = cat_star["M_4_PSF_1"] +M_4_2_psf = cat_star["M_4_PSF_2"] # %% -histtype = 'step' +histtype = "step" fig = plt.figure(figsize=(12, 8)) gs = GridSpec(3, 2, wspace=0.1, hspace=0.5) @@ -70,12 +68,12 @@ label=r"$\delta e_1$", ) ax1.text( - 0.05, 0.7, - f"Mean: {np.mean(res_e1):.2e}\n" - f"Std: {np.std(res_e1):.2e}", + 0.05, + 0.7, + f"Mean: {np.mean(res_e1):.2e}\nStd: {np.std(res_e1):.2e}", transform=ax1.transAxes, fontsize=18, - color='k' + color="k", ) ax2.hist( @@ -89,12 +87,12 @@ ) ax2.text( - 0.05, 0.7, - f"Mean: {np.mean(res_e2):.2e}\n" - f"Std: {np.std(res_e2):.2e}", + 0.05, + 0.7, + f"Mean: {np.mean(res_e2):.2e}\nStd: {np.std(res_e2):.2e}", transform=ax2.transAxes, fontsize=18, - color='k' + color="k", ) ax3.hist( @@ -107,12 +105,12 @@ label=r"$\delta r_{\rm hlr} / r_{\rm hlr}$", ) ax3.text( - 0.05, 0.7, - f"Mean: {np.mean(res_T):.2e}\n" - f"Std: {np.std(res_T):.2e}", + 0.05, + 0.7, + f"Mean: {np.mean(res_T):.2e}\nStd: {np.std(res_T):.2e}", transform=ax3.transAxes, fontsize=18, - color='k' + color="k", ) ax4.hist( @@ -126,12 +124,12 @@ ) ax4.text( - 0.05, 0.7, - f"Mean: {np.mean(res_M4_1[mask]):.2e}\n" - f"Std: {np.std(res_M4_1[mask]):.2e}", + 0.05, + 0.7, + f"Mean: {np.mean(res_M4_1[mask]):.2e}\nStd: {np.std(res_M4_1[mask]):.2e}", transform=ax4.transAxes, fontsize=18, - color='k' + color="k", ) ax5.hist( @@ -145,12 +143,12 @@ ) ax5.text( - 0.05, 0.7, - f"Mean: {np.mean(res_M4_2[mask]):.2e}\n" - f"Std: {np.std(res_M4_2[mask]):.2e}", + 0.05, + 0.7, + f"Mean: {np.mean(res_M4_2[mask]):.2e}\nStd: {np.std(res_M4_2[mask]):.2e}", transform=ax5.transAxes, fontsize=18, - color='k' + color="k", ) ax1.set_ylabel("Density", fontsize=20) @@ -162,21 +160,11 @@ ax4.minorticks_on() ax5.minorticks_on() -ax1.tick_params( - axis='both', which='both', labelsize=20 -) -ax2.tick_params( - axis='both', which='both', labelsize=20 -) -ax3.tick_params( - axis='both', which='both', labelsize=20 -) -ax4.tick_params( - axis='both', which='both', labelsize=20 -) -ax5.tick_params( - axis='both', which='both', labelsize=20 -) +ax1.tick_params(axis="both", which="both", labelsize=20) +ax2.tick_params(axis="both", which="both", labelsize=20) +ax3.tick_params(axis="both", which="both", labelsize=20) +ax4.tick_params(axis="both", which="both", labelsize=20) +ax5.tick_params(axis="both", which="both", labelsize=20) ax1.set_title(r"$\delta e_1$", fontsize=22) ax2.set_title(r"$\delta e_2$", fontsize=22) @@ -186,7 +174,7 @@ plt.tight_layout() -plt.savefig('./plots/residual_star_properties.pdf') +plt.savefig("./plots/residual_star_properties.pdf") plt.show() diff --git a/papers/catalog/2025_12_10_plot_maps.py b/papers/catalog/2025_12_10_plot_maps.py index 4af240b6..2ad0be7d 100644 --- a/papers/catalog/2025_12_10_plot_maps.py +++ b/papers/catalog/2025_12_10_plot_maps.py @@ -9,27 +9,19 @@ ipython = IPython.get_ipython() -import os -import numpy as np +import astropy.units as u +import bornraytrace.lensing as lensing import healpy as hp -from astropy.io import fits import matplotlib.pyplot as plt -import matplotlib.ticker as mticker -from matplotlib.ticker import ScalarFormatter -from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable +import numpy as np import seaborn as sns - import skymapper as skm from astropy.coordinates import SkyCoord -import astropy.units as u -from scipy.ndimage import gaussian_filter - -import glass.lensing -import bornraytrace.lensing as lensing +from astropy.io import fits +from matplotlib.ticker import ScalarFormatter +from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True plt.rcParams["font.size"] = 12 @@ -37,10 +29,11 @@ sns.set_palette("husl", 18) sns.color_palette("rocket", as_cmap=True) -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") + # %% def weighted_mean_per_pixel(pix, weights, values, nside): """ @@ -70,6 +63,7 @@ def weighted_mean_per_pixel(pix, weights, values, nside): result[valid] = v_sum[valid] / w_sum[valid] return result + def plot_galactic_planes(sky_map): """ Overlay galactic plane lines (b = 0°, ±10°) on a given sky map. @@ -81,16 +75,33 @@ def plot_galactic_planes(sky_map): """ planes = { r"$b=0^\circ$": {"l": np.linspace(-20, 270, 1000), "b": 0, "style": "solid"}, - r"$b=+10^\circ$": {"l": np.linspace(-20, 270, 1000), "b": 10, "style": "dashed"}, - r"$b=-10^\circ$": {"l": np.linspace(-20, 270, 1000), "b": -10, "style": "dashed"}, + r"$b=+10^\circ$": { + "l": np.linspace(-20, 270, 1000), + "b": 10, + "style": "dashed", + }, + r"$b=-10^\circ$": { + "l": np.linspace(-20, 270, 1000), + "b": -10, + "style": "dashed", + }, } for label, params in planes.items(): gal = SkyCoord(l=params["l"] * u.deg, b=params["b"] * u.deg, frame="galactic") eq = gal.transform_to("icrs") - sky_map.plot(eq.ra.deg, eq.dec.deg, color="gray", linestyle=params["style"], linewidth=1.2) + sky_map.plot( + eq.ra.deg, + eq.dec.deg, + color="gray", + linestyle=params["style"], + linewidth=1.2, + ) + -def plot_healpix_map(pix_vals, nside, map_style, label, output_name, show=False, cmap=None, mask=None): +def plot_healpix_map( + pix_vals, nside, map_style, label, output_name, show=False, cmap=None, mask=None +): """ Render and save a HEALPix map in an Albers equal-area projection. @@ -108,17 +119,15 @@ def plot_healpix_map(pix_vals, nside, map_style, label, output_name, show=False, Filename for the saved figure. """ ra, dec = hp.pix2ang(nside, np.arange(len(pix_vals)), nest=False, lonlat=True) - valid = pix_vals != 0 lon_0, lat_0, lat_1, lat_2 = 253, 40, 45, 49 proj = skm.Albers(lon_0, lat_0, lat_1, lat_2) - + fig = plt.figure(figsize=(24, 12)) - ax = fig.add_subplot(111, aspect=3.) + ax = fig.add_subplot(111, aspect=3.0) ax.set_xlabel("Right Ascension [deg]", fontsize=20) ax.yaxis.set_ticks([]) - - dec_ticks = np.arange(30, 61, 15) + sky_map = skm.Map(proj, ax=ax) plot_galactic_planes(sky_map) @@ -132,19 +141,21 @@ def plot_healpix_map(pix_vals, nside, map_style, label, output_name, show=False, masked_vals = np.ma.array(pix_vals, mask=mask) if cmap is None: cmap = plt.cm.coolwarm.copy() - cmap.set_under('lightgrey') - + cmap.set_under("lightgrey") + vmin, vmax = np.percentile(masked_vals[~masked_vals.mask], [1, 99]) - + mappable = sky_map.healpix( - masked_vals, color_percentiles=[2,98],cmap=cmap , vmin=vmin, vmax=vmax + masked_vals, color_percentiles=[2, 98], cmap=cmap, vmin=vmin, vmax=vmax ) divider = make_axes_locatable(ax) # 'size' can be a percent string or fraction, e.g. "5%" or 0.05 - cax = divider.append_axes("bottom", size="2%", pad=0.6) + cax = divider.append_axes("bottom", size="2%", pad=0.6) - cbar = fig.colorbar(mappable, cax=cax, orientation='horizontal', fraction=0.05, pad=0.1) + cbar = fig.colorbar( + mappable, cax=cax, orientation="horizontal", fraction=0.05, pad=0.1 + ) cbar.set_label(label, fontsize=14) cbar.ax.tick_params(labelsize=14) @@ -160,18 +171,19 @@ def plot_healpix_map(pix_vals, nside, map_style, label, output_name, show=False, sky_map.ylim(bottom=-0.2, top=1.38) sky_map.xlim(-1.29, 1.30) - + sky_map.fig.set_size_inches(12, 8) sky_map.grid(sep=15, lat_min=0, lat_max=360, lon_min=-179, lon_max=179) ax.text( - 0.39, 1.09, + 0.39, + 1.09, "Declination [deg]", fontsize=18, transform=ax.transAxes, - va="center" + va="center", ) - + for dec, positio in zip([30, 45, 60, 60, 45, 30], [12, 23, 36, 94, 107, 119]): ax.text( (positio - 7) / 120, @@ -179,19 +191,22 @@ def plot_healpix_map(pix_vals, nside, map_style, label, output_name, show=False, f"+{dec}°", fontsize=12, transform=ax.transAxes, - va="center" + va="center", ) ax.set_yticklabels([]) - ax.tick_params(axis='y', left=False, right=False) - ax.tick_params(axis='x', top=False) - + ax.tick_params(axis="y", left=False, right=False) + ax.tick_params(axis="x", top=False) + plt.savefig(output_name) if show: plt.show() plt.close() -def plot_healpix_map_ax(pix_vals, nside, map_style, label, fig, ax, cmap=None, mask=None): + +def plot_healpix_map_ax( + pix_vals, nside, map_style, label, fig, ax, cmap=None, mask=None +): """ Render and save a HEALPix map in an Albers equal-area projection. @@ -209,15 +224,13 @@ def plot_healpix_map_ax(pix_vals, nside, map_style, label, fig, ax, cmap=None, m Filename for the saved figure. """ ra, dec = hp.pix2ang(nside, np.arange(len(pix_vals)), nest=False, lonlat=True) - valid = pix_vals != 0 lon_0, lat_0, lat_1, lat_2 = 253, 40, 45, 49 proj = skm.Albers(lon_0, lat_0, lat_1, lat_2) - + ax.set_xlabel("Right Ascension [deg]", fontsize=12) ax.yaxis.set_ticks([]) - - dec_ticks = np.arange(30, 61, 15) + sky_map = skm.Map(proj, ax=ax) plot_galactic_planes(sky_map) @@ -231,20 +244,21 @@ def plot_healpix_map_ax(pix_vals, nside, map_style, label, fig, ax, cmap=None, m masked_vals = np.ma.array(pix_vals, mask=mask) if cmap is None: cmap = plt.cm.coolwarm.copy() - cmap.set_under('lightgrey') - + cmap.set_under("lightgrey") + vmin, vmax = np.percentile(masked_vals[~masked_vals.mask], [1, 99]) - + mappable = sky_map.healpix( - masked_vals, color_percentiles=[2,98],cmap=cmap , vmin=vmin, vmax=vmax + masked_vals, color_percentiles=[2, 98], cmap=cmap, vmin=vmin, vmax=vmax ) - divider = make_axes_locatable(ax) # 'size' can be a percent string or fraction, e.g. "5%" or 0.05 - cax = divider.append_axes("bottom", size="5%", pad=0.5) + cax = divider.append_axes("bottom", size="5%", pad=0.5) - cbar = fig.colorbar(mappable, cax=cax, orientation='horizontal', fraction=0.05, pad=0.1) + cbar = fig.colorbar( + mappable, cax=cax, orientation="horizontal", fraction=0.05, pad=0.1 + ) cbar.set_label(label, fontsize=14) cbar.ax.tick_params(labelsize=14) @@ -260,18 +274,19 @@ def plot_healpix_map_ax(pix_vals, nside, map_style, label, fig, ax, cmap=None, m sky_map.ylim(bottom=-0.2, top=1.38) sky_map.xlim(-1.29, 1.30) - + sky_map.fig.set_size_inches(12, 8) sky_map.grid(sep=15, lat_min=0, lat_max=360, lon_min=-179, lon_max=179) ax.text( - 0.35, 1.22, + 0.35, + 1.22, "Declination [deg]", fontsize=12, transform=ax.transAxes, - va="center" + va="center", ) - + for dec, positio in zip([30, 45, 60, 60, 45, 30], [12, 23, 36, 94, 107, 119]): ax.text( (positio - 7) / 120, @@ -279,38 +294,41 @@ def plot_healpix_map_ax(pix_vals, nside, map_style, label, fig, ax, cmap=None, m f"+{dec}°", fontsize=12, transform=ax.transAxes, - va="center" + va="center", ) ax.set_yticklabels([]) - ax.tick_params(axis='y', left=False, right=False) - ax.tick_params(axis='x', top=True) - ax.tick_params(axis='both', which='major', labelsize=12, top=True) + ax.tick_params(axis="y", left=False, right=False) + ax.tick_params(axis="x", top=True) + ax.tick_params(axis="both", which="major", labelsize=12, top=True) return ax + def get_kappa_kaiser_squire(gamma_1, gamma_2): gamma_map = gamma_1 + 1j * gamma_2 nside = hp.npix2nside(len(gamma_map)) - kappa_map = lensing.shear2kappa(gamma_map, lmax=2*nside) + kappa_map = lensing.shear2kappa(gamma_map, lmax=2 * nside) return -kappa_map.real, -kappa_map.imag # %% -path_cat_gal = "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" +path_cat_gal = ( + "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" +) path_cat_star = "/n17data/UNIONS/WL/v1.4.x/full_starcat-0000000.fits" cat_gal = fits.getdata(path_cat_gal) cat_star = fits.getdata(path_cat_star) # %% -#Plot maps of e1, e2 and N_eff +# Plot maps of e1, e2 and N_eff nside = 1024 -mask = cat_gal["Dec"] > 20 #thrwowing out cosmos -ra_gal, dec_gal = cat_gal['RA'][mask], cat_gal['DEC'][mask] -weights_gal = cat_gal['w_des'][mask] -e1_gal, e2_gal = cat_gal['e1'][mask], cat_gal['e2'][mask] +mask = cat_gal["Dec"] > 20 # thrwowing out cosmos +ra_gal, dec_gal = cat_gal["RA"][mask], cat_gal["DEC"][mask] +weights_gal = cat_gal["w_des"][mask] +e1_gal, e2_gal = cat_gal["e1"][mask], cat_gal["e2"][mask] pix = hp.ang2pix(nside, ra_gal, dec_gal, lonlat=True) @@ -338,7 +356,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$\gamma_1$", output_name=output_path_e1, show=True, - mask=mask_footprint + mask=mask_footprint, ) output_path_e2 = "./plots/sky_map_e2_healpix.pdf" @@ -350,7 +368,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$\gamma_2$", output_name=output_path_e2, show=True, - mask=mask_footprint + mask=mask_footprint, ) output_path_kappaE = "./plots/sky_map_kappaE_healpix.pdf" @@ -363,7 +381,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name=output_path_kappaE, show=True, cmap=plt.cm.magma.copy(), - mask=mask_footprint + mask=mask_footprint, ) output_path_kappaB = "./plots/sky_map_kappaB_healpix.pdf" @@ -377,18 +395,31 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name=output_path_kappaB, show=True, cmap=plt.cm.magma.copy(), - mask=mask_footprint + mask=mask_footprint, ) # %% plt.figure() -plt.hist(kappa_E_map[~mask_footprint], bins=1000, density=True, histtype='step', label=r"$\kappa_E$") +plt.hist( + kappa_E_map[~mask_footprint], + bins=1000, + density=True, + histtype="step", + label=r"$\kappa_E$", +) plt.show() # %% -hp.mollview(kappa_E_map, title=r"$\kappa_E$ map", unit=r"$\kappa_E$", cmap="coolwarm", min=-0.02, max=0.02) +hp.mollview( + kappa_E_map, + title=r"$\kappa_E$ map", + unit=r"$\kappa_E$", + cmap="coolwarm", + min=-0.02, + max=0.02, +) plt.show() # %% @@ -401,7 +432,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$\gamma_1$", fig=fig, ax=ax1, - mask=mask_footprint + mask=mask_footprint, ) plot_healpix_map_ax( @@ -411,7 +442,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$\gamma_2$", fig=fig, ax=ax2, - mask=mask_footprint + mask=mask_footprint, ) plot_healpix_map_ax( @@ -422,18 +453,17 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): fig=fig, ax=ax3, cmap=plt.cm.magma.copy(), - mask=mask_footprint + mask=mask_footprint, ) plt.savefig("./plots/sky_maps_e1_e2_kappaE_healpix.pdf", dpi=600) plt.show() - # %% # Plot number density map -#Get effective number density map +# Get effective number density map nside = 256 pix = hp.ang2pix(nside, ra_gal, dec_gal, lonlat=True) @@ -443,7 +473,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): valid = w_sum > 0 n_eff = np.zeros(npix) -n_eff[valid] = (w_sum[valid])**2 / w2_sum[valid] +n_eff[valid] = (w_sum[valid]) ** 2 / w2_sum[valid] pix_area_arcmin2 = hp.nside2pixarea(nside, degrees=True) * (60**2) print(pix_area_arcmin2) pix_vals_n_eff = n_eff / pix_area_arcmin2 @@ -456,7 +486,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name="./plots/sky_map_n_eff_healpix.pdf", show=True, cmap=sns.cubehelix_palette(as_cmap=True), - mask=~valid + mask=~valid, ) # %% @@ -466,10 +496,12 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): shape_noise_map = np.zeros(npix) shape_noise_map[unique_pix] = np.bincount( idx_rep, - weights= weights_gal**2 * (e1_gal**2 + e2_gal**2), + weights=weights_gal**2 * (e1_gal**2 + e2_gal**2), ) mask_shape_noise = shape_noise_map == 0 -shape_noise_map[~mask_shape_noise] = np.sqrt(1/2 * shape_noise_map[~mask_shape_noise] / w2_sum[~mask_shape_noise]) +shape_noise_map[~mask_shape_noise] = np.sqrt( + 1 / 2 * shape_noise_map[~mask_shape_noise] / w2_sum[~mask_shape_noise] +) plot_healpix_map( shape_noise_map, @@ -479,21 +511,23 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name="./plots/sky_map_shape_noise_healpix.pdf", show=True, cmap=sns.color_palette("crest", as_cmap=True), - mask=mask_shape_noise + mask=mask_shape_noise, ) # %% # GLASS mock maps -path_glass_mock = "/n09data/guerrini/glass_mock/results/unions_glass_sim_00109_4096.fits" +path_glass_mock = ( + "/n09data/guerrini/glass_mock/results/unions_glass_sim_00109_4096.fits" +) cat_glass = fits.getdata(path_glass_mock) nside = 1024 -mask = cat_glass["Dec"] > 20 #thrwowing out cosmos -ra_glass, dec_glass = cat_glass['RA'][mask], cat_glass['DEC'][mask] -weights_glass = cat_glass['w'][mask] -e1_glass, e2_glass = cat_glass['e1'][mask], cat_glass['e2'][mask] +mask = cat_glass["Dec"] > 20 # thrwowing out cosmos +ra_glass, dec_glass = cat_glass["RA"][mask], cat_glass["DEC"][mask] +weights_glass = cat_glass["w"][mask] +e1_glass, e2_glass = cat_glass["e1"][mask], cat_glass["e2"][mask] pix = hp.ang2pix(nside, ra_glass, dec_glass, lonlat=True) @@ -521,7 +555,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$\gamma_1$", output_name=output_path_e1, show=True, - mask=mask_footprint + mask=mask_footprint, ) output_path_e2 = "./plots/sky_map_e2_glass_healpix.pdf" @@ -533,7 +567,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$\gamma_2$", output_name=output_path_e2, show=True, - mask=mask_footprint + mask=mask_footprint, ) output_path_kappaE = "./plots/sky_map_kappaE_glass_healpix.pdf" @@ -546,7 +580,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name=output_path_kappaE, show=True, cmap=plt.cm.magma.copy(), - mask=mask_footprint + mask=mask_footprint, ) output_path_kappaB = "./plots/sky_map_kappaB_glass_healpix.pdf" @@ -560,14 +594,14 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name=output_path_kappaB, show=True, cmap=plt.cm.magma.copy(), - mask=mask_footprint + mask=mask_footprint, ) # %% # Maps of star/PSF information -ra_star, dec_star = cat_star['RA'], cat_star['DEC'] -e1_star, e2_star = cat_star['E1_STAR_HSM'], cat_star['E2_STAR_HSM'] -e1_psf, e2_psf = cat_star['E1_PSF_HSM'], cat_star['E2_PSF_HSM'] -t_star, t_psf = cat_star['SIGMA_STAR_HSM']**2, cat_star['SIGMA_PSF_HSM']**2 +ra_star, dec_star = cat_star["RA"], cat_star["DEC"] +e1_star, e2_star = cat_star["E1_STAR_HSM"], cat_star["E2_STAR_HSM"] +e1_psf, e2_psf = cat_star["E1_PSF_HSM"], cat_star["E2_PSF_HSM"] +t_star, t_psf = cat_star["SIGMA_STAR_HSM"] ** 2, cat_star["SIGMA_PSF_HSM"] ** 2 weights_star = np.ones_like(ra_star) # %% @@ -589,12 +623,16 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): output_name="./plots/sky_map_n_eff_stars_healpix.pdf", show=True, cmap=sns.cubehelix_palette(as_cmap=True), - mask=~valid_star + mask=~valid_star, ) # %% -pix_vals_dela_e1 = weighted_mean_per_pixel(pix_star, weights_star, e1_star - e1_psf, nside) -pix_vals_dela_e2 = weighted_mean_per_pixel(pix_star, weights_star, e2_star - e2_psf, nside) +pix_vals_dela_e1 = weighted_mean_per_pixel( + pix_star, weights_star, e1_star - e1_psf, nside +) +pix_vals_dela_e2 = weighted_mean_per_pixel( + pix_star, weights_star, e2_star - e2_psf, nside +) plot_healpix_map( pix_vals_dela_e1, @@ -603,7 +641,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$e_1^{\mathrm{star}} - e_1^{\mathrm{PSF}}$", output_name="./plots/sky_map_delta_e1_star_psf_healpix.pdf", show=True, - mask=pix_vals_dela_e1 == 0 + mask=pix_vals_dela_e1 == 0, ) plot_healpix_map( @@ -613,10 +651,12 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$e_2^{\mathrm{star}} - e_2^{\mathrm{PSF}}$", output_name="./plots/sky_map_delta_e2_star_psf_healpix.pdf", show=True, - mask=pix_vals_dela_e2 == 0 + mask=pix_vals_dela_e2 == 0, ) # %% -pix_vals_delta_t = weighted_mean_per_pixel(pix_star, weights_star, (t_star - t_psf)/t_star, nside) +pix_vals_delta_t = weighted_mean_per_pixel( + pix_star, weights_star, (t_star - t_psf) / t_star, nside +) plot_healpix_map( pix_vals_delta_t, @@ -625,7 +665,7 @@ def get_kappa_kaiser_squire(gamma_1, gamma_2): label=r"$(T^{\mathrm{star}} - T^{\mathrm{PSF}}) / T^{\mathrm{star}}$", output_name="./plots/sky_map_delta_t_star_psf_healpix.pdf", show=True, - mask=pix_vals_delta_t == 0 + mask=pix_vals_delta_t == 0, ) # %% diff --git a/papers/catalog/2025_12_11_histograms_gal_properties.py b/papers/catalog/2025_12_11_histograms_gal_properties.py index c05a6374..b73ad3ca 100644 --- a/papers/catalog/2025_12_11_histograms_gal_properties.py +++ b/papers/catalog/2025_12_11_histograms_gal_properties.py @@ -4,37 +4,35 @@ ipython = IPython.get_ipython() if ipython is not None: - ipython.run_line_magic("load_ext", "autoreload") - ipython.run_line_magic("autoreload", "2") + ipython.run_line_magic("load_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") -import os -from pathlib import Path import h5py - import matplotlib.pyplot as plt import numpy as np -import pandas as pd import seaborn as sns from astropy.io import fits -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("husl") if ipython is not None: - ipython.run_line_magic("matplotlib", "inline") + ipython.run_line_magic("matplotlib", "inline") # %% -path_cat_v146 = "/n17data/UNIONS/WL/v1.4.x/v1.4.6/unions_shapepipe_cut_struc_2024_v1.4.6.fits" -path_cat_comprehensive = "/n17data/UNIONS/WL/v1.4.x/unions_shapepipe_comprehensive_struc_2024_v1.X.c.hdf5" +path_cat_v146 = ( + "/n17data/UNIONS/WL/v1.4.x/v1.4.6/unions_shapepipe_cut_struc_2024_v1.4.6.fits" +) +path_cat_comprehensive = ( + "/n17data/UNIONS/WL/v1.4.x/unions_shapepipe_comprehensive_struc_2024_v1.X.c.hdf5" +) # %% # Star with plot with the sample v1.4.6 -#Load catalog +# Load catalog cat_v146 = fits.open(path_cat_v146)[1].data # %% @@ -42,17 +40,61 @@ plt_diagonal = True plt.figure() -plt.hist(cat_v146['R_g11'], bins=50, histtype='step', range=(-2.5, 2.5), density=True, color='C0', label=r"$R_{11}$") -plt.hist(cat_v146['R_g22'], bins=50, histtype='step', range=(-2.5, 2.5), density=True, color='C1', label=r"$R_{22}$") +plt.hist( + cat_v146["R_g11"], + bins=50, + histtype="step", + range=(-2.5, 2.5), + density=True, + color="C0", + label=r"$R_{11}$", +) +plt.hist( + cat_v146["R_g22"], + bins=50, + histtype="step", + range=(-2.5, 2.5), + density=True, + color="C1", + label=r"$R_{22}$", +) if plt_diagonal: - plt.hist(cat_v146['R_g12'], bins=50, histtype='step', range=(-2.5, 2.5), density=True, color='C2', label=r"$R_{12}$") - plt.hist(cat_v146['R_g21'], bins=50, histtype='step', range=(-2.5, 2.5), density=True, color='C3', label=r"$R_{21}$") - -plt.axvline(0, color='k', linestyle='--', alpha=0.7) - -plt.axvline(np.mean(cat_v146['R_g11']), color='C0', linestyle=':', alpha=0.7, label=rf"$\langle R_{11} \rangle = {np.mean(cat_v146['R_g11']):.2f}$") -plt.axvline(np.mean(cat_v146['R_g22']), color='C1', linestyle=':', alpha=0.7, label=rf"$\langle R_{22} \rangle = {np.mean(cat_v146['R_g22']):.2f}$") + plt.hist( + cat_v146["R_g12"], + bins=50, + histtype="step", + range=(-2.5, 2.5), + density=True, + color="C2", + label=r"$R_{12}$", + ) + plt.hist( + cat_v146["R_g21"], + bins=50, + histtype="step", + range=(-2.5, 2.5), + density=True, + color="C3", + label=r"$R_{21}$", + ) + +plt.axvline(0, color="k", linestyle="--", alpha=0.7) + +plt.axvline( + np.mean(cat_v146["R_g11"]), + color="C0", + linestyle=":", + alpha=0.7, + label=rf"$\langle R_{11} \rangle = {np.mean(cat_v146['R_g11']):.2f}$", +) +plt.axvline( + np.mean(cat_v146["R_g22"]), + color="C1", + linestyle=":", + alpha=0.7, + label=rf"$\langle R_{22} \rangle = {np.mean(cat_v146['R_g22']):.2f}$", +) plt.ylabel("Density") plt.xlabel("Response") @@ -64,5 +106,5 @@ # %% cat_comprehensive = h5py.File(path_cat_comprehensive, "r") # %% -cat_comprehensive['data']['RA'] +cat_comprehensive["data"]["RA"] # %% diff --git a/papers/catalog/2025_12_11_plot_star_properties.py b/papers/catalog/2025_12_11_plot_star_properties.py index 0dfd86e3..be992521 100644 --- a/papers/catalog/2025_12_11_plot_star_properties.py +++ b/papers/catalog/2025_12_11_plot_star_properties.py @@ -4,51 +4,48 @@ ipython = IPython.get_ipython() if ipython is not None: - ipython.run_line_magic("load_ext", "autoreload") - ipython.run_line_magic("autoreload", "2") + ipython.run_line_magic("load_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") -import os -from pathlib import Path import matplotlib.pyplot as plt -import numpy as np -import pandas as pd import seaborn as sns from astropy.io import fits -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("husl") if ipython is not None: - ipython.run_line_magic("matplotlib", "inline") + ipython.run_line_magic("matplotlib", "inline") # %% -path_cat_v146 = "/n17data/UNIONS/WL/v1.4.x/v1.4.6/unions_shapepipe_cut_struc_2024_v1.4.6.fits" -path_cat_comprehensive = "/n17data/UNIONS/WL/v1.4.x/unions_shapepipe_comprehensive_struc_2024_v1.X.c.hdf5" +path_cat_v146 = ( + "/n17data/UNIONS/WL/v1.4.x/v1.4.6/unions_shapepipe_cut_struc_2024_v1.4.6.fits" +) +path_cat_comprehensive = ( + "/n17data/UNIONS/WL/v1.4.x/unions_shapepipe_comprehensive_struc_2024_v1.X.c.hdf5" +) path_cat_star = "/n17data/UNIONS/WL/v1.4.x//full_starcat-0000000.fits" # %% -#Load catalog +# Load catalog cat_stars = fits.open(path_cat_star)[1].data -e1_star, e2_star = cat_stars['E1_STAR_HSM'], cat_stars['E2_STAR_HSM'] -e1_psf, e2_psf = cat_stars['E1_PSF_HSM'], cat_stars['E2_PSF_HSM'] -t_star, t_psf = cat_stars['SIGMA_STAR_HSM']**2, cat_stars['SIGMA_PSF_HSM']**2 -mag = cat_stars['MAG'] +e1_star, e2_star = cat_stars["E1_STAR_HSM"], cat_stars["E2_STAR_HSM"] +e1_psf, e2_psf = cat_stars["E1_PSF_HSM"], cat_stars["E2_PSF_HSM"] +t_star, t_psf = cat_stars["SIGMA_STAR_HSM"] ** 2, cat_stars["SIGMA_PSF_HSM"] ** 2 +mag = cat_stars["MAG"] # %% # Plot brighter fatter effect plt.figure() -plt.hist(mag, bins=50, histtype='step', density=True, color='C0') +plt.hist(mag, bins=50, histtype="step", density=True, color="C0") plt.show() - # %% diff --git a/papers/catalog/2025_12_11_plot_tau_stats_dependence_mag.py b/papers/catalog/2025_12_11_plot_tau_stats_dependence_mag.py index f0098a89..4b359e15 100644 --- a/papers/catalog/2025_12_11_plot_tau_stats_dependence_mag.py +++ b/papers/catalog/2025_12_11_plot_tau_stats_dependence_mag.py @@ -4,47 +4,44 @@ ipython = IPython.get_ipython() if ipython is not None: - ipython.run_line_magic("load_ext", "autoreload") - ipython.run_line_magic("autoreload", "2") + ipython.run_line_magic("load_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") -import os import copy -from pathlib import Path import matplotlib.pyplot as plt -from matplotlib.ticker import ScalarFormatter import numpy as np -import pandas as pd import seaborn as sns -from astropy.io import fits import treecorr +from astropy.io import fits +from matplotlib.ticker import ScalarFormatter -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("husl") if ipython is not None: - ipython.run_line_magic("matplotlib", "inline") + ipython.run_line_magic("matplotlib", "inline") # %% -path_cat_gal = "/n17data/UNIONS/WL/v1.4.x/v1.4.6/unions_shapepipe_cut_struc_2024_v1.4.6.fits" +path_cat_gal = ( + "/n17data/UNIONS/WL/v1.4.x/v1.4.6/unions_shapepipe_cut_struc_2024_v1.4.6.fits" +) path_cat_stars = "/n17data/UNIONS/WL/v1.4.x//full_starcat-0000000.fits" cat_gal = fits.getdata(path_cat_gal) cat_star = fits.getdata(path_cat_stars) # %% -mag = cat_gal['MAG'] +mag = cat_gal["MAG"] quantiles_mag = np.quantile(mag, [0.33, 0.66]) # %% plt.figure() -plt.hist(mag, bins=50, histtype='step', density=True, color='C0') +plt.hist(mag, bins=50, histtype="step", density=True, color="C0") plt.xlabel("Magnitude") plt.ylabel("Density") @@ -58,61 +55,60 @@ # %% treecorr_config = { - 'min_sep': 1.0, - 'max_sep': 250.0, - 'nbins': 20, - 'sep_units': 'arcmin', - 'var_method': 'jackknife', + "min_sep": 1.0, + "max_sep": 250.0, + "nbins": 20, + "sep_units": "arcmin", + "var_method": "jackknife", } tc_psf = treecorr.Catalog( - ra=cat_star['RA'], - dec=cat_star['DEC'], - g1=cat_star['E1_PSF_HSM'], - g2=cat_star['E2_PSF_HSM'], - ra_units='degrees', - dec_units='degrees', - npatch=100 + ra=cat_star["RA"], + dec=cat_star["DEC"], + g1=cat_star["E1_PSF_HSM"], + g2=cat_star["E2_PSF_HSM"], + ra_units="degrees", + dec_units="degrees", + npatch=100, ) patch_centers = tc_psf.patch_centers print("PSF catalog done") tc_faint = treecorr.Catalog( - ra=cat_gal['RA'][faint_objects], - dec=cat_gal['DEC'][faint_objects], - g1=cat_gal['e1'][faint_objects], - g2=cat_gal['e2'][faint_objects], - ra_units='degrees', - dec_units='degrees', - patch_centers=patch_centers + ra=cat_gal["RA"][faint_objects], + dec=cat_gal["DEC"][faint_objects], + g1=cat_gal["e1"][faint_objects], + g2=cat_gal["e2"][faint_objects], + ra_units="degrees", + dec_units="degrees", + patch_centers=patch_centers, ) print("Faint catalog done") tc_medium = treecorr.Catalog( - ra=cat_gal['RA'][medium_objects], - dec=cat_gal['DEC'][medium_objects], - g1=cat_gal['e1'][medium_objects], - g2=cat_gal['e2'][medium_objects], - ra_units='degrees', - dec_units='degrees', - patch_centers=patch_centers + ra=cat_gal["RA"][medium_objects], + dec=cat_gal["DEC"][medium_objects], + g1=cat_gal["e1"][medium_objects], + g2=cat_gal["e2"][medium_objects], + ra_units="degrees", + dec_units="degrees", + patch_centers=patch_centers, ) print("Medium catalog done") tc_bright = treecorr.Catalog( - ra=cat_gal['RA'][bright_objects], - dec=cat_gal['DEC'][bright_objects], - g1=cat_gal['e1'][bright_objects], - g2=cat_gal['e2'][bright_objects], - ra_units='degrees', - dec_units='degrees', - patch_centers=patch_centers + ra=cat_gal["RA"][bright_objects], + dec=cat_gal["DEC"][bright_objects], + g1=cat_gal["e1"][bright_objects], + g2=cat_gal["e2"][bright_objects], + ra_units="degrees", + dec_units="degrees", + patch_centers=patch_centers, ) print("Bright catalog done") - # %% tau_faint = treecorr.GGCorrelation(**treecorr_config) tau_faint.process(tc_faint, tc_psf) @@ -133,45 +129,45 @@ tau_faint.meanr, tau_faint.xip, yerr=np.sqrt(tau_faint.varxip), - fmt='o', - linestyle='-', - label='Faint galaxies', + fmt="o", + linestyle="-", + label="Faint galaxies", capsize=2, - color='C0' + color="C0", ) plt.errorbar( tau_medium.meanr, tau_medium.xip, yerr=np.sqrt(tau_medium.varxip), - fmt='o', - linestyle='-', - label='Medium galaxies', + fmt="o", + linestyle="-", + label="Medium galaxies", capsize=2, - color='C1' + color="C1", ) plt.errorbar( tau_bright.meanr, tau_bright.xip, yerr=np.sqrt(tau_bright.varxip), - fmt='o', - linestyle='-', - label='Bright galaxies', + fmt="o", + linestyle="-", + label="Bright galaxies", capsize=2, - color='C2' + color="C2", ) -plt.xscale('log') +plt.xscale("log") -plt.xlabel(r'$\theta$ [arcmin]') -plt.ylabel(r'$\tau_{0,+}(\theta)$') +plt.xlabel(r"$\theta$ [arcmin]") +plt.ylabel(r"$\tau_{0,+}(\theta)$") plt.legend(fontsize=12) plt.show() # %% -#Load tau_stats of the whole sample +# Load tau_stats of the whole sample path_tau_stats = "/home/guerrini/sp_validation/cosmo_val/output/rho_tau_stats/tau_stats_SP_v1.4.6.fits" tau_stats = fits.getdata(path_tau_stats) @@ -183,50 +179,50 @@ tau_faint.meanr, tau_faint.xip, yerr=np.sqrt(tau_faint.varxip), - fmt='o', - linestyle='-', - label=rf'Faint galaxies, $m > {quantiles_mag[1]:.2f}$', + fmt="o", + linestyle="-", + label=rf"Faint galaxies, $m > {quantiles_mag[1]:.2f}$", capsize=2, - color='C0' + color="C0", ) plt.errorbar( tau_medium.meanr, tau_medium.xip, yerr=np.sqrt(tau_medium.varxip), - fmt='o', - linestyle='-', - label=rf'Intermediate galaxies, ${quantiles_mag[0]:.2f} < m < {quantiles_mag[1]:.2f}$', + fmt="o", + linestyle="-", + label=rf"Intermediate galaxies, ${quantiles_mag[0]:.2f} < m < {quantiles_mag[1]:.2f}$", capsize=2, - color='C1' + color="C1", ) plt.errorbar( tau_bright.meanr, tau_bright.xip, yerr=np.sqrt(tau_bright.varxip), - fmt='o', - linestyle='-', - label=rf'Bright galaxies, $m < {quantiles_mag[0]:.2f}$', + fmt="o", + linestyle="-", + label=rf"Bright galaxies, $m < {quantiles_mag[0]:.2f}$", capsize=2, - color='C2' + color="C2", ) plt.errorbar( - tau_stats['theta'], - tau_stats['tau_0_p'], - yerr=np.sqrt(tau_stats['vartau_0_p']), - fmt='o', - linestyle='-', - label='All galaxies', + tau_stats["theta"], + tau_stats["tau_0_p"], + yerr=np.sqrt(tau_stats["vartau_0_p"]), + fmt="o", + linestyle="-", + label="All galaxies", capsize=2, - color='black' + color="black", ) -plt.xscale('log') +plt.xscale("log") -plt.xlabel(r'$\vartheta$ [arcmin]') -plt.ylabel(r'$\tau_{0,+}(\vartheta)$') +plt.xlabel(r"$\vartheta$ [arcmin]") +plt.ylabel(r"$\tau_{0,+}(\vartheta)$") plt.legend(fontsize=10) plt.savefig("./plots/plot_tau_stats_dependence_mag.pdf") @@ -238,13 +234,13 @@ # %% tc_very_faint = treecorr.Catalog( - ra=cat_gal['RA'][very_faint_objects], - dec=cat_gal['DEC'][very_faint_objects], - g1=cat_gal['e1'][very_faint_objects], - g2=cat_gal['e2'][very_faint_objects], - ra_units='degrees', - dec_units='degrees', - patch_centers=patch_centers + ra=cat_gal["RA"][very_faint_objects], + dec=cat_gal["DEC"][very_faint_objects], + g1=cat_gal["e1"][very_faint_objects], + g2=cat_gal["e2"][very_faint_objects], + ra_units="degrees", + dec_units="degrees", + patch_centers=patch_centers, ) tau_very_faint = treecorr.GGCorrelation(**treecorr_config) @@ -258,14 +254,14 @@ tau_very_faint.meanr, tau_very_faint.xip, yerr=np.sqrt(tau_very_faint.varxip), - fmt='o', - linestyle='-', - label='Very faint galaxies', + fmt="o", + linestyle="-", + label="Very faint galaxies", capsize=2, - color='C3' + color="C3", ) -plt.xscale('log') +plt.xscale("log") plt.show() @@ -278,13 +274,13 @@ print(f"Mag > {mag_cut}: {np.sum(objects)} galaxies") tc_object = treecorr.Catalog( - ra=cat_gal['RA'][objects], - dec=cat_gal['DEC'][objects], - g1=cat_gal['e1'][objects], - g2=cat_gal['e2'][objects], - ra_units='degrees', - dec_units='degrees', - patch_centers=patch_centers + ra=cat_gal["RA"][objects], + dec=cat_gal["DEC"][objects], + g1=cat_gal["e1"][objects], + g2=cat_gal["e2"][objects], + ra_units="degrees", + dec_units="degrees", + patch_centers=patch_centers, ) tau_object = treecorr.GGCorrelation(**treecorr_config) @@ -308,27 +304,27 @@ tau_object.meanr, tau_object.xip, yerr=np.sqrt(tau_object.varxip), - fmt='o', - linestyle='-', - label=rf'$m > {mag_cut}, {percent_object:.2f} \%$ galaxies', + fmt="o", + linestyle="-", + label=rf"$m > {mag_cut}, {percent_object:.2f} \%$ galaxies", capsize=2, - color=f'C{i}' + color=f"C{i}", ) plt.errorbar( - tau_stats['theta'], - tau_stats['tau_0_p'], - yerr=np.sqrt(tau_stats['vartau_0_p']), - fmt='o', - linestyle='-', - label='All galaxies', + tau_stats["theta"], + tau_stats["tau_0_p"], + yerr=np.sqrt(tau_stats["vartau_0_p"]), + fmt="o", + linestyle="-", + label="All galaxies", capsize=2, - color='black' + color="black", ) -plt.xscale('log') -plt.xlabel(r'$\vartheta$ [arcmin]') -plt.ylabel(r'$\tau_{0,+}(\vartheta)$') +plt.xscale("log") +plt.xlabel(r"$\vartheta$ [arcmin]") +plt.ylabel(r"$\tau_{0,+}(\vartheta)$") formatter = ScalarFormatter(useMathText=True) formatter.set_powerlimits((-2, 2)) diff --git a/papers/catalog/check_gaia.ipynb b/papers/catalog/check_gaia.ipynb index f5a88e77..4b69ed3f 100644 --- a/papers/catalog/check_gaia.ipynb +++ b/papers/catalog/check_gaia.ipynb @@ -6,9 +6,7 @@ "id": "0", "metadata": {}, "outputs": [], - "source": [ - "from matplotlib import pyplot as plt" - ] + "source": [] }, { "cell_type": "code", diff --git a/papers/catalog/download_gaia_catalogues.py b/papers/catalog/download_gaia_catalogues.py index 746a641b..39709e3e 100644 --- a/papers/catalog/download_gaia_catalogues.py +++ b/papers/catalog/download_gaia_catalogues.py @@ -12,13 +12,12 @@ ipython.run_line_magic("autoreload", "2") ipython.run_line_magic("reload_ext", "log_cell_time") -import numpy as np import time -from astroquery.gaia import Gaia -import astropy.units -from astropy.coordinates import SkyCoord from textwrap import dedent +import numpy as np +from astroquery.gaia import Gaia + def get_bins(results, key="phot_g_mean_mag", n_bins=3): @@ -35,7 +34,7 @@ def get_bins(results, key="phot_g_mean_mag", n_bins=3): subsets.append(this_subset) print( - f" Bin {i+1}: {quantiles[i]:.2f}, {quantiles[i+1]:.2f}, N={len(this_subset)}" + f" Bin {i + 1}: {quantiles[i]:.2f}, {quantiles[i + 1]:.2f}, N={len(this_subset)}" ) return subsets, quantiles @@ -47,17 +46,16 @@ def do_query(do_async=True, nmax=3_000_000): query = dedent( f""" SELECT TOP {nmax} ra, dec, phot_g_mean_mag, ruwe - FROM gaiadr3.gaia_source - WHERE dec > 30 - AND dec < 75 - AND phot_g_mean_mag < 20 - AND phot_g_mean_mag IS NOT NULL + FROM gaiadr3.gaia_source + WHERE dec > 30 + AND dec < 75 + AND phot_g_mean_mag < 20 + AND phot_g_mean_mag IS NOT NULL AND ruwe < 1.4 ORDER BY random_index - """ + """ ).strip() - # Launch the query if not do_async: print("Retrieveing GAIA data (sync)") @@ -81,7 +79,6 @@ def do_query(do_async=True, nmax=3_000_000): def write_subsets(subsets, quantiles): for idx, subset in enumerate(subsets): - if idx == 0: out_path = f"gaia_stars_g_smaller_{quantiles[idx + 1]}.fits" elif idx == len(subsets) - 1: diff --git a/papers/catalog/hist_mag.py b/papers/catalog/hist_mag.py index 2f19fe7a..262c38cd 100644 --- a/papers/catalog/hist_mag.py +++ b/papers/catalog/hist_mag.py @@ -1,34 +1,31 @@ -# %% +# %% # hist_mag.py # # Plot magnitude histogram for various cuts and selection criteria # %% -import matplotlib import matplotlib.pylab as plt -# enable autoreload for interactive sessions -from IPython import get_ipython -ipython = get_ipython() -if ipython is not None: +# enable autoreload for interactive sessions +from IPython import get_ipython + +ipython = get_ipython() +if ipython is not None: ipython.run_line_magic("matplotlib", "inline") - ipython.run_line_magic("reload_ext", "autoreload") - ipython.run_line_magic("autoreload", "2") - ipython.run_line_magic("reload_ext", "log_cell_time") + ipython.run_line_magic("reload_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") + ipython.run_line_magic("reload_ext", "log_cell_time") # %% -import sys import os -import re -import numpy as np -from astropy.io import fits +import sys from io import StringIO +import numpy as np + +from sp_validation import calibration from sp_validation import catalog_builders as sp_joint -from sp_validation import format from sp_validation.calibration import metacal -from sp_validation import calibration -import sp_validation.catalog as cat # %% # Initialize calibration class instance @@ -38,21 +35,22 @@ test_only = True + # %% # Funcitons def get_data(obj, test_only=False): """Get Data. - + Returns catalogue. - + Parameters ---------- obj : CalibrateCat instance Instance of CalibrateCat class - test_only : bool, optional + test_only : bool, optional If True, only load a subset of data for testing; default is False. - + """ # Get data. Set load_into_memory to False for very large files dat, dat_ext = obj.read_cat(load_into_memory=False) @@ -91,20 +89,16 @@ def read_hist_data(hist_data_path): for key in loaded.files: data = loaded[key] - hist_data[key] = { - 'counts': data[0], - 'bins': data[1], - 'label': str(data[2]) - } + hist_data[key] = {"counts": data[0], "bins": data[1], "label": str(data[2])} return hist_data def get_mask(masks, col_name): """Get Mask. - + Returns mask corresponding to col_name. - + Parameters ---------- masks : list @@ -117,7 +111,7 @@ def get_mask(masks, col_name): Mask object integer Mask position in list - + """ # Get mask fomr masks with col_name = col_name for idx, mask in enumerate(masks): @@ -210,8 +204,8 @@ def plot_hist(counts, bins, label, alpha=1, ax=None, color=None): width=np.diff(bins), alpha=alpha, label=label, - align='center', - color=color + align="center", + color=color, ) @@ -228,16 +222,11 @@ def plot_all_hists( if ax is None: plt.figure() - fig, (ax) = plt.subplots( - 1, - 1, - figsize=(figsize, figsize) - ) + fig, (ax) = plt.subplots(1, 1, figsize=(figsize, figsize)) counts0 = None for col_name in col_names: if col_name in hist_data: - data = hist_data[col_name] if fraction: if counts0 is None: @@ -248,15 +237,15 @@ def plot_all_hists( plot_hist( counts, - data['bins'], - data['label'], + data["bins"], + data["label"], alpha=alpha, ax=ax, - color=color_map[col_name] + color=color_map[col_name], ) - #print(f"{col_name}: n_valid = {data['n_valid']}") + # print(f"{col_name}: n_valid = {data['n_valid']}") - ax.set_xlabel('$r$') + ax.set_xlabel("$r$") ylabel = "fraction" if fraction else "number" ax.set_ylabel(ylabel) ax.set_xlim(17.5, 26.5) @@ -265,11 +254,7 @@ def plot_all_hists( if out_path: plt.tight_layout() - plt.savefig( - out_path, - dpi=150, - bbox_inches='tight' - ) + plt.savefig(out_path, dpi=150, bbox_inches="tight") # %% @@ -290,7 +275,7 @@ def plot_all_hists( dat, dat_ext = get_data(obj, test_only=test_only) hist_data = None - + # %% # Masking @@ -310,35 +295,38 @@ def plot_all_hists( col_names = ["basic masks"] if scenario == 0: - masks_labels_basic.extend([ - "FLAGS", - "IMAFLAGS_ISO", - "NGMIX_MOM_FAIL", - "NGMIX_ELL_PSFo_NOSHEAR_0", - "NGMIX_ELL_PSFo_NOSHEAR_1", - "4_Stars", - "8_Manual", - "1024_Maximask", - ]) + masks_labels_basic.extend( + [ + "FLAGS", + "IMAFLAGS_ISO", + "NGMIX_MOM_FAIL", + "NGMIX_ELL_PSFo_NOSHEAR_0", + "NGMIX_ELL_PSFo_NOSHEAR_1", + "4_Stars", + "8_Manual", + "1024_Maximask", + ] + ) col_names.extend(["N_EPOCH", "npoint3", "metacal"]) elif scenario == 1: - - col_names.extend([ - "IMAFLAGS_ISO", - "FLAGS", - "NGMIX_MOM_FAIL", - "NGMIX_ELL_PSFo_NOSHEAR_0", - "NGMIX_ELL_PSFo_NOSHEAR_1", - "4_Stars", - "8_Manual", - "1024_Maximask", - "N_EPOCH", - "npoint3", - "metacal", - ]) - + col_names.extend( + [ + "IMAFLAGS_ISO", + "FLAGS", + "NGMIX_MOM_FAIL", + "NGMIX_ELL_PSFo_NOSHEAR_0", + "NGMIX_ELL_PSFo_NOSHEAR_1", + "4_Stars", + "8_Manual", + "1024_Maximask", + "N_EPOCH", + "npoint3", + "metacal", + ] + ) + combine_cols = { "ngmix failures": [ "NGMIX_MOM_FAIL", @@ -346,7 +334,7 @@ def plot_all_hists( "NGMIX_ELL_PSFo_NOSHEAR_1", ] } - + # %% # Combine columns if specified. # Remove old columns after combining. @@ -377,7 +365,7 @@ def plot_all_hists( ) masks.insert(idx, masks_combined) col_names.insert(idx, new_col) - + for old_mask, old_col in zip(old_masks, old_cols): masks.remove(old_mask) col_names.remove(old_col) @@ -417,8 +405,9 @@ def plot_all_hists( kind="none", ) + # %% -def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSHEAR"): +def get_info_for_metacal_masking(dat, mask, prefix="NGMIX", name_shear="NOSHEAR"): res = {} @@ -427,35 +416,35 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH for key in ("flux", "flux_err", "T"): res[key] = dat[mask][f"{prefix}_{key.upper()}_{name_shear}"] res["Tpsf"] = dat[mask][f"{prefix}_Tpsf_{name_shear}"] - + return res + # %% if dat is not None: cm = config["metacal"] - # %% # Call metacal if data is available if dat is not None: cm = config["metacal"] - gal_metacal = metacal( - dat, - masks_basic_combined._mask, - snr_min=cm["gal_snr_min"], - snr_max=cm["gal_snr_max"], - rel_size_min=cm["gal_rel_size_min"], - rel_size_max=cm["gal_rel_size_max"], - size_corr_ell=cm["gal_size_corr_ell"], - sigma_eps=cm["sigma_eps_prior"], - global_R_weight=cm["global_R_weight"], - col_2d=False, + gal_metacal = metacal( + dat, + masks_basic_combined._mask, + snr_min=cm["gal_snr_min"], + snr_max=cm["gal_snr_max"], + rel_size_min=cm["gal_rel_size_min"], + rel_size_max=cm["gal_rel_size_max"], + size_corr_ell=cm["gal_size_corr_ell"], + sigma_eps=cm["sigma_eps_prior"], + global_R_weight=cm["global_R_weight"], + col_2d=False, verbose=True, ) - g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = ( - calibration.get_calibrated_m_c(gal_metacal) + g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = calibration.get_calibrated_m_c( + gal_metacal ) # Convert index array to boolean mask @@ -478,7 +467,7 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH alpha = 0.5 # Define explicit colors for each histogram -colors = [f'C{i}' for i in range(len(col_names))] # Use matplotlib default color cycle +colors = [f"C{i}" for i in range(len(col_names))] # Use matplotlib default color cycle color_map = dict(zip(col_names, colors)) @@ -489,7 +478,7 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH if dat is not None: # Get magnitude column - mag = dat['mag'] + mag = dat["mag"] mask_cumul = None for col_name in col_names: @@ -498,21 +487,17 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH col_name=col_name, mask_cumul=mask_cumul, mag=mag, - bins=mag_bins + bins=mag_bins, ) hist_data[col_name] = { - 'counts': counts, - 'bins': bins, - 'label': label, - 'n_valid': n_valid, + "counts": counts, + "bins": bins, + "label": label, + "n_valid": n_valid, } # %% # Create plots -fig, axes = plt.subplots( - 1, - 2, - figsize=(2 * figsize, figsize) -) +fig, axes = plt.subplots(1, 2, figsize=(2 * figsize, figsize)) # Plot histogram data plot_all_hists( hist_data, @@ -531,11 +516,7 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH ) plt.tight_layout() out_path = f"magnitude_histograms_scenario-{scenario}.png" -plt.savefig( - out_path, - dpi=150, - bbox_inches='tight' -) +plt.savefig(out_path, dpi=150, bbox_inches="tight") # %% # Save histogram data to file (only if we computed it) @@ -545,15 +526,15 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH **{ key: np.array( [ - val['counts'], - val['bins'], - val['label'], + val["counts"], + val["bins"], + val["label"], val["n_valid"], ], - dtype=object + dtype=object, ) for key, val in hist_data.items() - } + }, ) print(f"Histogram data saved to {hist_data_path}") @@ -566,7 +547,3 @@ def get_info_for_metacal_masking(dat, mask, prefix = "NGMIX", name_shear = "NOSH mask.print_condition(sys.stdout, latex=True) # %% -# print number of valid objects and name -for data in hist_data - -# %% diff --git a/papers/catalog/plot_gaia_sky.py b/papers/catalog/plot_gaia_sky.py index 622e7f14..f83d63f6 100644 --- a/papers/catalog/plot_gaia_sky.py +++ b/papers/catalog/plot_gaia_sky.py @@ -3,9 +3,9 @@ import glob import os import re -import numpy as np -from astropy.table import Table + import matplotlib.pyplot as plt +from astropy.table import Table from cs_util.plots import FootprintPlotter @@ -26,9 +26,9 @@ def load_gaia_fits(file_path): n_objects : int Number of objects in file """ - table = Table.read(file_path, format='fits') + table = Table.read(file_path, format="fits") print(f"Loaded {len(table)} objects from {file_path}") - return table['ra'], table['dec'], len(table) + return table["ra"], table["dec"], len(table) def extract_label_from_filename(filename): @@ -47,25 +47,25 @@ def extract_label_from_filename(filename): basename = os.path.basename(filename) # Match pattern: g_smaller_XX.X - match = re.search(r'g_smaller_([0-9.]+)', basename) + match = re.search(r"g_smaller_([0-9.]+)", basename) if match: mag = match.group(1) return f"G < {mag} (bright)" # Match pattern: g_in_XX.X_YY.Y - match = re.search(r'g_in_([0-9.]+)_([0-9.]+)', basename) + match = re.search(r"g_in_([0-9.]+)_([0-9.]+)", basename) if match: mag1, mag2 = match.group(1), match.group(2) return f"{mag1} ≤ G < {mag2} (medium)" # Match pattern: g_larger_XX.X - match = re.search(r'g_larger_([0-9.]+)', basename) + match = re.search(r"g_larger_([0-9.]+)", basename) if match: mag = match.group(1) return f"G ≥ {mag} (faint)" # Fallback - return basename.replace('.fits', '') + return basename.replace(".fits", "") def main(): @@ -113,8 +113,7 @@ def main(): # Plot all regions (NGC, SGC, fullsky) plotter.plot_all_regions( - hsp_map, - outbase=f"gaia_sky_{file_path.replace('.fits', '')}" + hsp_map, outbase=f"gaia_sky_{file_path.replace('.fits', '')}" ) print(f"Created plots for {label}") @@ -130,21 +129,19 @@ def main(): axes = [axes] for idx, (hsp_map, label) in enumerate(zip(hsp_maps, labels)): - ax = axes[idx] - # Use fullsky region parameters - region = plotter._regions['fullsky'] + region = plotter._regions["fullsky"] - projection = plotter.plot_area( + plotter.plot_area( hsp_map, - ra_0=region['ra_0'], - extend=region['extend'], - vmax=region['vmax'], - title=label + ra_0=region["ra_0"], + extend=region["extend"], + vmax=region["vmax"], + title=label, ) plt.tight_layout() - plt.savefig('gaia_sky_combined_all_bins.png', dpi=150, bbox_inches='tight') + plt.savefig("gaia_sky_combined_all_bins.png", dpi=150, bbox_inches="tight") print("Saved combined plot: gaia_sky_combined_all_bins.png") print("All plots created successfully!") diff --git a/papers/catalog/run_cosmo_val_GAIA.py b/papers/catalog/run_cosmo_val_GAIA.py index a3eae753..23d860a8 100644 --- a/papers/catalog/run_cosmo_val_GAIA.py +++ b/papers/catalog/run_cosmo_val_GAIA.py @@ -9,10 +9,14 @@ ipython.run_line_magic("autoreload", "2") ipython.run_line_magic("load_ext", "log_cell_time") +import sys # noqa: E402 + import matplotlib.pyplot as plt # noqa: E402, F401 import numpy as np # noqa: E402, F401 -import sys # noqa: E402 -sys.path.insert(0, '/home/mkilbing/astro/repositories/gitlab.euclid-sgs/FDQA/rho_tau_stats') + +sys.path.insert( + 0, "/home/mkilbing/astro/repositories/gitlab.euclid-sgs/FDQA/rho_tau_stats" +) from cosmo_val import CosmologyValidation # noqa: E402 # enable inline plotting for interactive sessions @@ -30,10 +34,10 @@ theta_min=1.0, theta_max=250.0, nbins=20, - cov_estimate_method='jk', + cov_estimate_method="jk", theta_min_plot=0.8, theta_max_plot=260.0, - rho_tau_method='emcee', + rho_tau_method="emcee", n_cov=1, ) diff --git a/papers/catalog/run_cosmo_val_gammat.py b/papers/catalog/run_cosmo_val_gammat.py index 4ddb1c35..70243697 100644 --- a/papers/catalog/run_cosmo_val_gammat.py +++ b/papers/catalog/run_cosmo_val_gammat.py @@ -9,11 +9,12 @@ ipython.run_line_magic("autoreload", "2") ipython.run_line_magic("reload_ext", "log_cell_time") -import matplotlib.pyplot as plt -import numpy as np -import sys import os -sys.path.insert(0, '/home/mkilbing/astro/repositories/gitlab.euclid-sgs/FDQA/rho_tau_stats') +import sys + +sys.path.insert( + 0, "/home/mkilbing/astro/repositories/gitlab.euclid-sgs/FDQA/rho_tau_stats" +) from cosmo_val import CosmologyValidation @@ -21,7 +22,7 @@ def rename_output(output_bases, output_dir, suff, version_string, rand_str): for base in output_bases: old_path = f"{output_dir}/plots/{base}{suff}" - new_path = f"{output_dir}/plots/{base}_{version_string}{rand_str}{suff}" + new_path = f"{output_dir}/plots/{base}_{version_string}{rand_str}{suff}" print(f"mv {old_path} -> {new_path}") os.rename(old_path, new_path) @@ -47,10 +48,10 @@ def rename_output(output_bases, output_dir, suff, version_string, rand_str): theta_min=1.0, theta_max=250.0, nbins=20, - cov_estimate_method='jk', + cov_estimate_method="jk", theta_min_plot=0.8, theta_max_plot=260.0, - rho_tau_method='emcee', + rho_tau_method="emcee", n_cov=1, star_weight_type="uniform", ) diff --git a/papers/catalog/run_cosmo_val_lambda.py b/papers/catalog/run_cosmo_val_lambda.py index 9ce7322b..089cbf5d 100644 --- a/papers/catalog/run_cosmo_val_lambda.py +++ b/papers/catalog/run_cosmo_val_lambda.py @@ -10,21 +10,19 @@ ipython.run_line_magic("load_ext", "log_cell_time") import sys -import os -import matplotlib.pyplot as plt -import numpy as np -sys.path.insert(0, '/home/mkilbing/astro/repositories/gitlab.euclid-sgs/FDQA/rho_tau_stats') +sys.path.insert( + 0, "/home/mkilbing/astro/repositories/gitlab.euclid-sgs/FDQA/rho_tau_stats" +) from cosmo_val import CosmologyValidation, rename_output # noqa: E402 - # enable inline plotting for interactive sessions # (must be done *after* importing package that sets agg backend) if ipython is not None: ipython.run_line_magic("matplotlib", "inline") # Different options -versions_146 = ["SP_v1.4.6"] #, "SP_v1.4.6_leak_corr"] +versions_146 = ["SP_v1.4.6"] # , "SP_v1.4.6_leak_corr"] versions_var = ["SP_v1.4.5", "SP_v1.4.6", "SP_v1.4.7", "SP_v1.4.8"] # We use this combination of versions @@ -46,20 +44,20 @@ theta_min=theta_min, theta_max=theta_max, nbins=15, - cov_estimate_method='jk', + cov_estimate_method="jk", theta_min_plot=theta_min / plot_range_fac, theta_max_plot=theta_max * plot_range_fac, - rho_tau_method='emcee', + rho_tau_method="emcee", n_cov=1, star_weight_type="uniform", random_multiple=5, ) output_bases = [ - "gammat_stars_around_galaxies_lin_non_tomographic", - "gammat_stars_around_galaxies_log_non_tomographic", - "dgammat_stars_around_galaxies_lin_non_tomographic", - "dsize_stars_around_galaxies_lin_non_tomographic", + "gammat_stars_around_galaxies_lin_non_tomographic", + "gammat_stars_around_galaxies_log_non_tomographic", + "dgammat_stars_around_galaxies_lin_non_tomographic", + "dsize_stars_around_galaxies_lin_non_tomographic", ] suff = ".png" version_string = "_".join(versions) @@ -67,7 +65,9 @@ # lambda_1 print("Computing λ_1...") cv.plot_gammat_stars_around_galaxies(offset=0.025, gammax=True, wo_rand_subtr=True) -cv.plot_gammat_stars_around_galaxies(offset=0.025, gammax=True, logy=True, wo_rand_subtr=True) +cv.plot_gammat_stars_around_galaxies( + offset=0.025, gammax=True, logy=True, wo_rand_subtr=True +) # lambda_2 print("Computing λ_2...") @@ -95,4 +95,3 @@ cv.plot_dsize_stars_around_galaxies(offset=0.025, gammax=True) rename_output(output_bases, output_dir, suff, version_string, "") - diff --git a/papers/consistency/2025_11_13_plot_contours.ipynb b/papers/consistency/2025_11_13_plot_contours.ipynb index d09b825c..0d0d1f31 100644 --- a/papers/consistency/2025_11_13_plot_contours.ipynb +++ b/papers/consistency/2025_11_13_plot_contours.ipynb @@ -10,42 +10,36 @@ "import os\n", "import sys\n", "\n", - "sys.path.append(\n", - " \"/home/guerrini/sp_validation/cosmo_inference/scripts/\"\n", - ")\n", + "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts/\")\n", "\n", "# Trick to have TeX figures\n", "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", + "import chain_postprocessing as cpp\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", - "import chain_postprocessing as cpp\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=7)\n", - "g.settings.axes_fontsize=15\n", - "g.settings.axes_labelsize=15\n", + "g.settings.axes_fontsize = 15\n", + "g.settings.axes_labelsize = 15\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 15\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/glass_mock_chains/\"\n", "\n", "index = 348\n", "n_contours = 2\n", - "roots = [\n", - " f\"glass_mock_v6_{i:05d}\" for i in range(index, index + n_contours)\n", - "]\n", + "roots = [f\"glass_mock_v6_{i:05d}\" for i in range(index, index + n_contours)]\n", "\n", "print(roots)" ] @@ -60,28 +54,37 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}.txt'.format('/'+root ,root, root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}.paramnames'.format('/'+root, root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}.paramnames\".format(\"/\" + root, root, root), \"w\"\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()\n", "\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root ,root, root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -92,41 +95,46 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}.txt'.format(root,root, root))\n", + " samples = np.loadtxt(root_dir + \"{}/{}/samples_{}.txt\".format(root, root, root))\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}.txt'.format(root,root, root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)\n", "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -138,8 +146,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -158,11 +188,13 @@ "\n", "Omega_m_fid = planck.Om0\n", "sigma_8_fid = 0.8102\n", - "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3)**0.5\n", + "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3) ** 0.5\n", "h = planck.h\n", "Omega_b_fig = planck.Ob0\n", "n_s_fid = 0.965\n", - "print(f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\")\n", + "print(\n", + " f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\"\n", + ")\n", "\n", "markers = {\n", " \"OMEGA_M\": Omega_m_fid,\n", @@ -170,7 +202,7 @@ " \"s_8_input\": s8_fid,\n", " \"h0\": h,\n", " \"ombh2\": Omega_b_fig * h**2,\n", - " \"n_s\": n_s_fid\n", + " \"n_s\": n_s_fid,\n", "}" ] }, @@ -182,11 +214,11 @@ "outputs": [], "source": [ "legend_labels = [\n", - " label\n", + " label\n", " for i in range(index, index + n_contours)\n", " for label in (\n", " rf\"$\\xi_\\pm(\\vartheta)$, GLASS mock {i}\",\n", - " rf\"$C_\\ell$, GLASS mock {i}\"\n", + " rf\"$C_\\ell$, GLASS mock {i}\",\n", " )\n", "]\n", "\n", @@ -194,17 +226,17 @@ " print(f\"Chain {legend}:\")\n", "\n", " print(\"Mean values\")\n", - " print(\"S8:\", cpp.compute_average(chain, 's_8_input'))\n", - " print(\"Omega_m:\", cpp.compute_average(chain, 'OMEGA_M'))\n", - " print(\"Sigma_8:\", cpp.compute_average(chain, 'SIGMA_8'))\n", + " print(\"S8:\", cpp.compute_average(chain, \"s_8_input\"))\n", + " print(\"Omega_m:\", cpp.compute_average(chain, \"OMEGA_M\"))\n", + " print(\"Sigma_8:\", cpp.compute_average(chain, \"SIGMA_8\"))\n", "\n", " print(\"MAP values (1D KDE)\")\n", - " print(\"S8:\", cpp.compute_map_1D(chain, 's_8_input'))\n", - " print(\"Omega_m:\", cpp.compute_map_1D(chain, 'OMEGA_M'))\n", - " print(\"Sigma_8:\", cpp.compute_map_1D(chain, 'SIGMA_8'))\n", + " print(\"S8:\", cpp.compute_map_1D(chain, \"s_8_input\"))\n", + " print(\"Omega_m:\", cpp.compute_map_1D(chain, \"OMEGA_M\"))\n", + " print(\"Sigma_8:\", cpp.compute_map_1D(chain, \"SIGMA_8\"))\n", "\n", " print(\"MAP values (2D KDE)\")\n", - " S8_map, Omega_m_map = cpp.compute_map_2D(chain, 's_8_input', 'OMEGA_M')\n", + " S8_map, Omega_m_map = cpp.compute_map_2D(chain, \"s_8_input\", \"OMEGA_M\")\n", " print(\"S8:\", S8_map)\n", " print(\"Omega_m:\", Omega_m_map)\n", "\n", @@ -229,15 +261,15 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", - " markers=markers\n", + " markers=markers,\n", ")\n", "\n", "\n", @@ -252,22 +284,29 @@ "outputs": [], "source": [ "preliminary_watermark = False\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", " title_limit=1,\n", - " markers=markers\n", + " markers=markers,\n", ")\n", "\n", "if preliminary_watermark:\n", - " plt.figtext(0.5, 0.5, 'PRELIMINARY',\n", - " fontsize=150, color='gray',\n", - " ha='center', va='center',\n", - " alpha=0.3, rotation=330)\n", + " plt.figtext(\n", + " 0.5,\n", + " 0.5,\n", + " \"PRELIMINARY\",\n", + " fontsize=150,\n", + " color=\"gray\",\n", + " ha=\"center\",\n", + " va=\"center\",\n", + " alpha=0.3,\n", + " rotation=330,\n", + " )\n", "\n", "plt.show()" ] diff --git a/papers/consistency/2025_11_24_check_consistency.py b/papers/consistency/2025_11_24_check_consistency.py index 33d769b3..bfeb3c04 100644 --- a/papers/consistency/2025_11_24_check_consistency.py +++ b/papers/consistency/2025_11_24_check_consistency.py @@ -2,49 +2,39 @@ # Trick to plot with tex import os import sys + os.environ["LD_LIBRARY_PATH"] = "" os.environ["CONDA_PREFIX"] = "/home/guerrini/.conda/envs/sp_validation_3.11" -sys.path.append( - "/home/guerrini/sp_validation/cosmo_inference/scripts/" -) +sys.path.append("/home/guerrini/sp_validation/cosmo_inference/scripts/") import IPython ipython = IPython.get_ipython() if ipython is not None: - ipython.run_line_magic('load_ext', 'autoreload') - ipython.run_line_magic('autoreload', '2') + ipython.run_line_magic("load_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") import os -import copy -from tqdm import tqdm +import chain_postprocessing as cp import matplotlib.pyplot as plt -from mpl_toolkits.axes_grid1 import make_axes_locatable import numpy as np -import healpy as hp -import seaborn as sns -from astropy.io import fits import pandas as pd +import seaborn as sns +from getdist import plots from scipy.stats import norm -import chain_postprocessing as cp - -from getdist import plots, MCSamples - if os.path.exists("/home/guerrini/matplotlib_config/paper.mplstyle"): - plt.style.use( - "/home/guerrini/matplotlib_config/paper.mplstyle" - ) + plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") # Set default palette - will be updated per plot as needed sns.set_palette("husl") if ipython is not None: - ipython.run_line_magic('matplotlib', 'inline') + ipython.run_line_magic("matplotlib", "inline") # %% output_fig_path = "./plots" @@ -55,7 +45,8 @@ # Load the output of the simulations simulation_output = pd.read_csv( - f"/n09data/guerrini/glass_mock_chains/summary_parameter_constraints_merged_{chain_version}.txt", delimiter=";" + f"/n09data/guerrini/glass_mock_chains/summary_parameter_constraints_merged_{chain_version}.txt", + delimiter=";", ) # %% @@ -70,26 +61,25 @@ Omega_m_fid = planck.Om0 sigma_8_fid = 0.8102 -s8_fid = sigma_8_fid * (Omega_m_fid / 0.3)**0.5 +s8_fid = sigma_8_fid * (Omega_m_fid / 0.3) ** 0.5 h = planck.h Omega_b_fig = planck.Ob0 n_s_fid = 0.965 -print(f"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}") +print( + f"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}" +) # %% sns.histplot( - simulation_output["S8_config_mean"], - kde=True, - bins=20, - label="Configuration space" + simulation_output["S8_config_mean"], kde=True, bins=20, label="Configuration space" ) sns.histplot( simulation_output["S8_harm_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.legend(fontsize=12) @@ -104,18 +94,18 @@ simulation_output["OMEGA_M_config_mean"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["OMEGA_M_harm_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.title("Using the weighted average") plt.xlabel(r"$\Omega_m$ estimated from mocks") @@ -127,18 +117,18 @@ simulation_output["SIGMA_8_config_mean"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["SIGMA_8_harm_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.title("Using the weighted average") plt.xlabel(r"$\sigma_8$ estimated from mocks") @@ -149,14 +139,14 @@ simulation_output["S8_config_mean"] - simulation_output["S8_harm_mean"], kde=False, bins=20, - stat='density', - label="Difference (Config - Harm)" + stat="density", + label="Difference (Config - Harm)", ) plt.xlabel(r"$\Delta S_8$ estimated from mocks") plt.legend(fontsize=12) plt.title("Using the weighted average") plt.savefig(f"{output_fig_path}/S8_difference_config_harm.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm.pdf") plt.show() @@ -166,15 +156,15 @@ simulation_output["S8_config_map_1D"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["S8_harm_map_1D"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.legend(fontsize=12) @@ -189,18 +179,18 @@ simulation_output["OMEGA_M_config_map_1D"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["OMEGA_M_harm_map_1D"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.title("Using the MAP (KDE 1D)") plt.xlabel(r"$\Omega_m$ estimated from mocks") @@ -212,18 +202,18 @@ simulation_output["SIGMA_8_config_map_1D"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["SIGMA_8_harm_map_1D"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.title("Using the MAP (KDE 1D)") plt.xlabel(r"$\sigma_8$ estimated from mocks") @@ -234,14 +224,14 @@ simulation_output["S8_config_map_1D"] - simulation_output["S8_harm_map_1D"], kde=False, bins=20, - stat='density', - label="Difference (Config - Harm)" + stat="density", + label="Difference (Config - Harm)", ) plt.xlabel(r"$\Delta S_8$ estimated from mocks") plt.legend(fontsize=12) plt.title("Using the MAP (KDE 1D)") plt.savefig(f"{output_fig_path}/S8_difference_config_harm_map.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm_map.pdf") plt.show() @@ -251,15 +241,15 @@ simulation_output["OMEGA_M_config_map_2D"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["OMEGA_M_harm_map_2D"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") plt.legend(fontsize=12) @@ -274,15 +264,15 @@ simulation_output["S8_config_map_2D"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["S8_harm_map_2D"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.legend(fontsize=12) @@ -298,14 +288,14 @@ simulation_output["S8_config_map_2D"] - simulation_output["S8_harm_map_2D"], kde=False, bins=20, - stat='density', - label="Difference (Config - Harm)" + stat="density", + label="Difference (Config - Harm)", ) plt.xlabel(r"$\Delta S_8$ estimated from mocks") plt.legend(fontsize=12) plt.title("Using the MAP (KDE 2D)") plt.savefig(f"{output_fig_path}/S8_difference_config_harm_map_2D.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm_map_2D.pdf") plt.show() @@ -315,15 +305,21 @@ x="S8_config_mean", y="S8_harm_mean", bins=25, - cbar='seismic', - cbar_kws={'label': 'Prob.'}, + cbar="seismic", + cbar_kws={"label": "Prob."}, ) plt.plot( - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - color='grey', - linestyle='--', - alpha=1.0 + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + color="grey", + linestyle="--", + alpha=1.0, ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.xlabel(r"$S_8$ estimated from mocks (Configuration space)") @@ -338,8 +334,8 @@ x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", bins=25, - cbar='seismic', - cbar_kws={'label': 'Counts'} + cbar="seismic", + cbar_kws={"label": "Counts"}, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") @@ -357,7 +353,7 @@ x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", bins=25, - cmap='mako', + cmap="mako", cbar=True, ax=axes[0], ) @@ -371,10 +367,10 @@ x="OMEGA_M_harm_mean", y="SIGMA_8_harm_mean", bins=25, - cmap='mako', + cmap="mako", cbar=True, ax=axes[1], - cbar_kws={'label': 'Counts'} + cbar_kws={"label": "Counts"}, ) axes[1].axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") axes[1].axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") @@ -386,7 +382,9 @@ plt.show() # %% -g = sns.JointGrid(data=simulation_output, x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", space=0) +g = sns.JointGrid( + data=simulation_output, x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", space=0 +) g.plot_joint( sns.histplot, @@ -405,7 +403,9 @@ plt.show() # %% -g = sns.JointGrid(data=simulation_output, x="OMEGA_M_harm_mean", y="SIGMA_8_harm_mean", space=0) +g = sns.JointGrid( + data=simulation_output, x="OMEGA_M_harm_mean", y="SIGMA_8_harm_mean", space=0 +) g.plot_joint( sns.histplot, @@ -415,7 +415,7 @@ g.plot_marginals(sns.histplot, bins=25) g.ax_joint.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -g.ax_joint.axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") +g.ax_joint.axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") g.ax_joint.set_xlabel(r"$\Omega_m$ estimated from mocks (Harmonic space)") g.ax_joint.set_ylabel(r"$\sigma_8$ estimated from mocks (Harmonic space)") @@ -424,7 +424,9 @@ # %% -g = sns.JointGrid(data=simulation_output, x="S8_config_map_2D", y="S8_harm_map_2D", space=0) +g = sns.JointGrid( + data=simulation_output, x="S8_config_map_2D", y="S8_harm_map_2D", space=0 +) g.plot_joint( sns.histplot, @@ -434,20 +436,26 @@ g.plot_marginals(sns.histplot, bins=25, kde=True) g.ax_joint.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") -g.ax_joint.axhline(s8_fid, color="black", linestyle="--", label="Fiducial S8") +g.ax_joint.axhline(s8_fid, color="black", linestyle="--", label="Fiducial S8") g.ax_joint.set_xlabel(r"$S_8$ estimated from mocks (Configuration space)") g.ax_joint.set_ylabel(r"$S_8$ estimated from mocks (Harmonic space)") g.ax_joint.plot( - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - color='royalblue', - linestyle='-', - alpha=0.7 + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + color="royalblue", + linestyle="-", + alpha=0.7, ) plt.savefig(f"{output_fig_path}/S8_joint_config_harm.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_joint_config_harm.pdf") plt.show() @@ -456,99 +464,88 @@ # Get p-value blind = "B" best_fit_method = "map_2D" -assert best_fit_method in ["weighted_mean", "map_1D", "map_2D"], "Invalid best fit method. Choose from 'weighted_mean', 'map_1D', 'map_2D'." +assert best_fit_method in ["weighted_mean", "map_1D", "map_2D"], ( + "Invalid best fit method. Choose from 'weighted_mean', 'map_1D', 'map_2D'." +) g = plots.get_subplot_plotter(width_inch=7) -root_harmonic = f'SP_v1.4.6.3_leak_corr_{blind}' -root_configuration = f'SP_v1.4.6.3_{blind}_fiducial_config' +root_harmonic = f"SP_v1.4.6.3_leak_corr_{blind}" +root_configuration = f"SP_v1.4.6.3_{blind}_fiducial_config" # Load configuration space result -path_configuration = f"/n09data/guerrini/output_chains/{root_configuration}/getdist_{root_configuration}" +path_configuration = ( + f"/n09data/guerrini/output_chains/{root_configuration}/getdist_{root_configuration}" +) chain_configuration = g.samples_for_root( - path_configuration, - cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + path_configuration, + cache=False, + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) if best_fit_method == "weighted_mean": - S8_configuration_analysis = cp.compute_average( - chain_configuration, - "S_8" - ) + S8_configuration_analysis = cp.compute_average(chain_configuration, "S_8") elif best_fit_method == "map_1D": - S8_configuration_analysis = cp.compute_map_1D( - chain_configuration, - "S_8" - ) + S8_configuration_analysis = cp.compute_map_1D(chain_configuration, "S_8") elif best_fit_method == "map_2D": S8_configuration_analysis, _ = cp.compute_map_2D( - chain_configuration, - "S_8", - "OMEGA_M" + chain_configuration, "S_8", "OMEGA_M" ) # Load harmonic space result path_harmonic = f"/n09data/guerrini/output_chains/{root_harmonic}/{root_harmonic}/getdist_{root_harmonic}" chain_harmonic = g.samples_for_root( - path_harmonic, - cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + path_harmonic, + cache=False, + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) if best_fit_method == "weighted_mean": - S8_harmonic_analysis = cp.compute_average( - chain_harmonic, - "S_8" - ) + S8_harmonic_analysis = cp.compute_average(chain_harmonic, "S_8") elif best_fit_method == "map_1D": - S8_harmonic_analysis = cp.compute_map_1D( - chain_harmonic, - "S_8" - ) + S8_harmonic_analysis = cp.compute_map_1D(chain_harmonic, "S_8") elif best_fit_method == "map_2D": - S8_harmonic_analysis, _ = cp.compute_map_2D( - chain_harmonic, - "S_8", - "OMEGA_M" - ) + S8_harmonic_analysis, _ = cp.compute_map_2D(chain_harmonic, "S_8", "OMEGA_M") delta_S8_analysis = S8_configuration_analysis - S8_harmonic_analysis print(f"Delta S8 from analysis: {delta_S8_analysis}") # Select glass mocks without nan values if best_fit_method == "weighted_mean": - selection = ~np.isnan(simulation_output["S8_config_mean"]) & ~np.isnan(simulation_output["S8_harm_mean"]) - delta_S8 = (simulation_output["S8_config_mean"][selection] - simulation_output["S8_harm_mean"][selection]).values + selection = ~np.isnan(simulation_output["S8_config_mean"]) & ~np.isnan( + simulation_output["S8_harm_mean"] + ) + delta_S8 = ( + simulation_output["S8_config_mean"][selection] + - simulation_output["S8_harm_mean"][selection] + ).values elif best_fit_method == "map_1D": - selection = ~np.isnan(simulation_output["S8_config_map_1D"]) & ~np.isnan(simulation_output["S8_harm_map_1D"]) - delta_S8 = (simulation_output["S8_config_map_1D"][selection] - simulation_output["S8_harm_map_1D"][selection]).values + selection = ~np.isnan(simulation_output["S8_config_map_1D"]) & ~np.isnan( + simulation_output["S8_harm_map_1D"] + ) + delta_S8 = ( + simulation_output["S8_config_map_1D"][selection] + - simulation_output["S8_harm_map_1D"][selection] + ).values elif best_fit_method == "map_2D": - selection = ~np.isnan(simulation_output["S8_config_map_2D"]) & ~np.isnan(simulation_output["S8_harm_map_2D"]) - delta_S8 = (simulation_output["S8_config_map_2D"][selection] - simulation_output["S8_harm_map_2D"][selection]).values + selection = ~np.isnan(simulation_output["S8_config_map_2D"]) & ~np.isnan( + simulation_output["S8_harm_map_2D"] + ) + delta_S8 = ( + simulation_output["S8_config_map_2D"][selection] + - simulation_output["S8_harm_map_2D"][selection] + ).values -counts, bin_edges = np.histogram( - delta_S8, - bins=20, - density=True -) +counts, bin_edges = np.histogram(delta_S8, bins=20, density=True) sns.histplot( delta_S8, kde=False, bins=bin_edges, - stat='density', + stat="density", label=r"$\Delta S_8$ in \texttt{GLASS} mocks", - color='blue', - alpha=0.3 + color="blue", + alpha=0.3, ) # Compute the p-value @@ -564,7 +561,12 @@ print(f"P-value for delta S8 = {delta_S8_analysis}: {p_value}") -plt.axvline(delta_S8_analysis, color="red", linestyle="--", label=r"$\Delta S_8$ in the analysis") +plt.axvline( + delta_S8_analysis, + color="red", + linestyle="--", + label=r"$\Delta S_8$ in the analysis", +) mantissa, exponent = f"{p_value:.1e}".split("e") exponent = int(exponent) @@ -581,14 +583,15 @@ y_text = 10 plt.text( - x_text, y_text, + x_text, + y_text, pte_string, - color='black', + color="black", fontsize=12, ) n_sigma = norm.isf(p_value) -sign = '-' if delta_S8_analysis < 0 else '+' +sign = "-" if delta_S8_analysis < 0 else "+" print(f"Number of sigma corresponding to the p-value: {n_sigma}") if best_fit_method == "weighted_mean": x_text = -0.045 @@ -600,15 +603,18 @@ x_text = 0.05 y_text = 9 plt.text( - x_text, y_text, + x_text, + y_text, rf"$N_\sigma = {sign}{n_sigma:.2f}\sigma$", - color='black', + color="black", fontsize=12, ) plt.xlabel(r"$\Delta S_8 =S_{8, {\rm config}} - S_{8, {\rm harm}}$") plt.legend(fontsize=11, framealpha=1.0) -plt.savefig(f"{output_fig_path}/S8_difference_config_harm_{best_fit_method}.png", dpi=300) -#Save PDF +plt.savefig( + f"{output_fig_path}/S8_difference_config_harm_{best_fit_method}.png", dpi=300 +) +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm_{best_fit_method}.pdf") plt.show() # %% diff --git a/papers/consistency/2025_11_24_get_param_values.py b/papers/consistency/2025_11_24_get_param_values.py index e65af5fc..e7f43f0b 100644 --- a/papers/consistency/2025_11_24_get_param_values.py +++ b/papers/consistency/2025_11_24_get_param_values.py @@ -4,232 +4,243 @@ ipython = IPython.get_ipython() if ipython is not None: - ipython.run_line_magic('load_ext', 'autoreload') - ipython.run_line_magic('autoreload', '2') + ipython.run_line_magic("load_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") -import os import copy -from tqdm import tqdm +import os import sys -sys.path.append( - "/home/guerrini/sp_validation/cosmo_inference/scripts/" -) +from tqdm import tqdm +sys.path.append("/home/guerrini/sp_validation/cosmo_inference/scripts/") + +import chain_postprocessing as cpp import matplotlib.pyplot as plt -from mpl_toolkits.axes_grid1 import make_axes_locatable import numpy as np -import healpy as hp import seaborn as sns -from astropy.io import fits -import chain_postprocessing as cpp - -from getdist import plots, MCSamples +from getdist import plots g = plots.get_subplot_plotter(width_inch=7) -g.settings.axes_fontsize=15 -g.settings.axes_labelsize=15 +g.settings.axes_fontsize = 15 +g.settings.axes_labelsize = 15 g.settings.alpha_filled_add = 0.7 g.settings.legend_fontsize = 15 if os.path.exists("/home/guerrini/matplotlib_config/paper.mplstyle"): - plt.style.use( - "/home/guerrini/matplotlib_config/paper.mplstyle" - ) + plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") # Set default palette - will be updated per plot as needed sns.set_palette("husl") if ipython is not None: - ipython.run_line_magic('matplotlib', 'inline') + ipython.run_line_magic("matplotlib", "inline") # %% -root_dir = '/n09data/guerrini/glass_mock_chains/' +root_dir = "/n09data/guerrini/glass_mock_chains/" -failed_simulations = [ - 82, 83, 281, 282, 283,284, 285, 286, 287 -] +failed_simulations = [82, 83, 281, 282, 283, 284, 285, 286, 287] -weird_simulations = [ - 82, 97, 123 -] +weird_simulations = [82, 97, 123] -weird_simulations_v1 = [ - 16, 97, 120, 123 -] +weird_simulations_v1 = [16, 97, 120, 123] -weird_simulations_v2 = [ -] +weird_simulations_v2 = [] version = "v6" -roots = [ - f"glass_mock_{version}_{i+1:05d}" for i in range(350) -] +roots = [f"glass_mock_{version}_{i + 1:05d}" for i in range(350)] + # %% def load_samples_and_write_paramames(root_dir, root, chain_type="configuration"): - assert chain_type in ["configuration", "harmonic"], "chain_type must be 'configuration' or 'harmonic'" + assert chain_type in ["configuration", "harmonic"], ( + "chain_type must be 'configuration' or 'harmonic'" + ) if chain_type == "configuration": - path_samples = root_dir + '{}/{}/samples_{}.txt'.format('/'+root, root, root) - path_paramnames = root_dir + '{}/{}/getdist_{}.paramnames'.format('/'+root, root, root) + path_samples = root_dir + "{}/{}/samples_{}.txt".format("/" + root, root, root) + path_paramnames = root_dir + "{}/{}/getdist_{}.paramnames".format( + "/" + root, root, root + ) else: - path_samples = root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root, root, root) - path_paramnames = root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root) - + path_samples = root_dir + "{}/{}/samples_{}_cell.txt".format( + "/" + root, root, root + ) + path_paramnames = root_dir + "{}/{}/getdist_{}_cell.paramnames".format( + "/" + root, root, root + ) + cpp.load_samples_and_write_paramnames(path_samples, path_paramnames) + def write_samples_getdist_format(root_dir, root, chain_type="configuration"): - assert chain_type in ["configuration", "harmonic"], "chain_type must be 'configuration' or 'harmonic'" + assert chain_type in ["configuration", "harmonic"], ( + "chain_type must be 'configuration' or 'harmonic'" + ) if chain_type == "configuration": - path_samples = root_dir + '{}/{}/samples_{}.txt'.format('/'+root, root, root) - path_gd_samples = root_dir + '{}/{}/getdist_{}.txt'.format('/'+root, root, root) - path_gd = root_dir + '{}/{}/getdist_{}'.format(root,root,root) + path_samples = root_dir + "{}/{}/samples_{}.txt".format("/" + root, root, root) + path_gd_samples = root_dir + "{}/{}/getdist_{}.txt".format( + "/" + root, root, root + ) + path_gd = root_dir + "{}/{}/getdist_{}".format(root, root, root) else: - path_samples = root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root, root, root) - path_gd_samples = root_dir + '{}/{}/getdist_{}_cell.txt'.format('/'+root, root, root) - path_gd = root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root) - - sampler_type = 'nautilus' if 'nautilus' in root else 'polychord' - cpp.write_samples_getdist_format(path_samples, path_gd_samples, chain_type=sampler_type) + path_samples = root_dir + "{}/{}/samples_{}_cell.txt".format( + "/" + root, root, root + ) + path_gd_samples = root_dir + "{}/{}/getdist_{}_cell.txt".format( + "/" + root, root, root + ) + path_gd = root_dir + "{}/{}/getdist_{}_cell".format(root, root, root) + + sampler_type = "nautilus" if "nautilus" in root else "polychord" + cpp.write_samples_getdist_format( + path_samples, path_gd_samples, chain_type=sampler_type + ) chain = g.samples_for_root( path_gd, cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) return chain + def extract_param_chain(chain, param_names): param_values = {} for param_name in param_names: if param_name not in chain.getParamNames().list(): raise ValueError(f"Parameter {param_name} not found in chain.") - + mean = cpp.compute_average(chain, param_name) - limi_68_upper, limi_68_lower, limi_95_upper, limi_95_lower = cpp.compute_limits(chain, param_name) + limi_68_upper, limi_68_lower, limi_95_upper, limi_95_lower = cpp.compute_limits( + chain, param_name + ) map_1D = cpp.compute_map_1D(chain, param_name) param_values[param_name] = { - 'mean': mean, - '1sigma_minus': limi_68_lower, - '1sigma_plus': limi_68_upper, - '2sigma_minus': limi_95_lower, - '2sigma_plus': limi_95_upper, - 'map_1D': map_1D + "mean": mean, + "1sigma_minus": limi_68_lower, + "1sigma_plus": limi_68_upper, + "2sigma_minus": limi_95_lower, + "2sigma_plus": limi_95_upper, + "map_1D": map_1D, } - if param_name == 'S_8' or param_name == 'OMEGA_M': - s8_map_2D, omega_m_map_2D = cpp.compute_map_2D(chain, 'S_8', 'OMEGA_M') - param_values[param_name]['map_2D'] = s8_map_2D if param_name == 'S_8' else omega_m_map_2D + if param_name == "S_8" or param_name == "OMEGA_M": + s8_map_2D, omega_m_map_2D = cpp.compute_map_2D(chain, "S_8", "OMEGA_M") + param_values[param_name]["map_2D"] = ( + s8_map_2D if param_name == "S_8" else omega_m_map_2D + ) return param_values + def concatenate_param_stats(name, param_values, verbose=False): output = [name] for key in param_values.keys(): param_stat = param_values[key] if verbose: - print(f"{name} - {key}: {param_stat['mean']:.4f} +{param_stat['1sigma_plus']:.4f}/-{param_stat['1sigma_minus']:.4f} (1σ), +{param_stat['2sigma_plus']:.4f}/-{param_stat['2sigma_minus']:.4f} (2σ)") + print( + f"{name} - {key}: {param_stat['mean']:.4f} +{param_stat['1sigma_plus']:.4f}/-{param_stat['1sigma_minus']:.4f} (1σ), +{param_stat['2sigma_plus']:.4f}/-{param_stat['2sigma_minus']:.4f} (2σ)" + ) param_list = [ - param_stat['mean'], - param_stat['1sigma_minus'], - param_stat['1sigma_plus'], - param_stat['2sigma_minus'], - param_stat['2sigma_plus'], - param_stat['map_1D'] + param_stat["mean"], + param_stat["1sigma_minus"], + param_stat["1sigma_plus"], + param_stat["2sigma_minus"], + param_stat["2sigma_plus"], + param_stat["map_1D"], ] - if key == 'S_8' or key == 'OMEGA_M': - param_list.append(param_stat['map_2D']) + if key == "S_8" or key == "OMEGA_M": + param_list.append(param_stat["map_2D"]) output += param_list return output + def merge_param_stats(params_configuration, params_harmonic): merged_params = {} for key in params_configuration.keys(): if key in params_harmonic: merged_params[key] = { - 'configuration': params_configuration[key], - 'harmonic': params_harmonic[key] + "configuration": params_configuration[key], + "harmonic": params_harmonic[key], } return merged_params + def concatenate_merge_params(name, merged_params, verbose=False): output = [name] for key in merged_params.keys(): - param_config = merged_params[key]['configuration'] - param_harm = merged_params[key]['harmonic'] + param_config = merged_params[key]["configuration"] + param_harm = merged_params[key]["harmonic"] if verbose: - print(f"{name} - {key} (Configuration): {param_config['mean']:.4f} +{param_config['1sigma_plus']:.4f}/-{param_config['1sigma_minus']:.4f} (1σ), +{param_config['2sigma_plus']:.4f}/-{param_config['2sigma_minus']:.4f} (2σ)") - print(f"{name} - {key} (Harmonic): {param_harm['mean']:.4f} +{param_harm['1sigma_plus']:.4f}/-{param_harm['1sigma_minus']:.4f} (1σ), +{param_harm['2sigma_plus']:.4f}/-{param_harm['2sigma_minus']:.4f} (2σ)") + print( + f"{name} - {key} (Configuration): {param_config['mean']:.4f} +{param_config['1sigma_plus']:.4f}/-{param_config['1sigma_minus']:.4f} (1σ), +{param_config['2sigma_plus']:.4f}/-{param_config['2sigma_minus']:.4f} (2σ)" + ) + print( + f"{name} - {key} (Harmonic): {param_harm['mean']:.4f} +{param_harm['1sigma_plus']:.4f}/-{param_harm['1sigma_minus']:.4f} (1σ), +{param_harm['2sigma_plus']:.4f}/-{param_harm['2sigma_minus']:.4f} (2σ)" + ) param_list = [ - param_config['mean'], - param_config['1sigma_minus'], - param_config['1sigma_plus'], - param_config['2sigma_minus'], - param_config['2sigma_plus'], - param_config['map_1D'], + param_config["mean"], + param_config["1sigma_minus"], + param_config["1sigma_plus"], + param_config["2sigma_minus"], + param_config["2sigma_plus"], + param_config["map_1D"], ] - if key == 'S_8' or key == 'OMEGA_M': - param_list.append(param_config['map_2D']) + if key == "S_8" or key == "OMEGA_M": + param_list.append(param_config["map_2D"]) param_list += [ - param_harm['mean'], - param_harm['1sigma_minus'], - param_harm['1sigma_plus'], - param_harm['2sigma_minus'], - param_harm['2sigma_plus'], - param_harm['map_1D'] + param_harm["mean"], + param_harm["1sigma_minus"], + param_harm["1sigma_plus"], + param_harm["2sigma_minus"], + param_harm["2sigma_plus"], + param_harm["map_1D"], ] - if key == 'S_8' or key == 'OMEGA_M': - param_list.append(param_harm['map_2D']) + if key == "S_8" or key == "OMEGA_M": + param_list.append(param_harm["map_2D"]) output += param_list return output + # %% chain_configuration = [] chain_harmonic = [] skip_weird = True for i, root in enumerate(tqdm(roots)): - if (i+1) in failed_simulations: - print(f"Skipping failed simulation {i+1}") - print(f"Add a flag 'ERROR' to the chains lists") - chain_configuration.append('ERROR') - chain_harmonic.append('ERROR') + if (i + 1) in failed_simulations: + print(f"Skipping failed simulation {i + 1}") + print("Add a flag 'ERROR' to the chains lists") + chain_configuration.append("ERROR") + chain_harmonic.append("ERROR") continue - if skip_weird and (i+1) in weird_simulations_v2: - print(f"Skipping weird simulation {i+1}") - print(f"Add a flag 'ERROR' to the chains lists") - chain_configuration.append('ERROR') - chain_harmonic.append('ERROR') + if skip_weird and (i + 1) in weird_simulations_v2: + print(f"Skipping weird simulation {i + 1}") + print("Add a flag 'ERROR' to the chains lists") + chain_configuration.append("ERROR") + chain_harmonic.append("ERROR") continue # Load samples and write paramnames for configuration space load_samples_and_write_paramames(root_dir, root, chain_type="configuration") write_samples_getdist_format(root_dir, root, chain_type="configuration") chain_config = g.samples_for_root( - root_dir + f'/{root}/{root}/getdist_{root}', + root_dir + f"/{root}/{root}/getdist_{root}", cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) chain_configuration.append(chain_config) @@ -237,46 +248,95 @@ def concatenate_merge_params(name, merged_params, verbose=False): load_samples_and_write_paramames(root_dir, root, chain_type="harmonic") write_samples_getdist_format(root_dir, root, chain_type="harmonic") chain_harm = g.samples_for_root( - root_dir + f'/{root}/{root}/getdist_{root}_cell', + root_dir + f"/{root}/{root}/getdist_{root}_cell", cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) chain_harmonic.append(chain_harm) # %% -param_names = ['S_8', 'OMEGA_M', 'SIGMA_8'] - -output_mocks_config = np.array([ - "Name", "S8_mean", "S8_1sigma_minus", "S8_1sigma_plus", "S8_2sigma_minus", "S8_2sigma_plus", "S8_map_1D", "S8_map_2D", - "OMEGA_M_mean", "OMEGA_M_1sigma_minus", "OMEGA_M_1sigma_plus", "OMEGA_M_2sigma_minus", "OMEGA_M_2sigma_plus", "OMEGA_M_map_1D", "OMEGA_M_map_2D", - "SIGMA_8_mean", "SIGMA_8_1sigma_minus", "SIGMA_8_1sigma_plus", "SIGMA_8_2sigma_minus", "SIGMA_8_2sigma_plus", "SIGMA_8_map_1D" -]) - +param_names = ["S_8", "OMEGA_M", "SIGMA_8"] + +output_mocks_config = np.array( + [ + "Name", + "S8_mean", + "S8_1sigma_minus", + "S8_1sigma_plus", + "S8_2sigma_minus", + "S8_2sigma_plus", + "S8_map_1D", + "S8_map_2D", + "OMEGA_M_mean", + "OMEGA_M_1sigma_minus", + "OMEGA_M_1sigma_plus", + "OMEGA_M_2sigma_minus", + "OMEGA_M_2sigma_plus", + "OMEGA_M_map_1D", + "OMEGA_M_map_2D", + "SIGMA_8_mean", + "SIGMA_8_1sigma_minus", + "SIGMA_8_1sigma_plus", + "SIGMA_8_2sigma_minus", + "SIGMA_8_2sigma_plus", + "SIGMA_8_map_1D", + ] +) output_mocks_harm = copy.deepcopy(output_mocks_config) -output_mocks_merged = np.array([ - "Name", - "S8_config_mean", "S8_config_1sigma_minus", "S8_config_1sigma_plus", "S8_config_2sigma_minus", "S8_config_2sigma_plus", "S8_config_map_1D", "S8_config_map_2D", - "S8_harm_mean", "S8_harm_1sigma_minus", "S8_harm_1sigma_plus", "S8_harm_2sigma_minus", "S8_harm_2sigma_plus", "S8_harm_map_1D", "S8_harm_map_2D", - "OMEGA_M_config_mean", "OMEGA_M_config_1sigma_minus", "OMEGA_M_config_1sigma_plus", "OMEGA_M_config_2sigma_minus", "OMEGA_M_config_2sigma_plus", "OMEGA_M_config_map_1D", "OMEGA_M_config_map_2D", - "OMEGA_M_harm_mean", "OMEGA_M_harm_1sigma_minus", "OMEGA_M_harm_1sigma_plus", "OMEGA_M_harm_2sigma_minus", "OMEGA_M_harm_2sigma_plus", "OMEGA_M_harm_map_1D", "OMEGA_M_harm_map_2D", - "SIGMA_8_config_mean", "SIGMA_8_config_1sigma_minus", "SIGMA_8_config_1sigma_plus", "SIGMA_8_config_2sigma_minus", "SIGMA_8_config_2sigma_plus", "SIGMA_8_config_map_1D", - "SIGMA_8_harm_mean", "SIGMA_8_harm_1sigma_minus", "SIGMA_8_harm_1sigma_plus", "SIGMA_8_harm_2sigma_minus", "SIGMA_8_harm_2sigma_plus", "SIGMA_8_harm_map_1D" -]) +output_mocks_merged = np.array( + [ + "Name", + "S8_config_mean", + "S8_config_1sigma_minus", + "S8_config_1sigma_plus", + "S8_config_2sigma_minus", + "S8_config_2sigma_plus", + "S8_config_map_1D", + "S8_config_map_2D", + "S8_harm_mean", + "S8_harm_1sigma_minus", + "S8_harm_1sigma_plus", + "S8_harm_2sigma_minus", + "S8_harm_2sigma_plus", + "S8_harm_map_1D", + "S8_harm_map_2D", + "OMEGA_M_config_mean", + "OMEGA_M_config_1sigma_minus", + "OMEGA_M_config_1sigma_plus", + "OMEGA_M_config_2sigma_minus", + "OMEGA_M_config_2sigma_plus", + "OMEGA_M_config_map_1D", + "OMEGA_M_config_map_2D", + "OMEGA_M_harm_mean", + "OMEGA_M_harm_1sigma_minus", + "OMEGA_M_harm_1sigma_plus", + "OMEGA_M_harm_2sigma_minus", + "OMEGA_M_harm_2sigma_plus", + "OMEGA_M_harm_map_1D", + "OMEGA_M_harm_map_2D", + "SIGMA_8_config_mean", + "SIGMA_8_config_1sigma_minus", + "SIGMA_8_config_1sigma_plus", + "SIGMA_8_config_2sigma_minus", + "SIGMA_8_config_2sigma_plus", + "SIGMA_8_config_map_1D", + "SIGMA_8_harm_mean", + "SIGMA_8_harm_1sigma_minus", + "SIGMA_8_harm_1sigma_plus", + "SIGMA_8_harm_2sigma_minus", + "SIGMA_8_harm_2sigma_plus", + "SIGMA_8_harm_map_1D", + ] +) for i, root in enumerate(tqdm(roots)): - if chain_configuration[i] == 'ERROR' or chain_harmonic[i] == 'ERROR': - param = [ - root ] + [ np.nan for _ in range(output_mocks_config.shape[1] - 1) - ] - param_merged = [ - root ] + [ np.nan for _ in range(output_mocks_merged.shape[1] - 1) + if chain_configuration[i] == "ERROR" or chain_harmonic[i] == "ERROR": + param = [root] + [np.nan for _ in range(output_mocks_config.shape[1] - 1)] + param_merged = [root] + [ + np.nan for _ in range(output_mocks_merged.shape[1] - 1) ] output_mocks_merged = np.vstack((output_mocks_merged, param_merged)) output_mocks_config = np.vstack((output_mocks_config, param)) @@ -290,13 +350,28 @@ def concatenate_merge_params(name, merged_params, verbose=False): param_config = concatenate_param_stats(root, param_values_config, verbose=False) param_harm = concatenate_param_stats(root, param_values_harm, verbose=False) param_merged = concatenate_merge_params(root, param_merged, verbose=False) - + output_mocks_config = np.vstack((output_mocks_config, param_config)) output_mocks_harm = np.vstack((output_mocks_harm, param_harm)) output_mocks_merged = np.vstack((output_mocks_merged, param_merged)) -np.savetxt(f"{root_dir}/summary_parameter_constraints_configuration_space_{version}.txt", output_mocks_config, fmt='%s', delimiter=';') -np.savetxt(f"{root_dir}/summary_parameter_constraints_harmonic_space_{version}.txt", output_mocks_harm, fmt='%s', delimiter=';') -np.savetxt(f"{root_dir}/summary_parameter_constraints_merged_{version}.txt", output_mocks_merged, fmt='%s', delimiter=';') +np.savetxt( + f"{root_dir}/summary_parameter_constraints_configuration_space_{version}.txt", + output_mocks_config, + fmt="%s", + delimiter=";", +) +np.savetxt( + f"{root_dir}/summary_parameter_constraints_harmonic_space_{version}.txt", + output_mocks_harm, + fmt="%s", + delimiter=";", +) +np.savetxt( + f"{root_dir}/summary_parameter_constraints_merged_{version}.txt", + output_mocks_merged, + fmt="%s", + delimiter=";", +) # %% diff --git a/papers/consistency/2026_01_15_compare_harmonic_scale_cut.py b/papers/consistency/2026_01_15_compare_harmonic_scale_cut.py index c65368ba..e7efcf03 100644 --- a/papers/consistency/2026_01_15_compare_harmonic_scale_cut.py +++ b/papers/consistency/2026_01_15_compare_harmonic_scale_cut.py @@ -1,6 +1,7 @@ # %% # Trick to plot with tex import os + os.environ["LD_LIBRARY_PATH"] = "" os.environ["CONDA_PREFIX"] = "/home/guerrini/.conda/envs/sp_validation_3.11" @@ -9,35 +10,26 @@ ipython = IPython.get_ipython() if ipython is not None: - ipython.run_line_magic('load_ext', 'autoreload') - ipython.run_line_magic('autoreload', '2') + ipython.run_line_magic("load_ext", "autoreload") + ipython.run_line_magic("autoreload", "2") import os -import copy -from tqdm import tqdm import matplotlib.pyplot as plt -from mpl_toolkits.axes_grid1 import make_axes_locatable import numpy as np -import healpy as hp -import seaborn as sns -from astropy.io import fits import pandas as pd +import seaborn as sns +from getdist import plots from scipy.stats import norm -from getdist import plots, MCSamples - - if os.path.exists("/home/guerrini/matplotlib_config/paper.mplstyle"): - plt.style.use( - "/home/guerrini/matplotlib_config/paper.mplstyle" - ) + plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") # Set default palette - will be updated per plot as needed sns.set_palette("husl") if ipython is not None: - ipython.run_line_magic('matplotlib', 'inline') + ipython.run_line_magic("matplotlib", "inline") # %% output_fig_path = "./plots" @@ -45,11 +37,14 @@ os.makedirs(output_fig_path) chain_version = "v2" -assert chain_version in [f"v{i}" for i in range(5)], "The chain run version is not correct." +assert chain_version in [f"v{i}" for i in range(5)], ( + "The chain run version is not correct." +) # Load the output of the simulations simulation_output = pd.read_csv( - f"/n09data/guerrini/glass_mock_chains/summary_parameter_constraints_harmonic_space_{chain_version}.txt", delimiter=";" + f"/n09data/guerrini/glass_mock_chains/summary_parameter_constraints_harmonic_space_{chain_version}.txt", + delimiter=";", ) # %% @@ -64,20 +59,22 @@ Omega_m_fid = planck.Om0 sigma_8_fid = 0.8054 -s8_fid = sigma_8_fid * (Omega_m_fid / 0.3)**0.5 +s8_fid = sigma_8_fid * (Omega_m_fid / 0.3) ** 0.5 h = planck.h Omega_b_fig = planck.Ob0 n_s_fid = 0.965 -print(f"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}") +print( + f"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}" +) # %% sns.histplot( simulation_output["S8_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.legend(fontsize=12) @@ -89,10 +86,7 @@ # %% sns.histplot( - simulation_output["S8_MAP"], - kde=True, - bins=20, - label="Configuration space" + simulation_output["S8_MAP"], kde=True, bins=20, label="Configuration space" ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.legend(fontsize=12) @@ -107,18 +101,18 @@ simulation_output["OMEGA_M_config_mean"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["OMEGA_M_harm_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.xlabel(r"$\Omega_m$ estimated from mocks") plt.title("Sample average") @@ -131,18 +125,18 @@ simulation_output["OMEGA_M_config_MAP"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["OMEGA_M_harm_MAP"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.xlabel(r"$\Omega_m$ estimated from mocks") plt.title("MAP") @@ -154,18 +148,18 @@ simulation_output["SIGMA_8_config_mean"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["SIGMA_8_harm_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.xlabel(r"$\sigma_8$ estimated from mocks") plt.title("Sample average") @@ -178,18 +172,18 @@ simulation_output["SIGMA_8_config_MAP"], kde=True, bins=20, - label="Configuration space" + label="Configuration space", ) sns.histplot( simulation_output["SIGMA_8_harm_MAP"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) plt.axvline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") -plt.legend(fontsize=12) +plt.legend(fontsize=12) plt.xlabel(r"$\sigma_8$ estimated from mocks") plt.title("MAP") @@ -198,20 +192,17 @@ # %% sns.histplot( - simulation_output["a_config_MAP"], - kde=True, - bins=20, - label="Configuration space" + simulation_output["a_config_MAP"], kde=True, bins=20, label="Configuration space" ) sns.histplot( simulation_output["a_harm_MAP"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) -plt.axvline(0., color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") +plt.axvline(0.0, color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") plt.legend(fontsize=12) plt.xlabel(r"$A_{\rm IA}$ estimated from mocks") @@ -221,20 +212,17 @@ # %% sns.histplot( - simulation_output["a_config_mean"], - kde=True, - bins=20, - label="Configuration space" + simulation_output["a_config_mean"], kde=True, bins=20, label="Configuration space" ) sns.histplot( simulation_output["a_harm_mean"], kde=True, label="Harmonic space", - color='blue', + color="blue", bins=20, - alpha = 0.3 + alpha=0.3, ) -plt.axvline(0., color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") +plt.axvline(0.0, color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") plt.legend(fontsize=12) plt.xlabel(r"$A_{\rm IA}$ estimated from mocks") @@ -247,13 +235,13 @@ simulation_output["S8_config_mean"] - simulation_output["S8_harm_mean"], kde=False, bins=20, - stat='density', - label="Difference (Config - Harm)" + stat="density", + label="Difference (Config - Harm)", ) plt.xlabel(r"$\Delta S_8$ estimated from mocks") plt.legend(fontsize=12) plt.savefig(f"{output_fig_path}/S8_difference_config_harm.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm.pdf") plt.show() @@ -262,13 +250,13 @@ simulation_output["S8_config_MAP"] - simulation_output["S8_harm_MAP"], kde=False, bins=20, - stat='density', - label="Difference (Config - Harm)" + stat="density", + label="Difference (Config - Harm)", ) plt.xlabel(r"$\Delta S_8$ estimated from mocks") plt.legend(fontsize=12) plt.savefig(f"{output_fig_path}/S8_difference_config_harm_map.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm_map.pdf") plt.show() @@ -278,15 +266,21 @@ x="S8_config_mean", y="S8_harm_mean", bins=25, - cbar='seismic', - cbar_kws={'label': 'Prob.'}, + cbar="seismic", + cbar_kws={"label": "Prob."}, ) plt.plot( - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - color='grey', - linestyle='--', - alpha=1.0 + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + color="grey", + linestyle="--", + alpha=1.0, ) plt.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") plt.xlabel(r"$S_8$ estimated from mocks (Configuration space)") @@ -300,8 +294,8 @@ x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", bins=25, - cbar='seismic', - cbar_kws={'label': 'Counts'} + cbar="seismic", + cbar_kws={"label": "Counts"}, ) plt.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") @@ -312,7 +306,9 @@ plt.show() # %% -g = sns.JointGrid(data=simulation_output, x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", space=0) +g = sns.JointGrid( + data=simulation_output, x="OMEGA_M_config_mean", y="SIGMA_8_config_mean", space=0 +) g.plot_joint( sns.histplot, @@ -331,7 +327,9 @@ plt.show() # %% -g = sns.JointGrid(data=simulation_output, x="OMEGA_M_harm_mean", y="SIGMA_8_harm_mean", space=0) +g = sns.JointGrid( + data=simulation_output, x="OMEGA_M_harm_mean", y="SIGMA_8_harm_mean", space=0 +) g.plot_joint( sns.histplot, @@ -341,7 +339,7 @@ g.plot_marginals(sns.histplot, bins=25) g.ax_joint.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -g.ax_joint.axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") +g.ax_joint.axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") g.ax_joint.set_xlabel(r"$\Omega_m$ estimated from mocks (Harmonic space)") g.ax_joint.set_ylabel(r"$\sigma_8$ estimated from mocks (Harmonic space)") @@ -349,7 +347,9 @@ plt.show() # %% -g = sns.JointGrid(data=simulation_output, x="OMEGA_M_harm_MAP", y="SIGMA_8_harm_MAP", space=0) +g = sns.JointGrid( + data=simulation_output, x="OMEGA_M_harm_MAP", y="SIGMA_8_harm_MAP", space=0 +) g.plot_joint( sns.histplot, @@ -359,7 +359,7 @@ g.plot_marginals(sns.histplot, bins=25) g.ax_joint.axvline(Omega_m_fid, color="black", linestyle="--", label="Fiducial Omega_m") -g.ax_joint.axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") +g.ax_joint.axhline(sigma_8_fid, color="black", linestyle="--", label="Fiducial sigma_8") g.ax_joint.set_xlabel(r"$\Omega_m$ estimated from mocks (Harmonic space)") g.ax_joint.set_ylabel(r"$\sigma_8$ estimated from mocks (Harmonic space)") @@ -377,7 +377,7 @@ g.plot_marginals(sns.histplot, bins=25) g.ax_joint.axvline(s8_fid, color="black", linestyle="--", label=r"Fiducial $S_8$") -g.ax_joint.axhline(0, color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") +g.ax_joint.axhline(0, color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") g.ax_joint.set_xlabel(r"$S_8$ estimated from mocks (Harmonic space)") g.ax_joint.set_ylabel(r"$A_{\rm IA}$ estimated from mocks (Harmonic space)") @@ -395,7 +395,7 @@ g.plot_marginals(sns.histplot, bins=25) g.ax_joint.axvline(s8_fid, color="black", linestyle="--", label=r"Fiducial $S_8$") -g.ax_joint.axhline(0, color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") +g.ax_joint.axhline(0, color="black", linestyle="--", label=r"Fiducial $A_{\rm IA}$") g.ax_joint.set_xlabel(r"$S_8$ estimated from mocks (Harmonic space)") g.ax_joint.set_ylabel(r"$A_{\rm IA}$ estimated from mocks (Harmonic space)") @@ -414,20 +414,26 @@ g.plot_marginals(sns.histplot, bins=25, kde=True) g.ax_joint.axvline(s8_fid, color="black", linestyle="--", label="Fiducial S8") -g.ax_joint.axhline(s8_fid, color="black", linestyle="--", label="Fiducial S8") +g.ax_joint.axhline(s8_fid, color="black", linestyle="--", label="Fiducial S8") g.ax_joint.set_xlabel(r"$S_8$ estimated from mocks (Configuration space)") g.ax_joint.set_ylabel(r"$S_8$ estimated from mocks (Harmonic space)") g.ax_joint.plot( - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - [simulation_output["S8_config_mean"].min(), simulation_output["S8_config_mean"].max()], - color='royalblue', - linestyle='-', - alpha=0.7 + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + [ + simulation_output["S8_config_mean"].min(), + simulation_output["S8_config_mean"].max(), + ], + color="royalblue", + linestyle="-", + alpha=0.7, ) plt.savefig(f"{output_fig_path}/S8_joint_config_harm.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_joint_config_harm.pdf") plt.show() @@ -436,63 +442,60 @@ # Get p-value g = plots.get_subplot_plotter(width_inch=7) -root_harmonic = 'SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell' -root_configuration = 'SP_v1.4.6_leak_corr_A_10_80' +root_harmonic = "SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell" +root_configuration = "SP_v1.4.6_leak_corr_A_10_80" # Load configuration space result -path_configuration = f"/n09data/guerrini/output_chains/{root_configuration}/getdist_{root_configuration}" +path_configuration = ( + f"/n09data/guerrini/output_chains/{root_configuration}/getdist_{root_configuration}" +) chain_configuration = g.samples_for_root( - path_configuration, - cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + path_configuration, + cache=False, + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) margestats = chain_configuration.getMargeStats() likestats = chain_configuration.getLikeStats() -param_stats = margestats.parWithName('S_8') +param_stats = margestats.parWithName("S_8") S8_configuration_analysis = param_stats.mean # Load harmonic space result -path_harmonic = f"/n09data/guerrini/output_chains/{root_harmonic}/getdist_{root_harmonic}" +path_harmonic = ( + f"/n09data/guerrini/output_chains/{root_harmonic}/getdist_{root_harmonic}" +) chain_harmonic = g.samples_for_root( - path_harmonic, - cache=False, - settings={ - 'ignore_rows': 0., - 'smooth_scale_2D': 0.5, - 'smooth_scale_1D': 0.5 - } + path_harmonic, + cache=False, + settings={"ignore_rows": 0.0, "smooth_scale_2D": 0.5, "smooth_scale_1D": 0.5}, ) margestats = chain_harmonic.getMargeStats() likestats = chain_harmonic.getLikeStats() -param_stats = margestats.parWithName('S_8') +param_stats = margestats.parWithName("S_8") S8_harmonic_analysis = param_stats.mean delta_S8_analysis = S8_configuration_analysis - S8_harmonic_analysis print(f"Delta S8 from analysis: {delta_S8_analysis}") # Select glass mocks without nan values -selection = ~np.isnan(simulation_output["S8_config_mean"]) & ~np.isnan(simulation_output["S8_harm_mean"]) -delta_S8 = (simulation_output["S8_config_mean"][selection] - simulation_output["S8_harm_mean"][selection]).values -counts, bin_edges = np.histogram( - delta_S8, - bins=20, - density=True +selection = ~np.isnan(simulation_output["S8_config_mean"]) & ~np.isnan( + simulation_output["S8_harm_mean"] ) +delta_S8 = ( + simulation_output["S8_config_mean"][selection] + - simulation_output["S8_harm_mean"][selection] +).values +counts, bin_edges = np.histogram(delta_S8, bins=20, density=True) sns.histplot( delta_S8, kde=False, bins=bin_edges, - stat='density', + stat="density", label="Difference (Config - Harm)", - color='blue', - alpha=0.3 + color="blue", + alpha=0.3, ) # Compute the p-value @@ -508,32 +511,36 @@ print(f"P-value for delta S8 = {delta_S8_analysis}: {p_value}") -plt.axvline(delta_S8_analysis, color="red", linestyle="--", label="Difference in the analysis") +plt.axvline( + delta_S8_analysis, color="red", linestyle="--", label="Difference in the analysis" +) mantissa, exponent = f"{p_value:.1e}".split("e") exponent = int(exponent) pte_string = rf"${{\rm PTE}} = {mantissa} \times 10^{{{exponent}}}$" plt.text( - -0.065, 15, + -0.065, + 15, pte_string, - color='black', + color="black", fontsize=12, ) n_sigma = norm.isf(p_value) -sign = '-' if delta_S8_analysis < 0 else '+' +sign = "-" if delta_S8_analysis < 0 else "+" print(f"Number of sigma corresponding to the p-value: {n_sigma}") plt.text( - -0.065, 13, + -0.065, + 13, rf"$N_\sigma = {sign}{n_sigma:.2f}\sigma$", - color='black', + color="black", fontsize=12, ) plt.xlabel(r"$\Delta S_8$ estimated from mocks") plt.legend(fontsize=12, framealpha=1.0) plt.savefig(f"{output_fig_path}/S8_difference_config_harm.png", dpi=300) -#Save PDF +# Save PDF plt.savefig(f"{output_fig_path}/S8_difference_config_harm.pdf") plt.show() diff --git a/papers/consistency/2026_02_11_test_sampling_glass_mocks.ipynb b/papers/consistency/2026_02_11_test_sampling_glass_mocks.ipynb deleted file mode 100644 index 53101453..00000000 --- a/papers/consistency/2026_02_11_test_sampling_glass_mocks.ipynb +++ /dev/null @@ -1,444 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "0", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "\n", - "# Trick to have TeX figures\n", - "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", - "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", - "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "import seaborn as sns\n", - "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", - "\n", - "sns.set_palette(\"husl\")\n", - "\n", - "g = plots.get_subplot_plotter(width_inch=7)\n", - "g.settings.axes_fontsize=15\n", - "g.settings.axes_labelsize=15\n", - "g.settings.alpha_filled_add = 0.7\n", - "g.settings.legend_fontsize = 15\n", - "\n", - "%matplotlib inline\n", - "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", - "root_dir = \"/n09data/guerrini/glass_mock_chains/glass_mock_v2_00001/glass_mock_v2_00001/\"\n", - "\n", - "roots = [\n", - " f\"glass_mock_v2_00001_cell\",\n", - " f\"glass_mock_v2_00001_cell_mh\",\n", - " f\"glass_mock_v2_00001_cell_no_sys\"\n", - "]\n", - "\n", - "print(roots)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1", - "metadata": {}, - "outputs": [], - "source": [ - "# MAKE PARAMNAMES FILE\n", - "\n", - "for root in roots:\n", - " try:\n", - " if root != \"glass_mock_v2_00001_cell_mh\":\n", - " with open(root_dir + '/samples_{}.txt'.format(root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", - " file.close()\n", - " else:\n", - " with open(root_dir + '/samples_{}_1.txt'.format(root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-2]\n", - " file.close()\n", - " \n", - " with open(root_dir + '/getdist_{}.paramnames'.format(root), \"w\") as file:\n", - " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", - " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", - " file.close()\n", - " except Exception as e:\n", - " print(e)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2", - "metadata": {}, - "outputs": [], - "source": [ - "#READ CHAIN\n", - "\n", - "chains=[]\n", - "mask_labels = []\n", - "\n", - "for root in roots:\n", - "\n", - " try:\n", - " if root != \"glass_mock_v2_00001_cell_mh\":\n", - " samples = np.loadtxt(root_dir + 'samples_{}.txt'.format(root))\n", - " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", - " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-2],samples[:,0:-4]))\n", - " np.savetxt(root_dir + 'getdist_{}.txt'.format(root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + 'getdist_{}'.format(root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", - "\n", - " chains.append(chain)\n", - " mask_labels.append(1)\n", - " else:\n", - " for i in range(1,5):\n", - " samples = np.loadtxt(root_dir + 'samples_{}_{}.txt'.format(root, i))\n", - " print(len(samples))\n", - " samples = np.column_stack((samples[:,-1],samples[:,-2],samples[:,0:-2]))\n", - " np.savetxt(root_dir + 'getdist_{}_{}.txt'.format(root, i), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + 'getdist_{}'.format(root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.3,\n", - " 'smooth_scale_1D':0.3})\n", - "\n", - " chains.append(chain)\n", - " mask_labels.append(1)\n", - " except Exception as e:\n", - " print(\"ERRROOOOOOOOR\")\n", - " print(e)\n", - " mask_labels.append(0)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3", - "metadata": {}, - "outputs": [], - "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}']\n", - "\n", - "for chain in chains:\n", - " param_names = chain.getParamNames()\n", - " for name, label in zip(name_list, label_list):\n", - " param_names.parWithName(name).label = label" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4", - "metadata": {}, - "outputs": [], - "source": [ - "from astropy.cosmology import Planck18 as planck\n", - "\n", - "Omega_m_fid = planck.Om0\n", - "sigma_8_fid = 0.8054\n", - "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3)**0.5\n", - "h = planck.h\n", - "Omega_b_fig = planck.Ob0\n", - "n_s_fid = 0.965\n", - "print(f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\")\n", - "\n", - "markers = {\n", - " \"OMEGA_M\": Omega_m_fid,\n", - " \"SIGMA_8\": sigma_8_fid,\n", - " \"s_8_input\": s8_fid,\n", - " \"h0\": h,\n", - " \"ombh2\": Omega_b_fig * h**2,\n", - " \"n_s\": n_s_fid\n", - "}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5", - "metadata": {}, - "outputs": [], - "source": [ - "legend_labels = np.array([\n", - " \"GLASS mock fiducial\",\n", - " \"GLASS mock with MH\",\n", - " \"GLASS mock no sys\"\n", - "])\n", - "\n", - "legend_labels = legend_labels[np.array(mask_labels, dtype=bool)]\n", - "\n", - "\n", - "\"\"\" #Plot all parameters\n", - "g.triangle_plot(\n", - " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", - " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", - " filled=True,\n", - " markers=markers\n", - ")\n", - "\n", - "plt.show() \"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6", - "metadata": {}, - "outputs": [], - "source": [ - "#Plot only cosmological parameters\n", - "\n", - "g.triangle_plot(\n", - " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", - " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", - " filled=True,\n", - " markers=markers\n", - ")\n", - "\n", - "\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7", - "metadata": {}, - "outputs": [], - "source": [ - "preliminary_watermark = False\n", - "#Plot S8 Omega_m only\n", - "g.triangle_plot(\n", - " chains,\n", - " ['OMEGA_M','s_8_input'],\n", - " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", - " filled=True,\n", - " title_limit=1,\n", - " markers=markers\n", - ")\n", - "\n", - "if preliminary_watermark:\n", - " plt.figtext(0.5, 0.5, 'PRELIMINARY',\n", - " fontsize=150, color='gray',\n", - " ha='center', va='center',\n", - " alpha=0.3, rotation=330)\n", - "\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8", - "metadata": {}, - "outputs": [], - "source": [ - "len(chains)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9", - "metadata": {}, - "outputs": [], - "source": [ - "for i, chain in enumerate(chains):\n", - " print(legend_labels[i])\n", - " samples = chain.samples\n", - "\n", - " likestats = chain.getLikeStats()\n", - " margestats = chain.getMargeStats()\n", - "\n", - " param_name = 'S_8'\n", - " print(param_name)\n", - " param_names = chain.getParamNames()\n", - " par = param_names.parWithName(param_name)\n", - " param_stats = margestats.parWithName(param_name)\n", - " print(\"Average:\", param_stats.mean)\n", - "\n", - " idx_map = np.argmax(chain.loglikes)\n", - " s8_index_in_samples = chain.index[param_name]\n", - " S8_map = samples[idx_map, s8_index_in_samples]\n", - " print(\"MAP:\", S8_map)\n", - "\n", - " S8_maxlike = samples[-1, s8_index_in_samples]\n", - " print(\"Max Likelihood:\", S8_maxlike)\n", - "\n", - " samples = chain.samples\n", - "\n", - " likestats = chain.getLikeStats()\n", - " margestats = chain.getMargeStats()\n", - "\n", - " param_name = 'OMEGA_M'\n", - " print(param_name)\n", - " param_names = chain.getParamNames()\n", - " par = param_names.parWithName(param_name)\n", - " param_stats = margestats.parWithName(param_name)\n", - " print(\"Average:\", param_stats.mean)\n", - "\n", - " idx_map = np.argmax(chain.loglikes)\n", - " s8_index_in_samples = chain.index[param_name]\n", - " S8_map = samples[idx_map, s8_index_in_samples]\n", - " print(\"MAP:\", S8_map)\n", - "\n", - " S8_maxlike = samples[-1, s8_index_in_samples]\n", - " print(\"Max Likelihood:\", S8_maxlike)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "10", - "metadata": {}, - "outputs": [], - "source": [ - "s8_samples = samples[:, s8_index_in_samples]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "11", - "metadata": {}, - "outputs": [], - "source": [ - "np.average(s8_samples, weights=chain.weights)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "12", - "metadata": {}, - "outputs": [], - "source": [ - "chain.weights" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "13", - "metadata": {}, - "outputs": [], - "source": [ - "chains=[]\n", - "mask_labels = []\n", - "\n", - "for root in roots:\n", - "\n", - " try:\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", - " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", - " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-2],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", - "\n", - " chains.append(chain)\n", - "\n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.1,\n", - " 'smooth_scale_1D':0.1})\n", - "\n", - " chains.append(chain)\n", - " mask_labels.append(1)\n", - " except Exception as e:\n", - " print(e)\n", - " mask_labels.append(0)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "14", - "metadata": {}, - "outputs": [], - "source": [ - "#Plot only cosmological parameters\n", - "legend_labels = [\n", - " label\n", - " for i in range(index, index + n_contours)\n", - " for label in (\n", - " rf\"$C_\\ell$, GLASS mock {i} (smooth 0.5)\",\n", - " rf\"$C_\\ell$, GLASS mock {i} (smooth 0.1)\"\n", - " )\n", - "]\n", - "\n", - "g.triangle_plot(\n", - " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", - " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", - " filled=True,\n", - " markers=markers\n", - ")\n", - "\n", - "\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "15", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "sp_validation_3.11", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/papers/harmonic/2025_09_11_namaster_covariance.py b/papers/harmonic/2025_09_11_namaster_covariance.py index 7e3c7693..aea3fc7e 100644 --- a/papers/harmonic/2025_09_11_namaster_covariance.py +++ b/papers/harmonic/2025_09_11_namaster_covariance.py @@ -8,27 +8,23 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import camb +import healpy as hp +import matplotlib.pyplot as plt import numpy as np -from astropy.io import fits +import pymaster as nmt +import seaborn as sns from astropy.cosmology import Planck18 -import matplotlib.pyplot as plt +from astropy.io import fits from matplotlib import scale as mscale -import seaborn as sns -from tqdm import tqdm -import healpy as hp -import pymaster as nmt -import camb -from sp_validation.rho_tau import SquareRootScale from sp_validation.cosmo_val import CosmologyValidation -from sp_validation.rho_tau import get_params_rho_tau +from sp_validation.rho_tau import SquareRootScale, get_params_rho_tau mscale.register_scale(SquareRootScale) -plt.style.use( - '/home/guerrini/matplotlib_config/paper.mplstyle' -) -plt.rcParams['text.usetex'] = False +plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") +plt.rcParams["text.usetex"] = False sns.set_palette("husl") @@ -43,12 +39,12 @@ nside = 1024 nside_mask = 4096 -lmax = 2*nside +lmax = 2 * nside path_redshift_distr = "/n23data1/n06data/lgoh/scratch/UNIONS/cosmo_inference/data/SP_v1.4.5_A/nz_SP_v1.4.5_A.txt" pw = hp.pixwin(nside, lmax=lmax) # %% planck = Planck18 -h = planck.H0.value/100 +h = planck.H0.value / 100 Om = planck.Om0 Ob = planck.Ob0 Oc = Om - Ob @@ -58,33 +54,53 @@ w = -1 print(h, Om, Ob) -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, ns=ns, mnu=m_nu, w=w, As=As, WantTransfer=True, NonLinear=camb.model.NonLinear_both) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + ns=ns, + mnu=m_nu, + w=w, + As=As, + WantTransfer=True, + NonLinear=camb.model.NonLinear_both, +) Onu = pars.omeganu Oc = Om - Ob - Onu -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, ns=ns, mnu=m_nu, w=w, As=As, WantTransfer=True, NonLinear=camb.model.NonLinear_both) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + ns=ns, + mnu=m_nu, + w=w, + As=As, + WantTransfer=True, + NonLinear=camb.model.NonLinear_both, +) print(camb.get_results(pars).get_sigma8()) z, dndz = np.loadtxt(path_redshift_distr, unpack=True) -#getthe expected cl's from CAMB +# getthe expected cl's from CAMB pars.min_l = 1 pars.set_for_lmax(lmax) pars.SourceWindows = [ - camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type='lensing') + camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type="lensing") ] theory_cls = camb.get_results(pars).get_source_cls_dict(lmax=lmax, raw_cl=True) fiducial_cl = theory_cls["W1xW1"] # %% lmin = 8 -lmax = 2*nside +lmax = 2 * nside b_lmax = lmax - 1 -ells = np.arange(lmin, lmax+1) -start = np.power(lmin, 1/2) -stop = np.power(lmax, 1/2) -bins_ell = np.power(np.linspace(start, stop, 32+1), 2) +ells = np.arange(lmin, lmax + 1) +start = np.power(lmin, 1 / 2) +stop = np.power(lmax, 1 / 2) +bins_ell = np.power(np.linspace(start, stop, 32 + 1), 2) -bpws = np.digitize(ells.astype(float), bins_ell) -1 +bpws = np.digitize(ells.astype(float), bins_ell) - 1 bpws[0] = 0 bpws[-1] = 31 @@ -93,7 +109,7 @@ cv = CosmologyValidation( versions=["SP_v1.4.5_leak_corr"], data_base_dir="/n17data/mkilbing/astro/data/", - catalog_config="/home/guerrini/sp_validation/cosmo_val/cat_config.yaml" + catalog_config="/home/guerrini/sp_validation/cosmo_val/cat_config.yaml", ) # %% params = get_params_rho_tau(cv.cc["SP_v1.4.5_leak_corr"], survey="SP_v1.4.5_leak_corr") @@ -104,21 +120,24 @@ mask = n_gal > 0 print("Computing noise...") -cl_noise, f, wsp = cv.get_sample(params, nside, b_lmax, b, cat_gal, n_gal, mask, unique_pix, idx_rep) +cl_noise, f, wsp = cv.get_sample( + params, nside, b_lmax, b, cat_gal, n_gal, mask, unique_pix, idx_rep +) -fiducial_cl = np.array([fiducial_cl, 0.*fiducial_cl, 0.*fiducial_cl, 0.*fiducial_cl])+ np.mean(cl_noise, axis=1, keepdims=True) +fiducial_cl = np.array( + [fiducial_cl, 0.0 * fiducial_cl, 0.0 * fiducial_cl, 0.0 * fiducial_cl] +) + np.mean(cl_noise, axis=1, keepdims=True) # %% mask_8192 = hp.read_map("/n09data/guerrini/glass_mock/mask_v1.4.5_nside8192.fits") # %% cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) # %% -covar_22_22 = nmt.gaussian_covariance(cw, 2, 2, 2, 2, - fiducial_cl, - fiducial_cl, - fiducial_cl, - fiducial_cl, - wsp, wb=wsp).reshape([32, 4, 32, 4]) +covar_22_22 = nmt.gaussian_covariance( + cw, 2, 2, 2, 2, fiducial_cl, fiducial_cl, fiducial_cl, fiducial_cl, wsp, wb=wsp +).reshape([32, 4, 32, 4]) + + # %% def get_cov_from_one_cov(cov_one_cov, gaussian=True): """ @@ -132,37 +151,50 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): for i in range(n_bins): for j in range(n_bins): cov[i, j] = cov_one_cov[i * n_bins + j, index_value] - + return cov + # %% covar_EE_EE = covar_22_22[:, 0, :, 0] -cov_one_cov = np.genfromtxt("/home/guerrini/OneCovariance/output/covariance_list_3x2pt_pure_Cell.dat") +cov_one_cov = np.genfromtxt( + "/home/guerrini/OneCovariance/output/covariance_list_3x2pt_pure_Cell.dat" +) ell = b.get_effective_ells() plt.figure() plt.plot(ell, np.diag(covar_EE_EE), label="NaMaster") -plt.plot(ell, np.diag(get_cov_from_one_cov(cov_one_cov, gaussian=True)), label="OneCovariance") -plt.xscale('squareroot') +plt.plot( + ell, + np.diag(get_cov_from_one_cov(cov_one_cov, gaussian=True)), + label="OneCovariance", +) +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.legend() plt.show() # %% plt.figure() -plt.plot(ell, np.sqrt(np.diag(covar_EE_EE)/np.diag(get_cov_from_one_cov(cov_one_cov, gaussian=True))), label="NaMaster/OneCovariance") -plt.xscale('squareroot') +plt.plot( + ell, + np.sqrt( + np.diag(covar_EE_EE) / np.diag(get_cov_from_one_cov(cov_one_cov, gaussian=True)) + ), + label="NaMaster/OneCovariance", +) +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.legend() diff --git a/papers/harmonic/2025_09_11_psf_leakage_cell.py b/papers/harmonic/2025_09_11_psf_leakage_cell.py index b8456dcc..c388affb 100644 --- a/papers/harmonic/2025_09_11_psf_leakage_cell.py +++ b/papers/harmonic/2025_09_11_psf_leakage_cell.py @@ -14,24 +14,22 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import healpy as hp +import matplotlib.pyplot as plt +import matplotlib.ticker as mticker import numpy as np +import pymaster as nmt +import seaborn as sns from astropy.io import fits -import matplotlib.pyplot as plt from matplotlib import scale as mscale -import matplotlib.ticker as mticker from mpl_toolkits.axes_grid1 import make_axes_locatable -import seaborn as sns -import healpy as hp -import pymaster as nmt from sp_validation.rho_tau import SquareRootScale mscale.register_scale(SquareRootScale) -plt.style.use( - '/home/guerrini/matplotlib_config/paper.mplstyle' -) -plt.rcParams['text.usetex'] = True +plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") +plt.rcParams["text.usetex"] = True sns.set_palette("Dark2") @@ -39,7 +37,7 @@ ipython.run_line_magic("matplotlib", "inline") # %% -#Import galaxy and star catalogs +# Import galaxy and star catalogs cat_gal = fits.getdata( "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" ) @@ -50,23 +48,25 @@ glass_root_dir = "/n09data/guerrini/glass_mock_v1.4.6.3_v2/results/" + # %% def get_shape_noise(e1, e2, w): shape_noise = 0.5 * np.sum(w**2 * (e1**2 + e2**2)) / np.sum(w**2) return np.sqrt(shape_noise) + # %% -#Define namaster binning +# Define namaster binning lmin = 8 lmax = 2048 b_lmax = lmax - 1 n_ells = 32 -ells = np.arange(lmin, lmax+1) +ells = np.arange(lmin, lmax + 1) -start = np.power(lmin, 1/2) -stop = np.power(lmax, 1/2) +start = np.power(lmin, 1 / 2) +stop = np.power(lmax, 1 / 2) bins_ell = np.power(np.linspace(start, stop, 33), 2) bpws = np.digitize(ells.astype(float), bins_ell) - 1 @@ -79,39 +79,39 @@ def get_shape_noise(e1, e2, w): # %% ell_eff = b.get_effective_ells() -#Create namaster fields +# Create namaster fields f_gal_corrected = nmt.NmtFieldCatalog( - positions=[cat_gal['RA'], cat_gal['DEC']], - weights=cat_gal['w_des'], - field=[cat_gal['e1_leak_corrected'], -cat_gal['e2_leak_corrected']], + positions=[cat_gal["RA"], cat_gal["DEC"]], + weights=cat_gal["w_des"], + field=[cat_gal["e1_leak_corrected"], -cat_gal["e2_leak_corrected"]], lmax=b_lmax, lmax_mask=b_lmax, spin=2, - lonlat=True + lonlat=True, ) f_gal = nmt.NmtFieldCatalog( - positions=[cat_gal['RA'], cat_gal['DEC']], - weights=cat_gal['w_des'], - field=[cat_gal['e1'], -cat_gal['e2']], + positions=[cat_gal["RA"], cat_gal["DEC"]], + weights=cat_gal["w_des"], + field=[cat_gal["e1"], -cat_gal["e2"]], lmax=b_lmax, lmax_mask=b_lmax, spin=2, - lonlat=True + lonlat=True, ) f_psf = nmt.NmtFieldCatalog( - positions=[cat_star['RA'], cat_star['DEC']], - weights=np.ones_like(cat_star['RA']), - field=[cat_star['E1_PSF_HSM'], -cat_star['E2_PSF_HSM']], + positions=[cat_star["RA"], cat_star["DEC"]], + weights=np.ones_like(cat_star["RA"]), + field=[cat_star["E1_PSF_HSM"], -cat_star["E2_PSF_HSM"]], lmax=b_lmax, lmax_mask=b_lmax, spin=2, - lonlat=True + lonlat=True, ) # %% -#Compute rho_0 +# Compute rho_0 wsp = nmt.NmtWorkspace.from_fields(f_psf, f_psf, b) @@ -119,7 +119,7 @@ def get_shape_noise(e1, e2, w): rho_cl = wsp.decouple_cell(rho_cl) # %% -#Compute tau_0 +# Compute tau_0 wsp = nmt.NmtWorkspace.from_fields(f_gal, f_psf, b) tau_cl = nmt.compute_coupled_cell(f_gal, f_psf) @@ -131,7 +131,7 @@ def get_shape_noise(e1, e2, w): tau_cl_corrected = wsp.decouple_cell(tau_cl_corrected) # %% -#Compute cell +# Compute cell wsp = nmt.NmtWorkspace.from_fields(f_gal, f_gal, b) @@ -142,11 +142,12 @@ def get_shape_noise(e1, e2, w): cell_cl_corrected = nmt.compute_coupled_cell(f_gal_corrected, f_gal_corrected) cell_cl_corrected = wsp.decouple_cell(cell_cl_corrected) + # %% # Compute the covariance for rho_0 and tau_0 with iNKA def project_cat_map(cat, e1_col, e2_col, nside=1024, w_col=None): - ra = cat['RA'] - dec = cat['DEC'] + ra = cat["RA"] + dec = cat["DEC"] e1 = cat[e1_col] e2 = cat[e2_col] @@ -157,18 +158,27 @@ def project_cat_map(cat, e1_col, e2_col, nside=1024, w_col=None): theta = np.radians(90.0 - dec) phi = np.radians(ra) pix_indices = hp.ang2pix(nside, theta, phi) - unique_pix, idx, idx_rep = np.unique(pix_indices, return_index=True, return_inverse=True) + unique_pix, idx, idx_rep = np.unique( + pix_indices, return_index=True, return_inverse=True + ) count_map = np.zeros(hp.nside2npix(nside)) count_map[unique_pix] = np.bincount(idx_rep, weights=weights) e1_map = np.zeros(hp.nside2npix(nside)) e2_map = np.zeros(hp.nside2npix(nside)) - e1_map[unique_pix] = np.bincount(idx_rep, weights=weights * e1) / count_map[unique_pix] - e2_map[unique_pix] = np.bincount(idx_rep, weights=weights * e2) / count_map[unique_pix] + e1_map[unique_pix] = ( + np.bincount(idx_rep, weights=weights * e1) / count_map[unique_pix] + ) + e2_map[unique_pix] = ( + np.bincount(idx_rep, weights=weights * e2) / count_map[unique_pix] + ) return count_map, e1_map, e2_map + def get_field_catalog(cat, e1_col, e2_col, nside, w_col=None): - count_map, e1_map, e2_map = project_cat_map(cat, e1_col, e2_col, nside=nside, w_col=w_col) + count_map, e1_map, e2_map = project_cat_map( + cat, e1_col, e2_col, nside=nside, w_col=w_col + ) field = nmt.NmtField( mask=count_map, @@ -178,10 +188,12 @@ def get_field_catalog(cat, e1_col, e2_col, nside, w_col=None): ) return field + def get_workspace(field1, field2): wsp = nmt.NmtWorkspace.from_fields(field1, field2, b) return wsp + def get_cell(field1, field2, wsp, unbin=False, coupled=False): cl_coupled = nmt.compute_coupled_cell(field1, field2) if coupled: @@ -194,34 +206,40 @@ def get_cell(field1, field2, wsp, unbin=False, coupled=False): cl_decoupled[i, :lowest_ell] = cl_decoupled[i, lowest_ell] return cl_decoupled + def compute_iNKA_covariance(field1, field2, coupled=False): - + cw = nmt.NmtCovarianceWorkspace.from_fields(field1, field2, field1, field2) wsp = get_workspace(field1, field2) if coupled: - cl_a1b1 = get_cell(field1, field1, get_workspace(field1, field1), unbin=False, coupled=True) / np.mean(field1.mask**2) - cl_a1b2 = get_cell(field1, field2, wsp, unbin=False, coupled=True) / np.mean(field1.mask * field2.mask) - cl_a2b2 = get_cell(field2, field2, get_workspace(field2, field2), unbin=False, coupled=True) / np.mean(field2.mask**2) + cl_a1b1 = get_cell( + field1, field1, get_workspace(field1, field1), unbin=False, coupled=True + ) / np.mean(field1.mask**2) + cl_a1b2 = get_cell(field1, field2, wsp, unbin=False, coupled=True) / np.mean( + field1.mask * field2.mask + ) + cl_a2b2 = get_cell( + field2, field2, get_workspace(field2, field2), unbin=False, coupled=True + ) / np.mean(field2.mask**2) else: cl_a1b1 = get_cell(field1, field1, get_workspace(field1, field1), unbin=True) cl_a1b2 = get_cell(field1, field2, wsp, unbin=True) cl_a2b2 = get_cell(field2, field2, get_workspace(field2, field2), unbin=True) - cov = nmt.gaussian_covariance(cw, 2, 2, 2, 2, - cl_a1b1, - cl_a1b2, - cl_a1b2, - cl_a2b2, - wsp, wb=wsp).reshape([n_ells, 4, n_ells, 4]) + cov = nmt.gaussian_covariance( + cw, 2, 2, 2, 2, cl_a1b1, cl_a1b2, cl_a1b2, cl_a2b2, wsp, wb=wsp + ).reshape([n_ells, 4, n_ells, 4]) return cov - + # %% -field_psf_map = get_field_catalog(cat_star, 'E1_PSF_HSM', 'E2_PSF_HSM', nside=1024) -field_gal_map = get_field_catalog(cat_gal, 'e1', 'e2', nside=1024, w_col='w_des') -field_gal_corrected_map = get_field_catalog(cat_gal, 'e1_leak_corrected', 'e2_leak_corrected', nside=1024, w_col='w_des') +field_psf_map = get_field_catalog(cat_star, "E1_PSF_HSM", "E2_PSF_HSM", nside=1024) +field_gal_map = get_field_catalog(cat_gal, "e1", "e2", nside=1024, w_col="w_des") +field_gal_corrected_map = get_field_catalog( + cat_gal, "e1_leak_corrected", "e2_leak_corrected", nside=1024, w_col="w_des" +) # %% cov_rho_0 = compute_iNKA_covariance(field_psf_map, field_psf_map, coupled=True) @@ -232,7 +250,7 @@ def compute_iNKA_covariance(field1, field2, coupled=False): print("Computed cov_tau_0_corrected") # %% -#Correct the diagonal of cov_rho +# Correct the diagonal of cov_rho for i in range(4): for j in range(4): for k in range(32): @@ -241,41 +259,47 @@ def compute_iNKA_covariance(field1, field2, coupled=False): # Get covariance of rho_0 and tau_0 n_sims = 350 -rho_0_cls = np.array([]).reshape((0, 32*4)) -tau_0_cls = np.array([]).reshape((0, 32*4)) +rho_0_cls = np.array([]).reshape((0, 32 * 4)) +tau_0_cls = np.array([]).reshape((0, 32 * 4)) for i in range(n_sims): - index_sim = str(i+1).zfill(5) - rho_0 = np.load(glass_root_dir + f"rho_cl_glass_mock_{index_sim}_4096.npy")[1:].reshape((32*4,)) - tau_0 = np.load(glass_root_dir + f"tau_cl_glass_mock_{index_sim}_4096.npy")[1:].reshape((32*4,)) + index_sim = str(i + 1).zfill(5) + rho_0 = np.load(glass_root_dir + f"rho_cl_glass_mock_{index_sim}_4096.npy")[ + 1: + ].reshape((32 * 4,)) + tau_0 = np.load(glass_root_dir + f"tau_cl_glass_mock_{index_sim}_4096.npy")[ + 1: + ].reshape((32 * 4,)) rho_0_cls = np.vstack((rho_0_cls, rho_0)) tau_0_cls = np.vstack((tau_0_cls, tau_0)) cov_rho_0_sim = np.cov(rho_0_cls.T) cov_tau_0_sim = np.cov(tau_0_cls.T) + # %% def cov_to_corr(cov): diag = np.sqrt(np.diag(cov)) corr = cov / np.outer(diag, diag) return corr + # %% fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10, 3)) plt.subplots_adjust(wspace=0.3) -im0 = ax0.imshow(cov_to_corr(cov_tau_0[:, 0,:,0]), vmin=-1, vmax=1, cmap='coolwarm') +im0 = ax0.imshow(cov_to_corr(cov_tau_0[:, 0, :, 0]), vmin=-1, vmax=1, cmap="coolwarm") ax0.set_title(r"Covariance $\tau_0$ iNKA") divider = make_axes_locatable(ax0) cax0 = divider.append_axes("right", size="5%", pad=0.1) cbar0 = fig.colorbar(im0, cax=cax0) -im1 = ax1.imshow(cov_to_corr(cov_tau_0_sim[:32,:32]), vmin=-1, vmax=1, cmap='coolwarm') +im1 = ax1.imshow(cov_to_corr(cov_tau_0_sim[:32, :32]), vmin=-1, vmax=1, cmap="coolwarm") ax1.set_title(r"Covariance $\tau_0$") divider = make_axes_locatable(ax1) cax1 = divider.append_axes("right", size="5%", pad=0.1) cbar1 = fig.colorbar(im1, cax=cax1) -im2 = ax2.imshow(cov_to_corr(cov_rho_0[:, 0,:,0]), vmin=-1, vmax=1, cmap='coolwarm') +im2 = ax2.imshow(cov_to_corr(cov_rho_0[:, 0, :, 0]), vmin=-1, vmax=1, cmap="coolwarm") ax2.set_title(r"Covariance $\rho_0$ iNKA") divider = make_axes_locatable(ax2) cax2 = divider.append_axes("right", size="5%", pad=0.1) @@ -289,16 +313,18 @@ def cov_to_corr(cov): plt.plot(ell_eff, np.sqrt(np.diag(cov_tau_0[:, 0, :, 0])), label="iNKA") plt.plot(ell_eff, np.sqrt(np.diag(cov_tau_0_sim[:32, :32])), label="Simulations") -plt.plot(ell_eff, np.sqrt(np.diag(cov_tau_0_corrected[:, 0, :, 0])), label="iNKA corrected") +plt.plot( + ell_eff, np.sqrt(np.diag(cov_tau_0_corrected[:, 0, :, 0])), label="iNKA corrected" +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") -plt.yscale('log') +plt.yscale("log") plt.ylabel(r"$\sigma(C_\ell^{\tau_0})$") plt.legend() @@ -314,16 +340,30 @@ def cov_to_corr(cov): # %% plt.figure() -plt.errorbar(ell_eff, ell_eff*rho_cl[0], yerr=ell_eff*np.sqrt(np.abs(cov_rho_0_ee.diagonal())), label=r"$\rho_0$ EE", fmt='o', capsize=2) -plt.errorbar(ell_eff, ell_eff*rho_cl[3], yerr=ell_eff*np.sqrt(np.abs(cov_rho_0_bb.diagonal())), label=r"$\rho_0$ BB", fmt='o', capsize=2) +plt.errorbar( + ell_eff, + ell_eff * rho_cl[0], + yerr=ell_eff * np.sqrt(np.abs(cov_rho_0_ee.diagonal())), + label=r"$\rho_0$ EE", + fmt="o", + capsize=2, +) +plt.errorbar( + ell_eff, + ell_eff * rho_cl[3], + yerr=ell_eff * np.sqrt(np.abs(cov_rho_0_bb.diagonal())), + label=r"$\rho_0$ BB", + fmt="o", + capsize=2, +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -#plt.xlim(100, 2048) +# plt.xlim(100, 2048) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C^{\rho_0}_\ell$") plt.legend() @@ -348,21 +388,49 @@ def cov_to_corr(cov): list_offset.append(jittered_ell) -plt.errorbar(list_offset[0], ell_eff*tau_cl[0], yerr=ell_eff*np.sqrt(cov_tau_0_ee.diagonal()), label=r"$\tau_0$ EE", fmt='o', capsize=2) -plt.errorbar(list_offset[1], ell_eff*tau_cl_corrected[0], yerr=ell_eff*np.sqrt(cov_tau_0_ee.diagonal()), label=r"$\tau_0$ corrected EE", fmt='o', capsize=2) -plt.errorbar(list_offset[2], ell_eff*tau_cl[3], yerr=ell_eff*np.sqrt(cov_tau_0_bb.diagonal()), label=r"$\tau_0$ BB", fmt='o', capsize=2) -plt.errorbar(list_offset[3], ell_eff*tau_cl_corrected[3], yerr=ell_eff*np.sqrt(cov_tau_0_bb.diagonal()), label=r"$\tau_0$ corrected BB", fmt='o', capsize=2) +plt.errorbar( + list_offset[0], + ell_eff * tau_cl[0], + yerr=ell_eff * np.sqrt(cov_tau_0_ee.diagonal()), + label=r"$\tau_0$ EE", + fmt="o", + capsize=2, +) +plt.errorbar( + list_offset[1], + ell_eff * tau_cl_corrected[0], + yerr=ell_eff * np.sqrt(cov_tau_0_ee.diagonal()), + label=r"$\tau_0$ corrected EE", + fmt="o", + capsize=2, +) +plt.errorbar( + list_offset[2], + ell_eff * tau_cl[3], + yerr=ell_eff * np.sqrt(cov_tau_0_bb.diagonal()), + label=r"$\tau_0$ BB", + fmt="o", + capsize=2, +) +plt.errorbar( + list_offset[3], + ell_eff * tau_cl_corrected[3], + yerr=ell_eff * np.sqrt(cov_tau_0_bb.diagonal()), + label=r"$\tau_0$ corrected BB", + fmt="o", + capsize=2, +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.axhline(0, c='k', ls='--', alpha=0.7) +plt.axhline(0, c="k", ls="--", alpha=0.7) -plt.tick_params(axis='both', which='major', labelsize=16) +plt.tick_params(axis="both", which="major", labelsize=16) plt.gca().yaxis.get_offset_text().set_visible(False) @@ -373,21 +441,17 @@ def cov_to_corr(cov): plt.ylim(-5e-7, 5e-7) plt.legend(fontsize=16) plt.savefig("./plots/tau_0_cl.png", dpi=300) -#Save pdf +# Save pdf plt.savefig("./plots/tau_0_cl.pdf") plt.show() # %% tau_cl_samples = np.random.multivariate_normal( - mean=tau_cl[0], - cov=cov_tau_0_ee, - size=10000 + mean=tau_cl[0], cov=cov_tau_0_ee, size=10000 ) tau_cl_corrected_samples = np.random.multivariate_normal( - mean=tau_cl_corrected[0], - cov=cov_tau_0_ee, - size=10000 + mean=tau_cl_corrected[0], cov=cov_tau_0_ee, size=10000 ) alpha_ell_samples = tau_cl_samples / rho_cl[0] @@ -411,16 +475,30 @@ def cov_to_corr(cov): jiterred_ell = ell_eff + jitter_fraction * ell_widths list_offset.append(jiterred_ell) -plt.errorbar(list_offset[0], tau_cl[0]/rho_cl[0], yerr=alpha_ell_std, label="Without object-wise leakage correction", fmt='o', capsize=2) -plt.errorbar(list_offset[1], tau_cl_corrected[0]/rho_cl[0], yerr=alpha_ell_corrected_std, label="With object-wise leakage correction", fmt='o', capsize=2) +plt.errorbar( + list_offset[0], + tau_cl[0] / rho_cl[0], + yerr=alpha_ell_std, + label="Without object-wise leakage correction", + fmt="o", + capsize=2, +) +plt.errorbar( + list_offset[1], + tau_cl_corrected[0] / rho_cl[0], + yerr=alpha_ell_corrected_std, + label="With object-wise leakage correction", + fmt="o", + capsize=2, +) -plt.xscale('squareroot') +plt.xscale("squareroot") -plt.axhline(0., color='black', linestyle='--', alpha=0.6) +plt.axhline(0.0, color="black", linestyle="--", alpha=0.6) plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$", fontsize=12) plt.ylabel(r"$\alpha_\ell$", fontsize=12) @@ -434,16 +512,22 @@ def cov_to_corr(cov): # %% plt.figure() -plt.plot(ell_eff, ell_eff*cell_cl[0], label=r"$C^{EE}_\ell$ uncorrected") -plt.plot(ell_eff, ell_eff*tau_cl[0]**2/rho_cl[0], label="Leakage bias uncorrected") -plt.plot(ell_eff, ell_eff*cell_cl_corrected[0], label=r"$C^{EE}_\ell$ corrected") -plt.plot(ell_eff, ell_eff*tau_cl_corrected[0]**2/rho_cl[0], label="Leakage bias corrected") +plt.plot(ell_eff, ell_eff * cell_cl[0], label=r"$C^{EE}_\ell$ uncorrected") +plt.plot( + ell_eff, ell_eff * tau_cl[0] ** 2 / rho_cl[0], label="Leakage bias uncorrected" +) +plt.plot(ell_eff, ell_eff * cell_cl_corrected[0], label=r"$C^{EE}_\ell$ corrected") +plt.plot( + ell_eff, + ell_eff * tau_cl_corrected[0] ** 2 / rho_cl[0], + label="Leakage bias corrected", +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") @@ -454,16 +538,16 @@ def cov_to_corr(cov): # %% plt.figure() -plt.plot(ell_eff, ell_eff*cell_cl[0], label=r"$C^{EE}_\ell$ uncorrected") -#plt.plot(ell_eff, ell_eff*tau_cl[0]**2/rho_cl[0], label="Leakage bias uncorrected") -plt.plot(ell_eff, ell_eff*cell_cl_corrected[0], label=r"$C^{EE}_\ell$ corrected") -#plt.plot(ell_eff, ell_eff*tau_cl_corrected[0]**2/rho_cl[0], label="Leakage bias corrected") +plt.plot(ell_eff, ell_eff * cell_cl[0], label=r"$C^{EE}_\ell$ uncorrected") +# plt.plot(ell_eff, ell_eff*tau_cl[0]**2/rho_cl[0], label="Leakage bias uncorrected") +plt.plot(ell_eff, ell_eff * cell_cl_corrected[0], label=r"$C^{EE}_\ell$ corrected") +# plt.plot(ell_eff, ell_eff*tau_cl_corrected[0]**2/rho_cl[0], label="Leakage bias corrected") -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") @@ -474,16 +558,16 @@ def cov_to_corr(cov): # %% plt.figure() -plt.plot(ell_eff, ell_eff*cell_cl[3], label=r"$C^{BB}_\ell$ uncorrected") -#plt.plot(ell_eff, ell_eff*tau_cl[0]**2/rho_cl[0], label="Leakage bias uncorrected") -plt.plot(ell_eff, ell_eff*cell_cl_corrected[3], label=r"$C^{BB}_\ell$ corrected") -#plt.plot(ell_eff, ell_eff*tau_cl_corrected[0]**2/rho_cl[0], label="Leakage bias corrected") +plt.plot(ell_eff, ell_eff * cell_cl[3], label=r"$C^{BB}_\ell$ uncorrected") +# plt.plot(ell_eff, ell_eff*tau_cl[0]**2/rho_cl[0], label="Leakage bias uncorrected") +plt.plot(ell_eff, ell_eff * cell_cl_corrected[3], label=r"$C^{BB}_\ell$ corrected") +# plt.plot(ell_eff, ell_eff*tau_cl_corrected[0]**2/rho_cl[0], label="Leakage bias corrected") -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") @@ -494,15 +578,23 @@ def cov_to_corr(cov): # %% plt.figure() -plt.plot(ell_eff, cell_cl_corrected[0]/cell_cl[0] - 1, label=r"Ratio corrected/uncorrected EE") -plt.plot(ell_eff, cell_cl_corrected[3]/cell_cl[3] - 1, label=r"Ratio corrected/uncorrected BB") +plt.plot( + ell_eff, + cell_cl_corrected[0] / cell_cl[0] - 1, + label=r"Ratio corrected/uncorrected EE", +) +plt.plot( + ell_eff, + cell_cl_corrected[3] / cell_cl[3] - 1, + label=r"Ratio corrected/uncorrected BB", +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\Delta C_\ell / C_\ell$") @@ -515,14 +607,14 @@ def cov_to_corr(cov): # %% plt.figure() -plt.plot(ell_eff, ell_eff*cell_cl_corrected[0], label=r"$C^{EE}_\ell$") -plt.plot(ell_eff, ell_eff*tau_cl_corrected[0]**2/rho_cl[0], label="Leakage bias") +plt.plot(ell_eff, ell_eff * cell_cl_corrected[0], label=r"$C^{EE}_\ell$") +plt.plot(ell_eff, ell_eff * tau_cl_corrected[0] ** 2 / rho_cl[0], label="Leakage bias") -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") @@ -541,22 +633,45 @@ def cov_to_corr(cov): # %% plt.figure() -leakage_bias = tau_cl[0]**2/rho_cl[0] -leakage_bias_corrected = tau_cl_corrected[0]**2/rho_cl[0] - -plt.errorbar(list_offset[0], leakage_bias/cell_cl[0], yerr=leakage_bias_std/cell_cl[0], label=r"Without object-wise leakage correction", fmt='o', markersize=3, capsize=2) -plt.errorbar(list_offset[1], leakage_bias_corrected/cell_cl_corrected[0], yerr=leakage_bias_corrected_std/cell_cl_corrected[0], label="With object-wise leakage correction", fmt='o', markersize=3, capsize=2) +leakage_bias = tau_cl[0] ** 2 / rho_cl[0] +leakage_bias_corrected = tau_cl_corrected[0] ** 2 / rho_cl[0] + +plt.errorbar( + list_offset[0], + leakage_bias / cell_cl[0], + yerr=leakage_bias_std / cell_cl[0], + label=r"Without object-wise leakage correction", + fmt="o", + markersize=3, + capsize=2, +) +plt.errorbar( + list_offset[1], + leakage_bias_corrected / cell_cl_corrected[0], + yerr=leakage_bias_corrected_std / cell_cl_corrected[0], + label="With object-wise leakage correction", + fmt="o", + markersize=3, + capsize=2, +) threshold = 0.05 -plt.fill_between(np.arange(7, 2050), -threshold, threshold, color='gray', alpha=0.3, label=r'5\% threshold') -plt.xscale('squareroot') +plt.fill_between( + np.arange(7, 2050), + -threshold, + threshold, + color="gray", + alpha=0.3, + label=r"5\% threshold", +) +plt.xscale("squareroot") -plt.axhline(threshold, color='black', linestyle='--', alpha=0.6) -plt.axhline(-threshold, color='black', linestyle='--', alpha=0.6) +plt.axhline(threshold, color="black", linestyle="--", alpha=0.6) +plt.axhline(-threshold, color="black", linestyle="--", alpha=0.6) plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlim(ell_eff[0], ell_eff[-1]) plt.xlabel(r"Multipole $\ell$", fontsize=16) @@ -566,20 +681,16 @@ def cov_to_corr(cov): plt.gca().yaxis.set_major_formatter(mticker.PercentFormatter(xmax=1)) plt.legend(loc="upper center", fontsize=12) plt.savefig("./plots/leakage_bias_fraction_ee.png", dpi=300) -#Save PDF +# Save PDF plt.savefig("./plots/leakage_bias_fraction_ee.pdf") plt.show() # %% tau_samples_bb = np.random.multivariate_normal( - mean=tau_cl[3], - cov=cov_tau_0_bb, - size=10000 + mean=tau_cl[3], cov=cov_tau_0_bb, size=10000 ) tau_corrected_samples_bb = np.random.multivariate_normal( - mean=tau_cl_corrected[3], - cov=cov_tau_0_bb, - size=10000 + mean=tau_cl_corrected[3], cov=cov_tau_0_bb, size=10000 ) leakage_bias_samples_bb = tau_samples_bb**2 / rho_cl[3] leakage_bias_corrected_samples_bb = tau_corrected_samples_bb**2 / rho_cl[3] @@ -591,17 +702,33 @@ def cov_to_corr(cov): # %% plt.figure() -leakage_bias = tau_cl[3]**2/rho_cl[3] -leakage_bias_corrected = tau_cl_corrected[3]**2/rho_cl[3] - -plt.errorbar(list_offset[0], leakage_bias, yerr=leakage_bias_std_bb, label=r"Without object-wise leakage correction", fmt='o', markersize=3, capsize=2) -plt.errorbar(list_offset[1], leakage_bias_corrected, yerr=leakage_bias_corrected_std_bb, label="With object-wise leakage correction", fmt='o', markersize=3, capsize=2) +leakage_bias = tau_cl[3] ** 2 / rho_cl[3] +leakage_bias_corrected = tau_cl_corrected[3] ** 2 / rho_cl[3] + +plt.errorbar( + list_offset[0], + leakage_bias, + yerr=leakage_bias_std_bb, + label=r"Without object-wise leakage correction", + fmt="o", + markersize=3, + capsize=2, +) +plt.errorbar( + list_offset[1], + leakage_bias_corrected, + yerr=leakage_bias_corrected_std_bb, + label="With object-wise leakage correction", + fmt="o", + markersize=3, + capsize=2, +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell^{\rm sys, BB}$") @@ -613,16 +740,22 @@ def cov_to_corr(cov): # %% plt.figure() -plt.plot(ell_eff, ell_eff*cell_cl[3], label=r"$C^{BB}_\ell$ uncorrected") -plt.plot(ell_eff, ell_eff*tau_cl[3]**2/rho_cl[3], label="Leakage bias uncorrected") -plt.plot(ell_eff, ell_eff*cell_cl_corrected[3], label=r"$C^{BB}_\ell$ corrected") -plt.plot(ell_eff, ell_eff*tau_cl_corrected[3]**2/rho_cl[3], label="Leakage bias corrected") +plt.plot(ell_eff, ell_eff * cell_cl[3], label=r"$C^{BB}_\ell$ uncorrected") +plt.plot( + ell_eff, ell_eff * tau_cl[3] ** 2 / rho_cl[3], label="Leakage bias uncorrected" +) +plt.plot(ell_eff, ell_eff * cell_cl_corrected[3], label=r"$C^{BB}_\ell$ corrected") +plt.plot( + ell_eff, + ell_eff * tau_cl_corrected[3] ** 2 / rho_cl[3], + label="Leakage bias corrected", +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") @@ -632,17 +765,17 @@ def cov_to_corr(cov): # %% plt.figure() -leakage_bias = tau_cl[3]**2/rho_cl[3] -leakage_bias_corrected = tau_cl_corrected[3]**2/rho_cl[3] +leakage_bias = tau_cl[3] ** 2 / rho_cl[3] +leakage_bias_corrected = tau_cl_corrected[3] ** 2 / rho_cl[3] -plt.plot(ell_eff, leakage_bias/cell_cl[3], label=r"Uncorrected") -plt.plot(ell_eff, leakage_bias_corrected/cell_cl_corrected[3], label="Corrected") +plt.plot(ell_eff, leakage_bias / cell_cl[3], label=r"Uncorrected") +plt.plot(ell_eff, leakage_bias_corrected / cell_cl_corrected[3], label="Corrected") -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell^{\rm sys, BB} / C_\ell^\mathrm{BB}$") @@ -652,19 +785,31 @@ def cov_to_corr(cov): plt.show() # %% -#Plot signal with errorbars -pseudo_cl = fits.getdata("/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.5_leak_corr.fits") -cov_cl = fits.open("/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.5_leak_corr.fits") +# Plot signal with errorbars +pseudo_cl = fits.getdata( + "/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.5_leak_corr.fits" +) +cov_cl = fits.open( + "/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.5_leak_corr.fits" +) plt.figure() -plt.errorbar(ell_eff, ell_eff*pseudo_cl['EE'], yerr=ell_eff*np.sqrt(np.diag(cov_cl["COVAR_EE_EE"].data)), label=r"$C^{EE}_\ell$ SP_v1.4.5 corrected", fmt='o', markersize=3, capsize=2) +plt.errorbar( + ell_eff, + ell_eff * pseudo_cl["EE"], + yerr=ell_eff * np.sqrt(np.diag(cov_cl["COVAR_EE_EE"].data)), + label=r"$C^{EE}_\ell$ SP_v1.4.5 corrected", + fmt="o", + markersize=3, + capsize=2, +) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") @@ -676,17 +821,25 @@ def cov_to_corr(cov): # %% plt.figure() -plt.errorbar(ell_eff, ell_eff*pseudo_cl['BB'], yerr=ell_eff*np.sqrt(np.diag(cov_cl["COVAR_BB_BB"].data)), label=r"$C^{BB}_\ell$ SP_v1.4.5 corrected", fmt='o', markersize=3, capsize=2) -plt.xscale('squareroot') +plt.errorbar( + ell_eff, + ell_eff * pseudo_cl["BB"], + yerr=ell_eff * np.sqrt(np.diag(cov_cl["COVAR_BB_BB"].data)), + label=r"$C^{BB}_\ell$ SP_v1.4.5 corrected", + fmt="o", + markersize=3, + capsize=2, +) +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) plt.xlabel(r"$\ell$") plt.ylabel(r"$\ell C_\ell$") plt.legend() -plt.axhline(0, color='black', linestyle='--') +plt.axhline(0, color="black", linestyle="--") plt.title("BB Pseudo-Cl") plt.savefig("bb_cl_with_errors.png", dpi=300) diff --git a/papers/harmonic/2025_09_11_validation_cov_cell.py b/papers/harmonic/2025_09_11_validation_cov_cell.py index 25c2856c..f235b4d7 100644 --- a/papers/harmonic/2025_09_11_validation_cov_cell.py +++ b/papers/harmonic/2025_09_11_validation_cov_cell.py @@ -14,24 +14,20 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import matplotlib.pyplot as plt import numpy as np +import seaborn as sns from astropy.io import fits -import matplotlib.pyplot as plt -from matplotlib.gridspec import GridSpec -import matplotlib.ticker as mticker from matplotlib import scale as mscale +from matplotlib.gridspec import GridSpec from mpl_toolkits.axes_grid1 import make_axes_locatable -import seaborn as sns from tqdm import tqdm -import healpy as hp from sp_validation.rho_tau import SquareRootScale mscale.register_scale(SquareRootScale) -plt.style.use( - './matplotlib_config/paper.mplstyle' -) +plt.style.use("./matplotlib_config/paper.mplstyle") sns.set_palette("Dark2") @@ -47,22 +43,27 @@ # %% -#Get covariance from GLASS mock +# Get covariance from GLASS mock n_sims = 350 cls_all = np.array([]).reshape((0, 32)) for i in tqdm(range(n_sims)): - cls = np.load(f"{path_glass_sims_output}/cl_glass_mock_{str(i+1).zfill(5)}_4096.npy") + cls = np.load( + f"{path_glass_sims_output}/cl_glass_mock_{str(i + 1).zfill(5)}_4096.npy" + ) cls_all = np.vstack((cls_all, cls[1])) # %% cov_sim = np.cov(cls_all.T) + + # %% def cov_to_corr(cov): diag = np.sqrt(np.diag(cov)) corr = cov / np.outer(diag, diag) return corr + # %% def get_cov_from_one_cov(cov_one_cov, gaussian=True): """ @@ -76,14 +77,17 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): for i in range(n_bins): for j in range(n_bins): cov[i, j] = cov_one_cov[i * n_bins + j, index_value] - + return cov + # %% ell_eff = cls[0] # %% -cov_one_cov = np.genfromtxt(f"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_onecov_SP_v1.4.6.3_{blind}_leak_corr/covariance_list_3x2pt_pure_Cell.dat") +cov_one_cov = np.genfromtxt( + f"/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_onecov_SP_v1.4.6.3_{blind}_leak_corr/covariance_list_3x2pt_pure_Cell.dat" +) gaussian_one_cov = get_cov_from_one_cov(cov_one_cov, gaussian=True) all_one_cov = get_cov_from_one_cov(cov_one_cov, gaussian=False) @@ -93,7 +97,7 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10, 3)) plt.subplots_adjust(wspace=0.3) -im0 = ax0.imshow(cov_to_corr(cov_sim), vmin=-1, vmax=1, cmap='coolwarm') +im0 = ax0.imshow(cov_to_corr(cov_sim), vmin=-1, vmax=1, cmap="coolwarm") ax0.set_title(r"\texttt{GLASS} mocks") divider = make_axes_locatable(ax0) cax0 = divider.append_axes("right", size="5%", pad=0.1) @@ -105,7 +109,9 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax0.set_xlabel(r"Multipole $\ell$", fontsize=18) ax0.set_ylabel(r"Multipole $\ell$", fontsize=18) -im1 = ax1.imshow(cov_to_corr(cov_namaster["COVAR_EE_EE"].data), vmin=-1, vmax=1, cmap='coolwarm') +im1 = ax1.imshow( + cov_to_corr(cov_namaster["COVAR_EE_EE"].data), vmin=-1, vmax=1, cmap="coolwarm" +) ax1.set_title("iNKA") divider = make_axes_locatable(ax1) cax1 = divider.append_axes("right", size="5%", pad=0.1) @@ -117,7 +123,7 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax1.set_xlabel(r"Multipole $\ell$", fontsize=18) ax1.set_ylabel(r"Multipole $\ell$", fontsize=18) -im2 = ax2.imshow(cov_to_corr(gaussian_one_cov), vmin=-1, vmax=1, cmap='coolwarm') +im2 = ax2.imshow(cov_to_corr(gaussian_one_cov), vmin=-1, vmax=1, cmap="coolwarm") ax2.set_title(r"\texttt{OneCovariance} (Gaussian only)") divider = make_axes_locatable(ax2) cax2 = divider.append_axes("right", size="5%", pad=0.1) @@ -129,22 +135,27 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax2.set_xlabel(r"Multipole $\ell$", fontsize=18) ax2.set_ylabel(r"Multipole $\ell$", fontsize=18) -#fig.suptitle("Comparison of correlation matrices for $C_\ell^{EE}$") +# fig.suptitle("Comparison of correlation matrices for $C_\ell^{EE}$") -plt.savefig("./plots/corr_matrix_comparison", dpi=300, bbox_inches='tight') -#Save pdf -plt.savefig("./plots/corr_matrix_comparison.pdf", bbox_inches='tight') +plt.savefig("./plots/corr_matrix_comparison", dpi=300, bbox_inches="tight") +# Save pdf +plt.savefig("./plots/corr_matrix_comparison.pdf", bbox_inches="tight") plt.show() # %% -#Plot correlation matrix versus the Gaussian part. +# Plot correlation matrix versus the Gaussian part. cov_one_cov_non_gaussian = all_one_cov - gaussian_one_cov fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10, 3)) plt.subplots_adjust(wspace=0.3) -im0 = ax0.imshow(cov_to_corr(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian), vmin=-1, vmax=1, cmap='coolwarm') +im0 = ax0.imshow( + cov_to_corr(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian), + vmin=-1, + vmax=1, + cmap="coolwarm", +) ax0.set_title(r"iNKA + \texttt{OneCovariance} (NG)") divider = make_axes_locatable(ax0) cax0 = divider.append_axes("right", size="5%", pad=0.1) @@ -156,7 +167,7 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax0.set_xlabel(r"Multipole $\ell$", fontsize=18) ax0.set_ylabel(r"Multipole $\ell$", fontsize=18) -im1 = ax1.imshow(cov_to_corr(all_one_cov), vmin=-1, vmax=1, cmap='coolwarm') +im1 = ax1.imshow(cov_to_corr(all_one_cov), vmin=-1, vmax=1, cmap="coolwarm") ax1.set_title(r"\texttt{OneCovariance} (All terms)") divider = make_axes_locatable(ax1) cax1 = divider.append_axes("right", size="5%", pad=0.1) @@ -170,7 +181,7 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): diag = np.sqrt(np.diag(all_one_cov)) non_gaussian_corr = cov_one_cov_non_gaussian / np.outer(diag, diag) -im2 = ax2.imshow(non_gaussian_corr, cmap='coolwarm') +im2 = ax2.imshow(non_gaussian_corr, cmap="coolwarm") ax2.set_title(r"\texttt{OneCovariance} (Non-Gaussian only)") divider = make_axes_locatable(ax2) cax2 = divider.append_axes("right", size="5%", pad=0.1) @@ -182,11 +193,11 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax2.set_xlabel(r"Multipole $\ell$", fontsize=18) ax2.set_ylabel(r"Multipole $\ell$", fontsize=18) -#fig.suptitle("Comparison of correlation matrices for $C_\ell^{EE}$") +# fig.suptitle("Comparison of correlation matrices for $C_\ell^{EE}$") -plt.savefig("./plots/corr_matrix_comparison_non_gaussian", dpi=300, bbox_inches='tight') -#Save pdf -plt.savefig("./plots/corr_matrix_comparison_non_gaussian.pdf", bbox_inches='tight') +plt.savefig("./plots/corr_matrix_comparison_non_gaussian", dpi=300, bbox_inches="tight") +# Save pdf +plt.savefig("./plots/corr_matrix_comparison_non_gaussian.pdf", bbox_inches="tight") plt.show() # %% @@ -194,45 +205,71 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): plt.figure() plt.plot(ell, np.sqrt(np.diag(cov_sim)), label="GLASS mocks") -plt.plot(ell[1:], np.sqrt(np.abs(np.diag(cov_sim, k=1))), label="GLASS mocks (k=1)", linestyle='--', color='C0') +plt.plot( + ell[1:], + np.sqrt(np.abs(np.diag(cov_sim, k=1))), + label="GLASS mocks (k=1)", + linestyle="--", + color="C0", +) plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), label="NaMaster") -plt.plot(ell[1:], np.sqrt(np.abs(np.diag(cov_namaster["COVAR_EE_EE"].data, k=1))), label="NaMaster (k=1)", linestyle='--', color='C1') +plt.plot( + ell[1:], + np.sqrt(np.abs(np.diag(cov_namaster["COVAR_EE_EE"].data, k=1))), + label="NaMaster (k=1)", + linestyle="--", + color="C1", +) plt.plot(ell, np.sqrt(np.diag(gaussian_one_cov)), label="OneCovariance (Gaussian only)") -#plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") -plt.xscale('squareroot') +# plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.ylabel(r"$\sigma(C_\ell^{EE})$") plt.xlabel(r"$\ell$") plt.legend() -plt.savefig("./plots/errorbar_comparison", dpi=300, bbox_inches='tight') +plt.savefig("./plots/errorbar_comparison", dpi=300, bbox_inches="tight") plt.show() # %% -#Plot relative error of the errorbars on the first diagonal +# Plot relative error of the errorbars on the first diagonal ell = cls[0] plt.figure() -plt.plot(ell, np.sqrt(np.diag(cov_sim))/np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), label="GLASS mocks") -plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data))/np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), label="NaMaster") -plt.plot(ell, np.sqrt(np.diag(gaussian_one_cov))/np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), label="OneCovariance (Gaussian only)") -#plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") -plt.xscale('squareroot') +plt.plot( + ell, + np.sqrt(np.diag(cov_sim)) / np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), + label="GLASS mocks", +) +plt.plot( + ell, + np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)) + / np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), + label="NaMaster", +) +plt.plot( + ell, + np.sqrt(np.diag(gaussian_one_cov)) + / np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), + label="OneCovariance (Gaussian only)", +) +# plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -#plt.yscale('log') +# plt.yscale('log') plt.ylabel(r"$\sigma(C_\ell^{EE})$/$\sigma(C_\ell^{EE})_{NaMaster}$") plt.xlabel(r"$\ell$") plt.legend() -plt.savefig("./plots/relative_errorbar_comparison", dpi=300, bbox_inches='tight') +plt.savefig("./plots/relative_errorbar_comparison", dpi=300, bbox_inches="tight") plt.show() @@ -240,71 +277,121 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): plt.figure() plt.plot(ell, np.sqrt(np.diag(cov_sim)), label="GLASS mocks") -plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), label="NaMaster + NG OneCov") +plt.plot( + ell, + np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), + label="NaMaster + NG OneCov", +) plt.plot(ell, np.sqrt(np.diag(all_one_cov)), label="OneCovariance (All terms)") -plt.plot(ell, np.sqrt(np.diag(cov_one_cov_non_gaussian)), label="OneCovariance (Non-Gaussian only)", linestyle='--', color='C2') -plt.plot(ell[1:], np.sqrt(np.abs(np.diag(all_one_cov, k=1))), label="OneCovariance (k=1)", linestyle='-.', color='C2') -plt.plot(ell[1:], np.sqrt(np.abs(np.diag(cov_sim, k=1))), label="GLASS mocks (k=1)", linestyle='-.', color='C0') -#plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") -plt.xscale('squareroot') +plt.plot( + ell, + np.sqrt(np.diag(cov_one_cov_non_gaussian)), + label="OneCovariance (Non-Gaussian only)", + linestyle="--", + color="C2", +) +plt.plot( + ell[1:], + np.sqrt(np.abs(np.diag(all_one_cov, k=1))), + label="OneCovariance (k=1)", + linestyle="-.", + color="C2", +) +plt.plot( + ell[1:], + np.sqrt(np.abs(np.diag(cov_sim, k=1))), + label="GLASS mocks (k=1)", + linestyle="-.", + color="C0", +) +# plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.ylabel(r"$\sigma(C_\ell^{EE})$") plt.xlabel(r"$\ell$") plt.legend() -plt.savefig("./plots/errorbar_comparison_non_gaussian", dpi=300, bbox_inches='tight') +plt.savefig("./plots/errorbar_comparison_non_gaussian", dpi=300, bbox_inches="tight") plt.show() # %% plt.figure() -plt.plot(ell, np.sqrt(np.diag(cov_sim))/np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), label="GLASS mocks") -plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data))/np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), label="NaMaster") -plt.plot(ell, np.sqrt(np.diag(all_one_cov))/np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), label="OneCovariance (All terms)") -#plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") -plt.xscale('squareroot') +plt.plot( + ell, + np.sqrt(np.diag(cov_sim)) + / np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), + label="GLASS mocks", +) +plt.plot( + ell, + np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)) + / np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), + label="NaMaster", +) +plt.plot( + ell, + np.sqrt(np.diag(all_one_cov)) + / np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data + cov_one_cov_non_gaussian)), + label="OneCovariance (All terms)", +) +# plt.plot(ell, np.diag(cov_namaster_glass), label="NaMaster (GLASS input)") +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -#plt.yscale('log') +# plt.yscale('log') plt.ylabel(r"$\sigma(C_\ell^{EE})$/$\sigma(C_\ell^{EE})_\mathrm{NaMaster + nG OneCov}$") plt.xlabel(r"$\ell$") plt.legend() -plt.savefig("./plots/relative_errorbar_comparison_non_gaussian", dpi=300, bbox_inches='tight') +plt.savefig( + "./plots/relative_errorbar_comparison_non_gaussian", dpi=300, bbox_inches="tight" +) plt.show() # %% -cov_one_cov = np.genfromtxt("/home/guerrini/OneCovariance/output/covariance_list_3x2pt_pure_Cell.dat") -cov_namaster_glass = fits.open("/home/guerrini/sp_validation/glass_mock/output/pseudo_cl_cov_SP_v1.4.5_glass_mock.fits")["COVAR_EE_EE"].data +cov_one_cov = np.genfromtxt( + "/home/guerrini/OneCovariance/output/covariance_list_3x2pt_pure_Cell.dat" +) +cov_namaster_glass = fits.open( + "/home/guerrini/sp_validation/glass_mock/output/pseudo_cl_cov_SP_v1.4.5_glass_mock.fits" +)["COVAR_EE_EE"].data ell = cls[0] plt.figure() -#plt.plot(ell, np.diag(cov_sim), label="GLASS mocks") -#plt.plot(ell, np.diag(cov_namaster["COVAR_EE_EE"].data), label="NaMaster") -plt.plot(ell, np.diag(get_cov_from_one_cov(cov_one_cov, gaussian=True))/np.diag(cov_namaster["COVAR_EE_EE"].data), label="OneCovariance") -plt.xscale('squareroot') +# plt.plot(ell, np.diag(cov_sim), label="GLASS mocks") +# plt.plot(ell, np.diag(cov_namaster["COVAR_EE_EE"].data), label="NaMaster") +plt.plot( + ell, + np.diag(get_cov_from_one_cov(cov_one_cov, gaussian=True)) + / np.diag(cov_namaster["COVAR_EE_EE"].data), + label="OneCovariance", +) +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.legend() plt.show() # %% -#Get the covariance for BB +# Get the covariance for BB cls_all_BB = np.array([]).reshape((0, 32)) for i in tqdm(range(n_sims)): - cls = np.load(f"{path_glass_sims_output}/cl_glass_mock_{str(i+1).zfill(5)}_4096.npy") + cls = np.load( + f"{path_glass_sims_output}/cl_glass_mock_{str(i + 1).zfill(5)}_4096.npy" + ) cls_all_BB = np.vstack((cls_all_BB, cls[4])) cov_sim_BB = np.cov(cls_all_BB.T) @@ -314,13 +401,13 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): plt.plot(ell, np.sqrt(np.diag(cov_sim_BB)), label="GLASS mocks") plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_BB_BB"].data)), label="NaMaster") -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell^{BB})$") plt.legend() @@ -341,100 +428,128 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax2 = fig.add_subplot(gs[1], sharex=ax1) ax1.plot(ell, np.sqrt(np.diag(cov_sim)), label=r"\texttt{GLASS} mocks") -ax1.plot(ell, np.sqrt(np.diag(cov_inka_onecov_ng)), label=r"iNKA + \texttt{OneCovariance} (NG)") -ax1.plot(ell, np.sqrt(np.diag(all_one_cov)), label=r"\texttt{OneCovariance}", color='C2') -ax1.plot([], [], label="iNKA (Gaussian only)", color='C4') # Empty plot for legend - -#Plot second diagonal -ax1.plot(ell[1:], np.sqrt(np.abs(np.diag(cov_sim, k=1))), linestyle='--', color='C0') -ax1.plot(ell[1:], np.sqrt(np.abs(np.diag(cov_inka_onecov_ng, k=1))), linestyle='--', color='C1') -ax1.plot([], [], linestyle='--', color='k', label='Second diagonal') +ax1.plot( + ell, + np.sqrt(np.diag(cov_inka_onecov_ng)), + label=r"iNKA + \texttt{OneCovariance} (NG)", +) +ax1.plot( + ell, np.sqrt(np.diag(all_one_cov)), label=r"\texttt{OneCovariance}", color="C2" +) +ax1.plot([], [], label="iNKA (Gaussian only)", color="C4") # Empty plot for legend + +# Plot second diagonal +ax1.plot(ell[1:], np.sqrt(np.abs(np.diag(cov_sim, k=1))), linestyle="--", color="C0") +ax1.plot( + ell[1:], + np.sqrt(np.abs(np.diag(cov_inka_onecov_ng, k=1))), + linestyle="--", + color="C1", +) +ax1.plot([], [], linestyle="--", color="k", label="Second diagonal") -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-12, 1e-11, 1e-10, 1e-9, 1e-8]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-12 for i in range(1, 10)] + [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 10)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-12 for i in range(1, 10)] + + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 10)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell^{EE})$") ax1.legend(fontsize=10) -relative_error_sim = np.sqrt(np.diag(cov_sim))/np.sqrt(np.diag(cov_inka_onecov_ng)) -relative_error_onecov = np.sqrt(np.diag(all_one_cov))/np.sqrt(np.diag(cov_inka_onecov_ng)) -relative_error_inka_gaussian = np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data))/np.sqrt(np.diag(cov_inka_onecov_ng)) +relative_error_sim = np.sqrt(np.diag(cov_sim)) / np.sqrt(np.diag(cov_inka_onecov_ng)) +relative_error_onecov = np.sqrt(np.diag(all_one_cov)) / np.sqrt( + np.diag(cov_inka_onecov_ng) +) +relative_error_inka_gaussian = np.sqrt( + np.diag(cov_namaster["COVAR_EE_EE"].data) +) / np.sqrt(np.diag(cov_inka_onecov_ng)) ax2.plot(ell, relative_error_sim - 1, label="GLASS mocks") -ax2.plot(ell, relative_error_onecov - 1, label="OneCovariance", color='C2') -ax2.plot(ell, relative_error_inka_gaussian - 1, label="iNKA (Gaussian only)", color='C4') -ax2.axhline(0, color='C1', linestyle='-') +ax2.plot(ell, relative_error_onecov - 1, label="OneCovariance", color="C2") +ax2.plot( + ell, relative_error_inka_gaussian - 1, label="iNKA (Gaussian only)", color="C4" +) +ax2.axhline(0, color="C1", linestyle="-") -#Set ticks and scales for the y-axis +# Set ticks and scales for the y-axis ax2.set_yticks([-0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3]) ax2.set_ylim(-0.3, 0.3) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") ax2.set_xlabel(r"Multipole $\ell$") -plt.savefig("./plots/paper_plot_errorbar_validation.png", dpi=300, bbox_inches='tight') +plt.savefig("./plots/paper_plot_errorbar_validation.png", dpi=300, bbox_inches="tight") # Save PDF -plt.savefig("./plots/paper_plot_errorbar_validation.pdf", bbox_inches='tight') +plt.savefig("./plots/paper_plot_errorbar_validation.pdf", bbox_inches="tight") plt.show() # %% -#Validation against Gaussian simulations +# Validation against Gaussian simulations -path_gaussian_sims = "/n17data/sguerrini/sp_validation/cosmo_val/harmonic_covariance_gaussian_sims_v1463" +path_gaussian_sims = ( + "/n17data/sguerrini/sp_validation/cosmo_val/harmonic_covariance_gaussian_sims_v1463" +) n_sims = 10_000 covs = {} -for blind in ['A', 'B', 'C']: - path_folder = path_gaussian_sims+"_"+blind - cls_all_gaussian = np.array([]).reshape((0, 4, 32)) +for blind in ["A", "B", "C"]: + path_folder = path_gaussian_sims + "_" + blind + cls_all_gaussian = np.array([]).reshape((0, 4, 32)) cls_noise_gaussian = np.array([]).reshape((0, 4, 32)) for i in tqdm(range(n_sims)): try: cls = np.load(f"{path_folder}/sample_{i}.npz") - cls_all_gaussian = np.vstack((cls_all_gaussian, cls['cl_all'][None, ...])) - cls_noise_gaussian = np.vstack((cls_noise_gaussian, cls['cl_noise'][None, ...])) + cls_all_gaussian = np.vstack((cls_all_gaussian, cls["cl_all"][None, ...])) + cls_noise_gaussian = np.vstack( + (cls_noise_gaussian, cls["cl_noise"][None, ...]) + ) except Exception as e: print(f"Error loading {i}: {e}") cls_all_gaussian = cls_all_gaussian - np.mean(cls_noise_gaussian, axis=0) cls_all_gaussian = cls_all_gaussian.reshape((n_sims, -1)) cov_gaussian = np.cov(cls_all_gaussian.T) covs[blind] = {} - covs[blind]['cov_gaussian'] = cov_gaussian - covs[blind]['diag_EE'] = np.sqrt(np.diag(cov_gaussian[:32, :32])) - covs[blind]['diag_BB'] = np.sqrt(np.diag(cov_gaussian[96:, 96:])) - covs[blind]['diag_EB'] = np.sqrt(np.diag(cov_gaussian[64:96, 64:96])) + covs[blind]["cov_gaussian"] = cov_gaussian + covs[blind]["diag_EE"] = np.sqrt(np.diag(cov_gaussian[:32, :32])) + covs[blind]["diag_BB"] = np.sqrt(np.diag(cov_gaussian[96:, 96:])) + covs[blind]["diag_EB"] = np.sqrt(np.diag(cov_gaussian[64:96, 64:96])) # %% -diag_ee = covs['A']['diag_EE'] -diag_bb = covs['A']['diag_BB'] -diag_eb = covs['A']['diag_EB'] +diag_ee = covs["A"]["diag_EE"] +diag_bb = covs["A"]["diag_BB"] +diag_eb = covs["A"]["diag_EB"] # %% plt.figure() plt.plot(ell, diag_ee, label="EE from Gaussian sims") -plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), label="EE from NaMaster") -plt.xscale('squareroot') +plt.plot( + ell, np.sqrt(np.diag(cov_namaster["COVAR_EE_EE"].data)), label="EE from NaMaster" +) +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.ylabel(r"$\sigma(C_\ell^{EE})$") plt.xlabel(r"$\ell$") plt.legend() @@ -444,15 +559,17 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): plt.figure() plt.plot(ell, diag_bb, label="BB from Gaussian sims") -plt.plot(ell, np.sqrt(np.diag(cov_namaster["COVAR_BB_BB"].data)), label="BB from NaMaster") +plt.plot( + ell, np.sqrt(np.diag(cov_namaster["COVAR_BB_BB"].data)), label="BB from NaMaster" +) plt.plot(ell, np.sqrt(np.diag(cov_sim_BB)), label="BB from GLASS mocks") -plt.xscale('squareroot') +plt.xscale("squareroot") plt.xticks(np.array([100, 400, 900, 1600])) plt.minorticks_on() -plt.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +plt.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] plt.xticks(minor_ticks, minor=True) -plt.yscale('log') +plt.yscale("log") plt.ylabel(r"$\sigma(C_\ell^{BB})$") plt.xlabel(r"$\ell$") plt.legend() @@ -472,39 +589,45 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax1.plot(ell, diag_bb, label="Gaussian simulations") ax1.plot(ell, np.sqrt(np.diag(cov_inka_bb)), label="iNKA") -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-11, 1e-11, 1e-10, 1e-9]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 8)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 8)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell^{BB})$") ax1.legend(fontsize=10) -relative_error_sim = diag_bb/np.sqrt(np.diag(cov_inka_bb)) +relative_error_sim = diag_bb / np.sqrt(np.diag(cov_inka_bb)) ax2.plot(ell, relative_error_sim - 1, label="Gaussian simulations") -ax2.axhline(0, color='C1', linestyle='-') +ax2.axhline(0, color="C1", linestyle="-") -#Set ticks and scales for the y-axis +# Set ticks and scales for the y-axis ax2.set_yticks([-0.1, -0.05, 0.0, 0.05, 0.1]) ax2.set_ylim(-0.1, 0.1) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") ax2.set_xlabel(r"Multipole $\ell$") -plt.savefig("./plots/paper_plot_errorbar_validation_BB.png", dpi=300, bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_BB.png", dpi=300, bbox_inches="tight" +) # Save PDF -plt.savefig("./plots/paper_plot_errorbar_validation_BB.pdf", bbox_inches='tight') +plt.savefig("./plots/paper_plot_errorbar_validation_BB.pdf", bbox_inches="tight") plt.show() # %% @@ -519,36 +642,42 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax1.plot(ell, diag_eb, label="Gaussian simulations") ax1.plot(ell, np.sqrt(np.diag(cov_inka_eb)), label="iNKA") -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-11, 1e-10, 1e-9]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 8)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 8)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell^{EB})$") ax1.legend(fontsize=10) -relative_error_sim = diag_eb/np.sqrt(np.diag(cov_inka_eb)) +relative_error_sim = diag_eb / np.sqrt(np.diag(cov_inka_eb)) ax2.plot(ell, relative_error_sim - 1, label="Gaussian simulations") -ax2.axhline(0, color='C1', linestyle='-') -#Set ticks and scales for the y-axis +ax2.axhline(0, color="C1", linestyle="-") +# Set ticks and scales for the y-axis ax2.set_yticks([-0.1, -0.05, 0.0, 0.05, 0.1]) ax2.set_ylim(-0.1, 0.1) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") ax2.set_xlabel(r"Multipole $\ell$") -plt.savefig("./plots/paper_plot_errorbar_validation_EB.png", dpi=300, bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_EB.png", dpi=300, bbox_inches="tight" +) # Save PDF -plt.savefig("./plots/paper_plot_errorbar_validation_EB.pdf", bbox_inches='tight') +plt.savefig("./plots/paper_plot_errorbar_validation_EB.pdf", bbox_inches="tight") plt.show() # %% @@ -569,36 +698,40 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax3.plot(ell, np.sqrt(np.diag(cov_inka_eb)), label="iNKA") ax3.text(300, 2e-10, "EB power spectrum", fontsize=14) -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) -ax3.set_xscale('squareroot') +ax3.set_xscale("squareroot") ax3.set_xticks(np.array([100, 400, 900, 1600])) ax3.minorticks_on() -ax3.tick_params(axis='x', which='minor', length=2, width=0.8) -ax3.set_xticks(minor_ticks, minor=True) +ax3.tick_params(axis="x", which="minor", length=2, width=0.8) +ax3.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-11, 1e-10, 1e-9]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 8)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 8)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell)$") ax3.legend(fontsize=10) -relative_error_sim_bb = diag_bb/np.sqrt(np.diag(cov_inka_bb)) -relative_error_sim_eb = diag_eb/np.sqrt(np.diag(cov_inka_eb)) +relative_error_sim_bb = diag_bb / np.sqrt(np.diag(cov_inka_bb)) +relative_error_sim_eb = diag_eb / np.sqrt(np.diag(cov_inka_eb)) ax2.plot(ell, relative_error_sim_bb - 1, label="Gaussian simulations") -ax2.axhline(0, color='C1', linestyle='-') +ax2.axhline(0, color="C1", linestyle="-") ax4.plot(ell, relative_error_sim_eb - 1, label="Gaussian simulations") -ax4.axhline(0, color='C1', linestyle='-') -#Set ticks and scales for the y-axis +ax4.axhline(0, color="C1", linestyle="-") +# Set ticks and scales for the y-axis ax2.set_yticks([-0.1, -0.05, 0.0, 0.05, 0.1]) ax2.set_ylim(-0.1, 0.1) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") @@ -606,12 +739,14 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax4.set_xlabel(r"Multipole $\ell$") # Remove only right-side y ticks -ax3.tick_params(axis='y', which='both', left=False, right=False, labelleft=False) -ax4.tick_params(axis='y', which='both', left=False, right=False, labelleft=False) +ax3.tick_params(axis="y", which="both", left=False, right=False, labelleft=False) +ax4.tick_params(axis="y", which="both", left=False, right=False, labelleft=False) -plt.savefig("./plots/paper_plot_errorbar_validation_BB_EB.png", dpi=300, bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_BB_EB.png", dpi=300, bbox_inches="tight" +) # Save PDF -plt.savefig("./plots/paper_plot_errorbar_validation_BB_EB.pdf", bbox_inches='tight') +plt.savefig("./plots/paper_plot_errorbar_validation_BB_EB.pdf", bbox_inches="tight") plt.show() # %% @@ -624,45 +759,56 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1], sharex=ax1) -for blind in ['A', 'B', 'C']: - diag_bb = covs[blind]['diag_BB'] +for blind in ["A", "B", "C"]: + diag_bb = covs[blind]["diag_BB"] ax1.plot(ell, diag_bb, label=f"Blind {blind}") -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-11, 1e-11, 1e-10, 1e-9]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 8)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 8)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell^{BB})$") ax1.legend(fontsize=10) -baseline = covs['A']['diag_BB'] +baseline = covs["A"]["diag_BB"] -for blind in ['A', 'B', 'C']: - diag_bb = covs[blind]['diag_BB'] +for blind in ["A", "B", "C"]: + diag_bb = covs[blind]["diag_BB"] relative_error = diag_bb / baseline ax2.plot(ell, relative_error - 1, label=f"Blind {blind}") -#Set ticks and scales for the y-axis +# Set ticks and scales for the y-axis ax2.set_yticks([-0.1, -0.05, 0.0, 0.05, 0.1]) ax2.set_ylim(-0.1, 0.1) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") ax2.set_xlabel(r"Multipole $\ell$") -plt.savefig("./plots/paper_plot_errorbar_validation_BB_blind_comparison.png", dpi=300, bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_BB_blind_comparison.png", + dpi=300, + bbox_inches="tight", +) # Save PDF -plt.savefig("./plots/paper_plot_errorbar_validation_BB_blind_comparison.pdf", bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_BB_blind_comparison.pdf", + bbox_inches="tight", +) plt.show() # %% @@ -673,52 +819,56 @@ def get_cov_from_one_cov(cov_one_cov, gaussian=True): ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1], sharex=ax1) -for blind in ['A', 'B', 'C']: - diag_bb = covs[blind]['diag_EE'] +for blind in ["A", "B", "C"]: + diag_bb = covs[blind]["diag_EE"] ax1.plot(ell, diag_bb, label=f"Blind {blind}") -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-11, 1e-11, 1e-10, 1e-9]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 8)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 8)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell^{EE})$") ax1.legend(fontsize=10) -baseline = covs['A']['diag_EE'] +baseline = covs["A"]["diag_EE"] -for blind in ['A', 'B', 'C']: - diag_bb = covs[blind]['diag_EE'] +for blind in ["A", "B", "C"]: + diag_bb = covs[blind]["diag_EE"] relative_error = diag_bb / baseline ax2.plot(ell, relative_error - 1, label=f"Blind {blind}") -#Set ticks and scales for the y-axis +# Set ticks and scales for the y-axis ax2.set_yticks([-0.1, -0.05, 0.0, 0.05, 0.1]) ax2.set_ylim(-0.1, 0.1) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") ax2.set_xlabel(r"Multipole $\ell$") -plt.savefig("./plots/paper_plot_errorbar_validation_EE_blind_comparison.png", dpi=300, bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_EE_blind_comparison.png", + dpi=300, + bbox_inches="tight", +) # Save PDF -plt.savefig("./plots/paper_plot_errorbar_validation_EE_blind_comparison.pdf", bbox_inches='tight') +plt.savefig( + "./plots/paper_plot_errorbar_validation_EE_blind_comparison.pdf", + bbox_inches="tight", +) plt.show() # %% -# Save gaussian simulation covariance for bb -cov_gaussian_bb = cov_sim_gaussian[96:, 96:] -np.save("/home/guerrini/sp_validation/cosmo_val/harmonic_covariance_gaussian_sims/cov_gaussian_sims_BB.npy", cov_gaussian_bb) -cov_gaussian_eb = cov_sim_gaussian[64:96, 64:96] -np.save("/home/guerrini/sp_validation/cosmo_val/harmonic_covariance_gaussian_sims/cov_gaussian_sims_EB.npy", cov_gaussian_eb) - -# %% diff --git a/papers/harmonic/2025_09_17_scalecut.py b/papers/harmonic/2025_09_17_scalecut.py index e9de5da7..2f26ab4b 100644 --- a/papers/harmonic/2025_09_17_scalecut.py +++ b/papers/harmonic/2025_09_17_scalecut.py @@ -5,18 +5,15 @@ os.environ["LD_LIBRARY_PATH"] = "" os.environ["CONDA_PREFIX"] = "/home/guerrini/.conda/envs/sp_validation_3.11" -import numpy as np import camb import matplotlib.pyplot as plt +import numpy as np import seaborn as sns from astropy.cosmology import Planck18 -from astropy.io import fits from scipy.interpolate import interp1d -plt.style.use( - '/home/guerrini/matplotlib_config/paper.mplstyle' -) -plt.rcParams['text.usetex'] = True +plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") +plt.rcParams["text.usetex"] = True sns.set_palette("Dark2") # %% @@ -30,17 +27,17 @@ plt.plot(z, dndz) -plt.xlabel('Redshift z') -plt.ylabel(r'$dN/dz$') +plt.xlabel("Redshift z") +plt.ylabel(r"$dN/dz$") plt.show() # %% -#Define a Planck cosmology object with CAMB +# Define a Planck cosmology object with CAMB planck = Planck18 -h = planck.H0.value/100 +h = planck.H0.value / 100 Om = planck.Om0 Ob = planck.Ob0 Oc = Om - Ob @@ -49,10 +46,30 @@ m_nu = 0.06 w = -1 -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, ns=ns, mnu=m_nu, w=w, As=As, WantTransfer=True, NonLinear=camb.model.NonLinear_both) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + ns=ns, + mnu=m_nu, + w=w, + As=As, + WantTransfer=True, + NonLinear=camb.model.NonLinear_both, +) Onu = pars.omeganu Oc = Om - Ob - Onu -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, ns=ns, mnu=m_nu, w=w, As=As, WantTransfer=True, NonLinear=camb.model.NonLinear_both) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + ns=ns, + mnu=m_nu, + w=w, + As=As, + WantTransfer=True, + NonLinear=camb.model.NonLinear_both, +) # %% @@ -61,12 +78,17 @@ # %% # Non-linear matter power interpolator -PK = camb.get_matter_power_interpolator(pars, nonlinear=True, hubble_units=True, k_hunit=True, kmax=20.0, zmax=5.0) +PK = camb.get_matter_power_interpolator( + pars, nonlinear=True, hubble_units=True, k_hunit=True, kmax=20.0, zmax=5.0 +) # %% # ---- Comoving distance <-> redshift mapping ---- -chi_of_z = np.vectorize(lambda z: planck.comoving_distance(z).value) # [Mpc] -z_of_chi = interp1d(chi_of_z(np.linspace(0,5,2000)), np.linspace(0,5,2000), fill_value="extrapolate") +chi_of_z = np.vectorize(lambda z: planck.comoving_distance(z).value) # [Mpc] +z_of_chi = interp1d( + chi_of_z(np.linspace(0, 5, 2000)), np.linspace(0, 5, 2000), fill_value="extrapolate" +) + # %% # ---- Example lensing efficiency function q^i(chi) ---- @@ -76,7 +98,7 @@ def q_i(chi, n_i): n_i(chi) should be normalized. """ a = 1.0 / (1.0 + z_of_chi(chi)) - prefactor = 1.5 * planck.Om0 * (planck.H0.value/299792.458)**2 * chi / a + prefactor = 1.5 * planck.Om0 * (planck.H0.value / 299792.458) ** 2 * chi / a chi_vals = np.linspace(chi, chi_of_z(5.0), 2000) integrand = n_i(chi_vals) * (chi_vals - chi) / chi_vals return prefactor * np.trapz(integrand, chi_vals) @@ -85,7 +107,7 @@ def q_i(chi, n_i): # ---- Example galaxy redshift distribution n_i(chi) ---- def n_i_example(chi): z = z_of_chi(chi) - return n_z(z) # arbitrary form (Smail-like) + return n_z(z) # arbitrary form (Smail-like) # ---- Integrand function ---- @@ -94,9 +116,10 @@ def Cl_integrand(k, ell, q_i_func, q_j_func): z = float(z_of_chi(chi)) q_i_val = q_i_func(chi, n_i_example) q_j_val = q_j_func(chi, n_i_example) - Pk = PK.P(z, k) # nonlinear P(k,z) + Pk = PK.P(z, k) # nonlinear P(k,z) return (k / (ell + 0.5)) * q_i_val * q_j_val * Pk + # %% # ---- Example usage ---- ell_list = [30, 100, 300, 1000, 3000] @@ -112,17 +135,17 @@ def Cl_integrand(k, ell, q_i_func, q_j_func): plt.figure() for ell, I_vals in zip(ell_list, I_vals_list): - plt.plot(k_vals, I_vals/np.max(I_vals), label=f'$\ell={ell}$') + plt.plot(k_vals, I_vals / np.max(I_vals), label=rf"$\ell={ell}$") print(np.trapz(I_vals, k_vals)) -plt.xscale('log') -plt.xlabel(r'$k$ [h/Mpc]', fontsize=16) -plt.ylabel(r'$\frac{\mathrm{d}C_\ell}{\mathrm{d}\ln k}$ (normalized)', fontsize=16) -plt.yticks([0.]) +plt.xscale("log") +plt.xlabel(r"$k$ [h/Mpc]", fontsize=16) +plt.ylabel(r"$\frac{\mathrm{d}C_\ell}{\mathrm{d}\ln k}$ (normalized)", fontsize=16) +plt.yticks([0.0]) plt.legend(fontsize=12) -plt.savefig('./plots/Cl_integrand_vs_k.png', dpi=300) +plt.savefig("./plots/Cl_integrand_vs_k.png", dpi=300) # Save pdf -plt.savefig('./plots/Cl_integrand_vs_k.pdf') +plt.savefig("./plots/Cl_integrand_vs_k.pdf") plt.show() @@ -134,14 +157,15 @@ def Cl_integrand(k, ell, q_i_func, q_j_func): plt.figure() -plt.plot(z_lensing, q_vals/np.trapz(q_vals[1:], z_lensing[1:]), label=r'$q^i(z)$') -plt.plot(z, dndz/np.trapz(dndz, z), linestyle="dashed", alpha=0.5, color='C0') +plt.plot(z_lensing, q_vals / np.trapz(q_vals[1:], z_lensing[1:]), label=r"$q^i(z)$") +plt.plot(z, dndz / np.trapz(dndz, z), linestyle="dashed", alpha=0.5, color="C0") -plt.xlabel('Redshift z') -plt.ylabel(r'$q^i(z)$') +plt.xlabel("Redshift z") +plt.ylabel(r"$q^i(z)$") plt.show() + # %% def get_kalpha(I_vals, k_vals, alpha): """ @@ -152,19 +176,27 @@ def get_kalpha(I_vals, k_vals, alpha): cumulative_signal = np.cumsum(I_vals) * np.diff(k_vals, prepend=0) k_alpha = k_vals[np.searchsorted(cumulative_signal, target_signal)] return k_alpha + + # %% alpha = 0.95 k_alpha_list = [get_kalpha(I_vals, k_vals, alpha) for I_vals in I_vals_list] # %% print(k_alpha_list) + # %% def get_lmax(k_max, alpha, k_vals, l_low=400, l_high=2048): """ Computes the maximum multipole l_max corresponding to a given k_max and alpha percent of the total Cl signal using dichotomy. """ + def objective(ell): - k_alpha = get_kalpha(np.vectorize(lambda k: Cl_integrand(k, ell, q_i, q_i))(k_vals), k_vals, alpha) + k_alpha = get_kalpha( + np.vectorize(lambda k: Cl_integrand(k, ell, q_i, q_i))(k_vals), + k_vals, + alpha, + ) print(k_alpha) return k_alpha - k_max @@ -179,7 +211,8 @@ def objective(ell): n_iter += 1 return l_low if objective(l_low) <= 0 else l_high - + + # %% get_lmax(3, 0.95, k_vals, l_high=4096) # %% diff --git a/papers/harmonic/2025_09_26_plot_contours.ipynb b/papers/harmonic/2025_09_26_plot_contours.ipynb index 212c21bf..f83065e7 100644 --- a/papers/harmonic/2025_09_26_plot_contours.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours.ipynb @@ -7,26 +7,24 @@ "metadata": {}, "outputs": [], "source": [ - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "\n", "roots = [\n", @@ -48,16 +46,18 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/samples_{}.txt'.format('/'+root ,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(root_dir + \"{}/samples_{}.txt\".format(\"/\" + root, root), \"r\") as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/getdist_{}.paramnames'.format('/'+root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/getdist_{}.paramnames\".format(\"/\" + root, root), \"w\"\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -68,25 +68,26 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root,root))\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -98,8 +99,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -138,18 +161,29 @@ " {\"color\": \"forestgreen\"},\n", "]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", " colors=colours,\n", " line_args=line_args,\n", ")\n", "\n", - "g.export('plots/contours_all_blind.png')\n", + "g.export(\"plots/contours_all_blind.png\")\n", "plt.show()" ] }, @@ -160,19 +194,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", " colors=colours,\n", " line_args=line_args,\n", ")\n", "\n", - "g.export('plots/contours_cosmo_blind.png')\n", + "g.export(\"plots/contours_cosmo_blind.png\")\n", "plt.show()" ] }, @@ -187,15 +221,15 @@ "blind_axes = True\n", "\n", "if blind_axes:\n", - " title_limit=None\n", + " title_limit = None\n", "else:\n", - " title_limit=1\n", - "#Plot S8 Omega_m only\n", + " title_limit = 1\n", + "# Plot S8 Omega_m only\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", " title_limit=title_limit,\n", " colors=colours,\n", @@ -203,31 +237,32 @@ ")\n", "\n", "if preliminary_watermark:\n", - " plt.figtext(0.5, 0.5, 'PRELIMINARY',\n", - " fontsize=150, color='gray',\n", - " ha='center', va='center',\n", - " alpha=0.3, rotation=330)\n", - " \n", + " plt.figtext(\n", + " 0.5,\n", + " 0.5,\n", + " \"PRELIMINARY\",\n", + " fontsize=150,\n", + " color=\"gray\",\n", + " ha=\"center\",\n", + " va=\"center\",\n", + " alpha=0.3,\n", + " rotation=330,\n", + " )\n", + "\n", "if blind_axes:\n", " axes = g.subplots\n", - " for ax in axes[:,0]:\n", + " for ax in axes[:, 0]:\n", " ref_tick = np.mean([chain.getMeans()[4] for chain in chains])\n", - " ax.set_yticks(\n", - " [ref_tick + i*0.1 for i in range(-2, 2)], labels=[]\n", - " )\n", - " for ax in axes[-1,:]:\n", + " ax.set_yticks([ref_tick + i * 0.1 for i in range(-2, 2)], labels=[])\n", + " for ax in axes[-1, :]:\n", " ref_tick = np.mean([chain.getMeans()[-1] for chain in chains])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.1 for i in range(-2, 3)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.1 for i in range(-2, 3)], labels=[])\n", " ax = axes[-1, -1]\n", " ref_tick_s8 = np.mean([chain.getMeans()[4] for chain in chains])\n", - " ax.set_xticks(\n", - " [ref_tick_s8 + i*0.1 for i in range(-2, 2)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick_s8 + i * 0.1 for i in range(-2, 2)], labels=[])\n", " ax.set_xlim(ref_tick_s8 - 0.25, ref_tick_s8 + 0.25)\n", "\n", - "g.export('plots/contours_s8_omegam_blind.png')\n", + "g.export(\"plots/contours_s8_omegam_blind.png\")\n", "plt.show()" ] }, @@ -260,15 +295,26 @@ "source": [ "s8_values = np.array([\"# Expt\", \"Mean\", \"S8_low\", \"S8_high\"])\n", "for i, chain in enumerate(chains):\n", - "\n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", "\n", - " param_stats = margestats.parWithName('S_8')\n", - " \n", - " s8_values = np.vstack((s8_values,[roots[i], param_stats.mean, param_stats.mean-param_stats.limits[0].lower, param_stats.limits[0].upper-param_stats.mean]))\n", + " param_stats = margestats.parWithName(\"S_8\")\n", + "\n", + " s8_values = np.vstack(\n", + " (\n", + " s8_values,\n", + " [\n", + " roots[i],\n", + " param_stats.mean,\n", + " param_stats.mean - param_stats.limits[0].lower,\n", + " param_stats.limits[0].upper - param_stats.mean,\n", + " ],\n", + " )\n", + " )\n", "print(s8_values)\n", - "np.savetxt(f\"{root_dir}/S8_means.txt\", s8_values, fmt=['%s','%s','%s','%s'], delimiter=',')" + "np.savetxt(\n", + " f\"{root_dir}/S8_means.txt\", s8_values, fmt=[\"%s\", \"%s\", \"%s\", \"%s\"], delimiter=\",\"\n", + ")" ] }, { @@ -278,11 +324,19 @@ "metadata": {}, "outputs": [], "source": [ - "s8s = np.loadtxt(f\"{root_dir}/S8_means.txt\",dtype={'names': ('Expt', 's8_mean', 's8_low', 's8_high'), 'formats': ('U40', 'U20', 'U20', 'U20')}, skiprows=1, delimiter=',')\n", - "expt = s8s['Expt']\n", - "s8s_mean = s8s['s8_mean'].astype(np.float64)\n", - "s8s_low = s8s['s8_low'].astype(np.float64)\n", - "s8s_high = s8s['s8_high'].astype(np.float64)" + "s8s = np.loadtxt(\n", + " f\"{root_dir}/S8_means.txt\",\n", + " dtype={\n", + " \"names\": (\"Expt\", \"s8_mean\", \"s8_low\", \"s8_high\"),\n", + " \"formats\": (\"U40\", \"U20\", \"U20\", \"U20\"),\n", + " },\n", + " skiprows=1,\n", + " delimiter=\",\",\n", + ")\n", + "expt = s8s[\"Expt\"]\n", + "s8s_mean = s8s[\"s8_mean\"].astype(np.float64)\n", + "s8s_low = s8s[\"s8_low\"].astype(np.float64)\n", + "s8s_high = s8s[\"s8_high\"].astype(np.float64)" ] }, { @@ -294,29 +348,48 @@ "source": [ "num_test_cases = 1\n", "\n", - "fig, axs = plt.subplots(1, 1, sharey=True, figsize=[7,1.5*len(expt)])\n", + "fig, axs = plt.subplots(1, 1, sharey=True, figsize=[7, 1.5 * len(expt)])\n", "axs.yaxis.set_visible(False)\n", "\n", - "y = np.arange(0,len(expt))\n", + "y = np.arange(0, len(expt))\n", "\n", "for i in y:\n", - " if i > len(expt)-num_test_cases-1:\n", - " axs.errorbar(s8s_mean[i], i+1, xerr=np.vstack((s8s_low[i],s8s_high[i])), fmt='o', c = 'darkblue',lw = 2, capsize=5,capthick=2)\n", + " if i > len(expt) - num_test_cases - 1:\n", + " axs.errorbar(\n", + " s8s_mean[i],\n", + " i + 1,\n", + " xerr=np.vstack((s8s_low[i], s8s_high[i])),\n", + " fmt=\"o\",\n", + " c=\"darkblue\",\n", + " lw=2,\n", + " capsize=5,\n", + " capthick=2,\n", + " )\n", " else:\n", - " axs.errorbar(s8s_mean[i], i+1, xerr=np.vstack((s8s_low[i],s8s_high[i])), fmt='o', c = 'darkgreen',lw = 2, capsize=5,capthick=2)\n", - " \n", + " axs.errorbar(\n", + " s8s_mean[i],\n", + " i + 1,\n", + " xerr=np.vstack((s8s_low[i], s8s_high[i])),\n", + " fmt=\"o\",\n", + " c=\"darkgreen\",\n", + " lw=2,\n", + " capsize=5,\n", + " capthick=2,\n", + " )\n", + "\n", " \"\"\" if i == 0: # Plot the band for \"Planck\"\n", " axs.axvspan(s8s_mean[i]-s8s_low[i], s8s_mean[i]+s8s_high[i], alpha=0.2, color='cyan')\n", " if i == len(expt)-1: # Plot the band for \"this work\"\n", " axs.axvspan(s8s_mean[i]-s8s_low[i], s8s_mean[i]+s8s_high[i], alpha=0.2, color='lightpink')\"\"\"\n", - " if i == len(expt)-num_test_cases-1: # Make a distinction between this work (and all its test cases) with external datasets\n", - " axs.axhline(i+1.5, ls='dashed',c='k') \n", - " \n", - " \n", - " axs.set_xlabel(r'$S_8=\\sigma_8\\sqrt{\\Omega_{\\rm m}/0.3}$') \n", - " axs.text(0.62, i+1, rf\"{expt[i]}\")\n", - "\n", - "plt.ylim([0,i+2])\n", + " if (\n", + " i == len(expt) - num_test_cases - 1\n", + " ): # Make a distinction between this work (and all its test cases) with external datasets\n", + " axs.axhline(i + 1.5, ls=\"dashed\", c=\"k\")\n", + "\n", + " axs.set_xlabel(r\"$S_8=\\sigma_8\\sqrt{\\Omega_{\\rm m}/0.3}$\")\n", + " axs.text(0.62, i + 1, rf\"{expt[i]}\")\n", + "\n", + "plt.ylim([0, i + 2])\n", "plt.savefig(\"./plots/S8_whisker.png\")" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_NL_modelling.ipynb b/papers/harmonic/2025_09_26_plot_contours_NL_modelling.ipynb index 54dd347b..ddc43a6b 100644 --- a/papers/harmonic/2025_09_26_plot_contours_NL_modelling.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_NL_modelling.ipynb @@ -13,26 +13,24 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "blind = \"B\"\n", "\n", @@ -58,16 +56,21 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root, root,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -78,25 +81,28 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -108,8 +114,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -130,33 +158,35 @@ " r\"$C_\\ell$, \\texttt{HMCode2020} no baryons\",\n", "]\n", "\n", - "colours = [\n", - " \"crimson\",\n", - " \"darkorange\",\n", - " \"darkorchid\"\n", - "]\n", + "colours = [\"crimson\", \"darkorange\", \"darkorchid\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'dashed',\n", - " 'dashed'\n", - "]\n", + "linestyle = [\"solid\", \"dashed\", \"dashed\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_NL_modelling/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_NL_modelling/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -167,19 +197,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_NL_modelling/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_NL_modelling/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -190,19 +220,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g = plots.get_subplot_plotter(width_inch=20)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -213,9 +243,9 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_NL_modelling/contours_s8_omegam_cell.png')\n", - "#Save PDF\n", - "g.export('plots/cosmological_constraints_NL_modelling/contours_s8_omegam_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_NL_modelling/contours_s8_omegam_cell.png\")\n", + "# Save PDF\n", + "g.export(\"plots/cosmological_constraints_NL_modelling/contours_s8_omegam_cell.pdf\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_blind.ipynb b/papers/harmonic/2025_09_26_plot_contours_blind.ipynb index c3983afe..de7d660e 100644 --- a/papers/harmonic/2025_09_26_plot_contours_blind.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_blind.ipynb @@ -13,26 +13,24 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "\n", "roots = [\n", @@ -57,16 +55,21 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root ,root, root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root,root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -77,25 +80,28 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -107,8 +113,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -129,33 +157,35 @@ " r\"$C_\\ell$ SP_v1.4.6 Blind C\",\n", "]\n", "\n", - "colours = [\n", - " \"royalblue\",\n", - " \"crimson\",\n", - " \"forestgreen\"\n", - "]\n", + "colours = [\"royalblue\", \"crimson\", \"forestgreen\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"solid\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_blind/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_blind/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -166,19 +196,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_blind/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_blind/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -189,19 +219,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g = plots.get_subplot_plotter(width_inch=20)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -212,9 +242,9 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_blind/contours_s8_omegam_cell.png')\n", - "#Save PDF\n", - "g.export('plots/cosmological_constraints_blind/contours_s8_omegam_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_blind/contours_s8_omegam_cell.png\")\n", + "# Save PDF\n", + "g.export(\"plots/cosmological_constraints_blind/contours_s8_omegam_cell.pdf\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_cl_vs_xi.ipynb b/papers/harmonic/2025_09_26_plot_contours_cl_vs_xi.ipynb index a5500a57..ca2ec8f5 100644 --- a/papers/harmonic/2025_09_26_plot_contours_cl_vs_xi.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_cl_vs_xi.ipynb @@ -16,60 +16,42 @@ "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", "# Append any useful folder in the path\n", - "sys.path.append(\n", - " \"/home/guerrini/sp_validation/cosmo_inference/scripts/\"\n", - ")\n", + "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts/\")\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", + "import chain_postprocessing as cp\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "import chain_postprocessing as cp\n", - "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=20)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 50\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "root_external = \"/n09data/guerrini/output_chains/ext_data/\"\n", "\n", "blind = \"B\"\n", "\n", - "colour_blind = {\n", - " \"A\": \"royalblue\",\n", - " \"B\": \"crimson\",\n", - " \"C\": \"forestgreen\"\n", - "}\n", + "colour_blind = {\"A\": \"royalblue\", \"B\": \"crimson\", \"C\": \"forestgreen\"}\n", "\n", "roots = [\n", " f\"SP_v1.4.6.3_leak_corr_{blind}\",\n", " f\"SP_v1.4.6.3_{blind}_fiducial_config\",\n", - " \"Planck18\"\n", + " \"Planck18\",\n", "]\n", "\n", - "categories = [\n", - " \"harmonic\",\n", - " \"configuration\",\n", - " \"external\"\n", - "]\n", + "categories = [\"harmonic\", \"configuration\", \"external\"]\n", "\n", - "need_compute_getdist = [\n", - " True,\n", - " True,\n", - " False\n", - "]\n", + "need_compute_getdist = [True, True, False]\n", "\n", "print(roots)\n", "\n", @@ -88,48 +70,22 @@ "for i, root in enumerate(roots):\n", " analysis_type_i = categories[i]\n", " if analysis_type_i == \"harmonic\":\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " root,\n", - " root,\n", - " f\"samples_{root}_cell.txt\"\n", - " )\n", - " path_gd = os.path.join(\n", - " root_dir,\n", - " root,\n", - " root,\n", - " f\"getdist_{root}_cell\"\n", - " )\n", + " path_samples = os.path.join(root_dir, root, root, f\"samples_{root}_cell.txt\")\n", + " path_gd = os.path.join(root_dir, root, root, f\"getdist_{root}_cell\")\n", " elif analysis_type_i == \"configuration\":\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " root,\n", - " f\"samples_{root}.txt\"\n", - " )\n", - " path_gd = os.path.join(\n", - " root_dir,\n", - " root,\n", - " f\"getdist_{root}\"\n", - " )\n", + " path_samples = os.path.join(root_dir, root, f\"samples_{root}.txt\")\n", + " path_gd = os.path.join(root_dir, root, f\"getdist_{root}\")\n", " elif analysis_type_i == \"external\":\n", - " path_samples = os.path.join(\n", - " root_external,\n", - " root,\n", - " f\"samples_{root}.txt\"\n", - " )\n", - " path_gd = os.path.join(\n", - " root_external,\n", - " root,\n", - " f\"getdist_{root}\"\n", - " )\n", + " path_samples = os.path.join(root_external, root, f\"samples_{root}.txt\")\n", + " path_gd = os.path.join(root_external, root, f\"getdist_{root}\")\n", " else:\n", " raise ValueError(f\"Invalid analysis type: {analysis_type_i}\")\n", - " \n", + "\n", " need_getdist = need_compute_getdist[i]\n", " if need_getdist:\n", " # Read files, format and load chains\n", - " cp.load_samples_and_write_paramnames(path_samples, path_gd+\".paramnames\")\n", - " cp.write_samples_getdist_format(path_samples, path_gd+\".txt\")\n", + " cp.load_samples_and_write_paramnames(path_samples, path_gd + \".paramnames\")\n", + " cp.write_samples_getdist_format(path_samples, path_gd + \".txt\")\n", " chain = cp.load_chain(path_gd, smoothing_scale=0.5)\n", " chains.append(chain)" ] @@ -141,15 +97,39 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8', 'S_8', 's_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " for name, label in zip(name_list, label_list):\n", " try:\n", " param_names.parWithName(name).label = label\n", - " except:\n", + " except Exception:\n", " warnings.warn(f\"Parameter {name} not found in chain {roots[i]}.\")" ] }, @@ -163,44 +143,46 @@ "legend_labels = [\n", " r\"UNIONS $C_\\ell$ (This work)\",\n", " r\"UNIONS $\\xi_\\pm$ (Goh et al., 2026)\",\n", - " r\"\\textit{Planck} 2018\"\n", + " r\"\\textit{Planck} 2018\",\n", "]\n", "\n", - "colours = [\n", - " colour_blind[\"B\"],\n", - " \"orange\",\n", - " \"violet\"\n", - "]\n", + "colours = [colour_blind[\"B\"], \"orange\", \"violet\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"solid\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=20\n", - "g.settings.axes_labelsize=30\n", + "g.settings.axes_fontsize = 20\n", + "g.settings.axes_labelsize = 30\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 50\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", - " chains[:-1], # Plot everything but Planck\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " chains[:-1], # Plot everything but Planck\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_cl_vs_xi/contours_all_cell.png')\n", - "#Save pdf\n", - "g.export('plots/cosmological_constraints_cl_vs_xi/contours_all_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_cl_vs_xi/contours_all_cell.png\")\n", + "# Save pdf\n", + "g.export(\"plots/cosmological_constraints_cl_vs_xi/contours_all_cell.pdf\")\n", "plt.show()" ] }, @@ -211,24 +193,24 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "g = plots.get_subplot_plotter(width_inch=20)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','S_8', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"S_8\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_cl_vs_xi/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_cl_vs_xi/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -239,17 +221,17 @@ "metadata": {}, "outputs": [], "source": [ - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','S_8'],\n", + " [\"OMEGA_M\", \"S_8\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -260,9 +242,9 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_cl_vs_xi/contours_s8_omegam_cell.png')\n", - "#Save pdf\n", - "g.export('plots/cosmological_constraints_cl_vs_xi/contours_s8_omegam_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_cl_vs_xi/contours_s8_omegam_cell.png\")\n", + "# Save pdf\n", + "g.export(\"plots/cosmological_constraints_cl_vs_xi/contours_s8_omegam_cell.pdf\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_covariance.ipynb b/papers/harmonic/2025_09_26_plot_contours_covariance.ipynb index 67424414..8191d181 100644 --- a/papers/harmonic/2025_09_26_plot_contours_covariance.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_covariance.ipynb @@ -9,26 +9,24 @@ "source": [ "import os\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "\n", "roots = [\n", @@ -52,16 +50,18 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/samples_{}.txt'.format('/'+root ,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(root_dir + \"{}/samples_{}.txt\".format(\"/\" + root, root), \"r\") as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/getdist_{}.paramnames'.format('/'+root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/getdist_{}.paramnames\".format(\"/\" + root, root), \"w\"\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -72,25 +72,26 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/samples_{}.txt'.format(root,root))\n", + " samples = np.loadtxt(root_dir + \"{}/samples_{}.txt\".format(root, root))\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/getdist_{}.txt'.format(root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/getdist_{}'.format(root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/getdist_{}.txt\".format(root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/getdist_{}\".format(root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -102,8 +103,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -123,33 +146,35 @@ " r\"\\texttt{OneCovariance}\",\n", "]\n", "\n", - "colours = [\n", - " \"royalblue\",\n", - " \"crimson\",\n", - " \"forestgreen\"\n", - "]\n", + "colours = [\"royalblue\", \"crimson\", \"forestgreen\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"solid\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_covariance/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -160,19 +185,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_covariance/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -183,19 +208,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g = plots.get_subplot_plotter(width_inch=20)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -206,9 +231,9 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_covariance/contours_s8_omegam_cell.png')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_s8_omegam_cell.png\")\n", "# Save PDF\n", - "g.export('plots/cosmological_constraints_covariance/contours_s8_omegam_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_s8_omegam_cell.pdf\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_glass_mock.ipynb b/papers/harmonic/2025_09_26_plot_contours_glass_mock.ipynb index d4eb796f..a815ccff 100644 --- a/papers/harmonic/2025_09_26_plot_contours_glass_mock.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_glass_mock.ipynb @@ -7,31 +7,27 @@ "metadata": {}, "outputs": [], "source": [ - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/glass_mock_chains/\"\n", "\n", - "roots = [\n", - " f\"glass_mock_v0_{i:05d}\" for i in range(1, 3)\n", - "] + [\n", + "roots = [f\"glass_mock_v0_{i:05d}\" for i in range(1, 3)] + [\n", " f\"glass_mock_v0_{i:05d}_cell\" for i in range(1, 3)\n", "]\n", "\n", @@ -48,20 +44,26 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " if '_cell' in root:\n", - " path_root = root.replace('_cell','')\n", + " if \"_cell\" in root:\n", + " path_root = root.replace(\"_cell\", \"\")\n", " else:\n", " path_root = root\n", - " with open(root_dir + '{}/{}/samples_{}.txt'.format('/'+path_root , path_root, root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}.txt\".format(\"/\" + path_root, path_root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}.paramnames'.format('/'+path_root, path_root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir\n", + " + \"{}/{}/getdist_{}.paramnames\".format(\"/\" + path_root, path_root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -72,28 +74,34 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - " if '_cell' in root:\n", - " path_root = root.replace('_cell','')\n", + " if \"_cell\" in root:\n", + " path_root = root.replace(\"_cell\", \"\")\n", " else:\n", " path_root = root\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}.txt'.format(path_root,path_root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}.txt\".format(path_root, path_root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}.txt'.format(path_root,path_root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}'.format(path_root,path_root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(\n", + " root_dir + \"{}/{}/getdist_{}.txt\".format(path_root, path_root, root), samples\n", + " )\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}\".format(path_root, path_root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -105,8 +113,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = ['\\Omega_m', '\\omega_b h^2', 'h_0', 'n_s', '\\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_m\",\n", + " r\"\\omega_b h^2\",\n", + " \"h_0\",\n", + " \"n_s\",\n", + " r\"\\sigma_8\",\n", + " \"S_8\",\n", + " \"log T_{AGN}\",\n", + " \"A_{IA}\",\n", + " \"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -125,11 +155,13 @@ "\n", "Omega_m_fid = planck.Om0\n", "sigma_8_fid = 0.8054\n", - "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3)**0.5\n", + "s8_fid = sigma_8_fid * (Omega_m_fid / 0.3) ** 0.5\n", "h = planck.h\n", "Omega_b_fig = planck.Ob0\n", "n_s_fid = 0.965\n", - "print(f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\")\n", + "print(\n", + " f\"Fiducial values: Omega_m = {Omega_m_fid}, sigma_8 = {sigma_8_fid}, S_8 = {s8_fid}\"\n", + ")\n", "\n", "markers = {\n", " \"OMEGA_M\": Omega_m_fid,\n", @@ -137,7 +169,7 @@ " \"s_8_input\": s8_fid,\n", " \"h0\": h,\n", " \"ombh2\": Omega_b_fig * h**2,\n", - " \"n_s\": n_s_fid\n", + " \"n_s\": n_s_fid,\n", "}" ] }, @@ -148,23 +180,32 @@ "metadata": {}, "outputs": [], "source": [ - "legend_labels = [\n", - " rf\"$\\xi_\\pm(\\vartheta)$, Mock {i}\" for i in range(1, 3)\n", - " ] + [\n", + "legend_labels = [rf\"$\\xi_\\pm(\\vartheta)$, Mock {i}\" for i in range(1, 3)] + [\n", " rf\"$C_\\ell$, Mock {i}\" for i in range(1, 3)\n", - " ]\n", + "]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", - " markers=markers\n", + " markers=markers,\n", ")\n", "\n", - "g.export('plots/contours_all_cell_glass_mock.png')\n", + "g.export(\"plots/contours_all_cell_glass_mock.png\")\n", "plt.show()" ] }, @@ -175,18 +216,18 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", - " markers=markers\n", + " markers=markers,\n", ")\n", "\n", - "g.export('plots/contours_cosmo_cell_glass_mock.png')\n", + "g.export(\"plots/contours_cosmo_cell_glass_mock.png\")\n", "plt.show()" ] }, @@ -197,15 +238,15 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " filled=True,\n", " title_limit=1,\n", - " markers=markers\n", + " markers=markers,\n", ")\n", "\n", "\"\"\" plt.figtext(0.5, 0.5, 'PRELIMINARY',\n", @@ -213,7 +254,7 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/contours_s8_omegam_cell_glass_mock.png')\n", + "g.export(\"plots/contours_s8_omegam_cell_glass_mock.png\")\n", "plt.show()" ] }, @@ -226,15 +267,26 @@ "source": [ "s8_values = np.array([\"# Expt\", \"Mean\", \"S8_low\", \"S8_high\"])\n", "for i, chain in enumerate(chains):\n", - "\n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", "\n", - " param_stats = margestats.parWithName('S_8')\n", - " \n", - " s8_values = np.vstack((s8_values,[roots[i], param_stats.mean, param_stats.mean-param_stats.limits[0].lower, param_stats.limits[0].upper-param_stats.mean]))\n", + " param_stats = margestats.parWithName(\"S_8\")\n", + "\n", + " s8_values = np.vstack(\n", + " (\n", + " s8_values,\n", + " [\n", + " roots[i],\n", + " param_stats.mean,\n", + " param_stats.mean - param_stats.limits[0].lower,\n", + " param_stats.limits[0].upper - param_stats.mean,\n", + " ],\n", + " )\n", + " )\n", "print(s8_values)\n", - "np.savetxt(f\"{root_dir}/S8_means.txt\", s8_values, fmt=['%s','%s','%s','%s'], delimiter=',')" + "np.savetxt(\n", + " f\"{root_dir}/S8_means.txt\", s8_values, fmt=[\"%s\", \"%s\", \"%s\", \"%s\"], delimiter=\",\"\n", + ")" ] }, { @@ -244,11 +296,19 @@ "metadata": {}, "outputs": [], "source": [ - "s8s = np.loadtxt(f\"{root_dir}/S8_means.txt\",dtype={'names': ('Expt', 's8_mean', 's8_low', 's8_high'), 'formats': ('U40', 'U20', 'U20', 'U20')}, skiprows=1, delimiter=',')\n", - "expt = s8s['Expt']\n", - "s8s_mean = s8s['s8_mean'].astype(np.float64)\n", - "s8s_low = s8s['s8_low'].astype(np.float64)\n", - "s8s_high = s8s['s8_high'].astype(np.float64)" + "s8s = np.loadtxt(\n", + " f\"{root_dir}/S8_means.txt\",\n", + " dtype={\n", + " \"names\": (\"Expt\", \"s8_mean\", \"s8_low\", \"s8_high\"),\n", + " \"formats\": (\"U40\", \"U20\", \"U20\", \"U20\"),\n", + " },\n", + " skiprows=1,\n", + " delimiter=\",\",\n", + ")\n", + "expt = s8s[\"Expt\"]\n", + "s8s_mean = s8s[\"s8_mean\"].astype(np.float64)\n", + "s8s_low = s8s[\"s8_low\"].astype(np.float64)\n", + "s8s_high = s8s[\"s8_high\"].astype(np.float64)" ] }, { @@ -260,29 +320,48 @@ "source": [ "num_test_cases = 1\n", "\n", - "fig, axs = plt.subplots(1, 1, sharey=True, figsize=[7,1.5*len(expt)])\n", + "fig, axs = plt.subplots(1, 1, sharey=True, figsize=[7, 1.5 * len(expt)])\n", "axs.yaxis.set_visible(False)\n", "\n", - "y = np.arange(0,len(expt))\n", + "y = np.arange(0, len(expt))\n", "\n", "for i in y:\n", - " if i > len(expt)-num_test_cases-1:\n", - " axs.errorbar(s8s_mean[i], i+1, xerr=np.vstack((s8s_low[i],s8s_high[i])), fmt='o', c = 'darkblue',lw = 2, capsize=5,capthick=2)\n", + " if i > len(expt) - num_test_cases - 1:\n", + " axs.errorbar(\n", + " s8s_mean[i],\n", + " i + 1,\n", + " xerr=np.vstack((s8s_low[i], s8s_high[i])),\n", + " fmt=\"o\",\n", + " c=\"darkblue\",\n", + " lw=2,\n", + " capsize=5,\n", + " capthick=2,\n", + " )\n", " else:\n", - " axs.errorbar(s8s_mean[i], i+1, xerr=np.vstack((s8s_low[i],s8s_high[i])), fmt='o', c = 'darkgreen',lw = 2, capsize=5,capthick=2)\n", - " \n", + " axs.errorbar(\n", + " s8s_mean[i],\n", + " i + 1,\n", + " xerr=np.vstack((s8s_low[i], s8s_high[i])),\n", + " fmt=\"o\",\n", + " c=\"darkgreen\",\n", + " lw=2,\n", + " capsize=5,\n", + " capthick=2,\n", + " )\n", + "\n", " \"\"\" if i == 0: # Plot the band for \"Planck\"\n", " axs.axvspan(s8s_mean[i]-s8s_low[i], s8s_mean[i]+s8s_high[i], alpha=0.2, color='cyan')\n", " if i == len(expt)-1: # Plot the band for \"this work\"\n", " axs.axvspan(s8s_mean[i]-s8s_low[i], s8s_mean[i]+s8s_high[i], alpha=0.2, color='lightpink')\"\"\"\n", - " if i == len(expt)-num_test_cases-1: # Make a distinction between this work (and all its test cases) with external datasets\n", - " axs.axhline(i+1.5, ls='dashed',c='k') \n", - " \n", - " \n", - " axs.set_xlabel(r'$S_8=\\sigma_8\\sqrt{\\Omega_{\\rm m}/0.3}$') \n", - " axs.text(0.62, i+1, rf\"{expt[i]}\")\n", + " if (\n", + " i == len(expt) - num_test_cases - 1\n", + " ): # Make a distinction between this work (and all its test cases) with external datasets\n", + " axs.axhline(i + 1.5, ls=\"dashed\", c=\"k\")\n", + "\n", + " axs.set_xlabel(r\"$S_8=\\sigma_8\\sqrt{\\Omega_{\\rm m}/0.3}$\")\n", + " axs.text(0.62, i + 1, rf\"{expt[i]}\")\n", "\n", - "plt.ylim([0,i+2])\n", + "plt.ylim([0, i + 2])\n", "plt.savefig(\"./plots/S8_whisker.png\")" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_iNKA_vs_OneCov.ipynb b/papers/harmonic/2025_09_26_plot_contours_iNKA_vs_OneCov.ipynb index 6412eaf8..f791c37f 100644 --- a/papers/harmonic/2025_09_26_plot_contours_iNKA_vs_OneCov.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_iNKA_vs_OneCov.ipynb @@ -13,26 +13,24 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "blind = \"B\"\n", "\n", @@ -57,16 +55,21 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root ,root,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -77,25 +80,28 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -107,8 +113,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -123,36 +151,37 @@ "metadata": {}, "outputs": [], "source": [ - "legend_labels = [\n", - " r\"iNKA Covariance (fiducial)\",\n", - " r\"\\texttt{OneCovariance}\"\n", - "]\n", + "legend_labels = [r\"iNKA Covariance (fiducial)\", r\"\\texttt{OneCovariance}\"]\n", "\n", - "colours = [\n", - " 'crimson',\n", - " \"royalblue\"\n", - "]\n", + "colours = [\"crimson\", \"royalblue\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_covariance/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -163,19 +192,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_covariance/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -186,12 +215,12 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -202,7 +231,7 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_covariance/contours_s8_omegam_cell.png')\n", + "g.export(\"plots/cosmological_constraints_covariance/contours_s8_omegam_cell.png\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_leakage.ipynb b/papers/harmonic/2025_09_26_plot_contours_leakage.ipynb index 13141686..05fce137 100644 --- a/papers/harmonic/2025_09_26_plot_contours_leakage.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_leakage.ipynb @@ -13,26 +13,24 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 55\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "blind = \"B\"\n", "\n", @@ -57,16 +55,21 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root , root, root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -77,25 +80,28 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -107,8 +113,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -128,33 +156,35 @@ " r\"Without leakage correction\",\n", "]\n", "\n", - "colours = [\n", - " \"crimson\",\n", - " \"royalblue\",\n", - " \"forestgreen\"\n", - "]\n", + "colours = [\"crimson\", \"royalblue\", \"forestgreen\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"solid\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_leakage/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_leakage/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -165,19 +195,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_leakage/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_leakage/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -188,19 +218,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g = plots.get_subplot_plotter(width_inch=25)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -211,9 +241,9 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_leakage/contours_s8_omegam_cell.png')\n", + "g.export(\"plots/cosmological_constraints_leakage/contours_s8_omegam_cell.png\")\n", "# Save pdf\n", - "g.export('plots/cosmological_constraints_leakage/contours_s8_omegam_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_leakage/contours_s8_omegam_cell.pdf\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_scale_cut.ipynb b/papers/harmonic/2025_09_26_plot_contours_scale_cut.ipynb index 1b6f2950..e4233f0e 100644 --- a/papers/harmonic/2025_09_26_plot_contours_scale_cut.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_scale_cut.ipynb @@ -13,34 +13,28 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrin/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "blind = \"B\"\n", "\n", - "colour_blind = {\n", - " \"A\": \"royalblue\",\n", - " \"B\": \"crimson\",\n", - " \"C\": \"forestgreen\"\n", - "}\n", + "colour_blind = {\"A\": \"royalblue\", \"B\": \"crimson\", \"C\": \"forestgreen\"}\n", "\n", "\n", "roots = [\n", @@ -48,7 +42,7 @@ " f\"SP_v1.4.6.3_leak_corr_kmax=5Mpc_{blind}\",\n", " f\"SP_v1.4.6.3_leak_corr_kmax=3Mpc_{blind}\",\n", " f\"SP_v1.4.6.3_leak_corr_kmax=1Mpc_{blind}\",\n", - " f\"SP_v1.4.6.3_leak_corr_include_large_scales_{blind}\"\n", + " f\"SP_v1.4.6.3_leak_corr_include_large_scales_{blind}\",\n", "]\n", "\n", "print(roots)\n", @@ -67,16 +61,21 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root ,root, root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root, root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -87,25 +86,28 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -117,8 +119,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -138,40 +162,38 @@ " r\"$C_\\ell$, $k_\\mathrm{max}=5 \\,h \\, \\textrm{Mpc}^{-1}$\",\n", " r\"$C_\\ell$, $k_\\mathrm{max}=3 \\,h \\, \\textrm{Mpc}^{-1}$\",\n", " r\"$C_\\ell$, $k_\\mathrm{max}=1 \\,h \\, \\textrm{Mpc}^{-1}$\",\n", - " r\"$C_\\ell$, $\\ell_\\mathrm{min}=0$, $\\ell_\\mathrm{max}=1600$\"\n", + " r\"$C_\\ell$, $\\ell_\\mathrm{min}=0$, $\\ell_\\mathrm{max}=1600$\",\n", "]\n", "\n", - "colours = [\n", - " \"crimson\",\n", - " \"darkorange\",\n", - " \"darkorchid\",\n", - " \"royalblue\",\n", - " \"forestgreen\"\n", - "]\n", + "colours = [\"crimson\", \"darkorange\", \"darkorchid\", \"royalblue\", \"forestgreen\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'dashed',\n", - " 'dashed',\n", - " 'dashed',\n", - " 'dashed'\n", - "]\n", + "linestyle = [\"solid\", \"dashed\", \"dashed\", \"dashed\", \"dashed\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_scale_cuts/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_scale_cuts/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -182,19 +204,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_scale_cuts/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_scale_cuts/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -205,18 +227,18 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g = plots.get_subplot_plotter(width_inch=25)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -227,9 +249,9 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_scale_cuts/contours_s8_omegam_cell.png')\n", - "#Save PDF\n", - "g.export('plots/cosmological_constraints_scale_cuts/contours_s8_omegam_cell.pdf')\n", + "g.export(\"plots/cosmological_constraints_scale_cuts/contours_s8_omegam_cell.png\")\n", + "# Save PDF\n", + "g.export(\"plots/cosmological_constraints_scale_cuts/contours_s8_omegam_cell.pdf\")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_small_vs_large_scales.ipynb b/papers/harmonic/2025_09_26_plot_contours_small_vs_large_scales.ipynb index 579313de..5a3fe9d5 100644 --- a/papers/harmonic/2025_09_26_plot_contours_small_vs_large_scales.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_small_vs_large_scales.ipynb @@ -13,26 +13,24 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import numpy as np\n", "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "blind = \"B\"\n", "\n", @@ -58,16 +56,21 @@ "# MAKE PARAMNAMES FILE\n", "\n", "for root in roots:\n", - " with open(root_dir + '{}/{}/samples_{}_cell.txt'.format('/'+root ,root,root), \"r\") as file:\n", - " params = file.readline()[1:].split('\\t')[:-4]\n", + " with open(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(\"/\" + root, root, root), \"r\"\n", + " ) as file:\n", + " params = file.readline()[1:].split(\"\\t\")[:-4]\n", " file.close()\n", - " \n", - " with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format('/'+root, root,root), \"w\") as file:\n", + "\n", + " with open(\n", + " root_dir + \"{}/{}/getdist_{}_cell.paramnames\".format(\"/\" + root, root, root),\n", + " \"w\",\n", + " ) as file:\n", " for i in range(len(params)):\n", - " if len(params[i].split('--')) > 1:\n", - " file.write(params[i].split('--')[1] + '\\n')\n", + " if len(params[i].split(\"--\")) > 1:\n", + " file.write(params[i].split(\"--\")[1] + \"\\n\")\n", " else:\n", - " file.write(params[i].split('--')[0] + '\\n')\n", + " file.write(params[i].split(\"--\")[0] + \"\\n\")\n", " file.close()" ] }, @@ -78,25 +81,28 @@ "metadata": {}, "outputs": [], "source": [ - "#READ CHAIN\n", + "# READ CHAIN\n", "\n", - "chains=[]\n", + "chains = []\n", "\n", "for root in roots:\n", - "\n", - " samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root))\n", + " samples = np.loadtxt(\n", + " root_dir + \"{}/{}/samples_{}_cell.txt\".format(root, root, root)\n", + " )\n", " print(len(samples))\n", - " if 'nautilus' in root:\n", - " samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3]))\n", + " if \"nautilus\" in root:\n", + " samples = np.column_stack(\n", + " (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3])\n", + " )\n", " else:\n", - " samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4]))\n", - " np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples)\n", - " \n", - " chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root),\n", - " cache=False,\n", - " settings={'ignore_rows':0,\n", - " 'smooth_scale_2D':0.5,\n", - " 'smooth_scale_1D':0.5})\n", + " samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4]))\n", + " np.savetxt(root_dir + \"{}/{}/getdist_{}_cell.txt\".format(root, root, root), samples)\n", + "\n", + " chain = g.samples_for_root(\n", + " root_dir + \"{}/{}/getdist_{}_cell\".format(root, root, root),\n", + " cache=False,\n", + " settings={\"ignore_rows\": 0, \"smooth_scale_2D\": 0.5, \"smooth_scale_1D\": 0.5},\n", + " )\n", "\n", " chains.append(chain)" ] @@ -108,8 +114,30 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", @@ -130,33 +158,35 @@ " r\"$C_\\ell$, large scales only\",\n", "]\n", "\n", - "colours = [\n", - " \"royalblue\",\n", - " \"darkorange\",\n", - " \"darkorchid\"\n", - "]\n", + "colours = [\"royalblue\", \"darkorange\", \"darkorchid\"]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'dashed',\n", - " 'dashed'\n", - "]\n", + "linestyle = [\"solid\", \"dashed\", \"dashed\"]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", - "#Plot all parameters\n", + "# Plot all parameters\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','ombh2', 'h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'],\n", + " [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + " ],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - "\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_small_vs_large_scales/contours_all_cell.png')\n", + "g.export(\"plots/cosmological_constraints_small_vs_large_scales/contours_all_cell.png\")\n", "plt.show()" ] }, @@ -167,19 +197,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot only cosmological parameters\n", + "# Plot only cosmological parameters\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input', 'SIGMA_8', 'a'],\n", + " [\"OMEGA_M\", \"s_8_input\", \"SIGMA_8\", \"a\"],\n", " legend_labels=legend_labels,\n", " line_args=line_args,\n", " contour_colors=colours,\n", - " legend_loc='upper right',\n", - " filled=True\n", + " legend_loc=\"upper right\",\n", + " filled=True,\n", ")\n", "\n", - "g.export('plots/cosmological_constraints_small_vs_large_scales/contours_cosmo_cell.png')\n", + "g.export(\"plots/cosmological_constraints_small_vs_large_scales/contours_cosmo_cell.png\")\n", "plt.show()" ] }, @@ -190,19 +220,19 @@ "metadata": {}, "outputs": [], "source": [ - "#Plot S8 Omega_m only\n", - "#Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", + "# Plot S8 Omega_m only\n", "g = plots.get_subplot_plotter(width_inch=20)\n", - "g.settings.axes_fontsize=35\n", - "g.settings.axes_labelsize=50\n", + "g.settings.axes_fontsize = 35\n", + "g.settings.axes_labelsize = 50\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 40\n", "\n", "g.triangle_plot(\n", " chains,\n", - " ['OMEGA_M','s_8_input'],\n", + " [\"OMEGA_M\", \"s_8_input\"],\n", " legend_labels=legend_labels,\n", - " legend_loc='upper right',\n", + " legend_loc=\"upper right\",\n", " line_args=line_args,\n", " contour_colors=colours,\n", " filled=True,\n", @@ -213,9 +243,13 @@ " ha='center', va='center',\n", " alpha=0.3, rotation=330) \"\"\"\n", "\n", - "g.export('plots/cosmological_constraints_small_vs_large_scales/contours_s8_omegam_cell.png')\n", + "g.export(\n", + " \"plots/cosmological_constraints_small_vs_large_scales/contours_s8_omegam_cell.png\"\n", + ")\n", "# Save PDF\n", - "g.export('plots/cosmological_constraints_small_vs_large_scales/contours_s8_omegam_cell.pdf')\n", + "g.export(\n", + " \"plots/cosmological_constraints_small_vs_large_scales/contours_s8_omegam_cell.pdf\"\n", + ")\n", "plt.show()" ] }, diff --git a/papers/harmonic/2025_09_26_plot_contours_weak_lensing.ipynb b/papers/harmonic/2025_09_26_plot_contours_weak_lensing.ipynb index dc812f54..20e410fb 100644 --- a/papers/harmonic/2025_09_26_plot_contours_weak_lensing.ipynb +++ b/papers/harmonic/2025_09_26_plot_contours_weak_lensing.ipynb @@ -14,19 +14,14 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "sys.path.append(\n", - " \"/home/guerrini/sp_validation/cosmo_inference/scripts/\"\n", - ")\n", + "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts/\")\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", + "import chain_postprocessing as cp\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", - "import chain_postprocessing as cp\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", "sns.set_palette(\"husl\")\n", "\n", @@ -34,7 +29,7 @@ "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "root_external = \"/n09data/guerrini/output_chains/ext_data/\"\n", "blind = \"B\"\n", @@ -54,7 +49,7 @@ " \"external\",\n", " \"external\",\n", " \"external\",\n", - " \"external\"\n", + " \"external\",\n", "]\n", "\n", "print(roots)\n", @@ -74,53 +69,34 @@ "for i, root in enumerate(roots):\n", " category = categories[i]\n", " if category != \"external\":\n", - " if category == 'configuration':\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/getdist_{root}\"\n", - " )\n", - " elif category == 'harmonic':\n", + " if category == \"configuration\":\n", + " path_samples = os.path.join(root_dir, f\"{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"{root}/getdist_{root}\")\n", + " elif category == \"harmonic\":\n", " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/samples_{root}_cell.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/getdist_{root}\"\n", + " root_dir, f\"{root}/{root}/samples_{root}_cell.txt\"\n", " )\n", + " path_getdist = os.path.join(root_dir, f\"{root}/{root}/getdist_{root}\")\n", " elif category == \"external_compute_sample\":\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", - " )\n", + " path_samples = os.path.join(root_dir, f\"ext_data/{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", " else:\n", " raise ValueError(f\"The category, {category}, of {root} is not correct\")\n", - " \n", + "\n", " if category == \"external_compute_sample\" and \"Legacy\" in root:\n", - " chain_type=\"nautilus\"\n", + " chain_type = \"nautilus\"\n", " else:\n", - " chain_type=\"polychord\"\n", - " cp.load_samples_and_write_paramnames(path_samples, path_getdist+\".paramnames\", chain_type=chain_type)\n", - " cp.write_samples_getdist_format(path_samples, path_getdist+\".txt\", chain_type=chain_type)\n", - " chains.append(\n", - " cp.load_chain(path_getdist, smoothing_scale=0.5)\n", + " chain_type = \"polychord\"\n", + " cp.load_samples_and_write_paramnames(\n", + " path_samples, path_getdist + \".paramnames\", chain_type=chain_type\n", " )\n", - " else:\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", + " cp.write_samples_getdist_format(\n", + " path_samples, path_getdist + \".txt\", chain_type=chain_type\n", " )\n", - " chains.append(\n", - " cp.load_chain(path_getdist)\n", - " )" + " chains.append(cp.load_chain(path_getdist, smoothing_scale=0.5))\n", + " else:\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", + " chains.append(cp.load_chain(path_getdist))" ] }, { @@ -130,15 +106,38 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8', 'S_8', 's_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = label_list = [r'\\Omega_{\\rm m}', r'\\omega_{\\rm b} h^2', r'h_0', r'n_{\\rm s}', r'\\sigma_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_{\\rm b} h^2\",\n", + " r\"h_0\",\n", + " r\"n_{\\rm s}\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for chain in chains:\n", " param_names = chain.getParamNames()\n", " for name, label in zip(name_list, label_list):\n", " try:\n", " param_names.parWithName(name).label = label\n", - " except:\n", + " except Exception:\n", " pass" ] }, @@ -161,6 +160,7 @@ "\n", " chain.setParamNames(param_names)\n", "\n", + "\n", "def derive_parameter_S8(chain):\n", " \"\"\"\n", " Derives the S_8 parameter from Omega_m and Sigma_8 in a GetDist chain.\n", @@ -171,10 +171,9 @@ "\n", " s_8 = sigma_8 * (omega_m / 0.3) ** 0.5\n", "\n", - " chain.addDerived(s_8, name='S_8', label='S_8')\n", + " chain.addDerived(s_8, name=\"S_8\", label=\"S_8\")\n", "\n", - " return chain\n", - " \n" + " return chain" ] }, { @@ -184,39 +183,39 @@ "metadata": {}, "outputs": [], "source": [ - "if 'KiDS-1000' in roots:\n", - " idx = roots.index('KiDS-1000')\n", - " adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', '\\Omega_m')\n", + "if \"KiDS-1000\" in roots:\n", + " idx = roots.index(\"KiDS-1000\")\n", + " adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_m\")\n", "\n", - "if 'HSC_Y3' in roots:\n", - " idx = roots.index('HSC_Y3')\n", - " adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', '\\Omega_m')\n", + "if \"HSC_Y3\" in roots:\n", + " idx = roots.index(\"HSC_Y3\")\n", + " adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_m\")\n", "\n", - "if 'DES+KiDS' in roots:\n", - " idx = roots.index('DES+KiDS')\n", - " adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', '\\Omega_m')\n", + "if \"DES+KiDS\" in roots:\n", + " idx = roots.index(\"DES+KiDS\")\n", + " adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_m\")\n", "\n", - "if 'DES_Y3' in roots:\n", - " idx = roots.index('DES_Y3')\n", - " adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', '\\Omega_m')\n", + "if \"DES_Y3\" in roots:\n", + " idx = roots.index(\"DES_Y3\")\n", + " adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_m\")\n", " derive_parameter_S8(chains[idx])\n", "\n", - "if 'KiDS-Legacy_cosebis' in roots:\n", - " idx = roots.index('KiDS-Legacy_cosebis')\n", + "if \"KiDS-Legacy_cosebis\" in roots:\n", + " idx = roots.index(\"KiDS-Legacy_cosebis\")\n", " derive_parameter_S8(chains[idx])\n", "\n", - "if 'KiDS-Legacy_bandpowers' in roots:\n", - " idx = roots.index('KiDS-Legacy_bandpowers')\n", + "if \"KiDS-Legacy_bandpowers\" in roots:\n", + " idx = roots.index(\"KiDS-Legacy_bandpowers\")\n", " derive_parameter_S8(chains[idx])\n", "\n", - "if 'DES_Y3_cell' in roots:\n", - " idx = roots.index('DES_Y3_cell')\n", - " cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')\n", + "if \"DES_Y3_cell\" in roots:\n", + " idx = roots.index(\"DES_Y3_cell\")\n", + " cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")\n", " cp.derive_parameter_S8(chains[idx])\n", "\n", - "if 'HSC_Y3_cell' in roots:\n", - " idx = roots.index('HSC_Y3_cell')\n", - " cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')" + "if \"HSC_Y3_cell\" in roots:\n", + " idx = roots.index(\"HSC_Y3_cell\")\n", + " cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")" ] }, { @@ -240,52 +239,48 @@ " \"darkorange\",\n", "]\n", "\n", - "linestyle = [\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid',\n", - " 'solid'\n", - "]\n", + "linestyle = [\"solid\", \"solid\", \"solid\", \"solid\", \"solid\", \"solid\"]\n", "\n", - "filled = [\n", - " True,\n", - " False,\n", - " False,\n", - " False,\n", - " False,\n", - " False\n", - "]\n", + "filled = [True, False, False, False, False, False]\n", "\n", "line_args = [dict(color=col, ls=ls) for col, ls in zip(colours, linestyle)]\n", "\n", "g = plots.get_single_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 45\n", "g.settings.figure_legend_ncol = 3\n", "g.settings.legend_frame = False\n", "\n", - "g.plot_2d(chains,\n", - " 'OMEGA_M', 'S_8',\n", - " filled=filled,\n", - " line_args=line_args,\n", - " contour_colors=colours,)\n", + "g.plot_2d(\n", + " chains,\n", + " \"OMEGA_M\",\n", + " \"S_8\",\n", + " filled=filled,\n", + " line_args=line_args,\n", + " contour_colors=colours,\n", + ")\n", "\n", "g.add_legend(\n", " legend_labels,\n", - " legend_loc='upper center',\n", - " bbox_to_anchor=(0.5, 1.12), # moves legend above the axes\n", + " legend_loc=\"upper center\",\n", + " bbox_to_anchor=(0.5, 1.12), # moves legend above the axes\n", ")\n", "\n", "plt.xlim(0.05, 0.65)\n", "\n", - "plt.savefig(\"plots/cosmological_constraints_weak_lensing/contours_OMEGA_M_S8_2D_weak_lensing_all_surveys.png\", dpi=300, bbox_inches='tight')\n", - "#Save PDF\n", - "plt.savefig(\"plots/cosmological_constraints_weak_lensing/contours_OMEGA_M_S8_2D_weak_lensing_all_surveys.pdf\", bbox_inches='tight')\n", - "plt.show()\n" + "plt.savefig(\n", + " \"plots/cosmological_constraints_weak_lensing/contours_OMEGA_M_S8_2D_weak_lensing_all_surveys.png\",\n", + " dpi=300,\n", + " bbox_inches=\"tight\",\n", + ")\n", + "# Save PDF\n", + "plt.savefig(\n", + " \"plots/cosmological_constraints_weak_lensing/contours_OMEGA_M_S8_2D_weak_lensing_all_surveys.pdf\",\n", + " bbox_inches=\"tight\",\n", + ")\n", + "plt.show()" ] }, { diff --git a/papers/harmonic/2025_10_08_plot_data_vectors.py b/papers/harmonic/2025_10_08_plot_data_vectors.py index e6f3d9a8..24edc97f 100644 --- a/papers/harmonic/2025_10_08_plot_data_vectors.py +++ b/papers/harmonic/2025_10_08_plot_data_vectors.py @@ -15,27 +15,24 @@ import os +import matplotlib.pyplot as plt import numpy as np +import seaborn as sns from astropy.io import fits -import matplotlib.pyplot as plt from matplotlib import scale as mscale -import matplotlib.ticker as mticker -import seaborn as sns -from sp_validation.rho_tau import SquareRootScale - from utils import get_chi2_and_pte +from sp_validation.rho_tau import SquareRootScale + mscale.register_scale(SquareRootScale) -plt.style.use( - "./matplotlib_config/paper.mplstyle" -) +plt.style.use("./matplotlib_config/paper.mplstyle") plt.rcParams["text.usetex"] = True sns.set_palette("Dark2") -#Matplotlib inline if in jupyter +# Matplotlib inline if in jupyter if ipython is not None: ipython.run_line_magic("matplotlib", "inline") @@ -44,27 +41,31 @@ base_dir = "/home/guerrini/sp_validation/cosmo_val/output/" versions = ["SP_v1.4.5_leak_corr", "SP_v1.4.6_leak_corr", "SP_v1.4.8_leak_corr"] -labels = ["SP v1.4.5 w/ leakage corr.", "SP v1.4.6 w/ leakage corr.", "SP v1.4.8 w/ leakage corr."] +labels = [ + "SP v1.4.5 w/ leakage corr.", + "SP v1.4.6 w/ leakage corr.", + "SP v1.4.8 w/ leakage corr.", +] colors = ["C0", "C1", "C2"] markers = ["o", "h", "x"] offset = 0.15 # %% -fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, nrows=1, figsize= (20, 12)) +fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, nrows=1, figsize=(20, 12)) for i, ver in enumerate(versions): cell = fits.getdata(f"{base_dir}/pseudo_cl_{ver}.fits") cov_cell = fits.open(f"{base_dir}/pseudo_cl_cov_{ver}.fits") - ell = cell['ell'] - cl_ee = cell['EE'] - cov_cl_ee = cov_cell['COVAR_EE_EE'].data - cl_eb = cell['EB'] - cov_cl_eb = cov_cell['COVAR_EB_EB'].data + ell = cell["ell"] + cl_ee = cell["EE"] + cov_cl_ee = cov_cell["COVAR_EE_EE"].data + cl_eb = cell["EB"] + cov_cl_eb = cov_cell["COVAR_EB_EB"].data chi2_eb, reduced_chi2_eb, pte_eb = get_chi2_and_pte(cl_eb, cov_cl_eb, verbose=False) - cl_bb = cell['BB'] - cov_cl_bb = cov_cell['COVAR_BB_BB'].data + cl_bb = cell["BB"] + cov_cl_bb = cov_cell["COVAR_BB_BB"].data chi2_bb, reduced_chi2_bb, pte_bb = get_chi2_and_pte(cl_bb, cov_cl_bb, verbose=False) ell_widths = np.diff(ell) @@ -81,7 +82,7 @@ label=labels[i], color=colors[i], fmt=markers[i], - capsize=2 + capsize=2, ) ax1.errorbar( jiterred_ell, @@ -90,7 +91,7 @@ label=labels[i], color=colors[i], fmt=markers[i], - capsize=2 + capsize=2, ) ax2.errorbar( jiterred_ell, @@ -99,14 +100,14 @@ label=labels[i], color=colors[i], fmt=markers[i], - capsize=2 + capsize=2, ) ax0.set_xscale("squareroot") ax0.set_xticks(np.array([100, 400, 900, 1600])) ax0.minorticks_on() -ax0.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax0.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax0.set_xticks(minor_ticks, minor=True) ax0.legend() @@ -116,11 +117,11 @@ ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) ax1.legend() -ax1.axhline(0, color='gray', linestyle='--', linewidth=1) +ax1.axhline(0, color="gray", linestyle="--", linewidth=1) ax1.set_xlabel(r"$\ell$") ax1.set_ylabel(r"$\ell \, C_\ell^{EB}$") @@ -128,32 +129,31 @@ ax2.set_xscale("squareroot") ax2.set_xticks(np.array([100, 400, 900, 1600])) ax2.minorticks_on() -ax2.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax2.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax2.set_xticks(minor_ticks, minor=True) ax2.legend() -ax2.axhline(0, color='gray', linestyle='--', linewidth=1) +ax2.axhline(0, color="gray", linestyle="--", linewidth=1) ax2.set_xlabel(r"$\ell$") ax2.set_ylabel(r"$\ell \, C_\ell^{BB}$") plt.tight_layout() -plt.savefig('./plots/data_vectors_cell.png', dpi=200) +plt.savefig("./plots/data_vectors_cell.png", dpi=200) plt.show() # %% -#Same plot with B-modes only -fig, ax = plt.subplots(figsize= (8, 6)) - +# Same plot with B-modes only +fig, ax = plt.subplots(figsize=(8, 6)) for i, ver in enumerate(versions): cell = fits.getdata(f"{base_dir}/pseudo_cl_{ver}.fits") cov_cell = fits.open(f"{base_dir}/pseudo_cl_cov_{ver}.fits") - ell = cell['ell'] - cl_bb = cell['BB'] - cov_cl_bb = cov_cell['COVAR_BB_BB'].data + ell = cell["ell"] + cl_bb = cell["BB"] + cov_cl_bb = cov_cell["COVAR_BB_BB"].data chi2, reduced_chi2, pte = get_chi2_and_pte(cl_bb, cov_cl_bb, verbose=False) # Better jittering: symmetric around original ell values @@ -167,17 +167,22 @@ label=labels[i], color=colors[i], fmt=markers[i], - capsize=2 + capsize=2, + ) + ax.text( + 100, + 1.25e-7 - i * 0.10e-7, + rf"$\chi^2_\nu={reduced_chi2:.3f}$ | PTE={pte:.3f}", + color=colors[i], ) - ax.text(100, 1.25e-7 - i*0.10e-7, rf"$\chi^2_\nu={reduced_chi2:.3f}$ | PTE={pte:.3f}", color=colors[i]) ax.set_xscale("squareroot") ax.set_xticks(np.array([100, 400, 900, 1600])) ax.minorticks_on() -ax.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax.set_xticks(minor_ticks, minor=True) -ax.axhline(0, color='gray', linestyle='--', linewidth=1) +ax.axhline(0, color="gray", linestyle="--", linewidth=1) ax.legend() ax.set_xlabel(r"$\ell$") @@ -185,13 +190,13 @@ plt.tight_layout() -plt.savefig('./plots/data_vectors_cell_bb.png', dpi=200) +plt.savefig("./plots/data_vectors_cell_bb.png", dpi=200) plt.show() # %% -#Same plot with EB and BB modes only of v1.4.6 -fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize= (8, 6), sharex=True) +# Same plot with EB and BB modes only of v1.4.6 +fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(8, 6), sharex=True) version_paper_plot = ["SP_v1.4.6.3_A", "SP_v1.4.6.3_leak_corr_A"] labels = ["W/o leakage correction", "With leakage correction"] @@ -202,11 +207,11 @@ cell = fits.getdata(f"{base_dir}/pseudo_cl_{ver}.fits") cov_cell = fits.open(f"{base_dir}/pseudo_cl_cov_{ver}.fits") - ell = cell['ell'] - cl_eb = cell['EB'] - cov_cl_eb = cov_cell['COVAR_EB_EB'].data - cl_bb = cell['BB'] - cov_cl_bb = cov_cell['COVAR_BB_BB'].data + ell = cell["ell"] + cl_eb = cell["EB"] + cov_cl_eb = cov_cell["COVAR_EB_EB"].data + cl_bb = cell["BB"] + cov_cl_bb = cov_cell["COVAR_BB_BB"].data ell_widths = np.diff(ell) ell_widths = np.append(ell_widths, ell_widths[-1]) @@ -223,9 +228,14 @@ label=labels[i], color=colors[i], fmt=markers[i], - capsize=2 + capsize=2, + ) + ax1.text( + 100, + -0.5e-7 - i * 0.25e-7, + rf"$\chi^2_\nu={reduced_chi2:.3f}$ | PTE={pte:.3f}", + color=colors[i], ) - ax1.text(100, -0.5e-7 - i*0.25e-7, rf"$\chi^2_\nu={reduced_chi2:.3f}$ | PTE={pte:.3f}", color=colors[i]) chi2, reduced_chi2, pte = get_chi2_and_pte(cl_bb, cov_cl_bb, verbose=False) ax2.errorbar( @@ -235,26 +245,31 @@ label=labels[i], color=colors[i], fmt=markers[i], - capsize=2 + capsize=2, + ) + ax2.text( + 100, + 1.25e-7 - i * 0.25e-7, + rf"$\chi^2_\nu={reduced_chi2:.3f}$ | PTE={pte:.3f}", + color=colors[i], ) - ax2.text(100, 1.25e-7 - i*0.25e-7, rf"$\chi^2_\nu={reduced_chi2:.3f}$ | PTE={pte:.3f}", color=colors[i]) ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -ax1.axhline(0, color='gray', linestyle='--', linewidth=1) +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +ax1.axhline(0, color="gray", linestyle="--", linewidth=1) ax2.set_xscale("squareroot") ax2.set_xticks(np.array([100, 400, 900, 1600])) ax2.minorticks_on() -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax2.set_xticks(minor_ticks, minor=True) -ax2.tick_params(axis='x', which='minor', length=2, width=0.8) -ax2.axhline(0, color='gray', linestyle='--', linewidth=1) -ax2.legend(fontsize=14) +ax2.tick_params(axis="x", which="minor", length=2, width=0.8) +ax2.axhline(0, color="gray", linestyle="--", linewidth=1) +ax2.legend(fontsize=14) ax2.set_xlabel(r"Multipole $\ell$", fontsize=16) ax1.set_ylabel(r"$\ell \, C_\ell^{EB} \times 10^{-7}$", fontsize=16) @@ -263,9 +278,9 @@ ax1.yaxis.get_offset_text().set_visible(False) ax2.yaxis.get_offset_text().set_visible(False) -plt.savefig("./plots/paperplot_data_vectors_eb_bb.png", dpi=300, bbox_inches='tight') -#Save pdf -plt.savefig("./plots/paperplot_data_vectors_eb_bb.pdf", bbox_inches='tight') +plt.savefig("./plots/paperplot_data_vectors_eb_bb.png", dpi=300, bbox_inches="tight") +# Save pdf +plt.savefig("./plots/paperplot_data_vectors_eb_bb.pdf", bbox_inches="tight") plt.show() diff --git a/papers/harmonic/2025_10_28_plot_whisker.ipynb b/papers/harmonic/2025_10_28_plot_whisker.ipynb index aa8a7bca..4a6515b6 100644 --- a/papers/harmonic/2025_10_28_plot_whisker.ipynb +++ b/papers/harmonic/2025_10_28_plot_whisker.ipynb @@ -17,43 +17,36 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "sys.path.append(\n", - " \"/home/guerrini/sp_validation/cosmo_inference/scripts/\"\n", - ")\n", + "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts/\")\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "import seaborn as sns\n", "import warnings\n", + "\n", "import chain_postprocessing as cp\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", - "plt.rc('text', usetex=True)\n", + "plt.rc(\"text\", usetex=True)\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "root_external = \"/n09data/guerrini/output_chains/ext_data/\"\n", "blind = \"B\"\n", "\n", - "colour_blind = {\n", - " \"A\": \"royalblue\",\n", - " \"B\": \"crimson\",\n", - " \"C\": \"forestgreen\"\n", - "}\n", + "colour_blind = {\"A\": \"royalblue\", \"B\": \"crimson\", \"C\": \"forestgreen\"}\n", "\n", "roots = [\n", " f\"SP_v1.4.6.3_leak_corr_{blind}\",\n", @@ -76,7 +69,7 @@ " f\"SP_v1.4.6.3_leak_corr_halofit_{blind}\",\n", " f\"SP_v1.4.6.3_leak_corr_HMCode_nobar_{blind}\",\n", " f\"SP_v1.4.6.3_leak_corr_OneCov_{blind}\",\n", - " f\"SP_v1.4.6.3_{blind}\"\n", + " f\"SP_v1.4.6.3_{blind}\",\n", "]\n", "\n", "legend_labels = [\n", @@ -148,24 +141,16 @@ " \"harmonic\",\n", " \"harmonic\",\n", " \"harmonic\",\n", - " \"harmonic\" \n", + " \"harmonic\",\n", "]\n", "\n", "# Add the blinds to each list\n", "for bl in [\"A\", \"B\", \"C\"]:\n", " if bl != blind:\n", - " roots.append(\n", - " f\"SP_v1.4.6.3_leak_corr_{bl}\"\n", - " )\n", - " legend_labels.append(\n", - " fr\"UNIONS $C_\\ell$, Blind {bl}\"\n", - " )\n", - " colours.append(\n", - " colour_blind[bl]\n", - " )\n", - " categories.append(\n", - " \"harmonic\"\n", - " )\n", + " roots.append(f\"SP_v1.4.6.3_leak_corr_{bl}\")\n", + " legend_labels.append(rf\"UNIONS $C_\\ell$, Blind {bl}\")\n", + " colours.append(colour_blind[bl])\n", + " categories.append(\"harmonic\")\n", "\n", "print(roots)" ] @@ -181,53 +166,34 @@ "for i, root in enumerate(roots):\n", " category = categories[i]\n", " if category != \"external\":\n", - " if category == 'configuration':\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/getdist_{root}\"\n", - " )\n", - " elif category == 'harmonic':\n", + " if category == \"configuration\":\n", + " path_samples = os.path.join(root_dir, f\"{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"{root}/getdist_{root}\")\n", + " elif category == \"harmonic\":\n", " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/samples_{root}_cell.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/getdist_{root}\"\n", + " root_dir, f\"{root}/{root}/samples_{root}_cell.txt\"\n", " )\n", + " path_getdist = os.path.join(root_dir, f\"{root}/{root}/getdist_{root}\")\n", " elif category == \"external_compute_sample\":\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", - " )\n", + " path_samples = os.path.join(root_dir, f\"ext_data/{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", " else:\n", " raise ValueError(f\"The category, {category}, of {root} is not correct\")\n", - " \n", + "\n", " if category == \"external_compute_sample\" and \"Legacy\" in root:\n", - " chain_type=\"nautilus\"\n", + " chain_type = \"nautilus\"\n", " else:\n", - " chain_type=\"polychord\"\n", - " cp.load_samples_and_write_paramnames(path_samples, path_getdist+\".paramnames\", chain_type=chain_type)\n", - " cp.write_samples_getdist_format(path_samples, path_getdist+\".txt\", chain_type=chain_type)\n", - " chains.append(\n", - " cp.load_chain(path_getdist, smoothing_scale=0.5)\n", + " chain_type = \"polychord\"\n", + " cp.load_samples_and_write_paramnames(\n", + " path_samples, path_getdist + \".paramnames\", chain_type=chain_type\n", " )\n", - " else:\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", + " cp.write_samples_getdist_format(\n", + " path_samples, path_getdist + \".txt\", chain_type=chain_type\n", " )\n", - " chains.append(\n", - " cp.load_chain(path_getdist)\n", - " )" + " chains.append(cp.load_chain(path_getdist, smoothing_scale=0.5))\n", + " else:\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", + " chains.append(cp.load_chain(path_getdist))" ] }, { @@ -237,8 +203,32 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','S_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_b h^2', r'h_0', r'n_s', r'\\sigma_8', r'S_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_b h^2\",\n", + " r\"h_0\",\n", + " r\"n_s\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for i, chain in enumerate(chains):\n", " print(legend_labels[i])\n", @@ -246,7 +236,7 @@ " for name, label in zip(name_list, label_list):\n", " try:\n", " param_names.parWithName(name).label = label\n", - " except:\n", + " except Exception:\n", " warnings.warn(f\"Parameter {name} not found in chain {roots[i]}.\")" ] }, @@ -259,42 +249,42 @@ "source": [ "# Micro management of external chains\n", "# Account for the missing parameter conventions\n", - "#OMEGA_M not in DES_Y3_cell\n", - "idx = roots.index('DES_Y3_cell')\n", - "cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')\n", + "# OMEGA_M not in DES_Y3_cell\n", + "idx = roots.index(\"DES_Y3_cell\")\n", + "cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")\n", "cp.derive_parameter_S8(chains[idx])\n", "\n", - "#OMEGA_M not in KiDS-1000\n", + "# OMEGA_M not in KiDS-1000\n", "try:\n", - " idx = roots.index('KiDS-1000')\n", - " cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')\n", - "except:\n", + " idx = roots.index(\"KiDS-1000\")\n", + " cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")\n", + "except Exception:\n", " print(\"KiDS-1000 chain not found, skipping parameter name adjustment.\")\n", "\n", - "#S8 to derive in KiDS-Legacy chains\n", + "# S8 to derive in KiDS-Legacy chains\n", "try:\n", - " idx = roots.index('KiDS-Legacy_xipm')\n", + " idx = roots.index(\"KiDS-Legacy_xipm\")\n", " cp.derive_parameter_S8(chains[idx])\n", - "except:\n", + "except Exception:\n", " print(\"KiDS-Legacy_xipm chain not found, skipping S8 derivation.\")\n", "try:\n", - " idx = roots.index('KiDS-Legacy_bandpowers')\n", + " idx = roots.index(\"KiDS-Legacy_bandpowers\")\n", " cp.derive_parameter_S8(chains[idx])\n", - "except:\n", + "except Exception:\n", " print(\"KiDS-Legacy_bandpowers chain not found, skipping S8 derivation.\")\n", "try:\n", - " idx = roots.index('KiDS-Legacy_cosebis')\n", + " idx = roots.index(\"KiDS-Legacy_cosebis\")\n", " cp.derive_parameter_S8(chains[idx])\n", - "except:\n", + "except Exception:\n", " print(\"KiDS-Legacy_cosebis chain not found, skipping S8 derivation.\")\n", "\n", - "#OMEGA_M not in DES+KiDS\n", - "idx = roots.index('DES+KiDS')\n", - "cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')\n", + "# OMEGA_M not in DES+KiDS\n", + "idx = roots.index(\"DES+KiDS\")\n", + "cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")\n", "\n", - "#OMEGA_M not in HSC_Y3_cell\n", - "idx = roots.index('HSC_Y3_cell')\n", - "cp.adjust_paramname_chain(chains[idx], 'omega_m', 'OMEGA_M', r'\\Omega_{\\rm m}')" + "# OMEGA_M not in HSC_Y3_cell\n", + "idx = roots.index(\"HSC_Y3_cell\")\n", + "cp.adjust_paramname_chain(chains[idx], \"omega_m\", \"OMEGA_M\", r\"\\Omega_{\\rm m}\")" ] }, { @@ -304,39 +294,60 @@ "metadata": {}, "outputs": [], "source": [ - "best_fit_method = \"2Dkde\" \n", - "\n", - "param_values = np.array([\"# Expt\", \"Colour\", \"S8_Mean\", \"S8_low\", \"S8_high\", \"sigma_8_Mean\", \"sigma_8_low\", \"sigma_8_high\", \"Omega_m_Mean\", \"Omega_m_low\", \"Omega_m_high\"])\n", - "escaped = np.char.replace(legend_labels, '\\\\', '\\\\\\\\')\n", + "best_fit_method = \"2Dkde\"\n", + "\n", + "param_values = np.array(\n", + " [\n", + " \"# Expt\",\n", + " \"Colour\",\n", + " \"S8_Mean\",\n", + " \"S8_low\",\n", + " \"S8_high\",\n", + " \"sigma_8_Mean\",\n", + " \"sigma_8_low\",\n", + " \"sigma_8_high\",\n", + " \"Omega_m_Mean\",\n", + " \"Omega_m_low\",\n", + " \"Omega_m_high\",\n", + " ]\n", + ")\n", + "escaped = np.char.replace(legend_labels, \"\\\\\", \"\\\\\\\\\")\n", "for i, chain in enumerate(chains):\n", " print(chain.root)\n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", "\n", - " s8_stats = margestats.parWithName('S_8')\n", - " sigma8_stats = margestats.parWithName('SIGMA_8')\n", - " omegam_stats = margestats.parWithName('OMEGA_M')\n", + " s8_stats = margestats.parWithName(\"S_8\")\n", + " sigma8_stats = margestats.parWithName(\"SIGMA_8\")\n", + " omegam_stats = margestats.parWithName(\"OMEGA_M\")\n", "\n", " best_fit = cp.extract_best_fit_params(chain, best_fit_method=best_fit_method)\n", "\n", - " param_values = np.vstack((\n", - " param_values,\n", - " [\n", - " escaped[i],\n", - " colours[i],\n", - " best_fit['S_8'],\n", - " best_fit['S_8']-s8_stats.limits[0].lower,\n", - " s8_stats.limits[0].upper-best_fit['S_8'],\n", - " best_fit['SIGMA_8'],\n", - " best_fit['SIGMA_8'] - sigma8_stats.limits[0].lower,\n", - " sigma8_stats.limits[0].upper - best_fit['SIGMA_8'],\n", - " best_fit['OMEGA_M'],\n", - " best_fit['OMEGA_M'] - omegam_stats.limits[0].lower,\n", - " omegam_stats.limits[0].upper - best_fit['OMEGA_M'],\n", - " ]\n", - " ))\n", + " param_values = np.vstack(\n", + " (\n", + " param_values,\n", + " [\n", + " escaped[i],\n", + " colours[i],\n", + " best_fit[\"S_8\"],\n", + " best_fit[\"S_8\"] - s8_stats.limits[0].lower,\n", + " s8_stats.limits[0].upper - best_fit[\"S_8\"],\n", + " best_fit[\"SIGMA_8\"],\n", + " best_fit[\"SIGMA_8\"] - sigma8_stats.limits[0].lower,\n", + " sigma8_stats.limits[0].upper - best_fit[\"SIGMA_8\"],\n", + " best_fit[\"OMEGA_M\"],\n", + " best_fit[\"OMEGA_M\"] - omegam_stats.limits[0].lower,\n", + " omegam_stats.limits[0].upper - best_fit[\"OMEGA_M\"],\n", + " ],\n", + " )\n", + " )\n", "print(param_values)\n", - "np.savetxt(f\"{root_dir}/param_values.txt\", param_values, fmt=['%s' for i in range(11)], delimiter=';')" + "np.savetxt(\n", + " f\"{root_dir}/param_values.txt\",\n", + " param_values,\n", + " fmt=[\"%s\" for i in range(11)],\n", + " delimiter=\";\",\n", + ")" ] }, { @@ -347,20 +358,50 @@ "outputs": [], "source": [ "# Load the value of the parameters\n", - "cosmo = np.loadtxt(f\"{root_dir}/param_values.txt\",\n", - " dtype={'names': ('Expt', 'colour', 's8_mean', 's8_low', 's8_high', 'sigma8_mean', 'sigma8_low', 'sigma8_high', 'omegam_mean', 'omegam_low', 'omegam_high'),\n", - " 'formats': ('U250', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20', 'U20')}, skiprows=1, delimiter=';')\n", - "expt = np.char.replace(cosmo['Expt'], '\\\\\\\\', '\\\\')\n", - "colours = cosmo['colour']\n", - "s8_mean = cosmo['s8_mean'].astype(np.float64)\n", - "s8_low = cosmo['s8_low'].astype(np.float64)\n", - "s8_high = cosmo['s8_high'].astype(np.float64)\n", - "sigma8_mean = cosmo['sigma8_mean'].astype(np.float64)\n", - "sigma8_low = cosmo['sigma8_low'].astype(np.float64)\n", - "sigma8_high = cosmo['sigma8_high'].astype(np.float64)\n", - "omegam_mean = cosmo['omegam_mean'].astype(np.float64)\n", - "omegam_low = cosmo['omegam_low'].astype(np.float64)\n", - "omegam_high = cosmo['omegam_high'].astype(np.float64)" + "cosmo = np.loadtxt(\n", + " f\"{root_dir}/param_values.txt\",\n", + " dtype={\n", + " \"names\": (\n", + " \"Expt\",\n", + " \"colour\",\n", + " \"s8_mean\",\n", + " \"s8_low\",\n", + " \"s8_high\",\n", + " \"sigma8_mean\",\n", + " \"sigma8_low\",\n", + " \"sigma8_high\",\n", + " \"omegam_mean\",\n", + " \"omegam_low\",\n", + " \"omegam_high\",\n", + " ),\n", + " \"formats\": (\n", + " \"U250\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " \"U20\",\n", + " ),\n", + " },\n", + " skiprows=1,\n", + " delimiter=\";\",\n", + ")\n", + "expt = np.char.replace(cosmo[\"Expt\"], \"\\\\\\\\\", \"\\\\\")\n", + "colours = cosmo[\"colour\"]\n", + "s8_mean = cosmo[\"s8_mean\"].astype(np.float64)\n", + "s8_low = cosmo[\"s8_low\"].astype(np.float64)\n", + "s8_high = cosmo[\"s8_high\"].astype(np.float64)\n", + "sigma8_mean = cosmo[\"sigma8_mean\"].astype(np.float64)\n", + "sigma8_low = cosmo[\"sigma8_low\"].astype(np.float64)\n", + "sigma8_high = cosmo[\"sigma8_high\"].astype(np.float64)\n", + "omegam_mean = cosmo[\"omegam_mean\"].astype(np.float64)\n", + "omegam_low = cosmo[\"omegam_low\"].astype(np.float64)\n", + "omegam_high = cosmo[\"omegam_high\"].astype(np.float64)" ] }, { @@ -408,16 +449,9 @@ " r\"Large scales only\",\n", " r\"\\texttt{HMCode} no baryons\",\n", " r\"\\texttt{OneCovariance} only\",\n", - " r\"No leakage correction\"\n", - "]\n", - "list_section_index = [\n", - " r\"(ii)\",\n", - " r\"(iii)\",\n", - " r\"(iv)\",\n", - " r\"(v)\",\n", - " r\"(vi)\",\n", - " r\"(vii)\"\n", + " r\"No leakage correction\",\n", "]\n", + "list_section_index = [r\"(ii)\", r\"(iii)\", r\"(iv)\", r\"(v)\", r\"(vi)\", r\"(vii)\"]\n", "\n", "preliminary_watermark = False\n", "blind_axes = False\n", @@ -429,78 +463,132 @@ "for ax, param in zip(axs, params):\n", " means, lows, highs, label = param\n", " for i, mean, low, high, color in zip(y, means, lows, highs, colours):\n", - " ax.errorbar(mean, 0.05+i*row_spacing, xerr=np.array([low, high])[:, None], fmt='o', color=color, ecolor=color, elinewidth=2, capsize=3)\n", + " ax.errorbar(\n", + " mean,\n", + " 0.05 + i * row_spacing,\n", + " xerr=np.array([low, high])[:, None],\n", + " fmt=\"o\",\n", + " color=color,\n", + " ecolor=color,\n", + " elinewidth=2,\n", + " capsize=3,\n", + " )\n", " ax.set_xlabel(label, fontsize=14)\n", - " \n", + "\n", " ax.grid(False)\n", - " ax.tick_params(axis='y', left=False, labelleft=False)\n", + " ax.tick_params(axis=\"y\", left=False, labelleft=False)\n", " if label == r\"$S_8$\":\n", - " ax.axvspan(s8_mean[index_ref] - s8_low[index_ref], s8_mean[index_ref] + s8_high[index_ref], color=colours[index_ref], alpha=0.2)\n", + " ax.axvspan(\n", + " s8_mean[index_ref] - s8_low[index_ref],\n", + " s8_mean[index_ref] + s8_high[index_ref],\n", + " color=colours[index_ref],\n", + " alpha=0.2,\n", + " )\n", " ax.set_xlim(0.20, 1.1)\n", " if blind_axes:\n", " ref_tick = np.mean(s8_mean[:4])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.1 for i in range(-5, 5)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.1 for i in range(-5, 5)], labels=[])\n", " else:\n", - " ax.set_xticks(\n", - " [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]\n", - " )\n", + " ax.set_xticks([0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])\n", " elif label == r\"$\\sigma_8$\":\n", - " ax.axvspan(sigma8_mean[index_ref] - sigma8_low[index_ref], sigma8_mean[index_ref] + sigma8_high[index_ref], color=colours[index_ref], alpha=0.2)\n", + " ax.axvspan(\n", + " sigma8_mean[index_ref] - sigma8_low[index_ref],\n", + " sigma8_mean[index_ref] + sigma8_high[index_ref],\n", + " color=colours[index_ref],\n", + " alpha=0.2,\n", + " )\n", " ax.set_xlim(0.5, 1.2)\n", " if blind_axes:\n", " ref_tick = np.mean(sigma8_mean[:4])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.2 for i in range(-2, 2)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.2 for i in range(-2, 2)], labels=[])\n", " elif label == r\"$\\Omega_{\\rm m}$\":\n", - " ax.axvspan(omegam_mean[index_ref] - omegam_low[index_ref], omegam_mean[index_ref] + omegam_high[index_ref], color=colours[index_ref], alpha=0.2)\n", + " ax.axvspan(\n", + " omegam_mean[index_ref] - omegam_low[index_ref],\n", + " omegam_mean[index_ref] + omegam_high[index_ref],\n", + " color=colours[index_ref],\n", + " alpha=0.2,\n", + " )\n", " ax.set_xlim(0.1, 0.5)\n", " if blind_axes:\n", " ref_tick = np.mean(omegam_mean[:4])\n", - " ax.set_xticks(\n", - " [ref_tick + i*0.1 for i in range(-2, 3)], labels=[]\n", - " )\n", + " ax.set_xticks([ref_tick + i * 0.1 for i in range(-2, 3)], labels=[])\n", "\n", "\n", - "axs[0].set_yticks(0.05+y*row_spacing)\n", + "axs[0].set_yticks(0.05 + y * row_spacing)\n", "axs[0].set_yticklabels([])\n", "for label, color in zip(expt, colours):\n", - " axs[0].text(0.21, 0.05 + row_spacing * np.where(expt == label)[0][0], label, fontsize=12, ha='left', va='center', color=color)\n", + " axs[0].text(\n", + " 0.21,\n", + " 0.05 + row_spacing * np.where(expt == label)[0][0],\n", + " label,\n", + " fontsize=12,\n", + " ha=\"left\",\n", + " va=\"center\",\n", + " color=color,\n", + " )\n", " if label != reference:\n", " index = np.where(expt == label)[0][0]\n", " s8_tension = get_sigma_tension(\n", - " s8_mean[index], s8_low[index], s8_high[index],\n", - " s8_mean[index_ref], s8_low[index_ref], s8_high[index_ref]\n", + " s8_mean[index],\n", + " s8_low[index],\n", + " s8_high[index],\n", + " s8_mean[index_ref],\n", + " s8_low[index_ref],\n", + " s8_high[index_ref],\n", " )\n", " sign_str = \"+\" if s8_tension > 0 else \"-\"\n", - " axs[0].text(1.0, 0.05 + row_spacing * index, rf\"${sign_str}{np.abs(s8_tension):.2f}\" + r\"\\, \\sigma$\", fontsize=10, ha='left', va='center', color=color)\n", + " axs[0].text(\n", + " 1.0,\n", + " 0.05 + row_spacing * index,\n", + " rf\"${sign_str}{np.abs(s8_tension):.2f}\" + r\"\\, \\sigma$\",\n", + " fontsize=10,\n", + " ha=\"left\",\n", + " va=\"center\",\n", + " color=color,\n", + " )\n", "# Add separation lines\n", "for i, sep in enumerate(separation_after):\n", " index_sep = np.where(expt == sep)[0][0]\n", " for ax in axs:\n", - " ax.axhline(row_spacing * (index_sep + 0.95), color='black', linestyle='dotted', linewidth=1)\n", - " axs[0].text(0.20, 0.05 + row_spacing * (index_sep + 1), \n", - " list_section_index[i], fontsize=14, fontweight='bold', va='center', ha='right')\n", + " ax.axhline(\n", + " row_spacing * (index_sep + 0.95),\n", + " color=\"black\",\n", + " linestyle=\"dotted\",\n", + " linewidth=1,\n", + " )\n", + " axs[0].text(\n", + " 0.20,\n", + " 0.05 + row_spacing * (index_sep + 1),\n", + " list_section_index[i],\n", + " fontsize=14,\n", + " fontweight=\"bold\",\n", + " va=\"center\",\n", + " ha=\"right\",\n", + " )\n", "\n", "\n", "# --- Add section labels (i), (ii)) ---\n", - "axs[0].text(0.20, 0.05, \n", - " r\"(i)\", fontsize=14, fontweight='bold', va='center', ha='right')\n", + "axs[0].text(0.20, 0.05, r\"(i)\", fontsize=14, fontweight=\"bold\", va=\"center\", ha=\"right\")\n", "\n", "if preliminary_watermark:\n", - " plt.figtext(0.5, 0.5, 'PRELIMINARY',\n", - " fontsize=50, color='gray',\n", - " ha='center', va='center',\n", - " alpha=0.3, rotation=330)\n", + " plt.figtext(\n", + " 0.5,\n", + " 0.5,\n", + " \"PRELIMINARY\",\n", + " fontsize=50,\n", + " color=\"gray\",\n", + " ha=\"center\",\n", + " va=\"center\",\n", + " alpha=0.3,\n", + " rotation=330,\n", + " )\n", "\n", "plt.gca().invert_yaxis()\n", "\n", "plt.tight_layout()\n", "\n", "plt.savefig(\"./plots/whisker_plot.png\", dpi=300)\n", - "#Save pdf\n", + "# Save pdf\n", "plt.savefig(\"./plots/whisker_plot.pdf\")\n", "plt.show()" ] diff --git a/papers/harmonic/2025_12_04_redshift_distribution.py b/papers/harmonic/2025_12_04_redshift_distribution.py index b992ec7c..7e5a373b 100644 --- a/papers/harmonic/2025_12_04_redshift_distribution.py +++ b/papers/harmonic/2025_12_04_redshift_distribution.py @@ -7,14 +7,12 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") -import numpy as np import matplotlib.pyplot as plt +import numpy as np import seaborn as sns -plt.style.use( - './/matplotlib_config/paper.mplstyle' -) -plt.rcParams['text.usetex'] = True +plt.style.use(".//matplotlib_config/paper.mplstyle") +plt.rcParams["text.usetex"] = True sns.set_palette("husl") @@ -23,7 +21,9 @@ # %% blind = "B" -path_redshift_distribution = f"/n17data/sguerrini/UNIONS/WL/nz/v1.4.6.3/nz_SP_v1.4.6.3_{blind}.txt" +path_redshift_distribution = ( + f"/n17data/sguerrini/UNIONS/WL/nz/v1.4.6.3/nz_SP_v1.4.6.3_{blind}.txt" +) redshift_distribution = np.loadtxt(path_redshift_distribution) @@ -43,10 +43,10 @@ ax.set_ylim(0, None) ax.set_xlabel(r"$z$", fontsize=14) ax.set_ylabel(r"$n(z)$", fontsize=14) -ax.tick_params(axis='both', which='major', labelsize=12) +ax.tick_params(axis="both", which="major", labelsize=12) plt.savefig("./plots/redshift_distribution_SP_v1.4.6.png", dpi=300) -#Save PDF +# Save PDF plt.savefig("./plots/redshift_distribution_SP_v1.4.6.pdf") plt.show() # %% diff --git a/papers/harmonic/2025_12_16_get_n_dof_from_glass.py b/papers/harmonic/2025_12_16_get_n_dof_from_glass.py index ff941bef..e0146fc4 100644 --- a/papers/harmonic/2025_12_16_get_n_dof_from_glass.py +++ b/papers/harmonic/2025_12_16_get_n_dof_from_glass.py @@ -7,19 +7,13 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") -import numpy as np -from astropy.io import fits -import scipy.stats as stats - import matplotlib.pyplot as plt +import numpy as np import seaborn as sns +from getdist import plots from tqdm import tqdm -from getdist import plots, MCSamples - -plt.style.use( - './matplotlib_config/paper.mplstyle' -) +plt.style.use("./matplotlib_config/paper.mplstyle") sns.set_palette("husl") @@ -34,7 +28,7 @@ root = f"glass_mock_v1_{str(num_sim).zfill(5)}" path_sim = f"{root_glass_chains}/{root}/{root}/samples_{root}.txt" with open(path_sim) as file: - params = file.readline()[1:].split('\t') + params = file.readline()[1:].split("\t") file.close() print(params) @@ -48,73 +42,66 @@ n_mocks = 350 chi2_map_configuration = [] chi2_map_harmonic = [] -for num_sim in tqdm(range(1, n_mocks+1)): +for num_sim in tqdm(range(1, n_mocks + 1)): root = f"glass_mock_v1_{str(num_sim).zfill(5)}" path_sim = f"{root_glass_chains}/{root}/{root}/samples_{root}.txt" samples = np.loadtxt(path_sim) - chi2_map_configuration.append(-2*samples[-1,-3]) + chi2_map_configuration.append(-2 * samples[-1, -3]) path_sim = f"{root_glass_chains}/{root}/{root}/samples_{root}_cell.txt" - samples = np.loadtxt(path_sim) - chi2_map_harmonic.append(-2*samples[-1, -3]) + samples = np.loadtxt(path_sim) + chi2_map_harmonic.append(-2 * samples[-1, -3]) # %% # Get p-value in harmonic space g = plots.get_subplot_plotter(width_inch=7) -root_harmonic = 'SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell' +root_harmonic = "SP_v1.4.6_leak_corr_A_lmin=300_lmax=1600_cell" # Load harmonic space result -path_harmonic = f"/n09data/guerrini/output_chains/{root_harmonic}/samples_{root_harmonic}.txt" +path_harmonic = ( + f"/n09data/guerrini/output_chains/{root_harmonic}/samples_{root_harmonic}.txt" +) samples_harmonic_fid = np.loadtxt(path_harmonic) -chi2_map_harmonic_fid = -2*samples_harmonic_fid[-1, -3] +chi2_map_harmonic_fid = -2 * samples_harmonic_fid[-1, -3] print(f"chi2_map_harmonic_fid: {chi2_map_harmonic_fid}") plt.figure() -counts, bin_edges = np.histogram( - chi2_map_harmonic, - bins=20, - density=True -) +counts, bin_edges = np.histogram(chi2_map_harmonic, bins=20, density=True) sns.histplot( chi2_map_harmonic, kde=False, bins=bin_edges, - stat='density', + stat="density", label=r"$\chi^2({\rm MAP})$ GLASS mock", - color='blue', - alpha=0.3 + color="blue", + alpha=0.3, ) -#plt.plot(x, chi2_stats, c='k') +# plt.plot(x, chi2_stats, c='k') -plt.xlabel(r'$\chi^2({\rm MAP})$') +plt.xlabel(r"$\chi^2({\rm MAP})$") plt.ylabel("Prob.") plt.savefig("./plots/chi2_map_harmonic_space.png", dpi=300) plt.show() # %% -root_configuration = 'SP_v1.4.6_leak_corr_A_10_80' +root_configuration = "SP_v1.4.6_leak_corr_A_10_80" # Load configuration space result path_configuration = f"/n09data/guerrini/output_chains/{root_configuration}/samples_{root_configuration}.txt" samples_configuration_fid = np.loadtxt(path_configuration) -chi2_map_configuration_fid = -2*samples_configuration_fid[-1, -3] +chi2_map_configuration_fid = -2 * samples_configuration_fid[-1, -3] print(f"chi2_map_configuration_fid: {chi2_map_configuration_fid}") plt.figure() -sns.histplot( - chi2_map_configuration, - bins=20, - kde=False, - stat='density' -) +sns.histplot(chi2_map_configuration, bins=20, kde=False, stat="density") plt.show() # %% diff --git a/papers/harmonic/2026_01_05_get_bestfit_glass_mock.py b/papers/harmonic/2026_01_05_get_bestfit_glass_mock.py index 4c74a553..d5ad2934 100644 --- a/papers/harmonic/2026_01_05_get_bestfit_glass_mock.py +++ b/papers/harmonic/2026_01_05_get_bestfit_glass_mock.py @@ -1,12 +1,10 @@ # %% -import os -import sys import configparser +import os import subprocess +import sys -sys.path.append( - "/home/guerrini/sp_validation/cosmo_inference/scripts/" -) +sys.path.append("/home/guerrini/sp_validation/cosmo_inference/scripts/") from IPython import get_ipython @@ -16,20 +14,13 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") -import numpy as np -from astropy.io import fits -import scipy.stats as stats - +import chain_postprocessing as cp import matplotlib.pyplot as plt +import numpy as np import seaborn as sns -from tqdm import tqdm - -from getdist import plots, MCSamples -import chain_postprocessing as cp +from getdist import plots -plt.style.use( - './matplotlib_config/paper.mplstyle' -) +plt.style.use("./matplotlib_config/paper.mplstyle") sns.set_palette("husl") @@ -37,23 +28,21 @@ ipython.run_line_magic("matplotlib", "inline") g = plots.get_subplot_plotter(width_inch=30) -g.settings.axes_fontsize=30 -g.settings.axes_labelsize=30 +g.settings.axes_fontsize = 30 +g.settings.axes_labelsize = 30 g.settings.alpha_filled_add = 0.7 g.settings.legend_fontsize = 40 # %% root_dir = "/n09data/guerrini/glass_mock_chains/" -chain_version = "v6" # choose v0 or v1 +chain_version = "v6" # choose v0 or v1 -#Path to the ini config files -path_ini_files = f'/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/glass_mocks_{chain_version}/' +# Path to the ini config files +path_ini_files = f"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config/glass_mocks_{chain_version}/" # Create the list of mocks max_sim = 350 -failed_simulations = [ - 82, 83, 281, 282, 283,284, 285, 286, 287 -] +failed_simulations = [82, 83, 281, 282, 283, 284, 285, 286, 287] roots = [] for i in range(1, max_sim + 1): if i in failed_simulations: @@ -63,47 +52,74 @@ lower_boud_cell_ee = 300.0 upper_bound_cell_ee = 1600.0 -run_best_fit_type = "2Dkde" +run_best_fit_type = "2Dkde" # %% # Retrieve the chains for root in roots: - with open(root_dir + '{}/{}/samples_{}_cell.txt'.format(root, root, root)) as file: - params = file.readline()[1:].split('\t')[:-4] + with open(root_dir + "{}/{}/samples_{}_cell.txt".format(root, root, root)) as file: + params = file.readline()[1:].split("\t")[:-4] file.close() - with open(root_dir + '{}/{}/getdist_{}_cell.paramnames'.format(root, root, root), "w") as file: + with open( + root_dir + "{}/{}/getdist_{}_cell.paramnames".format(root, root, root), "w" + ) as file: for i in range(len(params)): - if len(params[i].split('--')) > 1: - file.write(params[i].split('--')[1] + '\n') + if len(params[i].split("--")) > 1: + file.write(params[i].split("--")[1] + "\n") else: - file.write(params[i].split('--')[0] + '\n') + file.write(params[i].split("--")[0] + "\n") file.close() # Read chain -chains=[] +chains = [] for root in roots: - - samples = np.loadtxt(root_dir + '{}/{}/samples_{}_cell.txt'.format(root,root,root)) + samples = np.loadtxt( + root_dir + "{}/{}/samples_{}_cell.txt".format(root, root, root) + ) print(len(samples)) - if 'nautilus' in root: - samples = np.column_stack((np.exp(samples[:,-3]),samples[:,-1]-samples[:,-2],samples[:,0:-3])) + if "nautilus" in root: + samples = np.column_stack( + (np.exp(samples[:, -3]), samples[:, -1] - samples[:, -2], samples[:, 0:-3]) + ) else: - samples = np.column_stack((samples[:,-1],samples[:,-3],samples[:,0:-4])) - np.savetxt(root_dir + '{}/{}/getdist_{}_cell.txt'.format(root,root,root), samples) + samples = np.column_stack((samples[:, -1], samples[:, -3], samples[:, 0:-4])) + np.savetxt(root_dir + "{}/{}/getdist_{}_cell.txt".format(root, root, root), samples) - chain = g.samples_for_root(root_dir + '{}/{}/getdist_{}_cell'.format(root,root,root), - cache=False, - settings={'ignore_rows':0, - 'smooth_scale_2D':0.3, - 'smooth_scale_1D':0.3}) + chain = g.samples_for_root( + root_dir + "{}/{}/getdist_{}_cell".format(root, root, root), + cache=False, + settings={"ignore_rows": 0, "smooth_scale_2D": 0.3, "smooth_scale_1D": 0.3}, + ) chains.append(chain) # %% -name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','s_8_input', 'logt_agn','a','m1','bias_1'] -label_list = ['\Omega_m', '\omega_b h^2', 'h_0', 'n_s', '\sigma_8', 'S_8', 'log T_{AGN}', 'A_{IA}', 'm_1', '\Delta z_1'] +name_list = [ + "OMEGA_M", + "ombh2", + "h0", + "n_s", + "SIGMA_8", + "s_8_input", + "logt_agn", + "a", + "m1", + "bias_1", +] +label_list = [ + r"\Omega_m", + r"\omega_b h^2", + "h_0", + "n_s", + r"\sigma_8", + "S_8", + "log T_{AGN}", + "A_{IA}", + "m_1", + r"\Delta z_1", +] for chain in chains: param_names = chain.getParamNames() @@ -111,7 +127,7 @@ param_names.parWithName(name).label = label # %% -#Extract the best fit parameters +# Extract the best fit parameters best_fit = {} for root, chain in zip(roots, chains): @@ -122,11 +138,10 @@ ) - # %% # Run CosmoSis in test mode to get the data vectors -if not os.path.exists(path_ini_files+'/values_empty.ini'): +if not os.path.exists(path_ini_files + "/values_empty.ini"): content = """[cosmological_parameters] tau = 0.0544 @@ -147,36 +162,39 @@ [psf_leakage_parameters] """ - with open(path_ini_files+'/values_empty.ini', 'w') as f: + with open(path_ini_files + "/values_empty.ini", "w") as f: f.write(content) f.close() - print('File created successfully') + print("File created successfully") section_map = { - 'omch2': 'cosmological_parameters', - 'ombh2': 'cosmological_parameters', - 'h0': 'cosmological_parameters', - 'n_s': 'cosmological_parameters', - 's_8_input': 'cosmological_parameters', - 'logt_agn': 'halo_model_parameters', - 'a': 'intrinsic_alignment_parameters', - 'm1': 'shear_calibration_parameters', - 'bias_1': 'nofz_shifts', - 'alpha': 'psf_leakage_parameters', - 'beta': 'psf_leakage_parameters', + "omch2": "cosmological_parameters", + "ombh2": "cosmological_parameters", + "h0": "cosmological_parameters", + "n_s": "cosmological_parameters", + "s_8_input": "cosmological_parameters", + "logt_agn": "halo_model_parameters", + "a": "intrinsic_alignment_parameters", + "m1": "shear_calibration_parameters", + "bias_1": "nofz_shifts", + "alpha": "psf_leakage_parameters", + "beta": "psf_leakage_parameters", } env = os.environ.copy() -env["LD_LIBRARY_PATH"] = "/home/guerrini/.conda/envs/sp_validation_3.11/lib/python3.11/site-packages/cosmosis/datablock:" + env.get("LD_LIBRARY_PATH", "") +env["LD_LIBRARY_PATH"] = ( + "/home/guerrini/.conda/envs/sp_validation_3.11/lib/python3.11/site-packages/cosmosis/datablock:" + + env.get("LD_LIBRARY_PATH", "") +) -os.chdir('/home/guerrini/sp_validation/cosmo_inference/') +os.chdir("/home/guerrini/sp_validation/cosmo_inference/") for idx, root in enumerate(roots): print(root) config = configparser.ConfigParser() config.optionxform = str # Preserve case sensitivity of option names - config.read(path_ini_files+'/values_empty.ini') + config.read(path_ini_files + "/values_empty.ini") for param, value in best_fit[root].items(): section = section_map.get(param) if section is None: @@ -185,39 +203,38 @@ config.add_section(section) config[section][param] = str(value) - with open(path_ini_files+'/values_empty.ini', 'w') as configfile: + with open(path_ini_files + "/values_empty.ini", "w") as configfile: config.write(configfile) - #Modify the ini file to run in test mode at the best fit + # Modify the ini file to run in test mode at the best fit config = configparser.ConfigParser() config.optionxform = str # Preserve case sensitivity of option names - config_file_path = path_ini_files+f'/cosmosis_pipeline_glass_mocks_{chain_version}_glass_mock_{root[-5:]}_cell.ini' + config_file_path = ( + path_ini_files + + f"/cosmosis_pipeline_glass_mocks_{chain_version}_glass_mock_{root[-5:]}_cell.ini" + ) config.read(config_file_path) - sampler = config['runtime']['sampler'] - config['runtime']['sampler'] = 'test' - values = config['pipeline']['values'] - config['pipeline']['values'] = path_ini_files + '/values_empty.ini' - config['test']['save_dir'] = f"%(SCRATCH)s/best_fit/{root}_cell_{run_best_fit_type}" - + sampler = config["runtime"]["sampler"] + config["runtime"]["sampler"] = "test" + values = config["pipeline"]["values"] + config["pipeline"]["values"] = path_ini_files + "/values_empty.ini" + config["test"]["save_dir"] = f"%(SCRATCH)s/best_fit/{root}_cell_{run_best_fit_type}" - with open(config_file_path, 'w') as configfile: + with open(config_file_path, "w") as configfile: config.write(configfile) - #Run cosmosis + # Run cosmosis result = subprocess.run( - ['cosmosis', config_file_path], - env=env, - capture_output=True, - text=True + ["cosmosis", config_file_path], env=env, capture_output=True, text=True ) print(f"STDOUT:\n{result.stdout}") print(f"STDERR:\n{result.stderr}") - #Modify the ini file to the previous one - config['pipeline']['values'] = values - config['runtime']['sampler'] = sampler - config['test']['save_dir'] = f"%(SCRATCH)s/best_fit/{root}_cell" + # Modify the ini file to the previous one + config["pipeline"]["values"] = values + config["runtime"]["sampler"] = sampler + config["test"]["save_dir"] = f"%(SCRATCH)s/best_fit/{root}_cell" - with open(config_file_path, 'w') as configfile: + with open(config_file_path, "w") as configfile: config.write(configfile) # %% diff --git a/papers/harmonic/2026_01_05_get_p_value_glass_mock.py b/papers/harmonic/2026_01_05_get_p_value_glass_mock.py index bb14d06e..12e2032b 100644 --- a/papers/harmonic/2026_01_05_get_p_value_glass_mock.py +++ b/papers/harmonic/2026_01_05_get_p_value_glass_mock.py @@ -1,7 +1,7 @@ # %% import os -#Trick to use latex in the plots +# Trick to use latex in the plots os.environ["LD_LIBRARY_PATH"] = "" os.environ["CONDA_PREFIX"] = "/home/guerrini/.conda/envs/phd_manuscript" @@ -13,25 +13,20 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import matplotlib.pyplot as plt import numpy as np -from astropy.io import fits import scipy.stats as stats -from scipy.interpolate import interp1d - -import matplotlib.pyplot as plt -from matplotlib import scale as mscale import seaborn as sns -from tqdm import tqdm +from astropy.io import fits +from matplotlib import scale as mscale +from scipy.interpolate import interp1d from sp_validation.rho_tau import SquareRootScale mscale.register_scale(SquareRootScale) -from getdist import plots, MCSamples -plt.style.use( - './matplotlib_config/paper.mplstyle' -) +plt.style.use("./matplotlib_config/paper.mplstyle") sns.set_palette("husl") @@ -45,31 +40,42 @@ chain_version = "v6" # Path to the glass mock data vectors -root_glass_dv = f"/home/guerrini/sp_validation/cosmo_inference/data/glass_mocks/{chain_version}/" +root_glass_dv = ( + f"/home/guerrini/sp_validation/cosmo_inference/data/glass_mocks/{chain_version}/" +) # Choose the best-fit method best_fit_method = "2Dkde" # Create the list of mocks max_sim = 350 -failed_simulations = [ - 82, 83, 281, 282, 283,284, 285, 286, 287 +failed_simulations = [82, 83, 281, 282, 283, 284, 285, 286, 287] +roots_glass_mock = [ + f"glass_mock_{chain_version}_{str(i).zfill(5)}" for i in range(1, max_sim + 1) ] -roots_glass_mock = [f"glass_mock_{chain_version}_{str(i).zfill(5)}" for i in range(1, max_sim + 1)] lower_bound_cell_ee = 300.0 upper_bound_cell_ee = 1600.0 + # %% -def get_chi2_glass_mock(root, root_glass_chains, root_glass_dv, lower_bound, upper_bound): - #Read the theory prediction at best fit - ell = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/ell.txt") - shear_cl = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/bin_1_1.txt") +def get_chi2_glass_mock( + root, root_glass_chains, root_glass_dv, lower_bound, upper_bound +): + # Read the theory prediction at best fit + ell = np.loadtxt( + f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/ell.txt" + ) + shear_cl = np.loadtxt( + f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/bin_1_1.txt" + ) - #Read the data + # Read the data idx = root.replace(f"glass_mock_{chain_version}_", "") chain_version_data = "v0" if chain_version in ["v0", "v1"] else chain_version - data = fits.open(f"{root_glass_dv}/glass_mock_{idx}/cosmosis_glass_mock_{chain_version_data}_{idx}.fits") + data = fits.open( + f"{root_glass_dv}/glass_mock_{idx}/cosmosis_glass_mock_{chain_version_data}_{idx}.fits" + ) ell_data = data["CELL_EE"].data["ANG"] cell_data = data["CELL_EE"].data["VALUE"] @@ -79,7 +85,7 @@ def get_chi2_glass_mock(root, root_glass_chains, root_glass_dv, lower_bound, upp cov_cell = cov[80:, 80:] # Interpolate the model - interp_cell_ee = interp1d(ell, shear_cl, kind='cubic', fill_value='extrapolate') + interp_cell_ee = interp1d(ell, shear_cl, kind="cubic", fill_value="extrapolate") cell_model = interp_cell_ee(ell_data) @@ -90,16 +96,21 @@ def get_chi2_glass_mock(root, root_glass_chains, root_glass_dv, lower_bound, upp cell_data = cell_data[mask_cell] cov_cell = cov_cell[mask_cell][:, mask_cell] - cell_chi2 = np.dot((cell_model - cell_data), np.dot(np.linalg.inv(cov_cell), (cell_model - cell_data))) + cell_chi2 = np.dot( + (cell_model - cell_data), + np.dot(np.linalg.inv(cov_cell), (cell_model - cell_data)), + ) return cell_chi2 + def get_chi2_map_glass_mock(root, root_glass_chains): path_sim = f"{root_glass_chains}/{root}/{root}/samples_{root}_cell.txt" samples = np.loadtxt(path_sim) cell_chi2_map = samples[-1, -3] - return -2*cell_chi2_map + return -2 * cell_chi2_map + # %% metrics = {} @@ -108,51 +119,63 @@ def get_chi2_map_glass_mock(root, root_glass_chains): if i + 1 in failed_simulations: continue metrics[root] = {} - metrics[root]["chi2"] = get_chi2_glass_mock(root, root_glass_chains, root_glass_dv, lower_bound_cell_ee, upper_bound_cell_ee) + metrics[root]["chi2"] = get_chi2_glass_mock( + root, root_glass_chains, root_glass_dv, lower_bound_cell_ee, upper_bound_cell_ee + ) metrics[root]["chi2_map"] = get_chi2_map_glass_mock(root, root_glass_chains) # %% -chi2_glass_mocks = np.array([metrics[root]["chi2"] for i, root in enumerate(roots_glass_mock) if i + 1 not in failed_simulations]) -chi2_glass_mocks_map = np.array([metrics[root]["chi2_map"] for i, root in enumerate(roots_glass_mock) if i + 1 not in failed_simulations]) - -sns.histplot( - chi2_glass_mocks, - bins=25, - kde=False, - stat='density', - label=best_fit_method +chi2_glass_mocks = np.array( + [ + metrics[root]["chi2"] + for i, root in enumerate(roots_glass_mock) + if i + 1 not in failed_simulations + ] +) +chi2_glass_mocks_map = np.array( + [ + metrics[root]["chi2_map"] + for i, root in enumerate(roots_glass_mock) + if i + 1 not in failed_simulations + ] ) sns.histplot( - chi2_glass_mocks_map, - bins=25, - kde=False, - stat='density', - label='MLE' + chi2_glass_mocks, bins=25, kde=False, stat="density", label=best_fit_method ) +sns.histplot(chi2_glass_mocks_map, bins=25, kde=False, stat="density", label="MLE") + k = 10 x = np.linspace(0, 25) chi2 = stats.chi2.pdf(x, k) -plt.plot(x, chi2, c='k', label=rf"$n_{{\rm dof}}={k}$") +plt.plot(x, chi2, c="k", label=rf"$n_{{\rm dof}}={k}$") plt.xlabel(r"$\chi^2$") plt.legend() plt.show() + + # %% def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): - #Read the theory prediction at best fit - ell = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/ell.txt") - shear_cl = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/bin_1_1.txt") + # Read the theory prediction at best fit + ell = np.loadtxt( + f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/ell.txt" + ) + shear_cl = np.loadtxt( + f"{root_glass_chains}/{root}/best_fit/{root}_cell_{best_fit_method}/shear_cl/bin_1_1.txt" + ) - #ell_map = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_map/{root}_cell_map/shear_cl/ell.txt") - #shear_cl_map = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_map/{root}_cell_map/shear_cl/bin_1_1.txt") + # ell_map = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_map/{root}_cell_map/shear_cl/ell.txt") + # shear_cl_map = np.loadtxt(f"{root_glass_chains}/{root}/best_fit/{root}_cell_map/{root}_cell_map/shear_cl/bin_1_1.txt") - #Read the data + # Read the data idx = root.replace(f"glass_mock_{chain_version}_", "") chain_version_data = "v0" if chain_version in ["v0", "v1"] else chain_version - data = fits.open(f"{root_glass_dv}/glass_mock_{idx}/cosmosis_glass_mock_{chain_version_data}_{idx}.fits") + data = fits.open( + f"{root_glass_dv}/glass_mock_{idx}/cosmosis_glass_mock_{chain_version_data}_{idx}.fits" + ) ell_data = data["CELL_EE"].data["ANG"] cell_data = data["CELL_EE"].data["VALUE"] @@ -161,19 +184,14 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): plt.errorbar( ell_data, - ell_data*cell_data, - yerr=ell_data*np.sqrt(data["COVMAT"].data.diagonal()[80:]), - fmt='o', - label='Data', - alpha=0.5 + ell_data * cell_data, + yerr=ell_data * np.sqrt(data["COVMAT"].data.diagonal()[80:]), + fmt="o", + label="Data", + alpha=0.5, ) - plt.plot( - ell, - ell*shear_cl, - label='Best-fit model', - c='r' - ) + plt.plot(ell, ell * shear_cl, label="Best-fit model", c="r") """ plt.plot( ell_map, @@ -189,11 +207,12 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): plt.ylim(bottom=0.5e-7) plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell^{EE}$") - plt.xscale('squareroot') + plt.xscale("squareroot") plt.legend() return fig + # %% fig = plot_glass_mock_fit_root(roots_glass_mock[89], root_glass_chains, root_glass_dv) @@ -206,15 +225,13 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): path_first_run = "/n09data/guerrini/glass_mock_v1.4.6/results/" path_second_run = "/n09data/guerrini/glass_mock_v1.4.6_rerun/results/" -first_run = np.load( - f"{path_first_run}/cl_glass_mock_{idx_sim}_4096.npy" -) -second_run = np.load( - f"{path_second_run}/cl_glass_mock_{idx_sim}_4096.npy" -) +first_run = np.load(f"{path_first_run}/cl_glass_mock_{idx_sim}_4096.npy") +second_run = np.load(f"{path_second_run}/cl_glass_mock_{idx_sim}_4096.npy") chain_version_data = "v0" if chain_version in ["v0", "v1"] else "v2" -data = fits.open(f"{root_glass_dv}/glass_mock_{idx_sim}/cosmosis_glass_mock_{chain_version_data}_{idx_sim}.fits") +data = fits.open( + f"{root_glass_dv}/glass_mock_{idx_sim}/cosmosis_glass_mock_{chain_version_data}_{idx_sim}.fits" +) plt.figure() @@ -222,21 +239,21 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): first_run[0], first_run[0] * first_run[1], yerr=first_run[0] * np.sqrt(data["COVMAT"].data.diagonal()[80:]), - fmt='o', - label='First run', - alpha=0.5 + fmt="o", + label="First run", + alpha=0.5, ) plt.errorbar( second_run[0], second_run[0] * second_run[1], yerr=second_run[0] * np.sqrt(data["COVMAT"].data.diagonal()[80:]), - fmt='o', - label='Second run', - alpha=0.5 + fmt="o", + label="Second run", + alpha=0.5, ) -plt.xscale('squareroot') +plt.xscale("squareroot") plt.legend() plt.show() @@ -249,12 +266,8 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): path_first_run = "/n09data/guerrini/glass_mock_v1.4.6/results/" path_second_run = "/n09data/guerrini/glass_mock_v1.4.6_rerun/results/" - first_run = np.load( - f"{path_first_run}/cl_glass_mock_{idx_sim}_4096.npy" - ) - second_run = np.load( - f"{path_second_run}/cl_glass_mock_{idx_sim}_4096.npy" - ) + first_run = np.load(f"{path_first_run}/cl_glass_mock_{idx_sim}_4096.npy") + second_run = np.load(f"{path_second_run}/cl_glass_mock_{idx_sim}_4096.npy") first_run_cls.append(first_run[1]) second_run_cls.append(second_run[1]) @@ -268,7 +281,7 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): plt.plot(np.sqrt(cov_first_run.diagonal())) plt.plot(np.sqrt(cov_second_run.diagonal())) -plt.yscale('log') +plt.yscale("log") plt.show() # %% @@ -287,7 +300,9 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): low_ell_cut = 300 high_ell_cut = 1600 -mask = (dv_fiducial["CELL_EE"].data["ANG"] > low_ell_cut) & (dv_fiducial["CELL_EE"].data["ANG"] < high_ell_cut) +mask = (dv_fiducial["CELL_EE"].data["ANG"] > low_ell_cut) & ( + dv_fiducial["CELL_EE"].data["ANG"] < high_ell_cut +) cell_fiducial = cell_fiducial[mask] cov_fiducial = cov_fiducial[mask][:, mask] @@ -297,34 +312,33 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): ) cell_bf = np.loadtxt( f"/n09data/guerrini/output_chains/{root_fiducial}/best_fit/shear_cl/bin_1_1.txt" -) +) # Interpolate the model -interp_cell_ee = interp1d(ell_bf, cell_bf, kind='cubic', fill_value='extrapolate') +interp_cell_ee = interp1d(ell_bf, cell_bf, kind="cubic", fill_value="extrapolate") cell_bf = interp_cell_ee(dv_fiducial["CELL_EE"].data["ANG"][mask]) -chi2_fiducial = np.dot((cell_bf - cell_fiducial), np.dot(np.linalg.inv(cov_fiducial), (cell_bf - cell_fiducial))) +chi2_fiducial = np.dot( + (cell_bf - cell_fiducial), + np.dot(np.linalg.inv(cov_fiducial), (cell_bf - cell_fiducial)), +) print(f"Chi2 for the fiducial: {chi2_fiducial}") # %% # Make the plot -output_fig_path = f"/home/guerrini/sp_validation/papers/harmonic/plots/" -counts, bin_edges = np.histogram( - chi2_glass_mocks, - bins=25, - density=True -) +output_fig_path = "/home/guerrini/sp_validation/papers/harmonic/plots/" +counts, bin_edges = np.histogram(chi2_glass_mocks, bins=25, density=True) sns.histplot( chi2_glass_mocks, kde=False, bins=bin_edges, - stat='density', + stat="density", label=r"$\chi^2$ for \texttt{GLASS} mocks best-fits", - color='blue', - alpha=0.3 + color="blue", + alpha=0.3, ) # Compute the p-value @@ -337,10 +351,14 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): print(f"P-value: {p_value}") -plt.axvline(chi2_fiducial, color='red', ls='--', label=r'$\chi^2$ of the fiducial') +plt.axvline(chi2_fiducial, color="red", ls="--", label=r"$\chi^2$ of the fiducial") mantissa, exponent = np.frexp(p_value) -pte_string = rf"${{\rm PTE}} = {mantissa:.2f} \times 10^{{{exponent}}}$" if exponent != 0 else rf"${{\rm PTE}} = {p_value:.2f}$" +pte_string = ( + rf"${{\rm PTE}} = {mantissa:.2f} \times 10^{{{exponent}}}$" + if exponent != 0 + else rf"${{\rm PTE}} = {p_value:.2f}$" +) x_text = 40 y_text = max(counts) * 0.6 @@ -349,12 +367,7 @@ def plot_glass_mock_fit_root(root, root_glass_chains, root_glass_dv): chi2_string = rf"$\chi^2_{{\rm fid}} = {chi2_fiducial:.1f}$" x_text = 40 y_text = max(counts) * 0.5 -plt.text( - x_text, - y_text, - chi2_string, - fontsize=12 -) +plt.text(x_text, y_text, chi2_string, fontsize=12) plt.xlabel(r"$\chi^2$") plt.legend(fontsize=12) diff --git a/papers/harmonic/2026_01_13_check_BB_covariance.py b/papers/harmonic/2026_01_13_check_BB_covariance.py index f62d1662..8c444025 100644 --- a/papers/harmonic/2026_01_13_check_BB_covariance.py +++ b/papers/harmonic/2026_01_13_check_BB_covariance.py @@ -14,25 +14,18 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import matplotlib.pyplot as plt import numpy as np +import scipy.stats as stats +import seaborn as sns from astropy.io import fits -import matplotlib.pyplot as plt -from matplotlib.gridspec import GridSpec -import matplotlib.ticker as mticker from matplotlib import scale as mscale -from mpl_toolkits.axes_grid1 import make_axes_locatable -import seaborn as sns -from tqdm import tqdm -import healpy as hp -import scipy.stats as stats from sp_validation.rho_tau import SquareRootScale mscale.register_scale(SquareRootScale) -plt.style.use( - './matplotlib_config/paper.mplstyle' -) +plt.style.use("./matplotlib_config/paper.mplstyle") sns.set_palette("husl") @@ -40,10 +33,7 @@ ipython.run_line_magic("matplotlib", "inline") # %% -versions = [ - "SP_v1.4.6_leak_corr_A", - "SP_v1.4.6_leak_corr_A_KiDS" -] +versions = ["SP_v1.4.6_leak_corr_A", "SP_v1.4.6_leak_corr_A_KiDS"] path_cosmo_val = "/home/guerrini/sp_validation/cosmo_val/output/" @@ -52,22 +42,28 @@ for ver in versions: cov = fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{ver}.fits") - plt.plot(np.sqrt(np.diag(cov['COVAR_BB_BB'].data))) - print(np.sqrt(np.diag(cov['COVAR_BB_BB'].data))) + plt.plot(np.sqrt(np.diag(cov["COVAR_BB_BB"].data))) + print(np.sqrt(np.diag(cov["COVAR_BB_BB"].data))) -plt.yscale('log') +plt.yscale("log") plt.show() # %% plt.figure() -ref = np.sqrt(np.diag(fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{versions[0]}.fits")['COVAR_BB_BB'].data)) +ref = np.sqrt( + np.diag( + fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{versions[0]}.fits")[ + "COVAR_BB_BB" + ].data + ) +) for ver in versions: cov = fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{ver}.fits") - plt.plot(np.sqrt(np.diag(cov['COVAR_BB_BB'].data))/ref) - print(np.sqrt(np.diag(cov['COVAR_BB_BB'].data))) + plt.plot(np.sqrt(np.diag(cov["COVAR_BB_BB"].data)) / ref) + print(np.sqrt(np.diag(cov["COVAR_BB_BB"].data))) plt.show() @@ -76,21 +72,27 @@ for ver in versions: cov = fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{ver}.fits") - plt.plot(np.sqrt(np.diag(cov['COVAR_EE_EE'].data))) - print(np.sqrt(np.diag(cov['COVAR_EE_EE'].data))) + plt.plot(np.sqrt(np.diag(cov["COVAR_EE_EE"].data))) + print(np.sqrt(np.diag(cov["COVAR_EE_EE"].data))) -plt.yscale('log') +plt.yscale("log") plt.show() # %% plt.figure() -ref = np.sqrt(np.diag(fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{versions[0]}.fits")['COVAR_EE_EE'].data)) +ref = np.sqrt( + np.diag( + fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{versions[0]}.fits")[ + "COVAR_EE_EE" + ].data + ) +) for ver in versions: cov = fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{ver}.fits") - plt.plot(np.sqrt(np.diag(cov['COVAR_EE_EE'].data))/ref) - print(np.sqrt(np.diag(cov['COVAR_EE_EE'].data))) + plt.plot(np.sqrt(np.diag(cov["COVAR_EE_EE"].data)) / ref) + print(np.sqrt(np.diag(cov["COVAR_EE_EE"].data))) plt.show() @@ -103,10 +105,10 @@ cov = fits.open(f"{path_cosmo_val}/pseudo_cl_cov_{ver}.fits") cl = fits.getdata(f"{path_cosmo_val}/pseudo_cl_{ver}.fits") - ell = cl['ELL'] + ell = cl["ELL"] mask = (ell >= lower_bound_cell) & (ell <= upper_bound_cell) - data_vector = cl['BB'][mask] + data_vector = cl["BB"][mask] cov_cut = cov["COVAR_BB_BB"].data[mask][:, mask] chi2 = data_vector @ np.linalg.inv(cov_cut) @ data_vector diff --git a/papers/harmonic/2026_03_17_data_vector_and_best_fit.py b/papers/harmonic/2026_03_17_data_vector_and_best_fit.py index d9af88c8..4fbfb569 100644 --- a/papers/harmonic/2026_03_17_data_vector_and_best_fit.py +++ b/papers/harmonic/2026_03_17_data_vector_and_best_fit.py @@ -1,35 +1,24 @@ # %% import os -import configparser -import subprocess import sys -import warnings # Append any useful folder in the path -sys.path.append( - "/home/guerrini/sp_validation/cosmo_inference/scripts/" -) +sys.path.append("/home/guerrini/sp_validation/cosmo_inference/scripts/") sys.path.append( "/home/guerrini/sp_validation/cosmo_inference/notebooks/2D_cosmic_shear_unblinding/" ) -from getdist import plots, loadMCSamples -from astropy.io import fits -import numpy as np import matplotlib.pyplot as plt -from scipy.interpolate import interp1d -import scipy.stats as stats -from IPython.display import Markdown, display -import healpy as hp import matplotlib.scale as mscale -import matplotlib.ticker as ticker -import matplotlib.transforms as mtransforms import seaborn as sns +from getdist import plots from sp_validation.rho_tau import SquareRootScale + mscale.register_scale(SquareRootScale) import IPython + ipython = IPython.get_ipython() if ipython is not None: @@ -40,7 +29,6 @@ ipython.run_line_magic("matplotlib", "inline") import chain_postprocessing as cp -import utils plt.style.use( "/home/guerrini/sp_validation/papers/harmonic/matplotlib_config/paper.mplstyle" @@ -51,8 +39,8 @@ sns.set_palette("husl") g = plots.get_subplot_plotter(width_inch=30) -g.settings.axes_fontsize=30 -g.settings.axes_labelsize=30 +g.settings.axes_fontsize = 30 +g.settings.axes_labelsize = 30 g.settings.alpha_filled_add = 0.7 g.settings.legend_fontsize = 40 @@ -60,50 +48,44 @@ root_dir = "/n09data/guerrini/output_chains" # Path to the ini files used -path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config' -path_datavectors = '/home/guerrini/sp_validation/cosmo_inference/data/' +path_ini_files = "/home/guerrini/sp_validation/cosmo_inference/cosmosis_config" +path_datavectors = "/home/guerrini/sp_validation/cosmo_inference/data/" path_output_chains = "/n09data/guerrini/output_chains/" best_fit_method = "2Dkde" -assert best_fit_method in ["2Dkde", "1Dkde", "weighted_mean"], "Invalid best fit method. Choose one of: '2Dkde', '1Dkde', 'weighted_mean'" +assert best_fit_method in ["2Dkde", "1Dkde", "weighted_mean"], ( + "Invalid best fit method. Choose one of: '2Dkde', '1Dkde', 'weighted_mean'" +) # %% -# Prepare settings for the plot +# Prepare settings for the plot blind = "B" -colour_blind = { - "A": "royalblue", - "B": "crimson", - "C": "forestgreen" -} +colour_blind = {"A": "royalblue", "B": "crimson", "C": "forestgreen"} root_to_plot = [ f"SP_v1.4.6.3_leak_corr_{blind}", f"SP_v1.4.6.3_leak_corr_halofit_{blind}", - f"SP_v1.4.6.3_{blind}_fiducial_config" + f"SP_v1.4.6.3_{blind}_fiducial_config", ] labels = [ r"UNIONS $C_\ell$", r"UNIONS $C_\ell$, \texttt{Halofit}", - r"UNIONS $\xi_\pm(\vartheta)$, (Goh et al., 2026)" + r"UNIONS $\xi_\pm(\vartheta)$, (Goh et al., 2026)", ] line_args = [ - {'color': colour_blind[blind], 'linestyle': '-'}, - {'color': colour_blind[blind], 'linestyle': '--'}, - {'color': 'orange', 'linestyle': '-'} + {"color": colour_blind[blind], "linestyle": "-"}, + {"color": colour_blind[blind], "linestyle": "--"}, + {"color": "orange", "linestyle": "-"}, ] -analysis_type = [ - "harmonic", - "harmonic", - "configuration" -] +analysis_type = ["harmonic", "harmonic", "configuration"] ini_file_root = os.path.join( path_ini_files, - f'config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini' + f"config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini", ) properties = {} @@ -113,7 +95,7 @@ root, path_ini_files, with_configuration=analysis_type[i] == "configuration", - path_to_this_ini=ini_file_root if analysis_type[i] == "configuration" else None + path_to_this_ini=ini_file_root if analysis_type[i] == "configuration" else None, ) # %% @@ -121,48 +103,26 @@ for i, root in enumerate(root_to_plot): analysis_type_i = analysis_type[i] if analysis_type_i == "harmonic": - path_samples = os.path.join( - root_dir, - root, - root, - f"samples_{root}_cell.txt" - ) - path_gd = os.path.join( - root_dir, - root, - root, - f"getdist_{root}_cell" - ) + path_samples = os.path.join(root_dir, root, root, f"samples_{root}_cell.txt") + path_gd = os.path.join(root_dir, root, root, f"getdist_{root}_cell") elif analysis_type_i == "configuration": - path_samples = os.path.join( - root_dir, - root, - f"samples_{root}.txt" - ) - path_gd = os.path.join( - root_dir, - root, - f"getdist_{root}" - ) + path_samples = os.path.join(root_dir, root, f"samples_{root}.txt") + path_gd = os.path.join(root_dir, root, f"getdist_{root}") else: raise ValueError(f"Invalid analysis type: {analysis_type_i}") - + # Read files, format and load chains - cp.load_samples_and_write_paramnames(path_samples, path_gd+".paramnames") - cp.write_samples_getdist_format(path_samples, path_gd+".txt") + cp.load_samples_and_write_paramnames(path_samples, path_gd + ".paramnames") + cp.write_samples_getdist_format(path_samples, path_gd + ".txt") chain = cp.load_chain(path_gd, smoothing_scale=0.5) # Get the bestfit parameters best_fit = cp.extract_best_fit_params(chain, best_fit_method=best_fit_method) - print(best_fit['S_8']) + print(best_fit["S_8"]) # Get the datavector for the best fit if analysis_type_i == "harmonic": cp.compute_best_fit( - path_ini_files, - best_fit, - root, - is_harmonic=True, - blind=blind + path_ini_files, best_fit, root, is_harmonic=True, blind=blind ) elif analysis_type_i == "configuration": cp.compute_best_fit( @@ -171,11 +131,11 @@ root, is_harmonic=False, blind=blind, - ini_file_root=ini_file_root + ini_file_root=ini_file_root, ) else: raise ValueError(f"Invalid analysis type: {analysis_type_i}") - + # %% # Plot hyperparameter @@ -193,7 +153,7 @@ labels=labels, loc_legend=loc_legend, bbox_to_anchor=bbox_to_anchor, - properties=properties + properties=properties, ) # Perform the plot PDF format @@ -206,6 +166,6 @@ labels=labels, loc_legend=loc_legend, bbox_to_anchor=bbox_to_anchor, - properties=properties + properties=properties, ) # %% diff --git a/papers/harmonic/2026_03_24_make_latex_table_results.ipynb b/papers/harmonic/2026_03_24_make_latex_table_results.ipynb index 113329b7..25f96b98 100644 --- a/papers/harmonic/2026_03_24_make_latex_table_results.ipynb +++ b/papers/harmonic/2026_03_24_make_latex_table_results.ipynb @@ -17,43 +17,36 @@ "os.environ[\"LD_LIBRARY_PATH\"] = \"\"\n", "os.environ[\"CONDA_PREFIX\"] = \"/home/guerrini/.conda/envs/sp_validation_3.11\"\n", "\n", - "sys.path.append(\n", - " \"/home/guerrini/sp_validation/cosmo_inference/scripts/\"\n", - ")\n", + "sys.path.append(\"/home/guerrini/sp_validation/cosmo_inference/scripts/\")\n", "\n", - "from getdist import plots, loadMCSamples\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "import seaborn as sns\n", "import warnings\n", + "\n", "import chain_postprocessing as cp\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import seaborn as sns\n", + "from getdist import plots\n", "\n", - "plt.style.use(\n", - " \"/home/guerrini/matplotlib_config/paper.mplstyle\"\n", - ")\n", + "plt.style.use(\"/home/guerrini/matplotlib_config/paper.mplstyle\")\n", "\n", - "plt.rc('text', usetex=True)\n", + "plt.rc(\"text\", usetex=True)\n", "\n", "sns.set_palette(\"husl\")\n", "\n", "g = plots.get_subplot_plotter(width_inch=30)\n", - "g.settings.axes_fontsize=60\n", - "g.settings.axes_labelsize=60\n", + "g.settings.axes_fontsize = 60\n", + "g.settings.axes_labelsize = 60\n", "g.settings.alpha_filled_add = 0.7\n", "g.settings.legend_fontsize = 60\n", "\n", "%matplotlib inline\n", "\n", - "#SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", + "# SPECIFY DATA DIRECTORY AND DESIRED CHAINS TO ANALYSE\n", "root_dir = \"/n09data/guerrini/output_chains/\"\n", "root_external = \"/n09data/guerrini/output_chains/ext_data/\"\n", "blind = \"B\"\n", "\n", - "colour_blind = {\n", - " \"A\": \"royalblue\",\n", - " \"B\": \"crimson\",\n", - " \"C\": \"forestgreen\"\n", - "}\n", + "colour_blind = {\"A\": \"royalblue\", \"B\": \"crimson\", \"C\": \"forestgreen\"}\n", "\n", "roots = [\n", " f\"SP_v1.4.6.3_leak_corr_{blind}\",\n", @@ -67,7 +60,7 @@ " f\"SP_v1.4.6.3_leak_corr_halofit_{blind}\",\n", " f\"SP_v1.4.6.3_leak_corr_HMCode_nobar_{blind}\",\n", " f\"SP_v1.4.6.3_leak_corr_OneCov_{blind}\",\n", - " f\"SP_v1.4.6.3_{blind}\"\n", + " f\"SP_v1.4.6.3_{blind}\",\n", "]\n", "\n", "legend_labels = [\n", @@ -113,24 +106,16 @@ " \"harmonic\",\n", " \"harmonic\",\n", " \"harmonic\",\n", - " \"harmonic\" \n", + " \"harmonic\",\n", "]\n", "\n", "# Add the blinds to each list\n", "for bl in [\"A\", \"B\", \"C\"]:\n", " if bl != blind:\n", - " roots.append(\n", - " f\"SP_v1.4.6.3_leak_corr_{bl}\"\n", - " )\n", - " legend_labels.append(\n", - " fr\"UNIONS $C_\\ell$, Blind {bl}\"\n", - " )\n", - " colours.append(\n", - " colour_blind[bl]\n", - " )\n", - " categories.append(\n", - " \"harmonic\"\n", - " )\n", + " roots.append(f\"SP_v1.4.6.3_leak_corr_{bl}\")\n", + " legend_labels.append(rf\"UNIONS $C_\\ell$, Blind {bl}\")\n", + " colours.append(colour_blind[bl])\n", + " categories.append(\"harmonic\")\n", "\n", "print(roots)" ] @@ -146,53 +131,34 @@ "for i, root in enumerate(roots):\n", " category = categories[i]\n", " if category != \"external\":\n", - " if category == 'configuration':\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/getdist_{root}\"\n", - " )\n", - " elif category == 'harmonic':\n", + " if category == \"configuration\":\n", + " path_samples = os.path.join(root_dir, f\"{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"{root}/getdist_{root}\")\n", + " elif category == \"harmonic\":\n", " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/samples_{root}_cell.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"{root}/{root}/getdist_{root}\"\n", + " root_dir, f\"{root}/{root}/samples_{root}_cell.txt\"\n", " )\n", + " path_getdist = os.path.join(root_dir, f\"{root}/{root}/getdist_{root}\")\n", " elif category == \"external_compute_sample\":\n", - " path_samples = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/samples_{root}.txt\"\n", - " )\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", - " )\n", + " path_samples = os.path.join(root_dir, f\"ext_data/{root}/samples_{root}.txt\")\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", " else:\n", " raise ValueError(f\"The category, {category}, of {root} is not correct\")\n", - " \n", + "\n", " if category == \"external_compute_sample\" and \"Legacy\" in root:\n", - " chain_type=\"nautilus\"\n", + " chain_type = \"nautilus\"\n", " else:\n", - " chain_type=\"polychord\"\n", - " cp.load_samples_and_write_paramnames(path_samples, path_getdist+\".paramnames\", chain_type=chain_type)\n", - " cp.write_samples_getdist_format(path_samples, path_getdist+\".txt\", chain_type=chain_type)\n", - " chains.append(\n", - " cp.load_chain(path_getdist, smoothing_scale=0.5)\n", + " chain_type = \"polychord\"\n", + " cp.load_samples_and_write_paramnames(\n", + " path_samples, path_getdist + \".paramnames\", chain_type=chain_type\n", " )\n", - " else:\n", - " path_getdist = os.path.join(\n", - " root_dir,\n", - " f\"ext_data/{root}/getdist_{root}\"\n", + " cp.write_samples_getdist_format(\n", + " path_samples, path_getdist + \".txt\", chain_type=chain_type\n", " )\n", - " chains.append(\n", - " cp.load_chain(path_getdist)\n", - " )" + " chains.append(cp.load_chain(path_getdist, smoothing_scale=0.5))\n", + " else:\n", + " path_getdist = os.path.join(root_dir, f\"ext_data/{root}/getdist_{root}\")\n", + " chains.append(cp.load_chain(path_getdist))" ] }, { @@ -202,8 +168,32 @@ "metadata": {}, "outputs": [], "source": [ - "name_list = ['OMEGA_M','ombh2','h0','n_s','SIGMA_8','S_8','s_8_input', 'logt_agn','a','m1','bias_1']\n", - "label_list = [r'\\Omega_{\\rm m}', r'\\omega_b h^2', r'h_0', r'n_s', r'\\sigma_8', r'S_8', r'S_8', r'\\log T_{\\rm AGN}', r'A_{\\rm IA}', r'm_1', r'\\Delta z_1']\n", + "name_list = [\n", + " \"OMEGA_M\",\n", + " \"ombh2\",\n", + " \"h0\",\n", + " \"n_s\",\n", + " \"SIGMA_8\",\n", + " \"S_8\",\n", + " \"s_8_input\",\n", + " \"logt_agn\",\n", + " \"a\",\n", + " \"m1\",\n", + " \"bias_1\",\n", + "]\n", + "label_list = [\n", + " r\"\\Omega_{\\rm m}\",\n", + " r\"\\omega_b h^2\",\n", + " r\"h_0\",\n", + " r\"n_s\",\n", + " r\"\\sigma_8\",\n", + " r\"S_8\",\n", + " r\"S_8\",\n", + " r\"\\log T_{\\rm AGN}\",\n", + " r\"A_{\\rm IA}\",\n", + " r\"m_1\",\n", + " r\"\\Delta z_1\",\n", + "]\n", "\n", "for i, chain in enumerate(chains):\n", " print(legend_labels[i])\n", @@ -211,7 +201,7 @@ " for name, label in zip(name_list, label_list):\n", " try:\n", " param_names.parWithName(name).label = label\n", - " except:\n", + " except Exception:\n", " warnings.warn(f\"Parameter {name} not found in chain {roots[i]}.\")" ] }, @@ -222,30 +212,46 @@ "metadata": {}, "outputs": [], "source": [ - "best_fit_method = \"2Dkde\" \n", - "\n", - "param_values = np.array([\"# Expt\", \"Colour\", \"S8_Mean\", \"S8_low\", \"S8_high\", \"sigma_8_Mean\", \"sigma_8_low\", \"sigma_8_high\", \"Omega_m_Mean\", \"Omega_m_low\", \"Omega_m_high\"])\n", - "escaped = np.char.replace(legend_labels, '\\\\', '\\\\\\\\')\n", + "best_fit_method = \"2Dkde\"\n", + "\n", + "param_values = np.array(\n", + " [\n", + " \"# Expt\",\n", + " \"Colour\",\n", + " \"S8_Mean\",\n", + " \"S8_low\",\n", + " \"S8_high\",\n", + " \"sigma_8_Mean\",\n", + " \"sigma_8_low\",\n", + " \"sigma_8_high\",\n", + " \"Omega_m_Mean\",\n", + " \"Omega_m_low\",\n", + " \"Omega_m_high\",\n", + " ]\n", + ")\n", + "escaped = np.char.replace(legend_labels, \"\\\\\", \"\\\\\\\\\")\n", "best_fit = {}\n", "for i, chain in enumerate(chains):\n", " print(chain.root)\n", " margestats = chain.getMargeStats()\n", " likestats = chain.getLikeStats()\n", "\n", - " s8_stats = margestats.parWithName('S_8')\n", - " sigma8_stats = margestats.parWithName('SIGMA_8')\n", - " omegam_stats = margestats.parWithName('OMEGA_M')\n", + " s8_stats = margestats.parWithName(\"S_8\")\n", + " sigma8_stats = margestats.parWithName(\"SIGMA_8\")\n", + " omegam_stats = margestats.parWithName(\"OMEGA_M\")\n", "\n", - " best_fit[roots[i]] = cp.extract_best_fit_params(chain, best_fit_method=best_fit_method)\n", + " best_fit[roots[i]] = cp.extract_best_fit_params(\n", + " chain, best_fit_method=best_fit_method\n", + " )\n", "\n", " for param_name in name_list:\n", " upper, lower, _, _ = cp.compute_limits(chain, param_name)\n", " try:\n", " param_stats = margestats.parWithName(param_name)\n", " best_fit[roots[i]][f\"{param_name}_low\"] = lower\n", - " \n", + "\n", " best_fit[roots[i]][f\"{param_name}_high\"] = upper\n", - " except:\n", + " except Exception:\n", " warnings.warn(f\"Parameter {param_name} not found in chain {roots[i]}.\")" ] }, @@ -257,7 +263,7 @@ "outputs": [], "source": [ "# Run the best fit estimation for all the chains\n", - "path_ini_files = '/home/guerrini/sp_validation/cosmo_inference/cosmosis_config'\n", + "path_ini_files = \"/home/guerrini/sp_validation/cosmo_inference/cosmosis_config\"\n", "fiducial_root_cell = fiducial_root_cell = f\"SP_v1.4.6.3_leak_corr_{blind}\"\n", "fiducial_root_xi_data = f\"SP_v1.4.6.3_leak_corr_{blind}_masked\"\n", "fiducial_root_xi_chains = f\"SP_v1.4.6.3_{blind}_fiducial_config\"\n", @@ -267,16 +273,12 @@ " if categories[i] == \"harmonic\":\n", " blind_ = root.split(\"_\")[-1]\n", " cp.compute_best_fit(\n", - " path_ini_files, \n", - " best_fit[root],\n", - " root,\n", - " is_harmonic=True,\n", - " blind=blind_\n", + " path_ini_files, best_fit[root], root, is_harmonic=True, blind=blind_\n", " )\n", " elif categories[i] == \"configuration\":\n", " ini_file_root = os.path.join(\n", " path_ini_files,\n", - " f'config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini'\n", + " f\"config_space_v1.4.6.3_fiducial/pipeline/blind_{blind}/fiducial.ini\",\n", " )\n", " cp.compute_best_fit(\n", " path_ini_files,\n", @@ -284,7 +286,7 @@ " fiducial_root_xi_chains,\n", " is_harmonic=False,\n", " blind=blind,\n", - " ini_file_root=ini_file_root\n", + " ini_file_root=ini_file_root,\n", " )" ] }, @@ -298,47 +300,43 @@ "from astropy.io import fits\n", "from scipy.interpolate import interp1d\n", "\n", + "\n", "def get_chi2_ndata_xi_fiducial(output_chains):\n", - " path_best_fit_output = f\"{output_chains}/SP_v1.4.6.3_{blind}_fiducial_config/best_fit/\"\n", + " path_best_fit_output = (\n", + " f\"{output_chains}/SP_v1.4.6.3_{blind}_fiducial_config/best_fit/\"\n", + " )\n", "\n", " data = fits.open(\n", " f\"/home/guerrini/sp_validation/cosmo_inference/data/SP_v1.4.6.3_config/SP_v1.4.6.3_{blind}/cosmosis_SP_v1.4.6.3_leak_corr_{blind}_masked.fits\"\n", " )\n", "\n", " # Load the best fit data vector\n", - " theta = np.loadtxt(\n", - " path_best_fit_output + \"/shear_xi_plus/theta.txt\"\n", - " )\n", + " theta = np.loadtxt(path_best_fit_output + \"/shear_xi_plus/theta.txt\")\n", " theta_arcmin = theta * 180 * 60 / np.pi\n", - " shear_xi_plus = np.loadtxt(\n", - " path_best_fit_output + \"/shear_xi_plus/bin_1_1.txt\"\n", - " )\n", - " shear_xi_minus = np.loadtxt(\n", - " path_best_fit_output + \"/shear_xi_minus/bin_1_1.txt\"\n", - " )\n", - " xi_sys_plus = np.loadtxt(\n", - " path_best_fit_output + \"/xi_sys/shear_xi_plus.txt\"\n", - " )\n", - " xi_sys_minus = np.loadtxt(\n", - " path_best_fit_output + \"/xi_sys/shear_xi_minus.txt\"\n", - " )\n", + " shear_xi_plus = np.loadtxt(path_best_fit_output + \"/shear_xi_plus/bin_1_1.txt\")\n", + " shear_xi_minus = np.loadtxt(path_best_fit_output + \"/shear_xi_minus/bin_1_1.txt\")\n", + " xi_sys_plus = np.loadtxt(path_best_fit_output + \"/xi_sys/shear_xi_plus.txt\")\n", + " xi_sys_minus = np.loadtxt(path_best_fit_output + \"/xi_sys/shear_xi_minus.txt\")\n", "\n", " # Load the data vectors and covariance matrix\n", - " theta_data = data['XI_PLUS'].data['ANG']\n", - " xi_plus_data = data['XI_PLUS'].data['VALUE']\n", - " xi_minus_data = data['XI_MINUS'].data['VALUE']\n", - " \n", + " theta_data = data[\"XI_PLUS\"].data[\"ANG\"]\n", + " xi_plus_data = data[\"XI_PLUS\"].data[\"VALUE\"]\n", + " xi_minus_data = data[\"XI_MINUS\"].data[\"VALUE\"]\n", + "\n", " # interpolate the model\n", - " interp_xi_plus = interp1d(theta_arcmin, shear_xi_plus, kind='cubic', fill_value='extrapolate')\n", - " interp_xi_minus = interp1d(theta_arcmin, shear_xi_minus, kind='cubic', fill_value='extrapolate')\n", + " interp_xi_plus = interp1d(\n", + " theta_arcmin, shear_xi_plus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", + " interp_xi_minus = interp1d(\n", + " theta_arcmin, shear_xi_minus, kind=\"cubic\", fill_value=\"extrapolate\"\n", + " )\n", "\n", " xi_plus_model = interp_xi_plus(theta_data)\n", " xi_plus_model += xi_sys_plus\n", " xi_minus_model = interp_xi_minus(theta_data)\n", " xi_minus_model += xi_sys_minus\n", "\n", - "\n", - " cov = data['COVMAT'].data[0:2*len(theta_data), 0:2*len(theta_data)]\n", + " cov = data[\"COVMAT\"].data[0 : 2 * len(theta_data), 0 : 2 * len(theta_data)]\n", "\n", " # Concatenate the data and model vectors\n", " xi_data = np.concatenate([xi_plus_data, xi_minus_data])\n", @@ -349,11 +347,13 @@ " lower_bound_xi_m = 12\n", " upper_bound_xi_m = 83\n", "\n", - " selection = np.concatenate([\n", - " (theta_data > lower_bound_xi_p) & (theta_data < upper_bound_xi_p),\n", - " (theta_data > lower_bound_xi_m) & (theta_data < upper_bound_xi_m)\n", - " ])\n", - " \n", + " selection = np.concatenate(\n", + " [\n", + " (theta_data > lower_bound_xi_p) & (theta_data < upper_bound_xi_p),\n", + " (theta_data > lower_bound_xi_m) & (theta_data < upper_bound_xi_m),\n", + " ]\n", + " )\n", + "\n", " xi_data = xi_data[selection]\n", " xi_model = xi_model[selection]\n", " cov = cov[selection][:, selection]\n", @@ -364,14 +364,13 @@ " return chi2, n_data\n", "\n", "\n", - "\n", "def get_latex_table(roots, best_fit, categories, labels):\n", " latex_lines = [\n", " r\"\\begin{tabular}{l|c|c|c|c|c|c|c}\",\n", " r\"\\hline\",\n", " r\"\\hline\",\n", " r\"Experiment name & $S_8$ & $\\Omega_m$ & $\\sigma_8$ & $A_\\mathrm{IA}$ & $\\log T_\\mathrm{AGN}$ & $\\chi^2$ & $N_{\\rm data}$ \\\\ \",\n", - " r\"\\hline\"\n", + " r\"\\hline\",\n", " ]\n", "\n", " for i, (root, cat) in enumerate(zip(roots, categories)):\n", @@ -381,30 +380,29 @@ " # read the chi2 and p-value from the best fit output\n", " path_best_fit_output = f\"/n09data/guerrini/output_chains/{root}/best_fit/\"\n", "\n", - " if cat == 'harmonic':\n", - " with open(f'{path_best_fit_output}/data_vector/values.txt', 'r') as f:\n", + " if cat == \"harmonic\":\n", + " with open(f\"{path_best_fit_output}/data_vector/values.txt\", \"r\") as f:\n", " for line in f:\n", " # Remove whitespace and split by '='\n", - " if '=' in line:\n", - " key, value = line.strip().split('=')\n", + " if \"=\" in line:\n", + " key, value = line.strip().split(\"=\")\n", " key = key.strip()\n", - " value = float(value.strip()) # Convert to number\n", - " \n", - " if key == '2pt_like_chi2':\n", + " value = float(value.strip()) # Convert to number\n", + "\n", + " if key == \"2pt_like_chi2\":\n", " chi2 = value\n", - " elif key == '2pt_like_n':\n", - " n_data = int(value) # n is usually an integer count\n", - " elif cat == 'configuration':\n", + " elif key == \"2pt_like_n\":\n", + " n_data = int(value) # n is usually an integer count\n", + " elif cat == \"configuration\":\n", " output_chains = \"/n09data/guerrini/output_chains/\"\n", " chi2, n_data = get_chi2_ndata_xi_fiducial(output_chains)\n", " else:\n", " raise ValueError(f\"Category {cat} not recognized for root {root}.\")\n", "\n", - "\n", - " if ('halofit' in root) or ('HMCode_nobar' in root):\n", + " if (\"halofit\" in root) or (\"HMCode_nobar\" in root):\n", " logt_agn_str = \"N/A\"\n", " else:\n", - " logt_agn_str = f\"${best_fit_vals['logt_agn']:.3f}^{{+{(best_fit_vals['logt_agn_high']-best_fit_vals['logt_agn']):.3f}}}_{{-{(best_fit_vals['logt_agn'] - best_fit_vals['logt_agn_low']) :.3f}}}$\"\n", + " logt_agn_str = f\"${best_fit_vals['logt_agn']:.3f}^{{+{(best_fit_vals['logt_agn_high'] - best_fit_vals['logt_agn']):.3f}}}_{{-{(best_fit_vals['logt_agn'] - best_fit_vals['logt_agn_low']):.3f}}}$\"\n", " line = (\n", " f\"{label} & \"\n", " rf\" ${best_fit_vals['S_8']:.3f}^{{+{(best_fit_vals['S_8_high'] - best_fit_vals['S_8']):.3f}}}_{{-{(best_fit_vals['S_8'] - best_fit_vals['S_8_low']):.3f}}}$ & \"\n", diff --git a/papers/harmonic/S8_whisker.ipynb b/papers/harmonic/S8_whisker.ipynb index 181a752c..5f26cdd8 100644 --- a/papers/harmonic/S8_whisker.ipynb +++ b/papers/harmonic/S8_whisker.ipynb @@ -15,21 +15,16 @@ "metadata": {}, "outputs": [], "source": [ - "import matplotlib\n", - "from astropy.io import fits\n", "import matplotlib.pyplot as plt\n", - "import uncertainties\n", - "import yaml\n", - "import os\n", "import numpy as np\n", "\n", "plt.rcParams.update({\"text.usetex\": True})\n", - "plt.rcParams.update({'font.size': 25})\n", - "plt.rc('mathtext', fontset='stix')\n", - "plt.rc('font', family='serif')\n", + "plt.rcParams.update({\"font.size\": 25})\n", + "plt.rc(\"mathtext\", fontset=\"stix\")\n", + "plt.rc(\"font\", family=\"serif\")\n", "\n", - "chains_dir = f\"/n09data/guerrini/output_chains/\"\n", - "num_test_cases = 1\n" + "chains_dir = \"/n09data/guerrini/output_chains/\"\n", + "num_test_cases = 1" ] }, { @@ -38,14 +33,22 @@ "metadata": {}, "outputs": [], "source": [ - "# Read in text file with s8 values for the different test cases and external experiments \n", + "# Read in text file with s8 values for the different test cases and external experiments\n", "# (have had to run getdist to analyse the chains first)\n", "\n", - "s8s = np.loadtxt(f\"{chains_dir}/S8_means.txt\",dtype={'names': ('Expt', 's8_mean', 's8_low', 's8_high'), 'formats': ('U40', 'U20', 'U20', 'U20')}, skiprows=1, delimiter=',')\n", - "expt = s8s['Expt']\n", - "s8s_mean = s8s['s8_mean'].astype(np.float64)\n", - "s8s_low = s8s['s8_low'].astype(np.float64)\n", - "s8s_high = s8s['s8_high'].astype(np.float64)" + "s8s = np.loadtxt(\n", + " f\"{chains_dir}/S8_means.txt\",\n", + " dtype={\n", + " \"names\": (\"Expt\", \"s8_mean\", \"s8_low\", \"s8_high\"),\n", + " \"formats\": (\"U40\", \"U20\", \"U20\", \"U20\"),\n", + " },\n", + " skiprows=1,\n", + " delimiter=\",\",\n", + ")\n", + "expt = s8s[\"Expt\"]\n", + "s8s_mean = s8s[\"s8_mean\"].astype(np.float64)\n", + "s8s_low = s8s[\"s8_low\"].astype(np.float64)\n", + "s8s_high = s8s[\"s8_high\"].astype(np.float64)" ] }, { @@ -54,30 +57,48 @@ "metadata": {}, "outputs": [], "source": [ - "\n", - "fig, axs = plt.subplots(1, 1, sharey=True, figsize=[7,1.5*len(expt)])\n", + "fig, axs = plt.subplots(1, 1, sharey=True, figsize=[7, 1.5 * len(expt)])\n", "axs.yaxis.set_visible(False)\n", "\n", - "y = np.arange(0,len(expt))\n", + "y = np.arange(0, len(expt))\n", "\n", "for i in y:\n", - " if i > len(expt)-num_test_cases-1:\n", - " axs.errorbar(s8s_mean[i], i+1, xerr=np.vstack((s8s_low[i],s8s_high[i])), fmt='o', c = 'darkblue',lw = 2, capsize=5,capthick=2)\n", + " if i > len(expt) - num_test_cases - 1:\n", + " axs.errorbar(\n", + " s8s_mean[i],\n", + " i + 1,\n", + " xerr=np.vstack((s8s_low[i], s8s_high[i])),\n", + " fmt=\"o\",\n", + " c=\"darkblue\",\n", + " lw=2,\n", + " capsize=5,\n", + " capthick=2,\n", + " )\n", " else:\n", - " axs.errorbar(s8s_mean[i], i+1, xerr=np.vstack((s8s_low[i],s8s_high[i])), fmt='o', c = 'darkgreen',lw = 2, capsize=5,capthick=2)\n", - " \n", + " axs.errorbar(\n", + " s8s_mean[i],\n", + " i + 1,\n", + " xerr=np.vstack((s8s_low[i], s8s_high[i])),\n", + " fmt=\"o\",\n", + " c=\"darkgreen\",\n", + " lw=2,\n", + " capsize=5,\n", + " capthick=2,\n", + " )\n", + "\n", " \"\"\" if i == 0: # Plot the band for \"Planck\"\n", " axs.axvspan(s8s_mean[i]-s8s_low[i], s8s_mean[i]+s8s_high[i], alpha=0.2, color='cyan')\n", " if i == len(expt)-1: # Plot the band for \"this work\"\n", " axs.axvspan(s8s_mean[i]-s8s_low[i], s8s_mean[i]+s8s_high[i], alpha=0.2, color='lightpink')\"\"\"\n", - " if i == len(expt)-num_test_cases-1: # Make a distinction between this work (and all its test cases) with external datasets\n", - " axs.axhline(i+1.5, ls='dashed',c='k') \n", - " \n", - " \n", - " axs.set_xlabel(r'$S_8=\\sigma_8\\sqrt{\\Omega_{\\rm m}/0.3}$') \n", - " axs.text(0.62, i+1, rf\"{expt[i]}\")\n", + " if (\n", + " i == len(expt) - num_test_cases - 1\n", + " ): # Make a distinction between this work (and all its test cases) with external datasets\n", + " axs.axhline(i + 1.5, ls=\"dashed\", c=\"k\")\n", "\n", - "plt.ylim([0,i+2])\n", + " axs.set_xlabel(r\"$S_8=\\sigma_8\\sqrt{\\Omega_{\\rm m}/0.3}$\")\n", + " axs.text(0.62, i + 1, rf\"{expt[i]}\")\n", + "\n", + "plt.ylim([0, i + 2])\n", "# plt.savefig('plots/s8_whisker.pdf',bbox_inches=\"tight\")" ] }, @@ -88,10 +109,10 @@ "outputs": [], "source": [ "def compute_sigma_shift(s8_1, s8_2, s8_1_low, s8_1_high, s8_2_low, s8_2_high):\n", - " \"\"\" Compute the tension between two measurements of S8 given their 1-sigma errorbars.\n", - " The errorbars can be asymmetric.\n", - " The tension is computed as the distance between the two measurements divided by the sum in quadrature of the 1-sigma errorbars.\n", - " If the errorbars are asymmetric, we use the errorbar that is in the direction of the other measurement.\n", + " \"\"\"Compute the tension between two measurements of S8 given their 1-sigma errorbars.\n", + " The errorbars can be asymmetric.\n", + " The tension is computed as the distance between the two measurements divided by the sum in quadrature of the 1-sigma errorbars.\n", + " If the errorbars are asymmetric, we use the errorbar that is in the direction of the other measurement.\n", " \"\"\"\n", " if s8_1 < s8_2:\n", " sigma_1 = s8_1_high\n", @@ -110,13 +131,19 @@ "outputs": [], "source": [ "def compute_matrix_sigma_shift(expt):\n", - " \"\"\" Compute the tension between all measurements of S8 and the reference measurement (first one).\n", - " \"\"\"\n", + " \"\"\"Compute the tension between all measurements of S8 and the reference measurement (first one).\"\"\"\n", " tensions = np.zeros((len(expt), len(expt)))\n", " for i in range(len(expt)):\n", " for j in range(len(expt)):\n", " if i != j:\n", - " sigma_shift = compute_sigma_shift(s8s_mean[i], s8s_mean[j], s8s_low[i], s8s_high[i], s8s_low[j], s8s_high[j])\n", + " sigma_shift = compute_sigma_shift(\n", + " s8s_mean[i],\n", + " s8s_mean[j],\n", + " s8s_low[i],\n", + " s8s_high[i],\n", + " s8s_low[j],\n", + " s8s_high[j],\n", + " )\n", " tensions[i, j] = sigma_shift\n", " return tensions" ] @@ -138,14 +165,14 @@ "source": [ "plt.figure()\n", "\n", - "plt.matshow(tensions, cmap='coolwarm')\n", + "plt.matshow(tensions, cmap=\"coolwarm\")\n", "plt.colorbar()\n", "\n", "# Add values on top of each cell\n", "for (i, j), val in np.ndenumerate(tensions):\n", - " plt.text(j, i, f\"{val:.2f}$\\\\sigma$\", ha='center', va='center', color='black')\n", + " plt.text(j, i, f\"{val:.2f}$\\\\sigma$\", ha=\"center\", va=\"center\", color=\"black\")\n", "\n", - "labels = [f\"$\\ell_\\mathrm{{max}}={i}$\" for i in [800, 1200, 2048]]\n", + "labels = [rf\"$\\ell_\\mathrm{{max}}={i}$\" for i in [800, 1200, 2048]]\n", "plt.xticks(ticks=np.arange(len(labels)), labels=labels, rotation=45)\n", "plt.yticks(ticks=np.arange(len(labels)), labels=labels)\n", "\n", diff --git a/papers/harmonic/utils.py b/papers/harmonic/utils.py index 5e2952d0..49e7b88c 100644 --- a/papers/harmonic/utils.py +++ b/papers/harmonic/utils.py @@ -1,6 +1,7 @@ import numpy as np import scipy.stats as stats + def get_chi2_and_pte(data_vector, cov, verbose=True): """ Calculate chi2 and pte for a given data vector and covariance matrix. @@ -9,16 +10,16 @@ def get_chi2_and_pte(data_vector, cov, verbose=True): chi2 = data_vector @ np.linalg.inv(cov) @ data_vector if verbose: print(f"Chi2: {chi2:.4f}") - - #Calculate the reduced chi^2 + + # Calculate the reduced chi^2 dof = len(data_vector) reduced_chi2 = chi2 / dof if verbose: print(f"Reduced Chi2: {reduced_chi2:.4f}") - + # Calculate pte pte = 1 - stats.chi2.cdf(chi2, dof) if verbose: print(f"PTE: {pte:.4f}") - - return chi2, reduced_chi2, pte \ No newline at end of file + + return chi2, reduced_chi2, pte diff --git a/pyproject.toml b/pyproject.toml index c05b9606..7b8f1ed6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -121,6 +121,12 @@ markers = [ line-length = 88 target-version = "py311" +# Snakemake injects a `snakemake` object into rule scripts at runtime, so ruff +# can't see where it's defined. Declaring it a builtin silences the false +# `F821 undefined-name` it raises in `workflow/scripts/*` (~110 hits) while +# still catching every other undefined name in those scripts. +builtins = ["snakemake"] + [tool.ruff.lint] select = [ "E", # pycodestyle errors @@ -130,5 +136,19 @@ select = [ ] ignore = [ "E712", # comparison to True should be 'if cond is True:' or 'if cond:' + "E501", # line-too-long: the formatter owns line width (ruff's own guidance) ] +# Region-aware relaxations. src/ stays strict (no ignores). The peripheral +# trees — workflow scripts, per-paper analysis scripts, top-level scripts, and +# scratch — legitimately do sys.path manipulation before importing (E402) and +# pull names in via `from x import *` (F403/F405); enforcing those there would +# be noise, not signal. +[tool.ruff.lint.per-file-ignores] +"workflow/scripts/*" = ["E402"] +"papers/*/scripts/*" = ["E402", "F403", "F405"] +"papers/*/*.py" = ["E402", "F403", "F405"] # per-paper analysis scripts live at this level too +"scripts/*" = ["E402", "F403", "F405"] +"scratch/*" = ["E402", "F403", "F405"] +"cosmo_inference/**" = ["E402"] # notebooks + scripts do sys.path + mpl-backend setup before imports (covers .ipynb cells, not just .py) + diff --git a/scratch/guerrini/compute_pte_cell.py b/scratch/guerrini/compute_pte_cell.py index e543eef6..b5f2f38e 100644 --- a/scratch/guerrini/compute_pte_cell.py +++ b/scratch/guerrini/compute_pte_cell.py @@ -6,15 +6,13 @@ """ # %% +import camb import matplotlib.pyplot as plt import numpy as np -import treecorr +from IPython import get_ipython + from sp_validation.cosmo_val import CosmologyValidation from sp_validation.statistics import chi2_and_pte -import scipy.stats as stats -import camb - -from IPython import get_ipython ipython = get_ipython() if ipython is not None: @@ -25,17 +23,17 @@ versions=["SP_v1.4.6.3_A", "SP_v1.4.6.3_A_leak_corr"], npatch=100, nrandom_cell=25, - cell_method="catalog" + cell_method="catalog", ) # %% -cv.plot_pseudo_cl() #Loads c_ells in cv object +cv.plot_pseudo_cl() # Loads c_ells in cv object # %% header = ( - "| Root | $\chi^2$ (EB) | $\chi^2$ (EB) / dof | p-val (EB)| $\chi^2$ (BB) | $\chi^2$ (BB) / dof | p-val (BB) |\n" - "|------|----------------|------------|---------------|------------|------------------|--------------|\n" - ) + "| Root | $\\chi^2$ (EB) | $\\chi^2$ (EB) / dof | p-val (EB)| $\\chi^2$ (BB) | $\\chi^2$ (BB) / dof | p-val (BB) |\n" + "|------|----------------|------------|---------------|------------|------------------|--------------|\n" +) rows = [] @@ -59,31 +57,38 @@ print(header + "\n".join(rows)) # %% -redshift_distr = np.loadtxt('/n17data/mkilbing/astro/data/CFIS/v1.0/nz/dndz_SP_A.txt') +redshift_distr = np.loadtxt("/n17data/mkilbing/astro/data/CFIS/v1.0/nz/dndz_SP_A.txt") z, dndz = redshift_distr[:, 0], redshift_distr[:, 1] # %% -#Compute theory Cl'set +# Compute theory Cl'set h = 0.7 Oc = 0.25 Ob = 0.05 -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, - NonLinear=camb.model.NonLinear_both, WantTransfer=True) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + NonLinear=camb.model.NonLinear_both, + WantTransfer=True, +) nside = 1024 -lmax = 2*nside +lmax = 2 * nside -#getthe expected cl's from CAMB +# getthe expected cl's from CAMB pars.min_l = 1 pars.set_for_lmax(lmax) pars.SourceWindows = [ - camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type='lensing') + camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type="lensing") ] theory_cls = camb.get_results(pars).get_source_cls_dict(lmax=lmax, raw_cl=True) # %% -best_fit_axel = np.load('/n17data/guinot/CFIS_3500/CFIS_1.4.5/inference/plots_nautilus_SP_v1_full_model_no_B_IA_opti_scale/best_model_cell_nautilus_SP_v1_full_model_no_B_IA_opti_scale.npy') +best_fit_axel = np.load( + "/n17data/guinot/CFIS_3500/CFIS_1.4.5/inference/plots_nautilus_SP_v1_full_model_no_B_IA_opti_scale/best_model_cell_nautilus_SP_v1_full_model_no_B_IA_opti_scale.npy" +) # %% ell, cl_best_fit = best_fit_axel[0], best_fit_axel[1] @@ -91,39 +96,56 @@ # %% import healpy as hp import matplotlib.scale as mscale + from sp_validation.rho_tau import SquareRootScale mscale.register_scale(SquareRootScale) nside = 1024 -lmax = 2*nside -l = np.arange(lmax+1) +lmax = 2 * nside +ell_grid = np.arange(lmax + 1) pw = hp.pixwin(nside, lmax=lmax) -pseudo_cl_glass = cv._pseudo_cls['SP_v1.4.5.A']['pseudo_cl'] -cov_cl_glass = cv._pseudo_cls['SP_v1.4.5.A']['cov'] +pseudo_cl_glass = cv._pseudo_cls["SP_v1.4.5.A"]["pseudo_cl"] +cov_cl_glass = cv._pseudo_cls["SP_v1.4.5.A"]["cov"] -ell_eff = pseudo_cl_glass['ELL'] +ell_eff = pseudo_cl_glass["ELL"] fig, ax = plt.subplots(figsize=(8, 8)) -ax.errorbar(ell_eff, ell_eff*pseudo_cl_glass['EE'], yerr=ell_eff*np.sqrt(np.diag(cov_cl_glass["COVAR_EE_EE"].data)), fmt='o', capsize=2, markersize=4, label='EE') -ax.errorbar(ell_eff, ell_eff*pseudo_cl_glass['BB'], yerr=ell_eff*np.sqrt(np.diag(cov_cl_glass["COVAR_BB_BB"].data)), fmt='o', capsize=2, markersize=4, label='BB') -#ax.plot(l, l*theory_cls['W1xW1'], label=r'$C_\ell$ theory', c='k', ls='--') -ax.plot(ell, ell*cl_best_fit, label=r'$C_\ell$ best fit', c='k', ls='--') +ax.errorbar( + ell_eff, + ell_eff * pseudo_cl_glass["EE"], + yerr=ell_eff * np.sqrt(np.diag(cov_cl_glass["COVAR_EE_EE"].data)), + fmt="o", + capsize=2, + markersize=4, + label="EE", +) +ax.errorbar( + ell_eff, + ell_eff * pseudo_cl_glass["BB"], + yerr=ell_eff * np.sqrt(np.diag(cov_cl_glass["COVAR_BB_BB"].data)), + fmt="o", + capsize=2, + markersize=4, + label="BB", +) +# ax.plot(ell_grid, ell_grid*theory_cls['W1xW1'], label=r'$C_\ell$ theory', c='k', ls='--') +ax.plot(ell, ell * cl_best_fit, label=r"$C_\ell$ best fit", c="k", ls="--") -ax.set_xlabel(r'$\ell$') -ax.set_ylabel(r'$\ell C_\ell$') +ax.set_xlabel(r"$\ell$") +ax.set_ylabel(r"$\ell C_\ell$") -ax.set_xlim(ell_eff.min()-10, ell_eff.max()+100) -ax.set_xscale('squareroot') +ax.set_xlim(ell_eff.min() - 10, ell_eff.max() + 100) +ax.set_xscale("squareroot") ax.set_xticks(np.array([100, 400, 900, 1600])) ax.minorticks_on() -ax.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax.xaxis.set_ticks(minor_ticks, minor=True) -ax.set_yscale('log') +ax.set_yscale("log") plt.legend() plt.show() @@ -132,7 +154,7 @@ # ## SP_v1.4.6.3 # %% -#PTE EB SP_v1.4.6.3 +# PTE EB SP_v1.4.6.3 data_vector = cv._pseudo_cls["SP_v1.4.6.3_A"]["pseudo_cl"]["BB"][:] cov = cv._pseudo_cls["SP_v1.4.6.3_A"]["cov"]["COVAR_BB_BB"].data[:, :] @@ -143,28 +165,35 @@ chi2_and_pte(data_vector, cov, verbose=True) # %% -#PTE BB SP_v1.4.6.3 +# PTE BB SP_v1.4.6.3 from astropy.io import fits + cosmo_val_outdir = "/home/guerrini/sp_validation/cosmo_val/output/" -data_vector = fits.getdata(cosmo_val_outdir+"/pseudo_cl_SP_v1.4.6.3_B_leak_corr.fits")["BB"][12:-3] -cov = fits.open(cosmo_val_outdir+"/pseudo_cl_cov_SP_v1.4.6.3_B_leak_corr.fits")["COVAR_BB_BB"].data[12:-3, 12:-3] +data_vector = fits.getdata( + cosmo_val_outdir + "/pseudo_cl_SP_v1.4.6.3_B_leak_corr.fits" +)["BB"][12:-3] +cov = fits.open(cosmo_val_outdir + "/pseudo_cl_cov_SP_v1.4.6.3_B_leak_corr.fits")[ + "COVAR_BB_BB" +].data[12:-3, 12:-3] # %% -path_gaussian_sims = "/home/guerrini/sp_validation/cosmo_val/harmonic_covariance_gaussian_sims_v1463_B/" +path_gaussian_sims = ( + "/home/guerrini/sp_validation/cosmo_val/harmonic_covariance_gaussian_sims_v1463_B/" +) for i in range(10_000): - sim_cl = np.load( - path_gaussian_sims + f"sample_{i}.npz" - ) + sim_cl = np.load(path_gaussian_sims + f"sample_{i}.npz") if i == 0: - sim_dv = sim_cl['cl_all'][1] + sim_dv = sim_cl["cl_all"][1] else: - sim_dv = np.vstack((sim_dv, sim_cl['cl_all'][1])) + sim_dv = np.vstack((sim_dv, sim_cl["cl_all"][1])) cov_gaussian = np.cov(sim_dv.T) # %% -data_vector_gaussian = fits.getdata(cosmo_val_outdir+"/pseudo_cl_SP_v1.4.6.3_A_leak_corr.fits")["EB"] +data_vector_gaussian = fits.getdata( + cosmo_val_outdir + "/pseudo_cl_SP_v1.4.6.3_A_leak_corr.fits" +)["EB"] chi2_and_pte(data_vector_gaussian[12:-4], cov_gaussian[12:-4, 12:-4], verbose=True) # %% @@ -177,7 +206,7 @@ # ## SP_v1.4.5_leak_corr # %% -#PTE EB SP_v1.4.5 +# PTE EB SP_v1.4.5 data_vector = cv._pseudo_cls["SP_v1.4.5_intermediate"]["pseudo_cl"]["EB"] cov = cv._pseudo_cls["SP_v1.4.5_intermediate"]["cov"]["COVAR_EB_EB"].data @@ -196,7 +225,7 @@ # ## SP_v1.4.1 # %% -#PTE EB SP_v1.4.5 +# PTE EB SP_v1.4.5 data_vector = cv._pseudo_cls["SP_v1.4.5_bright"]["pseudo_cl"]["EB"] cov = cv._pseudo_cls["SP_v1.4.5_bright"]["cov"]["COVAR_EB_EB"].data @@ -204,7 +233,7 @@ chi2_and_pte(data_vector, cov, verbose=True) # %% -#PTE BB SP_v1.4.5 +# PTE BB SP_v1.4.5 data_vector = cv._pseudo_cls["SP_v1.4.5_bright"]["pseudo_cl"]["BB"][:] cov = cv._pseudo_cls["SP_v1.4.5_bright"]["cov"]["COVAR_BB_BB"].data[:, :] @@ -223,22 +252,36 @@ cov_145_sim = np.load("/home/guerrini/random_stuff/cov_cl_1.4.5.npy") corr_141 = np.array( - [[1/(cov_141[i,i]*cov_141[j,j]) for i in range(32)] for j in range(32)] + [[1 / (cov_141[i, i] * cov_141[j, j]) for i in range(32)] for j in range(32)] ) -corr_141 = cov_141*np.sqrt(corr_141) +corr_141 = cov_141 * np.sqrt(corr_141) corr_145 = np.array( - [[1/(cov_145[i,i]*cov_145[j,j]) for i in range(32)] for j in range(32)] + [[1 / (cov_145[i, i] * cov_145[j, j]) for i in range(32)] for j in range(32)] ) -corr_145 = cov_145*np.sqrt(corr_145) +corr_145 = cov_145 * np.sqrt(corr_145) # %% ell = cv._pseudo_cls["SP_v1.4.1"]["pseudo_cl"]["ELL"] plt.figure() -plt.errorbar(ell, ell*data_vector, yerr=ell*np.sqrt(np.diag(cov_141)), fmt='o', label='SP_v1.4.1', capsize=2) -plt.errorbar(ell, ell*data_vector_sim[3], yerr=ell*np.sqrt(np.diag(cov_141_sim[3])), fmt='o', label='SP_v1.4.1_sim', capsize=2) +plt.errorbar( + ell, + ell * data_vector, + yerr=ell * np.sqrt(np.diag(cov_141)), + fmt="o", + label="SP_v1.4.1", + capsize=2, +) +plt.errorbar( + ell, + ell * data_vector_sim[3], + yerr=ell * np.sqrt(np.diag(cov_141_sim[3])), + fmt="o", + label="SP_v1.4.1_sim", + capsize=2, +) plt.show() @@ -251,12 +294,12 @@ plt.subplot(121) -plt.imshow(corr_141, cmap='seismic') +plt.imshow(corr_141, cmap="seismic") plt.colorbar() plt.subplot(122) -plt.imshow(corr_145, cmap='seismic') +plt.imshow(corr_145, cmap="seismic") plt.colorbar() plt.tight_layout() @@ -266,9 +309,9 @@ plt.figure() plt.plot(np.diag(cov_141), label="SP_v1.4.1") -#plt.plot(np.diag(cov_145), label="SP_v1.4.5") +# plt.plot(np.diag(cov_145), label="SP_v1.4.5") plt.plot(np.diag(cov_141_sim[3]), label="SP_v1.4.1 sim") -#plt.plot(np.diag(cov_145_sim[3]), label="SP_v1.4.5 sim") +# plt.plot(np.diag(cov_145_sim[3]), label="SP_v1.4.5 sim") plt.legend() plt.xlabel("ell") @@ -284,7 +327,7 @@ # %% n_sims = 8000 n_dv = 32 -hartlap_factor = (n_sims-n_dv-2)/(n_sims-1) +hartlap_factor = (n_sims - n_dv - 2) / (n_sims - 1) print(hartlap_factor) # %% diff --git a/scratch/guerrini/exploration.py b/scratch/guerrini/exploration.py index 719b5c93..3e27d50c 100644 --- a/scratch/guerrini/exploration.py +++ b/scratch/guerrini/exploration.py @@ -22,32 +22,24 @@ ipython.run_line_magic("autoreload", "2") import os +import healpy as hp +import matplotlib.pyplot as plt +import namaster_utils as utils import numpy as np +import pymaster as nmt +import seaborn as sns +import skymapper as skm from astropy.io import fits -from astropy.cosmology import Planck18 -import matplotlib.pyplot as plt -from matplotlib.gridspec import GridSpec from matplotlib import scale as mscale -import seaborn as sns +from matplotlib.gridspec import GridSpec from tqdm import tqdm -import healpy as hp -import pymaster as nmt -import camb -import skymapper as skm from sp_validation.rho_tau import SquareRootScale -from sp_validation.cosmo_val import CosmologyValidation -from sp_validation.cosmology import get_theo_c_ell, get_cosmo -from sp_validation.rho_tau import get_params_rho_tau - -import namaster_utils as utils mscale.register_scale(SquareRootScale) -plt.style.use( - '/home/guerrini/matplotlib_config/paper.mplstyle' -) -plt.rcParams['text.usetex'] = True +plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") +plt.rcParams["text.usetex"] = True sns.set_palette("Dark2") @@ -58,27 +50,31 @@ # # Investigation of NaMaster covariance # %% -#Utils paths -path_cat_gal = '/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits' -path_redshift_distr = '/n17data/sguerrini/UNIONS/WL/nz/v1.4.6.3/nz_SP_v1.4.6.3_B.txt' +# Utils paths +path_cat_gal = ( + "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" +) +path_redshift_distr = "/n17data/sguerrini/UNIONS/WL/nz/v1.4.6.3/nz_SP_v1.4.6.3_B.txt" root_glass = "/n09data/guerrini/glass_mock_v1.4.6.3_v2/results/" -path_cl_measurement = '/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.6.3_leak_corr_B.fits' -path_cat_des = '/n17data/mkilbing/astro/data/DES/DES_Y3_cut.fits' -path_cat_glass = '/n09data/guerrini/glass_mock_v1.4.6.3_v2/results/unions_glass_sim_00001_4096.fits' +path_cl_measurement = "/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_SP_v1.4.6.3_leak_corr_B.fits" +path_cat_des = "/n17data/mkilbing/astro/data/DES/DES_Y3_cut.fits" +path_cat_glass = ( + "/n09data/guerrini/glass_mock_v1.4.6.3_v2/results/unions_glass_sim_00001_4096.fits" +) # Load onecovariance estimate pseudo_cl_cov = fits.open( - '/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_g_ng_OneCovariance_SP_v1.4.6.3_leak_corr_B.fits' + "/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_g_ng_OneCovariance_SP_v1.4.6.3_leak_corr_B.fits" ) # Load namaster estimate from cosmo_val pseudo_cl_cov_namaster = fits.open( - '/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.6.3_leak_corr_B.fits' + "/home/guerrini/sp_validation/cosmo_val/output/pseudo_cl_cov_SP_v1.4.6.3_leak_corr_B.fits" ) nside = 1024 lmin = 8 lmax = 2048 -ell_coupled = np.arange(1, lmax+1) +ell_coupled = np.arange(1, lmax + 1) num_bins = 32 b = utils.get_binning(lmin, lmax, num_bins) ell_eff = b.get_effective_ells() @@ -90,9 +86,7 @@ ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) e1_rot, e2_rot = utils.apply_random_rotations(e1, e2) -f_cat, wsp_cat = utils.get_field_and_workspace_cat( - ra, dec, e1, e2, lmax - 1, b, w -) +f_cat, wsp_cat = utils.get_field_and_workspace_cat(ra, dec, e1, e2, lmax - 1, b, w) # %% cl_noise_coupled = nmt.compute_coupled_cell(f_cat, f_cat) @@ -100,12 +94,12 @@ # %% coupling_cat = wsp_cat.get_coupling_matrix() -coupling_cat_re = np.reshape(coupling_cat, (lmax, 4, lmax, 4), order='F') +coupling_cat_re = np.reshape(coupling_cat, (lmax, 4, lmax, 4), order="F") # %% plt.figure() -plt.matshow(coupling_cat_re[:, 0, :, 0], cmap='seismic') +plt.matshow(coupling_cat_re[:, 0, :, 0], cmap="seismic") plt.colorbar() plt.show() @@ -115,17 +109,17 @@ plt.plot( ell_coupled, - ell_coupled*cl_noise_coupled[0], - label='E-modes', + ell_coupled * cl_noise_coupled[0], + label="E-modes", ) plt.plot( ell_coupled, - ell_coupled*cl_noise_coupled[3], - label='B-modes', + ell_coupled * cl_noise_coupled[3], + label="B-modes", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") @@ -138,17 +132,17 @@ plt.plot( ell_eff, - ell_eff*cl_noise[0], - label='E-modes', + ell_eff * cl_noise[0], + label="E-modes", ) plt.plot( ell_eff, - ell_eff*cl_noise[3], - label='B-modes', + ell_eff * cl_noise[3], + label="B-modes", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") @@ -158,66 +152,42 @@ # %% cov_cat, cl_cat = utils.get_covariance_from_glass( - root=root_glass, - n_sim=350, - num_bins=num_bins, - lmax=lmax, - type='cat' + root=root_glass, n_sim=350, num_bins=num_bins, lmax=lmax, type="cat" ) cov_cat_coupled, cl_cat_coupled = utils.get_covariance_from_glass( - root=root_glass, - n_sim=350, - num_bins=num_bins, - lmax=lmax, - type='cat_coupled' + root=root_glass, n_sim=350, num_bins=num_bins, lmax=lmax, type="cat_coupled" ) cov_map, cl_map = utils.get_covariance_from_glass( - root=root_glass, - n_sim=350, - num_bins=num_bins, - lmax=lmax, - type='map' + root=root_glass, n_sim=350, num_bins=num_bins, lmax=lmax, type="map" ) cov_map_coupled, cl_map_coupled = utils.get_covariance_from_glass( - root=root_glass, - n_sim=350, - num_bins=num_bins, - lmax=lmax, - type='map_coupled' + root=root_glass, n_sim=350, num_bins=num_bins, lmax=lmax, type="map_coupled" ) # %% plt.figure() -plt.plot( - ell_eff, - np.sqrt(np.diag(cov_cat)), - label='Catalog-based' -) +plt.plot(ell_eff, np.sqrt(np.diag(cov_cat)), label="Catalog-based") -plt.plot( - ell_eff, - np.sqrt(np.diag(cov_map)), - label='Map-based' -) +plt.plot(ell_eff, np.sqrt(np.diag(cov_map)), label="Map-based") plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), - label='OneCovariance', - color='black', - linestyle='--' + label="OneCovariance", + color="black", + linestyle="--", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() plt.savefig("./plots/errorbar_glass_mock_cat_vs_map.png", dpi=300) @@ -226,24 +196,16 @@ # %% plt.figure() -plt.plot( - ell_coupled, - np.sqrt(np.diag(cov_cat_coupled)), - label='Catalog-based' -) +plt.plot(ell_coupled, np.sqrt(np.diag(cov_cat_coupled)), label="Catalog-based") -plt.plot( - ell_coupled, - np.sqrt(np.diag(cov_map_coupled)), - label='Map-based' -) +plt.plot(ell_coupled, np.sqrt(np.diag(cov_map_coupled)), label="Map-based") -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() plt.savefig("./plots/errorbar_glass_mock_cat_vs_map_coupled.png", dpi=300) @@ -253,24 +215,16 @@ # %% plt.figure() -plt.plot( - ell_eff, - np.sqrt(np.diag(cov_map)), - label='Map-Deconvolved' -) +plt.plot(ell_eff, np.sqrt(np.diag(cov_map)), label="Map-Deconvolved") -plt.plot( - ell_coupled, - np.sqrt(np.diag(cov_map_coupled)), - label='Map-Coupled' -) +plt.plot(ell_coupled, np.sqrt(np.diag(cov_map_coupled)), label="Map-Coupled") -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() @@ -282,7 +236,7 @@ # ### Computation of the noise bias # %% -#Get samples from the noise bias +# Get samples from the noise bias if os.path.exists("./data/cl_noise_bias.npy"): cl_noise_bias = np.load("./data/cl_noise_bias.npy") else: @@ -291,7 +245,6 @@ n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 - for i in tqdm(range(50)): noise_map = utils.get_gaussian_real( nside=nside, @@ -301,15 +254,11 @@ idx_rep=idx_rep, e1=e1, e2=e2, - w=w + w=w, ) if i == 0: - _, wsp = utils.get_field_and_workspace_map( - mask, - lmax - 1, - b - ) + _, wsp = utils.get_field_and_workspace_map(mask, lmax - 1, b) cl_noise, _, _, _ = utils.get_sample(noise_map, mask, lmax - 1, b, wsp=wsp) cl_noise_bias = np.vstack((cl_noise_bias, cl_noise[np.newaxis, :, :])) @@ -319,21 +268,19 @@ noise_bias = np.mean(cl_noise_bias, axis=0) # %% -#Theoretical estimate +# Theoretical estimate ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 variance_map = np.zeros(hp.nside2npix(nside)) -variance_map[unique_pix] = np.bincount(idx_rep, weights=(e1**2 + e2**2)/2 * w**2) +variance_map[unique_pix] = np.bincount(idx_rep, weights=(e1**2 + e2**2) / 2 * w**2) noise_bias_th = hp.nside2pixarea(nside) * np.mean(variance_map) print(noise_bias_th) -_, wsp = utils.get_field_and_workspace_map( - mask, lmax - 1, b -) +_, wsp = utils.get_field_and_workspace_map(mask, lmax - 1, b) cl_in = np.zeros((4, lmax)) cl_in[0, :] = noise_bias_th @@ -346,39 +293,39 @@ for i in range(10): plt.plot(ell_eff, cl_noise_bias[i, 0], alpha=0.3) -plt.plot(ell_eff, noise_bias_decoupled[0], color='black', label="Theory estimation") +plt.plot(ell_eff, noise_bias_decoupled[0], color="black", label="Theory estimation") plt.show() # %% -#Plot catalog vs map +# Plot catalog vs map plt.figure() plt.plot( ell_eff, cl_cat[0], - label='Catalog-based', + label="Catalog-based", ) plt.plot( ell_eff, cl_map[0], - label='Map-based', + label="Map-based", ) plt.plot( ell_eff, noise_bias[0], - label='Noise bias', + label="Noise bias", ) plt.plot( ell_eff, cl_map[0] - noise_bias[0], - label='Map-based - Noise bias', + label="Map-based - Noise bias", ) plt.legend() -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") @@ -390,7 +337,7 @@ # ### Estimating the noise bias on DES # %% -#Theoretical estimate +# Theoretical estimate ra, dec, e1, e2, w = utils.load_cat_des(path_cat_des) n_gal_des, unique_pix_des, idx_des, idx_rep_des = utils.get_n_gal_map(nside, ra, dec, w) @@ -398,14 +345,14 @@ variance_map = np.zeros(hp.nside2npix(nside)) _w = w * float(len(w)) / np.sum(w) -variance_map[unique_pix_des] = np.bincount(idx_rep_des, weights=(e1**2 + e2**2)/2 * _w**2) +variance_map[unique_pix_des] = np.bincount( + idx_rep_des, weights=(e1**2 + e2**2) / 2 * _w**2 +) noise_bias_th = hp.nside2pixarea(nside) * np.mean(variance_map) print(noise_bias_th) -_, wsp = utils.get_field_and_workspace_map( - mask_des, lmax - 1, b -) +_, wsp = utils.get_field_and_workspace_map(mask_des, lmax - 1, b) cl_in = np.zeros((4, lmax)) cl_in[0, :] = noise_bias_th @@ -422,20 +369,15 @@ fiducial = utils.get_theory(lmax, z, dndz) -#Get field, wsp and cw +# Get field, wsp and cw n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 -f, wsp = utils.get_field_and_workspace_map( - mask, lmax - 1, b -) +f, wsp = utils.get_field_and_workspace_map(mask, lmax - 1, b) cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) # %% -#Get the noise bias -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias.npy"), - axis=0 -) +# Get the noise bias +noise_bias_ = np.mean(np.load("./data/cl_noise_bias.npy"), axis=0) noise_bias = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] @@ -443,38 +385,29 @@ noise_bias[i, :lowest_ell] = noise_bias[i, lowest_ell] # %% -fiducial_noise = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias - -cov_theo = utils.get_covariance( - fiducial_noise, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +fiducial_noise = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + noise_bias +) + +cov_theo = utils.get_covariance(fiducial_noise, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) # %% -#Apply coupling matrix to the fiducial theory +# Apply coupling matrix to the fiducial theory ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 coupling = wsp.get_coupling_matrix() -coupling_re = np.reshape(coupling, (4, lmax, 4, lmax), order='F') +coupling_re = np.reshape(coupling, (4, lmax, 4, lmax), order="F") coupled_fiducial = np.tensordot(coupling_re, fiducial_noise) / np.mean(mask**2) -cov_theo_coupled = utils.get_covariance( - coupled_fiducial, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +cov_theo_coupled = utils.get_covariance(coupled_fiducial, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) # %% @@ -501,70 +434,58 @@ cl_data_decoupled[i, :lowest_ell] = cl_data_decoupled[i, lowest_ell] -cov_data = utils.get_covariance( - cl_data / np.mean(mask**2), - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +cov_data = utils.get_covariance(cl_data / np.mean(mask**2), cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) -cov_data_decoupled = utils.get_covariance( - cl_data_decoupled, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +cov_data_decoupled = utils.get_covariance(cl_data_decoupled, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) # %% plt.figure() -plt.plot( - ell_eff, - np.sqrt(np.diag(cov_data[:, 0, :, 0])), - label='Data-based coupled' -) +plt.plot(ell_eff, np.sqrt(np.diag(cov_data[:, 0, :, 0])), label="Data-based coupled") plt.plot( - ell_eff, - np.sqrt(np.diag(cov_theo[:, 0, :, 0])), - label='Theory-based deconvolved' + ell_eff, np.sqrt(np.diag(cov_theo[:, 0, :, 0])), label="Theory-based deconvolved" ) plt.plot( ell_eff, np.sqrt(np.diag(cov_theo_coupled[:, 0, :, 0])), - label='Theory-based coupled' + label="Theory-based coupled", ) plt.plot( ell_eff, np.sqrt(np.diag(cov_data_decoupled[:, 0, :, 0])), - label='Data-based deconvolved' + label="Data-based deconvolved", ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), - color='black', - linestyle='--', - label='OneCovariance' + color="black", + linestyle="--", + label="OneCovariance", ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)), - color='gray', - label='NaMaster built-in covariance', - linestyle='--' + color="gray", + label="NaMaster built-in covariance", + linestyle="--", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() @@ -580,7 +501,7 @@ # %% ra, dec, e1, e2, w = utils.load_cat(path_cat_glass, w_col=None) -#Get samples from the noise bias +# Get samples from the noise bias if os.path.exists("./data/cl_noise_bias_glass.npy"): cl_noise_bias = np.load("./data/cl_noise_bias_glass.npy") else: @@ -589,7 +510,6 @@ n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 - for i in tqdm(range(50)): noise_map = utils.get_gaussian_real( nside=nside, @@ -599,80 +519,63 @@ idx_rep=idx_rep, e1=e1, e2=e2, - w=w + w=w, ) if i == 0: - _, wsp = utils.get_field_and_workspace_map( - mask, - lmax - 1, - b - ) + _, wsp = utils.get_field_and_workspace_map(mask, lmax - 1, b) cl_noise, _, _, _ = utils.get_sample(noise_map, mask, lmax - 1, b, wsp=wsp) cl_noise_bias = np.vstack((cl_noise_bias, cl_noise[np.newaxis, :, :])) np.save("./data/cl_noise_bias_glass.npy", cl_noise_bias) # %% -#Run the comparison +# Run the comparison # Fiducial theory ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) z, dndz = utils.load_redshift_distr(path_redshift_distr) fiducial = utils.get_theory(lmax, z, dndz) -#Get field, wsp and cw +# Get field, wsp and cw n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 -f, wsp = utils.get_field_and_workspace_map( - mask, lmax - 1, b -) +f, wsp = utils.get_field_and_workspace_map(mask, lmax - 1, b) cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) # %% # Using noise bias from UNIONS catalog -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias.npy"), axis=0) noise_bias = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] for i in range(4): noise_bias[i, :lowest_ell] = noise_bias[i, lowest_ell] -fiducial_noise = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias +fiducial_noise = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + noise_bias +) # Using noise bias from GLASS mock -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias_glass.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias_glass.npy"), axis=0) noise_bias_glass = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] for i in range(4): noise_bias_glass[i, :lowest_ell] = noise_bias_glass[i, lowest_ell] -fiducial_noise_glass = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias_glass +fiducial_noise_glass = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + + noise_bias_glass +) # %% # Estimate the noise bias from theory sigma_e = 0.3796 / np.sqrt(2) -n_eff = 6.128 #arcmin^-2 +n_eff = 6.128 # arcmin^-2 n_eff = n_eff * 60**2 # convert to deg-2 -n_eff = n_eff * (180/np.pi)**2 # convert to sr^-1 +n_eff = n_eff * (180 / np.pi) ** 2 # convert to sr^-1 n_l = sigma_e**2 / (2 * n_eff) print("Noise bias: ", n_l) @@ -688,25 +591,23 @@ noise_bias_theo[i, :lowest_ell] = noise_bias_theo[i, lowest_ell] -fiducial_noise_theo = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias_theo +fiducial_noise_theo = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + + noise_bias_theo +) # %% -#Compare the noise biases +# Compare the noise biases plt.figure() -plt.plot(ell_coupled, noise_bias[0], label='UNIONS catalog-based noise bias') -plt.plot(ell_coupled, noise_bias_glass[0], label='GLASS mock-based noise bias') -plt.plot(ell_coupled, noise_bias_theo[0], label='Theoretical noise bias') +plt.plot(ell_coupled, noise_bias[0], label="UNIONS catalog-based noise bias") +plt.plot(ell_coupled, noise_bias_glass[0], label="GLASS mock-based noise bias") +plt.plot(ell_coupled, noise_bias_theo[0], label="Theoretical noise bias") plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell^{\mathrm{noise}}$") -plt.xscale('squareroot') -plt.yscale('log') +plt.xscale("squareroot") +plt.yscale("log") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.legend() @@ -716,26 +617,17 @@ # %% # Compute the covariance -cov_unions = utils.get_covariance( - fiducial_noise, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +cov_unions = utils.get_covariance(fiducial_noise, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) -cov_glass = utils.get_covariance( - fiducial_noise_glass, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +cov_glass = utils.get_covariance(fiducial_noise_glass, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) -cov_theo = utils.get_covariance( - fiducial_noise_theo, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +cov_theo = utils.get_covariance(fiducial_noise_theo, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) # %% plt.figure() @@ -743,43 +635,41 @@ plt.plot( ell_eff, np.sqrt(np.diag(cov_unions[:, 0, :, 0])), - label='UNIONS catalog-based noise bias' + label="UNIONS catalog-based noise bias", ) plt.plot( ell_eff, np.sqrt(np.diag(cov_glass[:, 0, :, 0])), - label='GLASS mock-based noise bias' + label="GLASS mock-based noise bias", ) plt.plot( - ell_eff, - np.sqrt(np.diag(cov_theo[:, 0, :, 0])), - label='Theoretical noise bias' + ell_eff, np.sqrt(np.diag(cov_theo[:, 0, :, 0])), label="Theoretical noise bias" ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), - color='black', - linestyle='--', - label='OneCovariance' + color="black", + linestyle="--", + label="OneCovariance", ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)), - color='gray', - label='NaMaster built-in covariance', - linestyle='--' + color="gray", + label="NaMaster built-in covariance", + linestyle="--", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() @@ -796,7 +686,7 @@ n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 variance_map = np.zeros(hp.nside2npix(nside)) -variance_map[unique_pix] = np.bincount(idx_rep, weights=(e1**2 + e2**2)/2 * w**2) +variance_map[unique_pix] = np.bincount(idx_rep, weights=(e1**2 + e2**2) / 2 * w**2) noise_bias_th = hp.nside2pixarea(nside) * np.mean(variance_map) print(noise_bias_th) @@ -817,7 +707,9 @@ # %% ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) -ra_glass, dec_glass, e1_glass, e2_glass, w_glass = utils.load_cat(path_cat_glass, w_col=None) +ra_glass, dec_glass, e1_glass, e2_glass, w_glass = utils.load_cat( + path_cat_glass, w_col=None +) print("coucou") @@ -869,7 +761,7 @@ cbar = fig.colorbar( mappable, cax=cbar_ax, - orientation='horizontal', + orientation="horizontal", fraction=0.05, pad=0.1, ) @@ -878,7 +770,7 @@ plt.tight_layout() plt.savefig("./plots/number_density_map_unions_vs_glass.png", dpi=300) -#Save to pdf format as well +# Save to pdf format as well plt.savefig("./plots/number_density_map_unions_vs_glass.pdf") plt.show() @@ -897,7 +789,7 @@ # make density plot nside = 1024 mappable = map.healpix(np.ma.masked_invalid(variance_map_plot), vmin=0, vmax=3000) -cb = map.colorbar(mappable, cb_label="$\sigma_e^2$", loc="bottom") +cb = map.colorbar(mappable, cb_label=r"$\sigma_e^2$", loc="bottom") map.focus(ra, dec) @@ -907,7 +799,9 @@ shear_map_e1 = np.zeros(hp.nside2npix(nside_map)) shear_map_e2 = np.zeros(hp.nside2npix(nside_map)) -n_gal_map, unique_pix_map, idx_map, idx_rep_map = utils.get_n_gal_map(nside_map, ra, dec, w) +n_gal_map, unique_pix_map, idx_map, idx_rep_map = utils.get_n_gal_map( + nside_map, ra, dec, w +) mask_map = n_gal_map > 0 shear_map_e1[unique_pix_map] = np.bincount(idx_rep_map, weights=e1 * w) @@ -932,26 +826,22 @@ # 4) add data to the map, e.g. # make density plot nside = 1024 -mappable = map.healpix(shear_map_e1, cmap='seismic') -cb = map.colorbar(mappable, cb_label="$\gamma_1$", loc="bottom") +mappable = map.healpix(shear_map_e1, cmap="seismic") +cb = map.colorbar(mappable, cb_label=r"$\gamma_1$", loc="bottom") map.focus(ra, dec) # %% shear_map_e1[~mask_map] = 0.0 smoothed_shear_map_e1 = hp.smoothing( - shear_map_e1, - fwhm=np.radians(10/60), - verbose=False + shear_map_e1, fwhm=np.radians(10 / 60), verbose=False ) smoothed_mask = hp.smoothing( - mask_map.astype(float), - fwhm=np.radians(10/60), - verbose=False + mask_map.astype(float), fwhm=np.radians(10 / 60), verbose=False +) +smoothed_shear_map_e1 = np.where( + smoothed_mask > 1e-6, smoothed_shear_map_e1 / smoothed_mask, np.nan ) -smoothed_shear_map_e1 = np.where(smoothed_mask > 1e-6, - smoothed_shear_map_e1 / smoothed_mask, - np.nan) # %% # 2) construct map: will hold figure and projection @@ -967,15 +857,17 @@ # 4) add data to the map, e.g. # make density plot nside = 1024 -mappable = map.healpix(smoothed_shear_map_e1, cmap='seismic', vmin=-0.05, vmax=0.05) -cb = map.colorbar(mappable, cb_label="$\gamma_1$", loc="bottom") +mappable = map.healpix(smoothed_shear_map_e1, cmap="seismic", vmin=-0.05, vmax=0.05) +cb = map.colorbar(mappable, cb_label=r"$\gamma_1$", loc="bottom") map.focus(ra, dec) # %% plt.figure() -plt.hist(smoothed_shear_map_e1[~np.isnan(smoothed_shear_map_e1)], bins=100, density=True) +plt.hist( + smoothed_shear_map_e1[~np.isnan(smoothed_shear_map_e1)], bins=100, density=True +) plt.show() @@ -983,49 +875,38 @@ # ## 5. Using the weighted number of galaxies as the mask # %% -#Run the comparison +# Run the comparison # Fiducial theory ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) z, dndz = utils.load_redshift_distr(path_redshift_distr) fiducial = utils.get_theory(lmax, z, dndz) -#Get field, wsp and cw +# Get field, wsp and cw n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 -f, wsp = utils.get_field_and_workspace_map( - mask, lmax - 1, b -) +f, wsp = utils.get_field_and_workspace_map(mask, lmax - 1, b) cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) # Using noise bias from UNIONS catalog -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias.npy"), axis=0) noise_bias = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] for i in range(4): noise_bias[i, :lowest_ell] = noise_bias[i, lowest_ell] -fiducial_noise = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias - -cov_unions = utils.get_covariance( - fiducial_noise, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +fiducial_noise = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + noise_bias +) + +cov_unions = utils.get_covariance(fiducial_noise, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) # %% ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) -#Get samples from the noise bias +# Get samples from the noise bias if os.path.exists("./data/cl_noise_bias_n_gal.npy"): cl_noise_bias = np.load("./data/cl_noise_bias_n_gal.npy") else: @@ -1034,7 +915,6 @@ n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 - for i in tqdm(range(50)): noise_map = utils.get_gaussian_real( nside=nside, @@ -1044,90 +924,69 @@ idx_rep=idx_rep, e1=e1, e2=e2, - w=w + w=w, ) if i == 0: - _, wsp = utils.get_field_and_workspace_map( - n_gal, - lmax - 1, - b - ) + _, wsp = utils.get_field_and_workspace_map(n_gal, lmax - 1, b) cl_noise, _, _, _ = utils.get_sample(noise_map, n_gal, lmax - 1, b, wsp=wsp) cl_noise_bias = np.vstack((cl_noise_bias, cl_noise[np.newaxis, :, :])) np.save("./data/cl_noise_bias_n_gal.npy", cl_noise_bias) # %% -#Run the comparison +# Run the comparison # Fiducial theory ra, dec, e1, e2, w = utils.load_cat(path_cat_gal) z, dndz = utils.load_redshift_distr(path_redshift_distr) fiducial = utils.get_theory(lmax, z, dndz) -#Get field, wsp and cw +# Get field, wsp and cw n_gal, unique_pix, idx, idx_rep = utils.get_n_gal_map(nside, ra, dec, w) mask = n_gal > 0 -f, wsp = utils.get_field_and_workspace_map( - n_gal, lmax - 1, b -) +f, wsp = utils.get_field_and_workspace_map(n_gal, lmax - 1, b) cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) # Using noise bias from UNIONS catalog -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias_n_gal.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias_n_gal.npy"), axis=0) noise_bias = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] for i in range(4): noise_bias[i, :lowest_ell] = noise_bias[i, lowest_ell] -fiducial_noise = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias - -cov_unions_n_gal = utils.get_covariance( - fiducial_noise, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +fiducial_noise = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + noise_bias +) + +cov_unions_n_gal = utils.get_covariance(fiducial_noise, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) # %% plt.figure() -plt.plot( - ell_eff, - np.sqrt(np.diag(cov_unions[:, 0, :, 0])), - label='Using mask only' -) +plt.plot(ell_eff, np.sqrt(np.diag(cov_unions[:, 0, :, 0])), label="Using mask only") plt.plot( - ell_eff, - np.sqrt(np.diag(cov_unions_n_gal[:, 0, :, 0])), - label='Using n_gal map' + ell_eff, np.sqrt(np.diag(cov_unions_n_gal[:, 0, :, 0])), label="Using n_gal map" ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), - color='black', - linestyle='--', - label='OneCovariance' + color="black", + linestyle="--", + label="OneCovariance", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() plt.savefig("./plots/errorbar_n_gal_map_vs_mask_only.png", dpi=300) @@ -1141,9 +1000,7 @@ mask = n_gal > 0 variance_map = np.zeros(hp.nside2npix(nside)) -variance_map[unique_pix] = np.bincount( - idx_rep, weights=(e1**2 + e2**2)/2 * w**2 -) +variance_map[unique_pix] = np.bincount(idx_rep, weights=(e1**2 + e2**2) / 2 * w**2) noise_bias_th = hp.nside2pixarea(nside) * np.mean(variance_map) print(noise_bias_th) @@ -1152,9 +1009,7 @@ noise_bias_theo[0, :] = noise_bias_th noise_bias_theo[3, :] = noise_bias_th -_, wsp = utils.get_field_and_workspace_map( - n_gal, lmax - 1, b -) +_, wsp = utils.get_field_and_workspace_map(n_gal, lmax - 1, b) noise_bias_theo = wsp.decouple_cell(noise_bias_theo) noise_bias_theo = b.unbin_cell(noise_bias_theo) @@ -1165,13 +1020,15 @@ # Compute theorerical noise_bias for GLASS mock ra, dec, e1, e2, w = utils.load_cat(path_cat_glass, w_col=None) -n_gal_glass, unique_pix_glass, idx_glass, idx_rep_glass = utils.get_n_gal_map(nside, ra, dec, w) +n_gal_glass, unique_pix_glass, idx_glass, idx_rep_glass = utils.get_n_gal_map( + nside, ra, dec, w +) mask_glass = n_gal_glass > 0 variance_map_glass = np.zeros(hp.nside2npix(nside)) variance_map_glass[unique_pix_glass] = np.bincount( - idx_rep_glass, weights=(e1**2 + e2**2)/2 * w**2 + idx_rep_glass, weights=(e1**2 + e2**2) / 2 * w**2 ) noise_bias_th_glass = hp.nside2pixarea(nside) * np.mean(variance_map_glass) print(noise_bias_th_glass) @@ -1180,9 +1037,7 @@ noise_bias_theo_glass[0, :] = noise_bias_th_glass noise_bias_theo_glass[3, :] = noise_bias_th_glass -_, wsp_glass = utils.get_field_and_workspace_map( - n_gal_glass, lmax - 1, b -) +_, wsp_glass = utils.get_field_and_workspace_map(n_gal_glass, lmax - 1, b) noise_bias_theo_glass = wsp_glass.decouple_cell(noise_bias_theo_glass) noise_bias_theo_glass = b.unbin_cell(noise_bias_theo_glass) @@ -1191,10 +1046,7 @@ noise_bias_theo_glass[i, :lowest_ell] = noise_bias_theo_glass[i, lowest_ell] # Using noise bias from UNIONS catalog -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias.npy"), axis=0) noise_bias = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] @@ -1202,10 +1054,7 @@ noise_bias[i, :lowest_ell] = noise_bias[i, lowest_ell] # Using noise bias from GLASS mock -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias_glass.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias_glass.npy"), axis=0) noise_bias_glass = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] @@ -1215,23 +1064,17 @@ # %% plt.figure() +plt.plot(ell_coupled, noise_bias[0], label="UNIONS catalog-based noise bias mask") +plt.plot(ell_coupled, noise_bias_glass[0], label="GLASS mock-based noise bias mask") +plt.plot(ell_coupled, noise_bias_theo[0], label="Theoretical noise bias n_gal") plt.plot( - ell_coupled, noise_bias[0], label='UNIONS catalog-based noise bias mask' -) -plt.plot( - ell_coupled, noise_bias_glass[0], label='GLASS mock-based noise bias mask' -) -plt.plot( - ell_coupled, noise_bias_theo[0], label='Theoretical noise bias n_gal' -) -plt.plot( - ell_coupled, noise_bias_theo_glass[0], label='Theoretical noise bias GLASS n_gal' + ell_coupled, noise_bias_theo_glass[0], label="Theoretical noise bias GLASS n_gal" ) plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell^{\mathrm{noise}}$") -plt.xscale('squareroot') -plt.yscale('log') +plt.xscale("squareroot") +plt.yscale("log") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.legend() @@ -1241,10 +1084,7 @@ # %% # Using noise bias from UNIONS catalog -noise_bias_ = np.mean( - np.load("./data/cl_noise_bias_n_gal.npy"), - axis=0 -) +noise_bias_ = np.mean(np.load("./data/cl_noise_bias_n_gal.npy"), axis=0) noise_bias = b.unbin_cell(noise_bias_) lowest_ell = b.get_ell_list(0)[0] @@ -1253,22 +1093,20 @@ plt.figure() -plt.plot( - ell_coupled, noise_bias[0], label='UNIONS catalog-based noise bias n_gal' -) -plt.plot( - ell_coupled, noise_bias_theo[0], label='Theoretical noise bias n_gal' -) +plt.plot(ell_coupled, noise_bias[0], label="UNIONS catalog-based noise bias n_gal") +plt.plot(ell_coupled, noise_bias_theo[0], label="Theoretical noise bias n_gal") plt.xlabel(r"$\ell$") plt.ylabel(r"$C_\ell^{\mathrm{noise}}$") -plt.xscale('squareroot') -plt.yscale('log') +plt.xscale("squareroot") +plt.yscale("log") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.legend() -plt.savefig("./plots/noise_bias_comparison_unions_vs_glass_vs_theory_n_gal.png", dpi=300) +plt.savefig( + "./plots/noise_bias_comparison_unions_vs_glass_vs_theory_n_gal.png", dpi=300 +) plt.show() # %% [markdown] @@ -1281,75 +1119,57 @@ # %% ### Get covariances for UNIONS catalog -#Get field , wsp and cw -f, wsp = utils.get_field_and_workspace_map( - n_gal, lmax - 1, b -) +# Get field , wsp and cw +f, wsp = utils.get_field_and_workspace_map(n_gal, lmax - 1, b) cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) -#Get the fiducial noise -fiducial_noise = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias_theo - -#Get the covariance -cov_unions_theo = utils.get_covariance( - fiducial_noise, - cw, - wsp, - num_bins -).reshape((num_bins, 4, num_bins, 4)) +# Get the fiducial noise +fiducial_noise = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + + noise_bias_theo +) -#Couple the fiducial +# Get the covariance +cov_unions_theo = utils.get_covariance(fiducial_noise, cw, wsp, num_bins).reshape( + (num_bins, 4, num_bins, 4) +) + +# Couple the fiducial coupling = wsp.get_coupling_matrix() -coupling_re = np.reshape(coupling, (4, lmax, 4, lmax), order='F') +coupling_re = np.reshape(coupling, (4, lmax, 4, lmax), order="F") coupled_fiducial = np.tensordot(coupling_re, fiducial_noise) / np.mean(n_gal**2) -#Get the covariance +# Get the covariance cov_unions_theo_coupled = utils.get_covariance( - coupled_fiducial, - cw, - wsp, - num_bins + coupled_fiducial, cw, wsp, num_bins ).reshape((num_bins, 4, num_bins, 4)) ### Get covariances for GLASS mock -#Get field , wsp and cw -f_glass, wsp_glass = utils.get_field_and_workspace_map( - n_gal_glass, lmax - 1, b -) +# Get field , wsp and cw +f_glass, wsp_glass = utils.get_field_and_workspace_map(n_gal_glass, lmax - 1, b) cw_glass = nmt.NmtCovarianceWorkspace.from_fields(f_glass, f_glass, f_glass, f_glass) -#Get the fiducial noise -fiducial_noise_glass = np.array([ - fiducial, - 0.*fiducial, - 0.*fiducial, - 0.*fiducial -]) + noise_bias_theo_glass +# Get the fiducial noise +fiducial_noise_glass = ( + np.array([fiducial, 0.0 * fiducial, 0.0 * fiducial, 0.0 * fiducial]) + + noise_bias_theo_glass +) -#Get the covariance +# Get the covariance cov_glass_theo = utils.get_covariance( - fiducial_noise_glass, - cw_glass, - wsp_glass, - num_bins + fiducial_noise_glass, cw_glass, wsp_glass, num_bins ).reshape((num_bins, 4, num_bins, 4)) -#Couple the fiducial +# Couple the fiducial coupling_glass = wsp_glass.get_coupling_matrix() -coupling_re_glass = np.reshape(coupling_glass, (4, lmax, 4, lmax), order='F') -coupled_fiducial_glass = np.tensordot(coupling_re_glass, fiducial_noise_glass) / np.mean(n_gal_glass**2) +coupling_re_glass = np.reshape(coupling_glass, (4, lmax, 4, lmax), order="F") +coupled_fiducial_glass = np.tensordot( + coupling_re_glass, fiducial_noise_glass +) / np.mean(n_gal_glass**2) -#Get the covariance +# Get the covariance cov_glass_theo_coupled = utils.get_covariance( - coupled_fiducial_glass, - cw_glass, - wsp_glass, - num_bins + coupled_fiducial_glass, cw_glass, wsp_glass, num_bins ).reshape((num_bins, 4, num_bins, 4)) # %% @@ -1358,49 +1178,49 @@ plt.plot( ell_eff, np.sqrt(np.diag(cov_unions_theo[:, 0, :, 0])), - label='UNIONS catalog-based noise bias n_gal deconvolved' + label="UNIONS catalog-based noise bias n_gal deconvolved", ) plt.plot( ell_eff, np.sqrt(np.diag(cov_unions_theo_coupled[:, 0, :, 0])), - label='UNIONS catalog-based noise bias n_gal coupled' + label="UNIONS catalog-based noise bias n_gal coupled", ) plt.plot( ell_eff, np.sqrt(np.diag(cov_glass_theo[:, 0, :, 0])), - label='GLASS mock-based noise bias n_gal deconvolved' + label="GLASS mock-based noise bias n_gal deconvolved", ) plt.plot( ell_eff, np.sqrt(np.diag(cov_glass_theo_coupled[:, 0, :, 0])), - label='GLASS mock-based noise bias n_gal coupled' + label="GLASS mock-based noise bias n_gal coupled", ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), - color='black', - linestyle='--', - label='OneCovariance' + color="black", + linestyle="--", + label="OneCovariance", ) plt.plot( ell_eff, np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)), - color='gray', - label='NaMaster built-in covariance', - linestyle='--' + color="gray", + label="NaMaster built-in covariance", + linestyle="--", ) -plt.xscale('squareroot') +plt.xscale("squareroot") xticks = [100, 400, 900, 1600] plt.xticks(xticks, xticks) plt.xlabel(r"$\ell$") plt.ylabel(r"$\sigma(C_\ell)$") -plt.yscale('log') +plt.yscale("log") plt.legend() plt.savefig("./plots/errorbar_n_gal_map_decoupled_vs_coupled.png", dpi=300) @@ -1416,43 +1236,66 @@ ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1], sharex=ax1) -ax1.plot(ell_eff, np.sqrt(np.diag(cov_glass_theo_coupled[:, 0, :, 0])), label="GLASS mock noise bias") -ax1.plot(ell_eff, np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)), label="UNIONS data noise bias") -ax1.plot(ell_eff, np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), label="OneCovariance", color='C2') +ax1.plot( + ell_eff, + np.sqrt(np.diag(cov_glass_theo_coupled[:, 0, :, 0])), + label="GLASS mock noise bias", +) +ax1.plot( + ell_eff, + np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)), + label="UNIONS data noise bias", +) +ax1.plot( + ell_eff, + np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data)), + label="OneCovariance", + color="C2", +) -#Set ticks and scales for the x-axis -ax1.set_xscale('squareroot') +# Set ticks and scales for the x-axis +ax1.set_xscale("squareroot") ax1.set_xticks(np.array([100, 400, 900, 1600])) ax1.minorticks_on() -ax1.tick_params(axis='x', which='minor', length=2, width=0.8) -minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] +ax1.tick_params(axis="x", which="minor", length=2, width=0.8) +minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax1.set_xticks(minor_ticks, minor=True) # Set ticks and scale for the y-axis -ax1.set_yscale('log') +ax1.set_yscale("log") ax1.set_yticks([1e-11, 1e-10, 1e-9, 1e-8]) -ax1.tick_params(axis='y', which='minor', length=2, width=0.8) -minor_ticks = [i * 1e-11 for i in range(1, 10)] + [i * 1e-10 for i in range(1, 10)] + [i * 1e-9 for i in range(1, 10)] +ax1.tick_params(axis="y", which="minor", length=2, width=0.8) +minor_ticks = ( + [i * 1e-11 for i in range(1, 10)] + + [i * 1e-10 for i in range(1, 10)] + + [i * 1e-9 for i in range(1, 10)] +) ax1.set_yticks(minor_ticks, minor=True) # Set labels and legend ax1.set_ylabel(r"$\sigma(C_\ell^{EE})$") ax1.legend(fontsize=10) -relative_error_glass = np.sqrt(np.diag(cov_glass_theo_coupled[:, 0, :, 0]))/np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)) -relative_error_onecov = np.sqrt(np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data))/np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)) +relative_error_glass = np.sqrt(np.diag(cov_glass_theo_coupled[:, 0, :, 0])) / np.sqrt( + np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data) +) +relative_error_onecov = np.sqrt( + np.diag(pseudo_cl_cov["COVAR_GAUSSIAN"].data) +) / np.sqrt(np.diag(pseudo_cl_cov_namaster["COVAR_EE_EE"].data)) ax2.plot(ell_eff, relative_error_glass - 1, label="GLASS mock noise bias") -ax2.plot(ell_eff, relative_error_onecov - 1, label="OneCovariance", color='C2') -ax2.axhline(0, color='C1', linestyle='-') +ax2.plot(ell_eff, relative_error_onecov - 1, label="OneCovariance", color="C2") +ax2.axhline(0, color="C1", linestyle="-") -#Set ticks and scales for the y-axis +# Set ticks and scales for the y-axis ax2.set_yticks([-0.3, -0.15, 0.0, 0.15, 0.3]) ax2.set_ylim(-0.3, 0.3) ax2.set_ylabel(r"$ \Delta \hat{\sigma} / \sigma$") ax2.set_xlabel(r"$\ell$") -plt.savefig("./plots/density_variation_effect_errorbar.png", dpi=300, bbox_inches='tight') -#Save pdf version -plt.savefig("./plots/density_variation_effect_errorbar.pdf", bbox_inches='tight') +plt.savefig( + "./plots/density_variation_effect_errorbar.png", dpi=300, bbox_inches="tight" +) +# Save pdf version +plt.savefig("./plots/density_variation_effect_errorbar.pdf", bbox_inches="tight") plt.show() diff --git a/scratch/guerrini/get_prior_leakage.py b/scratch/guerrini/get_prior_leakage.py index 59d9fc6a..99affaa1 100644 --- a/scratch/guerrini/get_prior_leakage.py +++ b/scratch/guerrini/get_prior_leakage.py @@ -8,11 +8,11 @@ import numpy as np # %% -base_dir = 'output/rho_tau_stats/' -version = 'SP_v1.4.5_leak_corr' +base_dir = "output/rho_tau_stats/" +version = "SP_v1.4.5_leak_corr" # %% -samples_leakage = np.load(f'{base_dir}/samples_{version}.npy') +samples_leakage = np.load(f"{base_dir}/samples_{version}.npy") # %% mean_samples = np.mean(samples_leakage, axis=0) @@ -21,11 +21,11 @@ # %% prior_psf = { - 'bin1': { - 'mean': mean_samples, - 'cov': cov_samples, + "bin1": { + "mean": mean_samples, + "cov": cov_samples, } } # %% -np.save(f'{base_dir}/prior_psf_sys_{version}.npy', prior_psf) +np.save(f"{base_dir}/prior_psf_sys_{version}.npy", prior_psf) diff --git a/scratch/guerrini/namaster_utils.py b/scratch/guerrini/namaster_utils.py index 294e47f0..52dbdb90 100644 --- a/scratch/guerrini/namaster_utils.py +++ b/scratch/guerrini/namaster_utils.py @@ -17,76 +17,76 @@ ipython.run_line_magic("load_ext", "autoreload") ipython.run_line_magic("autoreload", "2") +import healpy as hp +import matplotlib.pyplot as plt import numpy as np +import pymaster as nmt +import seaborn as sns from astropy.io import fits -from astropy.cosmology import Planck18 -import matplotlib.pyplot as plt from matplotlib import scale as mscale -import seaborn as sns from tqdm import tqdm -import healpy as hp -import pymaster as nmt -import camb +from sp_validation.cosmology import get_cosmo, get_theo_c_ell from sp_validation.rho_tau import SquareRootScale -from sp_validation.cosmo_val import CosmologyValidation -from sp_validation.cosmology import get_theo_c_ell, get_cosmo -from sp_validation.rho_tau import get_params_rho_tau mscale.register_scale(SquareRootScale) -plt.style.use( - '/home/guerrini/matplotlib_config/paper.mplstyle' -) -plt.rcParams['text.usetex'] = False +plt.style.use("/home/guerrini/matplotlib_config/paper.mplstyle") +plt.rcParams["text.usetex"] = False sns.set_palette("husl") if ipython is not None: ipython.run_line_magic("matplotlib", "inline") -def load_cat(path_cat, w_col='w_des'): + +def load_cat(path_cat, w_col="w_des"): cat_gal = fits.getdata(path_cat) if w_col is None: - w = np.ones_like(cat_gal['RA']) + w = np.ones_like(cat_gal["RA"]) else: w = cat_gal[w_col] - return cat_gal['RA'], cat_gal['DEC'], cat_gal['e1'], cat_gal['e2'], w + return cat_gal["RA"], cat_gal["DEC"], cat_gal["e1"], cat_gal["e2"], w + def load_cat_des(path_cat): cat_gal = fits.getdata(path_cat) - ra = cat_gal['RA'] - dec = cat_gal['DEC'] - e1 = cat_gal['E1'] - e2 = cat_gal['E2'] - weight = cat_gal['w'] - - e1 = (e1 - np.average(e1, weights=weight)) / np.average(cat_gal['R11'], weights=weight) - e2 = (e2 - np.average(e2, weights=weight)) / np.average(cat_gal['R22'], weights=weight) + ra = cat_gal["RA"] + dec = cat_gal["DEC"] + e1 = cat_gal["E1"] + e2 = cat_gal["E2"] + weight = cat_gal["w"] + + e1 = (e1 - np.average(e1, weights=weight)) / np.average( + cat_gal["R11"], weights=weight + ) + e2 = (e2 - np.average(e2, weights=weight)) / np.average( + cat_gal["R22"], weights=weight + ) return ra, dec, e1, e2, weight + def load_redshift_distr(path_nz): z, dndz = np.loadtxt(path_nz, unpack=True) return z, dndz + def load_cl_measurement(path_cl): cl_data = fits.getdata(path_cl) - cl_array = np.array([ - cl_data['EE'].data, - cl_data['EB'].data, - cl_data['EB'].data, - cl_data['BB'].data - ]) + cl_array = np.array( + [cl_data["EE"].data, cl_data["EB"].data, cl_data["EB"].data, cl_data["BB"].data] + ) return cl_array + def get_binning(lmin, lmax, num_bins): b_lmax = lmax - 1 - ells = np.arange(lmin, lmax+1) + ells = np.arange(lmin, lmax + 1) - start = np.power(lmin, 1/2) - end = np.power(lmax, 1/2) - bins_ell = np.power(np.linspace(start, end, num_bins +1), 2) + start = np.power(lmin, 1 / 2) + end = np.power(lmax, 1 / 2) + bins_ell = np.power(np.linspace(start, end, num_bins + 1), 2) bpws = np.digitize(ells.astype(float), bins_ell) - 1 bpws[0] = 0 @@ -96,21 +96,17 @@ def get_binning(lmin, lmax, num_bins): return b + def get_theory(lmax, z, nz): ell = np.arange(1, lmax + 1) - cl_theo = get_theo_c_ell( - ell=ell, - z=z, - nz=nz, - backend="camb", - cosmo=get_cosmo() - ) + cl_theo = get_theo_c_ell(ell=ell, z=z, nz=nz, backend="camb", cosmo=get_cosmo()) return cl_theo + def get_n_gal_map(nside, ra, dec, w): - theta = (90. - dec) * np.pi / 180. - phi = ra * np.pi / 180. + theta = (90.0 - dec) * np.pi / 180.0 + phi = ra * np.pi / 180.0 pix = hp.ang2pix(nside, theta, phi) unique_pix, idx, idx_rep = np.unique(pix, return_index=True, return_inverse=True) @@ -118,13 +114,15 @@ def get_n_gal_map(nside, ra, dec, w): n_gal[unique_pix] = np.bincount(idx_rep, weights=w) return n_gal, unique_pix, idx, idx_rep + def apply_random_rotations(e1, e2): np.random.seed() - rot_angle = np.random.rand(len(e1))*2*np.pi - e1_out = e1*np.cos(rot_angle) + e2*np.sin(rot_angle) - e2_out = -e1*np.sin(rot_angle) + e2*np.cos(rot_angle) + rot_angle = np.random.rand(len(e1)) * 2 * np.pi + e1_out = e1 * np.cos(rot_angle) + e2 * np.sin(rot_angle) + e2_out = -e1 * np.sin(rot_angle) + e2 * np.cos(rot_angle) return e1_out, e2_out + def get_gaussian_real(nside, mask, n_gal, unique_pix, idx_rep, e1, e2, w): e1_rot, e2_rot = apply_random_rotations(e1, e2) @@ -139,17 +137,19 @@ def get_gaussian_real(nside, mask, n_gal, unique_pix, idx_rep, e1, e2, w): noise_map = noise_map_e1 + 1j * noise_map_e2 return noise_map + def get_sample(noise_map, mask, lmax, b, wsp=None): f = nmt.NmtField(mask=mask, maps=[noise_map.real, noise_map.imag], lmax=lmax) if wsp is None: - wsp = nmt.NmtWorkspace.from_fields(f, f, b) + wsp = nmt.NmtWorkspace.from_fields(f, f, b) cl_noise_coupled = nmt.compute_coupled_cell(f, f) cl_noise = wsp.decouple_cell(cl_noise_coupled) return cl_noise, f, wsp, cl_noise_coupled + def get_field_and_workspace_cat(ra, dec, e1, e2, lmax, b, w=None): if w is None: w = np.ones_like(e1) @@ -158,57 +158,64 @@ def get_field_and_workspace_cat(ra, dec, e1, e2, lmax, b, w=None): f_all = nmt.NmtFieldCatalog( positions=[ra, dec], - weights = w, + weights=w, field=[e1, -e2], - lmax = lmax, - lmax_mask = lmax, + lmax=lmax, + lmax_mask=lmax, spin=2, - lonlat=True + lonlat=True, ) wsp = nmt.NmtWorkspace.from_fields(f_all, f_all, b) return f_all, wsp + def get_field_and_workspace_map(mask, lmax, b): nside = hp.npix2nside(len(mask)) - f = nmt.NmtField(mask=mask, maps=[np.zeros(hp.nside2npix(nside)), np.zeros(hp.nside2npix(nside))], lmax=lmax) - wsp = nmt.NmtWorkspace.from_fields(f, f, b) + f = nmt.NmtField( + mask=mask, + maps=[np.zeros(hp.nside2npix(nside)), np.zeros(hp.nside2npix(nside))], + lmax=lmax, + ) + wsp = nmt.NmtWorkspace.from_fields(f, f, b) return f, wsp + def get_covariance_workspace(f): cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) return cw + def get_covariance(cl_array, cw, wsp, num_bins): - covar_22_22 = nmt.gaussian_covariance(cw, 2, 2, 2, 2, - cl_array, - cl_array, - cl_array, - cl_array, - wsp, wb=wsp).reshape((num_bins, 4, num_bins, 4)) + covar_22_22 = nmt.gaussian_covariance( + cw, 2, 2, 2, 2, cl_array, cl_array, cl_array, cl_array, wsp, wb=wsp + ).reshape((num_bins, 4, num_bins, 4)) return covar_22_22 + def get_covariance_from_glass(root, n_sim, num_bins, lmax, type): - if type == 'cat': - name_file = 'cl_glass_mock_' + if type == "cat": + name_file = "cl_glass_mock_" shape_output = num_bins - elif type == 'cat_coupled': - name_file = 'cl_coupled_glass_mock_' + elif type == "cat_coupled": + name_file = "cl_coupled_glass_mock_" shape_output = lmax - elif type == 'map': - name_file = 'cl_map_glass_mock_' + elif type == "map": + name_file = "cl_map_glass_mock_" shape_output = num_bins - elif type == 'map_coupled': - name_file = 'cl_coupled_map_glass_mock_' + elif type == "map_coupled": + name_file = "cl_coupled_map_glass_mock_" shape_output = lmax else: - raise ValueError("Type must be one of 'cat', 'cat_coupled', 'map', 'map_coupled'") + raise ValueError( + "Type must be one of 'cat', 'cat_coupled', 'map', 'map_coupled'" + ) cls = np.array([]).reshape((0, shape_output)) - for i in tqdm(range(1, n_sim+1)): - path_cl = f'{root}/{name_file}{i:05d}_4096.npy' + for i in tqdm(range(1, n_sim + 1)): + path_cl = f"{root}/{name_file}{i:05d}_4096.npy" cl_sim = np.load(path_cl) cls = np.vstack((cls, cl_sim[1])) return np.cov(cls.T), cls diff --git a/scratch/guerrini/one_covariance.py b/scratch/guerrini/one_covariance.py index 378c505d..ac79982b 100644 --- a/scratch/guerrini/one_covariance.py +++ b/scratch/guerrini/one_covariance.py @@ -7,18 +7,20 @@ """ # %% +import matplotlib.pyplot as plt +import matplotlib.scale as mscale import numpy as np from astropy.io import fits -import matplotlib.pyplot as plt -import matplotlib.scale as mscale from sp_validation.rho_tau import SquareRootScale from sp_validation.statistics import corr_from_cov, cov_from_one_covariance mscale.register_scale(SquareRootScale) # %% -redshift_distribution = np.loadtxt('/n17data/mkilbing/astro/data/CFIS/v1.0/nz/dndz_SP_A.txt') +redshift_distribution = np.loadtxt( + "/n17data/mkilbing/astro/data/CFIS/v1.0/nz/dndz_SP_A.txt" +) # %% z, dndz = redshift_distribution[:, 0], redshift_distribution[:, 1] @@ -32,13 +34,18 @@ # %% # Define the header -header = "# # binstart, density\n" \ - "# dndz_SP_A" +header = "# # binstart, density\n# dndz_SP_A" -np.savetxt("/home/guerrini/OneCovariance/input/redshift_distribution/dndz_SP_A.asc", redshift_distribution, header=header, fmt="%.18e", comments='') +np.savetxt( + "/home/guerrini/OneCovariance/input/redshift_distribution/dndz_SP_A.asc", + redshift_distribution, + header=header, + fmt="%.18e", + comments="", +) # %% -#Get healpix mask +# Get healpix mask path_cat = "/n17data/guinot/CFIS_3500_cat/catalogues_SPv1_v1.4.5/shapepipe_SPv1.fits" cat_gal = fits.getdata(path_cat) @@ -47,12 +54,12 @@ nside = 1024 -ra = cat_gal['ra'] -dec = cat_gal['dec'] -w = cat_gal['w'] +ra = cat_gal["ra"] +dec = cat_gal["dec"] +w = cat_gal["w"] -theta = (90. - dec) * np.pi / 180. -phi = ra * np.pi / 180. +theta = (90.0 - dec) * np.pi / 180.0 +phi = ra * np.pi / 180.0 pix = hp.ang2pix(nside, theta, phi) unique_pix, idx, idx_rep = np.unique(pix, return_index=True, return_inverse=True) @@ -71,29 +78,38 @@ print(f"Area: {area} deg^2") # %% -n_eff = 1/(area*3600)*(np.sum(cat_gal['w']))**2/np.sum(cat_gal['w']**2) +n_eff = 1 / (area * 3600) * (np.sum(cat_gal["w"])) ** 2 / np.sum(cat_gal["w"] ** 2) n_eff # %% -shape_noise = 0.5*(np.sum(cat_gal['w']**2*cat_gal['g1']**2)/np.sum(cat_gal['w']**2) + np.sum(cat_gal['w']**2*cat_gal['g2']**2)/np.sum(cat_gal['w']**2)) +shape_noise = 0.5 * ( + np.sum(cat_gal["w"] ** 2 * cat_gal["g1"] ** 2) / np.sum(cat_gal["w"] ** 2) + + np.sum(cat_gal["w"] ** 2 * cat_gal["g2"] ** 2) / np.sum(cat_gal["w"] ** 2) +) np.sqrt(shape_noise) # %% -cov_one_cov = np.genfromtxt("/home/guerrini/OneCovariance/output/covariance_list_3x2pt_pure_Cell.dat") +cov_one_cov = np.genfromtxt( + "/home/guerrini/OneCovariance/output/covariance_list_3x2pt_pure_Cell.dat" +) cov_cosmo_val = fits.open("./output/pseudo_cl_cov_SP_v1.4.5.A.fits")["COVAR_EE_EE"].data # %% cov_one_cov # %% -hp.write_map("/home/guerrini/OneCovariance/input/mask/healpix_mask_SPv1.fits", mask, overwrite=True) +hp.write_map( + "/home/guerrini/OneCovariance/input/mask/healpix_mask_SPv1.fits", + mask, + overwrite=True, +) # %% corr_cosmo_val = corr_from_cov(cov_cosmo_val) # %% -start = np.power(8, 1/2) -end = np.power(2048, 1/2) +start = np.power(8, 1 / 2) +end = np.power(2048, 1 / 2) bins_ell = np.power(np.linspace(start, end, 33), 2) bins_ell = 0.5 * (bins_ell[1:] + bins_ell[:-1]) @@ -101,14 +117,18 @@ # %% plt.figure() -plt.imshow(corr_cosmo_val, cmap='seismic', vmin=-1, vmax=1) -plt.colorbar(label='Correlation Coefficient') +plt.imshow(corr_cosmo_val, cmap="seismic", vmin=-1, vmax=1) +plt.colorbar(label="Correlation Coefficient") step = len(bins_ell) // 4 -plt.xticks(ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1)) -plt.yticks(ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1)) - -plt.title('Gaussian Covariance Matrix Cell') +plt.xticks( + ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1) +) +plt.yticks( + ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1) +) + +plt.title("Gaussian Covariance Matrix Cell") plt.show() @@ -124,25 +144,33 @@ plt.subplot(121) -plt.imshow(corr_one_cov_gaussian, cmap='seismic', vmin=-1, vmax=1) -plt.colorbar(label='Correlation Coefficient') +plt.imshow(corr_one_cov_gaussian, cmap="seismic", vmin=-1, vmax=1) +plt.colorbar(label="Correlation Coefficient") step = len(bins_ell) // 4 -plt.xticks(ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1)) -plt.yticks(ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1)) +plt.xticks( + ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1) +) +plt.yticks( + ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1) +) -plt.title('Gaussian (OneCov)') +plt.title("Gaussian (OneCov)") plt.subplot(122) -plt.imshow(corr_one_cov_pure, cmap='seismic', vmin=-1, vmax=1) -plt.colorbar(label='Correlation Coefficient') +plt.imshow(corr_one_cov_pure, cmap="seismic", vmin=-1, vmax=1) +plt.colorbar(label="Correlation Coefficient") step = len(bins_ell) // 4 -plt.xticks(ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1)) -plt.yticks(ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1)) +plt.xticks( + ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1) +) +plt.yticks( + ticks=np.arange(0, len(bins_ell), step), labels=np.round(bins_ell[::step], 1) +) -plt.title('G+NG (OneCov)') +plt.title("G+NG (OneCov)") plt.tight_layout() plt.savefig("Plots/corr_onecovariance.png") @@ -151,16 +179,22 @@ # %% plt.figure() -plt.plot(bins_ell, np.diag(cov_matrix_one_cov_gaussian), label='Gaussian Covariance (OneCovariance)') -plt.plot(bins_ell, np.diag(cov_matrix_one_cov_pure), label='Pure Covariance (OneCovariance)') -plt.plot(bins_ell, np.diag(cov_cosmo_val), label='Cosmo Val Covariance') - -plt.xlabel('Multipole (ell)') -plt.ylabel('Covariance') -plt.title('Diagonal of Covariance Matrices') - -plt.yscale('log') -plt.xscale('log') +plt.plot( + bins_ell, + np.diag(cov_matrix_one_cov_gaussian), + label="Gaussian Covariance (OneCovariance)", +) +plt.plot( + bins_ell, np.diag(cov_matrix_one_cov_pure), label="Pure Covariance (OneCovariance)" +) +plt.plot(bins_ell, np.diag(cov_cosmo_val), label="Cosmo Val Covariance") + +plt.xlabel("Multipole (ell)") +plt.ylabel("Covariance") +plt.title("Diagonal of Covariance Matrices") + +plt.yscale("log") +plt.xscale("log") plt.legend() plt.grid() @@ -168,19 +202,19 @@ plt.show() # %% -#mask comparison +# mask comparison path_145 = "/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/v1.4.x/v1.4.5/unions_shapepipe_cut_struc_2024_v1.4.5.fits" cat_145 = fits.getdata(path_145) # %% nside = 1024 -ra = cat_145['ra'] -dec = cat_145['dec'] -w = cat_145['w_des'] +ra = cat_145["ra"] +dec = cat_145["dec"] +w = cat_145["w_des"] -theta = (90. - dec) * np.pi / 180. -phi = ra * np.pi / 180. +theta = (90.0 - dec) * np.pi / 180.0 +phi = ra * np.pi / 180.0 pix = hp.ang2pix(nside, theta, phi) unique_pix, idx, idx_rep_145 = np.unique(pix, return_index=True, return_inverse=True) @@ -197,23 +231,23 @@ hp.mollview(n_gal_145) # %% -rot_angle = np.random.rand(len(cat_145))*2*np.pi -e1_rot = cat_145['e1'] * np.cos(rot_angle) + cat_145['e2'] * np.sin(rot_angle) -e2_rot = -cat_145['e1'] * np.sin(rot_angle) + cat_145['e2'] * np.cos(rot_angle) +rot_angle = np.random.rand(len(cat_145)) * 2 * np.pi +e1_rot = cat_145["e1"] * np.cos(rot_angle) + cat_145["e2"] * np.sin(rot_angle) +e2_rot = -cat_145["e1"] * np.sin(rot_angle) + cat_145["e2"] * np.cos(rot_angle) # %% noise_map_e1 = np.zeros(hp.nside2npix(nside)) noise_map_e2 = np.zeros(hp.nside2npix(nside)) -noise_map_e1[unique_pix] += np.bincount(idx_rep_145, weights=e1_rot*cat_145['w_des']) -noise_map_e2[unique_pix] += np.bincount(idx_rep_145, weights=e2_rot*cat_145['w_des']) +noise_map_e1[unique_pix] += np.bincount(idx_rep_145, weights=e1_rot * cat_145["w_des"]) +noise_map_e2[unique_pix] += np.bincount(idx_rep_145, weights=e2_rot * cat_145["w_des"]) noise_map_e1[~mask] = 0 noise_map_e2[~mask] = 0 noise_map_e1[mask] /= n_gal[mask] noise_map_e2[mask] /= n_gal[mask] # %% -lmax = 2*nside +lmax = 2 * nside path_redshift_distr = "/n17data/mkilbing/astro/data/CFIS/v1.0/nz/dndz_SP_A.txt" pw = hp.pixwin(nside, lmax=lmax) @@ -224,7 +258,7 @@ planck = Planck18 -h = planck.H0.value/100 +h = planck.H0.value / 100 Om = planck.Om0 Ob = planck.Ob0 Oc = Om - Ob @@ -233,18 +267,38 @@ m_nu = 0.06 w = -1 -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, ns=ns, mnu=m_nu, w=w, As=As, WantTransfer=True, NonLinear=camb.model.NonLinear_both) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + ns=ns, + mnu=m_nu, + w=w, + As=As, + WantTransfer=True, + NonLinear=camb.model.NonLinear_both, +) Onu = pars.omeganu Oc = Om - Ob - Onu -pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2, ns=ns, mnu=m_nu, w=w, As=As, WantTransfer=True, NonLinear=camb.model.NonLinear_both) +pars = camb.set_params( + H0=100 * h, + omch2=Oc * h**2, + ombh2=Ob * h**2, + ns=ns, + mnu=m_nu, + w=w, + As=As, + WantTransfer=True, + NonLinear=camb.model.NonLinear_both, +) z, dndz = np.loadtxt(path_redshift_distr, unpack=True) -#getthe expected cl's from CAMB +# getthe expected cl's from CAMB pars.min_l = 1 pars.set_for_lmax(lmax) pars.SourceWindows = [ - camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type='lensing') + camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type="lensing") ] theory_cls = camb.get_results(pars).get_source_cls_dict(lmax=lmax, raw_cl=True) @@ -262,15 +316,15 @@ lmin = 8 lmax = 2048 -start = np.power(lmin, 1/2) -end = np.power(lmax, 1/2) +start = np.power(lmin, 1 / 2) +end = np.power(lmax, 1 / 2) bins_ell = np.power(np.linspace(start, end, 33), 2) -ells = np.arange(lmin, lmax+1) +ells = np.arange(lmin, lmax + 1) bpws = np.digitize(ells.astype(float), bins_ell) - 1 bpws[0] = 0 -bpws[-1] = 32-1 +bpws[-1] = 32 - 1 b = nmt.NmtBin(ells=ells, bpws=bpws, lmax=lmax) @@ -281,34 +335,37 @@ cl_noise = wsp.decouple_cell(cl_noise) # %% -fiducial_cl = np.array([fiducial_cl, 0.*fiducial_cl, 0.*fiducial_cl, 0.*fiducial_cl])+ np.mean(cl_noise, axis=1, keepdims=True) +fiducial_cl = np.array( + [fiducial_cl, 0.0 * fiducial_cl, 0.0 * fiducial_cl, 0.0 * fiducial_cl] +) + np.mean(cl_noise, axis=1, keepdims=True) # %% cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) -covar_22_22 = nmt.gaussian_covariance(cw, 2, 2, 2, 2, - fiducial_cl, - fiducial_cl, - fiducial_cl, - fiducial_cl, - wsp, wb=wsp).reshape([32, 4, 32, 4]) +covar_22_22 = nmt.gaussian_covariance( + cw, 2, 2, 2, 2, fiducial_cl, fiducial_cl, fiducial_cl, fiducial_cl, wsp, wb=wsp +).reshape([32, 4, 32, 4]) # %% plt.figure() bins_ell_mid = 0.5 * (bins_ell[1:] + bins_ell[:-1]) -cov_cosmo_val_145 = fits.open("./output/pseudo_cl_cov_SP_v1.4.5.fits")["COVAR_EE_EE"].data +cov_cosmo_val_145 = fits.open("./output/pseudo_cl_cov_SP_v1.4.5.fits")[ + "COVAR_EE_EE" +].data -plt.plot(bins_ell_mid, np.diag(cov_cosmo_val_145), label='SP_v1.4.5') -plt.plot(bins_ell_mid, np.diag(covar_22_22[:, 0, :, 0]), label="SP_v1.4.5 w mask SP_v1.4.5.A") +plt.plot(bins_ell_mid, np.diag(cov_cosmo_val_145), label="SP_v1.4.5") +plt.plot( + bins_ell_mid, np.diag(covar_22_22[:, 0, :, 0]), label="SP_v1.4.5 w mask SP_v1.4.5.A" +) -plt.yscale('log') -plt.xscale('log') +plt.yscale("log") +plt.xscale("log") plt.legend() -plt.xlabel('Multipole (ell)') -plt.ylabel('Covariance') +plt.xlabel("Multipole (ell)") +plt.ylabel("Covariance") plt.savefig("Plots/mask_effect_covariance.png") plt.show() @@ -316,15 +373,15 @@ # %% plt.figure() -plt.plot(bins_ell_mid, np.diag(cov_cosmo_val_145), label='SP_v1.4.5') +plt.plot(bins_ell_mid, np.diag(cov_cosmo_val_145), label="SP_v1.4.5") plt.plot(bins_ell_mid, np.diag(cov_cosmo_val), label="SP_v1.4.5.A") -plt.xscale('log') -plt.yscale('log') +plt.xscale("log") +plt.yscale("log") plt.legend() -plt.xlabel('Multipole (ell)') -plt.ylabel('Covariance') +plt.xlabel("Multipole (ell)") +plt.ylabel("Covariance") plt.savefig("Plots/covariance_axel_comparison.png") plt.show() diff --git a/scratch/guerrini/plot_comparison.py b/scratch/guerrini/plot_comparison.py index bf61352b..42355e7f 100644 --- a/scratch/guerrini/plot_comparison.py +++ b/scratch/guerrini/plot_comparison.py @@ -7,31 +7,29 @@ # %% import os +import healpy as hp +import matplotlib.pyplot as plt import numpy as np from astropy.io import fits -import matplotlib.pyplot as plt -import healpy as hp from tqdm import tqdm -from mpl_toolkits.axes_grid1 import make_axes_locatable -plt.rcParams.update({ - "text.usetex": True, - "font.family": "serif", - "font.serif": ["Times New Roman"], # You can change this font if you prefer - "axes.labelsize": 14, # Adjust as needed - "axes.titlesize": 16, # Adjust as needed - "xtick.labelsize": 14, # Adjust as needed - "ytick.labelsize": 14, # Adjust as needed - "legend.fontsize": 12, # Adjust as needed - "figure.figsize": (15, 8), # Adjust as needed - "figure.dpi": 600 # Adjust as needed -}) -from shear_psf_leakage.rho_tau_stat import RhoStat, TauStat, PSFErrorFit +plt.rcParams.update( + { + "text.usetex": True, + "font.family": "serif", + "font.serif": ["Times New Roman"], # You can change this font if you prefer + "axes.labelsize": 14, # Adjust as needed + "axes.titlesize": 16, # Adjust as needed + "xtick.labelsize": 14, # Adjust as needed + "ytick.labelsize": 14, # Adjust as needed + "legend.fontsize": 12, # Adjust as needed + "figure.figsize": (15, 8), # Adjust as needed + "figure.dpi": 600, # Adjust as needed + } +) import treecorr - -from getdist import plots, MCSamples - from IPython import get_ipython +from shear_psf_leakage.rho_tau_stat import PSFErrorFit, RhoStat, TauStat ipython = get_ipython() if ipython is not None: @@ -40,16 +38,16 @@ ipython.run_line_magic("matplotlib", "inline") # %% -base_dir = '/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/' -path_gal = base_dir + 'v1.4.x/unions_shapepipe_cut_struc_2024_v1.4.2.fits' -path_psf = base_dir + 'unions_shapepipe_psf_2024_v1.4.1.fits' +base_dir = "/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/" +path_gal = base_dir + "v1.4.x/unions_shapepipe_cut_struc_2024_v1.4.2.fits" +path_psf = base_dir + "unions_shapepipe_psf_2024_v1.4.1.fits" cat_gal = fits.getdata(path_gal) cat_psf = fits.getdata(path_psf) # %% -ra = cat_gal['RA'] -dec = cat_gal['DEC'] +ra = cat_gal["RA"] +dec = cat_gal["DEC"] theta = (90 - dec) * np.pi / 180 phi = ra * np.pi / 180 @@ -62,88 +60,93 @@ unique_pix, idx, idx_pix = np.unique(pix, return_index=True, return_inverse=True) -n_map[unique_pix] += np.bincount(idx_pix, weights=cat_gal['w_iv']) +n_map[unique_pix] += np.bincount(idx_pix, weights=cat_gal["w_iv"]) -mask = (n_map != 0) +mask = n_map != 0 # %% -area = np.sum(mask)*hp.nside2pixarea(nside, degrees=True) -print(f'Area: {area} deg^2') +area = np.sum(mask) * hp.nside2pixarea(nside, degrees=True) +print(f"Area: {area} deg^2") -n_eff_gal = 1/(area*60*60)*(np.sum(cat_gal['w_iv']))**2/np.sum(cat_gal['w_iv']**2) -print(f'Effective number of galaxies: {n_eff_gal}') +n_eff_gal = ( + 1 / (area * 60 * 60) * (np.sum(cat_gal["w_iv"])) ** 2 / np.sum(cat_gal["w_iv"] ** 2) +) +print(f"Effective number of galaxies: {n_eff_gal}") -n_eff_psf = 1/(area*60*60)*(len(cat_psf))**2/np.sum(len(cat_psf)) -print(f'Effective number of PSFs: {n_eff_psf}') +n_eff_psf = 1 / (area * 60 * 60) * (len(cat_psf)) ** 2 / np.sum(len(cat_psf)) +print(f"Effective number of PSFs: {n_eff_psf}") sigma_e = np.sqrt( - 0.5*(np.sum((cat_gal['e1']*cat_gal['w_iv'])**2) + np.sum((cat_gal['e2']*cat_gal['w_iv'])**2)) / np.sum(cat_gal['w_iv']**2) + 0.5 + * ( + np.sum((cat_gal["e1"] * cat_gal["w_iv"]) ** 2) + + np.sum((cat_gal["e2"] * cat_gal["w_iv"]) ** 2) + ) + / np.sum(cat_gal["w_iv"] ** 2) ) -print(f'Intrinsic shape noise: {sigma_e}') +print(f"Intrinsic shape noise: {sigma_e}") # %% -mask_path = '/n17data/mkilbing/astro/data/CFIS/v1.0/Lensfit/masks/CFIS3500_THELI_mask_hp_8192.fits.gz' +mask_path = "/n17data/mkilbing/astro/data/CFIS/v1.0/Lensfit/masks/CFIS3500_THELI_mask_hp_8192.fits.gz" mask_theli = hp.read_map(mask_path) nside = hp.npix2nside(len(mask_theli)) area = np.sum(mask_theli == 0) * hp.nside2pixarea(nside, degrees=True) -print(f'Area: {area} deg^2') +print(f"Area: {area} deg^2") # %% [markdown] # ## Plot comparison between catalogs # %% -#versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha', 'DES'] -#versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4.1', 'SP_v1.4.1_noleakage', 'DES'] -versions = ['SP_v1.4.1', 'SP_v1.4.1_noleakage', 'SP_v1.4.5', 'SP_v1.4.5_leak_corr'] -#colors = ['blue', 'blueviolet', 'brown', 'black', 'orange'] -#colors = ['blue', 'blueviolet', 'black', 'brown', 'orange'] -colors = ['brown', 'black', 'blue', 'midnightblue', 'green', 'olive'] -root_dir = '/home/guerrini/sp_validation/cosmo_val/output/' - -#Inference of the xi_sys parameters -sep_units = 'arcmin' -coord_units = 'degrees' +# versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha', 'DES'] +# versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4.1', 'SP_v1.4.1_noleakage', 'DES'] +versions = ["SP_v1.4.1", "SP_v1.4.1_noleakage", "SP_v1.4.5", "SP_v1.4.5_leak_corr"] +# colors = ['blue', 'blueviolet', 'brown', 'black', 'orange'] +# colors = ['blue', 'blueviolet', 'black', 'brown', 'orange'] +colors = ["brown", "black", "blue", "midnightblue", "green", "olive"] +root_dir = "/home/guerrini/sp_validation/cosmo_val/output/" + +# Inference of the xi_sys parameters +sep_units = "arcmin" +coord_units = "degrees" theta_min = 0.1 theta_max = 250 nbins = 20 TreeCorrConfig_xi = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'jackknife', + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, + "var_method": "jackknife", } -rho_stats_handler = RhoStat( - output='.', - treecorr_config=TreeCorrConfig_xi, - verbose=True -) +rho_stats_handler = RhoStat(output=".", treecorr_config=TreeCorrConfig_xi, verbose=True) tau_stats_handler = TauStat( catalogs=rho_stats_handler.catalogs, - output='.', + output=".", treecorr_config=TreeCorrConfig_xi, - verbose=True - ) + verbose=True, +) -psf_fitter = PSFErrorFit(rho_stats_handler, tau_stats_handler, root_dir+'/rho_tau_stats/') +psf_fitter = PSFErrorFit( + rho_stats_handler, tau_stats_handler, root_dir + "/rho_tau_stats/" +) -#plot of the xi_sys +# plot of the xi_sys plt.figure() for ver, color in zip(versions, colors): print("Plotting version:", ver) - sample = np.load(root_dir+'rho_tau_stats/samples_'+ver+'.npy') - psf_fitter.load_rho_stat('rho_stats_'+ver+'.fits') + sample = np.load(root_dir + "rho_tau_stats/samples_" + ver + ".npy") + psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 - quantiles = [1-quant, quant] - nbins = psf_fitter.rho_stat_handler._treecorr_config['nbins'] + quantiles = [1 - quant, quant] + nbins = psf_fitter.rho_stat_handler._treecorr_config["nbins"] xi_psf_sys_samples = np.array([]).reshape(0, nbins) for i in range(len(sample)): xi_psf_sys = psf_fitter.compute_xi_psf_sys(sample[i]) @@ -151,33 +154,41 @@ xi_psf_sys_mean = np.mean(xi_psf_sys_samples, axis=0) xi_psf_sys_quant = np.quantile(xi_psf_sys_samples, quantiles, axis=0) - theta = psf_fitter.rho_stat_handler.rho_stats['theta'] - plt.plot(theta, xi_psf_sys_mean, c=color, label=r'$\xi_{\rm sys}$ '+ver) - plt.plot(theta, xi_psf_sys_quant[0], c=color, ls='--') - plt.plot(theta, xi_psf_sys_quant[1], c=color, ls='--') - plt.fill_between(theta, xi_psf_sys_quant[0], xi_psf_sys_quant[1], color=color, alpha=0.1) - - if os.path.exists(root_dir+'/xi_plus_'+ver+'.fits'): - xi_plus = fits.getdata(root_dir+'/xi_plus_'+ver+'.fits') - plt.plot(xi_plus['ANG'], xi_plus['VALUE'], c=color, ls='-.', label=r"$\xi_+(\vartheta)$ "+ver) + theta = psf_fitter.rho_stat_handler.rho_stats["theta"] + plt.plot(theta, xi_psf_sys_mean, c=color, label=r"$\xi_{\rm sys}$ " + ver) + plt.plot(theta, xi_psf_sys_quant[0], c=color, ls="--") + plt.plot(theta, xi_psf_sys_quant[1], c=color, ls="--") + plt.fill_between( + theta, xi_psf_sys_quant[0], xi_psf_sys_quant[1], color=color, alpha=0.1 + ) + + if os.path.exists(root_dir + "/xi_plus_" + ver + ".fits"): + xi_plus = fits.getdata(root_dir + "/xi_plus_" + ver + ".fits") + plt.plot( + xi_plus["ANG"], + xi_plus["VALUE"], + c=color, + ls="-.", + label=r"$\xi_+(\vartheta)$ " + ver, + ) else: raise ValueError("File not found") -plt.xscale('log') -plt.yscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'$\xi_{+}$') +plt.xscale("log") +plt.yscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"$\xi_{+}$") plt.legend() -plt.title("Systematic error comparison at 68\% level") -plt.savefig('Plots/xi_sys_lq_comparison.png', dpi=600) +plt.title(r"Systematic error comparison at 68\% level") +plt.savefig("Plots/xi_sys_lq_comparison.png", dpi=600) plt.show() # %% -#versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha', 'DES'] -#colors = ['blue', 'blueviolet', 'brown', 'black', 'orange'] -markers = ['o', 'h', 'd','x', '*', 's'] +# versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha', 'DES'] +# colors = ['blue', 'blueviolet', 'brown', 'black', 'orange'] +markers = ["o", "h", "d", "x", "*", "s"] -#plot of the xi_sys +# plot of the xi_sys plt.figure() offset = 0.02 @@ -185,11 +196,11 @@ for idx, (ver, color, marker) in enumerate(zip(versions, colors, markers)): print("Plotting version:", ver) - sample = np.load(root_dir+'rho_tau_stats/samples_'+ver+'.npy') - psf_fitter.load_rho_stat('rho_stats_'+ver+'.fits') + sample = np.load(root_dir + "rho_tau_stats/samples_" + ver + ".npy") + psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 - quantiles = [1-quant, quant] - nbins = psf_fitter.rho_stat_handler._treecorr_config['nbins'] + quantiles = [1 - quant, quant] + nbins = psf_fitter.rho_stat_handler._treecorr_config["nbins"] xi_psf_sys_samples = np.array([]).reshape(0, nbins) for i in range(len(sample)): xi_psf_sys = psf_fitter.compute_xi_psf_sys(sample[i]) @@ -197,52 +208,72 @@ xi_psf_sys_mean = np.mean(xi_psf_sys_samples, axis=0) xi_psf_sys_quant = np.quantile(xi_psf_sys_samples, quantiles, axis=0) - theta = psf_fitter.rho_stat_handler.rho_stats['theta'] + theta = psf_fitter.rho_stat_handler.rho_stats["theta"] - if os.path.exists(root_dir+'/xi_plus_'+ver+'.fits'): - xi_plus = fits.getdata(root_dir+'/xi_plus_'+ver+'.fits') - xip = xi_plus['VALUE'] + if os.path.exists(root_dir + "/xi_plus_" + ver + ".fits"): + xi_plus = fits.getdata(root_dir + "/xi_plus_" + ver + ".fits") + xip = xi_plus["VALUE"] else: raise ValueError("File not found") - ratio_mean = xi_psf_sys_mean/xip - ratio_quant = xi_psf_sys_quant/xip + ratio_mean = xi_psf_sys_mean / xip + ratio_quant = xi_psf_sys_quant / xip - jittered_theta = theta * (1+idx * offset) + jittered_theta = theta * (1 + idx * offset) - plt.errorbar(jittered_theta, ratio_mean, yerr=[ratio_mean-ratio_quant[0], ratio_quant[1]-ratio_mean], c=color, label=ver, fmt=marker, capsize=5) + plt.errorbar( + jittered_theta, + ratio_mean, + yerr=[ratio_mean - ratio_quant[0], ratio_quant[1] - ratio_mean], + c=color, + label=ver, + fmt=marker, + capsize=5, + ) threshold = 0.05 -plt.fill_between([0.1, 250], [-threshold, -threshold], [threshold, threshold], color='black', alpha=0.1) -plt.plot([0.1, 250], [threshold, threshold], c='black', ls='--', label=str(threshold*100)+'\% level') -plt.plot([0.1, 250], [-threshold, -threshold], c='k', ls='--') -plt.xscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'$\xi_{\rm sys}/\xi_{+}$') +plt.fill_between( + [0.1, 250], + [-threshold, -threshold], + [threshold, threshold], + color="black", + alpha=0.1, +) +plt.plot( + [0.1, 250], + [threshold, threshold], + c="black", + ls="--", + label=str(threshold * 100) + r"\% level", +) +plt.plot([0.1, 250], [-threshold, -threshold], c="k", ls="--") +plt.xscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"$\xi_{\rm sys}/\xi_{+}$") plt.legend() -plt.title("Systematic error comparison at 68\% level") -plt.savefig('Plots/ratio_lq_comparison.png', dpi=600) +plt.title(r"Systematic error comparison at 68\% level") +plt.savefig("Plots/ratio_lq_comparison.png", dpi=600) plt.show() # %% ratio_mean_des = ratio_mean # %% -#versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha'] -#colors = ['blue', 'blueviolet', 'brown', 'black'] -#markers = ['o', 'x', 'h', 'd'] +# versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha'] +# colors = ['blue', 'blueviolet', 'brown', 'black'] +# markers = ['o', 'x', 'h', 'd'] -#plot of the xi_sys +# plot of the xi_sys plt.figure() for idx, (ver, color, marker) in enumerate(zip(versions, colors, markers)): print("Plotting version:", ver) - sample = np.load(root_dir+'rho_tau_stats/samples_'+ver+'.npy') - psf_fitter.load_rho_stat('rho_stats_'+ver+'.fits') + sample = np.load(root_dir + "rho_tau_stats/samples_" + ver + ".npy") + psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 - quantiles = [1-quant, quant] - nbins = psf_fitter.rho_stat_handler._treecorr_config['nbins'] + quantiles = [1 - quant, quant] + nbins = psf_fitter.rho_stat_handler._treecorr_config["nbins"] xi_psf_sys_samples = np.array([]).reshape(0, nbins) for i in range(len(sample)): xi_psf_sys = psf_fitter.compute_xi_psf_sys(sample[i]) @@ -250,31 +281,51 @@ xi_psf_sys_mean = np.mean(xi_psf_sys_samples, axis=0) xi_psf_sys_quant = np.quantile(xi_psf_sys_samples, quantiles, axis=0) - theta = psf_fitter.rho_stat_handler.rho_stats['theta'] + theta = psf_fitter.rho_stat_handler.rho_stats["theta"] - if os.path.exists(root_dir+'/xi_plus_'+ver+'.fits'): - xi_plus = fits.getdata(root_dir+'/xi_plus_'+ver+'.fits') - xip = xi_plus['VALUE'] + if os.path.exists(root_dir + "/xi_plus_" + ver + ".fits"): + xi_plus = fits.getdata(root_dir + "/xi_plus_" + ver + ".fits") + xip = xi_plus["VALUE"] else: raise ValueError("File not found") - ratio_mean = (xi_psf_sys_mean/xip)/np.abs(ratio_mean_des) - ratio_quant = (xi_psf_sys_quant/xip)/np.abs(ratio_mean_des) + ratio_mean = (xi_psf_sys_mean / xip) / np.abs(ratio_mean_des) + ratio_quant = (xi_psf_sys_quant / xip) / np.abs(ratio_mean_des) - jittered_theta = theta * (1+idx * offset) + jittered_theta = theta * (1 + idx * offset) - plt.errorbar(jittered_theta, ratio_mean, yerr=[ratio_mean-ratio_quant[0], ratio_quant[1]-ratio_mean], c=color, label=ver, fmt=marker, capsize=5) + plt.errorbar( + jittered_theta, + ratio_mean, + yerr=[ratio_mean - ratio_quant[0], ratio_quant[1] - ratio_mean], + c=color, + label=ver, + fmt=marker, + capsize=5, + ) threshold = 2 -plt.fill_between([0.1, 250], [-threshold, -threshold], [threshold, threshold], color='black', alpha=0.1) -plt.plot([0.1, 250], [threshold, threshold], c='black', ls='--', label=str(threshold)+'X DES level') -plt.plot([0.1, 250], [-threshold, -threshold], c='k', ls='--') -plt.xscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'$\frac{\xi_{\rm sys}/\xi_{+}}{[\xi_{\rm sys}/\xi_{+}]_{\rm DES}}$') +plt.fill_between( + [0.1, 250], + [-threshold, -threshold], + [threshold, threshold], + color="black", + alpha=0.1, +) +plt.plot( + [0.1, 250], + [threshold, threshold], + c="black", + ls="--", + label=str(threshold) + "X DES level", +) +plt.plot([0.1, 250], [-threshold, -threshold], c="k", ls="--") +plt.xscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"$\frac{\xi_{\rm sys}/\xi_{+}}{[\xi_{\rm sys}/\xi_{+}]_{\rm DES}}$") plt.legend() -plt.title("Systematic error comparison at 68\% level") -plt.savefig('Plots/ratio_DES.png', dpi=600) +plt.title(r"Systematic error comparison at 68\% level") +plt.savefig("Plots/ratio_DES.png", dpi=600) plt.show() # %% [markdown] @@ -284,7 +335,7 @@ # ### Compare rho-stats # %% -base_dir = '/sps/euclid/Users/sguerrin/results/cov_mat/' +base_dir = "/sps/euclid/Users/sguerrin/results/cov_mat/" """ #versions = [ 'DES', @@ -303,28 +354,28 @@ ] """ filenames_rho = [ - base_dir+'/'+version+'/rho_stats_'+version+'.fits' for version in versions + base_dir + "/" + version + "/rho_stats_" + version + ".fits" for version in versions ] filenames_tau = [ - base_dir+'/'+version+'/tau_stats_'+version+'.fits' for version in versions + base_dir + "/" + version + "/tau_stats_" + version + ".fits" for version in versions ] # %% -#versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha', 'DES'] -#colors = ['blue', 'blueviolet', 'brown', 'black', 'orange'] -#markers = ['o', 'x', 'h', 'd', '*'] +# versions = ['SP_v1.3_LFmask_8k', 'SP_v1.3_LFmask_8k_no_alpha', 'SP_v1.4-P1+3', 'SP_v1.4-P1+3_no_alpha', 'DES'] +# colors = ['blue', 'blueviolet', 'brown', 'black', 'orange'] +# markers = ['o', 'x', 'h', 'd', '*'] -#plot of the xi_sys +# plot of the xi_sys plt.figure() for idx, (ver, color, marker) in enumerate(zip(versions, colors, markers)): print("Plotting version:", ver) - sample = np.load(root_dir+'rho_tau_stats/samples_'+ver+'.npy') - psf_fitter.load_rho_stat('rho_stats_'+ver+'.fits') + sample = np.load(root_dir + "rho_tau_stats/samples_" + ver + ".npy") + psf_fitter.load_rho_stat("rho_stats_" + ver + ".fits") quant = 0.84 - quantiles = [1-quant, quant] - nbins = psf_fitter.rho_stat_handler._treecorr_config['nbins'] + quantiles = [1 - quant, quant] + nbins = psf_fitter.rho_stat_handler._treecorr_config["nbins"] xi_psf_sys_samples = np.array([]).reshape(0, nbins) for i in range(len(sample)): xi_psf_sys = psf_fitter.compute_xi_psf_sys(sample[i]) @@ -332,31 +383,51 @@ xi_psf_sys_mean = np.mean(xi_psf_sys_samples, axis=0) xi_psf_sys_quant = np.quantile(xi_psf_sys_samples, quantiles, axis=0) - theta = psf_fitter.rho_stat_handler.rho_stats['theta'] + theta = psf_fitter.rho_stat_handler.rho_stats["theta"] - if os.path.exists(root_dir+'/xi_pm_'+ver+'.txt'): - xi_plus = fits.getdata(root_dir+'/xi_plus_'+ver+'.fits') - xip = xi_plus['VALUE'] + if os.path.exists(root_dir + "/xi_pm_" + ver + ".txt"): + xi_plus = fits.getdata(root_dir + "/xi_plus_" + ver + ".fits") + xip = xi_plus["VALUE"] gg = treecorr.GGCorrelation(TreeCorrConfig_xi) - gg.read(root_dir+'/xi_pm_'+ver+'.txt') + gg.read(root_dir + "/xi_pm_" + ver + ".txt") varxip = gg.varxip - ratio_mean = xi_psf_sys_mean/np.sqrt(varxip) - ratio_quant = xi_psf_sys_quant/np.sqrt(varxip) - jittered_theta = theta * (1+idx * offset) - - plt.errorbar(jittered_theta, ratio_mean, yerr=[ratio_mean-ratio_quant[0], ratio_quant[1]-ratio_mean], c=color, label=ver, fmt=marker, capsize=5) + ratio_mean = xi_psf_sys_mean / np.sqrt(varxip) + ratio_quant = xi_psf_sys_quant / np.sqrt(varxip) + jittered_theta = theta * (1 + idx * offset) + + plt.errorbar( + jittered_theta, + ratio_mean, + yerr=[ratio_mean - ratio_quant[0], ratio_quant[1] - ratio_mean], + c=color, + label=ver, + fmt=marker, + capsize=5, + ) threshold = 0.50 -plt.fill_between([0.1, 250], [-threshold, -threshold], [threshold, threshold], color='black', alpha=0.1) -plt.plot([0.1, 250], [threshold, threshold], c='black', ls='--', label=str(threshold*100)+'\% level') -plt.plot([0.1, 250], [-threshold, -threshold], c='k', ls='--') -plt.xscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'$\xi_{\rm sys}/\sigma_{\xi_+}$') +plt.fill_between( + [0.1, 250], + [-threshold, -threshold], + [threshold, threshold], + color="black", + alpha=0.1, +) +plt.plot( + [0.1, 250], + [threshold, threshold], + c="black", + ls="--", + label=str(threshold * 100) + r"\% level", +) +plt.plot([0.1, 250], [-threshold, -threshold], c="k", ls="--") +plt.xscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"$\xi_{\rm sys}/\sigma_{\xi_+}$") plt.legend() -plt.title("Systematic error comparison at 68\% level") -plt.savefig('Plots/ratio_varxip_lq_comparison.png', dpi=600) +plt.title(r"Systematic error comparison at 68\% level") +plt.savefig("Plots/ratio_varxip_lq_comparison.png", dpi=600) plt.show() # %% [markdown] @@ -367,14 +438,14 @@ for ver, color in zip(versions, colors): gg = treecorr.GGCorrelation(TreeCorrConfig_xi) - gg.read(root_dir+'/xi_pm_'+ver+'.txt') + gg.read(root_dir + "/xi_pm_" + ver + ".txt") plt.plot(gg.meanr, np.sqrt(gg.varxip), label=ver, color=color) -plt.xscale('log') -plt.yscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'$\sigma_{\xi_+}$') +plt.xscale("log") +plt.yscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"$\sigma_{\xi_+}$") plt.legend() -plt.savefig('Plots/sigma_xip_comparison.png', dpi=600) +plt.savefig("Plots/sigma_xip_comparison.png", dpi=600) plt.show() # %% [markdown] @@ -385,15 +456,17 @@ for ver, color in zip(versions, colors): gg = treecorr.GGCorrelation(TreeCorrConfig_xi) - gg.read(root_dir+'/xi_pm_'+ver+'.txt') - plt.errorbar(gg.meanr, gg.xip, yerr=np.sqrt(gg.varxip), label=ver, color=color, capsize=2) -plt.xscale('log') -plt.yscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'$\xi_+$') + gg.read(root_dir + "/xi_pm_" + ver + ".txt") + plt.errorbar( + gg.meanr, gg.xip, yerr=np.sqrt(gg.varxip), label=ver, color=color, capsize=2 + ) +plt.xscale("log") +plt.yscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"$\xi_+$") plt.title("2-Point correlation functions") plt.legend() -plt.savefig('./Plots/xi_p.png', dpi=600) +plt.savefig("./Plots/xi_p.png", dpi=600) plt.show() # %% @@ -407,19 +480,36 @@ for idx, (ver, color) in enumerate(zip(versions, colors)): gg = treecorr.GGCorrelation(TreeCorrConfig_xi) - gg.read(root_dir+'/xi_pm_'+ver+'.txt') - jittered_theta = gg.meanr * (1+idx * offset) - plt.errorbar(jittered_theta, gg.xip, yerr=np.sqrt(gg.varxip), label=ver, color=color, fmt='o', capsize=2, markersize=2) + gg.read(root_dir + "/xi_pm_" + ver + ".txt") + jittered_theta = gg.meanr * (1 + idx * offset) + plt.errorbar( + jittered_theta, + gg.xip, + yerr=np.sqrt(gg.varxip), + label=ver, + color=color, + fmt="o", + capsize=2, + markersize=2, + ) -#Line to guide the eye +# Line to guide the eye root = "SP_v1.4.5_A" -theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root)) +theta = np.loadtxt(output_folder + "best_fit/{}/shear_xi_plus/theta.txt".format(root)) theta_arcmin = theta * 180 * 60 / np.pi -shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root)) -shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root)) -xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root)) -xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root)) -theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root)) +shear_xi_plus = np.loadtxt( + output_folder + "best_fit/{}/shear_xi_plus/bin_1_1.txt".format(root) +) +shear_xi_minus = np.loadtxt( + output_folder + "best_fit/{}/shear_xi_minus/bin_1_1.txt".format(root) +) +xi_sys_plus = np.loadtxt( + output_folder + "best_fit/{}/xi_sys/shear_xi_plus.txt".format(root) +) +xi_sys_minus = np.loadtxt( + output_folder + "best_fit/{}/xi_sys/shear_xi_minus.txt".format(root) +) +theta_xi_sys = np.loadtxt(output_folder + "best_fit/{}/xi_sys/theta.txt".format(root)) theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi mask = (theta_arcmin > 0.1) & (theta_arcmin < 250) @@ -429,20 +519,29 @@ plt.plot(theta_arcmin[mask], xi_plus_model, color=color, label=root, alpha=0.5) -plt.ylabel(r'$\xi_{+}$', fontsize=26) -plt.xscale('log') -plt.yscale('log') +plt.ylabel(r"$\xi_{+}$", fontsize=26) +plt.xscale("log") +plt.yscale("log") plt.legend(loc="lower left", fontsize=8) plt.subplot(212) for idx, (ver, color) in enumerate(zip(versions, colors)): gg = treecorr.GGCorrelation(TreeCorrConfig_xi) - gg.read(root_dir+'/xi_pm_'+ver+'.txt') - jittered_theta = gg.meanr * (1+idx * offset) - plt.errorbar(jittered_theta, gg.xim, yerr=np.sqrt(gg.varxim), label=ver, color=color, fmt='o', capsize=2, markersize=2) + gg.read(root_dir + "/xi_pm_" + ver + ".txt") + jittered_theta = gg.meanr * (1 + idx * offset) + plt.errorbar( + jittered_theta, + gg.xim, + yerr=np.sqrt(gg.varxim), + label=ver, + color=color, + fmt="o", + capsize=2, + markersize=2, + ) -#Line to guide the eye +# Line to guide the eye mask = (theta_arcmin > 0.1) & (theta_arcmin < 250) xi_minus_model = shear_xi_minus[mask] if add_xi_sys: @@ -450,10 +549,10 @@ plt.plot(theta_arcmin[mask], xi_minus_model, color=color, label=root, alpha=0.5) -plt.xlabel(r'$\theta$ [arcmin]', fontsize=26) -plt.ylabel(r'$\xi_{-}$', fontsize=26) -plt.xscale('log') -plt.yscale('log') +plt.xlabel(r"$\theta$ [arcmin]", fontsize=26) +plt.ylabel(r"$\xi_{-}$", fontsize=26) +plt.xscale("log") +plt.yscale("log") plt.legend(loc="lower left", fontsize=8) plt.show() @@ -461,60 +560,96 @@ # %% -#Inference of the xi_sys parameters -sep_units = 'arcmin' -coord_units = 'degrees' +# Inference of the xi_sys parameters +sep_units = "arcmin" +coord_units = "degrees" theta_min = 0.1 theta_max = 250 nbins = 20 TreeCorrConfig_xi = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'shot', + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, + "var_method": "shot", } -colors = ['brown', 'black', 'blue', 'midnightblue', 'green', 'olive', 'magenta'] +colors = ["brown", "black", "blue", "midnightblue", "green", "olive", "magenta"] # %% offset = 0.005 output_folder = "/n09data/guerrini/output_chains/" add_xi_sys = True -cat_gal = fits.getdata('/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/unions_shapepipe_2024_v1.4.1.fits') +cat_gal = fits.getdata( + "/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/unions_shapepipe_2024_v1.4.1.fits" +) plt.figure(figsize=(15, 15)) plt.subplot(211) for i in tqdm(range(1, 8)): - mask = cat_gal['patch'] != i + mask = cat_gal["patch"] != i gg = treecorr.GGCorrelation(TreeCorrConfig_xi) - cat = treecorr.Catalog(ra=cat_gal['ra'][mask], dec=cat_gal['Dec'][mask], g1=cat_gal['e1'][mask], g2=cat_gal['e2'][mask], w=cat_gal['w'][mask], ra_units='deg', dec_units='deg') + cat = treecorr.Catalog( + ra=cat_gal["ra"][mask], + dec=cat_gal["Dec"][mask], + g1=cat_gal["e1"][mask], + g2=cat_gal["e2"][mask], + w=cat_gal["w"][mask], + ra_units="deg", + dec_units="deg", + ) gg.process(cat) - jittered_theta = gg.meanr * (1+(i-1) * offset) + jittered_theta = gg.meanr * (1 + (i - 1) * offset) plt.subplot(211) - plt.errorbar(jittered_theta, gg.xip, yerr=np.sqrt(gg.varxip), label=f"P{i}", color=colors[i-1], fmt='o', capsize=2, markersize=2) + plt.errorbar( + jittered_theta, + gg.xip, + yerr=np.sqrt(gg.varxip), + label=f"P{i}", + color=colors[i - 1], + fmt="o", + capsize=2, + markersize=2, + ) plt.subplot(212) - plt.errorbar(jittered_theta, gg.xim, yerr=np.sqrt(gg.varxim), label=f"P{i}", color=colors[i-1], fmt='o', capsize=2, markersize=2) + plt.errorbar( + jittered_theta, + gg.xim, + yerr=np.sqrt(gg.varxim), + label=f"P{i}", + color=colors[i - 1], + fmt="o", + capsize=2, + markersize=2, + ) plt.subplot(211) -#Line to guide the eye +# Line to guide the eye root = "SP_v1.4.5_A" -theta = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/theta.txt'.format(root)) +theta = np.loadtxt(output_folder + "best_fit/{}/shear_xi_plus/theta.txt".format(root)) theta_arcmin = theta * 180 * 60 / np.pi -shear_xi_plus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_plus/bin_1_1.txt'.format(root)) -shear_xi_minus = np.loadtxt(output_folder + 'best_fit/{}/shear_xi_minus/bin_1_1.txt'.format(root)) -xi_sys_plus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_plus.txt'.format(root)) -xi_sys_minus = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/shear_xi_minus.txt'.format(root)) -theta_xi_sys = np.loadtxt(output_folder + 'best_fit/{}/xi_sys/theta.txt'.format(root)) +shear_xi_plus = np.loadtxt( + output_folder + "best_fit/{}/shear_xi_plus/bin_1_1.txt".format(root) +) +shear_xi_minus = np.loadtxt( + output_folder + "best_fit/{}/shear_xi_minus/bin_1_1.txt".format(root) +) +xi_sys_plus = np.loadtxt( + output_folder + "best_fit/{}/xi_sys/shear_xi_plus.txt".format(root) +) +xi_sys_minus = np.loadtxt( + output_folder + "best_fit/{}/xi_sys/shear_xi_minus.txt".format(root) +) +theta_xi_sys = np.loadtxt(output_folder + "best_fit/{}/xi_sys/theta.txt".format(root)) theta_xi_sys_arcmin = theta_xi_sys * 180 * 60 / np.pi mask = (theta_arcmin > 0.1) & (theta_arcmin < 250) @@ -522,36 +657,38 @@ if add_xi_sys: xi_plus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_plus) -plt.plot(theta_arcmin[mask], xi_plus_model, color='blue', label=root, alpha=0.5) +plt.plot(theta_arcmin[mask], xi_plus_model, color="blue", label=root, alpha=0.5) -plt.ylabel(r'$\xi_{+}$', fontsize=26) -plt.xscale('log') -plt.yscale('log') +plt.ylabel(r"$\xi_{+}$", fontsize=26) +plt.xscale("log") +plt.yscale("log") plt.legend(loc="lower left", fontsize=8) plt.subplot(212) -#Line to guide the eye +# Line to guide the eye mask = (theta_arcmin > 0.1) & (theta_arcmin < 250) xi_minus_model = shear_xi_minus[mask] if add_xi_sys: xi_minus_model += np.interp(theta_arcmin[mask], theta_xi_sys_arcmin, xi_sys_minus) -plt.plot(theta_arcmin[mask], xi_minus_model, color='blue', label=root, alpha=0.5) +plt.plot(theta_arcmin[mask], xi_minus_model, color="blue", label=root, alpha=0.5) -plt.xlabel(r'$\theta$ [arcmin]', fontsize=26) -plt.ylabel(r'$\xi_{-}$', fontsize=26) -plt.xscale('log') -plt.yscale('log') +plt.xlabel(r"$\theta$ [arcmin]", fontsize=26) +plt.ylabel(r"$\xi_{-}$", fontsize=26) +plt.xscale("log") +plt.yscale("log") plt.legend(loc="lower left", fontsize=8) plt.show() # %% -cat_gal = fits.getdata('/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/unions_shapepipe_2024_v1.4.1.fits') +cat_gal = fits.getdata( + "/n17data/mkilbing/astro/data/CFIS/v1.0/ShapePipe/unions_shapepipe_2024_v1.4.1.fits" +) # %% -cat_gal['patch'] +cat_gal["patch"] # %% theta_min = 0.3 @@ -560,13 +697,13 @@ npatch = 25 TreeCorrConfig_map = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'max_sep': str(theta_max), - 'min_sep': str(theta_min), - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'jackknife', + "ra_units": coord_units, + "dec_units": coord_units, + "max_sep": str(theta_max), + "min_sep": str(theta_min), + "sep_units": sep_units, + "nbins": nbins, + "var_method": "jackknife", } n_bins_map = 15 @@ -575,25 +712,40 @@ # %% gg = treecorr.GGCorrelation(TreeCorrConfig_map) -#plot of the xi_sys +# plot of the xi_sys plt.figure() for ver, color in zip(versions, colors): print("Plotting version:", ver) - gg.read(root_dir+f'xi_for_map2_{ver}.txt') + gg.read(root_dir + f"xi_for_map2_{ver}.txt") mapsq, mapsq_im, mxsq, mxsq_im, varmapsq = gg.calculateMapSq( R=theta_map, - m2_uform='Schneider', + m2_uform="Schneider", + ) + plt.errorbar( + theta_map, + mapsq, + yerr=np.sqrt(varmapsq), + c=color, + label=r"$\langle M^2_{\rm ap} \rangle$ " + ver, + capsize=2, ) - plt.errorbar(theta_map, mapsq, yerr=np.sqrt(varmapsq), c=color, label=r'$\langle M^2_{\rm ap} \rangle$ '+ver, capsize=2) - plt.errorbar(theta_map, np.abs(mxsq), yerr=np.sqrt(varmapsq), c=color, ls='--', label=r'$\langle M^2_\times \rangle$ '+ver, capsize=2) - -plt.xscale('log') -plt.yscale('log') -plt.xlabel(r'$\theta$ (arcmin)') -plt.ylabel(r'dispersion') -plt.title('Mass-aperture') + plt.errorbar( + theta_map, + np.abs(mxsq), + yerr=np.sqrt(varmapsq), + c=color, + ls="--", + label=r"$\langle M^2_\times \rangle$ " + ver, + capsize=2, + ) + +plt.xscale("log") +plt.yscale("log") +plt.xlabel(r"$\theta$ (arcmin)") +plt.ylabel(r"dispersion") +plt.title("Mass-aperture") plt.legend() -plt.savefig('Plots/map_sq_comparison.png', dpi=600) +plt.savefig("Plots/map_sq_comparison.png", dpi=600) plt.show() diff --git a/scratch/kilbinger/analyse_matched_stars_UNIONS_HSC.py b/scratch/kilbinger/analyse_matched_stars_UNIONS_HSC.py index ce6f11ff..6a370b6f 100644 --- a/scratch/kilbinger/analyse_matched_stars_UNIONS_HSC.py +++ b/scratch/kilbinger/analyse_matched_stars_UNIONS_HSC.py @@ -23,16 +23,8 @@ # https://arxiv.org/abs/2108.13045 # %% -import os -import astropy.coordinates as coords from astropy.io import fits -from astropy import units - -from uncertainties import ufloat -import pandas as pd - -import matplotlib.colors as colors from sp_validation.plots import * @@ -40,11 +32,13 @@ # Tolerance: Set in ShapePipe config file of the match_external module. # Largest distance to define matched pair -tol = 0.5 # in arcsec +tol = 0.5 # in arcsec # %% # Open matched catalogue -hdu_list = fits.open('output/run_sp_match_ext/match_external_runner/output/cat_matched-0000000.fits') +hdu_list = fits.open( + "output/run_sp_match_ext/match_external_runner/output/cat_matched-0000000.fits" +) # %% data = hdu_list[1].data @@ -57,21 +51,18 @@ # %% # Use only objects with valid PSF flag -m_flag = (data['FLAG_STAR_HSM'] == 0) +m_flag = data["FLAG_STAR_HSM"] == 0 len(np.where(m_flag)[0]) # %% # Mask out objects with invalid coordinates (due to previous MCCD bug) -m_coord = ( - (data['RA'] != 0) - & (data['DEC'] != 0) -) +m_coord = (data["RA"] != 0) & (data["DEC"] != 0) len(np.where(m_coord)[0]) # %% # Remove unmatched objects which have been marked by -99 # (see ShapePipe config file) -m_match = data['ira'] != -99 +m_match = data["ira"] != -99 len(np.where(m_match)[0]) # %% @@ -84,68 +75,68 @@ ms = [0.2, 0.5] -axes[0].plot(data['RA'][m_coord], data['DEC'][m_coord], '.', markersize=ms[0], label='SP') -axes[1].plot(data['RA'][m_val], data['DEC'][m_val], '.', markersize=ms[1]) +axes[0].plot( + data["RA"][m_coord], data["DEC"][m_coord], ".", markersize=ms[0], label="SP" +) +axes[1].plot(data["RA"][m_val], data["DEC"][m_val], ".", markersize=ms[1]) for idx, ax in enumerate(axes): - ax.plot(data['ira'][m_val], data['idec'][m_val], '.', markersize=ms[idx], label='HSC') + ax.plot( + data["ira"][m_val], data["idec"][m_val], ".", markersize=ms[idx], label="HSC" + ) axes[0].legend(markerscale=30) for ax in axes: - ax.set_xlabel('R.A. [deg]') - ax.set_ylabel('dec [deg]') + ax.set_xlabel("R.A. [deg]") + ax.set_ylabel("dec [deg]") # %% -print(data['distance'].max(), data['distance'][m_val].max()) +print(data["distance"].max(), data["distance"][m_val].max()) # %% -len(np.where(data['distance'] < tol)[0]) +len(np.where(data["distance"] < tol)[0]) # %% -title = 'UNIONS-r HSC PSF stars' +title = "UNIONS-r HSC PSF stars" n_bin = 200 # %% plot_histograms( - [data['distance'], data['distance'][m_val]], - ['all', 'matched'], + [data["distance"], data["distance"][m_val]], + ["all", "matched"], title, - 'distance [arcsec]', - 'frequency', + "distance [arcsec]", + "frequency", [0, 1.5], n_bin, - 'hist_dist', - density=False + "hist_dist", + density=False, ) # %% n_bin = 50 plot_histograms( - [np.log10(data['distance']), np.log10(data['distance'][m_val])], - ['all', 'matched'], + [np.log10(data["distance"]), np.log10(data["distance"][m_val])], + ["all", "matched"], title, - 'log10(distance [arcsec])', - 'frequency', + "log10(distance [arcsec])", + "frequency", [-2, 3], n_bin, - 'log_hist_dist' + "log_hist_dist", ) # %% # Upper limiting distance to define potential match, -tol_up = 3 # in arcsec +tol_up = 3 # in arcsec # %% -m_unmatched = ( - (data['distance'] > tol) - & (data['distance'] < tol_up) - & m_flag -) +m_unmatched = (data["distance"] > tol) & (data["distance"] < tol_up) & m_flag # %% -print(len(data['RA'][m_unmatched]), len(data['RA'][m_val])) +print(len(data["RA"][m_unmatched]), len(data["RA"][m_val])) # %% # Plot spatial distribution @@ -153,17 +144,25 @@ ms = 5 -ax.plot(data['RA'][m_unmatched], data['DEC'][m_unmatched], '.', markersize=ms, label='unmatched') -ax.plot(data['ira'][m_val], data['idec'][m_val], '.', markersize=ms, label='matched') +ax.plot( + data["RA"][m_unmatched], + data["DEC"][m_unmatched], + ".", + markersize=ms, + label="unmatched", +) +ax.plot(data["ira"][m_val], data["idec"][m_val], ".", markersize=ms, label="matched") ax.legend(markerscale=3) -ax.set_xlabel('R.A. [deg]') -ax.set_ylabel('dec [deg]') +ax.set_xlabel("R.A. [deg]") +ax.set_ylabel("dec [deg]") # %% # Open original HSC star catalogue -hdu_list_hsc = fits.open('/n17data/mkilbing/astro/data/CFIS/imaging_surveys/HECTOMAP_stars.fits') +hdu_list_hsc = fits.open( + "/n17data/mkilbing/astro/data/CFIS/imaging_surveys/HECTOMAP_stars.fits" +) # %% data_hsc = hdu_list_hsc[1].data @@ -172,33 +171,33 @@ data_hsc.dtype.names # %% -ra_min = data['RA'][m_val].min() -ra_max = data['RA'][m_val].max() -dec_min = data['DEC'][m_val].min() -dec_max = data['DEC'][m_val].max() +ra_min = data["RA"][m_val].min() +ra_max = data["RA"][m_val].max() +dec_min = data["DEC"][m_val].min() +dec_max = data["DEC"][m_val].max() -print('UNIONS coordinate limits') +print("UNIONS coordinate limits") print(ra_min, ra_max, dec_min, dec_max) -ira_min = data_hsc['ira'].min() -ira_max = data_hsc['ira'].max() -idec_min = data_hsc['idec'].min() -idec_max = data_hsc['idec'].max() +ira_min = data_hsc["ira"].min() +ira_max = data_hsc["ira"].max() +idec_min = data_hsc["idec"].min() +idec_max = data_hsc["idec"].max() -print('HSC coordinate limits') +print("HSC coordinate limits") print(ira_min, ira_max, idec_min, idec_max) # Mask HSC stars in area of UNIONS - HSC match (rough estimate) m_area = ( - (data_hsc['ira'] > ra_min) - & (data_hsc['ira'] < ra_max) - & (data_hsc['idec'] > dec_min) - & (data_hsc['idec'] < dec_max) + (data_hsc["ira"] > ra_min) + & (data_hsc["ira"] < ra_max) + & (data_hsc["idec"] > dec_min) + & (data_hsc["idec"] < dec_max) ) # %% -print(len(data_hsc['ira']), len(data_hsc['ira'][m_area])) +print(len(data_hsc["ira"]), len(data_hsc["ira"][m_area])) # %% # Plot spatial distribution @@ -206,36 +205,42 @@ ms = 1 -prop_cycle = plt.rcParams['axes.prop_cycle'] -colors = prop_cycle.by_key()['color'] +prop_cycle = plt.rcParams["axes.prop_cycle"] +colors = prop_cycle.by_key()["color"] -ax.plot(data_hsc['ira'][m_area], data_hsc['idec'][m_area], '.', markersize=ms, color=colors[1]) -ax.plot(data_hsc['ira'], data_hsc['idec'], '.', markersize=ms, color=colors[2]) +ax.plot( + data_hsc["ira"][m_area], + data_hsc["idec"][m_area], + ".", + markersize=ms, + color=colors[1], +) +ax.plot(data_hsc["ira"], data_hsc["idec"], ".", markersize=ms, color=colors[2]) -ax.set_title('HSC-SSP stars') -ax.set_xlabel('R.A. [deg]') -ax.set_ylabel('dec [deg]') +ax.set_title("HSC-SSP stars") +ax.set_xlabel("R.A. [deg]") +ax.set_ylabel("dec [deg]") # %% -ids_matched = data['object_id'][m_val] +ids_matched = data["object_id"][m_val] # %% -m_ids_matched = np.in1d(data_hsc['object_id'], data['object_id'][m_val]) +m_ids_matched = np.in1d(data_hsc["object_id"], data["object_id"][m_val]) # %% n_bin = 50 -title = 'HSC stars' +title = "HSC stars" plot_histograms( - [data_hsc['imag_psf'][m_area], data_hsc['imag_psf'][m_ids_matched]], - ['all HSC', 'matched to UNIONS'], + [data_hsc["imag_psf"][m_area], data_hsc["imag_psf"][m_ids_matched]], + ["all HSC", "matched to UNIONS"], title, - '$i$', - 'frequency', + "$i$", + "frequency", [18, 22.5], n_bin, - 'mag_i_hsc', - density=True + "mag_i_hsc", + density=True, ) # %% diff --git a/scratch/kilbinger/demo_binned_mask.py b/scratch/kilbinger/demo_binned_mask.py index a8f5bae2..9d80f78e 100644 --- a/scratch/kilbinger/demo_binned_mask.py +++ b/scratch/kilbinger/demo_binned_mask.py @@ -48,7 +48,9 @@ # %% Mask directory and aux mask file(s) hsp_obj._params["mask_dir"] = f"{os.environ['HOME']}/masks" -hsp_obj._params["aux_mask_files"] = f"{hsp_obj._params['mask_dir']}/mask_r_nside131072_npoint.hsp" +hsp_obj._params["aux_mask_files"] = ( + f"{hsp_obj._params['mask_dir']}/mask_r_nside131072_npoint.hsp" +) hsp_obj._params["aux_mask_labels"] = "npoint3" hsp_obj._params["verbose"] = True @@ -56,11 +58,11 @@ # Masks to use # Bits -hsp_obj._params["bits"] = 1+2+4+8+64+1024 +hsp_obj._params["bits"] = 1 + 2 + 4 + 8 + 64 + 1024 # Index of base mask (e.g., coverage mask) label_base_mask = "npoint3" -#label_base_mask = "" +# label_base_mask = "" # Names masks_to_apply = [ @@ -75,17 +77,12 @@ # Load and initialise masks masks, labels = sp_joint.get_masks_from_config( - config, - dat, - dat_ext, - masks_to_apply=masks_to_apply, - verbose=True + config, dat, dat_ext, masks_to_apply=masks_to_apply, verbose=True ) # %% # if base mask: apply to all other masks if label_base_mask != "": - print(f"Applying base mask {label_base_mask} to all other masks:") # Find base mask @@ -98,7 +95,12 @@ for mask in masks: if mask._col_name != label_base_mask: mask._mask = mask._mask & base_mask - print(mask._col_name, "#True = ", sum(mask._mask), f"({100*sum(mask._mask)/len(mask._mask):.2f}%)") + print( + mask._col_name, + "#True = ", + sum(mask._mask), + f"({100 * sum(mask._mask) / len(mask._mask):.2f}%)", + ) else: print("No base mask applied.") @@ -106,12 +108,13 @@ # Bin data # Create array of nsides between min and max with powers of two -nsides = np.array([2**i for i in range(int(np.log2(nside_min)), int(np.log2(nside_max))+1)]) +nsides = np.array( + [2**i for i in range(int(np.log2(nside_min)), int(np.log2(nside_max)) + 1)] +) # %% areas_deg2 = {} for mask in masks: - print(f"Mask: {mask._col_name}") areas_deg2[mask._col_name] = {} @@ -122,10 +125,10 @@ dec = dat[key_dec][m] for nside in nsides: + area_deg2 = cs_cat.get_binned_area(ra, dec, nside=nside) + print(f"binned area (nside={nside}) ≈ {area_deg2:.3f} deg^2") + areas_deg2[mask._col_name][nside] = area_deg2 - area_deg2 = cs_cat.get_binned_area(ra, dec, nside=nside) - print(f"binned area (nside={nside}) ≈ {area_deg2:.3f} deg^2") - areas_deg2[mask._col_name][nside] = area_deg2 # %% def save_areas(areas_deg2, filename): @@ -139,7 +142,7 @@ def save_areas(areas_deg2, filename): filename : str Output filename. """ - with open(filename, 'w') as f: + with open(filename, "w") as f: # Write header f.write("# Mask_name nside area_deg2\n") for mask_name, area_dict in areas_deg2.items(): @@ -148,6 +151,7 @@ def save_areas(areas_deg2, filename): print(f"Wrote areas to {filename}") + # %% # %% @@ -177,9 +181,17 @@ def save_areas(areas_deg2, filename): # %% # Plot areas -markers = ['o', 's', '^', 'v', 'D', 'x', '*'] -ls = ['dashed', 'dotted', 'dashdot', 'solid', (0, (3, 1, 1, 1)), (0, (5, 10)), (0, (1, 10))] -fig, ax = plt.subplots(figsize=(8,8)) +markers = ["o", "s", "^", "v", "D", "x", "*"] +ls = [ + "dashed", + "dotted", + "dashdot", + "solid", + (0, (3, 1, 1, 1)), + (0, (5, 10)), + (0, (1, 10)), +] +fig, ax = plt.subplots(figsize=(8, 8)) plt.xticks(nsides, nsides) for idx, mask in enumerate(masks): label = mask._col_name @@ -190,9 +202,9 @@ def save_areas(areas_deg2, filename): ax.plot(nsides * dx, areas, marker=markers[idx], label=label, linestyle=ls[idx]) ax.hlines( area_wcov_deg2[label], - nsides[0]*0.8, - nsides[-1]*1.1, - colors='C'+str(idx), + nsides[0] * 0.8, + nsides[-1] * 1.1, + colors="C" + str(idx), linestyles=ls[idx], ) plt.legend() diff --git a/scratch/kilbinger/plot_binned_quantities.py b/scratch/kilbinger/plot_binned_quantities.py index 05f5b145..4c9559c3 100644 --- a/scratch/kilbinger/plot_binned_quantities.py +++ b/scratch/kilbinger/plot_binned_quantities.py @@ -19,7 +19,7 @@ ## %% ## enable inline plotting for interactive sessions ## (must be done *after* importing package that sets agg backend) -#if ipython is not None: +# if ipython is not None: # print("matplotlib inline") # ipython.run_line_magic("matplotlib", "inline") @@ -28,14 +28,25 @@ quantities = {} # %% -keys = ["number", "response", "leakage", "w_iv", "mag", "NGMIX_Tpsf_NOSHEAR", "N_EPOCH", "e1_PSF", "e2_PSF", "fwhm_PSF"] +keys = [ + "number", + "response", + "leakage", + "w_iv", + "mag", + "NGMIX_Tpsf_NOSHEAR", + "N_EPOCH", + "e1_PSF", + "e2_PSF", + "fwhm_PSF", +] for key in keys: fname = f"{key}_binned.npz" result = io.read_binned_quantity(fname) for xy in result: - if xy != "quantity": + if xy != "quantity": bin_edges[xy] = result[xy] quantities[key] = result["quantity"] @@ -48,10 +59,9 @@ } - # %% vmin = {"diag": -0.2, "offdiag": -0.1} -vmax = {"diag": 1.2, "offdiag": 0.1} +vmax = {"diag": 1.2, "offdiag": 0.1} plots.plot_binned( quantities, diff --git a/scripts/apply_alpha.py b/scripts/apply_alpha.py index fc88ba51..e363501b 100755 --- a/scripts/apply_alpha.py +++ b/scripts/apply_alpha.py @@ -9,22 +9,15 @@ """ -import sys import pickle - +import sys from optparse import OptionParser -from lmfit import Parameters from astropy.io import fits -from astropy.table import Table, Column - +from astropy.table import Column, Table from cs_util import logging - from shear_psf_leakage.leakage import func_bias_2d -from sp_validation import catalog as cat -from sp_validation import format - def params_default(): """Params Default @@ -90,10 +83,7 @@ def parse_options(p_def): dest="e1_col", default=p_def["e1_col"], type="string", - help=( - "e1 column name in galaxy catalogue," - + f" default='{p_def['e1_col']}'" - ), + help=("e1 column name in galaxy catalogue," + f" default='{p_def['e1_col']}'"), ) parser.add_option( "", @@ -101,10 +91,7 @@ def parse_options(p_def): dest="e2_col", default=p_def["e2_col"], type="string", - help=( - "e2 column name in galaxy catalogue," - + f" default='{p_def['e2_col']}'" - ), + help=("e2 column name in galaxy catalogue," + f" default='{p_def['e2_col']}'"), ) parser.add_option( "", @@ -198,9 +185,7 @@ def main(argv=None): t = Table(dat_shear) t.add_column(Column(name="e1_cor", data=e1_cor)) t.add_column(Column(name="e2_cor", data=e2_cor)) - print( - "Saving alpha-corrected ellipticities to" + f' {params["output_path"]}' - ) + print("Saving alpha-corrected ellipticities to" + f" {params['output_path']}") t.write(params["output_path"], overwrite=True, format="fits") return 0 diff --git a/scripts/apply_alpha_snr_size_bin.py b/scripts/apply_alpha_snr_size_bin.py index 47215c53..abb541bf 100644 --- a/scripts/apply_alpha_snr_size_bin.py +++ b/scripts/apply_alpha_snr_size_bin.py @@ -9,24 +9,18 @@ """ import sys -import pickle - -from optparse import OptionParser +import time +from optparse import OptionParser -from lmfit import Parameters -from astropy.io import fits -from astropy.table import Table, Column import matplotlib.pyplot as plt - -import statsmodels.api as sm - -from cs_util import logging - import numpy as np import pandas as pd - +import statsmodels.api as sm +from astropy.io import fits +from astropy.table import Column, Table +from cs_util import logging from tqdm import tqdm -import time + def params_default(): """Params Default @@ -40,145 +34,137 @@ def params_default(): """ param = { - 'e1_col': 'e1', - 'e2_col': 'e2', - 'e1_PSF_col': 'e1_PSF', - 'e2_PSF_col': 'e2_PSF', - 'snr_col': 'snr', - 'w_col': 'w', - 'TPSF_col': 'NGMIX_Tpsf_NOSHEAR', - 'T_col': 'NGMIX_T_NOSHEAR', - 'input_path_shear': 'SP/unions_shapepipe_extended_2022_v1.0.fits', - 'output_path': 'shape_cat_cor_alpha.fits', - 'plot_path': None + "e1_col": "e1", + "e2_col": "e2", + "e1_PSF_col": "e1_PSF", + "e2_PSF_col": "e2_PSF", + "snr_col": "snr", + "w_col": "w", + "TPSF_col": "NGMIX_Tpsf_NOSHEAR", + "T_col": "NGMIX_T_NOSHEAR", + "input_path_shear": "SP/unions_shapepipe_extended_2022_v1.0.fits", + "output_path": "shape_cat_cor_alpha.fits", + "plot_path": None, } return param -def parse_options(p_def): +def parse_options(p_def): """Parse Options Parse command line options and update parameter values accordingly. - + Parameters - ---------- + ---------- p_def: dict - default parameter values - - Returns - ------- - tuple - Command line options - string - Command line string - - """ - usage = "%prog [OPTIONS]" + default parameter values + + Returns + ------- + tuple + Command line options + string + Command line string + + """ + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) - parser.add_option( - '-i', - '--input_path_shear', - dest='input_path_shear', - default=p_def['input_path_shear'], - type='string', - help='input path of the extended shear catalogue' + parser.add_option( + "-i", + "--input_path_shear", + dest="input_path_shear", + default=p_def["input_path_shear"], + type="string", + help="input path of the extended shear catalogue", ) - parser.add_option( - '', - '--e1_col', - dest='e1_col', - default=p_def['e1_col'], - type='string', - help=( - 'e1 column name in galaxy catalogue,' - + f' default=\'{p_def["e1_col"]}\'' - ), - ) - parser.add_option( - '', - '--e2_col', - dest='e2_col', - default=p_def['e2_col'], - type='string', - help=( - 'e2 column name in galaxy catalogue,' - + f' default=\'{p_def["e2_col"]}\'' - ), - ) - parser.add_option( - '', - '--e1_PSF_col', - dest='e1_PSF_col', - default=p_def['e1_PSF_col'], - type='string', - help=f'PSF e1 column name, default=\'{p_def["e1_PSF_col"]}\'' - ) - parser.add_option( - '', - '--e2_PSF_col', - dest='e2_PSF_col', - default=p_def['e2_PSF_col'], - type='string', - help=f'PSF e2 column name, default=\'{p_def["e2_PSF_col"]}\'' + parser.add_option( + "", + "--e1_col", + dest="e1_col", + default=p_def["e1_col"], + type="string", + help=("e1 column name in galaxy catalogue," + f" default='{p_def['e1_col']}'"), ) - parser.add_option( - '', - '--snr_col', - dest='snr_col', - default=p_def['snr_col'], - type='string', - help=f'SNR column name, default=\'{p_def["snr_col"]}\'' + parser.add_option( + "", + "--e2_col", + dest="e2_col", + default=p_def["e2_col"], + type="string", + help=("e2 column name in galaxy catalogue," + f" default='{p_def['e2_col']}'"), ) - parser.add_option( - '', - '--w_col', - dest='w_col', - default=p_def['w_col'], - type='string', - help=f'w column name, default=\'{p_def["w_col"]}\'' + parser.add_option( + "", + "--e1_PSF_col", + dest="e1_PSF_col", + default=p_def["e1_PSF_col"], + type="string", + help=f"PSF e1 column name, default='{p_def['e1_PSF_col']}'", ) - parser.add_option( - '', - '--TPSF_col', - dest='TPSF_col', - default=p_def['TPSF_col'], - type='string', - help=f'T PSF column name, default=\'{p_def["TPSF_col"]}\'' + parser.add_option( + "", + "--e2_PSF_col", + dest="e2_PSF_col", + default=p_def["e2_PSF_col"], + type="string", + help=f"PSF e2 column name, default='{p_def['e2_PSF_col']}'", ) - parser.add_option( - '', - '--T_col', - dest='T_col', - default=p_def['T_col'], - type='string', - help=f'T_gal column name, default=\'{p_def["T_col"]}\'' + parser.add_option( + "", + "--snr_col", + dest="snr_col", + default=p_def["snr_col"], + type="string", + help=f"SNR column name, default='{p_def['snr_col']}'", ) - parser.add_option( - '-o', - '--output_path', - dest='output_path', - default=p_def['output_path'], - type='string', - help=( - 'output path of the extended shear catalogue,' - + f' default=\'{p_def["output_path"]}\'' - ) + parser.add_option( + "", + "--w_col", + dest="w_col", + default=p_def["w_col"], + type="string", + help=f"w column name, default='{p_def['w_col']}'", ) parser.add_option( - '-p', - '--plot_path', - dest='plot_path', - default=p_def['plot_path'], - type='string', + "", + "--TPSF_col", + dest="TPSF_col", + default=p_def["TPSF_col"], + type="string", + help=f"T PSF column name, default='{p_def['TPSF_col']}'", + ) + parser.add_option( + "", + "--T_col", + dest="T_col", + default=p_def["T_col"], + type="string", + help=f"T_gal column name, default='{p_def['T_col']}'", + ) + parser.add_option( + "-o", + "--output_path", + dest="output_path", + default=p_def["output_path"], + type="string", help=( - 'output path of the plots,' + f' default=\'{p_def["plot_path"]}\'' - ) + "output path of the extended shear catalogue," + + f" default='{p_def['output_path']}'" + ), + ) + parser.add_option( + "-p", + "--plot_path", + dest="plot_path", + default=p_def["plot_path"], + type="string", + help=("output path of the plots," + f" default='{p_def['plot_path']}'"), ) - options, args = parser.parse_args() - + options, args = parser.parse_args() + return options, args @@ -196,345 +182,392 @@ def main(argv=None): logging.log_command(argv) - cat_gal = fits.getdata(params['input_path_shear']) + cat_gal = fits.getdata(params["input_path_shear"]) - #check consistency of the names of columns - assert (params['e1_col'] in cat_gal.columns.names), ( - f'Column {params["e1_col"]} not found in galaxy catalogue' + # check consistency of the names of columns + assert params["e1_col"] in cat_gal.columns.names, ( + f"Column {params['e1_col']} not found in galaxy catalogue" ) - assert (params['e2_col'] in cat_gal.columns.names), ( - f'Column {params["e2_col"]} not found in galaxy catalogue' + assert params["e2_col"] in cat_gal.columns.names, ( + f"Column {params['e2_col']} not found in galaxy catalogue" ) - assert (params['e1_PSF_col'] in cat_gal.columns.names), ( - f'Column {params["e1_PSF_col"]} not found in galaxy catalogue' + assert params["e1_PSF_col"] in cat_gal.columns.names, ( + f"Column {params['e1_PSF_col']} not found in galaxy catalogue" ) - assert (params['e2_PSF_col'] in cat_gal.columns.names), ( - f'Column {params["e2_PSF_col"]} not found in galaxy catalogue' + assert params["e2_PSF_col"] in cat_gal.columns.names, ( + f"Column {params['e2_PSF_col']} not found in galaxy catalogue" ) - assert (params['snr_col'] in cat_gal.columns.names), ( - f'Column {params["snr_col"]} not found in galaxy catalogue' + assert params["snr_col"] in cat_gal.columns.names, ( + f"Column {params['snr_col']} not found in galaxy catalogue" ) - assert (params['w_col'] in cat_gal.columns.names), ( - f'Column {params["w_col"]} not found in galaxy catalogue' + assert params["w_col"] in cat_gal.columns.names, ( + f"Column {params['w_col']} not found in galaxy catalogue" ) - assert (params['TPSF_col'] in cat_gal.columns.names), ( - f'Column {params["TPSF_col"]} not found in galaxy catalogue' + assert params["TPSF_col"] in cat_gal.columns.names, ( + f"Column {params['TPSF_col']} not found in galaxy catalogue" ) - assert (params['T_col'] in cat_gal.columns.names), ( - f'Column {params["T_col"]} not found in galaxy catalogue' + assert params["T_col"] in cat_gal.columns.names, ( + f"Column {params['T_col']} not found in galaxy catalogue" ) - col_snr = params['snr_col'] - col_w = params['w_col'] - col_TPSF = params['TPSF_col'] - col_T = params['T_col'] - col_e1 = params['e1_col'] - col_e2 = params['e2_col'] - col_e1_PSF = params['e1_PSF_col'] - col_e2_PSF = params['e2_PSF_col'] - - ratio_size = cat_gal[col_TPSF]/(cat_gal[col_TPSF] + cat_gal[col_T]) - - #Create a dataframe - df_gal = pd.DataFrame(np.array([ - np.array(cat_gal[col_e1], dtype=np.float32), - np.array(cat_gal[col_e2], dtype=np.float32), - np.array(cat_gal[col_e1_PSF], dtype=np.float32), - np.array(cat_gal[col_e2_PSF], dtype=np.float32), - np.array(cat_gal[col_snr], dtype=np.float32), - np.array(cat_gal[col_w], dtype=np.float32), - np.array(ratio_size, dtype=np.float32) - ]).T, columns=['e1', 'e2', 'e1_PSF', 'e2_PSF', 'snr', 'w', 'ratio_size']) + col_snr = params["snr_col"] + col_w = params["w_col"] + col_TPSF = params["TPSF_col"] + col_T = params["T_col"] + col_e1 = params["e1_col"] + col_e2 = params["e2_col"] + col_e1_PSF = params["e1_PSF_col"] + col_e2_PSF = params["e2_PSF_col"] + + ratio_size = cat_gal[col_TPSF] / (cat_gal[col_TPSF] + cat_gal[col_T]) + + # Create a dataframe + df_gal = pd.DataFrame( + np.array( + [ + np.array(cat_gal[col_e1], dtype=np.float32), + np.array(cat_gal[col_e2], dtype=np.float32), + np.array(cat_gal[col_e1_PSF], dtype=np.float32), + np.array(cat_gal[col_e2_PSF], dtype=np.float32), + np.array(cat_gal[col_snr], dtype=np.float32), + np.array(cat_gal[col_w], dtype=np.float32), + np.array(ratio_size, dtype=np.float32), + ] + ).T, + columns=["e1", "e2", "e1_PSF", "e2_PSF", "snr", "w", "ratio_size"], + ) n_bins_snr = 20 n_bins_r = 20 print("Performing the binning of the catalog in size and SNR...") - #Bin in size and snr - df_gal.loc[:, 'bin_R'] = pd.qcut(df_gal['ratio_size'], n_bins_r, labels=False, retbins=False) + # Bin in size and snr + df_gal.loc[:, "bin_R"] = pd.qcut( + df_gal["ratio_size"], n_bins_r, labels=False, retbins=False + ) - #initialize bin snr - df_gal.loc[:, 'bin_snr'] = -999 + # initialize bin snr + df_gal.loc[:, "bin_snr"] = -999 for ibin_r in tqdm(range(n_bins_r)): - #select galaxies in the bin - mask_binr = df_gal['bin_R'].values == ibin_r + # select galaxies in the bin + mask_binr = df_gal["bin_R"].values == ibin_r - #bin in snr - df_gal.loc[mask_binr, 'bin_snr'] = pd.qcut(df_gal.loc[mask_binr, 'snr'], n_bins_snr, labels=False, retbins=False) + # bin in snr + df_gal.loc[mask_binr, "bin_snr"] = pd.qcut( + df_gal.loc[mask_binr, "snr"], n_bins_snr, labels=False, retbins=False + ) - #group by bin - df_gal_grouped = df_gal.groupby(['bin_R', 'bin_snr']) + # group by bin + df_gal_grouped = df_gal.groupby(["bin_R", "bin_snr"]) ngroups = df_gal_grouped.ngroups print("Performing first round calibration...") start = time.time() - alpha_df = pd.DataFrame(0., - index = np.arange(ngroups), - columns=['R', 'SNR', - 'alpha_1', 'alpha_1_err', - 'alpha_2', 'alpha_2_err']) + alpha_df = pd.DataFrame( + 0.0, + index=np.arange(ngroups), + columns=["R", "SNR", "alpha_1", "alpha_1_err", "alpha_2", "alpha_2_err"], + ) i_group = 0 for name, group in df_gal_grouped: - - #Save weighted average - alpha_df.loc[i_group, 'R'] = np.average(group['ratio_size'], weights=group['w']) - alpha_df.loc[i_group, 'SNR'] = np.average(group['snr'], weights=group['w']) - - #Fit linear model to compute alpha - e1_out = np.array(group['e1']) - e2_out = np.array(group['e2']) - weight_out = np.array(group['w']) - e1_PSF = np.array(group['e1_PSF']) - e2_PSF = np.array(group['e2_PSF']) + # Save weighted average + alpha_df.loc[i_group, "R"] = np.average(group["ratio_size"], weights=group["w"]) + alpha_df.loc[i_group, "SNR"] = np.average(group["snr"], weights=group["w"]) + + # Fit linear model to compute alpha + e1_out = np.array(group["e1"]) + e2_out = np.array(group["e2"]) + weight_out = np.array(group["w"]) + e1_PSF = np.array(group["e1_PSF"]) + e2_PSF = np.array(group["e2_PSF"]) del group - #Fit e1 + # Fit e1 mod_wls = sm.WLS(e1_out, sm.add_constant(e1_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_1'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_1_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_1"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_1_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del res_wls, mod_wls - #Fit e2 + # Fit e2 mod_wls = sm.WLS(e2_out, sm.add_constant(e2_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_2'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_2_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_2"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_2_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del weight_out, res_wls, mod_wls i_group += 1 - #Fit polynomial to remove general trend - fitting_weight = 1./np.square(alpha_df['alpha_1_err'].values) - A = np.vstack([fitting_weight*1, - fitting_weight*np.power(alpha_df['SNR'].values, -2), - fitting_weight*np.power(alpha_df['SNR'].values, -3), - fitting_weight*alpha_df['R'].values, - fitting_weight*alpha_df['R'].values*np.power(alpha_df['SNR'].values, -2)]).T - poly1 = np.linalg.lstsq(A, fitting_weight*alpha_df['alpha_1'].values, rcond=None)[0] + # Fit polynomial to remove general trend + fitting_weight = 1.0 / np.square(alpha_df["alpha_1_err"].values) + A = np.vstack( + [ + fitting_weight * 1, + fitting_weight * np.power(alpha_df["SNR"].values, -2), + fitting_weight * np.power(alpha_df["SNR"].values, -3), + fitting_weight * alpha_df["R"].values, + fitting_weight + * alpha_df["R"].values + * np.power(alpha_df["SNR"].values, -2), + ] + ).T + poly1 = np.linalg.lstsq(A, fitting_weight * alpha_df["alpha_1"].values, rcond=None)[ + 0 + ] del fitting_weight, A - fitting_weight = 1./np.square(alpha_df['alpha_2_err'].values) - A = np.vstack([fitting_weight*1, - fitting_weight*np.power(alpha_df['SNR'].values, -2), - fitting_weight*np.power(alpha_df['SNR'].values, -3), - fitting_weight*alpha_df['R'].values, - fitting_weight*alpha_df['R'].values*np.power(alpha_df['SNR'].values, -2)]).T - poly2 = np.linalg.lstsq(A, fitting_weight*alpha_df['alpha_2'].values, rcond=None)[0] + fitting_weight = 1.0 / np.square(alpha_df["alpha_2_err"].values) + A = np.vstack( + [ + fitting_weight * 1, + fitting_weight * np.power(alpha_df["SNR"].values, -2), + fitting_weight * np.power(alpha_df["SNR"].values, -3), + fitting_weight * alpha_df["R"].values, + fitting_weight + * alpha_df["R"].values + * np.power(alpha_df["SNR"].values, -2), + ] + ).T + poly2 = np.linalg.lstsq(A, fitting_weight * alpha_df["alpha_2"].values, rcond=None)[ + 0 + ] del fitting_weight, A - #Compute alpha to remove the general trend - #e1 - alpha = poly1[0] \ - + poly1[1] * np.power(df_gal['snr'], -2) \ - + poly1[2] * np.power(df_gal['snr'], -3) \ - + poly1[3] * df_gal['ratio_size'] \ - + poly1[4] * df_gal['ratio_size'] * np.power(df_gal['snr'], -2) - df_gal.loc[:, 'e1_cor'] = df_gal['e1'].values - alpha * df_gal['e1_PSF'].values + # Compute alpha to remove the general trend + # e1 + alpha = ( + poly1[0] + + poly1[1] * np.power(df_gal["snr"], -2) + + poly1[2] * np.power(df_gal["snr"], -3) + + poly1[3] * df_gal["ratio_size"] + + poly1[4] * df_gal["ratio_size"] * np.power(df_gal["snr"], -2) + ) + df_gal.loc[:, "e1_cor"] = df_gal["e1"].values - alpha * df_gal["e1_PSF"].values del alpha, poly1 - #e2 - alpha = poly2[0] \ - + poly2[1] * np.power(df_gal['snr'], -2) \ - + poly2[2] * np.power(df_gal['snr'], -3) \ - + poly2[3] * df_gal['ratio_size'] \ - + poly2[4] * df_gal['ratio_size'] * np.power(df_gal['snr'], -2) - df_gal.loc[:, 'e2_cor'] = df_gal['e2'].values - alpha * df_gal['e2_PSF'].values + # e2 + alpha = ( + poly2[0] + + poly2[1] * np.power(df_gal["snr"], -2) + + poly2[2] * np.power(df_gal["snr"], -3) + + poly2[3] * df_gal["ratio_size"] + + poly2[4] * df_gal["ratio_size"] * np.power(df_gal["snr"], -2) + ) + df_gal.loc[:, "e2_cor"] = df_gal["e2"].values - alpha * df_gal["e2_PSF"].values del alpha, poly2 - print(f'First round calibration took {time.time()-start} seconds') + print(f"First round calibration took {time.time() - start} seconds") print("Performing second round of calibration...") start = time.time() - #Initialise second run of calibration - df_gal.loc[:, 'e1_cor_2'] = -999. - df_gal.loc[:, 'e2_cor_2'] = -999. - alpha_df.loc[:, 'alpha_1_corr'] = -999. - alpha_df.loc[:, 'alpha_2_corr'] = -999. - alpha_df.loc[:, 'alpha_1_corr_err'] = -999. - alpha_df.loc[:, 'alpha_2_corr_err'] = -999. - - #Second run of calibration - for i_group in range(n_bins_r*n_bins_snr): - #Get the mask - mask_group = (df_gal['bin_R'].values == i_group//n_bins_snr) & (df_gal['bin_snr'].values == i_group%n_bins_snr) - #Fit linear model to compute alpha - e1_out = np.array(df_gal.loc[mask_group, 'e1_cor']) - e2_out = np.array(df_gal.loc[mask_group, 'e2_cor']) - weight_out = np.array(df_gal.loc[mask_group, 'w']) - e1_PSF = np.array(df_gal.loc[mask_group, 'e1_PSF']) - e2_PSF = np.array(df_gal.loc[mask_group, 'e2_PSF']) - - #Fit e1 + # Initialise second run of calibration + df_gal.loc[:, "e1_cor_2"] = -999.0 + df_gal.loc[:, "e2_cor_2"] = -999.0 + alpha_df.loc[:, "alpha_1_corr"] = -999.0 + alpha_df.loc[:, "alpha_2_corr"] = -999.0 + alpha_df.loc[:, "alpha_1_corr_err"] = -999.0 + alpha_df.loc[:, "alpha_2_corr_err"] = -999.0 + + # Second run of calibration + for i_group in range(n_bins_r * n_bins_snr): + # Get the mask + mask_group = (df_gal["bin_R"].values == i_group // n_bins_snr) & ( + df_gal["bin_snr"].values == i_group % n_bins_snr + ) + # Fit linear model to compute alpha + e1_out = np.array(df_gal.loc[mask_group, "e1_cor"]) + e2_out = np.array(df_gal.loc[mask_group, "e2_cor"]) + weight_out = np.array(df_gal.loc[mask_group, "w"]) + e1_PSF = np.array(df_gal.loc[mask_group, "e1_PSF"]) + e2_PSF = np.array(df_gal.loc[mask_group, "e2_PSF"]) + + # Fit e1 mod_wls = sm.WLS(e1_out, sm.add_constant(e1_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_1_corr'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_1_corr_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_1_corr"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_1_corr_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del res_wls, mod_wls - #Fit e2 + # Fit e2 mod_wls = sm.WLS(e2_out, sm.add_constant(e2_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_2_corr'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_2_corr_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_2_corr"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_2_corr_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del weight_out, res_wls, mod_wls - df_gal.loc[mask_group, 'e1_cor_2'] = e1_out - alpha_df.loc[i_group, 'alpha_1_corr'] * e1_PSF - df_gal.loc[mask_group, 'e2_cor_2'] = e2_out - alpha_df.loc[i_group, 'alpha_2_corr'] * e2_PSF + df_gal.loc[mask_group, "e1_cor_2"] = ( + e1_out - alpha_df.loc[i_group, "alpha_1_corr"] * e1_PSF + ) + df_gal.loc[mask_group, "e2_cor_2"] = ( + e2_out - alpha_df.loc[i_group, "alpha_2_corr"] * e2_PSF + ) - print(f'Second round calibration took {time.time()-start} seconds') - #Save the corrected ellipticities + print(f"Second round calibration took {time.time() - start} seconds") + # Save the corrected ellipticities t = Table(cat_gal) - t.add_column(Column(name='e1_cor', data=df_gal['e1_cor'].values)) - t.add_column(Column(name='e2_cor', data=df_gal['e2_cor'].values)) - t.add_column(Column(name='e1_cor_2', data=df_gal['e1_cor_2'].values)) - t.add_column(Column(name='e2_cor_2', data=df_gal['e2_cor_2'].values)) - print( - 'Saving alpha-corrected ellipticities to' + - f' {params["output_path"]}' - ) - t.write(params['output_path'], overwrite=True, format='fits') - - - if params['plot_path'] is not None: - + t.add_column(Column(name="e1_cor", data=df_gal["e1_cor"].values)) + t.add_column(Column(name="e2_cor", data=df_gal["e2_cor"].values)) + t.add_column(Column(name="e1_cor_2", data=df_gal["e1_cor_2"].values)) + t.add_column(Column(name="e2_cor_2", data=df_gal["e2_cor_2"].values)) + print("Saving alpha-corrected ellipticities to" + f" {params['output_path']}") + t.write(params["output_path"], overwrite=True, format="fits") + + if params["plot_path"] is not None: print("Plotting...") - #Compute alpha of the calibrated catalog - alpha_df.loc[:, 'alpha_1_corr_2'] = -999. - alpha_df.loc[:, 'alpha_2_corr_2'] = -999. - alpha_df.loc[:, 'alpha_1_corr_2_err'] = -999. - alpha_df.loc[:, 'alpha_2_corr_2_err'] = -999. - - for i_group in range(n_bins_r*n_bins_snr): - #Get the mask - mask_group = (df_gal['bin_R'].values == i_group//n_bins_snr) & (df_gal['bin_snr'].values == i_group%n_bins_snr) - #Fit linear model to compute alpha - e1_out = np.array(df_gal.loc[mask_group, 'e1_cor_2']) - e2_out = np.array(df_gal.loc[mask_group, 'e2_cor_2']) - weight_out = np.array(df_gal.loc[mask_group, 'w']) - e1_PSF = np.array(df_gal.loc[mask_group, 'e1_PSF']) - e2_PSF = np.array(df_gal.loc[mask_group, 'e2_PSF']) - - #Fit e1 + # Compute alpha of the calibrated catalog + alpha_df.loc[:, "alpha_1_corr_2"] = -999.0 + alpha_df.loc[:, "alpha_2_corr_2"] = -999.0 + alpha_df.loc[:, "alpha_1_corr_2_err"] = -999.0 + alpha_df.loc[:, "alpha_2_corr_2_err"] = -999.0 + + for i_group in range(n_bins_r * n_bins_snr): + # Get the mask + mask_group = (df_gal["bin_R"].values == i_group // n_bins_snr) & ( + df_gal["bin_snr"].values == i_group % n_bins_snr + ) + # Fit linear model to compute alpha + e1_out = np.array(df_gal.loc[mask_group, "e1_cor_2"]) + e2_out = np.array(df_gal.loc[mask_group, "e2_cor_2"]) + weight_out = np.array(df_gal.loc[mask_group, "w"]) + e1_PSF = np.array(df_gal.loc[mask_group, "e1_PSF"]) + e2_PSF = np.array(df_gal.loc[mask_group, "e2_PSF"]) + + # Fit e1 mod_wls = sm.WLS(e1_out, sm.add_constant(e1_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_1_corr_2'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_1_corr_2_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_1_corr_2"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_1_corr_2_err"] = np.sqrt( + res_wls.cov_params()[1, 1] + ) del res_wls, mod_wls - #Fit e2 + # Fit e2 mod_wls = sm.WLS(e2_out, sm.add_constant(e2_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_2_corr_2'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_2_corr__2err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_2_corr_2"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_2_corr__2err"] = np.sqrt( + res_wls.cov_params()[1, 1] + ) del weight_out, res_wls, mod_wls print( - 'Plotting the results and saving to directory' + f' {params["plot_path"]}' + "Plotting the results and saving to directory" + f" {params['plot_path']}" ) fig, axs = plt.subplots(3, 3, figsize=(15, 15)) - #Plot scatter in SNR vs r plane - SNR_bins_val = (alpha_df['SNR'].values) - R_bins_val = (alpha_df['R'].values) + # Plot scatter in SNR vs r plane + SNR_bins_val = alpha_df["SNR"].values + R_bins_val = alpha_df["R"].values - alpha = 0.5*((alpha_df['alpha_1'].values)+(alpha_df['alpha_2'].values)) + alpha = 0.5 * ((alpha_df["alpha_1"].values) + (alpha_df["alpha_2"].values)) vmin, vmax = np.min(alpha), np.max(alpha) - vmin, vmax = -np.max([np.abs(vmin), np.abs(vmax)]), np.max([np.abs(vmin), np.abs(vmax)]) - - cset1 = axs[0,0].scatter(SNR_bins_val, R_bins_val, c=alpha, cmap='seismic', vmin=vmin, vmax=vmax) - axs[0,0].set_xlabel('SNR') - axs[0,0].set_ylabel('R') - axs[0,0].set_ylim(max(R_bins_val)+0.01, min(R_bins_val)-0.01) - axs[0,0].set_title('Leakage - no calibration') - axs[0,0].set_xscale('log') - fig.colorbar(cset1, ax=axs[0,0]) - - alpha = 0.5*((alpha_df['alpha_1_corr'].values)+(alpha_df['alpha_2_corr'].values)) - cset2 = axs[1,0].scatter(SNR_bins_val, R_bins_val, c=alpha, cmap='seismic', vmin=vmin, vmax=vmax) - axs[1,0].set_xlabel('SNR') - axs[1,0].set_ylabel('R') - axs[1,0].set_ylim(max(R_bins_val)+0.01, min(R_bins_val)-0.01) - axs[1,0].set_title('Leakage - Calibration Ro 1') - axs[1,0].set_xscale('log') - fig.colorbar(cset2, ax=axs[1,0]) - - alpha = 0.5*((alpha_df['alpha_1_corr_2'].values)+(alpha_df['alpha_2_corr_2'].values)) - cset3 = axs[2,0].scatter(SNR_bins_val, R_bins_val, c=alpha, cmap='seismic', vmin=vmin, vmax=vmax) - axs[2,0].set_xlabel('SNR') - axs[2,0].set_ylabel('R') - axs[2,0].set_ylim(max(R_bins_val)+0.01, min(R_bins_val)-0.01) - axs[2,0].set_title('Leakage - Calibration Ro 2') - axs[2,0].set_xscale('log') - fig.colorbar(cset3, ax=axs[2,0]) - - #Plot leakage as a function of SNR for alpha_e1 - SNR = alpha_df['SNR'].values - alpha = alpha_df['alpha_1'].values - alpha_err = alpha_df['alpha_1_err'].values - axs[0,1].errorbar(SNR, alpha, yerr=alpha_err, fmt='o') - axs[0,1].axhline(0, color='black', linestyle='--') - axs[0,1].set_xlabel('SNR') - axs[0,1].set_xscale('log') - axs[0,1].set_ylabel(r'$\alpha_1$') - axs[0,1].set_title('Leakage vs SNR - no calibration') - - alpha = alpha_df['alpha_1_corr'].values - alpha_err = alpha_df['alpha_1_corr_err'].values - axs[1,1].errorbar(SNR, alpha, yerr=alpha_err, fmt='o') - axs[1,1].axhline(0, color='black', linestyle='--') - axs[1,1].set_xlabel('SNR') - axs[1,1].set_xscale('log') - axs[1,1].set_ylabel(r'$\alpha_1$') - axs[1,1].set_title('Leakage vs SNR - Calibration Ro 1') - - alpha = alpha_df['alpha_1_corr_2'].values - alpha_err = alpha_df['alpha_1_corr_2_err'].values - axs[2,1].errorbar(SNR, alpha, yerr=alpha_err, fmt='o') - axs[2,1].axhline(0, color='black', linestyle='--') - axs[2,1].set_xlabel('SNR') - axs[2,1].set_xscale('log') - axs[2,1].set_ylabel(r'$\alpha_1$') - axs[2,1].set_title('Leakage vs SNR - Calibration Ro 2') - - #Plot leakage as a function of size for alpha_e1 - R = alpha_df['R'].values - alpha = alpha_df['alpha_1'].values - alpha_err = alpha_df['alpha_1_err'].values - axs[0,2].errorbar(R, alpha, yerr=alpha_err, fmt='o') - axs[0,2].axhline(0, color='black', linestyle='--') - axs[0,2].set_xlabel('R') - axs[0,2].set_ylabel(r'$\alpha_1$') - axs[0,2].set_title('Leakage vs R - no calibration') - - alpha = alpha_df['alpha_1_corr'].values - alpha_err = alpha_df['alpha_1_corr_err'].values - axs[1,2].errorbar(R, alpha, yerr=alpha_err, fmt='o') - axs[1,2].axhline(0, color='black', linestyle='--') - axs[1,2].set_xlabel('R') - axs[1,2].set_ylabel(r'$\alpha_1$') - axs[1,2].set_title('Leakage vs R - Calibration Ro 1') - - alpha = alpha_df['alpha_1_corr_2'].values - alpha_err = alpha_df['alpha_1_corr_2_err'].values - axs[2,2].errorbar(R, alpha, yerr=alpha_err, fmt='o') - axs[2,2].axhline(0, color='black', linestyle='--') - axs[2,2].set_xlabel('R') - axs[2,2].set_ylabel(r'$\alpha_1$') - axs[2,2].set_title('Leakage vs R - Calibration Ro 2') + vmin, vmax = ( + -np.max([np.abs(vmin), np.abs(vmax)]), + np.max([np.abs(vmin), np.abs(vmax)]), + ) + + cset1 = axs[0, 0].scatter( + SNR_bins_val, R_bins_val, c=alpha, cmap="seismic", vmin=vmin, vmax=vmax + ) + axs[0, 0].set_xlabel("SNR") + axs[0, 0].set_ylabel("R") + axs[0, 0].set_ylim(max(R_bins_val) + 0.01, min(R_bins_val) - 0.01) + axs[0, 0].set_title("Leakage - no calibration") + axs[0, 0].set_xscale("log") + fig.colorbar(cset1, ax=axs[0, 0]) + + alpha = 0.5 * ( + (alpha_df["alpha_1_corr"].values) + (alpha_df["alpha_2_corr"].values) + ) + cset2 = axs[1, 0].scatter( + SNR_bins_val, R_bins_val, c=alpha, cmap="seismic", vmin=vmin, vmax=vmax + ) + axs[1, 0].set_xlabel("SNR") + axs[1, 0].set_ylabel("R") + axs[1, 0].set_ylim(max(R_bins_val) + 0.01, min(R_bins_val) - 0.01) + axs[1, 0].set_title("Leakage - Calibration Ro 1") + axs[1, 0].set_xscale("log") + fig.colorbar(cset2, ax=axs[1, 0]) + + alpha = 0.5 * ( + (alpha_df["alpha_1_corr_2"].values) + (alpha_df["alpha_2_corr_2"].values) + ) + cset3 = axs[2, 0].scatter( + SNR_bins_val, R_bins_val, c=alpha, cmap="seismic", vmin=vmin, vmax=vmax + ) + axs[2, 0].set_xlabel("SNR") + axs[2, 0].set_ylabel("R") + axs[2, 0].set_ylim(max(R_bins_val) + 0.01, min(R_bins_val) - 0.01) + axs[2, 0].set_title("Leakage - Calibration Ro 2") + axs[2, 0].set_xscale("log") + fig.colorbar(cset3, ax=axs[2, 0]) + + # Plot leakage as a function of SNR for alpha_e1 + SNR = alpha_df["SNR"].values + alpha = alpha_df["alpha_1"].values + alpha_err = alpha_df["alpha_1_err"].values + axs[0, 1].errorbar(SNR, alpha, yerr=alpha_err, fmt="o") + axs[0, 1].axhline(0, color="black", linestyle="--") + axs[0, 1].set_xlabel("SNR") + axs[0, 1].set_xscale("log") + axs[0, 1].set_ylabel(r"$\alpha_1$") + axs[0, 1].set_title("Leakage vs SNR - no calibration") + + alpha = alpha_df["alpha_1_corr"].values + alpha_err = alpha_df["alpha_1_corr_err"].values + axs[1, 1].errorbar(SNR, alpha, yerr=alpha_err, fmt="o") + axs[1, 1].axhline(0, color="black", linestyle="--") + axs[1, 1].set_xlabel("SNR") + axs[1, 1].set_xscale("log") + axs[1, 1].set_ylabel(r"$\alpha_1$") + axs[1, 1].set_title("Leakage vs SNR - Calibration Ro 1") + + alpha = alpha_df["alpha_1_corr_2"].values + alpha_err = alpha_df["alpha_1_corr_2_err"].values + axs[2, 1].errorbar(SNR, alpha, yerr=alpha_err, fmt="o") + axs[2, 1].axhline(0, color="black", linestyle="--") + axs[2, 1].set_xlabel("SNR") + axs[2, 1].set_xscale("log") + axs[2, 1].set_ylabel(r"$\alpha_1$") + axs[2, 1].set_title("Leakage vs SNR - Calibration Ro 2") + + # Plot leakage as a function of size for alpha_e1 + R = alpha_df["R"].values + alpha = alpha_df["alpha_1"].values + alpha_err = alpha_df["alpha_1_err"].values + axs[0, 2].errorbar(R, alpha, yerr=alpha_err, fmt="o") + axs[0, 2].axhline(0, color="black", linestyle="--") + axs[0, 2].set_xlabel("R") + axs[0, 2].set_ylabel(r"$\alpha_1$") + axs[0, 2].set_title("Leakage vs R - no calibration") + + alpha = alpha_df["alpha_1_corr"].values + alpha_err = alpha_df["alpha_1_corr_err"].values + axs[1, 2].errorbar(R, alpha, yerr=alpha_err, fmt="o") + axs[1, 2].axhline(0, color="black", linestyle="--") + axs[1, 2].set_xlabel("R") + axs[1, 2].set_ylabel(r"$\alpha_1$") + axs[1, 2].set_title("Leakage vs R - Calibration Ro 1") + + alpha = alpha_df["alpha_1_corr_2"].values + alpha_err = alpha_df["alpha_1_corr_2_err"].values + axs[2, 2].errorbar(R, alpha, yerr=alpha_err, fmt="o") + axs[2, 2].axhline(0, color="black", linestyle="--") + axs[2, 2].set_xlabel("R") + axs[2, 2].set_ylabel(r"$\alpha_1$") + axs[2, 2].set_title("Leakage vs R - Calibration Ro 2") plt.tight_layout() - fig.savefig(params['plot_path'], dpi=600) + fig.savefig(params["plot_path"], dpi=600) print("Done!") - return 0 - - -if __name__ == "__main__": - sys.exit(main(sys.argv)) \ No newline at end of file + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/scripts/calibration/calibrate_comprehensive_cat.py b/scripts/calibration/calibrate_comprehensive_cat.py index f39dba7c..38d67a53 100644 --- a/scripts/calibration/calibrate_comprehensive_cat.py +++ b/scripts/calibration/calibrate_comprehensive_cat.py @@ -36,7 +36,6 @@ config = obj.read_config_set_params("config_mask.yaml") - # %% obj._params @@ -46,7 +45,7 @@ # %% n_test = -1 -#n_test = 100000 +# n_test = 100000 if n_test > 0: print(f"MKDEBUG testing only first {n_test} objects") dat = dat[:n_test] @@ -57,12 +56,7 @@ # %% # ### Pre-processing ShapePipe flags -masks, labels = sp_joint.get_masks_from_config( - config, - dat, - dat_ext, - verbose=True -) +masks, labels = sp_joint.get_masks_from_config(config, dat, dat_ext, verbose=True) mask_combined = sp_joint.Mask.from_list( masks, @@ -75,7 +69,6 @@ sp_joint.print_mask_stats(dat.shape[0], masks, mask_combined) if obj._params["sky_regions"]: - # MKDBEUG TODO: zooms as list in config zoom_ra = [200, 205] zoom_dec = [55, 60] @@ -104,8 +97,8 @@ ) # %% -g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = ( - calibration.get_calibrated_m_c(gal_metacal) +g_corr_mc, g_uncorr, w, mask_metacal, c, c_err = calibration.get_calibrated_m_c( + gal_metacal ) num_ok = len(g_corr_mc[0]) @@ -166,7 +159,9 @@ if "binned_mask" in config: cbm = config["binned_mask"] print("Binning data to obtain mask...") - area_deg2, pix = cs_cat.get_binned_area(ra, dec, nside=cbm["nside"], return_pix=True) + area_deg2, pix = cs_cat.get_binned_area( + ra, dec, nside=cbm["nside"], return_pix=True + ) healpix_map = np.full(hp.nside2npix(cbm["nside"]), not cbm["good"]) healpix_map[pix] = cbm["good"] print(f"Writing binned mask to file {cbm['output_path']}...") @@ -195,12 +190,7 @@ ] add_cols_data = {} for key in add_cols: - add_cols_data[key] = cat.get_col( - dat, - key, - mask_combined._mask, - mask_metacal - ) + add_cols_data[key] = cat.get_col(dat, key, mask_combined._mask, mask_metacal) # Keep original NOSHEAR column, override with 1P PSF values (FHP/MK hack) print( @@ -243,9 +233,7 @@ my_mask.add_summary_to_FITS_header(header) # %% -output_shape_cat_path = obj._params["input_path"].replace( - "comprehensive", "cut" -) +output_shape_cat_path = obj._params["input_path"].replace("comprehensive", "cut") output_shape_cat_path = output_shape_cat_path.replace("hdf5", "fits") cat.write_shape_catalog( @@ -302,7 +290,6 @@ cms = np.zeros((n_key, n_key, 2, 2)) for idx in range(n_key): for jdx in range(n_key): - if idx == jdx: continue @@ -325,7 +312,6 @@ for idx in range(n_key): for jdx in range(n_key): - if idx == jdx: continue diff --git a/scripts/calibration/create_joint_comprehensive_cat.py b/scripts/calibration/create_joint_comprehensive_cat.py index ab541eec..dd725a5b 100755 --- a/scripts/calibration/create_joint_comprehensive_cat.py +++ b/scripts/calibration/create_joint_comprehensive_cat.py @@ -4,14 +4,15 @@ from sp_validation.catalog_builders import run_joint_comprehensive_cat + def main(argv=None): if argv is None: - args = sys.argv[0:] + argv = sys.argv[0:] run_joint_comprehensive_cat(*argv) return 0 if __name__ == "__main__": - sys.exit(main(sys.argv)) \ No newline at end of file + sys.exit(main(sys.argv)) diff --git a/scripts/calibration/demo_apply_hsp_masks.py b/scripts/calibration/demo_apply_hsp_masks.py index dc81de74..beafb909 100644 --- a/scripts/calibration/demo_apply_hsp_masks.py +++ b/scripts/calibration/demo_apply_hsp_masks.py @@ -90,14 +90,18 @@ if trace_mem: current, peak = tracemalloc.get_traced_memory() - print(f"Current (peak) memory usage: {current / 1024**2:.2f} ({peak / 1024**2:.2f}) MB") + print( + f"Current (peak) memory usage: {current / 1024**2:.2f} ({peak / 1024**2:.2f}) MB" + ) # Read catalogue dat = obj.read_cat(load_into_memory=False, mode="r") if trace_mem: current, peak = tracemalloc.get_traced_memory() - print(f"Current (peak) memory usage: {current / 1024**2:.2f} ({peak / 1024**2:.2f}) MB") + print( + f"Current (peak) memory usage: {current / 1024**2:.2f} ({peak / 1024**2:.2f}) MB" + ) # Get bit-coded masks masks = obj.get_masks(dat=dat) @@ -107,7 +111,9 @@ if trace_mem: current, peak = tracemalloc.get_traced_memory() - print(f"Current (peak) memory usage: {current / 1024**2:.2f} ({peak / 1024**2:.2f}) MB") + print( + f"Current (peak) memory usage: {current / 1024**2:.2f} ({peak / 1024**2:.2f}) MB" + ) # Write extended data to new HDF5 file obj.write_hdf5_file(dat, dat_new) diff --git a/scripts/calibration/extract_info.py b/scripts/calibration/extract_info.py index 2b163873..3868080b 100644 --- a/scripts/calibration/extract_info.py +++ b/scripts/calibration/extract_info.py @@ -35,10 +35,10 @@ import numpy as np from astropy.io import fits -#from sp_validation.catalog import * +# from sp_validation.catalog import * from sp_validation import catalog as spv_cat -from sp_validation.calibration import metacal from sp_validation.calibration import * +from sp_validation.calibration import metacal from sp_validation.galaxy import * from sp_validation.io import * from sp_validation.survey import * @@ -65,30 +65,28 @@ dd = np.load(galaxy_cat_path, mmap_mode=mmap_mode) else: print("Loading galaxy .hdf5 file...") - dd = spv_cat.read_hdf5_file(galaxy_cat_path, name, stats_file, param_path=param_list_path) + dd = spv_cat.read_hdf5_file( + galaxy_cat_path, name, stats_file, param_path=param_list_path + ) n_obj = len(dd) -print_stats(f'Read {n_obj} objects from file {galaxy_cat_path}', stats_file, verbose=verbose) +print_stats( + f"Read {n_obj} objects from file {galaxy_cat_path}", stats_file, verbose=verbose +) # - # #### Print some quantities to check nothing obvious is wrong with catalogue # PSF keys key_base = shape.upper() -key_PSF_ell = f'{key_base}_ELL_PSFo_NOSHEAR' -key_PSF_size = f'{key_base}_T_PSFo_NOSHEAR' +key_PSF_ell = f"{key_base}_ELL_PSFo_NOSHEAR" +key_PSF_size = f"{key_base}_T_PSFo_NOSHEAR" size_to_fwhm = T_to_fwhm -print_stats('Galaxies:', stats_file, verbose=verbose) +print_stats("Galaxies:", stats_file, verbose=verbose) n_tot = spv_cat.print_some_quantities(dd, stats_file, verbose=verbose) spv_cat.print_mean_ellipticity( - dd, - f'{key_base}_ELL_NOSHEAR', - 2, - n_tot, - stats_file, - invalid=-10, - verbose=verbose + dd, f"{key_base}_ELL_NOSHEAR", 2, n_tot, stats_file, invalid=-10, verbose=verbose ) # #### Survey area and potential missing tiles @@ -98,7 +96,9 @@ # Identify missing tiles by comparing tile ID from catalogue to external input tile ID file. -n_found, n_missing = missing_tiles(tile_IDs, path_tile_ID, path_found_ID, path_missing_ID, verbose=verbose) +n_found, n_missing = missing_tiles( + tile_IDs, path_tile_ID, path_found_ID, path_missing_ID, verbose=verbose +) # ### Load star catalogue @@ -106,16 +106,16 @@ d_star = fits.getdata(star_cat_path, hdu_star_cat) if star_cat_path: - print_stats('Stars:', stats_file, verbose=verbose) + print_stats("Stars:", stats_file, verbose=verbose) n_tot = spv_cat.print_some_quantities(d_star, stats_file, verbose=verbose) spv_cat.print_mean_ellipticity( d_star, - ['E1_PSF_HSM', 'E2_PSF_HSM'], + ["E1_PSF_HSM", "E2_PSF_HSM"], 1, n_tot, stats_file, invalid=-10, - verbose=verbose + verbose=verbose, ) # ### 3. Matching of stars @@ -131,12 +131,12 @@ ind_star, mask_area_tiles, n_star_tot = spv_cat.check_matching( d_star, dd, - ['RA', 'DEC'], + ["RA", "DEC"], [col_name_ra, col_name_dec], thresh, stats_file, name=None, - verbose=verbose + verbose=verbose, ) # #### Refine: Match to valid, unflagged galaxy sample @@ -145,10 +145,10 @@ # Flags to indicate valid star sample m_star = ( - (dd['FLAGS'][ind_star] == 0) - & (dd['IMAFLAGS_ISO'][ind_star] == 0) - & (dd['NGMIX_MCAL_FLAGS'][ind_star] == 0) - & (dd['NGMIX_ELL_PSFo_NOSHEAR'][:,0][ind_star] != -10) + (dd["FLAGS"][ind_star] == 0) + & (dd["IMAFLAGS_ISO"][ind_star] == 0) + & (dd["NGMIX_MCAL_FLAGS"][ind_star] == 0) + & (dd["NGMIX_ELL_PSFo_NOSHEAR"][:, 0][ind_star] != -10) ) ra_star, dec_star, g_star_psf = spv_cat.match_subsample( @@ -159,7 +159,7 @@ key_PSF_ell, n_star_tot, stats_file, - verbose=verbose + verbose=verbose, ) # - @@ -168,7 +168,7 @@ # ### Write PSF catalogue with multi-epoch shapes from shape measurement methods spv_cat.write_PSF_cat( - f'{output_PSF_cat_base}_{shape}.fits', + f"{output_PSF_cat_base}_{shape}.fits", ra_star, dec_star, g_star_psf[0], @@ -177,20 +177,26 @@ #### Refine: Match to SPREAD_CLASS samples if "SPREAD_CLASS" in dd.dtype.names: - spv_cat.match_spread_class(dd, ind_star, m_star, stats_file, len(ra_star), verbose=verbose) + spv_cat.match_spread_class( + dd, ind_star, m_star, stats_file, len(ra_star), verbose=verbose + ) else: - print_stats("No SPREAD_CLASS in input, skipping star-gal matching", stats_file, verbose=verbose) + print_stats( + "No SPREAD_CLASS in input, skipping star-gal matching", + stats_file, + verbose=verbose, + ) # ## Check for objects with invalid PSF spv_cat.check_invalid( dd, - [key_PSF_ell, f'{key_base}_ELL_NOSHEAR'], + [key_PSF_ell, f"{key_base}_ELL_NOSHEAR"], [0, 0], [-10, -10], stats_file, - name=['`PSF', 'galaxy ellipticity'], - verbose=verbose + name=["`PSF", "galaxy ellipticity"], + verbose=verbose, ) # + @@ -203,7 +209,11 @@ ) n_ok = sum(cut_overlap) -print_stats(f"Non-overlapping objects: {n_ok:10d}, {n_ok/n_obj:10.2%}", stats_file, verbose=verbose) +print_stats( + f"Non-overlapping objects: {n_ok:10d}, {n_ok / n_obj:10.2%}", + stats_file, + verbose=verbose, +) # - @@ -233,9 +243,9 @@ add_cols_pre_cal_format["overlap"] = "I" # Additional columns {e1, e2, size}_PSF -ext_cols_pre_cal['e1_PSF'] = dd[key_PSF_ell][:,0] -ext_cols_pre_cal['e2_PSF'] = dd[key_PSF_ell][:,1] -ext_cols_pre_cal['fwhm_PSF'] = size_to_fwhm(dd[key_PSF_size]) +ext_cols_pre_cal["e1_PSF"] = dd[key_PSF_ell][:, 0] +ext_cols_pre_cal["e2_PSF"] = dd[key_PSF_ell][:, 1] +ext_cols_pre_cal["fwhm_PSF"] = size_to_fwhm(dd[key_PSF_size]) _, _, iv_w = metacal.get_variance_ivweights(dd, sigma_eps_prior, mask=None, col_2d=True) @@ -249,7 +259,7 @@ print("Writing comprehensive catalogue...") spv_cat.write_shape_catalog( - f'{output_shape_cat_base}_comprehensive_{shape}.fits', + f"{output_shape_cat_base}_comprehensive_{shape}.fits", ra_all, dec_all, iv_w, @@ -259,7 +269,7 @@ g2_uncal=g2_uncal, add_cols=ext_cols_pre_cal, add_cols_format=add_cols_pre_cal_format, - ) +) # - do_selection_calibration = False @@ -303,11 +313,19 @@ raise ValueError(f"Invalid shape measurement method {shape}") n_ok = sum(cut_common) -print_stats(f"objects after common cut: {n_ok:10d}, {n_ok/n_obj:3.2%}", stats_file, verbose=verbose) +print_stats( + f"objects after common cut: {n_ok:10d}, {n_ok / n_obj:3.2%}", + stats_file, + verbose=verbose, +) # MKDEBUG for debugging calibrate_comprehensive n_ok = sum(m_gal) -print_stats(f"common & ngmix = galaxy selection: {n_ok:10d}, {n_ok/n_obj:3.2%}", stats_file, verbose=verbose) +print_stats( + f"common & ngmix = galaxy selection: {n_ok:10d}, {n_ok / n_obj:3.2%}", + stats_file, + verbose=verbose, +) # - # ### Metacal global @@ -319,14 +337,14 @@ from lenspack.geometry.projections.gnom import radec2xy from uncertainties import ufloat -from sp_validation.calibration import metacal from sp_validation.calibration import * +from sp_validation.calibration import metacal +from sp_validation.format import * from sp_validation.plot_style import * from sp_validation.plots import * # - from sp_validation.survey import * -from sp_validation.format import * # ## metacalibration for galaxies @@ -341,7 +359,7 @@ rel_size_max=gal_rel_size_max, size_corr_ell=gal_size_corr_ell, sigma_eps=sigma_eps_prior, - verbose=verbose + verbose=verbose, ) print_stats( @@ -359,21 +377,15 @@ # #### Extract quantities after calibration and cuts (in metacal) # + -g_corr, g_uncorr, w, mask = ( - get_calibrated_quantities(gal_metacal) -) +g_corr, g_uncorr, w, mask = get_calibrated_quantities(gal_metacal) -n_before_cuts = len(gal_metacal.ns['g1']) +n_before_cuts = len(gal_metacal.ns["g1"]) n_after_cuts = len(w) print_stats( - f"Number of objects before cuts = {n_before_cuts}", - stats_file, - verbose=verbose + f"Number of objects before cuts = {n_before_cuts}", stats_file, verbose=verbose ) print_stats( - f"Number of objects after cuts = {n_after_cuts}", - stats_file, - verbose=verbose + f"Number of objects after cuts = {n_after_cuts}", stats_file, verbose=verbose ) # coordinates @@ -390,11 +402,11 @@ dec_mean = np.mean(dec) print_stats( - 'Mean coordinates (ra, dec) =' - + f' ({ra_mean:.3f}, {dec_mean:.3f}) deg,' - + f' wrap_ra={wrap_ra} deg', + "Mean coordinates (ra, dec) =" + + f" ({ra_mean:.3f}, {dec_mean:.3f}) deg," + + f" wrap_ra={wrap_ra} deg", stats_file, - verbose=verbose + verbose=verbose, ) # magnitude, from SExtractor @@ -421,7 +433,7 @@ # - # Project all objects from spherical to Cartesian coordinates -x, y = radec2xy(ra_mean, dec_mean, ra_wrap, dec) +x, y = radec2xy(ra_mean, dec_mean, ra_wrap, dec) # #### Compute field size @@ -436,10 +448,10 @@ size_y_deg = np.rad2deg(max_y - min_y) print_stats( - 'Field size in projected coordinates is (x, y) ' - + f'= ({size_x_deg:.2f}, {size_y_deg:.2f}) deg', + "Field size in projected coordinates is (x, y) " + + f"= ({size_x_deg:.2f}, {size_y_deg:.2f}) deg", stats_file, - verbose=verbose + verbose=verbose, ) # + @@ -448,21 +460,21 @@ n_gal_sm = len(np.where(m_gal)[0]) print_stats( - f'Number of galaxies after metacal = {n_gal}/{n_gal_sm} ' - + f'= {n_gal / n_gal_sm * 100:.1f}%', + f"Number of galaxies after metacal = {n_gal}/{n_gal_sm} " + + f"= {n_gal / n_gal_sm * 100:.1f}%", stats_file, - verbose=verbose + verbose=verbose, ) print_stats( - f'Galaxy density (ignoring masks+overlaps)= {n_gal / area_amin2:.2f} gal/arcmin2', + f"Galaxy density (ignoring masks+overlaps)= {n_gal / area_amin2:.2f} gal/arcmin2", stats_file, - verbose=verbose + verbose=verbose, ) # - # Write number of galaxies for each tile to file -fname = f'{output_dir}/tile_id_gal_counts_{shape}.txt' -detection_IDs = dd['TILE_ID'] +fname = f"{output_dir}/tile_id_gal_counts_{shape}.txt" +detection_IDs = dd["TILE_ID"] galaxy_IDs = detection_IDs[m_gal] shape_IDs = galaxy_IDs[mask] write_tile_id_gal_counts(detection_IDs, galaxy_IDs, shape_IDs, fname) @@ -472,7 +484,7 @@ w_tot = np.sum(w) -print_stats(f'Sum of weights = {w_tot:.1f}', stats_file, verbose=verbose) +print_stats(f"Sum of weights = {w_tot:.1f}", stats_file, verbose=verbose) # + # Effective sample size ESS = 1/sum(w_n^2) @@ -482,30 +494,35 @@ # normalised weights wn = w / w_tot s = np.sum(wn**2) -ess = 1/s +ess = 1 / s -print_stats(f'effective sample size, ESS/N = {ess:.1f}/{n_gal} = {ess/n_gal:.3g}', - stats_file, verbose=verbose) +print_stats( + f"effective sample size, ESS/N = {ess:.1f}/{n_gal} = {ess / n_gal:.3g}", + stats_file, + verbose=verbose, +) # - # #### Plot spatial distribution of objects -x_label = 'R.A. [deg]' -y_label = 'DEC [deg]' -cbar_label_base = 'Density [$A_{\\rm pix}^{-1}$]' +x_label = "R.A. [deg]" +y_label = "DEC [deg]" +cbar_label_base = "Density [$A_{\\rm pix}^{-1}$]" len(dec) # + # Galaxies -Apix = 1 # [arcmin^2] -cbar_label = '{}, $A_{{\\rm pix}} \\approx {:.1g}$ arcmin$^2$'.format(cbar_label_base, Apix) +Apix = 1 # [arcmin^2] +cbar_label = "{}, $A_{{\\rm pix}} \\approx {:.1g}$ arcmin$^2$".format( + cbar_label_base, Apix +) n_grid = int(np.sqrt(area_amin2) / Apix) if verbose: - print('Number of pixels = {}^2'.format(n_grid)) + print("Number of pixels = {}^2".format(n_grid)) -title = f'Galaxies ({shape})' -out_path = f'{plot_dir}/galaxy_number_count_{shape}' +title = f"Galaxies ({shape})" +out_path = f"{plot_dir}/galaxy_number_count_{shape}" plot_spatial_density( ra_wrap, dec, @@ -515,13 +532,13 @@ cbar_label, out_path, n_grid=n_grid, - verbose=verbose + verbose=verbose, ) # All objects without overlap, useful for position-only # setting (no shapes) -title = 'Galaxies (all, no overlap)' -out_path = f'{plot_dir}/galaxy_number_count_all_nooverlap' +title = "Galaxies (all, no overlap)" +out_path = f"{plot_dir}/galaxy_number_count_all_nooverlap" plot_spatial_density( ra_wrap_all[cut_overlap], dec_all[cut_overlap], @@ -531,37 +548,37 @@ cbar_label, out_path, n_grid=n_grid, - verbose=verbose + verbose=verbose, ) # - # #### Plot galaxy signal-to-noise distribution # + -x_label = 'SNR' -y_label = 'Frequency' +x_label = "SNR" +y_label = "Frequency" density = True x_range = (0, 200) n_bin = 500 x_cut = gal_snr_min labels = [] -if shape == 'ngmix': -# Do not apply `mask_ns`, so use all galaxies +if shape == "ngmix": + # Do not apply `mask_ns`, so use all galaxies xs = [ - dd['NGMIX_FLUX_NOSHEAR'][m_gal] / dd['NGMIX_FLUX_ERR_NOSHEAR'][m_gal], - dd['SNR_WIN'][m_gal] + dd["NGMIX_FLUX_NOSHEAR"][m_gal] / dd["NGMIX_FLUX_ERR_NOSHEAR"][m_gal], + dd["SNR_WIN"][m_gal], ] - labels.append(['$F/\\sigma(F)$']) + labels.append(["$F/\\sigma(F)$"]) else: raise ValueError(f"Unknown shape measurement method {shape}") -labels.append('SExtractor SNR') +labels.append("SExtractor SNR") -title = 'Galaxies' +title = "Galaxies" -out_name = f'hist_SNR_{shape}.pdf' +out_name = f"hist_SNR_{shape}.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -574,34 +591,32 @@ n_bin, out_path, vline_x=[x_cut], - vline_lab=[f'SNR = {x_cut}'] + vline_lab=[f"SNR = {x_cut}"], ) # - # ### Plot galaxy relative size distribution # + -x_label = r'$R^2_{\rm gal} / R^2_{\rm PSF}$' -y_label = 'Frequency' +x_label = r"$R^2_{\rm gal} / R^2_{\rm PSF}$" +y_label = "Frequency" density = True x_range = (0, 1.5) n_bin = 500 x_cut = gal_rel_size_min labels = [] -if shape == 'ngmix': +if shape == "ngmix": # Do not apply `mask_ns`, so use all galaxies - xs = [ - dd['NGMIX_T_NOSHEAR'][m_gal] / dd['NGMIX_Tpsf_NOSHEAR'][m_gal] - ] - labels.append('size ratio') + xs = [dd["NGMIX_T_NOSHEAR"][m_gal] / dd["NGMIX_Tpsf_NOSHEAR"][m_gal]] + labels.append("size ratio") else: raise ValueError(f"Unknown shape measurement method {shape}") -title = 'Galaxies' +title = "Galaxies" -out_name = f'hist_rel_size_{shape}.pdf' +out_name = f"hist_rel_size_{shape}.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -614,35 +629,34 @@ n_bin, out_path, vline_x=[x_cut], - vline_lab=[f'rel_size = {x_cut}'] + vline_lab=[f"rel_size = {x_cut}"], ) # - # ## Metacalibration for stars -star_metacal = metacal( - dd[ind_star], - m_star, - masking_type='star', - verbose=verbose -) +star_metacal = metacal(dd[ind_star], m_star, masking_type="star", verbose=verbose) # #### Number density # + # mask for 'no shear' images -mask_ns_stars = star_metacal.mask_dict['ns'] -n_star = len(star_metacal.ns['g1'][mask_ns_stars]) +mask_ns_stars = star_metacal.mask_dict["ns"] +n_star = len(star_metacal.ns["g1"][mask_ns_stars]) -print_stats(f'Number of stars = {n_star}', stats_file, verbose=verbose) -print_stats('Star density = {:.2f} stars/deg2'.format(n_star / area_deg2), stats_file, verbose=verbose) +print_stats(f"Number of stars = {n_star}", stats_file, verbose=verbose) +print_stats( + "Star density = {:.2f} stars/deg2".format(n_star / area_deg2), + stats_file, + verbose=verbose, +) # - # ## Additive bias # Use raw, uncorrected ellipticities. -print_stats('additive bias', stats_file, verbose=verbose) +print_stats("additive bias", stats_file, verbose=verbose) # + # Compute mean, weighted mean, and (Poisson) error @@ -657,23 +671,27 @@ c_err[comp] = np.std(g_uncorr[comp]) cw[comp] = np.average(g_uncorr[comp], weights=w) - variance = np.average((g_uncorr[comp] - cw[comp])**2, weights=w) + variance = np.average((g_uncorr[comp] - cw[comp]) ** 2, weights=w) cw_err[comp] = np.sqrt(variance) for comp in (0, 1): - print_stats(f'c_{comp+1} = {c[comp]:.3g}', stats_file, verbose=verbose) - print_stats(f'cw_{comp+1} = {cw[comp]:.3g}', stats_file, verbose=verbose) + print_stats(f"c_{comp + 1} = {c[comp]:.3g}", stats_file, verbose=verbose) + print_stats(f"cw_{comp + 1} = {cw[comp]:.3g}", stats_file, verbose=verbose) for comp in (0, 1): - print_stats(f'dc_{comp+1} = {c_err[comp]:.3g}', stats_file, verbose=verbose) - print_stats(f'dcw_{comp+1} = {cw_err[comp]:.3g}', stats_file, verbose=verbose) + print_stats(f"dc_{comp + 1} = {c_err[comp]:.3g}", stats_file, verbose=verbose) + print_stats(f"dcw_{comp + 1} = {cw_err[comp]:.3g}", stats_file, verbose=verbose) # Error of mean: divide by sqrt(N) (TBC whether this is correct) for comp in (0, 1): - print_stats(f'dmc_{comp+1} = {c_err[comp]/np.sqrt(n_gal):.3e}', stats_file, verbose=verbose) print_stats( - f'dmcw_{comp+1} = {cw_err[comp]/np.sqrt(n_gal):.3e}', + f"dmc_{comp + 1} = {c_err[comp] / np.sqrt(n_gal):.3e}", + stats_file, + verbose=verbose, + ) + print_stats( + f"dmcw_{comp + 1} = {cw_err[comp] / np.sqrt(n_gal):.3e}", stats_file, - verbose=verbose + verbose=verbose, ) # + @@ -687,13 +705,10 @@ for comp in (0, 1): if n_jack > 0: cjk[comp], cjk_err[comp] = jackknif_weighted_average( - g_uncorr[comp], - w, - remove_size=remove_size, - n_realization=n_jack + g_uncorr[comp], w, remove_size=remove_size, n_realization=n_jack ) cjk_dc = ufloat(cjk[comp], cjk_err[comp]) - print_stats(f'cjk_{comp+1} = {cjk_dc:.3eP}', stats_file, verbose=verbose) + print_stats(f"cjk_{comp + 1} = {cjk_dc:.3eP}", stats_file, verbose=verbose) # - # ## Get quantities calibrated for both multiplicative and additive bias @@ -708,63 +723,58 @@ # ### Mean # + -print_stats('total response matrix:', stats_file, verbose=verbose) +print_stats("total response matrix:", stats_file, verbose=verbose) rs = np.array2string(gal_metacal.R) print_stats(rs, stats_file, verbose=verbose) -print_stats('shear response matrix:', stats_file, verbose=verbose) +print_stats("shear response matrix:", stats_file, verbose=verbose) R_shear = np.mean(gal_metacal.R_shear, 2) rs = np.array2string(R_shear) print_stats(rs, stats_file, verbose=verbose) -print_stats('selection response matrix:', stats_file, verbose=verbose) +print_stats("selection response matrix:", stats_file, verbose=verbose) rs = np.array2string(gal_metacal.R_selection) print_stats(rs, stats_file, verbose=verbose) # + print_stats("stars:", stats_file, verbose=verbose) -print_stats('total response matrix:', stats_file, verbose=verbose) +print_stats("total response matrix:", stats_file, verbose=verbose) rs = np.array2string(star_metacal.R) print_stats(rs, stats_file, verbose=verbose) -print_stats('shear response matrix:', stats_file, verbose=verbose) +print_stats("shear response matrix:", stats_file, verbose=verbose) R_shear_stars = np.mean(star_metacal.R_shear, 2) rs = np.array2string(R_shear_stars) print_stats(rs, stats_file, verbose=verbose) -print_stats('selection response matrix:', stats_file, verbose=verbose) +print_stats("selection response matrix:", stats_file, verbose=verbose) rs = np.array2string(star_metacal.R_selection) print_stats(rs, stats_file, verbose=verbose) # - # ### Plot distribution of response matrix elements -x_label = 'response matrix element' -y_label = 'Frequency' +x_label = "response matrix element" +y_label = "Frequency" x_range = (-3, 3) n_bin = 500 -colors = ['blue', 'red','blue', 'red'] -linestyles = ['-', '-', ':', ':'] +colors = ["blue", "red", "blue", "red"] +linestyles = ["-", "-", ":", ":"] # + -labels = [ - '$R_{11}$ galaxies', - '$R_{22}$ galaxies', - '$R_{11}$ stars', - '$R_{22}$ stars' -] +labels = ["$R_{11}$ galaxies", "$R_{22}$ galaxies", "$R_{11}$ stars", "$R_{22}$ stars"] xs = [ - gal_metacal.R_shear[0,0], - gal_metacal.R_shear[1,1], - star_metacal.R_shear[0,0], - star_metacal.R_shear[1,1] + gal_metacal.R_shear[0, 0], + gal_metacal.R_shear[1, 1], + star_metacal.R_shear[0, 0], + star_metacal.R_shear[1, 1], ] title = shape -out_name = f'R_{shape}_diag.pdf' +out_name = f"R_{shape}_diag.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -777,24 +787,20 @@ n_bin, out_path, colors=colors, - linestyles=linestyles + linestyles=linestyles, ) # + -labels = [ - '$R_{12}$ galaxies', - '$R_{21}$ galaxies', - '$R_{12}$ stars', - '$R_{21}$ stars' -] +labels = ["$R_{12}$ galaxies", "$R_{21}$ galaxies", "$R_{12}$ stars", "$R_{21}$ stars"] -xs = [gal_metacal.R_shear[0,1], - gal_metacal.R_shear[1,0], - star_metacal.R_shear[0,1], - star_metacal.R_shear[1,0] - ] +xs = [ + gal_metacal.R_shear[0, 1], + gal_metacal.R_shear[1, 0], + star_metacal.R_shear[0, 1], + star_metacal.R_shear[1, 0], +] title = shape -out_name = f'R_{shape}_offdiag.pdf' +out_name = f"R_{shape}_offdiag.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -807,28 +813,28 @@ n_bin, out_path, colors=colors, - linestyles=linestyles + linestyles=linestyles, ) # - # ## Ellipticities # + -x_label = 'ellipticity' -y_label = 'Frequency' +x_label = "ellipticity" +y_label = "Frequency" x_range = (-1, 1) n_bin = 500 -labels = ['$e_1$', '$e_2$'] -colors = ['blue', 'red'] -linestyles = ['-', '-'] +labels = ["$e_1$", "$e_2$"] +colors = ["blue", "red"] +linestyles = ["-", "-"] # + xs = [g_corr[0], g_corr[1]] weights = [w] * 2 -title = f'{shape} galaxies' -out_name = f'ell_gal_{shape}.pdf' +title = f"{shape} galaxies" +out_name = f"ell_gal_{shape}.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -842,15 +848,15 @@ out_path, weights=weights, colors=colors, - linestyles=linestyles + linestyles=linestyles, ) # + -xs = [star_metacal.ns['g1'][mask_ns_stars], star_metacal.ns['g2'][mask_ns_stars]] -weights = [star_metacal.ns['w'][mask_ns_stars]] * 2 +xs = [star_metacal.ns["g1"][mask_ns_stars], star_metacal.ns["g2"][mask_ns_stars]] +weights = [star_metacal.ns["w"][mask_ns_stars]] * 2 title = "stars" -out_name = f'ell_stars_{shape}.pdf' +out_name = f"ell_stars_{shape}.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -864,7 +870,7 @@ out_path, weights=weights, colors=colors, - linestyles=linestyles + linestyles=linestyles, ) # - @@ -873,12 +879,9 @@ # + key = key_PSF_ell -xs = [ - dd[key][:,0][mask_ns_stars], - dd[key][:,1][mask_ns_stars] -] +xs = [dd[key][:, 0][mask_ns_stars], dd[key][:, 1][mask_ns_stars]] title = "PSF" -out_name = f'ell_PSF_{shape}.pdf' +out_name = f"ell_PSF_{shape}.pdf" out_path = os.path.join(plot_dir, out_name) plot_histograms( @@ -891,28 +894,28 @@ n_bin, out_path, colors=colors, - linestyles=linestyles + linestyles=linestyles, ) # - # ## Magnitudes # + -x_label = '$r$-band magnitude' -y_label = 'Frequency' +x_label = "$r$-band magnitude" +y_label = "Frequency" x_range = (gal_mag_bright + 1, gal_mag_faint - 1) n_bin = 500 -colors = ['blue', 'red'] -linestyles = ['-', '-'] +colors = ["blue", "red"] +linestyles = ["-", "-"] -title = 'galaxies' -out_name = 'mag_gal.pdf' +title = "galaxies" +out_name = "mag_gal.pdf" out_path = os.path.join(plot_dir, out_name) # + labels = [shape] -xs = [dd['MAG_AUTO'][m_gal][mask]] +xs = [dd["MAG_AUTO"][m_gal][mask]] plot_histograms( xs, @@ -924,17 +927,24 @@ n_bin, out_path, colors=colors, - linestyles=linestyles + linestyles=linestyles, ) # - # ## Ellipticity dispersion sig_eps = np.sqrt(np.var(g_corr[0]) + np.var(g_corr[1])) -print_stats('Dispersion of complex ellipticity = {:.3f}' \ - ''.format(sig_eps), stats_file, verbose=verbose) -print_stats('Dispersion of (average) single-component ellipticity = {:.3f} = {:.3f} / sqrt(2)' \ - ''.format(sig_eps / np.sqrt(2), sig_eps), stats_file, verbose=verbose) +print_stats( + "Dispersion of complex ellipticity = {:.3f}".format(sig_eps), + stats_file, + verbose=verbose, +) +print_stats( + "Dispersion of (average) single-component ellipticity = {:.3f} = {:.3f} / sqrt(2)" + "".format(sig_eps / np.sqrt(2), sig_eps), + stats_file, + verbose=verbose, +) # ## Write catalogues @@ -949,7 +959,7 @@ # ### Write basic shape catalogue spv_cat.write_shape_catalog( - f'{output_shape_cat_base}_{shape}.fits', + f"{output_shape_cat_base}_{shape}.fits", ra, dec, w, @@ -980,15 +990,15 @@ # + # Additional columns: # {e1, e2, size}_PSF -ext_cols['e1_PSF'] = dd[key_PSF_ell][:,0][m_gal][mask] -ext_cols['e2_PSF'] = dd[key_PSF_ell][:,1][m_gal][mask] -ext_cols['fwhm_PSF'] = size_to_fwhm(dd[key_PSF_size][m_gal][mask]) +ext_cols["e1_PSF"] = dd[key_PSF_ell][:, 0][m_gal][mask] +ext_cols["e2_PSF"] = dd[key_PSF_ell][:, 1][m_gal][mask] +ext_cols["fwhm_PSF"] = size_to_fwhm(dd[key_PSF_size][m_gal][mask]) if mask_external_path: - ext_cols['mask_extern'] = m_extern + ext_cols["mask_extern"] = m_extern # Extended catalogue with SNR, individual R matrices, ext_cols spv_cat.write_shape_catalog( - f'{output_shape_cat_base}_extended_{shape}.fits', + f"{output_shape_cat_base}_extended_{shape}.fits", ra, dec, w, @@ -1007,16 +1017,15 @@ c=c, c_err=c_err, add_cols=ext_cols, - ) +) # - # ### Write galaxy (or random) position catalogue if shape == "": - print('writing random cat (hack)') - - ra = dd['RA'][cut_overlap] - dec = dd['DEC'][cut_overlap] - tile_id = dd['TILE_ID'][cut_overlap] - write_galaxy_cat(f'{output_shape_cat_base}.fits', ra, dec, tile_id) + print("writing random cat (hack)") + ra = dd["RA"][cut_overlap] + dec = dd["DEC"][cut_overlap] + tile_id = dd["TILE_ID"][cut_overlap] + write_galaxy_cat(f"{output_shape_cat_base}.fits", ra, dec, tile_id) diff --git a/scripts/calibration/params.py b/scripts/calibration/params.py index 903dc146..363b36f7 100644 --- a/scripts/calibration/params.py +++ b/scripts/calibration/params.py @@ -1,5 +1,5 @@ """ - + :Name: params.py :Description: This script contains parameters to run the validation notebook. @@ -20,14 +20,14 @@ verbose = True ## Math output -np.set_printoptions(precision=3, formatter={'float': '{: .3g}'.format}) +np.set_printoptions(precision=3, formatter={"float": "{: .3g}".format}) # Survey parameters ## Field or patch name. Put None if n/a -name = 'P7' -print('Field name = {}'.format(name)) +name = "P7" +print("Field name = {}".format(name)) ## Area of a tile in deg^2 area_tile = 0.25 @@ -38,27 +38,27 @@ ## Shape measurement method, implemented is ## 'ngix': multi-epoch model fitting ## 'galsim': stacked-image moments (experimental) -shape = 'ngmix' +shape = "ngmix" # Paths ## Input paths ### Input data directory -data_dir = '.' +data_dir = "." ### Tile IDs -path_tile_ID = f'{data_dir}/tiles_{name}.txt' +path_tile_ID = f"{data_dir}/tiles_{name}.txt" ### Weak-lensing galaxy catalog name -galaxy_cat_path = f'{data_dir}/final_cat_{name}.hdf5' -print(f'Galaxy catalogue = {galaxy_cat_path}') +galaxy_cat_path = f"{data_dir}/final_cat_{name}.hdf5" +print(f"Galaxy catalogue = {galaxy_cat_path}") ## Parameter list; optional, set to `None` if not required param_list_path = f"{data_dir}/cfis/final_cat.param" ### Star and PSF catalog name; optional, set to `None` if not required -star_cat_path = f'{data_dir}/full_starcat-0000000.fits' +star_cat_path = f"{data_dir}/full_starcat-0000000.fits" # HDU number of star and PSF catalogue hdu_star_cat = 1 @@ -69,37 +69,37 @@ ## Output paths ### Output base directory -output_dir = f'{data_dir}/sp_output' +output_dir = f"{data_dir}/sp_output" ### Galaxy shape catalogue base name. ### Will be appended by ### - '_{sh}.fits' for the basic catalogue ### - 'extended_{sh}.fits' for the extended catalogue -output_shape_cat_base= f'{output_dir}/shape_catalog' +output_shape_cat_base = f"{output_dir}/shape_catalog" ### PSF output catalogue base name. ### Will be appended by '_{sh}.fits' -output_PSF_cat_base = f'{output_dir}/psf_catalog' +output_PSF_cat_base = f"{output_dir}/psf_catalog" ### File for found tile IDs -path_found_ID = f'{output_dir}/found_ID.txt' +path_found_ID = f"{output_dir}/found_ID.txt" ### File for missing tile IDs -path_missing_ID = f'{output_dir}/missing_ID.txt' +path_missing_ID = f"{output_dir}/missing_ID.txt" ### Plot directory and subdirs -plot_dir = f'{output_dir}/plots/' +plot_dir = f"{output_dir}/plots/" ### Statistics text file -stats_file_name = 'stats_file.txt' +stats_file_name = "stats_file.txt" # Other IO options ## Input ### Coordinate column names -col_name_ra = 'XWIN_WORLD' -col_name_dec = 'YWIN_WORLD' +col_name_ra = "XWIN_WORLD" +col_name_dec = "YWIN_WORLD" ### Memory mode, set to None unless very large file mmap_mode = None @@ -107,14 +107,46 @@ ## Output ### Additional output columns -add_cols = ["FLUX_RADIUS", "FWHM_IMAGE", "FWHM_WORLD", "MAGERR_AUTO", "MAG_WIN", "MAGERR_WIN", "FLUX_AUTO", "FLUXERR_AUTO", "FLUX_APER", "FLUXERR_APER", "NGMIX_T_NOSHEAR", "NGMIX_Tpsf_NOSHEAR"] +add_cols = [ + "FLUX_RADIUS", + "FWHM_IMAGE", + "FWHM_WORLD", + "MAGERR_AUTO", + "MAG_WIN", + "MAGERR_WIN", + "FLUX_AUTO", + "FLUXERR_AUTO", + "FLUX_APER", + "FLUXERR_APER", + "NGMIX_T_NOSHEAR", + "NGMIX_Tpsf_NOSHEAR", +] ## Pre-calibration catalogue, including masked objects and mask flags -add_cols_pre_cal = ["TILE_ID", "NUMBER", "IMAFLAGS_ISO", "FLAGS", "NGMIX_MCAL_FLAGS", "NGMIX_MOM_FAIL", "N_EPOCH", "NGMIX_N_EPOCH", "NGMIX_ELL_PSFo_NOSHEAR", "NGMIX_ELL_ERR_NOSHEAR"] +add_cols_pre_cal = [ + "TILE_ID", + "NUMBER", + "IMAFLAGS_ISO", + "FLAGS", + "NGMIX_MCAL_FLAGS", + "NGMIX_MOM_FAIL", + "N_EPOCH", + "NGMIX_N_EPOCH", + "NGMIX_ELL_PSFo_NOSHEAR", + "NGMIX_ELL_ERR_NOSHEAR", +] ### Set flag columns as integer format add_cols_pre_cal_format = {} -for key in ("NUMBER", "IMAFLAGS_ISO", "FLAGS", "NGMIX_MCAL_FLAGS", "NGMIX_MOM_FAIL", "N_EPOCH", "NGMIX_N_EPOCH"): +for key in ( + "NUMBER", + "IMAFLAGS_ISO", + "FLAGS", + "NGMIX_MCAL_FLAGS", + "NGMIX_MOM_FAIL", + "N_EPOCH", + "NGMIX_N_EPOCH", +): add_cols_pre_cal_format[key] = "I" add_cols_pre_cal_format["TILE_ID"] = "A7" @@ -174,7 +206,7 @@ ### to select objects that are not too small compared to the PSF, thus not likely to be point-like, ### or to big as they seem to bias the correlation functions gal_rel_size_min = 0.5 -gal_rel_size_max = 3. +gal_rel_size_max = 3.0 ### Correct galaxy size for ellipticity gal_size_corr_ell = False diff --git a/scripts/check_footprint.py b/scripts/check_footprint.py index 86e80d91..ef098818 100755 --- a/scripts/check_footprint.py +++ b/scripts/check_footprint.py @@ -11,24 +11,16 @@ """ import sys -import os +from optparse import OptionParser -import numpy as np import healpy as hp - +import matplotlib.pylab as plt +import numpy as np from astropy.io import fits from astropy.table import Table - -import matplotlib.pylab as plt - -from optparse import OptionParser - -from unions_wl import catalogue as wl_cat - -from cs_util import logging -from cs_util import calc -from cs_util import plots from cs_util import cat as cs_cat +from cs_util import logging +from unions_wl import catalogue as wl_cat def params_default(): @@ -47,44 +39,44 @@ def params_default(): """ # Specify all parameter names and default values - input_base = 'agn' + input_base = "agn" params = { - 'input_cat': f'{input_base}.fits', - 'key_ra': 'ra', - 'key_dec': 'dec', - 'input_mask': 'mask.fits', - 'output_path': f'{input_base}_in_footprint.fits', - 'good' : 1, - 'plot': False, + "input_cat": f"{input_base}.fits", + "key_ra": "ra", + "key_dec": "dec", + "input_mask": "mask.fits", + "output_path": f"{input_base}_in_footprint.fits", + "good": 1, + "plot": False, } # Parameters which are not the default, which is ``str`` types = { - 'plot' : 'bool', - 'good' : 'int', + "plot": "bool", + "good": "int", } # Parameters which can be specified as command line option help_strings = { - 'input_cat': 'catalogue input path, default={}', - 'key_ra': 'right ascension column name, default={}', - 'key_dec': 'declination column name, default={}', - 'input_mask': 'mask input path, default={}', - 'output_path': 'output path of catalogue in footprint, default={}', - 'plot': 'create plot', - 'good': 'value of observed (good) pixels, default={}', + "input_cat": "catalogue input path, default={}", + "key_ra": "right ascension column name, default={}", + "key_dec": "declination column name, default={}", + "input_mask": "mask input path, default={}", + "output_path": "output path of catalogue in footprint, default={}", + "plot": "create plot", + "good": "value of observed (good) pixels, default={}", } # Options which have one-letter shortcuts short_options = { - 'input_cat': '-i', - 'input_mask': '-m', - 'output_dir': '-o', - 'plot': '-p', - 'good': '-g', + "input_cat": "-i", + "input_mask": "-m", + "output_dir": "-o", + "plot": "-p", + "good": "-g", } - params['input_base'] = input_base + params["input_base"] = input_base return params, short_options, types, help_strings @@ -105,35 +97,34 @@ def parse_options(p_def, short_options, types, help_strings): Command line options """ - usage = "%prog [OPTIONS]" + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) for key in p_def: if key in help_strings: - if key in short_options: short = short_options[key] else: - short = '' + short = "" if key in types: typ = types[key] else: - typ = 'string' - - if typ == 'bool': - parser.add_option( - f'{short}', - f'--{key}', - dest=key, - default=False, - action='store_true', + typ = "string" + + if typ == "bool": + parser.add_option( + f"{short}", + f"--{key}", + dest=key, + default=False, + action="store_true", help=help_strings[key].format(p_def[key]), ) else: parser.add_option( short, - f'--{key}', + f"--{key}", dest=key, type=typ, default=p_def[key], @@ -141,11 +132,7 @@ def parse_options(p_def, short_options, types, help_strings): ) parser.add_option( - '-v', - '--verbose', - dest='verbose', - action='store_true', - help=f'verbose output' + "-v", "--verbose", dest="verbose", action="store_true", help="verbose output" ) options, args = parser.parse_args() @@ -155,7 +142,7 @@ def parse_options(p_def, short_options, types, help_strings): def main(argv=None): - params, short_options, types, help_strings = params_default() + params, short_options, types, help_strings = params_default() options = parse_options(params, short_options, types, help_strings) @@ -167,35 +154,31 @@ def main(argv=None): logging.log_command(argv) # Open input catalogue - if params['verbose']: - print(f'Reading catalogue {params["input_cat"]}...') - dat = fits.getdata(params['input_cat']) - ra = dat[params['key_ra']] - dec = dat[params['key_dec']] + if params["verbose"]: + print(f"Reading catalogue {params['input_cat']}...") + dat = fits.getdata(params["input_cat"]) + ra = dat[params["key_ra"]] + dec = dat[params["key_dec"]] # Read input mask mask, nest, nside = wl_cat.read_hp_mask( - params['input_mask'], - verbose=params['verbose'] + params["input_mask"], verbose=params["verbose"] ) # Get mask pixel numbers of coordinates ipix = hp.ang2pix(nside, ra, dec, nest=nest, lonlat=True) ## Get pixels in footprint, where mask is 1 - in_footprint = (mask[ipix] == params['good']) - - # Get numbers of pixels in footprint - ipix_in_footprint = ipix[in_footprint] + in_footprint = mask[ipix] == params["good"] # Get indices of coordinates in footprint idx_np = np.where(in_footprint)[0] - if params['verbose']: + if params["verbose"]: n_in_footprint = len(idx_np) print( - f'{n_in_footprint}/{len(ra)} =' - + f' {n_in_footprint/len(ra):.2%} objects in footprint' + f"{n_in_footprint}/{len(ra)} =" + + f" {n_in_footprint / len(ra):.2%} objects in footprint" ) # Restrict data to footprint @@ -203,35 +186,40 @@ def main(argv=None): # Write data in footprint to disk t = Table(dat_in_footprint) - if params['verbose']: - print(f'Writing objects in footprint to {params["output_path"]}') + if params["verbose"]: + print(f"Writing objects in footprint to {params['output_path']}") cols = [] - for col_ind,key in enumerate(t.keys()): - cols.append(fits.Column(name=key, array=t[key], format=dat_in_footprint.columns[col_ind].format)) + for col_ind, key in enumerate(t.keys()): + cols.append( + fits.Column( + name=key, array=t[key], format=dat_in_footprint.columns[col_ind].format + ) + ) cs_cat.write_fits_BinTable_file(cols, params["output_path"]) # Create plots - if params['plot']: - out_path_plot = f'{params["input_base"]}.png' + if params["plot"]: + out_path_plot = f"{params['input_base']}.png" # Coordinates in footprint with mask point_size = 0.02 - if params['verbose']: - print(f'Creating plot {out_path_plot}...') + if params["verbose"]: + print(f"Creating plot {out_path_plot}...") ra_center_deg = 151 - hp.mollview(mask, rot=(ra_center_deg, 0, 0)) + hp.mollview(mask, rot=(ra_center_deg, 0, 0)) hp.projscatter( - dat_in_footprint['RA'], - dat_in_footprint['dec'], + dat_in_footprint["RA"], + dat_in_footprint["dec"], s=point_size, - color='g', + color="g", lonlat=True, ) - plt.savefig(out_path_plot) - plt.close() + plt.savefig(out_path_plot) + plt.close() return 0 -if __name__ == '__main__': + +if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/scripts/check_tile_IDs_SP_LF.py b/scripts/check_tile_IDs_SP_LF.py index 10b82e3b..54f23fda 100644 --- a/scripts/check_tile_IDs_SP_LF.py +++ b/scripts/check_tile_IDs_SP_LF.py @@ -1,66 +1,61 @@ -import numpy as np -import sys import re +import sys + +import numpy as np + def main(argv=None): - survey = 'v1' + survey = "v1" - if survey == 'v1': + if survey == "v1": n_patch = 7 - patches = [f'P{x}' for x in np.arange(n_patch) + 1] + patches = [f"P{x}" for x in np.arange(n_patch) + 1] - IDs_sp_base = 'found_ID_wshapes.txt' - tile_ID_gal_counts_sp_base = 'tile_id_gal_counts_ngmix.txt' + IDs_sp_base = "found_ID_wshapes.txt" + tile_ID_gal_counts_sp_base = "tile_id_gal_counts_ngmix.txt" n_SP_not_in_LF_all = 0 n_LF_not_in_SP_all = 0 for patch in patches: - print(patch) # ShapePipe - path = f'{patch}/sp_output/{IDs_sp_base}' + path = f"{patch}/sp_output/{IDs_sp_base}" with open(path) as f: dat = f.readlines() ID_SP = [] for line in dat: ID_SP.append(line.rstrip()) - print(f' #SP = {len(ID_SP)}') + print(f" #SP = {len(ID_SP)}") # LensFit - path = f'CFIS3500_THELI_{patch}.list' + path = f"CFIS3500_THELI_{patch}.list" with open(path) as f: dat = f.readlines() ID_LF = [] for line in dat: - m = re.match('.*CFIS\.(\d{3}\.\d{3})\.r', line) + m = re.match(r".*CFIS\.(\d{3}\.\d{3})\.r", line) if m: ID_LF.append(m[1].rstrip()) - print(f' #LF = {len(ID_LF)}') + print(f" #LF = {len(ID_LF)}") # tile stats for SP - dat = np.loadtxt(f'{patch}/sp_output/{tile_ID_gal_counts_sp_base}') + dat = np.loadtxt(f"{patch}/sp_output/{tile_ID_gal_counts_sp_base}") tile_ID = [] for my_ID in dat[:, 0]: - tile_ID.append(f'{my_ID:07.3f}') - n_det = dat[:, 1] - n_gal = dat[:, 2] - n_shape = dat[:, 3] + tile_ID.append(f"{my_ID:07.3f}") # ShapePipe tiles not contained in LensFit n_SP_not_in_LF = 0 for ID in ID_SP: if ID not in ID_LF: # Print number of galaxies on those tiles not contained in LF - if ID in tile_ID: - idx = tile_ID.index(ID) - #print(ID, tile_ID[idx], n_det[idx], n_gal[idx], n_shape[idx]) n_SP_not_in_LF += 1 if n_SP_not_in_LF == -1: - print(f' SP {ID} not in LF') + print(f" SP {ID} not in LF") n_SP_not_in_LF_all += n_SP_not_in_LF # LensFit tiles not contained in ShapePipe @@ -69,20 +64,20 @@ def main(argv=None): if ID not in ID_SP: n_LF_not_in_SP += 1 if n_LF_not_in_SP == -1: - print(f' LF {ID} not in SP') - print(f' [{ID}]') + print(f" LF {ID} not in SP") + print(f" [{ID}]") n_LF_not_in_SP_all += n_LF_not_in_SP - print(f' # SP not in LF = {n_SP_not_in_LF}') - print(f' # LF not in SP = {n_LF_not_in_SP}') + print(f" # SP not in LF = {n_SP_not_in_LF}") + print(f" # LF not in SP = {n_LF_not_in_SP}") print() - print('All patches') - print(f' # SP not in LF = {n_SP_not_in_LF_all}') - print(f' # LF not in SP = {n_LF_not_in_SP_all}') + print("All patches") + print(f" # SP not in LF = {n_SP_not_in_LF_all}") + print(f" # LF not in SP = {n_LF_not_in_SP_all}") return 0 - + + if __name__ == "__main__": sys.exit(main(sys.argv)) - diff --git a/scripts/combine_hp_masks.py b/scripts/combine_hp_masks.py index 216c9beb..ba88a344 100755 --- a/scripts/combine_hp_masks.py +++ b/scripts/combine_hp_masks.py @@ -8,29 +8,24 @@ """ -import sys import re +import sys +import warnings from glob import glob from optparse import OptionParser -import numpy as np - import matplotlib.pylab as plt - +import numpy as np from astropy.io import fits from astropy.table import Table -from astropy import wcs - -import warnings from astropy.wcs import FITSFixedWarning -warnings.filterwarnings('ignore', category=FITSFixedWarning) - -import healpy as hp -from reproject import reproject_from_healpix, reproject_to_healpix -from tqdm import tqdm +warnings.filterwarnings("ignore", category=FITSFixedWarning) +import healpy as hp from cs_util import logging +from reproject import reproject_to_healpix +from tqdm import tqdm def params_default(): @@ -50,54 +45,50 @@ def params_default(): """ # Specify all parameter names and default values params = { - 'input_tile_IDs': 'sp_output/found_ID_wshapes.txt', - 'input_dir_flags': 'output/run_sp_combined_flag/mask_runner/output', - 'input_dir_images': ( - 'output/run_sp_combined_image/get_images_runner/output' - ), - 'nside': 1024, - 'out_path': 'mask.fits', - 'out_path_plot': 'mask.png', - 'out_path2': None, - 'plot': '0', - 'verbose': False, + "input_tile_IDs": "sp_output/found_ID_wshapes.txt", + "input_dir_flags": "output/run_sp_combined_flag/mask_runner/output", + "input_dir_images": ("output/run_sp_combined_image/get_images_runner/output"), + "nside": 1024, + "out_path": "mask.fits", + "out_path_plot": "mask.png", + "out_path2": None, + "plot": "0", + "verbose": False, } # Parameters which are not the default, which is ``str`` types = { - 'nside': 'int', - 'plot': 'int', - 'verbose': 'bool', + "nside": "int", + "plot": "int", + "verbose": "bool", } # Parameters which can be specified as command line option help_strings = { - 'input_tile_IDs': 'input path of tile IDs, default={}', - 'input_dir_flags': ( - 'input directory for pipeline flag files, default={}' - ), - 'input_dir_images': ( - 'input directory for images (headers with WCS, default={}' + "input_tile_IDs": "input path of tile IDs, default={}", + "input_dir_flags": ("input directory for pipeline flag files, default={}"), + "input_dir_images": ( + "input directory for images (headers with WCS, default={}" ), - 'nside': 'Output resolution parameter, default={}', - 'out_path': ( - 'hp mask path; output if \'-p 0 or 1\';' - + 'input if \'-p 2\'), can be list, default={}' + "nside": "Output resolution parameter, default={}", + "out_path": ( + "hp mask path; output if '-p 0 or 1';" + + "input if '-p 2'), can be list, default={}" ), - 'out_path2': 'hp mask path on output if \'-p 2', - 'out_path_plot': 'output path for plot', - 'plot': '0: no plot; 1: create plot; 2: plot only, read mask file', - 'verbose': 'verbose output if True', + "out_path2": "hp mask path on output if '-p 2", + "out_path_plot": "output path for plot", + "plot": "0: no plot; 1: create plot; 2: plot only, read mask file", + "verbose": "verbose output if True", } # Options which have one-letter shortcuts short_options = { - 'input_tile_IDs': '-i', - 'nside': '-n', - 'out_path': '-o', - 'out_path_plot': '-O', - 'plot': '-p', - 'verbose': '-v', + "input_tile_IDs": "-i", + "nside": "-n", + "out_path": "-o", + "out_path_plot": "-O", + "plot": "-p", + "verbose": "-v", } return params, short_options, types, help_strings @@ -105,7 +96,7 @@ def params_default(): def parse_options(p_def, short_options, types, help_strings): """Parse Options. - + Parse command line options. Parameters @@ -121,35 +112,34 @@ def parse_options(p_def, short_options, types, help_strings): Command line options """ - usage = "%prog [OPTIONS]" + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) for key in p_def: if key in help_strings: - if key in short_options: short = short_options[key] else: - short = '' + short = "" if key in types: typ = types[key] else: - typ = 'string' + typ = "string" - if typ == 'bool': + if typ == "bool": parser.add_option( - f'{short}', - f'--{key}', + f"{short}", + f"--{key}", dest=key, default=False, - action='store_true', + action="store_true", help=help_strings[key].format(p_def[key]), ) else: parser.add_option( - f'{short}', - f'--{key}', + f"{short}", + f"--{key}", dest=key, type=typ, default=p_def[key], @@ -181,12 +171,13 @@ def read_list(fname, col=None): """ if col is None: - f = open(fname, 'r', encoding='latin1') + f = open(fname, "r", encoding="latin1") file_list = [x.strip() for x in f.readlines()] f.close() else: import pandas as pd - dat = pd.read_csv(fname, sep=r'\s+', dtype='string', header=None) + + dat = pd.read_csv(fname, sep=r"\s+", dtype="string", header=None) if col not in dat: col = int(col) file_list = dat[col] @@ -214,32 +205,31 @@ def read_pixel_mask_files(ID_arr, params): healpix mask information per tile """ - if params['verbose']: - print('Reading pixel mask files...') + if params["verbose"]: + print("Reading pixel mask files...") mask_1d = {} - for ID in tqdm(ID_arr, disable=not params['verbose']): - + for ID in tqdm(ID_arr, disable=not params["verbose"]): # Transform ID to file string format - m = re.search(r'(\d{3}).{1}(\d{3})', ID) - ID_file = f'{m[1]}-{m[2]}' + m = re.search(r"(\d{3}).{1}(\d{3})", ID) + ID_file = f"{m[1]}-{m[2]}" # Read mask file - path = f'{params["input_dir_flags"]}/pipeline_flag-{ID_file}.fits' + path = f"{params['input_dir_flags']}/pipeline_flag-{ID_file}.fits" hdu_mask = fits.open(path) mask = hdu_mask[0].data hdu_mask.close() # Get masked region indices (>= 1) and set to 0 - w_masked = (mask != 0) + w_masked = mask != 0 mask[w_masked] = 0 # Get observed regions (0) and set to 1 - w_observed = (mask == 0) + w_observed = mask == 0 mask[w_observed] = 1 # Read image header file - img = open(f'{params["input_dir_images"]}/CFIS_image-{ID_file}.fits') + img = open(f"{params['input_dir_images']}/CFIS_image-{ID_file}.fits") header = fits.Header.fromtextfile(img) img.close() @@ -247,12 +237,12 @@ def read_pixel_mask_files(ID_arr, params): # Default ordering is RING (nested=False) mask_1d[ID], footprint = reproject_to_healpix( (mask, header), - 'c', - nside=params['nside'], + "c", + nside=params["nside"], ) # Get region outside footprint and set to 0 - w_outside = (footprint == 0) + w_outside = footprint == 0 mask_1d[ID][w_outside] = 0 return mask_1d @@ -281,26 +271,25 @@ class Table : # Add up all mask values idx = 0 if verbose: - print('Combining mask tile information...') + print("Combining mask tile information...") for ID in tqdm(mask_1d, disable=not verbose): - if idx == 0: - t['flux'] = mask_1d[ID] + t["flux"] = mask_1d[ID] else: - t['flux'] += mask_1d[ID] + t["flux"] += mask_1d[ID] idx += 1 # Set all multiply added pixels back to 1 - w = t['flux'] > 1 - t['flux'][w] = 1 + w = t["flux"] > 1 + t["flux"][w] = 1 # Exchange 0 <-> 1 - w_ok = (t['flux'] == 1) - w_nok = (t['flux'] == 0) + w_ok = t["flux"] == 1 + w_nok = t["flux"] == 0 - t['flux'][w_ok] = 0 - t['flux'][w_nok] = 1 + t["flux"][w_ok] = 0 + t["flux"][w_nok] = 1 return t @@ -321,13 +310,13 @@ def write_combined_hp_mask(t, nside, output_path): """ # Set table header - t.meta['ORDERING'] = 'RING' - t.meta['COORDSYS'] = 'G' - t.meta['NSIDE'] = nside - t.meta['INDXSCHM'] = 'IMPLICIT' + t.meta["ORDERING"] = "RING" + t.meta["COORDSYS"] = "G" + t.meta["NSIDE"] = nside + t.meta["INDXSCHM"] = "IMPLICIT" # Write to file - t.write(output_path, overwrite=True, format='fits') + t.write(output_path, overwrite=True, format="fits") def main(argv=None): @@ -348,41 +337,38 @@ def main(argv=None): logging.log_command(argv) # Create mask - if params['plot'] != 2: - + if params["plot"] != 2: # For mask FITS file on output, make sure there are not # multiple output names # Get ID list - ID_arr = read_list(params['input_tile_IDs']) + ID_arr = read_list(params["input_tile_IDs"]) # Read mask and header files, project to healpix mask_1d = read_pixel_mask_files(ID_arr, params) # Create output mask as Table - t = combine_hp_masks(mask_1d, verbose=params['verbose']) + t = combine_hp_masks(mask_1d, verbose=params["verbose"]) write_combined_hp_mask( t, - params['nside'], + params["nside"], params["out_path"], ) # Plot mask - if params['plot'] > 0: - - if params['verbose']: - print('Creating plot of combined mask(s)...') + if params["plot"] > 0: + if params["verbose"]: + print("Creating plot of combined mask(s)...") # Initialise empty mask, set all pixels to 1=unobserved - mask_all = np.ones(shape=(hp.nside2npix(params['nside']))) + mask_all = np.ones(shape=(hp.nside2npix(params["nside"]))) # Combine input masks - out_path_arr = glob(f'{params["out_path"]}') + out_path_arr = glob(f"{params['out_path']}") for out_path in out_path_arr: - - if params['verbose']: - print(f'Reading file {out_path}...') + if params["verbose"]: + print(f"Reading file {out_path}...") # Read mask and multiply to cumulative mask. # Observed pixels (value=0) will be set to 0. @@ -392,12 +378,12 @@ def main(argv=None): # Correct plot for LF masks hp.mollview(mask_all, rot=(151, 0, 0)) - plt.savefig(params['out_path_plot']) + plt.savefig(params["out_path_plot"]) plt.close() # Write combined map as FITS File - if params['out_path2']: - hp.write_map(params['out_path2'], mask_all, overwrite=True) + if params["out_path2"]: + hp.write_map(params["out_path2"], mask_all, overwrite=True) return 0 diff --git a/scripts/combine_results.py b/scripts/combine_results.py index 66289a24..636d5c07 100755 --- a/scripts/combine_results.py +++ b/scripts/combine_results.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 -import sys import os import re +import sys import numpy as np import uncertainties as unc -from pathlib import Path from astropy.io import ascii @@ -19,7 +18,7 @@ def get_match(stats_files, patch, pattern, previous=None, n_previous=[1], typ=st m = re.search(pattern, line) if m: if (previous and prev_ok) or not previous: - if typ == 'ufloat': + if typ == "ufloat": u = unc.ufloat_fromstr(m[1]) return u.nominal_value, u.std_dev else: @@ -28,212 +27,207 @@ def get_match(stats_files, patch, pattern, previous=None, n_previous=[1], typ=st if previous: prev_ok = False for prev, n_prev in zip(previous, n_previous): - # Line index n_previous earlier, +1 since next line will be read in - # next loop - idx_prev = idx - n_prev + 1 - - # Look for pattern in previous line + # next loop; look for pattern in previous line m_prev = re.search(prev, stats_files[patch][idx - n_prev + 1]) if m_prev: prev_ok = True - raise ValueError(f'No match of \'{pattern}\' in patch {patch} (prev=\'{previous}\'), n_prev={n_previous}') + raise ValueError( + f"No match of '{pattern}' in patch {patch} (prev='{previous}'), n_prev={n_previous}" + ) def read_stats_files(patches, path, verbose=False): stats_files = {} for p in patches: - - fname = f'{p}/{path}' + fname = f"{p}/{path}" if os.path.exists(fname): if verbose: - print(f'Reading stats file \'{fname}\'') + print(f"Reading stats file '{fname}'") with open(fname) as f: stats_files[p] = f.readlines() else: - print(f'Warning: stats file {fname} not found') + print(f"Warning: stats file {fname} not found") if len(stats_files) == 0: - raise ValueError('No stats file found') + raise ValueError("No stats file found") - print(f'Found {len(stats_files)} stats files') + print(f"Found {len(stats_files)} stats files") return stats_files def combine(results): - for key in results['type']: - + for key in results["type"]: # Values - values = np.array(list(results['value'][key].values())) + values = np.array(list(results["value"][key].values())) - if results['type'][key] == 'sum': + if results["type"][key] == "sum": # Compute sum - results['all'][key] = sum(values) - - elif results['type'][key] == 'avg': + results["all"][key] = sum(values) + elif results["type"][key] == "avg": # Compute average - results['all'][key] = sum(values) / len(values) + results["all"][key] = sum(values) / len(values) - elif results['type'][key] == 'w_avg': + elif results["type"][key] == "w_avg": # Weight key - key_w = results['extra'][key] + key_w = results["extra"][key] # Weight values - w = np.array(list(results['value'][key_w].values())) + w = np.array(list(results["value"][key_w].values())) # Compute weighted average s = sum(w) if s > 0: - results['all'][key] = sum(values * w) / s + results["all"][key] = sum(values * w) / s else: - raise ZeroDivisionError(f'sum(w) = {s} for key={key}, key_w={key_w}') + raise ZeroDivisionError(f"sum(w) = {s} for key={key}, key_w={key_w}") - elif results['type'][key] == 'sqr_w_avg': + elif results["type"][key] == "sqr_w_avg": # Weight key - key_w = results['extra'][key] + key_w = results["extra"][key] # Weight values - w = np.array(list(results['value'][key_w].values())) + w = np.array(list(results["value"][key_w].values())) # Square values - v_sqr = values * values + v_sqr = values * values # Compute weighted average res_tmp = sum(v_sqr * w) / sum(w) # Take square root - results['all'][key] = np.sqrt(res_tmp) + results["all"][key] = np.sqrt(res_tmp) - elif results['type'][key] == 'var': + elif results["type"][key] == "var": # Combined variance # Weight key - key_w = results['extra'][key][0] + key_w = results["extra"][key][0] # Mean key - key_m = results['extra'][key][1] + key_m = results["extra"][key][1] # Weight values - w = np.array(list(results['value'][key_w].values())) + w = np.array(list(results["value"][key_w].values())) # Patch mean values - m = np.array(list(results['value'][key_m].values())) + m = np.array(list(results["value"][key_m].values())) # Overall mean - m_all = results['all'][key_m] + m_all = results["all"][key_m] # Square values - v_sqr = values * values + v_sqr = values * values # Compute weighted average - res_tmp = sum( (v_sqr + (m - m_all)**2) * w ) / sum(w) + res_tmp = sum((v_sqr + (m - m_all) ** 2) * w) / sum(w) # Take square root - results['all'][key] = np.sqrt(res_tmp) + results["all"][key] = np.sqrt(res_tmp) - elif results['type'][key] == 'var_m': + elif results["type"][key] == "var_m": # Combined variance of mean # Weight key - key_w = results['extra'][key][0] + key_w = results["extra"][key][0] # Mean key - key_m = results['extra'][key][1] + key_m = results["extra"][key][1] # Weight values - w = np.array(list(results['value'][key_w].values())) + w = np.array(list(results["value"][key_w].values())) # Patch mean values - m = np.array(list(results['value'][key_m].values())) + m = np.array(list(results["value"][key_m].values())) # Overall mean - m_all = results['all'][key_m] + m_all = results["all"][key_m] # Square values v_sqr = values * values # Compute weighted average, note extra factor of w in v_sqr - res_tmp = sum( (v_sqr * w + (m - m_all)**2) * w ) / sum(w) + res_tmp = sum((v_sqr * w + (m - m_all) ** 2) * w) / sum(w) # Take square root - results['all'][key] = np.sqrt(res_tmp / sum(w)) + results["all"][key] = np.sqrt(res_tmp / sum(w)) def init_key(results, key, typ, extra=None): - results['type'][key] = typ - results['value'][key] = {} + results["type"][key] = typ + results["value"][key] = {} if extra: - results['extra'][key] = extra + results["extra"][key] = extra -def print_all(results, stats_files, use_keys=None, fout=sys.stdout, header=True, all=True): +def print_all( + results, stats_files, use_keys=None, fout=sys.stdout, header=True, all=True +): if use_keys: keys = use_keys else: - keys = results['type'] + keys = results["type"] # Header if header: - print('# patch', ' '*3, end=' ', file=fout) + print("# patch", " " * 3, end=" ", file=fout) for key in keys: - print(f'{key:>11s}', end=' ', file=fout) + print(f"{key:>11s}", end=" ", file=fout) print(file=fout) # Loop over patches for patch in stats_files: - - print(f'{patch:11s}', end=' ', file=fout) + print(f"{patch:11s}", end=" ", file=fout) # Write value for each key for key in keys: - val = results['value'][key][patch] - if key == 'N_gal': - print(f'{val:>11.0f}', end=' ', file=fout) - elif key in ('w_tot', 'n_gal_am2'): - print(f'{val:>11.2f}', end=' ', file=fout) - elif key in ['xi_sys_p', 'xi_sys_m']: - print(f'{val:>11.3e}', end=' ', file=fout) + val = results["value"][key][patch] + if key == "N_gal": + print(f"{val:>11.0f}", end=" ", file=fout) + elif key in ("w_tot", "n_gal_am2"): + print(f"{val:>11.2f}", end=" ", file=fout) + elif key in ["xi_sys_p", "xi_sys_m"]: + print(f"{val:>11.3e}", end=" ", file=fout) else: - print(f'{val:>11.7f}', end=' ', file=fout) + print(f"{val:>11.7f}", end=" ", file=fout) print(file=fout) # Write total if all: - p = 'all' + p = "all" if p in results: - print(f'{p:11s}', end=' ', file=fout) + print(f"{p:11s}", end=" ", file=fout) for key in keys: val = results[p][key] - if key == 'N_gal': - print(f'{val:>11d}', end=' ', file=fout) - elif key == 'w_tot': - print(f'{val:>11.1f}', end=' ', file=fout) + if key == "N_gal": + print(f"{val:>11d}", end=" ", file=fout) + elif key == "w_tot": + print(f"{val:>11.1f}", end=" ", file=fout) else: - print(f'{val:>11.7f}', end=' ', file=fout) - #print(f'{val:>11.3e}', end=' ', file=fout) + print(f"{val:>11.7f}", end=" ", file=fout) + # print(f'{val:>11.3e}', end=' ', file=fout) print(file=fout) def get_area(fname): if os.path.exists(fname): - with open(fname) as f: lines = f.readlines() for line in lines: - m = re.search('nmasked patch area without overlap = (.*) deg', line) + m = re.search("nmasked patch area without overlap = (.*) deg", line) if m: return float(m[1]) else: - print(f'Warning: No file {fname} found to obtain area') + print(f"Warning: No file {fname} found to obtain area") return 1 @@ -257,281 +251,496 @@ def get_values(results, stats_files, shape, use_keys, area_deg2=-1): for each patch) """ # Number of galaxies - key = 'N_gal' + key = "N_gal" if use_keys[key]: - init_key(results, key, 'sum') + init_key(results, key, "sum") for patch in stats_files: - results['value'][key][patch] = get_match(stats_files, patch, 'Number of galaxies after metacal = (\d+)/', previous=[f'^{shape}$'], typ=int) + results["value"][key][patch] = get_match( + stats_files, + patch, + r"Number of galaxies after metacal = (\d+)/", + previous=[f"^{shape}$"], + typ=int, + ) area_deg2_tot = 0 - key_der = 'n_gal_am2' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "n_gal_am2" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: if area_deg2 < 0: - area_deg2_patch = get_area(f'{patch}/area.txt') + area_deg2_patch = get_area(f"{patch}/area.txt") area_deg2_tot += area_deg2_patch - print(f'area({patch}) = {area_deg2_patch} deg^2') + print(f"area({patch}) = {area_deg2_patch} deg^2") else: area_deg2_patch = area_deg2 - results['value'][key_der][patch] = results['value']['N_gal'][patch] / (area_deg2_patch * 3600) + results["value"][key_der][patch] = results["value"]["N_gal"][patch] / ( + area_deg2_patch * 3600 + ) if area_deg2 < 0: - with open('area_deg2_tot.txt', 'w') as f: + with open("area_deg2_tot.txt", "w") as f: print(area_deg2_tot, file=f) # Sum of weights - key = 'w_tot' + key = "w_tot" if use_keys[key]: - init_key(results, key, 'sum') + init_key(results, key, "sum") for patch in stats_files: - results['value'][key][patch] = get_match(stats_files, patch, 'Sum of weights = (\S+)', typ=float, previous=[f'^{shape}$']) + results["value"][key][patch] = get_match( + stats_files, + patch, + r"Sum of weights = (\S+)", + typ=float, + previous=[f"^{shape}$"], + ) # Additive bias (unweighted mean) - key_base = 'c_' + key_base = "c_" if use_keys[key_base]: for comp in (1, 2): - key = f'{key_base}{comp}' - init_key(results, key, 'w_avg', extra='N_gal') + key = f"{key_base}{comp}" + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - c = get_match(stats_files, patch, f'{key_base}{comp} = (\S+)', previous=[f'^{shape}:$'], n_previous=[2*comp - 1], typ=float) - results['value'][key][patch] = c + c = get_match( + stats_files, + patch, + rf"{key_base}{comp} = (\S+)", + previous=[f"^{shape}:$"], + n_previous=[2 * comp - 1], + typ=float, + ) + results["value"][key][patch] = c # Additive bias (unweighted error) - key_base = 'dc_' + key_base = "dc_" if use_keys[key_base]: for comp in (1, 2): - key = f'{key_base}{comp}' - init_key(results, key, 'var', extra=['N_gal', f'c_{comp}']) + key = f"{key_base}{comp}" + init_key(results, key, "var", extra=["N_gal", f"c_{comp}"]) for patch in stats_files: - dc = get_match(stats_files, patch, f'{key_base}{comp} = (\S+)', typ=float, previous=[f'^{shape}:$'], n_previous=[2*comp + 7]) - results['value'][key][patch] = dc - + dc = get_match( + stats_files, + patch, + rf"{key_base}{comp} = (\S+)", + typ=float, + previous=[f"^{shape}:$"], + n_previous=[2 * comp + 7], + ) + results["value"][key][patch] = dc # Additive bias (unweighted error of mean) - key_base = 'dmc_' + key_base = "dmc_" if use_keys[key_base]: for comp in (1, 2): - key = f'{key_base}{comp}' - init_key(results, key, 'var_m', extra=['N_gal', f'c_{comp}']) + key = f"{key_base}{comp}" + init_key(results, key, "var_m", extra=["N_gal", f"c_{comp}"]) for patch in stats_files: - dmc = get_match(stats_files, patch, f'{key_base}{comp} = (\S+)', typ=float, previous=[f'^{shape}:$'], n_previous=[2*comp + 7]) - results['value'][key][patch] = dmc + dmc = get_match( + stats_files, + patch, + rf"{key_base}{comp} = (\S+)", + typ=float, + previous=[f"^{shape}:$"], + n_previous=[2 * comp + 7], + ) + results["value"][key][patch] = dmc # Additive bias (weighted mean) - key_base = 'cw_' + key_base = "cw_" if use_keys[key_base]: for comp in (1, 2): - key = f'{key_base}{comp}' - init_key(results, key, 'w_avg', extra='w_tot') + key = f"{key_base}{comp}" + init_key(results, key, "w_avg", extra="w_tot") for patch in stats_files: - c = get_match(stats_files, patch, f'{key_base}{comp} = (\S+)', previous=[f'^{shape}:$'], n_previous=[comp*2], typ=float) - results['value'][key][patch] = c + c = get_match( + stats_files, + patch, + rf"{key_base}{comp} = (\S+)", + previous=[f"^{shape}:$"], + n_previous=[comp * 2], + typ=float, + ) + results["value"][key][patch] = c # Additive bias (weighted error of mean) - key_base = 'dmcw_' + key_base = "dmcw_" if use_keys[key_base]: for comp in (1, 2): - key = f'{key_base}{comp}' - init_key(results, key, 'var_m', extra=['N_gal', f'cw_{comp}']) + key = f"{key_base}{comp}" + init_key(results, key, "var_m", extra=["N_gal", f"cw_{comp}"]) for patch in stats_files: - dmc = get_match(stats_files, patch, f'{key_base}{comp} = (\S+)', typ=float, previous=[f'^{shape}:$'], n_previous=[2*comp + 8]) - results['value'][key][patch] = dmc + dmc = get_match( + stats_files, + patch, + rf"{key_base}{comp} = (\S+)", + typ=float, + previous=[f"^{shape}:$"], + n_previous=[2 * comp + 8], + ) + results["value"][key][patch] = dmc # Additive bias (jackknife) - key_base = 'cjk_' + key_base = "cjk_" if use_keys[key_base]: for comp in (1, 2): - # Mean - key = f'{key_base}{comp}' + key = f"{key_base}{comp}" key_m = key - init_key(results, key, 'w_avg', extra='w_tot') + init_key(results, key, "w_avg", extra="w_tot") # Error - key = f'd{key_base}{comp}' + key = f"d{key_base}{comp}" key_s = key - init_key(results, key, 'var_m', extra=['w_tot', f'cjk_{comp}']) + init_key(results, key, "var_m", extra=["w_tot", f"cjk_{comp}"]) for patch in stats_files: - c, dc = get_match(stats_files, patch, f'{key_base}{comp} = (\S+)', previous=[f'^{shape}:$'], n_previous=[comp], typ='ufloat') - results['value'][key_m][patch] = c - results['value'][key_s][patch] = dc + c, dc = get_match( + stats_files, + patch, + rf"{key_base}{comp} = (\S+)", + previous=[f"^{shape}:$"], + n_previous=[comp], + typ="ufloat", + ) + results["value"][key_m][patch] = c + results["value"][key_s][patch] = dc # Ellipticity dispersion - key = 'sigma2_epsilon' + key = "sigma2_epsilon" if use_keys[key]: - init_key(results, key, 'w_avg', extra='N_gal') + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key][patch] = get_match(stats_files, patch, 'Dispersion of complex ellipticity = (\S+)', previous=[f'^{shape}$'], typ=float) + results["value"][key][patch] = get_match( + stats_files, + patch, + r"Dispersion of complex ellipticity = (\S+)", + previous=[f"^{shape}$"], + typ=float, + ) # Galaxy total response matrix - key_base = 'R_tot_' + key_base = "R_tot_" if use_keys[key_base]: - keys = [f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22'] + keys = [f"{key_base}11", f"{key_base}12", f"{key_base}21", f"{key_base}22"] for key in keys: - init_key(results, key, 'w_avg', extra='N_gal') + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - tmp = get_match(stats_files, patch, '\[\[(\s?\S+)\s+\S+]', previous=['ngmix galaxies:', 'total response matrix:'], n_previous=[2, 1], typ=float) - results['value'][keys[0]][patch] = tmp - tmp = get_match(stats_files, patch, '\[\[\s?\S+\s+(\S+)]', previous=['ngmix galaxies:', 'total response matrix:'], n_previous=[2, 1], typ=float) - results['value'][keys[1]][patch] = tmp - tmp = get_match(stats_files, patch, '\[(\s?\S+)\s+\S+\]\]', previous=['ngmix galaxies', 'total response matrix:'], n_previous=[3, 2], typ=float) - results['value'][keys[2]][patch] = tmp - tmp = get_match(stats_files, patch, ' \[\s?\S+\s+(\S+)\]\]', previous=['ngmix galaxies:', 'total response matrix:'], n_previous=[3, 2], typ=float) - results['value'][keys[3]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[\[(\s?\S+)\s+\S+]", + previous=["ngmix galaxies:", "total response matrix:"], + n_previous=[2, 1], + typ=float, + ) + results["value"][keys[0]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[\[\s?\S+\s+(\S+)]", + previous=["ngmix galaxies:", "total response matrix:"], + n_previous=[2, 1], + typ=float, + ) + results["value"][keys[1]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[(\s?\S+)\s+\S+\]\]", + previous=["ngmix galaxies", "total response matrix:"], + n_previous=[3, 2], + typ=float, + ) + results["value"][keys[2]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r" \[\s?\S+\s+(\S+)\]\]", + previous=["ngmix galaxies:", "total response matrix:"], + n_previous=[3, 2], + typ=float, + ) + results["value"][keys[3]][patch] = tmp # Normalised trace = mean diagonal - key_der = 'trN_R_tot' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "trN_R_tot" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key_der][patch] = (results['value']['R_tot_11'][patch] + results['value']['R_tot_22'][patch]) / 2 + results["value"][key_der][patch] = ( + results["value"]["R_tot_11"][patch] + + results["value"]["R_tot_22"][patch] + ) / 2 # Sum of absolute off-diagonal - key_der = 'abs_off_R_tot' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "abs_off_R_tot" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key_der][patch] = np.abs(results['value']['R_tot_12'][patch]) + np.abs(results['value']['R_tot_21'][patch]) + results["value"][key_der][patch] = np.abs( + results["value"]["R_tot_12"][patch] + ) + np.abs(results["value"]["R_tot_21"][patch]) # Galaxy shear response matrix - key_base = 'R_shear_' + key_base = "R_shear_" if use_keys[key_base]: - keys = [f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22'] + keys = [f"{key_base}11", f"{key_base}12", f"{key_base}21", f"{key_base}22"] for key in keys: - init_key(results, key, 'w_avg', extra='N_gal') + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - tmp = get_match(stats_files, patch, '\[\[(\s?\S+)\s+\S+]', previous=['ngmix galaxies:', 'shear response matrix:'], n_previous=[5, 1], typ=float) - results['value'][keys[0]][patch] = tmp - tmp = get_match(stats_files, patch, '\[\[\s?\S+\s+(\S+)]', previous=['ngmix galaxies:', 'shear response matrix:'], n_previous=[5, 1], typ=float) - results['value'][keys[1]][patch] = tmp - tmp = get_match(stats_files, patch, '\[(\s?\S+)\s+\S+\]\]', previous=['ngmix galaxies', 'shear response matrix:'], n_previous=[6, 2], typ=float) - results['value'][keys[2]][patch] = tmp - tmp = get_match(stats_files, patch, ' \[\s?\S+\s+(\S+)\]\]', previous=['ngmix galaxies:', 'shear response matrix:'], n_previous=[6, 2], typ=float) - results['value'][keys[3]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[\[(\s?\S+)\s+\S+]", + previous=["ngmix galaxies:", "shear response matrix:"], + n_previous=[5, 1], + typ=float, + ) + results["value"][keys[0]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[\[\s?\S+\s+(\S+)]", + previous=["ngmix galaxies:", "shear response matrix:"], + n_previous=[5, 1], + typ=float, + ) + results["value"][keys[1]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[(\s?\S+)\s+\S+\]\]", + previous=["ngmix galaxies", "shear response matrix:"], + n_previous=[6, 2], + typ=float, + ) + results["value"][keys[2]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r" \[\s?\S+\s+(\S+)\]\]", + previous=["ngmix galaxies:", "shear response matrix:"], + n_previous=[6, 2], + typ=float, + ) + results["value"][keys[3]][patch] = tmp # Normalised trace = mean diagonal - key_der = 'trN_R_shear' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "trN_R_shear" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key_der][patch] = (results['value']['R_shear_11'][patch] + results['value']['R_shear_22'][patch]) / 2 + results["value"][key_der][patch] = ( + results["value"]["R_shear_11"][patch] + + results["value"]["R_shear_22"][patch] + ) / 2 # Sum of absolute off-diagonal - key_der = 'abs_off_R_shear' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "abs_off_R_shear" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key_der][patch] = np.abs(results['value']['R_shear_12'][patch]) + np.abs(results['value']['R_shear_21'][patch]) - + results["value"][key_der][patch] = np.abs( + results["value"]["R_shear_12"][patch] + ) + np.abs(results["value"]["R_shear_21"][patch]) # Galaxy selection response matrix - key_base = 'R_select_' + key_base = "R_select_" if use_keys[key_base]: - keys = [f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22'] + keys = [f"{key_base}11", f"{key_base}12", f"{key_base}21", f"{key_base}22"] for key in keys: - init_key(results, key, 'w_avg', extra='N_gal') + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - tmp = get_match(stats_files, patch, '\[\[(\s?\S+)\s+\S+]', previous=['ngmix galaxies:', 'selection response matrix:'], n_previous=[8, 1], typ=float) - results['value'][keys[0]][patch] = tmp - tmp = get_match(stats_files, patch, '\[\[\s?\S+\s+(\S+)]', previous=['ngmix galaxies:', 'selection response matrix:'], n_previous=[8, 1], typ=float) - results['value'][keys[1]][patch] = tmp - tmp = get_match(stats_files, patch, '\[(\s?\S+)\s+\S+\]\]', previous=['ngmix galaxies', 'selection response matrix:'], n_previous=[9, 2], typ=float) - results['value'][keys[2]][patch] = tmp - tmp = get_match(stats_files, patch, ' \[\s?\S+\s+(\S+)\]\]', previous=['ngmix galaxies:', 'selection response matrix:'], n_previous=[9, 2], typ=float) - results['value'][keys[3]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[\[(\s?\S+)\s+\S+]", + previous=["ngmix galaxies:", "selection response matrix:"], + n_previous=[8, 1], + typ=float, + ) + results["value"][keys[0]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[\[\s?\S+\s+(\S+)]", + previous=["ngmix galaxies:", "selection response matrix:"], + n_previous=[8, 1], + typ=float, + ) + results["value"][keys[1]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r"\[(\s?\S+)\s+\S+\]\]", + previous=["ngmix galaxies", "selection response matrix:"], + n_previous=[9, 2], + typ=float, + ) + results["value"][keys[2]][patch] = tmp + tmp = get_match( + stats_files, + patch, + r" \[\s?\S+\s+(\S+)\]\]", + previous=["ngmix galaxies:", "selection response matrix:"], + n_previous=[9, 2], + typ=float, + ) + results["value"][keys[3]][patch] = tmp # Normalised trace = mean diagonal - key_der = 'trN_R_select' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "trN_R_select" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key_der][patch] = (results['value']['R_select_11'][patch] + results['value']['R_select_22'][patch]) / 2 + results["value"][key_der][patch] = ( + results["value"]["R_select_11"][patch] + + results["value"]["R_select_22"][patch] + ) / 2 # Sum of absolute off-diagonal - key_der = 'abs_off_R_select' - init_key(results, key_der, 'w_avg', extra='N_gal') + key_der = "abs_off_R_select" + init_key(results, key_der, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key_der][patch] = np.abs(results['value']['R_select_12'][patch]) + np.abs(results['value']['R_select_21'][patch]) - + results["value"][key_der][patch] = np.abs( + results["value"]["R_select_12"][patch] + ) + np.abs(results["value"]["R_select_21"][patch]) # Object-wise PSF leakage - key_base = 'm_' + key_base = "m_" if use_keys[key_base]: - keys = [f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22', f'{key_base}s1', f'{key_base}s2'] + keys = [ + f"{key_base}11", + f"{key_base}12", + f"{key_base}21", + f"{key_base}22", + f"{key_base}s1", + f"{key_base}s2", + ] for key in keys: - init_key(results, key, 'w_avg', extra='N_gal') + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - m, dm = get_match(stats_files, patch, '\$e_\{1\}\^\{\\\\rm PSF\}\$: m_1=(\S*)', previous=['ngmix'], n_previous=[1], typ='ufloat') - results['value']['m_11'][patch] = m - m, dm = get_match(stats_files, patch, '\$e_\{1\}\^\{\\\\rm PSF\}\$: m_2=(\S*)', previous=['ngmix'], n_previous=[2], typ='ufloat') - results['value']['m_12'][patch] = m - m, dm = get_match(stats_files, patch, '\$e_\{2\}\^\{\\\\rm PSF\}\$: m_1=(\S*)', previous=['ngmix'], n_previous=[3], typ='ufloat') - results['value']['m_21'][patch] = m - m, dm = get_match(stats_files, patch, '\$e_\{2\}\^\{\\\\rm PSF\}\$: m_2=(\S*)', previous=['ngmix'], n_previous=[4], typ='ufloat') - results['value']['m_22'][patch] = m - m, dm = get_match(stats_files, patch, '\$\\\\mathrm\{FWHM\}\^\{\\\\rm PSF\}\$ \[arcsec]: m_1=(\S+)', previous=['ngmix'], n_previous=[5], typ='ufloat') - results['value']['m_s1'][patch] = m - m, dm = get_match(stats_files, patch, '\$\\\\mathrm\{FWHM\}\^\{\\\\rm PSF\}\$ \[arcsec]: m_2=(\S+)', previous=['ngmix'], n_previous=[6], typ='ufloat') - results['value']['m_s2'][patch] = m + m, dm = get_match( + stats_files, + patch, + "\\$e_\\{1\\}\\^\\{\\\\rm PSF\\}\\$: m_1=(\\S*)", + previous=["ngmix"], + n_previous=[1], + typ="ufloat", + ) + results["value"]["m_11"][patch] = m + m, dm = get_match( + stats_files, + patch, + "\\$e_\\{1\\}\\^\\{\\\\rm PSF\\}\\$: m_2=(\\S*)", + previous=["ngmix"], + n_previous=[2], + typ="ufloat", + ) + results["value"]["m_12"][patch] = m + m, dm = get_match( + stats_files, + patch, + "\\$e_\\{2\\}\\^\\{\\\\rm PSF\\}\\$: m_1=(\\S*)", + previous=["ngmix"], + n_previous=[3], + typ="ufloat", + ) + results["value"]["m_21"][patch] = m + m, dm = get_match( + stats_files, + patch, + "\\$e_\\{2\\}\\^\\{\\\\rm PSF\\}\\$: m_2=(\\S*)", + previous=["ngmix"], + n_previous=[4], + typ="ufloat", + ) + results["value"]["m_22"][patch] = m + m, dm = get_match( + stats_files, + patch, + "\\$\\\\mathrm\\{FWHM\\}\\^\\{\\\\rm PSF\\}\\$ \\[arcsec]: m_1=(\\S+)", + previous=["ngmix"], + n_previous=[5], + typ="ufloat", + ) + results["value"]["m_s1"][patch] = m + m, dm = get_match( + stats_files, + patch, + "\\$\\\\mathrm\\{FWHM\\}\\^\\{\\\\rm PSF\\}\\$ \\[arcsec]: m_2=(\\S+)", + previous=["ngmix"], + n_previous=[6], + typ="ufloat", + ) + results["value"]["m_s2"][patch] = m # Scale-dependent PSF leakage - key = 'alpha' + key = "alpha" if use_keys[key]: - init_key(results, key, 'w_avg', extra='N_gal') + init_key(results, key, "w_avg", extra="N_gal") for patch in stats_files: - results['value'][key][patch] = get_match(stats_files, patch, 'ngmix: Weighted average alpha =(\s?\S+)', typ=float) + results["value"][key][patch] = get_match( + stats_files, + patch, + r"ngmix: Weighted average alpha =(\s?\S+)", + typ=float, + ) # xi_sys - key_base = 'xi_sys' + key_base = "xi_sys" if use_keys[key_base]: - init_key(results, 'xi_sys_p', 'w_avg', extra='N_gal') - init_key(results, 'xi_sys_m', 'w_avg', extra='N_gal') + init_key(results, "xi_sys_p", "w_avg", extra="N_gal") + init_key(results, "xi_sys_m", "w_avg", extra="N_gal") for patch in stats_files: - tmp = get_match(stats_files, patch, 'ngmix: <\|xi_sys_\+\|> = (\S*)', typ=float) - results['value']['xi_sys_p'][patch] = tmp - tmp = get_match(stats_files, patch, 'ngmix: <\|xi_sys_\-\|> = (\S*)', typ=float) - results['value']['xi_sys_m'][patch] = tmp + tmp = get_match( + stats_files, patch, r"ngmix: <\|xi_sys_\+\|> = (\S*)", typ=float + ) + results["value"]["xi_sys_p"][patch] = tmp + tmp = get_match( + stats_files, patch, r"ngmix: <\|xi_sys_\-\|> = (\S*)", typ=float + ) + results["value"]["xi_sys_m"][patch] = tmp def latex_table(file_base, cols=None, col_names=None): - dat = ascii.read(f'{file_base}.txt') + dat = ascii.read(f"{file_base}.txt") - fout = open(f'{file_base}.tex', 'w') + fout = open(f"{file_base}.tex", "w") n_lines = len(dat) n_dat = len(cols) - print(r'\begin{tabular}{l', end='', file=fout) - print('c'*n_dat, end='', file=fout) - #print('r@{}l'*n_dat, end='', file=fout) - print(r'}\hline\hline', file=fout) + print(r"\begin{tabular}{l", end="", file=fout) + print("c" * n_dat, end="", file=fout) + # print('r@{}l'*n_dat, end='', file=fout) + print(r"}\hline\hline", file=fout) # Table header - str_line = 'patch\t&' + str_line = "patch\t&" for name in col_names: - str_line = f'{str_line} ${name}$\t&' - #str_line = f'{str_line} \\multicolumn{{2}}{{c}}{{${name}$}}\t&' - print(rf'{str_line[:len(str_line)-2]} \\ \hline', file=fout) + str_line = f"{str_line} ${name}$\t&" + # str_line = f'{str_line} \\multicolumn{{2}}{{c}}{{${name}$}}\t&' + print(rf"{str_line[: len(str_line) - 2]} \\ \hline", file=fout) # Loop over input lines for nl in range(n_lines): - str_line = '' + str_line = "" - str_line = f'{str_line}{dat["patch"][nl]}\t&' + str_line = f"{str_line}{dat['patch'][nl]}\t&" for col in cols: if len(col) == 2: # value and error bar val_err = unc.ufloat(dat[col[0]][nl], dat[col[1]][nl]) - str_line = f'{str_line} ${val_err:+.2eL}$\t&' + str_line = f"{str_line} ${val_err:+.2eL}$\t&" else: if dat[col][nl] < 0: - pref = '' + pref = "" else: - pref = '\phantom{-}' - str_line = f'{str_line} ${pref}{dat[col][nl]:#.4f}$\t&' - print(rf'{str_line[:len(str_line)-2]} \\', file=fout) + pref = r"\phantom{-}" + str_line = f"{str_line} ${pref}{dat[col][nl]:#.4f}$\t&" + print(rf"{str_line[: len(str_line) - 2]} \\", file=fout) - print(r'\hline', file=fout) - print(r'\end{tabular}', file=fout) + print(r"\hline", file=fout) + print(r"\end{tabular}", file=fout) fout.close() @@ -541,7 +750,7 @@ def get_matrix_elements(base, me): res = [] for m in me: - res.append(f'{base}_{{{m}}}') + res.append(f"{base}_{{{m}}}") return res @@ -552,89 +761,82 @@ def main(argv=None): Main program """ if len(argv) == 1: - argv.append('snr') + argv.append("snr") all = True - if argv[1] == 'snr': + if argv[1] == "snr": # All directories - patches = [f.path for f in os.scandir('.') if f.is_dir()] + patches = [f.path for f in os.scandir(".") if f.is_dir()] all = False - elif argv[1] == 'v1': + elif argv[1] == "v1": n_patch = 7 - patches = [f'P{x}' for x in np.arange(n_patch) + 1] - elif argv[1] == 'v1.5': + patches = [f"P{x}" for x in np.arange(n_patch) + 1] + elif argv[1] == "v1.5": n_patch = 8 - patches = [f'P{x}' for x in np.arange(n_patch) + 1] - elif argv[1] == 'test': - patches = ['P7', 'W3', 'S4'] - elif argv[1] == 'comb': + patches = [f"P{x}" for x in np.arange(n_patch) + 1] + elif argv[1] == "test": + patches = ["P7", "W3", "S4"] + elif argv[1] == "comb": # Validate with combined catalogue - patches = ['comb'] + patches = ["comb"] else: patches = argv[1].split("+") n_patch = len(patches) - print('combine_results.py:', patches) + print("combine_results.py:", patches) - directory = 'sp_output/plots' - fbase = 'stats_file' - fname = f'{fbase}.txt' - path = f'{directory}/{fname}' + directory = "sp_output/plots" + fbase = "stats_file" + fname = f"{fbase}.txt" + path = f"{directory}/{fname}" - shape = 'ngmix' + shape = "ngmix" verbose = False stats_files = read_stats_files(patches, path, verbose=verbose) - n_patch_found = len(stats_files) - results = { - 'value' : {}, - 'type' : {}, - 'extra' : {}, - 'all' : {} - } + results = {"value": {}, "type": {}, "extra": {}, "all": {}} use_keys = { - 'N_gal' : 1, - 'w_tot' : 1, - 'c_' : 1, - 'cw_' : 1, - 'dc_' : 0, - 'dmc_' : 0, - 'dmcw_' : 1, - 'cjk_' : 0, - 'sigma2_epsilon' : 1, - 'R_tot_' : 1, - 'R_shear_' : 1, - 'R_select_' : 1, - 'm_' : 1, - 'alpha' : 1, - 'xi_sys' : 0, + "N_gal": 1, + "w_tot": 1, + "c_": 1, + "cw_": 1, + "dc_": 0, + "dmc_": 0, + "dmcw_": 1, + "cjk_": 0, + "sigma2_epsilon": 1, + "R_tot_": 1, + "R_shear_": 1, + "R_select_": 1, + "m_": 1, + "alpha": 1, + "xi_sys": 0, } use_keys_m = { - 'N_gal' : 0, - 'w_tot' : 0, - 'c_' : 0, - 'cw_' : 0, - 'dc_' : 0, - 'dmc_' : 0, - 'dmcw_' : 0, - 'cjk_' : 0, - 'sigma2_epsilon' : 0, - 'R_tot_' : 0, - 'R_shear_' : 0, - 'R_select_' : 0, - 'm_' : 1, - 'alpha' : 0, - 'xi_sys' : 0, + "N_gal": 0, + "w_tot": 0, + "c_": 0, + "cw_": 0, + "dc_": 0, + "dmc_": 0, + "dmcw_": 0, + "cjk_": 0, + "sigma2_epsilon": 0, + "R_tot_": 0, + "R_shear_": 0, + "R_select_": 0, + "m_": 1, + "alpha": 0, + "xi_sys": 0, } - - if argv[1] == 'snr': - area_deg2 = get_area('area.txt') + if argv[1] == "snr": + area_deg2 = get_area("area.txt") else: area_deg2 = -1 get_values(results, stats_files, shape, use_keys, area_deg2=area_deg2) @@ -645,139 +847,194 @@ def main(argv=None): # Print all keys to terminal print_all(results, stats_files, all=all) - # Get value of combined run for precision check - if argv[1] == 'test': - stats_file_comb = read_stats_files(['comb'], path, verbose=verbose) - n_patch_comb = len(stats_files) + if argv[1] == "test": + stats_file_comb = read_stats_files(["comb"], path, verbose=verbose) results_comb = { - 'value' : {}, - 'type' : {}, - 'extra' : {}, + "value": {}, + "type": {}, + "extra": {}, } get_values(results_comb, stats_file_comb, shape, use_keys) - print('\nCombined data:') + print("\nCombined data:") print_all(results_comb, stats_file_comb, header=False) # Compute fractional difference - for key in results['type']: - results_comb['value'][key]['comb'] = (results_comb['value'][key]['comb'] / results['all'][key] - 1) * 100 - print('\nFractional difference [%]:') + for key in results["type"]: + results_comb["value"][key]["comb"] = ( + results_comb["value"][key]["comb"] / results["all"][key] - 1 + ) * 100 + print("\nFractional difference [%]:") print_all(results_comb, stats_file_comb, header=False) else: results_comb = None # Get value of entire-sample run - if argv[1] == 'v1': + if argv[1] == "v1": try: - stats_file_comb = read_stats_files(['joint'], f'leakage/{fbase}_leakage.txt', verbose=verbose) - n_patch_comb = len(stats_files) + stats_file_comb = read_stats_files( + ["joint"], f"leakage/{fbase}_leakage.txt", verbose=verbose + ) results_comb = { - 'value' : {}, - 'type' : {}, - 'extra' : {}, + "value": {}, + "type": {}, + "extra": {}, } get_values(results_comb, stats_file_comb, shape, use_keys_m, area_deg2=1) - except: + except Exception: print("leakage stats file of joint catalogue not found, skipping") - # Print some key (combinations) to text and LaTeX file - if argv[1] not in ['comb']: - + if argv[1] not in ["comb"]: file_base_arr = [] - file_base = 'c' + file_base = "c" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - print_all(results, stats_files, use_keys=['cw_1', 'dmcw_1', 'cw_2', 'dmcw_2'], fout=f, all=all) + f = open(f"{file_base}.txt", "w") + print_all( + results, + stats_files, + use_keys=["cw_1", "dmcw_1", "cw_2", "dmcw_2"], + fout=f, + all=all, + ) f.close() - latex_table(file_base, cols=[['cw_1', 'dmcw_1'], ['cw_2', 'dmcw_2']], col_names=['c_1', 'c_2']) + latex_table( + file_base, + cols=[["cw_1", "dmcw_1"], ["cw_2", "dmcw_2"]], + col_names=["c_1", "c_2"], + ) - key = 'sigma2_epsilon' + key = "sigma2_epsilon" if use_keys[key]: file_base = key file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') + f = open(f"{file_base}.txt", "w") print_all(results, stats_files, use_keys=[key], fout=f, all=all) f.close() - latex_table(file_base, cols=[key], col_names=['\sigma^2_\epsilon']) - + latex_table(file_base, cols=[key], col_names=[r"\sigma^2_\epsilon"]) - me = ['11', '12', '21', '22'] + me = ["11", "12", "21", "22"] - key_base = 'R_tot_' + key_base = "R_tot_" if use_keys[key_base]: - file_base = 'R' + file_base = "R" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - use_keys=[f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22'] + f = open(f"{file_base}.txt", "w") + use_keys = [ + f"{key_base}11", + f"{key_base}12", + f"{key_base}21", + f"{key_base}22", + ] print_all(results, stats_files, use_keys=use_keys, fout=f, all=all) f.close() - col_names = get_matrix_elements('R^{\\textrm{tot}}', me) + col_names = get_matrix_elements("R^{\\textrm{tot}}", me) latex_table(file_base, cols=use_keys, col_names=col_names) - file_base = 'R_shear' + file_base = "R_shear" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - key_base = 'R_shear_' - use_keys=[f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22'] + f = open(f"{file_base}.txt", "w") + key_base = "R_shear_" + use_keys = [f"{key_base}11", f"{key_base}12", f"{key_base}21", f"{key_base}22"] print_all(results, stats_files, use_keys=use_keys, fout=f, all=all) f.close() - col_names = get_matrix_elements('R^{\\textrm{shear}}', me) + col_names = get_matrix_elements("R^{\\textrm{shear}}", me) latex_table(file_base, cols=use_keys, col_names=col_names) - file_base = 'R_select' + file_base = "R_select" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - key_base = 'R_select_' - use_keys=[f'{key_base}11', f'{key_base}12', f'{key_base}21', f'{key_base}22'] + f = open(f"{file_base}.txt", "w") + key_base = "R_select_" + use_keys = [f"{key_base}11", f"{key_base}12", f"{key_base}21", f"{key_base}22"] print_all(results, stats_files, use_keys=use_keys, fout=f, all=all) f.close() - col_names = get_matrix_elements('R^{\\textrm{select}}', me) + col_names = get_matrix_elements("R^{\\textrm{select}}", me) latex_table(file_base, cols=use_keys, col_names=col_names) - if argv[1] not in ['comb']: - - file_base = 'summary_Rc' + if argv[1] not in ["comb"]: + file_base = "summary_Rc" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - use_keys=['cw_1', 'dmcw_1', 'cw_2', 'dmcw_2', 'trN_R_tot', 'abs_off_R_tot', 'trN_R_shear', 'trN_R_select'] + f = open(f"{file_base}.txt", "w") + use_keys = [ + "cw_1", + "dmcw_1", + "cw_2", + "dmcw_2", + "trN_R_tot", + "abs_off_R_tot", + "trN_R_shear", + "trN_R_select", + ] print_all(results, stats_files, use_keys=use_keys, fout=f, all=all) f.close() - col_names = ['c_1', '\Delta c_1', 'c_2', '\Delta c_2', '\langle R^{\\rm tot}_{ii} \\rangle', - '\\sum | R^{\\ tot}_{i \\ne j}|', '\langle R^{\\rm shear}_{ii} \\rangle', '\langle R^{\\rm select}_{ii} \\rangle'] + col_names = [ + "c_1", + r"\Delta c_1", + "c_2", + r"\Delta c_2", + "\\langle R^{\\rm tot}_{ii} \\rangle", + "\\sum | R^{\\ tot}_{i \\ne j}|", + "\\langle R^{\\rm shear}_{ii} \\rangle", + "\\langle R^{\\rm select}_{ii} \\rangle", + ] latex_table(file_base, cols=use_keys, col_names=col_names) - file_base = 'summary_leakage' + file_base = "summary_leakage" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - use_keys=['n_gal_am2', 'm_11', 'm_22', 'm_12', 'm_21', 'm_s1', 'm_s2', 'alpha'] + f = open(f"{file_base}.txt", "w") + use_keys = [ + "n_gal_am2", + "m_11", + "m_22", + "m_12", + "m_21", + "m_s1", + "m_s2", + "alpha", + ] print_all(results, stats_files, use_keys=use_keys, fout=f, all=all) f.close() - col_names = ['n_{\\rm gal} [{\\rm am}^{-2}]', 'm_{11}', 'm_{22}', 'm_{12}', 'm_{21}', 'm_{\\rm s1}', 'm_{\\rm s2}', '\\alpha'] + col_names = [ + "n_{\\rm gal} [{\\rm am}^{-2}]", + "m_{11}", + "m_{22}", + "m_{12}", + "m_{21}", + "m_{\\rm s1}", + "m_{\\rm s2}", + "\\alpha", + ] latex_table(file_base, cols=use_keys, col_names=col_names) - file_base = 'summary_leakage_m' + file_base = "summary_leakage_m" file_base_arr.append(file_base) - f = open(f'{file_base}.txt', 'w') - use_keys=['m_11', 'm_22', 'm_12', 'm_21', 'm_s1', 'm_s2'] + f = open(f"{file_base}.txt", "w") + use_keys = ["m_11", "m_22", "m_12", "m_21", "m_s1", "m_s2"] print_all(results, stats_files, use_keys=use_keys, fout=f, all=False) # Add joint sample results if results_comb: - print_all(results_comb, stats_file_comb, use_keys=use_keys, fout=f, all=False) + print_all( + results_comb, stats_file_comb, use_keys=use_keys, fout=f, all=False + ) f.close() - col_names = ['m_{11}', 'm_{22}', 'm_{12}', 'm_{21}', 'm_{\\rm s1}', 'm_{\\rm s2}'] + col_names = [ + "m_{11}", + "m_{22}", + "m_{12}", + "m_{21}", + "m_{\\rm s1}", + "m_{\\rm s2}", + ] latex_table(file_base, cols=use_keys, col_names=col_names) - for file_base in file_base_arr: - - #os.system(f'~/txt2tex.pl {file_base}.tex > {file_base}_out.tex') - #print(f'Creating LaTeX file {file_base}.tex') - #os.system(f'pdflatex {file_base}_out 2&>/dev/null') + # os.system(f'~/txt2tex.pl {file_base}.tex > {file_base}_out.tex') + # print(f'Creating LaTeX file {file_base}.tex') + # os.system(f'pdflatex {file_base}_out 2&>/dev/null') print("Skipping calling LaTeX") + if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/scripts/compute_area.py b/scripts/compute_area.py index c548034f..afe797ad 100755 --- a/scripts/compute_area.py +++ b/scripts/compute_area.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 -import sys -import os +import glob import re +import sys import numpy as np from uncertainties import ufloat -import glob def main(argv=None): @@ -17,19 +16,19 @@ def main(argv=None): """ # File variables - tile_ID_path = 'tile_numbers.txt' + tile_ID_path = "tile_numbers.txt" - random_log_path = 'output/run_sp_Rc/random_cat_runner/logs' - log_file_base = 'process' + random_log_path = "output/run_sp_Rc/random_cat_runner/logs" + log_file_base = "process" # Get expected number of tiles in patch num_lines = sum(1 for _ in open(tile_ID_path)) - print(f'Found {num_lines} tiles in ID file {tile_ID_path}') + print(f"Found {num_lines} tiles in ID file {tile_ID_path}") # Get all log files of random module run - log_files = glob.glob(f'{random_log_path}/{log_file_base}*') + log_files = glob.glob(f"{random_log_path}/{log_file_base}*") n_logs = len(log_files) - print(f'Found {n_logs} log files') + print(f"Found {n_logs} log files") area_deg2_non_overl = np.empty(len(log_files)) * np.nan area_deg2_non_overl_wgal = np.empty(len(log_files)) * np.nan @@ -38,32 +37,31 @@ def main(argv=None): ratio_unmasked_tot = np.empty(len(log_files)) * np.nan # Open stats tile ID gal count file - sh = 'ngmix' - tile_ID_from = 'random' + sh = "ngmix" + tile_ID_from = "random" n_gal = {} - if tile_ID_from == 'final': - dat = np.loadtxt(f'sp_output/tile_id_gal_counts_{sh}.txt') + if tile_ID_from == "final": + dat = np.loadtxt(f"sp_output/tile_id_gal_counts_{sh}.txt") tile_ID = dat[:, 0] for idx, t_ID in enumerate(tile_ID): - n_gal[f'{t_ID:07.3f}'] = dat[idx, 2] - elif tile_ID_from == 'random': - dat = np.loadtxt(f'sp_output_random/found_ID.txt') + n_gal[f"{t_ID:07.3f}"] = dat[idx, 2] + elif tile_ID_from == "random": + dat = np.loadtxt("sp_output_random/found_ID.txt") tile_ID = dat for idx, t_ID in enumerate(tile_ID): - n_gal[f'{t_ID:07.3f}'] = 1 + n_gal[f"{t_ID:07.3f}"] = 1 n_tile_no_gal = 0 n_tile_wgal = 0 # Loop over log files for idx, log_file in enumerate(log_files): - no_gal = False - pattern = re.compile(f'.*{log_file_base}(.*)-(.*)\.log') + pattern = re.compile(rf".*{log_file_base}(.*)-(.*)\.log") m = re.match(pattern, log_file) if m: - this_id = f'{m[1]}.{m[2]}' - if tile_ID_from == 'final': + this_id = f"{m[1]}.{m[2]}" + if tile_ID_from == "final": if this_id in n_gal: if n_gal[this_id] == 0: n_tile_no_gal += 1 @@ -72,7 +70,7 @@ def main(argv=None): if this_id not in n_gal: no_gal = True else: - raise ValueError('Invalid log file format') + raise ValueError("Invalid log file format") if not no_gal: n_tile_wgal += 1 @@ -82,7 +80,7 @@ def main(argv=None): # Loop over lines in log file for line in log_content: - m = re.search('Total area without overlap = (\S+) deg', line) + m = re.search(r"Total area without overlap = (\S+) deg", line) if m: area_deg2_non_overl[idx] = float(m[1]) if not no_gal: @@ -90,7 +88,7 @@ def main(argv=None): else: area_deg2_non_overl_wgal[idx] = 0 - m = re.search('Unmaskewd area without overlap = (\S+) deg', line) + m = re.search(r"Unmaskewd area without overlap = (\S+) deg", line) if m: area_deg2_eff_non_overl[idx] = float(m[1]) if not no_gal: @@ -98,29 +96,46 @@ def main(argv=None): else: area_deg2_eff_non_overl_wgal[idx] = 0 - m = re.search('Ratio masked to total pixels = (\S+)', line) + m = re.search(r"Ratio masked to total pixels = (\S+)", line) if m: ratio_unmasked_tot[idx] = float(m[1]) - print(f'Tiles with zero galaxies = {n_tile_no_gal}') - print(f'Tiles with galaxies = {n_tile_wgal}') + print(f"Tiles with zero galaxies = {n_tile_no_gal}") + print(f"Tiles with galaxies = {n_tile_wgal}") area_deg2_non_overl_total = np.sum(area_deg2_non_overl) area_deg2_non_overl_total_wgal = np.sum(area_deg2_non_overl_wgal) - area_deg2_non_overl_tile = ufloat(np.mean(area_deg2_non_overl), np.std(area_deg2_non_overl)) - print(f'Patch area without overlap = {area_deg2_non_overl_total:.3f} deg^2') - print(f'Patch area without overlap and no 0 gal = {area_deg2_non_overl_total_wgal:.3f} deg^2') - print(f'Tile area without overlap = {area_deg2_non_overl_tile:.3fP} deg^2') + area_deg2_non_overl_tile = ufloat( + np.mean(area_deg2_non_overl), np.std(area_deg2_non_overl) + ) + print(f"Patch area without overlap = {area_deg2_non_overl_total:.3f} deg^2") + print( + f"Patch area without overlap and no 0 gal = {area_deg2_non_overl_total_wgal:.3f} deg^2" + ) + print(f"Tile area without overlap = {area_deg2_non_overl_tile:.3fP} deg^2") area_deg2_eff_non_overl_total = np.sum(area_deg2_eff_non_overl) area_deg2_eff_non_overl_total_wgal = np.sum(area_deg2_eff_non_overl_wgal) - area_deg2_eff_non_overl_tile = ufloat(np.mean(area_deg2_eff_non_overl), np.std(area_deg2_eff_non_overl)) - print(f'Unmasked patch area without overlap = {area_deg2_eff_non_overl_total:.3f} deg^2') - print(f'Unmasked patch area without overlap and no 0 gal = {area_deg2_eff_non_overl_total_wgal:.3f} deg^2') - print(f'Unmasked tile area without overlap = {area_deg2_eff_non_overl_tile:.3fP} deg^2') + area_deg2_eff_non_overl_tile = ufloat( + np.mean(area_deg2_eff_non_overl), np.std(area_deg2_eff_non_overl) + ) + print( + f"Unmasked patch area without overlap = {area_deg2_eff_non_overl_total:.3f} deg^2" + ) + print( + f"Unmasked patch area without overlap and no 0 gal = {area_deg2_eff_non_overl_total_wgal:.3f} deg^2" + ) + print( + f"Unmasked tile area without overlap = {area_deg2_eff_non_overl_tile:.3fP} deg^2" + ) + + ratio_unmasked_tile = ufloat( + np.mean(ratio_unmasked_tot), np.std(ratio_unmasked_tot) + ) + print( + f"Ratio of unmasked to total pixels = {ratio_unmasked_tile:.3fP} (min = {np.min(ratio_unmasked_tot):.3f})" + ) - ratio_unmasked_tile = ufloat(np.mean(ratio_unmasked_tot), np.std(ratio_unmasked_tot)) - print(f'Ratio of unmasked to total pixels = {ratio_unmasked_tile:.3fP} (min = {np.min(ratio_unmasked_tot):.3f})') if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/scripts/examples/create_binned_mask_comprehensive.py b/scripts/examples/create_binned_mask_comprehensive.py index 2b433359..68964120 100644 --- a/scripts/examples/create_binned_mask_comprehensive.py +++ b/scripts/examples/create_binned_mask_comprehensive.py @@ -68,9 +68,7 @@ # %% # Load and initialise masks -masks, labels = sp_joint.get_masks_from_config( - config, dat, dat_ext, verbose=True -) +masks, labels = sp_joint.get_masks_from_config(config, dat, dat_ext, verbose=True) mask_combined = sp_joint.Mask.from_list( masks, @@ -98,9 +96,7 @@ my_mask.add_summary_to_FITS_header(header) # Transform in list form -extra_header = [ - (key, header[key], header.comments[key]) for key in header.keys() -] +extra_header = [(key, header[key], header.comments[key]) for key in header.keys()] hp.write_map( output_path, diff --git a/scripts/examples/demo_calibrate_minimal_cat.py b/scripts/examples/demo_calibrate_minimal_cat.py index ccb246f2..85a0f536 100644 --- a/scripts/examples/demo_calibrate_minimal_cat.py +++ b/scripts/examples/demo_calibrate_minimal_cat.py @@ -17,16 +17,12 @@ # %reload_ext autoreload # %autoreload 2 -import sys -import os import numpy as np from astropy.io import fits -import matplotlib.pylab as plt -from sp_validation import catalog_builders as sp_joint -from sp_validation import format -from sp_validation.calibration import metacal import sp_validation.catalog as cat +from sp_validation import catalog_builders as sp_joint +from sp_validation.calibration import get_calibrated_m_c, metacal # Initialize calibration class instance obj = sp_joint.CalibrateCat() @@ -61,7 +57,9 @@ "NGMIX_ELL_PSFo_NOSHEAR_1", ] -masks, labels = sp_joint.get_masks_from_config(config, dat, dat, masks_to_apply=masks_to_apply, verbose=obj._params["verbose"]) +masks, labels = sp_joint.get_masks_from_config( + config, dat, dat, masks_to_apply=masks_to_apply, verbose=obj._params["verbose"] +) mask_combined = sp_joint.Mask.from_list( masks, @@ -74,9 +72,7 @@ num_obj = dat.shape[0] -sp_joint.Mask.print_strings( - "flag", "label", f"{'num_ok':>10}", f"{'num_ok[%]':>10}" -) +sp_joint.Mask.print_strings("flag", "label", f"{'num_ok':>10}", f"{'num_ok[%]':>10}") for my_mask in masks: my_mask.print_stats(num_obj) @@ -84,7 +80,6 @@ # - if obj._params["sky_regions"]: - # MKDBEUG TODO: zooms as list in config zoom_ra = [200, 205] zoom_dec = [55, 60] @@ -210,9 +205,7 @@ my_mask.add_summary_to_FITS_header(header) # + -output_shape_cat_path = obj._params["input_path"].replace( - "comprehensive", "cut" -) +output_shape_cat_path = obj._params["input_path"].replace("comprehensive", "cut") output_shape_cat_path = output_shape_cat_path.replace("hdf5", "fits") cat.write_shape_catalog( diff --git a/scripts/examples/demo_check_footprint.py b/scripts/examples/demo_check_footprint.py index 2c33da59..de54bbd8 100644 --- a/scripts/examples/demo_check_footprint.py +++ b/scripts/examples/demo_check_footprint.py @@ -55,7 +55,7 @@ # - # Read catalogue -#dat = obj.read_cat(load_into_memory=False, mode="r") +# dat = obj.read_cat(load_into_memory=False, mode="r") hdu_list = fits.open(obj._params["input_path"]) dat = hdu_list[hdu].data @@ -68,7 +68,7 @@ ipix = hp.ang2pix(nside_coverage, dat[key_ra], dat[key_dec], lonlat=True) ## Get pixels in footprint, where mask is "good_value" -in_footprint = (footprint[ipix] == good_value) +in_footprint = footprint[ipix] == good_value # Get numbers of pixels in footprint ipix_in_footprint = ipix[in_footprint] @@ -76,11 +76,11 @@ # Get indices of coordinates in footprint idx_np = np.where(in_footprint)[0] -if obj._params['verbose']: +if obj._params["verbose"]: n_in_footprint = len(idx_np) print( - f'{n_in_footprint}/{len(dat[key_ra])} =' - + f' {n_in_footprint/len(dat[key_ra]):.2%} objects in footprint' + f"{n_in_footprint}/{len(dat[key_ra])} =" + + f" {n_in_footprint / len(dat[key_ra]):.2%} objects in footprint" ) # Restrict data to footprint @@ -88,9 +88,13 @@ # Write data in footprint to disk t = Table(dat_in_footprint) -if obj._params['verbose']: - print(f'Writing objects in footprint to {obj._params["output_path"]}') +if obj._params["verbose"]: + print(f"Writing objects in footprint to {obj._params['output_path']}") cols = [] -for col_ind,key in enumerate(t.keys()): - cols.append(fits.Column(name=key, array=t[key], format=dat_in_footprint.columns[col_ind].format)) +for col_ind, key in enumerate(t.keys()): + cols.append( + fits.Column( + name=key, array=t[key], format=dat_in_footprint.columns[col_ind].format + ) + ) cs_cat.write_fits_BinTable_file(cols, obj._params["output_path"]) diff --git a/scripts/examples/demo_comprehensive_to_minimal_cat.py b/scripts/examples/demo_comprehensive_to_minimal_cat.py index ca5e9aa8..243feda7 100644 --- a/scripts/examples/demo_comprehensive_to_minimal_cat.py +++ b/scripts/examples/demo_comprehensive_to_minimal_cat.py @@ -60,8 +60,7 @@ ] # List of masks not to apply and not to copy to minimal catalogue -masks_not_to_include = [ -] +masks_not_to_include = [] # - # ### Pre-processing ShapePipe flags @@ -81,7 +80,6 @@ ) if obj._params["sky_regions"]: - # MKDBEUG TODO: zooms as list in config zoom_ra = [200, 205] zoom_dec = [55, 60] @@ -94,12 +92,14 @@ num_obj = dat.shape[0] with open("masks.txt", "w") as f_out: - for my_mask in masks: my_mask.print_summary(f_out) sp_joint.Mask.print_strings( - "flag", "label", f"{'num_ok':>10}", f"{'num_ok[%]':>10}", + "flag", + "label", + f"{'num_ok':>10}", + f"{'num_ok[%]':>10}", f_out=f_out, ) print(file=f_out) @@ -124,7 +124,7 @@ def strip_h5py_metadata_dtype(dat_dtype, dat_ext_dtype): if isinstance(dt, tuple): cleaned_fields.append((name, dt[0])) # keep only the base dtype string else: - cleaned_fields.append((name, dt)) # use as-is + cleaned_fields.append((name, dt)) # use as-is return cleaned_fields @@ -132,20 +132,19 @@ def strip_h5py_metadata_dtype(dat_dtype, dat_ext_dtype): # Remove mask columns that were applied earlier # Columns to keep -names_to_keep = [name for name in dat.dtype.names + dat_ext.dtype.names if name not in masks_not_to_include] +names_to_keep = [ + name + for name in dat.dtype.names + dat_ext.dtype.names + if name not in masks_not_to_include +] # Remove metadata from the dtype (in particular, encoding for TILE_ID) clean_dtype_descr = strip_h5py_metadata_dtype(dat.dtype, dat_ext.dtype) -new_dtype = [ - (name, dt) for name, dt in - clean_dtype_descr - if name in names_to_keep -] +new_dtype = [(name, dt) for name, dt in clean_dtype_descr if name in names_to_keep] # Create a new structured array -new_dat = np.zeros(len(dat[mask_combined._mask]), dtype=new_dtype -) +new_dat = np.zeros(len(dat[mask_combined._mask]), dtype=new_dtype) # Copy relevant columns and lines from each source array for name in names_to_keep: @@ -174,7 +173,7 @@ def strip_h5py_metadata_dtype(dat_dtype, dat_ext_dtype): obj_appl = sp_joint.ApplyHspMasks() # Replace "c" (comprehensive) with "m" (minimal) -output_path = re.sub(r'1\.[A-Za-z0-9]\.c', '1.X.m', obj._params["input_path"]) +output_path = re.sub(r"1\.[A-Za-z0-9]\.c", "1.X.m", obj._params["input_path"]) obj_appl._params["output_path"] = output_path obj_appl._params["aux_mask_file_liast"] = [] @@ -201,9 +200,7 @@ def correlation_matrix(masks, confidence_level=0.9): for jdx, mask_jdx in enumerate(masks): res = stats.pearsonr(mask_idx._mask, mask_jdx._mask) r_val[idx][jdx] = res.statistic - r_cl[idx][jdx] = res.confidence_interval( - confidence_level=confidence_level - ) + r_cl[idx][jdx] = res.confidence_interval(confidence_level=confidence_level) return r_val, r_cl @@ -238,11 +235,8 @@ def confusion_matrix(prediction, observation): result = {} - pred_pos = sum(prediction) result["true_pos"] = sum(prediction & observation) - result["true_neg"] = sum( - np.logical_not(prediction) & np.logical_not(observation) - ) + result["true_neg"] = sum(np.logical_not(prediction) & np.logical_not(observation)) result["false_neg"] = sum(prediction & np.logical_not(observation)) result["false_pos"] = sum(np.logical_not(prediction) & observation) result["false_pos_rate"] = result["false_pos"] / ( @@ -273,7 +267,6 @@ def confusion_matrix(prediction, observation): cms = np.zeros((n_key, n_key, 2, 2)) for idx in range(n_key): for jdx in range(n_key): - if idx == jdx: continue @@ -296,7 +289,6 @@ def confusion_matrix(prediction, observation): for idx in range(n_key): for jdx in range(n_key): - if idx == jdx: continue diff --git a/scripts/examples/demo_create_footprint_mask.py b/scripts/examples/demo_create_footprint_mask.py index 311ec8ed..b99e742b 100644 --- a/scripts/examples/demo_create_footprint_mask.py +++ b/scripts/examples/demo_create_footprint_mask.py @@ -27,8 +27,8 @@ import healsparse as hsp from cs_util.plots import FootprintPlotter -from sp_validation import cosmo_val from sp_validation import catalog_builders as sp_joint +from sp_validation import cosmo_val # - @@ -60,7 +60,6 @@ auxiliary_labels = [] for section, mask_list in config_data.items(): for mask_params in mask_list: - # Check bit-coded masks if mask_params["col_name"] in all_masks_bits: bits = bits | all_masks_bits[mask_params["col_name"]] @@ -95,11 +94,17 @@ nside = 512 for idx, label in enumerate(paths): - fp.plot_region(hsp_maps[idx], fp._regions["fullsky"], outpath=f"mask_{label}.png", title=label) - fp.plot_footprint_as_hp(hsp_maps[idx], nside, outpath=f"footprint_{label}.png", title=label) + fp.plot_region( + hsp_maps[idx], fp._regions["fullsky"], outpath=f"mask_{label}.png", title=label + ) + fp.plot_footprint_as_hp( + hsp_maps[idx], nside, outpath=f"footprint_{label}.png", title=label + ) label = "combined" -fp.plot_region(map_comb, fp._regions["fullsky"], outpath=f"mask_{label}.png", title=label) +fp.plot_region( + map_comb, fp._regions["fullsky"], outpath=f"mask_{label}.png", title=label +) fp.plot_footprint_as_hp(map_comb, nside, outpath=f"footprint_{label}.png", title=label) # - diff --git a/scripts/examples/mask_fits2hsparse.py b/scripts/examples/mask_fits2hsparse.py index d9a08e2e..f54da62a 100644 --- a/scripts/examples/mask_fits2hsparse.py +++ b/scripts/examples/mask_fits2hsparse.py @@ -29,11 +29,11 @@ from astropy.io import fits from IPython.display import clear_output -warnings.filterwarnings('ignore') +warnings.filterwarnings("ignore") # List of FITS mask files directory = f"{os.environ['HOME']}/arc/projects/unions/catalogues/unions/GAaP_photometry/extfinalmask_UNIONS5000" -#directory = "." +# directory = "." fits_mask_files = glob.glob(f"{directory}/UNIONS.*_extfinalmask.fits") # Define HealSparse parameters @@ -52,11 +52,12 @@ def interpol(ra_step, dec_step, x, y, x_step, y_step): dec_values = dec_step.flatten() # Interpolate RA and Dec values to the fine grid - ra = griddata(points, ra_values, (x, y), method='cubic') - dec = griddata(points, dec_values, (x, y), method='cubic') + ra = griddata(points, ra_values, (x, y), method="cubic") + dec = griddata(points, dec_values, (x, y), method="cubic") return ra, dec + def process(fits_mask_file, nside_sparse, nside_coverage, step=1): # Get data @@ -74,7 +75,6 @@ def process(fits_mask_file, nside_sparse, nside_coverage, step=1): x_mesh, y_mesh = np.meshgrid(x, y) if step > 1: - # Note: Use nx+1, ny+1 to add additional point at x_ar=nx, y_ar=ny. # Required to # reduce extrapolation errors towards large x, y. @@ -82,43 +82,45 @@ def process(fits_mask_file, nside_sparse, nside_coverage, step=1): y_ar = np.arange(0, ny + 1, step) x_step, y_step = np.meshgrid(x_ar, y_ar, indexing="ij") - #start = timer() + # start = timer() ra_step, dec_step = WCS.wcs_pix2world(x_step, y_step, 0) - #end = timer() - #print(f"WCS pix2 world course {end - start:.1f}s") + # end = timer() + # print(f"WCS pix2 world course {end - start:.1f}s") - #start = timer() + # start = timer() ra_interp = RectBivariateSpline(y_ar, x_ar, ra_step, ky=2, kx=2) dec_interp = RectBivariateSpline(y_ar, x_ar, dec_step, ky=2, kx=2) - #end = timer() - #print(f"create spline {end - start:.1f}s") + # end = timer() + # print(f"create spline {end - start:.1f}s") - #start = timer() + # start = timer() ra = ra_interp(y, x).T dec = dec_interp(y, x).T - #end = timer() - #print(f"interpolate {end - start:.1f}s") + # end = timer() + # print(f"interpolate {end - start:.1f}s") else: - #start = timer() + # start = timer() ra, dec = WCS.wcs_pix2world(x_mesh, y_mesh, 0) - #end = timer() - #print(f"WCS pix2 world {end - start:.1f}s") + # end = timer() + # print(f"WCS pix2 world {end - start:.1f}s") # coordinates in deg return ra, dec, mask + def update_map(hs_map, ra, dec, mask): # Add pixels to healsparse map - #start = timer() + # start = timer() hs_map.update_values_pos(ra, dec, mask, lonlat=True, operation="or") - #end = timer() - #print(f"update map {end - start:.1f}s") + # end = timer() + # print(f"update map {end - start:.1f}s") # - + def flatten(ra, dec, mask): ra_flatten = np.ravel(np.radians(ra)) dec_flatten = np.ravel(np.radians(dec)) @@ -128,7 +130,9 @@ def flatten(ra, dec, mask): # Initialise map (first time) -hs_map = hs.HealSparseMap.make_empty(nside_coverage, nside_sparse, dtype="int16", sentinel=0) +hs_map = hs.HealSparseMap.make_empty( + nside_coverage, nside_sparse, dtype="int16", sentinel=0 +) do_plot = False do_gif = True @@ -142,8 +146,9 @@ def flatten(ra, dec, mask): # Loop over mask files idx = 0 -for fits_mask_file in tqdm.tqdm(fits_mask_files, total=len(fits_mask_files), disable=False): - +for fits_mask_file in tqdm.tqdm( + fits_mask_files, total=len(fits_mask_files), disable=False +): # Check for zero file size if os.stat(fits_mask_file).st_size == 0: print(f"File {fits_mask_file} has zero size, continuing") @@ -153,7 +158,6 @@ def flatten(ra, dec, mask): ra_flatten, dec_flatten, mask_flatten = flatten(ra, dec, mask) - # Append to batch batch_ra.append(ra_flatten) batch_dec.append(dec_flatten) @@ -174,7 +178,6 @@ def flatten(ra, dec, mask): batch_ra, batch_dec, batch_mask = [], [], [] if do_gif: - map_lowres = hs_map.generate_healpix_map(nside=256) hp.mollview(map_lowres) fname = f"map_lowres_{idx:04d}.jpg" @@ -194,42 +197,4 @@ def flatten(ra, dec, mask): plt.show() # - -hs_map.write('mask_all.fits', clobber=False) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +hs_map.write("mask_all.fits", clobber=False) diff --git a/scripts/examples/plot_footprints.py b/scripts/examples/plot_footprints.py index 995599af..99fdc83e 100644 --- a/scripts/examples/plot_footprints.py +++ b/scripts/examples/plot_footprints.py @@ -43,7 +43,9 @@ def create_sp_map(ra, dec, nside_coverage=32, nside_map=2048): - sp_map = hsp.HealSparseMap.make_empty(nside_coverage, nside_map, dtype=np.float32, sentinel=np.nan) + sp_map = hsp.HealSparseMap.make_empty( + nside_coverage, nside_map, dtype=np.float32, sentinel=np.nan + ) # Get pixel list corresponding to coordinates hpix = hp.ang2pix(nside_map, ra, dec, nest=True, lonlat=True) @@ -55,7 +57,9 @@ def create_sp_map(ra, dec, nside_coverage=32, nside_map=2048): unique_hpix = np.array(list(pixel_counts.keys())) # Number of objects - values = np.array(list(pixel_counts.values()), dtype=np.float32) # Use float32 to match dtype + values = np.array( + list(pixel_counts.values()), dtype=np.float32 + ) # Use float32 to match dtype # Create maps with numbers per pixel sp_map[unique_hpix] = values @@ -80,22 +84,14 @@ def plot_area( extend = [ra_low, ra_high, dec_low, dec_high] projection = skyproj.McBrydeSkyproj( - ax=ax, - lon_0=ra_0, - extent=extend, - autorescale=True, - vmax=vmax + ax=ax, lon_0=ra_0, extent=extend, autorescale=True, vmax=vmax ) - _ = projection.draw_hspmap( - hsp_map, lon_range=extend[0:2], - lat_range=extend[2:] - ) + _ = projection.draw_hspmap(hsp_map, lon_range=extend[0:2], lat_range=extend[2:]) # + for ver in versions: - ra = cats[ver]["RA"] dec = cats[ver]["Dec"] @@ -109,7 +105,6 @@ def plot_area( vmax = 60 - plt.clf() plot_area( @@ -125,5 +120,3 @@ def plot_area( # - - - diff --git a/scripts/get_matched_catalogue.py b/scripts/get_matched_catalogue.py index abf1729e..ad1f56b9 100644 --- a/scripts/get_matched_catalogue.py +++ b/scripts/get_matched_catalogue.py @@ -1,22 +1,15 @@ # Name file: get_matched_catalogue.py -# @author: Antonin Corinaldi +# @author: Antonin Corinaldi # Python file to match UNIONS shape catalogue with a spectroscopic catalogue and save the matched catalogue with redshift information. - -import numpy as np import pandas as pd +from astropy import units as u +from astropy.coordinates import SkyCoord, match_coordinates_sky from astropy.io import fits from astropy.table import Table -from astropy.coordinates import SkyCoord, match_coordinates_sky -from astropy.coordinates import concatenate -from astropy import units as u - - - - def read_fits_to_pandas(path): @@ -42,11 +35,6 @@ def read_fits_to_pandas(path): return table.to_pandas() - - - - - def load_and_merge_catalogues(paths): """ Load multiple FITS catalogues and merge them into a single DataFrame. @@ -65,10 +53,6 @@ def load_and_merge_catalogues(paths): return pd.concat(dfs, ignore_index=True) - - - - def load_unions_catalogue(path): """ Load UNIONS FITS catalogue handling endian issues. @@ -86,24 +70,25 @@ def load_unions_catalogue(path): with fits.open(path, memmap=True, ignore_missing_simple=True) as hdul: data = hdul[1].data - df = pd.DataFrame({ - col: data[col].byteswap().view(data[col].dtype.newbyteorder()) - for col in data.names - }) + df = pd.DataFrame( + { + col: data[col].byteswap().view(data[col].dtype.newbyteorder()) + for col in data.names + } + ) return df - - - - def cross_match_catalogues( - cat1, cat2, - ra1='RA', dec1='Dec', - ra2='RA', dec2='DEC', - z_col='Z', - max_sep_deg=0.00028 + cat1, + cat2, + ra1="RA", + dec1="Dec", + ra2="RA", + dec2="DEC", + z_col="Z", + max_sep_deg=0.00028, ): """ Cross-match two catalogues on the sky and attach redshift from cat2 to cat1. @@ -130,11 +115,9 @@ def cross_match_catalogues( """ # Building SkyCoord objects - coords1 = SkyCoord(ra=cat1[ra1].values * u.deg, - dec=cat1[dec1].values * u.deg) + coords1 = SkyCoord(ra=cat1[ra1].values * u.deg, dec=cat1[dec1].values * u.deg) - coords2 = SkyCoord(ra=cat2[ra2].values * u.deg, - dec=cat2[dec2].values * u.deg) + coords2 = SkyCoord(ra=cat2[ra2].values * u.deg, dec=cat2[dec2].values * u.deg) # Nearest-neighbour match idx, d2d, _ = match_coordinates_sky(coords1, coords2) @@ -153,9 +136,6 @@ def cross_match_catalogues( return merged - - - # Example of usage (change the path to use it): @@ -165,26 +145,28 @@ def cross_match_catalogues( ) # Loading a generic spectroscopic catalogue and merging NGC and SGC parts if needed -spec_catalogue = load_and_merge_catalogues([ - "/n17data/corinaldi/DESI/clustering_data/LRG/LRG_NGC_clustering.dat.fits", - "/n17data/corinaldi/DESI/clustering_data/LRG/LRG_SGC_clustering.dat.fits" -]) +spec_catalogue = load_and_merge_catalogues( + [ + "/n17data/corinaldi/DESI/clustering_data/LRG/LRG_NGC_clustering.dat.fits", + "/n17data/corinaldi/DESI/clustering_data/LRG/LRG_SGC_clustering.dat.fits", + ] +) # Cross-matching matched = cross_match_catalogues( cat1=unions, cat2=spec_catalogue, - ra1='RA', - dec1='Dec', - ra2='RA', - dec2='DEC', - z_col='Z', - max_sep_deg=0.00028 + ra1="RA", + dec1="Dec", + ra2="RA", + dec2="DEC", + z_col="Z", + max_sep_deg=0.00028, ) # Saving result Table.from_pandas(matched).write( "/n17data/corinaldi/matched_catalogues/unions1.6.9/UNIONS1.6.9_lrg_cross.fits", format="fits", - overwrite=True -) \ No newline at end of file + overwrite=True, +) diff --git a/scripts/get_nz_txt_files_from_fits.py b/scripts/get_nz_txt_files_from_fits.py index 59220559..2884a762 100644 --- a/scripts/get_nz_txt_files_from_fits.py +++ b/scripts/get_nz_txt_files_from_fits.py @@ -7,10 +7,9 @@ import sys -from astropy.io import fits import matplotlib.pyplot as plt import numpy as np - +from astropy.io import fits nz_hdu = sys.argv[1] root = sys.argv[2] @@ -26,14 +25,25 @@ for blind in ["A", "B", "C"]: z = hdu[1].data[f"Z_{blind}"] - (n , bins, _) = plt.hist(z, bins=200, range=(0, zmax), density=True, weights=som_weights, histtype="step", label=f"Blind {blind}") + (n, bins, _) = plt.hist( + z, + bins=200, + range=(0, zmax), + density=True, + weights=som_weights, + histtype="step", + label=f"Blind {blind}", + ) print("zmin = ", min(z)) print("zmax = ", max(z)) - + # Take the middle of the bin as the redshift value for the text file z_mid = (bins[:-1] + bins[1:]) / 2 - np.savetxt(f"/n17data/sguerrini/UNIONS/WL/nz/{root}/nz_SP_{root}_{blind}.txt", np.column_stack((z_mid, n))) + np.savetxt( + f"/n17data/sguerrini/UNIONS/WL/nz/{root}/nz_SP_{root}_{blind}.txt", + np.column_stack((z_mid, n)), + ) plt.legend() plt.xlabel("Redshift z") plt.ylabel("n(z)") diff --git a/scripts/glass_mock/compute_leakage_harmony.py b/scripts/glass_mock/compute_leakage_harmony.py index 480ae530..a63c6bd4 100644 --- a/scripts/glass_mock/compute_leakage_harmony.py +++ b/scripts/glass_mock/compute_leakage_harmony.py @@ -20,15 +20,24 @@ def get_parser(): ) parser.add_argument("-N", "--number", help="Mock Number", type=int, default=0) parser.add_argument( - "-n", "--nside", - help="Nside for the simulation. Nside=Lmax", type=int, default=32, + "-n", + "--nside", + help="Nside for the simulation. Nside=Lmax", + type=int, + default=32, ) parser.add_argument( - "-p", "--path", help="Path to the simulation data", type=str, + "-p", + "--path", + help="Path to the simulation data", + type=str, default="/n09data/guerrini/glass_mock_v1.4.6/results/", ) parser.add_argument( - "-s", "--star_cat_path", help="Path to the star catalog data", type=str, + "-s", + "--star_cat_path", + help="Path to the star catalog data", + type=str, default="/n17data/UNIONS/WL/v1.4.x/unions_shapepipe_psf_2024_v1.4.a.fits", ) return parser diff --git a/scripts/glass_mock/compute_two_point_stats_glass.py b/scripts/glass_mock/compute_two_point_stats_glass.py index 40dd45a1..56da1efa 100644 --- a/scripts/glass_mock/compute_two_point_stats_glass.py +++ b/scripts/glass_mock/compute_two_point_stats_glass.py @@ -24,11 +24,17 @@ def get_parser(): ) parser.add_argument("-N", "--number", help="Mock Number", type=int, default=0) parser.add_argument( - "-n", "--nside", - help="Nside for the simulation. Nside=Lmax", type=int, default=32, + "-n", + "--nside", + help="Nside for the simulation. Nside=Lmax", + type=int, + default=32, ) parser.add_argument( - "-p", "--path", help="Path to the simulation data", type=str, + "-p", + "--path", + help="Path to the simulation data", + type=str, default="/n09data/guerrini/glass_mock_v1.4.6/results/", ) return parser diff --git a/scripts/glass_mock/create_mask.py b/scripts/glass_mock/create_mask.py index 1281c35c..9dc2dd1c 100644 --- a/scripts/glass_mock/create_mask.py +++ b/scripts/glass_mock/create_mask.py @@ -16,24 +16,37 @@ def get_parser(): fromfile_prefix_chars="@", ) parser.add_argument( - "-n", "--nside", - help="Nside for the simulation. Nside=Lmax", type=int, default=32, + "-n", + "--nside", + help="Nside for the simulation. Nside=Lmax", + type=int, + default=32, ) parser.add_argument( - "-p", "--path", + "-p", + "--path", help="Path to the galaxy catalog used to generate the mask", - type=str, default="./", + type=str, + default="./", ) parser.add_argument( - "-o", "--output", help="Output path for the mask", type=str, default="./", + "-o", + "--output", + help="Output path for the mask", + type=str, + default="./", ) parser.add_argument( - "--ra_col", help="Column name for the right ascension", - type=str, default="RA", + "--ra_col", + help="Column name for the right ascension", + type=str, + default="RA", ) parser.add_argument( - "--dec_col", help="Column name for the declination", - type=str, default="DEC", + "--dec_col", + help="Column name for the declination", + type=str, + default="DEC", ) return parser diff --git a/scripts/glass_mock/make_unions_glass_sim.py b/scripts/glass_mock/make_unions_glass_sim.py index 17bf74f0..bc97abae 100644 --- a/scripts/glass_mock/make_unions_glass_sim.py +++ b/scripts/glass_mock/make_unions_glass_sim.py @@ -45,39 +45,61 @@ def get_parser(): parser.add_argument("-s", "--seed", help="Random seed", type=int, default=42) parser.add_argument("-N", "--number", help="Mock Number", type=int, default=0) parser.add_argument( - "-n", "--nside", - help="Nside for the simulation. Nside=Lmax", type=int, default=32, + "-n", + "--nside", + help="Nside for the simulation. Nside=Lmax", + type=int, + default=32, ) parser.add_argument( - "-ne", "--neff", - help="Effective number of galaxies per arcmin^2", type=float, default=6.0905, + "-ne", + "--neff", + help="Effective number of galaxies per arcmin^2", + type=float, + default=6.0905, ) parser.add_argument( - "-p", "--path", help="Output path to save the mocks", type=str, default="./", + "-p", + "--path", + help="Output path to save the mocks", + type=str, + default="./", ) parser.add_argument( - "-c", "--cls", - help="Pre-compute and saves the matter shell cls", action="store_true", + "-c", + "--cls", + help="Pre-compute and saves the matter shell cls", + action="store_true", ) parser.add_argument("-t", "--test", help="Test run", action="store_true") parser.add_argument( - "-sg", "--sigmae", - help="Set sigma of intrinsic ellipticity", type=float, default=0.2684, + "-sg", + "--sigmae", + help="Set sigma of intrinsic ellipticity", + type=float, + default=0.2684, ) parser.add_argument("-cb", "--camb", help="get Camb C_ell", action="store_true") parser.add_argument( - "-nz", "--pathnz", - help="Path to the n(z) file", type=str, + "-nz", + "--pathnz", + help="Path to the n(z) file", + type=str, default="/n17data/sguerrini/UNIONS/WL/nz/v1.4.6.3/nz_SP_v1.4.6.3_A.txt", ) parser.add_argument( - "-m", "--mask", - help="Path to the mask", type=str, + "-m", + "--mask", + help="Path to the mask", + type=str, default="/n09data/guerrini/glass_mock_v1.4.6_rerun/mask_nside4096.fits", ) parser.add_argument( - "-ia", "--ia_bias", - help="Intrinsic alignment bias for CCL", type=float, default=None, + "-ia", + "--ia_bias", + help="Intrinsic alignment bias for CCL", + type=float, + default=None, ) return parser @@ -93,13 +115,21 @@ def __init__(self): if args.test: print("[!!!] Running in test mode [!!!]") self.config = GlassMockConfig.from_planck18( - nside=32, seed=args.seed, n_arcmin2=0.0824, - dx=120.0, zmax=0.5, sigma_e=args.sigmae, ia_bias=args.ia_bias, + nside=32, + seed=args.seed, + n_arcmin2=0.0824, + dx=120.0, + zmax=0.5, + sigma_e=args.sigmae, + ia_bias=args.ia_bias, ) else: self.config = GlassMockConfig.from_planck18( - nside=args.nside, seed=args.seed, n_arcmin2=args.neff, - sigma_e=args.sigmae, ia_bias=args.ia_bias, + nside=args.nside, + seed=args.seed, + n_arcmin2=args.neff, + sigma_e=args.sigmae, + ia_bias=args.ia_bias, ) self.test = args.test @@ -184,8 +214,18 @@ def galaxies_simulation(self): fits = fitsio.FITS(out_file, "rw", clobber=True) fits.write(None) fits.create_table_hdu( - names=["RA", "Dec", "e1", "e2", "w", "n1", "n2", - "TOM_BIN_ID", "TRUE_Z", "PHOTO_Z"], + names=[ + "RA", + "Dec", + "e1", + "e2", + "w", + "n1", + "n2", + "TOM_BIN_ID", + "TRUE_Z", + "PHOTO_Z", + ], formats=["D", "D", "E", "E", "D", "E", "E", "J", "D", "D"], extname="SOURCE_CATALOGUE", ) @@ -215,7 +255,9 @@ def galaxies_simulation(self): gal_lon, gal_lat, gal_ellip, kappa_i, gamm1_i, gamm2_i ) noise_she = glass.galaxies.galaxy_shear( - gal_lon, gal_lat, gal_ellip, + gal_lon, + gal_lat, + gal_ellip, np.zeros(np.shape(kappa_i)), np.zeros(np.shape(gamm1_i)), np.zeros(np.shape(gamm2_i)), diff --git a/scripts/healsparse_masking.py b/scripts/healsparse_masking.py index 82a153dc..a8721291 100644 --- a/scripts/healsparse_masking.py +++ b/scripts/healsparse_masking.py @@ -8,10 +8,11 @@ """ import os + +import healsparse as hsp import numpy as np import pandas as pd import tqdm -import healsparse as hsp # %% print("\n=== HealSparse Area Analysis Exercise ===") @@ -20,15 +21,18 @@ print("PART 1: Loading UNIONS HealSparse Mask") print("=" * 50) + def create_npoint_mask(path): """Load coverage file and create boolean mask for coverage > 2.""" print(f"Loading coverage file: {path}") npoint_map = hsp.HealSparseMap.read(path) - print(f" ✓ nside_coverage: {npoint_map.nside_coverage}, nside_sparse: {npoint_map.nside_sparse}") + print( + f" ✓ nside_coverage: {npoint_map.nside_coverage}, nside_sparse: {npoint_map.nside_sparse}" + ) print(f" ✓ dtype: {npoint_map.dtype}") print(f" ✓ Total pixels with data: {npoint_map.n_valid:,}") - + full_area = npoint_map.get_valid_area(degrees=True) print(f" ✓ Total area: {full_area:.2f} deg²") @@ -38,16 +42,17 @@ def create_npoint_mask(path): nside_coverage=npoint_map.nside_coverage, nside_sparse=npoint_map.nside_sparse, dtype=np.int32, - sentinel=0 + sentinel=0, ) npoint_mask[valid_pixels] = 1 - + masked_area = npoint_mask.get_valid_area(degrees=True) print(f" ✓ Area after requiring npoint > 2: {masked_area:.2f} deg²") print(f" - Area lost: {full_area - masked_area:.2f} deg²") - + return npoint_mask + base_path = "/n17data/UNIONS/WL/masks/" npoint_mask = create_npoint_mask(f"{base_path}/coverage.hsp") @@ -59,12 +64,14 @@ def create_npoint_mask(path): for n_val in n_values: mask_file = f"mask_r_nside131072_n{n_val}.hsp" print(f"\nLoading mask n={n_val}: {mask_file}") - + mask = hsp.HealSparseMap.read(f"{base_path}/{mask_file}") individual_masks[n_val] = mask - + area_deg2 = mask.get_valid_area(degrees=True) - print(f" ✓ nside_coverage: {mask.nside_coverage}, nside_sparse: {mask.nside_sparse}") + print( + f" ✓ nside_coverage: {mask.nside_coverage}, nside_sparse: {mask.nside_sparse}" + ) print(f" ✓ Valid pixels: {mask.n_valid:,}") print(f" ✓ Individual area: {area_deg2:.2f} deg²") @@ -83,24 +90,24 @@ def create_npoint_mask(path): print(f" ✓ Pixel count: {upgraded_npoint_mask.n_valid:,} valid pixels") else: print(f"Creating upgraded npoint>2 mask and caching to: {upgraded_mask_file}") - + # Upgrade sentinel mask and create boolean mask matching quality mask parameters dummy_upgraded = npoint_mask.upgrade(131072) upgraded_npoint_mask = hsp.HealSparseMap.make_empty( nside_coverage=128, nside_sparse=131072, dtype=bool, bit_packed=True ) - + print(" Processing coverage pixels...") for covpix in tqdm.tqdm( dummy_upgraded.get_covpix_maps(), total=dummy_upgraded.coverage_mask.sum(), - desc="Coverage pixels" + desc="Coverage pixels", ): upgraded_npoint_mask[covpix.valid_pixels] = True - + upgraded_npoint_mask.write(upgraded_mask_file, clobber=True) print(" ✓ Mask created and cached") - + # Validate area preservation original_area = npoint_mask.get_valid_area(degrees=True) upgraded_area = upgraded_npoint_mask.get_valid_area(degrees=True) @@ -109,7 +116,9 @@ def create_npoint_mask(path): print(f" ✓ Pixel count: {upgraded_npoint_mask.n_valid:,} valid pixels") # Baseline area comparison -npoint_gt0_area = hsp.HealSparseMap.read(f"{base_path}/coverage.hsp").get_valid_area(degrees=True) +npoint_gt0_area = hsp.HealSparseMap.read(f"{base_path}/coverage.hsp").get_valid_area( + degrees=True +) print("\nBaseline areas for comparison:") print(f" npoint>0 (full coverage): {npoint_gt0_area:.2f} deg²") print(f" npoint>2 (analysis mask): {upgraded_area:.2f} deg²") @@ -131,58 +140,71 @@ def create_npoint_mask(path): mask_order = [64, 1024, 4, 2, 1] mask_descriptions = { 64: "r band", - 1024: "maximask", + 1024: "maximask", 4: "stars", 2: "bright star halos", - 1: "faint star halos" + 1: "faint star halos", } # Apply quality masks sequentially in specified order for n_val in mask_order: # Quality masks have opposite definition - negate to get keep-pixel mask cumulative_mask = cumulative_mask & (~individual_masks[n_val]) - + current_area = cumulative_mask.get_valid_area(degrees=True) area_lost_step = cumulative_areas[-1] - current_area total_area_lost = cumulative_areas[0] - current_area - + cumulative_areas.append(current_area) mask_names.append(f"+ mask_n{n_val} ({mask_descriptions[n_val]})") - + print(f"After applying mask_n{n_val}:") print(f" Current area: {current_area:.2f} deg²") print(f" Area lost this step: {area_lost_step:.2f} deg²") print(f" Total area lost: {total_area_lost:.2f} deg²") - print(f" Fraction remaining: {current_area/cumulative_areas[0]:.3f}") + print(f" Fraction remaining: {current_area / cumulative_areas[0]:.3f}") # Create summary DataFrame starting with npoint>0 baseline table_areas = [npoint_gt0_area] + cumulative_areas table_names = ["npoint>0 baseline"] + mask_names -step_losses = [0, npoint_gt0_area - upgraded_area] + [cumulative_areas[i] - cumulative_areas[i+1] - for i in range(len(cumulative_areas)-1)] +step_losses = [0, npoint_gt0_area - upgraded_area] + [ + cumulative_areas[i] - cumulative_areas[i + 1] + for i in range(len(cumulative_areas) - 1) +] # Calculate individual mask areas -mask_areas = [0, 0] # npoint>0 baseline and npoint>2 cut don't have individual mask areas +mask_areas = [ + 0, + 0, +] # npoint>0 baseline and npoint>2 cut don't have individual mask areas for n_val in mask_order: mask_areas.append(individual_masks[n_val].get_valid_area(degrees=True)) -cumulative_df = pd.DataFrame({ - 'Mask_Applied': table_names, - 'Area_deg2': table_areas, - 'Area_Lost_Step': step_losses, - 'Mask_Area': mask_areas -}) +cumulative_df = pd.DataFrame( + { + "Mask_Applied": table_names, + "Area_deg2": table_areas, + "Area_Lost_Step": step_losses, + "Mask_Area": mask_areas, + } +) print("\nCumulative Mask Effect Summary:") -print(cumulative_df.to_string(index=False, float_format='%.2f')) +print(cumulative_df.to_string(index=False, float_format="%.2f")) # Area loss summary npoint2_loss = npoint_gt0_area - cumulative_areas[0] total_loss = npoint_gt0_area - cumulative_areas[-1] -print(f"\nArea loss summary relative to npoint>0 baseline ({npoint_gt0_area:.2f} deg²):") -print(f" Area lost by npoint>2 cut: {npoint2_loss:.2f} deg² ({(npoint2_loss/npoint_gt0_area)*100:.1f}%)") +print( + f"\nArea loss summary relative to npoint>0 baseline ({npoint_gt0_area:.2f} deg²):" +) +print( + f" Area lost by npoint>2 cut: {npoint2_loss:.2f} deg² ({(npoint2_loss / npoint_gt0_area) * 100:.1f}%)" +) print(f" Final area after all masks: {cumulative_areas[-1]:.2f} deg²") -print(f" Total area lost from npoint>0: {total_loss:.2f} deg² ({(total_loss/npoint_gt0_area)*100:.1f}%)") +print( + f" Total area lost from npoint>0: {total_loss:.2f} deg² ({(total_loss / npoint_gt0_area) * 100:.1f}%)" +) -# %% \ No newline at end of file +# %% diff --git a/scripts/homogenize_cat_extended.py b/scripts/homogenize_cat_extended.py index a3e117ae..dd54922e 100644 --- a/scripts/homogenize_cat_extended.py +++ b/scripts/homogenize_cat_extended.py @@ -9,13 +9,12 @@ """ import sys - from optparse import OptionParser +from astropy.io import fits +from astropy.table import Column, Table from cs_util import logging -from astropy.io import fits -from astropy.table import Table, Column def params_default(): """Params Default @@ -29,119 +28,117 @@ def params_default(): """ param = { - 'e1_col': 'e1', - 'e2_col': 'e2', - 'input_path_shear': 'SP/unions_shapepipe_extended_2022_v1.0.fits', - 'input_path_shear_no_ext': 'SP/unions_shapepipe_2022_v1.0.fits', - 'output_path': 'shape_cat_homogenize.fits', + "e1_col": "e1", + "e2_col": "e2", + "input_path_shear": "SP/unions_shapepipe_extended_2022_v1.0.fits", + "input_path_shear_no_ext": "SP/unions_shapepipe_2022_v1.0.fits", + "output_path": "shape_cat_homogenize.fits", } return param -def parse_options(p_def): +def parse_options(p_def): """Parse Options Parse command line options and update parameter values accordingly. - + Parameters - ---------- + ---------- p_def: dict - default parameter values - - Returns - ------- - tuple - Command line options - string - Command line string - - """ - usage = "%prog [OPTIONS]" + default parameter values + + Returns + ------- + tuple + Command line options + string + Command line string + + """ + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) - parser.add_option( - '-i', - '--input_path_shear', - dest='input_path_shear', - default=p_def['input_path_shear'], - type='string', - help='input path of the extended shear catalogue' + parser.add_option( + "-i", + "--input_path_shear", + dest="input_path_shear", + default=p_def["input_path_shear"], + type="string", + help="input path of the extended shear catalogue", ) - parser.add_option( - '', - '--input_path_shear_no_ext', - dest='input_path_shear_no_ext', - default=p_def['input_path_shear_no_ext'], - type='string', - help='input path of the non extended shear catalogue' + parser.add_option( + "", + "--input_path_shear_no_ext", + dest="input_path_shear_no_ext", + default=p_def["input_path_shear_no_ext"], + type="string", + help="input path of the non extended shear catalogue", ) - parser.add_option( - '', - '--e1_col', - dest='e1_col', - default=p_def['e1_col'], - type='string', - help=( - 'e1 column name in galaxy catalogue,' - + f' default=\'{p_def["e1_col"]}\'' - ), - ) - parser.add_option( - '', - '--e2_col', - dest='e2_col', - default=p_def['e2_col'], - type='string', + parser.add_option( + "", + "--e1_col", + dest="e1_col", + default=p_def["e1_col"], + type="string", + help=("e1 column name in galaxy catalogue," + f" default='{p_def['e1_col']}'"), + ) + parser.add_option( + "", + "--e2_col", + dest="e2_col", + default=p_def["e2_col"], + type="string", + help=("e2 column name in galaxy catalogue," + f" default='{p_def['e2_col']}'"), + ) + parser.add_option( + "-o", + "--output_path", + dest="output_path", + default=p_def["output_path"], + type="string", help=( - 'e2 column name in galaxy catalogue,' - + f' default=\'{p_def["e2_col"]}\'' + "input path of the extended shear catalogue," + + f" default='{p_def['output_path']}'" ), - ) - parser.add_option( - '-o', - '--output_path', - dest='output_path', - default=p_def['output_path'], - type='string', - help=( - 'input path of the extended shear catalogue,' - + f' default=\'{p_def["output_path"]}\'' - ) ) - options, args = parser.parse_args() - + options, args = parser.parse_args() + return options, args + def main(argv=None): - #Get default parameter values + # Get default parameter values params = params_default() - #Parse command line options + # Parse command line options options, args = parse_options(params) - #Update parameters values + # Update parameters values for key in vars(options): params[key] = getattr(options, key) logging.log_command(argv) - #Read input files - dat_shear = fits.getdata(params['input_path_shear']) - dat_shear_no_ext = fits.getdata(params['input_path_shear_no_ext']) + # Read input files + dat_shear = fits.getdata(params["input_path_shear"]) + dat_shear_no_ext = fits.getdata(params["input_path_shear_no_ext"]) t = Table(dat_shear) - t.remove_columns([params['e1_col'], params['e2_col']]) - t.add_columns([Column(dat_shear_no_ext[params['e1_col']], name=params['e1_col']), - Column(dat_shear_no_ext[params['e2_col']], name=params['e2_col'])]) - print( - 'Saving the homogenized catalog to'+f' {params["output_path"]}' + t.remove_columns([params["e1_col"], params["e2_col"]]) + t.add_columns( + [ + Column(dat_shear_no_ext[params["e1_col"]], name=params["e1_col"]), + Column(dat_shear_no_ext[params["e2_col"]], name=params["e2_col"]), + ] ) - t.write(params['output_path'], overwrite=True, format='fits') + print("Saving the homogenized catalog to" + f" {params['output_path']}") + t.write(params["output_path"], overwrite=True, format="fits") return 0 + if __name__ == "__main__": - sys.exit(main(sys.argv)) \ No newline at end of file + sys.exit(main(sys.argv)) diff --git a/scripts/map2.py b/scripts/map2.py index b7b80740..361fe48e 100755 --- a/scripts/map2.py +++ b/scripts/map2.py @@ -11,51 +11,45 @@ """ import sys - -import numpy as np - from optparse import OptionParser -from astropy.io import fits -import matplotlib.pylab as plt +import numpy as np import treecorr - from cs_util import logging def params_default(): params = { - 'input_path': 'xip_xim.txt', - 'theta_min': 0.5, - 'theta_max': 200, - 'n_theta': 20, - 'n_Theta' : 10, - 'output_path' : './map2.txt', + "input_path": "xip_xim.txt", + "theta_min": 0.5, + "theta_max": 200, + "n_theta": 20, + "n_Theta": 10, + "output_path": "./map2.txt", } short_options = { - 'input_path': '-i', - 'output_path': '-o', + "input_path": "-i", + "output_path": "-o", } types = { - 'theta_min': 'float', - 'theta_max': 'float', - 'n_theta': 'int', - 'n_Theta': 'int', + "theta_min": "float", + "theta_max": "float", + "n_theta": "int", + "n_Theta": "int", } help_strings = { - 'input_path': 'input file containing xi+ and xi-, default={}', - 'theta_min': 'mininum angular scale [arcmin], default={}', - 'theta_max': 'maximum angular scale [arcmin], default={}', - 'n_theta': 'number of angular scales on input, default={}', - 'n_Theta': 'number of angular scales on output, default={}', - 'output_path': 'output path, default={}', + "input_path": "input file containing xi+ and xi-, default={}", + "theta_min": "mininum angular scale [arcmin], default={}", + "theta_max": "maximum angular scale [arcmin], default={}", + "n_theta": "number of angular scales on input, default={}", + "n_Theta": "number of angular scales on output, default={}", + "output_path": "output path, default={}", } - return params, short_options, types, help_strings @@ -75,25 +69,24 @@ def parse_options(p_def, short_options, types, help_strings): Command line options """ - usage = "%prog [OPTIONS]" + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) for key in p_def: if key in help_strings: - if key in short_options: short = short_options[key] else: - short = '' + short = "" if key in types: typ = types[key] else: - typ = 'string' + typ = "string" parser.add_option( short, - f'--{key}', + f"--{key}", dest=key, type=typ, default=p_def[key], @@ -101,11 +94,7 @@ def parse_options(p_def, short_options, types, help_strings): ) parser.add_option( - '-v', - '--verbose', - dest='verbose', - action='store_true', - help=f'verbose output' + "-v", "--verbose", dest="verbose", action="store_true", help="verbose output" ) options, args = parser.parse_args() @@ -115,7 +104,7 @@ def parse_options(p_def, short_options, types, help_strings): def main(argv=None): - params, short_options, types, help_strings = params_default() + params, short_options, types, help_strings = params_default() options = parse_options(params, short_options, types, help_strings) @@ -128,34 +117,35 @@ def main(argv=None): # Initizlies correlation object gg = treecorr.GGCorrelation( - min_sep=params['theta_min'], - max_sep=params['theta_max'], - bin_size=params['n_theta'], + min_sep=params["theta_min"], + max_sep=params["theta_max"], + bin_size=params["n_theta"], ) # Open input catalogue - if params['verbose']: - print(f'Reading xi+ and xi- input file {params["input_path"]}...') - gg.read(params['input_path']) + if params["verbose"]: + print(f"Reading xi+ and xi- input file {params['input_path']}...") + gg.read(params["input_path"]) # Set up angular smoothing scales on output R = np.geomspace( - params['theta_min'] * 5, - params['theta_max'] / 2, - params['n_Theta'], + params["theta_min"] * 5, + params["theta_max"] / 2, + params["n_Theta"], ) # Compute correlation - if params['verbose']: - print('Computing aperture mass dispersion...') - gg.calculateMapSq(R, m2_uform='Schneider') + if params["verbose"]: + print("Computing aperture mass dispersion...") + gg.calculateMapSq(R, m2_uform="Schneider") # Write to file - if params['verbose']: - print(f'Writing output file {params["output_path"]}') - gg.writeMapSq(params['output_path'], R=R, m2_uform='Schneider') + if params["verbose"]: + print(f"Writing output file {params['output_path']}") + gg.writeMapSq(params["output_path"], R=R, m2_uform="Schneider") return 0 -if __name__ == '__main__': + +if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/scripts/merge_psf_cat.py b/scripts/merge_psf_cat.py index e2aab865..65c1e891 100644 --- a/scripts/merge_psf_cat.py +++ b/scripts/merge_psf_cat.py @@ -11,10 +11,8 @@ import numpy as np from astropy.io import fits - -from cs_util import logging -from cs_util import cat from cs_util import args as cs_args +from cs_util import cat, logging class MergePsfCat: @@ -136,7 +134,6 @@ def merge_catalogues(self, patches): dat_all = {} for idx, patch in enumerate(patches): - if verbose: print(f" {patch}") diff --git a/scripts/plot_gal_random.py b/scripts/plot_gal_random.py index 29110bc2..a40e8b3a 100644 --- a/scripts/plot_gal_random.py +++ b/scripts/plot_gal_random.py @@ -1,12 +1,10 @@ #!/usr/bin/env python3 -import numpy as np import matplotlib.pylab as plt - from astropy.io import fits -shear_cat_name = 'sp_output/shape_catalog_ngmix.fits' -random_cat_name = 'sp_output_random/random_catalog.fits' +shear_cat_name = "sp_output/shape_catalog_ngmix.fits" +random_cat_name = "sp_output_random/random_catalog.fits" gal = fits.getdata(shear_cat_name) random = fits.getdata(random_cat_name) @@ -16,11 +14,11 @@ plt.figure(figsize=(20, 20)) -plt.plot(gal['ra'], gal['dec'], '.', markersize=1) -plt.plot(random['ra'], random['dec'], '.', markersize=1) -plt.xlabel('R.A. [deg]') -plt.ylabel('DEC [deg]') -plt.title(f'{len(gal)} galaxies') -plt.title(f'{len(random)} random objects') -plt.savefig('gal+rand.png') +plt.plot(gal["ra"], gal["dec"], ".", markersize=1) +plt.plot(random["ra"], random["dec"], ".", markersize=1) +plt.xlabel("R.A. [deg]") +plt.ylabel("DEC [deg]") +plt.title(f"{len(gal)} galaxies") +plt.title(f"{len(random)} random objects") +plt.savefig("gal+rand.png") plt.show() diff --git a/scripts/plot_leakage.py b/scripts/plot_leakage.py index a7c27de9..629d81dc 100755 --- a/scripts/plot_leakage.py +++ b/scripts/plot_leakage.py @@ -1,36 +1,34 @@ #!/usr/bin/env python3 -import sys import copy -import numpy as np -from glob import glob +import sys from optparse import OptionParser -from astropy.io import ascii +import numpy as np +from astropy.io import ascii from cs_util import logging from sp_validation.catalog import * from sp_validation.plots import * -from sp_validation import io - - -class param: - """Param Class. - - General class to store (default) variables. - - """ - - def __init__(self, **kwds): - self.__dict__.update(kwds) - - def print(self, **kwds): - """Print.""" - print(self.__dict__) - - def var_list(self, **kwds): - """Get Variable List.""" - return vars(self) + + +class param: + """Param Class. + + General class to store (default) variables. + + """ + + def __init__(self, **kwds): + self.__dict__.update(kwds) + + def print(self, **kwds): + """Print.""" + print(self.__dict__) + + def var_list(self, **kwds): + """Get Variable List.""" + return vars(self) def params_default(): @@ -47,7 +45,7 @@ def params_default(): """ p_def = param( - output_dir='.', + output_dir=".", ) return p_def @@ -69,31 +67,27 @@ def parse_options(p_def): Command line string """ - usage = "%prog [OPTIONS]" + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) parser.add_option( - '-o', - '--output_dir', - dest='output_dir', + "-o", + "--output_dir", + dest="output_dir", default=p_def.output_dir, - type='string', - help=f'output_dir, default=\'{p_def.output_dir}\'' + type="string", + help=f"output_dir, default='{p_def.output_dir}'", ) - parser.add_option( - '-s', - '--shapes', - dest='sh', - default=None, - type='string', - help=f'shape measurement method, default: read from parameter file' - ) parser.add_option( - '-v', - '--verbose', - dest='verbose', - action='store_true', - help=f'verbose output' + "-s", + "--shapes", + dest="sh", + default=None, + type="string", + help="shape measurement method, default: read from parameter file", + ) + parser.add_option( + "-v", "--verbose", dest="verbose", action="store_true", help="verbose output" ) options, args = parser.parse_args() @@ -120,14 +114,14 @@ def check_options(options): def update_param(p_def, options): """Return default parameter, updated and complemented according to options. - + Parameters ---------- p_def: class param parameter values optiosn: tuple command line options - + Returns ------- param: class param @@ -143,21 +137,21 @@ def update_param(p_def, options): # Add remaining keys from options to param for key in vars(options): - if not key in vars(param): + if key not in vars(param): setattr(param, key, getattr(options, key)) return param def plot_alpha_leakage( - meanr, - alpha_leak, - sig_alpha_leak, - shape_method, - output_dir, - xmin, - xmax, - ylim=None, + meanr, + alpha_leak, + sig_alpha_leak, + shape_method, + output_dir, + xmin, + xmax, + ylim=None, ): """Plot Alpha Leakage. @@ -181,21 +175,19 @@ def plot_alpha_leakage( y-axis plot limits, default is `Ǹone` """ - plot_dir_leakage = output_dir - theta = meanr alpha_theta = alpha_leak yerr = sig_alpha_leak - xlabel = r'$\theta$ [arcmin]' - ylabel = r'$\alpha(\theta)$' + xlabel = r"$\theta$ [arcmin]" + ylabel = r"$\alpha(\theta)$" title = shape_method - out_path = f'{output_dir}/alpha_leakage_{shape_method}_all.png' + out_path = f"{output_dir}/alpha_leakage_{shape_method}_all.png" linewidths = [1] * len(meanr) linewidths[0] = 3 - colors = ['grey', 'k', 'b', 'r', 'c', 'm', 'g', 'orange'] - labels = ['all', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'] + colors = ["grey", "k", "b", "r", "c", "m", "g", "orange"] + labels = ["all", "P1", "P2", "P3", "P4", "P5", "P6", "P7"] plot_data_1d( theta, @@ -235,8 +227,9 @@ def main(argv=None): # save calling command logging.log_command(argv) - sys.path.append('.') + sys.path.append(".") import params as config + if param.sh is None: param.sh = config.shapes[0] @@ -244,18 +237,18 @@ def main(argv=None): fnames = args if param.verbose: - print('Input files: ', fnames) + print("Input files: ", fnames) # read input files, append data theta = [] alpha_leak = [] sig_alpha_leak = [] for idx, fn in enumerate(fnames): - print('Loading ', fn) + print("Loading ", fn) dat = ascii.read(fn) - theta.append(dat['theta']) - alpha_leak.append(dat['alpha']) - sig = dat['sig_alpha'] + theta.append(dat["theta"]) + alpha_leak.append(dat["alpha"]) + sig = dat["sig_alpha"] # if not first (reference) file: mark error bars to not plot if idx != 0: @@ -271,7 +264,7 @@ def main(argv=None): param.output_dir, config.theta_min_amin, config.theta_max_amin, - config.leakage_alpha_ylim + config.leakage_alpha_ylim, ) return 0 diff --git a/scripts/plot_rand.py b/scripts/plot_rand.py index 29ed50da..437e8339 100644 --- a/scripts/plot_rand.py +++ b/scripts/plot_rand.py @@ -1,33 +1,35 @@ #!/usr/bin/env python3 -import numpy as np import matplotlib.pylab as plt +import numpy as np from sp_validation import galaxy -rand = np.load('random_cat.npy') +rand = np.load("random_cat.npy") print(rand.dtype.names) markersize = 0.01 plt.figure(figsize=(10, 10)) -plt.plot(rand['RA'], rand['DEC'], '.', markersize=markersize) -plt.xlabel('R.A. [deg]') -plt.ylabel('DEC [deg]') -plt.title(f'{len(rand)} random objects with overlap') -plt.savefig('rand_wo.png') +plt.plot(rand["RA"], rand["DEC"], ".", markersize=markersize) +plt.xlabel("R.A. [deg]") +plt.ylabel("DEC [deg]") +plt.title(f"{len(rand)} random objects with overlap") +plt.savefig("rand_wo.png") plt.show() plt.clf() # Duplicate objects due to tile overlaps -cut_overlap = galaxy.classification_galaxy_overlap_ra_dec(rand, ra_key='RA', dec_key='DEC') +cut_overlap = galaxy.classification_galaxy_overlap_ra_dec( + rand, ra_key="RA", dec_key="DEC" +) rand_no = rand[cut_overlap] -plt.plot(rand_no['RA'], rand_no['DEC'], '.', markersize=markersize) -plt.xlabel('R.A. [deg]') -plt.ylabel('DEC [deg]') -plt.title(f'{len(rand_no)} random objects overlap removed') -plt.savefig('rand_no.png') +plt.plot(rand_no["RA"], rand_no["DEC"], ".", markersize=markersize) +plt.xlabel("R.A. [deg]") +plt.ylabel("DEC [deg]") +plt.title(f"{len(rand_no)} random objects overlap removed") +plt.savefig("rand_no.png") plt.show() diff --git a/scripts/plot_rho_stats_patches.py b/scripts/plot_rho_stats_patches.py index 33ced978..915c07d1 100755 --- a/scripts/plot_rho_stats_patches.py +++ b/scripts/plot_rho_stats_patches.py @@ -12,38 +12,36 @@ # name: python3 # --- -import os -import sys import glob - import itertools -import numpy as np -import matplotlib.pylab as plt +import os +import matplotlib.pylab as plt from cs_util import args - from shear_psf_leakage.rho_tau_stat import RhoStat + # + # Set parameters from file or user input class dummy(object): def __init__(self): - + self._params = { "in_dir_base": ".", "title": None, } + obj = dummy() params_upd = args.read_param_script("params_rho.py", obj._params, verbose=True) for key in params_upd: obj._params[key] = params_upd[key] -#patches = [f'P{x}' for x in np.arange(n_patch) + 1] +# patches = [f'P{x}' for x in np.arange(n_patch) + 1] patches = glob.glob("P*") print(patches) -default_colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] +default_colors = plt.rcParams["axes.prop_cycle"].by_key()["color"] color_cycle = itertools.cycle(default_colors) col = {} for patch in patches: @@ -55,22 +53,20 @@ def __init__(self): sep_units = "arcmin" nbins = 20 -# ## Set up -TreeCorrConfig = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': theta_min, - 'max_sep': theta_max, - 'sep_units': sep_units, - 'nbins': nbins, - 'var_method':'bootstrap', +# ## Set up +TreeCorrConfig = { + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": theta_min, + "max_sep": theta_max, + "sep_units": sep_units, + "nbins": nbins, + "var_method": "bootstrap", } # - -rho_stat_handler = RhoStat( - output=obj._params["in_dir_base"], - treecorr_config=TreeCorrConfig, - verbose=True +rho_stat_handler = RhoStat( + output=obj._params["in_dir_base"], treecorr_config=TreeCorrConfig, verbose=True ) # + @@ -82,7 +78,7 @@ def __init__(self): if os.path.exists(f"{obj._params['in_dir_base']}/{path}"): print(f"Reading rho stats {obj._params['in_dir_base']}/{path}...") rho_stat_handler.load_rho_stats(path) - filenames.append(path) + filenames.append(path) colors.append(col[patch]) else: print( @@ -90,15 +86,13 @@ def __init__(self): ) # - -# Create plot -rho_stat_handler.plot_rho_stats( - filenames, - colors, - patches, - abs=False, - savefig='rho_stats.png', +# Create plot +rho_stat_handler.plot_rho_stats( + filenames, + colors, + patches, + abs=False, + savefig="rho_stats.png", legend="outside", title=obj._params["title"], ) - - diff --git a/scripts/print_matrix_from_fits.py b/scripts/print_matrix_from_fits.py index 0b284b19..d3b96ce0 100755 --- a/scripts/print_matrix_from_fits.py +++ b/scripts/print_matrix_from_fits.py @@ -1,12 +1,12 @@ #!/usr/bin/env python """Read matrix entries from FITS header and print in tex or pdf format.""" -from astropy.io import fits -import numpy as np +import os import subprocess import tempfile -import os +import numpy as np +from astropy.io import fits from cs_util.args import parse_options @@ -44,7 +44,7 @@ def format_scientific_latex(value, prec, exp_thresh=3): mantissa = value / 10**exp # Format mantissa with specified precision - mantissa_str = f"{mantissa:.{prec-1}f}" + mantissa_str = f"{mantissa:.{prec - 1}f}" return f"{mantissa_str} \\times 10^{{{exp}}}" @@ -60,7 +60,7 @@ def get_matrix_from_header(header, prefix): key = f"{prefix}{i}{j}" if key not in header: raise KeyError(f"Key '{key}' not found in FITS header") - matrix[i-1, j-1] = header[key] + matrix[i - 1, j - 1] = header[key] return matrix @@ -68,7 +68,9 @@ def matrix_to_tex(matrix, prec, exp_thresh): """Convert matrix to LaTeX format.""" rows = [] for i in range(2): - row_vals = [format_scientific_latex(matrix[i, j], prec, exp_thresh) for j in range(2)] + row_vals = [ + format_scientific_latex(matrix[i, j], prec, exp_thresh) for j in range(2) + ] rows.append(" & ".join(row_vals)) tex = r"\begin{pmatrix}" + "\n" @@ -79,12 +81,16 @@ def matrix_to_tex(matrix, prec, exp_thresh): def generate_pdf(tex_content, output_path): """Generate PDF from LaTeX content.""" - full_tex = r"""\documentclass[preview,border=2pt]{article} + full_tex = ( + r"""\documentclass[preview,border=2pt]{article} \usepackage{amsmath} \begin{document} -$""" + tex_content + r"""$ +$""" + + tex_content + + r"""$ \end{document} """ + ) with tempfile.TemporaryDirectory() as tmpdir: tex_file = os.path.join(tmpdir, "matrix.tex") @@ -93,9 +99,15 @@ def generate_pdf(tex_content, output_path): # Run pdflatex result = subprocess.run( - ["pdflatex", "-interaction=nonstopmode", "-output-directory", tmpdir, tex_file], + [ + "pdflatex", + "-interaction=nonstopmode", + "-output-directory", + tmpdir, + tex_file, + ], capture_output=True, - text=True + text=True, ) if result.returncode != 0: @@ -104,6 +116,7 @@ def generate_pdf(tex_content, output_path): pdf_file = os.path.join(tmpdir, "matrix.pdf") if output_path: import shutil + shutil.copy(pdf_file, output_path) print(f"PDF written to {output_path}") else: @@ -178,12 +191,16 @@ def main(): output_path = f"{options['output']}.{options['format']}" if options["format"] == "tex": - full_tex = r"""\documentclass[preview,border=2pt]{article} + full_tex = ( + r"""\documentclass[preview,border=2pt]{article} \usepackage{amsmath} \begin{document} -$""" + tex_content + r"""$ +$""" + + tex_content + + r"""$ \end{document} """ + ) print(full_tex) if output_path: with open(output_path, "w") as f: diff --git a/scripts/shear_calibration+leakage_correction_ellipticities.py b/scripts/shear_calibration+leakage_correction_ellipticities.py index bee7ecc9..7d040052 100644 --- a/scripts/shear_calibration+leakage_correction_ellipticities.py +++ b/scripts/shear_calibration+leakage_correction_ellipticities.py @@ -6,16 +6,11 @@ # (cf file get_matched_catalogue.py in src/sp_validation for the creation of the matched catalogues) - -from astropy.table import Table import numpy as np +from astropy.table import Table from calibration import fill_cat_gal, get_alpha_leakage_per_object - - - - class ResponseMatrix: def __init__(self, R11, R12, R21, R22): self.R11 = R11 @@ -24,7 +19,6 @@ def __init__(self, R11, R12, R21, R22): self.R22 = R22 - def get_R(cat, weights=None): """ Compute weighted mean response matrix. @@ -33,19 +27,14 @@ def get_R(cat, weights=None): for i in range(2): for j in range(2): - R[i, j] = np.average(cat[f'R_g{i+1}{j+1}'], weights=weights) + R[i, j] = np.average(cat[f"R_g{i + 1}{j + 1}"], weights=weights) return R - def process_matched_catalogue( - input_path, - output_path=None, - weight_column="w_iv", - num_bins=20, - weight_type="iv" - ): + input_path, output_path=None, weight_column="w_iv", num_bins=20, weight_type="iv" +): """ Function to: - read matched catalogue @@ -75,44 +64,33 @@ def process_matched_catalogue( df = table.to_pandas() - # Computing response matrix - g_uncorr = np.array([df['e1_uncal'], df['e2_uncal']]) + g_uncorr = np.array([df["e1_uncal"], df["e2_uncal"]]) - gal_metacal = ResponseMatrix( - df['R_g11'], - df['R_g12'], - df['R_g21'], - df['R_g22'] - ) + gal_metacal = ResponseMatrix(df["R_g11"], df["R_g12"], df["R_g21"], df["R_g22"]) cat = {} - fill_cat_gal(cat, df, g_uncorr, gal_metacal, - mask1=None, mask2=None, - purpose="weights") + fill_cat_gal( + cat, df, g_uncorr, gal_metacal, mask1=None, mask2=None, purpose="weights" + ) weights = df[weight_column] R = get_R(cat, weights=weights) - # Metacalibration correction R_inv = np.linalg.inv(R) - e_cal = R_inv.dot(np.array(df[['e1_uncal', 'e2_uncal']].T)) - - df['e1'] = e_cal[0, :] - df['e2'] = e_cal[1, :] + e_cal = R_inv.dot(np.array(df[["e1_uncal", "e2_uncal"]].T)) + df["e1"] = e_cal[0, :] + df["e2"] = e_cal[1, :] # PSF leakage correction alpha_1, alpha_2 = get_alpha_leakage_per_object( - df, - num_bins=num_bins, - weight_type=weight_type + df, num_bins=num_bins, weight_type=weight_type ) - df['e1_leak_corrected'] = df['e1'] - alpha_1 * df["e1_PSF"] - df['e2_leak_corrected'] = df['e2'] - alpha_2 * df["e2_PSF"] - + df["e1_leak_corrected"] = df["e1"] - alpha_1 * df["e1_PSF"] + df["e2_leak_corrected"] = df["e2"] - alpha_2 * df["e2_PSF"] # Save catalogue output_path = output_path or input_path @@ -125,9 +103,8 @@ def process_matched_catalogue( return output_table - # Example of usage process_matched_catalogue( "/n17data/corinaldi/matched_catalogues/unions1.6.9/UNIONS1.6.9_lrg_cross.fits" -) \ No newline at end of file +) diff --git a/scripts/star_match_stats.py b/scripts/star_match_stats.py index 7bbb1b19..5c74ed5c 100644 --- a/scripts/star_match_stats.py +++ b/scripts/star_match_stats.py @@ -1,17 +1,18 @@ #!/usr/bin/env python -import sys import re +import sys + import numpy as np def main(argv=None): - types = ['star', 'gal', 'other'] - text = 'Number of stars selected as' + types = ["star", "gal", "other"] + text = "Number of stars selected as" - n_patch = 7 - patches = [f'P{x}' for x in np.arange(n_patch) + 1] + n_patch = 7 + patches = [f"P{x}" for x in np.arange(n_patch) + 1] ntyp = {} ntot = {} @@ -20,24 +21,24 @@ def main(argv=None): ntot[typ] = 0 for patch in patches: - #print(patch) - path = f'{patch}/sp_output/plots/stats_file.txt' - with open(path, 'r') as fin: + # print(patch) + path = f"{patch}/sp_output/plots/stats_file.txt" + with open(path, "r") as fin: lines = fin.readlines() for typ in types: for line in lines: - pattern = f'{text} {typ}.*= (\d+)/(\d+)' + pattern = rf"{text} {typ}.*= (\d+)/(\d+)" m = re.search(pattern, line) if m: ntyp_patch = int(m.group(1)) ntot_patch = int(m.group(2)) - #print(typ, m.group(1), m.group(2)) + # print(typ, m.group(1), m.group(2)) ntyp[typ] += ntyp_patch ntot[typ] += ntot_patch for typ in types: - print(f'{text} {typ} = {ntyp[typ]}/{ntot[typ]} = {ntyp[typ]/ntot[typ]:.2%}') + print(f"{text} {typ} = {ntyp[typ]}/{ntot[typ]} = {ntyp[typ] / ntot[typ]:.2%}") -if __name__ == "__main__": +if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/scripts/stats_tile_id_gal_counts.py b/scripts/stats_tile_id_gal_counts.py index 0a2aa23c..caed59ae 100755 --- a/scripts/stats_tile_id_gal_counts.py +++ b/scripts/stats_tile_id_gal_counts.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 -import sys -import numpy as np import copy -import matplotlib.pylab as plt - +import sys from optparse import OptionParser +import matplotlib.pylab as plt +import numpy as np + class param: """Param Class. @@ -27,7 +27,6 @@ def var_list(self, **kwds): return vars(self) - def params_default(): """Set default parameter values. @@ -41,9 +40,7 @@ def params_default(): parameter values """ - p_def = param( - survey = 'v1' - ) + p_def = param(survey="v1") return p_def @@ -64,24 +61,12 @@ def parse_options(p_def): Command line string """ - usage = "%prog [OPTIONS]" + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) # I/O - parser.add_option( - '-i', - '--input', - dest='input', - type='string', - help='input file' - ) - parser.add_option( - '-s', - '--survey', - dest='survey', - type='string', - help='survey' - ) + parser.add_option("-i", "--input", dest="input", type="string", help="input file") + parser.add_option("-s", "--survey", dest="survey", type="string", help="survey") options, args = parser.parse_args() @@ -103,7 +88,7 @@ def check_options(options): """ if not options.input and not options.survey: - print('Either input or survey need to be specified') + print("Either input or survey need to be specified") return False return True @@ -111,14 +96,14 @@ def check_options(options): def update_param(p_def, options): """Return default parameter, updated and complemented according to options. - + Parameters ---------- p_def: class param parameter values optiosn: tuple command line options - + Returns ------- param: class param @@ -134,13 +119,12 @@ def update_param(p_def, options): # Add remaining keys from options to param for key in vars(options): - if not key in vars(param): + if key not in vars(param): setattr(param, key, getattr(options, key)) return param - def main(argv=None): """Main @@ -159,33 +143,31 @@ def main(argv=None): param = update_param(p_def, options) - sh = 'ngmix' + sh = "ngmix" # Get input file paths - print('Retrieving input file paths') + print("Retrieving input file paths") input_files = [] - if param.survey == 'v1': + if param.survey == "v1": n_patch = 7 - patches = [f'P{x}' for x in np.arange(n_patch) + 1] + patches = [f"P{x}" for x in np.arange(n_patch) + 1] for patch in patches: - path = f'{patch}/sp_output/tile_id_gal_counts_{sh}.txt' + path = f"{patch}/sp_output/tile_id_gal_counts_{sh}.txt" input_files.append(path) else: input_files = [param.input] - print(f'Found {len(input_files)} input files') + print(f"Found {len(input_files)} input files") # Read from input files number of detections, galaxies, shapes - print('Reading input detection, galaxy, shape numbers') + print("Reading input detection, galaxy, shape numbers") dat = {} n_det_arr = [] n_gal_arr = [] n_shape_arr = [] for patch, input_path in zip(patches, input_files): - dat[patch] = np.loadtxt(input_path) - tile_ID = dat[patch][:, 0] n_det = dat[patch][:, 1] n_gal = dat[patch][:, 2] n_shape = dat[patch][:, 3] @@ -194,35 +176,38 @@ def main(argv=None): n_gal_arr.extend(n_gal) n_shape_arr.extend(n_shape) - # Write tile IDs with number of shapes > 0 - print('Writing tile IDs with n_shapes>0') + print("Writing tile IDs with n_shapes>0") for patch in patches: - out_path = f'{patch}/sp_output/found_ID_wshapes.txt' - with open(out_path, 'w') as f_out: + out_path = f"{patch}/sp_output/found_ID_wshapes.txt" + with open(out_path, "w") as f_out: mask_n_shape = dat[patch][:, 3] > 0 tile_ID_masked = dat[patch][mask_n_shape, 0] for ID in tile_ID_masked: - print(f'{ID:07.3f}', file=f_out) + print(f"{ID:07.3f}", file=f_out) # Plot histograms - print('Plotting histograms') - print(f'Using {len(n_det_arr)} tiles') + print("Plotting histograms") + print(f"Using {len(n_det_arr)} tiles") bins = np.arange(0, 40000, 200) density = False alpha = 0.5 - plt.ylabel('frequency') - plt.xlabel('#obj/tile') + plt.ylabel("frequency") + plt.xlabel("#obj/tile") - plt.hist(n_det_arr, bins=bins, label='detections', density=density, alpha=alpha) + plt.hist(n_det_arr, bins=bins, label="detections", density=density, alpha=alpha) - plt.hist(n_gal_arr, bins=bins, label='selected galaxies', density=density, alpha=alpha) + plt.hist( + n_gal_arr, bins=bins, label="selected galaxies", density=density, alpha=alpha + ) - plt.hist(n_shape_arr, bins=bins, label='measured shapes', density=density, alpha=alpha) + plt.hist( + n_shape_arr, bins=bins, label="measured shapes", density=density, alpha=alpha + ) plt.legend() - plt.savefig('hist_obj_per_tile.png') + plt.savefig("hist_obj_per_tile.png") figure_mosaic = """ A @@ -230,17 +215,38 @@ def main(argv=None): C """ fig, axes = plt.subplot_mosaic(mosaic=figure_mosaic, figsize=(10, 6)) - axes['A'].hist(n_det_arr, bins=bins, label='detections', density=density, alpha=alpha, color='b') - axes['B'].hist(n_gal_arr, bins=bins, label='selected galaxies', density=density, alpha=alpha, color='orange') - axes['C'].hist(n_shape_arr, bins=bins, label='measured shapes', density=density, alpha=alpha, color='green') + axes["A"].hist( + n_det_arr, + bins=bins, + label="detections", + density=density, + alpha=alpha, + color="b", + ) + axes["B"].hist( + n_gal_arr, + bins=bins, + label="selected galaxies", + density=density, + alpha=alpha, + color="orange", + ) + axes["C"].hist( + n_shape_arr, + bins=bins, + label="measured shapes", + density=density, + alpha=alpha, + color="green", + ) - axes['C'].set_xlabel('#obj/tile') + axes["C"].set_xlabel("#obj/tile") for panel in axes: axes[panel].legend() - axes[panel].set_ylabel('frequency') + axes[panel].set_ylabel("frequency") - plt.savefig('hist_obj_per_tile_3.png') + plt.savefig("hist_obj_per_tile_3.png") return 0 diff --git a/scripts/xip_xim.py b/scripts/xip_xim.py index 519a8c9d..1345f01a 100755 --- a/scripts/xip_xim.py +++ b/scripts/xip_xim.py @@ -9,59 +9,53 @@ """ import sys - -import numpy as np - from optparse import OptionParser -from astropy.io import fits -import matplotlib.pylab as plt import treecorr - +from astropy.io import fits from cs_util import logging def params_default(): params = { - 'input_path': 'shape_catalog_ngmix.fits', - 'key_ra': 'RA', - 'key_dec': 'DEC', - 'key_e1': 'e1', - 'key_e2': 'e2', - 'sign_e1': +1, - 'sign_e2': +1, - 'theta_min': 0.5, - 'theta_max': 200, - 'n_theta': 20, - 'output_path' : './xip_xim.txt', + "input_path": "shape_catalog_ngmix.fits", + "key_ra": "RA", + "key_dec": "DEC", + "key_e1": "e1", + "key_e2": "e2", + "sign_e1": +1, + "sign_e2": +1, + "theta_min": 0.5, + "theta_max": 200, + "n_theta": 20, + "output_path": "./xip_xim.txt", } short_options = { - 'input_path': '-i', - 'output_path': '-o', + "input_path": "-i", + "output_path": "-o", } types = { - 'sign_e1': 'int', - 'sign_e2': 'int', + "sign_e1": "int", + "sign_e2": "int", } help_strings = { - 'input_path': 'shear catalogue input path, default={}', - 'key_ra': 'column name for right ascension, default={}', - 'key_dec': 'column name for declination, default={}', - 'key_e1': 'column name for ellipticity component 1, default={}', - 'key_e2': 'column name for ellipticity component 2, default={}', - 'sign_e1': 'sign for ellipticity component 1, default={}', - 'sign_e2': 'sign for ellipticity component 2, default={}', - 'theta_min': 'mininum angular scale [arcmin], default={}', - 'theta_max': 'maximum angular scale [arcmin], default={}', - 'n_theta': 'number of angular scales, default={}', - 'output_path': 'output path, default={}', + "input_path": "shear catalogue input path, default={}", + "key_ra": "column name for right ascension, default={}", + "key_dec": "column name for declination, default={}", + "key_e1": "column name for ellipticity component 1, default={}", + "key_e2": "column name for ellipticity component 2, default={}", + "sign_e1": "sign for ellipticity component 1, default={}", + "sign_e2": "sign for ellipticity component 2, default={}", + "theta_min": "mininum angular scale [arcmin], default={}", + "theta_max": "maximum angular scale [arcmin], default={}", + "n_theta": "number of angular scales, default={}", + "output_path": "output path, default={}", } - return params, short_options, types, help_strings @@ -81,25 +75,24 @@ def parse_options(p_def, short_options, types, help_strings): Command line options """ - usage = "%prog [OPTIONS]" + usage = "%prog [OPTIONS]" parser = OptionParser(usage=usage) for key in p_def: if key in help_strings: - if key in short_options: short = short_options[key] else: - short = '' + short = "" if key in types: typ = types[key] else: - typ = 'string' + typ = "string" parser.add_option( short, - f'--{key}', + f"--{key}", dest=key, type=typ, default=p_def[key], @@ -107,11 +100,7 @@ def parse_options(p_def, short_options, types, help_strings): ) parser.add_option( - '-v', - '--verbose', - dest='verbose', - action='store_true', - help=f'verbose output' + "-v", "--verbose", dest="verbose", action="store_true", help="verbose output" ) options, args = parser.parse_args() @@ -121,7 +110,7 @@ def parse_options(p_def, short_options, types, help_strings): def main(argv=None): - params, short_options, types, help_strings = params_default() + params, short_options, types, help_strings = params_default() options = parse_options(params, short_options, types, help_strings) @@ -133,51 +122,52 @@ def main(argv=None): logging.log_command(argv) # Open input catalogue - if params['verbose']: - print(f'Reading catalogue {params["input_path"]}...') - data = fits.getdata(params['input_path']) - - coord_units = 'degrees' - if params['verbose']: - print( - 'Signs for ellipticity components =' - + f' ({params["sign_e1"]:+d}, {params["sign_e2"]:+d})' + if params["verbose"]: + print(f"Reading catalogue {params['input_path']}...") + data = fits.getdata(params["input_path"]) + + coord_units = "degrees" + if params["verbose"]: + print( + "Signs for ellipticity components =" + + f" ({params['sign_e1']:+d}, {params['sign_e2']:+d})" ) - g1 = data[params['key_e1']] * params['sign_e1'] - g2 = data[params['key_e2']] * params['sign_e2'] + g1 = data[params["key_e1"]] * params["sign_e1"] + g2 = data[params["key_e2"]] * params["sign_e2"] cat = treecorr.Catalog( - ra=data[params['key_ra']], - dec=data[params['key_dec']], + ra=data[params["key_ra"]], + dec=data[params["key_dec"]], g1=g1, g2=g2, - w=data['w'], + w=data["w"], ra_units=coord_units, dec_units=coord_units, ) # Set treecorr config info for correlation - sep_units = 'arcmin' + sep_units = "arcmin" TreeCorrConfig = { - 'ra_units': coord_units, - 'dec_units': coord_units, - 'min_sep': params['theta_min'], - 'max_sep': params['theta_max'], - 'sep_units': sep_units, - 'nbins': params['n_theta'], + "ra_units": coord_units, + "dec_units": coord_units, + "min_sep": params["theta_min"], + "max_sep": params["theta_max"], + "sep_units": sep_units, + "nbins": params["n_theta"], } gg = treecorr.GGCorrelation(TreeCorrConfig) # Compute correlation - if params['verbose']: - print('Correlating...') + if params["verbose"]: + print("Correlating...") gg.process(cat, cat) # Write to file - if params['verbose']: - print(f'Writing output file {params["output_path"]}') - gg.write(params['output_path']) + if params["verbose"]: + print(f"Writing output file {params['output_path']}") + gg.write(params["output_path"]) return 0 -if __name__ == '__main__': + +if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/src/sp_validation/__init__.py b/src/sp_validation/__init__.py index 1b955e11..aca7a9da 100644 --- a/src/sp_validation/__init__.py +++ b/src/sp_validation/__init__.py @@ -1,17 +1,18 @@ from .version import __version__ __all__ = [ - 'b_modes', - 'calibration', - 'catalog', - 'catalog_builders', - 'cosmology', - 'format', - 'galaxy', - 'glass_mock', - 'masks', - 'plots', - 'rho_tau', - 'statistics', - 'survey', + "__version__", + "b_modes", + "calibration", + "catalog", + "catalog_builders", + "cosmology", + "format", + "galaxy", + "glass_mock", + "masks", + "plots", + "rho_tau", + "statistics", + "survey", ] diff --git a/src/sp_validation/b_modes.py b/src/sp_validation/b_modes.py index f2f2907c..f0da212a 100644 --- a/src/sp_validation/b_modes.py +++ b/src/sp_validation/b_modes.py @@ -5,7 +5,6 @@ and semi-analytical covariance calculations extracted from CosmologyValidation. """ - import warnings import matplotlib.pyplot as plt @@ -41,8 +40,7 @@ def find_conservative_scale_cut_key(results, requested_scale_cut): # Find all keys that fit conservatively within the requested range valid_keys = [ - key for key in results.keys() - if key[0] >= min_req and key[1] <= max_req + key for key in results.keys() if key[0] >= min_req and key[1] <= max_req ] if valid_keys: @@ -50,8 +48,7 @@ def find_conservative_scale_cut_key(results, requested_scale_cut): return max(valid_keys, key=lambda k: k[1] - k[0]) # If no conservative match, find closest by total distance - return min(results.keys(), - key=lambda k: abs(k[0] - min_req) + abs(k[1] - max_req)) + return min(results.keys(), key=lambda k: abs(k[0] - min_req) + abs(k[1] - max_req)) def scale_cut_to_bins(gg, min_scale=None, max_scale=None): @@ -82,13 +79,13 @@ def scale_cut_to_bins(gg, min_scale=None, max_scale=None): if min_scale is not None: # Conservative: exclude bins whose left edge is below min_scale - start_bin = np.searchsorted(gg.left_edges, min_scale, side='left') + start_bin = np.searchsorted(gg.left_edges, min_scale, side="left") else: start_bin = 0 if max_scale is not None: # Conservative: exclude bins whose right edge is above max_scale - stop_bin = np.searchsorted(gg.right_edges, max_scale, side='right') + stop_bin = np.searchsorted(gg.right_edges, max_scale, side="right") else: stop_bin = nbins @@ -120,7 +117,7 @@ def calculate_pure_eb_correlation( cov_path_int=None, cosmo_cov=None, n_samples=1000, - z_dist=None + z_dist=None, ): """ Calculate pure E/B modes from correlation function objects. @@ -157,9 +154,15 @@ def calculate_pure_eb_correlation( def pure_EB(corrs): gg, gg_int = corrs return get_pure_EB_modes( - theta=gg.meanr, xip=gg.xip, xim=gg.xim, - theta_int=gg_int.meanr, xip_int=gg_int.xip, xim_int=gg_int.xim, - tmin=min_sep, tmax=max_sep, parallel=True + theta=gg.meanr, + xip=gg.xip, + xim=gg.xim, + theta_int=gg_int.meanr, + xip_int=gg_int.xip, + xim_int=gg_int.xim, + tmin=min_sep, + tmax=max_sep, + parallel=True, ) # Initialize results dictionary with basic E/B mode data @@ -181,44 +184,50 @@ def pure_EB(corrs): # Set up integration binning and pre-compute binning matrix nbins_int, theta_int = len(gg_int.meanr), gg_int.meanr - reporting_bin_edges = np.concatenate([ - gg.left_edges, [gg.right_edges[-1]] - ]) + reporting_bin_edges = np.concatenate([gg.left_edges, [gg.right_edges[-1]]]) bin_indices = np.digitize(theta_int, reporting_bin_edges) - 1 valid_mask = (bin_indices >= 0) & (bin_indices < len(gg.meanr)) - row_indices, col_indices = ( - bin_indices[valid_mask], np.where(valid_mask)[0] - ) + row_indices, col_indices = (bin_indices[valid_mask], np.where(valid_mask)[0]) binning_matrix = sparse.csr_matrix( (np.ones(len(row_indices)), (row_indices, col_indices)), - shape=(len(gg.meanr), nbins_int) + shape=(len(gg.meanr), nbins_int), ) row_sums = np.array(binning_matrix.sum(axis=1)).flatten() - binning_matrix = sparse.diags(1/row_sums) @ binning_matrix + binning_matrix = sparse.diags(1 / row_sums) @ binning_matrix # Generate theoretical xi+/xi- predictions and sample - mean_int = np.concatenate(get_theo_xi( - theta=theta_int, z=z_dist[:, 0], nz=z_dist[:, 1], - backend="ccl", cosmo=cosmo_cov - )) - - samples_int = np.random.multivariate_normal( - mean_int, cov_int, size=n_samples + mean_int = np.concatenate( + get_theo_xi( + theta=theta_int, + z=z_dist[:, 0], + nz=z_dist[:, 1], + backend="ccl", + cosmo=cosmo_cov, + ) ) + + samples_int = np.random.multivariate_normal(mean_int, cov_int, size=n_samples) samples_int_xip = samples_int[:, :nbins_int] samples_int_xim = samples_int[:, nbins_int:] samples_rep_xip = (binning_matrix @ samples_int_xip.T).T samples_rep_xim = (binning_matrix @ samples_int_xim.T).T transformed_samples = [ - np.concatenate(get_pure_EB_modes( - theta=gg.meanr, theta_int=gg_int.meanr, - xip=samples_rep_xip[i], xim=samples_rep_xim[i], - xip_int=samples_int_xip[i], xim_int=samples_int_xim[i], - tmin=min_sep, tmax=max_sep, parallel=True - )) + np.concatenate( + get_pure_EB_modes( + theta=gg.meanr, + theta_int=gg_int.meanr, + xip=samples_rep_xip[i], + xim=samples_rep_xim[i], + xip_int=samples_int_xip[i], + xim_int=samples_int_xim[i], + tmin=min_sep, + tmax=max_sep, + parallel=True, + ) + ) for i in tqdm.tqdm(range(n_samples), desc="MC samples") ] @@ -241,7 +250,7 @@ def pure_EB(corrs): warnings.warn( "E/B mode covariance matrix is not positive definite. " "Chi-squared statistics may be unreliable.", - UserWarning + UserWarning, ) return results @@ -299,9 +308,7 @@ def calculate_cosebis(gg, nmodes=10, scale_cuts=None, cov_path=None): start_bin, stop_bin = scale_cut_to_bins(gg, min_theta, max_theta) inds = np.arange(start_bin, stop_bin) - theta_cut, xip_cut, xim_cut = [ - arr[inds] for arr in [gg.meanr, gg.xip, gg.xim] - ] + theta_cut, xip_cut, xim_cut = [arr[inds] for arr in [gg.meanr, gg.xip, gg.xim]] # Calculate COSEBIs E/B modes using actual theta range (per Axel's recommendation) # Use precision=120 (vs default 80) to avoid sympy root convergence failures @@ -326,15 +333,22 @@ def calculate_cosebis(gg, nmodes=10, scale_cuts=None, cov_path=None): ] results = { - 'En': En, 'Bn': Bn, 'cov': cov_cosebis, - 'hartlap_factor': hartlap_factor, 'chi2_E': chi2_E, 'chi2_B': chi2_B, - 'pte_B': 1 - stats.chi2.cdf(chi2_B, nmodes), - 'scale_cut': (min_theta, max_theta), 'inds': inds + "En": En, + "Bn": Bn, + "cov": cov_cosebis, + "hartlap_factor": hartlap_factor, + "chi2_E": chi2_E, + "chi2_B": chi2_B, + "pte_B": 1 - stats.chi2.cdf(chi2_B, nmodes), + "scale_cut": (min_theta, max_theta), + "inds": inds, } print(f"COSEBIs results [{min_theta:.1f}-{max_theta:.1f} arcmin]:") - print(f" chi2(E) = {chi2_E:.2f}, chi2(B) = {chi2_B:.2f}, " - f"PTE(B) = {results['pte_B']:.4f}") + print( + f" chi2(E) = {chi2_E:.2f}, chi2(B) = {chi2_B:.2f}, " + f"PTE(B) = {results['pte_B']:.4f}" + ) all_results[scale_cut] = results @@ -389,8 +403,7 @@ def calculate_eb_statistics( # Generate all valid (start, stop) combinations for scale cuts combinations = [ - (start, stop) for start in range(nbins) - for stop in range(start, nbins + 1) + (start, stop) for start in range(nbins) for stop in range(start, nbins + 1) ] for start_bin, stop_bin in combinations: @@ -398,9 +411,7 @@ def calculate_eb_statistics( hartlap_factor = (n_eff - nbins_eff - 2) / (n_eff - 1) # Individual B-mode chi-squared calculations - data_slices = [ - results[f"{xi}_B"][start_bin:stop_bin] for xi in ["xip", "xim"] - ] + data_slices = [results[f"{xi}_B"][start_bin:stop_bin] for xi in ["xip", "xim"]] cov_slices = [ results[f"cov_{xi}_B"][start_bin:stop_bin, start_bin:stop_bin] for xi in ["xip", "xim"] @@ -410,26 +421,27 @@ def calculate_eb_statistics( for data, cov in zip(data_slices, cov_slices) ] - pte_xip_B[start_bin, stop_bin-1] = stats.chi2.sf(chi2_values[0], nbins_eff) - pte_xim_B[start_bin, stop_bin-1] = stats.chi2.sf(chi2_values[1], nbins_eff) + pte_xip_B[start_bin, stop_bin - 1] = stats.chi2.sf(chi2_values[0], nbins_eff) + pte_xim_B[start_bin, stop_bin - 1] = stats.chi2.sf(chi2_values[1], nbins_eff) # Combined calculation with cross-correlation data_combined = np.concatenate(data_slices) # Build combined covariance matrix blocks - xip_slice = slice(2*nbins + start_bin, 2*nbins + stop_bin) - xim_slice = slice(3*nbins + start_bin, 3*nbins + stop_bin) + xip_slice = slice(2 * nbins + start_bin, 2 * nbins + stop_bin) + xim_slice = slice(3 * nbins + start_bin, 3 * nbins + stop_bin) cov_xip_block = cov[xip_slice, xip_slice] cov_xim_block = cov[xim_slice, xim_slice] cov_cross_block = cov[xip_slice, xim_slice] - cov_combined = np.block([ - [cov_xip_block, cov_cross_block], - [cov_cross_block.T, cov_xim_block] - ]) + cov_combined = np.block( + [[cov_xip_block, cov_cross_block], [cov_cross_block.T, cov_xim_block]] + ) chi2_combined = hartlap_factor * ( data_combined @ np.linalg.solve(cov_combined, data_combined) ) - pte_combined[start_bin, stop_bin-1] = stats.chi2.sf(chi2_combined, 2*nbins_eff) + pte_combined[start_bin, stop_bin - 1] = stats.chi2.sf( + chi2_combined, 2 * nbins_eff + ) # Store PTE matrices pte_data = {"xip_B": pte_xip_B, "xim_B": pte_xim_B, "combined": pte_combined} @@ -457,13 +469,13 @@ def plot_integration_vs_reporting(gg, gg_int, output_path, version): # Configure plot data for both xi+ and xi- in a consolidated loop plot_configs = [ - ('+', 'xip', 'varxip', r"$\theta \xi_+(\theta) \times 10^4$"), - ('-', 'xim', 'varxim', r"$\theta \xi_-(\theta) \times 10^4$") + ("+", "xip", "varxip", r"$\theta \xi_+(\theta) \times 10^4$"), + ("-", "xim", "varxim", r"$\theta \xi_-(\theta) \times 10^4$"), ] data_configs = [ - (gg_int, 'k.', 3, 0.3, 'Integration'), - (gg, '.', 12, 1, 'Reporting') + (gg_int, "k.", 3, 0.3, "Integration"), + (gg, ".", 12, 1, "Reporting"), ] for ax_idx, (xi_label, xi_attr, var_attr, ylabel) in enumerate(plot_configs): @@ -471,24 +483,29 @@ def plot_integration_vs_reporting(gg, gg_int, output_path, version): xi_val = getattr(data, xi_attr) yerr = ( data.meanr * np.sqrt(getattr(data, var_attr)) / 1e-4 - if hasattr(data, var_attr) and label_type == 'Reporting' + if hasattr(data, var_attr) and label_type == "Reporting" else None ) axs[ax_idx].errorbar( - data.meanr, data.meanr * xi_val / 1e-4, yerr=yerr, - fmt=fmt, ms=ms, alpha=alpha, capsize=3, - ls='' if label_type == 'Reporting' else None, + data.meanr, + data.meanr * xi_val / 1e-4, + yerr=yerr, + fmt=fmt, + ms=ms, + alpha=alpha, + capsize=3, + ls="" if label_type == "Reporting" else None, label=( rf"$\xi_{{{xi_label}}}$, {label_type}: " rf"${data.min_sep} < \theta < {data.max_sep}$, " rf"{data.nbins} bins" - ) + ), ) axs[ax_idx].set( xlabel=r"$\theta$ [arcmin]", ylabel=ylabel, xscale="log", - title=rf"$\xi_{{{xi_label}}}(\theta)$" + title=rf"$\xi_{{{xi_label}}}(\theta)$", ) axs[ax_idx].legend() @@ -519,7 +536,7 @@ def _get_pte_from_scale_cut(pte_matrix, gg, scale_cut): if scale_cut is None: # Return full-range PTE (first row, last column) - return pte_matrix[0, nbins-1] + return pte_matrix[0, nbins - 1] min_scale, max_scale = scale_cut @@ -529,7 +546,7 @@ def _get_pte_from_scale_cut(pte_matrix, gg, scale_cut): # Ensure valid range, otherwise fallback to full range if stop_bin <= start_bin or start_bin >= nbins or stop_bin <= 0: raise RuntimeError("Invalid scale cut range") - return pte_matrix[start_bin, stop_bin-1] + return pte_matrix[start_bin, stop_bin - 1] def plot_pure_eb_correlations( @@ -580,15 +597,20 @@ def plot_pure_eb_correlations( # ξ+^B block (2*nbins:3*nbins, 2*nbins:3*nbins) # ξ-^B block (3*nbins:4*nbins, 3*nbins:4*nbins) # ξ+^B-ξ-^B cross (2*nbins:3*nbins, 3*nbins:4*nbins) - cov_xip_B = cov[2*nbins + xip_start_bin:2*nbins + xip_stop_bin, - 2*nbins + xip_start_bin:2*nbins + xip_stop_bin] - cov_xim_B = cov[3*nbins + xim_start_bin:3*nbins + xim_stop_bin, - 3*nbins + xim_start_bin:3*nbins + xim_stop_bin] - cov_cross = cov[2*nbins + xip_start_bin:2*nbins + xip_stop_bin, - 3*nbins + xim_start_bin:3*nbins + xim_stop_bin] + cov_xip_B = cov[ + 2 * nbins + xip_start_bin : 2 * nbins + xip_stop_bin, + 2 * nbins + xip_start_bin : 2 * nbins + xip_stop_bin, + ] + cov_xim_B = cov[ + 3 * nbins + xim_start_bin : 3 * nbins + xim_stop_bin, + 3 * nbins + xim_start_bin : 3 * nbins + xim_stop_bin, + ] + cov_cross = cov[ + 2 * nbins + xip_start_bin : 2 * nbins + xip_stop_bin, + 3 * nbins + xim_start_bin : 3 * nbins + xim_stop_bin, + ] - cov_combined = np.block([[cov_xip_B, cov_cross], - [cov_cross.T, cov_xim_B]]) + cov_combined = np.block([[cov_xip_B, cov_cross], [cov_cross.T, cov_xim_B]]) # Calculate combined chi-squared with Hartlap factor nbins_eff = len(xip_B_data) + len(xim_B_data) @@ -619,10 +641,20 @@ def plot_pure_eb_correlations( # Main correlation functions and decomposed modes plot_configs = [ - ('xip', '+', 'varxip', - r"$\xi_{+}=\xi_{+}^{E}+\xi_{+}^{B}+\xi_{+}^{\mathrm{amb}}$", xip_B_pte), - ('xim', '-', 'varxim', - r"$\xi_{-}=\xi_{-}^{E}-\xi_{-}^{B}+\xi_{-}^{\mathrm{amb}}$", xim_B_pte) + ( + "xip", + "+", + "varxip", + r"$\xi_{+}=\xi_{+}^{E}+\xi_{+}^{B}+\xi_{+}^{\mathrm{amb}}$", + xip_B_pte, + ), + ( + "xim", + "-", + "varxim", + r"$\xi_{-}=\xi_{-}^{E}-\xi_{-}^{B}+\xi_{-}^{\mathrm{amb}}$", + xim_B_pte, + ), ] for ax_idx, (xi_type, xi_label, var_attr, main_label, pte_val) in enumerate( @@ -631,26 +663,43 @@ def plot_pure_eb_correlations( # Plot main correlation function xi_val = getattr(gg, xi_type) axs[ax_idx].errorbar( - gg.meanr, gg.meanr * xi_val / scale_factor, + gg.meanr, + gg.meanr * xi_val / scale_factor, yerr=gg.meanr * np.sqrt(getattr(gg, var_attr)) / scale_factor, - fmt="k.", capsize=3, label=main_label + fmt="k.", + capsize=3, + label=main_label, ) # Plot E/B/amb modes plot_data = [ (f"{xi_type}_E", "g", 0.25, rf"$\xi_{{{xi_label}}}^{{E}}$"), - (f"{xi_type}_B", "r", 1.0, - rf"$\xi_{{{xi_label}}}^{{B}}, {{\rm PTE}}=" - rf"{np.round(pte_val, 4)}$"), - (f"{xi_type}_amb", "purple", 0.25, - rf"$\xi_{{{xi_label}}}^{{\mathrm{{amb}}}}$") + ( + f"{xi_type}_B", + "r", + 1.0, + rf"$\xi_{{{xi_label}}}^{{B}}, {{\rm PTE}}=" + rf"{np.round(pte_val, 4)}$", + ), + ( + f"{xi_type}_amb", + "purple", + 0.25, + rf"$\xi_{{{xi_label}}}^{{\mathrm{{amb}}}}$", + ), ] for key, color, alpha, label in plot_data: axs[ax_idx].errorbar( - gg.meanr, gg.meanr * results[key] / scale_factor, + gg.meanr, + gg.meanr * results[key] / scale_factor, yerr=gg.meanr * results[f"std_{key}"] / scale_factor, - color=color, ls="", marker=".", alpha=alpha, capsize=3, label=label + color=color, + ls="", + marker=".", + alpha=alpha, + capsize=3, + label=label, ) axs[ax_idx].set_ylabel(r"$\theta\xi\times10^{4}$") @@ -679,18 +728,24 @@ def plot_pure_eb_correlations( # Show excluded regions based on bin edges used in PTE calculation # Lower exclusion: bins 0 to start_bin-1 are excluded if start_bin > 0: - lower_exclusion_edge = gg.right_edges[start_bin-1] + lower_exclusion_edge = gg.right_edges[start_bin - 1] axs[ax_idx].axvspan( - xlim[0], lower_exclusion_edge, alpha=0.2, color='gray', - label='Scale cuts' if ax_idx == 0 else None + xlim[0], + lower_exclusion_edge, + alpha=0.2, + color="gray", + label="Scale cuts" if ax_idx == 0 else None, ) # Upper exclusion: bins stop_bin to end are excluded if stop_bin < len(gg.left_edges): upper_exclusion_edge = gg.left_edges[stop_bin] axs[ax_idx].axvspan( - upper_exclusion_edge, xlim[1], alpha=0.2, color='gray', - label='Scale cuts' if ax_idx == 0 and start_bin == 0 else None + upper_exclusion_edge, + xlim[1], + alpha=0.2, + color="gray", + label="Scale cuts" if ax_idx == 0 and start_bin == 0 else None, ) # Restore original axis limits @@ -698,7 +753,8 @@ def plot_pure_eb_correlations( plt.suptitle( f"{version}: Pure Correlation Functions, Combined PTE = {combined_pte:.4f}", - fontsize=23, y=0.95 + fontsize=23, + y=0.95, ) plt.tight_layout() plt.savefig(output_path, dpi=300, bbox_inches="tight") @@ -734,7 +790,7 @@ def plot_cosebis_scale_cut_heatmap( snr_matrix = np.full((nbins, nbins), np.nan) pte_matrix = np.full((nbins, nbins), np.nan) - for (row, column, snr, pte) in zip(rows, columns, snrs, ptes): + for row, column, snr, pte in zip(rows, columns, snrs, ptes): snr_matrix[row, column] = snr pte_matrix[row, column] = pte @@ -745,47 +801,59 @@ def plot_cosebis_scale_cut_heatmap( result = cosebis_results.get(scale_cut) if result is not None: - snr_matrix[i, j] = np.sqrt(result['chi2_E']) - pte_matrix[i, j] = result['pte_B'] + snr_matrix[i, j] = np.sqrt(result["chi2_E"]) + pte_matrix[i, j] = result["pte_B"] # Create 1x2 subplot layout fig, axs = plt.subplots(1, 2, figsize=(15, 7)) # Set up colormaps mako_cmap, vlag_cmap = [ - sns.color_palette(name, as_cmap=True).copy() - for name in ["mako", "vlag"] + sns.color_palette(name, as_cmap=True).copy() for name in ["mako", "vlag"] ] for cmap in (mako_cmap, vlag_cmap): - cmap.set_bad(color='lightgray') + cmap.set_bad(color="lightgray") # Left plot: E-mode SNR snr_max = np.nanmax(snr_matrix) if not np.all(np.isnan(snr_matrix)) else 1 snr_plot_data = snr_matrix.T im1 = axs[0].imshow( - snr_plot_data, origin='lower', aspect='auto', cmap=mako_cmap, - vmin=0, vmax=snr_max, extent=[0, nbins, 0, nbins] + snr_plot_data, + origin="lower", + aspect="auto", + cmap=mako_cmap, + vmin=0, + vmax=snr_max, + extent=[0, nbins, 0, nbins], ) - axs[0].set_title(r'E-mode SNR: $\sqrt{\chi^2_E}$') - axs[0].set_xlabel('Lower scale cut') - axs[0].set_ylabel('Upper scale cut') + axs[0].set_title(r"E-mode SNR: $\sqrt{\chi^2_E}$") + axs[0].set_xlabel("Lower scale cut") + axs[0].set_ylabel("Upper scale cut") # Right plot: B-mode PTE with contours pte_plot_data = pte_matrix.T im2 = axs[1].imshow( - pte_plot_data, origin='lower', aspect='auto', cmap=vlag_cmap, - vmin=0, vmax=1, extent=[0, nbins, 0, nbins] + pte_plot_data, + origin="lower", + aspect="auto", + cmap=vlag_cmap, + vmin=0, + vmax=1, + extent=[0, nbins, 0, nbins], ) # Add contours to PTE plot cs = axs[1].contour( - pte_plot_data, levels=[0.05, 0.95], colors='black', linewidths=1, - extent=[0, nbins, 0, nbins] + pte_plot_data, + levels=[0.05, 0.95], + colors="black", + linewidths=1, + extent=[0, nbins, 0, nbins], ) axs[1].clabel(cs, inline=True, fontsize=8) - axs[1].set_title('B-mode PTE') - axs[1].set_xlabel('Lower scale cut') + axs[1].set_title("B-mode PTE") + axs[1].set_xlabel("Lower scale cut") # Add fiducial scale cut cross-hatching if provided if fiducial_scale_cut is not None: @@ -800,31 +868,40 @@ def plot_cosebis_scale_cut_heatmap( rect_height = 1 for ax_idx, ax in enumerate(axs): - ax.add_patch(plt.Rectangle( - (rect_x, rect_y), rect_width, rect_height, - fill=False, edgecolor='black', linewidth=2, - hatch='///', alpha=0.8, - label='Fiducial scale cut' if ax_idx == 0 else None - )) + ax.add_patch( + plt.Rectangle( + (rect_x, rect_y), + rect_width, + rect_height, + fill=False, + edgecolor="black", + linewidth=2, + hatch="///", + alpha=0.8, + label="Fiducial scale cut" if ax_idx == 0 else None, + ) + ) # Set angular scale ticks tick_indices = np.arange(0, nbins) - x_tick_labels = [f'{gg.left_edges[i]:.1f}' for i in tick_indices - if i < len(gg.left_edges)] - y_tick_labels = [f'{gg.right_edges[i]:.1f}' for i in tick_indices - if i < len(gg.right_edges)] + x_tick_labels = [ + f"{gg.left_edges[i]:.1f}" for i in tick_indices if i < len(gg.left_edges) + ] + y_tick_labels = [ + f"{gg.right_edges[i]:.1f}" for i in tick_indices if i < len(gg.right_edges) + ] x_tick_positions = tick_indices + 0.5 y_tick_positions = tick_indices + 0.5 for ax in axs: - ax.set_xticks(x_tick_positions[:len(x_tick_labels)]) + ax.set_xticks(x_tick_positions[: len(x_tick_labels)]) ax.set_xticklabels(x_tick_labels, rotation=45) - ax.set_yticks(y_tick_positions[:len(y_tick_labels)]) + ax.set_yticks(y_tick_positions[: len(y_tick_labels)]) ax.set_yticklabels(y_tick_labels) - ax.set_aspect('equal') + ax.set_aspect("equal") # Add colorbars to both plots - colorbar_data = [(im1, 'SNR'), (im2, 'PTE')] + colorbar_data = [(im1, "SNR"), (im2, "PTE")] for i, (im, label) in enumerate(colorbar_data): divider = make_axes_locatable(axs[i]) cax = divider.append_axes("right", size="5%", pad=0.1) @@ -833,16 +910,19 @@ def plot_cosebis_scale_cut_heatmap( # Add legend if fiducial scale cut shown if fiducial_scale_cut is not None: - axs[0].legend(loc='lower right', fontsize=8) + axs[0].legend(loc="lower right", fontsize=8) - plt.suptitle(f'{version}: COSEBIs Scale Cut Analysis', fontsize=23, y=0.95) + plt.suptitle(f"{version}: COSEBIs Scale Cut Analysis", fontsize=23, y=0.95) plt.tight_layout() plt.savefig(output_path, dpi=300, bbox_inches="tight") def plot_pte_2d_heatmaps( - results, version, output_path, - fiducial_xip_scale_cut=None, fiducial_xim_scale_cut=None + results, + version, + output_path, + fiducial_xip_scale_cut=None, + fiducial_xim_scale_cut=None, ): """ Plot 2D PTE matrix heatmaps for B-mode analysis. @@ -870,37 +950,53 @@ def plot_pte_2d_heatmaps( # Set up colormap vlag_cmap = sns.color_palette("vlag", as_cmap=True).copy() - vlag_cmap.set_bad(color='lightgray') + vlag_cmap.set_bad(color="lightgray") contour_levels = [0.05, 0.95] # Left plot: xi+ PTE xip_plot_data = pte_xip_B.T axs[0].imshow( - xip_plot_data, origin='lower', aspect='auto', cmap=vlag_cmap, - vmin=0, vmax=1, extent=[0, nbins, 0, nbins] + xip_plot_data, + origin="lower", + aspect="auto", + cmap=vlag_cmap, + vmin=0, + vmax=1, + extent=[0, nbins, 0, nbins], ) cs1 = axs[0].contour( - xip_plot_data, levels=contour_levels, colors='black', linewidths=1, - extent=[0, nbins, 0, nbins] + xip_plot_data, + levels=contour_levels, + colors="black", + linewidths=1, + extent=[0, nbins, 0, nbins], ) axs[0].clabel(cs1, inline=True, fontsize=8) - axs[0].set_title(r'$\xi_+^B$ PTE') - axs[0].set_xlabel('Lower scale cut') - axs[0].set_ylabel('Upper scale cut') + axs[0].set_title(r"$\xi_+^B$ PTE") + axs[0].set_xlabel("Lower scale cut") + axs[0].set_ylabel("Upper scale cut") # Right plot: xi- PTE xim_plot_data = pte_xim_B.T im2 = axs[1].imshow( - xim_plot_data, origin='lower', aspect='auto', cmap=vlag_cmap, - vmin=0, vmax=1, extent=[0, nbins, 0, nbins] + xim_plot_data, + origin="lower", + aspect="auto", + cmap=vlag_cmap, + vmin=0, + vmax=1, + extent=[0, nbins, 0, nbins], ) cs2 = axs[1].contour( - xim_plot_data, levels=contour_levels, colors='black', linewidths=1, - extent=[0, nbins, 0, nbins] + xim_plot_data, + levels=contour_levels, + colors="black", + linewidths=1, + extent=[0, nbins, 0, nbins], ) axs[1].clabel(cs2, inline=True, fontsize=8) - axs[1].set_title(r'$\xi_-^B$ PTE') - axs[1].set_xlabel('Lower scale cut') + axs[1].set_title(r"$\xi_-^B$ PTE") + axs[1].set_xlabel("Lower scale cut") # Add fiducial scale cuts individually fiducial_scale_cuts = [fiducial_xip_scale_cut, fiducial_xim_scale_cut] @@ -915,40 +1011,49 @@ def plot_pte_2d_heatmaps( rect_width = 1 rect_height = 1 - axs[ax_idx].add_patch(plt.Rectangle( - (rect_x, rect_y), rect_width, rect_height, - fill=False, edgecolor='black', linewidth=2, - hatch='///', alpha=0.8, - label='Fiducial scale cut' if ax_idx == 0 else None - )) + axs[ax_idx].add_patch( + plt.Rectangle( + (rect_x, rect_y), + rect_width, + rect_height, + fill=False, + edgecolor="black", + linewidth=2, + hatch="///", + alpha=0.8, + label="Fiducial scale cut" if ax_idx == 0 else None, + ) + ) # Set angular scale ticks tick_indices = np.arange(0, nbins) - x_tick_labels = [f'{gg.left_edges[i]:.1f}' for i in tick_indices - if i < len(gg.left_edges)] - y_tick_labels = [f'{gg.right_edges[i]:.1f}' for i in tick_indices - if i < len(gg.right_edges)] + x_tick_labels = [ + f"{gg.left_edges[i]:.1f}" for i in tick_indices if i < len(gg.left_edges) + ] + y_tick_labels = [ + f"{gg.right_edges[i]:.1f}" for i in tick_indices if i < len(gg.right_edges) + ] x_tick_positions = tick_indices + 0.5 y_tick_positions = tick_indices + 0.5 for ax in axs: - ax.set_xticks(x_tick_positions[:len(x_tick_labels)]) + ax.set_xticks(x_tick_positions[: len(x_tick_labels)]) ax.set_xticklabels(x_tick_labels, rotation=45) - ax.set_yticks(y_tick_positions[:len(y_tick_labels)]) + ax.set_yticks(y_tick_positions[: len(y_tick_labels)]) ax.set_yticklabels(y_tick_labels) - ax.set_aspect('equal') + ax.set_aspect("equal") # Add colorbar divider = make_axes_locatable(axs[1]) cax = divider.append_axes("right", size="5%", pad=0.1) cbar = plt.colorbar(im2, cax=cax) - cbar.set_label('PTE', rotation=270, labelpad=15) + cbar.set_label("PTE", rotation=270, labelpad=15) # Add legend if any fiducial scale cuts shown if any(cut is not None for cut in fiducial_scale_cuts): - axs[0].legend(loc='lower right', fontsize=8) + axs[0].legend(loc="lower right", fontsize=8) - plt.suptitle(f'{version}: PTEs as a Function of Scale Cut', fontsize=23, y=0.95) + plt.suptitle(f"{version}: PTEs as a Function of Scale Cut", fontsize=23, y=0.95) plt.tight_layout() plt.savefig(output_path, dpi=300, bbox_inches="tight") @@ -977,8 +1082,12 @@ def plot_eb_covariance_matrix(cov_matrix, var_method, output_path, version): # Configure tick labels for E/B modes tick_positions = np.arange(nbins / 2, nbins * 6, nbins) tick_labels = [ - r"$\xi_+^{E}$", r"$\xi_-^{E}$", r"$\xi_+^{B}$", - r"$\xi_-^{B}$", r"$\xi_+^{\rm amb}$", r"$\xi_-^{\rm amb}$" + r"$\xi_+^{E}$", + r"$\xi_-^{E}$", + r"$\xi_+^{B}$", + r"$\xi_-^{B}$", + r"$\xi_+^{\rm amb}$", + r"$\xi_-^{\rm amb}$", ] for ticks in (plt.xticks, plt.yticks): @@ -993,12 +1102,8 @@ def plot_eb_covariance_matrix(cov_matrix, var_method, output_path, version): ax.set_title(f"{version}: {var_method} correlation matrix") plt.savefig(output_path, dpi=300, bbox_inches="tight") -def plot_cosebis_modes( - results, - version, - output_path, - fiducial_scale_cut=None -): + +def plot_cosebis_modes(results, version, output_path, fiducial_scale_cut=None): """ Plot COSEBIs E/B mode correlation functions. @@ -1044,8 +1149,8 @@ def plot_cosebis_modes( # Add scale cut information to title - use actual scale cut from results scale_info = "" - if 'scale_cut' in results: - actual_scale_cut = results['scale_cut'] + if "scale_cut" in results: + actual_scale_cut = results["scale_cut"] scale_info = ( f", scale cut: ({actual_scale_cut[0]:.1f}, {actual_scale_cut[1]:.1f})" ) @@ -1102,7 +1207,6 @@ def _cosebis_result_to_dict(r, suffix=""): } - def save_cosebis_results(results, output_path, fiducial_scale_cut=None): """ Save COSEBIs data vectors and covariance to .npz. diff --git a/src/sp_validation/calibration.py b/src/sp_validation/calibration.py index 9a5fea31..b61e7261 100644 --- a/src/sp_validation/calibration.py +++ b/src/sp_validation/calibration.py @@ -10,19 +10,16 @@ import numpy as np import pandas as pd - -from astropy.io import fits import statsmodels.api as sm import tqdm +from astropy.io import fits +from shear_psf_leakage import leakage, run_object -from sp_validation.statistics import jackknif_weighted_average2 from sp_validation import catalog as sp_cat - -from shear_psf_leakage import leakage -from shear_psf_leakage import run_object +from sp_validation.statistics import jackknif_weighted_average2 -def get_calibrated_quantities(gal_metacal, shape_method='ngmix'): +def get_calibrated_quantities(gal_metacal, shape_method="ngmix"): """Get Calibrated Quantities. Return catalogue quantities for objects calibrated for multiplicative @@ -47,24 +44,21 @@ def get_calibrated_quantities(gal_metacal, shape_method='ngmix'): mask to indicate valid objects in "no-shear" sample """ # mask for 'no shear' images - mask = gal_metacal.mask_dict['ns'] + mask = gal_metacal.mask_dict["ns"] # uncalibrated shear estimates - g_uncorr = np.array([ - gal_metacal.ns['g1'][mask], - gal_metacal.ns['g2'][mask] - ]) + g_uncorr = np.array([gal_metacal.ns["g1"][mask], gal_metacal.ns["g2"][mask]]) # calibratied shear estimates: multiply with inverse response matrix g_corr = np.linalg.inv(gal_metacal.R).dot(g_uncorr) # weights - w = gal_metacal.ns['w'][mask] + w = gal_metacal.ns["w"][mask] return g_corr, g_uncorr, w, mask -def get_calibrated_m_c(gal_metacal, shape_method='ngmix'): +def get_calibrated_m_c(gal_metacal, shape_method="ngmix"): """Get Calibrated C. Return catalogue quantities for objects calibrated for multiplicative and @@ -76,17 +70,17 @@ def get_calibrated_m_c(gal_metacal, shape_method='ngmix'): galaxy metacalibration catalogue shape_method : string, optional, default='ngmix' shape measurement method, one in 'ngmix', 'galsim' - + Returns ------- numpy.ndarray : shear estimates calibrated for multiplicative and additive bias; array(2, ngal) of float - numpy.ndarray : + numpy.ndarray : uncalibrated shear estimates; array(2, ngal) of float numpy.ndarray : weights; array of float - numpy.ndarray : + numpy.ndarray : mask to indicate valid objects in "no-shear" sample; array of bool numpy.ndarray : additive bias for both components; @@ -95,9 +89,7 @@ def get_calibrated_m_c(gal_metacal, shape_method='ngmix'): """ # Get m-calibrated quantities - g_corr, g_uncorr, w, mask_metacal = get_calibrated_quantities( - gal_metacal - ) + g_corr, g_uncorr, w, mask_metacal = get_calibrated_quantities(gal_metacal) # Additive bias c = np.zeros(2) @@ -115,7 +107,7 @@ def get_calibrated_m_c(gal_metacal, shape_method='ngmix'): c_corr = np.linalg.inv(gal_metacal.R).dot(c) for comp in (0, 1): g_corr_mc[comp] = g_corr[comp] - c_corr[comp] - + return g_corr_mc, g_uncorr, w, mask_metacal, c, c_err @@ -142,14 +134,14 @@ def create_bins(x, num_bins, type="log", x_min=None, x_max=None): xmax = x.max() if not x_max else x_max return np.logspace(np.log10(xmin), np.log10(xmax), num_bins + 1) else: - raise ValueError("Type not supported") + raise ValueError("Type not supported") def cut_to_bins(df, key, num_bins, type="log", x_min=None, x_max=None): """Cut To Bins. - + Cut a given array into bins. Create a new column in the DataFrame with the binning. - + Parameters ---------- df : pandas.DataFrame @@ -171,17 +163,17 @@ def cut_to_bins(df, key, num_bins, type="log", x_min=None, x_max=None): bin edges """ key_cut = f"{key}_{type}_bins" - + bin_edges = create_bins(df[key], num_bins, type=type, x_min=x_min, x_max=x_max) df[key_cut] = pd.cut(df[key], bin_edges, labels=False) - + df.loc[np.isnan(df[key_cut]), key_cut] = num_bins - 1 return bin_edges def fill_cat_gal(cat_gal, dat, g_uncorr, gal_metacal, mask1, mask2, purpose="weights"): - + cat_gal["e1_uncal"] = g_uncorr[0] cat_gal["e2_uncal"] = g_uncorr[1] cat_gal["R_g11"] = gal_metacal.R11 @@ -189,39 +181,30 @@ def fill_cat_gal(cat_gal, dat, g_uncorr, gal_metacal, mask1, mask2, purpose="wei cat_gal["R_g21"] = gal_metacal.R21 cat_gal["R_g22"] = gal_metacal.R22 - cat_gal["NGMIX_T_NOSHEAR"] = ( - sp_cat.get_col(dat, "NGMIX_T_NOSHEAR", mask1, mask2) - ) - cat_gal["NGMIX_Tpsf_NOSHEAR"] = ( - sp_cat.get_col(dat, "NGMIX_Tpsf_NOSHEAR", mask1, mask2) - ) - cat_gal["size_ratio"] = ( - cat_gal['NGMIX_T_NOSHEAR'] / cat_gal['NGMIX_Tpsf_NOSHEAR'] + cat_gal["NGMIX_T_NOSHEAR"] = sp_cat.get_col(dat, "NGMIX_T_NOSHEAR", mask1, mask2) + cat_gal["NGMIX_Tpsf_NOSHEAR"] = sp_cat.get_col( + dat, "NGMIX_Tpsf_NOSHEAR", mask1, mask2 ) + cat_gal["size_ratio"] = cat_gal["NGMIX_T_NOSHEAR"] / cat_gal["NGMIX_Tpsf_NOSHEAR"] + + cat_gal["snr"] = sp_cat.get_col( + dat, "NGMIX_FLUX_NOSHEAR", mask1, mask2 + ) / sp_cat.get_col(dat, "NGMIX_FLUX_ERR_NOSHEAR", mask1, mask2) - cat_gal["snr"] = ( - sp_cat.get_col(dat, "NGMIX_FLUX_NOSHEAR", mask1, mask2) - / sp_cat.get_col(dat, "NGMIX_FLUX_ERR_NOSHEAR", mask1, mask2) - ) - if purpose == "weights": pass elif purpose == "leakage": cat_gal["w"] = sp_cat.get_col(dat, "w_iv", mask1, mask2) for idx in (1, 2): - cat_gal[f"e{idx}_PSF"] = ( - sp_cat.get_col(dat, f"e{idx}_PSF", mask1, mask2) - ) - cat_gal["fwhm_PSF"] = ( - sp_cat.get_col(dat, "fwhm_PSF", mask1, mask2) - ) + cat_gal[f"e{idx}_PSF"] = sp_cat.get_col(dat, f"e{idx}_PSF", mask1, mask2) + cat_gal["fwhm_PSF"] = sp_cat.get_col(dat, "fwhm_PSF", mask1, mask2) def build_df(cat_gal): """Build DF. - + Build pandas dataframe. - + Parameters ---------- cat_gal : dict @@ -233,13 +216,10 @@ def build_df(cat_gal): collected data """ - #Build a pandas dataframe to perform the binning and the computation of the weights - arr = np.array([ - np.array(cat_gal[key], dtype=np.float64) - for key in cat_gal - ]).T + # Build a pandas dataframe to perform the binning and the computation of the weights + arr = np.array([np.array(cat_gal[key], dtype=np.float64) for key in cat_gal]).T return pd.DataFrame(arr, columns=cat_gal.keys()) - + def get_w_des( cat_gal, @@ -278,7 +258,7 @@ def get_w_des( """ df_gal = build_df(cat_gal) - #Create logarithmic bins in size and SNR + # Create logarithmic bins in size and SNR cut_to_bins( df_gal, "snr", @@ -299,27 +279,26 @@ def get_w_des( # Compute shape noise and the shear response in each bin for i in range(num_bins): for j in range(num_bins): - bin_mask = ( - (df_gal['snr_log_bins'] == i) - & (df_gal['size_ratio_log_bins'] == j) + bin_mask = (df_gal["snr_log_bins"] == i) & ( + df_gal["size_ratio_log_bins"] == j ) ngal = np.sum(bin_mask) if ngal == 0: print(f"Zero galaxies in snr/size_ratio bin ({i},{j})") shape_noise = 0.5 * ( - np.sum(df_gal[bin_mask]['e1_uncal'] ** 2) / ngal - + np.sum(df_gal[bin_mask]['e2_uncal'] ** 2) / ngal + np.sum(df_gal[bin_mask]["e1_uncal"] ** 2) / ngal + + np.sum(df_gal[bin_mask]["e2_uncal"] ** 2) / ngal ) response = 0.5 * ( - np.average(df_gal[bin_mask]['R_g11']) - + np.average(df_gal[bin_mask]['R_g22']) + np.average(df_gal[bin_mask]["R_g11"]) + + np.average(df_gal[bin_mask]["R_g22"]) ) - df_gal.loc[bin_mask, 'w_des'] = response ** 2 / shape_noise - - return np.array(df_gal['w_des']) + df_gal.loc[bin_mask, "w_des"] = response**2 / shape_noise + + return np.array(df_gal["w_des"]) -def get_alpha_leakage_per_object(cat_gal, num_bins, weight_type='des'): +def get_alpha_leakage_per_object(cat_gal, num_bins, weight_type="des"): """ Compute the leakage per object (Li et al. 2024) Return an array of leakage coefficients obtained by binning in @@ -341,190 +320,222 @@ def get_alpha_leakage_per_object(cat_gal, num_bins, weight_type='des'): Array containing the correction coefficient for the PSF leakage per object for the second component. """ - assert weight_type in ['des', 'iv'], "weight_type must be either 'des' or 'iv'" + assert weight_type in ["des", "iv"], "weight_type must be either 'des' or 'iv'" # Compute the size ratio - size_ratio = ( - cat_gal['NGMIX_Tpsf_NOSHEAR'] - / (cat_gal['NGMIX_T_NOSHEAR'] + cat_gal['NGMIX_Tpsf_NOSHEAR']) + size_ratio = cat_gal["NGMIX_Tpsf_NOSHEAR"] / ( + cat_gal["NGMIX_T_NOSHEAR"] + cat_gal["NGMIX_Tpsf_NOSHEAR"] ) - df_gal = pd.DataFrame(np.array([ - np.array(cat_gal['e1'], dtype=np.float64), - np.array(cat_gal['e2'], dtype=np.float64), - np.array(cat_gal['e1_PSF'], dtype=np.float64), - np.array(cat_gal['e2_PSF'], dtype=np.float64), - np.array(cat_gal['snr'], dtype=np.float64), - np.array(cat_gal[f'w_{weight_type}'], dtype=np.float64), - np.array(size_ratio, dtype=np.float64) - ]).T, columns=['e1', 'e2', 'e1_PSF', 'e2_PSF', 'snr', 'w', 'size_ratio']) + df_gal = pd.DataFrame( + np.array( + [ + np.array(cat_gal["e1"], dtype=np.float64), + np.array(cat_gal["e2"], dtype=np.float64), + np.array(cat_gal["e1_PSF"], dtype=np.float64), + np.array(cat_gal["e2_PSF"], dtype=np.float64), + np.array(cat_gal["snr"], dtype=np.float64), + np.array(cat_gal[f"w_{weight_type}"], dtype=np.float64), + np.array(size_ratio, dtype=np.float64), + ] + ).T, + columns=["e1", "e2", "e1_PSF", "e2_PSF", "snr", "w", "size_ratio"], + ) del size_ratio n_bins_snr = num_bins n_bins_r = num_bins - #Create logarithmic bins in size and SNR - df_gal.loc[:, 'bin_R'] = pd.qcut(df_gal['size_ratio'], n_bins_r, labels=False, retbins=False) + # Create logarithmic bins in size and SNR + df_gal.loc[:, "bin_R"] = pd.qcut( + df_gal["size_ratio"], n_bins_r, labels=False, retbins=False + ) - #initialize bin snr - df_gal.loc[:, 'bin_snr'] = -999 + # initialize bin snr + df_gal.loc[:, "bin_snr"] = -999 for ibin_r in range(n_bins_r): - #select galaxies in the bin - mask_binr = df_gal['bin_R'].values == ibin_r - - #bin in snr - df_gal.loc[mask_binr, 'bin_snr'] = pd.qcut( - df_gal.loc[mask_binr, 'snr'], - n_bins_snr, - labels=False, - retbins=False + # select galaxies in the bin + mask_binr = df_gal["bin_R"].values == ibin_r + + # bin in snr + df_gal.loc[mask_binr, "bin_snr"] = pd.qcut( + df_gal.loc[mask_binr, "snr"], n_bins_snr, labels=False, retbins=False ) - #group by bin - df_gal_grouped = df_gal.groupby(['bin_R', 'bin_snr']) + # group by bin + df_gal_grouped = df_gal.groupby(["bin_R", "bin_snr"]) ngroups = df_gal_grouped.ngroups - #Performing first round calibration + # Performing first round calibration alpha_df = pd.DataFrame( - 0., + 0.0, index=np.arange(ngroups), - columns=['R', 'SNR','alpha_1', 'alpha_2', 'alpha_1_err', 'alpha_2_err'], + columns=["R", "SNR", "alpha_1", "alpha_2", "alpha_1_err", "alpha_2_err"], ) - + i_group = 0 for name, group in df_gal_grouped: - - #Save weighted average - alpha_df.loc[i_group, 'R'] = np.average(group['size_ratio'], weights=group['w']) - alpha_df.loc[i_group, 'SNR'] = np.average(group['snr'], weights=group['w']) - - #Fit linear model to compute alpha - e1_out = np.array(group['e1']) - e2_out = np.array(group['e2']) - weight_out = np.array(group['w']) - e1_PSF = np.array(group['e1_PSF']) - e2_PSF = np.array(group['e2_PSF']) + # Save weighted average + alpha_df.loc[i_group, "R"] = np.average(group["size_ratio"], weights=group["w"]) + alpha_df.loc[i_group, "SNR"] = np.average(group["snr"], weights=group["w"]) + + # Fit linear model to compute alpha + e1_out = np.array(group["e1"]) + e2_out = np.array(group["e2"]) + weight_out = np.array(group["w"]) + e1_PSF = np.array(group["e1_PSF"]) + e2_PSF = np.array(group["e2_PSF"]) del group - #Fit e1 + # Fit e1 mod_wls = sm.WLS(e1_out, sm.add_constant(e1_PSF), weights=weight_out) try: res_wls = mod_wls.fit() - except: - raise RunTimeError("Linear regression fit for PSF leakage failed") - alpha_df.loc[i_group, 'alpha_1'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_1_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + except Exception as err: + raise RuntimeError("Linear regression fit for PSF leakage failed") from err + alpha_df.loc[i_group, "alpha_1"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_1_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del res_wls, mod_wls - #Fit e2 + # Fit e2 mod_wls = sm.WLS(e2_out, sm.add_constant(e2_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_2'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_2_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_2"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_2_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del weight_out, res_wls, mod_wls i_group += 1 - #Fit polynomial to remove general trend - fitting_weight = 1./np.square(alpha_df['alpha_1_err'].values) - A = np.vstack([fitting_weight*1, - fitting_weight*np.power(alpha_df['SNR'].values, -2), - fitting_weight*np.power(alpha_df['SNR'].values, -3), - fitting_weight*alpha_df['R'].values, - fitting_weight*alpha_df['R'].values*np.power(alpha_df['SNR'].values, -2)]).T - poly1 = np.linalg.lstsq(A, fitting_weight*alpha_df['alpha_1'].values, rcond=None)[0] + # Fit polynomial to remove general trend + fitting_weight = 1.0 / np.square(alpha_df["alpha_1_err"].values) + A = np.vstack( + [ + fitting_weight * 1, + fitting_weight * np.power(alpha_df["SNR"].values, -2), + fitting_weight * np.power(alpha_df["SNR"].values, -3), + fitting_weight * alpha_df["R"].values, + fitting_weight + * alpha_df["R"].values + * np.power(alpha_df["SNR"].values, -2), + ] + ).T + poly1 = np.linalg.lstsq(A, fitting_weight * alpha_df["alpha_1"].values, rcond=None)[ + 0 + ] del fitting_weight, A - fitting_weight = 1./np.square(alpha_df['alpha_2_err'].values) - A = np.vstack([fitting_weight*1, - fitting_weight*np.power(alpha_df['SNR'].values, -2), - fitting_weight*np.power(alpha_df['SNR'].values, -3), - fitting_weight*alpha_df['R'].values, - fitting_weight*alpha_df['R'].values*np.power(alpha_df['SNR'].values, -2)]).T - poly2 = np.linalg.lstsq(A, fitting_weight*alpha_df['alpha_2'].values, rcond=None)[0] + fitting_weight = 1.0 / np.square(alpha_df["alpha_2_err"].values) + A = np.vstack( + [ + fitting_weight * 1, + fitting_weight * np.power(alpha_df["SNR"].values, -2), + fitting_weight * np.power(alpha_df["SNR"].values, -3), + fitting_weight * alpha_df["R"].values, + fitting_weight + * alpha_df["R"].values + * np.power(alpha_df["SNR"].values, -2), + ] + ).T + poly2 = np.linalg.lstsq(A, fitting_weight * alpha_df["alpha_2"].values, rcond=None)[ + 0 + ] del fitting_weight, A - #Compute alpha to remove the general trend - #e1 - alpha_1 = poly1[0] \ - + poly1[1] * np.power(df_gal['snr'], -2) \ - + poly1[2] * np.power(df_gal['snr'], -3) \ - + poly1[3] * df_gal['size_ratio'] \ - + poly1[4] * df_gal['size_ratio'] * np.power(df_gal['snr'], -2) + # Compute alpha to remove the general trend + # e1 + alpha_1 = ( + poly1[0] + + poly1[1] * np.power(df_gal["snr"], -2) + + poly1[2] * np.power(df_gal["snr"], -3) + + poly1[3] * df_gal["size_ratio"] + + poly1[4] * df_gal["size_ratio"] * np.power(df_gal["snr"], -2) + ) - df_gal.loc[:, 'e1_cor'] = df_gal['e1'].values - alpha_1 * df_gal['e1_PSF'].values + df_gal.loc[:, "e1_cor"] = df_gal["e1"].values - alpha_1 * df_gal["e1_PSF"].values del poly1 - #e2 - alpha_2 = poly2[0] \ - + poly2[1] * np.power(df_gal['snr'], -2) \ - + poly2[2] * np.power(df_gal['snr'], -3) \ - + poly2[3] * df_gal['size_ratio'] \ - + poly2[4] * df_gal['size_ratio'] * np.power(df_gal['snr'], -2) - df_gal.loc[:, 'e2_cor'] = df_gal['e2'].values - alpha_2 * df_gal['e2_PSF'].values + # e2 + alpha_2 = ( + poly2[0] + + poly2[1] * np.power(df_gal["snr"], -2) + + poly2[2] * np.power(df_gal["snr"], -3) + + poly2[3] * df_gal["size_ratio"] + + poly2[4] * df_gal["size_ratio"] * np.power(df_gal["snr"], -2) + ) + df_gal.loc[:, "e2_cor"] = df_gal["e2"].values - alpha_2 * df_gal["e2_PSF"].values del poly2 - #Initialise second run of calibration - df_gal.loc[:, 'alpha_1_cor'] = -999. - df_gal.loc[:, 'alpha_2_cor'] = -999. - alpha_df.loc[:, 'alpha_1_corr'] = -999. - alpha_df.loc[:, 'alpha_2_corr'] = -999. - alpha_df.loc[:, 'alpha_1_corr_err'] = -999. - alpha_df.loc[:, 'alpha_2_corr_err'] = -999. - - #Second run of calibration - for i_group in range(n_bins_r*n_bins_snr): - #Get the mask - mask_group = (df_gal['bin_R'].values == i_group//n_bins_snr) & (df_gal['bin_snr'].values == i_group%n_bins_snr) - #Fit linear model to compute alpha - e1_out = np.array(df_gal.loc[mask_group, 'e1_cor']) - e2_out = np.array(df_gal.loc[mask_group, 'e2_cor']) - weight_out = np.array(df_gal.loc[mask_group, 'w']) - e1_PSF = np.array(df_gal.loc[mask_group, 'e1_PSF']) - e2_PSF = np.array(df_gal.loc[mask_group, 'e2_PSF']) - - #Fit e1 + # Initialise second run of calibration + df_gal.loc[:, "alpha_1_cor"] = -999.0 + df_gal.loc[:, "alpha_2_cor"] = -999.0 + alpha_df.loc[:, "alpha_1_corr"] = -999.0 + alpha_df.loc[:, "alpha_2_corr"] = -999.0 + alpha_df.loc[:, "alpha_1_corr_err"] = -999.0 + alpha_df.loc[:, "alpha_2_corr_err"] = -999.0 + + # Second run of calibration + for i_group in range(n_bins_r * n_bins_snr): + # Get the mask + mask_group = (df_gal["bin_R"].values == i_group // n_bins_snr) & ( + df_gal["bin_snr"].values == i_group % n_bins_snr + ) + # Fit linear model to compute alpha + e1_out = np.array(df_gal.loc[mask_group, "e1_cor"]) + e2_out = np.array(df_gal.loc[mask_group, "e2_cor"]) + weight_out = np.array(df_gal.loc[mask_group, "w"]) + e1_PSF = np.array(df_gal.loc[mask_group, "e1_PSF"]) + e2_PSF = np.array(df_gal.loc[mask_group, "e2_PSF"]) + + # Fit e1 mod_wls = sm.WLS(e1_out, sm.add_constant(e1_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_1_corr'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_1_corr_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_1_corr"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_1_corr_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del res_wls, mod_wls - #Fit e2 + # Fit e2 mod_wls = sm.WLS(e2_out, sm.add_constant(e2_PSF), weights=weight_out) res_wls = mod_wls.fit() - alpha_df.loc[i_group, 'alpha_2_corr'] = res_wls.params[1] - alpha_df.loc[i_group, 'alpha_2_corr_err'] = np.sqrt(res_wls.cov_params()[1, 1]) + alpha_df.loc[i_group, "alpha_2_corr"] = res_wls.params[1] + alpha_df.loc[i_group, "alpha_2_corr_err"] = np.sqrt(res_wls.cov_params()[1, 1]) del weight_out, res_wls, mod_wls - df_gal.loc[mask_group, 'alpha_1_corr'] = alpha_df.loc[i_group, 'alpha_1_corr'] - df_gal.loc[mask_group, 'alpha_2_corr'] = alpha_df.loc[i_group, 'alpha_2_corr'] + df_gal.loc[mask_group, "alpha_1_corr"] = alpha_df.loc[i_group, "alpha_1_corr"] + df_gal.loc[mask_group, "alpha_2_corr"] = alpha_df.loc[i_group, "alpha_2_corr"] - alpha_1 += df_gal['alpha_1_corr'].values - alpha_2 += df_gal['alpha_2_corr'].values + alpha_1 += df_gal["alpha_1_corr"].values + alpha_2 += df_gal["alpha_2_corr"].values return alpha_1, alpha_2 -def get_quantities_binned(cat_gal, num_bins_x, num_bins_y=None, which=["response", "number", "leakage"], verbose=True): +def get_quantities_binned( + cat_gal, + num_bins_x, + num_bins_y=None, + which=["response", "number", "leakage"], + verbose=True, +): if verbose: print("Compute binned quantities") if num_bins_y is None: num_bins_y = num_bins_x - - bin_keys = ['snr', 'size_ratio'] # Create input dataframe df_gal = build_df(cat_gal) - + # Create logarithmic bins in size and SNR bin_edges = {} - bin_edges["snr"] = cut_to_bins(df_gal, "snr", num_bins_x, type="log", x_min=2, x_max=700) - bin_edges["size_ratio"] = cut_to_bins(df_gal, "size_ratio", num_bins_y, type="log", x_min=0.3, x_max=10) - + bin_edges["snr"] = cut_to_bins( + df_gal, "snr", num_bins_x, type="log", x_min=2, x_max=700 + ) + bin_edges["size_ratio"] = cut_to_bins( + df_gal, "size_ratio", num_bins_y, type="log", x_min=0.3, x_max=10 + ) + # Initialize output dict quantities = {} for key in which: @@ -542,38 +553,43 @@ def get_quantities_binned(cat_gal, num_bins_x, num_bins_y=None, which=["response quantities[key] = np.zeros((num_bins_x, num_bins_y, 2, 2)) else: quantities[key] = np.zeros((num_bins_x, num_bins_y)) - - + # Iniitialize parameter for minimizations params = leakage.init_parameters() # Loop over bins - for i in tqdm.tqdm(range(num_bins_x), position=0, disable=not verbose, desc="bins_x"): + for i in tqdm.tqdm( + range(num_bins_x), position=0, disable=not verbose, desc="bins_x" + ): for j in tqdm.tqdm(range(num_bins_y), position=1, leave=False, desc="bins_y"): - # Get indices for bin (i, j) - bin_mask = (df_gal['snr_log_bins'] == i) & (df_gal['size_ratio_log_bins'] == j) + bin_mask = (df_gal["snr_log_bins"] == i) & ( + df_gal["size_ratio_log_bins"] == j + ) if any(bin_mask): - # Compute quantity for key in which: if key == "response": for idx in (0, 1): for jdx in (0, 1): - quantities["response"][i, j, idx, jdx] = np.mean(df_gal[bin_mask][f"R_g{idx+1}{jdx+1}"]) + quantities["response"][i, j, idx, jdx] = np.mean( + df_gal[bin_mask][f"R_g{idx + 1}{jdx + 1}"] + ) elif key == "number": - quantities["number"][i,j] = np.sum(bin_mask) + quantities["number"][i, j] = np.sum(bin_mask) elif key == "leakage": obj._dat = df_gal[bin_mask] obj.PSF_leakage(params=params, do_plots=False) for idx in (0, 1): for jdx in (0, 1): - quantities["leakage"][i, j, idx, jdx] = obj.par_best_fit[f"a{idx+1}{jdx+1}"].value + quantities["leakage"][i, j, idx, jdx] = ( + obj.par_best_fit[f"a{idx + 1}{jdx + 1}"].value + ) return quantities, bin_edges -def get_calibrate_e_from_cat(path_cat_gal, weight_type='des', verbose=False): +def get_calibrate_e_from_cat(path_cat_gal, weight_type="des", verbose=False): """ Calibrates ellipticities from a galaxy catalog with a certain weight type. @@ -591,40 +607,44 @@ def get_calibrate_e_from_cat(path_cat_gal, weight_type='des', verbose=False): g_cal : np.array Calibrated ellipticities """ - assert (weight_type in ['iv', 'des']), "The weight_type is not correct. Options 'iv' (inverse variance) or 'des'." - + assert weight_type in ["iv", "des"], ( + "The weight_type is not correct. Options 'iv' (inverse variance) or 'des'." + ) + hdu_gal = fits.open(path_cat_gal) cat_gal = hdu_gal[1].data - R_select = np.array([ - [hdu_gal[0].header['R_S11'], hdu_gal[0].header['R_S12']], - [hdu_gal[0].header['R_S21'], hdu_gal[0].header['R_S22']] - ]) + R_select = np.array( + [ + [hdu_gal[0].header["R_S11"], hdu_gal[0].header["R_S12"]], + [hdu_gal[0].header["R_S21"], hdu_gal[0].header["R_S22"]], + ] + ) if verbose: - print('R_select\n', R_select) + print("R_select\n", R_select) - g = np.array([cat_gal['e1_uncal'], cat_gal['e2_uncal']]) + g = np.array([cat_gal["e1_uncal"], cat_gal["e2_uncal"]]) - c = np.average(g, axis=1, weights=cat_gal['w_'+weight_type]) + c = np.average(g, axis=1, weights=cat_gal["w_" + weight_type]) if verbose: - print('Additive bias\n', c) + print("Additive bias\n", c) R_shear = np.zeros((2, 2)) for iidx in range(2): for jidx in range(2): - R_shear[iidx, jidx] = np.mean(cat_gal[f'R_g{iidx+1}{jidx+1}']) + R_shear[iidx, jidx] = np.mean(cat_gal[f"R_g{iidx + 1}{jidx + 1}"]) if verbose: - print('R_shear\n', R_shear) + print("R_shear\n", R_shear) R = R_shear + R_select if verbose: - print('R\n', R) - + print("R\n", R) + c_corr = np.linalg.inv(R) @ c g_cal = np.linalg.inv(R) @ g @@ -634,7 +654,7 @@ def get_calibrate_e_from_cat(path_cat_gal, weight_type='des', verbose=False): return g_cal[0], g_cal[1] -def get_calibrate_no_leakage_e_from_cat(path_cat_gal, weight_type='des', verbose=False): +def get_calibrate_no_leakage_e_from_cat(path_cat_gal, weight_type="des", verbose=False): """ Calibrate ellipticities and removes leakage from a galaxy catalog with a certain weight type. @@ -646,7 +666,7 @@ def get_calibrate_no_leakage_e_from_cat(path_cat_gal, weight_type='des', verbose Type of weight to use. Options are 'des' (DES weight) or 'iv' (inverse variance) verbose : bool, optional, default=False If True, print intermediate results - + Returns ------- e1_noleak : np.array @@ -657,8 +677,8 @@ def get_calibrate_no_leakage_e_from_cat(path_cat_gal, weight_type='des', verbose e1, e2 = get_calibrate_e_from_cat(path_cat_gal, weight_type, verbose) cat_gal = fits.getdata(path_cat_gal) - e1_noleak = e1 - cat_gal['alpha_1']*cat_gal['e1_PSF'] - e2_noleak = e2 - cat_gal['alpha_2']*cat_gal['e2_PSF'] + e1_noleak = e1 - cat_gal["alpha_1"] * cat_gal["e1_PSF"] + e2_noleak = e2 - cat_gal["alpha_2"] * cat_gal["e2_PSF"] return e1_noleak, e2_noleak @@ -707,9 +727,9 @@ def __init__( self, data, mask, - masking_type='gal', + masking_type="gal", step=0.01, - prefix='NGMIX', + prefix="NGMIX", snr_min=10, snr_max=500, rel_size_min=0.5, @@ -732,12 +752,12 @@ def __init__( self._size_corr_ell = size_corr_ell if verbose: print( - f'Metacal cuts: {snr_min} self._snr_max or \ - rel_size_min < self._rel_size_min: + if ( + snr_min < self._snr_min + or snr_max > self._snr_max + or rel_size_min < self._rel_size_min + or rel_size_max > self._rel_size_max + ): print( - 'At least on cut is less stringend than existing one, ' - + 'skipping...' + "At least on cut is less stringend than existing one, " + "skipping..." ) return @@ -987,8 +995,8 @@ def add_cuts(self, snr_min=10, snr_max=500, rel_size_min=0.5): self._rel_size_max = rel_size_max if self._verbose: print( - f'Metacal new cuts: {snr_min} self._rel_size_min) & (Tr_tmp / Tpsf < self._rel_size_max) & (snr_flux > self._snr_min) @@ -1038,23 +1045,21 @@ def _masking_gal_mom(self): self.mask_dict = {} for data, name in zip( [self.ns, self.m1, self.p1, self.m2, self.p2], - ['ns', 'm1', 'p1', 'm2', 'p2'] + ["ns", "m1", "p1", "m2", "p2"], ): - Tr_tmp = data['T'] + Tr_tmp = data["T"] if self._size_corr_ell: - Tr_tmp *= ( - (1 - (data['g1'] ** 2 + data['g2'] ** 2)) - / (1 + (data['g1'] ** 2 + data['g2'] ** 2)) + Tr_tmp *= (1 - (data["g1"] ** 2 + data["g2"] ** 2)) / ( + 1 + (data["g1"] ** 2 + data["g2"] ** 2) ) - snr_flux = data['flux'] / data['flux_err'] mask_tmp = ( - (data['flag'] == 0) - & (1 - data['Tpsf'] / data['T'] > self._rel_size_min) - & (data['s2n'] > self._snr_min) - & (data['s2n'] < self._snr_max) - & (data['g1'] != -10) - & (data['g1'] != 0) + (data["flag"] == 0) + & (1 - data["Tpsf"] / data["T"] > self._rel_size_min) + & (data["s2n"] > self._snr_min) + & (data["s2n"] < self._snr_max) + & (data["g1"] != -10) + & (data["g1"] != 0) ) # Take care of rotated version @@ -1071,13 +1076,13 @@ def _masking_star(self): self.mask_dict = {} for data, name in zip( [self.ns, self.m1, self.p1, self.m2, self.p2], - ['ns', 'm1', 'p1', 'm2', 'p2'] + ["ns", "m1", "p1", "m2", "p2"], ): - if hasattr(self, 'snr_sextractor'): + if hasattr(self, "snr_sextractor"): snr_flux = self.snr_sextractor else: - snr_flux = data['flux'] / data['flux_err'] - mask_tmp = (data['flag'] == 0) & (snr_flux > 10) & (snr_flux < 500) + snr_flux = data["flux"] / data["flux_err"] + mask_tmp = (data["flag"] == 0) & (snr_flux > 10) & (snr_flux < 500) # Take care of rotated version ind_masked = np.where(mask_tmp == True)[0] @@ -1090,50 +1095,48 @@ def _shear_response(self): Compute shear response matrix """ sign = 1 - if self._prefix == 'GALSIM': + if self._prefix == "GALSIM": sign = -1 - ma = self.mask_dict['ns'] + ma = self.mask_dict["ns"] h2 = 2 * self._step - self.R11 = (self.p1['g1'][ma] - self.m1['g1'][ma]) / h2 - self.R22 = sign * (self.p2['g2'][ma] - self.m2['g2'][ma]) / h2 - self.R12 = (self.p2['g1'][ma] - self.m2['g1'][ma]) / h2 - self.R21 = (self.p1['g2'][ma] - self.m1['g2'][ma]) / h2 + self.R11 = (self.p1["g1"][ma] - self.m1["g1"][ma]) / h2 + self.R22 = sign * (self.p2["g2"][ma] - self.m2["g2"][ma]) / h2 + self.R12 = (self.p2["g1"][ma] - self.m2["g1"][ma]) / h2 + self.R21 = (self.p1["g2"][ma] - self.m1["g2"][ma]) / h2 self.R_shear = np.array([[self.R11, self.R12], [self.R21, self.R22]]) def _shear_response_std( - self, - stat_operator=lambda x: jackknif_weighted_average2(x, np.ones_like(x)) + self, stat_operator=lambda x: jackknif_weighted_average2(x, np.ones_like(x)) ): """Shear Response Std. Standard deviation of shear response """ - ma = self.mask_dict['ns'] + ma = self.mask_dict["ns"] h2 = 2 * self._step - if len(self.ns['g1'][ma]) == 0: + if len(self.ns["g1"][ma]) == 0: self.R_shear_std = np.array([[np.nan, np.nan], [np.nan, np.nan]]) else: - self.R11_stds = stat_operator( - (self.p1['g1'][ma] - self.m1['g1'][ma]) / h2 - )[1] - self.R22_stds = stat_operator( - (self.p2['g2'][ma] - self.m2['g2'][ma]) / h2 - )[1] - self.R12_stds = stat_operator( - (self.p2['g1'][ma] - self.m2['g1'][ma]) / h2 - )[1] - self.R21_stds = stat_operator( - (self.p1['g2'][ma] - self.m1['g2'][ma]) / h2 - )[1] - - self.R_shear_std = np.array([ - [self.R11_stds, self.R12_stds], - [self.R21_stds, self.R22_stds] - ]) + self.R11_stds = stat_operator((self.p1["g1"][ma] - self.m1["g1"][ma]) / h2)[ + 1 + ] + self.R22_stds = stat_operator((self.p2["g2"][ma] - self.m2["g2"][ma]) / h2)[ + 1 + ] + self.R12_stds = stat_operator((self.p2["g1"][ma] - self.m2["g1"][ma]) / h2)[ + 1 + ] + self.R21_stds = stat_operator((self.p1["g2"][ma] - self.m1["g2"][ma]) / h2)[ + 1 + ] + + self.R_shear_std = np.array( + [[self.R11_stds, self.R12_stds], [self.R21_stds, self.R22_stds]] + ) def _selection_response(self): """Add docstring. @@ -1142,36 +1145,31 @@ def _selection_response(self): """ sign = 1 - if self._prefix == 'GALSIM': + if self._prefix == "GALSIM": sign = -1 - ma_p1 = self.mask_dict['p1'] - ma_m1 = self.mask_dict['m1'] - ma_p2 = self.mask_dict['p2'] - ma_m2 = self.mask_dict['m2'] + ma_p1 = self.mask_dict["p1"] + ma_m1 = self.mask_dict["m1"] + ma_p2 = self.mask_dict["p2"] + ma_m2 = self.mask_dict["m2"] h2 = 2 * self._step self.R11_s = ( - np.mean(self.ns['g1'][ma_p1]) - - np.mean(self.ns['g1'][ma_m1]) - ) / h2 - self.R22_s = sign * ( - np.mean(self.ns['g2'][ma_p2]) - - np.mean(self.ns['g2'][ma_m2]) + np.mean(self.ns["g1"][ma_p1]) - np.mean(self.ns["g1"][ma_m1]) ) / h2 + self.R22_s = ( + sign * (np.mean(self.ns["g2"][ma_p2]) - np.mean(self.ns["g2"][ma_m2])) / h2 + ) self.R12_s = ( - np.mean(self.ns['g1'][ma_p2]) - - np.mean(self.ns['g1'][ma_m2]) + np.mean(self.ns["g1"][ma_p2]) - np.mean(self.ns["g1"][ma_m2]) ) / h2 self.R21_s = ( - np.mean(self.ns['g2'][ma_p1]) - - np.mean(self.ns['g2'][ma_m1]) + np.mean(self.ns["g2"][ma_p1]) - np.mean(self.ns["g2"][ma_m1]) ) / h2 - self.R_selection = np.array([ - [self.R11_s, self.R12_s], - [self.R21_s, self.R22_s] - ]) + self.R_selection = np.array( + [[self.R11_s, self.R12_s], [self.R21_s, self.R22_s]] + ) def _total_response(self): """Add docstring. @@ -1185,37 +1183,27 @@ def _total_response(self): else: print("Computing response weighted by", self._global_R_weight) # Get weights of masked no-shear objects - weights = self.ns[self._global_R_weight][self.mask_dict['ns']] + weights = self.ns[self._global_R_weight][self.mask_dict["ns"]] self.R_shear_global = np.average(self.R_shear, axis=2, weights=weights) self.R = self.R_shear_global + self.R_selection -def mask_gal_size(T, Tpsf, rel_size_min, rel_size_max, size_corr_ell=False, g1=None, g2=None): +def mask_gal_size( + T, Tpsf, rel_size_min, rel_size_max, size_corr_ell=False, g1=None, g2=None +): Tr_tmp = T if size_corr_ell: - Tr_tmp *= ( - (1 - g1 **2 + g2 ** 2) / (1 + g1 ** 2 + g2 **2) - ) + Tr_tmp *= (1 - g1**2 + g2**2) / (1 + g1**2 + g2**2) - mask = ( - (Tr_tmp / Tpsf > rel_size_min) - & (Tr_tmp / Tpsf < rel_size_max) - ) + mask = (Tr_tmp / Tpsf > rel_size_min) & (Tr_tmp / Tpsf < rel_size_max) return mask - - def mask_gal_SNR(SNR, snr_min, snr_max): - mask = ( - (SNR > snr_min) - & (SNR < snr_max) - ) + mask = (SNR > snr_min) & (SNR < snr_max) return mask - - diff --git a/src/sp_validation/catalog.py b/src/sp_validation/catalog.py index 867e4e7c..4fe45476 100644 --- a/src/sp_validation/catalog.py +++ b/src/sp_validation/catalog.py @@ -11,24 +11,17 @@ """ -import os -import re -import numpy as np import getpass import h5py +import numpy as np import tqdm - -from datetime import datetime - -from astropy.io import fits from astropy import coordinates as coords from astropy import units as u - +from astropy.io import fits from cs_util import cat -from sp_validation import format -from sp_validation import io +from sp_validation import format, io from sp_validation.survey import get_footprint from sp_validation.version import __version__ @@ -96,8 +89,8 @@ def print_mean_ellipticity( io.print_stats(msg, stats_file, verbose=verbose) msg = ( - f"Fraction of invalid objects = {n_tot-n_tot_val}/{n_tot}" - + f" = {(n_tot-n_tot_val)/n_tot:.3%}" + f"Fraction of invalid objects = {n_tot - n_tot_val}/{n_tot}" + + f" = {(n_tot - n_tot_val) / n_tot:.3%}" ) io.print_stats(msg, stats_file, verbose=verbose) @@ -252,10 +245,9 @@ def check_invalid(dd, key, comp, val, stats_file, name=None, verbose=False): n_all = len(dd) for i in range(len(key)): - w = dd[key[i]][:, comp[i]] == val[i] n_inv_psf = len(np.where(w)[0]) - msg = "Invalid {} found for {}/{} = {:.1g}% objects" "".format( + msg = "Invalid {} found for {}/{} = {:.1g}% objects".format( name[i], n_inv_psf, n_all, n_inv_psf / n_all ) io.print_stats(msg, stats_file, verbose=verbose) @@ -404,7 +396,6 @@ def read_shape_catalog( ra = dat[hdu_no].data["RA"] dec = dat[hdu_no].data["Dec"] - g = [np.empty_like(ra), np.empty_like(ra)] g1 = dat[hdu_no].data["e1_uncal"] g2 = dat[hdu_no].data["e2_uncal"] w = dat[hdu_no].data[w_name] @@ -537,8 +528,8 @@ def write_shape_catalog( for idx in (0, 1): col_info_arr.append( ( - fits.Column(name=f"e{idx+1}", array=g[idx], format="D"), - f"Calibrated reduced shear estimate comp {idx+1}", + fits.Column(name=f"e{idx + 1}", array=g[idx], format="D"), + f"Calibrated reduced shear estimate comp {idx + 1}", ) ) # Signal-to-noise ratio @@ -562,9 +553,7 @@ def write_shape_catalog( ], ): if x is not None: - col_info_arr.append( - (fits.Column(name=name, array=x, format="D"), descr) - ) + col_info_arr.append((fits.Column(name=name, array=x, format="D"), descr)) if add_cols is not None: for idx, name in enumerate(add_cols): @@ -578,9 +567,7 @@ def write_shape_catalog( my_format = f"{shape[1]}D" col_info_arr.append( ( - fits.Column( - name=name, array=add_cols[name], format=my_format - ), + fits.Column(name=name, array=add_cols[name], format=my_format), name, ) ) @@ -593,14 +580,14 @@ def write_shape_catalog( # Add human-readable descriptions for idx, col_info in enumerate(col_info_arr): - table_hdu.header[f"TTYPE{idx+1}"] = ( + table_hdu.header[f"TTYPE{idx + 1}"] = ( col_info[0].name, col_info[1], ) # Primary HDU with information in header primary_header = fits.Header() - + if add_header: primary_header.update(add_header) @@ -735,9 +722,7 @@ def read_param_file(path, verbose=False): return param_list_unique -def read_hdf5_file( - file_path, name, stats_file, check_only=False, param_path=None -): +def read_hdf5_file(file_path, name, stats_file, check_only=False, param_path=None): """Read HDF5 File. Read hdf5 file and return contained data. @@ -759,16 +744,12 @@ def read_hdf5_file( data """ - param_list = ( - read_param_file(param_path, verbose=True) if param_path else None - ) + param_list = read_param_file(param_path, verbose=True) if param_path else None with h5py.File(file_path, "r") as hdf5_file: # Find patch group in hierarchical structure - if not f"patches/{name}" in hdf5_file: - raise KeyError( - f"Entry patches/{name} not found in file {file_path}" - ) + if f"patches/{name}" not in hdf5_file: + raise KeyError(f"Entry patches/{name} not found in file {file_path}") patch_group = hdf5_file[f"patches/{name}"] # Get size of data array @@ -786,7 +767,6 @@ def read_hdf5_file( data_list = [] ID_pbl = set() for ID in tqdm.tqdm(patch_group): - # Get data for this ID from file data = patch_group[ID][()] @@ -829,7 +809,7 @@ def get_maked_col(dat, col, mask): Requested column from the data, filtered by the mask. """ - + return dat[col][mask] @@ -874,7 +854,7 @@ def get_col(dat, col, m_sel=None, m_flg=None): else: return dat[col][m_sel][m_flg] # The following does not work - #return get_maked_col(dat, col, mask_combined) + # return get_maked_col(dat, col, mask_combined) def get_snr(sh, dat, m_sel, m_flg): diff --git a/src/sp_validation/catalog_builders.py b/src/sp_validation/catalog_builders.py index 12fe0af6..1375ef8d 100644 --- a/src/sp_validation/catalog_builders.py +++ b/src/sp_validation/catalog_builders.py @@ -8,44 +8,41 @@ :Author: Martin Kilbinger """ -import sys -import os - -import numpy as np -import yaml - import datetime -from tqdm import tqdm - -from optparse import OptionParser +import os from importlib.metadata import version import h5py import healsparse as hsp import numpy as np - +import yaml from astropy.io import fits -from astropy.table import Column - -from cs_util import logging -from cs_util import cat from cs_util import args as cs_args - -from . import format -from . import calibration -from . import catalog as sp_cat +from cs_util import logging # Spatial-masking primitives now live in ``masks``; re-exported here so external # code using ``from sp_validation import catalog_builders as sp_joint`` keeps # resolving ``sp_joint.Mask``, ``sp_joint.get_masks_from_config``, etc. from sp_validation.masks import ( Mask, + confusion_matrix, + correlation_matrix, get_masks_from_config, print_mask_stats, - correlation_matrix, - confusion_matrix, ) +from . import calibration, format +from . import catalog as sp_cat + +# Names re-exported for external code that resolves them off this module. +__all__ = [ + "Mask", + "get_masks_from_config", + "print_mask_stats", + "correlation_matrix", + "confusion_matrix", +] + class BaseCat(object): """Base_Cat. @@ -64,8 +61,8 @@ def set_params_from_command_line(self, args): Does not work from ipython or jupyter. """ - # Read command line options - options = cs_args.parse_options( + # Read command line options (parses sys.argv; may exit on --help) + cs_args.parse_options( self._params, self._short_options, self._types, @@ -74,17 +71,17 @@ def set_params_from_command_line(self, args): # Save calling command logging.log_command(args) - + def read_config_set_params(self, fpath): """Read Config Set Params. - + Read configuration file and sets class parameters. - + Parameters ---------- fpath : str inpput file path - + Returns ------- dict @@ -101,7 +98,7 @@ def read_config_set_params(self, fpath): # Copy parameters to object for key in params: self._params[key] = params[key] - + return config def read_cat(self, load_into_memory=False, mode="r", hdu=1, name="data"): @@ -200,7 +197,7 @@ def get_header(self, path=None): """ if path is None: path = self._params["output_path"] - + with h5py.File(path, "r") as f: header = dict(f.attrs) return header @@ -226,14 +223,13 @@ def write_hdf5_file(self, dat, output_path=None): print("Creating hdf5 file") with h5py.File(output_path, "w") as f: - self.write_hdf5_header(f) dset = f.create_dataset("data", data=dat) dset[:] = dat if self._params["verbose"]: - print(f"Done.") + print("Done.") def close_hd5(self): """Close HD5. @@ -372,11 +368,11 @@ def get_n_obj(self, patches, base_path, input_sub_path): input_path = f"{base_path}/{patch}/{input_sub_path}" try: hdu_list = fits.open(input_path) - except: + except Exception as err: raise ValueError( f"Could not open file {input_path} at HDU" + f" #{self._params['hdu']}" - ) + ) from err hdu_lists.append(hdu_list) this_n = int(hdu_list[self._params["hdu"]].header["NAXIS2"]) @@ -421,9 +417,7 @@ def get_col_info(self, dat): n_col += 1 if self._params["verbose"]: - print( - f"Number of input (output) columns = {len(col_names)} ({n_col})" - ) + print(f"Number of input (output) columns = {len(col_names)} ({n_col})") return col_names, formats, ndim, n_col @@ -494,9 +488,7 @@ def init_data(self, n_col, n_obj, ndim, dat): dtype_tmp_list = [] for name in ndim: if ndim[name] == 1: - dtype_tmp_list.append( - (name, self.dtype_out(name, dat[name].dtype)) - ) + dtype_tmp_list.append((name, self.dtype_out(name, dat[name].dtype))) else: for jdx in range(ndim[name]): dtype_tmp_list.append( @@ -540,7 +532,6 @@ def write_hdf5_file(self, dat_all, patches): ) with h5py.File(output_path, "w") as f: - self.write_hdf5_header(f) dset = f.create_dataset("data", data=dat_all) @@ -592,22 +583,20 @@ def merge_catalogues(self, patches, base_path="."): # Read data start = end = 0 for idx, patch in enumerate(patches): - input_path = f"{base_path}/{patch}/{input_sub_path}" try: dat = fits.getdata(input_path, self._params["hdu"]) # dat = hdu_lists[idx][self._params["hdu"]].data hdu_lists[idx].close() - except: + except Exception as err: raise ValueError( f"Could not read data of file {input_path} at HDU" + f" #{self._params['hdu']}" - ) + ) from err # Create empty lists if first patch if idx == 0: - col_names, formats, ndim, n_col = self.get_col_info(dat) dat_all = self.init_data(n_col, n_obj, ndim, dat) @@ -624,22 +613,19 @@ def merge_catalogues(self, patches, base_path="."): else: # Copy all components of multi-D column for jdx in range(ndim[name]): - dat_all[names_out[i_col + jdx]][start:end] = dat[name][ - :, jdx - ] + dat_all[names_out[i_col + jdx]][start:end] = dat[name][:, jdx] i_col += ndim[name] # Add patch number dat_all["patch"][start:end] = patch[1:] if i_col + 1 != n_col: raise ValueError( - "Inconsistent number of columns, {i_col + 1}" - + f" != {n_col}" + "Inconsistent number of columns, {i_col + 1}" + f" != {n_col}" ) if self._params["verbose"]: print( f"{patch}: Added {len(dat)} (~{format.millify(len(dat))})" - + f" objects (from {start} to {end-1})." + + f" objects (from {start} to {end - 1})." ) start = end @@ -790,9 +776,7 @@ def update_params(self): stop=True, sep="\\", ) - self._params["aux_mask_num"] = len( - self._params["aux_mask_file_list"] - ) + self._params["aux_mask_num"] = len(self._params["aux_mask_file_list"]) self._params["aux_mask_label_list"] = cs_args.my_string_split( self._params["aux_mask_labels"], verbose=self._params["verbose"], @@ -802,7 +786,7 @@ def update_params(self): ) else: self._params["aux_mask_file_list"] = [] - + if "verbose" not in self._params: self._params["verbose"] = False @@ -850,22 +834,22 @@ def get_paths_bit_masks(self): + f"nside{self._params['nside']}_n{bit}.hsp" ) return paths - + def get_mask(self, path): """Get Mask. - + Read from file and return healsparse mask. - + Parameters ---------- path: str input path - + Returns ------- hsp.HealSparseMap mask - + """ if self._params["verbose"]: print(f"Reading mask file {path}...") @@ -1006,9 +990,9 @@ def append_masks(self, dat, masks): def write_hdf5_file(self, dat, dat_new=None, masks=None): """Write HDF5 File. - + Save data to a hdf5 file on disk. - + Parameters ---------- dat : h5py dataset @@ -1022,7 +1006,6 @@ def write_hdf5_file(self, dat, dat_new=None, masks=None): ------- """ with h5py.File(self._params["output_path"], "w") as f: - self.write_hdf5_header(f) dset = f.create_dataset( @@ -1044,17 +1027,17 @@ def write_hdf5_file(self, dat, dat_new=None, masks=None): dset[i:end] = dat[i:end] # Write chunk to dataset if dat_new is not None: dset_new[i:end] = dat_new[i:end] # Write chunk to dataset - + if masks is not None: # Adding mask descriptions to header dtype = np.dtype([("expr", "S20"), ("desc", "S20")]) descr_arr = np.zeros(len(masks), dtype=dtype) for idx, mask in enumerate(masks): descr_arr[idx] = ( - (mask._descr.encode("utf-8"), mask._label.encode("utf-8")) + mask._descr.encode("utf-8"), + mask._label.encode("utf-8"), ) f.create_dataset("applied_masks", data=descr_arr) - def write_hdf5_header(self, hd5file): """Write HDF5 Header. @@ -1168,7 +1151,7 @@ def read_cat(self, load_into_memory=False): return dat[()] else: return dat, dat_ext - + def add_params_to_FITS_header(self, header, cm=None): header_new = fits.Header() @@ -1178,15 +1161,15 @@ def add_params_to_FITS_header(self, header, cm=None): descriptions = ["input comprehensive catalogue"] for key, descr in zip(keys, descriptions): header_new[key] = (key, descr) - + # Metacal parameters if cm is not None: for idx, (descr, value) in enumerate(cm.items()): key = f"mc_par_{idx}" - header_new[key] = (descr, value) - + header_new[key] = (descr, value) + header.update(header_new) - + def run(self): """Run. @@ -1196,7 +1179,6 @@ def run(self): class ReadCat: - def __init__(self): self.params_default() @@ -1239,9 +1221,9 @@ def compute_weights_gatti( size_ratio_max=3, ): """Compute Weights Gatti. - + Compute Gatti et al. (2021) DES-like weights. - + """ calibration.fill_cat_gal( cat_gal, @@ -1250,7 +1232,7 @@ def compute_weights_gatti( gal_metacal, mask_combined._mask, mask_metacal, - purpose="weights" + purpose="weights", ) cat_gal["w_des"] = calibration.get_w_des( @@ -1271,17 +1253,11 @@ def compute_PSF_leakage( mask_metacal, num_bins=20, ): - """Compute PSF Leakage. - - """ + """Compute PSF Leakage.""" cat_gal["e1"] = g_corr_mc[0] cat_gal["e2"] = g_corr_mc[1] - cat_gal["e1_PSF"] = sp_cat.get_col( - dat, "e1_PSF", mask_combined._mask, mask_metacal - ) - cat_gal["e2_PSF"] = sp_cat.get_col( - dat, "e2_PSF", mask_combined._mask, mask_metacal - ) + cat_gal["e1_PSF"] = sp_cat.get_col(dat, "e1_PSF", mask_combined._mask, mask_metacal) + cat_gal["e2_PSF"] = sp_cat.get_col(dat, "e2_PSF", mask_combined._mask, mask_metacal) weight_type = "des" key = f"w_{weight_type}" @@ -1292,7 +1268,7 @@ def compute_PSF_leakage( alpha_1, alpha_2 = calibration.get_alpha_leakage_per_object( cat_gal, num_bins, weight_type ) - except: + except Exception: alpha_1, alpha_2 = -99, -99 return alpha_1, alpha_2 diff --git a/src/sp_validation/cosmo_val.py b/src/sp_validation/cosmo_val.py index 00b13c3a..4a4fd767 100644 --- a/src/sp_validation/cosmo_val.py +++ b/src/sp_validation/cosmo_val.py @@ -1,8 +1,8 @@ # %% +import configparser import copy import os from pathlib import Path -import configparser import colorama import healpy as hp @@ -218,15 +218,15 @@ def __init__( ylim_alpha=[-0.005, 0.05], ylim_xi_sys_ratio=[-0.02, 0.5], nside=1024, - nside_mask = 2**13, + nside_mask=2**13, binning="powspace", power=1 / 2, n_ell_bins=32, ell_step=10, pol_factor=True, - cell_method='map', - noise_bias_method='analytic', - fiducial_input_inka='coupled', + cell_method="map", + noise_bias_method="analytic", + fiducial_input_inka="coupled", nrandom_cell=10, path_onecovariance=None, cosmo_params=None, @@ -260,9 +260,15 @@ def __init__( self.path_onecovariance = path_onecovariance self.blind = blind - assert self.cell_method in ["map", "catalog"], "cell_method must be 'map' or 'catalog'" - assert self.noise_bias_method in ["analytic", "randoms"], "noise_bias_method must be 'analytical' or 'randoms'" - assert self.fiducial_input_inka in ["coupled", "decoupled"], "fiducial_input_inka must be 'coupled' or 'decoupled'" + assert self.cell_method in ["map", "catalog"], ( + "cell_method must be 'map' or 'catalog'" + ) + assert self.noise_bias_method in ["analytic", "randoms"], ( + "noise_bias_method must be 'analytical' or 'randoms'" + ) + assert self.fiducial_input_inka in ["coupled", "decoupled"], ( + "fiducial_input_inka must be 'coupled' or 'decoupled'" + ) # For theory calculations: # Create cosmology object using new functionality @@ -272,7 +278,6 @@ def __init__( # Use Planck 2018 defaults self.cosmo = get_cosmo() - self.treecorr_config = { "ra_units": "degrees", "dec_units": "degrees", @@ -318,7 +323,10 @@ def ensure_version_exists(ver): base_ver = ver[: -len(leak_suffix)] ensure_version_exists(base_ver) shear_cfg = cc[base_ver]["shear"] - if "e1_col_corrected" not in shear_cfg or "e2_col_corrected" not in shear_cfg: + if ( + "e1_col_corrected" not in shear_cfg + or "e2_col_corrected" not in shear_cfg + ): raise ValueError( f"{base_ver} does not have e1_col_corrected/e2_col_corrected " f"fields; cannot create {ver}" @@ -389,11 +397,12 @@ def get_redshift(self, version): configured path. """ import re + redshift_path = self.cc[version]["shear"]["redshift_path"] # Override blind if specified if self.blind is not None: - redshift_path = re.sub(r'_[ABC]\.txt$', f'_{self.blind}.txt', redshift_path) + redshift_path = re.sub(r"_[ABC]\.txt$", f"_{self.blind}.txt", redshift_path) return np.loadtxt(redshift_path, unpack=True) @@ -649,7 +658,6 @@ def basename(self, version, treecorr_config=None, npatch=None): f"_npatch={patches}" ) - def calculate_rho_tau_stats(self): out_dir = f"{self.cc['paths']['output']}/rho_tau_stats" if not os.path.exists(out_dir): @@ -688,13 +696,13 @@ def tau_stat_handler(self): @property def colors(self): return [self.cc[ver]["colour"] for ver in self.versions] - + @property def area(self): if not hasattr(self, "_area"): self.calculate_area() return self._area - + @property def n_eff_gal(self): if not hasattr(self, "_n_eff_gal"): @@ -713,7 +721,7 @@ def pseudo_cls(self): self.calculate_pseudo_cl() self.calculate_pseudo_cl_eb_cov() return self._pseudo_cls - + @property def pseudo_cls_onecov(self): if not hasattr(self, "_pseudo_cls_onecov"): @@ -725,7 +733,7 @@ def _get_binned_catalog_mask(self, ver): ra = self.results[ver].dat_shear["RA"] dec = self.results[ver].dat_shear["Dec"] hsp_map = hp.ang2pix( - self.nside_mask, + self.nside_mask, np.radians(90 - dec), np.radians(ra), lonlat=False, @@ -739,11 +747,13 @@ def calculate_area(self): for ver in self.versions: self.print_magenta(ver) - if 'mask' not in self.cc[ver]: - print("Mask not found in config file, calculating area from binned catalog") + if "mask" not in self.cc[ver]: + print( + "Mask not found in config file, calculating area from binned catalog" + ) area[ver] = self.calculate_area_from_binned_catalog(ver) else: - mask = hp.read_map(self.cc[ver]['mask'], verbose=False) + mask = hp.read_map(self.cc[ver]["mask"], verbose=False) nside_mask = hp.get_nside(mask) print(f"nside_mask = {nside_mask}") area[ver] = np.sum(mask) * hp.nside2pixarea(nside_mask, degrees=True) @@ -751,7 +761,7 @@ def calculate_area(self): self._area = area self.print_done("Area calculation finished") - + def calculate_area_from_binned_catalog(self, ver): print(f"nside_mask = {self.nside_mask}") mask = self._get_binned_catalog_mask(ver) @@ -768,9 +778,11 @@ def calculate_n_eff_gal(self): self.print_magenta(ver) with self.results[ver].temporarily_read_data(): w = self.results[ver].dat_shear[self.cc[ver]["shear"]["w_col"]] - n_eff_gal[ver] = 1/(self.area[ver]*60*60)* np.sum(w)**2/np.sum(w**2) + n_eff_gal[ver] = ( + 1 / (self.area[ver] * 60 * 60) * np.sum(w) ** 2 / np.sum(w**2) + ) print(f"n_eff_gal = {n_eff_gal[ver]:.2f} gal./arcmin^-2") - + self._n_eff_gal = n_eff_gal self.print_done("Effective number of galaxy calculation finished") @@ -784,15 +796,17 @@ def calculate_ellipticity_dispersion(self): e2 = self.results[ver].dat_shear[self.cc[ver]["shear"]["e2_col"]] w = self.results[ver].dat_shear[self.cc[ver]["shear"]["w_col"]] ellipticity_dispersion[ver] = np.sqrt( - 0.5*(np.average(e1**2, weights=w**2) + np.average(e2**2, weights=w**2)) + 0.5 + * ( + np.average(e1**2, weights=w**2) + + np.average(e2**2, weights=w**2) + ) ) print(f"Ellipticity dispersion = {ellipticity_dispersion[ver]:.4f}") self._ellipticity_dispersion = ellipticity_dispersion def plot_rho_stats(self, abs=False): - filenames = [ - f"rho_stats_{self.basename(ver)}.fits" for ver in self.versions - ] + filenames = [f"rho_stats_{self.basename(ver)}.fits" for ver in self.versions] savefig = "rho_stats.png" self.rho_stat_handler.plot_rho_stats( @@ -812,9 +826,7 @@ def plot_rho_stats(self, abs=False): ) def plot_tau_stats(self, plot_tau_m=False): - filenames = [ - f"tau_stats_{self.basename(ver)}.fits" for ver in self.versions - ] + filenames = [f"tau_stats_{self.basename(ver)}.fits" for ver in self.versions] savefig = "tau_stats.png" self.tau_stat_handler.plot_tau_stats( @@ -1614,9 +1626,7 @@ def calculate_2pcf(self, ver, npatch=None, save_fits=False, **treecorr_config): col2 = fits.Column(name="BIN2", format="K", array=np.ones(len(lst))) col3 = fits.Column(name="ANGBIN", format="K", array=lst) col4 = fits.Column(name="VALUE", format="D", array=gg.xip) - col5 = fits.Column( - name="ANG", format="D", unit="arcmin", array=gg.rnom - ) + col5 = fits.Column(name="ANG", format="D", unit="arcmin", array=gg.rnom) coldefs = fits.ColDefs([col1, col2, col3, col4, col5]) xiplus_hdu = fits.BinTableHDU.from_columns(coldefs, name="XI_PLUS") @@ -1833,7 +1843,7 @@ def plot_ratio_xi_sys_xi(self, threshold=0.1, offset=0.02): ) theta = gg.meanr - jittered_theta = theta * (1+idx*offset) + jittered_theta = theta * (1 + idx * offset) plt.errorbar( jittered_theta, @@ -1843,13 +1853,13 @@ def plot_ratio_xi_sys_xi(self, threshold=0.1, offset=0.02): ls=self.cc[ver]["ls"], color=self.cc[ver]["colour"], fmt=self.cc[ver].get("marker", None), - capsize=5 + capsize=5, ) plt.fill_between( [self.theta_min_plot, self.theta_max_plot], - - threshold, - + threshold, + -threshold, + +threshold, color="black", alpha=0.1, label=f"{threshold:.0%} threshold", @@ -1858,12 +1868,14 @@ def plot_ratio_xi_sys_xi(self, threshold=0.1, offset=0.02): [self.theta_min_plot, self.theta_max_plot], [threshold, threshold], ls="dashed", - color="black") + color="black", + ) plt.plot( [self.theta_min_plot, self.theta_max_plot], [-threshold, -threshold], ls="dashed", - color="black") + color="black", + ) plt.xscale("log") plt.xlabel(rf"$\theta$ [{self.treecorr_config['sep_units']}]") plt.ylabel(r"$\xi^{\rm psf}_{+, {\rm sys}} / \xi_+$") @@ -1875,7 +1887,6 @@ def plot_ratio_xi_sys_xi(self, threshold=0.1, offset=0.02): cs_plots.show() print(f"Ratio of xi_psf_sys to xi plot saved to {out_path}") - def calculate_aperture_mass_dispersion( self, theta_min=0.3, theta_max=200, nbins=500, nbins_map=15, npatch=25 ): @@ -2154,7 +2165,7 @@ def calculate_pure_eb( cov_path_int=cov_path_int, cosmo_cov=cosmo_cov, n_samples=n_samples, - z_dist=z_dist + z_dist=z_dist, ) return results @@ -2177,7 +2188,7 @@ def plot_pure_eb( cosmo_cov=None, n_samples=1000, results=None, - **kwargs + **kwargs, ): """ Generate comprehensive pure E/B mode analysis plots. @@ -2235,7 +2246,7 @@ def plot_pure_eb( """ # Use instance defaults for unspecified parameters versions = versions or self.versions - output_dir = output_dir or self.cc['paths']['output'] + output_dir = output_dir or self.cc["paths"]["output"] npatch = npatch or self.npatch # Override var_method to analytic when cov_path_int is provided @@ -2281,26 +2292,26 @@ def plot_pure_eb( # Get or calculate results for this version version_results = results_list[idx] or self.calculate_pure_eb( - version, - min_sep=min_sep, - max_sep=max_sep, - nbins=nbins, - min_sep_int=min_sep_int, - max_sep_int=max_sep_int, - nbins_int=nbins_int, - npatch=npatch, - var_method=var_method, - cov_path_int=cov_path_int, - cosmo_cov=cosmo_cov, - n_samples=n_samples, - ) + version, + min_sep=min_sep, + max_sep=max_sep, + nbins=nbins, + min_sep_int=min_sep_int, + max_sep_int=max_sep_int, + nbins_int=nbins_int, + npatch=npatch, + var_method=var_method, + cov_path_int=cov_path_int, + cosmo_cov=cosmo_cov, + n_samples=n_samples, + ) # Calculate E/B statistics for all bin combinations (only if not provided) version_results = calculate_eb_statistics( version_results, cov_path_int=cov_path_int, n_samples=n_samples, - **kwargs + **kwargs, ) # Generate all plots using specialized plotting functions @@ -2308,9 +2319,7 @@ def plot_pure_eb( # Integration vs Reporting comparison plot plot_integration_vs_reporting( - gg, gg_int, - out_stub + "_integration_vs_reporting.png", - version + gg, gg_int, out_stub + "_integration_vs_reporting.png", version ) # E/B/Ambiguous correlation functions plot @@ -2319,7 +2328,7 @@ def plot_pure_eb( out_stub + "_xis.png", version, fiducial_xip_scale_cut=fiducial_xip_scale_cut, - fiducial_xim_scale_cut=fiducial_xim_scale_cut + fiducial_xim_scale_cut=fiducial_xim_scale_cut, ) # 2D PTE heatmaps plot @@ -2328,7 +2337,7 @@ def plot_pure_eb( version, out_stub + "_ptes.png", fiducial_xip_scale_cut=fiducial_xip_scale_cut, - fiducial_xim_scale_cut=fiducial_xim_scale_cut + fiducial_xim_scale_cut=fiducial_xim_scale_cut, ) # Covariance matrix plot @@ -2336,7 +2345,7 @@ def plot_pure_eb( version_results["cov"], var_method, out_stub + "_covariance.png", - version + version, ) # Save data products and store on instance @@ -2448,7 +2457,7 @@ def calculate_cosebis( generated_cuts = [ (bin_edges[start], bin_edges[stop]) for start in range(nbins) - for stop in range(start+1, nbins+1) + for stop in range(start + 1, nbins + 1) ] print(f"Evaluating {len(generated_cuts)} scale cut combinations") @@ -2471,12 +2480,18 @@ def plot_cosebis( self, version=None, output_dir=None, - min_sep_int=0.5, max_sep_int=500, nbins_int=1000, # Integration binning - npatch=None, nmodes=10, cov_path=None, - scale_cuts=None, # Explicit scale cuts - evaluate_all_scale_cuts=False, # Grid-based scale cuts - min_sep=None, max_sep=None, nbins=None, # Reporting binning - fiducial_scale_cut=None, # For plotting reference + min_sep_int=0.5, + max_sep_int=500, + nbins_int=1000, # Integration binning + npatch=None, + nmodes=10, + cov_path=None, + scale_cuts=None, # Explicit scale cuts + evaluate_all_scale_cuts=False, # Grid-based scale cuts + min_sep=None, + max_sep=None, + nbins=None, # Reporting binning + fiducial_scale_cut=None, # For plotting reference results=None, ): """ @@ -2519,8 +2534,8 @@ def plot_cosebis( # Use instance defaults if not specified version = version or self.versions[0] - output_dir = output_dir or self.cc['paths']['output'] - npatch = npatch or self.treecorr_config.get('npatch', 256) + output_dir = output_dir or self.cc["paths"]["output"] + npatch = npatch or self.treecorr_config.get("npatch", 256) # Determine variance method based on whether theoretical covariance is used var_method = "analytic" if cov_path is not None else "jackknife" @@ -2556,8 +2571,9 @@ def plot_cosebis( # Generate plots using specialized plotting functions # Extract single result for plotting if multiple scale cuts were evaluated - if (isinstance(results, dict) and - all(isinstance(k, tuple) for k in results.keys())): + if isinstance(results, dict) and all( + isinstance(k, tuple) for k in results.keys() + ): # Multiple scale cuts: use fiducial_scale_cut if provided, otherwise use # full range if fiducial_scale_cut is not None: @@ -2576,20 +2592,19 @@ def plot_cosebis( plot_results, version, out_stub + "_cosebis.png", - fiducial_scale_cut=fiducial_scale_cut + fiducial_scale_cut=fiducial_scale_cut, ) plot_cosebis_covariance_matrix( - plot_results, - version, - var_method, - out_stub + "_covariance.png" + plot_results, version, var_method, out_stub + "_covariance.png" ) # Generate scale cut heatmap if we have multiple scale cuts - if (isinstance(results, dict) and - all(isinstance(k, tuple) for k in results.keys()) and - len(results) > 1): + if ( + isinstance(results, dict) + and all(isinstance(k, tuple) for k in results.keys()) + and len(results) > 1 + ): # Create temporary gg object with correct binning for mapping treecorr_config_temp = { **self.treecorr_config, @@ -2606,14 +2621,13 @@ def plot_cosebis( gg_temp, version, out_stub + "_scalecut_ptes.png", - fiducial_scale_cut=fiducial_scale_cut + fiducial_scale_cut=fiducial_scale_cut, ) # Save data products and store on instance save_cosebis_results(results, out_stub + "_data.npz", fiducial_scale_cut) self._cosebis_results[version] = results - def get_namaster_bin(self, lmin, lmax, b_lmax): """Build NaMaster binning object. @@ -2630,11 +2644,11 @@ def get_namaster_bin(self, lmin, lmax, b_lmax): """ ells = np.arange(lmin, lmax + 1) - if self.binning == 'linear': + if self.binning == "linear": bpws = (ells - lmin) // self.ell_step bpws = np.minimum(bpws, bpws[-1]) b = nmt.NmtBin(ells=ells, bpws=bpws, lmax=b_lmax) - elif self.binning == 'logspace': + elif self.binning == "logspace": # Start geomspace at ell_min_log (>= lmin) to avoid # sub-multipole bins at low ell that destabilize the MCM. # All ell below ell_min_log go into bin 0 as padding. @@ -2643,7 +2657,7 @@ def get_namaster_bin(self, lmin, lmax, b_lmax): bpws = np.digitize(ells.astype(float), bins_ell) - 1 bpws = np.clip(bpws, 0, self.n_ell_bins - 1) b = nmt.NmtBin(ells=ells, bpws=bpws, lmax=b_lmax) - elif self.binning == 'powspace': + elif self.binning == "powspace": start = np.power(lmin, self.power) end = np.power(lmax, self.power) bins_ell = np.power( @@ -2665,15 +2679,15 @@ def get_variance_map(self, nside, e1, e2, w, unique_pix, idx_rep): """ Create a variance map from the input catalog. """ - + variance_map = np.zeros(hp.nside2npix(nside)) variance_map[unique_pix] = np.bincount( - idx_rep, weights=(e1**2 + e2**2)/2*w**2 + idx_rep, weights=(e1**2 + e2**2) / 2 * w**2 ) return variance_map - + def get_field_and_workspace_from_map(self, mask, lmax, b): """ Create a NaMaster field and workspace from the input map. @@ -2681,10 +2695,14 @@ def get_field_and_workspace_from_map(self, mask, lmax, b): nside = hp.npix2nside(len(mask)) - #Create NaMaster field - f = nmt.NmtField(mask=mask, maps=[np.zeros(hp.nside2npix(nside)), np.zeros(hp.nside2npix(nside))], lmax=lmax) + # Create NaMaster field + f = nmt.NmtField( + mask=mask, + maps=[np.zeros(hp.nside2npix(nside)), np.zeros(hp.nside2npix(nside))], + lmax=lmax, + ) - #Create NaMaster workspace + # Create NaMaster workspace wsp = nmt.NmtWorkspace.from_fields(f, f, b) return f, wsp @@ -2707,10 +2725,14 @@ def calculate_pseudo_cl_eb_cov(self): if ver not in self._pseudo_cls.keys(): self._pseudo_cls[ver] = {} - out_path = os.path.abspath(f"{self.cc['paths']['output']}/pseudo_cl_cov_{ver}.fits") + out_path = os.path.abspath( + f"{self.cc['paths']['output']}/pseudo_cl_cov_{ver}.fits" + ) if os.path.exists(out_path): - self.print_done(f"Skipping Pseudo-Cl covariance calculation, {out_path} exists") - self._pseudo_cls[ver]['cov'] = fits.open(out_path) + self.print_done( + f"Skipping Pseudo-Cl covariance calculation, {out_path} exists" + ) + self._pseudo_cls[ver]["cov"] = fits.open(out_path) else: params = get_params_rho_tau(self.cc[ver], survey=ver) @@ -2723,9 +2745,9 @@ def calculate_pseudo_cl_eb_cov(self): if pw.shape[0] != len(ell) + 1: raise ValueError( "Unexpected pixwin length for lmax=" - f"{lmax}: got {pw.shape[0]}, expected {len(ell)+1}" + f"{lmax}: got {pw.shape[0]}, expected {len(ell) + 1}" ) - pw = pw[1:len(ell)+1] + pw = pw[1 : len(ell) + 1] # Load redshift distribution and calculate theory C_ell path_redshift_distr = self.cc[ver]["shear"]["redshift_path"] @@ -2745,68 +2767,100 @@ def calculate_pseudo_cl_eb_cov(self): self.print_cyan("Getting a binning, n_gal_map, field and workspace.") lmin = 8 - lmax = 2*self.nside + lmax = 2 * self.nside b_lmax = lmax - 1 b = self.get_namaster_bin(lmin, lmax, b_lmax) - #Load data and create shear and noise maps + # Load data and create shear and noise maps cat_gal = fits.getdata(self.cc[ver]["shear"]["path"]) - n_gal, unique_pix, idx, idx_rep = self.get_n_gal_map(params, nside, cat_gal) - mask = n_gal != 0 + n_gal, unique_pix, idx, idx_rep = self.get_n_gal_map( + params, nside, cat_gal + ) f, wsp = self.get_field_and_workspace_from_map(n_gal, b_lmax, b) - if self.noise_bias_method == 'randoms': - + if self.noise_bias_method == "randoms": self.print_cyan("Getting a sample of Cls with noise bias.") - - cl_noise, f, wsp = self.get_sample(params, self.nside, b_lmax, b, cat_gal, n_gal, n_gal, unique_pix, idx_rep) + + cl_noise, f, wsp = self.get_sample( + params, + self.nside, + b_lmax, + b, + cat_gal, + n_gal, + n_gal, + unique_pix, + idx_rep, + ) cl_noise = np.mean(cl_noise, axis=0) noise_bias_cl = cl_noise - noise_bias_cl = b.unbin_cell(noise_bias_cl) #Unbin + noise_bias_cl = b.unbin_cell(noise_bias_cl) # Unbin lowest_ell = b.get_ell_list(0)[0] for i in range(4): - noise_bias_cl[i, :lowest_ell] = noise_bias_cl[i, lowest_ell] #Fill the data vector below lmin - - elif self.noise_bias_method == 'analytic': + noise_bias_cl[i, :lowest_ell] = noise_bias_cl[ + i, lowest_ell + ] # Fill the data vector below lmin + elif self.noise_bias_method == "analytic": self.print_cyan("Getting analytic noise bias.") - e1, e2, w = cat_gal[self.cc[ver]["shear"]["e1_col"]], cat_gal[self.cc[ver]["shear"]["e2_col"]], cat_gal[self.cc[ver]["shear"]["w_col"]] - variance_map = self.get_variance_map(self.nside, e1, e2, w, unique_pix, idx_rep) + e1, e2, w = ( + cat_gal[self.cc[ver]["shear"]["e1_col"]], + cat_gal[self.cc[ver]["shear"]["e2_col"]], + cat_gal[self.cc[ver]["shear"]["w_col"]], + ) + variance_map = self.get_variance_map( + self.nside, e1, e2, w, unique_pix, idx_rep + ) - noise_bias = hp.nside2pixarea(self.nside)*np.mean(variance_map) + noise_bias = hp.nside2pixarea(self.nside) * np.mean(variance_map) noise_bias_cl = np.zeros((4, lmax)) noise_bias_cl[0, :] = noise_bias noise_bias_cl[3, :] = noise_bias - noise_bias_cl = wsp.decouple_cell(noise_bias_cl) #Decouple - noise_bias_cl = b.unbin_cell(noise_bias_cl) #Unbin + noise_bias_cl = wsp.decouple_cell(noise_bias_cl) # Decouple + noise_bias_cl = b.unbin_cell(noise_bias_cl) # Unbin lowest_ell = b.get_ell_list(0)[0] for i in range(4): - noise_bias_cl[i, :lowest_ell] = noise_bias_cl[i, lowest_ell] #Fill the data vector below lmin - + noise_bias_cl[i, :lowest_ell] = noise_bias_cl[ + i, lowest_ell + ] # Fill the data vector below lmin + else: - raise ValueError(f"Noise bias method {self.noise_bias_method} not recognized. It should be 'randoms' or 'analytic'.") - + raise ValueError( + f"Noise bias method {self.noise_bias_method} not recognized. It should be 'randoms' or 'analytic'." + ) + self.print_cyan("Adding noise bias to the fiducial Cls.") - - fiducial_cl = np.array([fiducial_cl, 0.*fiducial_cl, 0.*fiducial_cl, 0.*fiducial_cl])+ noise_bias_cl - if self.fiducial_input_inka == 'coupled': + fiducial_cl = ( + np.array( + [ + fiducial_cl, + 0.0 * fiducial_cl, + 0.0 * fiducial_cl, + 0.0 * fiducial_cl, + ] + ) + + noise_bias_cl + ) + + if self.fiducial_input_inka == "coupled": self.print_cyan("Coupling the fiducial Cls.") coupling_mat = wsp.get_coupling_matrix() - coupling_mat_re = np.reshape(coupling_mat, (4, lmax, 4, lmax), order='F') - fiducial_cl = np.tensordot( - coupling_mat_re, fiducial_cl - ) / np.mean(n_gal**2) #couple and divide by the mean of the mask squared + coupling_mat_re = np.reshape( + coupling_mat, (4, lmax, 4, lmax), order="F" + ) + fiducial_cl = np.tensordot(coupling_mat_re, fiducial_cl) / np.mean( + n_gal**2 + ) # couple and divide by the mean of the mask squared - self.print_cyan("Computing the Pseudo-Cl covariance") cw = nmt.NmtCovarianceWorkspace.from_fields(f, f, f, f) @@ -2814,12 +2868,19 @@ def calculate_pseudo_cl_eb_cov(self): # Get actual number of ell bins from binning scheme n_ell_actual = b.get_n_bands() - covar_22_22 = nmt.gaussian_covariance(cw, 2, 2, 2, 2, - fiducial_cl, - fiducial_cl, - fiducial_cl, - fiducial_cl, - wsp, wb=wsp).reshape([n_ell_actual, 4, n_ell_actual, 4]) + covar_22_22 = nmt.gaussian_covariance( + cw, + 2, + 2, + 2, + 2, + fiducial_cl, + fiducial_cl, + fiducial_cl, + fiducial_cl, + wsp, + wb=wsp, + ).reshape([n_ell_actual, 4, n_ell_actual, 4]) covar_EE_EE = covar_22_22[:, 0, :, 0] covar_EE_EB = covar_22_22[:, 0, :, 1] @@ -2861,7 +2922,7 @@ def calculate_pseudo_cl_eb_cov(self): hdu.writeto(out_path, overwrite=True) - self._pseudo_cls[ver]['cov'] = hdu + self._pseudo_cls[ver]["cov"] = hdu self.print_done("Done Pseudo-Cl covariance") @@ -2873,11 +2934,15 @@ def calculate_pseudo_cl_onecovariance(self): if self.path_onecovariance is None: raise ValueError("path_onecovariance must be provided to use OneCovariance") - + if not os.path.exists(self.path_onecovariance): - raise ValueError(f"OneCovariance path {self.path_onecovariance} does not exist") - - template_config = os.path.join(self.path_onecovariance, "config_files", "config_3x2pt_pure_Cell_UNIONS.ini") + raise ValueError( + f"OneCovariance path {self.path_onecovariance} does not exist" + ) + + template_config = os.path.join( + self.path_onecovariance, "config_files", "config_3x2pt_pure_Cell_UNIONS.ini" + ) if not os.path.exists(template_config): raise ValueError(f"Template config file {template_config} does not exist") @@ -2885,44 +2950,62 @@ def calculate_pseudo_cl_onecovariance(self): for ver in self.versions: self.print_magenta(ver) - out_dir = os.path.abspath(f"{self.cc['paths']['output']}/pseudo_cl_cov_onecov_{ver}/") + out_dir = os.path.abspath( + f"{self.cc['paths']['output']}/pseudo_cl_cov_onecov_{ver}/" + ) os.makedirs(out_dir, exist_ok=True) - if os.path.exists(os.path.join(out_dir, "covariance_list_3x2pt_pure_Cell.dat")): + if os.path.exists( + os.path.join(out_dir, "covariance_list_3x2pt_pure_Cell.dat") + ): self.print_done(f"Skipping OneCovariance calculation, {out_dir} exists") self._load_onecovariance_cov(out_dir, ver) else: - - mask_path = self.cc[ver]['mask'] + mask_path = self.cc[ver]["mask"] if not os.path.exists(mask_path): print("Mask file does not exist") print("Computing the mask from the binned catalog and saving...") mask = self._get_binned_catalog_mask(ver) hp.write_map(mask_path, mask, overwrite=True) - - redshift_distr_path = os.path.join(self.cc[ver]['shear']['redshift_path']) + + redshift_distr_path = os.path.join( + self.cc[ver]["shear"]["redshift_path"] + ) config_path = os.path.join(out_dir, f"config_onecov_{ver}.ini") - self.print_cyan(f"Modifying OneCovariance config file and saving it to {config_path}") - self._modify_onecov_config(template_config, config_path, out_dir, mask_path, redshift_distr_path, ver) + self.print_cyan( + f"Modifying OneCovariance config file and saving it to {config_path}" + ) + self._modify_onecov_config( + template_config, + config_path, + out_dir, + mask_path, + redshift_distr_path, + ver, + ) self.print_cyan("Running OneCovariance...") cmd = f"python {os.path.join(self.path_onecovariance, 'covariance.py')} {config_path}" self.print_cyan(f"Command: {cmd}") ret = os.system(cmd) if ret != 0: - raise RuntimeError(f"OneCovariance command failed with return code {ret}") + raise RuntimeError( + f"OneCovariance command failed with return code {ret}" + ) self.print_cyan("OneCovariance completed successfully.") self._load_onecovariance_cov(out_dir, ver) - + self.print_done("Done Pseudo-Cl covariance with OneCovariance") - def _modify_onecov_config(self, template_config, config_path, out_dir, mask_path, redshift_distr_path, ver): + def _modify_onecov_config( + self, template_config, config_path, out_dir, mask_path, redshift_distr_path, ver + ): """ - Modify OneCovariance configuration file with correct mask, redshift distribution, + Modify OneCovariance configuration file with correct mask, redshift distribution, and ellipticity dispersion parameters. - + Parameters ---------- template_config : str @@ -2934,30 +3017,32 @@ def _modify_onecov_config(self, template_config, config_path, out_dir, mask_path redshift_distr_path : str Path to the redshift distribution file """ - config = configparser.ConfigParser() + config = configparser.ConfigParser() # Load the template configuration config.read(template_config) - + # Update mask path mask_base = os.path.basename(os.path.abspath(mask_path)) mask_folder = os.path.dirname(os.path.abspath(mask_path)) - config['survey specs']['mask_directory'] = mask_folder - config['survey specs']['mask_file_lensing'] = mask_base - config['survey specs']['survey_area_lensing_in_deg2'] = str(self.area[ver]) - config['survey specs']['ellipticity_dispersion'] = str(self.ellipticity_dispersion[ver]) - config['survey specs']['n_eff_lensing'] = str(self.n_eff_gal[ver]) + config["survey specs"]["mask_directory"] = mask_folder + config["survey specs"]["mask_file_lensing"] = mask_base + config["survey specs"]["survey_area_lensing_in_deg2"] = str(self.area[ver]) + config["survey specs"]["ellipticity_dispersion"] = str( + self.ellipticity_dispersion[ver] + ) + config["survey specs"]["n_eff_lensing"] = str(self.n_eff_gal[ver]) # Update redshift distribution path redshift_distr_base = os.path.basename(os.path.abspath(redshift_distr_path)) redshift_distr_folder = os.path.dirname(os.path.abspath(redshift_distr_path)) - config['redshift']['z_directory'] = redshift_distr_folder - config['redshift']['zlens_file'] = redshift_distr_base + config["redshift"]["z_directory"] = redshift_distr_folder + config["redshift"]["zlens_file"] = redshift_distr_base + + # Update output directory + config["output settings"]["directory"] = out_dir - #Update output directory - config['output settings']['directory'] = out_dir - # Save the modified configuration - with open(config_path, 'w') as f: + with open(config_path, "w") as f: config.write(f) def get_cov_from_onecov(self, cov_one_cov, gaussian=True): @@ -2968,42 +3053,58 @@ def get_cov_from_onecov(self, cov_one_cov, gaussian=True): for i in range(n_bins): for j in range(n_bins): cov[i, j] = cov_one_cov[i * n_bins + j, index_value] - + return cov def _load_onecovariance_cov(self, out_dir, ver): self.print_cyan(f"Loading OneCovariance results from {out_dir}") - cov_one_cov = np.genfromtxt(os.path.join(out_dir, "covariance_list_3x2pt_pure_Cell.dat")) + cov_one_cov = np.genfromtxt( + os.path.join(out_dir, "covariance_list_3x2pt_pure_Cell.dat") + ) gaussian_one_cov = self.get_cov_from_onecov(cov_one_cov, gaussian=True) all_one_cov = self.get_cov_from_onecov(cov_one_cov, gaussian=False) self._pseudo_cls_onecov[ver] = { - 'gaussian_cov': gaussian_one_cov, - 'all_cov': all_one_cov + "gaussian_cov": gaussian_one_cov, + "all_cov": all_one_cov, } def calculate_pseudo_cl_g_ng_cov(self, gaussian_part="iNKA"): - assert gaussian_part in ["iNKA", "OneCovariance"], "gaussian_part must be 'iNKA' or 'OneCovariance'" - self.print_start(f"Gaussian and Non-Gaussian covariance of the Pseudo-Cl's using {gaussian_part} for the Gaussian part") - + assert gaussian_part in ["iNKA", "OneCovariance"], ( + "gaussian_part must be 'iNKA' or 'OneCovariance'" + ) + self.print_start( + f"Gaussian and Non-Gaussian covariance of the Pseudo-Cl's using {gaussian_part} for the Gaussian part" + ) + self._pseudo_cls_cov_g_ng = {} for ver in self.versions: self.print_magenta(ver) - out_file = os.path.abspath(f"{self.cc['paths']['output']}/pseudo_cl_cov_g_ng_{gaussian_part}_{ver}.fits") + out_file = os.path.abspath( + f"{self.cc['paths']['output']}/pseudo_cl_cov_g_ng_{gaussian_part}_{ver}.fits" + ) if os.path.exists(out_file): - self.print_done(f"Skipping Gaussian and Non-Gaussian covariance calculation, {out_file} exists") + self.print_done( + f"Skipping Gaussian and Non-Gaussian covariance calculation, {out_file} exists" + ) cov_hdu = fits.open(out_file) self._pseudo_cls_cov_g_ng[ver] = cov_hdu continue if gaussian_part == "iNKA": - gaussian_cov = self.pseudo_cls[ver]['cov']['COVAR_EE_EE'].data - non_gaussian_cov = self.pseudo_cls_onecov[ver]['all_cov'] - self.pseudo_cls_onecov[ver]['gaussian_cov'] + gaussian_cov = self.pseudo_cls[ver]["cov"]["COVAR_EE_EE"].data + non_gaussian_cov = ( + self.pseudo_cls_onecov[ver]["all_cov"] + - self.pseudo_cls_onecov[ver]["gaussian_cov"] + ) full_cov = gaussian_cov + non_gaussian_cov elif gaussian_part == "OneCovariance": - gaussian_cov = self.pseudo_cls_onecov[ver]['gaussian_cov'] - non_gaussian_cov = self.pseudo_cls_onecov[ver]['all_cov'] - self.pseudo_cls_onecov[ver]['gaussian_cov'] - full_cov = self.pseudo_cls_onecov[ver]['all_cov'] + gaussian_cov = self.pseudo_cls_onecov[ver]["gaussian_cov"] + non_gaussian_cov = ( + self.pseudo_cls_onecov[ver]["all_cov"] + - self.pseudo_cls_onecov[ver]["gaussian_cov"] + ) + full_cov = self.pseudo_cls_onecov[ver]["all_cov"] else: raise ValueError(f"Unknown gaussian_part: {gaussian_part}") self.print_cyan("Saving Gaussian and Non-Gaussian covariance...") @@ -3013,7 +3114,9 @@ def calculate_pseudo_cl_g_ng_cov(self, gaussian_part="iNKA"): hdu.append(fits.ImageHDU(full_cov, name="COVAR_FULL")) hdu.writeto(out_file, overwrite=True) self._pseudo_cls_cov_g_ng[ver] = hdu - self.print_done(f"Done Gaussian and Non-Gaussian covariance of the Pseudo-Cl's using {gaussian_part} for the Gaussian part") + self.print_done( + f"Done Gaussian and Non-Gaussian covariance of the Pseudo-Cl's using {gaussian_part} for the Gaussian part" + ) def calculate_pseudo_cl(self): """ @@ -3032,14 +3135,16 @@ def calculate_pseudo_cl(self): self._pseudo_cls[ver] = {} - out_path = os.path.abspath(f"{self.cc['paths']['output']}/pseudo_cl_{ver}.fits") + out_path = os.path.abspath( + f"{self.cc['paths']['output']}/pseudo_cl_{ver}.fits" + ) if os.path.exists(out_path): self.print_done(f"Skipping Pseudo-Cl's calculation, {out_path} exists") cl_shear = fits.getdata(out_path) - self._pseudo_cls[ver]['pseudo_cl'] = cl_shear - elif self.cell_method == 'map': + self._pseudo_cls[ver]["pseudo_cl"] = cl_shear + elif self.cell_method == "map": self.calculate_pseudo_cl_map(ver, nside, out_path) - elif self.cell_method == 'catalog': + elif self.cell_method == "catalog": self.calculate_pseudo_cl_catalog(ver, out_path) else: raise ValueError(f"Unknown cell method: {self.cell_method}") @@ -3049,10 +3154,10 @@ def calculate_pseudo_cl(self): def calculate_pseudo_cl_map(self, ver, nside, out_path): params = get_params_rho_tau(self.cc[ver], survey=ver) - #Load data and create shear and noise maps + # Load data and create shear and noise maps cat_gal = fits.getdata(self.cc[ver]["shear"]["path"]) - w = cat_gal[params['w_col']] + w = cat_gal[params["w_col"]] self.print_cyan("Creating maps and computing Cl's...") n_gal_map, unique_pix, idx, idx_rep = self.get_n_gal_map(params, nside, cat_gal) mask = n_gal_map != 0 @@ -3060,89 +3165,91 @@ def calculate_pseudo_cl_map(self, ver, nside, out_path): shear_map_e1 = np.zeros(hp.nside2npix(nside)) shear_map_e2 = np.zeros(hp.nside2npix(nside)) - e1 = cat_gal[params['e1_col']] - e2 = cat_gal[params['e2_col']] + e1 = cat_gal[params["e1_col"]] + e2 = cat_gal[params["e2_col"]] del cat_gal - - shear_map_e1[unique_pix] += np.bincount(idx_rep, weights=e1*w) - shear_map_e2[unique_pix] += np.bincount(idx_rep, weights=e2*w) + + shear_map_e1[unique_pix] += np.bincount(idx_rep, weights=e1 * w) + shear_map_e2[unique_pix] += np.bincount(idx_rep, weights=e2 * w) shear_map_e1[mask] /= n_gal_map[mask] shear_map_e2[mask] /= n_gal_map[mask] - shear_map = shear_map_e1 + 1j*shear_map_e2 + shear_map = shear_map_e1 + 1j * shear_map_e2 del shear_map_e1, shear_map_e2 ell_eff, cl_shear, wsp = self.get_pseudo_cls_map(shear_map, n_gal_map) cl_noise = np.zeros_like(cl_shear) - - for i in range(self.nrandom_cell): + for i in range(self.nrandom_cell): noise_map_e1 = np.zeros(hp.nside2npix(nside)) noise_map_e2 = np.zeros(hp.nside2npix(nside)) e1_rot, e2_rot = self.apply_random_rotation(e1, e2) - - noise_map_e1[unique_pix] += np.bincount(idx_rep, weights=e1_rot*w) - noise_map_e2[unique_pix] += np.bincount(idx_rep, weights=e2_rot*w) + noise_map_e1[unique_pix] += np.bincount(idx_rep, weights=e1_rot * w) + noise_map_e2[unique_pix] += np.bincount(idx_rep, weights=e2_rot * w) noise_map_e1[mask] /= n_gal_map[mask] noise_map_e2[mask] /= n_gal_map[mask] - noise_map = noise_map_e1 + 1j*noise_map_e2 + noise_map = noise_map_e1 + 1j * noise_map_e2 del noise_map_e1, noise_map_e2 _, cl_noise_, _ = self.get_pseudo_cls_map(noise_map, n_gal_map, wsp) cl_noise += cl_noise_ - + cl_noise /= self.nrandom_cell del e1, e2, w try: del e1_rot, e2_rot - except NameError: #Continue if the random generation has been skipped. + except NameError: # Continue if the random generation has been skipped. pass - del n_gal_map + del n_gal_map - #This is a problem because the measurement depends on the seed. To be fixed. - #cl_shear = cl_shear - np.mean(cl_noise, axis=1, keepdims=True) + # This is a problem because the measurement depends on the seed. To be fixed. + # cl_shear = cl_shear - np.mean(cl_noise, axis=1, keepdims=True) cl_shear = cl_shear - cl_noise self.print_cyan("Saving pseudo-Cl's...") self.save_pseudo_cl(ell_eff, cl_shear, out_path) cl_shear = fits.getdata(out_path) - self._pseudo_cls[ver]['pseudo_cl'] = cl_shear + self._pseudo_cls[ver]["pseudo_cl"] = cl_shear def calculate_pseudo_cl_catalog(self, ver, out_path): params = get_params_rho_tau(self.cc[ver], survey=ver) - #Load data and create shear and noise maps + # Load data and create shear and noise maps cat_gal = fits.getdata(self.cc[ver]["shear"]["path"]) - ell_eff, cl_shear, wsp = self.get_pseudo_cls_catalog(catalog=cat_gal, params=params) + ell_eff, cl_shear, wsp = self.get_pseudo_cls_catalog( + catalog=cat_gal, params=params + ) self.print_cyan("Saving pseudo-Cl's...") self.save_pseudo_cl(ell_eff, cl_shear, out_path) cl_shear = fits.getdata(out_path) - self._pseudo_cls[ver]['pseudo_cl'] = cl_shear + self._pseudo_cls[ver]["pseudo_cl"] = cl_shear def get_n_gal_map(self, params, nside, cat_gal): """ Compute the galaxy number density map. """ - ra = cat_gal[params['ra_col']] - dec = cat_gal[params['dec_col']] - w = cat_gal[params['w_col']] + ra = cat_gal[params["ra_col"]] + dec = cat_gal[params["dec_col"]] + w = cat_gal[params["w_col"]] - theta = (90. - dec) * np.pi / 180. - phi = ra * np.pi / 180. + theta = (90.0 - dec) * np.pi / 180.0 + phi = ra * np.pi / 180.0 pix = hp.ang2pix(nside, theta, phi) - unique_pix, idx, idx_rep = np.unique(pix, return_index=True, return_inverse=True) + unique_pix, idx, idx_rep = np.unique( + pix, return_index=True, return_inverse=True + ) n_gal = np.zeros(hp.nside2npix(nside)) n_gal[unique_pix] = np.bincount(idx_rep, weights=w) return n_gal, unique_pix, idx, idx_rep @@ -3156,16 +3263,20 @@ def get_gaussian_real( noise_map_e1 = np.zeros(hp.nside2npix(nside)) noise_map_e2 = np.zeros(hp.nside2npix(nside)) - w = cat_gal[params['w_col']] - noise_map_e1[unique_pix] += np.bincount(idx_rep, weights=e1_rot*w) - noise_map_e2[unique_pix] += np.bincount(idx_rep, weights=e2_rot*w) + w = cat_gal[params["w_col"]] + noise_map_e1[unique_pix] += np.bincount(idx_rep, weights=e1_rot * w) + noise_map_e2[unique_pix] += np.bincount(idx_rep, weights=e2_rot * w) noise_map_e1[mask] /= n_gal[mask] noise_map_e2[mask] /= n_gal[mask] - return noise_map_e1 + 1j*noise_map_e2 + return noise_map_e1 + 1j * noise_map_e2 - def get_sample(self, params, nside, lmax, b, cat_gal, n_gal, mask, unique_pix, idx_rep): - noise_map = self.get_gaussian_real(params, nside, lmax, cat_gal, n_gal, mask, unique_pix, idx_rep) + def get_sample( + self, params, nside, lmax, b, cat_gal, n_gal, mask, unique_pix, idx_rep + ): + noise_map = self.get_gaussian_real( + params, nside, lmax, cat_gal, n_gal, mask, unique_pix, idx_rep + ) f = nmt.NmtField(mask=mask, maps=[noise_map.real, noise_map.imag], lmax=lmax) @@ -3175,14 +3286,14 @@ def get_sample(self, params, nside, lmax, b, cat_gal, n_gal, mask, unique_pix, i cl_noise = wsp.decouple_cell(cl_noise) return cl_noise, f, wsp - + def get_pseudo_cls_map(self, map, mask, wsp=None): """ Compute the pseudo-cl for a given map. """ lmin = 8 - lmax = 2*self.nside + lmax = 2 * self.nside b_lmax = lmax - 1 b = self.get_namaster_bin(lmin, lmax, b_lmax) @@ -3191,10 +3302,10 @@ def get_pseudo_cls_map(self, map, mask, wsp=None): factor = -1 if self.pol_factor else 1 - f_all = nmt.NmtField(mask=mask, maps=[map.real, factor*map.imag], lmax=b_lmax) + f_all = nmt.NmtField(mask=mask, maps=[map.real, factor * map.imag], lmax=b_lmax) if wsp is None: wsp = nmt.NmtWorkspace.from_fields(f_all, f_all, b) - + cl_coupled = nmt.compute_coupled_cell(f_all, f_all) cl_all = wsp.decouple_cell(cl_coupled) @@ -3206,7 +3317,7 @@ def get_pseudo_cls_catalog(self, catalog, params, wsp=None): """ lmin = 8 - lmax = 2*self.nside + lmax = 2 * self.nside b_lmax = lmax - 1 b = self.get_namaster_bin(lmin, lmax, b_lmax) @@ -3215,17 +3326,19 @@ def get_pseudo_cls_catalog(self, catalog, params, wsp=None): factor = -1 if self.pol_factor else 1 - f_all = nmt.NmtFieldCatalog(positions=[catalog[params['ra_col']], catalog[params['dec_col']]], - weights=catalog[params['w_col']], - field=[catalog[params['e1_col']], factor*catalog[params['e2_col']]], - lmax=b_lmax, - lmax_mask=b_lmax, - spin=2, - lonlat=True) - + f_all = nmt.NmtFieldCatalog( + positions=[catalog[params["ra_col"]], catalog[params["dec_col"]]], + weights=catalog[params["w_col"]], + field=[catalog[params["e1_col"]], factor * catalog[params["e2_col"]]], + lmax=b_lmax, + lmax_mask=b_lmax, + spin=2, + lonlat=True, + ) + if wsp is None: wsp = nmt.NmtWorkspace.from_fields(f_all, f_all, b) - + cl_coupled = nmt.compute_coupled_cell(f_all, f_all) cl_all = wsp.decouple_cell(cl_coupled) @@ -3250,9 +3363,9 @@ def apply_random_rotation(self, e1, e2): Second component of the rotated ellipticity. """ np.random.seed() - rot_angle = np.random.rand(len(e1))*2*np.pi - e1_out = e1*np.cos(rot_angle) + e2*np.sin(rot_angle) - e2_out = -e1*np.sin(rot_angle) + e2*np.cos(rot_angle) + rot_angle = np.random.rand(len(e1)) * 2 * np.pi + e1_out = e1 * np.cos(rot_angle) + e2 * np.sin(rot_angle) + e2_out = -e1 * np.sin(rot_angle) + e2 * np.cos(rot_angle) return e1_out, e2_out def save_pseudo_cl(self, ell_eff, pseudo_cl, out_path): @@ -3266,7 +3379,7 @@ def save_pseudo_cl(self, ell_eff, pseudo_cl, out_path): out_path : str Path to save the pseudo-Cl's to. """ - #Create columns of the fits file + # Create columns of the fits file col1 = fits.Column(name="ELL", format="D", array=ell_eff) col2 = fits.Column(name="EE", format="D", array=pseudo_cl[0]) col3 = fits.Column(name="EB", format="D", array=pseudo_cl[1]) @@ -3276,149 +3389,188 @@ def save_pseudo_cl(self, ell_eff, pseudo_cl, out_path): cell_hdu.writeto(out_path, overwrite=True) - @property - def pseudo_cls(self): - if not hasattr(self, "_pseudo_cls"): - self.calculate_pseudo_cl() - self.calculate_pseudo_cl_eb_cov() - return self._pseudo_cls - def plot_pseudo_cl(self): """ Plot pseudo-Cl's for given catalogs. """ self.print_cyan("Plotting pseudo-Cl's") - #Plotting EE + # Plotting EE out_path = os.path.abspath(f"{self.cc['paths']['output']}/cell_ee.png") fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(8, 8)) for ver in self.versions: - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] - cov = self.pseudo_cls[ver]['cov']["COVAR_EE_EE"].data - ax[0].errorbar(ell, ell*self.pseudo_cls[ver]['pseudo_cl']["EE"], yerr=ell*np.sqrt(np.diag(cov)), fmt=self.cc[ver]["marker"], label=ver+" EE", color=self.cc[ver]["colour"], capsize=2) + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] + cov = self.pseudo_cls[ver]["cov"]["COVAR_EE_EE"].data + ax[0].errorbar( + ell, + ell * self.pseudo_cls[ver]["pseudo_cl"]["EE"], + yerr=ell * np.sqrt(np.diag(cov)), + fmt=self.cc[ver]["marker"], + label=ver + " EE", + color=self.cc[ver]["colour"], + capsize=2, + ) ax[0].set_ylabel(r"$\ell C_\ell$") - ax[0].set_xlim(ell.min()-10, ell.max()+100) - ax[0].set_xscale('squareroot') + ax[0].set_xlim(ell.min() - 10, ell.max() + 100) + ax[0].set_xscale("squareroot") ax[0].set_xticks(np.array([100, 400, 900, 1600])) ax[0].minorticks_on() - ax[0].tick_params(axis='x', which='minor', length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + ax[0].tick_params(axis="x", which="minor", length=2, width=0.8) + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax[0].xaxis.set_ticks(minor_ticks, minor=True) for ver in self.versions: - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] - cov = self.pseudo_cls[ver]['cov']["COVAR_EE_EE"].data - ax[1].errorbar(ell, self.pseudo_cls[ver]['pseudo_cl']["EE"], yerr=np.sqrt(np.diag(cov)), fmt=self.cc[ver]["marker"], label=ver+" EE", color=self.cc[ver]["colour"]) + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] + cov = self.pseudo_cls[ver]["cov"]["COVAR_EE_EE"].data + ax[1].errorbar( + ell, + self.pseudo_cls[ver]["pseudo_cl"]["EE"], + yerr=np.sqrt(np.diag(cov)), + fmt=self.cc[ver]["marker"], + label=ver + " EE", + color=self.cc[ver]["colour"], + ) ax[1].set_xlabel(r"$\ell$") ax[1].set_ylabel(r"$C_\ell$") - ax[1].set_xlim(ell.min()-10, ell.max()+100) - ax[1].set_xscale('squareroot') - ax[1].set_yscale('log') + ax[1].set_xlim(ell.min() - 10, ell.max() + 100) + ax[1].set_xscale("squareroot") + ax[1].set_yscale("log") ax[1].set_xticks(np.array([100, 400, 900, 1600])) ax[1].minorticks_on() - ax[1].tick_params(axis='x', which='minor', length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + ax[1].tick_params(axis="x", which="minor", length=2, width=0.8) + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax[1].xaxis.set_ticks(minor_ticks, minor=True) - plt.suptitle('Pseudo-Cl EE (Gaussian covariance)') + plt.suptitle("Pseudo-Cl EE (Gaussian covariance)") plt.legend() plt.savefig(out_path) - #Plotting EB + # Plotting EB out_path = os.path.abspath(f"{self.cc['paths']['output']}/cell_eb.png") fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(8, 8)) for ver in self.versions: - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] - cov = self.pseudo_cls[ver]['cov']["COVAR_EB_EB"].data - ax[0].errorbar(ell, ell*self.pseudo_cls[ver]['pseudo_cl']["EB"], yerr=ell*np.sqrt(np.diag(cov)), fmt=self.cc[ver]["marker"], label=ver+" EB", color=self.cc[ver]["colour"], capsize=2) + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] + cov = self.pseudo_cls[ver]["cov"]["COVAR_EB_EB"].data + ax[0].errorbar( + ell, + ell * self.pseudo_cls[ver]["pseudo_cl"]["EB"], + yerr=ell * np.sqrt(np.diag(cov)), + fmt=self.cc[ver]["marker"], + label=ver + " EB", + color=self.cc[ver]["colour"], + capsize=2, + ) ax[0].axhline(0, color="black", linestyle="--") ax[0].set_ylabel(r"$\ell C_\ell$") - ax[0].set_xlim(ell.min()-10, ell.max()+100) - ax[0].set_xscale('squareroot') + ax[0].set_xlim(ell.min() - 10, ell.max() + 100) + ax[0].set_xscale("squareroot") ax[0].set_xticks(np.array([100, 400, 900, 1600])) ax[0].minorticks_on() - ax[0].tick_params(axis='x', which='minor', length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + ax[0].tick_params(axis="x", which="minor", length=2, width=0.8) + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax[0].xaxis.set_ticks(minor_ticks, minor=True) for ver in self.versions: - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] - cov = self.pseudo_cls[ver]['cov']["COVAR_EB_EB"].data - ax[1].errorbar(ell, self.pseudo_cls[ver]['pseudo_cl']["EB"], yerr=np.sqrt(np.diag(cov)), fmt=self.cc[ver]["marker"], label=ver+" EB", color=self.cc[ver]["colour"]) + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] + cov = self.pseudo_cls[ver]["cov"]["COVAR_EB_EB"].data + ax[1].errorbar( + ell, + self.pseudo_cls[ver]["pseudo_cl"]["EB"], + yerr=np.sqrt(np.diag(cov)), + fmt=self.cc[ver]["marker"], + label=ver + " EB", + color=self.cc[ver]["colour"], + ) ax[1].set_xlabel(r"$\ell$") ax[1].set_ylabel(r"$C_\ell$") - ax[1].set_xlim(ell.min()-10, ell.max()+100) - ax[1].set_xscale('squareroot') - ax[1].set_yscale('log') + ax[1].set_xlim(ell.min() - 10, ell.max() + 100) + ax[1].set_xscale("squareroot") + ax[1].set_yscale("log") ax[1].set_xticks(np.array([100, 400, 900, 1600])) ax[1].minorticks_on() - ax[1].tick_params(axis='x', which='minor', length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + ax[1].tick_params(axis="x", which="minor", length=2, width=0.8) + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax[1].xaxis.set_ticks(minor_ticks, minor=True) - plt.suptitle('Pseudo-Cl EB (Gaussian covariance)') + plt.suptitle("Pseudo-Cl EB (Gaussian covariance)") plt.legend() plt.savefig(out_path) - #Plotting BB + # Plotting BB out_path = os.path.abspath(f"{self.cc['paths']['output']}/cell_bb.png") fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(8, 8)) for ver in self.versions: - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] - cov = self.pseudo_cls[ver]['cov']["COVAR_BB_BB"].data - ax[0].errorbar(ell, ell*self.pseudo_cls[ver]['pseudo_cl']["BB"], yerr=ell*np.sqrt(np.diag(cov)), fmt=self.cc[ver]["marker"], label=ver+" BB", color=self.cc[ver]["colour"], capsize=2) + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] + cov = self.pseudo_cls[ver]["cov"]["COVAR_BB_BB"].data + ax[0].errorbar( + ell, + ell * self.pseudo_cls[ver]["pseudo_cl"]["BB"], + yerr=ell * np.sqrt(np.diag(cov)), + fmt=self.cc[ver]["marker"], + label=ver + " BB", + color=self.cc[ver]["colour"], + capsize=2, + ) ax[0].axhline(0, color="black", linestyle="--") ax[0].set_ylabel(r"$\ell C_\ell$") - ax[0].set_xlim(ell.min()-10, ell.max()+100) - ax[0].set_xscale('squareroot') + ax[0].set_xlim(ell.min() - 10, ell.max() + 100) + ax[0].set_xscale("squareroot") ax[0].set_xticks(np.array([100, 400, 900, 1600])) ax[0].minorticks_on() - ax[0].tick_params(axis='x', which='minor', length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + ax[0].tick_params(axis="x", which="minor", length=2, width=0.8) + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax[0].xaxis.set_ticks(minor_ticks, minor=True) for ver in self.versions: - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] - cov = self.pseudo_cls[ver]['cov']["COVAR_BB_BB"].data - ax[1].errorbar(ell, self.pseudo_cls[ver]['pseudo_cl']["BB"], yerr=np.sqrt(np.diag(cov)), fmt=self.cc[ver]["marker"], label=ver+" BB", color=self.cc[ver]["colour"]) + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] + cov = self.pseudo_cls[ver]["cov"]["COVAR_BB_BB"].data + ax[1].errorbar( + ell, + self.pseudo_cls[ver]["pseudo_cl"]["BB"], + yerr=np.sqrt(np.diag(cov)), + fmt=self.cc[ver]["marker"], + label=ver + " BB", + color=self.cc[ver]["colour"], + ) ax[1].set_xlabel(r"$\ell$") ax[1].set_ylabel(r"$C_\ell$") - ax[1].set_xlim(ell.min()-10, ell.max()+100) - ax[1].set_xscale('squareroot') - ax[1].set_yscale('log') + ax[1].set_xlim(ell.min() - 10, ell.max() + 100) + ax[1].set_xscale("squareroot") + ax[1].set_yscale("log") ax[1].set_xticks(np.array([100, 400, 900, 1600])) ax[1].minorticks_on() - ax[1].tick_params(axis='x', which='minor', length=2, width=0.8) - minor_ticks = [i*10 for i in range(1, 10)] + [i*100 for i in range(1, 21)] + ax[1].tick_params(axis="x", which="minor", length=2, width=0.8) + minor_ticks = [i * 10 for i in range(1, 10)] + [i * 100 for i in range(1, 21)] ax[1].xaxis.set_ticks(minor_ticks, minor=True) - plt.suptitle('Pseudo-Cl BB (Gaussian covariance)') + plt.suptitle("Pseudo-Cl BB (Gaussian covariance)") plt.legend() plt.savefig(out_path) # Print C_l^BB PTE for each version and save BB data from scipy import stats as sp_stats + print("\nC_l^BB PTE summary:") for ver in self.versions: - cl_bb = self.pseudo_cls[ver]['pseudo_cl']["BB"] - cov_bb = self.pseudo_cls[ver]['cov']["COVAR_BB_BB"].data + cl_bb = self.pseudo_cls[ver]["pseudo_cl"]["BB"] + cov_bb = self.pseudo_cls[ver]["cov"]["COVAR_BB_BB"].data chi2_bb = float(cl_bb @ np.linalg.solve(cov_bb, cl_bb)) pte_bb = sp_stats.chi2.sf(chi2_bb, len(cl_bb)) print( @@ -3427,14 +3579,17 @@ def plot_pseudo_cl(self): ) # Save BB data + covariance to .npz - ell = self.pseudo_cls[ver]['pseudo_cl']["ELL"] + ell = self.pseudo_cls[ver]["pseudo_cl"]["ELL"] bb_out = os.path.abspath( f"{self.cc['paths']['output']}/{ver}_cell_bb_data.npz" ) np.savez( bb_out, - ell=ell, cl_bb=cl_bb, cov_bb=cov_bb, - chi2_bb=np.array(chi2_bb), pte_bb=np.array(pte_bb), + ell=ell, + cl_bb=cl_bb, + cov_bb=cov_bb, + chi2_bb=np.array(chi2_bb), + pte_bb=np.array(pte_bb), ) print(f" Saved BB data to {bb_out}") @@ -3484,9 +3639,7 @@ def summarize_bmodes(self, fiducial_scale_cut=(12, 83), versions=None): # COSEBIs PTE from stored results if ver in self._cosebis_results: cosebis_res = self._cosebis_results[ver] - has_multi_scale_cuts = all( - isinstance(k, tuple) for k in cosebis_res - ) + has_multi_scale_cuts = all(isinstance(k, tuple) for k in cosebis_res) if has_multi_scale_cuts: key = find_conservative_scale_cut_key( cosebis_res, fiducial_scale_cut @@ -3499,8 +3652,8 @@ def summarize_bmodes(self, fiducial_scale_cut=(12, 83), versions=None): # triggering computation) if hasattr(self, "_pseudo_cls") and ver in self._pseudo_cls: try: - cl_bb = self.pseudo_cls[ver]['pseudo_cl']["BB"] - cov_bb = self.pseudo_cls[ver]['cov']["COVAR_BB_BB"].data + cl_bb = self.pseudo_cls[ver]["pseudo_cl"]["BB"] + cov_bb = self.pseudo_cls[ver]["cov"]["COVAR_BB_BB"].data chi2_bb = float(cl_bb @ np.linalg.solve(cov_bb, cl_bb)) row["C_l_BB"] = sp_stats.chi2.sf(chi2_bb, len(cl_bb)) cov_methods.add("Gaussian (NaMaster)") @@ -3533,8 +3686,7 @@ def summarize_bmodes(self, fiducial_scale_cut=(12, 83), versions=None): for ver in versions: row = summary[ver] cells = "".join( - f"{row[s]:>10.4f}" if s in row else f"{'--':>10s}" - for s in stats_order + f"{row[s]:>10.4f}" if s in row else f"{'--':>10s}" for s in stats_order ) print(f"{ver:<28s}{cells}") diff --git a/src/sp_validation/cosmology.py b/src/sp_validation/cosmology.py index b7614280..2964aa08 100644 --- a/src/sp_validation/cosmology.py +++ b/src/sp_validation/cosmology.py @@ -10,14 +10,11 @@ """ +import camb import numpy as np import pyccl as ccl -import camb - -from astropy import cosmology from astropy.cosmology import Planck18 - # ============================================================================= # Fiducial Cosmology: astropy Planck18 # ============================================================================= @@ -31,13 +28,13 @@ # use A_s=2.1e-9 for CAMB-based predictions. # ============================================================================= PLANCK18 = { - "Omega_m": Planck18.Om0, # 0.30966 - "Omega_b": Planck18.Ob0, # 0.04897 - "h": Planck18.h, # 0.6766 - "n_s": Planck18.meta["n"], # 0.9665 - "sigma_8": Planck18.meta["sigma8"], # 0.8102 - "A_s": 2.1e-9, # ln(10^10 A_s) = 3.047 - "m_nu": 0.06, # eV, sum of neutrino masses + "Omega_m": Planck18.Om0, # 0.30966 + "Omega_b": Planck18.Ob0, # 0.04897 + "h": Planck18.h, # 0.6766 + "n_s": Planck18.meta["n"], # 0.9665 + "sigma_8": Planck18.meta["sigma8"], # 0.8102 + "A_s": 2.1e-9, # ln(10^10 A_s) = 3.047 + "m_nu": 0.06, # eV, sum of neutrino masses "w0": -1.0, "wa": 0.0, } @@ -297,7 +294,7 @@ def get_cosmo( "w0": ccl_params.get("w0", w0 or planck_defaults["w0"]), "wa": ccl_params.get("wa", wa or planck_defaults["wa"]), "m_nu": mnu, - "extra_parameters": extra_params + "extra_parameters": extra_params, } return ccl.Cosmology( diff --git a/src/sp_validation/format.py b/src/sp_validation/format.py index a7b4cee3..04a5571c 100644 --- a/src/sp_validation/format.py +++ b/src/sp_validation/format.py @@ -6,12 +6,7 @@ """ - -import sys -import os - import math -import numpy as np def millify(n): @@ -30,18 +25,17 @@ def millify(n): output name """ - millnames = ['', ' Thousand', ' Million', ' Billion', ' Trillion'] + millnames = ["", " Thousand", " Million", " Billion", " Trillion"] n = float(n) millidx = max( 0, min( - len(millnames) - 1, - int(math.floor(0 if n == 0 else math.log10(abs(n)) / 3)) - ) + len(millnames) - 1, int(math.floor(0 if n == 0 else math.log10(abs(n)) / 3)) + ), ) - return f'{n / 10**(3 * millidx):.0f}{millnames[millidx]}' + return f"{n / 10 ** (3 * millidx):.0f}{millnames[millidx]}" def print_millified(msg, n): diff --git a/src/sp_validation/galaxy.py b/src/sp_validation/galaxy.py index 8f5e79e2..616f0ad5 100644 --- a/src/sp_validation/galaxy.py +++ b/src/sp_validation/galaxy.py @@ -10,33 +10,26 @@ """ - import re -import numpy as np - -from joblib import Parallel, delayed -from tqdm import tqdm +import numpy as np import regions -from astropy import units from astropy import coordinates as coords -from astropy.wcs import WCS +from astropy import units from astropy.nddata import bitmask - +from astropy.wcs import WCS from cs_util import cfis from cs_util.size import T_to_fwhm, sigma_to_fwhm # noqa: F401 re-exported; +from joblib import Parallel, delayed +from tqdm import tqdm + # the previous local T_to_fwhm (T / 1.17741 * 2.355) treated the area # T = 2 sigma^2 as if it were sigma; the cs_util version carries the # required square root: FWHM = 2.35482 sqrt(T / 2) - from sp_validation import io -def classification_galaxy_overlap_ra_dec( - dd, - ra_key='XWIN_WORLD', - dec_key='YWIN_WORLD' -): +def classification_galaxy_overlap_ra_dec(dd, ra_key="XWIN_WORLD", dec_key="YWIN_WORLD"): """Classification Galaxy Overlap Ra Dec. Return mask corresponding to non-overlapping tile areas using @@ -59,12 +52,12 @@ def classification_galaxy_overlap_ra_dec( """ # Unique set of tile IDs in data - tile_ID_list = set(dd['TILE_ID']) + tile_ID_list = set(dd["TILE_ID"]) # Transform to string format tile_ID_str_list = [] for ID in tile_ID_list: - tile_ID_str_list.append(f'{ID:07.3f}') + tile_ID_str_list.append(f"{ID:07.3f}") # Extract integer numbers from tile IDs nix = [] @@ -78,7 +71,7 @@ def classification_galaxy_overlap_ra_dec( ra_cen, dec_cen = cfis.get_tile_coord_from_nixy(nix, niy) # Create limits on Dec by adding/subtracting half of the tile size - delta_dec = cfis.Cfis().size['tile'] / 2 + delta_dec = cfis.Cfis().size["tile"] / 2 dec_upper = dec_cen + delta_dec dec_lower = dec_cen - delta_dec @@ -89,14 +82,12 @@ def classification_galaxy_overlap_ra_dec( # Loop over tiles and mask objects outside the Dec limits for idx, tile_ID in enumerate(tile_ID_list): - # Get indices of galaxies on this tile ID - idx_ID = (dd['TILE_ID'] == tile_ID) + idx_ID = dd["TILE_ID"] == tile_ID # Set mask for this tile ID - mask_dec_ID = ( - (dd[idx_ID][dec_key] < dec_upper[idx].value) - & (dd[idx_ID][dec_key] >= dec_lower[idx].value) + mask_dec_ID = (dd[idx_ID][dec_key] < dec_upper[idx].value) & ( + dd[idx_ID][dec_key] >= dec_lower[idx].value ) # Apply to global mask @@ -107,9 +98,8 @@ def classification_galaxy_overlap_ra_dec( ra_lower_list = [] for idx, tile_ID in enumerate(tile_ID_str_list): - # Find tile ID towards increasing RA - ID_upper = f'{int(nix[idx]) + 1:03d}.{int(niy[idx]):03d}' + ID_upper = f"{int(nix[idx]) + 1:03d}.{int(niy[idx]):03d}" if ID_upper in tile_ID_str_list: # If found: compute halfway RA idx_upper = tile_ID_str_list.index(ID_upper) @@ -122,7 +112,7 @@ def classification_galaxy_overlap_ra_dec( ra_upper_list.append(ra_upper) # Repeat towards decreasing RA - ID_lower = f'{int(nix[idx]) - 1:03d}.{int(niy[idx]):03d}' + ID_lower = f"{int(nix[idx]) - 1:03d}.{int(niy[idx]):03d}" if ID_lower in tile_ID_str_list: idx_lower = tile_ID_str_list.index(ID_lower) ra_lower = (ra_cen[idx] + ra_cen[idx_lower]) / 2 @@ -135,10 +125,9 @@ def classification_galaxy_overlap_ra_dec( mask_ra = np.full(len(dd), True) for idx, tile_ID in enumerate(tile_ID_list): - idx_ID = (dd['TILE_ID'] == tile_ID) - mask_ra_ID = ( - (dd[idx_ID][ra_key] < ra_upper_list[idx].value) - & (dd[idx_ID][ra_key] >= ra_lower_list[idx].value) + idx_ID = dd["TILE_ID"] == tile_ID + mask_ra_ID = (dd[idx_ID][ra_key] < ra_upper_list[idx].value) & ( + dd[idx_ID][ra_key] >= ra_lower_list[idx].value ) mask_ra[idx_ID] = mask_ra_ID @@ -152,7 +141,7 @@ def classification_galaxy_base( gal_mag_faint=26, flags_keep=None, n_epoch_min=1, - do_spread_model=True + do_spread_model=True, ): """Classification Galaxy Base. @@ -161,14 +150,10 @@ def classification_galaxy_base( """ if do_spread_model: # spread model class, add two times the uncertainty to be conservative - sm_classif = dd['SPREAD_MODEL'] + 2 * dd['SPREADERR_MODEL'] + sm_classif = dd["SPREAD_MODEL"] + 2 * dd["SPREADERR_MODEL"] cut_sm = sm_classif > 0.0035 - cut_sm_all = ( - cut_sm - & (dd['SPREAD_MODEL'] > 0) - & (dd['SPREAD_MODEL'] < 0.03) - ) + cut_sm_all = cut_sm & (dd["SPREAD_MODEL"] > 0) & (dd["SPREAD_MODEL"] < 0.03) else: # Do not use spread model cut_sm_all = True @@ -176,20 +161,19 @@ def classification_galaxy_base( # SExtractor flags # Keep some flags if specified if flags_keep: - # Check whether flags are powers of 2 - if not all([bin(flag).count('1') == 1 for flag in flags_keep]): + if not all([bin(flag).count("1") == 1 for flag in flags_keep]): raise ValueError('Flag values in "flags_keep" not powers of 2') cut_flags = bitmask.bitfield_to_boolean_mask( - dd['FLAGS'], + dd["FLAGS"], good_mask_value=True, ignore_flags=flags_keep, dtype=bool, ) else: cut_flags = bitmask.bitfield_to_boolean_mask( - dd['FLAGS'], + dd["FLAGS"], good_mask_value=True, dtype=bool, ) @@ -198,10 +182,10 @@ def classification_galaxy_base( cut_overlap & cut_flags & cut_sm_all - & (dd['MAG_AUTO'] <= gal_mag_faint) - & (dd['MAG_AUTO'] >= gal_mag_bright) - & (dd['IMAFLAGS_ISO'] == 0) - & (dd['N_EPOCH'] >= n_epoch_min) + & (dd["MAG_AUTO"] <= gal_mag_faint) + & (dd["MAG_AUTO"] >= gal_mag_bright) + & (dd["IMAFLAGS_ISO"] == 0) + & (dd["N_EPOCH"] >= n_epoch_min) ) return cut_common @@ -219,9 +203,9 @@ def classification_galaxy_ngmix( """ m_gal_ngmix = ( cut_common - & (dd['NGMIX_MCAL_FLAGS'] == 0) - & (dd['NGMIX_ELL_PSFo_NOSHEAR'][:, 0] != -10) - & (dd['NGMIX_MOM_FAIL'] == 0) + & (dd["NGMIX_MCAL_FLAGS"] == 0) + & (dd["NGMIX_ELL_PSFo_NOSHEAR"][:, 0] != -10) + & (dd["NGMIX_MOM_FAIL"] == 0) ) n_gal_ngmix = len(np.where(m_gal_ngmix)[0]) @@ -229,11 +213,12 @@ def classification_galaxy_ngmix( if stats_file: io.print_ratio( - 'ngmix: Objects selected as galaxies', + "ngmix: Objects selected as galaxies", n_gal_ngmix, n_tot, stats_file, - verbose=verbose) + verbose=verbose, + ) return m_gal_ngmix @@ -244,20 +229,18 @@ def classification_galaxy_galsim(dd, cut_common, stats_file, verbose=False): Return mask corresponding to galsim classification of galaxies """ - m_gal_galsim = ( - cut_common - & (dd['GALSIM_PSF_ELL_ORIGINAL_PSF'][:, 0] != -10) - ) + m_gal_galsim = cut_common & (dd["GALSIM_PSF_ELL_ORIGINAL_PSF"][:, 0] != -10) n_gal_galsim = len(np.where(m_gal_galsim)[0]) n_tot = len(dd) io.print_ratio( - 'galsim: Objects selected as galaxies', + "galsim: Objects selected as galaxies", n_gal_galsim, n_tot, stats_file, - verbose=verbose) + verbose=verbose, + ) return m_gal_galsim @@ -292,11 +275,9 @@ def get_tile_wcs_new(xxx, yyy): w = WCS(naxis=2) w.wcs.crval = np.array([ra.deg, dec.deg]) w.wcs.crpix = np.array([5000, 5000]) - w.wcs.cd = np.array( - [[0.187 / 3600, 0], [0, 0.187 / 3600]] - ) - w.wcs.ctype = ['RA---TAN', 'DEC--TAN'] - w.wcs.cunit = ['deg', 'deg'] + w.wcs.cd = np.array([[0.187 / 3600, 0], [0, 0.187 / 3600]]) + w.wcs.ctype = ["RA---TAN", "DEC--TAN"] + w.wcs.cunit = ["deg", "deg"] w._naxis = [10000, 10000] return w @@ -306,24 +287,21 @@ def get_tile_wcs(xxx, yyy): ra = float(xxx) / 2 / np.cos(np.deg2rad(dec)) new_wcs = WCS(naxis=2) - new_wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN'] - new_wcs.wcs.cunit = ['deg ', 'deg '] - new_wcs.wcs.crpix = [5.000000000000E+03, 5.000000000000E+03] + new_wcs.wcs.ctype = ["RA---TAN", "DEC--TAN"] + new_wcs.wcs.cunit = ["deg ", "deg "] + new_wcs.wcs.crpix = [5.000000000000e03, 5.000000000000e03] new_wcs.wcs.crval = [ra, dec] - new_wcs.wcs.cd = [ - [-5.160234650248E-05, 0.], - [0., 5.160234650248E-05] - ] + new_wcs.wcs.cd = [[-5.160234650248e-05, 0.0], [0.0, 5.160234650248e-05]] return new_wcs def runner(r, all_tiles_id, all_tiles_ra, all_tiles_dec): - xxx, yyy = re.findall(r'\d+', re.split(r'\s', r.meta['text'])[1]) + xxx, yyy = re.findall(r"\d+", re.split(r"\s", r.meta["text"])[1]) idx = np.where(all_tiles_id == float(xxx) + float(yyy) / 1000) tile_points = coords.SkyCoord( all_tiles_ra[idx], all_tiles_dec[idx], - unit='deg', + unit="deg", ) m_cont = r.contains(tile_points, get_tile_wcs(xxx, yyy)) m_not_cont = np.invert(m_cont) @@ -336,12 +314,10 @@ def runner(r, all_tiles_id, all_tiles_ra, all_tiles_dec): all_regions = regions.Regions.read(region_file_path) - res = Parallel(n_jobs=n_jobs, backend='loky')(delayed(runner)( - r, - tile_id, - tile_ra, - tile_dec - ) for r in tqdm(all_regions, total=len(all_regions))) + res = Parallel(n_jobs=n_jobs, backend="loky")( + delayed(runner)(r, tile_id, tile_ra, tile_dec) + for r in tqdm(all_regions, total=len(all_regions)) + ) m_over = np.ones(len(tile_id), dtype=bool) for m_not_cont, idx in res: diff --git a/src/sp_validation/glass_mock.py b/src/sp_validation/glass_mock.py index 15b87b3b..e7a184b8 100644 --- a/src/sp_validation/glass_mock.py +++ b/src/sp_validation/glass_mock.py @@ -160,8 +160,7 @@ def build_camb_params(config: GlassMockConfig): realized = camb_sigma8(pars) assert np.isclose(config.sigma8, realized), ( - f"As rescaling missed target sigma8: wanted {config.sigma8}, " - f"got {realized}" + f"As rescaling missed target sigma8: wanted {config.sigma8}, got {realized}" ) return pars @@ -315,9 +314,7 @@ def create_mask_from_catalogue(nside, path, output, ra_col="RA", dec_col="DEC"): phi = ra * np.pi / 180.0 pix = hp.ang2pix(nside, theta, phi) - _unique_pix, _idx, idx_rep = np.unique( - pix, return_index=True, return_inverse=True - ) + _unique_pix, _idx, idx_rep = np.unique(pix, return_index=True, return_inverse=True) n_gal = np.zeros(hp.nside2npix(nside)) n_gal[np.unique(pix)] = np.bincount(idx_rep) mask = n_gal != 0 @@ -390,9 +387,7 @@ def get_n_gal_map(nside, ra, dec): phi = ra * np.pi / 180.0 pix = hp.ang2pix(nside, theta, phi) - unique_pix, idx, idx_rep = np.unique( - pix, return_index=True, return_inverse=True - ) + unique_pix, idx, idx_rep = np.unique(pix, return_index=True, return_inverse=True) n_gal = np.zeros(hp.nside2npix(nside)) n_gal[unique_pix] = np.bincount(idx_rep) return n_gal, unique_pix, idx, idx_rep diff --git a/src/sp_validation/io.py b/src/sp_validation/io.py index bc038b99..f2acaea9 100644 --- a/src/sp_validation/io.py +++ b/src/sp_validation/io.py @@ -31,13 +31,13 @@ def make_out_dirs(output_dir, plot_dir, plot_subdirs, verbose=False): for d in (output_dir, plot_dir): if not os.path.isdir(d): if verbose: - print('Creating dir {}'.format(d)) + print("Creating dir {}".format(d)) os.mkdir(d) for sd in plot_subdirs: - dsd = '{}/{}'.format(plot_dir, sd) + dsd = "{}/{}".format(plot_dir, sd) if not os.path.isdir(dsd): if verbose: - print('Creating dir {}'.format(dsd)) + print("Creating dir {}".format(dsd)) os.mkdir(dsd) @@ -53,7 +53,7 @@ def open_stats_file(directory, file_name): file_name : string file name """ - stats_file = open('{}/{}'.format(directory, file_name), 'w') + stats_file = open("{}/{}".format(directory, file_name), "w") return stats_file @@ -73,7 +73,7 @@ def print_stats(msg, stats_file, verbose=False): print message to stdout if True """ stats_file.write(msg) - stats_file.write('\n') + stats_file.write("\n") stats_file.flush() if verbose: @@ -102,18 +102,17 @@ def print_ratio(msg, numerator, denominator, stats_file, verbose=False): ratio = 0 print_stats( - f'{msg} = {numerator}/{denominator}' - + f' = {ratio:.1f}%', - stats_file, verbose=verbose + f"{msg} = {numerator}/{denominator}" + f" = {ratio:.1f}%", + stats_file, + verbose=verbose, ) def write_binned_quantity(quantity, key, bin_edges, extra_key="quantity"): - + shape = quantity.shape - len_shape = len(shape) nx, ny = shape[:2] - + filename = f"{key}_binned.npz" combined = {**bin_edges, extra_key: quantity} diff --git a/src/sp_validation/masks.py b/src/sp_validation/masks.py index f7abcc38..a7ea74d1 100644 --- a/src/sp_validation/masks.py +++ b/src/sp_validation/masks.py @@ -7,13 +7,11 @@ :Author: Martin Kilbinger """ -import numpy as np -import numexpr as ne -from scipy import stats - import healsparse as hsp - +import numexpr as ne +import numpy as np from astropy.io import fits +from scipy import stats def correlation_matrix(masks, confidence_level=0.9): @@ -29,9 +27,7 @@ def correlation_matrix(masks, confidence_level=0.9): for jdx, mask_jdx in enumerate(masks): res = stats.pearsonr(mask_idx._mask, mask_jdx._mask) r_val[idx][jdx] = res.statistic - r_cl[idx][jdx] = res.confidence_interval( - confidence_level=confidence_level - ) + r_cl[idx][jdx] = res.confidence_interval(confidence_level=confidence_level) return r_val, r_cl @@ -40,11 +36,8 @@ def confusion_matrix(prediction, observation): result = {} - pred_pos = sum(prediction) result["true_pos"] = sum(prediction & observation) - result["true_neg"] = sum( - np.logical_not(prediction) & np.logical_not(observation) - ) + result["true_neg"] = sum(np.logical_not(prediction) & np.logical_not(observation)) result["false_neg"] = sum(prediction & np.logical_not(observation)) result["false_pos"] = sum(np.logical_not(prediction) & observation) result["false_pos_rate"] = result["false_pos"] / ( @@ -71,11 +64,11 @@ def confusion_matrix(prediction, observation): return result -class Mask(): +class Mask: """Mask. - + Class to handle masking of catalogues. - + Parameters ---------- col_name : str @@ -95,7 +88,7 @@ class Mask(): """ def __init__(self, col_name, label, kind=None, value=0, dat=None, verbose=False): - + self._col_name = col_name self._label = label self._value = value @@ -108,20 +101,20 @@ def __init__(self, col_name, label, kind=None, value=0, dat=None, verbose=False) if dat is not None: self.apply(dat) - + def __repr__(self): - + return ( f"Mask(col_name={self._col_name}, label={self._label}, kind={self._kind}," + f" value={self._value})" ) - - @classmethod + + @classmethod def from_list(cls, masks, label="combined", verbose=False): if verbose: print(f"Combining {len(masks)} masks") - + my_mask = cls(label, label, kind="combined", value=None) my_mask._mask = np.logical_and.reduce([m._mask for m in masks]) @@ -129,20 +122,39 @@ def from_list(cls, masks, label="combined", verbose=False): return my_mask def apply(self, dat): - + # Get column col_data = dat[self._col_name] - + if self._kind == "equal": - self._mask = ne.evaluate("col_data == value", local_dict={"col_data": col_data, "value": self._value}) + self._mask = ne.evaluate( + "col_data == value", + local_dict={"col_data": col_data, "value": self._value}, + ) elif self._kind == "not_equal": - self._mask = ne.evaluate("col_data != value", local_dict={"col_data": col_data, "value": self._value}) + self._mask = ne.evaluate( + "col_data != value", + local_dict={"col_data": col_data, "value": self._value}, + ) elif self._kind == "greater_equal": - self._mask = ne.evaluate("col_data >= value", local_dict={"col_data": col_data, "value": self._value}) + self._mask = ne.evaluate( + "col_data >= value", + local_dict={"col_data": col_data, "value": self._value}, + ) elif self._kind == "smaller_equal": - self._mask = ne.evaluate("col_data <= value", local_dict={"col_data": col_data, "value": self._value}) + self._mask = ne.evaluate( + "col_data <= value", + local_dict={"col_data": col_data, "value": self._value}, + ) elif self._kind == "range": - self._mask = ne.evaluate("(col_data >= low) & (col_data <= high)", local_dict={"col_data": col_data, "low": self._value[0], "high": self._value[1]}) + self._mask = ne.evaluate( + "(col_data >= low) & (col_data <= high)", + local_dict={ + "col_data": col_data, + "low": self._value[0], + "high": self._value[1], + }, + ) else: raise ValueError(f"Invalid kind {self._kind}") @@ -165,33 +177,33 @@ def to_bool(self, hsp_mask): ) mask_bool[valid_pixels] = self._mask return mask_bool - + @classmethod def print_strings(cls, coln, lab, num, fnum, f_out=None): msg = f"{coln:30s} {lab:30s} {num:10s} {fnum:10s}" print(msg) if f_out: print(msg, file=f_out) - + def print_stats(self, num_obj, f_out=None): if self._num_ok is None: self._num_ok = sum(self._mask) si = f"{self._num_ok:10d}" - sf = f"{self._num_ok/num_obj:10.2%}" + sf = f"{self._num_ok / num_obj:10.2%}" self.print_strings(self._col_name, self._label, si, sf, f_out=f_out) def get_sign(self, latex=False): - + sign = None if self._kind == "equal": sign = "$=$" if latex else "=" elif self._kind == "not_equal": - sign = "$\ne$" if latex else "!=" + sign = r"$\ne$" if latex else "!=" elif self._kind in ("greater_equal", "range"): - sign = "$\leq$" if latex else ">=" + sign = r"$\leq$" if latex else ">=" elif self._kind == "smaller_equal": - sign = "$\geq$" if latex else "<=" + sign = r"$\geq$" if latex else "<=" return sign def print_condition(self, f_out, latex=False): @@ -200,7 +212,7 @@ def print_condition(self, f_out, latex=False): return "" sign = self.get_sign(latex=latex) - + name = self._label if latex else self._col_name if sign is not None: @@ -230,16 +242,17 @@ def create_descr(self): if self._kind == "range": descr = f"{self._value[0]}<={self._col_name}<={self._value[1]}" self._descr = descr - + # Create description for FITS header + def add_summary_to_FITS_header(self, header): header_new = fits.Header() - + self.create_descr() header_new[self._col_name] = (self._descr, self._label) - + header.update(header_new) @@ -251,7 +264,7 @@ def print_mask_stats(num_obj, masks, mask_combined): Parameters ---------- num_obj - + """ Mask.print_strings("flag", "label", f"{'num_ok':>10}", f"{'num_ok[%]':>10}") for my_mask in masks: @@ -260,17 +273,11 @@ def print_mask_stats(num_obj, masks, mask_combined): mask_combined.print_stats(num_obj) -def get_masks_from_config( - config, - dat, - dat_ext, - masks_to_apply=None, - verbose=False -): +def get_masks_from_config(config, dat, dat_ext, masks_to_apply=None, verbose=False): """Get Masks From Config. - + Return mask information from yaml config structure. - + Parameters ---------- config : dict @@ -283,41 +290,37 @@ def get_masks_from_config( masks to apply exclusively; if `None` (default), use all masks verbose : bool, optional verbose output if ``True``; default is ``False`` - + Returns ------- list list of masks dict list of indices for given mask column name (label) - + """ # List to store all mask objects masks = [] # Dict to associate labels with index in mask list labels = {} - - # Loop over mask sections from config file - config_data = { - key: config[key] for key in ["dat", "dat_ext"] if key in config - } + + # Loop over mask sections from config file + config_data = {key: config[key] for key in ["dat", "dat_ext"] if key in config} idx = 0 for section, mask_list in config_data.items(): - # Set data source dat_source = dat if section == "dat" else dat_ext # Loop over mask information in this section for mask_params in mask_list: - - use_this_mask = False + use_this_mask = False if masks_to_apply is not None: if mask_params["col_name"] in masks_to_apply: use_this_mask = True else: use_this_mask = True - + if use_this_mask: # Ensure 'range' kind has exactly two values value = mask_params["value"] @@ -337,5 +340,5 @@ def get_masks_from_config( if verbose: print(f"Skipping mask {mask_params['col_name']}") continue - + return masks, labels diff --git a/src/sp_validation/plot_style.py b/src/sp_validation/plot_style.py index a5c02ca4..597d57f9 100644 --- a/src/sp_validation/plot_style.py +++ b/src/sp_validation/plot_style.py @@ -10,9 +10,6 @@ """ import matplotlib as mpl -import matplotlib.pylab as plt -from matplotlib.collections import PatchCollection -from matplotlib.patches import Polygon mpl.rcParams["lines.linewidth"] = 2 mpl.rcParams["lines.markersize"] = 10 diff --git a/src/sp_validation/plots.py b/src/sp_validation/plots.py index 06fa4f25..4522c842 100644 --- a/src/sp_validation/plots.py +++ b/src/sp_validation/plots.py @@ -9,22 +9,69 @@ """ -from collections import Counter - -import healpy as hp import healsparse as hsp import matplotlib.pylab as plt import matplotlib.scale as mscale import matplotlib.ticker as ticker import matplotlib.transforms as mtransforms import numpy as np -import skyproj -from astropy import units as u -from astropy.coordinates import SkyCoord from cs_util import plots from lenspack.geometry.projections.gnom import radec2xy -from sp_validation.plot_style import * +# Imported for its import-time side effect: sets matplotlib rcParams (plot style). +import sp_validation.plot_style # noqa: F401 +from sp_validation.masks import Mask + + +class SquareRootScale(mscale.ScaleBase): + """ + ScaleBase class for generating square root scale. + + Usage example: axis.set_yscale('squareroot') + + """ + + name = "squareroot" + + def __init__(self, axis, **kwargs): + mscale.ScaleBase.__init__(self, axis, **kwargs) + + def set_default_locators_and_formatters(self, axis): + axis.set_major_locator(ticker.AutoLocator()) + axis.set_major_formatter(ticker.ScalarFormatter()) + axis.set_minor_locator(ticker.NullLocator()) + axis.set_minor_formatter(ticker.NullFormatter()) + + def limit_range_for_scale(self, vmin, vmax, minpos): + return max(0.0, vmin), vmax + + class SquareRootTransform(mtransforms.Transform): + input_dims = 1 + output_dims = 1 + is_separable = True + + def transform_non_affine(self, a): + return np.array(a) ** 0.5 + + def inverted(self): + return SquareRootScale.InvertedSquareRootTransform() + + class InvertedSquareRootTransform(mtransforms.Transform): + input_dims = 1 + output_dims = 1 + is_separable = True + + def transform(self, a): + return np.array(a) ** 2 + + def inverted(self): + return SquareRootScale.SquareRootTransform() + + def get_transform(self): + return self.SquareRootTransform() + + +mscale.register_scale(SquareRootScale) class SquareRootScale(mscale.ScaleBase): @@ -272,7 +319,6 @@ def plot_map( if clusters: x_cluster = (clusters["x"] + mean_x - min_x) / (max_x - min_x) * Nx y_cluster = (clusters["y"] + mean_y - min_y) / (max_y - min_y) * Ny - dy = 0.02 plt.plot( x_cluster, y_cluster, @@ -340,10 +386,9 @@ def plot_binned( len_shape = len(quantities[key].shape) fig_size = 2 * len_shape - fig = plt.figure(figsize=(fig_size, fig_size)) + plt.figure(figsize=(fig_size, fig_size)) if len_shape == 2: - ax = plt.subplot2grid((1, 1), (0, 0)) plot_binned_one( ax, @@ -380,7 +425,7 @@ def plot_binned( bin_edges_y, vmin=my_vmin, vmax=my_vmax, - title=f"${title_base}_{{{idx+1}{jdx+1}}}$", + title=f"${title_base}_{{{idx + 1}{jdx + 1}}}$", xlabel=xlabel, ylabel=ylabel, ) @@ -430,84 +475,90 @@ def hsp_map_logical_or(maps, verbose=False): return map_comb -def plot_area_mask(ra, dec, zoom, mask=None): - """Plot Area Mask. - - Create sky plot of objects. - - Parameters - ---------- - ra : list - R.A. coordinates - dec : list - Dec. coordinates - zoom : TBD - mask: TBD, optional - - """ - if mask is None: - mask == np.ones_like(ra) - - fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(30,15)) - axes[0].hexbin(ra[mask], dec[mask], gridsize=100) - axes[1].hexbin(ra[mask & zoom], dec[mask & zoom], gridsize=200) - for idx in (0, 1): - axes[idx].set_xlabel("R.A. [deg]") +def plot_area_mask(ra, dec, zoom, mask=None): + """Plot Area Mask. + + Create sky plot of objects. + + Parameters + ---------- + ra : list + R.A. coordinates + dec : list + Dec. coordinates + zoom : TBD + mask: TBD, optional + + """ + if mask is None: + mask == np.ones_like(ra) + + fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(30, 15)) + axes[0].hexbin(ra[mask], dec[mask], gridsize=100) + axes[1].hexbin(ra[mask & zoom], dec[mask & zoom], gridsize=200) + for idx in (0, 1): + axes[idx].set_xlabel("R.A. [deg]") axes[idx].set_ylabel("Dec [deg]") -def sky_plots(dat, masks, labels, zoom_ra, zoom_dec): - """Sky Plots. - - Plot sky regions with different masks. - - Parameters - ---------- - masks : list - masks to be applied - labels : dict - labels for masks - zoom_ra : list - min and max R.A. for zoom-in plot - zoom_dec : list - min and max Dec. for zoom-in plot - - """ - ra = dat["RA"][:] - dec = dat["Dec"][:] - - zoom_ra = (room_ra[0] < dat["RA"]) & (dat["RA"] < zoom_ra[1]) - zoom_dec = (zoom_dec[0] < dat["Dec"]) & (dat["Dec"] < zoom_dec[1]) - zoom = zoom_ra & zoom_dec - - # No mask - plot_area_mask(ra, dec, zoom) - - # SExtractor and SP flags - m_flags = masks[labels["FLAGS"]]._mask & masks[labels["IMAFLAGS_ISO"]]._mask - plot_area_mask(ra, dec, zoom, mask=m_flags) - - # Overlap regions - m_over = masks[labels["overlap"]]._mask & m_flags - plot_area_mask(ra, dec, zoom, mask=m_over) - - # Coverage mask - m_point = masks[labels["npoint3"]]._mask & m_over - plot_area_mask(ra, dec, zoom, mask=m_point) - - # Maximask - m_maxi = masks[labels["1024_Maximask"]]._mask & m_point - plot_area_mask(ra, dec, zoom, mask=m_maxi) - - m_comb = mask_combined._mask - plot_area_mask(ra, dec, zoom, mask=m_comb) - - m_man = m_maxi & masks[labels["8_Manual"]]._mask - plot_area_mask(ra, dec, zoom, mask=m_man) - - m_halos = ( - m_maxi - & masks[labels['1_Faint_star_halos']]._mask - & masks[labels['2_Bright_star_halos']]._mask - ) - plot_area_mask(ra, dec, zoom, mask=m_halos) +def sky_plots(dat, masks, labels, zoom_ra, zoom_dec): + """Sky Plots. + + Plot sky regions with different masks. + + Parameters + ---------- + masks : list + masks to be applied + labels : dict + labels for masks + zoom_ra : list + min and max R.A. for zoom-in plot + zoom_dec : list + min and max Dec. for zoom-in plot + + """ + ra = dat["RA"][:] + dec = dat["Dec"][:] + + zoom_ra = (zoom_ra[0] < dat["RA"]) & (dat["RA"] < zoom_ra[1]) + zoom_dec = (zoom_dec[0] < dat["Dec"]) & (dat["Dec"] < zoom_dec[1]) + zoom = zoom_ra & zoom_dec + + # No mask + plot_area_mask(ra, dec, zoom) + + # SExtractor and SP flags + m_flags = masks[labels["FLAGS"]]._mask & masks[labels["IMAFLAGS_ISO"]]._mask + plot_area_mask(ra, dec, zoom, mask=m_flags) + + # Overlap regions + m_over = masks[labels["overlap"]]._mask & m_flags + plot_area_mask(ra, dec, zoom, mask=m_over) + + # Coverage mask + m_point = masks[labels["npoint3"]]._mask & m_over + plot_area_mask(ra, dec, zoom, mask=m_point) + + # Maximask + m_maxi = masks[labels["1024_Maximask"]]._mask & m_point + plot_area_mask(ra, dec, zoom, mask=m_maxi) + + # Combined mask over all supplied masks (was passed in by the caller before + # this routine was extracted into a function; rebuilt here from ``masks``). + # ``masks`` is the list returned by ``get_masks_from_config`` (``labels`` + # maps name -> integer index into it), so ``from_list`` consumes it directly + # -- exactly as the caller does (``Mask.from_list(masks, label="combined")``). + mask_combined = Mask.from_list(masks, label="combined") + m_comb = mask_combined._mask + plot_area_mask(ra, dec, zoom, mask=m_comb) + + m_man = m_maxi & masks[labels["8_Manual"]]._mask + plot_area_mask(ra, dec, zoom, mask=m_man) + + m_halos = ( + m_maxi + & masks[labels["1_Faint_star_halos"]]._mask + & masks[labels["2_Bright_star_halos"]]._mask + ) + plot_area_mask(ra, dec, zoom, mask=m_halos) diff --git a/src/sp_validation/rho_tau.py b/src/sp_validation/rho_tau.py index 3040ef50..0a311432 100644 --- a/src/sp_validation/rho_tau.py +++ b/src/sp_validation/rho_tau.py @@ -161,11 +161,7 @@ def get_rho_tau( outdir_path = Path(outdir) rho_path = outdir_path / f"rho_stats_{base}.fits" catalog_id = f"{base}_jk" if cov_rho else base - cov_rho_path = ( - outdir_path / f"cov_rho_{catalog_id}.npy" - if cov_rho - else None - ) + cov_rho_path = outdir_path / f"cov_rho_{catalog_id}.npy" if cov_rho else None rho_stat_handler = RhoStat( output=outdir, treecorr_config=treecorr_config, verbose=True @@ -218,7 +214,6 @@ def get_rho_tau( print(f"Skipping tau statistics computation, file {tau_path} already exists.") tau_stat_handler.load_tau_stats(tau_path.name) else: - tau_stat_handler.catalogs.set_params(params, outdir) mask = version != "DES" @@ -283,9 +278,7 @@ def get_theory_cov( target_cov = Path(outdir) / f"cov_tau_{base}_th.npy" if target_cov.exists(): - print( - f"Skipping covariance computation, file {target_cov} already exists." - ) + print(f"Skipping covariance computation, file {target_cov} already exists.") return print("Computing the covariance matrix for the version: ", version) @@ -333,9 +326,7 @@ def get_jackknife_cov( tau_cov_path = Path(outdir) / f"cov_tau_{base}_jk.npy" if tau_cov_path.exists(): - print( - f"Skipping covariance computation, file {tau_cov_path} already exists." - ) + print(f"Skipping covariance computation, file {tau_cov_path} already exists.") rho_stat_handler = RhoStat( output=outdir, treecorr_config=treecorr_config, verbose=False ) @@ -375,13 +366,10 @@ def get_jackknife_cov( tau_stat_handler.catalogs.set_params(params, outdir) for i in range(ncov): - tau_chunk = outdir + f"/cov_tau_{version}{i}.npy" rho_chunk = outdir + f"/cov_rho_{version}{i}.npy" if not (os.path.exists(tau_chunk) and os.path.exists(rho_chunk)): - print( - f"Computing rho-statistics for {version} (patch {i+1}/{ncov})" - ) + print(f"Computing rho-statistics for {version} (patch {i + 1}/{ncov})") if f"psf_{version}{i}" not in rho_stat_handler.catalogs.catalogs_dict: # Build catalogues @@ -407,9 +395,7 @@ def get_jackknife_cov( ) else: - print( - f"Computing the patch centers for patch {i+1}/{ncov}" - ) + print(f"Computing the patch centers for patch {i + 1}/{ncov}") npatch = rho_stat_handler.catalogs._params["patch_number"] field = rho_stat_handler.catalogs.catalogs_dict[ @@ -417,7 +403,7 @@ def get_jackknife_cov( ].getNField(max_top=int.bit_length(npatch) - 1, coords="spherical") patch, centers = field.run_kmeans(npatch) - #Update the patch centers of the catalogs + # Update the patch centers of the catalogs for key, cat in rho_stat_handler.catalogs.catalogs_dict.items(): cat._centers = centers field = cat.getNField( @@ -443,17 +429,17 @@ def get_jackknife_cov( var_method="jackknife", ) - #Update the keys in the dictionaries + # Update the keys in the dictionaries rho_dict = rho_stat_handler.catalogs.catalogs_dict tau_dict = tau_stat_handler.catalogs.catalogs_dict - rho_dict[f"psf_{version}{i+1}"] = rho_dict.pop(f"psf_{version}{i}") - rho_dict[f"psf_error_{version}{i+1}"] = rho_dict.pop( + rho_dict[f"psf_{version}{i + 1}"] = rho_dict.pop(f"psf_{version}{i}") + rho_dict[f"psf_error_{version}{i + 1}"] = rho_dict.pop( f"psf_error_{version}{i}" ) - rho_dict[f"psf_size_error_{version}{i+1}"] = rho_dict.pop( + rho_dict[f"psf_size_error_{version}{i + 1}"] = rho_dict.pop( f"psf_size_error_{version}{i}" ) - tau_dict[f"gal_{version}{i+1}"] = tau_dict.pop(f"gal_{version}{i}") + tau_dict[f"gal_{version}{i + 1}"] = tau_dict.pop(f"gal_{version}{i}") cov_tau_loc = np.zeros_like(np.load(outdir + f"/cov_tau_{version}0.npy")) cov_rho_loc = np.zeros_like(np.load(outdir + f"/cov_rho_{version}0.npy")) diff --git a/src/sp_validation/statistics.py b/src/sp_validation/statistics.py index c8e3d862..bfd04c38 100644 --- a/src/sp_validation/statistics.py +++ b/src/sp_validation/statistics.py @@ -26,7 +26,7 @@ def jackknif_weighted_average2( keep_size_pc = 1 - remove_size if keep_size_pc < 0: - raise ValueError('remove size should be in [0, 1]') + raise ValueError("remove size should be in [0, 1]") subsamp_size = int(samp_size * keep_size_pc) @@ -36,7 +36,7 @@ def jackknif_weighted_average2( for i in range(n_realization): sub_data_ind = np.random.choice(all_ind, subsamp_size) - if (sum(data[sub_data_ind]) == 0): + if sum(data[sub_data_ind]) == 0: all_est.append(np.nan) else: all_est.append( diff --git a/src/sp_validation/survey.py b/src/sp_validation/survey.py index 66ae726d..9b2d10f2 100644 --- a/src/sp_validation/survey.py +++ b/src/sp_validation/survey.py @@ -7,9 +7,7 @@ """ - import os - from collections import Counter @@ -39,23 +37,23 @@ def get_area(dd, area_tile, verbose=False): tile IDs """ - if 'TILE_ID' in dd.dtype.names: + if "TILE_ID" in dd.dtype.names: # Get unique tile IDs - tile_IDs = set(dd['TILE_ID']) + tile_IDs = set(dd["TILE_ID"]) n_tile = len(tile_IDs) if verbose: - print(f'Number of tiles found in galaxy catalogue = {n_tile}') + print(f"Number of tiles found in galaxy catalogue = {n_tile}") else: # Set to dummy values tile_IDs = None - n_tiles = 1 + n_tile = 1 # Compute area area_deg2 = n_tile * area_tile area_amin2 = area_deg2 * 3600 if verbose: - print('Area [deg^2] = {}'.format(area_deg2)) + print("Area [deg^2] = {}".format(area_deg2)) return area_deg2, area_amin2, tile_IDs @@ -92,7 +90,6 @@ def missing_tiles( number of tiles missing, -1 if ID file path not found """ if os.path.exists(path_tile_ID): - # Loop over input tile ID file found_IDs = [] missing_IDs = [] @@ -112,29 +109,26 @@ def missing_tiles( if verbose: n_tile = len(tile_IDs) - print( - f'{n_missing}/{n_tile} = {n_missing / n_tile:.2%}' - + ' tiles missing' - ) + print(f"{n_missing}/{n_tile} = {n_missing / n_tile:.2%}" + " tiles missing") # Create output files with found and missing IDs if n_found > 0: if verbose: - print(f'Creating file \'{path_found_ID}\'') - with open(path_found_ID, 'w') as f_out: + print(f"Creating file '{path_found_ID}'") + with open(path_found_ID, "w") as f_out: for ID in found_IDs: print(ID, file=f_out) if n_missing > 0: if verbose: - print('Creating file \'{path_missing_ID}\'') - with open(path_missing_ID, 'w') as f_out: + print("Creating file '{path_missing_ID}'") + with open(path_missing_ID, "w") as f_out: for ID in missing_IDs: print(ID, file=f_out) else: if verbose: - print(f'Tile ID file \'{path_tile_ID}\' not found') + print(f"Tile ID file '{path_tile_ID}' not found") # Set to dummy values n_found = -1 @@ -167,17 +161,14 @@ def write_tile_id_gal_counts(detection_IDs, galaxy_IDs, shape_IDs, fname): galaxy_counts = Counter(galaxy_IDs) shape_counts = Counter(shape_IDs) - with open(fname, 'w') as f: - + with open(fname, "w") as f: # Loop over tile IDs of detected objects for tile_id in detection_counts: - # Write tile ID in CFIS tile format (`ABC.XYZ`) - print(f'{tile_id:007.3f}', end=' ', file=f) + print(f"{tile_id:007.3f}", end=" ", file=f) # Loop over counters for x in detection_counts, galaxy_counts, shape_counts: - # Get number of objects in corresponding counter if tile_id in x: num = x[tile_id] @@ -185,7 +176,7 @@ def write_tile_id_gal_counts(detection_IDs, galaxy_IDs, shape_IDs, fname): num = 0 # Write number to file - print(num, end=' ', file=f) + print(num, end=" ", file=f) print(file=f) @@ -223,50 +214,38 @@ def get_footprint(patch, ra, dec): # Check whether input matches one of the seven CFIS patch name. # Return coordinates within the patch - if patch == 'P1': - + if patch == "P1": return (ra > 100) & (ra < ra_14) & (dec > dec_min) & (dec < dec_max) - elif patch == 'P2': - + elif patch == "P2": # -30 < ra < 60 - return ( - ((ra > 0) & (ra < 60)) - | ((ra > 330) & (ra < 360)) - & (dec > dec_min) & (dec < dec_max) + return ((ra > 0) & (ra < 60)) | ((ra > 330) & (ra < 360)) & (dec > dec_min) & ( + dec < dec_max ) - elif patch == 'P3': - + elif patch == "P3": return (ra > ra2_34) & (ra < ra_36) & (dec > dec_3456) & (dec < 70) - elif patch == 'P4': - + elif patch == "P4": return ( ((ra > ra_14) & (ra < ra_45) & (dec > dec_min) & (dec < dec_3456)) | ((ra > ra_14) & (ra < ra2_34) & (dec > dec_min) & (dec < 70)) | ((ra > ra_45) & (ra < ra2_45) * (dec > dec_min) & (dec < 36)) ) - elif patch == 'P5': - - return ( - ((ra > ra2_45) & (ra < 330) & (dec > dec_min) & (dec < dec_3456)) - | ((ra > ra_45) & (ra < ra2_45) & (dec > 36) & (dec < dec_3456)) + elif patch == "P5": + return ((ra > ra2_45) & (ra < 330) & (dec > dec_min) & (dec < dec_3456)) | ( + (ra > ra_45) & (ra < ra2_45) & (dec > 36) & (dec < dec_3456) ) - elif patch == 'P6': - + elif patch == "P6": return (ra > ra_36) & (ra < 330) & (dec > dec_3456) & (dec > 70) - elif patch == 'P7': - + elif patch == "P7": return (ra > 60) & (ra < 180) & (dec > 60) & (dec < 90) - elif patch == 'W3': - + elif patch == "W3": return (ra > 208) & (ra < 221) & (dec > 51) & (dec < 58) else: - - return (dec > dec_min) + return dec > dec_min diff --git a/src/sp_validation/tests/test_b_modes.py b/src/sp_validation/tests/test_b_modes.py index 19ef78b5..e8e05c39 100644 --- a/src/sp_validation/tests/test_b_modes.py +++ b/src/sp_validation/tests/test_b_modes.py @@ -68,6 +68,7 @@ def _eb_inputs(): # 1. correlation_from_covariance # --------------------------------------------------------------------------- + def test_correlation_from_covariance_exact(): """Pin the correlation matrix derived from a known SPD covariance. @@ -80,18 +81,22 @@ def test_correlation_from_covariance_exact(): correlation, and the unit-diagonal / known off-diagonal structure would break under any rescaling bug in the normalization. """ - cov = np.array([ - [4.0, 2.0, 0.0], - [2.0, 9.0, -3.0], - [0.0, -3.0, 16.0], - ]) + cov = np.array( + [ + [4.0, 2.0, 0.0], + [2.0, 9.0, -3.0], + [0.0, -3.0, 16.0], + ] + ) corr = b_modes.correlation_from_covariance(cov) - expected = np.array([ - [1.0, 1.0 / 3.0, 0.0], - [1.0 / 3.0, 1.0, -1.0 / 4.0], - [0.0, -1.0 / 4.0, 1.0], - ]) + expected = np.array( + [ + [1.0, 1.0 / 3.0, 0.0], + [1.0 / 3.0, 1.0, -1.0 / 4.0], + [0.0, -1.0 / 4.0, 1.0], + ] + ) npt.assert_allclose(corr, expected, rtol=1e-12, atol=0) # Diagonal is exactly unity, off-diagonal symmetric. npt.assert_allclose(np.diag(corr), np.ones(3), rtol=0, atol=1e-14) @@ -104,11 +109,13 @@ def test_correlation_from_covariance_has_teeth(): Flipping the sign of cov[0,1] flips the (0,1) correlation; the pinned +1/3 must NOT survive this perturbation. """ - cov = np.array([ - [4.0, 2.0, 0.0], - [2.0, 9.0, -3.0], - [0.0, -3.0, 16.0], - ]) + cov = np.array( + [ + [4.0, 2.0, 0.0], + [2.0, 9.0, -3.0], + [0.0, -3.0, 16.0], + ] + ) cov_perturbed = cov.copy() cov_perturbed[0, 1] = cov_perturbed[1, 0] = -2.0 corr_perturbed = b_modes.correlation_from_covariance(cov_perturbed) @@ -120,6 +127,7 @@ def test_correlation_from_covariance_has_teeth(): # 2. scale_cut_to_bins # --------------------------------------------------------------------------- + def test_scale_cut_to_bins_known_cuts(): """Pin (start_bin, stop_bin) for known cuts on the log grid. @@ -182,16 +190,19 @@ def test_find_conservative_scale_cut_key_conservative_and_fallthrough(): req (4, 20): no key fits inside [4,20]; closest is (5,30) (dist |5-4|+|30-20| = 11, beats all others). """ - assert b_modes.find_conservative_scale_cut_key( - _SCALE_CUT_KEYS, (2.0, 80.0) - ) == (2.0, 80.0) - assert b_modes.find_conservative_scale_cut_key( - _SCALE_CUT_KEYS, (1.5, 60.0) - ) == (2.0, 50.0) + assert b_modes.find_conservative_scale_cut_key(_SCALE_CUT_KEYS, (2.0, 80.0)) == ( + 2.0, + 80.0, + ) + assert b_modes.find_conservative_scale_cut_key(_SCALE_CUT_KEYS, (1.5, 60.0)) == ( + 2.0, + 50.0, + ) # No conservative match -> closest-by-distance fallthrough. - assert b_modes.find_conservative_scale_cut_key( - _SCALE_CUT_KEYS, (4.0, 20.0) - ) == (5.0, 30.0) + assert b_modes.find_conservative_scale_cut_key(_SCALE_CUT_KEYS, (4.0, 20.0)) == ( + 5.0, + 30.0, + ) def test_find_conservative_scale_cut_key_has_teeth(): @@ -213,6 +224,7 @@ def test_find_conservative_scale_cut_key_has_teeth(): # 4. calculate_eb_statistics (headline) # --------------------------------------------------------------------------- + def test_calculate_eb_statistics_pte_matrices(): """Pin representative PTE-matrix entries from the full 2D E/B analysis. @@ -244,7 +256,7 @@ def test_calculate_eb_statistics_pte_matrices(): # Structural pins: off the valid upper triangle the matrices are NaN. for key in ("xip_B", "xim_B", "combined"): m = pm[key] - assert np.isnan(m[1, 0]) # start > stop-1 region is invalid + assert np.isnan(m[1, 0]) # start > stop-1 region is invalid assert np.isnan(m[2, 0]) assert np.all(np.isfinite(np.diag(m))) # single-bin cuts are valid diff --git a/src/sp_validation/tests/test_calibration.py b/src/sp_validation/tests/test_calibration.py index d61b8bdb..b1b0dd26 100644 --- a/src/sp_validation/tests/test_calibration.py +++ b/src/sp_validation/tests/test_calibration.py @@ -29,8 +29,7 @@ from astropy.table import Table from sp_validation import calibration -from sp_validation.calibration import metacal, mask_gal_size, mask_gal_SNR - +from sp_validation.calibration import mask_gal_size, mask_gal_SNR, metacal # Fixed, compact inputs shared across the tests. # @@ -53,9 +52,11 @@ def make_fake_gal_metacal(R=R, g1=G1, g2=G2, w=W, mask=MASK): """ return types.SimpleNamespace( R=np.asarray(R, dtype=float), - ns={"g1": np.asarray(g1, dtype=float), + ns={ + "g1": np.asarray(g1, dtype=float), "g2": np.asarray(g2, dtype=float), - "w": np.asarray(w, dtype=float)}, + "w": np.asarray(w, dtype=float), + }, mask_dict={"ns": np.asarray(mask, dtype=bool)}, ) @@ -82,18 +83,27 @@ def test_get_calibrated_quantities_pins_inv_R_application(gal_metacal): # Masked uncalibrated shears: indices 0, 1, 3, 4 of G1 / G2. npt.assert_allclose( g_uncorr, - [[0.10, -0.20, 0.05, -0.15], - [-0.05, 0.15, 0.20, 0.10]], + [[0.10, -0.20, 0.05, -0.15], [-0.05, 0.15, 0.20, 0.10]], rtol=1e-12, ) # Calibrated shears: committed literals from an observed estimator run. npt.assert_allclose( g_corr, - [[0.1482587064676617, -0.30149253731343284, - 0.05174129353233831, -0.22487562189054724], - [-0.07562189054726369, 0.2208955223880597, - 0.2756218905472637, 0.1482587064676617]], + [ + [ + 0.1482587064676617, + -0.30149253731343284, + 0.05174129353233831, + -0.22487562189054724, + ], + [ + -0.07562189054726369, + 0.2208955223880597, + 0.2756218905472637, + 0.1482587064676617, + ], + ], rtol=1e-12, ) @@ -136,27 +146,33 @@ def test_get_calibrated_m_c_pins_additive_bias_and_corrected_shear(gal_metacal): refactor that changed the additive-bias subtraction (e.g. subtracting the uncorrected c instead of inv(R) @ c) would break the closed-form check. """ - g_corr_mc, g_uncorr, w, mask, c, c_err = calibration.get_calibrated_m_c( - gal_metacal - ) + g_corr_mc, g_uncorr, w, mask, c, c_err = calibration.get_calibrated_m_c(gal_metacal) # Additive bias = component-wise mean of the masked uncalibrated shears. npt.assert_allclose(c, [-0.05, 0.10], rtol=1e-12) npt.assert_allclose(c, np.mean(g_uncorr, axis=1), rtol=1e-12) # Error = population std (ddof=0). - npt.assert_allclose( - c_err, [0.12747548783981963, 0.09354143466934854], rtol=1e-12 - ) + npt.assert_allclose(c_err, [0.12747548783981963, 0.09354143466934854], rtol=1e-12) npt.assert_allclose(c_err, np.std(g_uncorr, axis=1), rtol=1e-12) # m+c-corrected shear: committed literals. npt.assert_allclose( g_corr_mc, - [[0.22985074626865673, -0.21990049751243781, - 0.13333333333333333, -0.14328358208955222], - [-0.21791044776119406, 0.07860696517412935, - 0.13333333333333336, 0.005970149253731349]], + [ + [ + 0.22985074626865673, + -0.21990049751243781, + 0.13333333333333333, + -0.14328358208955222, + ], + [ + -0.21791044776119406, + 0.07860696517412935, + 0.13333333333333336, + 0.005970149253731349, + ], + ], rtol=1e-12, ) @@ -292,7 +308,7 @@ def test_metacal_R_matrix_recovers_injected_response(): masking_type="gal", step=0.01, prefix="NGMIX", - size_corr_ell=False, # avoid the in-place T mutation; clean linear cut + size_corr_ell=False, # avoid the in-place T mutation; clean linear cut global_R_weight=None, # unweighted mean over objects ) @@ -367,9 +383,7 @@ def test_mask_gal_size_boolean_mask(): mask = mask_gal_size(T, Tpsf, rel_size_min=0.5, rel_size_max=3.0) - expected = np.array( - [False, False, False, True, True, True, False, False, False] - ) + expected = np.array([False, False, False, True, True, True, False, False, False]) npt.assert_array_equal(mask, expected) # TEETH: tightening the lower bound to 1.0 drops the 0.75 element. diff --git a/src/sp_validation/tests/test_config_paths_exist.py b/src/sp_validation/tests/test_config_paths_exist.py index 9d7ac8da..9ae74ca0 100644 --- a/src/sp_validation/tests/test_config_paths_exist.py +++ b/src/sp_validation/tests/test_config_paths_exist.py @@ -54,9 +54,10 @@ def _repo_root() -> Path: def _on_candide() -> bool: - return socket.gethostname().split(".")[0] == "candide" or Path( - "/automnt/n17data/cdaley" - ).exists() + return ( + socket.gethostname().split(".")[0] == "candide" + or Path("/automnt/n17data/cdaley").exists() + ) def _pathish_key(key: str) -> bool: diff --git a/src/sp_validation/tests/test_cosmo_val.py b/src/sp_validation/tests/test_cosmo_val.py index 84993597..0a618e9a 100644 --- a/src/sp_validation/tests/test_cosmo_val.py +++ b/src/sp_validation/tests/test_cosmo_val.py @@ -38,9 +38,7 @@ def base_config(self, tmp_path): repo_root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ) - catalog_config = os.path.join( - repo_root, "cosmo_val", "cat_config.yaml" - ) + catalog_config = os.path.join(repo_root, "cosmo_val", "cat_config.yaml") # Use temporary directory for outputs output_dir = tmp_path / "test_output" @@ -214,9 +212,7 @@ def test_catalog_paths_exist(self, base_config): repo_root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ) - catalog_config_path = os.path.join( - repo_root, "cosmo_val", "cat_config.yaml" - ) + catalog_config_path = os.path.join(repo_root, "cosmo_val", "cat_config.yaml") config = yaml.safe_load(Path(catalog_config_path).read_text()) @@ -265,8 +261,7 @@ def test_catalog_paths_exist(self, base_config): print(f" - {v}: missing {nonfunctional[v]}") assert not nonfunctional, ( - "Catalog configuration references missing files: " - f"{dict(nonfunctional)}" + f"Catalog configuration references missing files: {dict(nonfunctional)}" ) def test_seed_variant_updates_shear_path(self, tmp_path): @@ -414,9 +409,9 @@ def _write_synthetic_catalogs( w = rng.uniform(0.5, 1.0, n_gal) shear_path = cat_dir / "shear.fits" - Table( - {"RA": ra, "Dec": dec, "e1": e1, "e2": e2, "w": w} - ).write(shear_path, overwrite=True) + Table({"RA": ra, "Dec": dec, "e1": e1, "e2": e2, "w": w}).write( + shear_path, overwrite=True + ) star_path = cat_dir / "star.fits" Table( @@ -438,9 +433,7 @@ def _write_synthetic_catalogs( # column "z" holds bin edges (n+1), "dn_dz" the densities. z_edges = np.linspace(0.05, 3.0, 31) dndz = np.exp(-(((z_edges - 0.7) / 0.3) ** 2)) - dndz_lines = ["# z dn_dz"] + [ - f"{zz} {nn}" for zz, nn in zip(z_edges, dndz) - ] + dndz_lines = ["# z dn_dz"] + [f"{zz} {nn}" for zz, nn in zip(z_edges, dndz)] (nz_dir / "dndz_SP_A.txt").write_text("\n".join(dndz_lines) + "\n") shear_cfg = { @@ -642,26 +635,46 @@ def test_calculate_pure_eb_runs_on_synthetic_catalog(self, tmp_path): # running calculate_pure_eb with the setup above and printing repr() of # results[key]. Tolerances justified in the docstring. expected = { - "xip_E": np.array([ - 1.6688018692521218e-06, -1.8392317186434428e-05, - 1.4170916007248522e-06, 8.1454486560987474e-06, - 6.2050467269160570e-06, 2.6649478149110497e-06, - ]), - "xim_E": np.array([ - -4.4552381788304276e-05, -1.1082898248960663e-04, - -9.2495668600755951e-05, -5.8456322151105526e-05, - -4.4270469941501174e-05, -2.4236697154723798e-05, - ]), - "xip_B": np.array([ - 1.8508958599700601e-05, 3.8264056862769537e-05, - -1.0482698132038303e-05, -7.3081832089716533e-06, - -9.1621105374021936e-06, -6.4075815485457576e-06, - ]), - "xim_B": np.array([ - -1.1129938750754923e-04, -4.7967477760986883e-05, - -3.4334760596175194e-05, -1.4776328993077835e-05, - -4.0078671892721522e-06, -8.3202301900417799e-07, - ]), + "xip_E": np.array( + [ + 1.6688018692521218e-06, + -1.8392317186434428e-05, + 1.4170916007248522e-06, + 8.1454486560987474e-06, + 6.2050467269160570e-06, + 2.6649478149110497e-06, + ] + ), + "xim_E": np.array( + [ + -4.4552381788304276e-05, + -1.1082898248960663e-04, + -9.2495668600755951e-05, + -5.8456322151105526e-05, + -4.4270469941501174e-05, + -2.4236697154723798e-05, + ] + ), + "xip_B": np.array( + [ + 1.8508958599700601e-05, + 3.8264056862769537e-05, + -1.0482698132038303e-05, + -7.3081832089716533e-06, + -9.1621105374021936e-06, + -6.4075815485457576e-06, + ] + ), + "xim_B": np.array( + [ + -1.1129938750754923e-04, + -4.7967477760986883e-05, + -3.4334760596175194e-05, + -1.4776328993077835e-05, + -4.0078671892721522e-06, + -8.3202301900417799e-07, + ] + ), } for key in ("xip_E", "xim_E", "xip_B", "xim_B"): @@ -672,7 +685,10 @@ def test_calculate_pure_eb_runs_on_synthetic_catalog(self, tmp_path): assert np.all(np.isfinite(vec)), f"{key} not finite" # Value-drift pins -- the deterministic-mode teeth. np.testing.assert_allclose( - vec, expected[key], rtol=1e-6, atol=1e-12, + vec, + expected[key], + rtol=1e-6, + atol=1e-12, err_msg=f"{key} drifted from pinned reference", ) diff --git a/src/sp_validation/tests/test_cosmology.py b/src/sp_validation/tests/test_cosmology.py index 4a61f70a..348f1985 100644 --- a/src/sp_validation/tests/test_cosmology.py +++ b/src/sp_validation/tests/test_cosmology.py @@ -346,9 +346,9 @@ def test_backend_consistency_fast(self, fast_ell_array, fast_redshift_data): rel_diff = np.abs(cl_camb - cl_ccl) / cl_ccl max_rel_diff = rel_diff.max() - assert ( - max_rel_diff < 0.08 - ), f"CCL and CAMB backends differ by >8%: {max_rel_diff:.1%}" + assert max_rel_diff < 0.08, ( + f"CCL and CAMB backends differ by >8%: {max_rel_diff:.1%}" + ) except ImportError: pytest.skip("CAMB not available") diff --git a/src/sp_validation/tests/test_cosmosis_fitting.py b/src/sp_validation/tests/test_cosmosis_fitting.py index d01e6b05..b9625c74 100644 --- a/src/sp_validation/tests/test_cosmosis_fitting.py +++ b/src/sp_validation/tests/test_cosmosis_fitting.py @@ -62,9 +62,7 @@ def _load_module(): try: spec.loader.exec_module(module) except ImportError as exc: # pragma: no cover - container has numpy/astropy - pytest.importorskip( - getattr(exc, "name", "") or "cosmosis_fitting_dependency" - ) + pytest.importorskip(getattr(exc, "name", "") or "cosmosis_fitting_dependency") raise return module @@ -145,9 +143,7 @@ def test_cov_with_tau_block_layout(tmp_path): cov_xi = _write_xi_cov(tmp_path / "cov_xi.txt") cov_tau = _write_tau_cov(tmp_path / "cov_tau.npy") - hdu = cf.covdat_to_fits( - str(cov_xi), filename_cov_tau=str(cov_tau) - ) + hdu = cf.covdat_to_fits(str(cov_xi), filename_cov_tau=str(cov_tau)) # Combined size: xi (2*N_ANG) + truncated tau (2*N_ANG). assert hdu.data.shape == (4 * N_ANG, 4 * N_ANG) @@ -218,9 +214,7 @@ def test_2pt_hdu_columns_and_header(): hdu = cf._create_2pt_hdu(values, theta, "XI_PLUS", "G+R", "G+R") assert hdu.name == "XI_PLUS" - assert set(["BIN1", "BIN2", "ANGBIN", "VALUE", "ANG"]).issubset( - set(hdu.data.names) - ) + assert set(["BIN1", "BIN2", "ANGBIN", "VALUE", "ANG"]).issubset(set(hdu.data.names)) assert np.array_equal(hdu.data["VALUE"], values) assert np.array_equal(hdu.data["ANG"], theta) assert np.array_equal(hdu.data["ANGBIN"], np.arange(1, len(values) + 1)) @@ -367,20 +361,31 @@ def _run_cli(tmp_path, *, use_rho_tau): cmd = [ sys.executable, str(_SCRIPT), - "--cosmosis-root", root, - "--data-dir", str(tmp_path / "chains"), - "--nz-file", str(nz), - "--output-root", str(out_root), - "--template-dir", str(template_dir), - "--xi", str(xip), str(xim), - "--cov-xi", str(cov_xi), + "--cosmosis-root", + root, + "--data-dir", + str(tmp_path / "chains"), + "--nz-file", + str(nz), + "--output-root", + str(out_root), + "--template-dir", + str(template_dir), + "--xi", + str(xip), + str(xim), + "--cov-xi", + str(cov_xi), ] if use_rho_tau: cmd += [ "--use-rho-tau", - "--rho-stats", str(_write_rho_stats(tmp_path / "rho.fits")), - "--tau-stats", str(_write_tau_stats(tmp_path / "tau.fits")), - "--cov-tau", str(_write_tau_cov(tmp_path / "cov_tau.npy")), + "--rho-stats", + str(_write_rho_stats(tmp_path / "rho.fits")), + "--tau-stats", + str(_write_tau_stats(tmp_path / "tau.fits")), + "--cov-tau", + str(_write_tau_cov(tmp_path / "cov_tau.npy")), ] proc = subprocess.run(cmd, capture_output=True, text=True) @@ -457,9 +462,7 @@ def test_cli_rho_tau_hdu_set_and_cov_block(tmp_path): # The tau block is the truncated tau covariance at the bottom-right. tau_trunc = _tau_cov_full()[: 2 * N_ANG, : 2 * N_ANG] - assert np.array_equal( - cov_hdu.data[2 * N_ANG :, 2 * N_ANG :], tau_trunc - ) + assert np.array_equal(cov_hdu.data[2 * N_ANG :, 2 * N_ANG :], tau_trunc) # rho/tau theta is forced onto the xi theta grid (consistency step). assert np.allclose(hdul["RHO_STATS"].data["theta"], _THETA) @@ -496,14 +499,23 @@ def test_cli_perturbed_xi_changes_data_vector(tmp_path): root = "TESTROOT2" proc = subprocess.run( [ - sys.executable, str(_SCRIPT), - "--cosmosis-root", root, - "--data-dir", str(tmp2 / "chains"), - "--nz-file", str(nz), - "--output-root", str(out_root), - "--template-dir", str(template_dir), - "--xi", str(xip), str(xim), - "--cov-xi", str(cov_xi), + sys.executable, + str(_SCRIPT), + "--cosmosis-root", + root, + "--data-dir", + str(tmp2 / "chains"), + "--nz-file", + str(nz), + "--output-root", + str(out_root), + "--template-dir", + str(template_dir), + "--xi", + str(xip), + str(xim), + "--cov-xi", + str(cov_xi), ], capture_output=True, text=True, diff --git a/src/sp_validation/tests/test_galaxy.py b/src/sp_validation/tests/test_galaxy.py index 30f21499..529fd0ad 100644 --- a/src/sp_validation/tests/test_galaxy.py +++ b/src/sp_validation/tests/test_galaxy.py @@ -13,7 +13,6 @@ class GalaxyTestCase(TestCase): - def test_galaxy_imports(self): """Test that the galaxy module imports. diff --git a/src/sp_validation/tests/test_no_stray_outputs.py b/src/sp_validation/tests/test_no_stray_outputs.py index a62434f4..17bc4054 100644 --- a/src/sp_validation/tests/test_no_stray_outputs.py +++ b/src/sp_validation/tests/test_no_stray_outputs.py @@ -19,8 +19,19 @@ # Binary/output extensions that should never be tracked under a scripts/ dir. _OUTPUT_SUFFIXES = { - ".png", ".jpg", ".jpeg", ".gif", ".svg", ".pdf", - ".fits", ".fits.gz", ".npy", ".npz", ".h5", ".hdf5", ".pkl", + ".png", + ".jpg", + ".jpeg", + ".gif", + ".svg", + ".pdf", + ".fits", + ".fits.gz", + ".npy", + ".npz", + ".h5", + ".hdf5", + ".pkl", } _REPO_ROOT = Path(__file__).resolve().parents[3] @@ -31,7 +42,9 @@ def _tracked_files(): try: out = subprocess.run( ["git", "-C", str(_REPO_ROOT), "ls-files"], - capture_output=True, text=True, check=True, + capture_output=True, + text=True, + check=True, ) except (subprocess.CalledProcessError, FileNotFoundError): return None diff --git a/src/sp_validation/tests/test_plots.py b/src/sp_validation/tests/test_plots.py index a14c5c94..8fca6341 100644 --- a/src/sp_validation/tests/test_plots.py +++ b/src/sp_validation/tests/test_plots.py @@ -47,5 +47,5 @@ def test_create_hsp_map_preserves_counts(): counts = counts[~np.isnan(counts)] assert counts.sum() == len(ra) # every object counted once - assert counts.max() == 2 # the coincident pixel - assert len(counts) == 2 # two distinct populated pixels + assert counts.max() == 2 # the coincident pixel + assert len(counts) == 2 # two distinct populated pixels diff --git a/src/sp_validation/tests/test_statistics.py b/src/sp_validation/tests/test_statistics.py index c681d6ab..8e56d040 100644 --- a/src/sp_validation/tests/test_statistics.py +++ b/src/sp_validation/tests/test_statistics.py @@ -21,6 +21,7 @@ jackknif_weighted_average2, ) + def test_jackknif_weighted_average2_mean_and_error(): """Pin the jackknife weighted average + error for a seeded RNG draw. @@ -82,16 +83,12 @@ def test_corr_from_cov_pins_correlation_matrix(): divided by the variances instead of the standard deviations would break the unit-diagonal property and the closed form simultaneously. """ - cov = np.array([[4.0, 1.0, -2.0], - [1.0, 9.0, 3.0], - [-2.0, 3.0, 16.0]]) + cov = np.array([[4.0, 1.0, -2.0], [1.0, 9.0, 3.0], [-2.0, 3.0, 16.0]]) corr = corr_from_cov(cov) npt.assert_allclose( corr, - [[1.0, 1.0 / 6.0, -0.25], - [1.0 / 6.0, 1.0, 0.25], - [-0.25, 0.25, 1.0]], + [[1.0, 1.0 / 6.0, -0.25], [1.0 / 6.0, 1.0, 0.25], [-0.25, 0.25, 1.0]], rtol=1e-12, ) @@ -121,7 +118,7 @@ def test_chi2_and_pte_diagonal_reduces_to_sum_of_squares(): """ d = np.array([1.0, -2.0, 0.5, 3.0]) sigma = np.array([2.0, 1.0, 0.5, 4.0]) - cov_diag = np.diag(sigma ** 2) + cov_diag = np.diag(sigma**2) chi2, reduced_chi2, pte = chi2_and_pte(d, cov_diag) @@ -138,10 +135,14 @@ def test_chi2_and_pte_diagonal_reduces_to_sum_of_squares(): # TEETH: off-diagonal covariance terms change the quadratic form, so the # full d^T C^-1 d path (not just the diagonal shortcut) is load-bearing. - cov_full = np.array([[4.0, 1.0, 0.0, 0.0], - [1.0, 1.0, 0.2, 0.0], - [0.0, 0.2, 0.25, 0.1], - [0.0, 0.0, 0.1, 16.0]]) + cov_full = np.array( + [ + [4.0, 1.0, 0.0, 0.0], + [1.0, 1.0, 0.2, 0.0], + [0.0, 0.2, 0.25, 0.1], + [0.0, 0.0, 0.1, 16.0], + ] + ) chi2_full, _, pte_full = chi2_and_pte(d, cov_full) npt.assert_allclose(chi2_full, 13.526036131774706, rtol=1e-12) npt.assert_allclose(pte_full, 0.00897199803545734, rtol=1e-12) diff --git a/src/sp_validation/tests/test_survey.py b/src/sp_validation/tests/test_survey.py index 9e7f660d..f42f884c 100644 --- a/src/sp_validation/tests/test_survey.py +++ b/src/sp_validation/tests/test_survey.py @@ -16,14 +16,11 @@ class SurveyTestCase(TestCase): - def setUp(self): - self._dd = np.array([ - (270.283, 1), - (270.283, 0), - (188.308, 0)], - dtype=[('TILE_ID', 'f8'), ('FLAGS', 'i2')] + self._dd = np.array( + [(270.283, 1), (270.283, 0), (188.308, 0)], + dtype=[("TILE_ID", "f8"), ("FLAGS", "i2")], ) self._area_tile = 0.5 self._verbose = False @@ -33,7 +30,7 @@ def setUp(self): self._ra = np.array([240.0]) self._dec = np.array([32.0]) - self._patch = ['P5'] + self._patch = ["P5"] def tearDown(self): @@ -42,15 +39,11 @@ def tearDown(self): self.number_int = None def test_get_area(self): - """Test ``sp_validation.survey_get_area`` method. - - """ + """Test ``sp_validation.survey_get_area`` method.""" # Test return values area_deg2, area_amin2, tile_IDs = survey.get_area( - self._dd, - self._area_tile, - self._verbose + self._dd, self._area_tile, self._verbose ) npt.assert_almost_equal( area_deg2, @@ -62,13 +55,11 @@ def test_get_area(self): ) self.assertTrue( sorted(tile_IDs) == sorted(self._tile_IDs), - msg=f'{tile_IDs}!={self._tile_IDs}' + msg=f"{tile_IDs}!={self._tile_IDs}", ) def test_get_footprint(self): - """Test ``sp_validation.survey_get_footprint`` method. - - """ + """Test ``sp_validation.survey_get_footprint`` method.""" for patch in self._patch: coords = survey.get_footprint(patch, self._ra, self._dec) self.assertTrue(coords[0]) diff --git a/workflow/common.py b/workflow/common.py index fbaeabd0..f86390e9 100644 --- a/workflow/common.py +++ b/workflow/common.py @@ -4,16 +4,9 @@ import re from pathlib import Path -COSMO_VAL = Path( - "/n17data/cdaley/unions/code/sp_validation/cosmo_val/output" -) -COSMO_INFERENCE = Path( - "/n17data/cdaley/unions/code/sp_validation/cosmo_inference" -) -CAT_CONFIG = ( - "/n17data/cdaley/unions/code/sp_validation/cosmo_val/" - "cat_config.yaml" -) +COSMO_VAL = Path("/n17data/cdaley/unions/code/sp_validation/cosmo_val/output") +COSMO_INFERENCE = Path("/n17data/cdaley/unions/code/sp_validation/cosmo_inference") +CAT_CONFIG = "/n17data/cdaley/unions/code/sp_validation/cosmo_val/cat_config.yaml" BLINDS = ["A", "B", "C"] BLOCK_PAIRS = [("++", "1"), ("--", "2"), ("+-", "3")] @@ -93,9 +86,7 @@ def covariance_base( mask_suffix if mask_suffix is not None else ( - DEFAULT_MASK_SUFFIX - if default_mask_suffix is None - else default_mask_suffix + DEFAULT_MASK_SUFFIX if default_mask_suffix is None else default_mask_suffix ) ) cov_version = resolve_covariance_version(version) if resolve_version else version @@ -162,8 +153,7 @@ def build_redshift_path(version, blind): base_version = "SP_v1.4.6" version_dir = base_version.replace("SP_", "") return ( - f"/n17data/sguerrini/UNIONS/WL/nz/{version_dir}/" - f"nz_{base_version}_{blind}.txt" + f"/n17data/sguerrini/UNIONS/WL/nz/{version_dir}/nz_{base_version}_{blind}.txt" ) diff --git a/workflow/scripts/analyze_mask_power_spectrum.py b/workflow/scripts/analyze_mask_power_spectrum.py index 037287e4..2e0e5b28 100644 --- a/workflow/scripts/analyze_mask_power_spectrum.py +++ b/workflow/scripts/analyze_mask_power_spectrum.py @@ -4,11 +4,11 @@ Loads downgraded mask, computes C_ell using healpy.anafast, exports for CosmoCov. """ -from datetime import datetime import os +from datetime import datetime -import numpy as np import healpy as hp +import numpy as np def load_healpix_mask(mask_path: str, verbose: bool = True) -> np.ndarray: @@ -61,7 +61,7 @@ def export_power_spectrum( f"# UNIONS mask power spectrum for nside={nside}\n" f"# Generated by analyze_mask_power_spectrum.py on {datetime.now().strftime('%Y-%m-%d')}\n" f"# Format: ell C_ell\n" - f"# lmax = {len(cl)-1}" + f"# lmax = {len(cl) - 1}" ) np.savetxt(output_path, np.column_stack([ell, cl]), fmt="%d %.12e", header=header) diff --git a/workflow/scripts/compute_glass_mock_covariance.py b/workflow/scripts/compute_glass_mock_covariance.py index cb1f80fe..6798ac8c 100644 --- a/workflow/scripts/compute_glass_mock_covariance.py +++ b/workflow/scripts/compute_glass_mock_covariance.py @@ -73,7 +73,9 @@ def covariance_to_correlation(covariance: np.ndarray) -> np.ndarray: diag = np.sqrt(np.diag(covariance)) if np.any(diag == 0): - raise ValueError("Encountered zero variance while constructing correlation matrix") + raise ValueError( + "Encountered zero variance while constructing correlation matrix" + ) corr = covariance / np.outer(diag, diag) np.clip(corr, -1.0, 1.0, out=corr) return corr @@ -105,7 +107,10 @@ def plot_correlation( ax.axhline(boundary, color="white", linewidth=1.2) ax.axvline(boundary, color="white", linewidth=1.2) - centers = [0.5 * (start + end) for start, end in zip([0] + list(boundaries[:-1]), boundaries)] + centers = [ + 0.5 * (start + end) + for start, end in zip([0] + list(boundaries[:-1]), boundaries) + ] labels = [r"$\xi_+$", r"$\xi_-$", r"$C_\ell^{EE}$", r"$C_\ell^{BB}$"] ax.set_xticks(centers) ax.set_xticklabels(labels, rotation=45, ha="right") @@ -125,22 +130,63 @@ def collect_mock_files(paths: list[Path]) -> dict[str, Path]: for path in sorted(paths): mock_id = extract_mock_id(path) if mock_id in files: - raise ValueError(f"Duplicate mock id {mock_id}: {path} and {files[mock_id]}") + raise ValueError( + f"Duplicate mock id {mock_id}: {path} and {files[mock_id]}" + ) files[mock_id] = path return files def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--xi-files", type=Path, nargs="+", required=True, help="Paths to xi mocks") - parser.add_argument("--cl-files", type=Path, nargs="+", required=True, help="Paths to C_ell mocks") - parser.add_argument("--xi-covariance", type=Path, required=True, help="Output path for xi-only covariance (npy)") - parser.add_argument("--cl-covariance", type=Path, required=True, help="Output path for cl-only covariance (npy)") - parser.add_argument("--combined-covariance", type=Path, required=True, help="Output path for combined covariance (npy)") - parser.add_argument("--combined-correlation-plot", type=Path, required=True, help="Output path for combined correlation heatmap (png)") - parser.add_argument("--xi-mean", type=Path, required=True, help="Output path for xi mean vector (npy)") - parser.add_argument("--cl-mean", type=Path, required=True, help="Output path for cl mean vector (npy)") - parser.add_argument("--combined-mean", type=Path, required=True, help="Output path for combined mean vector (npy)") + parser.add_argument( + "--xi-files", type=Path, nargs="+", required=True, help="Paths to xi mocks" + ) + parser.add_argument( + "--cl-files", type=Path, nargs="+", required=True, help="Paths to C_ell mocks" + ) + parser.add_argument( + "--xi-covariance", + type=Path, + required=True, + help="Output path for xi-only covariance (npy)", + ) + parser.add_argument( + "--cl-covariance", + type=Path, + required=True, + help="Output path for cl-only covariance (npy)", + ) + parser.add_argument( + "--combined-covariance", + type=Path, + required=True, + help="Output path for combined covariance (npy)", + ) + parser.add_argument( + "--combined-correlation-plot", + type=Path, + required=True, + help="Output path for combined correlation heatmap (png)", + ) + parser.add_argument( + "--xi-mean", + type=Path, + required=True, + help="Output path for xi mean vector (npy)", + ) + parser.add_argument( + "--cl-mean", + type=Path, + required=True, + help="Output path for cl mean vector (npy)", + ) + parser.add_argument( + "--combined-mean", + type=Path, + required=True, + help="Output path for combined mean vector (npy)", + ) return parser.parse_args() @@ -201,5 +247,6 @@ def main() -> None: correlation = covariance_to_correlation(combined_covariance) plot_correlation(correlation, block_lengths, correlation_plot_path) + if __name__ == "__main__": main() diff --git a/workflow/scripts/cv_additive_bias.py b/workflow/scripts/cv_additive_bias.py index 3ab8e70b..3fdec487 100644 --- a/workflow/scripts/cv_additive_bias.py +++ b/workflow/scripts/cv_additive_bias.py @@ -15,7 +15,9 @@ _unbuffer_streams() cv = make_cv(snakemake) cv.calculate_additive_bias() -additive_bias = {ver: {"c1": float(cv.c1[ver]), "c2": float(cv.c2[ver])} for ver in cv.versions} +additive_bias = { + ver: {"c1": float(cv.c1[ver]), "c2": float(cv.c2[ver])} for ver in cv.versions +} with open(snakemake.output["additive_bias"], "w") as f: json.dump(additive_bias, f, indent=2) verify_outputs(snakemake) diff --git a/workflow/scripts/generate_glass_mock_rhotau_samples.py b/workflow/scripts/generate_glass_mock_rhotau_samples.py index 5425bfcb..63d99095 100644 --- a/workflow/scripts/generate_glass_mock_rhotau_samples.py +++ b/workflow/scripts/generate_glass_mock_rhotau_samples.py @@ -10,8 +10,9 @@ """ import argparse -import numpy as np from pathlib import Path + +import numpy as np from astropy.io import fits @@ -39,37 +40,40 @@ def create_tau_fits(sampled_values, theta, header=None): # Create columns columns = [] - columns.append(fits.Column(name='theta', format='D', array=theta)) + columns.append(fits.Column(name="theta", format="D", array=theta)) # tau_0_p - columns.append(fits.Column(name='tau_0_p', format='D', array=tau_0_p)) - columns.append(fits.Column(name='vartau_0_p', format='D', - array=np.abs(tau_0_p) * 0.1)) + columns.append(fits.Column(name="tau_0_p", format="D", array=tau_0_p)) + columns.append( + fits.Column(name="vartau_0_p", format="D", array=np.abs(tau_0_p) * 0.1) + ) # tau_0_m - columns.append(fits.Column(name='tau_0_m', format='D', - array=np.zeros(nbins))) - columns.append(fits.Column(name='vartau_0_m', format='D', - array=np.ones(nbins) * 1e-15)) + columns.append(fits.Column(name="tau_0_m", format="D", array=np.zeros(nbins))) + columns.append( + fits.Column(name="vartau_0_m", format="D", array=np.ones(nbins) * 1e-15) + ) # tau_2_p - columns.append(fits.Column(name='tau_2_p', format='D', array=tau_2_p)) - columns.append(fits.Column(name='vartau_2_p', format='D', - array=np.abs(tau_2_p) * 0.1)) + columns.append(fits.Column(name="tau_2_p", format="D", array=tau_2_p)) + columns.append( + fits.Column(name="vartau_2_p", format="D", array=np.abs(tau_2_p) * 0.1) + ) # tau_2_m - columns.append(fits.Column(name='tau_2_m', format='D', - array=np.zeros(nbins))) - columns.append(fits.Column(name='vartau_2_m', format='D', - array=np.ones(nbins) * 1e-15)) + columns.append(fits.Column(name="tau_2_m", format="D", array=np.zeros(nbins))) + columns.append( + fits.Column(name="vartau_2_m", format="D", array=np.ones(nbins) * 1e-15) + ) # tau_5_p - columns.append(fits.Column(name='tau_5_p', format='D', array=tau_5_p)) - columns.append(fits.Column(name='vartau_5_p', format='D', - array=np.abs(tau_5_p) * 0.1)) + columns.append(fits.Column(name="tau_5_p", format="D", array=tau_5_p)) + columns.append( + fits.Column(name="vartau_5_p", format="D", array=np.abs(tau_5_p) * 0.1) + ) # tau_5_m - columns.append(fits.Column(name='tau_5_m', format='D', - array=np.zeros(nbins))) - columns.append(fits.Column(name='vartau_5_m', format='D', - array=np.ones(nbins) * 1e-15)) + columns.append(fits.Column(name="tau_5_m", format="D", array=np.zeros(nbins))) + columns.append( + fits.Column(name="vartau_5_m", format="D", array=np.ones(nbins) * 1e-15) + ) # Create HDU coldefs = fits.ColDefs(columns) @@ -96,10 +100,7 @@ def generate_samples_for_mock(mock_id, cov_tau, theta, ref_tau_header, output_di rng = np.random.default_rng(int(mock_id)) # Sample tau statistics from N(0, Cov_tau) - tau_samples = rng.multivariate_normal( - mean=np.zeros(cov_tau.shape[0]), - cov=cov_tau - ) + tau_samples = rng.multivariate_normal(mean=np.zeros(cov_tau.shape[0]), cov=cov_tau) # Create FITS file tau_hdu = create_tau_fits(tau_samples, theta, ref_tau_header) @@ -119,14 +120,30 @@ def main(): parser = argparse.ArgumentParser( description="Generate zero-mean tau samples for GLASS mocks" ) - parser.add_argument("--cov-tau", type=str, required=True, - help="Path to tau covariance matrix (.npy file)") - parser.add_argument("--ref-tau", type=str, required=True, - help="Reference tau FITS file for structure") - parser.add_argument("--output-dir", type=str, required=True, - help="Output directory for sampled FITS files") - parser.add_argument("--mock-ids", type=str, default="00001-00350", - help="Range of mock IDs (format: 00001-00350)") + parser.add_argument( + "--cov-tau", + type=str, + required=True, + help="Path to tau covariance matrix (.npy file)", + ) + parser.add_argument( + "--ref-tau", + type=str, + required=True, + help="Reference tau FITS file for structure", + ) + parser.add_argument( + "--output-dir", + type=str, + required=True, + help="Output directory for sampled FITS files", + ) + parser.add_argument( + "--mock-ids", + type=str, + default="00001-00350", + help="Range of mock IDs (format: 00001-00350)", + ) args = parser.parse_args() # Load tau covariance and reference @@ -136,16 +153,27 @@ def main(): print("Loading reference FITS...") ref_tau_data, ref_tau_header = load_reference_fits(args.ref_tau) - theta = ref_tau_data['theta'] - print(f" theta range: {theta.min():.3f} - {theta.max():.3f} arcmin, nbins: {len(theta)}") + theta = ref_tau_data["theta"] + print( + f" theta range: {theta.min():.3f} - {theta.max():.3f} arcmin, nbins: {len(theta)}" + ) # Parse mock ID range - mock_ids = (list(range(int(args.mock_ids.split("-")[0]), int(args.mock_ids.split("-")[1]) + 1)) - if "-" in args.mock_ids else [int(args.mock_ids)]) + mock_ids = ( + list( + range( + int(args.mock_ids.split("-")[0]), int(args.mock_ids.split("-")[1]) + 1 + ) + ) + if "-" in args.mock_ids + else [int(args.mock_ids)] + ) print(f"Generating tau samples for {len(mock_ids)} mocks...") for mock_id in mock_ids: - msg = generate_samples_for_mock(mock_id, cov_tau, theta, ref_tau_header, args.output_dir) + msg = generate_samples_for_mock( + mock_id, cov_tau, theta, ref_tau_header, args.output_dir + ) print(msg) print("Done!") diff --git a/workflow/scripts/generate_pseudo_cl.py b/workflow/scripts/generate_pseudo_cl.py index 1c86fafc..e43214ce 100644 --- a/workflow/scripts/generate_pseudo_cl.py +++ b/workflow/scripts/generate_pseudo_cl.py @@ -66,14 +66,16 @@ def generate_pseudo_cl( else: bin_str = f"nbins={nbins}, power={power}" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"Generating pseudo-Cl for {version}{blind_str}") print(f"Binning: {binning} ({bin_str})") if cosmo_params: - print(f"Cosmology: Om={cosmo_params.get('Omega_m')}, s8={cosmo_params.get('sigma_8')}") + print( + f"Cosmology: Om={cosmo_params.get('Omega_m')}, s8={cosmo_params.get('sigma_8')}" + ) else: print("Cosmology: Planck 2018 defaults") - print(f"{'='*60}\n") + print(f"{'=' * 60}\n") # Remap config cosmology param names to get_cosmo() expected names if cosmo_params: diff --git a/workflow/scripts/generate_pseudo_cl_cov.py b/workflow/scripts/generate_pseudo_cl_cov.py index 04c8ebe6..89cc3fe4 100644 --- a/workflow/scripts/generate_pseudo_cl_cov.py +++ b/workflow/scripts/generate_pseudo_cl_cov.py @@ -65,14 +65,16 @@ def generate_pseudo_cl_cov( else: bin_str = f"nbins={nbins}, power={power}" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"Generating pseudo-Cl covariance for {version}{blind_str}") print(f"Binning: {binning} ({bin_str})") if cosmo_params: - print(f"Cosmology: Om={cosmo_params.get('Omega_m')}, s8={cosmo_params.get('sigma_8')}") + print( + f"Cosmology: Om={cosmo_params.get('Omega_m')}, s8={cosmo_params.get('sigma_8')}" + ) else: print("Cosmology: Planck 2018 defaults") - print(f"{'='*60}\n") + print(f"{'=' * 60}\n") # Remap config cosmology param names to get_cosmo() expected names if cosmo_params: diff --git a/workflow/scripts/mock_cosebis_bias_test.py b/workflow/scripts/mock_cosebis_bias_test.py index e9ac8599..75593ae4 100644 --- a/workflow/scripts/mock_cosebis_bias_test.py +++ b/workflow/scripts/mock_cosebis_bias_test.py @@ -16,13 +16,12 @@ import numpy as np import seaborn as sns import treecorr +from cosmo_numba.B_modes.cosebis import COSEBIS +from plotting_utils import FIG_WIDTH_SINGLE, PAPER_MPLSTYLE from scipy.stats import chi2 as chi2_dist -from cosmo_numba.B_modes.cosebis import COSEBIS from sp_validation.b_modes import scale_cut_to_bins -from plotting_utils import FIG_WIDTH_SINGLE, PAPER_MPLSTYLE - plt.style.use(PAPER_MPLSTYLE) # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ @@ -53,7 +52,9 @@ # Analytic COSEBIS covariance from ξ± covariance # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -gg_ref = treecorr.GGCorrelation(min_sep=0.5, max_sep=500, nbins=1000, sep_units="arcmin") +gg_ref = treecorr.GGCorrelation( + min_sep=0.5, max_sep=500, nbins=1000, sep_units="arcmin" +) gg_ref.read(xi_ref_path) start, stop = scale_cut_to_bins(gg_ref, theta_min, theta_max) @@ -104,7 +105,9 @@ color = sns.color_palette("husl", 4)[0] fig, (ax_bn, ax_ratio) = plt.subplots( - 2, 1, figsize=(FIG_WIDTH_SINGLE, 3.5), + 2, + 1, + figsize=(FIG_WIDTH_SINGLE, 3.5), gridspec_kw={"height_ratios": [3, 1], "hspace": 0.08}, sharex=True, ) @@ -113,20 +116,31 @@ ax_bn.axhspan(-2, 2, color="0.92", alpha=0.5, zorder=0, label=r"$\pm 2\sigma$") for i in range(n_mocks): - ax_bn.plot(modes, all_Bn[i] / sigma_analytic, "o", color=color, - ms=2, alpha=0.15, zorder=1) + ax_bn.plot( + modes, all_Bn[i] / sigma_analytic, "o", color=color, ms=2, alpha=0.15, zorder=1 + ) ax_bn.errorbar( - modes, mean_Bn / sigma_analytic, + modes, + mean_Bn / sigma_analytic, yerr=np.full(nmodes, 1.0 / np.sqrt(n_mocks)), - fmt="o", color=color, ms=4, lw=0.8, capsize=2, zorder=5, + fmt="o", + color=color, + ms=4, + lw=0.8, + capsize=2, + zorder=5, label=rf"Mean ($N={n_mocks}$)", ) ax_bn.text( - 0.98, 0.95, + 0.98, + 0.95, rf"$\chi^2/{nmodes} = {chi2_val:.1f}$, PTE $= {pte:.2f}$", - transform=ax_bn.transAxes, fontsize=7, ha="right", va="top", + transform=ax_bn.transAxes, + fontsize=7, + ha="right", + va="top", ) ax_bn.set_ylabel(r"$B_n / \sigma_n$") @@ -142,7 +156,8 @@ fig.suptitle( rf"COSEBIS $B_n$ bias test: {n_mocks} zero-B GLASS mocks", - fontsize=8, y=0.98, + fontsize=8, + y=0.98, ) Path(figure_path).parent.mkdir(parents=True, exist_ok=True) diff --git a/workflow/scripts/mock_cosebis_scatter.py b/workflow/scripts/mock_cosebis_scatter.py index b04b2695..4da0c659 100644 --- a/workflow/scripts/mock_cosebis_scatter.py +++ b/workflow/scripts/mock_cosebis_scatter.py @@ -2,8 +2,8 @@ import numpy as np import treecorr - from cosmo_numba.B_modes.cosebis import COSEBIS + from sp_validation.b_modes import scale_cut_to_bins xi_path = snakemake.input.xi @@ -25,6 +25,13 @@ cosebis_obj = COSEBIS(np.min(theta_cut), np.max(theta_cut), nmodes, precision=120) En, Bn = cosebis_obj.cosebis_from_xipm(theta_cut, xip_cut, xim_cut, parallel=True) -np.savez(out_path, En=En, Bn=Bn, theta_min_actual=np.min(theta_cut), - theta_max_actual=np.max(theta_cut)) -print(f"Saved {out_path}: {nmodes} modes, θ=[{np.min(theta_cut):.2f}, {np.max(theta_cut):.2f}]") +np.savez( + out_path, + En=En, + Bn=Bn, + theta_min_actual=np.min(theta_cut), + theta_max_actual=np.max(theta_cut), +) +print( + f"Saved {out_path}: {nmodes} modes, θ=[{np.min(theta_cut):.2f}, {np.max(theta_cut):.2f}]" +) diff --git a/workflow/scripts/plotting_utils.py b/workflow/scripts/plotting_utils.py index 7a6234d0..05579600 100644 --- a/workflow/scripts/plotting_utils.py +++ b/workflow/scripts/plotting_utils.py @@ -3,13 +3,12 @@ import matplotlib.scale as mscale import matplotlib.ticker as ticker import matplotlib.transforms as mtransforms -from matplotlib.colors import BoundaryNorm, ListedColormap -from matplotlib.patches import Rectangle import numpy as np import seaborn as sns +from matplotlib.colors import BoundaryNorm, ListedColormap +from matplotlib.patches import Rectangle from scipy import stats - # Shared constants PAPER_MPLSTYLE = "/n17data/cdaley/unions/pure_eb/code/sp_validation/cosmo_inference/notebooks/2D_cosmic_shear_paper_plots/config/paper.mplstyle" FIG_WIDTH_FULL = 7.24 # Two-column A&A format (textwidth) @@ -39,8 +38,9 @@ PTE_COLORBAR_TICKLABELS = ["0", "0.05", "0.5", "0.95", "1"] -def make_pte_colormap(low=0.05, high=0.95, gradient_range=(0.15, 0.85), - n_gradient=PTE_GRADIENT_LEVELS): +def make_pte_colormap( + low=0.05, high=0.95, gradient_range=(0.15, 0.85), n_gradient=PTE_GRADIENT_LEVELS +): """Create a discrete PTE colormap with exact threshold breaks. Values below `low` and above `high` remain solid colors. The interval @@ -51,9 +51,7 @@ def make_pte_colormap(low=0.05, high=0.95, gradient_range=(0.15, 0.85), solid_blue = vlag(0.0) solid_red = vlag(1.0) g_lo, g_hi = gradient_range - gradient_colors = [ - vlag(val) for val in np.linspace(g_lo, g_hi, n_gradient) - ] + gradient_colors = [vlag(val) for val in np.linspace(g_lo, g_hi, n_gradient)] cmap = ListedColormap( [solid_blue] + gradient_colors + [solid_red], name="pte_threshold", @@ -64,11 +62,13 @@ def make_pte_colormap(low=0.05, high=0.95, gradient_range=(0.15, 0.85), def make_pte_norm(low=0.05, high=0.95, n_gradient=PTE_GRADIENT_LEVELS): """Create exact threshold normalization for PTE heatmaps.""" - boundaries = np.concatenate(( - [0.0], - np.linspace(low, high, n_gradient + 1), - [1.0], - )) + boundaries = np.concatenate( + ( + [0.0], + np.linspace(low, high, n_gradient + 1), + [1.0], + ) + ) return BoundaryNorm(boundaries, ncolors=n_gradient + 2, clip=True) @@ -142,9 +142,7 @@ def get_powspace_bin_edges(ell_eff, lmin=8, lmax=2048, power=0.5): ells = np.arange(lmin, lmax + 1) start = np.power(lmin, power) end = np.power(lmax, power) - bins_ell = np.power( - np.linspace(start, end, n_ell_bins + 1), 1 / power - ) + bins_ell = np.power(np.linspace(start, end, n_ell_bins + 1), 1 / power) bpws = np.digitize(ells.astype(float), bins_ell) - 1 bpws[0] = 0 bpws[-1] = n_ell_bins - 1 @@ -263,7 +261,9 @@ def version_label(version, version_labels): str Human-readable label, or cleaned version string if not in mapping. """ - return version_labels.get(version, version.replace("SP_", "").replace("_leak_corr", "")) + return version_labels.get( + version, version.replace("SP_", "").replace("_leak_corr", "") + ) def find_fiducial_index(datasets, fiducial_version, key="version"): @@ -283,10 +283,7 @@ def find_fiducial_index(datasets, fiducial_version, key="version"): int Index of fiducial version, or 0 if not found. """ - return next( - (i for i, d in enumerate(datasets) if d[key] == fiducial_version), - 0 - ) + return next((i for i, d in enumerate(datasets) if d[key] == fiducial_version), 0) def get_version_alpha(version, fiducial_version, plotting_config): @@ -330,9 +327,15 @@ def get_box_style(box_style=None): style = box_style or {} return { "edge_color": style.get("edge_color", VERSION_BOX_DEFAULTS["edge_color"]), - "edge_linewidth": style.get("edge_linewidth", VERSION_BOX_DEFAULTS["edge_linewidth"]), - "fiducial_line_color": style.get("fiducial_line_color", VERSION_BOX_DEFAULTS["fiducial_line_color"]), - "fiducial_line_width": style.get("fiducial_line_width", VERSION_BOX_DEFAULTS["fiducial_line_width"]), + "edge_linewidth": style.get( + "edge_linewidth", VERSION_BOX_DEFAULTS["edge_linewidth"] + ), + "fiducial_line_color": style.get( + "fiducial_line_color", VERSION_BOX_DEFAULTS["fiducial_line_color"] + ), + "fiducial_line_width": style.get( + "fiducial_line_width", VERSION_BOX_DEFAULTS["fiducial_line_width"] + ), } @@ -363,7 +366,7 @@ def draw_normalized_version_box(ax, x_left, x_right, y_vals, fiducial_val, style (x_left, box_bottom), x_right - x_left, box_top - box_bottom, - facecolor='none', + facecolor="none", edgecolor=style["edge_color"], linewidth=style["edge_linewidth"], zorder=1, @@ -371,15 +374,18 @@ def draw_normalized_version_box(ax, x_left, x_right, y_vals, fiducial_val, style ax.add_patch(rect) ax.hlines( - fiducial_val, x_left, x_right, + fiducial_val, + x_left, + x_right, colors=style["fiducial_line_color"], linewidth=style["fiducial_line_width"], - zorder=1 + zorder=1, ) -def draw_normalized_boxes_log_scale(ax, x_centers, datasets, y_norm_key, fiducial_idx, - x_offset_range, box_style=None): +def draw_normalized_boxes_log_scale( + ax, x_centers, datasets, y_norm_key, fiducial_idx, x_offset_range, box_style=None +): """Draw version boxes for log-scale x-axis (e.g., theta). For each bin, draws a box spanning the range of values across versions, @@ -413,11 +419,14 @@ def draw_normalized_boxes_log_scale(ax, x_centers, datasets, y_norm_key, fiducia y_vals = np.array([data[y_norm_key][i] for data in datasets]) x_left = x_i * box_left_factor x_right = x_i * box_right_factor - draw_normalized_version_box(ax, x_left, x_right, y_vals, y_vals[fiducial_idx], style) + draw_normalized_version_box( + ax, x_left, x_right, y_vals, y_vals[fiducial_idx], style + ) -def draw_normalized_boxes_linear_scale(ax, x_centers, datasets, y_norm_key, fiducial_idx, - x_offsets, box_style=None): +def draw_normalized_boxes_linear_scale( + ax, x_centers, datasets, y_norm_key, fiducial_idx, x_offsets, box_style=None +): """Draw version boxes for linear-scale x-axis (e.g., COSEBIS modes). For each bin, draws a box spanning the range of values across versions, @@ -449,11 +458,22 @@ def draw_normalized_boxes_linear_scale(ax, x_centers, datasets, y_norm_key, fidu y_vals = np.array([data[y_norm_key][i] for data in datasets]) x_left = x_i - box_half_width x_right = x_i + box_half_width - draw_normalized_version_box(ax, x_left, x_right, y_vals, y_vals[fiducial_idx], style) - - -def draw_normalized_boxes_ell_scale(ax, ell, ell_widths, datasets, y_norm_key, fiducial_idx, - jitter_fraction, n_versions, box_style=None): + draw_normalized_version_box( + ax, x_left, x_right, y_vals, y_vals[fiducial_idx], style + ) + + +def draw_normalized_boxes_ell_scale( + ax, + ell, + ell_widths, + datasets, + y_norm_key, + fiducial_idx, + jitter_fraction, + n_versions, + box_style=None, +): """Draw version boxes for ell-space plots with variable bin widths. For each multipole bin, draws a box spanning the range of values across @@ -489,7 +509,9 @@ def draw_normalized_boxes_ell_scale(ax, ell, ell_widths, datasets, y_norm_key, f half_width = ell_widths[i] * box_half_width_factor x_left = ell_i - half_width x_right = ell_i + half_width - draw_normalized_version_box(ax, x_left, x_right, y_vals, y_vals[fiducial_idx], style) + draw_normalized_version_box( + ax, x_left, x_right, y_vals, y_vals[fiducial_idx], style + ) def extract_version_number(version_string): @@ -511,7 +533,8 @@ def extract_version_number(version_string): Short version number (e.g., "v1.4.5"). """ import re - match = re.search(r'(v[\d.]+)', version_string) + + match = re.search(r"(v[\d.]+)", version_string) return match.group(1) if match else version_string diff --git a/workflow/scripts/process_mask.py b/workflow/scripts/process_mask.py index ff37a8fe..c0368147 100644 --- a/workflow/scripts/process_mask.py +++ b/workflow/scripts/process_mask.py @@ -11,12 +11,12 @@ Author: Claude Code """ -from datetime import datetime import os +from datetime import datetime -import numpy as np import healpy as hp import healsparse as hsp +import numpy as np import yaml @@ -24,38 +24,42 @@ def load_healsparse_mask(mask_path: str, verbose: bool = True) -> hsp.HealSparse """Load HealSparse mask from file.""" if verbose: print(f"Loading HealSparse mask from: {mask_path}") - + if not os.path.exists(mask_path): raise FileNotFoundError(f"Mask file not found: {mask_path}") - + mask = hsp.HealSparseMap.read(mask_path) - + if verbose: - print(f"Loaded mask with nside_coverage={mask.nside_coverage}, nside_sparse={mask.nside_sparse}") + print( + f"Loaded mask with nside_coverage={mask.nside_coverage}, nside_sparse={mask.nside_sparse}" + ) print(f"Map size: {mask.valid_pixels.sum()} valid pixels") - + return mask -def degrade_mask(mask: hsp.HealSparseMap, target_nside: int, verbose: bool = True) -> np.ndarray: +def degrade_mask( + mask: hsp.HealSparseMap, target_nside: int, verbose: bool = True +) -> np.ndarray: """Degrade HealSparse mask to target nside and return as HEALPix array.""" if verbose: print(f"Degrading mask to nside={target_nside}") - + # Degrade the mask - degraded_mask = mask.degrade(target_nside, reduction='mean') - + degraded_mask = mask.degrade(target_nside, reduction="mean") + # Convert to full HEALPix array healpix_mask = np.zeros(hp.nside2npix(target_nside)) valid_pixels = degraded_mask.valid_pixels healpix_mask[valid_pixels] = degraded_mask[valid_pixels] - + if verbose: coverage_fraction = np.sum(healpix_mask > 0) / len(healpix_mask) print(f"Coverage fraction: {coverage_fraction:.4f}") mean_value = np.mean(healpix_mask[healpix_mask > 0]) print(f"Mean mask value (valid pixels): {mean_value:.4f}") - + return healpix_mask @@ -63,32 +67,41 @@ def calculate_effective_area(healpix_mask: np.ndarray, nside: int) -> float: """Calculate effective survey area in square degrees.""" # HEALPix pixel area in steradians pixel_area_sr = hp.nside2pixarea(nside) - + # Convert to square degrees (1 steradian = (180/π)² square degrees) pixel_area_deg2 = pixel_area_sr * (180.0 / np.pi) ** 2 - + # Sum weighted area effective_area = np.sum(healpix_mask) * pixel_area_deg2 - + return effective_area -def save_healpix_mask(healpix_mask: np.ndarray, output_path: str, nside: int, - nest: bool = False, verbose: bool = True): +def save_healpix_mask( + healpix_mask: np.ndarray, + output_path: str, + nside: int, + nest: bool = False, + verbose: bool = True, +): """Save HEALPix mask to FITS file.""" if verbose: print(f"Saving HEALPix mask to: {output_path}") - + # Ensure output directory exists os.makedirs(os.path.dirname(output_path), exist_ok=True) - + # Write map - hp.write_map(output_path, healpix_mask, nest=nest, overwrite=True, - dtype=np.float64) + hp.write_map(output_path, healpix_mask, nest=nest, overwrite=True, dtype=np.float64) -def save_area_summary(nside: int, effective_area: float, healpix_mask: np.ndarray, - output_path: str, mask_output_path: str): +def save_area_summary( + nside: int, + effective_area: float, + healpix_mask: np.ndarray, + output_path: str, + mask_output_path: str, +): """Save area summary for single nside to YAML file.""" summary = { "mask_processing_summary": { @@ -98,22 +111,28 @@ def save_area_summary(nside: int, effective_area: float, healpix_mask: np.ndarra f"nside_{nside}": { "nside": int(nside), "effective_area_deg2": float(effective_area), - "coverage_fraction": float(np.sum(healpix_mask > 0) / len(healpix_mask)), + "coverage_fraction": float( + np.sum(healpix_mask > 0) / len(healpix_mask) + ), "n_valid_pixels": int(np.sum(healpix_mask > 0)), "total_pixels": int(len(healpix_mask)), - "mean_mask_value": float(np.mean(healpix_mask[healpix_mask > 0]) if np.sum(healpix_mask > 0) > 0 else 0.0), - "output_file": mask_output_path + "mean_mask_value": float( + np.mean(healpix_mask[healpix_mask > 0]) + if np.sum(healpix_mask > 0) > 0 + else 0.0 + ), + "output_file": mask_output_path, } - } + }, } } - + # Ensure output directory exists os.makedirs(os.path.dirname(output_path), exist_ok=True) - - with open(output_path, 'w') as f: + + with open(output_path, "w") as f: yaml.dump(summary, f, default_flow_style=False, sort_keys=True) - + print(f"Saved area summary to: {output_path}") @@ -127,31 +146,36 @@ def main(): target_nside = snakemake.params.nside output_mask_path = snakemake.output.mask output_summary_path = snakemake.output.summary - + verbose = True print(f"=== Processing Single Mask: nside={target_nside} ===") print(f"Source mask: {source_mask_path}") print(f"Output mask: {output_mask_path}") - + # Load original mask original_mask = load_healsparse_mask(source_mask_path, verbose=verbose) - + # Degrade mask healpix_mask = degrade_mask(original_mask, target_nside, verbose=verbose) - + # Calculate effective area effective_area = calculate_effective_area(healpix_mask, target_nside) print(f"Effective area: {effective_area:.2f} deg²") - + # Save mask save_healpix_mask(healpix_mask, output_mask_path, target_nside, verbose=verbose) - + # Save summary - save_area_summary(target_nside, effective_area, healpix_mask, - output_summary_path, output_mask_path) - + save_area_summary( + target_nside, + effective_area, + healpix_mask, + output_summary_path, + output_mask_path, + ) + print(f"Successfully processed nside={target_nside}") if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/workflow/scripts/run_2pcf_highres.py b/workflow/scripts/run_2pcf_highres.py index 54948e17..9b8f8168 100644 --- a/workflow/scripts/run_2pcf_highres.py +++ b/workflow/scripts/run_2pcf_highres.py @@ -18,7 +18,6 @@ """ import os -import sys import time import numpy as np @@ -45,8 +44,7 @@ # Configuration # --------------------------------------------------------------------------- CAT_PATH = ( - "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/" - "unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" + "/n17data/UNIONS/WL/v1.4.x/v1.4.6.3/unions_shapepipe_cut_struc_2024_v1.4.6.3.fits" ) VERSION = "SP_v1.4.6.3_leak_corr" @@ -67,10 +65,7 @@ NUM_THREADS = int(os.environ.get("SLURM_CPUS_PER_TASK", os.cpu_count() or 24)) # Output -OUTPUT_DIR = ( - "/n17data/cdaley/unions/pure_eb/" - "code/sp_validation/cosmo_val/output" -) +OUTPUT_DIR = "/n17data/cdaley/unions/pure_eb/code/sp_validation/cosmo_val/output" PATCH_FILE = os.path.join( OUTPUT_DIR, f"patch_centers_{VERSION}_{NPATCH}_{TMIN}_{TMAX}.dat", @@ -140,8 +135,7 @@ def write_xi_fits(gg, prefix, xi_data): """Write ξ+ or ξ- to FITS matching CosmologyValidation format.""" out_path = os.path.join( OUTPUT_DIR, - f"{prefix}_{VERSION}_minsep={TMIN}_maxsep={TMAX}" - f"_nbins={NBINS}_npatch=1.fits", + f"{prefix}_{VERSION}_minsep={TMIN}_maxsep={TMAX}_nbins={NBINS}_npatch=1.fits", ) n = len(xi_data) cols = [ @@ -170,7 +164,7 @@ def main(): t0 = time.time() log("=" * 60) - log(f"High-resolution ξ± measurement") + log("High-resolution ξ± measurement") log(f" MPI: {'yes' if USE_MPI else 'no'} (ranks={size})") log(f" Config: {NBINS:,} bins, [{TMIN}, {TMAX}] arcmin") log(f" Patches: {NPATCH}, Threads/rank: {NUM_THREADS}") @@ -225,8 +219,7 @@ def main(): if rank == 0: out_txt = os.path.join( OUTPUT_DIR, - f"{VERSION}_xi_minsep={TMIN}_maxsep={TMAX}" - f"_nbins={NBINS}_npatch=1.txt", + f"{VERSION}_xi_minsep={TMIN}_maxsep={TMAX}_nbins={NBINS}_npatch=1.txt", ) gg.write(out_txt, write_patch_results=True, write_cov=True) log(f" Wrote {out_txt}") @@ -235,7 +228,7 @@ def main(): write_xi_fits(gg, "xi_minus", gg.xim) elapsed = time.time() - t0 - log(f"Done! Total time: {elapsed/3600:.1f}h ({elapsed:.0f}s)") + log(f"Done! Total time: {elapsed / 3600:.1f}h ({elapsed:.0f}s)") if __name__ == "__main__": diff --git a/workflow/scripts/run_glass_mock_2pcf.py b/workflow/scripts/run_glass_mock_2pcf.py index c35dd7f6..a2343967 100644 --- a/workflow/scripts/run_glass_mock_2pcf.py +++ b/workflow/scripts/run_glass_mock_2pcf.py @@ -29,12 +29,19 @@ print(f"Loaded {len(ra)} galaxies from {catalog_path}") cat = treecorr.Catalog( - ra=ra, dec=dec, g1=e1, g2=e2, w=w, - ra_units="degrees", dec_units="degrees", + ra=ra, + dec=dec, + g1=e1, + g2=e2, + w=w, + ra_units="degrees", + dec_units="degrees", ) gg = treecorr.GGCorrelation( - min_sep=min_sep, max_sep=max_sep, nbins=nbins, + min_sep=min_sep, + max_sep=max_sep, + nbins=nbins, sep_units="arcmin", ) diff --git a/workflow/scripts/run_rho_tau.py b/workflow/scripts/run_rho_tau.py index 4132f8c7..ea2f35bc 100644 --- a/workflow/scripts/run_rho_tau.py +++ b/workflow/scripts/run_rho_tau.py @@ -2,7 +2,6 @@ # if interactive import os import sys - from pathlib import Path from IPython import get_ipython