intervals
_find_intersecting_intervals(set1, set2)
Find the amount of time two sets of intervals are intersecting each other for each interval in set1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
set1 | ndarray | An array of intervals represented as pairs of start and end times. | required |
set2 | ndarray | An array of intervals represented as pairs of start and end times. | required |
Returns:
Type | Description |
---|---|
list of float | A list of floats, where each float represents the amount of time the corresponding interval in set1 intersects with any interval in set2. |
Source code in neuro_py/process/intervals.py
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 |
|
find_intersecting_intervals(set1, set2, return_indices=True)
Find the amount of time two sets of intervals are intersecting each other for each intersection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
set1 | nelpy EpochArray | The first set of intervals to check for intersections. | required |
set2 | nelpy EpochArray | The second set of intervals to check for intersections. | required |
return_indices | bool | If True, return the indices of the intervals in set2 that intersect with each interval in set1. If False, return the amount of time each interval in set1 intersects with any interval in set2. | True |
Returns:
Type | Description |
---|---|
Union[ndarray, List[bool]] | If return_indices is True, returns a boolean array indicating whether each interval in set1 intersects with any interval in set2. If return_indices is False, returns a NumPy array with the amount of time each interval in set1 intersects with any interval in set2. |
Examples:
>>> set1 = nel.EpochArray([(1, 3), (5, 7), (9, 10)])
>>> set2 = nel.EpochArray([(2, 4), (6, 8)])
>>> find_intersecting_intervals(set1, set2)
[True, True, False]
>>> find_intersecting_intervals(set1, set2, return_indices=False)
[1, 2, 0]
Source code in neuro_py/process/intervals.py
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 |
|
find_interval(logical)
Find consecutive intervals of True values in a list of boolean values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logical | List[bool] | The list of boolean values. | required |
Returns:
Type | Description |
---|---|
List[Tuple[int, int]] | A list of tuples representing the start and end indices of each consecutive interval of True values in the logical list. |
Examples:
>>> find_interval([0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1])
[(2, 4), (6, 7), (10, 11)]
>>> find_interval([1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1])
[(0, 2), (4, 5), (9, 10)]
Source code in neuro_py/process/intervals.py
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 |
|
get_overlapping_intervals(start, stop, interval_width, slideby)
Generate overlapping intervals within a specified time range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start | float | The start time of the time range. | required |
stop | float | The stop time of the time range. | required |
interval_width | float | The width of each interval in seconds. | required |
slideby | float | The amount to slide the interval by in seconds. | required |
Returns:
Type | Description |
---|---|
ndarray | A 2D array containing (start, stop) pairs for all overlapping intervals. |
Examples:
>>> get_overlapping_intervals(0, 10, 2, 1)
array([[0, 2],
[1, 3],
[2, 4],
[3, 5],
[4, 6],
[5, 7],
[6, 8],
[7, 9]])
Source code in neuro_py/process/intervals.py
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 |
|
in_intervals(timestamps, intervals, return_interval=False, shift=False)
Find which timestamps fall within the given intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamps | ndarray | An array of timestamp values. Assumes sorted. | required |
intervals | ndarray | An array of time intervals, represented as pairs of start and end times. | required |
return_interval | (bool, optional(default=False)) | If True, return the index of the interval to which each timestamp belongs. | False |
shift | (bool, optional(default=False)) | If True, return the shifted timestamps | False |
Returns:
Name | Type | Description |
---|---|---|
in_interval | ndarray | A logical index indicating which timestamps fall within the intervals. |
interval | (ndarray, optional) | A ndarray indicating for each timestamps which interval it was within. |
shifted_timestamps | (ndarray, optional) | The shifted timestamps |
Examples:
>>> timestamps = np.array([1, 2, 3, 4, 5, 6, 7, 8])
>>> intervals = np.array([[2, 4], [5, 7]])
>>> in_intervals(timestamps, intervals)
array([False, True, True, True, True, True, True, False])
>>> in_intervals(timestamps, intervals, return_interval=True)
(array([False, True, True, True, True, True, True, False]),
array([nan, 0., 0., 0., 1., 1., 1., nan]))
>>> in_intervals(timestamps, intervals, shift=True)
(array([False, True, True, True, True, True, True, False]),
array([0, 1, 2, 2, 3, 4]))
>>> in_intervals(timestamps, intervals, return_interval=True, shift=True)
(array([False, True, True, True, True, True, True, False]),
array([0, 0, 0, 1, 1, 1]),
array([0, 1, 2, 2, 3, 4]))
Source code in neuro_py/process/intervals.py
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 |
|
in_intervals_interval(timestamps, intervals)
for each timestamps value, the index of the interval to which it belongs (nan = none)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamps | ndarray | An array of timestamp values. assumes sorted | required |
intervals | ndarray | An array of time intervals, represented as pairs of start and end times. | required |
Returns:
Name | Type | Description |
---|---|---|
ndarray | A ndarray indicating for each timestamps which interval it was within. | |
Note | produces same result as in_intervals with return_interval=True | |
Examples:
>>> timestamps = np.array([1, 2, 3, 4, 5, 6, 7, 8])
>>> intervals = np.array([[2, 4], [5, 7]])
>>> in_intervals_interval(timestamps, intervals)
array([nan, 0, 0, 0, 1, 1, 1, nan])
Source code in neuro_py/process/intervals.py
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 |
|
overlap_intersect(epoch, interval, return_indices=True)
Returns the epochs with overlap with the given interval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch | EpochArray | The epochs to check. | required |
interval | IntervalArray | The interval to check for overlap. | required |
return_indices | bool | If True, returns the indices of the overlapping epochs. Default is True. | True |
Returns:
Type | Description |
---|---|
EpochArray | The epochs with overlap with the interval. |
(Tuple[EpochArray, ndarray], optional) | If |
Source code in neuro_py/process/intervals.py
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 |
|
randomize_epochs(epoch, randomize_each=True, start_stop=None)
Randomly shifts the epochs of a EpochArray object and wraps them around the original time boundaries.
This method takes a EpochArray object as input, and can either randomly shift each epoch by a different amount (if randomize_each
is True) or shift all the epochs by the same amount (if randomize_each
is False). In either case, the method wraps the shifted epochs around the original time boundaries to make sure they remain within the original time range. It then returns the modified EpochArray object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch | EpochArray | The EpochArray object whose epochs should be shifted and wrapped. | required |
randomize_each | bool | If True, each epoch will be shifted by a different random amount. If False, all the epochs will be shifted by the same random amount. Defaults to True. | True |
start_stop | array | If not None, time support will be taken from start_stop | None |
Returns:
Name | Type | Description |
---|---|---|
new_epochs | EpochArray | The modified EpochArray object with the shifted and wrapped epochs. |
Source code in neuro_py/process/intervals.py
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 |
|
shift_epoch_array(epoch, epoch_shift)
Shift an EpochArray by another EpochArray.
Shifting means that intervals in 'epoch' will be relative to intervals in 'epoch_shift' as if 'epoch_shift' intervals were without gaps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch | EpochArray | The intervals to shift. | required |
epoch_shift | EpochArray | The intervals to shift by. | required |
Returns:
Type | Description |
---|---|
EpochArray | The shifted EpochArray. |
Notes
This function restricts 'epoch' to those within 'epoch_shift' as epochs between 'epoch_shift' intervals would result in a duration of 0.
Visual representation: inputs: epoch = [ ] [ ][ ] [] epoch_shift = [ ][ ] [ ] becomes: epoch = [ ] [ ] [] epoch_shift = [ ][ ][ ]
Source code in neuro_py/process/intervals.py
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 |
|
split_epoch_by_width(intervals, bin_width=0.001)
Generate combined intervals (start, stop) at a specified width within given intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
intervals | List[Tuple[float, float]] | A list of (start, end) tuples representing intervals. | required |
bin_width | float | The width of each bin in seconds. Default is 0.001 (1 ms). | 0.001 |
Returns:
Type | Description |
---|---|
ndarray | A 2D array containing (start, stop) pairs for all bins across intervals. |
Source code in neuro_py/process/intervals.py
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 |
|
split_epoch_equal_parts(intervals, n_parts, return_epoch_array=True)
Split multiple intervals into equal parts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
intervals | (array - like, shape(n_intervals, 2)) | The intervals to split. | required |
n_parts | int | The number of parts to split each interval into. | required |
return_epoch_array | bool | If True, returns the intervals as a nelpy.EpochArray object. Defaults to True. | True |
Returns:
Name | Type | Description |
---|---|---|
split_intervals | (array - like, shape(n_intervals * n_parts, 2) or EpochArray) | The split intervals. |
Source code in neuro_py/process/intervals.py
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 |
|
truncate_epoch(epoch, time=3600)
Truncates an EpochArray to achieve a specified cumulative time duration.
This function takes an input EpochArray 'epoch' and a 'time' value representing the desired cumulative time duration in seconds. It returns a new EpochArray containing intervals that cumulatively match the specified time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch | EpochArray | The input EpochArray containing intervals to be truncated. | required |
time | Union[int, float] | The desired cumulative time in seconds (default is 3600). | 3600 |
Returns:
Type | Description |
---|---|
EpochArray | A new EpochArray containing intervals that cumulatively match the specified time. |
Algorithm
- Calculate the cumulative lengths of intervals in the 'epoch'.
- If the cumulative time of the 'epoch' is already less than or equal to 'time', return the original 'epoch'.
- Find the last interval that fits within the specified 'time' and create a new EpochArray 'truncated_intervals' with intervals up to that point.
- To achieve the desired cumulative time, calculate the remaining time needed to reach 'time'.
- Add portions of the next interval to 'truncated_intervals' until the desired 'time' is reached or all intervals are used.
Examples:
>>> epoch_data = [(0, 2), (3, 6), (8, 10)]
>>> epoch = nel.EpochArray(epoch_data)
>>> truncated_epoch = truncate_epoch(epoch, time=7)
Source code in neuro_py/process/intervals.py
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 |
|