fix(viz): Refine implementation of plotting confounds correlations
This PR cleans up the code a little bit, and fixes poldracklab/fmriprep#1620
@@ -604,8 +604,7 @@ def compcor_variance_plot(metadata_files, metadata_sources=None,
for source, mask in data_sources:
if not np.isnan(
metadata.loc[
- (metadata['source'] == source)
- & (metadata['mask'] == mask)
+ (metadata['source'] == source) & (metadata['mask'] == mask)
]['singular_value'].values[0]):
decompositions.append((source, mask))
@@ -619,16 +618,14 @@ def compcor_variance_plot(metadata_files, metadata_sources=None,
ax = [plt.axes()]
for m, (source, mask) in enumerate(decompositions):
- components = metadata[(metadata['mask'] == mask)
- & (metadata['source'] == source)]
+ components = metadata[(metadata['mask'] == mask) & (metadata['source'] == source)]
if len([m for s, m in decompositions if s == source]) > 1:
title_mask = ' ({} mask)'.format(mask)
else:
title_mask = ''
fig_title = '{}{}'.format(source, title_mask)
- ax[m].plot(np.arange(components.shape[0]+1),
- [0] + list(
+ ax[m].plot(np.arange(components.shape[0] + 1), [0] + list(
100 * components['cumulative_variance_explained']),
color='purple',
linewidth=2.5)
@@ -716,19 +713,19 @@ def confounds_correlation_plot(confounds_file, output_file=None, figure=None,
gscorr[reference] = np.abs(gscorr[reference])
gs_descending = gscorr.sort_values(by=reference,
ascending=False)['index']
+ n_vars = corr.shape[0]
+ max_dim = min(n_vars, max_dim)
- if corr.shape[0] > max_dim:
gs_descending = gs_descending[:max_dim]
features = [p for p in corr.columns if p in gs_descending]
corr = corr.loc[features, features]
- n_vars = corr.shape[0]
np.fill_diagonal(corr.values, 0)
if figure is None:
- plt.figure(figsize=(3*n_vars*0.3, n_vars*0.3))
- gs = mgs.GridSpec(1, 15)
- ax0 = plt.subplot(gs[0, :7])
- ax1 = plt.subplot(gs[0, 7:])
+ plt.figure(figsize=(15, 5))
+ gs = mgs.GridSpec(1, 21)
+ ax0 = plt.subplot(gs[0, :10])
+ ax1 = plt.subplot(gs[0, 11:])
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
