def add_params(traj,param_dict): '''Adds parameters to a trajectory ''' flat_dict = flatten_dictionary(param_dict,'.') for key, val in flat_dict.items(): if isinstance(val, (np.ndarray, tuple)) or (isinstance(val, list) and (len(val) < 4 or val[3] != ())): traj.f_add_parameter(ArrayParameter,key,val, comment='comment') elif isinstance(val, (str,bool,float)+compat.int_types): traj.f_add_parameter(Parameter,key,val, comment='Im a comment!') elif spsp.isspmatrix(val): traj.f_add_parameter(SparseParameter,key,val, comment='comment').v_annotations.f_set( **{'Name':key,'Val' :str(val),'Favorite_Numbers:':[1,2,3], 'Second_Fav':np.array([43.0,43.0])}) elif isinstance(val,list): # The last item of the list `val` is an int between 0 and 2, we can use it as a # protocol read out to test all protocols traj.f_add_parameter(PickleParameter,key,val, comment='Im a comment!', protocol=val[-1]) else: raise RuntimeError('You shall not pass, %s is %s!' % (str(val),str(type(val)))) traj.f_add_derived_parameter('Another.String', 'Hi, how are you?', comment='test1') traj.f_add_derived_parameter('Another.StringGroup.$', 'too bad!?', comment='test2') traj.f_add_derived_parameter('Another.$set.$.String', 'Really?', comment='test3') traj.f_add_derived_parameter('Another.crun.String2', 'Really, again?', comment='test4') traj.f_add_result('Peter_Jackson',np.str(['is','full','of','suboptimal ideas']), comment='Only my opinion bro!',) traj.results.f_add_leaf('Test', 42, comment='NC') traj.f_add_group('derived_parameters.uo', comment='Yeah, this is unsuals') traj.dpar.f_add_leaf('uo.adsad', 3333, comment='Yo') traj.derived_parameters.f_add_leaf('Test2', 42, comment='sfsdf') traj.par.f_add_leaf('er.Test3', 42, comment='sdfds') for irun in range(13): traj.f_add_leaf('testleaf%d' % irun, 42, comment='f') traj.par.f_add_group('Empty', comment='Notting!') traj.f_add_group('imgeneric.bitch', comment='Generic_Group') traj.imgeneric.f_add_leaf('gentest', 'fortytwo', comment='Oh yeah!')
def add_params(traj,param_dict): '''Adds parameters to a trajectory ''' flat_dict = flatten_dictionary(param_dict,'.') for key, val in flat_dict.items(): if isinstance(val, (np.ndarray, tuple)) or (isinstance(val, list) and (len(val) < 4 or val[3] != ())): traj.f_add_parameter(ArrayParameter,key,val, comment='comment') elif isinstance(val, (str,bool,float)+compat.int_types): traj.f_add_parameter(Parameter,key,val, comment='Im a comment!') elif spsp.isspmatrix(val): traj.f_add_parameter(SparseParameter,key,val, comment='comment').v_annotations.f_set( **{'Name':key,'Val' :str(val),'Favorite_Numbers:':[1,2,3], 'Second_Fav':np.array([43.0,43.0])}) elif isinstance(val,list): # The last item of the list `val` is an int between 0 and 2, we can use it as a # protocol read out to test all protocols traj.f_add_parameter(PickleParameter,key,val, comment='Im a comment!', protocol=val[-1]) else: raise RuntimeError('You shall not pass, %s is %s!' % (str(val),str(type(val)))) traj.f_add_derived_parameter('Another.String', 'Hi, how are you?', comment='test1') traj.f_add_derived_parameter('Another.StringGroup.$', 'too bad!?', comment='test2') traj.f_add_derived_parameter('Another.$set.$.String', 'Really?', comment='test3') traj.f_add_derived_parameter('Another.crun.String2', 'Really, again?', comment='test4') traj.f_add_result('Peter_Jackson',np.str(['is','full','of','suboptimal ideas']), comment='Only my opinion bro!',) traj.results.f_add_leaf('Test', 42, comment='NC') traj.f_add_group('derived_parameters.uo', comment='Yeah, this is unsuals') traj.dpar.f_add_leaf('uo.adsad', 3333, comment='Yo') traj.derived_parameters.f_add_leaf('Test2', 42, comment='sfsdf') traj.par.f_add_leaf('er.Test3', 42, comment='sdfds') for irun in range(13): traj.f_add_leaf('testleaf%d' % irun, 42, comment='f') traj.par.f_add_group('Empty', comment='Notting!') traj.f_add_group('imgeneric.bitch', comment='Generic_Group') traj.imgeneric.f_add_leaf('gentest', 'fortytwo', comment='Oh yeah!')
def test_flatten_dictionary(self): mydict = {'a': {'b': {'c': 4}, 'c': 5}, 'd': 4} flattened = flatten_dictionary(mydict, separator='.') expected = {'a.b.c': 4, 'a.c': 5, 'd': 4} self.assertTrue(flattened == expected)
def test_flatten_dictionary(self): mydict = {'a':{'b':{'c':4}, 'c':5}, 'd':4} flattened = flatten_dictionary(mydict, separator='.') expected = {'a.b.c' : 4, 'a.c' : 5, 'd':4} self.assertTrue(flattened == expected)