def convert_MAP(directory, output_directory, min_shape, file_format = '.npy', search_keys = None, dtype = np.float32): ''' This program loops through given raw_data directory and converts .mat files to .npy files ''' new_dir = os.path.join(os.getcwd(), output_directory) if not os.path.exists(new_dir): os.mkdir(new_dir) else: shutil.rmtree(new_dir) os.mkdir(new_dir) for file in os.listdir(directory): filename = os.fsdecode(file) if filename.endswith(".mat"): #print(os.path.join(directory, filename)) filepath = os.path.join(directory, filename) array_dict = {} try: f = h5py.File(filepath, 'r') except: f = sio.loadmat(filepath) for k, v in f.items(): array_dict[k] = np.array(v, dtype = np.float32) # As we only need image info from dict (the last key) we do this if search_keys == None: search_keys = 'map' # out of struct of .mat files want "map" filtered_dict = dict(filter(lambda item: search_keys in item[0], array_dict.items())) else: filtered_dict = {} for i in range(len(search_keys)): search_key = search_keys[i] if search_key in array_dict: filtered_dict[search_key] = array_dict[search_key] if len(filtered_dict) == 0: print('No Data to Meet Search Key Requirements: Datapoint Rejected -> ' + filepath) else: #print(list(array_dict.keys())) #print(filtered_dict) arrays = [] for k, v in filtered_dict.items(): temp = np.transpose(v.astype(np.float32)) # To normalize data between [-1,1], use -> arrays = arrays/(np.max(arrays)/2) - 1 # To normalize data between [0,1], use -> arrays = arrays/(np.max(arrays)) # To normalize data between [0,255], # use -> arrays = (arrays/(np.max(arrays))*255).astype(np.uint8) temp = temp/(np.max(temp)) arrays.append(temp) for i in range(len(arrays)): if len(arrays[i].shape) > 2: #print(arrays[i].shape) arrays[i] = np.mean(arrays[i], axis = 2) for i in range(len(arrays)): new_dir_filepath = os.path.join(new_dir, filename.strip('.mat') + '_index'+str(i) + file_format) array = arrays[i] if array.shape[0] >= min_shape[0] and array.shape[1] >= min_shape[1]: if file_format == '.npy': np.save(new_dir_filepath, array, allow_pickle=True, fix_imports=True) else: imageio.imwrite(new_dir_filepath, array) elif i == 0: print('Min Size Not Met: Datapoint Rejected -> ' + filepath) return os.path.join(os.getcwd(), output_directory) ################################################################################################################################## # Data Cleaning Procedures: