Example #1
0
    def clean_and_validate(self, cleaned_data):
        print "enter clean_and_validate"
        file = cleaned_data.get('file')
        surface_left_file = cleaned_data.get('surface_left_file')
        surface_right_file = cleaned_data.get('surface_right_file')

        if surface_left_file and surface_right_file and not file:
            if "file" in self._errors.keys():
                del self._errors["file"]
            cleaned_data["data_origin"] = 'surface'
            tmp_dir = tempfile.mkdtemp()
            try:
                new_name = cleaned_data["name"] + ".nii.gz"
                ribbon_projection_file = os.path.join(tmp_dir, new_name)

                inputs_dict = {"lh": "surface_left_file",
                               "rh": "surface_right_file"}
                intent_dict = {"lh": "CortexLeft",
                               "rh": "CortexRight"}

                for hemi in ["lh", "rh"]:
                    print hemi
                    surface_file = cleaned_data.get(inputs_dict[hemi])
                    _, ext = splitext_nii_gz(surface_file.name)

                    if not ext.lower() in [".mgh", ".curv", ".gii", ".nii", ".nii.gz"]:
                        self._errors[inputs_dict[hemi]] = self.error_class(
                            ["Doesn't have proper extension"]
                        )
                        del cleaned_data[inputs_dict[hemi]]
                        return cleaned_data

                    infile = os.path.join(tmp_dir, hemi + ext)

                    print "write " + hemi
                    print surface_file.file
                    surface_file.open()
                    surface_file = StringIO(surface_file.read())
                    with open(infile, 'w') as fd:
                        surface_file.seek(0)
                        shutil.copyfileobj(surface_file, fd)

                    try:
                        if ext.lower() != ".gii":
                            out_gii = os.path.join(tmp_dir, hemi + '.gii')
                            subprocess.check_output(
                                [os.path.join(os.environ['FREESURFER_HOME'],
                                              "bin", "mris_convert"),
                                 "-c", infile,
                                 os.path.join(os.environ['FREESURFER_HOME'],
                                              "subjects", "fsaverage", "surf",
                                              hemi + ".white"),
                                 out_gii])
                        else:
                            out_gii = infile

                        gii = nb.load(out_gii)

                        if gii.darrays[0].dims != [163842]:
                            self._errors[inputs_dict[hemi]] = self.error_class(
                                ["Doesn't have proper dimensions - are you sure it's fsaverage?"]
                            )
                            del cleaned_data[inputs_dict[hemi]]
                            return cleaned_data

                        # fix intent
                        old_dict = gii.meta.metadata
                        old_dict['AnatomicalStructurePrimary'] = intent_dict[hemi]
                        gii.meta = gii.meta.from_dict(old_dict)
                        gii.to_filename(os.path.join(tmp_dir, hemi + '.gii'))

                        subprocess.check_output(
                            [os.path.join(os.environ['FREESURFER_HOME'],
                                          "bin", "mri_surf2surf"),
                             "--s", "fsaverage",
                             "--hemi", hemi,
                             "--srcsurfval",
                             os.path.join(tmp_dir, hemi+'.gii'),
                             "--trgsubject", "ICBM2009c_asym_nlin",
                             "--trgsurfval",
                             os.path.join(tmp_dir, hemi+'.MNI.gii')])
                    except CalledProcessError, e:
                        raise RuntimeError(str(e.cmd) + " returned code " +
                                           str(e.returncode) + " with output " + e.output)

                cleaned_data['surface_left_file'] = memory_uploadfile(
                    os.path.join(tmp_dir, 'lh.gii'),
                    new_name[:-7] + ".fsaverage.lh.func.gii", None)
                cleaned_data['surface_right_file'] = memory_uploadfile(
                    os.path.join(tmp_dir, 'rh.gii'),
                    new_name[:-7] + ".fsaverage.rh.func.gii", None)
                print "surf2vol"
                try:
                    subprocess.check_output(
                        [os.path.join(os.environ['FREESURFER_HOME'],
                                      "bin", "mri_surf2vol"),
                         "--subject", "ICBM2009c_asym_nlin",
                         "--o",
                         ribbon_projection_file[:-3],
                         "--so",
                         os.path.join(os.environ['FREESURFER_HOME'],
                                      "subjects", "ICBM2009c_asym_nlin", "surf", "lh.white"),
                         os.path.join(tmp_dir, 'lh.MNI.gii'),
                         "--so",
                         os.path.join(os.environ['FREESURFER_HOME'],
                                      "subjects", "ICBM2009c_asym_nlin", "surf", "rh.white"),
                         os.path.join(tmp_dir, 'rh.MNI.gii')])
                except CalledProcessError, e:
                    raise RuntimeError(str(e.cmd) + " returned code " +
                                       str(e.returncode) + " with output " + e.output)

                #fix one voxel offset
                nii = nb.load(ribbon_projection_file[:-3])
                affine = nii.affine
                affine[0, 3] -= 1
                nb.Nifti1Image(nii.get_data(), affine).to_filename(ribbon_projection_file)

                cleaned_data['file'] = memory_uploadfile(
                    ribbon_projection_file, new_name, None)
