RSA Integration Functions
Integration of RSA with DRIADA’s data structures.
- driada.rsa.integration.compute_experiment_rdm(experiment, items, data_type='calcium', metric='correlation', average_method='mean')[source]
Compute RDM from Experiment object using specified item definition.
Extracts neural data from a DRIADA Experiment and computes the representational dissimilarity matrix based on either behavioral variables or explicit trial structure.
- Parameters:
experiment (Experiment) – DRIADA Experiment object containing neural data
items (str or dict) – How to define items/conditions: - str: name of dynamic feature to use as condition labels - dict with ‘trial_starts’ and ‘trial_labels’ for explicit trials
data_type (str, default 'calcium') – Type of data to use (‘calcium’ or ‘spikes’)
metric (str, default 'correlation') – Distance metric for RDM computation
average_method (str, default 'mean') – How to average within conditions (‘mean’ or ‘median’)
- Return type:
- Returns:
rdm (np.ndarray) – Representational dissimilarity matrix of shape (n_conditions, n_conditions)
labels (np.ndarray) – The unique labels/conditions in the order they appear in RDM
- Raises:
ValueError – If experiment has no data of the specified type. If dynamic feature name not found in experiment. If trial structure dict missing required keys. If items is not str or dict.
Notes
When using dynamic features, the function extracts the feature data and uses it as condition labels for each timepoint. When using trial structure, explicit trial boundaries and labels are provided.
Examples
See compute_rdm_from_timeseries_labels and compute_rdm_from_trials for the core functionality that this function wraps. The function extracts data from Experiment objects and delegates to those functions.
Direct usage with Experiment objects requires creating complex data structures that are better demonstrated in the test files and tutorials.
See also
compute_rdm_from_timeseries_labelsLower-level function for labeled data
compute_rdm_from_trialsLower-level function for trial structure
compare_rdmsCompare two RDMs using correlation metrics
- driada.rsa.integration.compute_mvdata_rdm(mvdata, labels, metric='correlation', average_method='mean')[source]
Compute RDM from MVData object with condition labels.
Extracts data from MVData object and computes representational dissimilarity matrix based on provided condition labels.
- Parameters:
mvdata (MVData) – MVData object containing data matrix of shape (n_features, n_timepoints)
labels (np.ndarray) – Condition labels for each timepoint, shape (n_timepoints,)
metric (str, default 'correlation') – Distance metric for RDM computation (‘correlation’, ‘euclidean’, ‘cosine’, ‘manhattan’)
average_method (str, default 'mean') – How to average within conditions (‘mean’ or ‘median’)
- Return type:
- Returns:
rdm (np.ndarray) – Representational dissimilarity matrix of shape (n_conditions, n_conditions)
unique_labels (np.ndarray) – The unique labels in order as they appear in RDM
Notes
This is a thin wrapper around compute_rdm_from_timeseries_labels that extracts data from MVData objects. MVData stores data in the expected format (n_features, n_timepoints).
Examples
>>> import numpy as np >>> from driada.dim_reduction.data import MVData >>> >>> # Create MVData and compute RDM >>> data = np.random.randn(100, 500) # 100 features, 500 timepoints >>> mvdata = MVData(data) >>> labels = np.repeat(['A', 'B', 'C'], [150, 200, 150]) >>> rdm, unique_labels = compute_mvdata_rdm(mvdata, labels) >>> print(f"RDM shape: {rdm.shape}, conditions: {unique_labels}") RDM shape: (3, 3), conditions: ['A' 'B' 'C']
See also
compute_rdm_from_timeseries_labelsCore function this wraps
compute_rdm_unifiedUnified interface for all data types
- driada.rsa.integration.rsa_between_experiments(exp1, exp2, items, data_type='calcium', metric='correlation', comparison_method='spearman', average_method='mean', bootstrap=False, n_bootstrap=1000)[source]
Perform RSA comparison between two experiments.
Computes representational dissimilarity matrices for both experiments and quantifies their similarity. Optionally performs bootstrap analysis for statistical inference.
- Parameters:
exp1 (Experiment) – First experiment
exp2 (Experiment) – Second experiment
items (str or dict) – How to define items/conditions (must be same for both experiments): - str: name of dynamic feature to use as condition labels - dict: trial structure with ‘trial_starts’ and ‘trial_labels’
data_type (str, default 'calcium') – Type of data to use (‘calcium’ or ‘spikes’)
metric (str, default 'correlation') – Distance metric for RDM computation
comparison_method (str, default 'spearman') – Method for comparing RDMs (‘spearman’, ‘pearson’, ‘kendall’, ‘cosine’)
average_method (str, default 'mean') – How to average within conditions (‘mean’ or ‘median’)
bootstrap (bool, default False) – Whether to perform bootstrap significance testing
n_bootstrap (int, default 1000) – Number of bootstrap iterations
- Returns:
similarity – If bootstrap=False: similarity score between RDMs If bootstrap=True: dict with keys: - ‘observed’: observed similarity - ‘bootstrap_distribution’: bootstrap values - ‘p_value’: two-tailed p-value - ‘ci_lower’, ‘ci_upper’: 95% confidence interval - ‘mean’, ‘std’: bootstrap statistics
- Return type:
- Raises:
ValueError – If experiments have different condition labels.
NotImplementedError – If bootstrap requested with trial structure (not yet supported).
Notes
Both experiments must have the same conditions (same unique labels) for meaningful comparison. The function automatically extracts the appropriate data type and computes RDMs before comparing them.
Bootstrap analysis uses within-condition resampling to maintain experimental design while estimating variability.
Examples
>>> import numpy as np >>> from driada.experiment.synthetic import generate_synthetic_exp >>> >>> # Create two synthetic experiments >>> exp1 = generate_synthetic_exp( ... n_dfeats=3, n_cfeats=0, nneurons=30, ... duration=300, fps=1.0, seed=42, verbose=False ... ) >>> exp2 = generate_synthetic_exp( ... n_dfeats=3, n_cfeats=0, nneurons=25, ... duration=300, fps=1.0, seed=43, verbose=False ... ) >>> >>> # Add simple integer conditions to avoid string issues >>> conditions = np.repeat([1, 2, 3], 100) >>> exp1.dynamic_features['stimulus'] = conditions >>> exp2.dynamic_features['stimulus'] = conditions >>> >>> # Compare neural representations >>> similarity = rsa_between_experiments( ... exp1, exp2, items='stimulus', ... comparison_method='spearman' ... ) >>> # Random synthetic data gives variable similarity >>> print(f"RSA similarity between -1 and 1: {-1 <= similarity <= 1}") RSA similarity between -1 and 1: True
See also
compute_experiment_rdmCompute RDM from single experiment
compare_rdmsDirect RDM comparison
bootstrap_rdm_comparisonBootstrap analysis details
Functions for integrating RSA with DRIADA’s Experiment objects and other analysis pipelines.