Example #1
0
def plot_grid(images, tsne, tx, ty):
    nx = int(math.ceil(math.sqrt(len(images))))
    ny = nx

    grid_assignment = rasterfairy.transformPointCloud2D(tsne, target=(nx, ny))

    tile_width = 144
    tile_height = 112

    full_width = tile_width * nx
    full_height = tile_height * ny
    aspect_ratio = float(tile_width) / tile_height

    grid_image = PIL.Image.new('RGB', (full_width, full_height))

    for img, grid_pos in tqdm.tqdm(zip(images, grid_assignment[0].tolist())):
        idx_x, idx_y = grid_pos
        x, y = tile_width * idx_x, tile_height * idx_y
        tile = imageutils.load_image(img)
        tile_ar = float(
            tile.width
        ) / tile.height  # center-crop the tile to match aspect_ratio
        if (tile_ar > aspect_ratio):
            margin = 0.5 * (tile.width - aspect_ratio * tile.height)
            tile = tile.crop(
                (margin, 0, margin + aspect_ratio * tile.height, tile.height))
        else:
            margin = 0.5 * (tile.height - float(tile.width) / aspect_ratio)
            tile = tile.crop((0, margin, tile.width,
                              margin + float(tile.width) / aspect_ratio))
        tile = tile.resize((tile_width, tile_height), PIL.Image.ANTIALIAS)
        grid_image.paste(tile, (int(x), int(y)))

    return grid_image
Example #2
0
def image_grid2(images, Z, ny=25, nx=40, out_name="grid2.png"):
    import rasterfairy
    from PIL import Image

    # assign to grid
    grid_assignment = rasterfairy.transformPointCloud2D(Z, target=(nx, ny))
    print(grid_assignment)

    tile_width = 32
    tile_height = 32

    full_width = tile_width * nx
    full_height = tile_height * ny
    # aspect_ratio = float(tile_width) / tile_height

    grid_image = Image.new("RGB", (full_width, full_height))

    for img, grid_pos in zip(images, grid_assignment[0]):
        idx_x, idx_y = grid_pos
        x, y = tile_width * idx_x, tile_height * idx_y
        print(idx_x, idx_y, x, y)

        tile = Image.fromarray(img.reshape((32, 32, 3)).astype("uint8"), "RGB")
        grid_image.paste(tile, box=(int(x), int(y), int(x) + 32, int(y) + 32))

    grid_image.save(out_name)
Example #3
0
def generate_mosaic(embeddings,
                    images,
                    mosaic_width,
                    mosaic_height,
                    tile_width=72,
                    tile_height=56,
                    title="Doppler Mosaic",
                    title_rbg=(229, 229, 229),
                    save_as_file=False,
                    return_image=True,
                    verbose=False):
    '''
    Transforms 2-dimensional embeddings to a grid. 
    Plots the images for each embedding in the corresponding grid (mosaic).
    Includes arguments for the dimensions of each tile and the the mosaic.
    '''
    # assign to grid
    grid_assignment = transformPointCloud2D(embeddings,
                                            target=(mosaic_width,
                                                    mosaic_height))
    full_width = tile_width * mosaic_width
    full_height = tile_height * (mosaic_height + 2)
    aspect_ratio = float(tile_width) / tile_height

    # create an empty image for the mosaic
    mosaic = Image.new('RGB', (full_width, full_height))

    # iterate through each image and where it is possed to live.
    for f_img, (idx_x, idx_y) in tqdm(zip(images, grid_assignment[0]),
                                      disable=not verbose):
        # Find exactly where the image will be
        x, y = tile_width * idx_x, tile_height * idx_y

        # read the image, center crop the image and add it to the mosaic
        try:
            img = Image.open(f_img).convert('RGBA')
            tile = resize_image(img, tile_width, tile_height, aspect_ratio)
            mosaic.paste(tile, (int(x), int(y)))
        except Exception as e:
            print(f"Failed to add image {f_img} see error:\n{e}")

    # write an annotation
    fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf',
                             int(tile_height * 1.2))
    draw = ImageDraw.Draw(mosaic)
    draw.text((4, (tile_height * (mosaic_height)) + 10),
              title,
              title_rbg,
              font=fnt)

    if save_as_file and not os.path.exists(save_as_file):
        try:
            mosaic.save(save_as_file)
        except Exception as e:
            print(
                f'Saving the mosaic to {save_as_file} failed, see error:\n{e}')

    if return_image:
        return mosaic
