Beispiel #1
0
    def save(self):
        if self.perc_bad_voxels == None and self.file:
            import neurovault.apps.statmaps.utils as nvutils
            self.file.open()
            gzfileobj = GzipFile(filename=self.file.name,
                                 mode='rb',
                                 fileobj=self.file.file)
            nii = nb.Nifti1Image.from_file_map(
                {'image': nb.FileHolder(self.file.name, gzfileobj)})
            self.is_thresholded, ratio_bad = nvutils.is_thresholded(nii)
            self.perc_bad_voxels = ratio_bad * 100.0

        if self.brain_coverage == None and self.file:
            import neurovault.apps.statmaps.utils as nvutils
            self.file.open()
            gzfileobj = GzipFile(filename=self.file.name,
                                 mode='rb',
                                 fileobj=self.file.file)
            nii = nb.Nifti1Image.from_file_map(
                {'image': nb.FileHolder(self.file.name, gzfileobj)})
            self.not_mni, self.brain_coverage, self.perc_voxels_outside = nvutils.not_in_mni(
                nii)

        if self.map_type == self.OTHER:
            import neurovault.apps.statmaps.utils as nvutils
            self.file.open()
            gzfileobj = GzipFile(filename=self.file.name,
                                 mode='rb',
                                 fileobj=self.file.file)
            nii = nb.Nifti1Image.from_file_map(
                {'image': nb.FileHolder(self.file.name, gzfileobj)})
            self.map_type = nvutils.infer_map_type(nii)

        # Calculation of image reduced_representation and comparisons
        file_changed = False
        if self.pk is not None:
            existing = Image.objects.get(pk=self.pk)
            if existing.file != self.file:
                file_changed = True
        do_update = True if file_changed else False
        new_image = True if self.pk is None else False

        # If we have an update, delete old pkl and comparisons first before saving
        if do_update and self.collection:
            if self.reduced_representation:  # not applicable for private collections
                self.reduced_representation.delete()

                # If more than one metric is added to NeuroVault, this must also filter based on metric
                comparisons = Comparison.objects.filter(
                    Q(image1=self) | Q(image2=self))
                if comparisons:
                    comparisons.delete()
        super(BaseStatisticMap, self).save()

        # Calculate comparisons
        if do_update or new_image:
            run_voxelwise_pearson_similarity.apply_async([self.pk])

        self.file.close()
Beispiel #2
0
    def save(self):
        if self.perc_bad_voxels == None and self.file:
            import neurovault.apps.statmaps.utils as nvutils
            self.file.open()
            gzfileobj = GzipFile(filename=self.file.name, mode='rb', fileobj=self.file.file)
            nii = nb.Nifti1Image.from_file_map({'image': nb.FileHolder(self.file.name, gzfileobj)})
            self.is_thresholded, ratio_bad = nvutils.is_thresholded(nii)
            self.perc_bad_voxels = ratio_bad*100.0

        if self.brain_coverage == None and self.file:
            import neurovault.apps.statmaps.utils as nvutils
            self.file.open()
            gzfileobj = GzipFile(filename=self.file.name, mode='rb', fileobj=self.file.file)
            nii = nb.Nifti1Image.from_file_map({'image': nb.FileHolder(self.file.name, gzfileobj)})
            self.not_mni, self.brain_coverage, self.perc_voxels_outside = nvutils.not_in_mni(nii)

        if self.map_type == self.OTHER:
            import neurovault.apps.statmaps.utils as nvutils
            self.file.open()
            gzfileobj = GzipFile(filename=self.file.name, mode='rb', fileobj=self.file.file)
            nii = nb.Nifti1Image.from_file_map({'image': nb.FileHolder(self.file.name, gzfileobj)})
            self.map_type = nvutils.infer_map_type(nii)

        # Calculation of image reduced_representation and comparisons
        file_changed = False
        if self.pk is not None:
            existing = Image.objects.get(pk=self.pk)
            if existing.file != self.file:
                file_changed = True
        do_update = True if file_changed else False
        new_image = True if self.pk is None else False

        # If we have an update, delete old pkl and comparisons first before saving
        if do_update and self.collection:
            if self.reduced_representation: # not applicable for private collections
                self.reduced_representation.delete()

                # If more than one metric is added to NeuroVault, this must also filter based on metric
                comparisons = Comparison.objects.filter(Q(image1=self) | Q(image2=self))
                if comparisons:
                    comparisons.delete()
        super(BaseStatisticMap, self).save()

        # Calculate comparisons
        if do_update or new_image:
            run_voxelwise_pearson_similarity.apply_async([self.pk])

        self.file.close()
import os
from gzip import GzipFile

import django
import nibabel as nb

import neurovault.apps.statmaps.utils as nvutils
from neurovault.apps.statmaps.models import StatisticMap, BaseStatisticMap

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "neurovault.settings")
django.setup()

for image in StatisticMap.objects.filter(map_type=BaseStatisticMap.OTHER):
    image.file.open()
    gzfileobj = GzipFile(filename=image.file.name, mode='rb', fileobj=image.file.file)
    nii = nb.Nifti1Image.from_file_map({'image': nb.FileHolder(image.file.name, gzfileobj)})
    map_type = nvutils.infer_map_type(nii)
    if map_type != BaseStatisticMap.OTHER:
        print "changed type of %s to %s"%(image.get_absolute_url(), map_type)
        image.map_type = map_type
        image.save()
    image.file.close()
Beispiel #4
0
 def testInferMapType(self):
     self.assertEquals(infer_map_type(self.roi_map), BaseStatisticMap.R)
     self.assertEquals(infer_map_type(self.parcellation), BaseStatisticMap.Pa)
     self.assertEquals(infer_map_type(self.brain), BaseStatisticMap.OTHER)
import os
from gzip import GzipFile

import django
import nibabel as nb

import neurovault.apps.statmaps.utils as nvutils
from neurovault.apps.statmaps.models import StatisticMap, BaseStatisticMap

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "neurovault.settings")
django.setup()

for image in StatisticMap.objects.filter(map_type=BaseStatisticMap.OTHER):
    image.file.open()
    gzfileobj = GzipFile(filename=image.file.name,
                         mode='rb',
                         fileobj=image.file.file)
    nii = nb.Nifti1Image.from_file_map(
        {'image': nb.FileHolder(image.file.name, gzfileobj)})
    map_type = nvutils.infer_map_type(nii)
    if map_type != BaseStatisticMap.OTHER:
        print "changed type of %s to %s" % (image.get_absolute_url(), map_type)
        image.map_type = map_type
        image.save()
    image.file.close()
Beispiel #6
0
 def testInferMapType(self):
     self.assertEquals(infer_map_type(self.roi_map), BaseStatisticMap.R)
     self.assertEquals(infer_map_type(self.parcellation),
                       BaseStatisticMap.Pa)
     self.assertEquals(infer_map_type(self.brain), BaseStatisticMap.OTHER)