Exemplo n.º 1
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    reference = cli_tools.glob_args(options.reference)

    if (len(args) < 2) or (len(args) + len(reference) < 3):
        raise ValueError(
            'A data column and at least two datasets (at least one of which must not be a reference) must be provided!'
        )

    data_column, datasets = args[0], args[1:]

    # if the data column is convertible to an integer, do so and
    # then convert from 1-indexed to 0-indexed
    try:
        data_column = int(data_column)
        data_column -= 1
    except:
        pass

    if options.show_progress:
        datasets = terminal_tools.progress_list(datasets, "Reading input data")
    names, pops = read_files(datasets, data_column)
    if options.show_progress and len(reference) > 0:
        reference = terminal_tools.progress_list(reference,
                                                 "Reading reference data")
    ref_names, ref_pops = read_files(reference, data_column)

    if options.show_progress:
        pb = terminal_tools.IndeterminantProgressBar("Resampling data")
    if len(ref_pops) > 0:
        pvals = ks_resample.compare_to_ref(pops, ref_pops, options.n)
        if len(ref_names) > 1:
            ref_name = 'reference (%s)' % (', '.join(ref_names))
        else:
            ref_name = ref_names[0]
        rows = [[None, 'difference from %s' % ref_name]]
        for name, p in zip(names, pvals):
            rows.append([name, format_pval(p, options.n)])
    else:  # no ref pops
        pvals = ks_resample.symmetric_comparison(pops, options.n)
        rows = [[None] + names]
        for i, (name, p_row) in enumerate(zip(names, pvals)):
            rows.append([name] + [format_pval(p, options.n) for p in p_row])
            rows[-1][i +
                     1] = None  # empty out the self-self comparison diagonal

    datafile.write_data_file(rows, options.output_file)
Exemplo n.º 2
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) == 0:
        raise ValueError('Some contour files must be specified!')
    filenames = [path.path(arg) for arg in args]
    contours = simple_interface.load_contours(
        filenames, show_progress=options.show_progress)
    if options.reference is not None:
        reference = contour_class.from_file(options.reference)
        contours = simple_interface.align_contours_to(
            contours,
            reference,
            align_steps=options.alignment_steps,
            allow_reflection=options.allow_reflection,
            show_progress=options.show_progress)
    else:
        contours = simple_interface.align_contours(
            contours,
            options.alignment_steps,
            options.allow_reflection,
            max_iters=options.max_iterations,
            show_progress=options.show_progress)
    destination = path.path(options.destination)
    if not destination.exists():
        destination.makedirs()
    # note that with path objects, the '/' operator means 'join path components.'
    names = [destination / filename.name for filename in filenames]
    simple_interface.save_contours(contours, names, options.show_progress)
Exemplo n.º 3
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) == 0:
        raise ValueError('Some contour and image files must be specified!')
    matches = match_files.match_contours_and_images(args,
                                                    options.match_by_name,
                                                    options.show_progress)
    contours, image_names, unmatched_contours, unmatched_image_names = matches
    destination = path.path(options.destination)
    if not destination.exists():
        destination.makedirs()
    if options.file_type is not None:
        new_names = [
            destination / contour.simple_name() + '.' + options.file_type
            for contour in contours
        ]
    else:
        new_names = [
            destination / contour.simple_name() + image.ext
            for image, contour in zip(image_names, contours)
        ]
    simple_interface.reorient_images(contours,
                                     image_names,
                                     new_names,
                                     pad_factor=options.pad_factor,
                                     mask=options.mask_background,
                                     show_progress=options.show_progress)
Exemplo n.º 4
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) == 0:
        raise ValueError('Some contour files must be specified!')
    contours = simple_interface.load_contours(
        args, show_progress=options.show_progress)
    shape_model, header, rows, norm_header, norm_rows = simple_interface.make_shape_model(
        contours, options.variance_explained)
    shape_model.to_file(options.output_prefix + '.contour')
    if options.write_data:
        datafile.write_data_file([header] + rows,
                                 options.output_prefix + '-positions.csv')
        datafile.write_data_file([norm_header] + norm_rows,
                                 options.output_prefix +
                                 '-normalized-positions.csv')
Exemplo n.º 5
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) == 0:
        raise ValueError('Some contour files must be specified!')
    contours = simple_interface.load_contours(
        args, show_progress=options.show_progress)
    if options.grid:
        contours = simple_interface.grid_contours(contours)


