def get_cognitiveatlas_hierarchy(experiment_tags=None,get_html=False): '''get_cognitiveatlas_hierarchy return :param experiment_tags: a list of experiment exp_id tags to include. If None provided, all valid experiments will be used. :param get_html: if True, returns rendered HTML template with hierarchy. False returns json data structure. ''' from cognitiveatlas.datastructure import concept_node_triples from expfactory.graph import make_tree_from_triples tmpdir = custom_battery_download() experiment_folder = "%s/experiments" %tmpdir experiments = get_experiments(experiment_folder,load=True,warning=False) if experiment_tags != None: experiments = [e for e in experiments if e[0]["exp_id"] in experiment_tags] # We need a dictionary to look up experiments by task ids unique_tasks = numpy.unique([e[0]["cognitive_atlas_task_id"] for e in experiments]).tolist() experiment_lookup = dict() for u in unique_tasks: matching_tasks = numpy.unique([e[0]["exp_id"] for e in experiments if e[0]["cognitive_atlas_task_id"]==u]) experiment_lookup[u] = matching_tasks.tolist() triples = concept_node_triples(image_dict=experiment_lookup,save_to_file=False,lookup_key_type="task") # Experiments not in the tree get added to parent node 1 undefined_experiments = [x[0]["exp_id"] for x in experiments if x[0]["exp_id"] not in triples.name.tolist()] undefined_experiments.sort() last_defined_node = [x for x in triples.id.tolist() if re.search("node_",x)] last_defined_node.sort() last_defined_node = int(last_defined_node[-1].replace("node_","")) idx = triples.index.tolist()[-1]+1 for i in range(idx,idx+len(undefined_experiments)): undefined_experiment = undefined_experiments.pop(0) triples.loc[i] = ["node_%s" %(last_defined_node+1),1,undefined_experiment] last_defined_node+=1 # We want to add meta data for the experiments meta_data = dict() for experiment in experiments: node_ids = triples.id[triples.name==experiment[0]["exp_id"]].tolist() for node_id in node_ids: meta_data[node_id] = experiment[0] # Generate a data structure with task/concept hierarchy, prune_tree default is True if get_html == True: tree = make_tree_from_triples(triples,output_html=True,meta_data=meta_data) else: tree = make_tree_from_triples(triples,output_html=False) return tree
collections = pandas.read_csv("%s/collections_with_dois.tsv" %results,sep="\t") ### Step 1: Load meta data sources unique_contrasts = images.cognitive_contrast_cogatlas_id.unique().tolist() # Images that do not match the correct identifier will not be used (eg, "Other") expression = re.compile("cnt_*") unique_contrasts = [u for u in unique_contrasts if expression.match(u)] image_lookup = dict() for u in unique_contrasts: image_lookup[u] = images.image_id[images.cognitive_contrast_cogatlas_id==u].tolist() output_triples_file = "%s/task_contrast_triples.tsv" % results # Create a data structure of tasks and contrasts for our analysis concept_node_triples(image_dict=image_lookup,output_file=output_triples_file) relationship_table = pandas.read_csv(output_triples_file,sep="\t") # We want to give the concept categories as meta data so we produce category nodes #categories = get_concept_categories() # Get reverse inference scores from results scores_df = pandas.read_csv("%s/reverse_inference_scores.tsv" %data,sep="\t") unique_nodes = relationship_table.id.unique().tolist() # We will store a data frame of meta data # Lookup for meta_data is the id of the node! meta_data = {} for node in unique_nodes:
# This is a data structure that looks like this - showing id of the node (column 1), parent id (column 2) # and then the name ''' id parent name 1 none BASE # there is always a base node 2 1 MEMORY # high level concept groups 3 1 PERCEPTION 4 2 WORKING MEMORY # concepts 5 2 LONG TERM MEMORY 6 4 image1.nii.gz # associated images (discovered by way of contrasts) 7 4 image2.nii.gz ''' # Create a data structure of tasks and contrasts for our analysis relationship_table = concept_node_triples(save_to_file=False) # relationship_table # # id parent name #0 1 None BASE #1 trm_4a3fd79d096be trm_4a3fd79d0aec1 abductive reasoning #2 trm_4a3fd79d096e3 trm_4a3fd79d09827 abstract analogy #3 trm_4a3fd79d096f0 trm_4a3fd79d0a746 abstract knowledge #4 trm_4a3fd79d096fc 1 acoustic coding #5 trm_4a3fd79d09707 trm_4a3fd79d0b8e5 acoustic encoding #6 trm_4a3fd79d09707 trm_4a3fd79d0b8e5 acoustic encoding #7 trm_4a3fd79d09713 trm_4a3fd79d09ab2 acoustic phonetic processing # Note that you can include an optional "image_dict" argument if you want to add a # final layer of nodes (eg, a dictionary of keys (concept ids) that correspond to some final
## STEP 1: GENERATE TRIPLES DATA STRUCTURE # This is a data structure that looks like this - showing id of the node (column 1), parent id (column 2) # and then the name ''' id parent name 1 none BASE # there is always a base node 2 1 MEMORY # high level concept groups 3 1 PERCEPTION 4 2 WORKING MEMORY # concepts 5 2 LONG TERM MEMORY 6 4 image1.nii.gz # associated images (discovered by way of contrasts) 7 4 image2.nii.gz ''' # Create a data structure of tasks and contrasts for our analysis relationship_table = concept_node_triples(save_to_file=False) # relationship_table # # id parent name #0 1 None BASE #1 trm_4a3fd79d096be trm_4a3fd79d0aec1 abductive reasoning #2 trm_4a3fd79d096e3 trm_4a3fd79d09827 abstract analogy #3 trm_4a3fd79d096f0 trm_4a3fd79d0a746 abstract knowledge #4 trm_4a3fd79d096fc 1 acoustic coding #5 trm_4a3fd79d09707 trm_4a3fd79d0b8e5 acoustic encoding #6 trm_4a3fd79d09707 trm_4a3fd79d0b8e5 acoustic encoding #7 trm_4a3fd79d09713 trm_4a3fd79d09ab2 acoustic phonetic processing # Note that you can include an optional "image_dict" argument if you want to add a # final layer of nodes (eg, a dictionary of keys (concept ids) that correspond to some final
def main(): base = "data/" # Make a folder for mean images if not os.path.exists("mr"): os.mkdir("mr") # Get Neurovault Images with defined cognitive atlas contrast collections = get_collections() # Filter images to those that have a DOI collections = collections[collections.DOI.isnull() == False] # Get image meta data for collections (N=1023) images = get_images(collection_pks=collections.collection_id.tolist()) # Filter images to those with contrasts defined (N=98) images = images[images.cognitive_contrast_cogatlas_id.isnull() == False] # Get rid of any not in MNI images = images[images.not_mni == False] # Get rid of thresholded images images = images[images.is_thresholded == False] ### Step 1: Load meta data sources unique_contrasts = images.cognitive_contrast_cogatlas_id.unique().tolist() # Images that do not match the correct identifier will not be used (eg, "Other") expression = re.compile("cnt_*") unique_contrasts = [u for u in unique_contrasts if expression.match(u)] # Make sure exists in cognitive atlas existing_contrasts = [] for u in unique_contrasts: try: tmp = get_concept(contrast_id=u, silent=True) existing_contrasts.append(u) except: print "%s is defined in NeuroVault, does not exist in Cognitive Atlas" % u image_lookup = dict() for u in existing_contrasts: image_lookup[u] = images.image_id[images.cognitive_contrast_cogatlas_id == u].tolist() # Create a data structure of tasks and contrasts for our analysis relationship_table = concept_node_triples(image_dict=image_lookup, save_to_file=False) unique_nodes = relationship_table.id.unique().tolist() # We will store a data frame of meta data # Lookup for meta_data is the id of the node! meta_data = {} for node in unique_nodes: meta_single = {} # This is an image node if re.search("node_", node): print "Found image node!" relationship_table_row = relationship_table[relationship_table.id == node] image_id = relationship_table_row.name.tolist()[0] meta_single["category"] = "" meta_single["type"] = "nii" # NeuroVault metadata concepts = relationship_table.parent[relationship_table.name == image_id] concepts = [ relationship_table.name[relationship_table.id == c].tolist()[0] for c in concepts ] neurovault_row = images[images.image_id == int(image_id)] collection_row = collections[collections.collection_id == neurovault_row.collection_id.tolist() [0]] collection_meta = { "DOI": collection_row["DOI"].tolist()[0], "authors": collection_row["authors"].tolist()[0], "journal": collection_row["journal_name"].tolist()[0], "url": collection_row["url"].tolist()[0], "subjects": collection_row["number_of_subjects"].tolist()[0], "smoothing_fwhm": str(collection_row["smoothing_fwhm"].tolist()[0]).encode( "utf-8") } meta_single["url"] = neurovault_row["url"].tolist()[0] meta_single["thumbnail"] = neurovault_row["thumbnail"].tolist()[0] meta_single["images"] = neurovault_row["thumbnail"].tolist() meta_single["task"] = neurovault_row[ "cognitive_paradigm_cogatlas"].tolist()[0] meta_single["contrast"] = neurovault_row[ "cognitive_contrast_cogatlas"].tolist()[0] meta_single["download"] = neurovault_row["file"].tolist()[0] meta_single["concept"] = concepts if neurovault_row["description"].tolist()[0]: meta_single["description"] = str( neurovault_row["description"].tolist()[0]).encode("utf-8") else: meta_single["description"] = "" if len(meta_single["description"]) > 600: meta_single["description"] = "%s..." % meta_single[ "description"][0:600] else: # A concept node if node != "1": relationship_table_row = relationship_table[ relationship_table.id == node] concept = get_concept(id=node, silent=True).json children_nodes = [ relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x] == node ] while len( [x for x in children_nodes if not isinstance(x, int)]) > 0: new_parent_nodes = [ x for x in children_nodes if not isinstance(x, int) ] children_nodes = [ x for x in children_nodes if x not in new_parent_nodes ] for new_parent in new_parent_nodes: node_name = relationship_table.id[ relationship_table.name == new_parent].tolist()[0] children_nodes = children_nodes + [ relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x] == node_name ] # Now only keep children that are images meta_single["images"] = images["thumbnail"][ images.image_id.isin(children_nodes)].tolist() # Cognitive Atlas meta data meta_single[ "url"] = "http://www.cognitiveatlas.org/term/id/%s" % node meta_single["type"] = "concept" meta_single[ "thumbnail"] = "http://www.cognitiveatlas.org/images/logo-front.png" meta_single["concept"] = [ relationship_table.name[relationship_table.id == node].tolist()[0] ] meta_single["task"] = "" meta_single["contrast"] = [] meta_single[ "download"] = "http://www.cognitiveatlas.org/rdf/id/%s" % node if concept[0]["definition_text"]: meta_single["description"] = concept[0][ "definition_text"].encode("utf-8") else: meta_single["description"] = "" if len(meta_single["description"]) > 600: meta_single["description"] = "%s..." % meta_single[ "description"][0:600] meta_data[node] = meta_single ## STEP 2: VISUALIZATION WITH PYBRAINCOMPARE from pybraincompare.ontology.tree import named_ontology_tree_from_tsv, make_ontology_tree_d3 # First let's look at the tree structure # output_json = "%s/task_contrast_tree.json" % outfolder tree = named_ontology_tree_from_tsv(relationship_table, output_json=None, meta_data=meta_data) html_snippet = make_ontology_tree_d3(tree) web_folder = base make_analysis_web_folder(html_snippet, web_folder) # To get a dump of just the tree (for use in more advanced custom web interface) filey = open('%s/reverseinference.json' % base, 'wb') filey.write( json.dumps(tree, sort_keys=True, indent=4, separators=(',', ': '))) filey.close() ## STEP 3: Export individual nodes ### Images unique_images = images.image_id.unique().tolist() # Images for s in range(0, len(unique_images)): image_id = unique_images[s] meta_data = {} meta_data["image_id"] = image_id print "Parsing data for images %s of %s" % (s, len(unique_images)) concepts = relationship_table.parent[relationship_table.name == str( image_id)].tolist() concepts = [ relationship_table.name[relationship_table.id == c].tolist()[0] for c in concepts ] concepts_ids = [ relationship_table.id[relationship_table.id == c].tolist()[0] for c in concepts ] neurovault_row = images[images.image_id == int(image_id)] collection_row = collections[collections.collection_id == neurovault_row.collection_id.tolist()[0]] collection_meta = { "DOI": collection_row["DOI"].tolist()[0], "authors": collection_row["authors"].tolist()[0], "journal": collection_row["journal_name"].tolist()[0], "url": collection_row["url"].tolist()[0], "subjects": collection_row["number_of_subjects"].tolist()[0], "smoothing_fwhm": str(collection_row["smoothing_fwhm"].tolist()[0]).encode("utf-8"), "title": collection_row["name"].tolist()[0] } meta_data["collection"] = collection_meta meta_data["url"] = neurovault_row["url"].tolist()[0] meta_data["thumbnail"] = neurovault_row["thumbnail"].tolist()[0] meta_data["images"] = neurovault_row["thumbnail"].tolist() meta_data["task"] = neurovault_row[ "cognitive_paradigm_cogatlas"].tolist()[0] meta_data["contrast"] = neurovault_row[ "cognitive_contrast_cogatlas"].tolist()[0] meta_data["download"] = neurovault_row["file"].tolist()[0] meta_data["concept"] = concepts meta_data["concept_id"] = concepts_ids if neurovault_row["description"].tolist()[0]: try: description = str( neurovault_row["description"].tolist()[0]).encode("utf-8") except: description = "" if description != "nan": meta_data["description"] = description else: meta_data["description"] = "" else: meta_data["description"] = "" if len(meta_data["description"]) > 600: meta_data[ "description"] = "%s..." % meta_data["description"][0:600] output_file = "%s/ri_%s.json" % (base, meta_data["image_id"]) filey = open(output_file, 'wb') filey.write( json.dumps(meta_data, sort_keys=True, indent=4, separators=(',', ': '))) filey.close() ### Concepts for node in unique_nodes: # This is a concept node if not re.search("node_", node): if node != "1": relationship_table_row = relationship_table[ relationship_table.id == node] concept = get_concept(id=node).json meta_single = {} children_nodes = [ relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x] == node ] while len( [x for x in children_nodes if not isinstance(x, int)]) > 0: new_parent_nodes = [ x for x in children_nodes if not isinstance(x, int) ] children_nodes = [ x for x in children_nodes if x not in new_parent_nodes ] for new_parent in new_parent_nodes: node_name = relationship_table.id[ relationship_table.name == new_parent].tolist()[0] children_nodes = children_nodes + [ relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x] == node_name ] # Now only keep children that are images meta_single["images"] = images["thumbnail"][ images.image_id.isin(children_nodes)].tolist() meta_single["image_list"] = children_nodes # Cognitive Atlas meta data meta_single[ "url"] = "http://www.cognitiveatlas.org/term/id/%s" % node meta_single["type"] = "concept" meta_single[ "thumbnail"] = "http://www.cognitiveatlas.org/images/logo-front.png" meta_single["concept"] = [ relationship_table.name[relationship_table.id == node].tolist()[0] ] meta_single["task"] = "" meta_single["contrast"] = [] meta_single[ "download"] = "http://www.cognitiveatlas.org/rdf/id/%s" % node if concept[0]["definition_text"]: meta_single["description"] = concept[0][ "definition_text"].encode("utf-8") else: meta_single["description"] = "" if len(meta_single["description"]) > 600: meta_single["description"] = "%s..." % meta_single[ "description"][0:600] output_file = "%s/ri_%s.json" % (base, node) filey = open(output_file, 'wb') filey.write( json.dumps(meta_single, sort_keys=True, indent=4, separators=(',', ': '))) filey.close()
def main(): base = "data/" # Make a folder for mean images if not os.path.exists("mr"): os.mkdir("mr") # Get Neurovault Images with defined cognitive atlas contrast collections = get_collections() # Filter images to those that have a DOI collections = collections[collections.DOI.isnull()==False] # Get image meta data for collections (N=1023) images = get_images(collection_pks=collections.collection_id.tolist()) # Filter images to those with contrasts defined (N=98) images = images[images.cognitive_contrast_cogatlas_id.isnull()==False] # Get rid of any not in MNI images = images[images.not_mni == False] # Get rid of thresholded images images = images[images.is_thresholded == False] ### Step 1: Load meta data sources unique_contrasts = images.cognitive_contrast_cogatlas_id.unique().tolist() # Images that do not match the correct identifier will not be used (eg, "Other") expression = re.compile("cnt_*") unique_contrasts = [u for u in unique_contrasts if expression.match(u)] # Make sure exists in cognitive atlas existing_contrasts = [] for u in unique_contrasts: try: tmp = get_concept(contrast_id=u,silent=True) existing_contrasts.append(u) except: print "%s is defined in NeuroVault, does not exist in Cognitive Atlas" %u image_lookup = dict() for u in existing_contrasts: image_lookup[u] = images.image_id[images.cognitive_contrast_cogatlas_id==u].tolist() # Create a data structure of tasks and contrasts for our analysis relationship_table = concept_node_triples(image_dict=image_lookup,save_to_file=False) unique_nodes = relationship_table.id.unique().tolist() # We will store a data frame of meta data # Lookup for meta_data is the id of the node! meta_data = {} for node in unique_nodes: meta_single = {} # This is an image node if re.search("node_",node): print "Found image node!" relationship_table_row = relationship_table[relationship_table.id==node] image_id = relationship_table_row.name.tolist()[0] meta_single["category"] = "" meta_single["type"] = "nii" # NeuroVault metadata concepts = relationship_table.parent[relationship_table.name == image_id] concepts = [relationship_table.name[relationship_table.id==c].tolist()[0] for c in concepts] neurovault_row = images[images.image_id == int(image_id)] collection_row = collections[collections.collection_id == neurovault_row.collection_id.tolist()[0]] collection_meta = {"DOI":collection_row["DOI"].tolist()[0], "authors":collection_row["authors"].tolist()[0], "journal":collection_row["journal_name"].tolist()[0], "url":collection_row["url"].tolist()[0], "subjects":collection_row["number_of_subjects"].tolist()[0], "smoothing_fwhm":str(collection_row["smoothing_fwhm"].tolist()[0]).encode("utf-8")} meta_single["url"] = neurovault_row["url"].tolist()[0] meta_single["thumbnail"] = neurovault_row["thumbnail"].tolist()[0] meta_single["images"] = neurovault_row["thumbnail"].tolist() meta_single["task"] = neurovault_row["cognitive_paradigm_cogatlas"].tolist()[0] meta_single["contrast"] = neurovault_row["cognitive_contrast_cogatlas"].tolist()[0] meta_single["download"] = neurovault_row["file"].tolist()[0] meta_single["concept"] = concepts if neurovault_row["description"].tolist()[0]: meta_single["description"] = str(neurovault_row["description"].tolist()[0]).encode("utf-8") else: meta_single["description"] = "" if len(meta_single["description"]) > 600: meta_single["description"] = "%s..." % meta_single["description"][0:600] else: # A concept node if node != "1": relationship_table_row = relationship_table[relationship_table.id==node] concept = get_concept(id=node,silent=True).json children_nodes = [relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x]==node] while len([x for x in children_nodes if not isinstance(x,int)]) > 0: new_parent_nodes = [x for x in children_nodes if not isinstance(x,int)] children_nodes = [x for x in children_nodes if x not in new_parent_nodes] for new_parent in new_parent_nodes: node_name = relationship_table.id[relationship_table.name==new_parent].tolist()[0] children_nodes = children_nodes + [relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x]==node_name] # Now only keep children that are images meta_single["images"] = images["thumbnail"][images.image_id.isin(children_nodes)].tolist() # Cognitive Atlas meta data meta_single["url"] = "http://www.cognitiveatlas.org/term/id/%s" %node meta_single["type"] = "concept" meta_single["thumbnail"] = "http://www.cognitiveatlas.org/images/logo-front.png" meta_single["concept"] = [relationship_table.name[relationship_table.id==node].tolist()[0]] meta_single["task"] = "" meta_single["contrast"] = [] meta_single["download"] = "http://www.cognitiveatlas.org/rdf/id/%s" %node if concept[0]["definition_text"]: meta_single["description"] = concept[0]["definition_text"].encode("utf-8") else: meta_single["description"] = "" if len(meta_single["description"]) > 600: meta_single["description"] = "%s..." % meta_single["description"][0:600] meta_data[node] = meta_single ## STEP 2: VISUALIZATION WITH PYBRAINCOMPARE from pybraincompare.ontology.tree import named_ontology_tree_from_tsv, make_ontology_tree_d3 # First let's look at the tree structure # output_json = "%s/task_contrast_tree.json" % outfolder tree = named_ontology_tree_from_tsv(relationship_table,output_json=None,meta_data=meta_data) html_snippet = make_ontology_tree_d3(tree) web_folder = base make_analysis_web_folder(html_snippet,web_folder) # To get a dump of just the tree (for use in more advanced custom web interface) filey = open('%s/reverseinference.json' %base,'wb') filey.write(json.dumps(tree, sort_keys=True,indent=4, separators=(',', ': '))) filey.close() ## STEP 3: Export individual nodes ### Images unique_images = images.image_id.unique().tolist() # Images for s in range(0,len(unique_images)): image_id = unique_images[s] meta_data = {} meta_data["image_id"] = image_id print "Parsing data for images %s of %s" %(s,len(unique_images)) concepts = relationship_table.parent[relationship_table.name == str(image_id)].tolist() concepts = [relationship_table.name[relationship_table.id==c].tolist()[0] for c in concepts] concepts_ids = [relationship_table.id[relationship_table.id==c].tolist()[0] for c in concepts] neurovault_row = images[images.image_id == int(image_id)] collection_row = collections[collections.collection_id == neurovault_row.collection_id.tolist()[0]] collection_meta = {"DOI":collection_row["DOI"].tolist()[0], "authors":collection_row["authors"].tolist()[0], "journal":collection_row["journal_name"].tolist()[0], "url":collection_row["url"].tolist()[0], "subjects":collection_row["number_of_subjects"].tolist()[0], "smoothing_fwhm":str(collection_row["smoothing_fwhm"].tolist()[0]).encode("utf-8"), "title":collection_row["name"].tolist()[0]} meta_data["collection"] = collection_meta meta_data["url"] = neurovault_row["url"].tolist()[0] meta_data["thumbnail"] = neurovault_row["thumbnail"].tolist()[0] meta_data["images"] = neurovault_row["thumbnail"].tolist() meta_data["task"] = neurovault_row["cognitive_paradigm_cogatlas"].tolist()[0] meta_data["contrast"] = neurovault_row["cognitive_contrast_cogatlas"].tolist()[0] meta_data["download"] = neurovault_row["file"].tolist()[0] meta_data["concept"] = concepts meta_data["concept_id"] = concepts_ids if neurovault_row["description"].tolist()[0]: try: description = str(neurovault_row["description"].tolist()[0]).encode("utf-8") except: description = "" if description != "nan": meta_data["description"] = description else: meta_data["description"] = "" else: meta_data["description"] = "" if len(meta_data["description"]) > 600: meta_data["description"] = "%s..." % meta_data["description"][0:600] output_file = "%s/ri_%s.json" %(base,meta_data["image_id"]) filey = open(output_file,'wb') filey.write(json.dumps(meta_data, sort_keys=True,indent=4, separators=(',', ': '))) filey.close() ### Concepts for node in unique_nodes: # This is a concept node if not re.search("node_",node): if node != "1": relationship_table_row = relationship_table[relationship_table.id==node] concept = get_concept(id=node).json meta_single = {} children_nodes = [relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x]==node] while len([x for x in children_nodes if not isinstance(x,int)]) > 0: new_parent_nodes = [x for x in children_nodes if not isinstance(x,int)] children_nodes = [x for x in children_nodes if x not in new_parent_nodes] for new_parent in new_parent_nodes: node_name = relationship_table.id[relationship_table.name==new_parent].tolist()[0] children_nodes = children_nodes + [relationship_table.name.tolist()[x] for x in range(relationship_table.shape[0]) if relationship_table.parent.tolist()[x]==node_name] # Now only keep children that are images meta_single["images"] = images["thumbnail"][images.image_id.isin(children_nodes)].tolist() meta_single["image_list"] = children_nodes # Cognitive Atlas meta data meta_single["url"] = "http://www.cognitiveatlas.org/term/id/%s" %node meta_single["type"] = "concept" meta_single["thumbnail"] = "http://www.cognitiveatlas.org/images/logo-front.png" meta_single["concept"] = [relationship_table.name[relationship_table.id==node].tolist()[0]] meta_single["task"] = "" meta_single["contrast"] = [] meta_single["download"] = "http://www.cognitiveatlas.org/rdf/id/%s" %node if concept[0]["definition_text"]: meta_single["description"] = concept[0]["definition_text"].encode("utf-8") else: meta_single["description"] = "" if len(meta_single["description"]) > 600: meta_single["description"] = "%s..." % meta_single["description"][0:600] output_file = "%s/ri_%s.json" %(base,node) filey = open(output_file,'wb') filey.write(json.dumps(meta_single, sort_keys=True,indent=4, separators=(',', ': '))) filey.close()
3 1 PERCEPTION 4 2 WORKING MEMORY # concepts 5 2 LONG TERM MEMORY 6 4 image1.nii.gz # associated images (discovered by way of contrasts) 7 4 image2.nii.gz ''' # We need a dictionary to look up image lists by contrast ids unique_contrasts = images.cognitive_contrast_cogatlas_id.unique().tolist() # Images that do not match the correct identifier will not be used (eg, "Other") expression = re.compile("cnt_*") unique_contrasts = [u for u in unique_contrasts if expression.match(u)] image_lookup = dict() for u in unique_contrasts: image_lookup[u] = images.index[images.cognitive_contrast_cogatlas_id==u].tolist() output_triples_file = "../data/concepts.tsv" # Create a data structure of tasks and contrasts for our analysis relationship_table = concept_node_triples(image_dict=image_lookup,output_file=output_triples_file) # We don't want to keep the images on the tree keep_nodes = [x for x in relationship_table.id.tolist() if not re.search("node_",x)] relationship_table = relationship_table[relationship_table.id.isin(keep_nodes)] tree = named_ontology_tree_from_tsv(relationship_table,output_json=None) pickle.dump(tree,open("../data/concepts.pkl","w")) json.dump(tree,open("../static/concepts.json",'w'))