Пример #1
0
def main():
    sysrng = random.SystemRandom()

    params = load_json(os.path.join(SCRIPT_DIR, 'transient.json'))
    params['random_seed'] = sysrng.randint(1, 2**31 - 1)

    S_init = [sysrng.random(), sysrng.random()]
    params['S_init'] = S_init
    I_init = [sysrng.uniform(0.0, S_init[0]), sysrng.uniform(0.0, S_init[1])]
    params['I_init'] = I_init

    ts_filename = 'transient.npy'
    if os.path.exists(ts_filename):
        C = numpy.load(ts_filename)
    else:
        sir_out = models.run_via_pypy('multistrain_sde', params)
        C = numpy.array(sir_out['C'])
        numpy.save(ts_filename, C)

    years = 100
    simulation_samples_per_year = 36
    ccm_samples_per_year = 12
    add_samples = True
    log_transform = False
    first_difference = False
    standardize = False

    # Connect to output database
    if os.path.exists('results.sqlite'):
        sys.stderr.write('Output database present. Aborting.\n')
        sys.exit(1)
    db = sqlite3.connect('results.sqlite')

    # Set up RNG
    rng = numpy.random.RandomState(sysrng.randint(1, 2**31 - 1))
    X = subsample_simulation(C, years, simulation_samples_per_year,
                             ccm_samples_per_year, add_samples, log_transform,
                             first_difference, standardize)

    if numpy.any(numpy.logical_or(numpy.isnan(X), numpy.isinf(X))):
        sys.stderr.write('nans or infs in data; skipping all analyses.\n')
        sys.exit(0)

    x0 = X[:, 0]
    x1 = X[:, 1]

    variable_name = 'C'
    x0name = variable_name + '0'
    x1name = variable_name + '1'

    plot_timeseries([x0, x1], [x0name, x1name], 'time', 'value',
                    'timeseries.png')

    run_analysis(x0name, x0, x1name, x1, db, rng)
    run_analysis(x1name, x1, x0name, x0, db, rng)

    db.commit()
    db.close()

    plot_data_by_lag()
Пример #2
0
def run_simulation():
    try:
        db.execute('CREATE TABLE sir_params (params TEXT)')
        db.execute('INSERT INTO sir_params VALUES (?)', (params.dump_to_string(),))

        return models.run_via_pypy('multistrain_sde', params)
    except models.ExecutionException as e:
        sys.stderr.write('An exception occurred trying to run simulation...\n')
        sys.stderr.write('{0}\n'.format(e.cause))
        sys.stderr.write(e.stderr_data)
        sys.exit(1)
Пример #3
0
def run_simulation():
    try:
        db.execute('CREATE TABLE sir_params (params TEXT)')
        db.execute('INSERT INTO sir_params VALUES (?)',
                   (params.dump_to_string(), ))

        return models.run_via_pypy('multistrain_sde', params)
    except models.ExecutionException as e:
        sys.stderr.write('An exception occurred trying to run simulation...\n')
        sys.stderr.write('{0}\n'.format(e.cause))
        sys.stderr.write(e.stderr_data)
        sys.exit(1)
