def parseCTDCommandLine(argv, model, openms_param): # Configure CTDOpt to use OpenMS style on the command line. directives = parse_cl_directives(argv, input_ctd='ini', write_tool_ctd='write_ini', prefix='-') if directives[ "write_tool_ctd"] is not None: # triggered if -write_ini was provided on CML # if called with -write_ini write CTD model.write_ctd(directives["write_tool_ctd"]) exit(0) elif directives["input_ctd"] is not None: # read ctd/ini file model = CTDModel(from_file=directives["input_ctd"]) # print(model.get_defaults()) param = pms.Param() fh = pms.ParamXMLFile() fh.load(directives["input_ctd"], param) openms_param.update(param, True) return model.get_defaults(), openms_param else: # only command line options provided temp = tempfile.NamedTemporaryFile( suffix='ini') # makes sure we get a writable file tmp_name = temp.name temp.close() # removes the file model.write_ctd(tmp_name) param = pms.Param() fh = pms.ParamXMLFile() fh.load(tmp_name, param) openms_param.update(param) os.remove(tmp_name) return model.parse_cl_args(argv), openms_param
# Let's print out the name attributes of these parameters. print('The following parameters were registered in the model:') print([p.name for p in params]) print() # In the above model, certain parameters were registered under parameter groups. We can access their 'lineage' and see # their nesting levels. Let's display nesting levels separated by colons: print('The same parameters with subgroup information, if they were registered under parameter groups:') print([':'.join(p.get_lineage(name_only=True)) for p in params]) print() # (Parameter.get_lineage() returns a list of ParameterGroups down to the leaf Parameter. `name_only` setting returns # only the names of the objects, instead of the actual Parameter objects. # Some of the parameters had default values in the model. We can get those: print('A dictionary of parameters with default values, returned by CTDModel.get_defaults():') defaults = model_2.get_defaults() pretty_print(defaults) print() print ('As you can see, parameter values are usually stored in nested dictionaries. If you want a flat dictionary, you can' 'get that using CTDopts.flatten_dict(). Flat keys can be either tuples of tree node (subgroup) names down to the parameter...') flat_defaults = flatten_dict(defaults) pretty_print(flat_defaults) print() print('...or they can be strings where nesing levels are separated by colons:') flat_defaults_colon = flatten_dict(defaults, as_string=True) pretty_print(flat_defaults_colon) print() print ('We can create dictionaries of arguments on our own that we want to validate against the model.'
except ModelTypeError: pass try: ini_model = Parameters(from_file=args.ini_file) except ModelTypeError: pass assert ini_model is not None, "Could not parse %s, seems to be no CTD/PARAMS" % ( args.ini_file) # get a dictionary of the ctd arguments where the values of the parameters # given on the command line are overwritten ini_values = ini_model.parse_cl_args(cl_args=cliargs, ignore_required=True) if args.ctd_file: ctd_model = CTDModel(from_file=args.ctd_file) ctd_values = ctd_model.get_defaults() for param in ini_model.get_parameters(): if not param.required and (param.default is None or type(param.default) is _Null): lineage = param.get_lineage(name_only=True) try: default = getFromDict(ctd_values, lineage) except KeyError: continue setInDict(ini_values, lineage, default) # write the ctd with the values taken from the dictionary out = StringIO() ctd_tree = ini_model.write_ctd(out, ini_values) print(out.getvalue())
# In the above model, certain parameters were registered under parameter groups. We can access their 'lineage' and see # their nesting levels. Let's display nesting levels separated by colons: print( 'The same parameters with subgroup information, if they were registered under parameter groups:' ) print([':'.join(p.get_lineage(name_only=True)) for p in params]) print() # (Parameter.get_lineage() returns a list of ParameterGroups down to the leaf Parameter. `name_only` setting returns # only the names of the objects, instead of the actual Parameter objects. # Some of the parameters had default values in the model. We can get those: print( 'A dictionary of parameters with default values, returned by CTDModel.get_defaults():' ) defaults = model_2.get_defaults() pretty_print(defaults) print() print( 'As you can see, parameter values are usually stored in nested dictionaries. If you want a flat dictionary, you can' 'get that using CTDopts.flatten_dict(). Flat keys can be either tuples of tree node (subgroup) names down to the parameter...' ) flat_defaults = flatten_dict(defaults) pretty_print(flat_defaults) print() print('...or they can be strings where nesing levels are separated by colons:') flat_defaults_colon = flatten_dict(defaults, as_string=True) pretty_print(flat_defaults_colon) print()