def process_ml_features(self, X, y, filename, data_period_ml_train, tsfresh_features_to_extract_selected=None): logging.info("process ml features: " + filename) data_exists = False file_exists = os.path.isfile(os.path.join(__location__, filename)) if file_exists: X_features = DillSerializer(os.path.join(__location__, filename)).deserialize() if data_period_ml_train in X_features: X_all_features = X_features[data_period_ml_train][0] X_selected_features = X_features[data_period_ml_train][1] data_exists = True if not data_exists: if tsfresh_features_to_extract_selected: tsfresh_features = TsFreshFeatures() X_all_features = tsfresh_features.extract(X, y) X_selected_features = tsfresh_features.extract_selected_features( X, tsfresh_features_to_extract_selected) else: basic_features = BasicFeatures() X_all_features = basic_features.extract(X) X_selected_features = basic_features.extract_selected_features(X) if not file_exists: X_features = {data_period_ml_train: (X_all_features, X_selected_features)} else: X_features[data_period_ml_train] = (X_all_features, X_selected_features) DillSerializer(os.path.join(__location__, filename)).serialize(X_features) return X_all_features, X_selected_features
def tsfresh_performance_evaluation(single_light_pattern=False, range_len_light_pattern=range(2, 11, 2)): sampling_period = get_pattern_max_sampling_period() if single_light_pattern: # single light patterns elapsed_times = dict() for len_light_pattern in range_len_light_pattern: data = LightData(sampling_period, [len_light_pattern]) tsfresh_features = TsFreshFeatures() features_extracted, relevance_features = tsfresh_features.relevance( data.X_tsfresh, data.y_tsfresh) elapsed_time = tsfresh_features.performance_evaluation( features_extracted, relevance_features, data.X_tsfresh) elapsed_times[len_light_pattern] = elapsed_time filename = os.path.join(__location__, "raw-results", "feature-selection", "single-light-patterns-only-runtime-tsfresh") DillSerializer(filename).serialize(elapsed_times) else: # combined light patterns data = LightData(sampling_period) tsfresh_features = TsFreshFeatures() features_extracted, relevance_features = tsfresh_features.relevance( data.X_tsfresh, data.y_tsfresh) elapsed_time = tsfresh_features.performance_evaluation( features_extracted, relevance_features, data.X_tsfresh) filename = os.path.join( __location__, "raw-results", "feature-selection", "combined-light-patterns-only-runtime-tsfresh") DillSerializer(filename).serialize(elapsed_time)
def evaluate_morse(led, test_rounds, test_period): data_len = 1024 error = True latency = False throughput = True evaluator = VlcMetrics() path = os.path.join(__location__, "..", "results", "error_data_len", led.get_name(), "data_len_" + str(data_len)) test_data = "abcdefghijklmnopqrstuvwxyz0123456789" while evaluator.get_data_rounds() < test_rounds: print("evaluation round: ", evaluator.get_data_rounds()) conn = ReceiveLightControl.Connector.local action = ReceiveLightControl.Action.evaluate_morse light_control = ReceiveLightControl( action, conn=conn, evaluator=evaluator, evaluate_period=test_period, evaluate_template=test_data, evaluate_error=error, evaluate_latency=latency, evaluate_throughput=throughput, sampling_interval=led.get_sampling_interval()) light_control.start() print("-------------------") evaluator.check_data() evaluator.print_data() serializer = DillSerializer(path) serializer.serialize(evaluator)
def evaluate_similarity_runtime(len_light_patterns, path_similarity, path_runtime, rounds): results_runtime = nested_dict(4, list) results_similarity = nested_dict(4, list) same = list(zip(len_light_patterns, len_light_patterns)) combined = list(itertools.combinations(len_light_patterns, 2)) pattern_conbination = same + combined for len_light_pattern1, len_light_pattern2 in pattern_conbination: print("from-to:", len_light_pattern1, len_light_pattern2) for run in range(rounds): print("round: ", run) client1, client2 = get_light_signals([len_light_pattern1, len_light_pattern2]) for equalize_method in [vector_similarity.equalize_methods.fill, vector_similarity.equalize_methods.cut, vector_similarity.equalize_methods.dtw]: print("equalize:", equalize_method) for similarity_method in vector_similarity.similarity_methods: print("similarity:", similarity_method.__name__) start_time = time.time() similarity = similarity_method(client1.signal, client2.signal, equalize_method) elapsed_time = time.time() - start_time assert elapsed_time > 0 results_similarity[len_light_pattern1][len_light_pattern2][equalize_method][similarity_method.__name__].append( similarity) results_runtime[len_light_pattern1][len_light_pattern2][equalize_method][similarity_method.__name__].append( elapsed_time) DillSerializer(path_similarity).serialize(results_similarity) DillSerializer(path_runtime).serialize(results_runtime)
def find_max_latency(result_directory, stop_keyword="light_intensity"): latency = list() for f in os.listdir(result_directory): if stop_keyword not in f: path = result_directory + f vlc_evaluation = DillSerializer(path).deserialize() latency.extend(vlc_evaluation.get_latency()) return max(latency)
def evaluate_vlc_parameters(test_rounds, test_period, test_data, morse=True, throughput=True, error=True, latency=False, manchester_ip="192.168.0.2", manchester_port=11234): basedir_results = os.path.join(__location__, "..", "results", "vlc_parameters") time_base_unit = 50000 sampling_intervals = [ 5000, 10000, 15000, 20000, 25000, 30000, 50000, 100000, 150000 ] # ns # 5000, 10000, 15000, 20000, 25000, 30000, 50000, 100000, 150000 # pervasive LED # 100000, 110000, 120000, 130000, 140000, 150000 # directed LED for sampling_interval in sampling_intervals: if morse: print("sampling interval: ", sampling_interval) evaluator = VlcMetrics() while evaluator.get_data_rounds() < test_rounds: print("evaluation round: ", evaluator.get_data_rounds()) if morse: action = ReceiveLightControl.Action.evaluate_morse conn = ReceiveLightControl.Connector.local data_encoding = "morse" else: action = ReceiveLightControl.Action.evaluate_manchester conn = ReceiveLightControl.Connector.socket data_encoding = "manchester" print("start evaluation") light_control = ReceiveLightControl( action, conn=conn, evaluator=evaluator, evaluate_period=test_period, evaluate_template=test_data, evaluate_throughput=throughput, evaluate_error=error, evaluate_latency=latency, sampling_interval=sampling_interval, evaluate_ip=manchester_ip, evaluate_port=manchester_port) light_control.start() print("stop evaluation") evaluator.check_data() evaluator.print_data() if morse: filename = "%s_sampling_%d_time_base_unit_%d" filename = filename % (data_encoding, sampling_interval, time_base_unit) else: filename = "%s_freq_%d" filename = filename % (data_encoding) serializer = DillSerializer(basedir_results + filename) serializer.serialize(evaluator) print("---------------------------------")
def create_ml_coupling(self, coupling_ml_classifier, path_ml_train_data, data_period_ml_train, len_light_patterns, rounds=0): tsfresh_selected_features = os.path.join( __location__, "..", "tsfresh-features-to-be-extracted") tsfresh_selected_features = DillSerializer( tsfresh_selected_features).deserialize() single_raw_feature_data = glob.glob( os.path.join(path_ml_train_data, "single-*-raw-feature-data"))[0] single_raw_feature_data = DillSerializer( single_raw_feature_data).deserialize() coupling_classifiers = dict() for len_light_pattern in len_light_patterns: logging.info("ml len light pattern: " + str(len_light_pattern)) logging.info("data period ml train: " + str(data_period_ml_train)) logging.info("create features basic") X_basic = single_raw_feature_data[len_light_pattern][ data_period_ml_train][rounds].X_basic y_basic = single_raw_feature_data[len_light_pattern][ data_period_ml_train][rounds].y_basic X_basic_all_features, X_basic_selected_features = self.process_ml_features( X_basic, y_basic, "basic-features", len_light_pattern) assert len(X_basic_all_features) == len(y_basic) assert len(X_basic_selected_features) == len(y_basic) logging.info("create features tsfresh") X_tsfresh = single_raw_feature_data[len_light_pattern][ data_period_ml_train][rounds].X_tsfresh y_tsfresh = single_raw_feature_data[len_light_pattern][ data_period_ml_train][rounds].y_tsfresh _, X_tsfresh_selected_features = self.process_ml_features( X_tsfresh, y_tsfresh, "tsfresh-features", len_light_pattern, tsfresh_selected_features) assert len(X_tsfresh_selected_features) == len(y_tsfresh) clf_type = coupling_simulator.coupling_ml_classifiers[ coupling_ml_classifier] logging.info("create classifier basic all and selected") coupling_classifier_basic_all_features = Classifier.get_clf( clf_type) coupling_classifier_basic_all_features = coupling_classifier_basic_all_features.fit( X_basic_all_features, y_basic) coupling_classifier_basic_selected_features = Classifier.get_clf( clf_type) coupling_classifier_basic_selected_features = coupling_classifier_basic_selected_features.fit( X_basic_selected_features, y_basic) logging.info("create classifier tsfresh selected") coupling_classifier_tsfresh_selected_features = Classifier.get_clf( clf_type) coupling_classifier_tsfresh_selected_features = coupling_classifier_tsfresh_selected_features.fit( X_tsfresh_selected_features, y_tsfresh) coupling_classifiers[len_light_pattern] = ( coupling_classifier_basic_all_features, coupling_classifier_basic_selected_features, coupling_classifier_tsfresh_selected_features) return tsfresh_selected_features, coupling_classifiers
def sampling_time_light_patterns( path_light_pattern_data, path_sampling_time_signal_patterns, conversion_ms_to_s, test_rounds=10, scaling_factor=0.05): light_pattern_data = DillSerializer(path_light_pattern_data).deserialize() sampling_times = dict() light_pattern_lengths = dict() duration_signal_len = {key: value.signal_duration for key, value in light_pattern_data.items()} for test_round in range(test_rounds): print("round: ", test_round+1) sampling_time = dict() light_pattern_length = dict() for len_light_pattern, min_sampling_duration in duration_signal_len.items(): print("len light pattern: ", len_light_pattern) min_sampling_duration = min_sampling_duration / conversion_ms_to_s sampling_duration = min_sampling_duration base_sampling_duration = min_sampling_duration sampling_time_too_small = True while sampling_time_too_small: light_pattern, light_pattern_time = light_analysis.load_light_pattern(len_light_pattern) coupling_data_provider = CouplingDataProvider(light_pattern, light_pattern_time, None, None) voltage, voltage_time = coupling_data_provider.get_light_data(sampling_duration) try: light_pattern_duration, raw_light_pattern = light_analysis.detect_cycle_by_sequence(voltage, voltage_time) if misc.valid_light_pattern(light_pattern_duration, len_light_pattern): assert voltage.shape[0] == voltage_time.shape[0] length_signal_patterns = list(map(len, raw_light_pattern)) # to select a single light pattern as length references makes no sense, because we don't know # where the light pattern begins and thereby we have to compare multiple light patterns between # light bulb and mobile device: sum(signal patterns) or compare the raw voltage signal #voltage_signal_length = numpy.max(length_signal_patterns) voltage_signal_length = sum(length_signal_patterns) voltage_signal_length = voltage.shape[0] sampling_time_too_small = False else: sampling_duration += scaling_factor * base_sampling_duration except ValueError: sampling_duration += scaling_factor * base_sampling_duration pass light_pattern_length[len_light_pattern] = voltage_signal_length sampling_time[len_light_pattern] = sampling_duration * conversion_ms_to_s # convert s to ms sampling_times[test_round] = sampling_time light_pattern_lengths[test_round] = light_pattern_length # resort after length of signal pattern, append from multiple runs sampling_time_signal_pattern = defaultdict(list) length_light_patterns = defaultdict(list) for results_sampling_time, results_signal_pattern_length in zip(sampling_times.values(), light_pattern_lengths.values()): for light_pattern in results_sampling_time.keys(): sampling_time_signal_pattern[light_pattern].append(results_sampling_time[light_pattern]) length_light_patterns[light_pattern].append(results_signal_pattern_length[light_pattern]) # sort after length of light pattern sampling_time_signal_pattern = OrderedDict(sorted(sampling_time_signal_pattern.items())) DillSerializer(path_sampling_time_signal_patterns).serialize(sampling_time_signal_pattern)
def add_evaluation_data(evaluation_result): logging.info("add evaluation data") params = DillSerializer(path_active_parameters).deserialize() evaluation_data = DillSerializer(path_evaluation_data).deserialize() logging.info(params) evaluation_data[params["num clients"]][params["num reject clients"]] \ [params["len light pattern"]][params["sampling period coupling"]] \ [params["coupling compare method"]][params["coupling similarity threshold"]] \ [params["equalize method"]][params["sampling period localization"]] \ [params["sampling period ml train"]][params["coupling ml classifier"]].append(evaluation_result) DillSerializer(path_evaluation_data).serialize(evaluation_data)
def plot_evaluation_rounds(result_directory, statistics_subdir, min_latency, max_latency, stop_keyword="light_intensity"): latency = dict() for f in os.listdir(result_directory): if stop_keyword not in f: path = result_directory + f vlc_evaluation = DillSerializer(path).deserialize() latency[f] = vlc_evaluation.get_latency() data = latency_statistics(f, vlc_evaluation.get_latency()) misc.log(data, statistics_subdir, f) latency_plot(statistics_subdir, latency, min_latency, max_latency)
def vlc_window_glass(): data_directory = "vlc_window_glass/" result_directory = files.dir_results + data_directory statistics_subdir = data_directory min_throughput = -50 min_error = -0.008 max_throghput, max_error = parameters.get_max_throughput_error( result_directory) directed_led = dict() pervasive_led = dict() for f in os.listdir(result_directory): path = result_directory + f vlc_evaluation = DillSerializer(path).deserialize() if "directed" in f: directed_led[f] = vlc_evaluation elif "pervasive" in f: pervasive_led[f] = vlc_evaluation filename = ["directed_led", "pervasive_led"] for i, led in enumerate([directed_led, pervasive_led]): key_w_glass, vlc_evaluation_w = get_value("with_glass", led) key_wo_glass, vlc_evaluation_wo = get_value("without_glass", led) parameters.throughput_error_plot(statistics_subdir, key_w_glass, vlc_evaluation_w.get_throughput(), vlc_evaluation_w.get_error(), None, min_throughput, max_throghput, min_error, max_error) parameters.throughput_error_plot(statistics_subdir, key_wo_glass, vlc_evaluation_wo.get_throughput(), vlc_evaluation_wo.get_error(), None, min_throughput, max_throghput, min_error, max_error) glass_ratio(statistics_subdir, "ratio_" + filename[i], vlc_evaluation_w, vlc_evaluation_wo)
def get_runtime(result_path): def median(runtimes): return {key: numpy.median(values) for key, values in runtimes.items()} def sort(runtimes): return sorted(runtimes.items(), key=lambda kv: kv[1]) runtimes = DillSerializer(result_path).deserialize() runtime_equalize_methods = defaultdict(list) runtime_similarity_method = defaultdict(list) runtime_equalize_similarity_methods = defaultdict(list) len_light_patterns1, len_light_patterns2, equalize_methods, similarity_methods = misc.get_all_keys(runtimes) for len_light_pattern1 in len_light_patterns1: for len_light_pattern2 in len_light_patterns2: for equalize_method in equalize_methods: for similarity_method in similarity_methods: runtime = runtimes[len_light_pattern1][len_light_pattern2][equalize_method][similarity_method] if len(runtime) > 0: median_runtime = numpy.median(runtime) runtime_equalize_methods[equalize_method].append(median_runtime) runtime_similarity_method[similarity_method].append(median_runtime) key = equalize_method + ":" + similarity_method runtime_equalize_similarity_methods[key].append(median_runtime) runtime_equalize_methods = sort(median(runtime_equalize_methods)) runtime_similarity_method = sort(median(runtime_similarity_method)) runtime_equalize_similarity_methods = sort(median(runtime_equalize_similarity_methods)) return runtime_equalize_methods, runtime_similarity_method, runtime_equalize_similarity_methods
def received_msg(receive_timestamp): global repeat send_timestamp = control_sender.get_send_timestamp() print "send timestamp" print send_timestamp print "-------------------" evaluator.latency(send_timestamp, receive_timestamp) control_sender.stop_cmd() evaluator.print_data() repeat += 1 print "##############################" print "repeat: ", repeat print "##############################" if repeat < rounds: start_latency() else: serializer = DillSerializer(result_path + "latency") serializer.serialize(evaluator)
def start(self): logging.basicConfig(filename="dynamic-coupling-simulation.log", level=logging.DEBUG, format="%(asctime)s %(message)s", datefmt="%I:%M:%S") param_grid = ParameterGrid({ "num users": self.parameter.num_users, "num rooms": self.parameter.num_rooms, "simulation duration": self.parameter.simulation_duration, "coupling ml classifier": self.parameter.coupling_ml_classifiers, "coupling compare method": self.parameter.coupling_compare_methods, "equalize method": self.parameter.equalize_methods, "coupling similarity threshold": self.parameter.coupling_similarity_thresholds, "sampling period coupling": self.parameter.sampling_period_couplings, "sampling period localization": self.parameter.sampling_period_localizations, "sampling period ml train": self.parameter.sampling_period_ml_trains, "coupling frequency": self.parameter.coupling_frequency }) param_len = len(param_grid) for i, params in enumerate(param_grid): logging.info("######### start ##########") logging.info("Param: " + str(i + 1) + "/" + str(param_len)) logging.info("Time: " + str(datetime.datetime.now().time())) logging.info(params) DillSerializer(path_active_parameters).serialize(params) subprocess.check_output([ sys.executable, self.script, str(self.parameter.server_ip), str(self.parameter.server_port), str(params["sampling period coupling"]), params["coupling compare method"], str(params["coupling similarity threshold"]), params["equalize method"], str(params["sampling period ml train"]), self.parameter.path_ml_train_data, params["coupling ml classifier"], self.parameter.path_localization_data, str(self.parameter.localization_room_to_pos), str(params["sampling period localization"]), str(params["coupling frequency"]), str(params["num users"]), str(params["num rooms"]), str(params["simulation duration"]) ]) logging.info("######### end ##########")
def light(x, y): path = files.dir_results + "signal_propagation/light" signal_propagation_light = DillSerializer(path).deserialize() z_error_rate = numpy.ones((x.size, y.size), dtype=numpy.float32) # one - highest error measurement_1 = get_summarized_data([1,2,3,4], signal_propagation_light, True) measurement_2 = get_summarized_data([7,8], signal_propagation_light, True) measurement_3 = get_summarized_data([5,6], signal_propagation_light, True) z_error_rate[measurement_points[60]] = measurement_1 z_error_rate[measurement_points[63]] = measurement_2 z_error_rate[measurement_points[57]] = measurement_3 heatmap(x, y, z_error_rate, "light", bottom_limit=0, top_limit=1)
def timing_light_patterns(path_light_pattern_data, conversion_us_to_ms): light_pattern_data = dict() for len_light_pattern in range(2, 11, 2): # Use all data to detect light pattern light_signal, light_signal_time = light_analysis.load_light_pattern(len_light_pattern) _, light_patterns, light_pattern_times = light_analysis.detect_cycle_by_sequence( light_signal, light_signal_time, timing=True) signal_duration = [(time[-1] - time[0]) for time in light_pattern_times if time[-1] > time[0]] signal_duration = numpy.mean([duration / conversion_us_to_ms for duration in signal_duration]) light_pattern_data[len_light_pattern] = LightPatternData(light_patterns, light_pattern_times, signal_duration) DillSerializer(path_light_pattern_data).serialize(light_pattern_data)
def vlc_ambient_light(): data_directory = "vlc_ambient_light/" result_directory = eval_files.dir_results + data_directory ambient_light = dict() vlc_performance = dict() for root, _, files in os.walk(result_directory): for f in files: #for key in ["low", "mid", "high"]: main_key = os.path.basename(root) if main_key not in ambient_light: ambient_light[main_key] = dict() vlc_performance[main_key] = dict() for key in ["low", "mid", "high"]: if key in f or key.replace(" ", "_") in f: path = root + "/" + f if "light_intensity" in f: ambient_light[main_key][key] = [item["intensity"] for item in misc.read_json(open(path))] else: vlc_performance[main_key][key] = DillSerializer(path).deserialize() print_statistics_ambient_light(data_directory, ambient_light) min_error = -0.03 min_throughput = -50 max_error, max_throughput = get_max_throughput_error(vlc_performance) for led, vlc_results in vlc_performance.items(): error_data = list() throughput_data = list() for key, result in vlc_results.items(): if "high" in key: idx = 2 label = "Light high" marker = "D" color = "r" elif "low" in key: idx = 0 label = "Light low" marker = "s" color = "g" elif "mid" in key: idx = 1 label = "Light medium" marker = "o" color = "b" error_data.insert(idx, PlotDataPoint(label, marker, color, result.get_error())) throughput_data.insert(idx, PlotDataPoint(label, marker, color, result.get_throughput())) difference_plot(data_directory, "throughput_" + led, throughput_data, min_throughput, max_throughput, "Throughput (B/s)", "Evaluation round") difference_plot(data_directory, "error_" + led, error_data, min_error, max_error, "Error rate", "Evaluation round") print_ratio(data_directory, "error_ratio_" + led, error_data) print_ratio(data_directory, "throughput_ratio_" + led, throughput_data)
def start(self): logging.basicConfig(filename="static-coupling-simulation.log", level=logging.DEBUG) param_grid = ParameterGrid({ "num clients": self.parameter.num_clients, "num reject clients": self.parameter.num_reject_clients, "len light pattern": self.parameter.len_light_patterns, "coupling ml classifier": self.parameter.coupling_ml_classifiers, "coupling compare method": self.parameter.coupling_compare_methods, "equalize method": self.parameter.equalize_methods, "coupling similarity threshold": self.parameter.coupling_similarity_thresholds, "sampling period coupling": self.parameter.sampling_period_couplings, "sampling period localization": self.parameter.sampling_period_localizations, "sampling period ml train": self.parameter.sampling_period_ml_trains }) filtered_params = filter_params(param_grid) param_len = len(filtered_params) for i, params in enumerate(filtered_params): logging.info("######### start ##########") logging.info("Param: " + str(i + 1) + "/" + str(param_len)) logging.info("Time: " + str(datetime.datetime.now().time())) logging.info(params) DillSerializer(path_active_parameters).serialize(params) subprocess.check_output([ sys.executable, self.script, str(self.parameter.server_ip), str(self.parameter.server_port), str(params["num clients"]), str(params["num reject clients"]), str(params["len light pattern"]), str(params["sampling period coupling"]), params["coupling compare method"], str(params["coupling similarity threshold"]), params["equalize method"], str(params["sampling period ml train"]), self.parameter.path_ml_train_data, params["coupling ml classifier"], str(params["sampling period localization"]), str(self.parameter.localization_pos_in_area), self.parameter.path_wifi_scans, self.parameter.path_ble_scans ]) logging.info("######### end ##########")
def main(): base_path = "../results/vlc_window_glass_light_power/" with_glass = os.path.join(base_path, "*_with") groundtruth = dict() groundtruth["1w"] = "abcdefghijklmnopqrstuvwxyz0123456789" groundtruth["15w"] = "abcdefghijklmnopqrstuvwxyz0123456789" groundtruth["2w"] = "abcdefghijklmnopqrstuvwxyz0123456789" groundtruth["3w"] = "abcdefghijklmnopqrstuvwxyz0123456789" groundtruth["4w"] = "tbcdefghijklmnopqrstuvwxyz0123456789" for fpath in glob.glob(with_glass): light_power = fpath.split("_")[-2] with_glass_raw_data = DillSerializer(fpath).deserialize().raw_data without_glass_raw_data = DillSerializer(fpath + "out").deserialize().raw_data error1 = overall_bit_error_ratio(with_glass_raw_data, groundtruth[light_power]) error2 = overall_bit_error_ratio(without_glass_raw_data, groundtruth[light_power]) print("light power: ", light_power) print("error with glass: ", error1) print("error without glass: ", error2) print("diff: ", abs(error1-error2)) print("---")
def evaluate_impact_signal_distortion( len_light_patterns, distortion_rates, path_distorted_light_signals, path_distortion_similarity, rounds): distorted_light_signals = defaultdict(list) results_distortion_similarity = nested_dict(3, list) for run in range(rounds): print("round: ", run) for len_light_pattern in len_light_patterns: print("len light pattern:", len_light_pattern) equalize_method = "dummy" client = get_light_signals([len_light_pattern])[0] distorted_light_signals[len_light_pattern].append(client) for distortion_rate in distortion_rates: print("distortion rate: ", distortion_rate) for similarity_method in vector_similarity.similarity_methods: distorted_light_signal = client.get_distorted_light_signal(distortion_rate) similarity = similarity_method(client.signal, distorted_light_signal, equalize_method) if distortion_rate == 0: assert numpy.array_equal(client.signal, distorted_light_signal) assert similarity >= 0.98 results_distortion_similarity[len_light_pattern][distortion_rate][similarity_method.__name__].append(similarity) DillSerializer(path_distortion_similarity).serialize(results_distortion_similarity) DillSerializer(path_distorted_light_signals).serialize(distorted_light_signals)
def process_ml_features(self, X, y, filename, len_light_pattern, tsfresh_selected_features=None): data_exists = False file_exists = os.path.isfile(os.path.join(__location__, filename)) if file_exists: X_features = DillSerializer(os.path.join(__location__, filename)).deserialize() if len_light_pattern in X_features: X_all_features = X_features[len_light_pattern][0] X_selected_features = X_features[len_light_pattern][1] data_exists = True if not data_exists: if tsfresh_selected_features: tsfresh_features = TsFreshFeatures() X_all_features = tsfresh_features.extract(X, y) X_selected_features = tsfresh_features.extract_selected_features( X, tsfresh_selected_features) else: basic_features = BasicFeatures() X_all_features = basic_features.extract(X) X_selected_features = basic_features.extract_selected_features( X) if not file_exists: X_features = { len_light_pattern: (X_all_features, X_selected_features) } else: X_features[len_light_pattern] = (X_all_features, X_selected_features) DillSerializer(os.path.join(__location__, filename)).serialize(X_features) return X_all_features, X_selected_features
def test_light_propagation(led, test_rounds, test_period, test_data, measurement_point): result_file = "../results/signal_propagation/light" object_serializer = DillSerializer(result_file) evaluation_result = object_serializer.deserialize() if os.path.isfile( result_file) else dict() action = ReceiveLightControl.Action.evaluate_morse conn = ReceiveLightControl.Connector.local vlc_metrics = VlcMetrics() while vlc_metrics.get_data_rounds() < test_rounds: print("evaluation round: ", vlc_metrics.get_data_rounds()) light_control = ReceiveLightControl( action, conn=conn, evaluator=vlc_metrics, evaluate_period=test_period, evaluate_template=test_data, evaluate_throughput=True, evaluate_error=True, sampling_interval=led.get_sampling_interval()) light_control.start() vlc_metrics.check_data() evaluation_result[measurement_point] = vlc_metrics object_serializer.serialize(evaluation_result)
def runtime_analysis_tvgl_tsfresh_all(): def scalability_runtime(results): runtimes_tvgl = dict() runtimes_ml_tsfresh_all = dict() for num_clients in sorted(results.keys()): runtime_coupling_tvgl = [result.coupling_tvgl.runtime for result in results[num_clients]] runtime_ml_tsfresh_all = [result.coupling_machine_learning_tsfresh_all.runtime for result in results[num_clients]] runtimes_tvgl[num_clients] = numpy.median(runtime_coupling_tvgl) runtimes_ml_tsfresh_all[num_clients] = numpy.median(runtime_ml_tsfresh_all) return {"tvgl": runtimes_tvgl, "tsfresh all": runtimes_ml_tsfresh_all} print("Runtime analysis of TVGL and tsfresh all features") for path_evaluation_data in glob.glob(os.path.join(__location__, "raw-results", "*-coupling-simulation-tvgl")): scalability_runtimes = None evaluation_data = DillSerializer(path_evaluation_data).deserialize() num_clients, num_reject_clients, len_light_patterns, \ sampling_period_couplings, coupling_compare_methods, \ coupling_similarity_thresholds, equalize_methods, \ sampling_period_localizations, sampling_period_ml_trains, \ coupling_ml_classifiers = misc.get_all_keys(evaluation_data) all_results = list() structured_results = defaultdict(list) for num_client, num_reject_client, len_light_pattern, sampling_period_coupling, \ coupling_compare_method, coupling_similarity_threshold, equalize_method, \ sampling_period_localization, sampling_period_ml_train, coupling_ml_classifier in itertools.product( num_clients, num_reject_clients, len_light_patterns, sampling_period_couplings, coupling_compare_methods, coupling_similarity_thresholds, equalize_methods, sampling_period_localizations, sampling_period_ml_trains, coupling_ml_classifiers): results = evaluation_data[num_client][num_reject_client][len_light_pattern] \ [sampling_period_coupling][coupling_compare_method] \ [coupling_similarity_threshold][equalize_method] \ [sampling_period_localization][sampling_period_ml_train][coupling_ml_classifier] if len(results) > 0: all_results.extend(results) structured_results[num_client].extend(results) scalability_runtimes = scalability_runtime(structured_results) runtime_coupling = [result.runtime_coupling for result in all_results] for identifier, runtimes in scalability_runtimes.items(): print(identifier) abs_decrease = numpy.median(abs(numpy.diff(runtimes.values()))) ratio = (abs_decrease / numpy.median(runtimes.values())) * 100 print("Scalability over num clients {0} s ({1} %)".format(round(abs_decrease,2), round(ratio,2))) ratio_runtime = [runtime / numpy.mean(runtime_coupling) for runtime in runtimes.values()] ratio_runtime = [entry for entry in ratio_runtime if entry < 1] print("Ratio to entire coupling runtime: {0:.2f} %".format(numpy.mean(ratio_runtime)*100))
def plot_light_signals(path_light_pattern_data, conversion_us_to_ms, result_path, plot_format, plot_signal=0): print("plot light signals") light_patterns = DillSerializer(path_light_pattern_data).deserialize() fig, axarr = plt.subplots(len(light_patterns), sharex=True) second_axarr = list() for i, len_light_pattern in enumerate(light_patterns): light_pattern = light_patterns[len_light_pattern] signal = light_pattern.signals[plot_signal] time_signal = light_pattern.time_signals[plot_signal] relative_time_ms = (time_signal - time_signal[0]) / conversion_us_to_ms print("len light pattern:", len_light_pattern) print("Relative time (ms):", relative_time_ms[-1]) axarr[i].plot(relative_time_ms, signal) axarr[i].set_yticks([min(signal), max(signal)]) righty = axarr[i].twinx() righty.set_yticks([0.5]) righty.set_yticklabels([len_light_pattern]) righty.tick_params(axis="both", which="both", length=0) second_axarr.append(righty) axarr[-1].set_xlabel("Signal time (ms)") axarr[-1].set_xticks(numpy.arange(relative_time_ms[-1], step=5)) ax = fig.add_subplot(111, frameon=False) ax.set_yticks([]) ax.set_xticks([]) ax.set_ylabel("Voltage signal (mV)", labelpad=60) ax2 = ax.twinx() ax2.set_yticks([]) ax2.set_xticks([]) ax2.set_ylabel("Length of light pattern", labelpad=50) fig.subplots_adjust(hspace=0.7) plt.savefig(os.path.join(result_path, "random-light-pattern." + plot_format), format=plot_format, bbox_inches='tight') # clear plot to keep only the signal patterns for ax in axarr: ax.axis("off") for ax in second_axarr: ax.axis("off") ax.axis("off") ax2.axis("off") svgfile = os.path.join(result_path, "random-light-pattern.svg") plt.savefig(svgfile, format="svg", bbox_inches='tight') convert_from_svg_to_emf(svgfile) #plt.show() plt.close(fig)
def analysis_simulation(): plot_format = "pdf" coupling_labels = {"filtering": "Content-based filtering", "svm": "SVM"} feature_labels = {"basic all": "Basic features", "tsfresh selected": "Tsfresh selected features", "basic selected": "Basic selected features", "signal similarity": "Light signal", "signal pattern": "Light pattern", "signal pattern duration": "Duration of light pattern", "ble": "BLE features", "wifi": "Wi-Fi features"} result_path = os.path.join(__location__, "results") if not os.path.exists(result_path): os.makedirs(result_path) for path_evaluation_data in glob.glob(os.path.join(__location__, "raw-results", "*-coupling-simulation")): simulation_type = os.path.basename(path_evaluation_data) evaluation_data = DillSerializer(path_evaluation_data).deserialize() if "static" in simulation_type: analysis_static_simulation(evaluation_data, coupling_labels, feature_labels, result_path, plot_format) elif "dynamic" in simulation_type: analysis_dynamic_simulation(evaluation_data, coupling_labels, feature_labels, result_path, plot_format)
def distortion_similarity_analysis(path_distortion_similarity, result_path, plot_format): results = DillSerializer(path_distortion_similarity).deserialize() len_light_patterns, distortion_rates, similarity_methods = misc.get_all_keys(results) print("Similarity threshold by signal distortion") distortion_rate = 0.5 for similarity_method in ["spearman", "pearson", "distance_correlation"]: similarity = list() for len_light_pattern in len_light_patterns: result = results[len_light_pattern][distortion_rate][similarity_method] similarity.extend(result) print(similarity_method, round(numpy.median(similarity), 2)) fig, ax = plt.subplots() markers = itertools.cycle(misc.markers) colors = [plt.cm.tab20(i) for i in numpy.linspace(0, 1, len(vector_similarity.similarity_methods))] for i, similarity_method in enumerate(similarity_methods): distortion = list() similarity_mean = list() for distortion_rate in distortion_rates: mean = list() for len_light_pattern in len_light_patterns: result = results[len_light_pattern][distortion_rate][similarity_method] mean.append(numpy.mean(result)) distortion.append(distortion_rate) similarity_mean.append(numpy.median(mean)) label = similarity_method.replace("_", " ").capitalize().replace("Dtw", "DTW") ax.plot(distortion, similarity_mean, label=label, marker=next(markers), color=colors[i]) ax.plot([0, 1], [1, 0], color="black", linestyle="--") ax.axvline(0.4, color="red", linestyle="--") ax.grid() ax.set_xticks(distortion_rates) ax.set_ylabel("Signal similarity") ax.set_xlabel("Distortion rate") ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) fig.set_figwidth(fig.get_figwidth()*2.5) filename = "distortion-signal-similarity." + plot_format fig.savefig(os.path.join(result_path, filename), plot_format=plot_format, bbox_inches="tight") #plt.show() plt.close(fig)
def main(): if sender: kernel_module.remove(kernel_module.SEND_KERNEL_MODULE) __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) data_len = 5 # 5, 10, 50, 100, 150, 200, 300, 400, 500, 700, 900, 1024 led = Testbed.Pervasive_LED path = os.path.join(__location__, "..", "..", "localvlc", "results", "error_data_len", led.get_name(), "groundtruth_data_len_" + str(data_len)) test_string = ''.join( random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(data_len)) light_sender = LightSender() light_sender.set_data(test_string) light_sender.set_time_base_unit(led.get_time_base_unit()) light_sender.start() serializer = DillSerializer(path) serializer.serialize(test_string) else: leds = ["directed_led", "pervasive_led"] base_path = "../results/error_data_len/" for led in leds: print("led: ", led) path_groundtruth = os.path.join(base_path, led, "groundtruth_data_len_*") for path_groundtruth in glob.glob(path_groundtruth): data_len = path_groundtruth.split("_")[-1] path_raw_data = os.path.join(base_path, led, "data_len_" + data_len) groundtruth = DillSerializer(path_groundtruth).deserialize() raw_data = DillSerializer(path_raw_data).deserialize().raw_data count = 0 result = list() for series in raw_data: for msg in series: count += 1 result.append(bit_error_ratio(msg, groundtruth)) print("data len: ", len(groundtruth)) print("count: ", count) #print("groundtruth: ", groundtruth) #print("raw data: ", raw_data[0][0]) print("bit error ratio: ", numpy.mean(result)) print("max bit error ratio: ", numpy.max(result)) print("---") print("###")
def plot_distorted_light_signals( distortion_rates, path_light_signals, conversion_us_to_ms, result_path, plot_format, plot_round=0): print("plot distorted light signals") light_signals = DillSerializer(path_light_signals).deserialize() len_light_patterns = misc.get_all_keys(light_signals)[0] for len_light_pattern in len_light_patterns: print("len light pattern:", len_light_pattern) fig, axarr = plt.subplots(len(distortion_rates)) for i, distortion_rate in enumerate(distortion_rates): client = light_signals[len_light_pattern][plot_round] light_signal = client.get_distorted_light_signal(distortion_rate) light_signal_time = client.signal_time relative_time_ms = (light_signal_time-light_signal_time[0]) / conversion_us_to_ms axarr[i].plot(relative_time_ms, light_signal) xticks = [] if i+1 < len(distortion_rates) else numpy.arange(relative_time_ms[-1], step=10) axarr[i].set_xticks(xticks) #axarr[i].set_yticks([round(numpy.mean(light_signal))]) axarr[i].yaxis.tick_right() axis = "both" if i+1 < len(distortion_rates) else "y" axarr[i].tick_params(axis=axis, which='both', length=0) axarr[i].set_yticks([numpy.mean(light_signal)]) axarr[i].set_yticklabels([distortion_rate]) axarr[-1].set_xlabel("Signal time (ms)") ax = fig.add_subplot(111, frameon=False) ax.set_yticks([]) ax.set_xticks([]) ax.set_ylabel("Voltage signal (mV)") ax2 = ax.twinx() ax2.set_yticks([]) ax2.set_xticks([]) ax2.set_ylabel("Distortion rate", labelpad=50) filename = "distortion-rate-signal-len-" + str(len_light_pattern) + "." + plot_format filepath = os.path.join(result_path, filename) fig.savefig(filepath, format=plot_format, bbox_inches="tight") #plt.show() plt.close(fig)
def main(): data_directory = "vlc_distance/" result_directory = files.dir_results + data_directory statistics_subdir = data_directory distance_directed_led = dict() distance_pervasive_led = dict() for f in os.listdir(result_directory): if "light_intensity" in f: path = result_directory + f light_intensity = [item["intensity"] for item in misc.read_json(open(path))] data = statistics.get_summary(light_intensity) misc.log(data, statistics_subdir, f) elif "morse" in f: distance = get_distance(f) path = result_directory + f vlc_evaluation = DillSerializer(path).deserialize() if "directed_led" in f: distance_directed_led[distance] = vlc_evaluation elif "pervasive_led" in f: distance_pervasive_led[distance] = vlc_evaluation min_throughput = -50 min_error = -0.008 max_throughput_pervasive, max_error_pervasive = find_max_throughput_error(distance_pervasive_led) max_throughput_directed, max_error_directed = find_max_throughput_error(distance_directed_led) max_throughput = max(max_throughput_pervasive, max_throughput_directed) max_error = max(max_error_pervasive, max_error_directed) filename = "directed_led" throughput_error_plot(statistics_subdir, filename, distance_directed_led, 4.30, min_throughput, max_throughput, min_error, max_error) print_statistics(statistics_subdir, filename, distance_directed_led) filename = "pervasive_led" throughput_error_plot(statistics_subdir, filename, distance_pervasive_led, 1.60, min_throughput, max_throughput, min_error, max_error) print_statistics(statistics_subdir, filename, distance_pervasive_led)
def orientation_performance(result_directory, statistics_subdir): min_throughput = -50 min_error = -0.008 max_throughput, max_error = parameters.get_max_throughput_error( result_directory) for f in os.listdir(result_directory): path = result_directory + f vlc_evaluation = DillSerializer(path).deserialize() parameters.throughput_error_plot( statistics_subdir, f, vlc_evaluation.get_throughput(), vlc_evaluation.get_error(), vlc_evaluation.duration_data, #vlc_evaluation.get_data_period(), min_throughput, max_throughput, min_error, max_error) data = parameters.throughput_error_statistics( f, vlc_evaluation.get_throughput(), vlc_evaluation.get_error()) misc.log(data, statistics_subdir, f)