コード例 #1
0
    def sample(self, n_samples):
        ts = TimeSeries()
        ts.y = self.model.sample(n_samples)[0]
        ts.x = range(len(ts.y))

        out_path = "{}/plots/{}/sample.png".format(script_dir,
                                                   self.__class__.__name__)
        plot_procedures.plot_ts(ts, out_path, compress=True)
コード例 #2
0
def plot(row, dt_cp_list, ts, dt_start, dt_end):
    out_path = ("./plots/per_user/{}/{}_{}_dtstart{}_dtend{}"
                "".format(target_email, row["server"], row["mac"],
                          str(dt_start), str(dt_end)))
    plot_procedures.plot_ts(ts,
                            out_path,
                            dt_cp_list,
                            compress=True,
                            ylim=[-0.05, 1.05])
コード例 #3
0
def plot_latencies_traceroute(dt_start, dt_end, preprocess_args):
    str_dt = utils.get_str_dt(dt_start, dt_end)

    in_path = "{}/prints/{}/filtered/traceroute_per_mac.csv".format(script_dir,
                                                                    str_dt)
    df = pd.read_csv(in_path)
    for _, row, in df.iterrows():
        if row["valid_cnt_samples"]:
            in_path = utils.get_in_path(row["server"], row["mac"], dt_start,
                                        dt_end)
            ts_traceroute = TimeSeries(in_path=in_path, metric="traceroute",
                                       dt_start=dt_start, dt_end=dt_end)

            for traceroute_type in unsupervised_utils.iter_traceroute_types():
                valid_traceroute_field, traceroute_field = \
                    cp_utils.get_traceroute_fields(traceroute_type)
                if row[valid_traceroute_field]:
                    traceroute = ast.literal_eval(row[traceroute_field])
                    name_ts = get_ts_per_name(traceroute_type, ts_traceroute,
                                              dt_start, dt_end)

                    dir_path = ("{}/plots/paths/{}/{}/{}/{}".
                                format(script_dir, str_dt, "latency",
                                       traceroute_type, row["server"]))
                    traceroute_path = "/".join(map(str,
                                                   list(reversed(traceroute))))
                    dir_path = "{}/{}".format(dir_path, traceroute_path)

                    utils.create_dirs(["{}/traceroute_latencies/".
                                       format(dir_path),
                                       "{}/traceroute_latencies/{}".
                                       format(dir_path, row["mac"])])

                    for i in range(len(traceroute) - 1):
                        name = traceroute[i][0][0]
                        traceroute_path = "hop{}_{}".format(str(i).zfill(2),
                                                            name)
                        out_path = ("{}/traceroute_latencies/{}/{}.png".
                                    format(dir_path, row["mac"],
                                           traceroute_path))

                        ts_preprocessed = name_ts[name].copy()
                        cp_utils.preprocess(ts_preprocessed, preprocess_args)

                        # plot_procedures.plot_ts_share_x(
                        #     name_ts[name],
                        #     ts_preprocessed,
                        #     out_path,
                        #     plot_type2="scatter",
                        #     title1="raw",
                        #     title2="median filtered",
                        #     default_ylabel=True)
                        ts_preprocessed.metric = "latency"
                        plot_procedures.plot_ts(ts_preprocessed, out_path,
                                                title="median filtered")
コード例 #4
0
def mean_per_hour(in_path, server, mac):
    ts = TimeSeries(in_path,
                    "loss",
                    dt_start=dt_start,
                    dt_end=dt_end,
                    ts_type="hourly")
    ts.compress()
    plot_procedures.plot_ts(ts,
                            "./plots/ts/ts_{}_{}.png".format(server, mac),
                            ylim=[-0.05, 1.05],
                            ylabel="Loss Fraction",
                            xlabel="day/month")
