def test_GetProjectsComputedMetadata(): p1 = makeProjectTestFile("testfile.ttl") p2 = makeProjectTestFile2("testfile2.ttl") if not Path('./cmu_a.nidm.ttl').is_file(): urllib.request.urlretrieve( "https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/CMU_a/nidm.ttl", "cmu_a.nidm.ttl") #DBK: this is a bit unsafe as the TTL files in the github repo above can change and the UUID will change since they are randomly #generated at this point. It's probably more robust to explicitly create these files for the time being and explicitly set the #UUID in the test file: # For example: kwargs={Constants.NIDM_PROJECT_NAME:"FBIRN_PhaseIII",Constants.NIDM_PROJECT_IDENTIFIER:1200,Constants.NIDM_PROJECT_DESCRIPTION:"Test investigation2"} # project = Project(uuid="_654321",attributes=kwargs) # p3 = "nidm:_504a60b0-7e86-11e9-ae20-003ee1ce9545" # from the cmu_a ttl file json_response = Query.GetProjectsComputedMetadata( ["testfile.ttl", "testfile2.ttl", "cmu_a.nidm.ttl"]) parsed = json.loads(json_response) assert parsed['projects'][p1][str( Constants.NIDM_PROJECT_DESCRIPTION)] == "Test investigation" assert parsed['projects'][p2][str( Constants.NIDM_PROJECT_DESCRIPTION)] == "More Scans" assert parsed['projects'][p3][str( Constants.NIDM_PROJECT_NAME)] == "ABIDE CMU_a Site" assert parsed['projects'][p2][Query.matchPrefix( str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 0 #DBK - this isn't working #assert parsed['projects'][p3][Query.matchPrefix(str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 14 assert parsed['projects'][p1][Query.matchPrefix( str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 0
def test_CMU_GetProjectsComputedMetadata(): ''' This will run the getProjectsComputedMetadata on a downloaded ttl file from the ABIDE dataset This is for dev purposes only and should be skipped during a normal test ''' if not Path('./cmu_a.nidm.ttl').is_file(): urllib.request.urlretrieve( "https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/CMU_a/nidm.ttl", "cmu_a.nidm.ttl") json_response = Query.GetProjectsComputedMetadata(["cmu_a.nidm.ttl"]) parsed = json.loads(json_response) print(parsed) # should be only one p1 = list(parsed['projects'].keys())[0] assert parsed['projects'][p1][Query.matchPrefix( str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 14 assert parsed['projects'][p1]["age_min"] == 21 assert parsed['projects'][p1]["age_max"] == 33 assert parsed['projects'][p1][str(Constants.NIDM_GENDER)] == ['1', '2'] assert parsed['projects'][p1][str( Constants.NIDM_HANDEDNESS)] == ['R', 'L', 'Ambi']
def test_GetProjectsComputedMetadata(): p1 = makeProjectTestFile("testfile.ttl") p2 = makeProjectTestFile2("testfile2.ttl") files = ["testfile.ttl", "testfile2.ttl"] if USE_GITHUB_DATA: if not Path('./cmu_a.nidm.ttl').is_file(): urllib.request.urlretrieve( "https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/CMU_a/nidm.ttl", "cmu_a.nidm.ttl") files.append("cmu_a.nidm.ttl") parsed = Query.GetProjectsComputedMetadata(files) if USE_GITHUB_DATA: for project_id in parsed['projects']: print("looking at {}".format(project_id)) if project_id != p1 and project_id != p2: p3 = project_id print("got p3 of {}".format(p3)) assert parsed['projects'][p3][str( Constants.NIDM_PROJECT_NAME)] == "ABIDE CMU_a Site" assert parsed['projects'][p3][Query.matchPrefix( str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 14 assert parsed['projects'][p3]["age_min"] == 21 assert parsed['projects'][p3]["age_max"] == 33 assert parsed['projects'][p3][str(Constants.NIDM_GENDER)] == ['1', '2']
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) text = ('On', 'Disable', 'unsetting') if os.getenv('PYNIDM_GRAPH_CACHE') != None else ( 'Off', 'Enable', 'setting') restLog("Graph cache is {}".format(text[0]), 1, verbosity_level) restLog( "{} graph cache by {} the PYNIDM_GRAPH_CACHE environment variable". format(text[1], text[2]), 1, verbosity_level) 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 for project {}".format(project), 2, verbosity_level) agents = Query.GetParticipantUUIDsForProject(nidm_files, project) result = [] vals = agents.values for x in vals: result.append(str(x[0]).replace("http://iri.nidash.org/", "")) 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)) else: restLog("NO MATCH!", 2, verbosity_level) return result
def test_GetProjectsComputedMetadata(): p1 = makeProjectTestFile("testfile.ttl") p2 = makeProjectTestFile2("testfile2.ttl") json_response = Query.GetProjectsComputedMetadata( ["testfile.ttl", "testfile2.ttl"]) parsed = json.loads(json_response) assert parsed['projects'][p1][str( Constants.NIDM_PROJECT_DESCRIPTION)] == "Test investigation" assert parsed['projects'][p2][str( Constants.NIDM_PROJECT_DESCRIPTION)] == "More Scans" assert parsed['projects'][p2][Query.matchPrefix( str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 0 assert parsed['projects'][p1][Query.matchPrefix( str(Constants.NIDM_NUMBER_OF_SUBJECTS))] == 0
def projectStats(self): result = dict() subjects = None path = (urlparse(self.command)).path match = re.match(r"^/?statistics/projects/([^/]+)\??$", path) id = parse.unquote(str(match.group(1))) self.restLog("Returing project {} stats metadata".format(id), 2) projects = Query.GetProjectsComputedMetadata(self.nidm_files) for pid in projects['projects'].keys(): self.restLog("comparng " + str(pid) + " with " + str(id), 5) self.restLog( "comparng " + str(pid) + " with " + Constants.NIIRI + id, 5) self.restLog("comparng " + str(pid) + " with niiri:" + id, 5) if pid == id or pid == Constants.NIIRI + id or pid == "niiri:" + id: # stip off prefixes to make it more human readable for key in projects['projects'][pid]: short_key = key possible_prefix = re.sub(':.*', '', short_key) if possible_prefix in Constants.namespaces: short_key = re.sub('^.*:', '', short_key) result[short_key] = projects['projects'][pid][key] # now get any fields they reqested for field in self.query['fields']: if subjects == None: subjects = Query.GetParticipantUUIDsForProject( tuple(self.nidm_files), project_id=id, filter=self.query['filter']) result['subjects'] = subjects bits = field.split('.') if len(bits) > 1: stat_type = self.getStatType( bits[0] ) # should be either instruments or derivatives for now. self.addFieldStats(result, id, subjects, bits[1], stat_type) # bits[1] will be the ID return self.dictFormat(result)
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) 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(Query.matchPrefix(str(uuid))) 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) if pid == id: result = projects['projects'][pid] return result
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