Example #4
0
def tsne_to_grid(data, output):
    # https://github.com/Quasimondo/RasterFairy/blob/master/examples/Raster%20Fairy%20Demo%201.ipynb
    # https://github.com/bmcfee/RasterFairy (vpython3)

    #    from sklearn.decomposition import PCA
    import rasterfairy
    import math

    data = np.array(data)

    #    pca = PCA(n_components=500, whiten=True)
    n_pts = data.shape[0]
    #    data_pca = pca.fit_transform(data)
    data_pca, U, V = pca(data, pc_count=500)

    data_pca = list(data_pca)
    sqrt_npts = math.ceil(math.sqrt(n_pts))
    n_diff = sqrt_npts**2 - n_pts
    data_pca += [data_pca[-1]] * n_diff
    data_pca = np.array(data_pca)
    list_file += [list_file[-1]] * n_diff

    tsne = TSNE_multi(n_components=2, perplexity=50, n_jobs=8)
    data_tsne = tsne.fit_transform(data_pca.astype(np.float64))

    arrangements = rasterfairy.getRectArrangements(data_tsne.shape[0])

    img = cv2.imread(list_file[0])
    ratio = img.shape[0] / img.shape[1]
    r = 0.1
    dim = (int(img.shape[1] * r), int(img.shape[0] * r))
    img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    dim_x = img.shape[0]
    dim_y = img.shape[1]

    grid_xy, (width, height) = rasterfairy.transformPointCloud2D(
        data_tsne, target=arrangements[0])

    img_big = np.zeros((dim_x * width, dim_y * height, 3))

    for i, file_img in enumerate(list_file):
        xy = grid_xy[i, :]
        pos_x = int(xy[0] * dim_x)
        pos_y = int(xy[1] * dim_y)
        img = cv2.imread(file_img)
        r = 0.1
        dim = (int(img.shape[1] * r), int(img.shape[0] * r))
        img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
        img_big[pos_x:pos_x + dim_x, pos_y:pos_y + dim_y, :] = img
    cv2.imwrite(output, img_big)
Example #5
0
def get_rasterfairy_projection(**kwargs):
    '''Get the x, y position of images passed through a rasterfairy projection'''
    print(' * creating rasterfairy layout')
    out_path = get_path('layouts', 'rasterfairy', **kwargs)
    if os.path.exists(out_path) and kwargs['use_cache']: return out_path
    umap = np.array(read_json(kwargs['umap'], **kwargs))
    umap = (umap + 1) / 2  # scale 0:1
    umap = coonswarp.rectifyCloud(
        umap,  # stretch the distribution
        perimeterSubdivisionSteps=4,
        autoPerimeterOffset=False,
        paddingScale=1.05)
    pos = rasterfairy.transformPointCloud2D(umap)[0]
    return write_layout(out_path, pos, **kwargs)
def generate_mosaic(embeddings, images, titles, origins, urls, mosaic_width=1000, mosaic_height=1000, tile_width=150, tile_height=100,
                    title_rbg=(255, 255, 255), save_as_file='mosaic.png',
                    fb_counts=None,partisanship=None,verbose=True,
                    return_image=True, title="Doppler Mosaic" ):
    """
    Transforms 2-dimensional embeddings to a grid. 
    Plots the images for each embedding in the corresponding grid (mosaic).
    Includes arguments for the dimensions of each tile and the the mosaic.
    """
    # assign to grid
    grid_assignment = transformPointCloud2D(embeddings,
                                            target=(mosaic_width,
                                                    mosaic_height))
    full_width = tile_width * mosaic_width
    full_height = tile_height * (mosaic_height + 2)
    aspect_ratio = float(tile_width) / tile_height
    #print(grid_assignment)
    # create an empty image for the mosaic
    mosaic = Image.new('RGB', (full_width, full_height))

    # iterate through each image and where it is possed to live.
    for f_img, (idx_x, idx_y) in tqdm(zip(images, grid_assignment[0]),
                                      disable = False):
        # Find exactly where the image will be
        x, y = tile_width * idx_x, tile_height * idx_y
        
        # read the image, center crop the image and add it to the mosaic
        try:
            img = Image.open(f_img).convert('RGBA')
            tile = resize_image(img, tile_width, tile_height, aspect_ratio)
            mosaic.paste(tile, (int(x), int(y)))
        except Exception as e:
            print(f"Failed to add image {f_img} see error:\n{e}")

    draw = ImageDraw.Draw(mosaic)
        #draw.text((4, (tile_height * (mosaic_height)) + 10),title, title_rbg, font=fnt)
        
    if save_as_file and not os.path.exists(save_as_file):
        try:
            mosaic.save(save_as_file)
        except Exception as e:
            print(f'Saving the mosaic to {save_as_file} failed, see error:\n{e}')

    if return_image:
        return mosaic
