epidemioptim/analysis/plots.py
def extract_res(folder, algo, max_costs, min_costs):
    """
    This function go through all runs from an algorithm folder, gathers data about their Pareto front and compute the stair-case functions.
    Saves information into text file at the algorithm level.

    Parameters
    ----------
    folder: str
        Directory of the algorithm (full of directories, one for each run).
    algo: str
        Algorithm identifier

    """
    print('\n\tExtracting: ', folder.split('/')[-2])
    os.makedirs(folder + '/res', exist_ok=True)

    # First, we go through all runs and collect mean and std of the solutions in the Pareto front.
    centrals = []
    errors = []
    goals = []
    list_folds = sorted(os.listdir(folder))

    for fold in list_folds:
        if 'res' not in fold:
            try:
                with open(folder + fold + '/res_eval2.pk', 'rb') as f:
                    res = pickle.load(f)
            except:
                with open(folder + fold + '/res_eval.pk', 'rb') as f:
                    res = pickle.load(f)
            os.makedirs(folder + fold + '/plots', exist_ok=True)
            mean = res['F']  # mean of the points in Pareto front
            std = res['F_std']  # std of the points in Pareto front (over n evaluation episodes)

            # Swap costs depending on the desired x-axis.
            if SWITCH:
                mean[:, 0], mean[:, 1] = mean[:, 1].copy(), mean[:, 0].copy()
                std[:, 0], std[:, 1] = std[:, 1].copy(), std[:, 0].copy()

            artists = pareto_plot(mean, std)
            save_fig(folder + fold + '/plots/pareto_{}.pdf'.format(SWITCH), artists)

            if algo == 'DQN':
                goals.append(res['G_all'].flatten())  # collect the goal for DQN policies, so as to pull them into populations of policies
                mean = mean.flatten()
                std = std.flatten()

            centrals.append(mean)
            errors.append(std)

    # if DQN algorithm, we need to pull several policies to form a population (one for each value of the goal).
    if algo == 'DQN':
        unique_goals = np.unique(goals)
        nb_runs = np.argwhere(np.array(goals).flatten() == unique_goals[0]).size
        all_centrals = []
        all_errors = []
        centrals = np.array(centrals)
        errors = np.array(errors)
        for i_run in range(nb_runs):
            inds = np.array([np.argwhere(np.array(goals).flatten() == unique_goals[i])[i_run] for i in range(unique_goals.size)]).flatten()
            all_centrals.append(centrals[inds])
            all_errors.append(errors[inds])
        centrals = all_centrals.copy()
        errors = all_errors.copy()

    # compute areas under the curve
    areas = [compute_area_under_curve(mean, max_costs, min_costs) for mean in centrals]
    print(areas)

    # Here we form the staircase function from points in the Pareto front.
    n_lines = len(centrals)
    all_data = []
    all_data_std = []
    for i in range(n_lines):
        data = centrals[i]
        data_std = errors[i]
        sorted_inds = np.argsort(data[:, 0])
        data = data[sorted_inds]
        data_std = data_std[sorted_inds]

        # align data to compute mean
        inds = np.arange(0, XLIM[1], X_STEP)
        aligned_data = np.zeros([inds.size])
        aligned_std = np.zeros([inds.size])
        aligned_data.fill(np.nan)
        aligned_std.fill(np.nan)
        for i, cost in enumerate(inds):
            inds_inf = np.argwhere(data[:, 0] < cost).flatten()
            if inds_inf.size > 0:
                last_inf = inds_inf[-1]
                aligned_data[i] = data[last_inf, 1]
                aligned_std[i] = data_std[last_inf, 1]
        all_data.append(aligned_data)
        all_data_std.append(aligned_std)
    np.savetxt(folder + 'res/all_data_pareto_{}.txt'.format(SWITCH), np.array(all_data))
    np.savetxt(folder + 'res/all_data_pareto_std_{}.txt'.format(SWITCH), np.array(all_data_std))
    np.savetxt(folder + 'res/areas_under_curve.txt'.format(SWITCH), np.array(areas))

    This function go through all runs from an algorithm folder, gathers data about their Pareto front and compute the stair-case functions.
    Saves information into text file at the algorithm level.

    Parameters
    ----------
    folder: str
        Directory of the algorithm (full of directories, one for each run).
    algo: str
        Algorithm identifier

    
