-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide_k_fold.py
executable file
·185 lines (154 loc) · 4.99 KB
/
divide_k_fold.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Divide the dataset into k folds (e.g. 10)
本文件生成k折交叉验证的目录结构
Tiles
Blood
Tumor
...
>>>
Tiles
Blood
01
02
...
10
Tumor
01
02
...
10
...
"""
import argparse
import os
import shutil
def create_folds(dirname, N=10):
"""Move files into N subdirectories.
N: directory number
divide equally (except the last directory)
"""
abs_dirname = os.path.abspath(dirname)
files = [os.path.join(abs_dirname, f.path)
for f in os.scandir(abs_dirname) if not f.is_dir()]
i = 0
file_num = len(files)
num_in_subdir = file_num//N + 1
for k in range(N):
subdir_name = os.path.join(
abs_dirname, '{0:03d}'.format(k+1))
if not os.path.exists(subdir_name):
os.mkdir(subdir_name)
k = 0
for f in sorted(files):
if i % num_in_subdir != 0:
pass
else:
k += 1
subdir_name = os.path.join(
abs_dirname, '{0:03d}'.format(k))
# move file to current dir
f_base = os.path.basename(f)
shutil.move(f, os.path.join(subdir_name, f_base))
i += 1
def unfold(dirname, N=None):
"""move files in subdirs (folds) out to the basedir
dirname: base directory
"""
dirname = os.path.abspath(dirname)
subfolders = [f.path for f in os.scandir(dirname) if (
f.is_dir() and not os.path.basename(f).startswith('.'))]
for subdir in subfolders:
abs_dirname = os.path.abspath(subdir)
files = [os.path.join(abs_dirname, f) for f in os.listdir(abs_dirname)]
for f in sorted(files):
f_base = os.path.basename(f)
shutil.move(f, os.path.join(dirname, f_base))
shutil.rmtree(abs_dirname)
def parse_args():
"""Parse command line arguments passed to script invocation."""
parser = argparse.ArgumentParser(
description='Split files into multiple subfolders.')
parser.add_argument('src_dir',
help='source directory',
type=str,
default="./TILES/")
parser.add_argument('depth',
help='depth of diectory operations',
type=int,
default=100)
parser.add_argument('k',
help='fold number',
type=int,
default=10)
parser.add_argument('mode',
type=int,
help='create fold/undo',
default=None)
return parser.parse_args()
def execlevel(basedir, func=None, N=None, depth=2):
"""execute fold or unfold to a specified level of directory
Args:
basedir ([type]): [description]
func ([type], optional): [description]. Defaults to None.
N ([type], optional): [description]. Defaults to None.
depth (int, optional): [description]. Defaults to 2.
"""
curr_dirs = [f.path for f in os.scandir(basedir) if (
f.is_dir() and not os.path.basename(f).startswith('.'))]
if depth == 1:
for subdir in curr_dirs:
func(subdir, N)
else:
depth -= 1
for subdir in curr_dirs:
execlevel(subdir, func=func, N=N, depth=depth)
def main():
args = parse_args()
src_dir = args.src_dir # "./sample_directory/"
# Walk till the last hierachy of directory
if args.mode == 0:
func = unfold
elif args.mode == 1:
func = create_folds
else:
print(args.mode)
return 1
execlevel(basedir=src_dir,
func=func,
N=args.k,
depth=args.depth)
return 0
if __name__ == '__main__':
main()
"""
.
└── mask
├── annot
│ └── fooannot.annot
└── tile
├── healthy
│ ├── 001 [27 entries exceeds filelimit, not opening dir]
│ ├── 002 [27 entries exceeds filelimit, not opening dir]
│ ├── 003 [27 entries exceeds filelimit, not opening dir]
│ ├── 004 [27 entries exceeds filelimit, not opening dir]
│ └── 005 [24 entries exceeds filelimit, not opening dir]
└── tumor
├── 001 [21 entries exceeds filelimit, not opening dir]
├── 002 [21 entries exceeds filelimit, not opening dir]
├── 003 [21 entries exceeds filelimit, not opening dir]
├── 004 [21 entries exceeds filelimit, not opening dir]
└── 005 [17 entries exceeds filelimit, not opening dir]
python ../divide_k_fold.py ./mask/tile/ 1 5 0
===
python ../divide_k_fold.py ./ 3 5 0
===>
.
└── chips(same structure)
........
└── mask
├── annot
│ └── fooannot.annot
└── tile
├── healthy [132 entries exceeds filelimit, not opening dir]
└── tumor [101 entries exceeds filelimit, not opening dir]
"""