File: sessionmirror.py

package info (click to toggle)
pyxnat 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,204 kB
  • sloc: python: 6,016; makefile: 28; sh: 17; xml: 11
file content (699 lines) | stat: -rwxr-xr-x 24,499 bytes parent folder | download | duplicates (2)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/usr/bin/env python
'''
The MIT License (MIT)

Copyright (c) 2014 VUIIS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

'''
Adapted from xnatmirror (https://github.com/VUIIS/dax/blob/master/bin/Xnat_tools/Xnatmirror)
'''

def cmp(a, b):
    return (a > b) - (a < b)

from builtins import zip
from builtins import str

import os
import os.path as op
import sys

from xml.etree import cElementTree as ET
import pyxnat

PROJ_ATTRS = [
    'xnat:projectData/name',
    'xnat:projectData/description',
    'xnat:projectData/keywords',
]

SUBJ_ATTRS = [
    'xnat:subjectData/group',
    'xnat:subjectData/src',
    'xnat:subjectData/investigator/firstname',
    'xnat:subjectData/investigator/lastname',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/dob',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/yob',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/age',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/gender',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/handedness',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/ses',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/education',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/\
educationDesc',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/race',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/ethnicity',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/weight',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/height',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/\
gestational_age',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/\
post_menstrual_age',
    'xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/\
birth_weight'
]

MR_EXP_ATTRS = [
    'xnat:experimentData/date',
    'xnat:experimentData/visit_id',
    'xnat:experimentData/time',
    'xnat:experimentData/note',
    'xnat:experimentData/investigator/firstname',
    'xnat:experimentData/investigator/lastname',
    'xnat:imageSessionData/scanner/manufacturer',
    'xnat:imageSessionData/scanner/model',
    'xnat:imageSessionData/operator',
    'xnat:imageSessionData/dcmAccessionNumber',
    'xnat:imageSessionData/dcmPatientId',
    'xnat:imageSessionData/dcmPatientName',
    'xnat:imageSessionData/session_type',
    'xnat:imageSessionData/modality',
    'xnat:imageSessionData/UID',
    'xnat:mrSessionData/coil',
    'xnat:mrSessionData/fieldStrength',
    'xnat:mrSessionData/marker',
    'xnat:mrSessionData/stabilization'
]

OTHER_DICOM_SCAN_ATTRS = [
    'xnat:imageScanData/type',
    'xnat:imageScanData/UID',
    'xnat:imageScanData/note',
    'xnat:imageScanData/quality',
    'xnat:imageScanData/condition',
    'xnat:imageScanData/series_description',
    'xnat:imageScanData/documentation',
    'xnat:imageScanData/frames',
    'xnat:imageScanData/startTime',
    'xnat:imageScanData/scanner/manufacturer',
    'xnat:imageScanData/scanner/model'
]

MR_SCAN_ATTRS = [
    'xnat:imageScanData/type',
    'xnat:imageScanData/UID',
    'xnat:imageScanData/note',
    'xnat:imageScanData/quality',
    'xnat:imageScanData/condition',
    'xnat:imageScanData/series_description',
    'xnat:imageScanData/documentation',
    'xnat:imageScanData/frames',
    'xnat:imageScanData/startTime',
    'xnat:imageScanData/scanner/manufacturer',
    'xnat:imageScanData/scanner/model',
    'xnat:mrScanData/parameters/flip',
    'xnat:mrScanData/parameters/orientation',
    'xnat:mrScanData/parameters/tr',
    'xnat:mrScanData/parameters/ti',
    'xnat:mrScanData/parameters/te',
    'xnat:mrScanData/parameters/sequence',
    'xnat:mrScanData/parameters/imageType',
    'xnat:mrScanData/parameters/scanSequence',
    'xnat:mrScanData/parameters/seqVariant',
    'xnat:mrScanData/parameters/scanOptions',
    'xnat:mrScanData/parameters/acqType',
    'xnat:mrScanData/parameters/pixelBandwidth',
    'xnat:mrScanData/parameters/voxelRes/x',
    'xnat:mrScanData/parameters/voxelRes/y',
    'xnat:mrScanData/parameters/voxelRes/z',
    'xnat:mrScanData/parameters/fov/x',
    'xnat:mrScanData/parameters/fov/y',
    'xnat:mrScanData/parameters/matrix/x',
    'xnat:mrScanData/parameters/matrix/y',
    'xnat:mrScanData/parameters/partitions',
    'xnat:mrScanData/fieldStrength',
    'xnat:mrScanData/marker',
    'xnat:mrScanData/stabilization',
    'xnat:mrScanData/coil'
]

SC_SCAN_ATTRS = [
    'xnat:imageScanData/type',
    'xnat:imageScanData/UID',
    'xnat:imageScanData/note',
    'xnat:imageScanData/quality',
    'xnat:imageScanData/condition',
    'xnat:imageScanData/series_description',
    'xnat:imageScanData/documentation',
    'xnat:imageScanData/frames',
    'xnat:imageScanData/scanner/manufacturer',
    'xnat:imageScanData/scanner/model'
]

PET_EXP_ATTRS = [
    'xnat:experimentData/date',
    'xnat:experimentData/visit_id',
    'xnat:experimentData/time',
    'xnat:experimentData/note',
    'xnat:experimentData/investigator/firstname',
    'xnat:experimentData/investigator/lastname',
    'xnat:imageSessionData/scanner/manufacturer',
    'xnat:imageSessionData/scanner/model',
    'xnat:imageSessionData/operator',
    'xnat:imageSessionData/dcmAccessionNumber',
    'xnat:imageSessionData/dcmPatientId',
    'xnat:imageSessionData/dcmPatientName',
    'xnat:imageSessionData/session_type',
    'xnat:imageSessionData/modality',
    'xnat:imageSessionData/UID',
    'xnat:petSessionData/studyType',
    'xnat:petSessionData/patientID',
    'xnat:petSessionData/patientName',
    'xnat:petSessionData/stabilization',
    'xnat:petSessionData/start_time_scan',
    'xnat:petSessionData/start_time_injection',
    'xnat:petSessionData/tracer/name',
    'xnat:petSessionData/tracer/startTime',
    'xnat:petSessionData/tracer/dose',
    'xnat:petSessionData/tracer/specificActivity',
    'xnat:petSessionData/tracer/totalMass',
    'xnat:petSessionData/tracer/intermediate',
    'xnat:petSessionData/tracer/isotope',
    'xnat:petSessionData/tracer/isotope/half-life',
    'xnat:petSessionData/tracer/transmissions',
    'xnat:petSessionData/tracer/transmissions_starttime'
]

CT_EXP_ATTRS = [
    'xnat:experimentData/date',
    'xnat:experimentData/visit_id',
    'xnat:experimentData/time',
    'xnat:experimentData/note',
    'xnat:experimentData/investigator/firstname',
    'xnat:experimentData/investigator/lastname',
    'xnat:imageSessionData/scanner/manufacturer',
    'xnat:imageSessionData/scanner/model',
    'xnat:imageSessionData/operator',
    'xnat:imageSessionData/dcmAccessionNumber',
    'xnat:imageSessionData/dcmPatientId',
    'xnat:imageSessionData/dcmPatientName',
    'xnat:imageSessionData/session_type',
    'xnat:imageSessionData/modality',
    'xnat:imageSessionData/UID'
]

PET_SCAN_ATTRS = [
    'xnat:imageScanData/type',
    'xnat:imageScanData/UID',
    'xnat:imageScanData/note',
    'xnat:imageScanData/quality',
    'xnat:imageScanData/condition',
    'xnat:imageScanData/series_description',
    'xnat:imageScanData/documentation',
    'xnat:imageScanData/frames',
    'xnat:imageScanData/scanner/manufacturer',
    'xnat:imageScanData/scanner/model',
    'xnat:imageScanData/startTime',
    'xnat:petScanData/parameters/orientation',
    'xnat:petScanData/parameters/originalFileName',
    'xnat:petScanData/parameters/systemType',
    'xnat:petScanData/parameters/fileType',
    'xnat:petScanData/parameters/transaxialFOV',
    'xnat:petScanData/parameters/acqType',
    'xnat:petScanData/parameters/facility',
    'xnat:petScanData/parameters/numPlanes',
    'xnat:petScanData/parameters/frames/numFrames',
    'xnat:petScanData/parameters/numGates',
    'xnat:petScanData/parameters/planeSeparation',
    'xnat:petScanData/parameters/binSize',
    'xnat:petScanData/parameters/dataType'
]

CT_SCAN_ATTRS = [
    'xnat:imageScanData/type',
    'xnat:imageScanData/UID',
    'xnat:imageScanData/note',
    'xnat:imageScanData/quality',
    'xnat:imageScanData/condition',
    'xnat:imageScanData/series_description',
    'xnat:imageScanData/documentation',
    'xnat:imageScanData/frames',
    'xnat:imageScanData/scanner/manufacturer',
    'xnat:imageScanData/scanner/model'
]

PROC_ATTRS = [
    'proc:genProcData/validation/status',
    'proc:genProcData/procstatus',
    'proc:genProcData/proctype',
    'proc:genProcData/procversion',
    'proc:genProcData/walltimeused',
    'proc:genProcData/memused'
]


def copy_attrs(src_obj, dest_obj, attr_list):
    """ Copies list of attributes form source to destination"""
    src_attrs = src_obj.attrs.mget(attr_list)
    src_list = dict(list(zip(attr_list, src_attrs)))

    # NOTE: For some reason need to set te again b/c a bug somewhere sets te
    # to sequence name
    te_key = 'xnat:mrScanData/parameters/te'
    if te_key in src_list:
        src_list[te_key] = src_obj.attrs.get(te_key)

    dest_obj.attrs.mset(src_list)
    return 0


def copy_attributes(src_obj, dest_obj):
    '''Copy attributes from src to dest'''
    src_type = src_obj.datatype()
    types = {'xnat:projectData': PROJ_ATTRS,
             'xnat:subjectData': SUBJ_ATTRS,
             'xnat:mrSessionData': MR_EXP_ATTRS,
             'xnat:petSessionData': PET_EXP_ATTRS,
             'xnat:ctSessionData': CT_EXP_ATTRS,
             'xnat:mrScanData': MR_SCAN_ATTRS,
             'xnat:petScanData': PET_SCAN_ATTRS,
             'xnat:ctScanData': CT_SCAN_ATTRS,
             'xnat:scScanData': SC_SCAN_ATTRS,
             'proc:genProcData': PROC_ATTRS,
             'xnat:otherDicomScanData': OTHER_DICOM_SCAN_ATTRS}

    try:
        copy_attrs(src_obj, dest_obj, types[src_type])
    except KeyError:
        print('ERROR:cannot copy attributes, unsupported datatype:' + src_type)


def subj_compare(item1, item2):
    '''Compare sort of items'''
    return cmp(item1.label(), item2.label())


def copy_file(src_f, dest_r, cache_d):
    '''
    Copy file from XNAT file source to XNAT resource destination,
    using local cache in between'''
    f_label = src_f.label()
    loc_f = cache_d + '/' + f_label

    # Make subdirectories
    loc_d = op.dirname(loc_f)
    if not op.exists(loc_d):
        os.makedirs(loc_d)

    try:
        # Download file
        if op.exists(loc_f) is False:
            src_f.get(loc_f)

        # Get File Attributes
        f_in_attrs = src_f.attributes()
        f_content = f_in_attrs.get('file_content')
        f_format = f_in_attrs.get('file_format')
        f_tags = f_in_attrs.get('file_tags')

        # Upload File
        if f_format and f_content and not f_tags:         # format & content
            dest_r.file(f_label).put(loc_f, f_format, f_content)
        elif f_format and not f_content and not f_tags:   # format only
            dest_r.file(f_label).put(loc_f, f_format)
        elif f_format and f_content and f_tags:   # format, content, & tags
            dest_r.file(f_label).put(loc_f, f_format, f_content)
        else:                                             # none
            dest_r.file(f_label).put(loc_f)

        # Delete local copy
        os.remove(loc_f)
    except Exception:
        print("ERROR:failed to copy file:%s, error=%s"
              % (f_label, sys.exc_info()[0]))


def copy_res_zip(src_r, dest_r, cache_d):
    '''
    Copy a resource from XNAT source to XNAT destination using local cache
    in between
    '''
    try:
        # Download zip of resource
        print('INFO:Downloading resource as zip...')
        cache_z = src_r.get(cache_d, extract=False)

        # Upload zip of resource
        print('INFO:Uploading resource as zip...')
        dest_r.put_zip(cache_z, extract=True)

        # Delete cached zip
        os.remove(cache_z)

    except IndexError:
        print('ERROR:failed to copy:%s:%s' % (cache_z, sys.exc_info()[0]))
        raise


def is_empty_resource(_res):
    '''Check if resource contains any files'''
    f_count = 0
    for f_in in _res.files().fetchall('obj'):
        f_count += 1
        break

    return f_count == 0

# copy_project and copy_subject are untested

# def copy_project(src_proj, dst_proj, proj_cache_dir):
#     '''Copy XNAT project from source to destination'''
#
#     if not dst_proj.exists():
#         try:
#             dst_proj.create()
#         except Exception as e:
#             msg = 'ERROR: can not create project on destination '\
#                 'xnat. Check your user rights. %s' % e
#             raise Exception(msg)
#         copy_attributes(src_proj, dst_proj)
#     print('INFO:loading and sorting subject list...')
#
#     proj_label = src_proj.label()
#     subj_list = src_xnat.get_subjects(proj_label)
#     subj_list = [x for x in subj_list if x['label'] not in SUBJECTS_MIRRORED]
#     subj_i = 0
#     for subj in subj_list:
#         subj_i += 1
#         subject_label = subj['label']
#
#         print("INFO:Processing subject %s (%d)..." % (subject_label, subj_i))
#         src_subj = src_proj.subject(subject_label)
#         dst_subj = dst_proj.subject(subject_label)
#         subj_cache_dir = op.join(proj_cache_dir, subject_label)
#         copy_subject(src_subj, dst_subj, subj_cache_dir)


# def copy_subject(src_subj, dst_subj, subj_cache_dir):
#     '''Copy subject from XNAT src to XNAT dst'''
#
#     if not dst_subj.exists():
#         print('INFO:uploading subject attributes as xml')
#
#         # Create dirs
#         if not op.exists(subj_cache_dir):
#             os.makedirs(subj_cache_dir)
#
#         # Write xml to file
#         subj_xml = src_subj.get()
#         xml_path = op.join(subj_cache_dir, 'subj.xml')
#         write_xml(subj_xml, xml_path)
#         dst_subj.create(xml=xml_path, allowDataDeletion=False)
#
#     # Process each experiment of subject
#     for src_sess in src_subj.experiments().fetchall('obj'):
#         sess_label = src_sess.label()
#         sess_type = src_sess.datatype()
#         if sess_type != 'xnat:mrSessionData' and \
#            sess_type != 'xnat:petSessionData' and \
#            sess_type != 'xnat:ctSessionData':
#             print('WARN:Skipping, session is not MR, CT, or PET Session')
#             continue
#
#         print("INFO:Processing session:%s..." % (sess_label))
#
#         dst_sess = dst_subj.experiment(sess_label)
#         sess_cache_dir = op.join(subj_cache_dir, sess_label)
#         copy_session(src_sess, dst_sess, sess_cache_dir)


def copy_session(src_sess, dst_sess, sess_cache_dir):
    '''Copy XNAT session from source to destination'''

    print('INFO:uploading session attributes as xml')
    # Write xml to file
    if not op.exists(sess_cache_dir):
        os.makedirs(sess_cache_dir)
    sess_xml = src_sess.get()
    xml_path = op.join(sess_cache_dir, 'sess.xml')
    write_xml(sess_xml, xml_path)

    sess_type = src_sess.datatype()
    dst_sess.create(experiments=sess_type)
    copy_attributes(src_sess, dst_sess)

    # Process each scan of session
    for src_scan in src_sess.scans().fetchall('obj'):
        scan_label = src_scan.label()

        print('INFO:Processing scan:%s...' % scan_label)
        dst_scan = dst_sess.scan(scan_label)
        scan_cache_dir = op.join(sess_cache_dir, scan_label)
        copy_scan(src_scan, dst_scan, scan_cache_dir)

    # Process each assessor of session
    for src_assr in src_sess.assessors():
        assr_label = src_assr.label()
        print('INFO:Processing assessor:%s:...' % assr_label)
        # dst_assr = dst_sess.assessor(assr_label)
        # assr_cache_dir = op.join(sess_cache_dir, assr_label)
        # copy_assr(src_assr, dst_assr, assr_cache_dir)

    for src_res in src_sess.resources().fetchall('obj'):
        res_label = src_res.label()

        print('INFO:Processing resource:%s...' % res_label)

        dst_res = dst_sess.resource(res_label)

        res_cache_dir = op.join(sess_cache_dir, res_label)

        copy_res(src_res, dst_res, res_cache_dir, use_zip=True)


def copy_scan(src_scan, dst_scan, scan_cache_dir):
    '''Copy scan from source XNAT to destination XNAT'''

    scan_type = src_scan.datatype()
    if scan_type == '':
        scan_type = 'xnat:otherDicomScanData'
    dst_scan.create(scans=scan_type)
    copy_attributes(src_scan, dst_scan)

    # Process each resource of scan
    for src_res in src_scan.resources().fetchall('obj'):
        res_label = src_res.label()

        print('INFO:Processing resource:%s...' % res_label)

        dst_res = dst_scan.resource(res_label)

        res_cache_dir = op.join(scan_cache_dir, res_label)
        if res_label == 'SNAPSHOTS':
            copy_res(src_res, dst_res, res_cache_dir)
        else:
            copy_res(src_res, dst_res, res_cache_dir, use_zip=True)


def copy_res(src_res, dst_res, res_cache_dir, use_zip=False):
    '''Copy resource from source XNAT to destination XNAT'''
    # Create cache dir
    if not op.exists(res_cache_dir):
        os.makedirs(res_cache_dir)

    # Prepare resource and check for empty
    is_empty = False
    print(dst_res._uri)
    if not dst_res.exists():
        dst_res.create()
        is_empty = True
    elif is_empty_resource(dst_res):
        is_empty = True

    # Check for empty source
    if is_empty_resource(src_res):
        print('WARN:empty resource, nothing to copy')
        return

    if is_empty:
        if use_zip:
            # Try to copy as zip
            try:
                print('INFO:Copying resource as zip: %s...' % src_res.label())
                copy_res_zip(src_res, dst_res, res_cache_dir)
                return
            except Exception:
                try:
                    print('INFO: second attempt to copy resource as zip: %s...'
                          % src_res.label())
                    copy_res_zip(src_res, dst_res, res_cache_dir)
                    return
                except Exception:
                    msg = 'ERROR:failed twice to copy resource as zip, will'\
                        'copy individual files'
                    print(msg)

        copy_count = 0
        for f in src_res.files():
            print('INFO:Copying file: %s...' % f.label())
            copy_count += 1
            copy_file(f, dst_res, res_cache_dir)
        print('INFO:Finished copying resource, %d files copied' % copy_count)


# def copy_assr(src_assr, dst_assr, assr_cache_dir):
#     '''Copy assessor from source XNAT to destination XNAT'''
#
#     # Check type
#     assr_type = src_assr.datatype()
#     if assr_type != 'proc:genProcData' and assr_type != 'fs:fsData':
#         print('WARN:skipping unsupported assessor type: {}'.format(assr_type))
#         return
#
#     if not dst_assr.exists():
#         print('INFO:uploading assessor attributes as xml')
#         # Write xml to file
#         if not op.exists(assr_cache_dir):
#             os.makedirs(assr_cache_dir)
#         assr_xml = src_assr.get()
#         xml_path = op.join(assr_cache_dir, 'assr.xml')
#         write_xml(assr_xml, xml_path)
#         dst_assr.create(xml=xml_path, allowDataDeletion=False)
#
#     # Process each resource of assr
#     for src_res in src_assr.out_resources():
#         res_label = src_res.label()
#         print('INFO:Processing resource:%s...' % res_label)
#         dst_res = dst_assr.out_resource(res_label)
#         res_cache_dir = op.join(assr_cache_dir, res_label)
#
#         if res_label == 'SNAPSHOTS':
#             copy_res(src_res, dst_res, res_cache_dir)
#         else:
#             copy_res(src_res, dst_res, res_cache_dir, use_zip=True)


def write_xml(xml_str, file_path, clean_tags=True):
    """Writing XML."""
    root = ET.fromstring(xml_str)

    # We only want the tags and attributes relevant to root, no children
    if clean_tags:
        # Remove ID
        if 'ID' in root.attrib:
            del root.attrib['ID']

        # Remove sharing tags
        tag = '{http://nrg.wustl.edu/xnat}sharing'
        for child in root.findall(tag):
            root.remove(child)

        # Remove out
        for child in root.findall('{http://nrg.wustl.edu/xnat}out'):
            root.remove(child)
            break

        # Remove session ID
        tag = '{http://nrg.wustl.edu/xnat}imageSession_ID'
        for child in root.findall(tag):
            root.remove(child)

        # Remove subject ID
        for child in root.findall('{http://nrg.wustl.edu/xnat}subject_ID'):
            root.remove(child)

        # Remove _session ID
        tag = '{http://nrg.wustl.edu/xnat}image_session_ID'
        for child in root.findall(tag):
            root.remove(child)

        # Remove scans
        for child in root.findall('{http://nrg.wustl.edu/xnat}scans'):
            root.remove(child)
            break

        # Remove assessors
        for child in root.findall('{http://nrg.wustl.edu/xnat}assessors'):
            root.remove(child)
            break

        # Remove resources
        for child in root.findall('{http://nrg.wustl.edu/xnat}resources'):
            root.remove(child)
            break

        # Remove experiments
        for child in root.findall('{http://nrg.wustl.edu/xnat}experiments'):
            root.remove(child)
            break

    try:
        # Write to file
        ET.register_namespace('xnat', 'http://nrg.wustl.edu/xnat')
        ET.register_namespace('proc', 'http://nrg.wustl.edu/proc')
        ET.register_namespace('prov', 'http://www.nbirn.net/prov')
        ET.register_namespace('fs', 'http://nrg.wustl.edu/fs')
        ET.ElementTree(root).write(file_path)
    except IOError as error:
        print('ERROR:writing xml file: {}: {}'.format(file_path, str(error)))


def create_parser():
    import argparse
    """Parse commandline arguments."""
    arg_parser = argparse.ArgumentParser(
        description='Downloads a given experiment/session from an XNAT instance '
                    'and uploads it to an independent one. Only DICOM resources '
                    'will be imported.',
        formatter_class=argparse.RawTextHelpFormatter)
    arg_parser.add_argument(
        '--h1', '--source_config', dest='source_config',
        help='Source XNAT configuration file', required=True)
    arg_parser.add_argument(
        '--h2', '--dest_config', dest='dest_config', required=True,
        help='Destination XNAT configuration file')
    arg_parser.add_argument(
        '-e', '--experiment_id', required=True,
        help='Which resource to download? (Entity name/identifier)')
    arg_parser.add_argument(
        '-p', '--project_id', dest='project_id', required=True,
        help='Which project to store the resource in')
    arg_parser.add_argument(
        '-v', '--verbose', dest='verbose', action='store_true', default=False,
        help='Display verbosal information (optional)', required=False)

    return arg_parser


def main(args):
    x1 = pyxnat.Interface(config=args.source_config)
    x2 = pyxnat.Interface(config=args.dest_config)

    columns = ['subject_label', 'label']
    e1 = x1.array.experiments(experiment_id=args.experiment_id,
                              columns=columns).data[0]
    p = x2.select.project(args.project_id)
    s = p.subject(e1['subject_label'])
    if not s.exists():
        s.create()
    e = s.experiment(e1['label'])

    src_sess = x1.select.project(e1['project']).subject(e1['subject_ID']).experiment(e1['ID'])
    dst_sess = e

    copy_session(src_sess, dst_sess, '/tmp')


if __name__ == '__main__':
    parser = create_parser()
    arguments = parser.parse_args()
    main(arguments)