This function go through all runs from an algorithm folder, gathers data about their Pareto front and compute the stair-case functions.
Saves information into text file at the algorithm level.

Parameters

str
Directory of the algorithm (full of directories, one for each run).
algo: str
Algorithm identifier
This function go through all runs from an algorithm folder gathers data about their Pareto front and compute the stair - case functions . Saves information into text file at the algorithm level . Parameters str Directory of the algorithm ( full of directories one for each run ) . algo : str Algorithm identifier
def extract_res(folder, algo, max_costs, min_costs):
    print('\n\tExtracting: ', folder.split('/')[-2])
    os.makedirs(folder + '/res', exist_ok=True)
    centrals = []
    errors = []
    goals = []
    list_folds = sorted(os.listdir(folder))
    for fold in list_folds:
        if 'res' not in fold:
            try:
                with open(folder + fold + '/res_eval2.pk', 'rb') as f:
                    res = pickle.load(f)
            except:
                with open(folder + fold + '/res_eval.pk', 'rb') as f:
                    res = pickle.load(f)
            os.makedirs(folder + fold + '/plots', exist_ok=True)
            mean = res['F']  
            std = res['F_std']  
            if SWITCH:
                mean[:, 0], mean[:, 1] = mean[:, 1].copy(), mean[:, 0].copy()
                std[:, 0], std[:, 1] = std[:, 1].copy(), std[:, 0].copy()
            artists = pareto_plot(mean, std)
            save_fig(folder + fold + '/plots/pareto_{}.pdf'.format(SWITCH), artists)
            if algo == 'DQN':
                goals.append(res['G_all'].flatten())  
                mean = mean.flatten()
                std = std.flatten()
            centrals.append(mean)
            errors.append(std)
    if algo == 'DQN':
        unique_goals = np.unique(goals)
        nb_runs = np.argwhere(np.array(goals).flatten() == unique_goals[0]).size
        all_centrals = []
        all_errors = []
        centrals = np.array(centrals)
        errors = np.array(errors)
        for i_run in range(nb_runs):
            inds = np.array([np.argwhere(np.array(goals).flatten() == unique_goals[i])[i_run] for i in range(unique_goals.size)]).flatten()
            all_centrals.append(centrals[inds])
            all_errors.append(errors[inds])
        centrals = all_centrals.copy()
        errors = all_errors.copy()
    areas = [compute_area_under_curve(mean, max_costs, min_costs) for mean in centrals]
    print(areas)
    n_lines = len(centrals)
    all_data = []
    all_data_std = []
    for i in range(n_lines):
        data = centrals[i]
        data_std = errors[i]
        sorted_inds = np.argsort(data[:, 0])
        data = data[sorted_inds]
        data_std = data_std[sorted_inds]
        inds = np.arange(0, XLIM[1], X_STEP)
        aligned_data = np.zeros([inds.size])
        aligned_std = np.zeros([inds.size])
        aligned_data.fill(np.nan)
        aligned_std.fill(np.nan)
        for i, cost in enumerate(inds):
            inds_inf = np.argwhere(data[:, 0] < cost).flatten()
            if inds_inf.size > 0:
                last_inf = inds_inf[-1]
                aligned_data[i] = data[last_inf, 1]
                aligned_std[i] = data_std[last_inf, 1]
        all_data.append(aligned_data)
        all_data_std.append(aligned_std)
    np.savetxt(folder + 'res/all_data_pareto_{}.txt'.format(SWITCH), np.array(all_data))
    np.savetxt(folder + 'res/all_data_pareto_std_{}.txt'.format(SWITCH), np.array(all_data_std))
    np.savetxt(folder + 'res/areas_under_curve.txt'.format(SWITCH), np.array(areas))
