Example #1
0
    def _check_config(prob):
        if options.outfile is None:
            logger = get_logger('check_config', use_format=True)
        else:
            outfile = open(options.outfile, 'w')
            logger = get_logger('check_config', out_stream=outfile, use_format=True)

        if not options.checks:
            options.checks = sorted(_checks.keys())

        for c in options.checks:
            _checks[c](prob, logger)

        exit()
Example #2
0
    def _check_config(prob):
        if not MPI or MPI.COMM_WORLD.rank == 0:
            if options.outfile is None:
                logger = get_logger('check_config', out_stream='stdout',
                                    out_file=None, use_format=True)
            else:
                logger = get_logger('check_config', out_file=options.outfile, use_format=True)

            if not options.checks:
                options.checks = sorted(_default_checks)

            prob.check_config(logger, options.checks)

        exit()
Example #3
0
    def _check_config(prob):
        if not MPI or MPI.COMM_WORLD.rank == 0:
            if options.outfile is None:
                logger = get_logger('check_config', use_format=True)
            else:
                outfile = open(options.outfile, 'w')
                logger = get_logger('check_config', out_stream=outfile, use_format=True)

            if not options.checks:
                options.checks = sorted(_checks.keys())

            for c in options.checks:
                _checks[c](prob, logger)

        exit()
Example #4
0
def print_citations(prob, classes=None, out_stream='stdout'):
    """
    Write a list of citations from classes in the problem to the given stream.

    Parameters
    ----------
    prob : <Problem>
        The Problem instance to be searched
    classes : list of str
        List of class names for classes to include in the displayed citations.
    out_stream : 'stdout', 'stderr' or file-like
            Where to send human readable output. Default is 'stdout'.
            Set to None to suppress.
    """
    citations = _filter_citations(find_citations(prob), classes)

    if out_stream:
        logger = get_logger('citations', out_stream=out_stream)
        for klass, cite in citations.items():
            # print("Class: {}".format(klass), file=out_stream)
            logger.info("Class: {}".format(klass))
            lines = cite.split('\n')
            for line in lines:
                # print("    {}".format(line), file=out_stream)
                logger.info("    {}".format(line))
Example #5
0
def check_config(problem, logger=None):
    """
    Perform optional error checks on a Problem.

    Parameters
    ----------
    problem : Problem
        The Problem being checked.
    logger : object
        Logging object.
    """
    logger = logger if logger else get_logger('check_config', use_format=True)

    for c in sorted(_checks.keys()):
        _checks[c](problem, logger)
Example #6
0
def check_config(problem, logger=None):
    """
    Perform optional error checks on a Problem.

    Parameters
    ----------
    problem : Problem
        The Problem being checked.
    logger : object
        Logging object.
    """
    logger = logger if logger else get_logger('check_config', use_format=True)

    for c in sorted(_checks.keys()):
        _checks[c](problem, logger)
Example #7
0
def check_config(problem, logger=None):
    """
    Perform optional error checks on a Problem.

    Parameters
    ----------
    problem : Problem
        The Problem being checked.
    logger : object
        Logging object.
    """
    logger = logger if logger else get_logger('check_config', use_format=True)

    _check_hanging_inputs(problem, logger)

    for system in problem.model.system_iter(include_self=True, recurse=True):
        # system specific check
        system.check_config(logger)
        # check dataflow within Group
        if isinstance(system, Group):
            _check_dataflow(system, logger)
