F.batch_analysis(gt_list, anomalies_list, keep_sets=0) # Fill in R E.R[i, j, k]['met']['TP'] = F.metric['TP'] E.R[i, j, k]['met']['FP'] = F.metric['FP'] E.R[i, j, k]['met']['TN'] = F.metric['TN'] E.R[i, j, k]['met']['FN'] = F.metric['FN'] E.R[i, j, k]['met']['precision'] = F.metric['precision'] E.R[i, j, k]['met']['recall'] = F.metric['recall'] E.R[i, j, k]['met']['F05'] = F.metric['F05'] E.R[i, j, k]['met']['F1'] = F.metric['F1'] E.R[i, j, k]['met']['F2'] = F.metric['F2'] E.R[i, j, k]['met']['ACC'] = F.metric['ACC'] E.R[i, j, k]['met']['FDR'] = F.metric['FDR'] E.R[i, j, k]['met']['FPR'] = F.metric['FPR'] ti = time.time() - start tim = GetInHMS(ti) print 'Finished Algorithm ver %i of %i at %s' % (i + 1, alg_ver_count, tim) fin = time.time() - start hms = GetInHMS(fin) print 'Finished a total of %i iterations in %s' % (total_loops, hms) print 'Average of %f seconds per iteration' % (fin / total_loops) with open(path + '/' + "E.p", "wb") as f: pickle.dump(E, f) E.write2csv('TP', path + '/' + "TP.csv") E.write2csv('FP', path + '/' + "FP.csv")
ehighStr = '[ ' for s in e_highs: ehighStr = ehighStr + "%.2f" % s + ', ' alphasStr = '[ ' for s in alphas: alphasStr = alphasStr + "%.2f" % s + ', ' f.write('Algorithm Parameters\n') f.write('--------------------\n') f.write('Lower Energy Thresholds: ' + elowStr + ']\n') f.write('Upper Energy Thresholds: ' + ehighStr + ']\n') f.write('Alphas: ' + alphasStr + ']\n') f.write('Hold Current r lengths: ' + str(holdOffs) + '\n.\n') f.write('Algorithms Run\n') f.write('--------------------\n') if run_spirit : f.write('SPIRIT\n') if run_frahst : f.write('FRAHST - My Most Current Version\n') if run_frahst_pedro : f.write('FRAHST - Pedros Published Version\n') os.chdir(cwd) finish = time.time() - start print 'Runtime = ' + str(finish) + 'seconds\n' print 'In H:M:S = ' + GetInHMS(finish)
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)