Exemplo n.º 1
0
def test_log_values():
    state = Mock_Model_State_Shape(
        a=1.1,
        b=2.2,
        target='humbug',
    )
    log_process = log_values(state_inputs=lambda state: [
        I(state.a, as_='a'),
        I(state.target, as_='foo'),
    ], )
    processes = [log_process]
    assert type(log_process) == Process
    process_runner = ProcessRunner()
    assert len(process_runner.state_logs) == 1
    process_runner.run_processes(processes, state)
    assert len(process_runner.state_logs) == 1
    assert process_runner.state_logs[0] == {'a': 1.1, 'foo': 'humbug'}
Exemplo n.º 2
0
def test_log_skip_row():
    state = Mock_Model_State_Shape(
        a=1.1,
        b=2.2,
        target='humbug',
    )
    log_process = log_values(state_inputs=lambda state: [
        I(state.a, as_='a'),
        I(state.target, as_='foo'),
    ], )
    processes = [log_process]
    process_runner = ProcessRunner()
    process_runner.run_processes(processes, state)

    # Progress 2 time steps without logging
    process_runner.tm.advance_row()
    process_runner.tm.advance_row()
    log_process = log_values(state_inputs=lambda state: [
        I(state.a, as_='a'),
        I(state.target, as_='foo'),
    ], )
    processes = [log_process]
    process_runner.run_processes(processes, state)
    assert len(process_runner.state_logs) == 3
    assert process_runner.state_logs[0] == {'a': 1.1, 'foo': 'humbug'}
    assert process_runner.state_logs[1] == {}
    assert process_runner.state_logs[2] == {'a': 1.1, 'foo': 'humbug'}
Exemplo n.º 3
0
def test_log_next_row():
    state = Mock_Model_State_Shape(
        a=1.1,
        b=2.2,
        target='humbug',
    )
    log_process = log_values(state_inputs=lambda state: [
        I(state.a, as_='a'),
        I(state.target, as_='foo'),
    ], )
    processes = [log_process]
    process_runner = ProcessRunner()
    process_runner.run_processes(processes, state)

    # Add next row
    process_runner.tm.advance_row()
    log_process = log_values(state_inputs=lambda state: [
        I(state.a, as_='a'),
        I(state.target, as_='foo'),
    ], )
    processes = [log_process]
    process_runner.run_processes(processes, state)
    assert len(process_runner.state_logs) == 2
    assert process_runner.state_logs[0] == {'a': 1.1, 'foo': 'humbug'}
    assert process_runner.state_logs[1] == {'a': 1.1, 'foo': 'humbug'}
Exemplo n.º 4
0
def test_appending_to_state_list():
    """We can use '.+' to append to a list.

    This requires the process to set the format_output flag to true.
    """
    existing_logs = [
        {
            'a': 1,
            'foo': 'bar'
        },
        {
            'a': 2,
            'foo': 'barh'
        },
        {
            'a': 3,
            'foo': 'barhum'
        },
        {
            'a': 4,
            'foo': 'barhumb'
        },
    ]
    state = Mock_Model_State_Shape(
        1.1,
        2.2,
        logs=existing_logs,
    )
    new_log = {'a': 5, 'foo': 'barhumbug'},
    processes = [
        Process(
            func=lambda: new_log,
            # We have to use format output flag to enable this
            format_output=True,
            state_outputs=lambda result: [(result, 'logs.+')])
    ]
    process_runner = ProcessRunner()
    state_out = process_runner.run_processes(processes, state)
    assert state_out.logs[4] == new_log
Exemplo n.º 5
0

def process_add(x, y):
    return x + y


def process_add_complex(x, y, *args, **kwargs):
    total = 0
    for i in range(10000):
        total += x + y + i
    return total


process_runner = ProcessRunner(
    Mock_Config_Shape(),
    Mock_External_State_Shape(),
    Mock_Parameters_Shape(),
    DEBUG_MODE=True,
)


@pytest.fixture(scope="module", autouse=True)
def _():
    with patch('proflow.ProcessRunner.Model_State_Shape', side_effect=Mock_Model_State_Shape) \
            as Mocked_State_Shape:
        Mocked_State_Shape.__annotations__ = Mock_Model_State_Shape.__annotations__
        yield Mocked_State_Shape


@pytest.fixture(scope="module", autouse=True)
def __():
    with patch('proflow.ProcessRunner.Config_Shape',
Exemplo n.º 6
0
"""Using CProfile to find bottlenecks."""

from proflow.Objects.Interface import I
from proflow.Objects.Process import Process
from pstats import SortKey
from timeit import repeat
import pstats
import cProfile
from vendor.helpers.list_helpers import flatten_list
from proflow.ProcessRunnerCls import ProcessRunner
from proflow.tests.mocks import Mock_Model_State_Shape, Mock_Config_Shape, Mock_External_State_Shape
# %%
config = Mock_Config_Shape(1.1, 2.2)

process_runner = ProcessRunner(config, DEBUG_MODE=True)

# %%
process_runner.external_state = Mock_External_State_Shape()

# %%
# == SETUP INITIAL STATE
initial_state = Mock_Model_State_Shape(1, 2)
initial_state.matrix = [[i * j for i in range(100)] for j in range(100)]
# %%

DEMO_PROCESSES = [
    Process(func=lambda x, y, z: x + y + z,
            comment="Demo process a",
            config_inputs=lambda config: [
                I(config.foo, as_='x'),
            ],
Exemplo n.º 7
0
from proflow.tests.mocks import Mock_Config_Shape, Mock_External_State_Shape, \
    Mock_Model_State_Shape, Mock_Nested_State, Mock_Parameters_Shape
from proflow.ProcessRunnerCls import ProcessRunner, advance_time_step_process
from unittest.mock import MagicMock, patch
from vendor.helpers.list_helpers import flatten_list, filter_none

from .Objects.Interface import I
from .Objects.Process import Process


def process_add(x, y):
    return x + y


process_runner = ProcessRunner(Mock_Config_Shape(),
                               Mock_External_State_Shape(),
                               Mock_Parameters_Shape())

# @pytest.fixture(scope="module", autouse=True)
# def _():
#     with patch('proflow.ProcessRunner.Model_State_Shape', side_effect=Mock_Model_State_Shape) \
#             as Mocked_State_Shape:
#         Mocked_State_Shape.__annotations__ = Mock_Model_State_Shape.__annotations__
#         yield Mocked_State_Shape


@pytest.fixture(scope="module", autouse=True)
def __():
    with patch('proflow.ProcessRunnerCls.Config_Shape',
               return_value=Mock_Config_Shape) as _fixture:
        yield _fixture