def save(self, output_file, overwrite=False): """Save the model to disk""" if os.path.exists(output_file) and overwrite is False: raise ModelFileExists("The file %s exists already. If you want to overwrite it, use the 'overwrite=True' " "options as 'model.save(\"%s\", overwrite=True)'. " % (output_file, output_file)) else: data = self.to_dict_with_types() # Write it to disk try: # Get the YAML representation of the data representation = my_yaml.dump(data) with open(output_file, "w+") as f: # Add a new line at the end of each voice (just for clarity) f.write(representation.replace("\n", "\n\n")) except IOError: raise CannotWriteModel(os.path.dirname(os.path.abspath(output_file)), "Could not write model file %s. Check your permissions to write or the " "report on the free space which follows: " % output_file)
def save(self, output_file, overwrite=False): """Save the model to disk""" if os.path.exists(output_file) and overwrite is False: raise ModelFileExists( "The file %s exists already. If you want to overwrite it, use the 'overwrite=True' " "options as 'model.save(\"%s\", overwrite=True)'. " % (output_file, output_file)) else: data = self.to_dict_with_types() # Write it to disk try: # Get the YAML representation of the data representation = my_yaml.dump(data) with open(output_file, "w+") as f: # Add a new line at the end of each voice (just for clarity) f.write(representation.replace("\n", "\n\n")) except IOError: raise CannotWriteModel( os.path.dirname(os.path.abspath(output_file)), "Could not write model file %s. Check your permissions to write or the " "report on the free space which follows: " % output_file)
def xspec_model_factory(model_name, xspec_function, model_type, definition): class_name = 'XS_%s' % model_name # Get the path to the user data directory user_data_path = get_user_data_path() # Check if the code for this function already exists code_file_name = os.path.join(user_data_path, '%s.py' % class_name) if os.path.exists(code_file_name): # Code already exists pass else: print("Generating code for Xspec model %s..." % model_name) # If this is an additive model (model_type == 'add') we need to add # one more parameter (normalization) if model_type == 'add': definition['parameters']['norm'] = {'initial value': 1.0, 'desc': '(see https://heasarc.gsfc.nasa.gov/xanadu/xspec/manual/' 'XspecModels.html)', 'min': 0, 'max': None, 'delta': 0.1, 'unit': 'keV / (cm2 s)', 'free': True} assert model_type != 'con', "Convolution models are not yet supported" # Get a list of the parameter names parameters_names = ", ".join(definition['parameters'].keys()) # Create the docstring docstring = my_yaml.dump(definition) # Create the class by substituting in the class_definition_code the # relevant things for this model code = class_definition_code.replace('$MODEL_NAME$', model_name) code = code.replace('$DOCSTRING$', docstring) code = code.replace('$PARAMETERS_NAMES$', parameters_names) code = code.replace('$XSPEC_FUNCTION$', xspec_function) code = code.replace('$MODEL_TYPE$', model_type) # Write to the file with open(code_file_name, 'w+') as f: f.write("# This code has been automatically generated. Do not edit.\n") f.write("\n\n%s\n" % code) # Add the path to sys.path if it doesn't if user_data_path not in sys.path: sys.path.append(user_data_path) # Import the class in the current namespace (locals) with warnings.catch_warnings(): warnings.simplefilter("error") exec('from %s import %s' % (class_name, class_name)) # Return the class we just created return class_name, locals()[class_name]
def xspec_model_factory(model_name, xspec_function, model_type, definition): class_name = 'XS_%s' % model_name # Get the path to the user data directory user_data_path = get_user_data_path() # Check if the code for this function already exists code_file_name = os.path.join(user_data_path, '%s.py' % class_name) if os.path.exists(code_file_name): # Code already exists pass else: print("Generating code for Xspec model %s..." % model_name) # If this is an additive model (model_type == 'add') we need to add # one more parameter (normalization) if model_type == 'add': definition['parameters']['norm'] = { 'initial value': 1.0, 'desc': '(see https://heasarc.gsfc.nasa.gov/xanadu/xspec/manual/' 'XspecModels.html)', 'min': 0, 'max': None, 'delta': 0.1, 'unit': 'keV / (cm2 s)', 'free': True } assert model_type != 'con', "Convolution models are not yet supported" # Get a list of the parameter names parameters_names = ", ".join(definition['parameters'].keys()) # Create the docstring docstring = my_yaml.dump(definition) # Create the class by substituting in the class_definition_code the # relevant things for this model code = class_definition_code.replace('$MODEL_NAME$', model_name) code = code.replace('$DOCSTRING$', docstring) code = code.replace('$PARAMETERS_NAMES$', parameters_names) code = code.replace('$XSPEC_FUNCTION$', xspec_function) code = code.replace('$MODEL_TYPE$', model_type) # Write to the file with open(code_file_name, 'w+') as f: f.write( "# This code has been automatically generated. Do not edit.\n") f.write("\n\n%s\n" % code) # Add the path to sys.path if it doesn't if user_data_path not in sys.path: sys.path.append(user_data_path) # Import the class in the current namespace (locals) with warnings.catch_warnings(): warnings.simplefilter("error") exec('from %s import %s' % (class_name, class_name)) # Return the class we just created return class_name, locals()[class_name]