Пример #1
0
    def testUploadNIDMZip(self):

        for name, info in self.files.items():
            zip_file = open(info['file'], 'rb')
            post_dict = {
                'name': name,
                'description': '{0} upload test'.format(name),
                'collection': self.coll.pk,
            }

            fname = os.path.basename(info['file'])
            file_dict = {
                'zip_file': SimpleUploadedFile(fname, zip_file.read())}
            form = NIDMResultsForm(post_dict, file_dict)

            self.assertTrue(form.is_valid())

            nidm = form.save()

            self.assertEquals(len(nidm.nidmresultstatisticmap_set.all()),
                              info['num_statmaps'])

            map_type = info['output_row']['type'][0]
            map_img = nidm.nidmresultstatisticmap_set.filter(
                map_type=map_type).first()

            self.assertEquals(map_img.name, info['output_row']['name'])
Пример #2
0
    def test_adding_nidm(self):
        zip_file = open(os.path.join(self.test_path,'test_data/nidm/fsl.nidm.zip'), 'rb')
        post_dict = {
            'name': 'fsl_nidm',
            'description':'{0} upload test'.format('fsl_nidm'),
            'collection':self.Collection1.pk}
        fname = os.path.basename(os.path.join(self.test_path,'test_data/nidm/fsl.nidm.zip'))
        file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file.read())}
        zip_file.close()
        form = NIDMResultsForm(post_dict, file_dict)
        # Transforms should be generated synchronously
        nidm = form.save()
        images_processing = count_processing_comparisons()
        print "\nTesting Counter - added nidm result ###" 
        # And when we count, there should be 0 still processing
        print "%s images processing [should be 0]" %(images_processing)
        assert_equal(images_processing,0)

        # We should have 2 images total, so 1 comparison
        total_comparisons = count_existing_comparisons()
        assert_equal(total_comparisons,1)

        # Make sure comparisons were calculated
        number_comparisons = len(Comparison.objects.all())
        print "\n %s comparisons exist after adding NIDM [should not be 0]" %(number_comparisons)
        assert_equal(number_comparisons>0,True)
Пример #3
0
    def test_adding_nidm(self):
        Image2 = StatisticMap(name='Image2', collection=self.Collection1, file='beta_0001.nii.gz', map_type="Other")
        Image2.file = SimpleUploadedFile('beta_0001.nii.gz', file(os.path.join(self.test_path,'test_data/statmaps/beta_0001.nii.gz')).read())
        Image2.save()
        
        zip_file = open(os.path.join(self.test_path,'test_data/nidm/spm_example.nidm.zip'), 'rb')
        post_dict = {
            'name': 'spm_nidm',
            'description':'{0} upload test'.format('spm_example'),
            'collection':self.Collection2.pk}
        fname = os.path.basename(os.path.join(self.test_path,'test_data/nidm/spm_example.nidm.zip'))
        file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file.read())}
        zip_file.close()
        form = NIDMResultsForm(post_dict, file_dict)
        # Transforms should be generated synchronously
        nidm = form.save()
        print "\nTesting Counter - added nidm result ###"

        # We should have 2 images total, so 1 comparison
        total_comparisons = count_existing_comparisons(Image2.pk)
        self.assertEqual(total_comparisons,1)
        
        #Let's add a single subject map - this should not trigger a comparison
        Image2ss = StatisticMap(name='Image2 - single subject', collection=self.Collection3, file='beta_0001.nii.gz', map_type="Other", analysis_level='S')
        Image2ss.file = SimpleUploadedFile('beta_0001.nii.gz', file(os.path.join(self.test_path,'test_data/statmaps/beta_0001.nii.gz')).read())
        Image2ss.save()
        total_comparisons = count_existing_comparisons(Image2ss.pk)
        self.assertEqual(total_comparisons,0)

        # Make sure comparisons were calculated
        number_comparisons = len(Comparison.objects.all())
        print "\n %s comparisons exist after adding NIDM `[should not be 0]" %(number_comparisons)
        self.assertEqual(number_comparisons>0,True)
