Skip to content

Commit

Permalink
Produce empty plot when there are no spikes (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
joni-herttuainen committed Jun 1, 2023
1 parent 9fb8cce commit d274c40
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions bluepysnap/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,29 @@ def spikes_firing_rate_histogram(filtered_report, time_binsize=None, ax=None):
if time_binsize is not None and time_binsize <= 0:
raise BluepySnapError(f"Invalid time_binsize = {time_binsize}. Should be > 0.")

times = filtered_report.report.index
node_count = filtered_report.report[["ids", "population"]].drop_duplicates().shape[0]
if ax is None:
ax = plt.gca()
ax.set_xlabel("Time [ms]")
ax.set_ylabel("PSTH [Hz]")

if len(times) == 0:
raise BluepySnapError(
f"No data to display. You should check your 'group' query: {filtered_report.group}."
)
times = filtered_report.report.index

time_start = np.min(times)
time_stop = np.max(times)
if len(times) > 0:
time_start = np.min(times)
time_stop = np.max(times)

if time_binsize is None:
# heuristic for a nice bin size (~100 spikes per bin on average)
time_binsize = min(50.0, (time_stop - time_start) / ((len(times) / 100.0) + 1.0))
if time_binsize is None:
# heuristic for a nice bin size (~100 spikes per bin on average)
time_binsize = min(50.0, (time_stop - time_start) / ((len(times) / 100.0) + 1.0))

bins = np.append(np.arange(time_start, time_stop, time_binsize), time_stop)
hist, bin_edges = np.histogram(times, bins=bins)
freq = 1.0 * hist / node_count / (0.001 * time_binsize)
bins = np.append(np.arange(time_start, time_stop, time_binsize), time_stop)
hist, bin_edges = np.histogram(times, bins=bins)
node_count = filtered_report.report[["ids", "population"]].drop_duplicates().shape[0]
freq = 1.0 * hist / node_count / (0.001 * time_binsize)

if ax is None:
ax = plt.gca()
ax.set_xlabel("Time [ms]")
ax.set_ylabel("PSTH [Hz]")
# use the middle of the bins instead of the start of the bin
ax.plot(0.5 * (bin_edges[1:] + bin_edges[:-1]), freq, label="PSTH", drawstyle="steps-mid")

# use the middle of the bins instead of the start of the bin
ax.plot(0.5 * (bin_edges[1:] + bin_edges[:-1]), freq, label="PSTH", drawstyle="steps-mid")
return ax


Expand Down

0 comments on commit d274c40

Please sign in to comment.