def test_conversion():
    #Load test data
    ref = DATA_DIR + "uncinate"
    anat_filename = DATA_DIR + "anat.nii.gz"

    #Test every possible conversions
    for k1, ref_format in FORMATS.items():
        for k2, out_format in FORMATS.items():
            print "Testing {0}2{1}".format(k1, k2)
            f, out = tempfile.mkstemp('_{0}2{1}'.format(k1, k2))
            os.remove(out)

            ref_filename = ref + "." + k1
            out_filename = out + "." + k2

            input = ref_format(ref_filename, anat_filename)
            output = out_format.create(out_filename, input.hdr, anat_filename)
            tractconverter.convert(input, output)

            f, bak = tempfile.mkstemp('_{0}2{1}'.format(k2, k1))
            bak_filename = bak + "." + k1

            output = out_format(out_filename, anat_filename)
            backup = ref_format.create(bak_filename, input.hdr, anat_filename)
            tractconverter.convert(output, backup)

            input = ref_format(ref_filename, anat_filename)
            output = out_format(out_filename, anat_filename)
            compare_data(input, output)
            os.remove(out_filename)

            input = ref_format(ref_filename, anat_filename)
            backup = ref_format(bak_filename, anat_filename)
            compare_data(input, backup)
            os.remove(bak_filename)
Beispiel #2
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filenames = args.input
    out_filename = args.output
    #anat_filename = args.anat
    isForcing = args.isForce
    isVerbose = args.isVerbose

    if isVerbose:
        logging.basicConfig(level=logging.DEBUG)

    for in_filename in in_filenames:
        if not os.path.isfile(in_filename):
            parser.error('"{0}" must be an existing file!'.format(in_filename))

        if not tractconverter.is_supported(in_filename):
            parser.error('Input file must be one of {0}!'.format(",".join(
                FORMATS.keys())))

    if not tractconverter.is_supported(out_filename):
        parser.error('Output file must be one of {0}!'.format(",".join(
            FORMATS.keys())))

    if os.path.isfile(out_filename):
        if isForcing:
            logging.info('Overwriting "{0}".'.format(out_filename))
        else:
            parser.error('"{0}" already exist! Use -f to overwrite it.'.format(
                out_filename))

    inFormats = [
        tractconverter.detect_format(in_filename)
        for in_filename in in_filenames
    ]
    outFormat = tractconverter.detect_format(out_filename)

    # if anat_filename is not None:
    #     if not any(map(anat_filename.endswith, EXT_ANAT.split('|'))):
    #         if isForcing:
    #             logging.info('Reading "{0}" as a {1} file.'.format(anat_filename.split("/")[-1], EXT_ANAT))
    #         else:
    #             parser.error('Anatomy file must be one of {1}!'.format(EXT_ANAT))

    #     if not os.path.isfile(anat_filename):
    #         parser.error('"{0}" must be an existing file!'.format(anat_filename))

    #TODO: Consider different anat, space.
    hdr = {}
    hdr[Header.DIMENSIONS] = (1, 1, 1)
    hdr[Header.ORIGIN] = (1, 1, 1)
    hdr[Header.
        NB_FIBERS] = 0  # The actual number of streamlines will be added later.

    #Merge inputs to output
    inputs = (in_format(in_filename)
              for in_filename, in_format in zip(in_filenames, inFormats))
    output = outFormat.create(out_filename, hdr)
    tractconverter.merge(inputs, output)
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument(
        "-i",
        action="store",
        dest="input",
        metavar="FILE",
        required=True,
        help="input track file ({0})".format(",".join(FORMATS.keys())),
    )
    p.add_argument(
        "-o",
        action="store",
        dest="output",
        metavar="FILE",
        required=True,
        help="output track file ({0})".format(",".join(FORMATS.keys())),
    )
    p.add_argument(
        "-a",
        action="store",
        dest="anat",
        metavar="FILE",
        required=False,
        help="input anatomy file ({0})".format(EXT_ANAT),
    )
    p.add_argument(
        "-f", action="store_true", dest="isForce", help="force (pass extension check; overwrite output file)"
    )
    p.add_argument("-v", action="store_true", dest="isVerbose", help="produce verbose output")
    return p
