<s> __author__ = '<STR_LIT>' <EOL> import sys <EOL> import os <EOL> import numpy as np <EOL> from pypet . trajectory import Trajectory <EOL> from pypet . tests . testutils . ioutils import make_temp_dir , remove_data , run_suite , parse_args <EOL> from pypet . utils import comparisons as comp <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> if ( sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:0> ) ) : <EOL> import unittest2 as unittest <EOL> else : <EOL> import unittest <EOL> class AnnotationsTest ( unittest . TestCase ) : <EOL> tags = '<STR_LIT>' , '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . filename = make_temp_dir ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . traj = Trajectory ( name = '<STR_LIT>' , filename = self . filename ) <EOL> self . traj . f_add_result ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . traj . f_add_parameter ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . make_annotations ( ) <EOL> self . add_annotations ( self . traj ) <EOL> pred = lambda x : '<STR_LIT>' not in x . v_full_name <EOL> x = len ( [ node for node in self . traj . f_iter_nodes ( recursive = True , predicate = pred ) ] ) <EOL> self . assertTrue ( x == <NUM_LIT:5> , '<STR_LIT>' % ( str ( x ) , str ( <NUM_LIT:5> ) ) ) <EOL> def tearDown ( self ) : <EOL> remove_data ( ) <EOL> def make_annotations ( self ) : <EOL> self . annotations = { } <EOL> self . annotations [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:12> , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT:h>' ] , <NUM_LIT:3> : { '<STR_LIT:a>' : <NUM_LIT> } } <EOL> self . annotations [ '<STR_LIT:list>' ] = [ self . annotations [ '<STR_LIT>' ] , <NUM_LIT> ] <EOL> self . annotations [ '<STR_LIT:string>' ] = '<STR_LIT:string>' <EOL> self . annotations [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . annotations [ '<STR_LIT>' ] = ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> self . annotations [ '<STR_LIT>' ] = np . array ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . annotations [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> def add_annotations ( self , traj ) : <EOL> funcs = <NUM_LIT:5> <EOL> for idx , node in enumerate ( [ traj ] + [ node for node in traj . f_iter_nodes ( recursive = True ) ] ) : <EOL> for name in self . annotations : <EOL> anno = self . annotations [ name ] <EOL> if name == <NUM_LIT:0> : <EOL> node . f_set_annotations ( anno ) <EOL> node . v_annotations . f_set ( anno ) <EOL> elif idx % funcs == <NUM_LIT:0> : <EOL> node . f_set_annotations ( ** { name : anno } ) <EOL> elif idx % funcs == <NUM_LIT:1> : <EOL> node . v_annotations . f_set ( ** { name : anno } ) <EOL> elif idx % funcs == <NUM_LIT:2> : <EOL> node . v_annotations . f_set_single ( name , anno ) <EOL> elif idx % funcs == <NUM_LIT:3> : <EOL> setattr ( node . v_annotations , name , anno ) <EOL> elif idx % funcs == <NUM_LIT:4> : <EOL> node . v_annotations [ name ] = anno <EOL> def test_annotations_insert ( self ) : <EOL> for idx , node in enumerate ( [ self . traj ] + [ node for node in self . traj . f_iter_nodes ( recursive = True ) ] ) : <EOL> for name in self . annotations : <EOL> anno = self . annotations [ name ] <EOL> node_anno = node . v_annotations [ name ] <EOL> self . assertTrue ( comp . nested_equal ( anno , node_anno ) , <EOL> '<STR_LIT>' % ( str ( anno ) , str ( node_anno ) ) ) <EOL> def test_pickling ( self ) : <EOL> dump = pickle . dumps ( self . traj ) <EOL> del self . traj <EOL> self . traj = pickle . loads ( dump ) <EOL> self . test_annotations_insert ( ) <EOL> def test_storage_and_loading ( self ) : <EOL> self . traj . f_store ( ) <EOL> traj_name = self . traj . v_name <EOL> del self . traj <EOL> self . traj = Trajectory ( filename = self . filename ) <EOL> self . traj . f_load ( name = traj_name , load_parameters = <NUM_LIT:2> , load_derived_parameters = <NUM_LIT:2> , <EOL> load_results = <NUM_LIT:2> , load_other_data = <NUM_LIT:2> ) <EOL> self . test_annotations_insert ( ) <EOL> def test_attribute_deletion ( self ) : <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> name_list = [ name for name in node . v_annotations ] <EOL> for name in name_list : <EOL> delattr ( node . v_annotations , name ) <EOL> self . assertTrue ( node . v_annotations . f_is_empty ( ) ) <EOL> def test_item_deletion ( self ) : <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> name_list = [ name for name in node . v_annotations ] <EOL> for name in name_list : <EOL> del node . v_annotations [ name ] <EOL> self . assertTrue ( node . v_annotations . f_is_empty ( ) ) <EOL> def test_get_item ( self ) : <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> for key , val1 in node . v_annotations . f_to_dict ( ) . items ( ) : <EOL> val2 = node . v_annotations [ key ] <EOL> self . assertTrue ( comp . nested_equal ( val1 , val2 ) ) <EOL> def test_get_item_no_copy ( self ) : <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> for key , val1 in node . v_annotations . f_to_dict ( copy = False ) . items ( ) : <EOL> val2 = node . v_annotations [ key ] <EOL> self . assertTrue ( comp . nested_equal ( val1 , val2 ) ) <EOL> @ staticmethod <EOL> def dict_to_str ( dictionary ) : <EOL> resstr = '<STR_LIT>' <EOL> new_dict = { } <EOL> for key , val in dictionary . items ( ) : <EOL> if key == <NUM_LIT:0> : <EOL> key = '<STR_LIT>' <EOL> new_dict [ key ] = val <EOL> for key in sorted ( new_dict . keys ( ) ) : <EOL> resstr += '<STR_LIT>' % ( key , str ( new_dict [ key ] ) ) <EOL> return resstr [ : - <NUM_LIT:2> ] <EOL> def test_to_str ( self ) : <EOL> dict_str = self . dict_to_str ( self . annotations ) <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> ann_str = node . f_ann_to_str ( ) <EOL> self . assertTrue ( not ann_str . endswith ( '<STR_LIT:U+0020>' ) or not ann_str . endswith ( '<STR_LIT:U+002C>' ) ) <EOL> for name in self . annotations : <EOL> if name == <NUM_LIT:0> : <EOL> name = '<STR_LIT>' <EOL> self . assertTrue ( name in ann_str ) <EOL> self . assertEqual ( dict_str , ann_str , '<STR_LIT>' % ( dict_str , ann_str ) ) <EOL> ann_str = str ( node . v_annotations ) <EOL> self . assertEqual ( dict_str , ann_str , '<STR_LIT>' % ( dict_str , ann_str ) ) <EOL> def test_single_get_and_getattr_and_setattr ( self ) : <EOL> self . traj . f_add_parameter ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . traj . f_get ( '<STR_LIT>' ) . v_annotations . test = <NUM_LIT:4> <EOL> self . assertTrue ( self . traj . f_get ( '<STR_LIT>' ) . v_annotations . test , <NUM_LIT:4> ) <EOL> self . assertTrue ( self . traj . f_get ( '<STR_LIT>' ) . v_annotations . f_get ( ) , <NUM_LIT:4> ) <EOL> def test_get_annotations ( self ) : <EOL> key_list = list ( self . annotations . keys ( ) ) <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> for name in self . annotations : <EOL> self . assertTrue ( comp . nested_equal ( self . annotations [ name ] , <EOL> node . f_get_annotations ( name ) ) ) <EOL> val_list = node . f_get_annotations ( * key_list ) <EOL> for idx , val in enumerate ( val_list ) : <EOL> self . assertTrue ( comp . nested_equal ( self . annotations [ key_list [ idx ] ] , val ) ) <EOL> def test_f_get_errors ( self ) : <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> node . v_annotations . f_get ( ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> node . v_annotations . f_get ( '<STR_LIT>' ) <EOL> testparam = self . traj . f_add_parameter ( '<STR_LIT>' , <NUM_LIT> ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> testparam . v_annotations . f_get ( ) <EOL> def test_f_set_numbering ( self ) : <EOL> int_list = list ( range ( <NUM_LIT:10> ) ) <EOL> for node in self . traj . f_iter_nodes ( recursive = True ) : <EOL> node . v_annotations . f_set ( * int_list ) <EOL> self . assertEqual ( node . v_annotations . f_get ( * int_list ) , tuple ( int_list ) ) <EOL> for integer in int_list : <EOL> if integer == <NUM_LIT:0> : <EOL> name = '<STR_LIT>' <EOL> else : <EOL> name = '<STR_LIT>' % integer <EOL> self . assertTrue ( name in node . v_annotations ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> opt_args = parse_args ( ) <EOL> run_suite ( ** opt_args ) </s>
