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
code
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
It might not necessarily be an issue with the code logic, but rather the complexity of the problem that's causing the code to take a long time to run. The code is trying to add observers until the function add_observer_until_target() reaches a certain target or the variance stops decreasing. Depending on the input values, this could potentially take a while to complete.
Here are a few suggestions to help optimize the code and potentially reduce the runtime:
Decrease the values of num_samples, num_images, and target_observers to test and see if the code is functioning correctly with lower numbers.
Add print statements throughout the function add_observer_until_target() to monitor the progress of the loop. This way, you can see if the code is stuck in an infinite loop or just taking a long time to compute.
You can adjust the conditions in the while loop within the add_observer_until_target() function or use a maximum number of iterations to avoid the code running for a very long time.
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
By adjusting these factors, you can potentially make the code run in a reasonable time and get the desired output. Once you are confident that the code is working correctly, you can gradually increase the input values and see how the code scales with the problem size.