From b3bfc27a09dfdf0d781379bbffbfb94ad5a9ff0b Mon Sep 17 00:00:00 2001 From: KostyaYamshanov Date: Sat, 5 Nov 2022 22:55:36 +0700 Subject: [PATCH 1/3] added script for plots --- utils/plotState.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 utils/plotState.py diff --git a/utils/plotState.py b/utils/plotState.py new file mode 100755 index 0000000..ac8d433 --- /dev/null +++ b/utils/plotState.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +import os +import argparse +import matplotlib.pyplot as plt + + +FONT_SIZE = 12 + +def parse_trajectory(path): + """ + + """ + + data = {"t": [], "x": [], "y": [], "yaw": []} + with open(path, 'r') as f: + keys = f.readline().rstrip().split(' ') # get keys from first line, remove '/n' + for line in f.readlines(): + line = line[0:-1].split(' ') + for i in range(len(keys)): + data[keys[i]].append(float(line[i])) + + return data + + +def plot_data(data, output): + """ + + """ + + # Initialise the subplot function using number of rows and columns + figure, axis = plt.subplots(2, 2) + + # For X(t) function + axis[0, 0].plot(data['t'], data['x']) + axis[0, 0].set_title("x(t)") + + # For Y(t) + axis[0, 1].plot(data['t'], data['y']) + axis[0, 1].set_title("y(t)") + + # For YAW(t) + axis[1, 0].plot(data['t'], data['yaw']) + axis[1, 0].set_title("yaw(t)") + + # For Y(X) + axis[1, 1].plot(data['x'], data['y']) + axis[1, 1].set_title("y(x)") + + # Combine all the operations and display + plt.show() + + +def main(): + """ """ + + plt.rcParams['font.size'] = '12' + + parser = argparse.ArgumentParser() + parser.add_argument('-input', action='store', dest='input', + required=True, help='absolute path to the data') + + parser.add_argument('-output', action='store', dest='output', + required=False, default="", + help="absolute path to the output folder to store images") + + args = parser.parse_args() + + print("input_path: ",args.input) + + data = parse_trajectory(args.input) + + plot_data(data, args.output) + + +if __name__ == "__main__": + main() From 53338f2641cde72d793ab3089a6de4b418ae7fd9 Mon Sep 17 00:00:00 2001 From: KostyaYamshanov Date: Sat, 5 Nov 2022 23:41:42 +0700 Subject: [PATCH 2/3] added plot saving --- utils/plotState.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/utils/plotState.py b/utils/plotState.py index ac8d433..b1f33b6 100755 --- a/utils/plotState.py +++ b/utils/plotState.py @@ -4,7 +4,7 @@ import matplotlib.pyplot as plt -FONT_SIZE = 12 +FONT_SIZE = 14 def parse_trajectory(path): """ @@ -21,8 +21,25 @@ def parse_trajectory(path): return data +def save_plot(path): + """Saves graph to the output pkg""" -def plot_data(data, output): + folder_path = path + name = "graph" + + print("folder_path: ", folder_path) + print("name: ", name) + + pwd = os.getcwd() + os.chdir(os.path.expanduser('~')) + if not os.path.exists(folder_path): + os.makedirs(folder_path) + + os.chdir(folder_path) + plt.savefig('{}.png'.format(name)) + os.chdir(pwd) + +def plot_data(data): """ """ @@ -46,14 +63,15 @@ def plot_data(data, output): axis[1, 1].plot(data['x'], data['y']) axis[1, 1].set_title("y(x)") + # Combine all the operations and display - plt.show() + # plt.show() def main(): """ """ - plt.rcParams['font.size'] = '12' + plt.rcParams['font.size'] = FONT_SIZE parser = argparse.ArgumentParser() parser.add_argument('-input', action='store', dest='input', @@ -69,8 +87,12 @@ def main(): data = parse_trajectory(args.input) - plot_data(data, args.output) + plot_data(data) + + if(args.output): + save_plot(args.output) + plt.show() if __name__ == "__main__": main() From 8e287001d064aa3e0ffa753d8823a4bfb7523853 Mon Sep 17 00:00:00 2001 From: KostyaYamshanov Date: Sun, 6 Nov 2022 00:15:26 +0700 Subject: [PATCH 3/3] kostya wip, fixed saving --- utils/plotState.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/utils/plotState.py b/utils/plotState.py index b1f33b6..932ea60 100755 --- a/utils/plotState.py +++ b/utils/plotState.py @@ -21,11 +21,10 @@ def parse_trajectory(path): return data -def save_plot(path): +def save_plot(path, name): """Saves graph to the output pkg""" folder_path = path - name = "graph" print("folder_path: ", folder_path) print("name: ", name) @@ -34,7 +33,7 @@ def save_plot(path): os.chdir(os.path.expanduser('~')) if not os.path.exists(folder_path): os.makedirs(folder_path) - + os.chdir(folder_path) plt.savefig('{}.png'.format(name)) os.chdir(pwd) @@ -64,9 +63,6 @@ def plot_data(data): axis[1, 1].set_title("y(x)") - # Combine all the operations and display - # plt.show() - def main(): """ """ @@ -82,15 +78,16 @@ def main(): help="absolute path to the output folder to store images") args = parser.parse_args() + input_path = args.input + print("input_path: ", input_path) - print("input_path: ",args.input) - - data = parse_trajectory(args.input) + data = parse_trajectory(input_path) plot_data(data) if(args.output): - save_plot(args.output) + name = "plot_" + input_path.split('/')[-1].split('.')[0] + save_plot(args.output, name) plt.show()