In [5]:
import os
import numpy as np
import glob
import csv
import random
test_array = np.repeat(np.arange(6), 12)
print(test_array)
stim_cat_set = 12
cat_names = {0: 'animal', 1: 'music', 2: 'nature',
3: 'speech', 4: 'tools', 5: 'voice'}
cat_num = len(np.arange(len(cat_names.keys())))
In [2]:
np.random.shuffle(test_array)
flag = np.hstack(([False], test_array[:-1] == test_array[1:]))
flag_array = test_array[flag]
num_ocurrences = np.sum(test_array[:-1] == test_array[1:])
print(flag_array, num_ocurrences)
np.random.shuffle(flag_array)
test_array[flag] = flag_array
num_ocurrences = np.sum(test_array[:-1] == test_array[1:])
print(flag_array, num_ocurrences)
In [3]:
def no_repetition_randomizer(array):
"""Returns a randomized version of the array where there are no consecutive numbers that are equal."""
np.random.shuffle(array)
# Check if any two consecutive numbers are the same.
num_ocurrences = np.sum(array[:-1] == array[1:])
# While there are any ocurrences of this...
while num_ocurrences!=0:
# ...shuffle the array...
np.random.shuffle(array)
# ...create a flag for ocurrences...
flag = np.hstack(([False], array[:-1] == array[1:]))
flag_array = array[flag]
# ...and shuffle them.
np.random.shuffle(flag_array)
# Then re-assign them to the original array...
array[flag] = flag_array
# ...and check the number of ocurrences again
num_ocurrences = np.sum(array[:-1] == array[1:])
return array
In [8]:
random_labels = no_repetition_randomizer(test_array)
In [6]:
sounds = np.arange(stim_cat_set * cat_list)
final_sounds = np.zeros(stim_cat_set * cat_list)
print(sounds, final_sounds)
In [9]:
for label in range(len(np.unique(random_labels))):
final_sounds[random_labels == label] = np.random.permutation(sounds[label * stim_cat_set: (label + 1) * stim_cat_set])
print(final_sounds)
In [25]:
def random_map(label_array):
"""Returns a list the same length of the label array with numbers up to len(label_array)."""
# Initialize the output list.
output_list = np.zeros(len(label_array))
# Find out the number of categories...
cat_num = len(np.unique(label_array))
# ...and the number of elements for each category.
stim_per_cat = int(len(label_array) / cat_num)
for label in range(cat_num):
output_list[label_array == label] = np.random.permutation(range(label * stim_per_cat,
(label + 1) * stim_per_cat))
return output_list
In [27]:
final_list = random_map(random_labels)
print(final_list)
In [ ]: