SyMBac.renderer
- class SyMBac.renderer.Renderer(simulation, PSF, real_image, camera=None, additional_real_images=None)[source]
Instantiates a renderer, which given a simulation, PSF, real image, and optionally a camera, generates the synthetic data
Example:
>>> from SyMBac.renderer import Renderer >>> my_renderer = Renderer(my_simulation, my_kernel, real_image, my_camera) >>> my_renderer.select_intensity_napari() >>> my_renderer.optimise_synth_image(manual_update=False) >>> my_renderer.generate_training_data( sample_amount=0.2, randomise_hist_match=True, randomise_noise_match=True, burn_in=40, n_samples = 500, save_dir="/tmp/test/", in_series=False )
- __init__(simulation, PSF, real_image, camera=None, additional_real_images=None)[source]
- Parameters
simulation (SyMBac.simulation.Simulation) – The SyMBac simulation.
PSF (SyMBac.psf.PSF_generator) – The PSF to be applied to the synthetic data.
real_image (np.ndarray) – A real image sample
camera (SyMBac.PSF.Camera) – (optional) The simulation camera object to be applied to the synthetic data
additional_real_images (List) – List of additional images which will be randomly used to fourier match during the rendering process.
- generate_PC_OPL(scene, mask, media_multiplier, cell_multiplier, device_multiplier, y_border_expansion_coefficient, x_border_expansion_coefficient, defocus)[source]
Takes a scene drawing, adds the trenches and colours all parts of the image to generate a first-order phase contrast image, uncorrupted (unconvolved) by the phase contrat optics. Also has a fluorescence parameter to quickly switch to fluorescence if you want.
- Parameters
main_segments (list) – A list of the trench segments, used for drawing the trench
offset (int) – The same offset from the draw_scene function. Used to know the cell offset.
scene (2D numpy array) – A scene image
mask (2D numpy array) – The mask for the scene
media_multiplier (float) – Intensity multiplier for media (the area between cells which isn’t the device)
cell_multiplier (float) – Intensity multiplier for cell
device_multiplier (float) – Intensity multiplier for device
y_border_expansion_coefficient (int) – Another offset-like argument. Multiplies the size of the image on each side by this value. 3 is a good starting value because you want the image to be relatively larger than the PSF which you are convolving over it.
x_border_expansion_coefficient (int) – Another offset-like argument. Multiplies the size of the image on each side by this value. 3 is a good starting value because you want the image to be relatively larger than the PSF which you are convolving over it.
fluorescence (bool) – If true converts image to a fluorescence (hides the trench and swaps to the fluorescence PSF).
defocus (float) – Simulated optical defocus by convolving the kernel with a 2D gaussian of radius defocus.
- Returns
expanded_scene (2D numpy array) – A large (expanded on x and y axis) image of cells in a trench, but unconvolved. (The raw PC image before convolution)
expanded_scene_no_cells (2D numpy array) – Same as expanded_scene, except with the cells removed (this is necessary for later intensity tuning)
expanded_mask (2D numpy array) – The masks for the expanded scene
- generate_test_comparison(media_multiplier=75, cell_multiplier=1.7, device_multiplier=29, sigma=8.85, scene_no=- 1, match_fourier=False, match_histogram=True, match_noise=False, debug_plot=False, noise_var=0.001, defocus=3.0, halo_top_intensity=1, halo_bottom_intensity=1, halo_start=0, halo_end=1, random_real_image=None)[source]
Takes all the parameters we’ve defined and calculated, and uses them to finally generate a synthetic image.
- Parameters
media_multiplier (float) – Intensity multiplier for media (the area between cells which isn’t the device)
cell_multiplier (float) – Intensity multiplier for cell
device_multiplier (float) – Intensity multiplier for device
sigma (float) – Radius of a gaussian which simulates PSF apodisation
scene_no (int in range(len(cell_timeseries_properties))) – The index of which scene to render
scale (float) – The micron/pixel value of the image
match_fourier (bool) – If true, use sfmatch to match the rotational fourier spectrum of the synthetic image to a real image sample
match_histogram (bool) – If true, match the intensity histogram of a synthetic image to a real image
offset (int) – The same offset value from draw_scene
debug_plot (bool) – True if you want to see a quick preview of the rendered synthetic image
noise_var (float) – The variance for the simulated camera noise (gaussian)
kernel (SyMBac.PSF.PSF_generator) – A kernel object from SyMBac.PSF.PSF_generator
resize_amount (int) – The upscaling factor to render the image by. E.g a resize_amount of 3 will interally render the image at 3x resolution before convolving and then downsampling the image. Values >2 are recommended.
real_image (2D numpy array) – A sample real image from the experiment you are trying to replicate
image_params (tuple) – A tuple of parameters which describe the intensities and variances of the real image, in this order: (real_media_mean, real_cell_mean, real_device_mean, real_means, real_media_var, real_cell_var, real_device_var, real_vars).
error_params (tuple) – A tuple of parameters which characterises the error between the intensities in the real image and the synthetic image, in this order: (mean_error,media_error,cell_error,device_error,mean_var_error,media_var_error, cell_var_error,device_var_error). I have given an example of their calculation in the example notebooks.
fluorescence (bool) – If true converts image to a fluorescence (hides the trench and swaps to the fluorescence PSF).
defocus (float) – Simulated optical defocus by convolving the kernel with a 2D gaussian of radius defocus.
halo_top_intensity (float) – Simulated “halo” caused by the microfluidic device. This sets the starting muliplier of a linear ramp which is applied down the length of the image in the direction of the trench. ,
halo_bottom_intensity (float) – Simulated “halo” caused by the microfluidic device. This sets the ending multiplier of a lienar ramp which is applied down the length of the image. E.g, if
image
has shape(y, x)
, then this results inimage = image * np.linspace(halo_lower_int,halo_upper_int, image.shape[0])[:, None]
.
- Returns
noisy_img (2D numpy array) – The final simulated microscope image
expanded_mask_resized_reshaped (2D numpy array) – The final image’s accompanying masks
- generate_training_data(sample_amount, randomise_hist_match, randomise_noise_match, burn_in, n_samples, save_dir, in_series=False, seed=False, n_jobs=1, dtype=<class 'numpy.uint8'>)[source]
Generates the training data from a Jupyter interactive output of generate_test_comparison
- Parameters
sample_amount (float) – The percentage sampling variance (drawn from a uniform distribution) to vary intensities by. For example, a sample_amount of 0.05 will randomly sample +/- 5% above and below the chosen intensity for cells, media and device. Can be used to create a little bit of variance in the final training data.
randomise_hist_match (bool) – If true, histogram matching is randomly turned on and off each time a training sample is generated
randomise_noise_match (bool) – If true, noise matching is randomly turned on and off each time a training sample is generated
burn_in (int) – Number of frames to wait before generating training data. Can be used to ignore the start of the simulation where the trench only has 1 cell in it.
n_samples (int) – The number of training images to generate
save_dir (str) – The save directory of the training data
in_series (bool) – Whether the images should be randomly sampled, or rendered in the order that the simulation was run in.
seed (float) – Optional arg, if specified then the numpy random seed will be set for the rendering, allows reproducible rendering results.