Exemplo n.º 1
0
def test_GetParticipantDetails():

    import time
    start = time.time()

    restParser = RestParser()
    if cmu_test_project_uuid:
        project = cmu_test_project_uuid
    else:
        projects = restParser.run(BRAIN_VOL_FILES, '/projects')
        project = projects[0]
    import time
    start = time.time()
    subjects = restParser.run(BRAIN_VOL_FILES,
                              '/projects/{}/subjects'.format(project))
    subject = subjects['uuid'][0]

    Query.GetParticipantInstrumentData(BRAIN_VOL_FILES, project, subject)

    details = Query.GetParticipantDetails(BRAIN_VOL_FILES, project, subject)

    assert ('uuid' in details)
    assert ('id' in details)
    assert ('activity' in details)
    assert ('instruments' in details)
    assert ('derivatives' in details)

    end = time.time()
    runtime = end - start
Exemplo n.º 2
0
def test_CheckSubjectMatchesFilter():
    restParser = RestParser()
    if cmu_test_project_uuid:
        project = cmu_test_project_uuid
    else:
        projects = restParser.run(BRAIN_VOL_FILES, '/projects')
        project = projects[0]
    subjects = restParser.run(BRAIN_VOL_FILES,
                              '/projects/{}/subjects'.format(project))
    subject = subjects['uuid'][0]

    derivatives = Query.GetDerivativesDataForSubject(BRAIN_VOL_FILES, project,
                                                     subject)

    for skey in derivatives:
        for vkey in derivatives[skey]['values']:
            dt = vkey
            val = derivatives[skey]['values'][vkey]['value']
            if (dt and val):
                break

    # find an actual stat and build a matching filter to make sure our matcher passes it
    filter = "derivatives.{} eq {}".format(dt, val)
    assert Query.CheckSubjectMatchesFilter(BRAIN_VOL_FILES, project, subject,
                                           filter)

    instruments = Query.GetParticipantInstrumentData(BRAIN_VOL_FILES, project,
                                                     subject)
    for (i, inst) in instruments.items():
        if 'AGE_AT_SCAN' in inst:
            age = inst['AGE_AT_SCAN']

    older = str(float(age) + 1)
    younger = str(float(age) - 1)

    assert Query.CheckSubjectMatchesFilter(
        BRAIN_VOL_FILES, project, subject,
        "instruments.AGE_AT_SCAN eq {}".format(str(age)))
    assert (Query.CheckSubjectMatchesFilter(
        BRAIN_VOL_FILES, project, subject,
        "instruments.AGE_AT_SCAN lt {}".format(younger)) == False)
    assert (Query.CheckSubjectMatchesFilter(
        BRAIN_VOL_FILES, project, subject,
        "instruments.AGE_AT_SCAN gt {}".format(younger)) == True)
    assert Query.CheckSubjectMatchesFilter(
        BRAIN_VOL_FILES, project, subject,
        "instruments.AGE_AT_SCAN lt {}".format(older))
    assert (Query.CheckSubjectMatchesFilter(
        BRAIN_VOL_FILES, project, subject,
        "instruments.AGE_AT_SCAN gt {}".format(older)) == False)

    eq__format = "instruments.{} eq '{}'".format('WISC_IV_VOCAB_SCALED', 'nan')
    assert Query.CheckSubjectMatchesFilter(BRAIN_VOL_FILES, project, subject,
                                           eq__format)
    eq__format = "instruments.{} eq '{}'".format('WISC_IV_VOCAB_SCALED',
                                                 'not a match')
    assert (Query.CheckSubjectMatchesFilter(BRAIN_VOL_FILES, project, subject,
                                            eq__format) == False)
Exemplo n.º 3
0
 def instrumentSummary(self):
     match = re.match(
         r"^/?projects/([^/]+)/subjects/([^/]+)/instruments/([^/]+)$",
         self.command)
     self.restLog(
         "Returning instrument {} in subject {}".format(
             match.group(3), match.group(2)), 2)
     instruments = Query.GetParticipantInstrumentData(
         self.nidm_files, match.group(1), match.group(2))
     return self.format(instruments[match.group(3)],
                        headers=["Category", "Value"])
Exemplo n.º 4
0
 def instrumentsList(self):
     result = []
     match = re.match(r"^/?projects/([^/]+)/subjects/([^/]+)$",
                      self.command)
     self.restLog(
         "Returning instruments in subject {}".format(match.group(2)), 2)
     instruments = Query.GetParticipantInstrumentData(
         self.nidm_files, match.group(1), match.group(2))
     for i in instruments:
         result.append(i)
     return self.format(result)
Exemplo n.º 5
0
    def addFieldStats(self, result, project, subjects, field, type):
        '''
        Geneerates basic stats on a group of subjects and adds it to the result
        :param result:
        :param subjects:
        :param field:
        :return:
        '''
        values = []
        for s in subjects:
            if type == self.STAT_TYPE_INSTRUMENTS:
                data = Query.GetParticipantInstrumentData(
                    tuple(self.nidm_files), project, s)
                for i in data:
                    if field in data[i]:
                        values.append(float(data[i][field]))
            # derivatives are of the form [UUID]['values'][URI]{datumType, label, values, units}
            if type == self.STAT_TYPE_DERIVATIVES:
                data = Query.GetDerivativesDataForSubject(
                    tuple(self.nidm_files), project, s)
                for deriv in data:
                    for URI in data[deriv]['values']:
                        measures = data[deriv]['values'][URI]
                        if field == measures[
                                'label'] or field == self.getTailOfURI(URI):
                            values.append(float(measures['value']))

        if len(values) > 0:
            med = median(values)
            avg = mean(values)
            st = std(values)
            mn = min(values)
            mx = max(values)
        else:
            med = avg = st = mn = mx = None
        result[field] = {
            "max": mx,
            "min": mn,
            "median": med,
            "mean": avg,
            "standard_deviation": st
        }
