def testaddAFNI(self): post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality': 'fMRI-BOLD', 'map_type': 'T', 'number_of_subjects': 10, 'analysis_level': 'G', 'collection': self.coll.pk, 'target_template_image': 'GenericMNI', } testpath = os.path.abspath(os.path.dirname(__file__)) fname = os.path.join(testpath, 'test_data/statmaps/saccade.I_C.MNI.nii.gz') nii = nb.load(fname) self.assertTrue(detect_4D(nii)) self.assertTrue(len(split_4D_to_3D(nii)) > 0) file_dict = {'file': SimpleUploadedFile(fname, open(fname).read())} form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual( StatisticMap.objects.filter(collection=self.coll.pk).count(), 2)
def save_statmap_form(image_path,collection,ignore_file_warning=False,image_name=None): if image_name == None: if isinstance(image_path,list): image_name = image_path[0] else: image_name = image_path post_dict = { 'name': image_name, 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality': 'fMRI-BOLD', 'map_type': 'T', 'collection': collection.pk, 'ignore_file_warning': ignore_file_warning } # If image path is a list, we have img/hdr if isinstance(image_path,list): file_dict = {'file': SimpleUploadedFile(image_path[0], open(image_path[0]).read()), 'hdr_file': SimpleUploadedFile(image_path[1], open(image_path[1]).read())} else: file_dict = {'file': SimpleUploadedFile(image_path, open(image_path).read())} form = StatisticMapForm(post_dict, file_dict) # this is necessary to split 4D volumes form.is_valid() return form.save()
def testaddAFNI(self): post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality':'fMRI-BOLD', 'map_type': 'T', 'collection':self.coll.pk, } testpath = os.path.abspath(os.path.dirname(__file__)) fname = os.path.join(testpath,'test_data/statmaps/saccade.I_C.MNI.nii.gz') nii = nb.load(fname) self.assertTrue(detect_4D(nii)) self.assertTrue(len(split_4D_to_3D(nii)) > 0) file_dict = {'file': SimpleUploadedFile(fname, open(fname).read())} form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual(StatisticMap.objects.filter(collection=self.coll.pk).count(), 2)
def save_statmap_form(image_path,collection,ignore_file_warning=False,image_name=None): if image_name == None: if isinstance(image_path,list): image_name = image_path[0] else: image_name = image_path post_dict = { 'name': image_name, 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality': 'fMRI-BOLD', 'map_type': 'T', 'target_template_image': 'GenericMNI', 'collection': collection.pk, 'ignore_file_warning': ignore_file_warning } # If image path is a list, we have img/hdr if isinstance(image_path,list): file_dict = {'file': SimpleUploadedFile(image_path[0], open(image_path[0]).read()), 'hdr_file': SimpleUploadedFile(image_path[1], open(image_path[1]).read())} else: file_dict = {'file': SimpleUploadedFile(image_path, open(image_path).read())} form = StatisticMapForm(post_dict, file_dict) # this is necessary to split 4D volumes form.is_valid() return form.save()
def save_statmap_form(image_path, collection, ignore_file_warning=False, image_name=None): if image_name == None: if isinstance(image_path, list): image_name = image_path[0] else: image_name = image_path post_dict = { "name": image_name, "cognitive_paradigm_cogatlas": "trm_4f24126c22011", "modality": "fMRI-BOLD", "map_type": "T", "collection": collection.pk, "ignore_file_warning": ignore_file_warning, } # If image path is a list, we have img/hdr if isinstance(image_path, list): file_dict = { "file": SimpleUploadedFile(image_path[0], open(image_path[0]).read()), "hdr_file": SimpleUploadedFile(image_path[1], open(image_path[1]).read()), } else: file_dict = {"file": SimpleUploadedFile(image_path, open(image_path).read())} form = StatisticMapForm(post_dict, file_dict) return form.save()
def add_image(request, collection_cid): collection = get_collection(collection_cid,request) image = StatisticMap(collection=collection) if request.method == "POST": form = StatisticMapForm(request.POST, request.FILES, instance=image) if form.is_valid(): image = form.save() return HttpResponseRedirect(image.get_absolute_url()) else: form = StatisticMapForm(instance=image) context = {"form": form} return render(request, "statmaps/add_image.html.haml", context)
def testaddImgHdr(self): post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality': 'fMRI-BOLD', 'map_type': 'T', 'number_of_subjects': 10, 'analysis_level': 'G', 'collection': self.coll.pk, 'target_template_image': 'GenericMNI', } testpath = os.path.abspath(os.path.dirname(__file__)) fname_img = os.path.join(testpath, 'test_data/statmaps/box_0b_vs_1b.img') fname_hdr = os.path.join(testpath, 'test_data/statmaps/box_0b_vs_1b.hdr') file_dict = { 'file': SimpleUploadedFile(fname_img, open(fname_img).read()), 'hdr_file': SimpleUploadedFile(fname_hdr, open(fname_hdr).read()) } form = StatisticMapForm(post_dict, file_dict) self.assertFalse(form.is_valid()) self.assertTrue("thresholded" in form.errors["file"][0]) post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality': 'fMRI-BOLD', 'map_type': 'T', 'number_of_subjects': 10, 'analysis_level': 'G', 'collection': self.coll.pk, 'ignore_file_warning': True, 'target_template_image': 'GenericMNI', } testpath = os.path.abspath(os.path.dirname(__file__)) fname_img = os.path.join(testpath, 'test_data/statmaps/box_0b_vs_1b.img') fname_hdr = os.path.join(testpath, 'test_data/statmaps/box_0b_vs_1b.hdr') file_dict = { 'file': SimpleUploadedFile(fname_img, open(fname_img).read()), 'hdr_file': SimpleUploadedFile(fname_hdr, open(fname_hdr).read()) } form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual( StatisticMap.objects.filter(collection=self.coll.pk)[0].name, "test map")
def testaddNiiGz(self): post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality':'fMRI-BOLD', 'map_type': 'T', 'collection':self.coll.pk, } testpath = os.path.abspath(os.path.dirname(__file__)) fname = os.path.join(testpath,'test_data/statmaps/motor_lips.nii.gz') file_dict = {'file': SimpleUploadedFile(fname, open(fname).read())} form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual(StatisticMap.objects.filter(collection=self.coll.pk)[0].name, "test map")
def testaddNiiGz(self): post_dict = { "name": "test map", "cognitive_paradigm_cogatlas": "trm_4f24126c22011", "modality": "fMRI-BOLD", "map_type": "T", "collection": self.coll.pk, } testpath = os.path.abspath(os.path.dirname(__file__)) fname = os.path.join(testpath, "test_data/statmaps/motor_lips.nii.gz") file_dict = {"file": SimpleUploadedFile(fname, open(fname).read())} form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual(StatisticMap.objects.filter(collection=self.coll.pk)[0].name, "test map")
def testaddNiiGz(self): post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality': 'fMRI-BOLD', 'map_type': 'T', 'collection': self.coll.pk, } testpath = os.path.abspath(os.path.dirname(__file__)) fname = os.path.join(testpath, 'test_data/statmaps/motor_lips.nii.gz') file_dict = {'file': SimpleUploadedFile(fname, open(fname).read())} form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual( StatisticMap.objects.filter(collection=self.coll.pk)[0].name, "test map")
def add_image(request, collection_cid): collection = get_collection(collection_cid, request) image = StatisticMap(collection=collection) if request.method == "POST": form = StatisticMapForm(request.POST, request.FILES, instance=image) if form.is_valid(): image = form.save() return HttpResponseRedirect(image.get_absolute_url()) else: form = StatisticMapForm(instance=image) context = {"form": form} return render(request, "statmaps/add_image.html.haml", context)
def edit_images(request, collection_cid): collection = get_collection(collection_cid, request) if not owner_or_contrib(request, collection): return HttpResponseForbidden() if request.method == "POST": formset = CollectionFormSet(request.POST, request.FILES, instance=collection) for n, form in enumerate(formset): # hack: check fields to determine polymorphic type if form.instance.polymorphic_ctype is None: atlas_f = 'image_set-{0}-label_description_file'.format(n) has_atlas = [v for v in form.files if v == atlas_f] if has_atlas: use_model = Atlas use_form = AtlasForm else: use_model = StatisticMap use_form = StatisticMapForm form.instance = use_model(collection=collection) form.base_fields = use_form.base_fields form.fields = use_form.base_fields if formset.is_valid(): formset.save() return HttpResponseRedirect(collection.get_absolute_url()) else: formset = CollectionFormSet(instance=collection) blank_statmap = StatisticMapForm(instance=StatisticMap( collection=collection)) blank_atlas = AtlasForm(instance=Atlas(collection=collection)) upload_form = UploadFileForm() context = { "formset": formset, "blank_statmap": blank_statmap, "blank_atlas": blank_atlas, "upload_form": upload_form } return render(request, "statmaps/edit_images.html", context)
def testaddImgHdr(self): post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality':'fMRI-BOLD', 'map_type': 'T', 'number_of_subjects': 10, 'analysis_level': 'G', 'collection':self.coll.pk, 'target_template_image': 'GenericMNI', } testpath = os.path.abspath(os.path.dirname(__file__)) fname_img = os.path.join(testpath,'test_data/statmaps/box_0b_vs_1b.img') fname_hdr = os.path.join(testpath,'test_data/statmaps/box_0b_vs_1b.hdr') file_dict = {'file': SimpleUploadedFile(fname_img, open(fname_img).read()), 'hdr_file': SimpleUploadedFile(fname_hdr, open(fname_hdr).read())} form = StatisticMapForm(post_dict, file_dict) self.assertFalse(form.is_valid()) self.assertTrue("thresholded" in form.errors["file"][0]) post_dict = { 'name': "test map", 'cognitive_paradigm_cogatlas': 'trm_4f24126c22011', 'modality':'fMRI-BOLD', 'map_type': 'T', 'number_of_subjects': 10, 'analysis_level': 'G', 'collection':self.coll.pk, 'ignore_file_warning': True, 'target_template_image': 'GenericMNI', } testpath = os.path.abspath(os.path.dirname(__file__)) fname_img = os.path.join(testpath,'test_data/statmaps/box_0b_vs_1b.img') fname_hdr = os.path.join(testpath,'test_data/statmaps/box_0b_vs_1b.hdr') file_dict = {'file': SimpleUploadedFile(fname_img, open(fname_img).read()), 'hdr_file': SimpleUploadedFile(fname_hdr, open(fname_hdr).read())} form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual(StatisticMap.objects.filter(collection=self.coll.pk)[0].name, "test map")
def testaddImgHdr(self): post_dict = { "name": "test map", "cognitive_paradigm_cogatlas": "trm_4f24126c22011", "modality": "fMRI-BOLD", "map_type": "T", "collection": self.coll.pk, } testpath = os.path.abspath(os.path.dirname(__file__)) fname_img = os.path.join(testpath, "test_data/statmaps/box_0b_vs_1b.img") fname_hdr = os.path.join(testpath, "test_data/statmaps/box_0b_vs_1b.hdr") file_dict = { "file": SimpleUploadedFile(fname_img, open(fname_img).read()), "hdr_file": SimpleUploadedFile(fname_hdr, open(fname_hdr).read()), } form = StatisticMapForm(post_dict, file_dict) self.assertFalse(form.is_valid()) self.assertTrue("thresholded" in form.errors["file"][0]) post_dict = { "name": "test map", "cognitive_paradigm_cogatlas": "trm_4f24126c22011", "modality": "fMRI-BOLD", "map_type": "T", "collection": self.coll.pk, "ignore_file_warning": True, } testpath = os.path.abspath(os.path.dirname(__file__)) fname_img = os.path.join(testpath, "test_data/statmaps/box_0b_vs_1b.img") fname_hdr = os.path.join(testpath, "test_data/statmaps/box_0b_vs_1b.hdr") file_dict = { "file": SimpleUploadedFile(fname_img, open(fname_img).read()), "hdr_file": SimpleUploadedFile(fname_hdr, open(fname_hdr).read()), } form = StatisticMapForm(post_dict, file_dict) self.assertTrue(form.is_valid()) form.save() self.assertEqual(StatisticMap.objects.filter(collection=self.coll.pk)[0].name, "test map")
def crawl_anima(): import neurovault.apps.statmaps.models as models from neurovault.apps.statmaps.forms import StatisticMapForm, CollectionForm username = "******" email = "*****@*****.**" try: anima_user = models.User.objects.create_user(username, email) anima_user.save() except IntegrityError: anima_user = models.User.objects.get(username=username, email=email) url = "http://anima.fz-juelich.de/api/studies" response = urllib.urlopen(url) datasets = json.loads(response.read()) # results = tarfile.open(mode="r:gz", fileobj=StringIO(response.content)) # for member in results.getmembers(): # f = results.extractfile(member) # if member.name.endswith(".study"): for url in datasets: response = requests.get(url) content = response.content.replace("PubMed ID", "PubMedID") xml_obj = e.fromstring(content) version = xml_obj.find(".").find( ".//Element[@name='Version']").text.strip() study_description = xml_obj.find( ".//Element[@name='Description']").text.strip() study_description += " This dataset was automatically imported from the ANIMA <http://anima.fz-juelich.de/> database. Version: %s" % version study_name = xml_obj.find(".").attrib['name'] tags = xml_obj.find(".//Element[@name='Keywords']").text.strip().split( ";") tags.append("ANIMA") doi = xml_obj.find(".//Element[@name='DOI']") pubmedid = xml_obj.find(".//Element[@name='PubMedID']") post_dict = { 'name': study_name, 'description': study_description, 'full_dataset_url': "http://anima.fz-juelich.de/studies/" + os.path.split(url)[1].replace(".study", "") } if doi != None: post_dict['DOI'] = doi.text.strip() elif pubmedid != None: pubmedid = pubmedid.text.strip() url = "http://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?ids=%s&format=json" % pubmedid response = urllib.urlopen(url) parsed = json.loads(response.read()) post_dict['DOI'] = parsed['records'][0]['doi'] try: col = models.Collection.objects.get(DOI=post_dict['DOI']) except models.Collection.DoesNotExist: col = None if col and not col.description.endswith(version): col.DOI = None old_version = re.search(r"Version: (?P<version>\w)", col.description).group("version") col.name = study_name + " (version %s - deprecated)" % old_version col.save() if not col or not col.description.endswith(version): collection = models.Collection(owner=anima_user) form = CollectionForm(post_dict, instance=collection) form.is_valid() collection = form.save() arch_response = requests.get( url.replace("library", "library/archives").replace(".study", ".tar.gz")) arch_results = tarfile.open(mode="r:gz", fileobj=StringIO( arch_response.content)) for study_element in xml_obj.findall( ".//StudyElement[@type='VolumeFile']"): image_name = study_element.attrib['name'].strip() image_filename = study_element.attrib['file'] try: image_fileobject = arch_results.extractfile( xml_obj.find(".").attrib['directory'] + "/" + image_filename) except KeyError: image_fileobject = arch_results.extractfile( xml_obj.find(".").attrib['directory'] + "/" + xml_obj.find(".").attrib['directory'] + "/" + image_filename) map_type = models.BaseStatisticMap.OTHER quantity_dict = { "Mask": models.BaseStatisticMap.M, "F-statistic": models.BaseStatisticMap.F, "T-statistic": models.BaseStatisticMap.T, "Z-statistic": models.BaseStatisticMap.Z, "Beta": models.BaseStatisticMap.U } quantity = study_element.find( "./Metadata/Element[@name='Quantity']") if quantity != None: quantity = quantity.text.strip() if quantity in quantity_dict.keys(): map_type = quantity_dict[quantity] post_dict = { 'name': image_name, 'modality': models.StatisticMap.fMRI_BOLD, 'map_type': map_type, 'analysis_level': models.BaseStatisticMap.M, 'collection': collection.pk, 'ignore_file_warning': True, 'cognitive_paradigm_cogatlas': 'None', 'tags': ", ".join(tags) } image_description = study_element.find( "./Metadata/Element[@name='Caption']").text if image_description: post_dict["description"] = image_description.strip() file_dict = { 'file': SimpleUploadedFile(image_filename, image_fileobject.read()) } form = StatisticMapForm(post_dict, file_dict) form.is_valid() form.save()
print quantity if quantity in quantity_dict.keys(): map_type = quantity_dict[quantity] post_dict = { 'name': image_name, 'modality': StatisticMap.fMRI_BOLD, 'map_type': map_type, 'analysis_level': BaseStatisticMap.M, 'collection': collection.pk, 'ignore_file_warning': True, 'cognitive_paradigm_cogatlas': 'None', 'tags': ", ".join(tags) } image_description = study_element.find( "./Metadata/Element[@name='Caption']").text if image_description: post_dict["description"] = image_description.strip() file_dict = { 'file': SimpleUploadedFile(image_filename, image_fileobject.read()) } form = StatisticMapForm(post_dict, file_dict) form.is_valid() print post_dict print form.errors.keys() print form.errors form.save()