Visualization#
Visualizer#
- class calibrain.visualization.Visualizer(base_save_path='results/figures', logger=None)[source]#
Bases:
object- 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:
- 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 ... )