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())))


[0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3
 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5]

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)


[4 2 5 5 2 4 4 3 3] 9
[2 4 4 3 2 5 5 4 3] 3

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)


[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

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)


[15. 39. 54. 66.  6. 68. 34. 67. 27. 22. 53.  4. 52. 19.  7. 64.  5. 12.
 60. 36. 61. 50. 25. 71. 44. 17. 47. 56. 10. 13. 29. 21.  3. 38. 65. 42.
 31. 11. 41. 69. 51. 62. 48. 43. 24. 70.  1. 18. 33. 37. 58. 32.  2. 30.
  9. 46. 59. 20. 26. 63. 40. 14.  8. 35. 45. 49. 23. 57.  0. 16. 55. 28.]

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)


12 6
[15. 42. 49. 66.  1. 71. 35. 70. 28. 17. 50.  9. 57. 13.  5. 64. 10. 19.
 62. 38. 60. 54. 30. 68. 45. 20. 39. 56.  7. 14. 25. 22. 11. 46. 65. 37.
 26.  8. 36. 61. 55. 67. 59. 47. 24. 69.  3. 18. 29. 41. 48. 31.  2. 27.
  4. 40. 58. 16. 33. 63. 44. 23.  0. 34. 43. 52. 21. 53.  6. 12. 51. 32.]

In [ ]: