Remove all annotations that are outside of [tmin, tmax]. The method operates inplace. Parameters ---------- tmin : float | datetime | None Start time of selection in seconds. tmax : float | datetime | None End time of selection in sec
(
self, tmin=None, tmax=None, emit_warning=False, use_orig_time=True, verbose=None
)
| 772 | |
| 773 | @verbose |
| 774 | def crop( |
| 775 | self, tmin=None, tmax=None, emit_warning=False, use_orig_time=True, verbose=None |
| 776 | ): |
| 777 | """Remove all annotations that are outside of [tmin, tmax]. |
| 778 | |
| 779 | The method operates inplace. |
| 780 | |
| 781 | Parameters |
| 782 | ---------- |
| 783 | tmin : float | datetime | None |
| 784 | Start time of selection in seconds. |
| 785 | tmax : float | datetime | None |
| 786 | End time of selection in seconds. |
| 787 | emit_warning : bool |
| 788 | Whether to emit warnings when limiting or omitting annotations. |
| 789 | Defaults to False. |
| 790 | use_orig_time : bool |
| 791 | Whether to use orig_time as an offset. |
| 792 | Defaults to True. |
| 793 | %(verbose)s |
| 794 | |
| 795 | Returns |
| 796 | ------- |
| 797 | self : instance of Annotations |
| 798 | The cropped Annotations object. |
| 799 | """ |
| 800 | if len(self) == 0: |
| 801 | return self # no annotations, nothing to do |
| 802 | offset, absolute_tmin, absolute_tmax = self._get_crop_lims( |
| 803 | tmin, tmax, use_orig_time |
| 804 | ) |
| 805 | del tmin, tmax |
| 806 | logger.debug(f"Cropping annotations {absolute_tmin} - {absolute_tmax}") |
| 807 | |
| 808 | onsets, durations, descriptions, ch_names, extras = [], [], [], [], [] |
| 809 | out_of_bounds, clip_left_elem, clip_right_elem = [], [], [] |
| 810 | for idx, (onset, duration, description, ch, extra) in enumerate( |
| 811 | zip(self.onset, self.duration, self.description, self.ch_names, self.extras) |
| 812 | ): |
| 813 | # if duration is NaN behave like a zero |
| 814 | if np.isnan(duration): |
| 815 | duration = 0.0 |
| 816 | # convert to absolute times |
| 817 | absolute_onset = timedelta(seconds=onset) + offset |
| 818 | absolute_offset = absolute_onset + timedelta(seconds=duration) |
| 819 | out_of_bounds.append( |
| 820 | absolute_onset > absolute_tmax or absolute_offset < absolute_tmin |
| 821 | ) |
| 822 | if out_of_bounds[-1]: |
| 823 | clip_left_elem.append(False) |
| 824 | clip_right_elem.append(False) |
| 825 | logger.debug( |
| 826 | f" [{idx}] Dropping " |
| 827 | f"({absolute_onset} - {absolute_offset}: {description})" |
| 828 | ) |
| 829 | else: |
| 830 | # clip the left side |
| 831 | clip_left_elem.append(absolute_onset < absolute_tmin) |