#NEWLINE# @ testing . requires_testing_data #NEWLINE# def test_extract_label_time_course( ) : #NEWLINE# #INDENT# 'Test extraction of label time courses from stc\n    ' #NEWLINE# n_stcs = 3 #NEWLINE# n_times = 50 #NEWLINE# src = read_inverse_operator ( fname_inv ) [ 'src' ] #NEWLINE# vertices = [ src [ 0 ] [ 'vertno' ] , src [ 1 ] [ 'vertno' ] ] #NEWLINE# n_verts = ( len ( vertices [ 0 ] ) + len ( vertices [ 1 ] ) ) #NEWLINE# labels_lh = read_labels_from_annot ( 'sample' , hemi = 'lh' , subjects_dir = subjects_dir ) #NEWLINE# labels_rh = read_labels_from_annot ( 'sample' , hemi = 'rh' , subjects_dir = subjects_dir ) #NEWLINE# labels = list ( ) #NEWLINE# labels . extend ( labels_lh [ : 5 ] ) #NEWLINE# labels . extend ( labels_rh [ : 4 ] ) #NEWLINE# n_labels = len ( labels ) #NEWLINE# label_means = ( np . arange ( n_labels ) [ : , None ] * np . ones ( ( n_labels , n_times ) ) ) #NEWLINE# label_maxs = ( np . arange ( n_labels ) [ : , None ] * np . ones ( ( n_labels , n_times ) ) ) #NEWLINE# label_means_flipped = np . zeros_like ( label_means ) #NEWLINE# for ( i , label ) in enumerate ( labels ) : #NEWLINE# #INDENT# label_means_flipped [ i ] = ( i * np . mean ( label_sign_flip ( label , src ) ) ) #NEWLINE# #UNINDENT# stcs = list ( ) #NEWLINE# for i in range ( n_stcs ) : #NEWLINE# #INDENT# data = np . zeros ( ( n_verts , n_times ) ) #NEWLINE# for ( j , label ) in enumerate ( labels ) : #NEWLINE# #INDENT# if ( label . hemi == 'lh' ) : #NEWLINE# #INDENT# idx = np . intersect1d ( vertices [ 0 ] , label . vertices ) #NEWLINE# idx = np . searchsorted ( vertices [ 0 ] , idx ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( label . hemi == 'rh' ) : #NEWLINE# #INDENT# idx = np . intersect1d ( vertices [ 1 ] , label . vertices ) #NEWLINE# idx = ( len ( vertices [ 0 ] ) + np . searchsorted ( vertices [ 1 ] , idx ) ) #NEWLINE# #UNINDENT# #UNINDENT# data [ idx ] = label_means [ j ] #NEWLINE# #UNINDENT# this_stc = SourceEstimate ( data , vertices , 0 , 1 ) #NEWLINE# stcs . append ( this_stc ) #NEWLINE# #UNINDENT# assert_raises ( ValueError , extract_label_time_course , stcs , labels , src , mode = 'notamode' ) #NEWLINE# empty_label = labels [ 0 ] . copy ( ) #NEWLINE# empty_label . vertices += 1000000 #NEWLINE# assert_raises ( ValueError , extract_label_time_course , stcs , empty_label , src , mode = 'mean' ) #NEWLINE# with warnings . catch_warnings ( record = True ) : #NEWLINE# #INDENT# tc = extract_label_time_course ( stcs , empty_label , src , mode = 'mean' , allow_empty = True ) #NEWLINE# #UNINDENT# for arr in tc : #NEWLINE# #INDENT# assert_true ( ( arr . shape == ( 1 , n_times ) ) ) #NEWLINE# assert_array_equal ( arr , np . zeros ( ( 1 , n_times ) ) ) #NEWLINE# #UNINDENT# modes = [ 'mean' , 'mean_flip' , 'pca_flip' , 'max' ] #NEWLINE# for mode in modes : #NEWLINE# #INDENT# label_tc = extract_label_time_course ( stcs , labels , src , mode = mode ) #NEWLINE# label_tc_method = [ stc . extract_label_time_course ( labels , src , mode = mode ) for stc in stcs ] #NEWLINE# assert_true ( ( len ( label_tc ) == n_stcs ) ) #NEWLINE# assert_true ( ( len ( label_tc_method ) == n_stcs ) ) #NEWLINE# for ( tc1 , tc2 ) in zip ( label_tc , label_tc_method ) : #NEWLINE# #INDENT# assert_true ( ( tc1 . shape == ( n_labels , n_times ) ) ) #NEWLINE# assert_true ( ( tc2 . shape == ( n_labels , n_times ) ) ) #NEWLINE# assert_true ( np . allclose ( tc1 , tc2 , rtol = 1e-08 , atol = 1e-16 ) ) #NEWLINE# if ( mode == 'mean' ) : #NEWLINE# #INDENT# assert_array_almost_equal ( tc1 , label_means ) #NEWLINE# #UNINDENT# if ( mode == 'mean_flip' ) : #NEWLINE# #INDENT# assert_array_almost_equal ( tc1 , label_means_flipped ) #NEWLINE# #UNINDENT# if ( n_verts == 'max' ) : #NEWLINE# #INDENT# assert_array_almost_equal ( tc1 , label_maxs ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# label = Label ( vertices = src [ 0 ] [ 'vertno' ] [ : 2 ] , hemi = 'lh' ) #NEWLINE# x = label_sign_flip ( label , src ) #NEWLINE# assert_true ( ( len ( x ) == 2 ) ) #NEWLINE# label = Label ( vertices = [ ] , hemi = 'lh' ) #NEWLINE# x = label_sign_flip ( label , src ) #NEWLINE# assert_true ( ( x . size == 0 ) )