<jupyter_start><jupyter_text>Tutorial-0 Part B: introduction to simple image processing Before working through this notebook, please make a copy and save it with the same name plus your initials. Continue this tutorial using your copy.  Table of Contents This tutorial contains a couple of data processing examples and exercises as a preparation for the main summer school tutorials. If you have no previous experience with Python or Jupyter Notebook, please complete part A of tutorial-0 before continuing to part B. * [1. Loading libraries](heading1)* [2. Load, plot, calibrate and save data](heading2)    * [Load an image](heading2sub1)    * [Plot an image](heading2sub2)    * [Check and input file information](heading2sub3)     * [Exercise 1: Adding scale bar and saving image](heading2sub4)    * [Exercise 2: plot an image](heading2sub5)     * [Process a batch of images](heading2sub6) * [3. Edit and analyze data](heading3)    * [Binning an image](sub7)    * [Rotating an images](sub8)    * [Exercise 3: Adjusting image contrast](sub9)    * [Exercise 4: line profiles](sub10)  Load libraries * Numpy, pyplot, tkinter and os are Python standard libraries. More information of these libraries are avalible through the Python standard library documentation.* EM_forWin includes standard routines for processing STEM data in Windows environment. For more information, please check the .py files in the EM_forWin folder.<jupyter_code># Output plot style
%matplotlib notebook
# Standard Python libraries needed for this tutorial
import numpy as np
import matplotlib.pyplot as plt
import tkinter.filedialog as tf
import os
# Prepopulated library 
from EM_forWin import *<jupyter_output><empty_output><jupyter_text>[go to top](toc)  Load, plot, process and save data  * Load an image  Electron microscopy (EM) data have a variety of different file types, such as .ser, .dm4, .tif, .mrc etc. As an initial example, we will start with a .tif file. You will learn more on processing different file types in Tutorial 2. In the next cell, you will first find the image using the `askopenfilenames` command and then load it with the `EM.EM_image` function.  Please execute the following cell and using the pop-up window, navigate to Tutorial 0 -> data -> images, and select **"corrCROP_35_O4_10Mx_50x0.5us_1024px_50umC2_spot9_160mmCL_1"**<jupyter_code>tiffIm_fn = tf.askopenfilenames(initialdir='../')
tiffIm = EM.EM_image(tiffIm_fn[0])<jupyter_output><empty_output><jupyter_text>* Plot an image<jupyter_code>tiffIm.show()<jupyter_output><empty_output><jupyter_text>You can zoom in on a specific area with the small box icon in the interactive GUI above. You can go back to original view with the home icon.       After you are done adjusting the figure, you can close the interactive GUI at the upper right cornor to save RAM.   Another way to find the image is by entering the file path.      You can also locate the image you would like to load without using the interactive GUI shown above. Instead, you can directly give `EM.EM_image` the path to the image. Use `../` to get the parent directory of the current working directory and you can append the rest of the file path by adding strings using the `+` key.<jupyter_code>tiffIm_fn_path = '../'+'/data'+'/images'+'/corrCROP_35_O4_10Mx_50x0.5us_1024px_50umC2_spot9_160mmCL_1.tif'
tiffIm = EM.EM_image(tiffIm_fn_path)<jupyter_output><empty_output><jupyter_text>You can select a different     [color map](https://matplotlib.org/stable/tutorials/colors/colormaps.html) for the image.<jupyter_code>tiffIm.show(cmap='magma')<jupyter_output><empty_output><jupyter_text>There is more data in the *more_images* folder under the *data* directory that you can play with. [go to top](toc) * Check and input file information  The file is stored as a class. file.data is the raw numeric data. Any numerical opration used for numpy arrasy can be applied to file.data.   Notice the data types for tiffIm and tiffIm.data.<jupyter_code>print(type(tiffIm))
print(type(tiffIm.data))<jupyter_output><empty_output><jupyter_text>Input file information such as pixel size.  Some files have matedata stored within and some don't. Here is an example of a file witout meta data, where you would need to manually input the pixel size.<jupyter_code>tiffIm.info['pixelSize'] = 0.0087 ##### need to double check the 
tiffIm.info['unit'] = 'nm'
print('The x-dimension of tiffIm is',tiffIm.x)
print('The y-dimension of tiffIm is',tiffIm.y)
print(tiffIm.info)<jupyter_output><empty_output><jupyter_text>[go to top](toc)   **Exercise 1: Adding a scale bar and saving an image**    You can add a scale bar to an image as long as it has pixel size information by `functions.scalebar`. Can you save tiffIm with a scale bar to **data -> outPuts_imgs** and name it **'corrCROP_35_O4_10Mx_50x0.5us_1024px_50umC2_spot9_160mmCL_1_withScaleBar'**?   **Hint**      You can use `tf.askdirectory` to assign the output directory and append the rest of the pathname yourself. Use `readwrite.saveTiff` to save as tiff image. To learn what inputs this function takes, run `readwrite.saveTiff?`in the cell below, and to learn what it does, run `readwrite.saveTiff??`<jupyter_code>tiffIm_with_scaleBar = functions.scalebar(tiffIm)
readwrite.saveTiff??<jupyter_output><empty_output><jupyter_text>**Exercise 2: Plot an image**    You can use `functions.show` to plot `image.data` as shown below. Can you plot only the upper-right quarter of serim with an orange color map and **(4,4)** figure size?   **Hint**  `functions.show??`<jupyter_code>tiffIm_2 = EM.EM_image('../'+'/data'+'/images'+'/corr_HAADF_06_3.6Mx_50x0.5us_1024px_0.4nAscreen_50umC2_1.tif')
functions.show(tiffIm_2.data)
functions.show??<jupyter_output><empty_output><jupyter_text>[go to top](toc) * Process a batch of images  It is often useful to process a batch of data at a time. Here is an example of processing a batch of data, adding scale bars to them and saving them as tiffs. All the images in this batch are .ser images, which usually do contain metadata such as the pixel size. The images in this batch include different magnifications of the same regions.   The pixel size in the metadata is extracted directly from the microscope. In Tutorial 2, you will learn more about manually calibrating the pixel size as well.<jupyter_code>data_patch_dir = os.path.dirname(os.getcwd())+'/Data/image_batch'
outPut_patch_dir = os.path.dirname(os.getcwd())+'/Data/outPuts_batch'
for f in os.listdir(data_patch_dir):
    if f.split('.')[-1] == 'ser':
        image = EM.EM_image(data_patch_dir+'/'+f)
        image_scale = functions.scalebar(image)
        readwrite.saveTiff(image_scale, outPut_patch_dir+'/'+f)<jupyter_output><empty_output><jupyter_text>Now check the processed images.<jupyter_code>for f in os.listdir(outPut_patch_dir):
    image = EM.EM_image(outPut_patch_dir+'/'+f)
    functions.show(image.data)<jupyter_output><empty_output><jupyter_text>[go to top](toc)  Edit and analyze data   * Binning an image   There are many useful image analysis functions in the EM pacakge. Here, we show an example of binning an image. The function applied on the image directly edits the raw data. To "undo" the changes, you can just reload the image.   Notice after binning by 8, there are Moire patterns introduced!<jupyter_code>serIm_fn ='../'+'/Data/images/corrCROP_25_5.1x_50x0.5us_1024px_130mmCL_spot9_mono31_0.3nAscreenI_50umC2_1.tif'
serIm = EM.EM_image(serIm_fn)
print("Unbinned shape {}".format(serIm.shape()))
serIm.show()
serIm.rebin2d(4)
print("Binned shape {}".format(serIm.shape()))
serIm.show()
serIm.rebin2d(2)
print("Binned shape {}".format(serIm.shape()))
serIm.show()<jupyter_output><empty_output><jupyter_text>[go to top](toc) * rotating images   As mentioned earlier in this tutorial, the numpy methods applicable to numpy arrays can all be applied to file.data. Here is an example applying `np.rot90` to rotate an image by 90°.<jupyter_code>serIm = EM.EM_image(serIm_fn)
functions.show(np.rot90(serIm.data))
serIm = EM.EM_image(serIm_fn)<jupyter_output><empty_output><jupyter_text>[go to top](toc) Next, you will enhance the contrast in this image by using the `enhance contrast` function. Run the following cell to learn more about what this function does.<jupyter_code>functions.enhance_contrast??<jupyter_output><empty_output><jupyter_text>**Exercise 3: Adjusting image contrast**    Adjusting the contrast of an image is essentially adjusting the display range. Here is an image with an un-optimized display range. First, can you plot the image data as a histogram? Recall the histogram exercise in tutorial0-A.After obtaining a histogram, use `file.enhance_contrast` to 'saturate' some pixels in order to optimize the display range. Compare the histogram before and after adjusting contrast.  **Hint**  - `array.flatten()` may be helpful for plotting the histogram. - Try `bins = 100`  Clipping / saturating too many pixels can actually be risky. Discuss the values you chose to saturate with your TA.<jupyter_code>tiffIm_2_fn ='../' +'/Data/images/14_1.3Mx_1x2us_4096px_50umC2_CL300mm_600pAscreen_1.tif'
tiffIm_2 = EM.EM_image(tiffIm_2_fn)
tiffIm_2.show()<jupyter_output><empty_output><jupyter_text>Notice that you can call the `enhance_contrast` function directly to tiffIm_2. Run the next cell to learn how it works.<jupyter_code>tiffIm_2.enhance_contrast??<jupyter_output><empty_output><jupyter_text>**Exercise 4: Line profiles**     Plot line profiles in horizontal and vertical directions separately with a line width of 30 pixels and centered with respect to x=925 for the horizontal profile and y=925 for the vertical profile.  See if you can identify interfaces. It will be best if you can plot the line profiles for the x and y directions on the same scale.        **Hint**      `np.average`<jupyter_code>tifImag_fn_2 = '../'+'/Data/images/interface_rot.tif'
tiffIm_2 = EM.EM_image(tifImag_fn_2)
tiffIm_2.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0886317907444668, 'fraction_numerical': 0.020724346076458754, 'mean_word_length': 5.327816677275621, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 72, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '371707', 'license': 'MIT', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3076, 'n_words': 1142, 'path': 'Tutorial 0 - Python 101/Tutorial0b.ipynb', 'repo_name': 'paradimdata/Cornell_EM_SummerSchool_2021', 'size': '9940'}