Ejemplo n.º 1
0
def save_params(cursor, expID, p_init, param_IDs, pre_ID, post_ID, delta_param):
    """Records the initial parameters for a simulation."""
    stmt = ("INSERT INTO param_value "
            "(experiment, parameter, at_time, value, of_interest) "
            "VALUES (%s, %s, %s, %s, %s)")

    params = par_list()

    if len(p_init) != len(params):
        err_msg = "ERROR: zipping param lists of length %d and %d"
        print >> sys.stderr, (err_msg % (len(p_init), len(params)))

    p_list = zip(p_init, params)
    data_fn = lambda (v,n): (expID, param_IDs[n], 0, v, par_of_interest(n))
    data = map(data_fn, p_list)

    d_name = del_list()[delta_param]
    d_id = param_IDs[d_name]

    p_stmt = ("INSERT INTO indiv_param (individual, parameter, value) "
              "VALUES (%s, %s, %s)")
    for row in data:
        (_, param_id, _, value, _) = row
        do_stmt(cursor, stmt, row)
        do_stmt(cursor, p_stmt, (pre_ID, param_id, value))
        if param_id != d_id:
            do_stmt(cursor, p_stmt, (post_ID, param_id, value))
Ejemplo n.º 2
0
def save_delta(cursor, expID, indiv_ID, delta_param, delta_incr, at_time,
               param_IDs):
    """Records the delta perturbation for a simulation."""
    pname = del_list()[delta_param]
    paramID = param_IDs[pname]
    # the delta perturbation is applied after four weeks
    # at_time = 60 * 24 * 7 * 4

    stmt = ("SELECT value FROM param_value "
            "WHERE experiment = %s AND parameter = %s AND at_time = %s ")
    data = (expID, paramID, 0)

    matches = do_stmt(cursor, stmt, data, retval=True)
    if len(matches) != 1:
        print >> sys.stderr, "ERROR: " + str(len(matches)) + " initial values"
        return
    else:
        new_val = delta_incr + matches[0][0]

    p_stmt = ("INSERT INTO indiv_param (individual, parameter, value) "
              "VALUES (%s, %s, %s)")
    do_stmt(cursor, p_stmt, (indiv_ID, paramID, new_val))

    stmt = ("INSERT INTO param_value "
            "(experiment, parameter, at_time, value, of_interest) "
            "VALUES (%s, %s, %s, %s, %s) RETURNING id")
    of_interest = par_of_interest(pname)
    data = (expID, paramID, at_time, new_val, of_interest)

    return do_stmt(cursor, stmt, data, retval=True)[0][0]