def import_project(dsc, xnatProject, timezone='UTC', importProjectTree=True): """ Import a single XNAT project. """ projectID = xnat_api(xnatProject.id) _log.info('Importing project: ' + projectID) name = xnat_api(xnatProject.attrs.get, 'xnat:projectData/name') purpose = xnat_api(xnatProject.attrs.get, 'xnat:projectData/description') if not purpose: purpose = "<None>" # Find the earliest session date in the project xnat = xnatProject._intf sessionTypes = xnat.inspect.experiment_types() if len(sessionTypes) == 0: sessionTypes = ('xnat:mrSessionData', 'xnat:ctSessionData') #raise XnatImportError("No session types defined in database") minSessionDate = None for sessionType in sessionTypes: columns = (sessionType + '/DATE', sessionType + '/PROJECT') xnat_api_pause() query = xnat.select(sessionType, columns=columns).where([ (sessionType + '/Project', '=', projectID), 'AND' ]) sessionDates = [ datetime.fromtimestamp(mktime(strptime(row['date'], DATE_FORMAT))) for row in query if len(row['date']) > 0 ] if len(sessionDates) > 0: for sd in sessionDates: if minSessionDate is not None: if sd < minSessionDate: minSessionDate = sd else: minSessionDate = sd if minSessionDate is not None: startTime = to_joda_datetime(minSessionDate, timezone) else: startTime = api.datetime() ctx = dsc.getContext() project = ctx.insertProject(name, purpose, startTime) _import_entity_common(project, xnatProject) if importProjectTree: _log.info(" Importing project tree...") subjectIDs = xnat_api(xnatProject.subjects().get) for subjectID in subjectIDs: s = xnat_api(xnatProject.subject, subjectID) import_subject(dsc, s, timezone=timezone, project=project) return project
def import_project(dsc, xnatProject, timezone='UTC', importProjectTree=True): """ Import a single XNAT project. """ projectID = xnat_api(xnatProject.id) _log.info('Importing project: ' + projectID) name = xnat_api(xnatProject.attrs.get,'xnat:projectData/name') purpose = xnat_api(xnatProject.attrs.get, 'xnat:projectData/description') if not purpose: purpose = "<None>" # Find the earliest session date in the project xnat = xnatProject._intf sessionTypes = xnat.inspect.experiment_types() if len(sessionTypes) == 0: sessionTypes = ('xnat:mrSessionData', 'xnat:ctSessionData') #raise XnatImportError("No session types defined in database") minSessionDate = None for sessionType in sessionTypes: columns = (sessionType + '/DATE', sessionType + '/PROJECT') xnat_api_pause() query = xnat.select(sessionType, columns=columns).where([(sessionType + '/Project', '=', projectID), 'AND']) sessionDates = [ datetime.fromtimestamp(mktime(strptime(row['date'], DATE_FORMAT))) for row in query if len(row['date']) > 0] if len(sessionDates) > 0: for sd in sessionDates: if minSessionDate is not None: if sd < minSessionDate: minSessionDate = sd else: minSessionDate = sd if minSessionDate is not None: startTime = to_joda_datetime(minSessionDate, timezone) else: startTime = api.datetime() ctx = dsc.getContext() project = ctx.insertProject(name, purpose, startTime) _import_entity_common(project, xnatProject) if importProjectTree: _log.info(" Importing project tree...") subjectIDs = xnat_api(xnatProject.subjects().get) for subjectID in subjectIDs: s = xnat_api(xnatProject.subject, subjectID) import_subject(dsc, s, timezone=timezone, project=project) return project
def should_import_project_keywords(self): xnatProject,projectURI = self._import_first_project() ctx = self.dsc.getContext() project = ctx.objectdWithURI(projectURI) xnat_api_pause() expectedKeywords = xnatProject.attrs.get('xnat:projectData/keywords').split() for k in expectedKeywords: assert k in project.getTags()
def should_import_project_keywords(self): xnatProject, projectURI = self._import_first_project() ctx = self.dsc.getContext() project = ctx.objectdWithURI(projectURI) xnat_api_pause() expectedKeywords = xnatProject.attrs.get( 'xnat:projectData/keywords').split() for k in expectedKeywords: assert k in project.getTags()
def should_import_project_name_and_purpose_from_entity_attributes(self): xnatProject, projectURI = self._import_first_project() ctx = self.dsc.getContext() project = ctx.objectdWithURI(projectURI) xnat_api_pause() expectedName = xnatProject.attrs.get('xnat:projectData/name') xnat_api_pause() expectedPurpose = xnatProject.attrs.get('xnat:projectData/description') eq_(project.getName(), expectedName) eq_(project.getPurpose(), expectedPurpose)
def should_import_project_name_and_purpose_from_entity_attributes(self): xnatProject,projectURI = self._import_first_project() ctx = self.dsc.getContext() project = ctx.objectdWithURI(projectURI) xnat_api_pause() expectedName = xnatProject.attrs.get('xnat:projectData/name') xnat_api_pause() expectedPurpose = xnatProject.attrs.get('xnat:projectData/description') eq_(project.getName(), expectedName) eq_(project.getPurpose(), expectedPurpose)
def import_project(dsc, xnatProject, timezone="UTC"): """ Import a single XNAT project. """ projectID = xnat_api(xnatProject.id) name = xnat_api(xnatProject.attrs.get, "xnat:projectData/name") purpose = xnat_api(xnatProject.attrs.get, "xnat:projectData/description") # Find the earliest session date in the project xnat = xnatProject._intf sessionTypes = xnat.inspect.experiment_types() if len(sessionTypes) == 0: sessionTypes = ("xnat:mrSessionData", "xnat:ctSessionData") # raise XnatImportError("No session types defined in database") minSessionDate = None for sessionType in sessionTypes: columns = (sessionType + "/DATE", sessionType + "/PROJECT") xnat_api_pause() query = xnat.select(sessionType, columns=columns).where([(sessionType + "/Project", "=", projectID), "AND"]) sessionDates = [ datetime.fromtimestamp(mktime(strptime(row["date"], DATE_FORMAT))) for row in query if len(row["date"]) > 0 ] if len(sessionDates) > 0: for sd in sessionDates: if minSessionDate is not None: if sd < minSessionDate: minSessionDate = sd else: minSessionDate = sd if minSessionDate is not None: startTime = to_joda_datetime(minSessionDate, timezone) else: startTime = api.datetime() ctx = dsc.getContext() project = ctx.insertProject(name, purpose, startTime) _import_entity_common(project, xnatProject) for s in iterate_entity_collection(xnatProject.subjects): src = import_source(dsc, s) for session in iterate_entity_collection(s.sessions): import_session(dsc, src, project, session) return project
def should_set_project_start_date_from_earliest_session_date(self): xnatProject, projectURI = self._import_first_project() # Find the earliest session date in the project projectID = xnat_api(xnatProject.id) xnat = xnatProject._intf sessionTypes = xnat.inspect.experiment_types() if len(sessionTypes) == 0: sessionTypes = ('xnat:mrSessionData', 'xnat:ctSessionData') #raise XnatImportError("No session types defined in database") minSessionDate = None for sessionType in sessionTypes: columns = (sessionType + '/DATE', sessionType + '/PROJECT') xnat_api_pause() query = xnat.select(sessionType, columns=columns).where([ (sessionType + '/Project', '=', projectID), 'AND' ]) sessionDates = [ datetime.fromtimestamp( mktime(strptime(time_str['date'], DATE_FORMAT))) for time_str in query if len(time_str['date']) > 0 ] if len(sessionDates) > 0: for sd in sessionDates: if minSessionDate is not None: if sd < minSessionDate: minSessionDate = sd else: minSessionDate = sd if minSessionDate is not None: startTime = to_joda_datetime(minSessionDate, 'UTC') else: self.fail("Unable to find project sessions' start date") ctx = self.dsc.getContext() project = ctx.objectdWithURI(projectURI) eq_(project.getStartTime(), startTime)
def should_set_project_start_date_from_earliest_session_date(self): xnatProject,projectURI = self._import_first_project() # Find the earliest session date in the project projectID = xnat_api(xnatProject.id) xnat = xnatProject._intf sessionTypes = xnat.inspect.experiment_types() if len(sessionTypes) == 0: sessionTypes = ('xnat:mrSessionData', 'xnat:ctSessionData') #raise XnatImportError("No session types defined in database") minSessionDate = None for sessionType in sessionTypes: columns = (sessionType + '/DATE', sessionType + '/PROJECT') xnat_api_pause() query = xnat.select(sessionType, columns=columns).where([(sessionType + '/Project', '=', projectID), 'AND']) sessionDates = [ datetime.fromtimestamp(mktime(strptime(time_str['date'], DATE_FORMAT))) for time_str in query if len(time_str['date']) > 0] if len(sessionDates) > 0: for sd in sessionDates: if minSessionDate is not None: if sd < minSessionDate: minSessionDate = sd else: minSessionDate = sd if minSessionDate is not None: startTime = to_joda_datetime(minSessionDate, 'UTC') else: self.fail("Unable to find project sessions' start date") ctx = self.dsc.getContext() project = ctx.objectdWithURI(projectURI) eq_(project.getStartTime(), startTime)