From 607c3ad12f928de85f224b4b40bb58cf143f632b Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Wed, 13 May 2026 14:34:46 -0400 Subject: [PATCH] bwm_usage_notebook: work around brainglobe S3 403 on Colab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit brainglobe-atlasapi 2.3.1 fetches its atlas-version manifest from https://brainglobe.s3.us-west-2.amazonaws.com/... on every BrainGlobeAtlas() construction. Some Colab IPs hit a 403 against that bucket, and the library doesn't gracefully fall back when the local cache is missing on a fresh runtime — it raises: Could not fetch the latest atlas versions: 403 Using the last cached version from /root/.brainglobe/last_versions.conf FileNotFoundError: Last versions cache file not found. Same upstream issue tracked at brainglobe/brainglobe-atlasapi#738. Workaround: pre-seed the manifest cache before BrainGlobeAtlas is called. First try fetching the manifest directly with stdlib urllib (skips brainglobe's pooch path that's getting 403'd); if that also fails, fall back to a minimal hardcoded `allen_mouse_25um = 3.0` entry — enough for this notebook's one call. Writes the cache to both `~/.brainglobe/last_versions.conf` (V1 path that brainglobe-atlasapi 2.3.1 actually reads) and the V2 subpath in case the library upgrades. Co-Authored-By: Claude Opus 4.7 (1M context) --- 000409/IBL/bwm_usage_notebook.ipynb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/000409/IBL/bwm_usage_notebook.ipynb b/000409/IBL/bwm_usage_notebook.ipynb index cbfe116..76a1d10 100644 --- a/000409/IBL/bwm_usage_notebook.ipynb +++ b/000409/IBL/bwm_usage_notebook.ipynb @@ -2277,6 +2277,31 @@ } ], "source": [ + "# Workaround: brainglobe-atlasapi 2.3.1 fetches its atlas-version manifest\n", + "# from a Colab-unfriendly S3 endpoint that returns 403 for some Colab IPs.\n", + "# Pre-seed the cache so BrainGlobeAtlas() below can find a version manifest\n", + "# even when the live fetch fails. See https://github.com/brainglobe/brainglobe-atlasapi/issues/738\n", + "from pathlib import Path\n", + "import urllib.request\n", + "\n", + "_bg_dir = Path.home() / \".brainglobe\"\n", + "_manifest_url = (\n", + " \"https://brainglobe.s3.us-west-2.amazonaws.com/atlas/atlases/last_versions.conf\"\n", + ")\n", + "try:\n", + " _manifest = urllib.request.urlopen(_manifest_url, timeout=20).read().decode()\n", + "except Exception as _e: # noqa: BLE001\n", + " print(f\"Couldn't reach brainglobe manifest ({_e}); writing a minimal fallback.\")\n", + " _manifest = \"[atlases]\\nallen_mouse_25um = 3.0\\n\"\n", + "\n", + "# Write to both the V1 and V2 cache paths so whichever brainglobe-atlasapi\n", + "# code path reads from disk finds the file.\n", + "for _p in (_bg_dir / \"last_versions.conf\",\n", + " _bg_dir / \"brainglobe-atlasapi\" / \"atlases\" / \"last_versions.conf\"):\n", + " _p.parent.mkdir(parents=True, exist_ok=True)\n", + " if not _p.exists():\n", + " _p.write_text(_manifest)\n", + "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from brainglobe_atlasapi import BrainGlobeAtlas\n",