Пример #4
0
    def testFEAT_NIDM(self):

        # test feat parsing
        for fname, info in self.testfiles.items():
            info['found_feat'] = False
            for root, dirs, files in os.walk(info['dir']):
                if detect_feat_directory(root):
                    print 'Found FEAT directory at {}.'.format(root)
                    info['found_feat'] = True
                    try:
                        fslnidm = FSLtoNIDMExporter(feat_dir=root, version="0.2.0")
                        fslnidm.parse()
                        export_dir = fslnidm.export()
                        ttl_file = os.path.join(export_dir,'nidm.ttl')
                    except:
                        print("Unable to parse the FEAT directory: \n{0}.".format(get_traceback()))

                    # confirm results path and existence
                    self.assertTrue(os.path.exists(export_dir))
                    self.assertEquals(os.path.join(info['dir'],info['export_dir']),export_dir)

                    # incomplete ttl = failure in processing
                    self.assertGreaterEqual(os.path.getsize(ttl_file),info['ttl_fsize'])

        # test upload nidm
        for fname, info in self.testfiles.items():

            nidm_zpath = os.path.join(self.tmpdir,'{}.nidm.zip'.format(fname.replace('.zip','')))
            nidm_zip = zipfile.ZipFile(nidm_zpath, 'w')

            for root, dirs, files in os.walk(os.path.join(info['dir'],info['export_dir'])):
                for nfile in files:
                    nidm_zip.write(os.path.join(root, nfile))
            nidm_zip.close()

            zname = os.path.basename(nidm_zip.filename)
            post_dict = {
                'name': zname,
                'description':'{0} upload test'.format(zname),
                'collection':self.coll.pk,
            }
            

            file_dict = {'zip_file': SimpleUploadedFile(zname, open(nidm_zpath,'r').read())}
            form = NIDMResultsForm(post_dict, file_dict)

            # validate NIDM Results
            self.assertEqual(form.errors, {})
            nidm = form.save()

            statmaps = nidm.nidmresultstatisticmap_set.all()
            self.assertEquals(len(statmaps),info['num_statmaps'])

            map_types = [v.map_type for v in statmaps]
            self.assertEquals(sorted(map_types), sorted(info['map_types']))

            names = [v.name for v in statmaps]
            self.assertEquals(sorted(names), sorted(info['names']))
Пример #5
0
    def test_adding_nidm(self):
        Image2 = StatisticMap(name='Image2',
                              collection=self.Collection1,
                              file='beta_0001.nii.gz',
                              map_type="Other")
        Image2.file = SimpleUploadedFile(
            'beta_0001.nii.gz',
            file(
                os.path.join(self.test_path,
                             'test_data/statmaps/beta_0001.nii.gz')).read())
        Image2.save()

        zip_file = open(
            os.path.join(self.test_path,
                         'test_data/nidm/spm_example.nidm.zip'), 'rb')
        post_dict = {
            'name': 'spm_nidm',
            'description': '{0} upload test'.format('spm_example'),
            'collection': self.Collection2.pk
        }
        fname = os.path.basename(
            os.path.join(self.test_path,
                         'test_data/nidm/spm_example.nidm.zip'))
        file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file.read())}
        zip_file.close()
        form = NIDMResultsForm(post_dict, file_dict)
        # Transforms should be generated synchronously
        nidm = form.save()
        print "\nTesting Counter - added nidm result ###"

        # We should have 2 images total, so 1 comparison
        total_comparisons = count_existing_comparisons(Image2.pk)
        self.assertEqual(total_comparisons, 1)

        #Let's add a single subject map - this should not trigger a comparison
        Image2ss = StatisticMap(name='Image2 - single subject',
                                collection=self.Collection3,
                                file='beta_0001.nii.gz',
                                map_type="Other",
                                analysis_level='S')
        Image2ss.file = SimpleUploadedFile(
            'beta_0001.nii.gz',
            file(
                os.path.join(self.test_path,
                             'test_data/statmaps/beta_0001.nii.gz')).read())
        Image2ss.save()
        total_comparisons = count_existing_comparisons(Image2ss.pk)
        self.assertEqual(total_comparisons, 0)

        # Make sure comparisons were calculated
        number_comparisons = len(Comparison.objects.all())
        print "\n %s comparisons exist after adding NIDM `[should not be 0]" % (
            number_comparisons)
        self.assertEqual(number_comparisons > 0, True)