Beispiel #4
0
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument(
        "-i",
        action="store",
        dest="input",
        nargs="+",
        metavar="FILE",
        required=True,
        help="input streamlines file ({0})".format(",".join(FORMATS.keys())),
    )
    p.add_argument(
        "-o",
        action="store",
        dest="output",
        metavar="FILE",
        required=True,
        help="merged streamline file ({0})".format(",".join(FORMATS.keys())),
    )
    # p.add_argument('-a', action='store', dest='anat',
    #                metavar='FILE', required=False,
    #                help='input anatomy file ({0})'.format(EXT_ANAT))
    p.add_argument(
        "-f", action="store_true", dest="isForce", help="force (pass extension check; overwrite output file)"
    )
    p.add_argument("-v", action="store_true", dest="isVerbose", help="produce verbose output")
    return p
Beispiel #5
0
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i',
                   action='store',
                   dest='input',
                   nargs='+',
                   metavar='FILE',
                   required=True,
                   help='input streamlines file ({0})'.format(",".join(
                       FORMATS.keys())))
    p.add_argument('-o',
                   action='store',
                   dest='output',
                   metavar='FILE',
                   required=True,
                   help='merged streamline file ({0})'.format(",".join(
                       FORMATS.keys())))
    # p.add_argument('-a', action='store', dest='anat',
    #                metavar='FILE', required=False,
    #                help='input anatomy file ({0})'.format(EXT_ANAT))
    p.add_argument('-f',
                   action='store_true',
                   dest='isForce',
                   help='force (pass extension check; overwrite output file)')
    p.add_argument('-v',
                   action='store_true',
                   dest='isVerbose',
                   help='produce verbose output')
    return p
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filename = args.input
    out_filename = args.output
    anat_filename = args.anat
    isForcing = args.isForce
    isVerbose = args.isVerbose

    if isVerbose:
        logging.basicConfig(level=logging.DEBUG)

    if not os.path.isfile(in_filename):
        parser.error('"{0}" must be an existing file!'.format(in_filename))

    if not tractconverter.is_supported(in_filename):
        parser.error('Input file must be one of {0}!'.format(",".join(
            FORMATS.keys())))

    if not tractconverter.is_supported(out_filename):
        parser.error('Output file must be one of {0}!'.format(",".join(
            FORMATS.keys())))

    if os.path.isfile(out_filename):
        if isForcing:
            if out_filename == in_filename:
                parser.error(
                    'Cannot use the same name for input and output files. Conversion would fail.'
                )
            else:
                logging.info('Overwriting "{0}".'.format(out_filename))
        else:
            parser.error('"{0}" already exist! Use -f to overwrite it.'.format(
                out_filename))

    inFormat = tractconverter.detect_format(in_filename)
    outFormat = tractconverter.detect_format(out_filename)

    #if inFormat == outFormat:
    #    parser.error('Input and output must be from different types!'.format(",".join(FORMATS.keys())))

    if anat_filename is not None:
        if not any(map(anat_filename.endswith, EXT_ANAT.split('|'))):
            if isForcing:
                logging.info('Reading "{0}" as a {1} file.'.format(
                    anat_filename.split("/")[-1], EXT_ANAT))
            else:
                parser.error(
                    'Anatomy file must be one of {1}!'.format(EXT_ANAT))

        if not os.path.isfile(anat_filename):
            parser.error(
                '"{0}" must be an existing file!'.format(anat_filename))

    #Convert input to output
    input = inFormat(in_filename, anat_filename)
    output = outFormat.create(out_filename, input.hdr, anat_filename)
    tractconverter.convert(input, output)
