Exemplo n.º 1
0
    def should_import_epoch_per_file(self):
        """
        Should import files as URLResponses, not as Resources
        """
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)
        sessionType = xnatSession.datatype()
        sessionScanner = xnatSession.attrs.get(sessionType + '/scanner')

        for scanID in xnatSession.scans().get():
            s = xnatSession.scan(scanID)
            id = s.attrs.get('xnat:mrScanData/type')
            g = exp.getEpochGroupsWithLabel(id).iterator().next()
            for r in s.resources():
                label = r.label()
                typeGroup = g.getChildren(label)[0]
                self.assertEqual(typeGroup.getEpochCount(), len(r.files()))

                for epoch in typeGroup.getEpochs():
                    self.assertEqual(len(epoch.getResponseNames()), 1)
                    r = epoch.getResponse(sessionScanner)
                    self.assertIsNotNone(r)
Exemplo n.º 2
0
    def should_import_epoch_per_file(self):
        """
        Should import files as URLResponses, not as Resources
        """
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)
        sessionType = xnatSession.datatype()
        sessionScanner = xnatSession.attrs.get(sessionType + '/scanner')

        for scanID in xnatSession.scans().get():
            s = xnatSession.scan(scanID)
            id = s.attrs.get('xnat:mrScanData/type')
            g = exp.getEpochGroupsWithLabel(id).iterator().next()
            for r in s.resources():
                label = r.label()
                typeGroup = g.getChildren(label)[0]
                self.assertEqual(typeGroup.getEpochCount(), len(r.files()))

                for epoch in typeGroup.getEpochs():
                    self.assertEqual(len(epoch.getResponseNames()), 1)
                    r = epoch.getResponse(sessionScanner)
                    self.assertIsNotNone(r)
Exemplo n.º 3
0
def to_joda_datetime(date, timezone):
    return api.datetime(date.year,
        date.month,
        date.day,
        date.hour,
        date.minute,
        int(date.microsecond / 1000),
        api.timezone_with_id(timezone))
Exemplo n.º 4
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
Exemplo n.º 5
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
    def should_import_session_from_subject_as_experiments(self):
        xnatSubject = subject_mock("1", "PROJECT_NAME")

        ctx = self.dsc.getContext()
        project = ctx.insertProject("session_import", "session_import", api.datetime())
        s = import_subject(self.dsc, xnatSubject, project=project)

        # Assertions
        self.assertGreaterEqual(len(xnatSubject.experiments().get()), 1)
        self.assertEqual(len(project.getExperiments()), len(xnatSubject.experiments().get()))
Exemplo n.º 7
0
    def should_import_epochgroup_per_scan_with_scantype_as_label(self):
        ctx = self.dsc.getContext()
        project = ctx.insertProject('session_import', 'session_import', api.datetime())
        src = ctx.insertSource('test')

        xnatSession = session_mock()

        exp = import_session(self.dsc, src, project, xnatSession)

        self.assertGreaterEqual(xnatSession.scans().get(), 1)
        self.assertEqual(len(exp.getEpochGroups()), len(xnatSession.scans().get()))
    def should_import_session_from_subject_as_experiments(self):
        xnatSubject = subject_mock("1", "PROJECT_NAME")

        ctx = self.dsc.getContext()
        project = ctx.insertProject('session_import', 'session_import',
                                    api.datetime())
        s = import_subject(self.dsc, xnatSubject, project=project)

        # Assertions
        self.assertGreaterEqual(len(xnatSubject.experiments().get()), 1)
        self.assertEqual(len(project.getExperiments()),
                         len(xnatSubject.experiments().get()))
Exemplo n.º 9
0
    def should_set_scan_datatype(self):

        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)

        for g in exp.getEpochGroups():
            scanID = xnatSession.scans().get()[0]
            dtype = xnatSession.scan(scanID).datatype()
            self.assertEqual(g.getOwnerProperty(DATATYPE_PROPERTY), dtype)
Exemplo n.º 10
0
    def should_import_scan_parameters_as_epoch_parameters(self):
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)
        sessionType = xnatSession.datatype()
        sessionScanner = xnatSession.attrs.get(sessionType + '/scanner')

        for epoch in exp.getEpochsIterable().iterator():
            parameters = epoch.getProtocolParameters()
            self.assertGreater(parameters.size(), 0)
Exemplo n.º 11
0
    def should_import_epochgroup_per_scan_with_scantype_as_label(self):
        ctx = self.dsc.getContext()
        project = ctx.insertProject('session_import', 'session_import',
                                    api.datetime())
        src = ctx.insertSource('test')

        xnatSession = session_mock()

        exp = import_session(self.dsc, src, project, xnatSession)

        self.assertGreaterEqual(xnatSession.scans().get(), 1)
        self.assertEqual(len(exp.getEpochGroups()),
                         len(xnatSession.scans().get()))