Example #7
0
    def make_collage(self) -> np.array:
        """
        Make collage based on activation space visual similarity.

        This method:
        1. computes t-SNE embeddings
        2. computes the square grid based on embeddings
        3. creates the collage array and fills it with images from dataset

        :return: 3-channel array representing square collage image
        """

        collage_activations: Tensor = self.data_activations[:self.
                                                            collage_data_size]

        # edge case: only 1 image uploaded -> return the image itself
        if len(collage_activations) == 1:
            return self._get_array_from_image(self.data.train_ds[0][0])

        # prepare the grid
        tsne: np.array = TSNE().fit_transform(collage_activations)
        grid_arrangement: Tuple[int, int] = (self.collage_grid_size,
                                             self.collage_grid_size)
        grid_xy, _ = rasterfairy.transformPointCloud2D(tsne,
                                                       target=grid_arrangement)

        # create the collage
        collage_size = self.collage_grid_size * self.data.img_size
        collage = np.zeros([collage_size, collage_size, 3])  # empty collage
        for i in range(self.collage_data_size):
            row, col = map(
                int, grid_xy[i])  # grid has floats, but need ints to index
            up = row * self.data.img_size
            down = (row + 1) * self.data.img_size
            left = col * self.data.img_size
            right = (col + 1) * self.data.img_size
            collage[up:down, left:right] = self._get_array_from_image(
                self.data.train_ds[i][0])
        return collage
Example #8
0
#     full_image.paste(tile, (int((width-max_dim)*x), int((height-max_dim)*y)), mask=tile.convert('RGBA'))

# matplotlib.pyplot.figure(figsize = (16,12))
# imshow(full_image)


# -----------------------
# Grid-wise visualization
# -----------------------

# Note that nx * ny = len(images)
nx = int(np.sqrt(len(images)))
ny = int(np.sqrt(len(images)))

# assign to grid
grid_assignment = rasterfairy.transformPointCloud2D(tsne, target=(nx, ny))


tile_width = 100
tile_height = 100

full_width = tile_width * nx
full_height = tile_height * ny
aspect_ratio = float(tile_width) / tile_height

grid_image = Image.new('RGB', (full_width, full_height))

for img, grid_pos in zip(images, grid_assignment[0]):
    idx_x, idx_y = grid_pos
    x, y = tile_width * idx_x, tile_height * idx_y
    tile = Image.open(img)
