This code runs for a very long time without an output does anything seem wrong?


import pandas as pd
import random
import numpy as np
import matplotlib.pyplot as plt

def generate_images(num_samples, num_images):
    # Take list (with 3 equal chunks)
    # Give each chunk the same number of distortions
    chunk_size = num_samples // 3

    imgs_chunk1 = random.sample(range(1, num_images // 3 + 1), chunk_size)
    imgs_chunk2 = random.sample(range(num_images // 3 + 1, 2 * num_images // 3 + 1), chunk_size)
    imgs_chunk3 = random.sample(range(2 * num_images // 3 + 1, num_images + 1), chunk_size)
    imgs = imgs_chunk1 + imgs_chunk2 + imgs_chunk3

    # Create letters for each chunk
    distortions = [f'{d}{l}' for d in [1, 2, 3] for l in ['a', 'b', 'c']] + ['original']
    num_distortions = len(distortions)
    num_chunks = 3

    let = pd.Series(distortions * (num_samples // (num_distortions * num_chunks)))
    # Combine chunks, each randomized
    test = pd.concat([let.sample(frac=1) for _ in range(num_chunks)], ignore_index=True)

    # Check that each chunk has the same number of letters
    test = test.astype('category')  # Convert to category for overview

    # Combine and shuffle
    images = pd.DataFrame({'imgs': imgs, 'test': test})

    return images

def add_observer_until_target(result_df, target_observers, num_samples, num_images):
    current_observers = result_df['observer_nr'].nunique()
    min_variance = np.var(result_df.groupby(['imgs', 'test']).size().reset_index(name='count')['count'])

    while current_observers < target_observers:
        seed = current_observers + 0  # Change the seed based on the number of observers

        # Generate a new observer DataFrame
        random.seed(seed)
        np.random.seed(seed)
        new_observer_df = generate_images(num_samples, num_images)
        new_observer_df['observer_nr'] = current_observers + 1

        # Append the new observer DataFrame to the existing result DataFrame
        combined_result_df = pd.concat([result_df, new_observer_df], ignore_index=True)

        # Convert 'imgs' and 'test' columns to category data type for overview
        combined_result_df['imgs'] = combined_result_df['imgs'].astype('category')
        combined_result_df['test'] = combined_result_df['test'].astype('category')

        # Count the occurrences of each combination
        combination_counts = combined_result_df.groupby(['imgs', 'test']).size().reset_index(name='count')

        # Measure the variance in the count
        variance = np.var(combination_counts['count'])

        # If this addition of observer reduces the variance, update the result DataFrame and increment the observer count
        if variance < min_variance:
            min_variance = variance
            result_df = combined_result_df
            current_observers += 1
            print(f"New Observer {current_observers}: Variance in count: {variance}")

    return result_df

num_samples = 420 
num_images = 540
start_observers = 20
target_observers = 25

# Initialize the seeds and DataFrames list for the initial number of observers
seeds = list(range(1, 1 + start_observers))
observer_dfs = []

# Generate initial DataFrames for start_observers observers
for seed in seeds:
    random.seed(seed)
    np.random.seed(seed)
    observer = len(observer_dfs) + 1
    observer_images = generate_images(num_samples, num_images)
    observer_images['observer_nr'] = observer
    observer_dfs.append(observer_images)

# Concatenate the initial DataFrames
initial_result_df = pd.concat(observer_dfs, ignore_index=True)

# Add more observers until the target number is reached or adding new observer doesn’t reduce the variance
final_result_df = add_observer_until_target(initial_result_df, target_observers, num_samples, num_images)

# Plot the distribution with the least variance
combination_counts = final_result_df.groupby(['imgs', 'test']).size().reset_index(name='count')
variance = np.var(combination_counts['count'])
plt.hist(combination_counts['count'], bins=10)
plt.xlabel('Count')
plt.ylabel('Frequency')
plt.title(f'Histogram of Counts\nVariance: {variance:.4f}')
plt.show()