You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main_script.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from localization import Localization
  2. from plot_results import Plotting
  3. from ART import ART_plotting
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import re
  7. """------------------ A lot of generic plotting (no need to change or read anything here) -------------------------"""
  8. mm = 1000 # this factor will convert everything from metre to millimetre
  9. font = {"family": "serif", "color": "black", "size": 15}
  10. fig = plt.figure(figsize=[24, 9])
  11. ax1 = plt.subplot2grid((1, 2), (0, 1))
  12. ax2 = plt.subplot2grid((1, 2), (0, 0))
  13. plt.subplots_adjust(left=0.07, bottom=0.07, right=0.89, top=0.89, wspace=0.2, hspace=0.2)
  14. # Set Axis
  15. ax1.set_xlim([-0.02*mm, 0.52*mm])
  16. ax1.set_ylim([-0.02*mm, 0.52*mm])
  17. #ax1.set_xticks(np.arange(0.0*mm, 0.55*mm, 0.05*mm))
  18. #ax1.set_yticks(np.arange(0.0*mm, 0.55*mm, 0.05*mm))
  19. ax1.set_xlabel('X-Position of object [mm]', fontdict=font)
  20. ax1.set_ylabel('Y-Position of object [mm]', fontdict=font)
  21. ax1.grid(alpha=0.6)
  22. #for label in ax1.xaxis.get_ticklabels():
  23. # label.set_rotation(90)
  24. ax2.set_ylim([0.1*mm, 0.6*mm])
  25. ax2.set_xlim([0.0*mm, 0.2*mm])
  26. ax2.set_yticks(np.arange(-0.05*mm, 0.65*mm, 0.05*mm))
  27. ax2.set_xticks(np.arange(0.00*mm, 0.20*mm, 0.01*mm))
  28. ax2.set_ylabel('Y-Position of object [mm]', fontdict=font)
  29. ax2.set_xlabel('Z-Position of object [mm]', fontdict=font)
  30. ax2.grid(alpha=0.5)
  31. for label in ax2.xaxis.get_ticklabels():
  32. label.set_rotation(90)
  33. # Set Titles
  34. ax1.set_title("XY", fontdict=font)
  35. ax2.set_title("ZY", fontdict=font)
  36. # CONFIGURE X, Y PLOT
  37. # Add the legend for different data plot lines
  38. # data_legend = plt.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.)
  39. # data_legend = plt.gca().add_artist(data_legend)
  40. # Indicate Exciter
  41. Exciter = ax1.vlines(x=0.0*mm, ymin=0.0*mm, ymax=0.5*mm, linestyles="-", colors="#5b9bd5", label="Exciter", linewidth=6)
  42. ax1.vlines(x=0.5*mm, ymin=0.0*mm, ymax=0.5*mm, linestyles="-", colors="#5b9bd5", linewidth=6)
  43. ax1.hlines(y=0.0*mm, xmin=0.0*mm, xmax=0.5*mm, linestyles="-", colors="#5b9bd5", linewidth=6)
  44. ax1.hlines(y=0.5*mm, xmin=0.0*mm, xmax=0.5*mm, linestyles="-", colors="#5b9bd5", linewidth=6)
  45. # Indicate Localization Area
  46. # LocArea = ax1.vlines(x=0.045, ymin=0.045, ymax=0.495, linestyles="dashed", colors="#7f7f7f",
  47. # label="Loc.\nArea")
  48. # ax1.vlines(x=0.495, ymin=0.045, ymax=0.495, linestyles="dashed", colors="#7f7f7f")
  49. # ax1.hlines(y=0.045, xmin=0.045, xmax=0.495, linestyles="dashed", colors="#7f7f7f")
  50. # ax1.hlines(y=0.495, xmin=0.045, xmax=0.495, linestyles="dashed", colors="#7f7f7f")
  51. # SHOW ANTENNAS
  52. AntPosX = [0*mm, 0*mm, 0.13*mm, 0.375*mm, 0.5*mm, 0.5*mm, 0.125*mm, 0.38*mm] # Safing each Antennas Position in a list -> e.g. : X position of frame ant 1, X pos of frame ant 2, x pos of frame ant 3, ...
  53. AntPosY = [0.125*mm, 0.375*mm, 0.5*mm, 0.5*mm, 0.125*mm, 0.375*mm, 0*mm, 0*mm]
  54. AntOrientY = [270, 270, 180, 180, 90, 90, 0,
  55. 0] # analog as AntPosX,Y , just with Orientation degrees (direction in z-axis
  56. # Select Antenna plotting options here
  57. patchAlpha = 1.0
  58. # ITERATE THROUGH CMAP-------------
  59. n = 8
  60. # color = iter(cm.jet(np.linspace(0, 1, n)))
  61. # ----------------------------
  62. for i in range(8): # iterate through antennas
  63. x = AntPosX[i] # get x and y pos of antennas
  64. y = AntPosY[i]
  65. # c = next(color) # Comment in for coloured antennas
  66. c = "k" # black antennas
  67. if i == 7: # only print one label for the Antennas (otherwise its 8 times in legend)
  68. label = label = str("Receiving\nCoils")
  69. else:
  70. label = ""
  71. if AntOrientY[i] == 0:
  72. # identify antennas orientation
  73. patchX = float(x - 0.033*mm / 2)
  74. patchY = float(y - 0.033*mm / 2)
  75. rect = plt.Rectangle((patchX, patchY), 0.033*mm, 0.033*mm,
  76. color=c, alpha=patchAlpha, label=label)
  77. ax1.add_patch(rect)
  78. if AntOrientY[i] == 180:
  79. patchX = float(x - 0.033*mm / 2)
  80. patchY = float(y - 0.033*mm / 2)
  81. rect = plt.Rectangle((patchX, patchY), 0.033*mm, 0.033*mm,
  82. color=c, alpha=patchAlpha)
  83. ax1.add_patch(rect)
  84. if AntOrientY[i] == 90:
  85. patchX = float(x - 0.033*mm / 2)
  86. patchY = float(y - 0.033*mm / 2)
  87. rect = plt.Rectangle((patchX, patchY), 0.033*mm, 0.033*mm,
  88. color=c, alpha=patchAlpha)
  89. ax1.add_patch(rect)
  90. if AntOrientY[i] == 270:
  91. patchX = float(x - 0.033*mm / 2)
  92. patchY = float(y - 0.033*mm / 2)
  93. rect = plt.Rectangle((patchX, patchY), 0.033*mm, 0.033*mm,
  94. color=c, alpha=patchAlpha)
  95. ax1.add_patch(rect)
  96. """------------------- End of generic plotting stuff ----------------------------------------------------------------"""
  97. def plot_art(recording_number):
  98. """
  99. :param recording_name: int, Choose which recording to print e.g. Recording_!1! , Recording_!2!, etc.
  100. """
  101. """--------------- Open, format and save ART positions from text files---------------------------------------"""
  102. # Opening a recording
  103. art_recording = open('ART_IndLoc precision tests/Recording_' + str(recording_number) + '.txt', 'r')
  104. art_recording = art_recording.read()
  105. # Formatting a recording
  106. art_recording = re.split(' |\n', art_recording) # split string at whitespace and linebreak
  107. art_recording = np.array(art_recording) # list -> np.array
  108. # Count the total number of recorded positions
  109. total_pos_counter = 0
  110. for i in art_recording:
  111. if i == '6d': # this is the flag marking a 6d position (Syntax of the ART guys in the txt files)
  112. total_pos_counter += 1
  113. # Create an empty array which will store the ART measurement infos
  114. art_info = np.zeros((total_pos_counter, 7)) # [t, x, y, z, alpha, beta, gamma]
  115. # Fill the empty numpy array with the information's of the art_recording
  116. pos_counter = 0
  117. for i in range(len(art_recording)-9):
  118. if art_recording[i] == 'ts': # following entry is timestamp
  119. art_info[pos_counter] = float(art_recording[i + 1])
  120. if art_recording[i] == '6d': # following entries are x,y,z,alpha,beta,gamma
  121. art_info[pos_counter, 1] = float(art_recording[i + 3].split('[')[1]) # x (had to delete some excess text)
  122. art_info[pos_counter, 2] = float(art_recording[i + 4]) # y
  123. art_info[pos_counter, 3] = float(art_recording[i + 5]) # z
  124. art_info[pos_counter, 4] = float(art_recording[i + 6]) # alpha
  125. art_info[pos_counter, 5] = float(art_recording[i + 7]) # beta
  126. art_info[pos_counter, 6] = float(
  127. art_recording[i + 8].split(']')[0]) # gamma (had to delete some excess text
  128. pos_counter += 1
  129. ax1.plot(art_info[:, 1], art_info[:, 2], color="green", label="ART System", linewidth=1)
  130. print("x=", art_info[:, 1])
  131. print("y=", art_info[:, 2])
  132. # Constructors for other classes we need for plotting and localizing
  133. localization = Localization()
  134. plotting = Plotting()
  135. # Decide what scaling_factors and Recording
  136. scale_factors = np.arange(0.001, 0.0035, 0.0005)
  137. #scale_factors = [1]
  138. localization.k_nearest = 15
  139. Recording = 2 # which ART Recording to pick
  140. for sf in scale_factors:
  141. print("sf =", sf)
  142. localization.scale_factor = sf
  143. # Localization calculation
  144. localization.localize_all_samples("ART Recordings/Recording_" + str(Recording),"Recording_" + str(Recording)+"_sf=" + str(sf)) # NORMAL LOCALIZATION
  145. # localization.localize_averaged_samples(filename_localize_in.get(), filename_localize_out.get()) #AVERAGED LOCALIZATION
  146. # Load IndLoc positions
  147. positions = np.load("calculated_positions/Recording_" + str(Recording)+"_sf=" + str(sf) + ".npy")
  148. positions = positions * mm # convert from m to mm
  149. # Plot IndLoc Positions
  150. ax1.plot(positions[:, 0], positions[:, 1], label="sf = " + str(sf), linewidth=1)
  151. ax2.plot(positions[:, 2], positions[:, 1], label="sf = " + str(sf), linewidth=1)
  152. plot_art(Recording)
  153. ax1.legend(bbox_to_anchor=(1.01, 1.00), loc=2, borderaxespad=0.)
  154. plt.show()