def test_check_one_conflict(): """ simplest senario """ with raises(RuntimeError): pm = ParameterHelper( kernels=["twobody", "threebody"], parameters={"cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05}, ones=True, random=True, verbose="DEBUG", ) with raises(RuntimeError): pm = ParameterHelper( kernels=["twobody", "threebody"], parameters={ "sigma": 0.5, "lengthscale": 1.0, "cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05, }, ones=True, random=False, verbose="DEBUG", )
def generate_hm(ntwobody, nthreebody, nmanybody=1, constraint=False, multihyps=True): cutoff = 0.8 if multihyps is False: kernels = [] parameters = {} if ntwobody > 0: kernels += ["twobody"] parameters["cutoff_twobody"] = cutoff if nthreebody > 0: kernels += ["threebody"] parameters["cutoff_threebody"] = cutoff if nmanybody > 0: kernels += ["manybody"] parameters["cutoff_manybody"] = cutoff pm = ParameterHelper(kernels=kernels, random=True, parameters=parameters) hm = pm.as_dict() hyps = hm["hyps"] cut = hm["cutoffs"] return hyps, hm, cut pm = ParameterHelper(species=["H", "He"], parameters={"noise": 0.05}) if ntwobody > 0: pm.define_group("twobody", "b1", ["*", "*"], parameters=random(2)) pm.set_parameters("cutoff_twobody", cutoff) if nthreebody > 0: pm.define_group("threebody", "t1", ["*", "*", "*"], parameters=random(2)) pm.set_parameters("cutoff_threebody", cutoff) if nmanybody > 0: pm.define_group("manybody", "manybody1", ["*", "*"], parameters=random(2)) pm.set_parameters("cutoff_manybody", cutoff) if ntwobody > 1: pm.define_group("twobody", "b2", ["H", "H"], parameters=random(2)) if nthreebody > 1: pm.define_group("threebody", "t2", ["H", "H", "H"], parameters=random(2)) if constraint is False: hm = pm.as_dict() hyps = hm["hyps"] cut = hm["cutoffs"] return hyps, hm, cut pm.set_constraints("b1", opt=[True, False]) pm.set_constraints("t1", opt=[False, True]) hm = pm.as_dict() hyps = hm["hyps"] cut = hm["cutoffs"] return hyps, hm, cut
def test_initialization5(): """check universal""" pm = ParameterHelper( species=["O", "C", "H"], kernels={ "twobody": [["*", "*"], ["O", "O"]], "threebody": [["*", "*", "*"], ["O", "O", "O"]], }, parameters={ "sigma": 1, "lengthscale": 0.5, "cutoff_threebody": 3, "cutoff_twobody": 2, }, verbose="DEBUG", ) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm) pm = ParameterHelper( kernels=["twobody", "threebody"], parameters={ "sigma": 1.0, "lengthscale": 0.5, "cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05, }, verbose="DEBUG", ) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm)
def generate_same_hm(kernels, multi_cutoff=False): """ generate hyperparameter and masks that are effectively the same but with single or multi group expression """ pm1 = ParameterHelper(species=["H", "He"], parameters={"noise": 0.05}) pm2 = ParameterHelper(species=["H", "He"], parameters={"noise": 0.05}) if "twobody" in kernels: para = 2.5 + 0.1 * random(3) pm1.set_parameters("cutoff_twobody", para[-1]) pm1.define_group("twobody", "twobody0", ["*", "*"], para[:-1]) pm2.set_parameters("cutoff_twobody", para[-1]) pm2.define_group("twobody", "twobody0", ["*", "*"], para[:-1]) pm2.define_group("twobody", "twobody1", ["H", "H"], para[:-1]) if multi_cutoff: pm2.set_parameters("twobody0", para) pm2.set_parameters("twobody1", para) if "threebody" in kernels: para = 1.2 + 0.1 * random(3) pm1.set_parameters("cutoff_threebody", para[-1]) pm1.define_group("threebody", "threebody0", ["*", "*", "*"], para[:-1]) pm2.set_parameters("cutoff_threebody", para[-1]) pm2.define_group("threebody", "threebody0", ["*", "*", "*"], para[:-1]) pm2.define_group("threebody", "threebody1", ["H", "H", "H"], para[:-1]) if multi_cutoff: pm2.define_group("cut3b", "c1", ["*", "*"], parameters=para) pm2.define_group("cut3b", "c2", ["H", "H"], parameters=para) if "manybody" in kernels: para = 1.2 + 0.1 * random(3) pm1.set_parameters("cutoff_manybody", para[-1]) pm1.define_group("manybody", "manybody0", ["*", "*"], para[:-1]) pm2.set_parameters("cutoff_manybody", para[-1]) pm2.define_group("manybody", "manybody0", ["*", "*"], para[:-1]) pm2.define_group("manybody", "manybody1", ["H", "H"], para[:-1]) if multi_cutoff: pm2.set_parameters("manybody0", para) pm2.set_parameters("manybody1", para) hm1 = pm1.as_dict() hyps1 = hm1["hyps"] cut = hm1["cutoffs"] hm2 = pm2.as_dict() hyps2 = hm2["hyps"] cut = hm2["cutoffs"] return cut, hyps1, hyps2, hm1, hm2
def generate_same_hm(kernels, multi_cutoff=False): """ generate hyperparameter and masks that are effectively the same but with single or multi group expression """ pm1 = ParameterHelper(species=['H', 'He'], parameters={'noise': 0.05}) pm2 = ParameterHelper(species=['H', 'He'], parameters={'noise': 0.05}) if ('twobody' in kernels): para = 2.5 + 0.1 * random(3) pm1.set_parameters('cutoff_twobody', para[-1]) pm1.define_group('twobody', 'twobody0', ['*', '*'], para[:-1]) pm2.set_parameters('cutoff_twobody', para[-1]) pm2.define_group('twobody', 'twobody0', ['*', '*'], para[:-1]) pm2.define_group('twobody', 'twobody1', ['H', 'H'], para[:-1]) if (multi_cutoff): pm2.set_parameters('twobody0', para) pm2.set_parameters('twobody1', para) if ('threebody' in kernels): para = 1.2 + 0.1 * random(3) pm1.set_parameters('cutoff_threebody', para[-1]) pm1.define_group('threebody', 'threebody0', ['*', '*', '*'], para[:-1]) pm2.set_parameters('cutoff_threebody', para[-1]) pm2.define_group('threebody', 'threebody0', ['*', '*', '*'], para[:-1]) pm2.define_group('threebody', 'threebody1', ['H', 'H', 'H'], para[:-1]) if (multi_cutoff): pm2.define_group('cut3b', 'c1', ['*', '*'], parameters=para) pm2.define_group('cut3b', 'c2', ['H', 'H'], parameters=para) if ('manybody' in kernels): para = 1.2 + 0.1 * random(3) pm1.set_parameters('cutoff_manybody', para[-1]) pm1.define_group('manybody', 'manybody0', ['*', '*'], para[:-1]) pm2.set_parameters('cutoff_manybody', para[-1]) pm2.define_group('manybody', 'manybody0', ['*', '*'], para[:-1]) pm2.define_group('manybody', 'manybody1', ['H', 'H'], para[:-1]) if (multi_cutoff): pm2.set_parameters('manybody0', para) pm2.set_parameters('manybody1', para) hm1 = pm1.as_dict() hyps1 = hm1['hyps'] cut = hm1['cutoffs'] hm2 = pm2.as_dict() hyps2 = hm2['hyps'] cut = hm2['cutoffs'] return cut, hyps1, hyps2, hm1, hm2
def test_generate_by_list_error(): pm = ParameterHelper(verbose="DEBUG") pm.list_groups("specie", ["O", ["C", "N"], "H"]) with raises(RuntimeError): pm.list_groups("specie", ["O", "C", "H"]) pm = ParameterHelper(verbose="DEBUG") with raises(RuntimeError): pm.list_groups("specie", "O") pm = ParameterHelper(verbose="DEBUG") with raises(RuntimeError): pm.list_groups("specie", "O")
def test_generate_by_list_error(): pm = ParameterHelper(verbose="DEBUG") pm.list_groups('specie', ['O', ['C', 'N'], 'H']) with raises(RuntimeError): pm.list_groups('specie', ['O', 'C', 'H']) pm = ParameterHelper(verbose="DEBUG") with raises(RuntimeError): pm.list_groups('specie', 'O') pm = ParameterHelper(verbose="DEBUG") with raises(RuntimeError): pm.list_groups('specie', 'O')
def generate_hm(ntwobody, nthreebody, nmanybody=1, constraint=False, multihyps=True): cutoff = 0.8 if (multihyps is False): kernels = [] parameters = {} if (ntwobody > 0): kernels += ['twobody'] parameters['cutoff_twobody'] = cutoff if (nthreebody > 0): kernels += ['threebody'] parameters['cutoff_threebody'] = cutoff if (nmanybody > 0): kernels += ['manybody'] parameters['cutoff_manybody'] = cutoff pm = ParameterHelper(kernels=kernels, random=True, parameters=parameters) hm = pm.as_dict() hyps = hm['hyps'] cut = hm['cutoffs'] return hyps, hm, cut pm = ParameterHelper(species=['H', 'He'], parameters={'noise':0.05}) if (ntwobody > 0): pm.define_group('twobody', 'b1', ['*', '*'], parameters=random(2)) pm.set_parameters('cutoff_twobody', cutoff) if (nthreebody > 0): pm.define_group('threebody', 't1', ['*', '*', '*'], parameters=random(2)) pm.set_parameters('cutoff_threebody', cutoff) if (nmanybody > 0): pm.define_group('manybody', 'manybody1', ['*', '*'], parameters=random(2)) pm.set_parameters('cutoff_manybody', cutoff) if (ntwobody > 1): pm.define_group('twobody', 'b2', ['H', 'H'], parameters=random(2)) if (nthreebody > 1): pm.define_group('threebody', 't2', ['H', 'H', 'H'], parameters=random(2)) if (constraint is False): hm = pm.as_dict() hyps = hm['hyps'] cut = hm['cutoffs'] return hyps, hm, cut pm.set_constraints('b1', opt=[True, False]) pm.set_constraints('t1', opt=[False, True]) hm = pm.as_dict() hyps = hm['hyps'] cut = hm['cutoffs'] return hyps, hm, cut
def test_generate_by_line(): pm = ParameterHelper(verbose="DEBUG") pm.define_group('specie', 'O', ['O']) pm.define_group('specie', 'C', ['C']) pm.define_group('specie', 'H', ['H']) pm.define_group('twobody', '**', ['C', 'H']) pm.define_group('twobody', 'OO', ['O', 'O'], atomic_str=True) pm.define_group('threebody', '***', ['O', 'O', 'C']) pm.define_group('threebody', 'OOO', ['O', 'O', 'O']) pm.define_group('manybody', '1.5', ['C', 'H']) pm.define_group('manybody', '1.5', ['C', 'O']) pm.define_group('manybody', '1.5', ['O', 'H']) pm.define_group('manybody', '2', ['O', 'O']) pm.define_group('manybody', '2', ['H', 'O']) pm.define_group('manybody', '2.8', ['O', 'O']) pm.set_parameters('**', [1, 0.5]) pm.set_parameters('OO', [1, 0.5]) pm.set_parameters('***', [1, 0.5]) pm.set_parameters('OOO', [1, 0.5]) pm.set_parameters('1.5', [1, 0.5, 1.5]) pm.set_parameters('2', [1, 0.5, 2]) pm.set_parameters('2.8', [1, 0.5, 2.8]) pm.set_constraints('2', [True, False]) pm.set_constraints('2.8', False) pm.set_parameters('cutoff_twobody', 5) pm.set_parameters('cutoff_threebody', 4) pm.set_parameters('cutoff_manybody', 3) hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def test_generate_by_line(): pm = ParameterHelper(verbose="DEBUG") pm.define_group("specie", "O", ["O"]) pm.define_group("specie", "C", ["C"]) pm.define_group("specie", "H", ["H"]) pm.define_group("twobody", "**", ["C", "H"]) pm.define_group("twobody", "OO", ["O", "O"], atomic_str=True) pm.define_group("threebody", "***", ["O", "O", "C"]) pm.define_group("threebody", "OOO", ["O", "O", "O"]) pm.define_group("manybody", "1.5", ["C", "H"]) pm.define_group("manybody", "1.5", ["C", "O"]) pm.define_group("manybody", "1.5", ["O", "H"]) pm.define_group("manybody", "2", ["O", "O"]) pm.define_group("manybody", "2", ["H", "O"]) pm.define_group("manybody", "2.8", ["O", "O"]) pm.set_parameters("**", [1, 0.5]) pm.set_parameters("OO", [1, 0.5]) pm.set_parameters("***", [1, 0.5]) pm.set_parameters("OOO", [1, 0.5]) pm.set_parameters("1.5", [1, 0.5, 1.5]) pm.set_parameters("2", [1, 0.5, 2]) pm.set_parameters("2.8", [1, 0.5, 2.8]) pm.set_constraints("2", [True, False]) pm.set_constraints("2.8", False) pm.set_parameters("cutoff_twobody", 5) pm.set_parameters("cutoff_threebody", 4) pm.set_parameters("cutoff_manybody", 3) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm)
def test_constraints1(): ''' simplest senario ''' pm = ParameterHelper(species=['O', 'C', 'H'], kernels={'twobody': [['*', '*'], ['O', 'O']], 'threebody': [['*', '*', '*'], ['O', 'O', 'O']]}, parameters={'twobody0': [1, 0.5], 'twobody1': [2, 0.2], 'threebody0': [1, 0.5], 'threebody1': [2, 0.2], 'cutoff_twobody': 2, 'cutoff_threebody': 1}, constraints={'twobody0': [True, False], 'threebody0': [False, True], 'noise': False}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm) assert hm['train_noise'] is False hyps = hm['hyps'] assert len(hyps) == 6 assert hyps[0] == 1 assert hyps[1] == 2 assert hyps[2] == 0.2 assert hyps[3] == 2 assert hyps[4] == 0.5 assert hyps[5] == 0.2
def test_initialization5(): """check universal""" pm = ParameterHelper(species=['O', 'C', 'H'], kernels={'twobody': [['*', '*'], ['O', 'O']], 'threebody': [['*', '*', '*'], ['O', 'O', 'O']]}, parameters={'sigma': 1, 'lengthscale': 0.5, 'cutoff_threebody': 3, 'cutoff_twobody': 2}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm) pm = ParameterHelper(kernels=['twobody', 'threebody'], parameters={'sigma': 1.0, 'lengthscale': 0.5, 'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def flare_calc(): flare_calc_dict = {} for md_engine in md_list: # ---------- create gaussian process model ------------------- # set up GP hyperparameters kernels = ["twobody", "threebody"] # use 2+3 body kernel parameters = {"cutoff_twobody": 5.0, "cutoff_threebody": 3.5} pm = ParameterHelper(kernels=kernels, random=True, parameters=parameters) hm = pm.as_dict() hyps = hm["hyps"] cut = hm["cutoffs"] print("hyps", hyps) gp_model = GaussianProcess( kernels=kernels, component="sc", # single-component. For multi-comp, use 'mc' hyps=hyps, cutoffs=cut, hyp_labels=["sig2", "ls2", "sig3", "ls3", "noise"], opt_algorithm="L-BFGS-B", n_cpus=1, ) # ----------- create mapped gaussian process ------------------ grid_params = { "twobody": { "grid_num": [64] }, "threebody": { "grid_num": [16, 16, 16] }, } mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=[1, 2], n_cpus=1, var_map="pca") # ------------ create ASE's flare calculator ----------------------- flare_calculator = FLARE_Calculator(gp_model, mgp_model=mgp_model, par=True, use_mapping=True) flare_calc_dict[md_engine] = flare_calculator print(md_engine) yield flare_calc_dict del flare_calc_dict
def test_initialization3(): """check group definition""" pm = ParameterHelper(species=['O', 'C', 'H'], kernels={'twobody': [['*', '*'], ['O', 'O']], 'threebody': [['*', '*', '*'], ['O', 'O', 'O']]}, parameters={'twobody0': [1, 0.5], 'twobody1': [2, 0.2], 'threebody0': [1, 0.5], 'threebody1': [2, 0.2], 'cutoff_twobody': 2, 'cutoff_threebody': 1}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def test_opt(): pm = ParameterHelper(species=['O', 'C', 'H'], kernels={'twobody': [['*', '*'], ['O', 'O']], 'threebody': [['*', '*', '*'], ['O', 'O', 'O']]}, parameters={'twobody0': [1, 0.5, 1], 'twobody1': [2, 0.2, 2], 'threebody0': [1, 0.5], 'threebody1': [2, 0.2], 'cutoff_twobody': 2, 'cutoff_threebody': 1}, constraints={'twobody0': [False, True]}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def test_generate_by_list(): pm = ParameterHelper(verbose="DEBUG") pm.list_groups('specie', ['O', ['C', 'N'], 'H']) pm.list_groups('twobody', [['*', '*'], ['O', 'O']]) pm.list_groups('threebody', [['*', '*', '*'], ['O', 'O', 'O']]) pm.list_parameters({'twobody0': [1, 0.5], 'twobody1': [2, 0.2], 'threebody0': [1, 0.5], 'threebody1': [2, 0.2], 'cutoff_twobody': 2, 'cutoff_threebody': 1}) hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def flare_calc(): flare_calc_dict = {} for md_engine in md_list: # ---------- create gaussian process model ------------------- # set up GP hyperparameters kernels = ['twobody', 'threebody'] # use 2+3 body kernel parameters = {'cutoff_twobody': 5.0, 'cutoff_threebody': 3.5} pm = ParameterHelper(kernels=kernels, random=True, parameters=parameters) hm = pm.as_dict() hyps = hm['hyps'] cut = hm['cutoffs'] print('hyps', hyps) gp_model = GaussianProcess( kernels=kernels, component='sc', # single-component. For multi-comp, use 'mc' hyps=hyps, cutoffs=cut, hyp_labels=['sig2', 'ls2', 'sig3', 'ls3', 'noise'], opt_algorithm='L-BFGS-B', n_cpus=1) # ----------- create mapped gaussian process ------------------ grid_params = { 'twobody': { 'grid_num': [64] }, 'threebody': { 'grid_num': [16, 16, 16] } } mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=[1, 2], n_cpus=1, var_map='pca') # ------------ create ASE's flare calculator ----------------------- flare_calculator = FLARE_Calculator(gp_model, mgp_model=mgp_model, par=True, use_mapping=True) flare_calc_dict[md_engine] = flare_calculator print(md_engine) yield flare_calc_dict del flare_calc_dict
def test_initialization2(ones): """ check ones, random """ pm = ParameterHelper( kernels=["twobody", "threebody"], parameters={"cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05}, ones=ones, random=not ones, verbose="DEBUG", ) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm)
def test_check_one_conflict(): ''' simplest senario ''' with raises(RuntimeError): pm = ParameterHelper(kernels=['twobody', 'threebody'], parameters={'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, ones=True, random=True, verbose="DEBUG") with raises(RuntimeError): pm = ParameterHelper(kernels=['twobody', 'threebody'], parameters={'sigma': 0.5, 'lengthscale': 1.0, 'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, ones=True, random=False, verbose="DEBUG")
def test_initialization_allsep(ones): ''' check ones, random ''' specie_list = ['C', 'H', 'O'] pm = ParameterHelper(species=specie_list, kernels=['twobody', 'threebody'], parameters={'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, allseparate=True, ones=ones, random=not ones, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm) name_list = [] for i in range(3): name = pm.find_group('specie', specie_list[i]) assert name not in name_list name_list += [name] name_list = [] for i in range(3): for j in range(i, 3): name = pm.find_group('twobody', [specie_list[i], specie_list[j]]) assert name not in name_list name_list += [name] with raises(RuntimeError): pm = ParameterHelper(species=[], kernels=['twobody', 'threebody'], parameters={'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, allseparate=True, ones=ones, random=not ones)
def test_initialization2(ones): ''' check ones, random ''' pm = ParameterHelper(kernels=['twobody', 'threebody'], parameters={'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, ones=ones, random=not ones, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def test_initialization(): ''' simplest senario ''' pm = ParameterHelper(kernels=['twobody', 'threebody'], parameters={'twobody': [1, 0.5], 'threebody': [1, 0.5], 'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm)
def test_initialization_allsep(ones): """ check ones, random """ specie_list = ["C", "H", "O"] pm = ParameterHelper( species=specie_list, kernels=["twobody", "threebody"], parameters={"cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05}, allseparate=True, ones=ones, random=not ones, verbose="DEBUG", ) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm) name_list = [] for i in range(3): name = pm.find_group("specie", specie_list[i]) assert name not in name_list name_list += [name] name_list = [] for i in range(3): for j in range(i, 3): name = pm.find_group("twobody", [specie_list[i], specie_list[j]]) assert name not in name_list name_list += [name] with raises(RuntimeError): pm = ParameterHelper( species=[], kernels=["twobody", "threebody"], parameters={"cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05}, allseparate=True, ones=ones, random=not ones, )
def test_from_dict(): pm = ParameterHelper( species=["O", "C", "H"], kernels=["twobody", "threebody"], allseparate=True, random=True, parameters={"cutoff_twobody": 7, "cutoff_threebody": 4.5, "cutoff_manybody": 3}, verbose="debug", ) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm) pm1 = ParameterHelper.from_dict(hm, verbose="debug", init_spec=["O", "C", "H"]) hm1 = pm1.as_dict() Parameters.compare_dict(hm, hm1)
def generate_diff_hm(kernels, diff_cutoff=False, constraint=False): pm = ParameterHelper(species=['H', 'He'], parameters={'noise': 0.05}) if ('twobody' in kernels): para1 = 2.5 + 0.1 * random(3) para2 = 2.5 + 0.1 * random(3) pm.set_parameters('cutoff_twobody', para1[-1]) pm.define_group('twobody', 'twobody0', ['*', '*']) pm.set_parameters('twobody0', para1[:-1], not constraint) pm.define_group('twobody', 'twobody1', ['H', 'H'], para2[:-1]) if (diff_cutoff): pm.set_parameters('twobody0', para1, not constraint) pm.set_parameters('twobody1', para2) if ('threebody' in kernels): para1 = 1.2 + 0.1 * random(3) para2 = 1.2 + 0.1 * random(3) pm.set_parameters('cutoff_threebody', para1[-1]) pm.define_group('threebody', 'threebody0', ['*', '*', '*'], para1[:-1]) pm.set_parameters('threebody0', para1[:-1], not constraint) pm.define_group('threebody', 'threebody1', ['H', 'H', 'H'], para2[:-1]) if (diff_cutoff): pm.define_group('cut3b', 'c1', ['*', '*'], parameters=para1) pm.define_group('cut3b', 'c2', ['H', 'H'], parameters=para2) if ('manybody' in kernels): para1 = 1.2 + 0.1 * random(3) para2 = 1.2 + 0.1 * random(3) pm.set_parameters('cutoff_manybody', para1[-1]) pm.define_group('manybody', 'manybody0', ['*', '*']) pm.set_parameters('manybody0', para1[:-1], not constraint) pm.define_group('manybody', 'manybody1', ['H', 'H'], para2[:-1]) if (diff_cutoff): pm.set_parameters('manybody0', para1, not constraint) pm.set_parameters('manybody1', para2) hm = pm.as_dict() hyps = hm['hyps'] cut = hm['cutoffs'] return cut, hyps, hm
def test_generate_by_line2(): pm = ParameterHelper(verbose="DEBUG") pm.define_group("specie", "O", ["O"]) pm.define_group("specie", "rest", ["C", "H"]) pm.define_group("twobody", "**", ["*", "*"]) pm.define_group("twobody", "OO", ["O", "O"]) pm.define_group("threebody", "***", ["*", "*", "*"]) pm.define_group("threebody", "Oall", ["O", "O", "O"]) pm.set_parameters("**", [1, 0.5]) pm.set_parameters("OO", [1, 0.5]) pm.set_parameters("Oall", [1, 0.5]) pm.set_parameters("***", [1, 0.5]) pm.set_parameters("cutoff_twobody", 5) pm.set_parameters("cutoff_threebody", 4) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm)
def generate_diff_hm(kernels, diff_cutoff=False, constraint=False): pm = ParameterHelper(species=["H", "He"], parameters={"noise": 0.05}) if "twobody" in kernels: para1 = 2.5 + 0.1 * random(3) para2 = 2.5 + 0.1 * random(3) pm.set_parameters("cutoff_twobody", para1[-1]) pm.define_group("twobody", "twobody0", ["*", "*"]) pm.set_parameters("twobody0", para1[:-1], not constraint) pm.define_group("twobody", "twobody1", ["H", "H"], para2[:-1]) if diff_cutoff: pm.set_parameters("twobody0", para1, not constraint) pm.set_parameters("twobody1", para2) if "threebody" in kernels: para1 = 1.2 + 0.1 * random(3) para2 = 1.2 + 0.1 * random(3) pm.set_parameters("cutoff_threebody", para1[-1]) pm.define_group("threebody", "threebody0", ["*", "*", "*"], para1[:-1]) pm.set_parameters("threebody0", para1[:-1], not constraint) pm.define_group("threebody", "threebody1", ["H", "H", "H"], para2[:-1]) if diff_cutoff: pm.define_group("cut3b", "c1", ["*", "*"], parameters=para1) pm.define_group("cut3b", "c2", ["H", "H"], parameters=para2) if "manybody" in kernels: para1 = 1.2 + 0.1 * random(3) para2 = 1.2 + 0.1 * random(3) pm.set_parameters("cutoff_manybody", para1[-1]) pm.define_group("manybody", "manybody0", ["*", "*"]) pm.set_parameters("manybody0", para1[:-1], not constraint) pm.define_group("manybody", "manybody1", ["H", "H"], para2[:-1]) if diff_cutoff: pm.set_parameters("manybody0", para1, not constraint) pm.set_parameters("manybody1", para2) hm = pm.as_dict() hyps = hm["hyps"] cut = hm["cutoffs"] return cut, hyps, hm
def test_initialization(): """ simplest senario """ pm = ParameterHelper( kernels=["twobody", "threebody"], parameters={ "twobody": [1, 0.5], "threebody": [1, 0.5], "cutoff_twobody": 2, "cutoff_threebody": 1, "noise": 0.05, }, verbose="DEBUG", ) hm = pm.as_dict() Parameters.check_instantiation(hm["hyps"], hm["cutoffs"], hm["kernels"], hm)
def test_from_dict(): pm = ParameterHelper(species=['O', 'C', 'H'], kernels=['twobody', 'threebody'], allseparate=True, random=True, parameters={'cutoff_twobody': 7, 'cutoff_threebody': 4.5, 'cutoff_manybody': 3}, verbose="debug") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm) pm1 = ParameterHelper.from_dict( hm, verbose="debug", init_spec=['O', 'C', 'H']) hm1 = pm1.as_dict() Parameters.compare_dict(hm, hm1)
def test_constraints2(): ''' simplest senario ''' pm = ParameterHelper(kernels=['twobody', 'threebody'], parameters={'twobody': [1, 0.5], 'threebody': [1, 0.5], 'cutoff_twobody': 2, 'cutoff_threebody': 1, 'noise': 0.05}, constraints={'twobody': [True, False]}, verbose="DEBUG") hm = pm.as_dict() Parameters.check_instantiation( hm['hyps'], hm['cutoffs'], hm['kernels'], hm) hyps = hm['hyps'] assert hyps[0] == 1 assert hyps[1] == 1