Exemplo n.º 1
0
@author: musselle
"""

import scipy.io as sio
from utils import analysis
from PedrosFrahst import frahst_pedro
from Frahst_v3 import FRAHST_V3
from artSigs import genCosSignals_no_rand , genCosSignals

#AbileneMat = sio.loadmat('C:\DataSets\Abilene\Abilene.mat')
#data = AbileneMat['F']

data = genCosSignals_no_rand()

data = genCosSignals(0,-3.0)

e_high = 0.98
e_low = 0.96
alpha = 0.96


holdTime = 0
# My version  
res_me = FRAHST_V3(data, alpha=0.96, e_low=0.96, e_high=0.98, sci = -1, \
holdOffTime = holdTime, r = 1, evalMetrics = 'T') 
#metric_me_5, sets_me, anom_det_tab_me = analysis(res_me, AbileneMat['F_g_truth_tab'])

# Pedros Version
res_ped = frahst_pedro(data, r=1, alpha=0.96, energy_low=0.96, energy_high=0.98,  \
holdOffTime = holdTime, evaluateTruth='T')
#metric_ped_5, sets_ped, anom_det_tab_ped = analysis(res_ped, AbileneMat['F_g_truth_tab'])
Exemplo n.º 2
0
def singleShiftRerun(parameter_string): 

    params = parameter_string.split(', ')
    
    
    # Data Sets 
    baseLine = 0.0
    
    num_streams = [int(params[0].split()[-1])]            # n
    SNRs = [int(params[1].split()[-1])]                  # snr
    anomaly_lengths = [int(params[2].split()[-1])]       # l
    anomaly_magnitudes = [int(params[3].split()[-1])]    # m
    
    initial_conditions = 20      # i
    
    # Algorithm Parameters
    thresholds = params[4].split()[-1].split(',')
    
    e_highs = [float(thresholds[1][0:-1])]             # eh
    e_lows = [float(thresholds[0][1:])]              # el 
    alphas = [float(params[5].split()[-1])]              # a
    holdOffs = [int(params[6].split()[-1])]             # h
        
    # Algorithm flags    
    run_spirit = 0
    run_frahst = 1
    run_frahst_pedro = 0
    
    #===============================================================================
    # Initialise Data sets
    #========-=======================================================================
    
    # For Profiling 
    start = time.time() 
    
    for n in num_streams:
        for snr in SNRs:
            for l in anomaly_lengths:
                for m in anomaly_magnitudes:
                    A = 0
                    for i in range(initial_conditions):    
                        
                        # Seed random number generator 
                        np.random.seed(i)                    
                        
                        # Two ts that have anomalous shift 
                        s0 = Tseries(0)
                        s1 = Tseries(0)
                        s0.makeSeries([1,3,1],[100, l, 200 - l],[baseLine, baseLine, baseLine + m], 
                                      gradient = float(m)/float(l), noise_type ='none')
                        s1.makeSeries([1,4,1],[200, l, 100 - l],[baseLine, baseLine, baseLine - m], 
                                      gradient = float(m)/float(l), noise_type ='none')
                        # The rest of the ts
                        for k in range(2, n) :
                            name = 's'+ str(k)
                            vars()[name] = Tseries(0)
                            vars()[name].makeSeries([1], [300], [baseLine], noise_type ='none')
                        
                        # Concat into one matrix 
                        S = scipy.c_[s0]
                        for k in range(1, n) :
                            S = scipy.c_[ S, vars()['s'+ str(k)] ]
    
                        # Concatonate to 3d array, timesteps x streams x initial condition 
                        if type(A) == int:
                            A = S 
                        else:
                            A = np.dstack((A, S))                        
                            
                    # Calculate the noise                         
                    Ps = np.sum(A[:,:,0] ** 2)
                    Pn = Ps / (10. ** (snr/10.))
                    scale = Pn / (n * 300.)
                    noise = np.random.randn(A.shape[0], A.shape[1], A.shape[2]) * np.sqrt(scale)
                    A = A + noise
                                              
    
    #===============================================================================
    # Ground Truths 
    #===============================================================================
    #                                 # time step | length 
                    ground_truths = np.array([[100, l],
                                              [200, l]])
                                        
    #==============================================================================
    #  Run Algorithm 
    #==============================================================================   
                    alg_count = 1    
        
                    for eh in e_highs :             
                        for el in e_lows :              
                            for a in alphas :              
                                for h in holdOffs :            
                                    
                                    print 'Running Algorithm(s) with:\nE_Thresh = (' + str(el) + ',' + str(eh) + ')\n' + \
                                    'alpha = ' + str(a) + '\nHoldOff = ' + str(h)  
                                    
                                    SPIRIT_metricList = []                                
                                    FRAHST_metricList = []                                
                                    PEDRO_FRAHST_metricList = []
                                    
                                    for i in range(initial_conditions):
                                        
                                        # Load Data 
                                        streams = A[:,:,i]
                                        
                                        if run_spirit == 1:
                                            # SPIRIT
                                            res_sp = SPIRIT(streams, a, [el, eh], evalMetrics = 'F', 
                                                            reorthog = False, holdOffTime = h) 
                                            res_sp['Alg'] = 'SPIRIT: alpha = ' + str(a) + \
                                            ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
         
                                            pltSummary2(res_sp, streams, (el, eh))                            
                                
                                
                                            SPIRIT_metricList.append(analysis(res_sp, ground_truths, 300 ))
                                                                                
    #                                        data = res_sp
    #                                        Title = 'SPIRIT alpha = ' + str(a) + ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    #                            
        #                                    plot_4x1(streams, data['hidden'], data['e_ratio'], data['orthog_error'], 
        #                                             ['Input Data','Hidden\nVariables',
        #                                    'Energy Ratio', 'Orthogonality\nError (dB)'] , 'Time Steps', Title)
                                            
    #                                        plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    #                                ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'], 
    #                                                                 'Time Steps', Title)             
    #                            
                                        if run_frahst == 1:
                                        
                                            # My version of Frahst 
                                            res_fr = FRAHST_V3_1(streams, alpha=a, e_low=el, e_high=eh, 
                                                             holdOffTime=h, fix_init_Q = 1, r = 1, evalMetrics = 'F') 
                                            res_fr['Alg'] = 'MyFrahst: alpha = ' + str(a) + ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
                                        
                                            FRAHST_metricList.append(analysis(res_fr, ground_truths, 300 ))                                    
    
                                            pltSummary2(res_fr, streams, (el, eh))
    #                                        
    #                                        data = res_fr
    #                                        Title = 'My Frahst with alpha = ' + str(a) + ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    #                                        plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    #                                                 ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'], 
    #                                                                 'Time Steps', Title)            
                                            
                                        if run_frahst_pedro == 1:
                                        
                                            # Pedros version of Frahst 
                                            res_frped = frahst_pedro(streams, alpha=a, e_low=el, e_high=eh, 
                                                             holdOffTime=h, r = 1, evalMetrics = 'F') 
                                            res_frped['Alg'] = 'Pedros Frahst: alpha = ' + str(a) +  \
                                            ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    
                                            PEDRO_FRAHST_metricList.append(analysis(res_frped, ground_truths, 300 ))
    
                                        
                                            pltSummary2(res_frped, streams, (el, eh))
    #                                
    #                                        data = res_frped
    #                                        Title = 'Pedros Frahst: alpha = ' + str(a) +  \
    #                                        ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    #                                        
    #                                        plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    #                                                 ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'], 
    #                                                                 'Time Steps', Title)   
                                                                     
                                    
    finish  = time.time() - start
    print 'Runtime = ' + str(finish) + 'seconds\n'
    print 'In H:M:S = ' + GetInHMS(finish)
Exemplo n.º 3
0
                                        res_fr = FRAHST_V3_1(streams, alpha=a, e_low=el, e_high=eh, 
                                                         holdOffTime=h, fix_init_Q = 1, r = 1, evalMetrics = 'F') 
                                        res_fr['Alg'] = 'MyFrahst: alpha = ' + str(a) + ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
                                    
                                        FRAHST_RawResults.append(res_fr)                                        
                                        
                                        FRAHST_metricList.append(analysis(res_fr, ground_truths, 300, 
                                                                          epsilon = epsilon, ignoreUpTo = ignore))                                    

#                                    pltSummary2(res_fr, streams, (el, eh))

                                        
                                    if run_frahst_pedro == 1:
                                    
                                        # Pedros version of Frahst 
                                        res_frped = frahst_pedro(streams, alpha=a, e_low=el, e_high=eh, 
                                                         holdOffTime=h, r = 1, evalMetrics = 'F') 
                                        res_frped['Alg'] = 'Pedros Frahst: alpha = ' + str(a) +  \
                                        ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'

                                        PEDRO_FRAHST_RawResults.append(res_frped)                                        
                                        
                                        PEDRO_FRAHST_metricList.append(analysis(res_frped, ground_truths, 300,
                                                                                epsilon = epsilon, ignoreUpTo = ignore))

                                        
#                                        pltSummary2(res_frped, streams, (el, eh))
#                                
#                                                                
                                print 'Finished running alg(s) with parameter set ' + str(alg_count) + ' of ' + str(total_alg) + ' for this dataset\n'
                                print  '\t' + str(total_data - dataset_count) + ' datasets remaining\n'
                                alg_count += 1                                
Exemplo n.º 4
0
            res_fr['Alg'] = 'My Frahst with alpha = ' + str(valA) + ' and e_low = ' + str(valB)
        
            pltSummary2(res_fr, streams, (e_low, e_high))
    
            data = res_fr

            Title = 'My Frahst with alpha = ' + str(valA) + ' and e_low = ' + str(valB)

            plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'], 
                                     'Time Steps', Title)            
            
        if run_frahst_pedro == 1:
        
            # My version of Frahst 
            res_frped = frahst_pedro(streams, alpha=alpha, e_low=e_low, e_high=e_high, 
                             holdOffTime=holdOff, r = 1, evalMetrics = 'T') 
            res_frped['Alg'] = 'Pedros Frahst with alpha = ' + str(valA) + ' and e_low = ' + str(valB)
        
            pltSummary2(res_frped, streams, (e_low, e_high))
    
            data = res_frped

            Title = 'Pedros Frahst with alpha = ' + str(valA) + ' and e_low = ' + str(valB)

            plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'], 
                                     'Time Steps', Title)            
            
finish  = time.time() - start
print 'Runtime = ', finish
Exemplo n.º 5
0
            Title = 'My Frahst with alpha = ' + str(
                valA) + ' and e_low = ' + str(valB)

            plot_4x1(streams, data['hidden'], data['orthog_error'],
                     data['subspace_error'], [
                         'Input Data', 'Hidden\nVariables',
                         'Orthogonality\nError (dB)', 'Subspace\nError (dB)'
                     ], 'Time Steps', Title)

        if run_frahst_pedro == 1:

            # My version of Frahst
            res_frped = frahst_pedro(streams,
                                     alpha=alpha,
                                     e_low=e_low,
                                     e_high=e_high,
                                     holdOffTime=holdOff,
                                     r=1,
                                     evalMetrics='T')
            res_frped['Alg'] = 'Pedros Frahst with alpha = ' + str(
                valA) + ' and e_low = ' + str(valB)

            pltSummary2(res_frped, streams, (e_low, e_high))

            data = res_frped

            Title = 'Pedros Frahst with alpha = ' + str(
                valA) + ' and e_low = ' + str(valB)

            plot_4x1(streams, data['hidden'], data['orthog_error'],
                     data['subspace_error'], [
Exemplo n.º 6
0
def singleShiftRerun(parameter_string):

    params = parameter_string.split(', ')

    # Data Sets
    baseLine = 0.0

    num_streams = [int(params[0].split()[-1])]  # n
    SNRs = [int(params[1].split()[-1])]  # snr
    anomaly_lengths = [int(params[2].split()[-1])]  # l
    anomaly_magnitudes = [int(params[3].split()[-1])]  # m

    initial_conditions = 20  # i

    # Algorithm Parameters
    thresholds = params[4].split()[-1].split(',')

    e_highs = [float(thresholds[1][0:-1])]  # eh
    e_lows = [float(thresholds[0][1:])]  # el
    alphas = [float(params[5].split()[-1])]  # a
    holdOffs = [int(params[6].split()[-1])]  # h

    # Algorithm flags
    run_spirit = 0
    run_frahst = 1
    run_frahst_pedro = 0

    #===============================================================================
    # Initialise Data sets
    #========-=======================================================================

    # For Profiling
    start = time.time()

    for n in num_streams:
        for snr in SNRs:
            for l in anomaly_lengths:
                for m in anomaly_magnitudes:
                    A = 0
                    for i in range(initial_conditions):

                        # Seed random number generator
                        np.random.seed(i)

                        # Two ts that have anomalous shift
                        s0 = Tseries(0)
                        s1 = Tseries(0)
                        s0.makeSeries([1, 3, 1], [100, l, 200 - l],
                                      [baseLine, baseLine, baseLine + m],
                                      gradient=float(m) / float(l),
                                      noise_type='none')
                        s1.makeSeries([1, 4, 1], [200, l, 100 - l],
                                      [baseLine, baseLine, baseLine - m],
                                      gradient=float(m) / float(l),
                                      noise_type='none')
                        # The rest of the ts
                        for k in range(2, n):
                            name = 's' + str(k)
                            vars()[name] = Tseries(0)
                            vars()[name].makeSeries([1], [300], [baseLine],
                                                    noise_type='none')

                        # Concat into one matrix
                        S = scipy.c_[s0]
                        for k in range(1, n):
                            S = scipy.c_[S, vars()['s' + str(k)]]

                        # Concatonate to 3d array, timesteps x streams x initial condition
                        if type(A) == int:
                            A = S
                        else:
                            A = np.dstack((A, S))

                    # Calculate the noise
                    Ps = np.sum(A[:, :, 0]**2)
                    Pn = Ps / (10.**(snr / 10.))
                    scale = Pn / (n * 300.)
                    noise = np.random.randn(A.shape[0], A.shape[1],
                                            A.shape[2]) * np.sqrt(scale)
                    A = A + noise

                    #===============================================================================
                    # Ground Truths
                    #===============================================================================
                    #                                 # time step | length
                    ground_truths = np.array([[100, l], [200, l]])

                    #==============================================================================
                    #  Run Algorithm
                    #==============================================================================
                    alg_count = 1

                    for eh in e_highs:
                        for el in e_lows:
                            for a in alphas:
                                for h in holdOffs:

                                    print 'Running Algorithm(s) with:\nE_Thresh = (' + str(el) + ',' + str(eh) + ')\n' + \
                                    'alpha = ' + str(a) + '\nHoldOff = ' + str(h)

                                    SPIRIT_metricList = []
                                    FRAHST_metricList = []
                                    PEDRO_FRAHST_metricList = []

                                    for i in range(initial_conditions):

                                        # Load Data
                                        streams = A[:, :, i]

                                        if run_spirit == 1:
                                            # SPIRIT
                                            res_sp = SPIRIT(streams,
                                                            a, [el, eh],
                                                            evalMetrics='F',
                                                            reorthog=False,
                                                            holdOffTime=h)
                                            res_sp['Alg'] = 'SPIRIT: alpha = ' + str(a) + \
                                            ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'

                                            pltSummary2(
                                                res_sp, streams, (el, eh))

                                            SPIRIT_metricList.append(
                                                analysis(
                                                    res_sp, ground_truths,
                                                    300))

    #                                        data = res_sp
    #                                        Title = 'SPIRIT alpha = ' + str(a) + ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    #
    #                                    plot_4x1(streams, data['hidden'], data['e_ratio'], data['orthog_error'],
    #                                             ['Input Data','Hidden\nVariables',
    #                                    'Energy Ratio', 'Orthogonality\nError (dB)'] , 'Time Steps', Title)

    #                                        plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    #                                ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'],
    #                                                                 'Time Steps', Title)
    #
                                        if run_frahst == 1:

                                            # My version of Frahst
                                            res_fr = FRAHST_V3_1(
                                                streams,
                                                alpha=a,
                                                e_low=el,
                                                e_high=eh,
                                                holdOffTime=h,
                                                fix_init_Q=1,
                                                r=1,
                                                evalMetrics='F')
                                            res_fr[
                                                'Alg'] = 'MyFrahst: alpha = ' + str(
                                                    a
                                                ) + ' ,E_Thresh = (' + str(
                                                    el) + ',' + str(eh) + ')'

                                            FRAHST_metricList.append(
                                                analysis(
                                                    res_fr, ground_truths,
                                                    300))

                                            pltSummary2(
                                                res_fr, streams, (el, eh))
    #
    #                                        data = res_fr
    #                                        Title = 'My Frahst with alpha = ' + str(a) + ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    #                                        plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    #                                                 ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'],
    #                                                                 'Time Steps', Title)

                                        if run_frahst_pedro == 1:

                                            # Pedros version of Frahst
                                            res_frped = frahst_pedro(
                                                streams,
                                                alpha=a,
                                                e_low=el,
                                                e_high=eh,
                                                holdOffTime=h,
                                                r=1,
                                                evalMetrics='F')
                                            res_frped['Alg'] = 'Pedros Frahst: alpha = ' + str(a) +  \
                                            ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'

                                            PEDRO_FRAHST_metricList.append(
                                                analysis(
                                                    res_frped, ground_truths,
                                                    300))

                                            pltSummary2(
                                                res_frped, streams, (el, eh))
    #
    #                                        data = res_frped
    #                                        Title = 'Pedros Frahst: alpha = ' + str(a) +  \
    #                                        ' ,E_Thresh = (' + str(el) + ',' + str(eh) + ')'
    #
    #                                        plot_4x1(streams, data['hidden'], data['orthog_error'], data['subspace_error'],
    #                                                 ['Input Data','Hidden\nVariables', 'Orthogonality\nError (dB)','Subspace\nError (dB)'],
    #                                                                 'Time Steps', Title)

    finish = time.time() - start
    print 'Runtime = ' + str(finish) + 'seconds\n'
    print 'In H:M:S = ' + GetInHMS(finish)
Exemplo n.º 7
0
    
    e_high = 0.98
    e_low = 0.96
    alpha = 0.96
    sci = -1

    holdOFF = 50

    # My version 
    res_me = FRAHST_V3(data, alpha=0.96, e_low=0.96, e_high=0.98, sci = -1, \
    holdOffTime=holdOFF, r = 1, evalMetrics = 'F') 

    metric_me, sets_me, anom_det_tab_me = analysis(res_me, AbileneMat['P_g_truth_tab_alt'], n)

    new_metrics_me = scoreEvaluation(n, res_me['anomalies'], 
                                  AbileneMat['P_g_truth_tab_alt'], ignoreUpTo = 400)
                                  
    # Pedros Version
    res_ped = frahst_pedro(data, r = 1, alpha=0.96, energy_low=0.96, energy_high=0.98,  \
    holdOffTime=holdOFF, evaluateTruth='FALSE')
    
    metric_ped, sets_ped, anom_det_tab_ped = analysis(res_ped, AbileneMat['P_g_truth_tab_alt'], n)
    
    new_metrics_ped = scoreEvaluation(n, res_ped['anomalies'], 
                                  AbileneMat['P_g_truth_tab_alt'], ignoreUpTo = 400)

    
    
    
    
    
Exemplo n.º 8
0
    res_me3['Alg'] = 'My First Implimentation of FRAUST sci = -1'
    
    pltSummary(res_me3, data)
    
    # My second version
    
    res_me2 = FRAHST_V3_1(data, r = 1,
                        alpha = param['alpha'], 
                        e_low = param['e_low'], 
                        e_high = param['e_high'],
                        holdOffTime=param['holdOffTime'],
                        fix_init_Q = 1, 
                        evalMetrics = 'F')   
    
    res_me2['Alg'] = 'My Second Implimentation of FRAUST'
    
    pltSummary(res_me2, data)
    
    # Pedros Version
    res_ped = frahst_pedro(data, r = 1,
                            alpha = param['alpha'],
                            energy_low = param['e_low'],
                            energy_high = param['e_high'],
                            holdOffTime=param['holdOffTime'],
                            evaluateTruth='FALSE')

    res_ped['Alg'] = 'Pedros Implimentation of FRAUST'

    pltSummary(res_ped, data)