Skip to content

Refactor Dataset to mirror MATLAB architecture with base/subclass pattern#21

Merged
stevevanhooser merged 3 commits into
mainfrom
claude/fix-python-dataset-download-Piywi
Mar 12, 2026
Merged

Refactor Dataset to mirror MATLAB architecture with base/subclass pattern#21
stevevanhooser merged 3 commits into
mainfrom
claude/fix-python-dataset-download-Piywi

Conversation

@stevevanhooser

Copy link
Copy Markdown
Contributor

Summary

This PR refactors the Dataset class to mirror the MATLAB ndi.dataset architecture, introducing a base Dataset class and a DatasetDir subclass for directory-backed datasets. The refactoring improves code organization, adds MATLAB compatibility documentation, and simplifies session management through a new session info tracking system.

Key Changes

  • Base/Subclass Architecture: Split Dataset into an abstract base class and DatasetDir subclass (mirrors MATLAB's ndi.dataset and ndi.dataset.dir)

    • Base Dataset class now contains core session management logic
    • DatasetDir handles directory-specific initialization and session discovery
    • Backward compatibility maintained: ndi.dataset.Dataset is aliased to DatasetDir
  • Session Info Management: Replaced document-based session tracking with in-memory _session_info and _session_array structures

    • Added build_session_info() method to populate session tracking from database documents
    • Implemented static methods: addSessionInfoToDataset(), removeSessionInfoFromDataset(), repairDatasetSessionInfo()
    • Sessions are now opened lazily and cached in _session_array
  • Simplified Constructor: DatasetDir now supports multiple calling conventions

    • DatasetDir(path) — open existing dataset
    • DatasetDir(reference, path) — MATLAB-style creation
    • DatasetDir(path, reference='name') — keyword-style
    • Hidden documents parameter for bulk-loading (used by downloadDataset)
  • Session Discovery: Added _discover_correct_session() and _ensure_session_tracking() methods

    • Automatically detects correct session ID from database documents
    • Creates missing session_in_a_dataset tracking records for existing sessions
    • Repairs legacy dataset_session_info documents
  • Enhanced Documentation: Added MATLAB equivalents throughout

    • Module docstring now documents MATLAB mapping
    • Methods include MATLAB equivalent: comments
    • Properties and static methods clearly marked with MATLAB counterparts
  • Query Module Improvements: Added module-level convenience functions

    • ndi.query.all(), ndi.query.none(), ndi.query.from_search() now work like MATLAB
  • Updated downloadDataset(): Uses new DatasetDir constructor with bulk document loading

    • Simplifies document ingestion workflow
    • Mirrors MATLAB's ndi.dataset.dir([], folder, docs) pattern

Implementation Details

  • Removed dependency on Ido class from base Dataset
  • Session caching moved from _session_cache dict to _session_array list for consistency with MATLAB
  • _session attribute is now set by subclass (was previously set in base constructor)
  • Database operations now route documents based on session_id (dataset's own vs. other sessions)
  • Binary file copying logic preserved but integrated into session ingestion workflow
  • All internal helper methods updated to work with new session info structures

https://claude.ai/code/session_01ByofAviQrhGPLQ9BLquGjV

claude added 3 commits March 12, 2026 12:05
…ions

Three issues made downloaded datasets unusable in Python vs MATLAB:

1. ndi.query.all() raised AttributeError because all() was a static method
   on the Query class but ndi.query is a module in Python (vs a class in
   MATLAB). Added module-level all(), none(), from_search() wrappers.

2. D.reference returned the cloud dataset ID (directory name) instead of
   the human-readable name. downloadDataset now passes ds_info["name"]
   to the Dataset constructor.

3. sessions=0 after download because no session_in_a_dataset tracking
   docs were created. Added _discover_sessions() to Dataset.__init__
   (mirroring MATLAB ndi.dataset.dir constructor) which scans the
   database for session-type documents and registers untracked sessions.

https://claude.ai/code/session_01ByofAviQrhGPLQ9BLquGjV
Major restructuring to match the MATLAB codebase:

**ndi.dataset (base class):**
- reference/id() delegate to session (matching MATLAB)
- build_session_info() discovers sessions from session_in_a_dataset docs
  filtered by base.session_id == dataset.id() (matching MATLAB)
- Static methods: repairDatasetSessionInfo, addSessionInfoToDataset,
  removeSessionInfoFromDataset (matching MATLAB)
- database_search() searches session._database directly + linked sessions
- database_add() routes docs by session_id (matching MATLAB)

**ndi.dataset.dir (DatasetDir subclass):**
- Constructor mirrors MATLAB ndi.dataset.dir with 1/2/3-arg forms
- Hidden documents parameter for downloadDataset (MATLAB 3rd arg)
- Session discovery: dataset_session_info → session_in_a_dataset →
  session docs, then re-creates session with correct ref/id
- _ensure_session_tracking() creates session_in_a_dataset docs for
  untracked sessions found in the database
- Uses dataset path directly for session (.ndi/ at root, not .ndi_dataset/)

**ndi.query module-level functions:**
- Added all(), none(), from_search() at module level so ndi.query.all()
  works like MATLAB (where ndi.query is a class, not a module)

**downloadDataset:**
- Now passes documents to DatasetDir constructor (matching MATLAB pattern)
- Reference discovered from session documents, not cloud API name

https://claude.ai/code/session_01ByofAviQrhGPLQ9BLquGjV
@stevevanhooser stevevanhooser merged commit dd39893 into main Mar 12, 2026
4 checks passed
@stevevanhooser stevevanhooser deleted the claude/fix-python-dataset-download-Piywi branch March 12, 2026 12:48
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