Example #9
0
def main():
    images, pca_features = pickle.load(
        open(os.path.join(save_dir, 'features.p'), 'r'))
    for i, f in list(zip(images, pca_features))[0:5]:
        print(i, f[0], f[1], f[2])

    if len(images) > num_images_to_plot:
        sort_order = sorted(
            random.sample(range(len(images)), num_images_to_plot))
        images = [images[i] for i in sort_order]
        pca_features = [pca_features[i] for i in sort_order]

    X = np.array(pca_features)
    tsne = TSNE(n_components=2,
                learning_rate=150,
                perplexity=30,
                angle=0.2,
                verbose=2).fit_transform(X)

    tx, ty = tsne[:, 0], tsne[:, 1]
    tx = (tx - np.min(tx)) / (np.max(tx) - np.min(tx))
    ty = (ty - np.min(ty)) / (np.max(ty) - np.min(ty))
    #random.shuffle(tx)
    #random.shuffle(ty)

    T = np.arctan2(ty, tx)
    print(type(T))
    print(len(T))

    #plt.scatter(tx,ty,s=55, c=T, alpha=.3)
    #plt.savefig(os.path.join(save_dir, 'image', 'scatter.png'))

    width = 4000
    height = 3000
    max_dim = 120

    full_list = []

    for gif_file in images:
        sample_list = _sample_image(gif_file)
        full_list.append(sample_list)
    full_list = np.array(full_list)
    for i in range(_STRIDE):
        image_list = full_list[:, i]
        full_image = Image.new('RGBA', (width, height))
        for img, x, y in tqdm(zip(image_list, tx, ty)):
            tile = Image.open(img)
            rs = max(1, tile.width / max_dim, tile.height / max_dim)
            tile = tile.resize((int(tile.width / rs), int(tile.height / rs)),
                               Image.ANTIALIAS)
            full_image.paste(tile, (int((width - max_dim) * x), 3000 - int(
                (height - max_dim) * y)),
                             mask=tile.convert('RGBA'))

        #plt.figure(figsize = (16,12))
        #imshow(full_image)
        full_image.save(os.path.join(save_dir, 'image', str(i) + '.png'))
        #plt.show()

    tsne_path = os.path.join(save_dir, 'grid.json')

    data = [{
        "path": os.path.abspath(img),
        "point": [float(x), float(y)]
    } for img, x, y in zip(images, tx, ty)]
    with open(tsne_path, 'w') as outfile:
        json.dump(data, outfile)

    print "saved t-SNE result to %s" % tsne_path
    exit(1)

    arrangements = rasterfairy.getRectArrangements(num_images_to_plot)
    nx = arrangements[0][1]
    ny = arrangements[0][0]
    grid_assignment = rasterfairy.transformPointCloud2D(tsne, target=(nx, ny))
    print 'finished', arrangements

    tile_width = 72
    tile_height = 56

    full_width = tile_width * nx
    full_height = tile_height * ny
    aspect_ratio = float(tile_width) / tile_height

    grid_image = Image.new('RGB', (full_width, full_height))

    grid_list = grid_assignment[0]

    for i in range(_STRIDE):
        image_list = full_list[:, i]
        for img, grid_pos in tqdm(zip(image_list, grid_list)):
            idx_x, idx_y = grid_pos
            x, y = tile_width * idx_x, tile_height * idx_y
            tile = Image.open(img)
            tile_ar = float(
                tile.width
            ) / tile.height  # center-crop the tile to match aspect_ratio
            if (tile_ar > aspect_ratio):
                margin = 0.5 * (tile.width - aspect_ratio * tile.height)
                tile = tile.crop(
                    (margin, 0, margin + aspect_ratio * tile.height,
                     tile.height))
            else:
                margin = 0.5 * (tile.height - float(tile.width) / aspect_ratio)
                tile = tile.crop((0, margin, tile.width,
                                  margin + float(tile.width) / aspect_ratio))
            tile = tile.resize((tile_width, tile_height), Image.ANTIALIAS)
            grid_image.paste(tile, (int(x), int(y)))

        plt.figure(figsize=(16, 12))
        grid_image.save(os.path.join(save_dir, 'image', str(i) + '_grid.png'))
Example #10
0
    for i, line in enumerate(f):
        if i == 0 and skip_header:
            header = line
            continue
        data = line.split(',')
        syllable = data[0]
        x = float(data[1])
        y = float(data[2])
        labels.append(syllable)
        points.append([x, y])
print('- found ' + str(len(labels)) + ' unique words')

# convert to tsne object and grid it
print('reformatting into a grid...')
xy = TSNE().fit_transform(points)
arrangements = rasterfairy.getRectArrangements(len(labels))
grid_xy, (width, height) = rasterfairy.transformPointCloud2D(xy)
print('- grid dimensions: ' + str(width) + ' x ' + str(height))
print('- done')

# write out grid data to file
print('saving to file...')
data = zip(labels, grid_xy)
with open(output_filename, 'w') as f:
    if header is not None:  # put back csv header, if there was one
        f.write(header)
    for label, point in data:
        f.write(label + ',' + str(int(point[0])) + ',' + str(int(point[1])) +
                '\n')
print('- done!')
Example #11
0
PATH = "/Users/Muriz/Desktop/05vis.png"
Image(filename=PATH, width='100%', height=140)

# In[ ]:

