Skip to content

Add DYSCO for aligned repeated-trial dynamics - #317

Draft
myscience wants to merge 9 commits into
AdaptiveMotorControlLab:mainfrom
myscience:codex/dysco-aligned-trials
Draft

myscience wants to merge 9 commits into
AdaptiveMotorControlLab:mainfrom
myscience:codex/dysco-aligned-trials

Conversation

@myscience

@myscience myscience commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

This draft adds a scikit-learn entry point for DYSCO, the multi-view dynamics method of Muratore and Mathis. It targets regularly sampled repetitions in a shared observation space, with optional observed inputs. Feedback on the trial contract and the solver interface would help before we finalize this for release. Paper · Standalone implementation and examples

import cebra

# Each array has shape (time, feature). Trials with the same ID already align.
model = cebra.DYSCO(output_dimension=2, sample_interval=0.05)
model.fit(trials, alignment_ids=condition_ids)
z = model.transform(trials[0])
forecast = model.rollout(z[0], num_steps=10)
print(*model.equations(), sep="\n")

The loader samples synchronized windows across distinct repetitions. A shared encoder and a continuous-time polynomial field generate forward and backward RK4 trajectories. The criterion implements the submitted three-block loss with CEBRA’s full-denominator InfoNCE reduction. The estimator exposes embedding, field evaluation, equation inspection, rollout and checkpoint continuation. The offline oscillator and Lorenz examples generate their own observations.

Dependency. This branch includes the exact checkpoint commits from #312. They preserve the auxiliary reference model, which owns DCL dynamics. Once that PR lands we can update this branch against main. The DYSCO-specific changes start at 5526c10.

Review and validation

  • Frozen reference cases compare loss values and gradients with the submitted implementation. Coverage also includes window boundaries, observed-control interpolation in both time directions, analytic fields, checkpoint round trips, and continuation with the private samplers.
  • On the final commit, the full local CPU suite passed 3,364 tests, with 110 skipped and 6,965 dataset-dependent cases deselected. The focused run passed 96 tests and skipped two accelerator cases. The last change preserves the prior model after an invalid refit and keeps forecasting on the sampling interval actually fitted.
  • A separate FP32 check matched an additive controlled quadratic field, four RK4 steps in both directions, encoded states, and random event draws across the public implementation and this branch. The scalar loss agreed exactly in this case. Maximum absolute differences were 1.2e-7 for the field and rollouts, 3.0e-7 for gradients with respect to encoded states, and 4.2e-9 for gradients with respect to controls.
  • On two recorded Lorenz runs with an unseen initial condition, transported field R² was 0.505–0.544. At 20 steps, forecast MSE was 0.305–0.326 times persistence MSE. Those examples use a smaller training regime, direct polynomial coefficients and a different observation setup from the reference experiments. Matching end-to-end recovery under the paper protocol remains open.
  • The current solver adapter carries trajectory tensors through CEBRA’s three batch slots. In particular, we would welcome feedback on introducing a named trajectory result and a shared loss hook. Broader accelerator/platform validation remains open.

Upstream CI. In the completed Python 3.14 / Torch 2.9.1 job, both DYSCO test modules passed. Two unchanged data-loading doctests failed when GitHub returned HTTP 503 for the DeepLabCut example HDF5 file. Both passed on a local retry. The matrix canceled its remaining jobs, and restarting it requires upstream admin rights. Failing job

I have signed the Contributor License Agreement.

@cla-bot cla-bot Bot added the CLA signed label Nov 26, 2026
@myscience myscience assigned stes and unassigned stes Nov 26, 2026
@myscience
myscience requested a review from stes November 26, 2026 18:15

@stes stes left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Really nice!!!

Left some comments, different levels of importance, see details.

I think the docs pages are currently lengthy; I would vote that in case they are LLM written to cut them down to minimum and replace by human-curated text when there is time, vs. putting LLM generated content there (which a user can easily generate themselves). Wdyt?

from cebra.data.base import Loader


def _positive_int(value, name):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

type annotations

negative_controls: Optional[torch.Tensor]
index: AlignedWindowIndex

def to(self, device):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

type annotations



@dataclass
class AlignedTrialBatch:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i think there is a HasDevice to integrate the to functionality smoothly within cebra that should be used here as superclass

self.controls = torch.cat(control_arrays)

@property
def input_dimension(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def input_dimension(self):
def input_dimension(self) -> int:

Comment on lines +132 to +133
variable_names=None,
control_names=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

types?

A core window has ``rollout_steps + 1`` nodes. Raw observations include the
encoder's receptive context; controls and times cover the core nodes only.
Views are distinct trials from the same group at the same indices. Set
``num_views=1`` for singleton groups. Impossible view counts and windows are

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what are singleton groups?

assert batch.negative.shape == (32, 13, 5)
A core window has ``rollout_steps + 1`` nodes. Raw observations include the
encoder's receptive context; controls and times cover the core nodes only.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

receptive field?

what are core nodes?

Comment on lines +111 to +112
candidate collisions, for the contrastive objective's exclusion policy. This
loader does not define the DYSCO loss or claim submitted-objective parity.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what does this mean? i think can be deleted?

next_state = flow(z)
trajectory = flow.rollout(z, num_steps=20) # (8, 21, 3)
``dt`` is the time interval represented by one map application. In DCL it must

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

map application?

Comment on lines +53 to +61
trajectory = [z.copy()]
for k in range(len(times) - 1):
start, end = u[k, 0], u[k + 1, 0]
middle = (start + end) / 2
k1 = field(z, start)
k2 = field(z + dt * k1 / 2, middle)
k3 = field(z + dt * k2 / 2, middle)
k4 = field(z + dt * k3, end)
z = z + dt * (k1 + 2 * k2 + 2 * k3 + k4) / 6

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

helper function for RK4? saw this code snippet a couple of times now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants