Example #1
0
def trk_fixer(trkpath, trk_newpath, verbose=False):

    if verbose:
        t1 = time()
        txt = f'Beginning to load {trkpath}'
    trkdata = load_trk_spe(trkpath, 'same')
    if verbose:
        t2 = time()
        txt = f'It took {str(t2-t1)} seconds to load {trkpath}'
        print(txt)
    if hasattr(trkdata, 'space_attribute'):
        header = trkdata.space_attribute
    elif hasattr(trkdata, 'space_attributes'):
        header = trkdata.space_attributes
    remove, keep = trkdata.remove_invalid_streamlines()
    if verbose:
        t3 = time()
        duration = t3 - t2
        txt = f"it took {duration} to do the test on {trkpath}, and {np.size(remove)} streamlines were removed. saving it to {trk_newpath}"
        print(txt)
    if np.size(remove)==0:
        shutil.copy(trkpath, trk_newpath)
    else:
        new_streamlines = trkdata.streamlines
        save_trk_header(trk_newpath, new_streamlines, header, verbose=verbose)
    if verbose:
        t4 = time()
        txt = f'It took {str(t4-t3)} seconds to save {trk_newpath}'
        print(txt)
Example #2
0
def trk_fixer_old(trkpath, trk_newpath, verbose = False):

    if verbose:
        t1 = time()
        txt = f'Beginning to load {trkpath}'
    trkdata = load_trk_spe(trkpath, 'same')
    trk_streamlines = trkdata.streamlines
    if hasattr(trkdata, 'space_attribute'):
        header = trkdata.space_attribute
    elif hasattr(trkdata, 'space_attributes'):
        header = trkdata.space_attributes

    voxdim = trkdata.dimensions

    if verbose:
        t2 = time()
        txt = f'It took {str(t2-t1)} seconds to load {trkpath}'
        print(txt)
    vverbose=True

    orig_size = np.shape(trk_streamlines)[0]

    trk_streamlines[:] = (x for x in trk_streamlines if streamline_checker(x, voxdim,vverbose))

    """
    for i, streamline in enumerate(trk_streamlines):
        mymin = np.min(trk_streamlines, axis=0)
        mymax = np.max(trk_streamlines, axis=0)
        np.asarray(list(product(*zip(mymin, mymax))))
        bbox_min = np.min(trk_streamlines, axis=0)
        bbox_max = np.max(trk_streamlines, axis=0)
        bbox_corners = np.asarray(list(product(*zip(bbox_min, bbox_max))))
        if np.any(bbox_corners[:, 0] > voxdim[0]) or \
        np.any(bbox_corners[:, 1] > voxdim[1]) or \
        np.any(bbox_corners[:, 2] > voxdim[2]):
            np.pop()
    """

    new_size = np.shape(trk_streamlines)[0]
    cut_streamlines = orig_size - new_size

    if verbose:
        t3 = time()
        duration = t3 - t2
        txt = f"it took {duration} to do the test on {trkpath}, and {str(cut_streamlines)} streamlines were removed. saving it to {trk_newpath}"
        print(txt)

    #np.asarray(list(product(*zip(bbox_min, bbox_max))))

    if cut_streamlines!=0:
        save_trk_header(trk_newpath, trk_streamlines, header, verbose = verbose)
    else:
        shutil.copy(trkpath, trk_newpath)

    if verbose:
        t4 = time()
        txt = f'It took {str(t4-t3)} seconds to save {trk_newpath}'
        print(txt)
Example #3
0
def trktotck(trk_path, overwrite=False):

    import warnings
    try:
        tractogram = load_trk(trk_path, 'same')
    except:
        tractogram = load_trk_spe(trk_path, 'same')

    if nib.streamlines.detect_format(tractogram) is not nib.streamlines.TrkFile:
        warnings.warn("Skipping non TRK file: '{}'".format(tractogram))

    output_filename = tractogram[:-4] + '.tck'
    if os.path.isfile(output_filename) and not overwrite:
        warnings.warn("Skipping existing file: '{}'. Set overwrite to true".format(output_filename))

    trk = nib.streamlines.load(tractogram)
    nib.streamlines.save(trk.tractogram, output_filename)
            trk_folder, group_str + '_MDT' + ratio_str + '_' +
            index_to_struct[target_tuple[0]] + '_to_' +
            index_to_struct[target_tuple[1]] + '_streamlines.trk')

        if os.path.exists(fa_path):
            with open(fa_path, 'rb') as f:
                fa_lines = pickle.load(f)
        if os.path.exists(md_path):
            with open(md_path, 'rb') as f:
                md_lines = pickle.load(f)
        # '/Volumes/Data/Badea/Lab/human/AD_Decode/Analysis/Centroids_MDT_non_inclusive_symmetric_100/APOE4_MDT_ratio_100_ctx-lh-inferiorparietal_left_to_ctx-lh-inferiortemporal_left_streamlines.trk'
        if os.path.exists(trk_path):
            try:
                streamlines_data = load_trk(trk_path, 'same')
            except:
                streamlines_data = load_trk_spe(trk_path, 'same')
        streamlines = streamlines_data.streamlines

        if 'fa_lines' in locals():
            cutoff = np.percentile(fa_lines, 100 - top_percentile)
            select_streams = fa_lines > cutoff
            fa_lines = list(compress(fa_lines, select_streams))
            streamlines = list(compress(streamlines, select_streams))
            streamlines = nib.streamlines.ArraySequence(streamlines)
            if np.shape(streamlines)[0] != np.shape(fa_lines)[0]:
                raise Exception(
                    'Inconsistency between streamlines and fa lines')
        else:
            txt = f'Cannot find {fa_path}, could not select streamlines based on fa'
            warnings.warn(txt)
            fa_lines = [None]