import numpy as np import pylab as pl from opensimplex import OpenSimplex noise = OpenSimplex() sheet_size = 900 number_of_grid_points_per_row = 60 scale = 200 # Get simplex values (not uniformly distributed) x = y = np.linspace(0, sheet_size, number_of_grid_points_per_row) simplex_values = [[noise.noise2d(i / scale, j / scale) for j in y] for i in x] simplex_values = np.concatenate(simplex_values) # Sort values and map them to a set of linearly increasing numbers from -1 to 1 sorted_idx = np.argsort(simplex_values) rescaled_simplex_values = np.zeros(shape=np.shape(simplex_values)) number_of_available_values = 360 number_of_grid_points = len(simplex_values) is_divisible = number_of_grid_points % number_of_available_values == 0 if not is_divisible: print("Number of values {:d} does not divide number of grid points {:d}. Distribution will not be uniform.".format( number_of_available_values, number_of_grid_points)) repeats = number_of_grid_points // number_of_available_values if is_divisible else number_of_grid_points // \ number_of_available_values + 1 available_values = np.linspace(-1, 1, number_of_available_values, endpoint=False) new_values = np.repeat(available_values, repeats) rescaled_simplex_values[sorted_idx] = new_values[:len(rescaled_simplex_values)] # Plot and check uniformity rescaled_simplex_values = rescaled_simplex_values bins = np.linspace(-1, 1, number_of_available_values + 1, endpoint=True) # To get the same bin edges as the available # values occurrences, _, _ = pl.hist(rescaled_simplex_values, bins=bins) pl.matshow(rescaled_simplex_values.reshape(number_of_grid_points_per_row, -1), vmin=-1, vmax=1, extent=(0, sheet_size, 0, sheet_size)) pl.colorbar() pl.xlim(0, sheet_size) pl.ylim(0, sheet_size) pl.show() """ Old version with telling variable names old_rescaled_simplex_values = np.zeros(shape=np.shape(simplex_values)) number_of_available_values = number_of_grid_points * 2 number_of_instances_for_each_value = len(simplex_values) // number_of_available_values for ii, val in enumerate(range(number_of_available_values)): old_rescaled_simplex_values[sorted_idx[ii * number_of_instances_for_each_value:(ii + 1) * number_of_instances_for_each_value]] = val old_rescaled_simplex_values = (old_rescaled_simplex_values - number_of_grid_points) / number_of_grid_points """