From 6222fd447ae297ba2a8b504587159b5ff266b93d Mon Sep 17 00:00:00 2001 From: Sebastian Merz <62221161+MerzSebastian@users.noreply.github.com> Date: Sat, 4 Dec 2021 01:18:57 +0100 Subject: [PATCH 1/3] Update decode.py --- src/decode.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/decode.py b/src/decode.py index 048b1a9..d967844 100644 --- a/src/decode.py +++ b/src/decode.py @@ -17,6 +17,10 @@ start_time = 0 last_time = 0 +logger = True +def log(log): + if logger: + print(log) def calculate_error(current_pulse_length, single_pulse_length, pulse_count): return [(current_pulse_length % (pulse_count * single_pulse_length)) for _ in range(pulse_count)] #maybe calculating error outside of foreach is more efficient ? i dont know. should be fine @@ -62,7 +66,7 @@ def write_output(path, data): def time_round(): global last_time - print("Elapsed time:", round((time.perf_counter() - last_time)*1000), "ms") + log("Elapsed time: " + str(round((time.perf_counter() - last_time)*1000)) + "ms") last_time = time.perf_counter() @@ -75,46 +79,46 @@ def time_start(): def time_overall(): global start_time - print("Overall elapsed time:", round((time.perf_counter() - start_time) * 1000), "ms") + log("Overall elapsed time: " + str(round((time.perf_counter() - start_time) * 1000)) + "ms") def calculate(file): - print("# # # # # # # # # # # # # # # # # # # # # #") - print("Reading and converting binary data", file, "...") + log("# # # # # # # # # # # # # # # # # # # # # #") + log("Reading and converting binary data " + file + "...") raw_data = bin(file).convert()[0] #doesnt work for multiple channels, have to test if channel 2, 3 and 4 works. only testet first i think x, y = reformat_raw_data(raw_data) global ui_raw_data ui_raw_data = [x, y] time_round() - print("Calculating threshold...") + log("Calculating threshold...") threshold = max(y) / 2 - print("Threshold:", threshold, "Volt") + log("Threshold: " + str(threshold) + "Volt") time_round() - print("Correct time offset...") + log("Correct time offset...") y = [(True if i > threshold else False) for i in y] time_round() - print("Removing doubled data points...") + log("Removing doubled data points...") x, y = remove_redundant_data_points(x, y) time_round() - print("Shifting time value so it starts at 0...") + log("Shifting time value so it starts at 0...") x = [x[i] - min(x) for i in range(len(x))] time_round() global ui_cleaned_data ui_cleaned_data = [x, y] - print("Getting shortest pulse length...") + log("Getting shortest pulse length...") single_pulse_length = min([abs(x[i] - x[i + 2]) for i in range(len(y) - 2)]) - print("Shortest pulse length:", format(single_pulse_length, '.12f')) + log("Shortest pulse length: " + format(single_pulse_length, '.12f')) time_round() - print("Decoding data...") + log("Decoding data...") decoded_data, error = decode_normalized_data(x, y, single_pulse_length) decoded_string_data = ''.join(str(val) for val in decoded_data) - print("Decoded data:", decoded_string_data) + log("Decoded data: " + decoded_string_data) time_round() return { @@ -160,12 +164,12 @@ def main(): for file in file_paths: result[os.path.basename(file)] = calculate(file) - print("# # # # # # # # # # # # # # # # # # # # # #") - print("Writing output file...") + log("# # # # # # # # # # # # # # # # # # # # # #") + log("Writing output file...") path = write_output(args.output if args.output else "./", result) - print("Written file:", path) + log("Written file: " + path) time_overall() - print("# # # # # # # # # # # # # # # # # # # # # #") + log("# # # # # # # # # # # # # # # # # # # # # #") if len(file_paths) is 1 and args.graph: ui.showTwoGraphs(ui_raw_data, ui_cleaned_data) From d61b9726df7dcb3a3aadeb26348bf8ed9c7a8604 Mon Sep 17 00:00:00 2001 From: Sebastian Merz <62221161+MerzSebastian@users.noreply.github.com> Date: Sun, 5 Dec 2021 14:30:42 +0100 Subject: [PATCH 2/3] adding Logger class, user PRocesses with multiple files --- src/bin.py | 21 +++++++------ src/decode.py | 84 +++++++++++++++++++++++++++++++-------------------- src/logger.py | 14 +++++++++ 3 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 src/logger.py diff --git a/src/bin.py b/src/bin.py index 82a0358..4eba9bd 100644 --- a/src/bin.py +++ b/src/bin.py @@ -11,8 +11,9 @@ class bin: magnitudes = [10e-24, 10e-21, 10e-18, 10e-15, 10e-12, 10e-9, 10e-6, 10e-3, 1, 10e3, 10e6, 10e9, 10e12, 10e15] file = '' - def __init__(self, file): + def __init__(self, file, logger): self.file = file + self.logger = logger def data_to_unit(self, data): result = [] @@ -46,15 +47,15 @@ def convert(self): data = file_data[2048:] f.close() - print('channel_states: ', channel_states) - print('channel_volt_division: ', channel_volt_division) - print('channel_offset: ', channel_offset) - #print('digital_states: ', digital_states) - print('horizontal_list: ', horizontal_list) - print('wave_length: ', wave_length) - print('sample_rate: ', sample_rate) - #print('digital_wave_length: ', digital_wave_length) - #print('digital_sample_rate: ', digital_sample_rate) + self.logger.log('channel_states: ' + str(channel_states)) + self.logger.log('channel_volt_division: ' + str(channel_volt_division)) + self.logger.log('channel_offset: ' + str(channel_offset)) + #self.logger.log('digital_states: ' + str(digital_states)) + self.logger.log('horizontal_list: ' + str(horizontal_list)) + self.logger.log('wave_length: ' + str(wave_length)) + self.logger.log('sample_rate: ' + str(sample_rate)) + #print('digital_wave_length: ' + str(digital_wave_length)) + #print('digital_sample_rate: ' + str(digital_sample_rate)) block_length = (1000000 if len(data) >= 14e6 or wave_length >= 1E6 else wave_length) block_number = int(wave_length // block_length) diff --git a/src/decode.py b/src/decode.py index d967844..e2a6d25 100644 --- a/src/decode.py +++ b/src/decode.py @@ -4,6 +4,7 @@ # Parameter: decode.py # Example: decode.py ../test/samples ./ +from multiprocessing import Process, Manager import time import os as os from bin import bin @@ -13,14 +14,12 @@ import json import statistics import argparse +from logger import Logger start_time = 0 last_time = 0 +logger = Logger() -logger = True -def log(log): - if logger: - print(log) def calculate_error(current_pulse_length, single_pulse_length, pulse_count): return [(current_pulse_length % (pulse_count * single_pulse_length)) for _ in range(pulse_count)] #maybe calculating error outside of foreach is more efficient ? i dont know. should be fine @@ -64,9 +63,9 @@ def write_output(path, data): return name -def time_round(): +def time_round(logger): global last_time - log("Elapsed time: " + str(round((time.perf_counter() - last_time)*1000)) + "ms") + logger.log("Elapsed time: " + str(round((time.perf_counter() - last_time)*1000)) + "ms") last_time = time.perf_counter() @@ -79,47 +78,47 @@ def time_start(): def time_overall(): global start_time - log("Overall elapsed time: " + str(round((time.perf_counter() - start_time) * 1000)) + "ms") + print("Overall elapsed time: " + str(round((time.perf_counter() - start_time) * 1000)) + "ms") -def calculate(file): - log("# # # # # # # # # # # # # # # # # # # # # #") - log("Reading and converting binary data " + file + "...") - raw_data = bin(file).convert()[0] #doesnt work for multiple channels, have to test if channel 2, 3 and 4 works. only testet first i think +def calculate(file, logger): + logger.log("# # # # # # # # # # # # # # # # # # # # # #") + logger.log("Reading and converting binary data " + file + "...") + raw_data = bin(file, logger).convert()[0] #doesnt work for multiple channels, have to test if channel 2, 3 and 4 works. only testet first i think x, y = reformat_raw_data(raw_data) global ui_raw_data ui_raw_data = [x, y] - time_round() + time_round(logger) - log("Calculating threshold...") + logger.log("Calculating threshold...") threshold = max(y) / 2 - log("Threshold: " + str(threshold) + "Volt") - time_round() + logger.log("Threshold: " + str(threshold) + "Volt") + time_round(logger) - log("Correct time offset...") + logger.log("Correct time offset...") y = [(True if i > threshold else False) for i in y] - time_round() + time_round(logger) - log("Removing doubled data points...") + logger.log("Removing doubled data points...") x, y = remove_redundant_data_points(x, y) - time_round() + time_round(logger) - log("Shifting time value so it starts at 0...") + logger.log("Shifting time value so it starts at 0...") x = [x[i] - min(x) for i in range(len(x))] - time_round() + time_round(logger) global ui_cleaned_data ui_cleaned_data = [x, y] - log("Getting shortest pulse length...") + logger.log("Getting shortest pulse length...") single_pulse_length = min([abs(x[i] - x[i + 2]) for i in range(len(y) - 2)]) - log("Shortest pulse length: " + format(single_pulse_length, '.12f')) - time_round() + logger.log("Shortest pulse length: " + format(single_pulse_length, '.12f')) + time_round(logger) - log("Decoding data...") + logger.log("Decoding data...") decoded_data, error = decode_normalized_data(x, y, single_pulse_length) decoded_string_data = ''.join(str(val) for val in decoded_data) - log("Decoded data: " + decoded_string_data) - time_round() + logger.log("Decoded data: " + decoded_string_data) + time_round(logger) return { "date": datetime.now().strftime("%d.%m.%Y %H:%M:%S"), @@ -150,6 +149,10 @@ def getArguments(): return parser.parse_args() +def calculateAsnyc(file, return_dict, logger): + return_dict[os.path.basename(file)] = calculate(file, logger) + + def main(): time_start() args = getArguments() @@ -160,16 +163,31 @@ def main(): #Get all files file_paths = ([os.path.join(args.input, f) for f in os.listdir(args.input) if f.endswith('.bin')] if os.path.isdir(args.input) else [args.input]) + result = {} - for file in file_paths: - result[os.path.basename(file)] = calculate(file) - log("# # # # # # # # # # # # # # # # # # # # # #") - log("Writing output file...") + # Turn off logging and run multiple threads when there is more then 1 file + if len(file_paths) > 1: + logger.off() + threads = [] + tempResult = Manager().dict() + for file in file_paths: + t = Process(target=calculateAsnyc, args=(file, tempResult, logger)) + t.start() + threads.append(t) + for thread in threads: + thread.join() + result.update(tempResult) + else: + for file in file_paths: + result[os.path.basename(file)] = calculate(file, logger) + + logger.log("# # # # # # # # # # # # # # # # # # # # # #") + logger.log("Writing output file...") path = write_output(args.output if args.output else "./", result) - log("Written file: " + path) + logger.log("Written file: " + path) time_overall() - log("# # # # # # # # # # # # # # # # # # # # # #") + logger.log("# # # # # # # # # # # # # # # # # # # # # #") if len(file_paths) is 1 and args.graph: ui.showTwoGraphs(ui_raw_data, ui_cleaned_data) diff --git a/src/logger.py b/src/logger.py new file mode 100644 index 0000000..c7c8b14 --- /dev/null +++ b/src/logger.py @@ -0,0 +1,14 @@ +class Logger: + + def __init__(self): + self.loggerActive = True + + def log(self, value): + if self.loggerActive: + print(value) + + def on(self): + self.loggerActive = True + + def off(self): + self.loggerActive = False From 09dd6c09cbc9989fc2e3574f9eae8bfd57f7ae6c Mon Sep 17 00:00:00 2001 From: Sebastian Merz <62221161+MerzSebastian@users.noreply.github.com> Date: Sun, 5 Dec 2021 14:40:59 +0100 Subject: [PATCH 3/3] Update test.py --- test/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.py b/test/test.py index 6dca37a..0f37b9e 100644 --- a/test/test.py +++ b/test/test.py @@ -19,7 +19,7 @@ mainPath = args.script if args.script else mainPath inputPath = os.path.join(basePath, 'test\samples') outputPath = os.path.join(basePath, 'test\output') -startCommand = ' '.join([mainPath, "-i=" + inputPath, "-o=" + outputPath]) +startCommand = ' '.join([mainPath, '-i="' + inputPath + '"', '-o="' + outputPath + '"']) #webfetchMainPath = os.path.join(basePath, 'src\webfetch.py') #webfetchOutputPath = os.path.join(basePath, 'test\webfetch_output')