Beispiel #7
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filenames = args.input
    out_filename = args.output
    #anat_filename = args.anat
    isForcing = args.isForce
    isVerbose = args.isVerbose

    if isVerbose:
        logging.basicConfig(level=logging.DEBUG)

    for in_filename in in_filenames:
        if not os.path.isfile(in_filename):
            parser.error('"{0}" must be an existing file!'.format(in_filename))

        if not tractconverter.is_supported(in_filename):
            parser.error('Input file must be one of {0}!'.format(",".join(FORMATS.keys())))

    if not tractconverter.is_supported(out_filename):
        parser.error('Output file must be one of {0}!'.format(",".join(FORMATS.keys())))

    if os.path.isfile(out_filename):
        if isForcing:
            if any(in_name == out_filename for in_name in in_filenames):
                parser.error('Cannot output to a file which is also an input file ({0}).'.format(out_filename))
            else:
                logging.info('Overwriting "{0}".'.format(out_filename))
        else:
            parser.error('"{0}" already exist! Use -f to overwrite it.'.format(out_filename))

    inFormats = [tractconverter.detect_format(in_filename) for in_filename in in_filenames]
    outFormat = tractconverter.detect_format(out_filename)

    # if anat_filename is not None:
    #     if not any(map(anat_filename.endswith, EXT_ANAT.split('|'))):
    #         if isForcing:
    #             logging.info('Reading "{0}" as a {1} file.'.format(anat_filename.split("/")[-1], EXT_ANAT))
    #         else:
    #             parser.error('Anatomy file must be one of {1}!'.format(EXT_ANAT))

    #     if not os.path.isfile(anat_filename):
    #         parser.error('"{0}" must be an existing file!'.format(anat_filename))


    #TODO: Consider different anat, space.
    hdr = {}
    hdr[Header.DIMENSIONS] = (1,1,1)
    hdr[Header.ORIGIN] = (1,1,1)
    hdr[Header.NB_FIBERS] = 0  # The actual number of streamlines will be added later.

    #Merge inputs to output
    inputs = (in_format(in_filename) for in_filename, in_format in zip(in_filenames, inFormats))
    output = outFormat.create(out_filename, hdr)
    tractconverter.merge(inputs, output)
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filename = args.input
    out_filename = args.output
    anat_filename = args.anat
    isForcing = args.isForce
    isVerbose = args.isVerbose

    if isVerbose:
        logging.basicConfig(level=logging.DEBUG)

    if not os.path.isfile(in_filename):
        parser.error('"{0}" must be an existing file!'.format(in_filename))

    if not tractconverter.is_supported(in_filename):
        parser.error("Input file must be one of {0}!".format(",".join(FORMATS.keys())))

    if not tractconverter.is_supported(out_filename):
        parser.error("Output file must be one of {0}!".format(",".join(FORMATS.keys())))

    if os.path.isfile(out_filename):
        if isForcing:
            if out_filename == in_filename:
                parser.error("Cannot use the same name for input and output files. Conversion would fail.")
            else:
                logging.info('Overwriting "{0}".'.format(out_filename))
        else:
            parser.error('"{0}" already exist! Use -f to overwrite it.'.format(out_filename))

    inFormat = tractconverter.detect_format(in_filename)
    outFormat = tractconverter.detect_format(out_filename)

    # if inFormat == outFormat:
    #    parser.error('Input and output must be from different types!'.format(",".join(FORMATS.keys())))

    if anat_filename is not None:
        if not any(map(anat_filename.endswith, EXT_ANAT.split("|"))):
            if isForcing:
                logging.info('Reading "{0}" as a {1} file.'.format(anat_filename.split("/")[-1], EXT_ANAT))
            else:
                parser.error("Anatomy file must be one of {1}!".format(EXT_ANAT))

        if not os.path.isfile(anat_filename):
            parser.error('"{0}" must be an existing file!'.format(anat_filename))

    # Convert input to output
    input = inFormat(in_filename, anat_filename)
    output = outFormat.create(out_filename, input.hdr, anat_filename)
    tractconverter.convert(input, output)
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i', action='store', dest='input',
                   metavar='FILE', required=True,
                   help='input track file ({0})'.format(",".join(FORMATS.keys())))
    p.add_argument('-o', action='store', dest='output',
                   metavar='FILE', required=True,
                   help='output track file ({0})'.format(",".join(FORMATS.keys())))
    p.add_argument('-a', action='store', dest='anat',
                   metavar='FILE', required=False,
                   help='input anatomy file ({0})'.format(EXT_ANAT))
    p.add_argument('-f', action='store_true', dest='isForce',
                   help='force (pass extension check; overwrite output file)')
    p.add_argument('-v', action='store_true', dest='isVerbose',
                   help='produce verbose output')
    return p
