def clean(self, **kwargs): cleaned_data = super(StatisticMapForm, self).clean() django_file = cleaned_data.get("file") cleaned_data[ "is_valid"] = True #This will be only saved if the form will validate cleaned_data["tags"] = clean_tags(cleaned_data) if django_file and "file" not in self._errors and "hdr_file" not in self._errors: django_file.open() fileobj = StringIO(django_file.read()) django_file.seek(0) gzfileobj = GzipFile(filename=django_file.name, mode='rb', fileobj=fileobj) nii = nb.Nifti1Image.from_file_map( {'image': nb.FileHolder(django_file.name, gzfileobj)}) cleaned_data["is_thresholded"], ratio_bad = is_thresholded(nii) cleaned_data["perc_bad_voxels"] = ratio_bad * 100.0 if cleaned_data["is_thresholded"] and not cleaned_data.get( "ignore_file_warning") and cleaned_data.get( "map_type") != "R": self._errors["file"] = self.error_class([ "This map seems to be thresholded (%.4g%% of voxels are zeros). Please use an unthresholded version of the map if possible." % (cleaned_data["perc_bad_voxels"]) ]) if cleaned_data.get("hdr_file"): self._errors["hdr_file"] = self.error_class([ "This map seems to be thresholded (%.4g%% of voxels are zeros). Please use an unthresholded version of the map if possible." % (cleaned_data["perc_bad_voxels"]) ]) self.fields[ "ignore_file_warning"].widget = forms.CheckboxInput() else: cleaned_data["not_mni"], cleaned_data[ "brain_coverage"], cleaned_data[ "perc_voxels_outside"] = not_in_mni(nii) if cleaned_data["not_mni"] and not cleaned_data.get( "ignore_file_warning") and cleaned_data.get( "map_type") != "R": self._errors["file"] = self.error_class([ "This map seems not to be in the MNI space (%.4g%% of meaningful voxels are outside of the brain). Please use transform your data to MNI space." % (cleaned_data["perc_voxels_outside"]) ]) if cleaned_data.get("hdr_file"): self._errors["hdr_file"] = self.error_class([ "This map seems not to be in the MNI space (%.4g%% of meaningful voxels are outside of the brain). Please use transform your data to MNI space." % (cleaned_data["perc_voxels_outside"]) ]) self.fields[ "ignore_file_warning"].widget = forms.CheckboxInput() if cleaned_data.get("map_type") == "R": if "not_mni" in cleaned_data: del cleaned_data["not_mni"] if "is_thresholded" in cleaned_data: del cleaned_data["is_thresholded"] return cleaned_data
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()
def clean(self, **kwargs): cleaned_data = super(StatisticMapForm, self).clean() django_file = cleaned_data.get("file") cleaned_data["is_valid"] = True #This will be only saved if the form will validate cleaned_data["tags"] = clean_tags(cleaned_data) # print cleaned_data if "analysis_level" in cleaned_data.keys(): if cleaned_data.get('analysis_level') == 'S': cleaned_data['number_of_subjects'] = 1 if 'number_of_subjects' in self._errors: del self._errors['number_of_subjects'] if "data_origin" in cleaned_data.keys() and cleaned_data["data_origin"] == "surface": cleaned_data["is_thresholded"] = False cleaned_data["not_mni"] = False cleaned_data["perc_bad_voxels"] = 0 cleaned_data["brain_coverage"] = 100 elif django_file and "file" not in self._errors and "hdr_file" not in self._errors: django_file.open() fileobj = StringIO(django_file.read()) django_file.seek(0) gzfileobj = GzipFile( filename=django_file.name, mode='rb', fileobj=fileobj) nii = nb.Nifti1Image.from_file_map( {'image': nb.FileHolder(django_file.name, gzfileobj)}) cleaned_data["is_thresholded"], ratio_bad = is_thresholded(nii) cleaned_data["perc_bad_voxels"] = ratio_bad*100.0 if cleaned_data["is_thresholded"] and not cleaned_data.get("ignore_file_warning") and cleaned_data.get("map_type") != "R": self._errors["file"] = self.error_class( ["This map seems to be thresholded (%.4g%% of voxels are zeros). Please use an unthresholded version of the map if possible." % (cleaned_data["perc_bad_voxels"])]) if cleaned_data.get("hdr_file"): self._errors["hdr_file"] = self.error_class( ["This map seems to be thresholded (%.4g%% of voxels are zeros). Please use an unthresholded version of the map if possible." % (cleaned_data["perc_bad_voxels"])]) self.fields[ "ignore_file_warning"].widget = forms.CheckboxInput() else: cleaned_data["not_mni"], cleaned_data["brain_coverage"], cleaned_data[ "perc_voxels_outside"] = not_in_mni(nii, target_template_image=cleaned_data["target_template_image"]) if cleaned_data["not_mni"] and not cleaned_data.get("ignore_file_warning") and cleaned_data.get( "map_type") != "R": self._errors["file"] = self.error_class( ["This map seems not to be in the MNI space (%.4g%% of meaningful voxels are outside of the brain). Please use transform your data to MNI space." % (cleaned_data["perc_voxels_outside"])]) if cleaned_data.get("hdr_file"): self._errors["hdr_file"] = self.error_class( ["This map seems not to be in the MNI space (%.4g%% of meaningful voxels are outside of the brain). Please use transform your data to MNI space." % (cleaned_data["perc_voxels_outside"])]) self.fields[ "ignore_file_warning"].widget = forms.CheckboxInput() if cleaned_data.get("map_type") == "R": if "not_mni" in cleaned_data: del cleaned_data["not_mni"] if "is_thresholded" in cleaned_data: del cleaned_data["is_thresholded"] return cleaned_data
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()
def clean(self, **kwargs): cleaned_data = super(StatisticMapForm, self).clean() django_file = cleaned_data.get("file") cleaned_data["is_valid"] = True #This will be only saved if the form will validate if django_file and "file" not in self._errors and "hdr_file" not in self._errors: django_file.open() gzfileobj = GzipFile( filename=django_file.name, mode='rb', fileobj=django_file.file) nii = nb.Nifti1Image.from_file_map( {'image': nb.FileHolder(django_file.name, gzfileobj)}) cleaned_data["is_thresholded"], ratio_bad = is_thresholded(nii) cleaned_data["perc_bad_voxels"] = ratio_bad*100.0 if cleaned_data["is_thresholded"] and not cleaned_data.get("ignore_file_warning"): self._errors["file"] = self.error_class( ["This map seems to be thresholded (%.4g%% of voxels are zeros). Please use an unthresholded version of the map if possible." % (cleaned_data["perc_bad_voxels"])]) if cleaned_data.get("hdr_file"): self._errors["hdr_file"] = self.error_class( ["This map seems to be thresholded (%.4g%% of voxels are zeros). Please use an unthresholded version of the map if possible." % (cleaned_data["perc_bad_voxels"])]) self.fields[ "ignore_file_warning"].widget = forms.CheckboxInput() else: cleaned_data["not_mni"], cleaned_data["brain_coverage"], cleaned_data[ "perc_voxels_outside"] = not_in_mni(nii) if cleaned_data["not_mni"] and not cleaned_data.get("ignore_file_warning"): self._errors["file"] = self.error_class( ["This map seems not to be in the MNI space (%.4g%% of meaningful voxels are outside of the brain). Please use transform your data to MNI space." % (cleaned_data["perc_voxels_outside"])]) if cleaned_data.get("hdr_file"): self._errors["hdr_file"] = self.error_class( ["This map seems not to be in the MNI space (%.4g%% of meaningful voxels are outside of the brain). Please use transform your data to MNI space." % (cleaned_data["perc_voxels_outside"])]) self.fields[ "ignore_file_warning"].widget = forms.CheckboxInput() return cleaned_data