<s> import os <EOL> import os . path as op <EOL> import shutil <EOL> import glob <EOL> import warnings <EOL> import sys <EOL> import numpy as np <EOL> from scipy import sparse <EOL> from numpy . testing import assert_array_equal , assert_array_almost_equal <EOL> from nose . tools import assert_equal , assert_true , assert_false , assert_raises <EOL> from mne . datasets import testing <EOL> from mne import ( read_label , stc_to_label , read_source_estimate , <EOL> read_source_spaces , grow_labels , read_labels_from_annot , <EOL> write_labels_to_annot , split_label , spatial_tris_connectivity , <EOL> read_surface ) <EOL> from mne . label import Label , _blend_colors , label_sign_flip <EOL> from mne . utils import ( _TempDir , requires_sklearn , get_subjects_dir , <EOL> run_tests_if_main , slow_test ) <EOL> from mne . fixes import digitize , in1d , assert_is , assert_is_not <EOL> from mne . label import _n_colors <EOL> from mne . source_space import SourceSpaces <EOL> from mne . source_estimate import mesh_edges <EOL> from mne . externals . six import string_types <EOL> from mne . externals . six . moves import cPickle as pickle <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> data_path = testing . data_path ( download = False ) <EOL> subjects_dir = op . join ( data_path , '<STR_LIT>' ) <EOL> src_fname = op . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> stc_fname = op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> real_label_fname = op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> real_label_rh_fname = op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> v1_label_fname = op . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> fwd_fname = op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> src_bad_fname = op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> label_dir = op . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> test_path = op . join ( op . split ( __file__ ) [ <NUM_LIT:0> ] , '<STR_LIT:..>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:data>' ) <EOL> label_fname = op . join ( test_path , '<STR_LIT>' ) <EOL> label_rh_fname = op . join ( test_path , '<STR_LIT>' ) <EOL> def _stc_to_label ( stc , src , smooth , subjects_dir = None ) : <EOL> """<STR_LIT>""" <EOL> src = stc . subject if src is None else src <EOL> if isinstance ( src , string_types ) : <EOL> subject = src <EOL> else : <EOL> subject = stc . subject <EOL> if isinstance ( src , string_types ) : <EOL> subjects_dir = get_subjects_dir ( subjects_dir ) <EOL> surf_path_from = op . join ( subjects_dir , src , '<STR_LIT>' ) <EOL> rr_lh , tris_lh = read_surface ( op . join ( surf_path_from , <EOL> '<STR_LIT>' ) ) <EOL> rr_rh , tris_rh = read_surface ( op . join ( surf_path_from , <EOL> '<STR_LIT>' ) ) <EOL> rr = [ rr_lh , rr_rh ] <EOL> tris = [ tris_lh , tris_rh ] <EOL> else : <EOL> if not isinstance ( src , SourceSpaces ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if len ( src ) != <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> rr = [ <NUM_LIT> * src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT> * src [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ] <EOL> tris = [ src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , src [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ] <EOL> labels = [ ] <EOL> cnt = <NUM_LIT:0> <EOL> for hemi_idx , ( hemi , this_vertno , this_tris , this_rr ) in enumerate ( <EOL> zip ( [ '<STR_LIT>' , '<STR_LIT>' ] , stc . vertices , tris , rr ) ) : <EOL> this_data = stc . data [ cnt : cnt + len ( this_vertno ) ] <EOL> e = mesh_edges ( this_tris ) <EOL> e . data [ e . data == <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> n_vertices = e . shape [ <NUM_LIT:0> ] <EOL> e = e + sparse . eye ( n_vertices , n_vertices ) <EOL> clusters = [ this_vertno [ np . any ( this_data , axis = <NUM_LIT:1> ) ] ] <EOL> cnt += len ( this_vertno ) <EOL> clusters = [ c for c in clusters if len ( c ) > <NUM_LIT:0> ] <EOL> if len ( clusters ) == <NUM_LIT:0> : <EOL> this_labels = None <EOL> else : <EOL> this_labels = [ ] <EOL> colors = _n_colors ( len ( clusters ) ) <EOL> for c , color in zip ( clusters , colors ) : <EOL> idx_use = c <EOL> for k in range ( smooth ) : <EOL> e_use = e [ : , idx_use ] <EOL> data1 = e_use * np . ones ( len ( idx_use ) ) <EOL> idx_use = np . where ( data1 ) [ <NUM_LIT:0> ] <EOL> label = Label ( idx_use , this_rr [ idx_use ] , None , hemi , <EOL> '<STR_LIT>' , subject = subject , <EOL> color = color ) <EOL> this_labels . append ( label ) <EOL> this_labels = this_labels [ <NUM_LIT:0> ] <EOL> labels . append ( this_labels ) <EOL> return labels <EOL> def assert_labels_equal ( l0 , l1 , decimal = <NUM_LIT:5> , comment = True , color = True ) : <EOL> if comment : <EOL> assert_equal ( l0 . comment , l1 . comment ) <EOL> if color : <EOL> assert_equal ( l0 . color , l1 . color ) <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> attr0 = getattr ( l0 , attr ) <EOL> attr1 = getattr ( l1 , attr ) <EOL> msg = "<STR_LIT>" % ( attr , attr0 , attr1 ) <EOL> assert_equal ( attr0 , attr1 , msg ) <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> a0 = getattr ( l0 , attr ) <EOL> a1 = getattr ( l1 , attr ) <EOL> assert_array_almost_equal ( a0 , a1 , decimal ) <EOL> def test_label_subject ( ) : <EOL> """<STR_LIT>""" <EOL> label = read_label ( label_fname ) <EOL> assert_is ( label . subject , None ) <EOL> assert_true ( '<STR_LIT>' in repr ( label ) ) <EOL> label = read_label ( label_fname , subject = '<STR_LIT>' ) <EOL> assert_true ( label . subject == '<STR_LIT>' ) <EOL> assert_true ( '<STR_LIT>' in repr ( label ) ) <EOL> def test_label_addition ( ) : <EOL> """<STR_LIT>""" <EOL> pos = np . random . RandomState ( <NUM_LIT:0> ) . rand ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> values = np . arange ( <NUM_LIT> ) / <NUM_LIT:10> <EOL> idx0 = list ( range ( <NUM_LIT:7> ) ) <EOL> idx1 = list ( range ( <NUM_LIT:7> , <NUM_LIT:10> ) ) <EOL> idx2 = list ( range ( <NUM_LIT:5> , <NUM_LIT:10> ) ) <EOL> l0 = Label ( idx0 , pos [ idx0 ] , values [ idx0 ] , '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> l1 = Label ( idx1 , pos [ idx1 ] , values [ idx1 ] , '<STR_LIT>' ) <EOL> l2 = Label ( idx2 , pos [ idx2 ] , values [ idx2 ] , '<STR_LIT>' , color = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> assert_equal ( len ( l0 ) , len ( idx0 ) ) <EOL> l_good = l0 . copy ( ) <EOL> l_good . subject = '<STR_LIT>' <EOL> l_bad = l1 . copy ( ) <EOL> l_bad . subject = '<STR_LIT:foo>' <EOL> assert_raises ( ValueError , l_good . __add__ , l_bad ) <EOL> assert_raises ( TypeError , l_good . __add__ , '<STR_LIT:foo>' ) <EOL> assert_raises ( ValueError , l_good . __sub__ , l_bad ) <EOL> assert_raises ( TypeError , l_good . __sub__ , '<STR_LIT:foo>' ) <EOL> l01 = l0 + l1 <EOL> assert_equal ( len ( l01 ) , len ( l0 ) + len ( l1 ) ) <EOL> assert_array_equal ( l01 . values [ : len ( l0 ) ] , l0 . values ) <EOL> assert_equal ( l01 . color , l0 . color ) <EOL> assert_labels_equal ( l01 - l0 , l1 , comment = False , color = False ) <EOL> assert_labels_equal ( l01 - l1 , l0 , comment = False , color = False ) <EOL> l = l0 + l2 <EOL> i0 = np . where ( l0 . vertices == <NUM_LIT:6> ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> i2 = np . where ( l2 . vertices == <NUM_LIT:6> ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> i = np . where ( l . vertices == <NUM_LIT:6> ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert_equal ( l . values [ i ] , l0 . values [ i0 ] + l2 . values [ i2 ] ) <EOL> assert_equal ( l . values [ <NUM_LIT:0> ] , l0 . values [ <NUM_LIT:0> ] ) <EOL> assert_array_equal ( np . unique ( l . vertices ) , np . unique ( idx0 + idx2 ) ) <EOL> assert_equal ( l . color , _blend_colors ( l0 . color , l2 . color ) ) <EOL> l2 . hemi = '<STR_LIT>' <EOL> bhl = l0 + l2 <EOL> assert_equal ( bhl . hemi , '<STR_LIT>' ) <EOL> assert_equal ( len ( bhl ) , len ( l0 ) + len ( l2 ) ) <EOL> assert_equal ( bhl . color , l . color ) <EOL> assert_true ( '<STR_LIT>' in repr ( bhl ) ) <EOL> assert_labels_equal ( bhl - l0 , l2 ) <EOL> assert_labels_equal ( bhl - l2 , l0 ) <EOL> bhl2 = l1 + bhl <EOL> assert_labels_equal ( bhl2 . lh , l01 ) <EOL> assert_equal ( bhl2 . color , _blend_colors ( l1 . color , bhl . color ) ) <EOL> assert_array_equal ( ( l2 + bhl ) . rh . vertices , bhl . rh . vertices ) <EOL> assert_array_equal ( ( bhl + bhl ) . lh . vertices , bhl . lh . vertices ) <EOL> assert_raises ( TypeError , bhl . __add__ , <NUM_LIT:5> ) <EOL> bhl_ = bhl2 - l1 <EOL> assert_labels_equal ( bhl_ . lh , bhl . lh , comment = False , color = False ) <EOL> assert_labels_equal ( bhl_ . rh , bhl . rh ) <EOL> assert_labels_equal ( bhl2 - l2 , l0 + l1 ) <EOL> assert_labels_equal ( bhl2 - l1 - l0 , l2 ) <EOL> bhl_ = bhl2 - bhl2 <EOL> assert_array_equal ( bhl_ . vertices , [ ] ) <EOL> @ testing . requires_testing_data <EOL> def test_label_in_src ( ) : <EOL> """<STR_LIT>""" <EOL> src = read_source_spaces ( src_fname ) <EOL> label = read_label ( v1_label_fname ) <EOL> vert_in_src = np . intersect1d ( label . vertices , src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , True ) <EOL> where = in1d ( label . vertices , vert_in_src ) <EOL> pos_in_src = label . pos [ where ] <EOL> values_in_src = label . values [ where ] <EOL> label_src = Label ( vert_in_src , pos_in_src , values_in_src , <EOL> hemi = '<STR_LIT>' ) . fill ( src ) <EOL> vertices_status = in1d ( src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , label . vertices ) <EOL> vertices_in = np . nonzero ( vertices_status ) [ <NUM_LIT:0> ] <EOL> vertices_out = np . nonzero ( np . logical_not ( vertices_status ) ) [ <NUM_LIT:0> ] <EOL> assert_array_equal ( label_src . vertices , vertices_in ) <EOL> assert_array_equal ( in1d ( vertices_out , label_src . vertices ) , False ) <EOL> value_idx = digitize ( src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ vertices_in ] , vert_in_src , True ) <EOL> assert_array_equal ( label_src . values , values_in_src [ value_idx ] ) <EOL> vertices = np . append ( [ - <NUM_LIT:1> ] , vert_in_src ) <EOL> assert_raises ( ValueError , Label ( vertices , hemi = '<STR_LIT>' ) . fill , src ) <EOL> @ testing . requires_testing_data <EOL> def test_label_io_and_time_course_estimates ( ) : <EOL> """<STR_LIT>""" <EOL> stc = read_source_estimate ( stc_fname ) <EOL> label = read_label ( real_label_fname ) <EOL> stc_label = stc . in_label ( label ) <EOL> assert_true ( len ( stc_label . times ) == stc_label . data . shape [ <NUM_LIT:1> ] ) <EOL> assert_true ( len ( stc_label . vertices [ <NUM_LIT:0> ] ) == stc_label . data . shape [ <NUM_LIT:0> ] ) <EOL> @ testing . requires_testing_data <EOL> def test_label_io ( ) : <EOL> """<STR_LIT>""" <EOL> tempdir = _TempDir ( ) <EOL> label = read_label ( label_fname ) <EOL> assert_equal ( label . name , '<STR_LIT>' ) <EOL> assert_is ( label . subject , None ) <EOL> assert_is ( label . color , None ) <EOL> label . save ( op . join ( tempdir , '<STR_LIT:foo>' ) ) <EOL> label2 = read_label ( op . join ( tempdir , '<STR_LIT>' ) ) <EOL> assert_labels_equal ( label , label2 ) <EOL> dest = op . join ( tempdir , '<STR_LIT>' ) <EOL> with open ( dest , '<STR_LIT:wb>' ) as fid : <EOL> pickle . dump ( label , fid , pickle . HIGHEST_PROTOCOL ) <EOL> with open ( dest , '<STR_LIT:rb>' ) as fid : <EOL> label2 = pickle . load ( fid ) <EOL> assert_labels_equal ( label , label2 ) <EOL> def _assert_labels_equal ( labels_a , labels_b , ignore_pos = False ) : <EOL> """<STR_LIT>""" <EOL> for label_a , label_b in zip ( labels_a , labels_b ) : <EOL> assert_array_equal ( label_a . vertices , label_b . vertices ) <EOL> assert_true ( label_a . name == label_b . name ) <EOL> assert_true ( label_a . hemi == label_b . hemi ) <EOL> if not ignore_pos : <EOL> assert_array_equal ( label_a . pos , label_b . pos ) <EOL> @ testing . requires_testing_data <EOL> def test_annot_io ( ) : <EOL> """<STR_LIT>""" <EOL> tempdir = _TempDir ( ) <EOL> subject = '<STR_LIT>' <EOL> label_src = os . path . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT:label>' ) <EOL> surf_src = os . path . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> label_dir = os . path . join ( tempdir , subject , '<STR_LIT:label>' ) <EOL> surf_dir = os . path . join ( tempdir , subject , '<STR_LIT>' ) <EOL> os . makedirs ( label_dir ) <EOL> os . mkdir ( surf_dir ) <EOL> shutil . copy ( os . path . join ( label_src , '<STR_LIT>' ) , label_dir ) <EOL> shutil . copy ( os . path . join ( label_src , '<STR_LIT>' ) , label_dir ) <EOL> shutil . copy ( os . path . join ( surf_src , '<STR_LIT>' ) , surf_dir ) <EOL> shutil . copy ( os . path . join ( surf_src , '<STR_LIT>' ) , surf_dir ) <EOL> assert_raises ( IOError , read_labels_from_annot , subject , '<STR_LIT>' , <EOL> subjects_dir = tempdir ) <EOL> labels = read_labels_from_annot ( subject , '<STR_LIT>' , <EOL> subjects_dir = tempdir ) <EOL> parc = [ l for l in labels if l . name == '<STR_LIT>' ] <EOL> write_labels_to_annot ( parc , subject , '<STR_LIT>' , subjects_dir = tempdir ) <EOL> parc1 = read_labels_from_annot ( subject , '<STR_LIT>' , subjects_dir = tempdir ) <EOL> parc1 = [ l for l in parc1 if not l . name . startswith ( '<STR_LIT>' ) ] <EOL> assert_equal ( len ( parc1 ) , len ( parc ) ) <EOL> for l1 , l in zip ( parc1 , parc ) : <EOL> assert_labels_equal ( l1 , l ) <EOL> parc = [ l for l in labels if l . name . startswith ( '<STR_LIT>' ) ] <EOL> write_labels_to_annot ( parc , subject , '<STR_LIT>' , hemi = '<STR_LIT>' , <EOL> subjects_dir = tempdir ) <EOL> annot_fname = os . path . join ( tempdir , subject , '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> assert_true ( os . path . isfile ( annot_fname % '<STR_LIT:l>' ) ) <EOL> assert_false ( os . path . isfile ( annot_fname % '<STR_LIT:r>' ) ) <EOL> parc1 = read_labels_from_annot ( subject , '<STR_LIT>' , <EOL> annot_fname = annot_fname % '<STR_LIT:l>' , <EOL> subjects_dir = tempdir ) <EOL> parc_lh = [ l for l in parc if l . name . endswith ( '<STR_LIT>' ) ] <EOL> for l1 , l in zip ( parc1 , parc_lh ) : <EOL> assert_labels_equal ( l1 , l ) <EOL> @ testing . requires_testing_data <EOL> def test_read_labels_from_annot ( ) : <EOL> """<STR_LIT>""" <EOL> assert_raises ( ValueError , read_labels_from_annot , '<STR_LIT>' , hemi = '<STR_LIT>' , <EOL> subjects_dir = subjects_dir ) <EOL> assert_raises ( ValueError , read_labels_from_annot , '<STR_LIT>' , <EOL> annot_fname = '<STR_LIT>' , subjects_dir = subjects_dir ) <EOL> labels_lh = read_labels_from_annot ( '<STR_LIT>' , hemi = '<STR_LIT>' , <EOL> subjects_dir = subjects_dir ) <EOL> for label in labels_lh : <EOL> assert_true ( label . name . endswith ( '<STR_LIT>' ) ) <EOL> assert_true ( label . hemi == '<STR_LIT>' ) <EOL> if sys . version_info [ : <NUM_LIT:2> ] > ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> assert_is_not ( label . color , None ) <EOL> annot_fname = op . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> labels_rh = read_labels_from_annot ( '<STR_LIT>' , annot_fname = annot_fname , <EOL> subjects_dir = subjects_dir ) <EOL> for label in labels_rh : <EOL> assert_true ( label . name . endswith ( '<STR_LIT>' ) ) <EOL> assert_true ( label . hemi == '<STR_LIT>' ) <EOL> assert_is_not ( label . color , None ) <EOL> labels_lhrh = list ( ) <EOL> labels_lhrh . extend ( labels_lh ) <EOL> labels_lhrh . extend ( labels_rh ) <EOL> names = [ label . name for label in labels_lhrh ] <EOL> labels_lhrh = [ label for ( name , label ) in sorted ( zip ( names , labels_lhrh ) ) ] <EOL> labels_both = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir ) <EOL> _assert_labels_equal ( labels_lhrh , labels_both ) <EOL> assert_true ( len ( labels_both ) == <NUM_LIT> ) <EOL> label = read_labels_from_annot ( '<STR_LIT>' , parc = '<STR_LIT>' , <EOL> regexp = '<STR_LIT>' , subjects_dir = subjects_dir ) [ <NUM_LIT:0> ] <EOL> assert_true ( label . name == '<STR_LIT>' ) <EOL> label = read_labels_from_annot ( '<STR_LIT>' , '<STR_LIT>' , <EOL> regexp = '<STR_LIT>' , <EOL> subjects_dir = subjects_dir ) [ <NUM_LIT:0> ] <EOL> assert_true ( label . name == '<STR_LIT>' ) <EOL> assert_raises ( RuntimeError , read_labels_from_annot , '<STR_LIT>' , parc = '<STR_LIT>' , <EOL> annot_fname = annot_fname , regexp = '<STR_LIT>' , <EOL> subjects_dir = subjects_dir ) <EOL> @ testing . requires_testing_data <EOL> def test_read_labels_from_annot_annot2labels ( ) : <EOL> """<STR_LIT>""" <EOL> label_fnames = glob . glob ( label_dir + '<STR_LIT>' ) <EOL> label_fnames . sort ( ) <EOL> labels_mne = [ read_label ( fname ) for fname in label_fnames ] <EOL> labels = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir ) <EOL> _assert_labels_equal ( labels , labels_mne , ignore_pos = True ) <EOL> @ testing . requires_testing_data <EOL> def test_write_labels_to_annot ( ) : <EOL> """<STR_LIT>""" <EOL> tempdir = _TempDir ( ) <EOL> labels = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir ) <EOL> surf_dir = op . join ( subjects_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> temp_surf_dir = op . join ( tempdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> os . makedirs ( temp_surf_dir ) <EOL> shutil . copy ( op . join ( surf_dir , '<STR_LIT>' ) , temp_surf_dir ) <EOL> shutil . copy ( op . join ( surf_dir , '<STR_LIT>' ) , temp_surf_dir ) <EOL> os . makedirs ( op . join ( tempdir , '<STR_LIT>' , '<STR_LIT:label>' ) ) <EOL> dst = op . join ( tempdir , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' ) <EOL> write_labels_to_annot ( labels , '<STR_LIT>' , '<STR_LIT>' , subjects_dir = tempdir ) <EOL> assert_true ( op . exists ( dst % ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> assert_true ( op . exists ( dst % ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> for label in labels : <EOL> if label . hemi == '<STR_LIT>' : <EOL> break <EOL> write_labels_to_annot ( [ label ] , '<STR_LIT>' , '<STR_LIT>' , subjects_dir = tempdir ) <EOL> assert_true ( op . exists ( dst % ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> assert_true ( op . exists ( dst % ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> for label in labels : <EOL> if label . hemi == '<STR_LIT>' : <EOL> break <EOL> write_labels_to_annot ( [ label ] , '<STR_LIT>' , '<STR_LIT>' , subjects_dir = tempdir ) <EOL> assert_true ( op . exists ( dst % ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> assert_true ( op . exists ( dst % ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> assert_raises ( TypeError , write_labels_to_annot , labels [ <NUM_LIT:0> ] , '<STR_LIT>' , <EOL> '<STR_LIT>' , subjects_dir = tempdir ) <EOL> fnames = [ op . join ( tempdir , hemi + '<STR_LIT>' ) for hemi in [ '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> with warnings . catch_warnings ( record = True ) : <EOL> for fname in fnames : <EOL> write_labels_to_annot ( labels , annot_fname = fname ) <EOL> labels2 = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] ) <EOL> labels22 = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir , <EOL> annot_fname = fnames [ <NUM_LIT:1> ] ) <EOL> labels2 . extend ( labels22 ) <EOL> names = [ label . name for label in labels2 ] <EOL> for label in labels : <EOL> idx = names . index ( label . name ) <EOL> assert_labels_equal ( label , labels2 [ idx ] ) <EOL> for fname in fnames : <EOL> write_labels_to_annot ( labels , '<STR_LIT>' , annot_fname = fname , <EOL> overwrite = True , subjects_dir = subjects_dir ) <EOL> labels3 = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] ) <EOL> labels33 = read_labels_from_annot ( '<STR_LIT>' , subjects_dir = subjects_dir , <EOL> annot_fname = fnames [ <NUM_LIT:1> ] ) <EOL> labels3 . extend ( labels33 ) <EOL> names3 = [ label . name for label in labels3 ] <EOL> for label in labels : <EOL> idx = names3 . index ( label . name ) <EOL> assert_labels_equal ( label , labels3 [ idx ] ) <EOL> assert_raises ( ValueError , write_labels_to_annot , labels , '<STR_LIT>' , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] , subjects_dir = subjects_dir ) <EOL> write_labels_to_annot ( labels , '<STR_LIT>' , annot_fname = fnames [ <NUM_LIT:0> ] , <EOL> overwrite = True , subjects_dir = subjects_dir ) <EOL> labels_ = labels [ : ] <EOL> labels_ [ <NUM_LIT:0> ] = labels_ [ <NUM_LIT:0> ] . copy ( ) <EOL> labels_ [ <NUM_LIT:0> ] . color = None <EOL> write_labels_to_annot ( labels_ , '<STR_LIT>' , annot_fname = fnames [ <NUM_LIT:0> ] , <EOL> overwrite = True , subjects_dir = subjects_dir ) <EOL> labels_ [ <NUM_LIT:0> ] . color = labels_ [ <NUM_LIT:2> ] . color <EOL> assert_raises ( ValueError , write_labels_to_annot , labels_ , '<STR_LIT>' , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] , overwrite = True , <EOL> subjects_dir = subjects_dir ) <EOL> labels_ [ <NUM_LIT:0> ] . color = ( <NUM_LIT> , <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ) <EOL> assert_raises ( ValueError , write_labels_to_annot , labels_ , '<STR_LIT>' , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] , overwrite = True , <EOL> subjects_dir = subjects_dir ) <EOL> labels_ = labels [ : ] <EOL> cuneus_lh = labels [ <NUM_LIT:6> ] <EOL> precuneus_lh = labels [ <NUM_LIT:50> ] <EOL> labels_ . append ( precuneus_lh + cuneus_lh ) <EOL> assert_raises ( ValueError , write_labels_to_annot , labels_ , '<STR_LIT>' , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] , overwrite = True , <EOL> subjects_dir = subjects_dir ) <EOL> labels_lh = [ label for label in labels if label . name . endswith ( '<STR_LIT>' ) ] <EOL> write_labels_to_annot ( labels_lh [ <NUM_LIT:1> : ] , '<STR_LIT>' , annot_fname = fnames [ <NUM_LIT:0> ] , <EOL> overwrite = True , subjects_dir = subjects_dir ) <EOL> labels_reloaded = read_labels_from_annot ( '<STR_LIT>' , annot_fname = fnames [ <NUM_LIT:0> ] , <EOL> subjects_dir = subjects_dir ) <EOL> assert_equal ( len ( labels_lh ) , len ( labels_reloaded ) ) <EOL> label0 = labels_lh [ <NUM_LIT:0> ] <EOL> label1 = labels_reloaded [ - <NUM_LIT:1> ] <EOL> assert_equal ( label1 . name , "<STR_LIT>" ) <EOL> assert_true ( np . all ( in1d ( label0 . vertices , label1 . vertices ) ) ) <EOL> labels4 = labels [ : ] <EOL> labels4 [ <NUM_LIT:0> ] . name = None <EOL> assert_raises ( ValueError , write_labels_to_annot , labels4 , <EOL> annot_fname = fnames [ <NUM_LIT:0> ] ) <EOL> @ testing . requires_testing_data <EOL> def test_split_label ( ) : <EOL> """<STR_LIT>""" <EOL> aparc = read_labels_from_annot ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> regexp = '<STR_LIT>' , subjects_dir = subjects_dir ) <EOL> lingual = aparc [ <NUM_LIT:0> ] <EOL> parts = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> post , ant = split_label ( lingual , parts , subjects_dir = subjects_dir ) <EOL> assert_equal ( post . name , parts [ <NUM_LIT:0> ] ) <EOL> assert_equal ( ant . name , parts [ <NUM_LIT:1> ] ) <EOL> lingual_reconst = post + ant <EOL> lingual_reconst . name = lingual . name <EOL> lingual_reconst . comment = lingual . comment <EOL> lingual_reconst . color = lingual . color <EOL> assert_labels_equal ( lingual_reconst , lingual ) <EOL> post1 , ant1 = lingual . split ( parts , subjects_dir = subjects_dir ) <EOL> assert_labels_equal ( post1 , post ) <EOL> assert_labels_equal ( ant1 , ant ) <EOL> antmost = split_label ( lingual , <NUM_LIT> , None , subjects_dir , True ) [ - <NUM_LIT:1> ] <EOL> fs_vert = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> assert_array_equal ( antmost . vertices , fs_vert ) <EOL> assert_equal ( antmost . name , "<STR_LIT>" ) <EOL> @ slow_test <EOL> @ testing . requires_testing_data <EOL> @ requires_sklearn <EOL> def test_stc_to_label ( ) : <EOL> """<STR_LIT>""" <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> src = read_source_spaces ( fwd_fname ) <EOL> src_bad = read_source_spaces ( src_bad_fname ) <EOL> stc = read_source_estimate ( stc_fname , '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = op . join ( data_path , '<STR_LIT>' ) <EOL> labels1 = _stc_to_label ( stc , src = '<STR_LIT>' , smooth = <NUM_LIT:3> ) <EOL> labels2 = _stc_to_label ( stc , src = src , smooth = <NUM_LIT:3> ) <EOL> assert_equal ( len ( labels1 ) , len ( labels2 ) ) <EOL> for l1 , l2 in zip ( labels1 , labels2 ) : <EOL> assert_labels_equal ( l1 , l2 , decimal = <NUM_LIT:4> ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> labels_lh , labels_rh = stc_to_label ( stc , src = src , smooth = True , <EOL> connected = True ) <EOL> assert_true ( len ( w ) > <NUM_LIT:0> ) <EOL> assert_raises ( ValueError , stc_to_label , stc , '<STR_LIT>' , smooth = True , <EOL> connected = True ) <EOL> assert_raises ( RuntimeError , stc_to_label , stc , smooth = True , src = src_bad , <EOL> connected = True ) <EOL> assert_equal ( len ( labels_lh ) , <NUM_LIT:1> ) <EOL> assert_equal ( len ( labels_rh ) , <NUM_LIT:1> ) <EOL> tris = labels_lh [ <NUM_LIT:0> ] . get_tris ( src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , vertices = stc . vertices [ <NUM_LIT:0> ] ) <EOL> assert_raises ( ValueError , spatial_tris_connectivity , tris , <EOL> remap_vertices = False ) <EOL> connectivity = spatial_tris_connectivity ( tris , remap_vertices = True ) <EOL> assert_true ( connectivity . shape [ <NUM_LIT:0> ] == len ( stc . vertices [ <NUM_LIT:0> ] ) ) <EOL> assert_raises ( TypeError , stc_to_label , stc , src = <NUM_LIT:1> , smooth = False , <EOL> connected = False , subjects_dir = subjects_dir ) <EOL> assert_raises ( ValueError , stc_to_label , stc , src = SourceSpaces ( [ src [ <NUM_LIT:0> ] ] ) , <EOL> smooth = False , connected = False , subjects_dir = subjects_dir ) <EOL> assert_raises ( ValueError , stc_to_label , stc , src = '<STR_LIT>' , smooth = False , <EOL> connected = True , subjects_dir = subjects_dir ) <EOL> assert_raises ( ValueError , stc_to_label , stc , src = '<STR_LIT>' , smooth = True , <EOL> connected = False , subjects_dir = subjects_dir ) <EOL> labels_lh , labels_rh = stc_to_label ( stc , src = '<STR_LIT>' , smooth = False , <EOL> connected = False , <EOL> subjects_dir = subjects_dir ) <EOL> assert_true ( len ( labels_lh ) > <NUM_LIT:1> ) <EOL> assert_true ( len ( labels_rh ) > <NUM_LIT:1> ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> labels_patch = stc_to_label ( stc , src = src , smooth = True ) <EOL> assert_equal ( len ( w ) , <NUM_LIT:1> ) <EOL> assert_equal ( len ( labels_patch ) , len ( labels1 ) ) <EOL> for l1 , l2 in zip ( labels1 , labels2 ) : <EOL> assert_labels_equal ( l1 , l2 , decimal = <NUM_LIT:4> ) <EOL> @ slow_test <EOL> @ testing . requires_testing_data <EOL> def test_morph ( ) : <EOL> """<STR_LIT>""" <EOL> label_orig = read_label ( real_label_fname ) <EOL> label_orig . subject = '<STR_LIT>' <EOL> vals = list ( ) <EOL> for grade in [ <NUM_LIT:5> , [ np . arange ( <NUM_LIT> ) , np . arange ( <NUM_LIT> ) ] , np . arange ( <NUM_LIT> ) ] : <EOL> label = label_orig . copy ( ) <EOL> assert_raises ( ValueError , label . morph , '<STR_LIT>' , '<STR_LIT>' ) <EOL> label . values . fill ( <NUM_LIT:1> ) <EOL> label = label . morph ( None , '<STR_LIT>' , <NUM_LIT:5> , grade , subjects_dir , <NUM_LIT:1> ) <EOL> label = label . morph ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:5> , None , subjects_dir , <NUM_LIT:2> ) <EOL> assert_true ( np . mean ( in1d ( label_orig . vertices , label . vertices ) ) == <NUM_LIT:1.0> ) <EOL> assert_true ( len ( label . vertices ) < <NUM_LIT:3> * len ( label_orig . vertices ) ) <EOL> vals . append ( label . vertices ) <EOL> assert_array_equal ( vals [ <NUM_LIT:0> ] , vals [ <NUM_LIT:1> ] ) <EOL> assert_equal ( label . subject , '<STR_LIT>' ) <EOL> verts = [ np . arange ( <NUM_LIT> ) , np . arange ( <NUM_LIT> ) ] <EOL> for hemi in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> label . hemi = hemi <EOL> label . morph ( None , '<STR_LIT>' , <NUM_LIT:5> , verts , subjects_dir , <NUM_LIT:2> ) <EOL> assert_raises ( TypeError , label . morph , None , <NUM_LIT:1> , <NUM_LIT:5> , verts , <EOL> subjects_dir , <NUM_LIT:2> ) <EOL> assert_raises ( TypeError , label . morph , None , '<STR_LIT>' , <NUM_LIT> , verts , <EOL> subjects_dir , <NUM_LIT:2> ) <EOL> with warnings . catch_warnings ( record = True ) : <EOL> label . smooth ( subjects_dir = subjects_dir ) <EOL> @ testing . requires_testing_data <EOL> def test_grow_labels ( ) : <EOL> """<STR_LIT>""" <EOL> seeds = [ <NUM_LIT:0> , <NUM_LIT> ] <EOL> should_be_in = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> hemis = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> labels = grow_labels ( '<STR_LIT>' , seeds , <NUM_LIT:3> , hemis , subjects_dir , names = names ) <EOL> tgt_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> tgt_hemis = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for label , seed , hemi , sh , name in zip ( labels , seeds , tgt_hemis , <EOL> should_be_in , tgt_names ) : <EOL> assert_true ( np . any ( label . vertices == seed ) ) <EOL> assert_true ( np . all ( in1d ( sh , label . vertices ) ) ) <EOL> assert_equal ( label . hemi , hemi ) <EOL> assert_equal ( label . name , name ) <EOL> seeds = [ <NUM_LIT> , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> l01 , l02 = grow_labels ( '<STR_LIT>' , seeds , <NUM_LIT:20> , [ <NUM_LIT:0> , <NUM_LIT:0> ] , subjects_dir ) <EOL> seeds = [ <NUM_LIT> , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> l11 , l12 = grow_labels ( '<STR_LIT>' , seeds , <NUM_LIT:20> , [ <NUM_LIT:0> , <NUM_LIT:0> ] , subjects_dir , <EOL> overlap = False ) <EOL> assert_equal ( l01 . name , '<STR_LIT>' ) <EOL> assert_equal ( l02 . name , '<STR_LIT>' ) <EOL> assert_equal ( l11 . name , '<STR_LIT>' ) <EOL> assert_equal ( l12 . name , '<STR_LIT>' ) <EOL> overlap = np . intersect1d ( l11 . vertices , l12 . vertices , True ) <EOL> assert_array_equal ( overlap , [ ] ) <EOL> l0 = l01 + l02 <EOL> l1 = l11 + l12 <EOL> assert_array_equal ( l1 . vertices , l0 . vertices ) <EOL> @ testing . requires_testing_data <EOL> def test_label_sign_flip ( ) : <EOL> src = read_source_spaces ( src_fname ) <EOL> label = Label ( vertices = src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ : <NUM_LIT:5> ] , hemi = '<STR_LIT>' ) <EOL> src [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ label . vertices ] = np . array ( <EOL> [ [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1.> ] , <EOL> [ <NUM_LIT:1.> / np . sqrt ( <NUM_LIT:2> ) , <NUM_LIT:1.> / np . sqrt ( <NUM_LIT:2> ) , <NUM_LIT:0.> ] , <EOL> [ <NUM_LIT:1.> / np . sqrt ( <NUM_LIT:2> ) , <NUM_LIT:1.> / np . sqrt ( <NUM_LIT:2> ) , <NUM_LIT:0.> ] ] ) <EOL> known_flips = np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> , np . nan , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> idx = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> flip = label_sign_flip ( label , src ) <EOL> assert_array_almost_equal ( np . abs ( np . dot ( flip [ idx ] , known_flips [ idx ] ) ) , <EOL> len ( idx ) ) <EOL> run_tests_if_main ( ) </s>
