Preprocessing tools
otsu_intraclass_variance(time_series, threshold)
Otsu's intra-class variance. If all datapoints are above or below the threshold, this will throw a warning that can safely be ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_series
|
array
|
needs to be 1D. |
required |
threshold
|
float
|
This helps with binarizing the signal |
required |
Returns:
Source code in intracranial_ephys_utils/preprocess.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
otsu_threshold(time_series)
Otsu thresholding. I know it's for an image, but it should get the job done here in this time series signal, since it quite literally is two classes with noise. What difference does it make it if the variation in foreground and background happen in time than space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_series
|
array
|
needs to be 1D. |
required |
Returns:
Source code in intracranial_ephys_utils/preprocess.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
decay_step_model(t, t0, initial, ph_inf, tau)
Model a step followed by exponential decay t0: time of step amplitude: size of step baseline: long_run behavior tau: decay time constant
Source code in intracranial_ephys_utils/preprocess.py
57 58 59 60 61 62 63 64 65 66 67 68 69 | |
binarize_ph(ph_signal, sampling_rate, task_time=None, event_threshold=2, debug=True)
Binarizes the photodiode signal using the midpoint of the signal. Use local midpoints if given a tau. New version of this script uses a high pass filter and then peaks to find timepoints at which the signal changes by a lot and fast. We then try to find the exact timepoints when this happens by localizing runs where the high pass filter result is away from 0. We find the change in average signal before and after these timepoints to get a sense of whether it's an event onset or offset. Finally, we assume we'll find some noise so we threshold these onsets and offsets by the point of greatest difference in changes (heuristic for Otsu threshold) :param event_threshold: (float) - tells us how many standard deviations away from the mean to look for events after bandpass filtering for events :param ph_signal: This is the photodiode signal itself (array of floats) :param sampling_rate: How many samples per second (float) :param task_time: This is how long the task took (in minutes). Helpful to zoom in on particular data regions (float) :return: ph_signal_bin: np.array (same length signal as the input) only now the values should only be 1 and 0. :return: event_onsets_final: Array of event onsets - this gives the indices of each run in ph_signal_bin where a series of 1s will start :return: event_offsets_final np.array - same as above but telling the indices of when each run of 1s ends.
Source code in intracranial_ephys_utils/preprocess.py
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 | |
BCI_LFP_processing(lfp_signals, sampling_rate)
This code is meant to resample BCI data down to a numpy array of a more managable sampling, and get rid of power line noise :param lfp_signals: :param sampling_rate: :return:
Source code in intracranial_ephys_utils/preprocess.py
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 | |
broadband_seeg_processing(lfp_signals, sampling_rate, lowfreq, high_freq)
This function takes in an lfp signal and performs basic processing. This function is janky but that's only because decimations of too big a factor result in artifacts, so I need one solution for 32K, and a different one for other sampling rates. Preprocessing is as follows, downsample once. We want to make sure we stay above Nyquist limit, so then we run our a 4th order Butterworth to bandpass from lowfreq to highfreq. Downsample once more to get down to 1KHz sampling rate Finally, we'll pass through a Notch filter to get rid of powerline noise and associated harmonics. WARNING: This is NOT good for European data because of the power line noise there(its 50Hz) :param lfp_signals: (np.array) (1, n_samples) :param sampling_rate: (int) :param lowfreq: (int) lower frequency for bandpass :param high_freq: (int) higher frequency for bandpass :return: processed_signals: numpy array shape (1, n_samples) :return: effective_fs: (int) Final sampling rate after processing
Source code in intracranial_ephys_utils/preprocess.py
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 | |
preprocess_dataset(file_paths, neuro_folder_name, low_pass=1000, task=None, events_file=None)
Read in all data from a given directory and run basic preprocessing on it so I can load it live on my shitty computer. :param file_paths: (list) A list of filenames. Ex(['LAC1.ncs','LAC2.ncs']) :param neuro_folder_name: (Path) The folderpath where the data is held :param task: (optional) A string that dictates the task name, only use if you have the event labels already, so already parsed through the photodiode file and annotated the task duration :param events_file: (optional) (Path) Where the annotation file is located, needed if task is given. :param low_pass: the largest frequency to use for band-pass filtering :return: dataset: Numpy array, shape is (n_channels, n_samples) :return: eff_fs: Effective sampling rate :return: electrode_names: List of electrode names
Source code in intracranial_ephys_utils/preprocess.py
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 | |
save_small_dataset(subject, session, task, events_file, low_pass=1000, data_directory=None)
Load, process, and savedata. :param subject: (string) subject identifier :param session: (string) subject session :param task: (string) task name, used to select only part of entire ncs file, assuming annotations file exists :param events_file: (Path) path to where events annotation file is located :param low_pass: (int) specify low pass frequency, usually :param data_directory: (path) specify where data directory is :return:
Source code in intracranial_ephys_utils/preprocess.py
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 | |
save_as_npy(subject, session, task_name, data_directory, events_file, electrode_selection, one_file=False)
Load data from neuralynx files and package them into .npy files. No preprocessing done to the data, so microwires, and macrocontacts at different sampling rates, treated separately. :param subject: (string) subject identifier :param session: (string) session identifier :param task_name: (string) task identifier :param data_directory: (Path) path object that tells us where the raw data lives (if in the cluster, it won't be in our expected data/subject/session style, hence why this function is the way it is) :param events_file: (Path) path object that tells us where the events file, ideally the events_file contains one event titled f"{task_name} duration" :param electrode_selection: (string) Whether to save macrocontact or microwire data :param one_file: (optional) (bool) whether to package data into one file. If false, package data into different files :return:
Source code in intracranial_ephys_utils/preprocess.py
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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
make_trialwise_data(event_times, electrode_names, fs, dataset, tmin=-1.0, tmax=1.0, baseline=None, annotations=None)
This function serves to convert a dataset that is from start to stop, into one that is organized by trials. :param event_times: (timestamps for trial onsets, offsets, or anything of interest) :param electrode_names: (list) list of strings that contain the name for each signal :param fs: (int) sampling rate :param dataset: (np.array) raw data :param tmin: (opt) :param tmax: (opt) :param baseline: (opt) Tuple that defines the period to use as baseline :param annotations: mne annotations object :return: epochs_object
Source code in intracranial_ephys_utils/preprocess.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
smooth_data(data, fs, window, step)
Smooth data by taking the average in windows, and stepping by some amount of time Expects data to be 3D (number of trials X number of electrodes X number of timepoints) We will smooth by taking the centered average about a window, so the smoothed data will be smaller than expected :param data: np.array :param fs: (int) sampling rate :param window: (float) in seconds, how much to average over, the larger this is the more our signal is smeared. :param step: (float) in seconds. How much to step forward, determines new_fs :return: smoothed_data, new_fs
Source code in intracranial_ephys_utils/preprocess.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 | |