def from_yml(cls, yml_path: str): input_dict = load_yaml(yml_path) cls_dict = dict() vlsrs, Ncols = list(), list() # make sure the number of components is the same assert len(input_dict["vlsrs"]) == len(input_dict["Ncols"]) n_components = len(input_dict["vlsrs"]) # parse in all the different parameters for param_list, parameter in zip([vlsrs, Ncols], ["vlsrs", "Ncols"]): for index in range(n_components): size_params = input_dict[parameter][index] size_params["name"] = f"{parameter}_{index}" if "mu" in size_params: dist = GaussianLikelihood elif "value" in size_params: dist = DeltaLikelihood else: dist = UniformLikelihood param_list.append(dist.from_values(**size_params)) cls_dict[parameter] = param_list # the three stragglers for key in ["source_size", "Tex", "dV"]: input_dict[key]["name"] = key if "mu" in input_dict[key]: dist = GaussianLikelihood elif "value" in input_dict[key]: dist = DeltaLikelihood else: dist = UniformLikelihood cls_dict[key] = dist.from_values(**input_dict[key]) # load in the observed data cls_dict["observation"] = load(input_dict["observation"]) cls_dict["molecule"] = load(input_dict["molecule"]) return cls(**cls_dict)
def from_yml(cls, yml_path: str): """ Creates a composite model from a YAML input. As one can imagine, the format of this YAML is substantially different from the non-composite models. The structure of the YAML should be like: ``` param_indices: [[1, 2, 3,], [1, 2, 3]] model_A: model: TMC1FourComponent yml_path: model_A.yml model_B: model: CospatialTMC1 yml_path: model_B.yml ``` The `model` subkeys should correspond to the name of a model class, which is used to grab the correct one to instantiate. It basically then relies on each `submodel.from_yml` method to create the submodel and then append it to full model list. Parameters ---------- yml_path : str Path to the composite model YAML specification Raises ------ KeyError If the parameter indices are not found in the composite YAML specification. NameError If the model name specified """ yml_data = load_yaml(yml_path) # get the parameter indices and remove it from the dictionary param_indices = yml_data.pop("param_indices", None) if not param_indices: raise KeyError("param_indices not specified in YAML file.") models = list() for index, subdict in enumerate(yml_data.values()): # this tries to get the class model_type = globals()[subdict.get("model")] if not model_type: raise NameError( f"Model type for model {index + 1} is not implemented: {subdict.get('model')}" ) # construct the submodel, and throw it in the pile submodel = model_type.from_yml(subdict.get("yml_path")) models.append(submodel) return cls(param_indices, *models)
def from_yml(cls, yml_path: str): input_dict = load_yaml(yml_path) cls_dict = dict() # the two stragglers for key in input_dict.keys(): if key not in ["observation", "molecule", "nominal_vlsr"]: if hasattr(input_dict[key], "mu"): dist = GaussianLikelihood else: dist = UniformLikelihood cls_dict[key] = dist.from_values(**input_dict[key]) else: if key != "nominal_vlsr": # load in the observed data cls_dict[key] = load(input_dict[key]) else: logger.warning(f"{key} is not recognized, and therefore ignored.") return cls(**cls_dict)