def subjectSummary(self): match = re.match(r"^/?subjects/([^/]+)/?$", self.command) self.restLog("Returning info about subject {}".format(match.group(1)), 2) id = match.group(1) # if we were passed in a sub_id rather than a UUID, lookup the associated UUID. (we might get multiple!) if validate_uuid(id): sub_ids = id else: sub_ids = Navigate.getSubjectUUIDsfromID(self.nidm_files, id) if len(sub_ids) == 1: sub_ids = sub_ids[0] activities = Navigate.getActivities(self.nidm_files, id) activityData = [] for a in activities: data = Navigate.getActivityData(self.nidm_files, a) activityData.append(data) return self.subjectSummaryFormat_v2({ 'uuid': sub_ids, 'instruments': list(filter(lambda x: x.category == 'instrument', activityData)), 'derivatives': list(filter(lambda x: x.category == 'derivative', activityData)) })
def getActivities(nidm_file_tuples, subject_id): activities = set([]) # if we were passed in a sub_id rather than a UUID, lookup the associated UUID. (we might get multiple!) if validate_uuid(URITail(subject_id)): sub_uris = [subject_id] else: sub_uris = getSubjectUUIDsfromID(nidm_file_tuples, subject_id) for file in nidm_file_tuples: rdf_graph = OpenGraph(file) for subject_uri in sub_uris: subject_uri = expandID(subject_uri, Constants.NIIRI) for blank_node in rdf_graph.subjects( predicate=Constants.PROV['agent'], object=subject_uri): for activity in rdf_graph.subjects( predicate=Constants.PROV['qualifiedAssociation'], object=blank_node): if (activity, isa, Constants.PROV['Activity']) in rdf_graph: activities.add(activity) return activities