Skip to content

fix KeyError: 'weight_mode' when packing v2.1 recipes with under-specified gradients#470

Merged
rugeli merged 4 commits into
mainfrom
gradient-validation
Jun 9, 2026
Merged

fix KeyError: 'weight_mode' when packing v2.1 recipes with under-specified gradients#470
rugeli merged 4 commits into
mainfrom
gradient-validation

Conversation

@rugeli

@rugeli rugeli commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Problem

closes #465

Sorry for the delay on this one, after tracing it back through the packing pipeline, I figured the gradient packing error is not a problem with our Pydantic schema. The root cause is a data-pipeline gap. Gradient defaults were only ever filled in one place: the v2.0→v2.1 migration. But recipe_loader._read only runs migration when format_version != current_version. A recipe hand-authored directly as 2.1 skips migration entirely, so its gradients reach Gradient.__init__ still missing keys.

In short, without the changes in this pr, relabeling format_version as 2.0, everything else stays the same, the recipe cellpack/tests/recipes/v2/test_peroxisome_combined_gradient.json would pass.

Solution

in recipe_loader:

  • added gradient normalization that converts list-of-dict form and fill missing top-level keys. Made this conversion happen before validation.
  • removed now-redundant gradient dict to list block

with @mogres

Note: If we want to further refactor, we can:

  1. make use of the validator output. validate_recipe builds a normalized copy that _read currently discards, we currently only use validator for validating which makes sense to me. But if we want to, we can make the validated recipe source of truth. This will be a larger pr so I'd keep this separate.
  2. unify version migration scripts(default filling, structure flatten&nested, etc). more files involved, so better be another separate pr

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Steps to Verify:

  1. run pack -r cellpack/tests/recipes/v2/test_peroxisome_combined_gradient.json -c cellpack/tests/packing-configs/test_gradient_mixing.json

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Packing analysis report

Analysis for packing results located at cellpack/tests/outputs/test_spheres/spheresSST

Ingredient name Encapsulating radius Average number packed
ext_A 25 236.0

Packing image

Packing image

Distance analysis

Expected minimum distance: 50.00
Actual minimum distance: 50.01

Ingredient key Pairwise distance distribution
ext_A Distance distribution ext_A

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a packing-time KeyError: 'weight_mode' for v2.1 recipes whose gradients are under-specified (i.e., they skip v2.0→v2.1 migration, so gradient defaults never get filled). The core change is to normalize gradients earlier in RecipeLoader._read() so gradients are consistently list-of-dicts and have required top-level keys before validation and before constructing Gradient instances.

Changes:

  • Add RecipeLoader._normalize_gradients() to convert dict→list gradient format and fill missing top-level gradient keys from defaults prior to validation.
  • Remove the older late-stage “gradients dict→list” conversion block and move serializable_recipe_data capture to occur after dict-level normalization.
  • Add a reproduction recipe/config under cellpack/tests/ for manual verification of gradient-mixing packing.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
cellpack/autopack/loaders/recipe_loader.py Adds gradient normalization + reorders normalization/validation/serialization to avoid missing gradient keys in v2.1 recipes.
cellpack/tests/recipes/v2/test_peroxisome_combined_gradient.json Adds a v2.1 recipe fixture demonstrating combined gradients / under-specified gradient keys.
cellpack/tests/packing-configs/test_gradient_mixing.json Adds a packing config used to reproduce the gradient-mixing scenario.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cellpack/autopack/loaders/recipe_loader.py
Comment thread cellpack/tests/recipes/v2/test_peroxisome_combined_gradient.json
Comment thread cellpack/autopack/loaders/recipe_loader.py
@rugeli rugeli requested review from ascibisz, meganrm and mogres June 1, 2026 20:46
@rugeli rugeli force-pushed the gradient-validation branch from 592a091 to 460ab53 Compare June 4, 2026 01:05

@mogres mogres left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Ruge! I ran the verification successfully.