Example #8
0
def get_test_src(method_path):
    """
    Return desired source code for a single feature after testing it.

    Used by embed_test.

    1. Get the source code for a unit test method
    2. Replace the asserts with prints -> source_minus_docstrings_with_prints_cleaned
    3. Split source_minus_docstrings_with_prints_cleaned up into groups of "In" blocks -> input_blocks
    4. Insert extra print statements into source_minus_docstrings_with_prints_cleaned
            to indicate start and end of print Out blocks -> source_with_output_start_stop_indicators
    5. Run the test using source_with_out_start_stop_indicators -> run_outputs
    6. Extract from run_outputs, the Out blocks -> output_blocks
    7. Return source_minus_docstrings_with_prints_cleaned, input_blocks, output_blocks, skipped, failed

    Parameters
    ----------
    method_path : str
        Module hiearchy path to the test.

    Returns
    -------
    str
        Cleaned source code, ready for inclusion in doc.
    str
        Reason that the test failed or was skipped.
    list of str
        List of input code blocks
    list of str
        List of Python output blocks
    bool
        True if test was skipped
    bool
        True if test failed
    """

    #----------------------------------------------------------
    # 1. Get the source code for a unit test method.
    #----------------------------------------------------------

    module_path = '.'.join(method_path.split('.')[:-2])
    class_name = method_path.split('.')[-2]
    method_name = method_path.split('.')[-1]

    test_module = importlib.import_module(module_path)
    cls = getattr(test_module, class_name)
    try:
        import mpi4py
    except ImportError:
        use_mpi = False
    else:
        N_PROCS = getattr(cls, 'N_PROCS', 1)
        use_mpi = N_PROCS > 1
    meth = getattr(cls, method_name)
    class_source_code = inspect.getsource(cls)

    # Directly manipulating function text to strip header and remove leading whitespace.
    # Should be faster than redbaron
    method_source_code = inspect.getsource(meth)
    meth_lines = method_source_code.split('\n')
    counter = 0
    past_header = False
    new_lines = []
    for line in meth_lines:
        if not past_header:
            n1 = len(line)
            newline = line.lstrip()
            n2 = len(newline)
            tab = n1 - n2
            if counter == 0:
                first_len = tab
            elif n1 == 0:
                continue
            if tab == first_len:
                counter += 1
                newline = line[tab:]
            else:
                past_header = True
        else:
            newline = line[tab:]
        # exclude 'global' directives, not needed the way we are running things
        if not newline.startswith("global "):
            new_lines.append(newline)

    method_source = '\n'.join(new_lines[counter:])

    # Remove docstring from source code
    source_minus_docstrings = remove_docstrings(method_source)

    #-----------------------------------------------------------------------------------
    # 2. Replace the asserts with prints -> source_minus_docstrings_with_prints_cleaned
    #-----------------------------------------------------------------------------------

    # Replace some of the asserts with prints of the actual values
    # This calls RedBaron
    source_minus_docstrings_with_prints = replace_asserts_with_prints(
        source_minus_docstrings)

    # remove raise SkipTest lines
    # We decided to leave them in for now
    # source_minus_docstrings_with_prints = remove_raise_skip_tests(source_minus_docstrings_with_prints)

    # Remove the initial empty lines
    source_minus_docstrings_with_prints_cleaned = remove_initial_empty_lines_from_source(
        source_minus_docstrings_with_prints)

    #-----------------------------------------------------------------------------------
    # 4. Insert extra print statements into source_minus_docstrings_with_prints_cleaned
    #        to indicate start and end of print Out blocks -> source_with_output_start_stop_indicators
    #-----------------------------------------------------------------------------------
    source_with_output_start_stop_indicators = insert_output_start_stop_indicators(
        source_minus_docstrings_with_prints_cleaned)

    #-----------------------------------------------------------------------------------
    # 5. Run the test using source_with_out_start_stop_indicators -> run_outputs
    #-----------------------------------------------------------------------------------

    # Get all the pieces of code needed to run the unit test method
    global_imports = globals_for_imports(method_source)
    teardown_source_code = get_method_body(
        inspect.getsource(getattr(cls, 'tearDown')))

    code_to_run = '\n'.join([
        global_imports, source_with_output_start_stop_indicators,
        teardown_source_code
    ])

    skipped = False
    failed = False
    try:

        # Use Subprocess if we are under MPI.
        if use_mpi:

            # Write it to a file so we can run it.
            fd, code_to_run_path = tempfile.mkstemp()

            with os.fdopen(fd, 'w') as tmp:
                tmp.write(code_to_run)
                tmp.close()
            env = os.environ.copy()
            env['USE_PROC_FILES'] = '1'
            p = subprocess.Popen(
                ['mpirun', '-n',
                 str(N_PROCS), 'python', code_to_run_path],
                env=env)
            p.wait()
            multi_out_blocks = []
            for i in range(N_PROCS):
                with open('%d.out' % i) as f:
                    multi_out_blocks.append(extract_output_blocks(f.read()))
                os.remove('%d.out' % i)
            output_blocks = []
            for i in range(len(multi_out_blocks[0])):
                output_blocks.append('\n'.join([
                    "(rank %d) %s" % (j, m[i])
                    for j, m in enumerate(multi_out_blocks) if m[i]
                ]))

        # Just Exec the code for serial tests.
        else:

            stdout = sys.stdout
            stderr = sys.stderr
            strout = cStringIO()
            sys.stdout = strout
            sys.stderr = strout

            # Hacking, but trying to make sure we capture the check config messages.
            from openmdao.utils.logger_utils import _loggers, get_logger
            if 'check_config' not in _loggers:
                get_logger('check_config', use_format=True)
            if 'check_partials' not in _loggers:
                get_logger('check_partials')
            if 'check_totals' not in _loggers:
                get_logger('check_totals')

            _loggers['check_config']['logger'].handlers[0].stream = strout
            _loggers['check_partials']['logger'].handlers[0].stream = strout
            _loggers['check_totals']['logger'].handlers[0].stream = strout

            # We need more precision from numpy
            save_opts = np.get_printoptions()
            np.set_printoptions(precision=8)

            exec(code_to_run, {})
            np.set_printoptions(precision=save_opts['precision'])
            run_outputs = strout.getvalue()

    except subprocess.CalledProcessError as e:
        # Get a traceback.
        if 'raise unittest.SkipTest' in e.output.decode('utf-8'):
            reason_for_skip = e.output.splitlines(
            )[-1][len('unittest.case.SkipTest: '):]
            run_outputs = reason_for_skip
            skipped = True
        else:
            run_outputs = "Running of embedded test {} in docs failed due to: \n\n{}".format(
                method_path, e.output.decode('utf-8'))
            failed = True

    except Exception as err:
        if 'SkipTest' in code_to_run:
            txt1 = code_to_run.split('SkipTest(')[1]
            run_outputs = txt1.split(')')[0]
            skipped = True
        else:
            msg = "Running of embedded test {} in docs failed due to: \n\n{}"
            run_outputs = msg.format(method_path, str(err))
            failed = True

    finally:
        if use_mpi:
            os.remove(code_to_run_path)
        else:
            sys.stdout = stdout
            sys.stderr = stderr

    if PY3 and not use_mpi and not isinstance(run_outputs, str):
        run_outputs = "".join(map(
            chr, run_outputs))  # in Python 3, run_outputs is of type bytes!

    if not skipped and not failed:
        #####################
        ### 3. Split source_minus_docstrings_with_prints_cleaned up into groups of "In" blocks -> input_blocks ###
        #####################
        input_blocks = split_source_into_input_blocks(
            source_minus_docstrings_with_prints_cleaned)

        #####################
        ### 6. Extract from run_outputs, the Out blocks -> output_blocks ###
        #####################
        if not use_mpi:
            output_blocks = extract_output_blocks(run_outputs)

        # Need to deal with the cases when there is no outputblock for a given input block
        # Merge an input block with the previous block and throw away the output block
        input_blocks, output_blocks = clean_up_empty_output_blocks(
            input_blocks, output_blocks)
        skipped_failed_output = None
    else:
        input_blocks = output_blocks = None
        skipped_failed_output = run_outputs

    return source_minus_docstrings_with_prints_cleaned, skipped_failed_output, input_blocks, output_blocks, skipped, failed