def ReadConfigFile(stream, schema): """ Read parameters from a file using YAML syntax The function uses the specified `schema` to: * check if parameter values are consistent with `choices` * infer the `type` of each parameter * check if any required parameters are missing Parameters ---------- stream : open file object, str an open file object or the string returned by calling `read` schema : ConstructorSchema the schema which tells the parser which holds the relevant information about the necessary parameters Returns ------- ns : argparse.Namespace the namespace holding the loaded parameters that are valid according to the input schema unknown : argparse.Namespace a namespace holding any parsed parameters not present in the scema """ from nbodykit.cosmology import Cosmology from nbodykit import plugin_manager, GlobalCosmology # make new namespaces to hold parsed attributes ns, unknown = Namespace(), Namespace() # read the yaml config file try: config = yaml.load(stream) # if the YAML loader returns a string or None # then it failed to find any valid keys if isinstance(config, (str, type(None))): raise EmptyConfigurationError( "no valid keys found in input YAML file") except EmptyConfigurationError: raise # just re-raise this type of error except Exception as e: raise ConfigurationError("error parsing YAML file: %s" % str(e)) # FIRST: tell plugin manager to load user plugins plugins = [] if 'X' in config: plugins = config['X'] if isinstance(plugins, str): plugins = [plugins] plugin_manager.add_user_plugin(*plugins) config.pop('X') # SECOND: load any cosmology specified via 'cosmo' key cosmo = None if 'cosmo' in config: cosmo = Cosmology(**config.pop('cosmo')) GlobalCosmology.set(cosmo) # THIRD: iterate through schema, filling parameter namespace missing = [] extra = config.copy() for name in schema: fill_namespace(ns, schema[name], extra, missing) # FOURTH: update the 'unknown' namespace with extra, ignored keys for k in extra: setattr(unknown, k, extra[k]) # FIFTH: crash if we don't have all required parameters if len(missing): raise ValueError("missing required arguments: %s" % str(missing)) return ns, unknown
def ReadConfigFile(stream, schema): """ Read parameters from a file using YAML syntax The function uses the specified `schema` to: * check if parameter values are consistent with `choices` * infer the `type` of each parameter * check if any required parameters are missing Parameters ---------- stream : open file object, str an open file object or the string returned by calling `read` schema : ConstructorSchema the schema which tells the parser which holds the relevant information about the necessary parameters Returns ------- ns : argparse.Namespace the namespace holding the loaded parameters that are valid according to the input schema unknown : argparse.Namespace a namespace holding any parsed parameters not present in the scema """ from nbodykit.cosmology import Cosmology from nbodykit import plugin_manager, GlobalCosmology # make new namespaces to hold parsed attributes ns, unknown = Namespace(), Namespace() # read the yaml config file try: config = yaml.load(stream) # if the YAML loader returns a string or None # then it failed to find any valid keys if isinstance(config, (str, type(None))): raise EmptyConfigurationError("no valid keys found in input YAML file") except EmptyConfigurationError: raise # just re-raise this type of error except Exception as e: raise ConfigurationError("error parsing YAML file: %s" %str(e)) # FIRST: tell plugin manager to load user plugins plugins = [] if 'X' in config: plugins = config['X'] if isinstance(plugins, str): plugins = [plugins] plugin_manager.add_user_plugin(*plugins) config.pop('X') # SECOND: load any cosmology specified via 'cosmo' key cosmo = None if 'cosmo' in config: cosmo = Cosmology(**config.pop('cosmo')) GlobalCosmology.set(cosmo) # THIRD: iterate through schema, filling parameter namespace missing = [] extra = config.copy() for name in schema: fill_namespace(ns, schema[name], extra, missing) # FOURTH: update the 'unknown' namespace with extra, ignored keys for k in extra: setattr(unknown, k, extra[k]) # FIFTH: crash if we don't have all required parameters if len(missing): raise ValueError("missing required arguments: %s" %str(missing)) return ns, unknown
def wrapper(self, *args, **kwargs): from nbodykit import GlobalCosmology setattr(self, 'cosmo', kwargs.pop('cosmo', GlobalCosmology.get())) return init(self, *args, **kwargs)