-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
79 lines (63 loc) · 2.5 KB
/
analysis.py
File metadata and controls
79 lines (63 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import csv
import shutil
from progress.bar import IncrementalBar
def rename(new_folder_name: str) -> None:
"""The function renames files and changes the hierarchy
Args:
new_folder_name (str): path to destination directory
"""
for i in range(1, 6):
bar = IncrementalBar(f'Renaming class_{i}', max=1000)
relative_path = os.path.relpath(f'{new_folder_name}')
class_path = os.path.join(relative_path, str(i))
names = os.listdir(class_path)
relative_paths = []
new_relative_paths = []
for name in names:
relative_paths.append(os.path.join(class_path, name))
for name in names:
new_relative_paths.append(
os.path.join(relative_path, f'{i}_{name}'))
for old_name, new_name in zip(relative_paths, new_relative_paths):
bar.next()
os.replace(old_name, new_name)
os.chdir(f'{new_folder_name}')
if os.path.isdir(str(i)):
os.rmdir(str(i))
os.chdir('..')
def move_dataset(old_folder_name: str, new_folder_name: str) -> None:
"""The function gets old and new folders paths and copies files to a new directory
Args:
old_folder_name (str): path to source directory
new_folder_name (str): path to destination directory
"""
old_path = os.path.relpath(f'{old_folder_name}')
new_path = os.path.relpath(f'{new_folder_name}')
shutil.copytree(old_path, new_path)
def new_make_csv(new_folder_name: str) -> None:
"""The function get a folder path and writes data to a csv file in the following format: absolute path, relative path, class label
Args:
new_folder_name (str): path to destination directory
"""
bar = IncrementalBar(f'Writting csv', max=5000)
work_catalog = os.getcwd()
os.chdir(new_folder_name)
names = os.listdir()
os.chdir(work_catalog)
f = open("paths2.csv", 'w')
writer = csv.writer(f, delimiter=',', lineterminator='\n')
for name in names:
num_class = name[0]
absolute_path = os.path.abspath(new_folder_name)
absolute_path_file = os.path.join(absolute_path, name)
relative_path = os.path.relpath(f'{new_folder_name}')
relative_path_file = os.path.join(relative_path, name)
writer.writerow([absolute_path_file, relative_path_file, num_class])
bar.next()
def main() -> None:
move_dataset('dataset', 'dataset2')
rename('dataset2')
new_make_csv('dataset2')
if __name__ == '__main__':
main()