Example #1
0
    def create(cls, my_file, my_file_name, my_name, my_desc, my_collection_pk, my_map_type):
        my_collection = Collection.objects.get(pk=my_collection_pk)

        # Copy the nifti file into the proper location
        image = cls(description=my_desc, name=my_name, collection=my_collection)
        f = open(my_file)
        niftiFile = File(f);
        image.file.save(my_file_name, niftiFile);

        # If a .img file was loaded then load the correspoding .hdr file as well
        _, ext = os.path.splitext(my_file_name)
        print ext
        if ext in ['.img']:
            f = open(my_file[:-3] + "hdr")
            hdrFile = File(f);
            image.hdr_file.save(my_file_name[:-3] + "hdr", hdrFile);

        image.map_type = my_map_type;
        
        #create JSON file for neurosynth viewer
        if os.path.exists(image.file.path):
            json_file = image.file.path + '.json'
            imageutils.img_to_json(image.file.path, swap=True, save=json_file)
            image.json_path = image.file.url + '.json'
            
        image.save();

        return image
Example #2
0
    def save(self):

        # If a new file or header has been uploaded, redo the JSON conversion
        if 'file' in self.get_dirty_fields() or 'hdr_file' in self.get_dirty_fields():
            self.file.save(self.file.name, self.file, save = False)
            if self.hdr_file:
                self.hdr_file.save(self.hdr_file.name, self.hdr_file, save = False)
            if os.path.exists(self.file.path):
                json_file = self.file.path + '.json'
                imageutils.img_to_json(self.file.path, swap=True, save=json_file)
                self.json_path = self.file.url + '.json'

        super(Image, self).save()
Example #3
0
    def save(self):

        # Save the file before the rest of the data so we can convert it to json
        if self.file and not os.path.exists(self.file.path):
            self.file.save(self.file.name, self.file, save = False)
        # Convert binary image to JSON using neurosynth
        try:
            if os.path.exists(self.file.path):
                json_file = self.file.path + '.json'
                try:
                    imageutils.img_to_json(self.file.path, swap=True, save=json_file)
                    self.json_path = self.file.url + '.json'
                except Exception, e:
                    pass
        except Exception, e:
            pass
Example #4
0
 def test_img_to_json(self):
     path = get_test_data_path() + 'sgacc_mask.nii.gz'
     json_data = imageutils.img_to_json(path)
     data = json.loads(json_data)
     self.assertEqual(data['max'], 1)
     self.assertEqual(data['dims'], [91, 109, 91])
     self.assertEqual(data['values'], [1.0])
     self.assertEqual(len(data['indices'][0]), 1142)
Example #5
0
 def test_img_to_json(self):
     path = get_test_data_path() + 'sgacc_mask.nii.gz'
     json_data = imageutils.img_to_json(path)
     data = json.loads(json_data)
     self.assertEqual(data['max'], 1)
     self.assertEqual(data['dims'], [91, 109, 91])
     self.assertEqual(data['values'], [1.0])
     self.assertEqual(len(data['indices'][0]), 1142)
Example #6
0
    def save(self):

        # Save the file before the rest of the data so we can convert it to json
        if self.file and not os.path.exists(self.file.path):
            self.file.save(self.file.name, self.file, save=False)
        # Convert binary image to JSON using neurosynth
        try:
            if os.path.exists(self.file.path):
                json_file = self.file.path + '.json'
                try:
                    imageutils.img_to_json(self.file.path,
                                           swap=True,
                                           save=json_file)
                    self.json_path = self.file.url + '.json'
                except Exception, e:
                    pass
        except Exception, e:
            pass