forked from QTIM-Lab/qtim_ROP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.py
executable file
·73 lines (45 loc) · 1.86 KB
/
metadata.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
#!/usr/bin/env python
from os.path import basename, dirname, splitext, join
import pandas as pd
import yaml
from common import find_images, make_sub_dir
QUERY = 'subjectID == "{subjectID}" and eye == "{eye}" and reader == "{gold}" and Session == "{session}"'
VIEWS = ['posterior', 'nasal', 'temporal']
dir_name = dirname(__file__)
conf_name = join(dir_name, 'config/conf.yaml')
with open(conf_name) as y:
conf = yaml.load(y)
GOLD = conf['golden_reader']
def image_to_metadata(im_path):
im_name = basename(im_path)
im_str = splitext(im_name)[0]
try:
subject_id, im_id, session, view, eye, class_ = im_str.split('_')[:7]
except ValueError:
subject_id, _, im_id, session, view, eye, class_ = im_str.split('_')[:7]
return {'imID': im_id, 'subjectID': subject_id, 'session': session, 'eye': eye, 'class': class_,
'image': im_path, 'prefix': im_str, 'view': view}
def image_csv_data(im_path, csv_df):
meta_dict = image_to_metadata(im_path)
meta_dict.update({'gold': GOLD})
image_view = meta_dict['view']
row = csv_df.query(QUERY.format(**meta_dict)).iloc[[0]]
for view in VIEWS:
if view != image_view:
del row[view]
row.rename(columns={image_view: 'image'}, inplace=True)
row['ID'] = meta_dict['imID']
row['downloadFile'] = basename(im_path)
return row, meta_dict
def unique_images(in_dir):
unique = []
im_df = pd.DataFrame(data=[image_to_metadata(im) for im in find_images(join(in_dir, '*'))])
for imID, group in im_df.groupby('imID'):
unique.append((group['class'].iloc[0], group['image'].iloc[0]))
return unique
if __name__ == '__main__':
import sys
from shutil import copy
for class_name, u_im in unique_images(sys.argv[1]):
class_dir = make_sub_dir(sys.argv[2], class_name)
copy(u_im, join(class_dir, basename(u_im)))