Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid clobber in setup_anndata using model instance and manager ID back references #1342

Merged
merged 7 commits into from
Feb 9, 2022

Conversation

justjhong
Copy link
Contributor

This PR introduces model instance and AnnDataManager ids (uuid4) to avoid clobbering between models referencing the same AnnData object with different parameters.

This is accomplished by adding a model instance ID which is used to create a model instance specific mapping to AnnDataManagers used to initialize given models. The same goes with AnnDataManagers created via transfer_setup on different AnnData objects (e.g. via get_latent_representation()). This allows model instances to reliably fetch AnnDataManagers created for/by that model instance, rather than the last AnnDataManager created for this AnnData object in this class.

Secondly, it is still possible for an AnnData object to be mutated by another AnnDataManager's register_fields() function call. This can result in a situation where a model instance retrieves the correct AnnDataManager object, but the underlying AnnData has changed. To avoid this situation, we replace source_scvi_uuid with scvi_last_manager_uuid which reports which AnnDataManager instance was used last to setup the AnnData object. Now, one can call adata_manager.validate() to check whether the AnnData has been tampered with, and if so rerun register_fields() with the original arguments to recreate the desired setup.

@codecov
Copy link

codecov bot commented Feb 8, 2022

Codecov Report

Merging #1342 (cd86d20) into master (3908e3c) will decrease coverage by 0.09%.
The diff coverage is 91.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1342      +/-   ##
==========================================
- Coverage   90.81%   90.72%   -0.10%     
==========================================
  Files         109      109              
  Lines        8180     8223      +43     
==========================================
+ Hits         7429     7460      +31     
- Misses        751      763      +12     
Impacted Files Coverage Δ
scvi/external/gimvi/_model.py 92.71% <83.33%> (-0.32%) ⬇️
scvi/model/base/_base_model.py 88.67% <89.28%> (-3.37%) ⬇️
scvi/data/anndata/_constants.py 100.00% <100.00%> (ø)
scvi/data/anndata/_manager.py 98.51% <100.00%> (+0.09%) ⬆️
scvi/external/solo/_model.py 94.69% <100.00%> (ø)
scvi/model/base/_archesmixin.py 93.61% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3908e3c...cd86d20. Read the comment docs.

scvi/data/anndata/_manager.py Outdated Show resolved Hide resolved
scvi/model/base/_base_model.py Outdated Show resolved Hide resolved
scvi/model/base/_base_model.py Outdated Show resolved Hide resolved
scvi/model/base/_base_model.py Outdated Show resolved Hide resolved
scvi/model/base/_base_model.py Show resolved Hide resolved
scvi/data/anndata/_manager.py Outdated Show resolved Hide resolved
scvi/data/anndata/_manager.py Outdated Show resolved Hide resolved
scvi/model/base/_base_model.py Outdated Show resolved Hide resolved
"""

def __init__(cls, name, bases, dct):
cls.manager_store = dict()
cls._setup_adata_manager_store: Dict[
Copy link
Member

Choose a reason for hiding this comment

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

correct me if I'm wrong, but this manager store will only ever receive one anndata now, the one used to initialize the model?

Copy link
Member

Choose a reason for hiding this comment

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

if this is the case, should we make it not a dict?

Copy link
Member

Choose a reason for hiding this comment

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

actually this is general enough...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it stores it for the whole class. this allows for situations where an AnnData is setup once and used to create two separate models.

scvi/data/anndata/_manager.py Show resolved Hide resolved
@justjhong justjhong merged commit 87075cb into master Feb 9, 2022
@justjhong justjhong deleted the jhong/clobberfix branch February 9, 2022 22:28
nrclaudio pushed a commit to nrclaudio/scvi-tools-tune that referenced this pull request Jun 21, 2022
…ck references (scverse#1342)

* add failing test

* avoid clobber using back reference to manager and model uuids

* fix arches mixin

* fix gimvi tests

* add additional test, address minor comments

* make more explicit manager stores for each case

* address second round comments
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.

2 participants