get_ipython().system('git clone https://github.com/Quasimondo/RasterFairy.git')

# In[ ]:

get_ipython().system(' pip install ./RasterFairy/.')

# In[ ]:

import rasterfairy

grid_assignment = rasterfairy.transformPointCloud2D(tsne)

# In[ ]:

tile_width = 48
tile_height = 50

full_width = tile_width * 125
full_height = tile_height * 160
aspect_ratio = float(tile_width) / tile_height

grid_image = Image.new('RGB', (full_width, full_height))

for img, grid_pos in tqdm(zip(images, grid_assignment[0])):
    idx_x, idx_y = grid_pos
    x, y = tile_width * idx_x, tile_height * idx_y
Example #12
0
def main():
    """Perform t-SNE."""
    # read parameters for wanted dataset from config file
    with open('config.json') as json_file:
        config_json = json.load(json_file)
        config = config_json[args['dataset']]

    # create directories to save results if they don't exist yet
    resultspath = os.path.join(os.path.dirname(os.getcwd()), 'results')
    if not os.path.exists(resultspath):
        os.makedirs(resultspath)

    # set a random seed
    seed = 28

    # if features are not yet extracted, extract them
    if not os.path.exists(
            os.path.join(config['output_path'], 'pca_features_50.p')):
        # load the pre-trained source network
        print("loading network...")
        dataset = args['dataset']
        modelpath = os.path.join(config['model_savepath'],
                                 '{}_model.h5'.format(dataset))
        model = load_model(modelpath)
        model.summary()

        # create network instance
        network = NeuralNetwork(model, config, batchsize=1, seed=seed)

        # set create a bottleneck model at specified layer
        network.set_bottleneck_model(outputlayer='flatten_1')
        network.model.summary()

        # extract features using bottleneck model
        print("extracting features...")
        bn_features_train, bn_features_test, true_labels_train, true_labels_test = network.extract_bottleneck_features(
        )

        # scale the data to zero mean, unit variance for PCA
        scaler = StandardScaler()
        train_features = scaler.fit_transform(bn_features_train)

        # fit PCA
        print("applying PCA...")
        # pca = PCA(.90)
        pca = PCA(n_components=50)
        # print('Cumulative explained variation for 50 principal components: {}'.format(np.sum(pca.explained_variance_ratio_)))
        pca.fit(train_features)

        # apply PCA to features and test data
        reduced_train_features = pca.transform(train_features)

        # create list of full file paths
        train_paths = [
            os.path.join(config['trainingpath'], filename)
            for filename in network.gen_training.filenames
        ]

        # convert to arrays
        pca_features = np.array(reduced_train_features)
        images = np.array(train_paths)

        # save extracted features in a pickle file
        print("saving features...")
        pickle.dump([images, pca_features, pca],
                    open(
                        os.path.join(config['output_path'],
                                     'pca_features_50.p'), 'wb'))

    else:
        # load features if they are already once extracted
        print("loading features...")
        images, pca_features, pca = pickle.load(
            open(os.path.join(config['output_path'], 'pca_features_50.p'),
                 'rb'))

    for img, f in list(zip(images, pca_features))[0:5]:
        print("image: %s, features: %0.2f,%0.2f,%0.2f,%0.2f... " %
              (img, f[0], f[1], f[2], f[3]))

    # only plot the input amount of images, if no input is given or input is larger than number of images, use all images
    if args['num_images'] == 0 or args['num_images'] > len(images):
        num_images_to_plot = len(images)
    else:
        num_images_to_plot = args['num_images']

    print("number of images used for t-SNE: {}".format(num_images_to_plot))

    # set random seed to always get the same random samples
    random.seed(seed)
    if len(images) > num_images_to_plot:
        sort_order = sorted(
            random.sample(range(len(images)), num_images_to_plot))
        images = [images[i] for i in sort_order]
        pca_features = [pca_features[i] for i in sort_order]

    # # only take the number of images to plot
    # images = images[:num_images_to_plot]
    # pca_features = pca_features[:num_images_to_plot]

    print("performing t-SNE...")
    if args['dims'] == '2D':
        perplexity = int(np.sqrt(num_images_to_plot))
        tsne = TSNE(n_components=2,
                    learning_rate=150,
                    perplexity=perplexity,
                    random_state=seed,
                    angle=0.2,
                    verbose=2).fit_transform(np.array(pca_features))
    if args['dims'] == '3D':
        perplexity = int(np.sqrt(num_images_to_plot))
        tsne = TSNE(n_components=3,
                    learning_rate=150,
                    perplexity=perplexity,
                    random_state=seed,
                    angle=0.2,
                    verbose=2).fit_transform(np.array(pca_features))

    if args['mode'] == 'images':
        print("creating t-SNE image...")
        # normalize the embedding
        tx, ty = tsne[:, 0], tsne[:, 1]
        tx = (tx - np.min(tx)) / (np.max(tx) - np.min(tx))
        ty = (ty - np.min(ty)) / (np.max(ty) - np.min(ty))

        width = 4000
        height = 3000
        max_dim = 100

        full_image = Image.new('RGBA', (width, height))
        for img, x, y in zip(images, tx, ty):
            tile = Image.open(img)
            if 'ISIC' in args['dataset']:
                if 'malignant' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "red")

                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im
                if 'benign' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "green")

                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im

            if 'CNMC' in args['dataset']:
                if 'leukemic' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "red")

                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im
                if 'normal' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "green")

                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im

            rs = max(1, tile.width / max_dim, tile.height / max_dim)
            tile = tile.resize((int(tile.width / rs), int(tile.height / rs)),
                               Image.ANTIALIAS)
            full_image.paste(tile, (int(
                (width - max_dim) * x), int((height - max_dim) * y)),
                             mask=tile.convert('RGBA'))

        plt.figure(figsize=(16, 12))
        plt.imshow(full_image)

        full_image.save(
            os.path.join(
                config['output_path'],
                'tSNE-images-{}-{}-2D-pca_50.png'.format(
                    args['dataset'], num_images_to_plot)))

        print("creating t-SNE grid image...")
        # get dimensions for the raster that is closest to a square, where all the images fit
        max_side = int(num_images_to_plot**(1 / 2.0))
        for ny in range(2, max_side + 1)[::-1]:
            nx = num_images_to_plot // ny
            if (ny * nx) == num_images_to_plot:
                break

        # assign to grid
        grid_assignment = rasterfairy.transformPointCloud2D(tsne,
                                                            target=(nx, ny))

        tile_width = 70
        tile_height = 70

        full_width = tile_width * nx
        full_height = tile_height * ny
        aspect_ratio = float(tile_width) / tile_height

        grid_image = Image.new('RGB', (full_width, full_height))

        for img, grid_pos in zip(images, grid_assignment[0]):
            idx_x, idx_y = grid_pos
            x, y = tile_width * idx_x, tile_height * idx_y
            # tile = Image.open(img)
            tile = Image.open(img)
            if 'ISIC' in args['dataset']:
                if 'malignant' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "red")
                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im
                if 'benign' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "green")
                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im

            if 'CNMC' in args['dataset']:
                if 'leukemic' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "red")
                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im
                if 'normal' in img:
                    old_size = tile.size
                    new_size = (old_size[0] + 20, old_size[1] + 20)
                    new_im = Image.new("RGB", new_size, "green")
                    new_im.paste(tile, (int((new_size[0] - old_size[0]) / 2),
                                        int((new_size[1] - old_size[1]) / 2)))
                    tile = new_im

            tile_ar = float(
                tile.width
            ) / tile.height  # center-crop the tile to match aspect_ratio
            if (tile_ar > aspect_ratio):
                margin = 0.5 * (tile.width - aspect_ratio * tile.height)
                tile = tile.crop(
                    (margin, 0, margin + aspect_ratio * tile.height,
                     tile.height))
            else:
                margin = 0.5 * (tile.height - float(tile.width) / aspect_ratio)
                tile = tile.crop((0, margin, tile.width,
                                  margin + float(tile.width) / aspect_ratio))
            tile = tile.resize((tile_width, tile_height), Image.ANTIALIAS)
            grid_image.paste(tile, (int(x), int(y)))

        plt.figure(figsize=(16, 12))
        plt.imshow(grid_image)

        grid_image.save(
            os.path.join(
                config['output_path'],
                'tSNE-grid-{}-{}-pca_50.png'.format(args['dataset'],
                                                    num_images_to_plot)))

    # for plotting points
    if args['mode'] == 'points':
        if args['dims'] == '2D':
            print("creating t-SNE image...")

            plt.figure(figsize=(16, 10))

            tx, ty = tsne[:, 0], tsne[:, 1]
            y = []

            # create labels for color coding
            for img in images:
                if 'ISIC' in args['dataset']:
                    if 'malignant' in img:
                        y.append(0)
                    if 'benign' in img:
                        y.append(1)

                if 'CNMC' in args['dataset']:
                    if 'leukemic' in img:
                        y.append(0)
                    if 'normal' in img:
                        y.append(1)

            # set red and green color palette for the classes
            palette = sns.color_palette(['#00FF00', '#FF0000'])

            # plot the data
            sns.scatterplot(tx, ty, hue=y, legend='full', palette=palette)

            # set legend
            if 'ISIC' in args['dataset']:
                plt.legend(labels=['malignant', 'benign'], loc="upper right")
            if 'CNMC' in args['dataset']:
                plt.legend(labels=['leukemic', 'normal'], loc="upper right")

            # save the t-SNE plot
            plt.savefig(
                os.path.join(
                    config['output_path'],
                    'tSNE-points-{}-{}-2D-pca_50.png'.format(
                        args['dataset'], num_images_to_plot)))

        if args['dims'] == '3D':
            print("creating t-SNE image...")

            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')

            tx, ty, tz = tsne[:, 0], tsne[:, 1], tsne[:, 2]
            y = []

            # create labels for color coding
            for img in images:
                if 'ISIC' in args['dataset']:
                    if 'malignant' in img:
                        y.append(0)
                    if 'benign' in img:
                        y.append(1)

                if 'CNMC' in args['dataset']:
                    if 'leukemic' in img:
                        y.append(0)
                    if 'normal' in img:
                        y.append(1)

            ax.scatter(tx, ty, tz, c=y, cmap="RdYlGn_r", alpha=1.0)

            # set legend
            if 'ISIC' in args['dataset']:
                plt.legend(labels=['benign', 'malignant'], loc="upper right")
            if 'CNMC' in args['dataset']:
                plt.legend(labels=['normal', 'leukemic'], loc="upper right")

            # save the t-SNE plot
            plt.savefig(
                os.path.join(
                    config['output_path'],
                    'tSNE-points-{}-{}-3D-pca_50.png'.format(
                        args['dataset'], num_images_to_plot)))
