.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorials/tutorials/preprocess_imgs_sim.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_tutorials_tutorials_preprocess_imgs_sim.py: Image Preprocessing =================== This script illustrates the preprocess steps implemented prior to starting the pipeline of reconstructing a 3D map using simulated 2D images. .. GENERATED FROM PYTHON SOURCE LINES 8-23 .. code-block:: Python import os import matplotlib.pyplot as plt import numpy as np from aspire.noise import WhiteNoiseAdder from aspire.operators import RadialCTFFilter from aspire.source.simulation import Simulation from aspire.volume import Volume file_path = os.path.join( os.path.dirname(os.getcwd()), "data", "clean70SRibosome_vol_65p.mrc" ) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Specify Parameters ------------------ .. GENERATED FROM PYTHON SOURCE LINES 26-50 .. code-block:: Python # Set the initial simulated full size of images. img_size = 33 # Set the demonstration downsampled image size. ds_img_size = 15 # Set the total number of images generated from the 3D map num_imgs = 512 # Set the noise variance and build the noise filter noise_variance = 4e-1 noise_adder = WhiteNoiseAdder(var=noise_variance) # Specify the CTF parameters not used for this example # but necessary for initializing the simulation object pixel_size = 5 * 65 / img_size # Pixel size of the images (in angstroms) voltage = 200 # Voltage (in KV) defocus_min = 1.5e4 # Minimum defocus value (in angstroms) defocus_max = 2.5e4 # Maximum defocus value (in angstroms) defocus_ct = 7 # Number of defocus groups Cs = 2.0 # Spherical aberration alpha = 0.1 # Amplitude contrast .. GENERATED FROM PYTHON SOURCE LINES 51-53 Build Simulation Object and Apply Noise --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 53-80 .. code-block:: Python print("Initialize simulation object and CTF filters.") # Create CTF filters ctf_filters = [ RadialCTFFilter(pixel_size, voltage, defocus=d, Cs=2.0, alpha=0.1) for d in np.linspace(defocus_min, defocus_max, defocus_ct) ] # Load the map file of a 70S ribosome and downsample the 3D map to desired image size. print("Load 3D map from mrc file") vols = Volume.load(file_path) # Downsample the volume to a desired image size and increase density # by 1.0e5 time for a better graph view print(f"Downsample map to a image size of {img_size} x {img_size} x {img_size}") vols = vols.downsample(img_size) * 1.0e5 # Create a simulation object with specified filters and the downsampled 3D map print("Use downsampled map to create simulation object.") source = Simulation( L=img_size, n=num_imgs, vols=vols, unique_filters=ctf_filters, noise_adder=noise_adder, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Initialize simulation object and CTF filters. Load 3D map from mrc file Downsample map to a image size of 33 x 33 x 33 Use downsampled map to create simulation object. .. GENERATED FROM PYTHON SOURCE LINES 81-88 Apply Independent Preprocessing Techniques ------------------------------------------ Now we'll apply each technique sequentially. This is easily accomplished because each preprocessing technique returns a new ``ImageSource`` object. In this case we assign each to a new variable ``source_*``. That leaves the original ``source`` object untouched. .. GENERATED FROM PYTHON SOURCE LINES 88-108 .. code-block:: Python print("Obtain original images.") imgs_od = source.images[0].asnumpy() print("Perform phase flip to input images.") source_pf = source.phase_flip() print(f"Downsample image size to {ds_img_size} X {ds_img_size}") source_ds = source.downsample(ds_img_size) print("Normalize images to background noise.") source_nb = source.normalize_background() print("Whiten noise of images") source_wt = source.whiten() print("Invert the global density contrast if need") source_rc = source.invert_contrast() .. rst-class:: sphx-glr-script-out .. code-block:: none Obtain original images. Perform phase flip to input images. Downsample image size to 15 X 15 Normalize images to background noise. Whiten noise of images 0%| | 0/1 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: preprocess_imgs_sim.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: preprocess_imgs_sim.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_