def from_dict(hyps_mask, verbose=False, init_spec=[]): """convert dictionary mask to HM instance This function is not tested yet """ Parameters.check_instantiation( hyps_mask["hyps"], hyps_mask["cutoffs"], hyps_mask["kernels"], hyps_mask ) pm = ParameterHelper(verbose=verbose) nspecie = hyps_mask["nspecie"] if nspecie > 1: max_species = np.max(hyps_mask["specie_mask"]) specie_mask = hyps_mask["specie_mask"] for i in range(max_species + 1): elelist = np.where(specie_mask == i)[0] if len(elelist) > 0: for ele in elelist: if ele != 0: elename = Z_to_element(ele) if len(init_spec) > 0: if elename in init_spec: pm.define_group("specie", i, [elename]) else: pm.define_group("specie", i, [elename]) else: pm.define_group("specie", i, ["*"]) for kernel in hyps_mask["kernels"] + ParameterHelper.cutoff_types_keys: n = hyps_mask.get("n" + kernel, 0) if n >= 0: if kernel not in ParameterHelper.cutoff_types: chyps, copt = Parameters.get_component_hyps( hyps_mask, kernel, constraint=True, noise=False ) sig = chyps[0] ls = chyps[1] csig = copt[0] cls = copt[1] cutoff = hyps_mask["cutoffs"][kernel] pm.set_parameters("cutoff_" + kernel, cutoff) cutoff_list = hyps_mask.get( f"{kernel}_cutoff_list", np.ones(len(sig)) * cutoff ) elif kernel in ParameterHelper.cutoff_types and n > 1: cutoff_list = hyps_mask[ ParameterHelper.cutoff_types[kernel] + "_cutoff_list" ] if n > 1: all_specie = np.arange(nspecie) all_comb = combinations_with_replacement( all_specie, ParameterHelper.ndim[kernel] ) for comb in all_comb: mask_id = 0 for ele in comb: mask_id += ele mask_id *= nspecie mask_id = mask_id // nspecie ttype = hyps_mask[f"{kernel}_mask"][mask_id] pm.define_group(f"{kernel}", f"{kernel}{ttype}", comb) if (kernel not in ParameterHelper.cutoff_types) and ( kernel not in ParameterHelper.cutoff_types_values ): pm.set_parameters( f"{kernel}{ttype}", [sig[ttype], ls[ttype], cutoff_list[ttype]], opt=[csig[ttype], cls[ttype]], ) elif kernel in ParameterHelper.cutoff_types_values: pm.set_parameters( f"{kernel}{ttype}", [sig[ttype], ls[ttype]], opt=[csig[ttype], cls[ttype]], ) else: pm.set_parameters(f"{kernel}{ttype}", cutoff_list[ttype]) else: pm.define_group( kernel, kernel, ["*"] * ParameterHelper.ndim[kernel] ) if kernel not in ParameterHelper.cutoff_types_keys: pm.set_parameters( kernel, parameters=np.hstack([sig, ls, cutoff]), opt=copt ) else: pm.set_parameters(kernel, parameters=cutoff) hyps = Parameters.get_hyps(hyps_mask) pm.set_parameters("noise", hyps[-1]) if "cutoffs" in hyps_mask: cutoffs = hyps_mask["cutoffs"] for k in cutoffs: pm.set_parameters(f"cutoff_{k}", cutoffs[k]) return pm
def from_mask_to_args(hyps, cutoffs, hyps_mask=None): """Return the tuple of arguments needed for kernel function. The order of the tuple has to be exactly the same as the one taken by the kernel function. :param hyps: list of hyperparmeter values :type hyps: nd.array :param hyps_mask: all the remaining parameters needed :type hyps_mask: dictionary :param cutoffs: cutoffs used :return: args """ # no special setting multihyps = True if hyps_mask is None: multihyps = False elif hyps_mask["nspecie"] == 1: multihyps = False if not multihyps: cutoffs_array = [0, 0, 0] cutoffs_array[0] = cutoffs.get("twobody", 0) cutoffs_array[1] = cutoffs.get("threebody", 0) cutoffs_array[2] = cutoffs.get("manybody", 0) return (hyps, cutoffs_array) # setting for mc_sephyps nspecie = hyps_mask["nspecie"] n2b = hyps_mask.get("ntwobody", 0) n3b = hyps_mask.get("nthreebody", 0) nmanybody = hyps_mask.get("nmanybody", 0) ncut3b = hyps_mask.get("ncut3b", 0) twobody_mask = hyps_mask.get("twobody_mask", None) threebody_mask = hyps_mask.get("threebody_mask", None) manybody_mask = hyps_mask.get("manybody_mask", None) cut3b_mask = hyps_mask.get("cut3b_mask", None) # TO DO , should instead use the non-sephyps kernel if n2b == 1: twobody_mask = np.zeros(nspecie ** 2, dtype=int) if n3b == 1: threebody_mask = np.zeros(nspecie ** 3, dtype=int) if nmanybody == 1: manybody_mask = np.zeros(nspecie ** 2, dtype=int) cutoff_2b = cutoffs.get("twobody", 0) cutoff_3b = cutoffs.get("threebody", 0) cutoff_mb = cutoffs.get("manybody", 0) if "bond_cutoff_list" in hyps_mask: cutoff_2b = hyps_mask["bond_cutoff_list"] else: cutoff_2b = np.ones(nspecie ** 2, dtype=float) * cutoff_2b if "threebody_cutoff_list" in hyps_mask: cutoff_3b = hyps_mask["threebody_cutoff_list"] if "manybody_cutoff_list" in hyps_mask: cutoff_mb = hyps_mask["manybody_cutoff_list"] (sig2, ls2) = Parameters.get_component_hyps(hyps_mask, "twobody", hyps=hyps) (sig3, ls3) = Parameters.get_component_hyps(hyps_mask, "threebody", hyps=hyps) (sigm, lsm) = Parameters.get_component_hyps(hyps_mask, "manybody", hyps=hyps) return ( cutoff_2b, cutoff_3b, cutoff_mb, nspecie, np.array(hyps_mask["species_mask"]), n2b, twobody_mask, n3b, threebody_mask, ncut3b, cut3b_mask, nmanybody, manybody_mask, sig2, ls2, sig3, ls3, sigm, lsm, )