neuro_py.detectors
DetectDS
¶
Bases: object
Class for detecting dentate spikes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basepath
|
str
|
Path to the folder containing the data |
required |
hilus_ch
|
int
|
Channel number of the hilus signal (0 indexing) |
required |
mol_ch
|
int
|
Channel number of the mol signal (0 indexing) |
required |
noise_ch
|
int
|
Channel number of the noise signal or signal far from dentate (0 indexing) |
None
|
lowcut
|
float
|
Low cut frequency for the signal filter |
10
|
highcut
|
float
|
High cut frequency for the signal filter |
250
|
filter_signal_bool
|
bool
|
If True, the signal will be filtered |
True
|
primary_threshold
|
float
|
Primary threshold for detecting the dentate spikes (difference method only) |
5
|
secondary_threshold
|
float
|
Secondary threshold for detecting the dentate spikes (difference method only) |
required |
primary_thres_mol
|
float
|
Primary threshold for detecting the dentate spikes in the mol signal |
2
|
primary_thres_hilus
|
float
|
Primary threshold for detecting the dentate spikes in the hilus signal |
5
|
min_duration
|
float
|
Minimum duration of the dentate spikes |
0.005
|
max_duration
|
float
|
Maximum duration of the dentate spikes |
0.05
|
filter_order
|
int
|
Order of the filter |
4
|
filter_rs
|
int
|
Resonance frequency of the filter |
20
|
method
|
str
|
Method for detecting the dentate spikes. "difference" for detecting the dentate spikes by difference between the hilus and mol signal "seperately" for detecting the dentate spikes by the hilus and mol signal separately |
'seperately'
|
clean_lfp
|
bool
|
If True, the LFP signal will be cleaned |
False
|
emg_threshold
|
float
|
Threshold for the EMG signal to remove dentate spikes |
0.9
|
Attributes:
| Name | Type | Description |
|---|---|---|
lfp |
AnalogSignalArray
|
LFP signal |
filtered_lfp |
AnalogSignalArray
|
Filtered LFP signal |
mol_hilus_diff |
AnalogSignalArray
|
Difference between the hilus and mol signal |
ds_epoch |
EpochArray
|
EpochArray with the dentate spikes |
peak_val |
ndarray
|
Peak value of the dentate spikes |
Methods:
| Name | Description |
|---|---|
load_lfp |
Load the LFP signal |
filter_signal |
Filter the LFP signal |
get_filtered_lfp |
Get the filtered LFP signal |
get_lfp_diff |
Get the difference between the hilus and mol signal |
detect_ds_difference |
Detect the dentate spikes by difference between the hilus and mol signal |
detect_ds_seperately |
Detect the dentate spikes by the hilus and mol signal separately |
save_ds_epoch |
Save the dentate spikes as an EpochArray |
Examples:
In IDE or python console
>>> from ds_swr.detection.detect_dentate_spike import DetectDS
>>> from neuro_py.io import loading
>>> channel_tags = loading.load_channel_tags(basepath)
>>> dds = DetectDS(
basepath,
channel_tags["hilus"]["channels"] - 1,
channel_tags["mol"]["channels"] - 1
)
>>> dds.detect_ds()
>>> dds.save_ds_epoch()
>>> dds
<DetectDS at 0x17fe787c640: dentate spikes 5,769> of length 1:11:257 minutes
In command line
>>> python detect_dentate_spike.py Z:/Data/Can/OML22/day20
Source code in neuro_py/detectors/dentate_spike.py
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 | |
_detach()
¶
Detach the data from the object to allow for pickling
Source code in neuro_py/detectors/dentate_spike.py
478 479 480 481 482 | |
detect_ds()
¶
Detect the dentate spikes based on the method provided
Source code in neuro_py/detectors/dentate_spike.py
362 363 364 365 366 367 368 369 370 371 372 373 | |
filter_signal()
¶
Filter the LFP signal
Returns:
| Type | Description |
|---|---|
ndarray
|
Filtered LFP signal |
Source code in neuro_py/detectors/dentate_spike.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
get_average_trace(shank=None, window=[-0.15, 0.15])
¶
Get the average LFP trace around the dentate spikes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shank
|
int
|
Shank number of the hilus signal |
None
|
window
|
list
|
Window around the dentate spikes |
[-0.15, 0.15]
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Average LFP trace around the dentate spikes |
ndarray
|
Time lags around the dentate spikes |
Source code in neuro_py/detectors/dentate_spike.py
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 | |
get_xml_data()
¶
Load the XML file to get the number of channels, sampling frequency and shank to channel mapping
Source code in neuro_py/detectors/dentate_spike.py
145 146 147 148 149 150 151 152 153 | |
load(filename)
classmethod
¶
Load a DetectDS object from a pickle file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file where the DetectDS object is saved |
required |
Returns:
| Type | Description |
|---|---|
DetectDS
|
The loaded DetectDS object |
Source code in neuro_py/detectors/dentate_spike.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
load_lfp()
¶
Load the LFP signal
Source code in neuro_py/detectors/dentate_spike.py
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 | |
plot(ax=None, window=[-0.15, 0.15], channel_offset=90000.0)
¶
Plot the average LFP trace around the dentate spikes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
AxesSubplot
|
Axis to plot the average LFP trace |
None
|
window
|
list
|
Window around the dentate spikes |
[-0.15, 0.15]
|
channel_offset
|
float
|
Offset between the channels |
90000.0
|
Returns:
| Type | Description |
|---|---|
AxesSubplot
|
Axis with the average LFP trace |
Source code in neuro_py/detectors/dentate_spike.py
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 | |
save(filename)
¶
Save the DetectDS object as a pickle file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file where the DetectDS object will be saved |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in neuro_py/detectors/dentate_spike.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
save_ds_epoch()
¶
Save the dentate spikes as a cellexplorer mat file
Source code in neuro_py/detectors/dentate_spike.py
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 | |
bimodal_thresh(bimodal_data, max_thresh=np.inf, schmidt=False, max_hist_bins=25, start_bins=10, set_thresh=None, nboot=100, force_bimodal=False)
¶
BimodalThresh: Find threshold between bimodal data modes (e.g., UP vs DOWN states) and return crossing times (UP/DOWN onset/offset times).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bimodal_data
|
array - like
|
Vector of bimodal data |
required |
max_thresh
|
float
|
Maximum threshold value (default: inf) |
inf
|
schmidt
|
bool
|
Use Schmidt trigger with halfway points between trough and peaks (default: False) |
False
|
max_hist_bins
|
int
|
Maximum number of histogram bins to try before giving up (default: 25) |
25
|
start_bins
|
int
|
Minimum number of histogram bins for initial histogram (default: 10) |
10
|
set_thresh
|
float
|
Manually set your own threshold (default: None) |
None
|
nboot
|
int
|
Number of bootstrap iterations for dip test (default: 100) |
100
|
force_bimodal
|
bool
|
If True, skip bimodality test and proceed with threshold detection (default: False) |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
thresh |
float
|
Threshold value between modes |
cross |
dict
|
Dictionary with keys: - 'upints': array of UP state intervals [onsets, offsets] - 'downints': array of DOWN state intervals [onsets, offsets] |
bihist |
dict
|
Dictionary with keys: - 'bins': bin centers - 'hist': counts |
diptest_result |
dict
|
Dictionary with keys: - 'dip': Hartigan's dip test statistic - 'p': p-value for bimodal distribution |
Example
data = np.concatenate([np.random.normal(0, 1, 1000), ... np.random.normal(5, 1, 1000)]) thresh, cross, bihist, diptest_result = bimodal_thresh(data)
Notes
Python translation of BimodalThresh.m from MehrotraLevenstein_2023
Source code in neuro_py/detectors/up_down_state.py
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 668 669 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 702 703 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 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | |
detect_up_down_states(basepath=None, st=None, nrem_epochs=None, region='ILA|PFC|PL|EC1|EC2|EC3|EC4|EC5|MEC|CTX', min_dur=0.03, max_dur=0.5, percentile=20, bin_size=0.01, smooth_sigma=0.02, min_cells=10, save_mat=True, epoch_by_epoch=False, beh_epochs=None, show_figure=False, overwrite=False)
¶
Detect UP and DOWN states in neural data.
UP and DOWN states are identified by computing the total firing rate of all
simultaneously recorded neurons in bins of 10 ms, smoothed with a Gaussian kernel
of 20 ms s.d. Epochs with a firing rate below the specified percentile threshold
are considered DOWN states, while the intervals between DOWN states are classified
as UP states. Epochs shorter than min_dur or longer than max_dur are discarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basepath
|
str
|
Base directory path where event files and neural data are stored. |
None
|
st
|
Optional[SpikeTrain]
|
Spike train data. If None, spike data will be loaded based on specified regions. |
None
|
nrem_epochs
|
Optional[EpochArray]
|
NREM epochs. If None, epochs will be loaded from the basepath. |
None
|
region
|
str
|
Brain regions for loading spikes. The first region is prioritized. |
"ILA|PFC|PL|EC1|EC2|EC3|EC4|EC5|MEC"
|
min_dur
|
float
|
Minimum duration for DOWN states, in seconds. |
0.03
|
max_dur
|
float
|
Maximum duration for DOWN states, in seconds. |
0.5
|
percentile
|
float
|
Percentile threshold for determining DOWN states based on firing rate. |
20
|
bin_size
|
float
|
Bin size for computing firing rates, in seconds. |
0.01
|
smooth_sigma
|
float
|
Standard deviation for Gaussian kernel smoothing, in seconds. |
0.02
|
min_cells
|
int
|
Minimum number of neurons required for analysis. |
10
|
save_mat
|
bool
|
Whether to save the detected UP and DOWN states to .mat files. |
True
|
epoch_by_epoch
|
bool
|
Whether to perform detection epoch by epoch. If True, detection will be performed separately for each sleep epoch. |
False
|
beh_epochs
|
Optional[EpochArray]
|
Optional behavioral epochs to use for epoch-by-epoch detection. If None, sleep epochs will be loaded and used. |
None
|
show_figure
|
bool
|
Whether to display a figure showing firing rates during detected UP and DOWN states. |
False
|
overwrite
|
bool
|
Whether to overwrite existing .mat files when saving detected states. |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[Optional[EpochArray], Optional[EpochArray]]
|
A tuple containing the detected DOWN state epochs and UP state epochs. Returns (None, None) if no suitable states are found or insufficient data is available. |
Examples:
>>> down_state, up_state = detect_up_down_states(basepath="/path/to/data", show_figure=True)
From command line: $ python up_down_state.py /path/to/data
Notes
Detection method based on https://doi.org/10.1038/s41467-020-15842-4
Source code in neuro_py/detectors/up_down_state.py
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 | |
detect_up_down_states_bimodal_thresh(basepath=None, st=None, nrem_epochs=None, region='ILA|PFC|PL|EC1|EC2|EC3|EC4|EC5|MEC|CTX', bin_size=0.01, smooth_sigma=0.02, min_cells=10, save_mat=True, epoch_by_epoch=False, beh_epochs=None, show_figure=False, overwrite=False, schmidt=False, nboot=100, force_bimodal=False)
¶
Detect UP and DOWN states using bimodal_thresh on firing rate distribution.
Uses the same data loading and epoch-by-epoch logic as detect_up_down_states,
but applies Hartigan's dip test and bimodal threshold detection instead of a
fixed percentile. This is useful when UP/DOWN states form a clear bimodal
distribution in the firing rate histogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basepath
|
str
|
Base directory path where event files and neural data are stored. |
None
|
st
|
Optional[SpikeTrainArray]
|
Spike train data. If None, spike data will be loaded based on specified regions. |
None
|
nrem_epochs
|
Optional[EpochArray]
|
NREM epochs. If None, epochs will be loaded from the basepath. |
None
|
region
|
str
|
Brain regions for loading spikes. The first region is prioritized. |
"ILA|PFC|PL|EC1|EC2|EC3|EC4|EC5|MEC|CTX"
|
bin_size
|
float
|
Bin size for computing firing rates, in seconds. |
0.01
|
smooth_sigma
|
float
|
Standard deviation for Gaussian kernel smoothing, in seconds. |
0.02
|
min_cells
|
int
|
Minimum number of neurons required for analysis. |
10
|
save_mat
|
bool
|
Whether to save the detected UP and DOWN states to .mat files. |
True
|
epoch_by_epoch
|
bool
|
Whether to perform detection epoch by epoch. |
False
|
beh_epochs
|
Optional[EpochArray]
|
Optional behavioral epochs to use for epoch-by-epoch detection. |
None
|
show_figure
|
bool
|
Whether to display a figure showing firing rates during detected UP and DOWN states. |
False
|
overwrite
|
bool
|
Whether to overwrite existing .mat files when saving detected states. |
False
|
schmidt
|
bool
|
Use Schmidt trigger (hysteresis) for state transitions in bimodal_thresh. |
False
|
nboot
|
int
|
Number of bootstrap iterations for Hartigan's dip test. Reduce further (e.g., 50) for very long recordings to improve performance. |
100
|
force_bimodal
|
bool
|
If True, skip the bimodality test and force threshold detection even if the distribution appears unimodal. Use with caution. |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[Optional[EpochArray], Optional[EpochArray]]
|
A tuple containing the detected DOWN state epochs and UP state epochs. Returns (None, None) if no suitable states are found or insufficient data is available. |
Source code in neuro_py/detectors/up_down_state.py
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 | |