filetools.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:pyconnectome 作者: neurospin 项目源码 文件源码
def merge_fibers(tractograms, tempdir=None):
    """ Merge tractograms.

    Parameters
    ----------
    tractograms: list of str
        paths to the input tractograms.
    tempdir: str, default None
        a temporary directory to store intermediate tractogram.

    Returns
    -------
    merge_tractogram_file: str
        all the streamlines in one TRK file.
    """
    # Check existence of input file
    for path in tractograms:
        if not os.path.isfile(path):
            raise ValueError("File does not exist: {0}.".format(path))

    # Create a temporary directory to store an intermediate tractogram
    tempdir = tempfile.mkdtemp(prefix="tractconverter_", dir=tempdir)

    # Combine tractograms in one file
    trk = nibabel.streamlines.load(tractograms[0])
    for trk_path in tractograms[1:]:
        part_trk = nibabel.streamlines.load(trk_path)
        trk.streamlines.extend(part_trk.streamlines)
    merge_tractogram_file = os.path.join(tempdir, "tmp.trk")
    trk.save(merge_tractogram_file)

    return merge_tractogram_file
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号