/input/data_parser.py

https://github.com/felipecode/coiltraine
Python | 68 lines | 51 code | 10 blank | 7 comment | 4 complexity | 451452f41d93582a9637c1f5ab08f8ec MD5 | raw file
  1. import glob
  2. import os
  3. import json
  4. import numpy as np
  5. """
  6. Module used to check attributes existent on data before incorporating them
  7. to the coil dataset
  8. """
  9. def orientation_vector(measurement_data):
  10. pitch = np.deg2rad(measurement_data['rotation_pitch'])
  11. yaw = np.deg2rad(measurement_data['rotation_yaw'])
  12. orientation = np.array([np.cos(pitch)*np.cos(yaw), np.cos(pitch)*np.sin(yaw), np.sin(pitch)])
  13. return orientation
  14. def forward_speed(measurement_data):
  15. vel_np = np.array([measurement_data['velocity_x'], measurement_data['velocity_y'],
  16. measurement_data['velocity_z']])
  17. speed = np.dot(vel_np, orientation_vector(measurement_data))
  18. return speed
  19. def get_speed(measurement_data):
  20. """ Extract the proper speed from the measurement data dict """
  21. # If the forward speed is not on the dataset it is because speed is zero.
  22. if 'playerMeasurements' in measurement_data and \
  23. 'forwardSpeed' in measurement_data['playerMeasurements']:
  24. return measurement_data['playerMeasurements']['forwardSpeed']
  25. elif 'velocity_x' in measurement_data: # We have a 0.9.X data here
  26. return forward_speed(measurement_data)
  27. else: # There is no speed key, probably speed is zero.
  28. return 0
  29. def check_available_measurements(episode):
  30. """ Try to automatically check the measurements
  31. The ones named 'steer' are probably the steer for the vehicle
  32. This needs to be made more general to avoid possible mistakes on dataset reading
  33. """
  34. measurements_list = glob.glob(os.path.join(episode, 'measurement*'))
  35. # Open a sample measurement
  36. with open(measurements_list[0]) as f:
  37. measurement_data = json.load(f)
  38. available_measurements = {}
  39. for meas_name in measurement_data.keys():
  40. # Add steer
  41. if 'steer' in meas_name and 'noise' not in meas_name:
  42. available_measurements.update({'steer': meas_name})
  43. # Add Throttle
  44. if 'throttle' in meas_name and 'noise' not in meas_name:
  45. available_measurements.update({'throttle': meas_name})
  46. # Add brake ( Not hand brake)
  47. if 'brake' in meas_name and 'noise' not in meas_name and 'hand' not in meas_name:
  48. available_measurements.update({'brake': meas_name})
  49. # add game time
  50. return available_measurements