-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarizer.py
334 lines (273 loc) · 9.92 KB
/
binarizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
Binarizer module. This module contains functions to convert timestamps to binary signals. This process is basically binning
the timestamps into bins, so that each bin contains a 1 if a spike occurred in that bin, and a 0 otherwise.
This module makes heavy use of the Polars library to increase performance.
@ Marvin Seifert 2024
"""
import polars as pl
from functools import partial
import numpy as np
import scipy.signal as signal
def timestamps_to_binary_polars(df_timestamps, sample_rate=None, max_window=None):
"""
This function converts timestamps to binary signals. It takes a DataFrame with timestamps and converts them to binary
signals. Conversion is done at a given sample rate and considering a window of a given size.
Parameters
----------
df_timestamps : pl.DataFrame
DataFrame with timestamps
sample_rate : int
The sample rate at which the timestamps will be binarized
max_window : int
The maximum window size for the binary signal
Returns
-------
pl.DataFrame
DataFrame with the binary signals
"""
if sample_rate is None:
sample_rate = int(1 / df_timestamps["times_triggered"].sort().diff().min())
if max_window is None:
max_window = np.ceil(df_timestamps["times_triggered"].max()).astype(int)
# Create a DataFrame with the timestamps
# Calculate the indices for the timestamps based on the sampling rate
df_timestamps = df_timestamps.with_columns(
(df_timestamps["times_triggered"] / sample_rate).cast(pl.Int64).alias("index")
)
# Create a DataFrame with a sequence of zeros up to the maximum index
# max_index = df_timestamps.select(pl.col("index").max()).item()
df_binary_signal = pl.DataFrame(
{"index": list(range(max_window + 1)), "value": [0] * (max_window + 1)},
schema={"index": pl.Int64, "value": pl.UInt8},
)
# Overlay the ones onto the zeros based on the calculated indices
df_result = df_binary_signal.join(
df_timestamps, on="index", how="left"
).with_columns(
pl.when(pl.col("times_triggered").is_null())
.then(0)
.otherwise(1)
.cast(pl.UInt8)
.alias("value")
)
if len(df_result) != max_window + 1:
df_result = df_result.unique(subset="index")
df_result = df_result.with_columns(
pl.col("cell_index").fill_null(strategy="backward")
).with_columns(pl.col("repeat").fill_null(strategy="backward"))
df_result = (
df_result.with_columns(pl.col("cell_index").fill_null(strategy="forward"))
.with_columns(pl.col("repeat").fill_null(strategy="forward"))
.select("cell_index", "value", "repeat", "index")
)
# Fill dataframes with single spikes
if df_result["cell_index"].unique().max() is None:
df_result = df_result.with_columns(
cell_index=pl.lit(df_timestamps["cell_index"])
)
df_result = df_result.with_columns(repeat=pl.lit(df_timestamps["repeat"]))
# If cell didn't spike in a repeat, fill with zeros
return df_result[:max_window]
def fill_missing_repeats(max_repeat, nr_bins, df_result):
"""
This function fills missing repeats with zeros.
Parameters
----------
max_repeat : int
The maximum number of repeats
nr_bins : int
The number of bins per repeat
df_result : pl.DataFrame
The DataFrame with the binary signals
Returns
-------
pl.DataFrame
The DataFrame with the missing repeats filled with zeros
"""
if len(df_result["repeat"].unique()) == max_repeat:
return df_result
repeats = df_result["repeat"].unique().to_numpy()
cell_index = df_result["cell_index"][0]
expected_repeats = np.arange(max_repeat)
missing_repeats = np.setdiff1d(expected_repeats, repeats)
dfs = []
for missing_repeat in missing_repeats:
dfs.append(
pl.DataFrame(
data={
"cell_index": [cell_index] * nr_bins,
"value": [0] * nr_bins,
"repeat": [missing_repeat] * nr_bins,
"index": np.arange(nr_bins),
},
schema={
"cell_index": pl.Int64,
"value": pl.UInt8,
"repeat": pl.Int32,
"index": pl.Int64,
},
)
)
dfs.append(df_result)
df_result = pl.concat(dfs)
return df_result
def apply_on_group(sample_rate, max_window, group_df):
"""
Apply the timestamps_to_binary_polars function on a group of a DataFrame.
This is just a wrapper function.
Parameters
----------
sample_rate : int
The sample rate at which the timestamps will be binarized
max_window : int
The maximum window size for the binary signal
group_df : pl.GroupBy
The group of the DataFrame
Returns
-------
pl.DataFrame
The DataFrame with the binary signals
"""
try:
return timestamps_to_binary_polars(group_df, sample_rate, max_window)
except Exception as e: # This is broad, but we want to catch all exceptions
print(group_df)
def timestamps_to_binary_multi(df, bin_size, max_window, max_repeat):
max_window = np.ceil(max_window / bin_size).astype(int)
nr_cells = df["cell_index"].unique().len()
# Reject spikes larger than max_window
df = df.filter(pl.col("times_triggered") <= max_window)
df = reject_single_spikes(
df
) # This filters cells with single spikes or spikes only in one repeat
if nr_cells > 1: # Special case if we have only one cell.
func = partial(apply_on_group, bin_size, max_window)
results = df.group_by("cell_index", "repeat").map_groups(func)
fill_func = partial(fill_missing_repeats, max_repeat, max_window)
results = results.group_by("cell_index").map_groups(fill_func)
else: # One cell
results = timestamps_to_binary_polars(df, bin_size, max_window)
results = fill_missing_repeats(max_repeat, max_window, results)
results = results.sort("cell_index", "repeat", "index")
return results
def binary_as_array(repeats, nr_bins, df):
"""
Extract the binary signal from a DataFrame and reshape it into an array.
Parameters
----------
repeats : int
The number of repeats
nr_bins : int
The number of bins
df : pl.DataFrame
The DataFrame with the binary signals
Returns
-------
np.ndarray
The reshaped binary signal
"""
return np.reshape(
df["value"].to_numpy().astype(np.uint8),
(repeats, nr_bins),
)
def calc_qis(result_df):
"""
Calculate the quality index for a DataFrame with binary signals.
Parameters
----------
result_df : pl.DataFrame
The DataFrame with the binary signals
Returns
-------
np.ndarray
The quality indices
"""
repeats = result_df["repeat"].max() + 1
bins_per_repeat = np.ceil(
(len(result_df.filter(pl.col("cell_index") == result_df["cell_index"][0])) - 1)
/ repeats
).astype(int)
result_df = result_df.partition_by("cell_index")
qis = np.zeros(len(result_df))
for idx, cell in enumerate(result_df):
qis[idx] = quality_index(repeats, bins_per_repeat, cell)
return qis
def quality_index(repeats, nr_bins, df):
"""
Calculate the quality index for a single cell.
Parameters
----------
repeats : int
The number of repeats
nr_bins : int
The number of bins
df : pl.DataFrame
The DataFrame with the binary signals
"""
gauswins = np.tile(kernel_template(sampling_freq=1000)[::-1], (repeats, 1))
array = binary_as_array(repeats, nr_bins, df)
exs = signal.oaconvolve(array, gauswins, mode="valid", axes=1)
return calc_tradqi(exs)
# array = np.reshape(result["value"].to_numpy(), (5, 2401), order="C")
#
# gauswins = np.tile(kernel_template(width=0.0125 / 100)[::-1], (5, 1))
#
# exs = signal.oaconvolve(array, gauswins, mode="valid", axes=1)
#
def calc_tradqi(kernelfits):
"""
Calculates the Quality_index of the (Gaussian-kernel) fitted spike data. Cell-specific and stimulus-specific,
but of nr-of-trials length
Parameters
----------
kernelfits : np.ndarray
The convolved binary signals
Returns
-------
float
The quality index
"""
return np.var(np.mean(kernelfits, 0)) / np.mean(np.var(kernelfits, 0))
def kernel_template(width=0.0100, sampling_freq=17852.767845719834):
"""
create Gaussian kernel by providing the width (FWHM) value. According to wiki, this is approx. 2.355*std of dist.
width=given in seconds, converted internally in sampling points.
Parameters
----------
width : float
The width of the Gaussian kernel in seconds
sampling_freq : float
The sampling frequency
Returns
-------
np.ndarray
The Gaussian kernel
"""
fwhm = int((sampling_freq) * width) # in points
# normalized time vector in ms
k = int((sampling_freq) * 0.02)
gtime = np.arange(-k, k)
# create Gaussian window
gauswin = np.exp(-(4 * np.log(2) * gtime**2) / fwhm**2)
gauswin = gauswin / np.sum(gauswin)
# initialize filtered signal vector
return gauswin
def reject_single_spikes(df):
"""
Reject cells with single spikes or spikes only in one repeat.
Parameters
----------
df : pl.DataFrame
The DataFrame with the binary signals
Returns
-------
pl.DataFrame
The DataFrame with the rejected cells removed
"""
unique_spikes = df.group_by("cell_index").count().filter(pl.col("count") == 1)
unique_repeat = df.group_by("repeat").count().filter(pl.col("count") == 1)
all_unique = np.concatenate(
[unique_spikes["cell_index"].to_numpy(), unique_repeat["repeat"].to_numpy()]
)
unique_cells = np.unique(all_unique)
return df.filter(pl.col("cell_index").is_in(unique_cells) == False)