예제 #1
0
def import_session(dsc, src, project, xnatSession, timezone='UTC'):
    if project == None:
        return

    try:
        attrs = xnatSession.attrs
        dtype = xnat_api(xnatSession.datatype)
        #print(dtype)
        _log.info('    Importing session: ' + dtype)

        dateString = xnat_api(attrs.get, dtype + '/date')
        timeString = xnat_api(attrs.get, dtype + '/time')
        purpose = xnat_api(attrs.get, dtype + '/note')
        if not purpose: #None or len 0
            purpose = 'XNAT experiment ' + xnatSession._uri

        dateTimeString = dateString + ' ' + timeString
        if timeString:
            format = DATETIME_FORMAT if timeString.find('.') > 0 else DATETIMESHORT_FORMAT
        else:
            format = DATE_FORMAT

        startTime = datetime.fromtimestamp(mktime(strptime(dateTimeString, format)))

        exp = project.insertExperiment(purpose, to_joda_datetime(startTime, timezone))
        _import_entity_common(exp, xnatSession)

        scanIDs = xnat_api(xnatSession.scans().get)
        for scanID in scanIDs:
            import_scan(dsc, src, exp, xnat_api(xnatSession.scan, scanID), timezone=timezone)

        return exp
    except DatabaseError, e:
        _log.exception("Unable to import session: " + e.message)
        return None
예제 #2
0
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
예제 #3
0
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
예제 #4
0
    def should_import_session_from_subject_as_experiments_with_one_epochgroup(self):
        xnatProject,projectURI = self._import_first_project()

        ctx = self.dsc.getContext()
        project = ctx.objectdWithURI(projectURI)

        for xnatSession in self.xnat.select('/projects/' + xnat_api(xnatProject.id) + '/subjects/*/experiments/*'):
            attrs = xnatSession.attrs
            dtype = xnat_api(xnatSession.datatype)
            datestr = xnat_api(attrs.get, dtype + '/date') + ' ' + xnat_api(attrs.get, dtype + '/time')
            startTime = to_joda_datetime(datestr, 'UTC')
            exps = project.getExperiments(startTime)
예제 #5
0
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
예제 #6
0
    def should_import_session_from_subject_as_experiments_with_one_epochgroup(
            self):
        xnatProject, projectURI = self._import_first_project()

        ctx = self.dsc.getContext()
        project = ctx.objectdWithURI(projectURI)

        for xnatSession in self.xnat.select('/projects/' +
                                            xnat_api(xnatProject.id) +
                                            '/subjects/*/experiments/*'):
            attrs = xnatSession.attrs
            dtype = xnat_api(xnatSession.datatype)
            datestr = xnat_api(attrs.get, dtype + '/date') + ' ' + xnat_api(
                attrs.get, dtype + '/time')
            startTime = to_joda_datetime(datestr, 'UTC')
            exps = project.getExperiments(startTime)
예제 #7
0
def import_session(dsc, src, project, xnatSession, timezone='UTC'):
    if project == None:
        return

    try:
        attrs = xnatSession.attrs
        dtype = xnat_api(xnatSession.datatype)
        #print(dtype)
        _log.info('    Importing session: ' + dtype)

        dateString = xnat_api(attrs.get, dtype + '/date')
        timeString = xnat_api(attrs.get, dtype + '/time')
        purpose = xnat_api(attrs.get, dtype + '/note')
        if not purpose:  #None or len 0
            purpose = 'XNAT experiment ' + xnatSession._uri

        dateTimeString = dateString + ' ' + timeString
        if timeString:
            format = DATETIME_FORMAT if timeString.find(
                '.') > 0 else DATETIMESHORT_FORMAT
        else:
            format = DATE_FORMAT

        startTime = datetime.fromtimestamp(
            mktime(strptime(dateTimeString, format)))

        exp = project.insertExperiment(purpose,
                                       to_joda_datetime(startTime, timezone))
        _import_entity_common(exp, xnatSession)

        scanIDs = xnat_api(xnatSession.scans().get)
        for scanID in scanIDs:
            import_scan(dsc,
                        src,
                        exp,
                        xnat_api(xnatSession.scan, scanID),
                        timezone=timezone)

        return exp
    except DatabaseError, e:
        _log.exception("Unable to import session: " + e.message)
        return None
예제 #8
0
    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)
예제 #9
0
    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)