Пример #4
0
def main():
    ccm_params = load_json(os.path.join('ccm_params.json'))
    params = load_json(os.path.join('params.json'))

    ts_filename = os.path.join('transient.npy')
    if os.path.exists(ts_filename):
        C = numpy.load(ts_filename)
    else:
        try:
            sir_out = models.run_via_pypy('multistrain_sde', params)
        except models.ExecutionException as e:
            print e.cause
            print e.stdout_data
            print e.stderr_data
            sys.exit(1)
        C = numpy.array(sir_out['C'])
        numpy.save(ts_filename, C)

    years = int(params['t_end'] / 360)
    simulation_samples_per_year = 36
    ccm_samples_per_year = ccm_params['samples_per_year']
    add_samples = True
    log_transform = False
    first_difference = False
    standardize = False

    #     t = numpy.arange(years * 12) / 12.0

    #     print t.shape
    print C.shape

    X = subsample_simulation(C, years, simulation_samples_per_year,
                             ccm_samples_per_year, add_samples, log_transform,
                             first_difference, standardize)

    if numpy.any(numpy.logical_or(numpy.isnan(X), numpy.isinf(X))):
        sys.stderr.write('nans or infs in data; skipping all analyses.\n')
        sys.exit(0)

    x0 = X[:, 0]
    x1 = X[:, 1]

    variable_name = 'C'
    x0name = variable_name + '0'
    x1name = variable_name + '1'

    # Set up RNG
    rng = numpy.random.RandomState(random.SystemRandom().randint(1, 2**31 - 1))
    theiler_window = ccm_params['samples_per_year'] * 2

    # RUN ON START
    start_range = ccm_params['start_range']
    x0_start = x0[start_range[0] * ccm_samples_per_year:start_range[1] *
                  ccm_samples_per_year]
    x1_start = x1[start_range[0] * ccm_samples_per_year:start_range[1] *
                  ccm_samples_per_year]
    plot_timeseries([x0_start, x1_start], [x0name, x1name], 'time', 'value',
                    'ts_start.png')

    mid_range = ccm_params['mid_range']
    x0_mid = x0[mid_range[0] * ccm_samples_per_year:mid_range[1] *
                ccm_samples_per_year]
    x1_mid = x1[mid_range[0] * ccm_samples_per_year:mid_range[1] *
                ccm_samples_per_year]
    plot_timeseries([x0_mid, x1_mid], [x0name, x1name], 'time', 'value',
                    'ts_mid.png')

    end_range = ccm_params['end_range']
    x0_end = x0[end_range[0] * ccm_samples_per_year:end_range[1] *
                ccm_samples_per_year]
    x1_end = x1[end_range[0] * ccm_samples_per_year:end_range[1] *
                ccm_samples_per_year]
    plot_timeseries([x0_end, x1_end], [x0name, x1name], 'time', 'value',
                    'ts_end.png')

    if os.path.exists('results_start.sqlite'):
        sys.stderr.write('Output database present. Skipping CCM run.\n')
    else:
        db = sqlite3.connect('results_start.sqlite')
        run_analysis(x0name, x0_start, x1name, x1_start, db, rng,
                     theiler_window)
        run_analysis(x1name, x1_start, x0name, x0_start, db, rng,
                     theiler_window)
        db.commit()
        db.close()
    plot_data_by_lag('results_start.sqlite', 'ccm_by_lag_start.png')

    # RUN ON END

    if os.path.exists('results_end.sqlite'):
        sys.stderr.write('Output database present. Skipping CCM run.\n')
    else:
        db = sqlite3.connect('results_end.sqlite')
        run_analysis(x0name, x0_end, x1name, x1_end, db, rng, theiler_window)
        run_analysis(x1name, x1_end, x0name, x0_end, db, rng, theiler_window)
        db.commit()
        db.close()
    plot_data_by_lag('results_end.sqlite', 'ccm_by_lag_end.png')

    # RUN ON MIDDLE

    if os.path.exists('results_mid.sqlite'):
        sys.stderr.write('Output database present. Skipping CCM run.\n')
    else:
        db = sqlite3.connect('results_mid.sqlite')
        run_analysis(x0name, x0_mid, x1name, x1_mid, db, rng, theiler_window)
        run_analysis(x1name, x1_mid, x0name, x0_mid, db, rng, theiler_window)
        db.commit()
        db.close()
    plot_data_by_lag('results_mid.sqlite', 'ccm_by_lag_mid.png')
Пример #5
0
def main():
#     params = load_json(os.path.join(SCRIPT_DIR, 'transient.json'))
#     
    ts_filename = os.path.join(SCRIPT_DIR, 'transient.npy')
    if os.path.exists(ts_filename):
        C = numpy.load(ts_filename)
    else:
        sir_out = models.run_via_pypy('multistrain_sde', params)
        C = numpy.array(sir_out['C'])
        numpy.save(ts_filename, C)
#     
    years = 100
    simulation_samples_per_year = 36
    ccm_samples_per_year = 12
    add_samples = True
    log_transform = False
    first_difference = False
    standardize = False