Exemplo n.º 12
0
    def should_set_scan_datatype(self):

        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)

        for g in exp.getEpochGroups():
            scanID = xnatSession.scans().get()[0]
            dtype = xnatSession.scan(scanID).datatype()
            self.assertEqual(g.getOwnerProperty(DATATYPE_PROPERTY), dtype)
Exemplo n.º 13
0
    def should_import_scan_parameters_as_epoch_parameters(self):
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)
        sessionType = xnatSession.datatype()
        sessionScanner = xnatSession.attrs.get(sessionType + '/scanner')

        for epoch in exp.getEpochsIterable().iterator():
            parameters = epoch.getProtocolParameters()
            self.assertGreater(parameters.size(), 0)
Exemplo n.º 14
0
    def should_set_session_id_as_epochgroup_label(self):
        ctx = self.dsc.getContext()
        project = ctx.insertProject('session_import', 'session_import', api.datetime())
        src = ctx.insertSource('test')

        xnatSession = session_mock()

        exp = import_session(self.dsc, src, project, xnatSession)

        for scanID in xnatSession.scans().get():
            scan = xnatSession.scan(scanID)
            id = scan.attrs.get('xnat:mrScanData/type')
            self.assertEqual(len(list(exp.getEpochGroupsWithLabel(id).iterator())), 1)
Exemplo n.º 15
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
Exemplo n.º 16
0
    def should_set_session_id_as_epochgroup_label(self):
        ctx = self.dsc.getContext()
        project = ctx.insertProject('session_import', 'session_import',
                                    api.datetime())
        src = ctx.insertSource('test')

        xnatSession = session_mock()

        exp = import_session(self.dsc, src, project, xnatSession)

        for scanID in xnatSession.scans().get():
            scan = xnatSession.scan(scanID)
            id = scan.attrs.get('xnat:mrScanData/type')
            self.assertEqual(
                len(list(exp.getEpochGroupsWithLabel(id).iterator())), 1)
Exemplo n.º 17
0
    def should_import_scanner_perameters_as_device_parameters(self):
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)
        sessionType = xnatSession.datatype()
        sessionScanner = xnatSession.attrs.get(sessionType + '/scanner')

        for epoch in exp.getEpochsIterable().iterator():
            r = epoch.getResponse(sessionScanner)
            self.assertIsNotNone(r)
            parameters = r.getDeviceParameters()
            self.assertIsNotNone(parameters)
            self.assertGreater(parameters.size(), 0)
Exemplo n.º 18
0
    def should_import_scanner_perameters_as_device_parameters(self):
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)
        sessionType = xnatSession.datatype()
        sessionScanner = xnatSession.attrs.get(sessionType + '/scanner')

        for epoch in exp.getEpochsIterable().iterator():
            r = epoch.getResponse(sessionScanner)
            self.assertIsNotNone(r)
            parameters = r.getDeviceParameters()
            self.assertIsNotNone(parameters)
            self.assertGreater(parameters.size(), 0)
Exemplo n.º 19
0
    def should_insert_epochgroup_per_resource_group(self):
        """
        Should import files as URLResponses, not as Resources
        """
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)

        for scanID in xnatSession.scans().get():
            s = xnatSession.scan(scanID)
            id = s.attrs.get('xnat:mrScanData/type')
            g = exp.getEpochGroupsWithLabel(id).iterator().next()
            for r in s.resources():
                label = r.label()
                self.assertEqual(len(g.getChildren(label)), 1)
Exemplo n.º 20
0
    def should_insert_epochgroup_per_resource_group(self):
        """
        Should import files as URLResponses, not as Resources
        """
        ctx = self.dsc.getContext()
        src = ctx.insertSource('test')
        project = ctx.insertProject('test', 'test', api.datetime())

        xnatSession = session_mock()
        exp = import_session(self.dsc, src, project, xnatSession)

        for scanID in xnatSession.scans().get():
            s = xnatSession.scan(scanID)
            id = s.attrs.get('xnat:mrScanData/type')
            g = exp.getEpochGroupsWithLabel(id).iterator().next()
            for r in s.resources():
                label = r.label()
                self.assertEqual(len(g.getChildren(label)), 1)
Exemplo n.º 21
0
def to_joda_datetime(date, timezone):
    return api.datetime(date.year, date.month, date.day, date.hour,
                        date.minute, date.second, int(date.microsecond / 1000),
                        api.timezone_with_id(timezone))