import json
from pathlib import Path


def get_total_epochs(save_path, run, last_gen):
    """
        Compute the total number of performed epochs.

        Parameters
        ----------
        save_path: str
            path where the ojects needed to resume evolution are stored.

        run : int
            current evolutionary run

        last_gen : int
            count the number of performed epochs until the last_gen generation


        Returns
        -------
        total_epochs : int
            sum of the number of epochs performed by all trainings
    """

    total_epochs = 0
    for gen in range(0, last_gen+1):
        j = json.load(open(Path('%s/run_%d/gen_%d.csv' % (save_path, run, gen))))
        num_epochs = [elm['num_epochs'] for elm in j]
        total_epochs += sum(num_epochs)

    return total_epochs