-
Notifications
You must be signed in to change notification settings - Fork 0
/
stimulus_trace.py
372 lines (312 loc) · 12.6 KB
/
stimulus_trace.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 6 12:23:16 2021
This file contains functions, and classes to deal and analyse the stimulus trace
obtained from 3Brain BioCam MEAs by exporting the trigger channel in .brw or .hdf
file formate.
@author: Marvin
"""
from polarspike import backbone, stimulus_spikes
import h5py
import pandas as pd
import numpy as np
import scipy.signal as sg
import polars as pl
import re
class Stimulus_Extractor:
"""
Stimulus trace class
This class reads and handles the stimulus trace. The stimulus trace is plotted and
than the user can select stimulus frames based on the plotted trigger signals.
"""
def __init__(self, stimulus_file, freq=1, stream=0):
"""
Initialize function. Opens the stimulus file and extracts the important
data for further analysis. It also initializes object attributes which
store information about how many stimuli have been defined, and which
position these stimuli have in the stimulus channel.
Parameters
----------
stimulus_file: str: The file location of the stimulus trace.
"""
# Check if file is .brw or .mat
self.recording_folder = stimulus_file[: stimulus_file.rfind("/") + 1]
format = backbone.get_file_ending(stimulus_file)
if format == ".brw":
with h5py.File(stimulus_file, "r") as f:
self.channel = pd.DataFrame(
np.array([f["/3BData/Raw"]])[0, :], columns=["Voltage"]
)
self.sampling_frequency = np.array(
f["/3BRecInfo/3BRecVars/SamplingRate"]
)[0]
if format == ".h5":
with h5py.File(stimulus_file, "r") as f:
try:
self.channel = pd.DataFrame(
np.array(
f["Data//Recording_0//AnalogStream//Stream_2//ChannelData"][
:
][stream]
),
columns=["Voltage"],
)
except KeyError:
self.channel = pd.DataFrame(
np.asarray(
f["Data//Recording_0//AnalogStream//Stream_0//ChannelData"][
0, :
]
),
columns=["Voltage"],
)
self.sampling_frequency = freq
if format == ".dat":
self.channel = pd.DataFrame(
np.fromfile(stimulus_file, dtype=np.int16), columns=["Voltage"]
)
self.sampling_frequency = freq
self.max_Voltage = self.channel.Voltage.max(axis=0)
self.min_Voltage = self.channel.Voltage.min(axis=0)
self.half_Voltage = self.min_Voltage + (self.max_Voltage - self.min_Voltage) / 2
self.Frames = range(0, len(self.channel.index), 1)
self.Time = np.asarray(self.Frames) / self.sampling_frequency
self.channel["Frame"] = self.Frames
self.channel["Time_s"] = self.Time
self.channel.Time_s = pd.to_timedelta(self.channel.Time_s, unit="s")
self.channel.set_index("Time_s", inplace=True)
self.switch = 0
self.begins = []
self.ends = []
self.stimuli = pd.DataFrame()
def downsample(self, dsf):
dsf = int(self.sampling_frequency / convert_time_string_to_frequency(dsf))
channel = self.channel
channel = pl.from_pandas(channel)
channel = channel.sort("Frame")
df = channel.with_columns((channel["Frame"] // dsf).alias("Group_Key"))
grouped_df = df.group_by(["Group_Key"], maintain_order=True).agg(
pl.col("Voltage").max().alias("Voltage")
)
grouped_df = grouped_df.with_columns(
(pl.col("Group_Key").mul(dsf)).alias("Frame")
)
channel = grouped_df.to_pandas()
print(len(channel))
return channel
def get_stim_range_new(self, begins, ends):
"""
Function that calculates the stimulus range and find the trigger times
based on which stimulus borders were selected by the user in the interactive
plotly graph beforehand.
Parameters
----------
Needs a plotted stimulus trigger plotly graph and selected stimulus borders
for at least 1 stimulus.
Returns
-------
returns to self a dataframe containing the stimulus information
"""
self.stimuli = create_stim_df()
self.begins = begins
self.ends = ends
channel = self.channel.set_index("Frame")
for i in range(len(self.begins)):
limits_temp = np.array(
[
self.begins[i] - self.sampling_frequency,
self.ends[i] + self.sampling_frequency,
],
dtype=int,
)
if limits_temp[0] < 0:
limits_temp[0] = 0
if limits_temp[1] < 0:
limits_temp[1] = 0
limits_int = limits_temp.astype(int)
channel_cut = channel[limits_int[0] : limits_int[1]]
channel_log = channel_cut.Voltage > self.half_Voltage
peaks = sg.find_peaks(channel_log, height=1, plateau_size=2)
peaks[0][:] = peaks[0][:] + limits_temp[0]
peaks_left = peaks[1]["left_edges"] + limits_temp[0]
stim_begin = int(peaks_left[0])
trigger_interval = np.diff(peaks_left)
min_trigger_interval = np.max(trigger_interval)
stim_end = int(
peaks_left[-1] + min_trigger_interval
) # Adds time after the last trigger, to get the whole stimulus
# Failsafe: If last trigger was close to the end of the recording make last frame stimulus end
if len(channel) < stim_end:
stim_end = len(channel)
peaks_left = np.append(peaks_left, peaks_left[-1] + min_trigger_interval)
trigger_interval = np.diff(peaks_left)
df_temp = pd.DataFrame()
df_temp["stimulus_name"] = ""
df_temp["begin_fr"] = [stim_begin]
df_temp["end_fr"] = [stim_end]
df_temp["trigger_fr_relative"] = [peaks_left - peaks_left[0]]
df_temp["trigger_int"] = [trigger_interval]
df_temp["stimulus_index"] = [i]
df_temp["stimulus_repeat_logic"] = [1]
df_temp["stimulus_repeat_sublogic"] = [1]
df_temp["sampling_freq"] = self.sampling_frequency
self.stimuli = pd.concat([self.stimuli, df_temp], ignore_index=True)
return self.stimuli
def get_stim_start_end(self, limits):
"""
Function that cuts out one specific limit from the whole trigger channel
Parameters
----------
limits: np.array() size(2): Array containing the left and right limits
of the stimulus window in frames
Returns
-------
channel: The cut out trigger channel for one stimulus
"""
for i in range(0, self.nr_stim_input.value):
channel = self.channel.Voltage[limits[i, 0] : limits[i, 1]]
return channel
def get_changed_names(self):
"""
Adds information about stimulus names to the previously created plotly
plot of the trigger channel.
Parameters
----------
overview: qgrid object: The qgrid overview over the stimulus data frame
overview: dataframe: The stimulus dataframe can also be provided directly.
Returns
-------
"""
self.stimuli = self.stimuli.set_index("stimulus_index")
for stimulus in range(len(self.stimuli)):
self.f.data[stimulus + 1].name = self.stimuli["stimulus_name"].loc[stimulus]
self.stimuli = find_ends(self.stimuli)
self.stimuli = get_nr_repeats(self.stimuli)
def load_from_saved(self, savefile, show_plot=False):
"""
Function that loads a pickled stimulus dataframe
Parameters
----------
savefile: The pickled stimulus dataframe
show_plot=False: IF true, the complete trigger channel is plotted with
respective stimulus ranges and names
Returns
-------
Adds stimulus dataframe and trigger channel figure to self.
"""
stimulus_df = pd.read_pickle(savefile, compression="zip")
self.begins = stimulus_df["Begin_Fr"].to_list()
self.ends = stimulus_df["End_Fr"].to_list()
self.plot_trigger_channel_new("200ms")
self.get_stim_range_new()
self.stimuli = stimulus_df.reset_index(drop=False)
self.get_changed_names()
if show_plot:
self.f.show()
def find_ends(df):
trigger_ends = []
for row in df.itertuples():
stimulus_repeat_logic = row.stimulus_repeat_logic
trigger_int = row.trigger_int
trigger_fr_relative = row.trigger_fr_relative
if stimulus_repeat_logic == 0 or stimulus_repeat_logic >= trigger_int.shape[0]:
trigger_ends.append(trigger_fr_relative[:-1] + trigger_int)
else:
shape = (
stimulus_repeat_logic,
int(trigger_int.shape[0] / stimulus_repeat_logic),
)
ints = trigger_int[: np.multiply(*shape)].reshape(shape)
min_int = np.min(ints, axis=0)
triggers_sorted = trigger_fr_relative[: np.multiply(*shape)].reshape(shape)
trigger_ends.append((triggers_sorted + min_int).flatten())
df["trigger_ends"] = trigger_ends
return df
def get_nr_repeats(df):
repeats = []
nr_triggers = np.asarray([len(x) - 1 for x in df["trigger_fr_relative"]])
nr_repeats = nr_triggers // df["stimulus_repeat_logic"]
df["nr_repeats"] = nr_repeats
return df
def convert_time_string_to_frequency(time_str):
"""
Convert a time string like "150ms" to a frequency (reciprocal of time in seconds).
Parameters:
time_str (str): The time string to convert.
Returns:
float: The frequency derived from the time string.
"""
# Use a regular expression to separate the numeric and unit parts
match = re.match(r"(\d+)(\w+)", time_str)
if not match:
raise ValueError(f"Invalid time string: {time_str}")
# Extract parts
quantity, unit = match.groups()
# Convert quantity to float
quantity = float(quantity)
# Convert to seconds based on the unit
if unit == "ms": # milliseconds
time_s = quantity / 1000
elif unit == "s": # seconds
time_s = quantity
elif unit == "us": # microseconds
time_s = quantity / 1_000_000
elif unit == "ns": # nanoseconds
time_s = quantity / 1_000_000_000
else:
raise ValueError(f"Unrecognized unit: {unit}")
# Return the reciprocal of time as frequency
return 1 / time_s
def create_stim_df():
stimuli_df = pd.DataFrame(
columns=[
"stimulus_name",
"begin_fr",
"end_fr",
"trigger_fr_relative",
"trigger_int",
"stimulus_index",
"stimulus_repeat_logic",
"stimulus_repeat_sublogic",
"sampling_freq",
]
)
stimuli_df = stimuli_df.astype(
{
"stimulus_name": "str",
"begin_fr": "int32",
"end_fr": "int32",
"trigger_fr_relative": "object",
"trigger_int": "object",
"stimulus_index": "int32",
"stimulus_repeat_logic": "int32",
"stimulus_repeat_sublogic": "int32",
"sampling_freq": "float",
}
)
return stimuli_df
# def correct_last_trigger(df):
# sampling_freq = df["sampling_freq"].values[0]
# trigger_fr_relative = []
# for row in df.itertuples():
# stimulus_index = row.stimulus_index
# mean_trigger_times = stimulus_spikes.mean_trigger_times(
# df, [stimulus_index], time="frames"
# )
#
#
#
# # Check if the last trigger matches the mean trigger times
# last_trigger_diff = row.trigger_int[-1]
# if last_trigger_diff > mean_trigger_times[-1]:
# # Correct the last trigger
# new_trigger_fr_relative = row.trigger_fr_relative.copy()
# new_trigger_fr_relative[-1] = (
# new_trigger_fr_relative[-2] + mean_trigger_times[-1]
# )
# trigger_fr_relative.append(new_trigger_fr_relative)
# else:
# trigger_fr_relative.append(row.trigger_fr_relative)
#
# df["trigger_fr_relative"] = trigger_fr_relative
# return df