コード例 #5
0
def plot(dt_start, dt_end, metric):
    dt_dir = utils.get_dt_dir(dt_start, dt_end)
    str_dt = utils.get_str_dt(dt_start, dt_end)
    utils.create_dirs([
        "{}/{}".format(script_dir, str_dt),
        "{}/{}/{}".format(script_dir, str_dt, metric)
    ])
    for server, mac, in_path in utils.iter_server_mac(dt_dir, True):
        out_file_name = utils.get_out_file_name(server, mac, dt_start, dt_end)
        out_path = "{}/{}/{}/{}.png".format(script_dir, str_dt, metric,
                                            out_file_name)

        # comparison between not filtered and filtered
        ts = TimeSeries(in_path, metric, dt_start, dt_end)
        ts_filter = TimeSeries(in_path, metric, dt_start, dt_end)
        ts_filter.percentile_filter(win_len=5, p=0.5)

        # if len(ts_filter.y) > 100:
        #     plot_procedures.plot_stl_decomposition(ts_filter,
        #                                            "median_filtered",
        #                                            out_path)

        # comparison between with cross traffic and without
        # ts = TimeSeries(in_path, metric, dt_start, dt_end)
        # ts.percentile_filter(win_len=13, p=0.5)
        # ts_filter = TimeSeries(in_path, metric, dt_start, dt_end,
        #                        cross_traffic_thresh=0)
        # ts_filter.percentile_filter(win_len=13, p=0.5)

        # plot_procedures.plot_ts_share_x(ts, ts_filter, out_path,
        #                                 compress=True,
        #                                 plot_type2="scatter",
        #                                 title1="raw",
        #                                 title2="median filtered",
        #                                 default_ylabel=True,
        #                                 xlabel="$i$")

        ylabel = plot_procedures.get_default_ylabel(ts)
        plot_procedures.plot_ts(ts_filter,
                                out_path,
                                ylabel=ylabel,
                                compress=False,
                                title="median filtered")
コード例 #6
0
def plot_dataset():
    utils.create_dirs(["{}/plots/".format(script_dir)])
    in_dir = "{}/change_point/input/".format(base_dir)

    ts_cnt = get_cnt_class_per_ts(in_dir)

    for dataset in os.listdir(in_dir):
        if os.path.isdir("{}/{}".format(in_dir, dataset)) and \
                (dataset != "unsupervised"):
            df = pd.read_csv("{}/{}/dataset.csv".format(in_dir, dataset))
            for idx, row in df.iterrows():
                print "dataset={}, idx={}".format(dataset, idx)

                ts = cp_utils.get_ts(row, {"filter_type": "none"}, "loss")
                if not ts.y:
                    continue

                correct = cp_utils.from_str_to_int_list(
                    row["change_points_ids"])

                _, dt_start, dt_end = cp_utils.unpack_pandas_row(row)
                ts_key = (row["server"], row["mac"], row["dt_start"],
                          row["dt_end"])
                out_file_name = utils.get_out_file_name(
                    row["server"], row["mac"], dt_start, dt_end)
                out_dir = "{}/plots/cnt{}_{}".format(script_dir,
                                                     ts_cnt[ts_key],
                                                     out_file_name)
                out_path = "{}/{}.png".format(out_dir, dataset)
                utils.create_dirs([out_dir])
                plot_procedures.plot_ts(ts,
                                        out_path,
                                        ylim=[-0.02, 1.02],
                                        dt_axvline=np.asarray(ts.x)[correct],
                                        compress=True,
                                        ylabel="loss fraction",
                                        xlabel="$i$",
                                        title="raw")
コード例 #7
0
 def plot(self, ts, ts_raw, correct, pred, conf, out_path):
     if "unsupervised" in out_path:
         ylabel = plot_procedures.get_default_ylabel(ts)
         plot_procedures.plot_ts(ts,
                                 out_path,
                                 dt_axvline=np.asarray(ts.x)[correct],
                                 ylabel=ylabel,
                                 xlabel="$i$",
                                 compress=True,
                                 title="median filtered")
     else:
         dt_axvline1 = np.asarray(ts.x)[correct]
         dt_axvline2 = np.asarray(ts.x)[pred]
         plot_procedures.plot_ts_share_x(
             ts_raw,
             ts,
             out_path,
             compress=False,
             title1="correct",
             dt_axvline1=dt_axvline1,
             dt_axvline2=dt_axvline2,
             title2="predicted. conf={}".format(conf),
             plot_type2="scatter")
