#NEWLINE# def _sliding_window( times , window , sfreq ) : #NEWLINE# #INDENT# "Aux function of GeneralizationAcrossTime\n\n    Define the slices on which to train each classifier. The user either define\n    the time slices manually in window['slices'] or s/he passes optional params\n    to set them from window['start'], window['stop'], window['step'] and\n    window['length'].\n\n    Parameters\n    ----------\n    times : ndarray, shape (n_times,)\n        Array of times from MNE epochs.\n    window : dict keys: ('start', 'stop', 'step', 'length')\n        Either train or test times.\n\n    Returns\n    -------\n    window : dict\n        Dictionary to set training and testing times.\n\n    See Also\n    --------\n    GeneralizationAcrossTime\n\n    " #NEWLINE# import copy #NEWLINE# window = _DecodingTime ( copy . deepcopy ( window ) ) #NEWLINE# time_slices = window . get ( 'slices' , None ) #NEWLINE# if ( time_slices is None ) : #NEWLINE# #INDENT# window [ 'start' ] = window . get ( 'start' , times [ 0 ] ) #NEWLINE# window [ 'stop' ] = window . get ( 'stop' , times [ ( - 1 ) ] ) #NEWLINE# window [ 'step' ] = window . get ( 'step' , ( 1.0 / sfreq ) ) #NEWLINE# window [ 'length' ] = window . get ( 'length' , ( 1.0 / sfreq ) ) #NEWLINE# if ( not ( times [ 0 ] <= window [ 'start' ] <= times [ ( - 1 ) ] ) ) : #NEWLINE# #INDENT# raise ValueError ( ( '`start` (%.2f s) outside time range [%.2f, %.2f].' % ( window [ 'start' ] , times [ 0 ] , times [ ( - 1 ) ] ) ) ) #NEWLINE# #UNINDENT# if ( not ( times [ 0 ] <= window [ 'stop' ] <= times [ ( - 1 ) ] ) ) : #NEWLINE# #INDENT# raise ValueError ( ( '`stop` (%.2f s) outside time range [%.2f, %.2f].' % ( window [ 'stop' ] , times [ 0 ] , times [ ( - 1 ) ] ) ) ) #NEWLINE# #UNINDENT# if ( window [ 'step' ] < ( 1.0 / sfreq ) ) : #NEWLINE# #INDENT# raise ValueError ( '`step` must be >= 1 / sampling_frequency' ) #NEWLINE# #UNINDENT# if ( window [ 'length' ] < ( 1.0 / sfreq ) ) : #NEWLINE# #INDENT# raise ValueError ( '`length` must be >= 1 / sampling_frequency' ) #NEWLINE# #UNINDENT# if ( window [ 'length' ] > np . ptp ( times ) ) : #NEWLINE# #INDENT# raise ValueError ( '`length` must be <= time range' ) #NEWLINE# #UNINDENT# def find_t_idx( t ) : #NEWLINE# #INDENT# return np . argmin ( np . abs ( ( np . asarray ( times ) - t ) ) ) #NEWLINE# #UNINDENT# start = find_t_idx ( window [ 'start' ] ) #NEWLINE# stop = find_t_idx ( window [ 'stop' ] ) #NEWLINE# step = int ( round ( ( window [ 'step' ] * sfreq ) ) ) #NEWLINE# length = int ( round ( ( window [ 'length' ] * sfreq ) ) ) #NEWLINE# time_slices = [ range ( start , ( start + length ) ) ] #NEWLINE# while ( ( time_slices [ ( - 1 ) ] [ 0 ] + step ) <= ( ( stop - length ) + 1 ) ) : #NEWLINE# #INDENT# start = ( time_slices [ ( - 1 ) ] [ 0 ] + step ) #NEWLINE# time_slices . append ( range ( start , ( start + length ) ) ) #NEWLINE# #UNINDENT# window [ 'slices' ] = time_slices #NEWLINE# #UNINDENT# window [ 'times' ] = _set_window_time ( window [ 'slices' ] , times ) #NEWLINE# return window