Пример #1
0
def compute_acquisition_rates_time_invariant_part(n_types, states, k_matrix):
    """
    Compute the time invariant part of the acquisition rate
    :param n_types: number of types
    :param states: a list of infection states
    :param k_matrix: np.array of the pairwise interaction parameters in acquisition
    :return: output: list of length n_types. Each item in the list is a n_state**2 np.array.
       [j,k]-element of i-th matrix: acquisition interaction parameter from state j to state k.
    """
    n_states = len(states)
    output = []
    for t in range(1, n_types + 1):
        output_i = np.zeros((n_states, n_states))
        for end_state in states:
            if t not in end_state.types:
                continue
            start_state = getState([x for x in end_state.types if x != t],
                                   states)
            d = 1
            if len(start_state.types) > 0:
                d = np.prod([
                    k_matrix[start_t - 1, t - 1]
                    for start_t in start_state.types
                ])
            output_i[start_state.state_id, end_state.state_id] = d
        output.append(output_i)
    return output
Пример #2
0
def compute_clearance_rates_group(n_types, states, h_matrix, mu, partition):
    """
    Compute the clearance rate
    :param n_types: number of types
    :param states: a list of state objects
    :param h_matrix: np.array of the pairwise interaction parameters in clearance
    :param mu: the baseline type-specific clearance rate
    :param partition: partition of groups (clusters) of types
    :return: output: a n_state**2 np.array.
    The [j,k]-element the clearance rate from state j to state k including the interaction parameters.
    """
    num_states = len(states)
    output = np.zeros((num_states, num_states))
    for t in range(1, n_types + 1):
        type_cluster = np.where([t in part for part in partition])[0][0] + 1
        for startState in states:
            if t not in startState.types:
                continue
            end_state = getState([x for x in startState.types if x != t],
                                 states)
            d = mu[t - 1]
            if len(end_state.types) > 0:
                d *= np.prod([
                    h_matrix[end_clus - 1, type_cluster - 1]
                    for end_clus in end_state.clusters
                ])
            output[startState.state_id, end_state.state_id] = d
    return output
Пример #3
0
def compute_clearance_rates(n_types, states, h_matrix, mu):
    """
    Compute the clearance rate
    :param n_types: number of types
    :param states: a list of state objects
    :param h_matrix: np.array of the pairwise interaction parameters in clearance
    :param mu: the baseline type-specific clearance rate
    :return: output: a n_state**2 np.array.
        The [j,k]-element the clearance rate from state j to state k including the interaction parameters.
    """
    num_states = len(states)
    output = np.zeros((num_states, num_states))
    for t in range(1, n_types + 1):
        for start_state in states:
            if t not in start_state.types:
                continue
            end_state = getState([x for x in start_state.types if x != t],
                                 states)
            d = mu[t - 1]
            if len(end_state.types) > 0:
                d = np.prod(
                    [h_matrix[end_t - 1, t - 1]
                     for end_t in end_state.types]) * mu[t - 1]
            output[start_state.state_id, end_state.state_id] = d
    return output
Пример #4
0
def compute_acquisition_rates_time_invariant_part_group(
        n_types, states, k_matrix, partition):
    """
    Compute the time invariant part of the acquisition rate of the all or nothing model
    :param n_types: number of types
    :param states: a list of state objects
    :param k_matrix: np.array of the pairwise interaction parameters in acquisition between clusters
    :param partition: list of lists of types belonging to the same cluster
    :return: output: list of length n_types. Each item in the list is a n_state**2 np.array.
       [j,k]-element of i-th matrix: acquisition interaction parameter from state j to state k.
    """
    n_states = len(states)
    output = []
    for t in range(1, n_types + 1):
        type_cluster = np.where([t in part for part in partition])[0][0] + 1

        output_i = np.zeros((n_states, n_states))
        for end_state in states:
            if t not in end_state.types:
                continue
            # if type is in endState
            state_state = getState([x for x in end_state.types if x != t],
                                   states)
            d = 1
            if len(state_state.types) > 0:
                d = np.prod([
                    k_matrix[startCluster - 1, type_cluster - 1]
                    for startCluster in state_state.clusters
                ])
            output_i[state_state.state_id, end_state.state_id] = d
        output.append(output_i)
    return output
