Skip to content

Commit

Permalink
work in progress for plot traces
Browse files Browse the repository at this point in the history
  • Loading branch information
zm711 committed Oct 3, 2023
1 parent d4c1d67 commit 7b81ef3
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion src/spikeanalysis/spike_plotter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Optional, Union, Literal

import numpy as np
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -671,6 +671,10 @@ def plot_zscores_ind(self, z_bar: Optional[list[int]] = None, show_stim: bool =
plt.show()

def plot_latencies(self):
"""
Function for plotting latencies
"""

try:
latency = self.data.latency
except AttributeError:
Expand Down Expand Up @@ -698,7 +702,12 @@ def plot_latencies(self):
plt.figure(dpi=self.dpi)
plt.show()


def plot_isi(self):
"""
Function for plotting ISI distributions
"""

try:
raw_isi = self.data.isi_raw
except AttributeError:
Expand All @@ -711,11 +720,74 @@ def plot_isi(self):
ax.hist(isi, density=True, bins=bins, color="k")
ax.set_xlabel("Time (ms)")
ax.set_ylabel("Counts")
self._despine(ax)
plt.title(f"ISI {cluster}")
plt.tight_layout()
plt.figure(dpi=self.dpi)
plt.show()


def plot_response_trace(self, type: Literal['zscore', 'raw'] = 'zscore', by_neuron: bool = False, by_trial:bool = False, ebar: bool = False, color='black'):

assert type in ['zscore', 'raw'], 'type of data must be zscore or raw'

if type == "zscore":
data = self.data.z_scores
bins = self.data.z_bins
elif type == "raw":
data = self.data.mean_firing_rate
bins = self.data.fr_bins

for stimulus, response in data.items():
current_bins = bins[stimulus]
if by_trial and by_neuron:
for neuron in range(np.shape(response)[0]):
for trial in range(np.shape(response)[1]):
self._plot_one_trace(current_bins, response[neuron, trial, :], ebars=None, color=color, stim=stimulus)
elif by_neuron:
for neuron in range(np.shape(response)[0]):
avg_response = np.mean(response[neuron], axis=0)
ebars = np.std(response[neuron], axis=0)
if ebar:
self._plot_one_trace(current_bins, avg_response, ebars=ebars, color=color,stim=stimulus)
else:
self._plot_one_trace(current_bins, avg_response, ebars=None, color=color,stim=stimulus)
elif by_trial:
for trial in range(np.shape(response)[1]):
avg_response= np.mean(response[:, trial, :], axis=0)
ebars = np.std(response[:, trial, :], axis=0)
if ebar:
self._plot_one_trace(current_bins, avg_response, ebars=ebars, color=color,stim=stimulus)
else:
self._plot_one_trace(current_bins, avg_response, ebars=None, color=color, stim=stimulus)
else:
avg_response = np.mean(np.mean(response, axis=1), axis=0)
if ebar:
self._plot_one_trace(current_bins, avg_response, ebars=ebars, color=color, stim=stimulus)
else:
self._plot_one_trace(current_bins, avg_response, ebars=None, color=color,stim=stimulus)




def _plot_one_trace(self, bins, trace, ebars=None, color='black', stim=''):

fig, ax = plt.subplots(figsize=self.figsize)
ax.plot(bins, trace, color=color)
if ebars is not None:
ax.plot(bins, trace+ebars, color=color)
ax.plot(bins, trace-ebars, color=color)
ax.fill_between(bins, trace-ebars, trace+ebars, color=color, alpha=0.02)

ax.set_xlabel("Time (s)")
ax.set_ylabel(self.y_axis)
self._despine(ax)
plt.title(f"trace {stim}")
plt.tight_layout()
plt.figure(dpi=self.dpi)
plt.show()


def _get_event_lengths(self) -> dict:
"""
Utility function to get the event lengths and convert from samples to seconds on a trial
Expand Down

0 comments on commit 7b81ef3

Please sign in to comment.