Helper functions - dime-worldbank/Disease-Modelling-SSA GitHub Wiki

Code to create a new non-cumulative probability OD matrix from the cumulative probability ones

def convert_cumulative_od_matrix_to_probabilities(data_path, save_path, average_week):
    A function to convert the cumulative probability OD matrix to the probability of each someone in a given
    district visiting a given district
    :param data_path: Where you have the OD matrix saved
    :param save_path: Where you want to save the new matrix
    :param average_week: Boolean, if you want to keep the probabilities for each day or just average the week out
    data = pd.read_csv(data_path)
    data = data.loc[:, ~data.columns.str.contains('^Unnamed')]
    for idx in data.index:
        cumulative_values = list(data.iloc[idx][2:])
        displaced_cumulative_values = np.insert(np.delete(cumulative_values, -1), 0, 0)
        probabilities = cumulative_values - displaced_cumulative_values
        probabilities = probabilities / 100
        data.iloc[idx][2:] = probabilities
        for idx_2, val in enumerate(data.columns[2:]):
            data.loc[idx, val] = probabilities[idx_2]
    if average_week:
        data = data.groupby('home_region').mean()
        data = data.drop('weekday', axis=1)
            data = data.drop('weekday', axis=1)
        except KeyError:
            print("not found")