Repository navigation
Fix shelf loading error - #3558
Conversation
There was a problem hiding this comment.
@Ayskin great effort in this PR, good that you are addressing this!
A suggestion: what do you think about centralizing all of this logic into a single place, such that it can be reused at different call sites? What I currently see repeated is: what format are we using? Does the file exist on disk? Should we overwrite existing? This also matters for correctness. With the implemented backends, a shelf is stored as side files (e.g. .db) next to _full.pickle. If a user ran once with use_shelve=True and later without, both a fresh pickle and an old shelf exist, and dbm.whichdb reports the shelf.
Ideas for the centralized code:
- the storage format could be persisted in
_meta.pickle(e.g."storage": "pickle" | "shelve"). - a centralized context manager could return the same mapping regardless whether the file is a pickle or a shelf (or maybe a zarr in the future). The if/else branches can be dropped in all call sites (i.e. assemblies, CTD, LoadFullMultiAnimalData, outlier frames).
- the writer owns the file: when an analysis actually (re)writes the output, it can remove leftovers of the other format (or refuse to open a shelf when a pickle already exist).
- it can convert both data layouts
predictions->full_dataand vice versa
I know this might be a bit of effort, but since you are refactoring it here anyway, this might pay off quickly: one place to test, and a natural spot to plug in a different storage backend later. Curious how this fits with your plans.
cab117b to
c902f81
Compare
deruyter92
left a comment
There was a problem hiding this comment.
Great PR, looks good. Great that you managed to centralize helpers in a context manager. Call sites look cleaner now. Just a few minor comments.
ea94c79 to
07b49b5
Compare
deruyter92
left a comment
There was a problem hiding this comment.
Nice work @Ayskin! LGTM now
Bug description
When running
analyze_videosinference withuse_shelve=True, the predictions holding file_full.pickleis written as adbmdatabase. Depending on the Python version and the availabledbmbackend (dbm.gnu,dbm.sqlite3,dbm.ndbm, etc), it can end up as different file names"_full.pickle","_full.pickle.db","_full.pickle.dir", etc.Currently, the code only checks if the filename
"_full.pickle"exists before callingpickle.load. This fails if"_full.pickle"(with no additional extension) is a shelf.Fix
Centralized the predictions loading in
deeplabcut/utils/auxiliaryfunctions.py:open_full_data(path): context manager yielding a dict for a plain pickle, or a read-only shelf otherwise.load_full_data(path): same, but returns an in-memory dict copy.full_data_format(path): reads the type of the predictions file (None= no file,""= plain pickle, else the dbm module name).remove_full_data(path): removes existing files in case of overwrite.These helpers fix the loading error that may happen at several places in the code :
_generate_assemblies_file, CTD tracking,tracklets.py,outlier_frames.py,scoring.py,LoadFullMultiAnimalData.Additional fix :
overwrite=Trueis now safe to be used inanalyze_videosas the helpers look for previously existing files and remove/overwrite them accordingly depending on their format.This handles the issue where switching writing type (shelving or not) could leave coexisting files and mess up reading freshly created files.
Tests
Standard i/o tests for the helpers in
tests/test_auxiliaryfunctions.py.New tests to lock the fixes of
analyze_videosintests/pose_estimation_pytorch/apis/test_videos.py.Tests parametrize on available
dbmbackends with unavailable ones being skipped.I am not sure about best practice here so any comment on this is much appreciated.