Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor with with #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions check_for_subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
dest_project = sys.argv[1]
subject = sys.argv[2]

xnat = XnatUtils.InterfaceTemp()

# Subject variables
dest_subject = xnat.select(
'/projects/{0}/subjects/{1}'.format(dest_project,subject) )

if dest_subject.exists():
xnat.disconnect()
raise Exception('Subject {0} exists in project {1}'.format(subject,dest_project))
sys.exit(1)

xnat.disconnect()
with XnatUtils.InterfaceTemp() as xnat:
dest_subject = xnat.select(f'/projects/{dest_project}/subjects/{subject}')
if dest_subject.exists():
raise Exception(f'Subject {subject} exists in project {dest_project}')
sys.exit(1)
62 changes: 27 additions & 35 deletions set_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,31 @@
dest_project = sys.argv[2]
subject = sys.argv[3]

xnat = XnatUtils.InterfaceTemp()

# Subject variables
source_subject = xnat.select(
'/projects/{0}/subjects/{1}'.format(source_project,subject) )
dest_subject = xnat.select(
'/projects/{0}/subjects/{1}'.format(dest_project,subject) )

dob = source_subject.attrs.get('xnat:demographicData/dob')
gender = source_subject.attrs.get('xnat:demographicData/gender')
handedness = source_subject.attrs.get('xnat:demographicData/handedness')
race = source_subject.attrs.get('xnat:demographicData/race')
#sid = source_subject.attrs.get("xnat:subjectData/fields/field[name='id']/field")
sid = source_subject.xpath("/xnat:Subject/xnat:fields/xnat:field[@name='id']/text()[2]")[0]

dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/dob', dob)
dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/gender', gender)
dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/handedness', handedness)
dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/race', race)
dest_subject.attrs.set("xnat:subjectData/fields/field[name='id']/field", sid)

# Session variables
source_session = xnat.select(
'/projects/{0}/subjects/{1}/experiments/{1}'.format(source_project,subject) )
dest_session = xnat.select(
'/projects/{0}/subjects/{1}/experiments/{1}'.format(dest_project,subject) )

sdate = source_session.attrs.get('xnat:mrSessionData/date')
stime = source_session.attrs.get('xnat:mrSessionData/time')
dest_session.attrs.set('xnat:mrSessionData/date',sdate)
dest_session.attrs.set('xnat:mrSessionData/time',stime)


# Clean up
xnat.disconnect()
with XnatUtils.InterfaceTemp() as xnat:

# Subject variables
source_subject = xnat.select(f'/projects/{source_project}/subjects/{subject}')
dest_subject = xnat.select(f'/projects/{dest_project}/subjects/{subject}')

dob = source_subject.attrs.get('xnat:demographicData/dob')
gender = source_subject.attrs.get('xnat:demographicData/gender')
handedness = source_subject.attrs.get('xnat:demographicData/handedness')
race = source_subject.attrs.get('xnat:demographicData/race')
#sid = source_subject.attrs.get("xnat:subjectData/fields/field[name='id']/field")
sid = source_subject.xpath("/xnat:Subject/xnat:fields/xnat:field[@name='id']/text()[2]")[0]

dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/dob', dob)
dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/gender', gender)
dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/handedness', handedness)
dest_subject.attrs.set('xnat:subjectData/demographics[@xsi:type=xnat:demographicData]/race', race)
dest_subject.attrs.set("xnat:subjectData/fields/field[name='id']/field", sid)

# Session variables
source_session = xnat.select(f'/projects/{source_project}/subjects/{subject}/experiments/{subject}')
dest_session = xnat.select(f'/projects/{dest_project}/subjects/{subject}/experiments/{subject}')

sdate = source_session.attrs.get('xnat:mrSessionData/date')
stime = source_session.attrs.get('xnat:mrSessionData/time')
dest_session.attrs.set('xnat:mrSessionData/date',sdate)
dest_session.attrs.set('xnat:mrSessionData/time',stime)