Skip to content

Commit

Permalink
Add a useful directory process tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylark0924 committed Aug 18, 2023
1 parent c2312aa commit ad6fc08
Show file tree
Hide file tree
Showing 128 changed files with 80 additions and 4 deletions.
Binary file removed examples/data/felt/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_circle/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_circle/trial_01/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_circle/trial_02/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_circle/trial_03/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeC/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeC/trial_01/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeC/trial_02/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeC/trial_03/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeR/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeR/trial_01/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeR/trial_02/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_edgeR/trial_03/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_raster/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_raster/trial_01/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_raster/trial_02/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_raster/trial_03/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_spiral/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_spiral/trial_01/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_spiral/trial_02/desktop.ini
Binary file not shown.
Binary file removed examples/data/felt/wipe_spiral/trial_03/desktop.ini
Binary file not shown.
15 changes: 11 additions & 4 deletions examples/learning_ml/example_felt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
with Tactile Sensor" by Junjia LIU, et al.
"""
import numpy as np

import os
import rofunc as rf


# --- Data processing ---
def data_process(dat_path):
raw_demo = np.load('../../data/LFD_ML/LeftHand.npy')
def data_process(data_path):
for trial in os.listdir(data_path):
if trial.startswith('trial'):
trial_path = os.path.join(data_path, trial)
hand_rigid = np.load(os.path.join(trial_path, 'mocap_hand_rigid.npy'))
object_rigid = np.load(os.path.join(trial_path, 'mocap_object_rigid.npy'))
raw_demo = np.load(data_path)

return demos_x


demos_x = data_process('../data/LFD_ML/LeftHand.npy')
demos_x = data_process('../data/felt/wipe_circle')

# --- TP-GMM ---
# Define the task parameters
Expand Down
69 changes: 69 additions & 0 deletions rofunc/utils/file/dir_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import rofunc as rf


def list_absl_path(dir, recursive=False):
"""
Get the absolute path of each file in the directory.
Example::
>>> list_absl_path('/home/ubuntu/Github/Rofunc/examples/data/felt', recursive=True)
['/home/ubuntu/Github/Rofunc/examples/data/felt/trial_1/mocap_hand_rigid.npy',
'/home/ubuntu/Github/Rofunc/examples/data/felt/trial_1/mocap_object_rigid.npy',
...]
:param dir: directory path
:param recursive: if True, list the files in the subdirectories as well
:return: list
"""
if recursive:
return [os.path.join(root, file) for root, dirs, files in os.walk(dir) for file in files]
else:
return [os.path.join(dir, file) for file in os.listdir(dir)]


def delete_files(dir, file_list_to_delete, recursive=False):
"""
Delete the appointed files in the directory.
Example::
>>> delete_files('/home/ubuntu/Github/Rofunc/examples/data/felt', ['desktop.ini'], recursive=True)
:param dir: directory path
:param file_list_to_delete: the list of file names need to be deleted
:param recursive: if True, delete the files in the subdirectories as well
:return: None
"""
all_files = list_absl_path(dir, recursive=recursive)
for file in all_files:
if os.path.basename(file) in file_list_to_delete:
os.remove(file)
rf.logger.beauty_print('File {} deleted.'.format(file), type='info')


def rename_files(dir, source_file_list=None, target_file_list=None, recursive=False):
"""
Rename the appointed files from source_file_list to target_file_list.
Example::
>>> rename_files('/home/ubuntu/Github/Rofunc/examples/data/felt', \
source_file_list=['wiping_spiral_mocap_hand.csv', 'wiping_spiral_mocap_hand_rigid.csv'], \
target_file_list=['mocap_hand.csv', 'mocap_hand_rigid.csv', 'mocap_object.csv'], \
recursive=True)
:param dir: directory path
:param source_file_list:
:param target_file_list:
:param recursive: if True, rename the files in the subdirectories as well
:return: None
"""
all_files = list_absl_path(dir, recursive=recursive)
for file in all_files:
if os.path.basename(file) in source_file_list:
target_name = target_file_list[source_file_list.index(os.path.basename(file))]
os.rename(file, os.path.join(os.path.dirname(file), target_name))
rf.logger.beauty_print('File {} renamed from {} to {}.'.format(file, os.path.basename(file), target_name),
type='info')

0 comments on commit ad6fc08

Please sign in to comment.