File size: 3,282 Bytes
5e1ef3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import csv
import random
import os 

def merge_prompt_with_four_colors(
    prompt_file,
    color_file,
    output_file,
    variation=3,
    color_level=3,
    samples_per_color1=2  # Choose two different colors_2 for each color_1
):
    # read prompt 
    with open(prompt_file, "r", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        prompts = list(reader)

    # read color 
    with open(color_file, "r", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        colors = list(reader)

    if len(colors) < samples_per_color1:
        raise ValueError("There are not enough colors to choose two different second colors.")

    merged_rows = []
    id_counter = 1

    for prompt in prompts:
        prompt_text = prompt["prompt"]

        # Iterate through the first color (color_1).
        for color1 in colors:
            hex1 = color1.get("hex") or color1.get("color") or ""
            clean_hex1 = hex1.replace("#", "")

            # Randomly select two different colors_2 for color_1 (they can be equal to color_1, but they must be different from each other).
            color2_choices = random.sample(colors, samples_per_color1)

            for color2 in color2_choices:
                hex2 = color2.get("hex") or color2.get("color") or ""
                clean_hex2 = hex2.replace("#", "")

                # color_3 and color_4 are randomly selected with replacement.
                rand3 = random.choice(colors)
                rand4 = random.choice(colors)
                hex3 = rand3.get("hex") or rand3.get("color") or ""
                hex4 = rand4.get("hex") or rand4.get("color") or ""
                clean_hex3 = hex3.replace("#", "")
                clean_hex4 = hex4.replace("#", "")


                merged_rows.append({
                    "id": id_counter,
                    "prompt": prompt_text,
                    "color_1": hex1,
                    "color_2": hex2,
                    "color_3": hex3,
                    "color_4": hex4,
                    "variation": variation,
                    "color_level": color_level,
                    "split": "test",
                    "color_format": "Hex code"
                })
                id_counter += 1

    
    output_dir = os.path.dirname(output_file) 
    if output_dir and not os.path.exists(output_dir): 
        os.makedirs(output_dir, exist_ok=True)

    # write CSV
    with open(output_file, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=[
            "id","prompt",
            "color_1","color_2","color_3","color_4",
            "variation","color_level","split","color_format"
        ])
        writer.writeheader()
        writer.writerows(merged_rows)

    print(f"Generation complete: {output_file}")

random.seed(42)
merge_prompt_with_four_colors("raw_config/Variation_3.csv", "raw_config/Color_Level_1.csv", "metadata/Variation_3_Color_1.csv", variation=3, color_level=1)
merge_prompt_with_four_colors("raw_config/Variation_3.csv", "raw_config/Color_Level_2.csv", "metadata/Variation_3_Color_2.csv", variation=3, color_level=2)
merge_prompt_with_four_colors("raw_config/Variation_3.csv", "raw_config/Color_Level_3.csv", "metadata/Variation_3_Color_3.csv", variation=3, color_level=3)