def extract_res ( folder , algo , max_costs , min_costs ) : print ( '\n\tExtracting: ' , folder . split ( '/' ) [ - 2 ] ) os . makedirs ( folder + '/res' , exist_ok = True ) centrals = [ ] errors = [ ] goals = [ ] list_folds = sorted ( os . listdir ( folder ) ) for fold in list_folds : if 'res' not in fold : try : with open ( folder + fold + '/res_eval2.pk' , 'rb' ) as f : res = pickle . load ( f ) except : with open ( folder + fold + '/res_eval.pk' , 'rb' ) as f : res = pickle . load ( f ) os . makedirs ( folder + fold + '/plots' , exist_ok = True ) mean = res [ 'F' ] std = res [ 'F_std' ] if SWITCH : mean [ : , 0 ] , mean [ : , 1 ] = mean [ : , 1 ] . copy ( ) , mean [ : , 0 ] . copy ( ) std [ : , 0 ] , std [ : , 1 ] = std [ : , 1 ] . copy ( ) , std [ : , 0 ] . copy ( ) artists = pareto_plot ( mean , std ) save_fig ( folder + fold + '/plots/pareto_{}.pdf' . format ( SWITCH ) , artists ) if algo == 'DQN' : goals . append ( res [ 'G_all' ] . flatten ( ) ) mean = mean . flatten ( ) std = std . flatten ( ) centrals . append ( mean ) errors . append ( std ) if algo == 'DQN' : unique_goals = np . unique ( goals ) nb_runs = np . argwhere ( np . array ( goals ) . flatten ( ) == unique_goals [ 0 ] ) . size all_centrals = [ ] all_errors = [ ] centrals = np . array ( centrals ) errors = np . array ( errors ) for i_run in range ( nb_runs ) : inds = np . array ( [ np . argwhere ( np . array ( goals ) . flatten ( ) == unique_goals [ i ] ) [ i_run ] for i in range ( unique_goals . size ) ] ) . flatten ( ) all_centrals . append ( centrals [ inds ] ) all_errors . append ( errors [ inds ] ) centrals = all_centrals . copy ( ) errors = all_errors . copy ( ) areas = [ compute_area_under_curve ( mean , max_costs , min_costs ) for mean in centrals ] print ( areas ) n_lines = len ( centrals ) all_data = [ ] all_data_std = [ ] for i in range ( n_lines ) : data = centrals [ i ] data_std = errors [ i ] sorted_inds = np . argsort ( data [ : , 0 ] ) data = data [ sorted_inds ] data_std = data_std [ sorted_inds ] inds = np . arange ( 0 , XLIM [ 1 ] , X_STEP ) aligned_data = np . zeros ( [ inds . size ] ) aligned_std = np . zeros ( [ inds . size ] ) aligned_data . fill ( np . nan ) aligned_std . fill ( np . nan ) for i , cost in enumerate ( inds ) : inds_inf = np . argwhere ( data [ : , 0 ] < cost ) . flatten ( ) if inds_inf . size > 0 : last_inf = inds_inf [ - 1 ] aligned_data [ i ] = data [ last_inf , 1 ] aligned_std [ i ] = data_std [ last_inf , 1 ] all_data . append ( aligned_data ) all_data_std . append ( aligned_std ) np . savetxt ( folder + 'res/all_data_pareto_{}.txt' . format ( SWITCH ) , np . array ( all_data ) ) np . savetxt ( folder + 'res/all_data_pareto_std_{}.txt' . format ( SWITCH ) , np . array ( all_data_std ) ) np . savetxt ( folder + 'res/areas_under_curve.txt' . format ( SWITCH ) , np . array ( areas ) )
This function go through all runs from an algorithm folder, gathers data about their Pareto front and compute the stair-case functions.
This function go through all runs from an algorithm folder gathers data about their Pareto front and compute the stair - case functions .
"""
    This function go through all runs from an algorithm folder, gathers data about their Pareto front and compute the stair-case functions.
    Saves information into text file at the algorithm level.

    Parameters
    ----------
    folder: str
        Directory of the algorithm (full of directories, one for each run).
    algo: str
        Algorithm identifier

    """ # First, we go through all runs and collect mean and std of the solutions in the Pareto front. # mean of the points in Pareto front # std of the points in Pareto front (over n evaluation episodes) # Swap costs depending on the desired x-axis. # collect the goal for DQN policies, so as to pull them into populations of policies # if DQN algorithm, we need to pull several policies to form a population (one for each value of the goal). # compute areas under the curve # Here we form the staircase function from points in the Pareto front. # align data to compute mean
folder
algo
max_costs
min_costs