Пример #6
0
def save_nidm_form(zip_file, collection, name=None):

    if name == None:
        name = "fsl_nidm"
    zip_file_obj = open(zip_file, "rb")
    post_dict = {"name": name, "description": "{0} upload test".format("fsl_nidm"), "collection": collection.pk}
    fname = os.path.basename(zip_file)
    file_dict = {"zip_file": SimpleUploadedFile(fname, zip_file_obj.read())}
    zip_file_obj.close()
    form = NIDMResultsForm(post_dict, file_dict)
    return form.save()
Пример #7
0
def save_nidm_form(zip_file,collection,name=None):
    
    if name == None:
        name = "fsl_nidm"
    zip_file_obj = open(zip_file, 'rb')
    post_dict = {'name': name,
                 'description':'{0} upload test'.format('fsl_nidm'),
                 'collection':collection.pk}
    fname = os.path.basename(zip_file)
    file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file_obj.read())}
    zip_file_obj.close()
    form = NIDMResultsForm(post_dict, file_dict)
    return form.save()
Пример #8
0
def save_nidm_form(zip_file,collection,name=None):
    
    if name == None:
        name = "fsl_nidm"
    zip_file_obj = open(zip_file, 'rb')
    post_dict = {'name': name,
                 'description':'{0} upload test'.format('fsl_nidm'),
                 'collection':collection.pk}
    fname = os.path.basename(zip_file)
    file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file_obj.read())}
    zip_file_obj.close()
    form = NIDMResultsForm(post_dict, file_dict)
    return form.save()
Пример #9
0
    def testDownloadCollection_NIDM_results(self):

        collection = Collection(owner=self.user, name="Collection2")
        collection.save()

        # Upload NIMDResult zip file
        zip_file = open(
            os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip'), 'rb')
        post_dict = {
            'name': 'auditory',
            'description': '{0} upload test'.format('spm_auditory_v1.2.0'),
            'collection': collection.pk,
        }
        fname = os.path.basename(
            os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip'))
        file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file.read())}
        form = NIDMResultsForm(post_dict, file_dict)
        form.save()

        # Upload Statistic Map
        image = save_statmap_form(image_path=os.path.join(
            TEST_PATH, 'test_data/statmaps/all.nii.gz'),
                                  collection=collection,
                                  image_name="all.nii.gz")

        factory = RequestFactory()
        self.client.login(username=self.user)
        request = factory.get('/collections/%s/download' % collection.pk,
                              {'format': 'img.zip'})
        request.user = self.user
        response = download_collection(request, str(collection.pk))

        self.assertTrue(response.streaming_content)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get('Content-Disposition'),
                         "attachment; filename=" + collection.name + ".zip")

        zf = zipfile.ZipFile(io.BytesIO(''.join(response.streaming_content)))

        self.assertEqual(len(zf.filelist), 2)  # 1 NIDMResult, 1 Statmap
        self.assertIsNone(zf.testzip())
        self.assertIn("Collection2/all.nii.gz", zf.namelist())
        self.assertIn("Collection2/auditory.nidm.zip", zf.namelist())
