<commit_before>cimport revolve_c

from enum import Enum
import warnings

class RevolveError(Exception):
    pass
    # TODO: the hardcoded limits really should be removed in a future version. This should be as easy as replacing the arrays in the C++ code with an std::vector.
    errCodes = {
        10: "Number of checkpoints stored exceeds hardcoded limit. Modify 'checkup' and recompile pyrevolve.",
        11: "Number of checkpoints stored exceeds snaps. This is a bug in pyrevolve, please report it.",
        12: "Internal error in numforw, please report a pyrevolve bug.",
        13: "Enhancement of 'fine', increase 'snaps'. Please rerun and allow for more checkpoints to be stored.",
        14: "Number of snaps exceeds hardcoded limit. Modify 'snapsup' and recompile pyrevolve.",
        15: "Number of reps exceeds hardcoded limit. Modify 'repsup' and recompile pyrevolve."
    }

class Action(Enum):
    advance   = 1
    takeshot  = 2
    restore   = 3
    firstrun  = 4
    youturn   = 5
    terminate = 6
    error     = 7

def adjust(timesteps):
    cdef int c_st = timesteps
    return revolve_c.revolve_adjust(c_st)

def maxrange(snapshots, timefactor):
    cdef int c_ss = snapshots
    cdef int c_tt = timefactor
    return revolve_c.revolve_maxrange(c_ss, c_tt)

def numforw(timesteps, snapshots):
    cdef int c_st = timesteps
    cdef int c_sn = snapshots
    return revolve_c.revolve_numforw(c_st, c_sn)

def expense(timesteps, snapshots):
    cdef int c_st = timesteps
    cdef int c_sn = snapshots
    return revolve_c.revolve_expense(c_st, c_sn)

cdef class CRevolve(object):
    cdef revolve_c.CRevolve __r

    def __init__(self, snapshots, timesteps=None, snapshots_disk=None):
        cdef int c_sn
        cdef int c_st
        cdef int c_sr
        
        # if no number of steps is given, we need an online strategy
        if(timesteps == None):
            if(snapshots_disk != None):
                warnings.warn("Multi-stage online checkpointing is not implemented.")
                warnings.warn("Using single-stage online checkpointing instead.")
            c_sn = snapshots
            self.__r = revolve_c.revolve_create_online(c_sn)
        # if number of steps is given and no multi-stage strategy is requested,
        # use standard offline Revolve
        elif(snapshots_disk == None):
            c_sn = snapshots
            c_st = timesteps
            self.__r = revolve_c.revolve_create_offline(c_st, c_sn)
        # number of steps is known and multi-stage is requested,
        # use offline multistage strategy
        else:
            c_sn = snapshots
            c_st = timesteps
            c_sr = snapshots_disk
            self.__r = revolve_c.revolve_create_multistage(c_st, c_sr, c_sn)

    @property
    def info(self):
        return revolve_c.revolve_getinfo(self.__r)

    def revolve(self):
        cdef revolve_c.CACTION action
        action = revolve_c.revolve(self.__r)
        if(action == revolve_c.CACTION_ADVANCE):
            retAction = Action.advance
        elif(action == revolve_c.CACTION_TAKESHOT):
            retAction = Action.takeshot
        elif(action == revolve_c.CACTION_RESTORE):
            retAction = Action.restore
        elif(action == revolve_c.CACTION_FIRSTRUN):
            retAction = Action.firstrun
        elif(action == revolve_c.CACTION_YOUTURN):
            retAction = Action.youturn
        elif(action == revolve_c.CACTION_TERMINATE):
            retAction = Action.terminate
        else:
            # in this case, action must be "error"
            retAction = Action.error
            raise(RevolveError(RevolveError.errCodes[self.info]))
        return retAction

    @property
    def advances(self):
        return revolve_c.revolve_getadvances(self.__r)<commit_msg>Fix issue 4: wrong argument order in multistage<commit_after>cimport revolve_c

from enum import Enum
import warnings