#     
#     t = numpy.arange(100*12) / 12.0
#     
#     print t.shape
#     print C.shape
#     
#     # Connect to output database
#     if os.path.exists('results.sqlite'):
#         sys.stderr.write('Output database present. Aborting.\n')
#         sys.exit(1)
#     db = sqlite3.connect('results.sqlite')
# 
#     # Set up RNG
#     random_seed = 1234
#     rng = numpy.random.RandomState(random_seed)
    X = subsample_simulation(
        C, years, simulation_samples_per_year, ccm_samples_per_year,
        add_samples, log_transform, first_difference, standardize
    )
#     
#     if numpy.any(numpy.logical_or(
#         numpy.isnan(X),
#         numpy.isinf(X)
#     )):
#         sys.stderr.write('nans or infs in data; skipping all analyses.\n')
#         sys.exit(0)
#     
    x0 = X[:,0]
    x1 = X[:,1]

    variable_name = 'C'
    x0name = variable_name + '0'
    x1name = variable_name + '1'
    
    plot_timeseries([x0, x1], [x0name, x1name], 'time', 'value', 'timeseries.png')
#     
#     run_analysis(x0name, x0, x1name, x1, db, rng)
#     run_analysis(x1name, x1, x0name, x0, db, rng)
# 
#     db.commit()
#     db.close()
    
    plot_data_by_lag()
Пример #6
0
def main():
    sysrng = random.SystemRandom()
    
    params = load_json(os.path.join(SCRIPT_DIR, 'transient.json'))
    params['random_seed'] = sysrng.randint(1, 2**31 - 1)
    
    S_init = [sysrng.random(), sysrng.random()]
    params['S_init'] = S_init
    I_init = [sysrng.uniform(0.0, S_init[0]), sysrng.uniform(0.0, S_init[1])]
    params['I_init'] = I_init
    
    ts_filename = 'transient.npy'
    if os.path.exists(ts_filename):
        C = numpy.load(ts_filename)
    else:
        sir_out = models.run_via_pypy('multistrain_sde', params)
        C = numpy.array(sir_out['C'])
        numpy.save(ts_filename, C)
     
    years = 100
    simulation_samples_per_year = 36
    ccm_samples_per_year = 12
    add_samples = True
    log_transform = False
    first_difference = False
    standardize = False
    
    # Connect to output database
    if os.path.exists('results.sqlite'):
        sys.stderr.write('Output database present. Aborting.\n')
        sys.exit(1)
    db = sqlite3.connect('results.sqlite')

    # Set up RNG
    rng = numpy.random.RandomState(sysrng.randint(1, 2**31 - 1))
    X = subsample_simulation(
        C, years, simulation_samples_per_year, ccm_samples_per_year,
        add_samples, log_transform, first_difference, standardize
    )
    
    if numpy.any(numpy.logical_or(
        numpy.isnan(X),
        numpy.isinf(X)
    )):
        sys.stderr.write('nans or infs in data; skipping all analyses.\n')
        sys.exit(0)
    
    x0 = X[:,0]
    x1 = X[:,1]

    variable_name = 'C'
    x0name = variable_name + '0'
    x1name = variable_name + '1'
    
    plot_timeseries([x0, x1], [x0name, x1name], 'time', 'value', 'timeseries.png')
    
    run_analysis(x0name, x0, x1name, x1, db, rng)
    run_analysis(x1name, x1, x0name, x0, db, rng)

    db.commit()
    db.close()
    
    plot_data_by_lag()
Пример #7
0
def main():
    #     params = load_json(os.path.join(SCRIPT_DIR, 'transient.json'))
    #
    ts_filename = os.path.join(SCRIPT_DIR, 'transient.npy')
    if os.path.exists(ts_filename):
        C = numpy.load(ts_filename)
    else:
        sir_out = models.run_via_pypy('multistrain_sde', params)
        C = numpy.array(sir_out['C'])
        numpy.save(ts_filename, C)


