-
Notifications
You must be signed in to change notification settings - Fork 1
/
padre_import.py
executable file
·101 lines (87 loc) · 3.35 KB
/
padre_import.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
#!/usr/bin/env python
'''straight-forward importing program using argparse parameters'''
import padre as p
import neural as nl
import argparse,os,shutil
# Recognize unverified sessions so we don't get confused as we're importing
p._include_all = True
parser = argparse.ArgumentParser()
parser.add_argument("subject")
parser.add_argument("session")
parser.add_argument("-t","--tag",nargs='*')
parser.add_argument("-e","--experiment")
parser.add_argument("--date")
parser.add_argument("--scan-sheet")
parser.add_argument("-l","--label")
parser.add_argument("-d","--dsets",nargs='*')
parser.add_argument("-m","--meta",nargs='*')
parser.add_argument("-r","--raw")
parser.add_argument("--verify",help="automatically accept datasets as verified",action='store_true')
args = parser.parse_args()
with p.maint.commit_wrap():
nl.notify('Loading subject %s' % args.subject)
subj = p.maint.create_subject(args.subject)
if args.session not in subj.sessions:
nl.notify('Creating new session (%s)' % args.session)
p.maint.new_session(subj,args.session)
sess = subj._sessions[args.session]
if args.experiment:
nl.notify('Setting experiment to %s' % args.experiment)
sess['experiment'] = args.experiment
if args.tag:
nl.notify('Adding tags: %s' % repr(args.tag))
if 'tags' not in sess:
sess['tags'] = []
for t in args.tag:
if t not in sess['tags']:
sess['tags'].append(t)
if args.date:
nl.notify('Setting date to %s' % args.date)
sess['date'] = args.date
if args.verify:
sess['unverified'] = False
else:
sess['unverified'] = True
subj.save()
sess_dir = os.path.join(p.sessions_dir(subj),args.session)
if args.scan_sheet:
if os.path.exists(args.scan_sheet):
with p.maint.commit_wrap():
try:
shutil.copy(args.scan_sheet,sess_dir)
sess['scan_sheets'] = os.path.basename(args.scan_sheets)
except:
pass
if args.dsets:
for i in xrange(len(args.dsets)):
with p.maint.commit_wrap():
dset = args.dsets[i]
try:
meta = args.meta[i]
except:
meta = None
with nl.notify('Trying to add %s' % dset):
if os.path.exists(dset) and dset not in [x.__str__(False) for x in subj.dsets(include_all=True)]:
dset_dict = {}
dset_dict['filename'] = os.path.basename(dset)
dset_dict['md5'] = nl.hash(dset)
dset_dict['complete'] = True
dset_dict['meta'] = {}
label = args.label
if label==None:
label = 'unsorted'
if label not in sess['labels']:
sess['labels'][label] = []
sess['labels'][label].append(dset_dict)
dset_fname = os.path.join(sess_dir,dset_dict['filename'])
nl.notify('copying dset')
if not os.path.exists(dset_fname):
shutil.copy(dset,dset_fname)
if meta:
nl.notify('Warning: meta not implemented yet',level=nl.level.warning)
subj.save()
if args.raw:
try:
shutil.copytree(args.raw,p.raw_dir(subj))
except:
pass