def run_specific_client(dt_start, dt_end, cmp_class_args, preprocess_args, param, metric, mac, server, model_class, out_path): model = model_class(preprocess_args=preprocess_args, metric=metric, **param) out_file_name = utils.get_out_file_name(server, mac, dt_start, dt_end) out_path = "{}/{}".format(out_path, out_file_name) row = {"mac": mac, "server": server, "dt_start": str(dt_start), "dt_end": str(dt_end), "change_points_ids": None} model.plot_single(row, cmp_class_args, "unsupervised", out_path)
def plot_all(self, dataset, out_dir_path, cmp_class_args): train_path = "{}/change_point/input/{}/dataset.csv".format(base_dir, dataset) df = pd.read_csv(train_path) cnt = 0 for idx, row in df.iterrows(): cnt += 1 print "cnt={}".format(cnt) in_path, dt_start, dt_end = cp_utils.unpack_pandas_row(row) out_file_name = utils.get_out_file_name(row["server"], row["mac"], dt_start, dt_end) out_path = "{}/id{}_{}".format(out_dir_path, idx, out_file_name) self.plot_single(row, cmp_class_args, dataset, out_path)
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")
def plot(self, server, mac, dt_start, dt_end, ts, hidden_state_path): ts_hidden_state_path = TimeSeries() ts_hidden_state_path.x = copy.deepcopy(ts.x) ts_hidden_state_path.y = copy.deepcopy(hidden_state_path) out_file_name = utils.get_out_file_name(server, mac, dt_start, dt_end) out_path = "{}/plots/{}/{}.png".format(script_dir, self.__class__.__name__, out_file_name) plot_procedures.plot_ts_share_x( ts, ts_hidden_state_path, out_path, compress=True, title1="raw time series", title2="best hidden state path", plot_type2="scatter", yticks2=range(self.model.n_components), ylim2=[-0.5, self.model.n_components - 0.5])
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")
def plot_per_node(dt_start, dt_end, metric, only_unique_traceroute): 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/nodes".format(script_dir), "{}/plots/nodes/{}".format(script_dir, str_dt), "{}/plots/nodes/{}/{}".format(script_dir, str_dt, metric) ]) valid_nodes = read_input.get_valid_nodes() mac_node = read_input.get_mac_node() macs_unique_traceroute = 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 only_unique_traceroute and (mac not in macs_unique_traceroute): continue if mac_node[mac] in valid_nodes: utils.create_dirs([ "{}/plots/nodes/{}/{}/{}".format(script_dir, str_dt, metric, mac_node[mac]) ]) out_file_name = utils.get_out_file_name(server, mac, dt_start, dt_end) out_path = ("{}/plots/nodes/{}/{}/{}/{}.png".format( script_dir, str_dt, metric, mac_node[mac], out_file_name)) 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=13, p=0.5) plot_procedures.plot_ts_share_x(ts, ts_filter, out_path, compress=False, plot_type2="scatter")
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")
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)