#
    years = 100
    simulation_samples_per_year = 36
    ccm_samples_per_year = 12
    add_samples = True
    log_transform = False
    first_difference = False
    standardize = False
    #
    #     t = numpy.arange(100*12) / 12.0
    #
    #     print t.shape
    #     print C.shape
    #
    #     # Connect to output database
    #     if os.path.exists('results.sqlite'):
    #         sys.stderr.write('Output database present. Aborting.\n')
    #         sys.exit(1)
    #     db = sqlite3.connect('results.sqlite')
    #
    #     # Set up RNG
    #     random_seed = 1234
    #     rng = numpy.random.RandomState(random_seed)
    X = subsample_simulation(C, years, simulation_samples_per_year,
                             ccm_samples_per_year, add_samples, log_transform,
                             first_difference, standardize)
    #
    #     if numpy.any(numpy.logical_or(
    #         numpy.isnan(X),
    #         numpy.isinf(X)
    #     )):
    #         sys.stderr.write('nans or infs in data; skipping all analyses.\n')
    #         sys.exit(0)
    #
    x0 = X[:, 0]
    x1 = X[:, 1]

    variable_name = 'C'
    x0name = variable_name + '0'
    x1name = variable_name + '1'

    plot_timeseries([x0, x1], [x0name, x1name], 'time', 'value',
                    'timeseries.png')
    #
    #     run_analysis(x0name, x0, x1name, x1, db, rng)
    #     run_analysis(x1name, x1, x0name, x0, db, rng)
    #
    #     db.commit()
    #     db.close()

    plot_data_by_lag()
def main():
    ccm_params = load_json(os.path.join('ccm_params.json'))
    params = load_json(os.path.join('params.json'))
    
    ts_filename = os.path.join('transient.npy')
    if os.path.exists(ts_filename):
        C = numpy.load(ts_filename)
    else:
        try:
            sir_out = models.run_via_pypy('multistrain_sde', params)
        except models.ExecutionException as e:
            print e.cause
            print e.stdout_data
            print e.stderr_data
            sys.exit(1)
        C = numpy.array(sir_out['C'])
        numpy.save(ts_filename, C)
    
    years = int(params['t_end'] / 360)
    simulation_samples_per_year = 36
    ccm_samples_per_year = ccm_params['samples_per_year']
    add_samples = True
    log_transform = False
    first_difference = False
    standardize = False
    
#     t = numpy.arange(years * 12) / 12.0
    
#     print t.shape
    print C.shape

    X = subsample_simulation(
        C, years, simulation_samples_per_year, ccm_samples_per_year,
        add_samples, log_transform, first_difference, standardize
    )
    
    if numpy.any(numpy.logical_or(
        numpy.isnan(X),
        numpy.isinf(X)
    )):
        sys.stderr.write('nans or infs in data; skipping all analyses.\n')
        sys.exit(0)
    
    x0 = X[:,0]
    x1 = X[:,1]

    variable_name = 'C'
    x0name = variable_name + '0'
    x1name = variable_name + '1'
    
    # Set up RNG
    rng = numpy.random.RandomState(random.SystemRandom().randint(1, 2**31 - 1))
    theiler_window = ccm_params['samples_per_year'] * 2
    
    # RUN ON START
    start_range = ccm_params['start_range']
    x0_start = x0[start_range[0] * ccm_samples_per_year:start_range[1] * ccm_samples_per_year]
    x1_start = x1[start_range[0] * ccm_samples_per_year:start_range[1] * ccm_samples_per_year]
    save_timeseries(x0_start, x1_start, 'ts_start.json')
    
    mid_range = ccm_params['mid_range']
    x0_mid = x0[mid_range[0] * ccm_samples_per_year:mid_range[1] * ccm_samples_per_year]
    x1_mid = x1[mid_range[0] * ccm_samples_per_year:mid_range[1] * ccm_samples_per_year]
    save_timeseries(x0_mid, x1_mid, 'ts_mid.json')
    
    end_range = ccm_params['end_range']
    x0_end = x0[end_range[0] * ccm_samples_per_year:end_range[1] * ccm_samples_per_year]
    x1_end = x1[end_range[0] * ccm_samples_per_year:end_range[1] * ccm_samples_per_year]
    save_timeseries(x0_end, x1_end, 'ts_end.json')