class DownloadCollectionsTest(TestCase): def setUp(self): self.factory = RequestFactory() self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username="******") self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name="Collection1", owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas(name="unorderedAtlas", description="", collection=self.Collection1) self.unorderedAtlas.file = SimpleUploadedFile( "VentralFrontal_thr75_summaryimage_2mm.nii.gz", file(os.path.join(self.test_path, "test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz")).read(), ) self.unorderedAtlas.label_description_file = SimpleUploadedFile( "test_VentralFrontal_thr75_summaryimage_2mm.xml", file( os.path.join(self.test_path, "test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml") ).read(), ) self.unorderedAtlas.save() def tearDown(self): clearDB() self.user.delete() def testDownloadCollection(self): self.client.login(username=self.user) pk1 = self.Collection1.pk request = self.factory.get("/collections/%s/download" % pk1) request.user = self.user response = download_collection(request, str(pk1)) self.assertTrue(len(response.getvalue())) # If there is something in the response, the file was generated. self.assertEqual(response.status_code, 200)
class DownloadCollectionsTest(TestCase): def setUp(self): self.factory = RequestFactory() self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username='******') self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name='Collection1',owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas(name='unorderedAtlas', description='',collection=self.Collection1) self.unorderedAtlas.file = SimpleUploadedFile('VentralFrontal_thr75_summaryimage_2mm.nii.gz', file(os.path.join(self.test_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz')).read()) self.unorderedAtlas.label_description_file = SimpleUploadedFile('test_VentralFrontal_thr75_summaryimage_2mm.xml', file(os.path.join(self.test_path,'test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml')).read()) self.unorderedAtlas.save() def tearDown(self): clearDB() self.user.delete() def testDownloadCollection(self): self.client.login(username=self.user) pk1 = self.Collection1.pk request = self.factory.get('/collections/%s/download' %pk1) request.user = self.user response = download_collection(request, str(pk1)) self.assertTrue(response.streaming_content) self.assertEqual(response.status_code, 200) zf = zipfile.ZipFile(io.BytesIO(''.join(response.streaming_content))) self.assertEqual(len(zf.filelist), 1) # 1 Atlas self.assertIsNone(zf.testzip()) self.assertIn("Collection1/VentralFrontal_thr75_summaryimage_2mm.nii.gz", zf.namelist())
class DeleteCollectionsTest(TestCase): def setUp(self): self.factory = RequestFactory() self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username="******") self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name="Collection1", owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas(name="unorderedAtlas", description="", collection=self.Collection1) self.unorderedAtlas.file = SimpleUploadedFile( "VentralFrontal_thr75_summaryimage_2mm.nii.gz", file(os.path.join(self.test_path, "test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz")).read(), ) self.unorderedAtlas.label_description_file = SimpleUploadedFile( "test_VentralFrontal_thr75_summaryimage_2mm.xml", file( os.path.join(self.test_path, "test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml") ).read(), ) self.unorderedAtlas.save() self.Collection2 = Collection(name="Collection2", owner=self.user) self.Collection2.save() self.orderedAtlas = Atlas( name="orderedAtlas", collection=self.Collection2, label_description_file="VentralFrontal_thr75_summaryimage_2mm.xml", ) self.orderedAtlas.file = SimpleUploadedFile( "VentralFrontal_thr75_summaryimage_2mm.nii.gz", file(os.path.join(self.test_path, "test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz")).read(), ) self.orderedAtlas.label_description_file = SimpleUploadedFile( "test_VentralFrontal_thr75_summaryimage_2mm.xml", file(os.path.join(self.test_path, "test_data/api/VentralFrontal_thr75_summaryimage_2mm.xml")).read(), ) self.orderedAtlas.save() def tearDown(self): clearDB() def testDeleteCollection(self): self.client.login(username=self.user) pk1 = self.Collection1.pk pk2 = self.Collection2.pk request = self.factory.get("/collections/%s/delete" % pk1) request.user = self.user delete_collection(request, str(pk1)) imageDir = os.path.join(PRIVATE_MEDIA_ROOT, "images") dirList = os.listdir(imageDir) print dirList self.assertIn(str(self.Collection2.pk), dirList) self.assertNotIn(str(self.Collection1.pk), dirList)
class MoveImageTest(TestCase): def setUp(self): self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create_user('NeuroGuy') self.user.save() self.client = Client() self.client.login(username=self.user) self.coll1 = Collection(owner=self.user, name="Test Collection 1") self.coll1.save() self.coll2 = Collection(owner=self.user, name="Test Collection 2") self.coll2.save() def tearDown(self): clearDB() def testCollectionSharing(self): self.unorderedAtlas = Atlas(name='unorderedAtlas', description='',collection=self.coll1) self.unorderedAtlas.file = SimpleUploadedFile('VentralFrontal_thr75_summaryimage_2mm.nii.gz', file(os.path.join(self.test_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz')).read()) self.unorderedAtlas.label_description_file = SimpleUploadedFile('test_VentralFrontal_thr75_summaryimage_2mm.xml', file(os.path.join(self.test_path,'test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml')).read()) self.unorderedAtlas.save() self.unorderedAtlas = Image.objects.get(id=self.unorderedAtlas.id) print self.unorderedAtlas.file.path self.assertTrue(os.path.exists(self.unorderedAtlas.file.path)) self.assertTrue(os.path.exists(self.unorderedAtlas.label_description_file.path)) self.assertTrue(os.path.exists(self.unorderedAtlas.thumbnail.path)) old_path = self.unorderedAtlas.file.path self.unorderedAtlas.collection = self.coll2 self.unorderedAtlas.save() #check if old files were deleted self.assertFalse(os.path.exists(old_path)) #check if new files exist self.unorderedAtlas = Image.objects.get(id=self.unorderedAtlas.id) self.assertTrue(os.path.exists(self.unorderedAtlas.file.path)) self.assertTrue(os.path.exists(self.unorderedAtlas.label_description_file.path)) self.assertTrue(os.path.exists(self.unorderedAtlas.thumbnail.path)) self.coll1.delete() self.unorderedAtlas = Image.objects.get(id=self.unorderedAtlas.id) print self.unorderedAtlas.file.path self.assertTrue(os.path.exists(self.unorderedAtlas.file.path)) self.assertTrue(os.path.exists(self.unorderedAtlas.label_description_file.path)) self.assertTrue(os.path.exists(self.unorderedAtlas.thumbnail.path))
def setUp(self): app_path = os.path.abspath(os.path.dirname(__file__)) self.u1 = User.objects.create(username='******') atlasCollection = Collection(name='atlasCollection',owner=self.u1) atlasCollection.save() unorderedAtlas = Atlas(name='unorderedAtlas', description='',collection=atlasCollection) unorderedAtlas.file = SimpleUploadedFile('VentralFrontal_thr75_summaryimage_2mm.nii.gz', file(os.path.join(app_path,'test_data/VentralFrontal_thr75_summaryimage_2mm.nii.gz')).read()) unorderedAtlas.label_description_file = SimpleUploadedFile('test_VentralFrontal_thr75_summaryimage_2mm.xml', file(os.path.join(app_path,'test_data/unordered_VentralFrontal_thr75_summaryimage_2mm.xml')).read()) unorderedAtlas.save() orderedAtlas = Atlas(name='orderedAtlas', collection=atlasCollection, file='VentralFrontal_thr75_summaryimage_2mm.nii.gz', label_description_file='VentralFrontal_thr75_summaryimage_2mm.xml') orderedAtlas.file = SimpleUploadedFile('VentralFrontal_thr75_summaryimage_2mm.nii.gz', file(os.path.join(app_path,'test_data/VentralFrontal_thr75_summaryimage_2mm.nii.gz')).read()) orderedAtlas.label_description_file = SimpleUploadedFile('test_VentralFrontal_thr75_summaryimage_2mm.xml', file(os.path.join(app_path,'test_data/VentralFrontal_thr75_summaryimage_2mm.xml')).read()) orderedAtlas.save()
class DownloadCollectionsTest(TestCase): def setUp(self): self.factory = RequestFactory() self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username='******') self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name='Collection1', owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas(name='unorderedAtlas', description='', collection=self.Collection1) self.unorderedAtlas.file = SimpleUploadedFile( 'VentralFrontal_thr75_summaryimage_2mm.nii.gz', file( os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz' )).read()) self.unorderedAtlas.label_description_file = SimpleUploadedFile( 'test_VentralFrontal_thr75_summaryimage_2mm.xml', file( os.path.join( self.test_path, 'test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml' )).read()) self.unorderedAtlas.save() def tearDown(self): clearDB() self.user.delete() def testDownloadCollection(self): self.client.login(username=self.user) pk1 = self.Collection1.pk request = self.factory.get('/collections/%s/download' % pk1) request.user = self.user response = download_collection(request, str(pk1)) self.assertTrue(len(response.getvalue( ))) # If there is something in the response, the file was generated. self.assertEqual(response.status_code, 200)
class DeleteCollectionsTest(TestCase): def setUp(self): self.factory = RequestFactory() self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username='******') self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name='Collection1',owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas(name='unorderedAtlas', description='',collection=self.Collection1) self.unorderedAtlas.file = SimpleUploadedFile('VentralFrontal_thr75_summaryimage_2mm.nii.gz', file(os.path.join(self.test_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz')).read()) self.unorderedAtlas.label_description_file = SimpleUploadedFile('test_VentralFrontal_thr75_summaryimage_2mm.xml', file(os.path.join(self.test_path,'test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml')).read()) self.unorderedAtlas.save() self.Collection2 = Collection(name='Collection2',owner=self.user) self.Collection2.save() self.orderedAtlas = Atlas(name='orderedAtlas', collection=self.Collection2, label_description_file='VentralFrontal_thr75_summaryimage_2mm.xml') self.orderedAtlas.file = SimpleUploadedFile('VentralFrontal_thr75_summaryimage_2mm.nii.gz', file(os.path.join(self.test_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz')).read()) self.orderedAtlas.label_description_file = SimpleUploadedFile('test_VentralFrontal_thr75_summaryimage_2mm.xml', file(os.path.join(self.test_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.xml')).read()) self.orderedAtlas.save() def tearDown(self): clearDB() def testDeleteCollection(self): self.client.login(username=self.user) pk1 = self.Collection1.pk pk2 = self.Collection2.pk request = self.factory.get('/collections/%s/delete' %pk1) request.user = self.user delete_collection(request, str(pk1)) imageDir = os.path.join(PRIVATE_MEDIA_ROOT, 'images') dirList = os.listdir(imageDir) print dirList self.assertIn(str(self.Collection2.pk), dirList) self.assertNotIn(str(self.Collection1.pk), dirList)
def upload_folder(request, collection_cid): collection = get_collection(collection_cid, request) allowed_extensions = ['.nii', '.img', '.nii.gz'] niftiFiles = [] if request.method == 'POST': print request.POST print request.FILES form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): tmp_directory = tempfile.mkdtemp() print tmp_directory try: # Save archive (.zip or .tar.gz) to disk if "file" in request.FILES: archive_name = request.FILES['file'].name if fnmatch(archive_name, '*.nidm.zip'): populate_nidm_results(request, collection) return HttpResponseRedirect( collection.get_absolute_url()) _, archive_ext = os.path.splitext(archive_name) if archive_ext == '.zip': compressed = zipfile.ZipFile(request.FILES['file']) elif archive_ext == '.gz': django_file = request.FILES['file'] django_file.open() compressed = tarfile.TarFile(fileobj=gzip.GzipFile( fileobj=django_file.file, mode='r'), mode='r') else: raise Exception("Unsupported archive type %s." % archive_name) compressed.extractall(path=tmp_directory) elif "file_input[]" in request.FILES: for f, path in zip(request.FILES.getlist("file_input[]"), request.POST.getlist("paths[]")): if fnmatch(f.name, '*.nidm.zip'): request.FILES['file'] = f populate_nidm_results(request, collection) continue new_path, _ = os.path.split( os.path.join(tmp_directory, path)) mkdir_p(new_path) filename = os.path.join(new_path, f.name) tmp_file = open(filename, 'w') tmp_file.write(f.read()) tmp_file.close() else: raise Exception("Unable to find uploaded files.") atlases = {} for root, subdirs, filenames in os.walk(tmp_directory): if detect_feat_directory(root): populate_feat_directory(request, collection, root) del (subdirs) filenames = [] # .gfeat parent dir under cope*.feat should not be added as statmaps # this may be affected by future nidm-results_fsl parsing changes if root.endswith('.gfeat'): filenames = [] filenames = [f for f in filenames if not f[0] == '.'] for fname in sorted(filenames): name, ext = splitext_nii_gz(fname) nii_path = os.path.join(root, fname) if ext == '.xml': print "found xml" dom = minidom.parse(os.path.join(root, fname)) for atlas in dom.getElementsByTagName( "summaryimagefile"): print "found atlas" path, base = os.path.split( atlas.lastChild.nodeValue) nifti_name = os.path.join(path, base) atlases[str(os.path.join( root, nifti_name[1:]))] = os.path.join( root, fname) if ext in allowed_extensions: nii = nib.load(nii_path) if detect_afni4D(nii): niftiFiles.extend(split_afni4D_to_3D(nii)) else: niftiFiles.append((fname, nii_path)) for label, fpath in niftiFiles: # Read nifti file information nii = nib.load(fpath) if len(nii.get_shape()) > 3 and nii.get_shape()[3] > 1: print "skipping wrong size" continue hdr = nii.get_header() raw_hdr = hdr.structarr # SPM only !!! # Check if filename corresponds to a T-map Tregexp = re.compile('spmT.*') # Fregexp = re.compile('spmF.*') if Tregexp.search(fpath) is not None: map_type = StatisticMap.T else: # Check if filename corresponds to a F-map if Tregexp.search(fpath) is not None: map_type = StatisticMap.F else: map_type = StatisticMap.OTHER path, name, ext = split_filename(fpath) dname = name + ".nii.gz" spaced_name = name.replace('_', ' ').replace('-', ' ') if ext.lower() != ".nii.gz": new_file_tmp_dir = tempfile.mkdtemp() new_file_tmp = os.path.join(new_file_tmp_dir, name) + '.nii.gz' nib.save(nii, new_file_tmp) f = ContentFile(open(new_file_tmp).read(), name=dname) shutil.rmtree(new_file_tmp_dir) label += " (old ext: %s)" % ext else: f = ContentFile(open(fpath).read(), name=dname) collection = get_collection(collection_cid, request) if os.path.join(path, name) in atlases: new_image = Atlas(name=spaced_name, description=raw_hdr['descrip'], collection=collection) new_image.label_description_file = ContentFile( open(atlases[os.path.join(path, name)]).read(), name=name + ".xml") else: new_image = StatisticMap(name=spaced_name, description=raw_hdr['descrip'] or label, collection=collection) new_image.map_type = map_type new_image.file = f new_image.save() except: raise error = traceback.format_exc().splitlines()[-1] msg = "An error occurred with this upload: {}".format(error) messages.warning(request, msg) return HttpResponseRedirect(collection.get_absolute_url()) finally: shutil.rmtree(tmp_directory) return HttpResponseRedirect(collection.get_absolute_url()) else: form = UploadFileForm() return render_to_response("statmaps/upload_folder.html", {'form': form}, RequestContext(request))
def upload_folder(request, collection_cid): collection = get_collection(collection_cid,request) allowed_extensions = ['.nii', '.img', '.nii.gz'] niftiFiles = [] if request.method == 'POST': print request.POST print request.FILES form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): tmp_directory = tempfile.mkdtemp() print tmp_directory try: # Save archive (.zip or .tar.gz) to disk if "file" in request.FILES: archive_name = request.FILES['file'].name if fnmatch(archive_name,'*.nidm.zip'): form = populate_nidm_results(request,collection) if not form: messages.warning(request, "Invalid NIDM-Results file.") return HttpResponseRedirect(collection.get_absolute_url()) _, archive_ext = os.path.splitext(archive_name) if archive_ext == '.zip': compressed = zipfile.ZipFile(request.FILES['file']) elif archive_ext == '.gz': django_file = request.FILES['file'] django_file.open() compressed = tarfile.TarFile(fileobj=gzip.GzipFile(fileobj=django_file.file, mode='r'), mode='r') else: raise Exception("Unsupported archive type %s."%archive_name) compressed.extractall(path=tmp_directory) elif "file_input[]" in request.FILES: for f, path in zip(request.FILES.getlist( "file_input[]"), request.POST.getlist("paths[]")): if fnmatch(f.name,'*.nidm.zip'): request.FILES['file'] = f populate_nidm_results(request,collection) continue new_path, _ = os.path.split(os.path.join(tmp_directory, path)) mkdir_p(new_path) filename = os.path.join(new_path,f.name) tmp_file = open(filename, 'w') tmp_file.write(f.read()) tmp_file.close() else: raise Exception("Unable to find uploaded files.") atlases = {} for root, subdirs, filenames in os.walk(tmp_directory): if detect_feat_directory(root): populate_feat_directory(request,collection,root) del(subdirs) filenames = [] # .gfeat parent dir under cope*.feat should not be added as statmaps # this may be affected by future nidm-results_fsl parsing changes if root.endswith('.gfeat'): filenames = [] filenames = [f for f in filenames if not f[0] == '.'] for fname in sorted(filenames): name, ext = splitext_nii_gz(fname) nii_path = os.path.join(root, fname) if ext == '.xml': print "found xml" dom = minidom.parse(os.path.join(root, fname)) for atlas in dom.getElementsByTagName("summaryimagefile"): print "found atlas" path, base = os.path.split(atlas.lastChild.nodeValue) nifti_name = os.path.join(path, base) atlases[str(os.path.join(root, nifti_name[1:]))] = os.path.join(root, fname) if ext in allowed_extensions: nii = nib.load(nii_path) if detect_4D(nii): niftiFiles.extend(split_4D_to_3D(nii)) else: niftiFiles.append((fname,nii_path)) for label,fpath in niftiFiles: # Read nifti file information nii = nib.load(fpath) if len(nii.get_shape()) > 3 and nii.get_shape()[3] > 1: messages.warning(request, "Skipping %s - not a 3D file."%label) continue hdr = nii.get_header() raw_hdr = hdr.structarr # SPM only !!! # Check if filename corresponds to a T-map Tregexp = re.compile('spmT.*') # Fregexp = re.compile('spmF.*') if Tregexp.search(fpath) is not None: map_type = StatisticMap.T else: # Check if filename corresponds to a F-map if Tregexp.search(fpath) is not None: map_type = StatisticMap.F else: map_type = StatisticMap.OTHER path, name, ext = split_filename(fpath) dname = name + ".nii.gz" spaced_name = name.replace('_',' ').replace('-',' ') if ext.lower() != ".nii.gz": new_file_tmp_dir = tempfile.mkdtemp() new_file_tmp = os.path.join(new_file_tmp_dir, name) + '.nii.gz' nib.save(nii, new_file_tmp) f = ContentFile(open(new_file_tmp).read(), name=dname) shutil.rmtree(new_file_tmp_dir) label += " (old ext: %s)" % ext else: f = ContentFile(open(fpath).read(), name=dname) collection = get_collection(collection_cid,request) if os.path.join(path, name) in atlases: new_image = Atlas(name=spaced_name, description=raw_hdr['descrip'], collection=collection) new_image.label_description_file = ContentFile( open(atlases[os.path.join(path,name)]).read(), name=name + ".xml") else: new_image = StatisticMap(name=spaced_name, description=raw_hdr['descrip'] or label, collection=collection) new_image.map_type = map_type new_image.file = f new_image.save() except: error = traceback.format_exc().splitlines()[-1] msg = "An error occurred with this upload: {}".format(error) messages.warning(request, msg) return HttpResponseRedirect(collection.get_absolute_url()) finally: shutil.rmtree(tmp_directory) if not niftiFiles: messages.warning(request, "No NIFTI files (.nii, .nii.gz, .img/.hdr) found in the upload.") return HttpResponseRedirect(collection.get_absolute_url()) else: form = UploadFileForm() return render_to_response("statmaps/upload_folder.html", {'form': form}, RequestContext(request))