Beispiel #10
0
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i',
                   action='store',
                   dest='input',
                   metavar='FILE',
                   required=True,
                   help='input track file ({0})'.format(",".join(
                       FORMATS.keys())))
    return p
Beispiel #11
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filename = args.input

    if not os.path.isfile(in_filename):
        parser.error('"{0}" must be an existing file!'.format(in_filename))

    if not tractconverter.is_supported(in_filename):
        parser.error('Input file must be one of {0}!'.format(",".join(FORMATS.keys())))

    inFormat = tractconverter.detect_format(in_filename)

    #Print info about the input file.
    print inFormat(in_filename, None)
Beispiel #12
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filename = args.input

    if not os.path.isfile(in_filename):
        parser.error('"{0}" must be an existing file!'.format(in_filename))

    if not tractconverter.is_supported(in_filename):
        parser.error('Input file must be one of {0}!'.format(",".join(
            FORMATS.keys())))

    inFormat = tractconverter.detect_format(in_filename)

    #Print info about the input file.
    print inFormat(in_filename, None)
Beispiel #13
0
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i', action='store', dest='input',
                   metavar='FILE', required=True,
                   help='input track file ({0})'.format(",".join(FORMATS.keys())))
    return p
Beispiel #14
0
import logging
import os
import tractconverter.info as info

import tractconverter
from tractconverter import FORMATS
from tractconverter import EXT_ANAT

from tractconverter.formats.header import Header

# Script description
DESCRIPTION = """
TractMerger {0}.
Merge streamlines files.
Supported formats are {1}
""".format(info.__version__, ",".join(FORMATS.keys()))


#####
# Script part
###
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i',
                   action='store',
                   dest='input',
                   nargs='+',
                   metavar='FILE',
                   required=True,
                   help='input streamlines file ({0})'.format(",".join(
                       FORMATS.keys())))
                output = FORMATS[v].create(outFile, input.hdr, p_anatFile)
                tractconverter.convert(input, output)
                logging.info(inFile)

        logging.info('{0} skipped (none track files)'.format(len(allFiles) - nbFiles))
        if not p_isRecursive:
            break

    logging.info("Conversion finished!")

#####
# Script part
###

#Script description
DESCRIPTION = 'Convert track files while walking down a path. ({0})'.format(",".join(FORMATS.keys()))


def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i', action='store', dest='input',
                   metavar='DIR', required=True,
                   help='path to walk')
    p.add_argument('-o', action='store', dest='output',
                   metavar='DIR',
                   help='output folder (if omitted, the walking folder is used)')
    p.add_argument('-a', action='store', dest='anat',
                   metavar='FILE', required=False,
                   help='anatomy file ({0})'.format(EXT_ANAT))

    #VTK
Beispiel #16
0
        logging.info(
            '{0} skipped (none track files)'.format(len(allFiles) - nbFiles))
        if not p_isRecursive:
            break

    logging.info("Conversion finished!")


#####
# Script part
###

#Script description
DESCRIPTION = 'Convert streamlines files while walking down a path. ({0})'.format(
    ",".join(FORMATS.keys()))


