-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_1.py
More file actions
86 lines (71 loc) · 2.87 KB
/
random_1.py
File metadata and controls
86 lines (71 loc) · 2.87 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
80
81
82
83
84
85
86
import os
import shutil
import csv
import random
from progress.bar import IncrementalBar
def rename(new_folder_name: str) -> dict:
"""The function renames files and changes the hierarchy,
Args:
new_folder_name (str): path to destination directory
Returns:
dict: key - file path, value - class label
"""
random_numbers = random.sample(range(0, 10001), 5000)
count = 0
class_nums = {}
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'{random_numbers[count]}.txt'))
class_nums[random_numbers[count]] = i
count += 1
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('..')
return class_nums
def move_dataset(old_folder_name: str, new_folder_name: str) -> None:
"""The function 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 make_csv_random(new_folder_name: str, class_number: dict) -> None:
"""The function 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
class_number (dict): key - file path, value - class label
"""
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("paths3.csv", 'w')
writer = csv.writer(f, delimiter=',', lineterminator='\n')
for name in names:
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)
name = name.replace('.txt', '')
writer.writerow(
[absolute_path_file, relative_path_file, class_number[int(name)]])
bar.next()
def main(old_folder_name: str, new_folder_name: str) -> None:
move_dataset(old_folder_name, new_folder_name)
make_csv_random(new_folder_name, rename(new_folder_name))