Example #13
0
num_datapoints = len(filenames)
print '- loaded data for ' + str(num_datapoints) + ' images'


# calculate arrangement
print 'calculating arrangements...'
arrangements = rasterfairy.getRectArrangements(num_datapoints)
print '- best candidate: ' + str(arrangements[0])
if force_layout:
	print '- ignoring and using (' + str(grid_width) + 'x' + str(grid_height) + ') instead'


# convert to a grid, save results to a CSV file for later use
print 'laying out grid...'
if force_layout:
	grid, (width, height) = rasterfairy.transformPointCloud2D(X, target=(grid_width, grid_height))
else:
	grid, (width, height) = rasterfairy.transformPointCloud2D(X, target=arrangements[0])
data = zip(filenames, grid)
with open(output_filename, 'w') as f:
	f.write('path,x,y' + '\n')
	for path, pos in data:
		f.write(path + ',' + str(int(pos[0])) + ',' + str(int(pos[1])) + '\n')
print '- done (laid out to ' + str(width) + 'x' + str(height) + ' grid)'


# try optimizing the grid layout, though may choke on large data # print 'optimizing (' + str(num_iterations) + ' iterations)'
# optimizer = rfoptimizer.SwapOptimizer()
# swap_table = optimizer.optimize(X, grid, grid_width, grid_height, num_iterations)