コード例 #8
0
def plot_per_name(dt_start, dt_end, metric, preprocess_args, plot_cps=True):
    dt_dir = utils.get_dt_dir(dt_start, dt_end)
    str_dt = utils.get_str_dt(dt_start, dt_end)

    utils.create_dirs([
        "{}/plots/".format(script_dir), "{}/plots/names".format(script_dir),
        "{}/plots/names/{}".format(script_dir, str_dt),
        "{}/plots/names/{}/{}".format(script_dir, str_dt, metric)
    ])

    client_cps = unsupervised_utils.get_client_cps(plot_cps, str_dt, metric)

    # avoid reploting
    client_plotPath = {}

    for traceroute_type in unsupervised_utils.iter_traceroute_types():
        valid_traceroute_field, traceroute_field = \
            cp_utils.get_traceroute_fields(traceroute_type)

        utils.create_dirs([
            "{}/plots/names/{}/{}/{}".format(script_dir, str_dt, metric,
                                             traceroute_type)
        ])

        df = pd.read_csv("{}/prints/{}/filtered/traceroute_per_mac.csv".format(
            script_dir, str_dt))
        cnt = 0
        for idx, row in df.iterrows():
            if row["valid_cnt_samples"] and row[valid_traceroute_field]:
                print("cnt={}, traceroute_type={}, str_dt={}".format(
                    cnt, traceroute_type, str_dt))
                cnt += 1

                client = utils.get_client(row["server"], row["mac"])

                for name in cp_utils.iter_names_traceroute_filtered(
                        ast.literal_eval(row[traceroute_field])):

                    utils.create_dirs([
                        "{}/plots/names/{}/{}/{}/{}".format(
                            script_dir, str_dt, metric, traceroute_type,
                            row["server"]),
                        "{}/plots/names/{}/{}/{}/{}/{}".format(
                            script_dir, str_dt, metric, traceroute_type,
                            row["server"], name)
                    ])

                    out_file_name = utils.get_out_file_name(
                        row["server"], row["mac"], dt_start, dt_end)
                    out_path = ("{}/plots/names/{}/{}/{}/{}/{}/{}.png".format(
                        script_dir, str_dt, metric, traceroute_type,
                        row["server"], name, out_file_name))

                    # avoid reploting
                    if client in client_plotPath:
                        shutil.copyfile(client_plotPath[client], out_path)
                    else:
                        client_plotPath[client] = out_path
                        cp_dts = client_cps[client]

                        in_path = "{}/input/{}/{}/{}.csv".format(
                            base_dir, dt_dir, row["server"], row["mac"])

                        ts = TimeSeries(in_path, metric, dt_start, dt_end)
                        cp_utils.preprocess(ts, preprocess_args)
                        plot_procedures.plot_ts(ts,
                                                out_path,
                                                dt_axvline=cp_dts,
                                                title="median filtered")
コード例 #9
0
def print_empty_segs(dt_start,
                     dt_end,
                     metric,
                     min_seg_len,
                     filtered,
                     plot=False):
    dt_dir = utils.get_dt_dir(dt_start, dt_end)
    str_dt = utils.get_str_dt(dt_start, dt_end)

    utils.create_dirs([
        "{}/prints/".format(script_dir),
        "{}/prints/{}".format(script_dir, str_dt),
        "{}/prints/{}/{}".format(script_dir, str_dt, filtered),
        "{}/prints/{}/{}/{}".format(script_dir, str_dt, filtered, metric)
    ])

    out_path = "{}/prints/{}/{}/{}/empty_segs_per_mac.csv".format(
        script_dir, str_dt, filtered, metric)
    with open(out_path, "w") as f:
        f.write("server,mac,empty_segs\n")

        target_macs = read_input.get_macs_traceroute_filter(
            dt_start, dt_end, filtered)
        for server, mac, in_path in utils.iter_server_mac(dt_dir, True):
            if mac not in target_macs:
                continue

            ts = TimeSeries(in_path=in_path,
                            metric=metric,
                            dt_start=dt_start,
                            dt_end=dt_end)

            axvline_dts = []
            empty_segs = []
            if len(ts.x) >= 2:
                if is_empty_seg(dt_start, ts.x[0], min_seg_len):
                    axvline_dts.append(ts.x[0])
                    empty_segs.append([str(dt_start), str(ts.x[0])])
                for i in xrange(1, len(ts.x)):
                    if is_empty_seg(ts.x[i - 1], ts.x[i], min_seg_len):
                        axvline_dts.append(ts.x[i - 1])
                        axvline_dts.append(ts.x[i])
                        empty_segs.append([str(ts.x[i - 1]), str(ts.x[i])])
                if is_empty_seg(ts.x[-1], dt_end, min_seg_len):
                    axvline_dts.append(ts.x[i - 1])
                    empty_segs.append([str(ts.x[-1]), str(dt_end)])

            f.write("{},{},\"{}\"\n".format(server, mac, empty_segs))

            if plot:
                utils.create_dirs([
                    "{}/plots/".format(script_dir),
                    "{}/plots/empty_segs".format(script_dir),
                    "{}/plots/empty_segs/{}".format(script_dir, str_dt),
                    "{}/plots/empty_segs/{}/{}".format(script_dir, str_dt,
                                                       metric)
                ])

                out_file_name = utils.get_out_file_name(
                    server, mac, dt_start, dt_end)
                out_path = ("{}/plots/empty_segs/{}/{}/{}.png".format(
                    script_dir, str_dt, metric, out_file_name))
                plot_procedures.plot_ts(ts, out_path, dt_axvline=axvline_dts)