OHBA-analysis / osl-dynamics

Methods for studying dynamic functional brain activity in neuroimaging data.
https://osl-dynamics.readthedocs.io/
MIT License
57 stars 18 forks source link

HMM dual estimation errors #281

Closed saltwater-tensor closed 1 month ago

saltwater-tensor commented 1 month ago

For the following lines of code:

https://github.com/OHBA-analysis/osl-dynamics/blob/main/osl_dynamics/models/hmm.py#L1541-L1556

If we supply the training data as osl_dynamics.data.Data object that contains a single time series. Then the code will always lead to an error because the following line will be run: https://github.com/OHBA-analysis/osl-dynamics/blob/main/osl_dynamics/models/hmm.py#L1550

data = training_data.time_series(prepared=True, concatenate=False)

And subsequently, when the following lines are run len(data) will result in the number of samples in the time series rather than the number of sessions or subjects, causing errors. Although that should not be the case.

if len(alpha) != len(data):
    raise ValueError(
        "len(alpha) and training_data.n_sessions must be the same."
    )
saltwater-tensor commented 1 month ago

Another point/ clarification:

Why is the following check performed:

https://github.com/OHBA-analysis/osl-dynamics/blob/main/osl_dynamics/models/hmm.py#L1557-L1558

# Make sure the data and alpha have the same number of samples
data = [d[: a.shape[0]] for d, a in zip(data, alpha)]

Shouldn't alpha and prepared data always have the same number of samples since alpha is getting estimated using the properly prepared data?

Or is there some particular step during alpha estimation that trims the estimated alpha?

Thanks.

saltwater-tensor commented 1 month ago

Similarly, we encounter errors in the following function: https://github.com/OHBA-analysis/osl-dynamics/blob/main/osl_dynamics/models/hmm.py#L1564

def _single_dual_estimation(a, x)

Here in the following lines: https://github.com/OHBA-analysis/osl-dynamics/blob/main/osl_dynamics/models/hmm.py#L1578-L1586

Since the data supplied is for a single session a and x both would be two dimensional. Therefore, diff produces an error.

diff = x - session_means[state]
session_covariances[state] = (
np.sum(
    diff[:, :, None]
    * diff[:, None, :]
    * a[:, state, None, None],
    axis=0,
)
/ sum_a[state]
cgohil8 commented 1 month ago

Why is the following check performed:

https://github.com/OHBA-analysis/osl-dynamics/blob/main/osl_dynamics/models/hmm.py#L1557-L1558

Just an extra check, might not be needed but kept to be safe.