Esempio n. 1
0
def save_streamlines(streamlines: list, path: str, to_lps=True, binary=False):
    """
    Saves the given streamlines to a file
    Parameters
    ----------
    streamlines
        The streamlines we want to save
    path
        The path we save the streamlines to
    to_lps
        A boolean indicating whether we want to save them in the LPS format instead of RAS (True by default)
    binary
        If True, the file will be written in a binary format.
    Returns
    -------

    """
    save_vtk_streamlines(streamlines, path, to_lps=to_lps, binary=binary)
        for p in range(0, pointsPerLine[i]):
            #if( np.isnan(np.sum(points[p]))==0):
            templine.append(points[p])
        if (len(templine) > 1):  # and len(templine) == pointsPerLine[i]):
            streamlines.append(templine)
        first = first + pointsPerLine[i]
    return streamlines



            #path="/home/uzair/PycharmProjects/Unfolding/data/diffusionSimulations_nonConformal_scale/"
ntracking = loc_track(path ,default_sphere)
coords = coordinates.coordinates(path,'')
utracking = loc_track(path+'Unfolded/',default_sphere,coords=coords,npath=path,UParams=Uparams)
u2n_streamlines=unfold2nativeStreamlines(utracking,coords)
streamline.save_vtk_streamlines(ntracking.streamlines,
                                path+"native_streamlines.vtk")
streamline.save_vtk_streamlines(utracking.streamlines,
                                path + "Unfolded/unfold_streamlines.vtk")
streamline.save_vtk_streamlines(u2n_streamlines,
                    path + "from_unfold_streamlines.vtk")


def plot_streamlines(streamlines):
    if has_fury:
        # Prepare the display objects.
        color = colormap.line_colors(streamlines)

        streamlines_actor = actor.line(streamlines,
                                       colormap.line_colors(streamlines))

        # Create the 3D display.
Esempio n. 3
0
 def save_to_file(self, path):
     """Save the calculated streamlines to file"""
     if self.streamlines is None:
         raise StreamlinesNotTrackedError(self) from None
     save_vtk_streamlines(self.streamlines, path)
Esempio n. 4
0
    def track(self, ang_thr=None, default_sphere=default_sphere):
        default_sphere = default_sphere.subdivide()
        default_sphere = default_sphere.subdivide()
        default_sphere = default_sphere.subdivide()
        default_sphere.vertices = np.append(default_sphere.vertices,
                                            [[1, 0, 0]])
        default_sphere.vertices = np.append(default_sphere.vertices,
                                            [[-1, 0, 0]])
        default_sphere.vertices = np.append(default_sphere.vertices,
                                            [[0, 1, 0]])
        default_sphere.vertices = np.append(default_sphere.vertices,
                                            [[0, -1, 0]])
        default_sphere.vertices = default_sphere.vertices.reshape([-1, 3])

        # this needs to be moved into tracking class (in unfoldTracking)
        def loc_track(path,
                      default_sphere,
                      coords=None,
                      npath=None,
                      UParams=None,
                      ang_thr=None):
            data, affine = load_nifti(path + 'data.nii.gz')
            data[np.isnan(data) == 1] = 0
            mask, affine = load_nifti(path + 'nodif_brain_mask.nii.gz')
            mask[np.isnan(mask) == 1] = 0
            mask[:, :, 1:] = 0
            stopper = copy.deepcopy(mask)
            #stopper[:, :, :] = 1
            gtab = gradient_table(path + 'bvals', path + 'bvecs')

            csa_model = CsaOdfModel(gtab, smooth=1, sh_order=12)
            peaks = peaks_from_model(csa_model,
                                     data,
                                     default_sphere,
                                     relative_peak_threshold=0.99,
                                     min_separation_angle=25,
                                     mask=mask)
            if ang_thr is not None:
                peaks.ang_thr = ang_thr
            if os.path.exists(path + 'grad_dev.nii.gz'):
                gd, affine_g = load_nifti(path + 'grad_dev.nii.gz')
                nmask, naffine = load_nifti(npath + 'nodif_brain_mask.nii.gz')
                nmask[np.isnan(nmask) == 1] = 0
                nmask[:, :, 1:] = 0
                seedss = copy.deepcopy(nmask)
                seedss = utils.seeds_from_mask(seedss, naffine, [2, 2, 2])
                useed = []
                UParams = coords.Uparams
                for seed in seedss:
                    us = coords.rFUa_xyz(seed[0], seed[1], seed[2])
                    vs = coords.rFVa_xyz(seed[0], seed[1], seed[2])
                    ws = coords.rFWa_xyz(seed[0], seed[1], seed[2])
                    condition = us >= UParams.min_a and us <= UParams.max_a and vs >= UParams.min_b and vs <= UParams.max_b \
                                and ws >= UParams.min_c and ws <= UParams.max_c
                    if condition == True:
                        useed.append([float(us), float(vs), float(ws)])
                seeds = np.asarray(useed)

            else:
                gd = None
                seedss = copy.deepcopy(mask)
                seeds = utils.seeds_from_mask(seedss, affine, [2, 2, 2])

            stopping_criterion = BinaryStoppingCriterion(stopper)
            tracked = tracking(peaks,
                               stopping_criterion,
                               seeds,
                               affine,
                               graddev=gd,
                               sphere=default_sphere)
            tracked.localTracking()
            return tracked

        # this needs to be moved to unfoldStreamlines (in unfoldTracking)
        def unfold2nativeStreamlines(tracking, coords):
            points, X = getPointsData(coords.X_uvwa_nii)
            points, Y = getPointsData(coords.Y_uvwa_nii)
            points, Z = getPointsData(coords.Z_uvwa_nii)
            allLines = tracking.streamlines.get_data()
            x = coords.rFX_uvwa(allLines[:, 0], allLines[:, 1], allLines[:, 2])
            y = coords.rFY_uvwa(allLines[:, 0], allLines[:, 1], allLines[:, 2])
            z = coords.rFZ_uvwa(allLines[:, 0], allLines[:, 1], allLines[:, 2])
            allLines = np.asarray([x, y, z]).T
            pointsPerLine = tracking.NpointsPerLine
            streamlines = []
            first = 0
            for i in range(0, len(pointsPerLine) - 1):
                templine = []
                points = allLines[first:first + pointsPerLine[i]]
                for p in range(0, pointsPerLine[i]):
                    if (np.isnan(np.sum(points[p])) == 0):
                        templine.append(points[p])
                if (len(templine) >
                        1):  # and len(templine) == pointsPerLine[i]):
                    streamlines.append(templine)
                first = first + pointsPerLine[i]
            return streamlines

            # path="/home/uzair/PycharmProjects/Unfolding/data/diffusionSimulations_nonConformal_scale/"

        ntracking = loc_track(self.npath, default_sphere, ang_thr=ang_thr)
        coords = coordinates.coordinates(self.npath, '')
        utracking = loc_track(self.upath,
                              default_sphere,
                              coords=coords,
                              npath=self.npath,
                              UParams=self.Uparams,
                              ang_thr=ang_thr)
        u2n_streamlines = unfold2nativeStreamlines(utracking, coords)
        streamline.save_vtk_streamlines(ntracking.streamlines,
                                        self.npath + "native_streamlines.vtk")
        streamline.save_vtk_streamlines(utracking.streamlines,
                                        self.upath + "unfold_streamlines.vtk")
        streamline.save_vtk_streamlines(
            u2n_streamlines, self.npath + "from_unfold_streamlines.vtk")
