Пример #1
0
def test_length():
    timesteps = 5 * 12
    d = {
        'initial_account_balance': [1200, 1000],
        'initial_salary': [65000, 65000],
        'initial_yearly_expenses': [40000, 40000],
        'initial_investments': [0, 0],
        'average_market_growth': [0.06, 0.06],
        'initial_debt': [30000, 30000],
        'debt_interest_rate': [0.0376, 0.0376],
        'initial_monthly_debt_payment': [600, 600],
        'simplified_income_tax_rate': [0.25, 0.25],
        'initial_year': [2013, 2013],
        'initial_month': [3, 3],
        'investment_rate': [0.10, 0.10]
    }
    df = pd.DataFrame(d)
    declarative.turn_off_progress_bar = True
    ie = declarative.IterativeEngine(df, 'home_economics', timesteps, True)

    # giving more processers than needed
    # failing because we can't handle the return type of tax_brackets
    ie.calculate(processors=1)

    df = ie.results_to_df()
    print(df)
    for xs in df.values:
        for x in xs:
            assert x is not pd.NA
    assert len(df) == timesteps * len(d['initial_debt'])
Пример #2
0
def test_parallel_runs_unneeded2():
    timesteps = 5 * 12
    d = {
        'initial_account_balance': [1200],
        'initial_salary': [65000],
        'initial_yearly_expenses': [40000],
        'initial_investments': [0],
        'average_market_growth': [0.06],
        'initial_debt': [30000],
        'debt_interest_rate': [0.0376],
        'initial_monthly_debt_payment': [600],
        'simplified_income_tax_rate': [0.25],
        'initial_year': [2013],
        'initial_month': [3],
        'investment_rate': [0.10]
    }
    df = pd.DataFrame(d)
    declarative.turn_off_progress_bar = True
    ie = declarative.IterativeEngine(df, 'home_economics', timesteps, True)

    # having iterative engine to split work acorss available cpus
    ie.calculate(processors=None)

    df = ie.results_to_df()
    print(df[list(ie.engine.func_dict.keys())])
    print(df[list([key for key in ie.engine.func_dict.keys() if 'debt' in key])])
    for xs in df.values:
        for x in xs:
            assert x is not pd.NA
    assert len(df) == timesteps
Пример #3
0
def test_missing_file():
    timesteps = 5 * 12
    d = {
        'initial_account_balance': [1200],
        'initial_salary': [65000],
        'initial_yearly_expenses': [40000],
        'initial_investments': [0],
        'average_market_growth': [0.06],
        'initial_debt': [30000],
        'debt_interest_rate': [0.0376],
        'initial_monthly_debt_payment': [600],
        'simplified_income_tax_rate': [0.25],
        'initial_year': [2013],
        'initial_month': [3],
        'investment_rate': [0.10]
    }
    df = pd.DataFrame(d)
    declarative.turn_off_progress_bar = True
    try:
        # we specifically want this error thrown as early as possible
        ie = declarative.IterativeEngine(df, 'non_existent_file', timesteps, True)

        # we don't want the error thrown from calculate
    except ModuleNotFoundError:
        pass  # good!
Пример #4
0
def f48(t, f47):
    return f47[t] + 1


def f49(t, f48):
    return f48[t] + 1


if __name__ == '__main__':
    import pandas as pd
    import numpy as np
    from pathlib import Path
    import context
    import declarative
    current_file = Path(__file__).stem
    timesteps = 100
    for optimization in range(0, 6):
        df = pd.DataFrame()
        declarative.turn_off_progress_bar = True
        ie = declarative.IterativeEngine(df,
                                         t=timesteps,
                                         display_progressbar=True)
        ie.calculate(1, optimization)
        #print(ie.engine.results)
        df = ie.results_to_df()
        print(df)

        for col in df.columns:
            assert df.isna().sum(
            )[col] == 0, f"in optimization {optimization} -- {col} is has pd.NA values"
Пример #5
0
    import os
    import pandas as pd
    import numpy as np

    # Make our process run on a dedicated CPU
    # p = psutil.Process(os.getpid())
    # p.nice(psutil.REALTIME_PRIORITY_CLASS)
    # p.cpu_affinity([1])

    timesteps = 60 * 24 #* 35 #* 12
    repeat = 30
    # Test every optimization for single and multiprocess
    for processors in [1, 3]:
        for optimization in range(1,6):
            df = pd.DataFrame([[100, 17, 0.99, 0]], columns=['initial_cash', 'fixed_expenses', 'sales_price', 'cost_per_sale'])
            df = pd.DataFrame(np.repeat(df.values, repeat, axis=0), columns=df.columns)
            declarative.turn_off_progress_bar = True
            ie = declarative.IterativeEngine(df, 'to_profile', timesteps, True)
            ie.calculate(processors, optimization)

            df = ie.results_to_df()
            print(df)
        
            # ASSERT NO NA
            for col in df.columns:
                assert df.isna().sum()[col] == 0, f"running with {processors} processor(s) -- in optimization {optimization} -- {col} is has pd.NA values"

            # ASSERT NO MISSING ROWS
            assert len(df) == timesteps * repeat, f'running with {processors} processor(s) -- in optimization {optimization} -- {len(df)} <> {timesteps * repeat}'

def g(f):

    return f * 2


def h(input_value):
    return input_value**2


def print_g(g):
    print(f'g:{g}')
    return f'g:{g}'


def print_f_after_g(f, print_g):
    print(f'f: {f}')
    return f'f: {f}'


if __name__ == '__main__':
    import pandas as pd
    d = {'input_value': [3.14, 3.22]}
    ie = declarative.IterativeEngine(pd.DataFrame(d),
                                     t=1,
                                     display_progressbar=False)
    ie.calculate()
    print(ie.results_to_df())

    for bp in ie.engine.bps:
        print(bp.code)