Skip to content

Commit

Permalink
fix: remove calls to plt.gcf() as they are problematic with memory
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Aug 25, 2024
1 parent 6fd489b commit 0321b0a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
29 changes: 2 additions & 27 deletions nireports/reportlets/nuisance.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,26 +614,6 @@ def spikesplot(
return ax


def spikesplot_cb(position, cmap="viridis", fig=None):
# Add colorbar
if fig is None:
fig = plt.gcf()

cax = fig.add_axes(position)
cb = ColorbarBase(
cax,
cmap=mpl.colormaps[cmap],
spacing="proportional",
orientation="horizontal",
drawedges=False,
)
cb.set_ticks([0, 0.5, 1.0])
cb.set_ticklabels(["Inferior", "(axial slice)", "Superior"])
cb.outline.set_linewidth(0)
cb.ax.xaxis.set_tick_params(width=0)
return cax


def confoundplot(
tseries,
gs_ts,
Expand Down Expand Up @@ -902,8 +882,7 @@ def confounds_correlation_plot(
corr = corr.loc[features, features]
np.fill_diagonal(corr.values, 0)

if figure is None:
plt.figure(figsize=(15, 5))
figure = figure if figure is not None else plt.figure(figsize=(15, 5))
gs = GridSpec(1, 21)
ax0 = plt.subplot(gs[0, :10])
ax1 = plt.subplot(gs[0, 11:])
Expand Down Expand Up @@ -944,7 +923,6 @@ def confounds_correlation_plot(
ax1.spines[side].set_visible(False)

if output_file is not None:
figure = plt.gcf()
figure.savefig(output_file, bbox_inches="tight")
plt.close(figure)
figure = None
Expand Down Expand Up @@ -1120,9 +1098,7 @@ def plot_raincloud(
df_clip = df.copy(deep=True)
df_clip[feature] = df[feature].clip(lower=lower_limit_value, upper=upper_limit_value)

if figure is None:
plt.figure(figsize=(7, 5))

figure = figure if figure is not None else plt.figure(figsize=(7, 5))
gs = GridSpec(1, 1)
ax = plt.subplot(gs[0, 0])

Expand Down Expand Up @@ -1216,7 +1192,6 @@ def plot_raincloud(
)

if output_file is not None:
figure = plt.gcf()
plt.tight_layout()
figure.savefig(output_file, bbox_inches="tight")
plt.close(figure)
Expand Down
3 changes: 1 addition & 2 deletions nireports/reportlets/xca.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def compcor_variance_plot(
elif len(decompositions) > 1:
fig, ax = plt.subplots(1, len(decompositions), figsize=(5 * len(decompositions), 5))
else:
fig = plt.gcf()
ax = [plt.axes()]

for m, (source, mask) in enumerate(decompositions):
Expand Down Expand Up @@ -367,8 +368,6 @@ def compcor_variance_plot(
ax[m].spines[side].set_visible(False)

if output_file is not None:
if fig is None:
fig = plt.gcf()
fig.savefig(output_file, bbox_inches="tight")
fig.clf()
fig = None
Expand Down

0 comments on commit 0321b0a

Please sign in to comment.