Esempio n. 5
0
                        return out

                    base = "/home/u2hussai/scratch/simulations/diffusionSimulations_LambdaStep-k-60_scale-50_res-"
                    npath=base + str(int(res[i_io] * 1000)) + "_um-drt-" +str(int(drt[l_io] * 100)) +"_w-"+\
                        str(int(round(w[j_io],5)*1000))+"_angthr-" + str(int(ang_thr[k_io])) + "_beta-"+str(int(beta[b]*100))+"/"
                    maskpath=base + str(int(res[0] * 1000)) + "_um-drt-" +str(int(drt[l_io] * 100)) +"_w-"+\
                        str(int(round(w[j_io],5)*1000))+"_angthr-" + str(int(ang_thr[k_io])) + "_beta-"+str(int(beta[b]*100))+"/"

                    nlines = lines(npath + 'native_streamlines.vtk')
                    ulines = lines(npath + 'from_unfold_streamlines.vtk')
                    simlines = linesFromSims(npath, nlines, ulines, phi,
                                             phiInv, drt[l_io], maskpath)

                    simlines.filter(simlines.nlines, dphi)
                    simlines.filter(simlines.ulines, dphi)
                    save_vtk_streamlines(simlines.nlines.seedlines,
                                         npath + 'seedlines_tang_native.vtk')
                    save_vtk_streamlines(simlines.ulines.seedlines,
                                         npath + 'seedlines_tang_unfold.vtk')

                    sens_roc[ttt, 0], spec_roc[ttt, 0], tangCount[0], radCount[
                        0] = simlines.thresholdSenSpec(simlines.nlines,
                                                       thres[ttt])
                    sens_roc[ttt, 1], spec_roc[ttt, 1], tangCount[1], radCount[
                        1] = simlines.thresholdSenSpec(simlines.ulines,
                                                       thres[ttt])
                    #print(sens_roc)
                    tangCountPara[0] = len(simlines.ntanglines)
                    tangCountPara[1] = len(simlines.utanglines)
                    radCountPara[0] = len(simlines.nradlines)
                    radCountPara[1] = len(simlines.uradlines)