-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPETH.py
More file actions
626 lines (512 loc) · 25.7 KB
/
PETH.py
File metadata and controls
626 lines (512 loc) · 25.7 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import numpy as np
import pickle
import matplotlib.pyplot as plt
import torch
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from umap import UMAP
from scipy.ndimage import gaussian_filter1d
from scipy.interpolate import interp1d
from multiprocessing import Pool, cpu_count
from matplotlib import cm
from matplotlib.colors import Normalize
# Function to load a pickle file
def load_data(pkl_file):
with open(pkl_file, 'rb') as f:
data = pickle.load(f)
return data
def extract_continuous_data_per_trial(force_times, force_data, event_times, window_start, window_end, common_times):
trial_data_dict = {}
for idx, t0 in enumerate(event_times):
# Calculate window start and end times for this trial
t_start = t0 + window_start
t_end = t0 + window_end
# Find indices in force_times within this window
indices = np.where((force_times >= t_start) & (force_times <= t_end))[0]
if len(indices) == 0:
continue
# Extract data segment for this trial
times_segment = force_times[indices] - t0 # Shift times relative to t0
data_segment = force_data[indices]
# Interpolate onto common_times
f = interp1d(times_segment, data_segment, kind='linear', bounds_error=False, fill_value="extrapolate")
interpolated_data = f(common_times) # Shape: (len(common_times),)
# Store the interpolated data
trial_data_dict[idx] = interpolated_data[np.newaxis, :] # Shape: (1, len(common_times))
return trial_data_dict
# Function to extract spike times for each subunit
def extract_spike_times(data_dict, unit_selection):
spike_times_dict = {}
for channel_key in data_dict:
channel_data = data_dict[channel_key]
channel_number = channel_key.replace('Channel', '').lstrip('0')
unit_keys = []
if unit_selection == 'unit1' or unit_selection == 'both':
unit_key1 = f'ID_ch{channel_number}#1'
unit_keys.append(unit_key1)
if unit_selection == 'unit2' or unit_selection == 'both':
unit_key2 = f'ID_ch{channel_number}#2'
unit_keys.append(unit_key2)
for unit_key in unit_keys:
if unit_key in channel_data:
spike_times = channel_data[unit_key]['spike_times']
spike_times_dict[unit_key] = spike_times
else:
print(f"Unit {unit_key} not found in {channel_key}.")
return spike_times_dict
# Function to bin spike times
def bin_spike_times(spike_times_list, bin_size, duration):
n_neurons = len(spike_times_list)
n_bins = int(np.ceil(duration / bin_size))
spike_counts = np.zeros((n_neurons, n_bins))
bin_edges = np.arange(0, duration + bin_size, bin_size)
bin_times = (bin_edges[:-1] + bin_edges[1:]) / 2 # Centers of bins
for i, neuron_spike_times in enumerate(spike_times_list):
if len(neuron_spike_times) > 0:
counts, _ = np.histogram(neuron_spike_times, bins=bin_edges)
spike_counts[i, :] = counts.astype(float)
else:
spike_counts[i, :] = 0
return spike_counts, bin_times
# Function to smooth data with a Gaussian filter
def smooth_data(data, sigma=1):
smoothed_data = gaussian_filter1d(data, sigma=sigma, axis=1)
return smoothed_data
# Function to apply PCA using PyTorch
def apply_pca_torch(data, n_components=None, return_components=False):
data_tensor = torch.tensor(data, dtype=torch.float32).cuda()
data_mean = torch.mean(data_tensor, dim=0)
data_centered = data_tensor - data_mean
cov_matrix = torch.mm(data_centered.t(), data_centered) / (data_centered.shape[0] - 1)
eigenvalues, eigenvectors = torch.linalg.eigh(cov_matrix)
idx = torch.argsort(eigenvalues, descending=True)
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
if n_components is not None:
eigenvectors = eigenvectors[:, :n_components]
eigenvalues = eigenvalues[:n_components]
pca_result = torch.mm(data_centered, eigenvectors)
explained_variance = eigenvalues / torch.sum(eigenvalues)
pca_result = pca_result.cpu().numpy()
explained_variance = explained_variance.cpu().numpy()
eigenvectors = eigenvectors.cpu().numpy()
if return_components:
return pca_result, explained_variance, eigenvectors
else:
return pca_result, explained_variance
# Function to visualize variance explained by PCA
def plot_variance_explained_single(explained_variance):
components = np.arange(1, len(explained_variance) + 1)
cumulative_variance = np.cumsum(explained_variance) * 100
plt.figure(figsize=(8, 6))
plt.bar(components, explained_variance * 100, alpha=0.7, label='Variance explained by component')
plt.plot(components, cumulative_variance, marker='o', color='red', label='Cumulative variance')
plt.xlabel('Principal Component')
plt.ylabel('Variance Explained (%)')
plt.title('Variance Explained by Principal Components')
plt.ylim(0, 100)
plt.legend()
plt.grid(True)
plt.show()
# Function to apply UMAP
def apply_umap(data, n_components=3):
umap = UMAP(n_components=n_components)
umap_result = umap.fit_transform(data)
return umap_result
# Function to apply t-SNE
def apply_tsne(data, n_components=3):
tsne = TSNE(n_components=n_components)
tsne_result = tsne.fit_transform(data)
return tsne_result
# Function to extract projected data per trial with interpolation onto common_times
def extract_projected_data_per_trial(data, event_times, bin_size, window_start, window_end, common_times):
"""
Extracts the projected data per trial, interpolated onto common_times.
Parameters:
- data: numpy array of shape (T, n_components)
- event_times: list or array of event times
- bin_size: time resolution
- window_start: start of the window relative to event (e.g., -1.0)
- window_end: end of the window relative to event (e.g., 2.0)
- common_times: array of time points to interpolate onto
Returns:
- trial_data_dict: dictionary where keys are trial indices, and values are matrices of n_components x len(common_times)
"""
trial_data_dict = {}
for idx, t0 in enumerate(event_times):
# Shift times relative to t_0
relative_times = np.arange(0, len(data)) * bin_size - t0
# Find indices corresponding to the time window
indices = np.where((relative_times >= window_start) & (relative_times <= window_end))[0]
if len(indices) == 0:
continue
# Extract data segments for this time window
segment = data[indices, :] # shape: (len(indices), n_components)
times_segment = relative_times[indices]
# Interpolate onto the common time grid to align results
interpolated_data = np.zeros((len(common_times), segment.shape[1]))
for i in range(segment.shape[1]):
f = interp1d(times_segment, segment[:, i], kind='linear',
bounds_error=False, fill_value="extrapolate")
interpolated_data[:, i] = f(common_times)
# Store the interpolated data
trial_data_dict[idx] = interpolated_data.T # shape: n_components x len(common_times)
return trial_data_dict
# Function to average across all trials
def average_across_trials(extracted_data):
extracted_data_array = np.array(list(extracted_data.values())) # Shape: (n_trials, n_components, n_times)
average_data = np.mean(extracted_data_array, axis=0) # Shape: (n_components, n_times)
return average_data
# Wrapper function for multiprocessing
def process_unit(unit_key, spike_times, bin_size, duration, sigma):
binned_data, bin_times = bin_spike_times([spike_times], bin_size, duration)
smoothed_data = smooth_data(binned_data, sigma=sigma)
return unit_key, smoothed_data
# Align projected data to event times
def align_projected_data(projection_data, event_times, bin_size, window_start, window_end, common_times):
n_components = projection_data.shape[1]
aligned_data = {i: [] for i in range(n_components)}
for t0 in event_times:
relative_times = np.arange(0, len(projection_data) * bin_size, bin_size) - t0
indices = np.where((relative_times >= window_start) & (relative_times <= window_end))[0]
if len(indices) == 0:
continue
for i in range(n_components):
segment = projection_data[indices, i]
interp_data = np.interp(common_times, relative_times[indices], segment)
aligned_data[i].append(interp_data)
return aligned_data
def compute_mean_std(data_array, axis, indices_to_average):
"""
Computes the mean and standard deviation over specified axis.
Parameters:
- data_array: numpy array of shape (n_trials, n_components, len(common_times))
- axis: integer, axis over which to compute mean and std (0 for trials, 1 for components)
- indices_to_average: list of indices to average over (either trial indices or component indices)
Returns:
- mean_data: numpy array of shape determined by the operation
- std_data: numpy array of shape determined by the operation
"""
# Select the data along the axis
if axis == 0:
# Averaging over trials
selected_data = data_array[indices_to_average, :, :] # Shape: (n_selected_trials, n_components, len(common_times))
elif axis == 1:
# Averaging over components
selected_data = data_array[:, indices_to_average, :] # Shape: (n_trials, n_selected_components, len(common_times))
else:
raise ValueError("Axis must be 0 (trials) or 1 (components).")
# Compute mean and std over the specified axis
mean_data = np.mean(selected_data, axis=axis)
std_data = np.std(selected_data, axis=axis)
return mean_data, std_data
def plot_peth_with_error(mean_data, std_data, common_times, method_name, averaged_over='trials', indices=None):
"""
Plots the mean ± std over time.
Parameters:
- mean_data: numpy array of mean values
- std_data: numpy array of std values
- common_times: array of time points
- method_name: string, name of the dimensionality reduction method
- averaged_over: string, 'trials' or 'components' to indicate averaging
- indices: list of indices that were averaged over
"""
plt.figure(figsize=(10, 7))
if averaged_over == 'trials':
n_components = mean_data.shape[0]
for comp in range(n_components):
plt.plot(common_times, mean_data[comp, :], label=f'{method_name} Component {comp + 1}')
plt.fill_between(common_times,
mean_data[comp, :] - std_data[comp, :],
mean_data[comp, :] + std_data[comp, :],
alpha=0.3)
plt.title(f'PETH Averaged Over Trials ({method_name})')
plt.ylabel("Component Value")
elif averaged_over == 'components':
n_trials = mean_data.shape[0]
for idx in range(n_trials):
plt.plot(common_times, mean_data[idx, :], label=f'Trial {indices[idx] + 1}')
plt.fill_between(common_times,
mean_data[idx, :] - std_data[idx, :],
mean_data[idx, :] + std_data[idx, :],
alpha=0.3)
plt.title(f'PETH Averaged Over Components ({method_name})')
plt.ylabel("Mean Component Value")
else:
raise ValueError("averaged_over must be 'trials' or 'components'.")
plt.axvline(0, color='red', linestyle='--')
plt.xlabel("Time (s) relative to event")
plt.legend()
plt.grid(True)
plt.show()
def compute_component_mean_std(trial_data, pcs_to_average):
"""
Computes the mean and standard deviation over selected components for a single trial.
Parameters:
- trial_data: numpy array of shape (n_components, len(common_times))
- pcs_to_average: list of component indices (zero-based) to average over
Returns:
- mean_over_components: array of shape (len(common_times),)
- std_over_components: array of shape (len(common_times),)
"""
# Select the components
selected_data = trial_data[pcs_to_average, :] # shape: (n_selected_components, len(common_times))
# Compute mean and std over components (axis=0)
mean_over_components = np.mean(selected_data, axis=0) # shape: (len(common_times),)
std_over_components = np.std(selected_data, axis=0) # shape: (len(common_times),)
return mean_over_components, std_over_components
# Main code
if __name__ == "__main__":
pkl_file = 'experiment_data.pkl'
tdt_file = 'force.pkl'
data = load_data(pkl_file)
data_dict = data['data']
tdt_signals = load_data(tdt_file)
t_0_times = pkl_file['Event Time']
unit_selection = 'unit2'
spike_times_dict = extract_spike_times(data_dict, unit_selection)
if not spike_times_dict:
raise ValueError("No spike times were extracted. Please check your unit selection and data.")
duration_list = [np.max(spike_times) for spike_times in spike_times_dict.values() if len(spike_times) > 0]
if duration_list:
duration = max(duration_list)
else:
raise ValueError("No spike times found in the data.")
bin_size = 0.005
smoothing_length = 0.05
sigma = (smoothing_length / bin_size) / 2
# Use multiprocessing to process each unit in parallel
with Pool(cpu_count()) as pool:
results = pool.starmap(process_unit, [(unit_key, spike_times, bin_size, duration, sigma) for unit_key, spike_times in spike_times_dict.items()])
smoothed_data_dict = {unit_key: smoothed_data for unit_key, smoothed_data in results}
all_smoothed_data = np.vstack([data for data in smoothed_data_dict.values()])
all_smoothed_data_T = all_smoothed_data.T # Shape: (T, n_neurons)
# Variables for PETH visualization
window_start = -1.0
window_end = 2.0
bin_size = 0.005
# Define common_times using the same window and bin_size
common_times = np.arange(window_start, window_end + bin_size, bin_size)
# Variables to handle saving
save_data = False
save_filename = 'projected_data_test.pkl'
# Variable for PETH visualization
PETH = True
# Specify the maximum number of PCs to plot
max_pc = 3
# Data containers for results and averages
results = {}
averages = {}
saved_data = {}
# Parameters to specify
average_over = 'trials' # 'trials' or 'components'
# For averaging over trials
pcs_to_use = [0, 1, 2] # Zero-based indices of PCs to use
trials_to_average = 'all' # 'all' or list of trial indices
# For averaging over components
trials_to_use = [0] # Zero-based indices of trials to use
pcs_to_average = 'all' # 'all' or list of component indices ([0, 1, 2])
# Trial selection variable for visualization
trial_selection = 'all' # Can be 'all', an integer, or a list of integers or None
selected_methods = ['PCA'] # Use 'PCA', 'UMAP', 't-SNE', or 'all' to select methods
show_average = False # Set to False if you do not want to see the average projections
projection_dim = 1 # Set to 1, 2, or 3 for 1D, 2D, or 3D projections
# Apply PCA if selected
if 'PCA' in selected_methods or selected_methods == 'all':
try:
pca_result, explained_variance, pca_components = apply_pca_torch(all_smoothed_data_T, return_components=True)
results['PCA'] = pca_result
# Extract projected data per trial with interpolation
pca_trial_data = extract_projected_data_per_trial(
pca_result, t_0_times, bin_size, window_start, window_end, common_times
)
# Store the extracted data in the specified format
saved_data['PCA'] = pca_trial_data
# Average over trials for visualization
averages['PCA'] = average_across_trials(pca_trial_data)
except Exception as e:
print(f"PCA failed for bin_size {bin_size}s and smoothing_length {smoothing_length}s: {e}")
# Apply UMAP if selected
if 'UMAP' in selected_methods or selected_methods == 'all':
try:
umap_result = apply_umap(all_smoothed_data_T)
results['UMAP'] = umap_result
# Extract projected data per trial with interpolation
umap_trial_data = extract_projected_data_per_trial(
umap_result, t_0_times, bin_size, window_start, window_end, common_times
)
# Store the extracted data
saved_data['UMAP'] = umap_trial_data
# Average over trials for visualization
averages['UMAP'] = average_across_trials(umap_trial_data)
except Exception as e:
print(f"UMAP failed: {e}")
# Apply t-SNE if selected
if 't-SNE' in selected_methods or selected_methods == 'all':
try:
tsne_result = apply_tsne(all_smoothed_data_T)
results['t-SNE'] = tsne_result
# Extract projected data per trial with interpolation
tsne_trial_data = extract_projected_data_per_trial(
tsne_result, t_0_times, bin_size, window_start, window_end, common_times
)
# Store the extracted data
saved_data['t-SNE'] = tsne_trial_data
# Average over trials for visualization
averages['t-SNE'] = average_across_trials(tsne_trial_data)
except Exception as e:
print(f"t-SNE failed: {e}")
try:
# Extract 'Force' data
force_fs = 1017.3
force_data = tdt_signals['x'] # Shape: (N,)
# Create time axis for 'Force' data
force_times = np.arange(len(force_data)) / force_fs
# Extract per-trial 'Force' data
force_trial_data = extract_continuous_data_per_trial(
force_times, force_data, t_0_times, window_start, window_end, common_times
)
# Save 'Force' data into 'saved_data'
saved_data['Force'] = force_trial_data
# Optionally, compute average over trials for visualization
averages['Force'] = average_across_trials(force_trial_data)
except Exception as e:
print(f"Processing Force data failed: {e}")
# Save the projected data if required
if save_data:
with open(save_filename, 'wb') as f:
pickle.dump(saved_data, f)
if PETH:
for method in selected_methods:
if method not in saved_data:
print(f"Skipping PETH for {method} as the method did not run successfully.")
continue
trial_data_dict = saved_data[method]
# Convert to array: Shape (n_trials, n_components, len(common_times))
data_array = np.array([trial_data_dict[idx] for idx in sorted(trial_data_dict.keys())])
n_trials, n_components, n_times = data_array.shape
if average_over == 'trials':
# Average over trials for selected PCs
if pcs_to_use == 'all':
pcs_to_use_indices = list(range(n_components))
else:
pcs_to_use_indices = pcs_to_use
if trials_to_average == 'all':
trials_to_average_indices = list(range(n_trials))
else:
trials_to_average_indices = trials_to_average
# Select the data
data_selected = data_array[trials_to_average_indices, :, :] # Shape: (n_selected_trials, n_components, n_times)
# Keep only selected PCs
data_selected = data_selected[:, pcs_to_use_indices, :] # Shape: (n_selected_trials, n_selected_PCs, n_times)
# Compute mean and std over trials (axis=0)
mean_data = np.mean(data_selected, axis=0) # Shape: (n_selected_PCs, n_times)
std_data = np.std(data_selected, axis=0)
# Plot
plot_peth_with_error(mean_data, std_data, common_times, method, averaged_over='trials')
elif average_over == 'components':
# Average over components for selected trials
if pcs_to_average == 'all':
pcs_to_average_indices = list(range(n_components))
else:
pcs_to_average_indices = pcs_to_average
if trials_to_use == 'all':
trials_to_use_indices = list(range(n_trials))
else:
trials_to_use_indices = trials_to_use
# Select the data
data_selected = data_array[trials_to_use_indices, :, :] # Shape: (n_selected_trials, n_components, n_times)
# Keep only selected PCs
data_selected = data_selected[:, pcs_to_average_indices, :] # Shape: (n_selected_trials, n_selected_PCs, n_times)
# Compute mean and std over components (axis=1)
mean_data = np.mean(data_selected, axis=1) # Shape: (n_selected_trials, n_times)
std_data = np.std(data_selected, axis=1)
# Plot
plot_peth_with_error(mean_data, std_data, common_times, method, averaged_over='components', indices=trials_to_use_indices)
else:
print("Invalid option for average_over. Choose 'trials' or 'components'.")
# Visualize the average for each selected method if show_average is True
if show_average and trial_selection is not None:
# Create a normalization object for the color map from window_start to window_end
norm = plt.Normalize(vmin=window_start, vmax=window_end)
colors = plt.cm.hot(norm(common_times))
for method_name in selected_methods:
if method_name in averages and averages[method_name] is not None:
average_data = averages[method_name] # Shape: (n_components, len(common_times))
# Ensure that common_times and average_data have matching lengths
if len(common_times) != average_data.shape[1]:
print(f"Length mismatch: common_times has length {len(common_times)}, "
f"but average_data has length {average_data.shape[1]}")
continue # Skip plotting if lengths don't match
fig = plt.figure(figsize=(10, 7))
# Check if enough components are available for the desired projection_dim
n_components = average_data.shape[0]
if n_components < projection_dim:
print(f"Not enough components to plot {projection_dim}D projection for {method_name}. "
f"Available components: {n_components}")
continue
# Plot based on projection_dim
if projection_dim == 1:
ax = fig.add_subplot(111)
for i in range(len(common_times) - 1):
ax.plot(common_times[i:i+2], average_data[0, i:i+2], color=colors[i])
ax.set_xlabel('Time (s)')
ax.set_ylabel(f'{method_name}1')
ax.set_title(f'Average 1D Projection ({method_name})')
elif projection_dim == 2:
ax = fig.add_subplot(111)
for i in range(len(common_times) - 1):
ax.plot(average_data[0, i:i+2], average_data[1, i:i+2], color=colors[i])
ax.set_xlabel(f'{method_name}1')
ax.set_ylabel(f'{method_name}2')
ax.set_title(f'Average 2D Projection ({method_name})')
elif projection_dim == 3:
ax = fig.add_subplot(111, projection='3d')
for i in range(len(common_times) - 1):
ax.plot(
average_data[0, i:i+2],
average_data[1, i:i+2],
average_data[2, i:i+2],
color=colors[i]
)
ax.set_xlabel(f'{method_name}1')
ax.set_ylabel(f'{method_name}2')
ax.set_zlabel(f'{method_name}3')
ax.set_title(f'Average 3D Projection ({method_name})')
else:
raise ValueError("Invalid projection_dim. Choose 1, 2, or 3.")
time_markers = {
window_start: 'red',
0.0: 'green',
window_end: 'black'
}
# Plot markers with a small tolerance to match closest time in common_times
tolerance = 1e-6
for t_mark, color in time_markers.items():
idx_t = np.where(np.abs(common_times - t_mark) < tolerance)[0]
if idx_t.size > 0:
idx_t = idx_t[0]
if projection_dim == 1:
ax.scatter(common_times[idx_t], average_data[0, idx_t], color=color, s=50, marker='o')
elif projection_dim == 2:
ax.scatter(
average_data[0, idx_t],
average_data[1, idx_t],
color=color,
s=50,
marker='o'
)
elif projection_dim == 3:
ax.scatter(
average_data[0, idx_t],
average_data[1, idx_t],
average_data[2, idx_t],
color=color,
s=50,
marker='o'
)
# Add color bar with range window_start to window_end
sm = plt.cm.ScalarMappable(cmap='hot', norm=norm)
sm.set_array([])
plt.colorbar(sm, ax=ax, label='Time (s)')
plt.show()