Beispiel #1
0
def join_segments(time_segments):
	from mypy.utils import group

	# check which should be joined
	join_segments = (time_segments[:-1, 1] - time_segments[1:, 0]) == 0
	segment_groups = group(join_segments)
	segment_groups[:, 1] += 1

	# join segments
	final_segments = np.vstack([time_segments[segment_groups[:, 0], 0],
	                            time_segments[segment_groups[:, 1], 1]]).T

	# we missed single-segments, search for them
	prev = 0
	missed_segments = list()
	for row in segment_groups:
		if not row[0] == prev:
			missed_segments.extend(list(range(prev + 1, row[0])))
		prev = row[1]
	if prev < len(time_segments):
		missed_segments.extend(list(range(prev + 1, len(time_segments))))

	# append missed single segments to multi-segments and sort
	final_segments = np.vstack(
		[final_segments, time_segments[missed_segments, :]])
	final_segments = final_segments[final_segments[:, 0].argsort(), :]

	return final_segments
Beispiel #2
0
def join_segments(time_segments):
    from mypy.utils import group

    # check which should be joined
    join_segments = (time_segments[:-1, 1] - time_segments[1:, 0]) == 0
    segment_groups = group(join_segments)
    segment_groups[:, 1] += 1

    # join segments
    final_segments = np.vstack([
        time_segments[segment_groups[:, 0], 0],
        time_segments[segment_groups[:, 1], 1]
    ]).T

    # we missed single-segments, search for them
    prev = 0
    missed_segments = list()
    for row in segment_groups:
        if not row[0] == prev:
            missed_segments.extend(list(range(prev + 1, row[0])))
        prev = row[1]
    if prev < len(time_segments):
        missed_segments.extend(list(range(prev + 1, len(time_segments))))

    # append missed single segments to multi-segments and sort
    final_segments = np.vstack(
        [final_segments, time_segments[missed_segments, :]])
    final_segments = final_segments[final_segments[:, 0].argsort(), :]

    return final_segments
Beispiel #3
0
	def _to_annotation(self, bad_bool, description='BAD_'):
		import mne
		groups = group(bad_bool)
		if len(groups) > 0:
			onset = groups[:, 0] * (self.step / self.sfreq)
			duration = (self.window / self.sfreq) + np.diff(groups).ravel() * (self.step / self.sfreq)
			return mne.Annotations(onset, duration, [description] * groups.shape[0])
		else:
			return mne.Annotations([], [], [])
Beispiel #4
0
def apply_artifacts_to_tfr(tfr, artif, orig_time, fillval=np.nan):
	'''Fill tfr data with `fillval` according to `artif`.
	Operates in-place.'''
	n_items, n_samples = artif.shape
	bad_inds = np.any(artif, axis=1).nonzero()[0]
	for bad in bad_inds:
		limits = group(artif[bad, :])
		for lim in limits:
			slc = find_range(tfr.times, list(orig_time[lim]))
			tfr.data[bad, :, :, slc] = fillval
	return tfr
Beispiel #5
0
 def _to_annotation(self, bad_bool, description='BAD_'):
     import mne
     groups = group(bad_bool)
     if len(groups) > 0:
         onset = groups[:, 0] * (self.step / self.sfreq)
         duration = (self.window / self.sfreq
                     ) + np.diff(groups).ravel() * (self.step / self.sfreq)
         return mne.Annotations(onset, duration,
                                [description] * groups.shape[0])
     else:
         return mne.Annotations([], [], [])
Beispiel #6
0
def apply_artifacts_to_tfr(tfr, artif, orig_time, fillval=np.nan):
    '''Fill tfr data with `fillval` according to `artif`.
	Operates in-place.'''
    n_items, n_samples = artif.shape
    bad_inds = np.any(artif, axis=1).nonzero()[0]
    for bad in bad_inds:
        limits = group(artif[bad, :])
        for lim in limits:
            slc = find_range(tfr.times, list(orig_time[lim]))
            tfr.data[bad, :, :, slc] = fillval
    return tfr
Beispiel #7
0
def extend_bads(rej_win, extend, copy=True):
	'''Extend rejection periods by specfied number of samples.'''
	if copy:
		rej_win = rej_win.copy()
	n_samples = rej_win.shape[1]
	bad_inds = np.any(rej_win, axis=1).nonzero()[0]
	for bad in bad_inds:
		slices = group(rej_win[bad, :], return_slice=True)
		for slc in slices:
			slc = extend_slice(slc, extend, n_samples)
			rej_win[bad, slc] = True
	return rej_win
Beispiel #8
0
def extend_bads(rej_win, extend, copy=True):
    '''Extend rejection periods by specfied number of samples.'''
    if copy:
        rej_win = rej_win.copy()
    n_samples = rej_win.shape[1]
    bad_inds = np.any(rej_win, axis=1).nonzero()[0]
    for bad in bad_inds:
        slices = group(rej_win[bad, :], return_slice=True)
        for slc in slices:
            slc = extend_slice(slc, extend, n_samples)
            rej_win[bad, slc] = True
    return rej_win
Beispiel #9
0
def highlight(x_values,
              which_highligh,
              kind='patch',
              color=None,
              alpha=0.3,
              axis=None,
              level=0.04,
              height=0.03):
    '''Highlight ranges along x axis.

    Parameters
    ----------
    x_values : numpy array
        Values specifying x axis points along which which_highligh operates.
    which_highligh : numpy array of bool
        Boolean values - each entry specifies whether corresponding value for
        x_values belongs to highlighted ranges.
    '''
    from matplotlib.patches import Rectangle
    from mypy.utils import group

    if color is None:
        color = 'orange' if kind == 'patch' else 'k'
    axis = plt.gca() if axis is None else axis

    ylims = axis.get_ylim()
    y_rng = np.diff(ylims)
    hlf_dist = np.diff(x_values).mean() / 2
    grp = group(which_highligh, return_slice=True)
    for slc in grp:
        this_x = x_values[slc]
        start = this_x[0] - hlf_dist
        length = np.diff(this_x[[0, -1]]) + hlf_dist * 2
        ptch = Rectangle((start, ylims[0]),
                         length,
                         y_rng,
                         lw=0,
                         facecolor=color,
                         alpha=alpha)
        axis.add_patch(ptch)