Пример #10
0
    def testFEAT_NIDM(self):

        # test feat parsing
        for fname, info in self.testfiles.items():
            info['found_feat'] = False
            for root, dirs, files in os.walk(info['dir']):
                if detect_feat_directory(root):
                    print 'Found FEAT directory at {}.'.format(root)
                    info['found_feat'] = True
                    fslnidm = FSLtoNIDMExporter(feat_dir=root, version="1.2.0")
                    fslnidm.parse()
                    info['nidm_file'] = fslnidm.export()

                    # confirm results path and existence
                    self.assertTrue(os.path.exists(info['nidm_file']))

        # test upload nidm
        for fname, info in self.testfiles.items():

            zname = os.path.basename(info['nidm_file'])
            post_dict = {
                'name': zname,
                'description': '{0} upload test'.format(zname),
                'collection': self.coll.pk,
            }
            

            file_dict = {'zip_file': SimpleUploadedFile(zname, open(info['nidm_file'],'r').read())}
            form = NIDMResultsForm(post_dict, file_dict)

            # validate NIDM Results
            self.assertEqual(form.errors, {})
            nidm = form.save()

            statmaps = nidm.nidmresultstatisticmap_set.all()
            self.assertEquals(len(statmaps),info['num_statmaps'])

            map_types = [v.map_type for v in statmaps]
            self.assertEquals(sorted(map_types), sorted(info['map_types']))

            names = [v.name for v in statmaps]
            self.assertEquals(sorted(names), sorted(info['names']))
