|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import plotly.graph_objs as go |
| 4 | +from scipy.ndimage import gaussian_filter1d |
| 5 | +from .. import ephys_no_curation as ephys |
| 6 | + |
| 7 | + |
| 8 | +class QualityMetricFigs(object): |
| 9 | + def __init__(self, key=None, **kwargs) -> None: |
| 10 | + self._key = key |
| 11 | + self._amplitude_cutoff_max = kwargs.get("amplitude_cutoff_maximum", None) |
| 12 | + self._presence_ratio_min = kwargs.get("presence_ratio_minimum", None) |
| 13 | + self._isi_violations_max = kwargs.get("isi_violations_maximum", None) |
| 14 | + self._units = pd.DataFrame() |
| 15 | + |
| 16 | + @property |
| 17 | + def units(self): |
| 18 | + assert self._key, "Must use key when retrieving units for QC figures" |
| 19 | + if self._units.empty: |
| 20 | + restrictions = ["TRUE"] |
| 21 | + if self._amplitude_cutoff_max: |
| 22 | + restrictions.append(f"amplitude_cutoff < {self._amplitude_cutoff_max}") |
| 23 | + if self._presence_ratio_min: |
| 24 | + restrictions.append(f"presence_ratio > {self._presence_ratio_min}") |
| 25 | + if self._isi_violations_max: |
| 26 | + restrictions.append(f"isi_violation < {self._isi_violations_max}") |
| 27 | + " AND ".join(restrictions) |
| 28 | + return ( |
| 29 | + ephys.QualityMetrics |
| 30 | + * ephys.QualityMetrics.Cluster |
| 31 | + * ephys.QualityMetrics.Waveform |
| 32 | + & self._key |
| 33 | + & restrictions |
| 34 | + ).fetch(format="frame") |
| 35 | + return self._units |
| 36 | + |
| 37 | + def _plot_metric( |
| 38 | + self, |
| 39 | + data, |
| 40 | + bins, |
| 41 | + x_axis_label, |
| 42 | + scale=1, |
| 43 | + vline=None, |
| 44 | + ): |
| 45 | + fig = go.Figure() |
| 46 | + fig.update_layout( |
| 47 | + xaxis_title=x_axis_label, |
| 48 | + template="plotly_dark", # "simple_white", |
| 49 | + width=350 * scale, |
| 50 | + height=350 * scale, |
| 51 | + margin=dict(l=20 * scale, r=20 * scale, t=20 * scale, b=20 * scale), |
| 52 | + xaxis=dict(showgrid=False, zeroline=False, linewidth=2, ticks="outside"), |
| 53 | + yaxis=dict(showgrid=False, linewidth=0, zeroline=True, visible=False), |
| 54 | + ) |
| 55 | + if data.isnull().all(): |
| 56 | + return fig.add_annotation(text="No data available", showarrow=False) |
| 57 | + |
| 58 | + histogram, histogram_bins = np.histogram(data, bins=bins, density=True) |
| 59 | + |
| 60 | + fig.add_trace( |
| 61 | + go.Scatter( |
| 62 | + x=histogram_bins[:-1], |
| 63 | + y=gaussian_filter1d(histogram, 1), # TODO: remove smoothing |
| 64 | + mode="lines", |
| 65 | + line=dict(color="rgb(0, 160, 223)", width=2 * scale), # DataJoint Blue |
| 66 | + hovertemplate="%{x:.2f}<br>%{y:.2f}<extra></extra>", |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + if vline: |
| 71 | + fig.add_vline(x=vline, line_width=2 * scale, line_dash="dash") |
| 72 | + |
| 73 | + return fig |
| 74 | + |
| 75 | + def empty_fig(self): # TODO: Remove before submission? |
| 76 | + return self._plot_metric( |
| 77 | + pd.Series(["nan"]), np.linspace(0, 0, 0), "This fig left blank" |
| 78 | + ) |
| 79 | + |
| 80 | + def firing_rate_plot(self): |
| 81 | + return self._plot_metric( |
| 82 | + np.log10(self.units["firing_rate"]), |
| 83 | + np.linspace(-3, 2, 100), # If linear, use np.linspace(0, 50, 100) |
| 84 | + "log<sub>10</sub> firing rate (Hz)", |
| 85 | + ) |
| 86 | + |
| 87 | + def presence_ratio_plot(self): |
| 88 | + return self._plot_metric( |
| 89 | + self.units["presence_ratio"], |
| 90 | + np.linspace(0, 1, 100), |
| 91 | + "Presence ratio", |
| 92 | + vline=0.9, |
| 93 | + ) |
| 94 | + |
| 95 | + def amp_cutoff_plot(self): |
| 96 | + return self._plot_metric( |
| 97 | + self.units["amplitude_cutoff"], |
| 98 | + np.linspace(0, 0.5, 200), |
| 99 | + "Amplitude cutoff", |
| 100 | + vline=0.1, |
| 101 | + ) |
| 102 | + |
| 103 | + def isi_violation_plot(self): |
| 104 | + return self._plot_metric( |
| 105 | + np.log10(self.units["isi_violation"] + 1e-5), # Offset b/c log(0) |
| 106 | + np.linspace(-6, 2.5, 100), # If linear np.linspace(0, 10, 200) |
| 107 | + "log<sub>10</sub> ISI violations", |
| 108 | + vline=np.log10(0.5), |
| 109 | + ) |
| 110 | + |
| 111 | + def snr_plot(self): |
| 112 | + return self._plot_metric(self.units["snr"], np.linspace(0, 10, 100), "SNR") |
| 113 | + |
| 114 | + def iso_dist_plot(self): |
| 115 | + return self._plot_metric( |
| 116 | + self.units["isolation_distance"], |
| 117 | + np.linspace(0, 170, 50), |
| 118 | + "Isolation distance", |
| 119 | + ) |
| 120 | + |
| 121 | + def d_prime_plot(self): |
| 122 | + return self._plot_metric( |
| 123 | + self.units["d_prime"], np.linspace(0, 15, 50), "d-prime" |
| 124 | + ) |
| 125 | + |
| 126 | + def nn_hit_plot(self): |
| 127 | + return self._plot_metric( |
| 128 | + self.units["nn_hit_rate"], |
| 129 | + np.linspace(0, 1, 100), |
| 130 | + "Nearest-neighbors hit rate", |
| 131 | + ) |
0 commit comments