Here is my experimental design: - Total of 840 reference images, evenly divided into 3…
- User
Here is my experimental design:
-
Total of 840 reference images, evenly divided into 3 categories: animals, people, nature, and (280 images per category).
-
Each image can have 3 distortions applied to them (01, 02, 03), and each distortion has 3 levels (A, B, C).
-
I want approximately 300 observers to rate around 420 reference images each such that on the observer level:
- Each observer rates an equal number of images from each of the 4 categories.
- The distortions and levels are evenly applied within each category.
- Each reference image is maximally seen once by each observer.
A less important priority is that on a group level, all reference images should receive a similar number of ratings if possible and not compromise with the above goals.
Please propose python code to generate a csv with images for each observer. The image format could be as follows 637_03_A_nature.jpg
You can be inspired by this JS code that did something similar
// function for shuffling function shuffle(array) { var copy = [], n = array.length, i;
// While there remain elements to shuffle… while (n) {
// Pick a remaining element… i = Math.floor(Math.random() * array.length); // If not already shuffled, move it to the new array. if (i in array) { copy.push(array[i]); delete array[i]; n--; }}
return copy; }
// function for generating a sequence function range(start, end) { return Array(end - start + 1).fill().map((_, idx) => start + idx) }
numimg = 192; letter1 = new Array(numimg / 16).fill([".jpg", "_06_03.jpg", "_07_03.jpg", "_08_03.jpg"]).flat(); letter2 = new Array(numimg / 16).fill([".jpg", "_06_03.jpg", "_07_03.jpg", "_08_03.jpg"]).flat(); letter3 = new Array(numimg / 16).fill([".jpg", "_06_03.jpg", "_07_03.jpg", "_08_03.jpg"]).flat(); letter4 = new Array(numimg / 16).fill([".jpg", "_06_03.jpg", "_07_03.jpg", "_08_03.jpg"]).flat();
letter1 = shuffle(letter1); letter2 = shuffle(letter2); letter3 = shuffle(letter3); letter4 = shuffle(letter4);
console.log(letter1)
// generate image array imagearray = range(1, numimg) imagearray = shuffle(imagearray);
distortion = letter1.concat(letter2).concat(letter3).concat(letter4)
// merge both arrays element by element to get the final stim array stims = [imagearray , distortion ].reduce((a, b) => a.map((v, i) => v + b[i]))
// shuffle stims = shuffle(stims)
-
- Assistant
Here's the equivalent Python code to generate a CSV file with images for each observer:
import random import csv def shuffle(array): copy = [] n = len(array) while n: i = random.randint(0, len(array) - 1) if array[i] is not None: copy.append(array[i]) array[i] = None n -= 1 return copy def range_(start, end): return [i for i in range(start, end + 1)] num_img = 840 num_observers = 300 letters = ["01_A", "01_B", "01_C", "02_A", "02_B", "02_C", "03_A", "03_B", "03_C"] categories = ["_animals", "_people", "_nature"] image_list = [] for cat in categories: for letter in letters: image_list.extend(range_(1, 280//3) * 3) img_set = ["{}_{}_{}".format(img, letter, cat) + ".jpg" for img in image_list] img_set = shuffle(img_set) with open("output.csv", "w", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(["Observer", "Images"]) for i in range(1, num_observers + 1): observer_images = shuffle(img_set)[:420] writer.writerow(["Observer {}".format(i), ", ".join(observer_images)])