Example #1
0
def detection_with_bothwindows(eigen_file="UCI_eigs_slices.pkl",
                               timestamps=195,
                               percent_ranked=0.05,
                               window1=10,
                               window2=30,
                               initial_window=30,
                               difference=True):
    principal = True
    spectrums = normal_util.load_object(eigen_file)
    spectrums = np.asarray(spectrums)

    spectrums = spectrums.real
    spectrums = spectrums.reshape((timestamps, -1))

    spectrums = normalize(spectrums, norm='l2')

    print("short window is " + str(window1))
    print("long window is " + str(window2))
    print("initial window is " + str(initial_window))
    print(spectrums.shape)
    (z_shorts, z_longs, z_scores,
     anomalies) = change_detection_two_windows(spectrums,
                                               principal=principal,
                                               percent_ranked=percent_ranked,
                                               window1=window1,
                                               window2=window2,
                                               initial_window=initial_window,
                                               difference=difference)
    print("found anomalous time stamps are")
    print(anomalies)

    events = anomalies
    return (z_shorts, z_longs, z_scores, events)
Example #2
0
def detection_with_shortwindow(eigen_file="UCI_eigs_slices.pkl",
                               timestamps=195,
                               percent_ranked=0.05,
                               window=10,
                               initial_window=15,
                               difference=True):

    principal = True
    spectrums = normal_util.load_object(eigen_file)
    spectrums = np.asarray(spectrums).real
    spectrums = spectrums.reshape((timestamps, -1))

    spectrums = normalize(spectrums, norm='l2')

    print("window is " + str(window))
    print("initial window is " + str(initial_window))
    print(spectrums.shape)
    (z_scores, anomalies) = change_detection(spectrums,
                                             principal=principal,
                                             percent_ranked=percent_ranked,
                                             window=window,
                                             initial_window=initial_window,
                                             difference=difference)
    print("found anomalous time stamps are")
    print(anomalies)

    return z_scores
Example #3
0
def visiualize_vecs_UCI(eigen_file, vec_file, eigen_name, vec_name):
    Temporal_eigenvalues = normal_util.load_object(eigen_file)
    activity_vecs = normal_util.load_object(vec_file)
    limit = 5

    for i in range(0, len(Temporal_eigenvalues)):
        Temporal_eigenvalues[i] = Temporal_eigenvalues[i][0:limit]

    for i in range(0, len(activity_vecs)):
        activity_vecs[i] = activity_vecs[i].flatten()[0:limit]

    graph_name = "UCI"
    # normal_util.plot_laplacian_spectrum(diag_vecs, graph_name)
    normal_util.plot_activity_intensity(
        np.asarray(Temporal_eigenvalues).real, eigen_name)
    normal_util.plot_activity_intensity(
        np.asarray(activity_vecs).real, vec_name)
Example #4
0
def plot_anomaly_and_spectro(fname,
                             scores,
                             score_labels,
                             events,
                             eigen_file="USLegis_L_singular.pkl"):

    labels = list(range(97, 109, 1))
    scores[0] = set_non_negative(scores[0])

    fig, axs = plt.subplots(2)
    plt.rcParams.update({'figure.autolayout': True})

    diag_vecs = normal_util.load_object(eigen_file)
    diag_vecs = np.transpose(np.asarray(diag_vecs))  #let time be x-axis
    diag_vecs = np.flip(diag_vecs, 0)

    max_time = len(scores[0])
    t = list(range(0, max_time))
    colors = ['#fdbb84', '#43a2ca', '#bc5090', '#e5f5e0', '#fa9fb5', '#c51b8a']
    for i in range(len(scores)):
        axs[0].plot(t,
                    scores[0],
                    color=colors[i],
                    ls='solid',
                    label=score_labels[i])

    for event in events:
        max_score = 0
        for i in range(len(scores)):
            if scores[i][event] > max_score:
                max_score = scores[i][event]

        axs[0].annotate(
            str(labels[event]),  # this is the text
            (event, max_score),  # this is the point to label
            textcoords="offset points",  # how to position the text
            xytext=(0, -12),  # distance from text to points (x,y)
            ha='center')  # horizontal alignment can be left, right or center

    addLegend = True

    for event in events:
        #plt.axvline(x=event,color='k', linestyle="--", linewidth=0.5)
        max_score = 0
        for i in range(len(scores)):
            if scores[i][event] > max_score:
                max_score = scores[i][event]
        if (addLegend):
            axs[0].plot(event,
                        max_score,
                        marker="*",
                        color='#de2d26',
                        ls='solid',
                        label="detected anomalies")
            addLegend = False
        else:
            axs[0].plot(event,
                        max_score,
                        marker="*",
                        color='#de2d26',
                        ls='solid')

    #US senate
    for i in range(len(labels)):
        labels[i] = str(labels[i])

    axs[0].set_xlabel('Congress')
    axs[0].set_ylabel('anomaly score')
    plt.tight_layout()
    axs[0].legend()
    axs[0].set_xticks(t)
    axs[0].set_xticklabels(labels)

    axs[1].set_xlabel('Congress')
    axs[1].set_ylabel('rank')
    axs[1].set_xticks(t)
    axs[1].set_xticklabels(labels)

    axs[1].imshow(diag_vecs, aspect='auto')

    plt.savefig(fname + 'anomalySpectro.pdf')
Example #5
0
def load_mp():
    fname = "datasets/canVote_processed/mp_dict.pkl"
    MP_dict = normal_util.load_object(fname)

    return MP_dict
Example #6
0
def plot_spectrum(pkl_name, graph_name):
    eigen_slices = normal_util.load_object(pkl_name)
    normal_util.plot_activity_intensity(eigen_slices, graph_name)