Exemplo n.º 6
0
def test_GetParticipantDetails():
    projects  = restParser(BRAIN_VOL_FILES, '/projects')
    project = projects[0]
    subjects = restParser(BRAIN_VOL_FILES, '/projects/{}/subjects'.format(projects[0]))
    subject = subjects[0]

    import time
    start = time.time()

    Query.GetParticipantInstrumentData( BRAIN_VOL_FILES, project, subject )

    end = time.time()
    runtime = end - start
    # assert (runtime <  12)

    details = Query.GetParticipantDetails( BRAIN_VOL_FILES, project, subject )

    assert ('uuid' in details)
    assert ('id' in details)
    assert ('activity' in details)
    assert ('instruments' in details)
    assert ('stats' in details)
Exemplo n.º 7
0
def restParser(nidm_files, command, verbosity_level=0):

    restLog("parsing command " + command, 1, verbosity_level)
    restLog("Files to read:" + str(nidm_files), 1, verbosity_level)
    restLog("Using {} as the graph cache directory".format(gettempdir()), 1,
            verbosity_level)

    filter = ""
    if str(command).find('?') != -1:
        (command, query) = str(command).split('?')
        for q in query.split('&'):
            if len(q.split('=')) == 2:
                left, right = q.split('=')[0], q.split('=')[1]
                if left == 'filter':
                    filter = right

    result = []
    if re.match(r"^/?projects/?$", command):
        restLog("Returning all projects", 2, verbosity_level)
        projects = Query.GetProjectsUUID(nidm_files)
        for uuid in projects:
            result.append(str(uuid).replace(Constants.NIIRI, ""))

    elif re.match(r"^/?projects/[^/]+$", command):
        restLog("Returing metadata ", 2, verbosity_level)
        match = re.match(r"^/?projects/([^/]+)$", command)
        id = parse.unquote(str(match.group(1)))
        restLog("computing metadata", 5, verbosity_level)
        projects = Query.GetProjectsComputedMetadata(nidm_files)
        for pid in projects['projects'].keys():
            restLog("comparng " + str(pid) + " with " + str(id), 5,
                    verbosity_level)
            restLog("comparng " + str(pid) + " with " + Constants.NIIRI + id,
                    5, verbosity_level)
            restLog("comparng " + str(pid) + " with niiri:" + id, 5,
                    verbosity_level)
            if pid == id or pid == Constants.NIIRI + id or pid == "niiri:" + id:
                result = projects['projects'][pid]

    elif re.match(r"^/?projects/[^/]+/subjects/?$", command):
        match = re.match(r"^/?projects/([^/]+)/subjects/?$", command)
        project = match.group((1))
        restLog(
            "Returning all agents matching filter '{}' for project {}".format(
                filter, project), 2, verbosity_level)
        result = Query.GetParticipantUUIDsForProject(nidm_files, project,
                                                     filter, None)

    elif re.match(r"^/?projects/[^/]+/subjects/[^/]+/?$", command):
        match = re.match(r"^/?projects/([^/]+)/subjects/([^/]+)/?$", command)
        restLog("Returning info about subject {}".format(match.group(2)), 2,
                verbosity_level)
        result = Query.GetParticipantDetails(nidm_files, match.group(1),
                                             match.group(2))

    elif re.match(r"^/?projects/[^/]+/subjects/[^/]+/instruments/?$", command):
        match = re.match(r"^/?projects/([^/]+)/subjects/([^/]+)", command)
        restLog("Returning instruments in subject {}".format(match.group(2)),
                2, verbosity_level)
        instruments = Query.GetParticipantInstrumentData(
            nidm_files, match.group(1), match.group(2))
        for i in instruments:
            result.append(i)

    elif re.match(r"^/?projects/[^/]+/subjects/[^/]+/instruments/[^/]+/?$",
                  command):
        match = re.match(
            r"^/?projects/([^/]+)/subjects/([^/]+)/instruments/([^/]+)",
            command)
        restLog(
            "Returning instrument {} in subject {}".format(
                match.group(3), match.group(2)), 2, verbosity_level)
        instruments = Query.GetParticipantInstrumentData(
            nidm_files, match.group(1), match.group(2))
        result = instruments[match.group(3)]

    elif re.match(r"^/?projects/[^/]+/subjects/[^/]+/derivatives/?$", command):
        match = re.match(r"^/?projects/([^/]+)/subjects/([^/]+)", command)
        restLog("Returning derivatives in subject {}".format(match.group(2)),
                2, verbosity_level)
        derivatives = Query.GetDerivativesDataForSubject(
            nidm_files, match.group(1), match.group(2))
        for s in derivatives:
            result.append(s)

    elif re.match(r"^/?projects/[^/]+/subjects/[^/]+/derivatives/[^/]+/?$",
                  command):
        match = re.match(
            r"^/?projects/([^/]+)/subjects/([^/]+)/derivatives/([^/]+)",
            command)
        restLog(
            "Returning stat {} in subject {}".format(match.group(3),
                                                     match.group(2)), 2,
            verbosity_level)
        derivatives = Query.GetDerivativesDataForSubject(
            nidm_files, match.group(1), match.group(2))
        result = derivatives[match.group(3)]

    else:
        restLog("NO MATCH!", 2, verbosity_level)

    return result