def run_rasterfairy(projections):
    side_len = math.ceil(math.sqrt(len(projections)))
    return rasterfairy.transformPointCloud2D(projections,
                                             target=(side_len, side_len))
def run(start_x, end_x, start_y, end_y):
	print '(' + str(start_x) + ', ' + str(start_y) + ')'
	
	# load up all 2D points and filenames
	print 'loading data from file...'
	filenames = []
	X = []
	with open(input_filename) as f:
		f.next()						# skip header
		for i, line in enumerate(f):
			line = line.strip().split(',')
			x = float(line[1])
			y = float(line[2])
			if x >= start_x and x <=end_x and y >= start_y and y <= end_y:
				X.append( [ x, y ] )
				filenames.append(line[0])

	X = np.array(X)
	num_datapoints = X.shape[0]
	print '- loaded data for ' + str(num_datapoints) + ' images'


	# calculate arrangement
	print 'calculating arrangements...'
	arrangements = rasterfairy.getRectArrangements(num_datapoints)
	print '- best candidate: ' + str(arrangements[0])


	# convert to a grid
	print 'laying out grid...'
	# grid, (width, height) = rasterfairy.transformPointCloud2D(X, target=(grid_width,grid_height))
	grid, (width, height) = rasterfairy.transformPointCloud2D(X, target=arrangements[0])
	print '- done (laid out to ' + str(width) + 'x' + str(height) + ' grid)'


	# try optimizing the grid layout, though may choke on large data 
	print 'optimizing (' + str(num_iterations) + ' iterations)'
	optimizer = SwapOptimizer()
	swap_table, improvement = optimizer.optimize(X, grid, width, height, num_iterations, verbose=False)
	print '- improvement of ' + str(improvement)
	if improvement > 0:
		print 'still room for more optimization, running again...'
		iterations = 0
		num_improvement_iterations = 5
		while True:
			if improvement <= 0:
				print '  - no more improvement, stopping...'
				break
			elif iterations == num_improvement_iterations:
				print '  - reached max number of iterations, stopping...'
				break
			
			print '- continuing ' + str(num_iterations) + ' iterations of optimization...'
			swap_table, improvement = optimizer.continueOptimization(iterations*num_iterations, shakeIterations=shake_iterations, verbose=False)
			iterations += 1
			print '  - improvement of ' + str(improvement)
	print '- done'


	# save results to a csv file for later use
	print 'saving to file...'
	data = zip(filenames, grid[swap_table])
	with open(output_filename, 'w') as f:
		f.write('path,x,y' + '\n')
		for path, pos in data:
			f.write(path + ',' + str(int(pos[0])) + ',' + str(int(pos[1])) + '\n')
	print '- done'
