Example #1
0
def example_load_letter():
    """
    """
    logg = logging.getLogger(f"c.{__name__}.example_load_letter")
    logg.debug(f"Starting example_load_letter")

    letter_dir = Path(__file__).resolve().parent / "letters"
    logg.debug(f"letter_dir: {letter_dir}")
    letter_file_path = letter_dir / "l_lower.tsv"

    scale = 10
    spline_sequence = utils.load_points(letter_file_path, scale)
    print(spline_sequence)

    xlim = 0 * scale, 124 * scale
    ylim = -250 * scale, 0 * scale
    #  draw_long_spline(spline_sequence, xlim, ylim, plot_vectors=True)
    draw_long_spline(spline_sequence, xlim, ylim, plot_vectors=False)
Example #2
0
def generate_figure(idx):
    scan, origin, spacing = load_itk("./training/dataset0%d/image0%d.mhd" %
                                     (idx, idx))

    fig = plt.figure(figsize=(15, 8))
    ax = fig.add_subplot(111, projection='3d')
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')

    for image_id in range(4):
        path = "./training/dataset0%d/vessel%s/reference.txt" % (idx,
                                                                 str(image_id))
        print(path)
        points = load_points(path)
        X, Y, Z = points[:, 0], points[:, 1], points[:, 2]
        X, Y, Z = X / spacing[1], Y / spacing[1], Z / spacing[0]
        ax.scatter(X, Y, Z, s=0.3, c=np.arange(len(points)))

    plt.axis("square")
    plt.savefig("./visualizations/" + str(idx) + ".png")
Example #3
0
def get_batch(dataset_idx, vessel_idx, batch_size=32):
    """
    :param dataset_idx: dataset to use
    :param vessel_idx: vessel to use
    :param batch_size: size of the batch gee
    :return: a batch of data from the given dataset and vessel
    """

    print("-", end="")
    reference_points = load_reference_points("./preprocessing/reference_directions.txt")
    probs, radii, directions, input_data = [], [], [], []

    points_path = join(training_dir, "dataset0%d/vessel%s/reference.txt" % (dataset_idx, str(vessel_idx)))
    points = load_points(points_path)

    image, _, _ = load_itk(join(training_dir, "dataset0%d/image0%d.mhd" % (dataset_idx, dataset_idx)))
    idxs = np.random.randint(300, len(points) - 300, batch_size)
    for idx in idxs:
        radius, direction = create_sample(idx, points, reference_points)

        point = world_to_voxel(points[idx, :3])
        patch = segment_image(image, point).copy()

        if patch.shape == (19, 19, 19):
            input_data.append(patch)
            probs.append(1.)
            radii.append(radius)
            directions.append(direction)

    input_data = np.asarray(input_data).reshape(-1, 19, 19, 19, 1)
    radii = np.asarray(radii).reshape(-1, 1)
    directions = np.asarray(directions).reshape(-1, 500)
    probs = np.asarray(probs).reshape(-1, 1)
    # print(input_data.shape, radii.shape, directions.shape, probs.shape)

    return input_data, [probs, radii, directions]
Example #4
0
    # save this intermediate step
    penrose_segments = penrose.segments
    LOGN("\tsegments", len(penrose_segments))
    with open("d%i_penrose.segments" % depth, "w") as fd:
        utils.write_segments(penrose_segments, fd)

########################################################################
# TSP
########################################################################

trajs = []

if ask_for.tour != [None]:
    for tour in ask_for.tour:
        with open(tour) as fd:
            trajs.append(utils.load_points(fd))

if ask_for.notsp:
    if ask_for.tour == [None] or not ask_for.pheromones:
        LOGN(
            "If you do not want to solve the TSP, you must provide a solution tour (--tour) and a pheromones matrix (--pheromones)"
        )
        sys.exit(error_codes["NO-TSP"])

    if ask_for.pheromones:
        with open(ask_for.pheromones) as fd:
            phero = utils.load_matrix(fd)

else:
    LOGN("Solve the TSP with an Ant Colony Algorithm")
from preprocessing import *
from utils import load_points

if __name__ == "__main__":
    # change_voxel_spacing()
    dataset_idx, image_idx = 0, 0
    path = "./training/dataset0%d/vessel%s/reference.txt" % (dataset_idx,
                                                             str(image_idx))
    points = load_points(path)
    reference_points = load_reference_points(
        "./preprocessing/reference_directions.txt")

    print(points.shape, reference_points.shape)

    print(points[0, :3], points[20, :3])
    cat = find_direction_cat(points[0, :3], points[20, :3], reference_points)
    print(reference_points[cat])
    # X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    # X = np.random.rand(20,3)
    # nbrs = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(X)
    # distances, indices = nbrs.kneighbors(X)

    # Load points
    if not exists(args.pcl_path):
        exit(args.pcl_path + " could not be found")

    ext = splitext(args.pcl_path)[-1].lower()
    if ext is None or ext not in [".pcd", ".txt", ".xyz"]:
        exit("Point-cloud file has wrong extension")

    # http://www.open3d.org/docs/release/tutorial/Basic/file_io.html
    pcd = load_points(args.pcl_path, "pcd" if ext[1:] == "pcd" else "xyz")
    # o3d.visualization.draw_geometries([pcd])
    print(len(pcd.points), "points found")
    # pcd_array = np.asarray(pcd.points)

    # search_knn_vector_3d - returns a list of indices of the k nearest neighbors of the anchor point
    # [_, idx, _] = pcd_tree.search_knn_vector_3d(pcd.points[0], args.nn)
    # print(np.asarray(pcd.points)[idx[1:], :])

    # search_radius_vector_3d - all points with distances to the anchor point less than a given radius
    # [k, idx, _] = pcd_tree.search_radius_vector_3d(pcd.points[0], args.r)
    # print(np.asarray(pcd.points)[idx[1:], :])

    # search_hybrid_vector_3d - at most k nearest neighbors that have distances to the anchor point less than a given radius
    # Combines criteria of KNN search and RNN search
    # [_, idx, _] = pcd_tree.search_hybrid_vector_3d(pcd.points[0], args.r, args.nn)
Example #7
0
    LOGN( "\tsegments",len(penrose_segments) )
    with open("d%i_penrose.segments" % depth, "w") as fd:
        utils.write_segments( penrose_segments, fd )



########################################################################
# TSP
########################################################################

trajs = []

if ask_for.tour != [None]:
    for tour in ask_for.tour:
        with open(tour) as fd:
            trajs.append( utils.load_points(fd) )

if ask_for.notsp:
    if ask_for.tour == [None] or not ask_for.pheromones:
        LOGN( "If you do not want to solve the TSP, you must provide a solution tour (--tour) and a pheromones matrix (--pheromones)" )
        sys.exit(error_codes["NO-TSP"])

    if ask_for.pheromones:
        with open(ask_for.pheromones) as fd:
            phero = utils.load_matrix(fd)

else:
    LOGN( "Solve the TSP with an Ant Colony Algorithm" )

    LOGN( "\tConvert the segment list into an adjacency list graph" )
    G = graph.graph_of( penrose_segments )