Visualization#

Visualizer#

class calibrain.visualization.Visualizer(base_save_path='results/figures', logger=None)[source]#

Bases: object

__init__(base_save_path='results/figures', logger=None)[source]#
plot_source_signals(ERP_config, x_trial, x_active_indices=None, units=None, trial_idx=None, title='Source Signals', save_dir=None, file_name=None, show=True)[source]#
plot_sensor_signals(ERP_config, y_trials, trial_idx=None, channels=None, units=None, mode='stack', title='Sensor Signals', save_dir=None, file_name=None, show=True)[source]#
plot_stc_3d_brain(ERP_config, x_one_trial, x_hat_one_trial, orientations, source_units, sample_idx, subject, subjects_dir, fwd_path, save_dir=None, file_name=None, show=False)[source]#

Plot source estimates with complete headless support.

plot_source_and_sensors(x_one_trial, x_active_indices, y_clean_one_trial, y_noisy_one_trial, nnz, ERP_config=None, source_units=202(FIFF_UNIT_AM), sensor_units=107(FIFF_UNIT_V), orientation_type='fixed', max_sensors=3, plot_sensors_together=False, shift=20.0, figsize=(14, 10), file_name='data_simulation.png', save_path='results/figures/data_sim.png', show=False)[source]#

Visualize source activity and sensor measurements.

Plots active source time courses and compares clean vs. noisy sensor signals. Includes a line indicating stimulus onset. Uses self.tmin and self.tmax for the time axis.

Parameters:
  • x (np.ndarray) – Source activity. Shape depends on orientation type.

  • y_clean (np.ndarray) – Clean sensor measurements (n_sensors, n_times).

  • y_noisy (np.ndarray) – Noisy sensor measurements (n_sensors, n_times).

  • active_sources (Optional[np.ndarray], optional) – Indices of non-zero (active) sources. If None, they are determined from x, by default None.

  • nnz_to_plot (int, optional) – Number of non-zero sources to plot. If -1, plot all non-zero sources found, by default -1.

  • sfreq (float, optional) – Sampling frequency in Hz, by default self.sfreq.

  • max_sensors (int, optional) – Maximum number of sensors to plot, by default 3.

  • plot_sensors_together (bool, optional) – If True, plot all sensors on the same subplot. If False, stack plots vertically, by default False.

  • shift (float, optional) – Vertical shift between sensors when plotting together, by default 20.0.

  • figsize (Tuple[float, float], optional) – Figure size for the plot, by default (14, 10).

  • save_path (Optional[str], optional) – Path to save the figure. If None, the figure is not saved, by default β€˜results/figures/data_sim.png’.

  • show (bool, optional) – If True, display the plot, by default False.

Return type:

None

plot_active_sources(x_one_trial_one_time, x_hat_one_trial_one_time, x_active_indices, x_hat_active_indices, n_sources, source_units=202(FIFF_UNIT_AM), orientation_type='fixed', save_path=None, file_name=None, title=None, show=True)[source]#

Plot the active sources at a specific time step, or averaged across time, comparing ground truth and estimated values.

Parameters:
  • x (np.ndarray) – Ground truth values for components specified by active_indices.

  • x_hat (np.ndarray) – Estimated values for components specified by active_indices.

  • x_active_indices (np.ndarray) – Indices of active sources in the ground truth.

  • x_hat_active_indices (np.ndarray) – Indices of active sources in the estimated values.

  • n_sources (int) – Total number of sources.

  • source_units (str, optional) – Units for the source signals, by default FIFF.FIFF_UNIT_AM

  • orientation_type (str, optional) – Orientation type for the plot, by default β€œfixed”

  • save_path (Optional[str], optional) – Path to save the plot, by default None

  • file_name (Optional[str], optional) – Name of the file to save the plot, by default None

  • title (Optional[str], optional) – Title of the plot, by default None

  • show (bool, optional) – Whether to show the plot, by default True

plot_ci(x_one_trial_one_time, x_hat_one_trial_one_time, x_active_indices, x_hat_active_indices, n_sources, source_units, ci_lower, ci_upper, confidence_levels, orientation_type='fixed', sharey=True, file_name='active_sources_ci', save_path=None, show=True, figsize=(12, 6))[source]#
plot_calibration_curve(confidence_levels, empirical_coverage, result=None, which_legend='active_indices', file_name='calibration_curve', save_path=None, show=True)[source]#