# Transform (begin, end) from a one-indexed, inclusive range (normal for humans)
# to a zero-indexed inclusive range.
    if options.begin is not None:
        options.begin -= 1
    if options.end is not None:
        options.end -= 1
    if options.color_by == 'none':
        gradient_factory = None
    else:
        gradient_factory = plot_tools.default_gradient
    if options.color_by == 'points' or options.begin or options.end or options.label_points:
        # do point ordering plot
        colorbar = options.color_by == 'points'
        color_by_point = options.color_by == 'points'
        # use a color bar if we're doing a gradient
        plot_tools.point_order_plot(contours,
                                    options.output_file,
                                    plot_title=options.title,
                                    label_points=options.label_points,
                                    colorbar=colorbar,
                                    begin=options.begin,
                                    end=options.end,
                                    gradient_factory=gradient_factory,
                                    color_by_point=color_by_point,
                                    scale=options.scale,
                                    show_progress=options.show_progress)
    else:
        plot_tools.contour_plot(contours,
                                options.output_file,
                                plot_title=options.title,
                                gradient_factory=gradient_factory,
                                scale=options.scale,
                                show_progress=options.show_progress)
Exemplo n.º 6
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) != 1:
        raise ValueError(
            'Only a single PCA contour can be plotted at a time -- please specify only one model file on the command line.'
        )
    pca_contour = contour_class.from_file(args[0], contour_class.PCAContour)
    if not options.output_file:
        output_file = pca_contour.simple_name() + '.svg'
    else:
        output_file = options.output_file
    plot_tools.pca_modes_plot(pca_contour,
                              output_file,
                              modes=options.modes,
                              positions=options.positions,
                              scale=options.scale)
Exemplo n.º 7
0
def main(name, arguments):
  parser.prog = name
  options, args = parser.parse_args(arguments)
  args = cli_tools.glob_args(args)
  if len(args) == 0:
    raise ValueError('Some contour files must be specified!')
  filenames = [path.path(arg) for arg in args]
  contours = simple_interface.load_contours(filenames, show_progress = options.show_progress)
  if options.endpoints is None:
    endpoints = options.endpoint_method
  else:
    endpoints = options.endpoints
  contours = simple_interface.find_centerlines(contours, options.axis_points, endpoints, options.show_progress)
  destination = path.path(options.destination)
  if not destination.exists():
    destination.makedirs()
  # note that with path objects, the '/' operator means 'join path components.'
  names = [destination / filename.name for filename in filenames]
  simple_interface.save_contours(contours, names, options.show_progress)
Exemplo n.º 8
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) == 0:
        raise ValueError('Some contour files must be specified!')
    filenames = [path.path(arg) for arg in args]
    contours = simple_interface.load_contours(
        filenames, show_progress=options.show_progress)
    if options.first_point is not None:
        options.first_point -= 1
    if options.scale is not None or options.rotate is not None or options.units is not None or options.first_point is not None:
        in_radians = False
        if options.units is not None and options.units.lower() in ('um',
                                                                   'micron',
                                                                   'microns'):
            options.units = u'\N{MICRO SIGN}m'
        contours = simple_interface.transform_contours(
            contours,
            options.scale,
            options.rotate,
            in_radians,
            options.units,
            options.first_point,
            options.show_progress,
            title='Modifying Contours')
    if options.weights is not None:
        contours = simple_interface.reweight_landmarks(contours,
                                                       options.weights,
                                                       options.show_progress)

    destination = path.path(options.destination)
    if not destination.exists():
        destination.makedirs()
    # note that with path objects, the '/' operator means 'join path components.'
    names = [destination / filename.name for filename in filenames]
    simple_interface.save_contours(contours, names, options.show_progress)
Exemplo n.º 9
0
def main(name, arguments):
  parser.prog = name
  options, args = parser.parse_args(arguments)
  args = cli_tools.glob_args(args)
  if len(args) == 0:
    raise ValueError('Some image files must be specified!')
  filenames = [path.path(arg) for arg in args]
  contours_groups = simple_interface.extract_contours(filenames, options.contour_value, 
    options.min_area, options.max_area, options.show_progress)
  contours = []
  names = []
  destination = path.path(options.destination)
  if not destination.exists():
    destination.makedirs()
  for contour_group, image_name in zip(contours_groups, filenames):
    num_contours = len(contour_group)
    if num_contours == 1:
      contours.append(contour_group[0])
      # note that with path objects, the '/' operator means 'join path components.'
      names.append(destination / image_name.namebase + '.contour')
      contour_group[0]._filename = image_name.namebase
    else:
      width = len(str(num_contours))
      for i, contour in enumerate(contour_group):
        contours.append(contour)
        names.append(destination / image_name.namebase + '-%.*d.contour'%(width, i+1))
        contour._filename = image_name.namebase + '-%.*d'%(width, i+1)
  if options.scale is not None:
    # if not rescaling, contours are already denominated in pixels, so do nothing.
    units = options.units
    if units.lower() in ('um', 'micron', 'microns'):
      units = u'\N{MICRO SIGN}m'
    contours = simple_interface.transform_contours(contours, scale_factor=options.scale, 
      units=units, show_progress=options.show_progress, title='Rescaling Contours')
  if options.resample:
    contours = simple_interface.resample_contours(contours, options.resample_points, options.smoothing_factor, options.show_progress)
  simple_interface.save_contours(contours, names, options.show_progress)