@rugeli rugeli merged commit 7672d73 into main Jun 9, 2026
17 checks passed
@rugeli rugeli deleted the gradient-validation branch June 9, 2026 21:56
mogres added a commit that referenced this pull request Jun 16, 2026
* user workflow improvements and config updates  (#411)

* default upload_results to false

* update config file options

* update schema docs

* propose to remove config files from the packing commands but add a section to explain how configs work

* set open_results_in_browser to false since it's dependent on upload_results

* improve loggings

* add a config file for upload results

* correct option default and log level

* update schema

* add aws db to path

* add a layer to detect non-interactive envs for db selection

* fix readme

* add cellpack website url

* remove unnecessary restriction

* update readme

* add screenshots to readme (#413)

* add screenshots to readme

* Quick fix: update bbox value in recipes (#420)

* add randomness_seed to upload and download (#416)

* update bbox value in recipes

* add config option for plot display

* replace result screenshots and update readme

* update docs/index to display images correctly

* Add description field to recipe schema and backend utilities (#430)

* add `description` and `randomness_seed` to top level recipe settings

* add description in example recipes

* add description to download and upload script

* add description field to unit tests

* pr commenting: use native github CLI (#432)

* test github cli

* remove test branch

* Minimal fix: set camera position (#431)

* set camera

* use max dimension

* add checks for pull requests (#434)

* Feature/client upload script (#417)

* add upload script

* add example data and more documentation

* point to correct collection

* return firebase IDs from upload_recipe adn upload_config

* add documentation

* add installation step

* lint fixes

* lint fix

* fix typos

* keep all upload functionality in upload.py, and rename client to studio

* remove accidential line

* update example editable fields to match updates we've done

* add output param

* lint fixes

* small wording fixes

* make config_path optional

* ci(dependabot): bump astral-sh/setup-uv from 4 to 7 (#436)

Bumps [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) from 4 to 7.
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](astral-sh/setup-uv@v4...v7)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Fix Comment Update job: use gh api instead of gh pr review (#438)

* use gh api instead of gh pr review

* remove backslashes

* trigger workflow to test comment update

* use patch instead of delete

* take2-trigger workflow to test comment update

* use an actual new line to avoid \n being treated as literal chars

* Studio: add recipe and config json to outputs for downloading  (#435)

* add recipe and config to output

* refactor

* path guard

* Potential fix for code scanning alert no. 21: Uncontrolled data used in path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* format

* fix 404 comment

* handle firebase recipe, serialize it before passing to s3

* save recipe data directly to outputs

* remove unused import

* one more unused import

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* ci(dependabot): bump actions/upload-artifact from 4 to 6 (#439)

Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4 to 6.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](actions/upload-artifact@v4...v6)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Miscellaneous gradient updates (#428)

* Add gradient weights to Environment.py and Gradient.py

* Remove unused import

* Refactor Environment.py and utils.py

This commit refactors the `Environment.py` file by separating the loading of grid, compartment grids, and mesh store into separate loops. It also clears the triangles_tree cache and resets the grid. Additionally, the `load_object_from_pickle` function has been removed from `utils.py` as it is no longer used.

* Ignore venv folders

* Update logging level

* Update `aicsimageio` to `bioio`

- Also update the logging of recipe validation to `debug`

* Suppress logging outputs for lower level functions

* Update logs in `ImageWriter.py`

* Update path preference loading to catch empty json

* Update logging for recipe validation

* sort imports

* Update uv lock

* Allow gradient weights to be specified as a dict while combining

* Add uniform weight map option

* Fix gradient weight scaling for equal weights

* Update scaling to just divide by max value

* Add recipe to test gradients

* Rename scaling method to normalize_by_max_value and update references

* Use uniform weight if `decay_length` is 0

* Linting updates

* Update validation for decay length

* Update validation for multiple gradients

* Add zero decay gradient in test recipe

* ci(dependabot): bump aws-actions/configure-aws-credentials from 4 to 5 (#447)

* ci(dependabot): bump aws-actions/configure-aws-credentials from 4 to 5

Bumps [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) from 4 to 5.
- [Release notes](https://github.com/aws-actions/configure-aws-credentials/releases)
- [Changelog](https://github.com/aws-actions/configure-aws-credentials/blob/main/CHANGELOG.md)
- [Commits](aws-actions/configure-aws-credentials@v4...v5)

---
updated-dependencies:
- dependency-name: aws-actions/configure-aws-credentials
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* matching the regular pr format

* trigger codeQL

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ruge Li <rugeli0605@gmail.com>

* ci(dependabot): bump peter-evans/create-pull-request from 7 to 8 (#450)

Bumps [peter-evans/create-pull-request](https://github.com/peter-evans/create-pull-request) from 7 to 8.
- [Release notes](https://github.com/peter-evans/create-pull-request/releases)
- [Commits](peter-evans/create-pull-request@v7...v8)

---
updated-dependencies:
- dependency-name: peter-evans/create-pull-request
  dependency-version: '8'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Use native gh CLI for requirements actions (#453)

* update UV setup to version 7
* update Dependabot configuration

* remove unnecessary permissions for open-PR job in make-requirements.yml

* Make timestamp unique using the run ID

* Avoid expanding the WORKFLOW_PAT

* ci(dependabot): bump the actions group with 4 updates (#456)

Bumps the actions group with 4 updates: [actions/checkout](https://github.com/actions/checkout), [actions/upload-artifact](https://github.com/actions/upload-artifact), [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) and [actions/download-artifact](https://github.com/actions/download-artifact).


Updates `actions/checkout` from 4 to 6
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v4...v6)

Updates `actions/upload-artifact` from 6 to 7
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](actions/upload-artifact@v6...v7)

Updates `aws-actions/configure-aws-credentials` from 5 to 6
- [Release notes](https://github.com/aws-actions/configure-aws-credentials/releases)
- [Changelog](https://github.com/aws-actions/configure-aws-credentials/blob/main/CHANGELOG.md)
- [Commits](aws-actions/configure-aws-credentials@v5...v6)

Updates `actions/download-artifact` from 6 to 8
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](actions/download-artifact@v6...v8)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/upload-artifact
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: aws-actions/configure-aws-credentials
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/download-artifact
  dependency-version: '8'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Upgrading dependencies (#457)

* Upgrading dependencies

* Update black target version

* Formatting via `uv run black cellpack`

* Updating requirements.txt after change to `uv.lock` was pushed to `main` (#458)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Server should Accept Recipe JSON  (#419)

* add upload script

* add example data and more documentation

* point to correct collection

* have server accept recipe as json object in body of request

* update documentation

* remove accidential dockerfile changes

* rename param json_recipe

* remove file that shouldn't be in this PR

* remove accidential file

* lint fixes

* refactor to try to improve clarity of json recipe vs file path

* lint fixes

* lint fix

* minimize changeset

* minimize changeset

* simplify changeset

* code cleanup

* minimize changeset

* remove trailing comma

* Feature/firebase lookup (#445)

* remove os fetch for job_id

* use dedup_hash instead of job id

* proposal: get hash from recipe loader

* renaming and add TODOs

* format

* rename param to hash

* remove unused validate param and doc strings in pack

* simplify get_ dedup_hash

* refactor job_status update

* cleanup

* fix upload_job_status to handle awshandler

* pass dedup_pash to env for fetching across files

* add tests

* format1

* format test

* Only upload simularium file once (#446)

* proposal: get hash from recipe loader

* simplify get_ dedup_hash

* only post simularium results file once for server job runs

* update code for rebase

* code cleanup

---------

Co-authored-by: Ruge Li <rugeli0605@gmail.com>

* Maint/firebase collection cleanup (#448)

* remove local metadata writes for auto-pop feature

* remove cleanup firebase workflow

* remove cleanup firebase code

* 1. make doc url a constant 2.remove unused param

* handle both recipe_path and json body requests (#449)

* change error message body

* lint fixes

* add more checks when attempting to read json body

* add upload script

* add example data and more documentation

* point to correct collection

* have server accept recipe as json object in body of request

* update documentation

* remove accidential dockerfile changes

* rename param json_recipe

* remove file that shouldn't be in this PR

* remove accidential file

* lint fixes

* refactor to try to improve clarity of json recipe vs file path

* lint fixes

* lint fix

* minimize changeset

* minimize changeset

* simplify changeset

* code cleanup

* minimize changeset

* remove trailing comma

* Feature/firebase lookup (#445)

* remove os fetch for job_id

* use dedup_hash instead of job id

* proposal: get hash from recipe loader

* renaming and add TODOs

* format

* rename param to hash

* remove unused validate param and doc strings in pack

* simplify get_ dedup_hash

* refactor job_status update

* cleanup

* fix upload_job_status to handle awshandler

* pass dedup_pash to env for fetching across files

* add tests

* format1

* format test

* Only upload simularium file once (#446)

* proposal: get hash from recipe loader

* simplify get_ dedup_hash

* only post simularium results file once for server job runs

* update code for rebase

* code cleanup

---------

Co-authored-by: Ruge Li <rugeli0605@gmail.com>

* Maint/firebase collection cleanup (#448)

* remove local metadata writes for auto-pop feature

* remove cleanup firebase workflow

* remove cleanup firebase code

* 1. make doc url a constant 2.remove unused param

* handle both recipe_path and json body requests (#449)

* change error message body

* lint fixes

* add more checks when attempting to read json body

* let recipe loader check the input and key stripping

* Update cellpack/autopack/writers/__init__.py

Co-authored-by: Saurabh Mogre <saurabh.mogre@alleninstitute.org>

* use `isinstance` for AWSHandler, and misc

* update aws tests

* initialize dedup_hash

* add in-line comment

* temp solution: use requirement.txt

---------

Co-authored-by: Ruge Li <91452427+rugeli@users.noreply.github.com>
Co-authored-by: Ruge Li <rugeli0605@gmail.com>
Co-authored-by: Saurabh Mogre <saurabh.mogre@alleninstitute.org>

* ci(dependabot): bump the actions group across 1 directory with 4 updates (#466)

Bumps the actions group with 4 updates in the / directory: [actions/configure-pages](https://github.com/actions/configure-pages), [actions/upload-pages-artifact](https://github.com/actions/upload-pages-artifact), [actions/deploy-pages](https://github.com/actions/deploy-pages) and [codecov/codecov-action](https://github.com/codecov/codecov-action).


Updates `actions/configure-pages` from 5 to 6
- [Release notes](https://github.com/actions/configure-pages/releases)
- [Commits](actions/configure-pages@v5...v6)

Updates `actions/upload-pages-artifact` from 4 to 5
- [Release notes](https://github.com/actions/upload-pages-artifact/releases)
- [Commits](actions/upload-pages-artifact@v4...v5)

Updates `actions/deploy-pages` from 4 to 5
- [Release notes](https://github.com/actions/deploy-pages/releases)
- [Commits](actions/deploy-pages@v4...v5)

Updates `codecov/codecov-action` from 5 to 6
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](codecov/codecov-action@v5...v6)

---
updated-dependencies:
- dependency-name: actions/configure-pages
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/upload-pages-artifact
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/deploy-pages
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: codecov/codecov-action
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Test updates: stabler debup hash and cli integration coverage (#462)

* normalize data order before hashing and tests

* test pack cli

* refactor test file: use tmp_path instead of writing to a working dir

* cleanup

* formatting

* add recipe fixture

* Remove Support for Recipe URL Param (#464)

* we only support recipe as JSON body now, remove support for reading recipe file path from URL

* update documentation to use new communication protocol and strip out references to AWS batch

* remove unused import

* Maint/deprecate requirements txt (#467)

* remove requirements.txt related code

* update pyproject.toml

* generate new uv file

* update pip install for dockerfile

* use uv sync

* move server dep list under dependency-groups

* update docker

* fix path and use uv sync

* add pydantic to pyproject toml

* update lock file

* Feature/v2 bloodplasma (#471)

* update migration script and validation

* add v2 blood plasma recipe

* ci(dependabot): bump actions/add-to-project in the actions group (#469)

Bumps the actions group with 1 update: [actions/add-to-project](https://github.com/actions/add-to-project).


Updates `actions/add-to-project` from 1.0.2 to 2.0.0
- [Release notes](https://github.com/actions/add-to-project/releases)
- [Commits](actions/add-to-project@v1.0.2...v2.0.0)

---
updated-dependencies:
- dependency-name: actions/add-to-project
  dependency-version: 2.0.0
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* fix KeyError: 'weight_mode' when packing v2.1 recipes with under-specified gradients (#470)

* Add recipe and config to test gradient mixing in peroxisome data

* normalize grad in early process

* add tests

---------

Co-authored-by: mogres <saurabh.mogre@alleninstitute.org>

* pack: log resolved cellpack package path at DEBUG level

Adds a DEBUG-level log of which cellpack package pack.py imported
(autopack path), so it is easy to confirm whether a run used an
installed/pinned cellpack vs a checked-out branch when debugging.
Kept at DEBUG so normal packing output is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Ruge Li <91452427+rugeli@users.noreply.github.com>
Co-authored-by: Alli <111383930+ascibisz@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Ruge Li <rugeli0605@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validation schema improvements

4 participants