def compute_hazard_based_predictor(steady_state, trans_matrix, states, vt, nvt_):
    """
    computing type-specific hazard-based predictors
    :param steady_state: a list of prevalence of infection states
    :param trans_matrix: transition matrix (hazards)
    :param states: a list of state objects
    :param vt: a list of vaccine types
    :param nvt_: a integer giving the non-vaccine type at interest
    :return: type-specific hazard-based- predictor for type replacement by the non-vaccine type at interest
    """
    entries = np.zeros((2, 4))

    for state in states:
        if len(set(state.types) & set(vt)) > 0 and len(set(state.types) & set([nvt_])) == 0:
            state_nvt = getState(state.types + [nvt_], states)
            # hazards of acquiring the non-vaccine type at interest from states:
            #   - containing at least a vaccine type
            #   - not containing the non-vaccine type at interest
            entries[0, 0] += steady_state[state.state_id] * trans_matrix[state.state_id, state_nvt.state_id]
            entries[1, 0] += steady_state[state.state_id]
            # hazards of clearing the non-vaccine type at interest from states:
            #   - containing at least a vaccine type
            #   - containing the non-vaccine type at interest
            entries[0, 1] += steady_state[state_nvt.state_id] * trans_matrix[state_nvt.state_id, state.state_id]
            entries[1, 1] += steady_state[state_nvt.state_id]
        elif len(set(state.types) & set(vt)) == 0 and len(set(state.types) & set([nvt_])) == 0:
            state_nvt = getState(state.types + [nvt_], states)
            # hazards of acquiring the non-vaccine type at interest from states:
            #   - not containing any vaccine type
            #   - not containing the non-vaccine type at interest
            entries[0, 2] += steady_state[state.state_id] * trans_matrix[state.state_id, state_nvt.state_id]
            entries[1, 2] += steady_state[state.state_id]
            # hazards of clearing the non-vaccine type at interest from states:
            #   - not containing any vaccine type
            #   - containing the non-vaccine type at interest
            entries[0, 3] += steady_state[state_nvt.state_id] * trans_matrix[state_nvt.state_id, state.state_id]
            entries[1, 3] += steady_state[state_nvt.state_id]

    # compute all weighted averages
    combined_entries = entries[0]/entries[1]
    predictor = (combined_entries[0]/combined_entries[2]) / (combined_entries[1]/combined_entries[3])
    return predictor
Пример #6
0
def simulate_post_vaccination(n, n_vt, m):
    # """
    # :param n: number of types
    # :param n_vt: number of vaccine types
    # :param m: interaction structure
    # """

    # param parameter_sets: np.array of parameter sets (transmissibilities and interaction parameters)
    # part_sets: np.array of partition (clusters/groups of types) sets
    # sign_sets: np.array of sign parameters
    # num_simulations: number parameter sets to simulate from
    # states: list of infection states
    # mu: the baseline per capita type-specific clearance rate
    # marginal operator: matrix for left-multiplying with the state variable to get the marginal prevalence
    # steady_states: np.array for saving the simulated steady state prevalence per infection state
    parameter_sets = read_parameters(n)
    num_simulations = parameter_sets.shape[0]
    part_sets = np.zeros((num_simulations, n))
    sign_sets = np.zeros((num_simulations, 2 * n**2))
    if m[1] == 'groupwise':
        part_sets = read_partitions(n)
    if m[3] != 0:
        sign_sets = read_sign(n)

    n_nvt = n - n_vt
    full_states = computeStates(n)
    reduced_states = computeStates(n_nvt)
    n_full_states = len(full_states)
    n_reduced_states = len(reduced_states)

    # matrix to translate a state in the reduced system to the full system
    state_translater = np.zeros((n_full_states, n_reduced_states))
    for i in range(n_reduced_states):
        reduced_state = reduced_states[i]
        state_id = getState(list(np.array(reduced_state.types) + n_vt),
                            full_states).state_id
        state_translater[state_id, i] = 1

    mu = np.ones(n - n_vt)
    marginal_operator = np.array(
        [[1 if (t + 1) in s.types else 0 for s in reduced_states]
         for t in range(n - n_vt)])
    steady_states = np.ones((num_simulations, 2**n))
    # Simulate pre-vaccination steady states for each parameter set
    previous_run_times = []
    for j in range(num_simulations):
        start_time = time()

        # Get the type-specific transmissibilities
        beta = parameter_sets[j, n_vt:n]

        # Derive the required parameters
        k_matrix, h_matrix, states, partition = derive_param(
            parameter_sets[j], sign_sets[j], part_sets[j], n, n_vt, m)

        # Simulate the equilibrium corresponding to the j-th parameter set
        ss = get_steady_state(n - n_vt, states, k_matrix, h_matrix, beta, mu,
                              marginal_operator, False, m[1], partition)
        steady_states[j, :] = state_translater.dot(ss)

        end_time = time()

        # Save the time used to simulate the first 100 parameter sets and estimate the remaining simulation time
        if j < 100:
            # save the time used to simulate the j-th parameter set
            previous_run_times.append(end_time - start_time)
        if j % int(num_simulations / 10) == 0:
            # print each time 10% of all parameter sets are simulated
            print(j, end_time - start_time)
        if j == 100:
            # print the estimated time until all parameter sets are simulated
            print([int(x)
                   for x in strftime("%Y,%m,%d,%H,%M,%S").split(',')][3:5])
            print(
                sum(previous_run_times) / 100 * num_simulations / 3600,
                ' hours to go.')

    # write the simulated steady states in a csv file
    filename = '_'.join(m) + '_{}types_{}vts_VE1_prevalence.csv'.format(
        n, n_vt)
    df = pd.DataFrame(steady_states)
    df.to_csv(filename)