Example #2
0
    def clean_and_validate(self, cleaned_data):
        print "enter clean_and_validate"
        file = cleaned_data.get('file')
        surface_left_file = cleaned_data.get('surface_left_file')
        surface_right_file = cleaned_data.get('surface_right_file')

        if surface_left_file and surface_right_file and not file:
            if "file" in self._errors.keys():
                del self._errors["file"]
            cleaned_data["data_origin"] = 'surface'
            tmp_dir = tempfile.mkdtemp()
            try:
                new_name = cleaned_data["name"] + ".nii.gz"
                ribbon_projection_file = os.path.join(tmp_dir, new_name)

                inputs_dict = {"lh": "surface_left_file",
                               "rh": "surface_right_file"}
                intent_dict = {"lh": "CortexLeft",
                               "rh": "CortexRight"}

                for hemi in ["lh", "rh"]:
                    print hemi
                    surface_file = cleaned_data.get(inputs_dict[hemi])
                    _, ext = splitext_nii_gz(surface_file.name)

                    if not ext.lower() in [".mgh", ".curv", ".gii", ".nii", ".nii.gz"]:
                        self._errors[inputs_dict[hemi]] = self.error_class(
                            ["Doesn't have proper extension"]
                        )
                        del cleaned_data[inputs_dict[hemi]]
                        return cleaned_data

                    infile = os.path.join(tmp_dir, hemi + ext)

                    print "write " + hemi
                    print surface_file.file
                    surface_file.open()
                    surface_file = StringIO(surface_file.read())
                    with open(infile, 'w') as fd:
                        surface_file.seek(0)
                        shutil.copyfileobj(surface_file, fd)

                    try:
                        if ext.lower() != ".gii":
                            out_gii = os.path.join(tmp_dir, hemi + '.gii')
                            subprocess.check_output(
                                [os.path.join(os.environ['FREESURFER_HOME'],
                                              "bin", "mris_convert"),
                                 "-c", infile,
                                 os.path.join(os.environ['FREESURFER_HOME'],
                                              "subjects", "fsaverage", "surf",
                                              hemi + ".white"),
                                 out_gii])
                        else:
                            out_gii = infile

                        gii = nb.load(out_gii)

                        if gii.darrays[0].dims != [163842]:
                            self._errors[inputs_dict[hemi]] = self.error_class(
                                ["Doesn't have proper dimensions - are you sure it's fsaverage?"]
                            )
                            del cleaned_data[inputs_dict[hemi]]
                            return cleaned_data

                        # fix intent
                        old_dict = gii.meta.metadata
                        old_dict['AnatomicalStructurePrimary'] = intent_dict[hemi]
                        gii.meta = gii.meta.from_dict(old_dict)
                        gii.to_filename(os.path.join(tmp_dir, hemi + '.gii'))

                        subprocess.check_output(
                            [os.path.join(os.environ['FREESURFER_HOME'],
                                          "bin", "mri_surf2surf"),
                             "--s", "fsaverage",
                             "--hemi", hemi,
                             "--srcsurfval",
                             os.path.join(tmp_dir, hemi+'.gii'),
                             "--trgsubject", "ICBM2009c_asym_nlin",
                             "--trgsurfval",
                             os.path.join(tmp_dir, hemi+'.MNI.gii')])
                    except CalledProcessError, e:
                        raise RuntimeError(str(e.cmd) + " returned code " +
                                           str(e.returncode) + " with output " + e.output)

                cleaned_data['surface_left_file'] = memory_uploadfile(
                    os.path.join(tmp_dir, 'lh.gii'),
                    new_name[:-7] + ".fsaverage.lh.func.gii", None)
                cleaned_data['surface_right_file'] = memory_uploadfile(
                    os.path.join(tmp_dir, 'rh.gii'),
                    new_name[:-7] + ".fsaverage.rh.func.gii", None)
                print "surf2vol"
                try:
                    subprocess.check_output(
                        [os.path.join(os.environ['FREESURFER_HOME'],
                                      "bin", "mri_surf2vol"),
                         "--subject", "ICBM2009c_asym_nlin",
                         "--o",
                         ribbon_projection_file[:-3],
                         "--so",
                         os.path.join(os.environ['FREESURFER_HOME'],
                                      "subjects", "ICBM2009c_asym_nlin", "surf", "lh.white"),
                         os.path.join(tmp_dir, 'lh.MNI.gii'),
                         "--so",
                         os.path.join(os.environ['FREESURFER_HOME'],
                                      "subjects", "ICBM2009c_asym_nlin", "surf", "rh.white"),
                         os.path.join(tmp_dir, 'rh.MNI.gii')])
                except CalledProcessError, e:
                    raise RuntimeError(str(e.cmd) + " returned code " +
                                       str(e.returncode) + " with output " + e.output)

                #fix one voxel offset
                nii = nb.load(ribbon_projection_file[:-3])
                affine = nii.affine
                affine[0, 3] -= 1
                nb.Nifti1Image(nii.get_data(), affine).to_filename(ribbon_projection_file)

                cleaned_data['file'] = memory_uploadfile(
                    ribbon_projection_file, new_name, None)
Example #3
0
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))
Example #4
0
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))