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

Populating Project/Subproject/Current_Stage in Loris DB #558

Merged
merged 3 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__pycache__/*
*.pyc
.loris_mri/
71 changes: 46 additions & 25 deletions python/bids_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,28 @@ def read_and_insert_bids(bids_dir, config_file, verbose, createcand, createvisit
loris_cand_info = grep_or_create_candidate_db_info(
bids_reader, bids_id, db, createcand, loris_bids_root_dir, verbose
)
cand_id = loris_cand_info['CandID']
center_id = loris_cand_info['RegistrationCenterID']
cand_id = loris_cand_info['CandID']
center_id = loris_cand_info['RegistrationCenterID']
project_id = loris_cand_info['RegistrationProjectID']

subproject_id = None
if 'subproject' in bids_subject_info:
subproject = bids_subject_info['subproject']
subproject_info = db.pselect(
"SELECT SubprojectID FROM subproject WHERE title = %s",
[subproject,]
)
if(len(subproject_info) > 0):
subproject_id = subproject_info[0]['SubprojectID']

# greps BIDS session's info for the candidate from LORIS (creates the
# session if it does not exist yet in LORIS and the createvisit is set
# to true. If no visit in BIDS structure, then use default visit_label
# stored in the Config module)
loris_sessions_info = grep_candidate_sessions_info(
bids_sessions, bids_id, cand_id, loris_bids_root_dir,
createvisit, verbose, db, default_bids_vl,
center_id
bids_sessions, bids_id, cand_id, loris_bids_root_dir,
createvisit, verbose, db, default_bids_vl,
center_id, project_id, subproject_id
)

# read list of modalities per session / candidate and register data
Expand Down Expand Up @@ -333,35 +344,43 @@ def grep_or_create_candidate_db_info(bids_reader, bids_id, db,
return loris_cand_info


def grep_or_create_visit_label_db_info(
bids_id, cand_id, visit_label, db, createvisit,
verbose, loris_bids_dir, center_id):
def grep_or_create_session_db_info(
bids_id, cand_id, visit_label,
db, createvisit, verbose, loris_bids_dir,
center_id, project_id, subproject_id):
"""
Greps (or creates if candidate does not exist and createcand is true) the
BIDS candidate in the LORIS candidate's table and return a list of
candidates with their related fields from the database.
Greps (or creates if session does not exist and createvisit is true) the
BIDS session in the LORIS session's table and return a list of
sessions with their related fields from the database.

:parma bids_id : BIDS ID of the candidate
:parma bids_id : BIDS ID of the session
:type bids_id : str
:param cand_id : CandID to use to create the session
:type cand_id : int
:param visit_label : Visit label to use to create the session
:type visit_label : str
:param db : database handler object
:type db : object
:param createvisit : if true, creates the candidate in LORIS
:param createvisit : if true, creates the session in LORIS
:type createvisit : bool
:param verbose : if true, prints out information while executing
:type verbose : bool
:param loris_bids_dir: LORIS BIDS import root directory to copy data
:type loris_bids_dir: str
:param center_id : CenterID to use to create the session
:type center_id : int
:param project_id : ProjectID to use to create the session
:type project_id : int
:param subproject_id : SubprojectID to use to create the session
:type subproject_id : int

:return: session information grepped from LORIS for cand_id and visit_label
:rtype: dict
"""

session = Session(
verbose, cand_id=cand_id, visit_label=visit_label, center_id=center_id
verbose, cand_id, visit_label,
center_id, project_id, subproject_id
)
loris_vl_info = session.get_session_info_from_loris(db)

Expand All @@ -378,9 +397,9 @@ def grep_or_create_visit_label_db_info(
return loris_vl_info


def grep_candidate_sessions_info(bids_ses, bids_id, cand_id, loris_bids_dir,
createvisit, verbose, db, default_vl,
center_id):
def grep_candidate_sessions_info(bids_ses, bids_id, cand_id, loris_bids_dir,
createvisit, verbose, db, default_vl,
center_id, project_id, subproject_id):
"""
Greps all session info dictionaries for a given candidate and aggregates
them into a list, with one entry per session. If the session does not
Expand Down Expand Up @@ -413,18 +432,20 @@ def grep_candidate_sessions_info(bids_ses, bids_id, cand_id, loris_bids_dir,
loris_sessions_info = []

if not bids_ses:
loris_vl_info = grep_or_create_visit_label_db_info(
bids_id, cand_id, default_vl, db,
createvisit, verbose, loris_bids_dir, center_id
loris_ses_info = grep_or_create_session_db_info(
bids_id, cand_id, default_vl, db,
createvisit, verbose, loris_bids_dir,
center_id, project_id, subproject_id
)
loris_sessions_info.append(loris_vl_info)
loris_sessions_info.append(loris_ses_info)
else:
for visit_label in bids_ses:
loris_vl_info = grep_or_create_visit_label_db_info(
bids_id, cand_id, visit_label, db,
createvisit, verbose, loris_bids_dir, center_id
loris_ses_info = grep_or_create_session_db_info(
bids_id, cand_id, visit_label, db,
createvisit, verbose, loris_bids_dir,
center_id, project_id, subproject_id
)
loris_sessions_info.append(loris_vl_info)
loris_sessions_info.append(loris_ses_info)

return loris_sessions_info

Expand Down
35 changes: 24 additions & 11 deletions python/lib/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ def __init__(self, verbose, psc_id=None, cand_id=None, sex=None, dob=None):
self.verbose = verbose

# create the candidate object
self.psc_id = psc_id
self.cand_id = cand_id
self.sex = sex
self.dob = dob
self.age = None
self.site = None
self.center_id = None
self.psc_id = psc_id
self.cand_id = cand_id
self.sex = sex
self.dob = dob
self.age = None
self.site = None
self.center_id = None
self.project_id = None

def create_candidate(self, db, participants_info):
"""
Expand Down Expand Up @@ -84,13 +85,21 @@ def create_candidate(self, db, participants_info):
self.age = row['age']
if 'site' in row:
self.site = row['site']
if 'project' in row:
project_info = db.pselect(
"SELECT ProjectID FROM Project WHERE Alias = %s",
[row['project'],]
)
if(len(project_info) > 0):
self.project_id = project_info[0]['ProjectID']

if self.site:
site_info = db.pselect(
"SELECT CenterID FROM psc WHERE Alias = %s",
[self.site,]
)
self.center_id = site_info[0]['CenterID']
if(len(site_info) > 0):
self.center_id = site_info[0]['CenterID']
else:
db_sites = db.pselect("SELECT CenterID, Alias FROM psc")
for site in db_sites:
Expand All @@ -107,9 +116,10 @@ def create_candidate(self, db, participants_info):

if self.verbose:
print("Creating candidate with " + \
" PSCID = " + self.psc_id + "," + \
" CandID = " + str(self.cand_id) + \
" and Site = " + str(self.site))
"PSCID = " + self.psc_id + ", " + \
"CandID = " + str(self.cand_id) + ", " + \
"Site = " + str(self.site) + " and " + \
"Project = " + str(self.project_id))

insert_col = ('PSCID', 'CandID', 'RegistrationCenterID')
insert_val = (self.psc_id, str(self.cand_id), str(self.center_id))
Expand All @@ -120,6 +130,9 @@ def create_candidate(self, db, participants_info):
if self.dob:
insert_col = insert_col + ('DoB',)
insert_val = insert_val + (self.dob,)
if self.project_id:
insert_col = insert_col + ('RegistrationProjectID',)
insert_val = insert_val + (str(self.project_id),)

db.insert(
table_name='candidate',
Expand Down
21 changes: 17 additions & 4 deletions python/lib/eeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ def __init__(self, bids_reader, bids_sub_id, bids_ses_id, bids_modality, db,
self.psc_id = self.loris_cand_info['PSCID']
self.cand_id = self.loris_cand_info['CandID']
self.center_id = self.loris_cand_info['RegistrationCenterID']
self.project_id = self.loris_cand_info['RegistrationProjectID']

self.subproject_id = None
for row in bids_reader.participants_info:
if not row['participant_id'] == self.psc_id:
continue
if 'subproject' in row:
subproject_info = db.pselect(
"SELECT SubprojectID FROM subproject WHERE title = %s",
[row['subproject'],]
)
if(len(subproject_info) > 0):
self.subproject_id = subproject_info[0]['SubprojectID']
break

self.session_id = self.get_loris_session_id()

# grep the channels, electrodes, eeg and events files
Expand Down Expand Up @@ -177,10 +192,8 @@ def get_loris_session_id(self):
visit_label = self.bids_ses_id if self.bids_ses_id else self.default_vl

session = Session(
verbose = self.verbose,
cand_id = self.cand_id,
center_id = self.center_id,
visit_label = visit_label
self.verbose, self.cand_id, visit_label,
self.center_id, self.project_id, self.subproject_id
)
loris_vl_info = session.get_session_info_from_loris(self.db)

Expand Down
21 changes: 17 additions & 4 deletions python/lib/mri.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ def __init__(self, bids_reader, bids_sub_id, bids_ses_id, bids_modality, db,
self.psc_id = self.loris_cand_info['PSCID']
self.cand_id = self.loris_cand_info['CandID']
self.center_id = self.loris_cand_info['RegistrationCenterID']
self.project_id = self.loris_cand_info['RegistrationProjectID']

self.subproject_id = None
for row in bids_reader.participants_info:
if not row['participant_id'] == self.psc_id:
continue
if 'subproject' in row:
subproject_info = db.pselect(
"SELECT SubprojectID FROM subproject WHERE title = %s",
[row['subproject'],]
)
if(len(subproject_info) > 0):
self.subproject_id = subproject_info[0]['SubprojectID']
break

self.session_id = self.get_loris_session_id()

# grep all the NIfTI files for the modality
Expand Down Expand Up @@ -163,10 +178,8 @@ def get_loris_session_id(self):
visit_label = self.bids_ses_id if self.bids_ses_id else self.default_vl

session = Session(
verbose = self.verbose,
cand_id = self.cand_id,
center_id = self.center_id,
visit_label = visit_label
self.verbose, self.cand_id, visit_label,
self.center_id, self.project_id, self.subproject_id
)
loris_vl_info = session.get_session_info_from_loris(self.db)

Expand Down
50 changes: 35 additions & 15 deletions python/lib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Session:
db.connect()

session = Session(
verbose, cand_id=cand_id, visit_label=visit_label, center_id=center_id
verbose, cand_id, visit_label,
center_id, project_id, subproject_id
)

# grep session information from the database
Expand All @@ -32,25 +33,33 @@ class Session:
db.disconnect()
"""

def __init__(self, verbose, cand_id, visit_label, center_id):
def __init__(self, verbose, cand_id, visit_label,
center_id, project_id, subproject_id):
"""
Constructor method for the Session class.

:param verbose : whether to be verbose
:type verbose : bool
:param cand_id : candidate's CandID
:type cand_id : int
:param visit_label: visit label
:type visit_label: str
:param center_id : center ID to associate with the session
:type center_id : int
:param verbose : whether to be verbose
:type verbose : bool
:param cand_id : candidate's CandID
:type cand_id : int
:param visit_label : visit label
:type visit_label : str
:param center_id : center ID to associate with the session
:type center_id : int
:param project_id : project ID to associate with the session
:type project_id : int
:param subproject_id: subproject ID to associate with the session
:type subproject_id: int
"""
self.verbose = verbose

self.cand_id = str(cand_id)
self.visit_label = visit_label
self.center_id = center_id
self.cand_id = str(cand_id)
self.visit_label = visit_label
self.center_id = center_id
self.project_id = project_id
self.subproject_id = subproject_id


def create_session(self, db):
"""
Creates a session using BIDS information.
Expand All @@ -66,10 +75,21 @@ def create_session(self, db):
print("Creating visit " + self.visit_label \
+ " for CandID " + self.cand_id)

column_names = ('CandID', 'Visit_label', 'CenterID')
values = (self.cand_id, self.visit_label, str(self.center_id))

if self.project_id:
column_names = column_names + ('ProjectID',)
values = values + (str(self.project_id),)

if self.subproject_id:
column_names = column_names + ('SubprojectID',)
values = values + (str(self.subproject_id),)

db.insert(
table_name='session',
column_names=('CandID', 'Visit_label', 'CenterID'),
values=(self.cand_id, self.visit_label, str(self.center_id))
column_names=column_names,
values=values
)

loris_session_info = self.get_session_info_from_loris(db)
Expand Down