Exemplo n.º 10
0
def main(name, arguments):
    parser.prog = name
    options, args = parser.parse_args(arguments)
    args = cli_tools.glob_args(args)
    if len(args) == 0:
        raise ValueError('Some contour and image files must be specified!')
    intensity_ranges = []
    for r in options.intensity_ranges:
        try:
            low, high = r
        except:
            low = high = r
        intensity_ranges.append([low, high])
    if options.weights is None or len(options.weights) == 0:
        options.weights = [0.5]
    elif len(options.weights) != 1 and len(
            options.weights) != len(intensity_ranges):
        raise optparse.OptionValueError(
            "Either one or %d weights are required; %d sepcified." %
            (len(intensity_ranges), len(options.weights)))
    elif sum(options.weights) > 1:
        raise optparse.OptionValueError("The sum of the weights must be <= 1.")

    matches = match_files.match_contours_and_images(args,
                                                    options.match_by_name,
                                                    options.show_progress)
    contours, image_names, unmatched_contours, unmatched_image_names = matches
    filenames = [path.path(contour._filename) for contour in contours]
    contours = simple_interface.add_image_landmarks_to_contours(
        contours, image_names, intensity_ranges, options.weights,
        options.image_type, options.show_progress)
    destination = path.path(options.destination)
    if not destination.exists():
        destination.makedirs()
    # note that with path objects, the '/' operator means 'join path components.'
    names = [destination / filename.name for filename in filenames]
    simple_interface.save_contours(contours, names, options.show_progress)
Exemplo n.º 11
0
def main(name, arguments):
  parser.prog = name
  options, args = parser.parse_args(arguments)
  data_files = []
  new_args = []
  for arg in args:
    if type(arg) == list:
      data_files.append(arg)
    else:
      new_args.append(arg)
  args = cli_tools.glob_args(new_args)
  if len(args) == 0:
    raise ValueError('Some data files (and optionally contour files, for 2D plots) must be specified!')
  
  # if the x, y, and name columns are convertible to integers, do so and 
  # then convert them from 1-indexed to 0-indexed
  try:
    options.name_column = int(options.name_column)
    options.name_column -= 1
  except:
    pass
  try:
    options.x_column = int(options.x_column)
    options.x_column -= 1
  except:
    pass
  try:
    options.y_column = int(options.y_column)
    options.y_column -= 1
  except:
    pass
  contours = {}
  if options.show_progress:
    args = terminal_tools.progress_list(args, "Reading input data and contours")
  for arg in args:
    contour = None
    try:
      contour = contour_class.from_file(arg)
    except:
      data_files.append(arg)
    if contour is not None:
      contours[contour.simple_name()] = contour
  if len(data_files) == 0:
    raise ValueError('No data files were specified!')
  headers, data_ranges, data_names, data_files, row_ranges = get_data(data_files)
  if not options.x_title:
      if isinstance(options.x_column, int):
        try: options.x_title = headers[0][options.x_column]
        except: pass
      else:
        options.x_title = options.x_column
  if options.y_column is not None:
    # make scatterplot
    if not options.y_title:
      if isinstance(options.y_column, int):
        try: options.y_title = headers[0][options.y_column]
        except: pass
      else:
        options.y_title = options.y_column
    contour_groups = get_contour_groups(data_ranges, contours, data_files, row_ranges, options.name_column, options.x_column, options.y_column)
    if numpy.alltrue([len(cg)==0 for cg in contour_groups]):
      raise RuntimeError('No contours found for data rows specified (perhaps the names mismatch or there were no data rows?).')
    plot_tools.contour_scatterplot(contour_groups, options.output_file, options.scale,
      (options.x_title, options.y_title), options.title, names=data_names, 
      axes_at_origin=options.axes_at_origin, fix_xrange=(options.x_min, options.x_max),
      fix_yrange=(options.y_min, options.y_max), show_contour_axes=options.contour_axes, 
      show_progress=options.show_progress)
  else:
    data_groups = [[row[options.x_column] for row in data_range] for data_range in data_ranges]
    plot_tools.distribution_plot(data_groups, options.output_file, options.x_title,
      options.title, names=data_names, axes_at_origin=options.axes_at_origin, 
      fix_xrange=(options.x_min, options.x_max))