Пример #1
0
def generation_results(chain, generation, sublexical=False, permutations=1000, meaning_distances=False, experiment=False):
  if type(meaning_distances) == bool and meaning_distances == False:
    meaning_distances = rater_analysis.reliable_distance_array
  if type(experiment) == bool and experiment == False:
    experiment = basics.determine_experiment_number(chain)
  strings = basics.getWords(experiment, chain, generation, 's')
  if len(set(strings)) > 1:
    if sublexical == True:
      return sublexical_structure.test(strings, meaning_distances, permutations)
    string_distances = basics.stringDistances(strings)
    return Mantel.test(string_distances, meaning_distances, permutations)[2]
  return None
Пример #2
0
def generation_results(experiment, chain, generation):
  strings = basics.getWords(experiment, chain, generation, 's')
  # Return None if there are < 3 unique strings
  if len(set(strings)) > 2:
    string_distances = basics.stringDistances(strings)
    # Iterate through feature combinations to find the best correlation
    best_r = -1
    for i in range(0, len(all_combination_matrices)):
      r = np.corrcoef(string_distances, all_combination_matrices[i])[0,1]
      if r > best_r:
        best_matrix = i
        best_r = r
    # Return type number and correlation coefficient for combination of features with strongest correlation
    return best_matrix + 1, best_r
  return None
Пример #3
0
def generate_colour_palette(strings, use_rgb=False, spectrum=[0.0, 1.0], random_seed=False):

  # Get list of unique strings
  words = list(set(strings))

  # If there's only one word, just map that word to a grey colour and return, since
  # it won't make sense to arrange the words in colour space.
  if len(words) == 1:
    return {words[0] : ('#B1B0CB', '#D8D8E5')}

  # Create distance matrix giving normalized Levenshtein distances between the words
  string_distances = np.array(basics.stringDistances(words), dtype=float)
  string_distance_matrix = distance.squareform(string_distances, 'tomatrix')

  if type(random_seed) != int:
    # Pick a random number for the MDS algorithm
    random_seed = np.random.randint(1, 1000000)

  hex_colour_values = []

  if use_rgb == True:

    # Run distance matrix through MDS to determine the position of each word in 3-dimensional space
    string_mds = MDS(dissimilarity='precomputed', n_components=3, n_init=25, max_iter=2000, random_state=random_seed)
    string_coordinates = string_mds.fit_transform(string_distance_matrix)

    # Scale the dimensions of the space over the interval [0, 255] to create an RGB colour space.
    # The spectrum argument determines how much of the colour space will be used, allowing you to
    # avoid very dark and very light colours.
    for dim in range(0, 3):
      minimum = string_coordinates[:, dim].min()
      difference = string_coordinates[:, dim].max() - minimum
      string_coordinates[:, dim] = (((string_coordinates[:, dim] - minimum) / difference) * (255 * (spectrum[1] - spectrum[0]))) + (255 * spectrum[0])

    # Convert RGB values to hexadecimal triplets (the light version is for the Voronoi cells)
    for r, g, b in string_coordinates:
      hex_colour = rgb_to_hex((r, g, b))
      hex_colour_light = rgb_to_hex(lighten((r, g, b)))
      hex_colour_values.append((hex_colour, hex_colour_light))

  else:

    # Run distance matrix through MDS to determine the position of each word in 2-dimensional space
    string_mds = MDS(dissimilarity='precomputed', n_components=2, n_init=25, max_iter=2000, random_state=random_seed)
    string_coordinates = string_mds.fit_transform(string_distance_matrix)

    # Convert Cartesian coordinates to polar coordinates
    polar_coordinates = np.array([polarize(point) for point in string_coordinates])

    # Rescale the saturation coordinates in the specified spectrum
    minimum = polar_coordinates[:, 1].min()
    difference = polar_coordinates[:, 1].max() - minimum
    polar_coordinates[:, 1] = (((polar_coordinates[:, 1] - minimum) / difference) * (spectrum[1] - spectrum[0])) + (spectrum[0])

    # Convert HSV values to hexadecimal triplets via RGB, keeping V (brightness) constant
    # The light version is for the Voronoi cells
    for h, s in polar_coordinates:
      hex_colour = rgb_to_hex(hsv_to_rgb(h, s, 0.8))
      hex_colour_light = rgb_to_hex(hsv_to_rgb(h, s, 1.0))
      hex_colour_values.append((hex_colour, hex_colour_light))

  #print('Correspondence: %s' % correspondence_correlation(string_distances, string_coordinates))
  #print('Stress-1: %s' % stress_1(string_mds.stress_, string_distances))

  # Return the colour palette and the random seed
  return dict(zip(words, hex_colour_values)), random_seed