spreizer_test.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import numpy as np
  2. import pylab as pl
  3. from opensimplex import OpenSimplex
  4. noise = OpenSimplex()
  5. sheet_size = 900
  6. number_of_grid_points_per_row = 60
  7. scale = 200
  8. # Get simplex values (not uniformly distributed)
  9. x = y = np.linspace(0, sheet_size, number_of_grid_points_per_row)
  10. simplex_values = [[noise.noise2d(i / scale, j / scale) for j in y] for i in x]
  11. simplex_values = np.concatenate(simplex_values)
  12. # Sort values and map them to a set of linearly increasing numbers from -1 to 1
  13. sorted_idx = np.argsort(simplex_values)
  14. rescaled_simplex_values = np.zeros(shape=np.shape(simplex_values))
  15. number_of_available_values = 360
  16. number_of_grid_points = len(simplex_values)
  17. is_divisible = number_of_grid_points % number_of_available_values == 0
  18. if not is_divisible:
  19. print("Number of values {:d} does not divide number of grid points {:d}. Distribution will not be uniform.".format(
  20. number_of_available_values, number_of_grid_points))
  21. repeats = number_of_grid_points // number_of_available_values if is_divisible else number_of_grid_points // \
  22. number_of_available_values + 1
  23. available_values = np.linspace(-1, 1, number_of_available_values, endpoint=False)
  24. new_values = np.repeat(available_values, repeats)
  25. rescaled_simplex_values[sorted_idx] = new_values[:len(rescaled_simplex_values)]
  26. # Plot and check uniformity
  27. rescaled_simplex_values = rescaled_simplex_values
  28. bins = np.linspace(-1, 1, number_of_available_values + 1, endpoint=True) # To get the same bin edges as the available
  29. # values
  30. occurrences, _, _ = pl.hist(rescaled_simplex_values, bins=bins)
  31. pl.matshow(rescaled_simplex_values.reshape(number_of_grid_points_per_row, -1), vmin=-1, vmax=1, extent=(0,
  32. sheet_size,
  33. 0, sheet_size))
  34. pl.colorbar()
  35. pl.xlim(0, sheet_size)
  36. pl.ylim(0, sheet_size)
  37. pl.show()
  38. """
  39. Old version with telling variable names
  40. old_rescaled_simplex_values = np.zeros(shape=np.shape(simplex_values))
  41. number_of_available_values = number_of_grid_points * 2
  42. number_of_instances_for_each_value = len(simplex_values) // number_of_available_values
  43. for ii, val in enumerate(range(number_of_available_values)):
  44. old_rescaled_simplex_values[sorted_idx[ii * number_of_instances_for_each_value:(ii + 1) * number_of_instances_for_each_value]] = val
  45. old_rescaled_simplex_values = (old_rescaled_simplex_values - number_of_grid_points) / number_of_grid_points
  46. """