Visualizes the calibration curve.

Parameters: - empirical_coverage (np.ndarray): 1D array of empirical coverage values,

corresponding to each confidence level in self.confidence_levels.

  • results (dict): Dictionary possibly containing calibration metrics.

  • which_legend (str): Specifies which set of metrics to display in the legend.

  • file_name (str): Base name for the saved plot file.

plot_all(x_trials, x_active_indices_trials, x_hat_one_trial, x_hat_active_indices_one_trial, y_clean_trials, y_noisy_trials, trial_idx=0, n_sources=1, subject=None, subjects_dir=None, fwd_path=None, nnz=1, ERP_config=None, sample_idx=0, source_units=202(FIFF_UNIT_AM), sensor_units=107(FIFF_UNIT_V), confidence_levels=None, empirical_coverages=None, ci_lower=None, ci_upper=None, orientation_type='fixed', result=None, experiment_dir=None)[source]#

Comprehensive visualization wrapper that generates all plots for experiment analysis.

This is a high-level wrapper function that orchestrates multiple visualization methods to create a complete set of plots for ERP source localization analysis.

Generated Visualizations: - Source signal plots (individual and all trials) - Sensor signal plots (stacked and concatenated) - 3D brain surface plots (if available) - Source-sensor comparison plots - Active source analysis plots - Confidence interval plots - Calibration curve plots

Called Methods: - plot_source_signals() - Source time series visualization - plot_sensor_signals() - Sensor measurements visualization - plot_stc_3d_brain() - 3D brain surface visualization - plot_source_and_sensors() - Combined source-sensor plots - plot_active_sources() - Active source comparison - plot_ci() - Confidence interval visualization - plot_calibration_curve() - Uncertainty calibration analysis

Parameters:
  • x_trials (np.ndarray) – Source activity trials, shape (n_trials, n_sources, n_times)

  • x_active_indices_trials (np.ndarray) – Active source indices per trial

  • x_hat_one_trial (np.ndarray) – Estimated source activity for one trial

  • x_hat_active_indices_one_trial (np.ndarray) – Estimated active source indices for one trial

  • y_clean_trials (np.ndarray) – Clean sensor measurements, shape (n_trials, n_sensors, n_times)

  • y_noisy_trials (np.ndarray) – Noisy sensor measurements, shape (n_trials, n_sensors, n_times)

  • trial_idx (int, optional) – Trial index for single-trial visualizations, by default 0

  • n_sources (int, optional) – Total number of sources, by default 1

  • subject (str, optional) – Subject name for brain plots

  • subjects_dir (str, optional) – FreeSurfer subjects directory

  • fwd_path (str, optional) – Forward model path

  • nnz (int, optional) – Number of non-zero sources, by default 1

  • ERP_config (dict, optional) – ERP configuration parameters

  • sample_idx (int, optional) – Time sample index for brain plots, by default 0

  • source_units (str, optional) – Source signal units, by default FIFF.FIFF_UNIT_AM

  • sensor_units (str, optional) – Sensor signal units, by default FIFF.FIFF_UNIT_V

  • confidence_levels (np.ndarray, optional) – Confidence levels for uncertainty analysis

  • empirical_coverages (dict, optional) – Empirical coverage data for calibration

  • ci_lower (np.ndarray, optional) – Lower confidence bounds

  • ci_upper (np.ndarray, optional) – Upper confidence bounds

  • orientation_type (str, optional) – Source orientation type, by default β€œfixed”

  • result (dict, optional) – Analysis results containing metrics

  • experiment_dir (str, optional) – Experiment directory for saving plots

Notes

This wrapper function automatically saves all plots to organized subdirectories: - data_simulation/ - Data visualization plots - uncertainty_analysis/ - Uncertainty quantification plots

All plots are saved with show=False for batch processing compatibility.

Examples

>>> viz = Visualizer(base_save_path="results/figures")
>>> viz.plot_all(
...     x_trials=x_trials,
...     x_active_indices_trials=active_indices,
...     x_hat_one_trial=x_hat,
...     # ... other parameters
... )
calibrain.visualization.main()[source]#