Esempio n. 1
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)
Esempio n. 2
0
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)
Esempio n. 3
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)
Esempio n. 4
0
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)
Esempio n. 5
0
def main():
    parser = _buildArgsParser()
    args = parser.parse_args()

    if not os.path.isfile(args.tracts):
        parser.error('"{0}" must be a file!'.format(args.tracts))

    if not tc.is_supported(args.tracts):
        parser.error('Format of "{0}" not supported.'.format(args.tracts))

    if not os.path.isfile(args.roi_anat):
        parser.error('"{0}" must be a file!'.format(args.roi_anat))

    roi_idx_range = [item for sublist in args.roi_ids for item in sublist]

    counts = count(args.tracts, args.roi_anat, roi_idx_range)

    formatted_output = _format_output(counts)

    if args.out:
        f = open(args.out, 'w')
        f.write(formatted_output)
        f.close()
    else:
        print(formatted_output)
Esempio n. 6
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    np.random.seed(int(time.time()))
    in_filename = args.input
    out_filename = args.output
    isForcing = args.isForce
    isVerbose = args.isVerbose

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

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

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

    if not inFormat == outFormat:
        parser.error('Input and output must be of the same types!'.format(
            ",".join(tractconverter.FORMATS.keys())))

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

    tract = inFormat(in_filename)
    streamlines = [i for i in tract]

    if args.non_fixed_seed:
        rng = np.random.RandomState()
    else:
        rng = None

    results = subsample_streamlines(streamlines, args.minL, args.maxL, args.n,
                                    args.npts, args.arclength, rng)

    logging.info('"{0}" contains {1} streamlines.'.format(
        out_filename, len(results)))

    hdr = tract.hdr
    hdr[tractconverter.formats.header.Header.NB_FIBERS] = len(results)

    output = outFormat.create(out_filename, hdr)
    output += results
Esempio n. 7
0
def check_tracts_support(parser, path, tp):
    # Check left in place to make sure that this is checked
    # even if the next check (TCK or TRK) is removed at one point.
    if not tc.is_supported(path):
        parser.error('Format of "{}" not supported.'.format(path))

    if not scilpy_supports(path):
        parser.error(
            'The format of the input tracts is not currently supported by '
            'this script, because the TC space is undefined.\nPlease see '
            '[email protected] for solutions.')

    if tp is None and is_trk(path):
        parser.error(
            'When providing a trk file, please also set the --tp argument.')
Esempio n. 8
0
def main():
    parser = _build_args_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, [args.tracts, args.ref_anat])
    assert_outputs_exists(parser, args, [args.out])
    check_tracts_support(parser, args.tracts, False)

    if not tc.is_supported(args.out):
        parser.error('Format of "{0}" not supported.'.format(args.out))

    if not args.x and not args.y and not args.z:
        parser.error('No flipping axis specified.')

    flip_streamlines(args.tracts, args.ref_anat, args.out, args.x, args.y,
                     args.z, args.mode)
Esempio n. 9
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)
Esempio n. 10
0
def main():
    parser = _buildArgsParser()
    args = parser.parse_args()

    if not os.path.isfile(args.tracts):
        parser.error('"{0}" must be a file!'.format(args.tracts))

    if not tc.is_supported(args.tracts):
        parser.error('Format of "{0}" not supported.'.format(args.tracts))

    tract_count = get_tract_count(args.tracts)

    if args.out:
        with open(args.out) as f:
            f.write(str(tract_count))
    else:
        print(tract_count)
Esempio n. 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)
Esempio n. 12
0
def main():
    parser = buildArgsParser()
    args = parser.parse_args()

    in_filename = args.input
    out_filename_loops = args.output_loops
    out_filename_clean = args.output_clean
    streamlines_c = []
    loops = []

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

    in_format = tc.detect_format(in_filename)
    out_format_clean = tc.detect_format(out_filename_clean)

    if not in_format == out_format_clean:
        parser.error('Input and output must be of the same types!'.format(
            ",".join(tc.FORMATS.keys())))

    if args.output_loops:
        out_format_loops = tc.detect_format(out_filename_loops)
        if not in_format == out_format_loops:
            parser.error(
                'Input and output loops must be of the same types!'.format(
                    ",".join(tc.FORMATS.keys())))
        if os.path.isfile(args.output_loops):
            if args.isForce:
                logging.info('Overwriting "{0}".'.format(out_filename_loops))
            else:
                parser.error(
                    '"{0}" already exist! Use -f to overwrite it.'.format(
                        out_filename_loops))

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

    if args.threshold <= 0:
        parser.error('"{0}"'.format(args.threshold) + 'must be greater than 0')

    if args.angle <= 0:
        parser.error('"{0}"'.format(args.angle) + 'must be greater than 0')

    tract = in_format(in_filename)
    streamlines = [i for i in tract]

    if len(streamlines) > 1:
        streamlines_c, loops = remove_loops_and_sharp_turns(
            streamlines, args.QB, args.angle, args.threshold)
    else:
        parser.error('Zero or one streamline in ' + '{0}'.format(in_filename) +
                     '. The file must have more than one streamline.')

    hdr = tract.hdr.copy()
    nb_points_init = hdr[tc.formats.header.Header.NB_POINTS]
    nb_points_clean = 0

    if len(streamlines_c) > 0:
        hdr[tc.formats.header.Header.NB_FIBERS] = len(streamlines_c)
        if in_format is tc.formats.vtk.VTK:
            for s in streamlines_c:
                nb_points_clean += len(s)
            hdr[tc.formats.header.Header.NB_POINTS] = nb_points_clean
        output_clean = out_format_clean.create(out_filename_clean, hdr)
        output_clean += streamlines_c
        output_clean.close()
    else:
        logging.warning("No clean streamlines in {0}".format(args.input))

    if len(loops) == 0:
        logging.warning("No loops in {0}".format(args.input))

    if args.output_loops and len(loops) > 0:
        hdr[tc.formats.header.Header.NB_FIBERS] = len(loops)
        if in_format is tc.formats.vtk.VTK:
            hdr[tc.formats.header.Header.
                NB_POINTS] = nb_points_init - nb_points_clean
        output_loops = out_format_loops.create(out_filename_loops, hdr)
        output_loops += loops
        output_loops.close()
Esempio n. 13
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)