def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i',
                   action='store',
                   dest='input',
                   metavar='DIR',
                   required=True,
                   help='path to walk')
    p.add_argument(
        '-o',
        action='store',
        dest='output',
        metavar='DIR',
import argparse
import logging
import os
import tractconverter.info as info

import tractconverter
from tractconverter import FORMATS
from tractconverter import EXT_ANAT

# Script description
DESCRIPTION = """
TractConverter {0}.
Convert track files.
Supported formats are {1}
""".format(info.__version__,
           ",".join(FORMATS.keys()))


#####
# Script part
###
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument('-i', action='store', dest='input',
                   metavar='FILE', required=True,
                   help='input track file ({0})'.format(",".join(FORMATS.keys())))
    p.add_argument('-o', action='store', dest='output',
                   metavar='FILE', required=True,
                   help='output track file ({0})'.format(",".join(FORMATS.keys())))
    p.add_argument('-a', action='store', dest='anat',
                   metavar='FILE', required=False,
Beispiel #18
0
import copy
import tractconverter.info as info

import tractconverter
from tractconverter import FORMATS
from tractconverter import EXT_ANAT

from tractconverter.formats.header import Header

# Script description
DESCRIPTION = """
TractMerger {0}.
Merge streamlines files.
Supported formats are {1}
""".format(
    info.__version__, ",".join(FORMATS.keys())
)


#####
# Script part
###
def buildArgsParser():
    p = argparse.ArgumentParser(description=DESCRIPTION)
    p.add_argument(
        "-i",
        action="store",
        dest="input",
        nargs="+",
        metavar="FILE",
        required=True,
Beispiel #19
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filenames = glob.glob(os.path.join(args.input[0], "*.trk"))
    out_filename = args.output
    #anat_filename = args.anat
    isForcing = args.isForce
    isVerbose = args.isVerbose

    if isVerbose:
        logging.basicConfig(level=logging.DEBUG)

    for in_filename in in_filenames:
        if not os.path.isfile(in_filename):
            parser.error('"{0}" must be an existing file!'.format(in_filename))

        if not tractconverter.is_supported(in_filename):
            parser.error('Input file must be one of {0}!'.format(",".join(
                FORMATS.keys())))

    if not tractconverter.is_supported(out_filename):
        parser.error('Output file must be one of {0}!'.format(",".join(
            FORMATS.keys())))

    if os.path.isfile(out_filename):
        if isForcing:
            if any(in_name == out_filename for in_name in in_filenames):
                parser.error(
                    'Cannot output to a file which is also an input file ({0}).'
                    .format(out_filename))
            else:
                logging.info('Overwriting "{0}".'.format(out_filename))
        else:
            parser.error('"{0}" already exist! Use -f to overwrite it.'.format(
                out_filename))

    inFormats = [
        tractconverter.detect_format(in_filename)
        for in_filename in in_filenames
    ]
    outFormat = tractconverter.detect_format(out_filename)

    # if anat_filename is not None:
    #     if not any(map(anat_filename.endswith, EXT_ANAT.split('|'))):
    #         if isForcing:
    #             logging.info('Reading "{0}" as a {1} file.'.format(anat_filename.split("/")[-1], EXT_ANAT))
    #         else:
    #             parser.error('Anatomy file must be one of {1}!'.format(EXT_ANAT))

    #     if not os.path.isfile(anat_filename):
    #         parser.error('"{0}" must be an existing file!'.format(anat_filename))

    #TODO: Consider streamlines files with different anat/space ?

    # Use information from the first streamlines file to create the header.
    hdr = copy.deepcopy(inFormats[0](in_filenames[0]).hdr)
    hdr[Header.
        NB_FIBERS] = 0  # The actual number of streamlines will be added later (as we add the streamlines).

    #Merge inputs to output
    inputs = (in_format(in_filename)
              for in_filename, in_format in zip(in_filenames, inFormats))
    output = outFormat.create(out_filename, hdr)
    tractconverter.merge(inputs, output)