# imports import random import concurrent.futures import numpy as np import scipy import skimage def get_single_spot_im( # type: ignore[no-untyped-def] height, width, spot_x, spot_y, spot_size, spot_intensity, secondary_spot_scale, is_primary, ): single_spot = np.zeros((height, width), dtype=np.float64) if is_primary: single_spot += 0.75 * spot_intensity intensity = spot_intensity if is_primary else random.random()*spot_intensity * secondary_spot_scale single_spot[spot_y, spot_x] += 0.5*intensity single_spot[ spot_y - spot_size // 2 : spot_y + spot_size // 2, spot_x - spot_size // 2 : spot_x + spot_size // 2, ] += 0.5*intensity if is_primary: sigma_y = random.random() * spot_size sigma_x = random.random() * spot_size else: sigma_y = 2.0 * random.random() * spot_size sigma_x = 2.0 * random.random() * spot_size while sigma_y < 0.5*spot_size: sigma_y = 2.0 * random.random() * spot_size while sigma_x < 0.5*spot_size: sigma_x = 2.0 * random.random() * spot_size single_spot = scipy.ndimage.gaussian_filter( single_spot, sigma=(sigma_y, sigma_x), ) if is_primary: single_spot -= 0.75 * spot_intensity single_spot = skimage.transform.rotate( single_spot, random.random() * 360, center=(spot_x, spot_y) ) return single_spot def make_test_image( # type: ignore[no-untyped-def] height=677, width=685, num_spots=25, spot_size=25, spot_intensity=10000, secondary_spot_scale=0.5, noise_mean=500, noise_stddev=10, ): test_im = np.zeros((height, width), dtype=np.float64) noise = np.random.normal(noise_mean, noise_stddev, test_im.shape) noise[noise < 0] = 0 primary_spot_x = None primary_spot_y = None futures = [] with concurrent.futures.ProcessPoolExecutor() as executor: for i in range(num_spots): bright_spot_x = random.randint( int(2 * spot_size), width - int(2 * spot_size) - 1 ) bright_spot_y = random.randint( int(2 * spot_size), height - int(2 * spot_size) - 1 ) if i == 0: primary_spot_x = bright_spot_x primary_spot_y = bright_spot_y futures.append( executor.submit( get_single_spot_im, height, width, bright_spot_x, bright_spot_y, spot_size, spot_intensity, secondary_spot_scale, i == 0, ) ) for future in concurrent.futures.as_completed(futures): test_im += future.result() test_im += noise test_im[test_im < 0] = 0 return test_im, (primary_spot_y, primary_spot_x)