class RevolveError(Exception):
    pass
    # TODO: the hardcoded limits really should be removed in a future version. This should be as easy as replacing the arrays in the C++ code with an std::vector.
    errCodes = {
        10: "Number of checkpoints stored exceeds hardcoded limit. Modify 'checkup' and recompile pyrevolve.",
        11: "Number of checkpoints stored exceeds snaps. This is a bug in pyrevolve, please report it.",
        12: "Internal error in numforw, please report a pyrevolve bug.",
        13: "Enhancement of 'fine', increase 'snaps'. Please rerun and allow for more checkpoints to be stored.",
        14: "Number of snaps exceeds hardcoded limit. Modify 'snapsup' and recompile pyrevolve.",
        15: "Number of reps exceeds hardcoded limit. Modify 'repsup' and recompile pyrevolve."
    }

class Action(Enum):
    advance   = 1
    takeshot  = 2
    restore   = 3
    firstrun  = 4
    youturn   = 5
    terminate = 6
    error     = 7

def adjust(timesteps):
    cdef int c_st = timesteps
    return revolve_c.revolve_adjust(c_st)

def maxrange(snapshots, timefactor):
    cdef int c_ss = snapshots
    cdef int c_tt = timefactor
    return revolve_c.revolve_maxrange(c_ss, c_tt)

def numforw(timesteps, snapshots):
    cdef int c_st = timesteps
    cdef int c_sn = snapshots
    return revolve_c.revolve_numforw(c_st, c_sn)

def expense(timesteps, snapshots):
    cdef int c_st = timesteps
    cdef int c_sn = snapshots
    return revolve_c.revolve_expense(c_st, c_sn)

cdef class CRevolve(object):
    cdef revolve_c.CRevolve __r

    def __init__(self, snapshots, timesteps=None, snapshots_disk=None):
        cdef int c_sn
        cdef int c_st
        cdef int c_sr
        
        # if no number of steps is given, we need an online strategy
        if(timesteps == None):
            if(snapshots_disk != None):
                warnings.warn("Multi-stage online checkpointing is not implemented.")
                warnings.warn("Using single-stage online checkpointing instead.")
            c_sn = snapshots
            self.__r = revolve_c.revolve_create_online(c_sn)
        # if number of steps is given and no multi-stage strategy is requested,
        # use standard offline Revolve
        elif(snapshots_disk == None):
            c_sn = snapshots
            c_st = timesteps
            self.__r = revolve_c.revolve_create_offline(c_st, c_sn)
        # number of steps is known and multi-stage is requested,
        # use offline multistage strategy
        else:
            c_sn = snapshots
            c_st = timesteps
            c_sr = snapshots_disk
            self.__r = revolve_c.revolve_create_multistage(c_st, c_sn, c_sr)

    @property
    def info(self):
        return revolve_c.revolve_getinfo(self.__r)

    def revolve(self):
        cdef revolve_c.CACTION action
        action = revolve_c.revolve(self.__r)
        if(action == revolve_c.CACTION_ADVANCE):
            retAction = Action.advance
        elif(action == revolve_c.CACTION_TAKESHOT):
            retAction = Action.takeshot
        elif(action == revolve_c.CACTION_RESTORE):
            retAction = Action.restore
        elif(action == revolve_c.CACTION_FIRSTRUN):
            retAction = Action.firstrun
        elif(action == revolve_c.CACTION_YOUTURN):
            retAction = Action.youturn
        elif(action == revolve_c.CACTION_TERMINATE):
            retAction = Action.terminate
        else:
            # in this case, action must be "error"
            retAction = Action.error
            raise(RevolveError(RevolveError.errCodes[self.info]))
        return retAction

    @property
    def advances(self):
        return revolve_c.revolve_getadvances(self.__r)
{'fraction_non_alphanumeric': 0.07227177334732424, 'fraction_numerical': 0.005115424973767052, 'mean_word_length': 2.5073597056117753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6575502', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2327, 'n_words': 684}