|
| 1 | +from scipy.io import savemat |
| 2 | +import numpy as np |
| 3 | +import zarr |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import matplotlib.pyplot as plt |
| 8 | + |
| 9 | +SECONDS_IN_HR = 3600 |
| 10 | +# Save large numpy file as a mat file. Save smaller segments of the numpy file as mat file. |
| 11 | + |
| 12 | + |
| 13 | +class TUM_npy2mat: |
| 14 | + def __init__(self, config): |
| 15 | + self.config = config |
| 16 | + |
| 17 | + def slice_accelerometer_data(self, data, offset_time, duration): |
| 18 | + rows, col = stationary_acclerometer_data.shape |
| 19 | + tot_time = stationary_acclerometer_data[-1, |
| 20 | + 0] - stationary_acclerometer_data[0, 0] |
| 21 | + print("No. of hrs %f" % (tot_time/3600)) |
| 22 | + self.avg_fs = int(np.round(rows/tot_time)) |
| 23 | + self.offset_in_hr = offset_time |
| 24 | + self.duration_in_hr = duration |
| 25 | + sample_num_start = self.offset_in_hr*self.avg_fs*SECONDS_IN_HR |
| 26 | + sample_num_end = sample_num_start + self.duration_in_hr*self.avg_fs*SECONDS_IN_HR |
| 27 | + return data[sample_num_start:sample_num_end, :] |
| 28 | + |
| 29 | + def visualize_accelerometer_data(self, data): |
| 30 | + fig, axes = plt.subplots(nrows=3, ncols=2) |
| 31 | + for i, ax in enumerate(axes): |
| 32 | + ax[0].plot(data[:, 0]/3600, data[:, i+1]) |
| 33 | + ax[0].set_xlabel('time(hr)') |
| 34 | + ax[0].set_ylabel('rad/s') |
| 35 | + ax[0].grid(True) |
| 36 | + |
| 37 | + ax[1].plot(data[:, 0]/3600, data[:, 4+i]) |
| 38 | + ax[1].set_xlabel('time(hr)') |
| 39 | + ax[1].set_ylabel('m/s^2') |
| 40 | + ax[1].grid(True) |
| 41 | + fig.tight_layout() |
| 42 | + plt.show(block=False) |
| 43 | + plt.pause(.5) |
| 44 | + |
| 45 | + def save_to_mat(self, acc_data): |
| 46 | + |
| 47 | + acc_data_dict = {"fs_hz": self.avg_fs, |
| 48 | + "duration_hr": self.duration_in_hr, |
| 49 | + "offset_hr": self.offset_in_hr, |
| 50 | + "timestamp": acc_data[:, 0], |
| 51 | + "gyro_x": acc_data[:, 1], |
| 52 | + "gyro_y": acc_data[:, 2], |
| 53 | + "gyro_z": acc_data[:, 3], |
| 54 | + "accel_x": acc_data[:, 4], |
| 55 | + "accel_y": acc_data[:, 5], |
| 56 | + "accel_z": acc_data[:, 6], |
| 57 | + "temperature": acc_data[:, 7]} |
| 58 | + |
| 59 | + savemat(os.path.join(self.config.output_directory_path, |
| 60 | + self.config.output_file_name + '.mat'), acc_data_dict) |
| 61 | + |
| 62 | + |
| 63 | +# data = np.load( |
| 64 | +# r'E:\Windows\Documents\Northeastern\NEUFRL\IMU_Paper_Part_2\dataset-calib-imu-static2.npy', mmap_mode='r') |
| 65 | + |
| 66 | + |
| 67 | +def check_config(config): |
| 68 | + try: |
| 69 | + assert(os.path.exists(config.input_file_path) |
| 70 | + ), 'Invalid path for input npy file' |
| 71 | + _, _, ext = file_path_parts(config.input_file_path) |
| 72 | + assert(ext == ".npy"), 'Invalid extention for input file' |
| 73 | + if config.output_directory_path == None and config.output_file_name == None: |
| 74 | + config.output_directory_path, config.output_file_name, _ = file_path_parts( |
| 75 | + config.input_file_path) |
| 76 | + |
| 77 | + elif config.output_directory_path == None: |
| 78 | + config.output_directory_path, _, _ = file_path_parts( |
| 79 | + config.input_file_path) |
| 80 | + |
| 81 | + elif config.output_file_name == None: |
| 82 | + config.output_file_name, _, _ = file_path_parts( |
| 83 | + config.input_file_path) |
| 84 | + |
| 85 | + if (not os.path.exists(config.output_directory_path)): |
| 86 | + os.path.mkdir(config.output_directory_path) |
| 87 | + except AssertionError as msg: |
| 88 | + print(msg) |
| 89 | + sys.exit() |
| 90 | + |
| 91 | + |
| 92 | +def file_path_parts(full_file_path): |
| 93 | + output_dir, file = os.path.split(full_file_path) |
| 94 | + filename, ext = os.path.splitext(file) |
| 95 | + return output_dir, filename, ext |
| 96 | + |
| 97 | + # ax[1:0].plot(data[:, 0], data[:, 7]) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + argument_parser = argparse.ArgumentParser( |
| 102 | + prog="numpy 2 mat inputs", usage="converts .npy file to .mat") |
| 103 | + argument_parser.add_argument( |
| 104 | + '-i', '--input_file_path', type=str, required=True, help='Input numpy file path', default='./dataset-calib-imu-static2.npy') |
| 105 | + argument_parser.add_argument( |
| 106 | + '-o', '--output_file_name', type=str, help='Output mat file path') |
| 107 | + argument_parser.add_argument( |
| 108 | + '-d', '--output_directory_path', type=str, help='Output mat file directory') |
| 109 | + argument_parser.add_argument( |
| 110 | + '--offset', type=int, help='offset of samples', default=1) |
| 111 | + argument_parser.add_argument( |
| 112 | + '--duration', type=int, help="Duration of samples extracted", default=7) |
| 113 | + config = argument_parser.parse_args() |
| 114 | + check_config(config) |
| 115 | + print(vars(config)) |
| 116 | + print("----------------") |
| 117 | + stationary_acclerometer_data = np.load( |
| 118 | + config.input_file_path, mmap_mode='r') |
| 119 | + |
| 120 | + # Slice data |
| 121 | + tum_npy2mat = TUM_npy2mat(config) |
| 122 | + sliced_data = tum_npy2mat.slice_accelerometer_data( |
| 123 | + stationary_acclerometer_data, config.offset, config.duration) |
| 124 | + |
| 125 | + tum_npy2mat.save_to_mat(sliced_data) |
| 126 | + tum_npy2mat.visualize_accelerometer_data( |
| 127 | + sliced_data) |
| 128 | + plt.show(block=True) |
0 commit comments