Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-page pdf pagination for recording figures #173

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions bluepyefe/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from bluepyefe.ecode import eCodes
from bluepyefe.reader import *
from bluepyefe.plotting import _save_fig
from matplotlib.backends.backend_pdf import PdfPages

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -230,52 +231,58 @@ def plot_recordings(self, protocol_name, output_dir=None, show=False):
recordings_sorted = [recordings[k] for k in numpy.argsort(recordings_amp)]

n_cols = 6
max_plots_per_page = 24
max_plots_per_page = 12
total_pages = int(numpy.ceil(len(recordings_sorted) / max_plots_per_page))

for page in range(total_pages):
start_idx = page * max_plots_per_page
end_idx = start_idx + max_plots_per_page
page_recordings = recordings_sorted[start_idx:end_idx]
if output_dir is not None:
filename = f"{self.name}_{protocol_name}_recordings.pdf"
dirname = pathlib.Path(output_dir) / self.name
dirname.mkdir(parents=True, exist_ok=True)
filepath = dirname / filename

n_rows = int(numpy.ceil(len(page_recordings) / n_cols)) * 2
with PdfPages(filepath) as pdf:
for page in range(total_pages):
start_idx = page * max_plots_per_page
end_idx = start_idx + max_plots_per_page
page_recordings = recordings_sorted[start_idx:end_idx]

fig, axs = plt.subplots(
n_rows, n_cols,
figsize=[3.0 * n_cols, 2.5 * n_rows],
squeeze=False
)
n_rows = int(numpy.ceil(len(page_recordings) / n_cols)) * 2

for i, rec in enumerate(page_recordings):
col = i % n_cols
row = (i // n_cols) * 2
fig, axs = plt.subplots(
n_rows, n_cols,
figsize=[3.0 * n_cols, 2.5 * n_rows],
squeeze=False
)

display_ylabel = col == 0
display_xlabel = (row // 2) + 1 == n_rows // 2
for i, rec in enumerate(page_recordings):
col = i % n_cols
row = (i // n_cols) * 2

rec.plot(
axis_current=axs[row][col],
axis_voltage=axs[row + 1][col],
display_xlabel=display_xlabel,
display_ylabel=display_ylabel
)
display_ylabel = col == 0
display_xlabel = (row // 2) + 1 == n_rows // 2

rec.plot(
axis_current=axs[row][col],
axis_voltage=axs[row + 1][col],
display_xlabel=display_xlabel,
display_ylabel=display_ylabel
)

fig.suptitle(f"Cell: {self.name}, Experiment: {protocol_name}, Page: {page + 1}")
plt.subplots_adjust(wspace=0.53, hspace=0.7)
fig.suptitle(f"Cell: {self.name}, Experiment: {protocol_name}, Page: {page + 1}")
plt.subplots_adjust(wspace=0.53, hspace=0.7)

for ax in axs.flatten():
if not ax.lines:
ax.set_visible(False)
for ax in axs.flatten():
if not ax.lines:
ax.set_visible(False)

plt.margins(0, 0)
plt.margins(0, 0)

if show:
fig.show()
if show:
plt.show()

if output_dir is not None:
filename = f"{self.name}_{protocol_name}_recordings_page_{page + 1}.pdf"
dirname = pathlib.Path(output_dir) / self.name
_save_fig(dirname, filename)
pdf.savefig(fig, dpi=80)
plt.close("all")
plt.clf()

def plot_all_recordings(self, output_dir=None, show=False):
"""Plot all the recordings of the cell.
Expand Down
Loading