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_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 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 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_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_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_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
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_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
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 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_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_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_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_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 get_random_training_set(nenv, nstruc): """Create a random training_set array with parameters And generate four different kinds of hyperparameter sets: * multi hypper parameters with two twobody type and two threebody type * constrained optimization, with noise parameter optimized * constrained optimization, without noise parameter optimized * simple hyper parameters without multihyps set up """ np.random.seed(0) cutoffs = {"twobody": 0.8, "threebody": 0.8} parameters = { "cutoff_twobody": 0.8, "cutoff_threebody": 0.8, "noise": 0.05 } # 9 different hyper-parameters pm = ParameterHelper( species=["H", "He"], kernels={ "twobody": [["*", "*"], ["H", "H"]], "threebody": [["*", "*", "*"], ["H", "H", "H"]], }, parameters=parameters, ones=True, random=False, verbose="DEBUG", ) hyps_mask1 = pm.as_dict() # 9 different hyper-parameters, onlye train the 0, 2, 4, 6, 8 pm.set_constraints("twobody0", [True, True]) pm.set_constraints("twobody1", [False, False]) pm.set_constraints("threebody0", [True, True]) pm.set_constraints("threebody1", [False, False]) hyps_mask2 = pm.as_dict() # 9 different hyper-parameters, only train the 0, 2, 4, 6 pm.set_constraints("noise", False) hyps_mask3 = pm.as_dict() # 5 different hyper-parameters, equivalent to no multihyps pm = ParameterHelper( species=["H", "He"], kernels={ "twobody": [["*", "*"]], "threebody": [["*", "*", "*"]] }, parameters=parameters, ones=True, random=False, verbose="DEBUG", ) hyps_mask4 = pm.as_dict() # 5 different hyper-parameters, no multihyps pm = ParameterHelper( kernels=["twobody", "threebody"], parameters=parameters, ones=True, random=False, verbose="DEBUG", ) hyps_mask5 = pm.as_dict() hyps_mask_list = [ hyps_mask1, hyps_mask2, hyps_mask3, hyps_mask4, hyps_mask5 ] # create training environments and forces cell = np.eye(3) unique_species = [0, 1] noa = 5 training_data = [] training_labels = [] for _ in range(nenv): positions = np.random.uniform(-1, 1, [noa, 3]) species = np.random.randint(0, len(unique_species), noa) + 1 struc = Structure(cell, species, positions) training_data += [AtomicEnvironment(struc, 1, cutoffs)] training_labels += [np.random.uniform(-1, 1, 3)] training_labels = np.hstack(training_labels) # create training structures and energies training_structures = [] energy_labels = [] for _ in range(nstruc): positions = np.random.uniform(-1, 1, [noa, 3]) species = np.random.randint(0, len(unique_species), noa) + 1 struc = Structure(cell, species, positions) struc_envs = [] for n in range(noa): struc_envs.append(AtomicEnvironment(struc, n, cutoffs)) training_structures.append(struc_envs) energy_labels.append(np.random.uniform(-1, 1)) energy_labels = np.array(energy_labels) # store it as global variables name = "unit_test" flare.gp_algebra._global_training_data[name] = training_data flare.gp_algebra._global_training_labels[name] = training_labels flare.gp_algebra._global_training_structures[name] = training_structures flare.gp_algebra._global_energy_labels[name] = energy_labels energy_noise = 0.01 return name, cutoffs, hyps_mask_list, energy_noise
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_list2(): pm = ParameterHelper(verbose="DEBUG") pm.list_groups('specie', {'s1': 'O', 's2': ['C', 'N'], 's3': 'H'}) pm.list_groups( 'twobody', {'t0': ['*', '*'], 't1': [['s1', 's1'], ['s1', 's3']]}) pm.list_groups('threebody', [['*', '*', '*'], ['s1', 's1', 's1']]) pm.list_parameters({'t0': [1, 0.5], 't1': [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 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 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 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 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_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)