My Project
plotPadSingle.py
Go to the documentation of this file.
1 import os
2 import sys
3 import numpy as np
4 from matplotlib import pyplot as plt
5 
6 import matplotlib as mpl
7 
8 mpl.rcParams['svg.fonttype'] = "none"
9 mpl.rcParams['font.size'] = 16
10 
11 
12 mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
13 mpl.rc('text', usetex=True)
14 
15 import yaml
16 
17 try:
18  from yaml import CLoader as Loader, CDumper as Dumper
19 except ImportError:
20  from yaml import Loader, Dumper
21 
22 class PlotPad:
23 
24  def __init__(self, run_name = "", numeric_method = "_midpoint_"):
25  self.loadFiles(run_name, numeric_method)
26  self.setUpPlot()
27 
28 
29  def loadFiles(self, run_name, numeric_method):
30  self.results_path = os.path.dirname(os.path.realpath(__file__)) + "/../../../results/"
31 
32  results_folder = "/results/"
33 
34  stream = open(self.results_path + run_name + "/doc/yaml/parameters.yaml", 'r')
35  self.run_parameters = yaml.load(stream, Loader = Loader)
36 
37  shared_names = self.run_parameters["Parameters"]["file_name"]
38 
39  self.pad_position = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "pad_position.csv", delimiter = ',')
40  if(not only_position):
41  self.pad_velocity = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "pad_velocity.csv", delimiter = ',')
42  self.pad_friction = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "pad_friction.csv", delimiter = ',')
43 
44  def setUpPlot(self):
45  self.one_block_boolean = False
46 
47  self.linewidth = 0.9
48 
49  start = 0
50  stop = self.run_parameters["Parameters"]["max_time"]
51 
52  save_interval = 1
53 
54  if("-i" in sys.argv):
55  try:
56  save_interval = self.run_parameters["Parameters"]["save_interval_dt"]
57  except ValueError:
58  print("Look like this run does not contain a \"save_interval_dt\" parameter.")
59 
60  number_of_points = len(self.pad_position)
61 
62  self.x = np.linspace(start = start, stop = stop, num = number_of_points)
63 
64  self.percent_interval = int(1.0*number_of_points)
65  print(self.percent_interval)
66 
67  def plot_pad_position(self):
68  plt.plot(self.x[0:self.percent_interval], self.pad_position[0:self.percent_interval], label = "Pad position", linewidth = self.linewidth)
69 
70  plt.ylabel("$y$")
71  plt.xlabel("$t$")
72  plt.legend()
73  plt.show()
74 
75  def plot_pad_velocity(self):
76  plt.plot(self.x, self.pad_velocity, label = "Pad velocity", linewidth = self.linewidth - 0.2)
77 
78  plt.xlabel("$y$")
79  plt.ylabel("$t$")
80  plt.legend()
81  plt.show()
82 
83  def plot_pad_friction(self):
84  plt.plot(self.x, self.pad_friction, label = "Pad friction", linewidth = self.linewidth - 0.2)
85 
86  plt.xlabel("$y$")
87  plt.ylabel("$t$")
88  plt.legend()
89  plt.show()
90 
92  plt.plot(self.pad_position, self.pad_velocity)
93  plt.show()
94 
95  def plot_segments(self):
96  if('-s' in sys.argv):
97  try:
98  n_segments = int(sys.argv[-1])
99  print("Number of intervals: ", n_segments)
100  except ValueError:
101  print("Please enter the number of segments as the last argument when using \'-s\'")
102 
103  n_data_points_in_segment = int(len(self.x)/n_segments)
104 
105  for i in range(n_segments):
106  start_index = i * n_data_points_in_segment
107  end_index = (i + 1) * n_data_points_in_segment
108 
109  if(i == n_segments - 1):
110  end_index = -1
111 
112  plt.plot(self.x[start_index:end_index], self.pad_position[start_index:end_index], label = "Segment " + str(i))
113  plt.legend
114  plt.show()
115 
116 
117 
118 
119 
120 def plotastic():
121  if(len(sys.argv) > 2):
122  plot_pad = PlotPad(sys.argv[1], sys.argv[2])
123  elif(len(sys.argv) == 2):
124  plot_pad = PlotPad(sys.argv[1])
125  else:
126  print("This will probably not work, have you remembered to give input to the command?")
127  plot_pad = PlotPad()
128 
129  plot_pad.plot_pad_position()
130  plot_pad.plot_segments()
131  if(not only_position):
132  plot_pad.plot_pad_velocity()
133  plot_pad.plot_pad_friction()
134  plot_pad.plot_pad_position_vers_velocity()
135 only_position = True
136 
137 if __name__ == "__main__":
138  plotastic()
def __init__(self, run_name="", numeric_method="_midpoint_")
def plot_pad_friction(self)
def plot_pad_velocity(self)
def plot_pad_position_vers_velocity(self)
def plot_pad_position(self)
def loadFiles(self, run_name, numeric_method)