gemseo / wrappers

disc_from_exe module

Make a discipline from an executable

class gemseo.wrappers.disc_from_exe.DiscFromExe(input_template, output_template, output_folder_basepath, executable_command, input_filename, output_filename, folders_iter='numbered', name=None, parse_outfile_method='TEMPLATE_PARSER', write_input_file_method=None, parse_out_separator='=')[source]

Bases: gemseo.core.discipline.MDODiscipline

Generic wrapper for executables.

The DiscFromExe is a generic wrapper for executables. It generates a MDODiscipline from an executable and in inputs/output files wrappers. The input and output files are described by templates. The templates can be generated by executing the module template_grammar_editor to open a GUI.

It requires the creation of templates for input and output file, for instance, from the following input JSON file:

{
"a": 1.01515112125,
"b": 2.00151511213,
"c": 3.00151511213
}

A template that declares the inputs must be generated under this format, where “a” is the name of the input, and “1.0” is the default input. GEMSEO_INPUT declares an input, GEMSEO_OUTPUT declares an output, similarly.

{
"a": GEMSEO_INPUT{a::1.0},
"b": GEMSEO_INPUT{b::2.0},
"c": GEMSEO_INPUT{c::3.0}
}

Current limitations :

Only one input and one output file, otherwise, inherit from this class and modify the parsers. Only limited input writing and output parser strategies are implemented. To change that, you can pass custom parsing and writing methods to the constructor.

The only limitation in the current file format is that it must be a plain text file and not a binary file. In this case, the way of interfacing it is to provide a specific parser to the DiscFromExe, with the write_input_file_method and parse_outfile_method arguments of the constructor.

Constructor.

Create the discipline from the inputs, outputs wrapper and the executable command.

Parameters
  • input_template (str) – path to the input file template. The input locations in the file are marked by GEMSEO_INPUT{input_name::1.0}, where “input_name” is the input name, and 1.0 is here the default input

  • output_template (str) – path to the output file template. The input locations in the file are marked by GEMSEO_OUTPUT{output_name::1.0}, where “output_name” is the input name

  • output_folder_basepath (str) – path to the output folder, in which the executions will be performed

  • executable_command (str) – command to run the executable. Will be called through a system call. Example: “python myscript.py -i input.txt -o output.txt

  • input_filename (str) – name of the input file. This will determine the name of the input file generated in the output folder. Example “input.txt”

  • output_filename (str) – name of the output file. This will determine the name of the output file generated in the output folder. Example “output.txt”

  • folders_iter (str) – type of unique identifiers for the output folders. If NUMBERED the generated output folders will be “output_folder_basepath”+str(i+1), where i is the maximum value of the already existing “output_folder_basepath”+str(i) folders. Otherwise, a unique number based on the UUID function is generated. This last option shall be used if multiple MDO processes are runned in the same work directory.

  • parse_outfile_method (str) – optional method that can be provided by the user to parse the output file. To see the signature of the method, see the parse_outfile method of this file.

  • parse_out_separator (str) – if the KEY_VALUE_PARSER is used as output parser, specify the separator key (default : “=”).

  • write_input_file_method (str) – method to write the input file, if None, use this modules’ write_input_file. To see the signature of the method, see the write_input_file method of this file.

KEY_VALUE_PARSER = 'KEY_VALUE_PARSER'
NUMBERED = 'numbered'
TEMPLATE_PARSER = 'TEMPLATE_PARSER'
UUID = 'uuid'
generate_uid()[source]

Generate an UUID.

Generate a unique identifier for the current execution If the folders_iter strategy is NUMBERED, the successive iterations are named by an integer 1, 2, 3 etc. This is multiprocess safe. Otherwise, a unique number based on the UUID function is generated. This last option shall be used if multiple MDO processes are runned in the same workdir.

Returns

a unique string identifier

Return type

str

gemseo.wrappers.disc_from_exe.parse_key_value_file(_, out_lines, separator='=')[source]

Parse the output file from the expected text positions.

Parameters
  • out_lines – list of lines of the output file template (result of file.readlines())

  • separator – separating characters of the key=value format

Returns

the values dictionary in dict of numpy array formats

gemseo.wrappers.disc_from_exe.parse_outfile(output_positions, out_lines)[source]

Parse the output file from the expected text positions.

Parameters
  • output_positions – dictionary containing the information from the template format {data_name:(start,end,dictionary)}, where name is the name of the output data, start is the index of the starting point in the input file template. This index is a line index (character number on the line) end is the index of the end character in the template line_number is the index of the line in the file

  • out_lines – list of lines of the output file template (result of file.readlines())

Returns

the values dictionary in dict of numpy array formats

gemseo.wrappers.disc_from_exe.parse_template(template_lines, grammar_type='input')[source]

Parse the input or output template.

Parameters
  • template_lines – list of lines of the file template (result of file.readlines())

  • grammar_type – INPUT_GRAMMAR or OUTPUT_GRAMMAR

Returns

data_dict, pos_dict, where data_dict is the {name:value} dict, where name is the data name and value is the parsed input or output value in the template pos_dict in the format dictionary containing the information from the template format {data_name:(start,end,line_number)}, where name is the name of the input data, start is the index of the starting point in the input file template. This index is a line index (character number on the line) end is the index of the end character in the template line_number is the index of the line in the file

gemseo.wrappers.disc_from_exe.write_input_file(input_file_path, data, input_positions, input_lines, float_format='{:1.18g}')[source]

Write the input file from the input data.

Parameters
  • input_file_path – absolute path to the file to be written

  • data – data dictionary, ie the local data of the discipline

  • input_positions – dictionary containing the information from the template format {data_name:(start,end,line_number)}, where name is the name of the input data, start is the index of the starting point in the input file template. This index is a line index (character number on the line) end is the index of the end character in the template line_number is the index of the line in the file

  • input_lines – list of lines of the input file template (result of file.readlines())

  • float_format – formating of the input data in the file