def __init__(self, model_file=None, model_dict=None): assert (model_file is not None) or (model_dict is not None), ( "You have to provide either a model file or a" "model dictionary") if model_file is not None: # Read model file and deserialize into a dictionary try: with open(model_file) as f: self._model_dict = my_yaml.load(f, Loader=my_yaml.FullLoader) except IOError: raise ModelIOError( "File %s cannot be read. Check path and permissions for current user." % model_file) except my_yaml.YAMLError: raise ModelYAMLError( "Could not parse file %s. Check your syntax." % model_file) else: self._model_dict = model_dict self._parse()
def _load_one_results(fits_extension): # Gather analysis type analysis_type = fits_extension.header.get("RESUTYPE") # Gather the optimized model serialized_model = _escape_back_yaml_from_fits( fits_extension.header.get("MODEL")) model_dict = my_yaml.load(serialized_model) optimized_model = ModelParser(model_dict=model_dict).get_model() # Gather statistics values statistic_values = collections.OrderedDict() measure_values = collections.OrderedDict() for key in fits_extension.header.keys(): if key.find("STAT") == 0: # Found a keyword with a statistic for a plugin # Gather info about it id = int(key.replace("STAT", "")) value = float(fits_extension.header.get(key)) name = fits_extension.header.get("PN%i" % id) statistic_values[name] = value if key.find("MEAS") == 0: # Found a keyword with a statistic for a plugin # Gather info about it id = int(key.replace("MEAS", "")) name = fits_extension.header.get(key) value = float(fits_extension.header.get("MV%i" % id)) measure_values[name] = value if analysis_type == "MLE": # Get covariance matrix covariance_matrix = np.atleast_2d( fits_extension.data.field("COVARIANCE").T) # Instance and return return MLEResults(optimized_model, covariance_matrix, statistic_values, statistical_measures=measure_values) elif analysis_type == "Bayesian": # Gather samples samples = fits_extension.data.field("SAMPLES") # Instance and return return BayesianResults(optimized_model, samples.T, statistic_values, statistical_measures=measure_values)