import rasterfairy
import sys

from lib.utils import *

# input
parser = argparse.ArgumentParser()
parser.add_argument('-in',
                    dest="INPUT_FILE",
                    default="data/photographic_tsne.csv",
                    help="Input csv file")
parser.add_argument('-out',
                    dest="OUTPUT_FILE",
                    default="data/photographic_grid.csv",
                    help="Output csv file")
a = parser.parse_args()

model = np.loadtxt(a.INPUT_FILE, delimiter=",")
count = len(model)

print("Determining grid assignment...")
gridAssignment = rasterfairy.transformPointCloud2D(model)
grid, gridShape = gridAssignment
print("Resulting shape:")
print(gridShape)

print("Saving grid assignment file %s..." % a.OUTPUT_FILE)
makeDir(a.OUTPUT_FILE)
np.savetxt(a.OUTPUT_FILE, grid, delimiter=",")
print("Done.")
Example #17
0
    samples = samples[:GRID_COUNT]
elif sampleCount < GRID_COUNT:
    print(
        "Not enough samples (%s) for the grid you want (%s x %s = %s). Exiting."
        % (sampleCount, GRID_W, GRID_H, GRID_COUNT))
    sys.exit()

# prep values
for i, s in enumerate(samples):
    for p in PROPS:
        value = s[p]
        if p == "hz":
            value = math.log(value)
        value *= 1000.0
        samples[i]["_" + p] = value

xy = [[s["_" + PROP1], s["_" + PROP2]] for s in samples]
# pprint(xy)
# sys.exit()
xy = np.array(xy)

print("Determining grid assignment...")
gridAssignment = rasterfairy.transformPointCloud2D(xy, target=(GRID_W, GRID_H))
grid, gridShape = gridAssignment
for i, s in enumerate(samples):
    gridX, gridY = grid[i]
    samples[i][OUT_PROP1] = int(gridX)
    samples[i][OUT_PROP2] = int(gridY)

writeCsv(OUTPUT_FILE, samples, headings=fieldNames)
Example #18
0
 def fit_transform(X):
     return rasterfairy.transformPointCloud2D(X[:, :2])[0]