def _check_max_patts_eval(dSysSettings):

    # =================================================================
    # Get the verbose settings
    (bInfo, _) = _verbose.get_settings(dSysSettings)

    # =================================================================

    # =================================================================
    # Check the number of patterns analyzed

    # -----------------------------------------------------------------
    # Get the needed data
    dStop = dSysSettings['dStop']           # Get the 'dStop' dictionary
                                            # with computations stop
                                            # criteria

    nMaxPatts = dStop['nMaxPatts']          # Get the maximum number of
                                            # patterns analyzed for one
                                            # type of patterns

    # - - - - - - - - - - - - - - - - - - - -
    dMemory = dSysSettings['dMemory']       # Get the the dictionary with
                                            # memory configuration

    nPattPack = dMemory['nPattPack']        # Get the size of a patterns
                                            # pack

    # - - - - - - - - - - - - - - - - - - - -

    inxPP = dSysSettings['inxPP']           # Get the index of patterns pack

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    #------------------------------------------------------------------

    # Compute the number of evaluated patterns
    nPattsEvaluated = (inxPP+1)*nPattPack

    # if the number of evaluated patterns is higher than allowed
    # - terminate the evaluations and counters on this type of patterns
    if nPattsEvaluated >= nMaxPatts:

        # Report to the console
        if bInfo == 1:
            strMessage1 = 'STOP !!!:  The maximum allowed number of patterns'
            strMessage2 = 'was reached!\n\n'
            print('%s %s') % (strMessage1, strMessage2)

        bStop = 1  # <- Stop the computations
    else:
        bStop = 0  # <- Do not stop the computations

    # =================================================================

    return (bStop)
def _check_time(dSysSettings, dSTOPData):

    # =================================================================
    # Get the verbose settings
    (bInfo, _) = _verbose.get_settings(dSysSettings)

    # =================================================================

    # =================================================================
    # Check the time

    # Get the dictionary with computations stop criteria
    dStop = dSysSettings['dStop']

    # Get the maximum time spend on one type of pattern specified by user
    tMax = dStop['tMax']

    # Get the moment of start
    tStart = dSTOPData['tStart']

    # Measure the time
    tTime = time.time() - tStart

    # If the time is too long - terminate the evaluations on this type of
    # patterns
    if tTime >= tMax:

        # Report to the console
        if bInfo == 1:

            # Print the stop info
            strMessage1 = 'STOP!!!: Maximum time allowed for computations'
            strMessage2 = 'exceed!\n\n'
            print('%s %s') % (strMessage1, strMessage2)

        # Set the stop flag
        bStop = 1
    else:

        # Clear the stop flag
        bStop = 0

    # =================================================================

    return (bStop)
def main(dSysSettings, dSTOPData, dEvalData):

    # =================================================================
    # Print an empty new line (if needed)

    # Get the verbose settings
    (bInfo, _) = _verbose.get_settings(dSysSettings)
    if bInfo == 1:
        print ' \n\n'
    # =================================================================

    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    # CRITICAL STOP CHECK START HERE
    #
    # - maximum time of computations
    # - maximum number of evaluated patterns
    #
    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    # =================================================================
    # Check the time of the current evaluation
    # (It can not be higher than the maximum time spend
    #  on one type of patterns specified by user)
    (bStop) = _check_time(dSysSettings, dSTOPData)
    if bStop == 1:
        return (bStop, dSysSettings, dSTOPData, dEvalData)
    # =================================================================

    # =================================================================
    # Check the number of evaluated patterns
    # (It can not be higher than the maximum number
    #  of patterns evaluated for one type of patterns
    #  specified by user)
    (bStop) = _check_max_patts_eval(dSysSettings)
    if bStop == 1:
        return (bStop, dSysSettings, dSTOPData, dEvalData)

    # =================================================================

    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    # CONVERGENCE CHECKS START HERE
    # (ALL THE CONVERGENCE CONDITIONS MUST BE FULFILLED TO STOP THE
    # (EVALUATION)
    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    # Reset the stop flag
    bStop = 1

    # Frequency stability convergence check (if needed)
    (bStop, dSysSettings, dSTOPData) = \
        _freq_eval.check_convergence(dSysSettings, dEvalData,
                                     dSTOPData, bStop)

    # Min allowed distance convergence check (if needed)
    (bStop, dSysSettings, dSTOPData) = \
        _min_distance_eval.check_convergence(dSysSettings, dEvalData,
                                             dSTOPData, bStop)

    # Max allowed distance convergence check (if needed)
    (bStop, dSysSettings, dSTOPData) = \
        _max_distance_eval.check_convergence(dSysSettings, dEvalData,
                                             dSTOPData, bStop)

    # Correct patterna counter convergence check (if needed)
    (bStop, dSysSettings, dSTOPData) = \
        _correct_counter.check_convergence(dSysSettings, dEvalData,
                                           dSTOPData, bStop)

    # PDF evaluation convergence (if needed)
    (bStop, dSysSettings, dSTOPData) = \
        _pdf_total_eval.check_convergence(dSysSettings, dEvalData,
                                          dSTOPData, bStop)

    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    # CONVERGENCE CHECKS ENDS HERE
    #
    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    # =================================================================
    # Check if the minimum number of patterns was already evaluated
    # (If the minimum number of patterns was not yet evaluated
    #  then do not stop the computations)
    (bStop) = _check_min_patts_eval(dSysSettings, bStop)

    # =================================================================

    # =================================================================
    # Print a delimiter (if needed)

    # Get the verbose settings
    (bInfo, _) = _verbose.get_settings(dSysSettings)
    if bInfo == 1:
        stdout.write('----------------------------------------')
        stdout.write('----------------------------------------')
        stdout.write('----------------------------------------')
        stdout.write('-----------------\n')
    # =================================================================

    return (bStop, dSysSettings, dSTOPData, dEvalData)
def _check_min_patts_eval(dSysSettings, bStop):

    # =================================================================
    # Get the verbose settings
    (bInfo, _) = _verbose.get_settings(dSysSettings)

    # =================================================================

    # =================================================================
    # Get the needed data

    dStop = dSysSettings['dStop']           # Get the dictionary with
                                            # computations stop criteria

    nMinPatts = dStop['nMinPatts']          # The minimum number of patterns

    # - - - - - - - - - - - - - - - - - - - -
    dMemory = dSysSettings['dMemory']       # Get the dictionary with memory
                                            # configuration

    nPattPack = dMemory['nPattPack']        # The size of a patterns pack

    # - - - - - - - - - - - - - - - - - - - -

    inxPP = dSysSettings['inxPP']           # The index of patterns pack

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # =================================================================

    # =================================================================
    # Check the stop condition

    # Reset the stop minimum number of patterns flag
    bStopMin = 1

    # Compute the number of evaluated patterns
    nPattsEvaluated = (inxPP+1)*nPattPack

    # Check if the minimum number of patterns was not reached yet
    if nMinPatts > nPattsEvaluated:

        # Clear the stop flags
        bStop = 0
        bStopMin = 0

    # =================================================================

    # =================================================================
    # Report

    # Report to the console, if minimum number of patterns was not
    # reached yet
    if bInfo == 1 and bStopMin == 0:

        #  Print out
        strMessage = '\nSTOP CHECK: Minimum no. of patterns not reached'
        print('%s (%.1f k < %.1f k)') % \
            (strMessage, nPattsEvaluated/1e3, nMinPatts/1e3)

    # =================================================================

    # =================================================================
    return (bStop)