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.

plot_precision_tests.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. import math
  8. mm = 1000 # millimetre
  9. localization = Localization()
  10. plotting = Plotting()
  11. fig = plt.figure(figsize=[24, 9])
  12. xy_subplot = plt.subplot2grid((1, 2), (0, 1))
  13. yz_subplot = plt.subplot2grid((1, 2), (0, 0))
  14. # Load all the IndLoc Recordings
  15. center = np.load("recorded_data/center.npy")[1:, :, :]
  16. off_center = np.load("recorded_data/off_center.npy")[1:, :, :]
  17. horizontal = np.load("recorded_data/horizontal.npy")[1:, :, :]
  18. diagonal = np.load("recorded_data/diagonal.npy")[1:, :, :]
  19. zigzag_1 = np.load("recorded_data/zigzag_1.npy")
  20. zigzag_2 = np.load("recorded_data/zigzag_2.npy")
  21. wave_1 = np.load("recorded_data/wave_1.npy")
  22. wave_2 = np.load("recorded_data/wave_2.npy")
  23. spiral_1 = np.load("recorded_data/spiral_1.npy")
  24. half_current_horizontal = np.load("recorded_data/half_current_horizontal.npy")[1:, :, :]
  25. half_current_diagonal = np.load("recorded_data/half_current_diagonal.npy")[1:, :, :]
  26. double_current_horizontal = np.load("recorded_data/double_current_horizontal.npy")[1:, :, :]
  27. double_current_diagonal = np.load("recorded_data/half_current_diagonal.npy")[1:, :, :]
  28. z_axis = np.load("recorded_data/z_axis.npy")[1:, :, :]
  29. z_axis_off_center = np.load("recorded_data/z_axis_off_center.npy")[1:, :, :]
  30. Three_D_movement_1 = np.load("recorded_data/3D_movement_1.npy")
  31. Three_D_movement_2 = np.load("recorded_data/3D_movement_2.npy")
  32. Three_D_zigzag_1 = np.load("recorded_data/3D_zigzag_1.npy")
  33. Three_D_zigzag_2 = np.load("recorded_data/3D_zigzag_2.npy")
  34. z_axis_moving_1 = np.load("recorded_data/z_axis_moving_1.npy")
  35. # z_axis_moving_2 = np.load("recorded_data/z_axis_moving_2.npy") # Recording missing?
  36. z_wave_1 = np.load("recorded_data/z_wave_1.npy")
  37. z_wave_2 = np.load("recorded_data/z_wave_2.npy")
  38. recordings_dict = {"center": center, # shape: (26, 2000, 19) = [P1-P25, 2k samples, fv]
  39. "off_center": off_center,
  40. "horizontal": horizontal,
  41. "diagonal": diagonal,
  42. "horizontal_and_diagonal": np.append(horizontal, diagonal, axis=0),
  43. "zigzag_1": zigzag_1,
  44. "zigzag_2": zigzag_2,
  45. "wave_1": wave_1,
  46. "wave_2": wave_2,
  47. "spiral_1": spiral_1,
  48. "half_current_horizontal": half_current_horizontal,
  49. "half_current_diagonal": half_current_diagonal,
  50. "double_current_horizontal": double_current_horizontal,
  51. "double_current_diagonal": double_current_diagonal,
  52. "z_axis": z_axis,
  53. "z_axis_off_center": z_axis_off_center,
  54. "Three_D_movement_1": Three_D_movement_1,
  55. "Three_D_movement_2": Three_D_movement_2,
  56. "Three_D_zigzag_1": Three_D_zigzag_1,
  57. "Three_D_zigzag_2": Three_D_zigzag_2,
  58. "z_axis_moving_1": z_axis_moving_1,
  59. "z_wave_1": z_wave_1,
  60. "z_wave_2": z_wave_2}
  61. # this only takes the first sample of each recording
  62. recording_dict_fs = {"center": center[:, 0, :],
  63. "off_center": off_center[:, 0, :],
  64. "horizontal": horizontal[:, 0, :],
  65. "diagonal": diagonal[:, 0, :],
  66. "horizontal_and_diagonal": recordings_dict["horizontal_and_diagonal"][:, 0, :],
  67. "zigzag_1": zigzag_1[0, :],
  68. "zigzag_2": zigzag_2[0, :],
  69. "wave_1": wave_1[0, :],
  70. "wave_2": wave_2[0, :],
  71. "spiral_1": spiral_1[0, :],
  72. "half_current_horizontal": half_current_horizontal[:, 0, :],
  73. "half_current_diagonal": half_current_diagonal[:, 0, :],
  74. "double_current_horizontal": double_current_horizontal[:, 0, :],
  75. "double_current_diagonal": double_current_diagonal[:, 0, :],
  76. "z_axis": z_axis[:, 0, :],
  77. "z_axis_off_center": z_axis_off_center[:, 0, :],
  78. "Three_D_movement_1": Three_D_movement_1[0, :],
  79. "Three_D_movement_2": Three_D_movement_2[0, :],
  80. "Three_D_zigzag_1": Three_D_zigzag_1[0, :],
  81. "Three_D_zigzag_2": Three_D_zigzag_2[0, :],
  82. "z_axis_moving_1": z_axis_moving_1[0, :],
  83. "z_wave_1": z_wave_1[0, :],
  84. "z_wave_2": z_wave_2[0, :]}
  85. def plot_surroundings():
  86. font = {"family": "serif", "color": "black", "size": 15}
  87. plt.subplots_adjust(left=0.07, bottom=0.07, right=0.89, top=0.89, wspace=0.2, hspace=0.2)
  88. # Set Axis
  89. xy_subplot.set_xlim([-0.02 * mm, 0.52 * mm])
  90. xy_subplot.set_ylim([-0.02 * mm, 0.52 * mm])
  91. # xy_subplot.set_xticks(np.arange(0.0*mm, 0.55*mm, 0.05*mm))
  92. # xy_subplot.set_yticks(np.arange(0.0*mm, 0.55*mm, 0.05*mm))
  93. xy_subplot.set_xlabel('X [mm]', fontdict=font)
  94. xy_subplot.set_ylabel('Y [mm]', fontdict=font)
  95. xy_subplot.grid(alpha=0.6)
  96. # for label in xy_subplot.xaxis.get_ticklabels():
  97. # label.set_rotation(90)
  98. # Set Axis
  99. yz_subplot.set_ylim([-0.02 * mm, 0.52 * mm])
  100. yz_subplot.set_xlim([-0.02 * mm, 0.22 * mm])
  101. # yz_subplot.set_ylim([0.1 * mm, 0.5 * mm])
  102. # yz_subplot.set_xlim([0.0 * mm, 0.2 * 100])
  103. # yz_subplot.set_yticks(np.arange(0.00 * mm, 0.5 * mm, 0.05 * mm))
  104. # yz_subplot.set_xticks(np.arange(0.00 * mm, 0.20 * mm, 0.01 * mm))
  105. xy_subplot.tick_params(labelsize=13)
  106. yz_subplot.tick_params(labelsize=13)
  107. yz_subplot.set_ylabel('Y [mm]', fontdict=font)
  108. yz_subplot.set_xlabel('Z [mm]', fontdict=font)
  109. yz_subplot.grid(alpha=0.5)
  110. # Set Titles
  111. xy_subplot.set_title("XY", fontdict=font)
  112. yz_subplot.set_title("ZY", fontdict=font)
  113. # CONFIGURE X, Y PLOT
  114. # Indicate Exciter
  115. Exciter = xy_subplot.vlines(x=0.0 * mm, ymin=0.0 * mm, ymax=0.5 * mm, linestyles="-", colors="#5b9bd5", label="Exciter",
  116. linewidth=6)
  117. xy_subplot.vlines(x=0.5 * mm, ymin=0.0 * mm, ymax=0.5 * mm, linestyles="-", colors="#5b9bd5", linewidth=6)
  118. xy_subplot.hlines(y=0.0 * mm, xmin=0.0 * mm, xmax=0.5 * mm, linestyles="-", colors="#5b9bd5", linewidth=6)
  119. xy_subplot.hlines(y=0.5 * mm, xmin=0.0 * mm, xmax=0.5 * mm, linestyles="-", colors="#5b9bd5", linewidth=6)
  120. yz_subplot.vlines(x=0.0, ymin=0.0 * mm, ymax=0.5 * mm, linestyles="-", colors="#5b9bd5", linewidth=6, alpha=0.3)
  121. # Indicate Localization Area
  122. # LocArea = xy_subplot.vlines(x=0.045, ymin=0.045, ymax=0.495, linestyles="dashed", colors="#7f7f7f",
  123. # label="Loc.\nArea")
  124. # xy_subplot.vlines(x=0.495, ymin=0.045, ymax=0.495, linestyles="dashed", colors="#7f7f7f")
  125. # xy_subplot.hlines(y=0.045, xmin=0.045, xmax=0.495, linestyles="dashed", colors="#7f7f7f")
  126. # xy_subplot.hlines(y=0.495, xmin=0.045, xmax=0.495, linestyles="dashed", colors="#7f7f7f")
  127. # SHOW ANTENNAS
  128. AntPosX = [0 * mm, 0 * mm, 0.13 * mm, 0.375 * mm, 0.5 * mm, 0.5 * mm, 0.125 * mm,
  129. 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, ...
  130. AntPosY = [0.125 * mm, 0.375 * mm, 0.5 * mm, 0.5 * mm, 0.125 * mm, 0.375 * mm, 0 * mm, 0 * mm]
  131. AntOrientY = [270, 270, 180, 180, 90, 90, 0,
  132. 0] # analog as AntPosX,Y , just with Orientation degrees (direction in z-axis
  133. # Select Antenna plotting options here
  134. patchAlpha = 1.0
  135. # ITERATE THROUGH CMAP-------------
  136. n = 8
  137. # color = iter(cm.jet(np.linspace(0, 1, n)))
  138. # ----------------------------
  139. for i in range(8): # iterate through antennas
  140. x = AntPosX[i] # get x and y pos of antennas
  141. y = AntPosY[i]
  142. # c = next(color) # Comment in for coloured antennas
  143. c = "k" # black antennas
  144. if i == 7: # only print one label for the Antennas (otherwise its 8 times in legend)
  145. label = label = str("Receiving\nCoils")
  146. else:
  147. label = ""
  148. if AntOrientY[i] == 0:
  149. # identify antennas orientation
  150. patchX = float(x - 0.033 * mm / 2)
  151. patchY = float(y - 0.033 * mm / 2)
  152. rect = plt.Rectangle((patchX, patchY), 0.033 * mm, 0.033 * mm,
  153. color=c, alpha=patchAlpha, label=label)
  154. xy_subplot.add_patch(rect)
  155. if AntOrientY[i] == 180:
  156. patchX = float(x - 0.033 * mm / 2)
  157. patchY = float(y - 0.033 * mm / 2)
  158. rect = plt.Rectangle((patchX, patchY), 0.033 * mm, 0.033 * mm,
  159. color=c, alpha=patchAlpha)
  160. xy_subplot.add_patch(rect)
  161. if AntOrientY[i] == 90:
  162. patchX = float(x - 0.033 * mm / 2)
  163. patchY = float(y - 0.033 * mm / 2)
  164. rect = plt.Rectangle((patchX, patchY), 0.033 * mm, 0.033 * mm,
  165. color=c, alpha=patchAlpha)
  166. xy_subplot.add_patch(rect)
  167. if AntOrientY[i] == 270:
  168. patchX = float(x - 0.033 * mm / 2)
  169. patchY = float(y - 0.033 * mm / 2)
  170. rect = plt.Rectangle((patchX, patchY), 0.033 * mm, 0.033 * mm,
  171. color=c, alpha=patchAlpha)
  172. xy_subplot.add_patch(rect)
  173. # Add antenna patches in yz plot
  174. # rect2 = plt.Rectangle((patchX, patchY), 0.033 * mm, 0.033 * mm,
  175. # color=c, alpha=0.3, label=label)
  176. # yz_subplot.add_patch(rect2)
  177. def art_file_to_position(recording_name):
  178. """
  179. :param recording_name: int, Choose which recording to print e.g. Recording_!1! , Recording_!2!, etc.
  180. """
  181. """--------------- Open, format and save ART positions from text files---------------------------------------"""
  182. # Opening a recording
  183. art_recording = open('ART Recordings (no ferrite)/' + str(recording_name) + '.drf', 'r')
  184. art_recording = art_recording.read()
  185. # Formatting a recording
  186. art_recording = re.split(' |\n', art_recording) # split string at whitespace and linebreak
  187. art_recording = np.array(art_recording) # list -> np.array
  188. # Count the total number of recorded positions
  189. total_pos_counter = 0
  190. for i in art_recording:
  191. if i == '6d': # this is the flag marking a 6d position (Syntax of the ART guys in the txt files)
  192. total_pos_counter += 1
  193. # Create an empty array which will store the ART measurement infos
  194. art_info = np.zeros((total_pos_counter, 7)) # [t, x, y, z, alpha, beta, gamma]
  195. # Fill the empty numpy array with the information's of the art_recording
  196. pos_counter = 0
  197. for i in range(len(art_recording) - 9):
  198. if art_recording[i] == 'ts': # following entry is timestamp
  199. art_info[pos_counter] = float(art_recording[i + 1])
  200. if art_recording[i] == '6d': # following entries are x,y,z,alpha,beta,gamma
  201. art_info[pos_counter, 1] = float(art_recording[i + 3].split('[')[1]) # x (had to delete some excess text)
  202. art_info[pos_counter, 2] = float(art_recording[i + 4]) # y
  203. art_info[pos_counter, 3] = float(art_recording[i + 5]) # z
  204. art_info[pos_counter, 4] = float(art_recording[i + 6]) # alpha
  205. art_info[pos_counter, 5] = float(art_recording[i + 7]) # beta
  206. art_info[pos_counter, 6] = float(
  207. art_recording[i + 8].split(']')[0]) # gamma (had to delete some excess text
  208. pos_counter += 1
  209. # print("x=", art_info[:, 1])
  210. # print("y=", art_info[:, 2])
  211. pos = [art_info[0, 1], art_info[0, 2], art_info[0, 3]] # only take the first x,y,z position of each recording (as the 0.33mm precision wont have a lot of noise anyways)
  212. return pos
  213. def plot_art_positions(positions, annotate_flag, markersize, annotation_size):
  214. for i in range(len(positions)):
  215. if i == 0: # only add the label to one of the points
  216. xy_subplot.scatter(positions[i, 0], positions[i, 1], color="green", label="ART System", s=markersize, marker="x")
  217. yz_subplot.scatter(positions[i, 2], positions[i, 1], color="green", label="ART System", s=markersize, marker="x")
  218. else:
  219. xy_subplot.scatter(positions[i, 0], positions[i, 1], color="green", s=markersize, marker="x")
  220. yz_subplot.scatter(positions[i, 2], positions[i, 1], color="green", s=markersize, marker="x")
  221. #print("ART: x=", art_info[0, 1], ", y=", art_info[0, 2], ", z=", art_info[0, 3])
  222. if annotate_flag:
  223. xy_subplot.annotate("P" + str(i+1), (positions[i, 0], positions[i, 1]), color="green", size=annotation_size)
  224. yz_subplot.annotate("P" + str(i+1), (positions[i, 2], positions[i, 1]), color="green", size=annotation_size)
  225. def calc_multiple_positions(data, output_filename, only_fs, sf_list, k_list):
  226. """
  227. Takes a recording name and calculates a lot of positions for it, for each given scaling_factor and k_nearest_factor
  228. and stores them all as numpy arrays in folder "calculated_positions/..."
  229. :param recording_name: string name of recording (see recordings_dict)
  230. :param only_fs: only take first sample of recording
  231. :param sf_list: list with all scaling factors to localize with
  232. :param k_list: list with all k_nearest factors to localize with
  233. :param output_filename: it saves the positions under this name. It adds stuff like the scaling factor , k-nearest automatically
  234. :return returns an array containing all positions [3, len(sf_list)*len(k_list)]
  235. """
  236. positions = np.zeros((len(sf_list)*len(k_list), np.shape(data)[0], 3)) # shape: [1000, 42, 3] = [Combinations of sf and k, P1-P42, 3 xyz]
  237. i = 0
  238. j = 0
  239. print("----Calculating positions for ", len(sf_list) * len(k_list), " combinations of sf and k-----")
  240. for sf in sf_list:
  241. localization.scale_factor = sf
  242. for k in k_list:
  243. localization.k_nearest = k
  244. positions[(i+j), :, :] = localization.localize_all_samples_direct(data, output_filename + "_sf=" + str(sf) + "_k=" + str(k))
  245. if len(k_list) != 1: # only increment this if we have multiple k_nearests getting tested
  246. j += 1
  247. if len(sf_list) != 1: # only increment i if we have mutliple scaling factors getting tested
  248. i += 1
  249. print("progress ", i, "/", (len(sf_list)*len(k_list)))
  250. print("----Finished all position calculations-----")
  251. return positions
  252. def plot_multiple_positions(positions_filename, sf_list, k_list, only_label_once, xy_title, yz_title, annotate_points, labels, color_all_black, markersize, annotationsize):
  253. """
  254. You can give this function the name of an IndLoc recording and a bunch of scaling factors and k-nearests
  255. and it will plot you all of them.
  256. :param recording_name:
  257. :param sf_list:
  258. :param k_list:
  259. :param only_label_once:
  260. :param xy_title:
  261. :param yz_title:
  262. :param annotate_points:
  263. :param labels:
  264. :param color_all_black:
  265. :return:
  266. """
  267. colors = iter(plt.cm.jet(np.linspace(0, 1, len(sf_list)*len(k_list))))
  268. #markers = iter(['o', '8', 'p', '>', 'd', 'H', 'x','v', '^','*', 'D','s', 'h', '+', '<'])
  269. markers = iter(['x', 'x'])
  270. #colors = iter(["blue", "orange", "red"])
  271. for sf in sf_list:
  272. for k in k_list:
  273. color = next(colors)
  274. marker = next(markers)
  275. positions = np.load("calculated_positions/"+positions_filename + "_sf=" + str(sf) + "_k=" + str(k)+".npy")
  276. # print("Plotting positions for: sf=", sf, ", k=", k, positions) # show all positions
  277. print("Plotting positions for: sf=", sf, ", k=", k) # minimal print view
  278. if color_all_black:
  279. xy_subplot.scatter(positions[:, 0], positions[:, 1], marker=marker, color="black", label=next(labels), s=markersize)
  280. yz_subplot.scatter(positions[:, 2], positions[:, 1], marker=marker, color="black", s=markersize)
  281. if annotate_points:
  282. for i in range(len(positions[:, 0])):
  283. xy_subplot.annotate("P" + str(i + 1), (positions[i, 0], positions[i, 1]), color="black", size=annotationsize)
  284. yz_subplot.annotate("P" + str(i + 1), (positions[i, 2], positions[i, 1]), color="black", size=annotationsize)
  285. else:
  286. xy_subplot.scatter(positions[:, 0], positions[:, 1], marker=marker, facecolor=color, label=next(labels))
  287. yz_subplot.scatter(positions[:, 2], positions[:, 1], marker=marker, facecolor=color)
  288. if annotate_points:
  289. for i in range(len(positions[:, 0])):
  290. xy_subplot.annotate("P" + str(i + 1), (positions[i, 0], positions[i, 1]), color=color, size=annotationsize)
  291. yz_subplot.annotate("P" + str(i + 1), (positions[i, 2], positions[i, 1]), color=color, size=annotationsize)
  292. yz_subplot.set_title(str(xy_title), size=20)
  293. xy_subplot.set_title(str(yz_title), size=20)
  294. def eval_loc(IndLoc_pos, ART_pos):
  295. """
  296. Takes 2 arrays full of x,y,z positions. Calculated the distance between the two for each position.
  297. Then calculates the distance for each point. Then the mean_distance and the standard_deviation of all positions together.
  298. :param IndLoc_pos: np.array[i, 3] (a number of positions: x,y,z)
  299. :param ART_pos: np.array[i, 3] (a number of positions: x,y,z)
  300. :return: distances, mean_distance, std_dev_distance
  301. """
  302. if np.shape(IndLoc_pos) != np.shape(ART_pos):
  303. print("During calc distance function. Positions arrays dont have the same shape. \n IndLoc (shape):",
  304. np.shape(IndLoc_pos), " ART (shape):", np.shape(ART_pos))
  305. else:
  306. distances = np.zeros((np.shape(IndLoc_pos)[0], 1)) # create an array containing all distances
  307. for i in range(np.shape(IndLoc_pos)[0]): # iterate through all positions
  308. x_IndLoc = IndLoc_pos[i, 0]
  309. y_IndLoc = IndLoc_pos[i, 1]
  310. z_IndLoc = IndLoc_pos[i, 2]
  311. x_ART = ART_pos[i, 0]
  312. y_ART = ART_pos[i, 1]
  313. z_ART = ART_pos[i, 2]
  314. distance = math.sqrt(
  315. (x_IndLoc - x_ART) ** 2 + (y_IndLoc - y_ART) ** 2) # calculate difference for each position
  316. distances[i] = distance
  317. # print("i", i)
  318. # print("IndLoc: x=", x_IndLoc, ", y=", y_IndLoc)
  319. # print("ART: x=", x_ART, ", y=", y_ART)
  320. mean_distance = np.mean(distances)
  321. std_dev_distance = np.std(distances)
  322. return distances, round(mean_distance, 2), round(std_dev_distance, 2)
  323. def load_entire_ART(recording_names):
  324. """
  325. :param recording_names: list of all recording that are to be loaded ["center", "horizontal", "diagonal"]
  326. :return: an array containing them all appended together
  327. """
  328. x_list = []
  329. y_list = []
  330. z_list = []
  331. for i in range(len(recording_names)): # iterate through list of wanted recordings
  332. print("Loading ART", recording_names[i])
  333. for j in range(1, 30): # it always starts with "P1 and ends variable but never over P30"
  334. try:
  335. current_pos = art_file_to_position(recording_names[i]+"_P"+(str(j)))
  336. x_list.append(current_pos[0])
  337. y_list.append(current_pos[1])
  338. z_list.append(current_pos[2])
  339. except FileNotFoundError:
  340. break
  341. pos = np.zeros((len(x_list), 3))
  342. pos[:, 0] = x_list
  343. pos[:, 1] = y_list
  344. pos[:, 2] = z_list
  345. return pos
  346. # Normal ART Loading (1 Recording)
  347. # for i in range(1, 26):
  348. # print("i", i)
  349. # # if i <= 21:
  350. # # pos = plot_art("horizontal_P" + str(i), annotate_point=False)
  351. # if i == 26:
  352. # label_flag = True
  353. # pos = plot_art(recording_selection+"_P" + str(i), annotate_point=False)
  354. # x_list.append(pos[0])
  355. # y_list.append(pos[1])
  356. # z_list.append(pos[2])
  357. # Make the plot look nice
  358. plot_surroundings()
  359. # Load ART positions
  360. ART_pos = load_entire_ART(["double_current_horizontal", "double_current_diagonal"])
  361. # Load IndLoc data
  362. horizontal = recordings_dict["double_current_horizontal"]
  363. diagonal = recordings_dict["double_current_diagonal"]
  364. IndLoc_data = np.append(horizontal, diagonal, axis=0) # shape (42, 2000, 19) = (P1-P42, 2k samples, fv)
  365. # Average IndLoc data
  366. IndLoc_data = np.average(IndLoc_data, axis=1) # shape (42, 19) = (P1-P42, fv_avg)
  367. # Localize IndLoc data for each scaling factor in sf_list and each k in k_list
  368. sf_list = [0.001] # [0.002]
  369. k_list = [3]
  370. IndLoc_pos = calc_multiple_positions(data=IndLoc_data,
  371. output_filename="horizontal_diagonal",
  372. only_fs=True,
  373. sf_list=sf_list,
  374. k_list=k_list)
  375. # Find the best localization
  376. best_mean = 10000
  377. best_std_dev = 10000
  378. best_both = 10000
  379. for i in range((len(sf_list)*len(k_list))):
  380. distances, curr_mean, curr_std_dev = eval_loc(ART_pos=ART_pos, IndLoc_pos=IndLoc_pos[i])
  381. curr_both = curr_mean + curr_std_dev
  382. if curr_mean <= best_mean:
  383. best_both = curr_both
  384. best_mean = curr_mean
  385. best_std_dev = curr_std_dev
  386. index = i
  387. best_sf = sf_list[index]
  388. print("\n----Best localization parameters were found!-----\n scaling_factor = ", best_sf, "\n mean_error = ", best_mean,
  389. "\n standard_deviation = ", best_std_dev)
  390. # Plot IndLoc pos
  391. plot_multiple_positions(positions_filename="horizontal_diagonal",
  392. sf_list=[best_sf],
  393. k_list=k_list,
  394. only_label_once=True,
  395. xy_title="Statistical optimization of the scaling factor for the entire localization area",
  396. yz_title="Statistical optimization of the scaling factor for the entire localization area",
  397. annotate_points=False,
  398. annotationsize=15,
  399. labels=iter(["scaling factor = " + str("{:.6f}".format(best_sf))]),
  400. color_all_black=True,
  401. markersize=100)
  402. # Plot ART positions
  403. plot_art_positions(ART_pos, annotate_flag=False, markersize=100, annotation_size=15)
  404. # print(IndLoc_pos)
  405. #recording_selection = "horizontal_and_diagonal"
  406. # scale_factor_list = [0.001755]
  407. #
  408. # calc_multiple_positions(recording_name=recording_selection,
  409. # only_fs=True,
  410. # sf_list=[0.001755],
  411. # k_list=[3])
  412. #
  413. # plot_multiple_positions(recording_name=recording_selection,
  414. # sf_list=[0.001755],
  415. # k_list=[3],
  416. # only_label_once=True,
  417. # xy_title="Statistical optimization of the scaling factor for an offcenter set of points",
  418. # yz_title="Statistical optimization of the scaling factor for an offcenter set of points",
  419. # annotate_points=False,
  420. # labels=iter(["scaling factor = " + str("{:.6f}".format(0.001755))]),
  421. # color_all_black=True)
  422. xy_subplot.legend(bbox_to_anchor=(1.01, 1.00), loc=2, borderaxespad=0., fontsize=10)
  423. plt.show()