Пример #11
0
    def testDownloadCollection_NIDM_results(self):

        collection = Collection(owner=self.user, name="Collection2")
        collection.save()

        # Upload NIMDResult zip file
        zip_file = open(os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip'), 'rb')
        post_dict = {
            'name': 'auditory',
            'description': '{0} upload test'.format('spm_auditory_v1.2.0'),
            'collection': collection.pk,
        }
        fname = os.path.basename(os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip'))
        file_dict = {
            'zip_file': SimpleUploadedFile(fname, zip_file.read())}
        form = NIDMResultsForm(post_dict, file_dict)
        form.save()

        # Upload Statistic Map
        image = save_statmap_form(image_path=os.path.join(TEST_PATH, 'test_data/statmaps/all.nii.gz'),
                                   collection=collection,
                                   image_name="all.nii.gz")

        factory = RequestFactory()
        self.client.login(username=self.user)
        request = factory.get('/collections/%s/download' % collection.pk, {'format': 'img.zip'})
        request.user = self.user
        response = download_collection(request, str(collection.pk))

        self.assertTrue(response.streaming_content)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get('Content-Disposition'), "attachment; filename=" + collection.name + ".zip")

        zf = zipfile.ZipFile(io.BytesIO(''.join(response.streaming_content)))

        self.assertEqual(len(zf.filelist),2)  # 1 NIDMResult, 1 Statmap
        self.assertIsNone(zf.testzip())
        self.assertIn("Collection2/all.nii.gz", zf.namelist())
        self.assertIn("Collection2/auditory.nidm.zip", zf.namelist())
Пример #12
0
def view_nidm_results(request, collection_cid, nidm_name):
    collection = get_collection(collection_cid,request)
    nidmr = get_object_or_404(NIDMResults, collection=collection,name=nidm_name)
    if request.method == "POST":
        if not request.user.has_perm("statmaps.change_nidmresults", nidmr):
            return HttpResponseForbidden()
        form = NIDMResultsForm(request.POST, request.FILES, instance=nidmr)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            form.save_nidm()
            form.save_m2m()
            return HttpResponseRedirect(collection.get_absolute_url())
    else:
        if owner_or_contrib(request,collection):
            form = NIDMResultsForm(instance=nidmr)
        else:
            form = NIDMViewForm(instance=nidmr)

    # Generate viewer for nidm result
    nidm_files = [nidmr.ttl_file.path.encode("utf-8")]
    standard_brain = "/static/images/MNI152.nii.gz"

    # We will remove these scripts
    remove_resources = ["BOOTSTRAPCSS","ROBOTOFONT"]

    # We will remove these columns
    columns_to_remove = ["statmap_location","statmap",
                         "statmap_type","coordinate_id"]

    html_snippet = generate(nidm_files,
                            base_image=standard_brain,
                            remove_scripts=remove_resources,
                            columns_to_remove=columns_to_remove,
                            template_choice="embed")

    context = {"form": form,"nidm_viewer":html_snippet}
    return render(request, "statmaps/edit_nidm_results.html", context)
Пример #13
0
def view_nidm_results(request, collection_cid, nidm_name):
    collection = get_collection(collection_cid,request)
    try:
        nidmr = NIDMResults.objects.get(collection=collection,name=nidm_name)
    except NIDMResults.DoesNotExist:
        return Http404("This NIDM Result was not found.")
    if request.method == "POST":
        if not owner_or_contrib(request,collection):
            return HttpResponseForbidden()
        form = NIDMResultsForm(request.POST, request.FILES, instance=nidmr)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            form.save_nidm()
            form.save_m2m()
            return HttpResponseRedirect(collection.get_absolute_url())
    else:
        if owner_or_contrib(request,collection):
            form = NIDMResultsForm(instance=nidmr)
        else:
            form = NIDMViewForm(instance=nidmr)

    context = {"form": form}
    return render(request, "statmaps/edit_nidm_results.html.haml", context)
Пример #14
0
    def testFEAT_NIDM(self):

        # test feat parsing
        for fname, info in self.testfiles.items():
            info['found_feat'] = False
            for root, dirs, files in os.walk(info['dir']):
                if detect_feat_directory(root):
                    print 'Found FEAT directory at {}.'.format(root)
                    info['found_feat'] = True
                    try:
                        fslnidm = FSLtoNIDMExporter(feat_dir=root,
                                                    version="0.2.0")
                        fslnidm.parse()
                        export_dir = fslnidm.export()
                        ttl_file = os.path.join(export_dir, 'nidm.ttl')
                    except:
                        print("Unable to parse the FEAT directory: \n{0}.".
                              format(get_traceback()))

                    # confirm results path and existence
                    self.assertTrue(os.path.exists(export_dir))
                    self.assertEquals(
                        os.path.join(info['dir'], info['export_dir']),
                        export_dir)

                    # incomplete ttl = failure in processing
                    self.assertEquals(os.path.getsize(ttl_file),
                                      info['ttl_fsize'])

        # test upload nidm
        for fname, info in self.testfiles.items():

            nidm_zpath = os.path.join(
                self.tmpdir, '{}.nidm.zip'.format(fname.replace('.zip', '')))
            nidm_zip = zipfile.ZipFile(nidm_zpath, 'w')

            for root, dirs, files in os.walk(
                    os.path.join(info['dir'], info['export_dir'])):
                for nfile in files:
                    nidm_zip.write(os.path.join(root, nfile))
            nidm_zip.close()

            zname = os.path.basename(nidm_zip.filename)
            post_dict = {
                'name': zname,
                'description': '{0} upload test'.format(zname),
                'collection': self.coll.pk,
            }

            file_dict = {
                'zip_file': SimpleUploadedFile(zname,
                                               open(nidm_zpath, 'r').read())
            }
            form = NIDMResultsForm(post_dict, file_dict)

            # validate NIDM Results
            self.assertTrue(form.is_valid())
            nidm = form.save()

            statmaps = nidm.nidmresultstatisticmap_set.all()
            self.assertEquals(len(statmaps), info['num_statmaps'])

            map_types = [v.map_type for v in statmaps]
            self.assertEquals(sorted(map_types), sorted(info['map_types']))

            names = [v.name for v in statmaps]
            self.assertEquals(sorted(names), sorted(info['names']))
Пример #15
0
def view_nidm_results(request, collection_cid, nidm_name):
    collection = get_collection(collection_cid, request)
    try:
        nidmr = NIDMResults.objects.get(collection=collection, name=nidm_name)
    except NIDMResults.DoesNotExist:
        return Http404("This NIDM Result was not found.")
    if request.method == "POST":
        if not owner_or_contrib(request, collection):
            return HttpResponseForbidden()
        form = NIDMResultsForm(request.POST, request.FILES, instance=nidmr)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            form.save_nidm()
            form.save_m2m()
            return HttpResponseRedirect(collection.get_absolute_url())
    else:
        if owner_or_contrib(request, collection):
            form = NIDMResultsForm(instance=nidmr)
        else:
            form = NIDMViewForm(instance=nidmr)

    context = {"form": form}
    return render(request, "statmaps/edit_nidm_results.html.haml", context)