Intro to CZI files Part 2
Reading CZI files with Python.
Purpose: Jclub tutorial for how to process czi files in python. This notebook will just be how to open CZI files and view them in jupyter notebook.
Disclaimer: Marc and Ciera taught me everything I know
General info: The best image analysis tutorial everrrrr
Open a notebook and specify where you are¶
### in the terminal use cd to get to the directory you want to be in.
### go into the conda environemnt where you installed czifile stuff
souce activate 'yourenv' # change 'yourenv' to whatever you named your environment
### Then type:
jupyter notebook
### this should open up a directory in your browser.
## Click new and then click on whichever environment you are in. This should open a new notebook!
Import packages¶
### Essential for viewing importing and viewing images
import czifile #this is the package to import CZI files
import matplotlib.pyplot as plt #this package visualizes images
### Packages for manipulating images
from scipy import ndimage as ndi
from skimage import filters, measure, segmentation, transform, exposure, img_as_ubyte, feature, morphology
Mise en place - get those folders organized!¶
- Download 100118-oligopaint3-2-02.czi
- Move it to whatever folder you want to work out of #### use 'os' packages to change directories in python
import os
I like to make variables that store paths for specific projects¶
ProjectDirectory = ('/Users/jennahaines/Box Sync/Eisen_Lab/IntrotoCZIfiles')
ProjectData = ('/Users/jennahaines/Box Sync/Eisen_Lab/IntrotoCZIfiles/data/')
ProjectBin = (ProjectDirectory + '/bin')
Make results notebook¶
I like to make a new results notebook in the same directory for each jupyter notebook - that way I know which images came from which code later. I based it off of Nobel et al., 2009.
ProjectDirectory
- bin
- docs
- data
NotebookResultsPath = (ProjectDirectory)
Change directory to the results folder so that anything you generate will automatically go in there¶
os.chdir(NotebookResultsPath)
Open a CZI file¶
I like to put the CZI name as a seperate variable so that I can easily hook things I am writing into loops
CziName = '100118-oligopaint3-2-02.czi'
I make a full path varaible in a second step so that I can loop through files easily
FullPath = (ProjectData + CziName)
print(FullPath)
to open the Czi - use the czifile package¶
- czifile imports as a numpy array.
- Numpy Tutorial
import czifile
czi_array = czifile.imread(FullPath) #read the czi file.
print(czi_array.shape)
Shape¶
- shape just tells you how many dimensions the array is.
- There are a lot of dimensions and I forget what they all are (written on a sticky note in lab :(). I think the first three are like time point, scene and stuff like that that I don't generally use..
- (1-?, 1-?, 1-?, 4 - channels, 19 - slices, 678 - y plane, 678 - x plane, 1)
I use squeeze function to take out all of the unused elements
czi_array = czi_array.squeeze() #take out the dimensions that are not important
print(czi_array.shape)
Looking at the czi¶
- usually you want to look at an individual image to see what's going on.
- to do that use matplotlib - it can print out the czi image with imshow
- this stack has 20 x 5 images in it though in an array. Matplotlib only prints out one at once.
- use numpy array functions to dissect array
Pull out all images pertaining to one channel¶
Tip its 0 based meaning 0 is a channel too.
ChannelStack = czi_array[2,...]
print(ChannelStack.shape)
import matplotlib¶
import matplotlib.pyplot as plt
%matplotlib inline
pick an image in the stack and print it using matplotlib¶
in this case I picked 15.. change the numbers around to go through the different images
plt.imshow(ChannelStack[15])
Print all of the images in a stack at once!¶
There may come a time (and that time might be right now) where you would like to see all the images in this stack at once instead of typing them out manually.
I wrote out a loop that does this.
fig, axs = plt.subplots(nrows = 4, ncols = 5, figsize=(50, 50))
for ax, pln in zip(axs.flat, range(ChannelStack.shape[0])) :
ax.imshow(ChannelStack[pln])
ax.axis('off')
ax.set_title(str(pln))
plt.tight_layout()
### To save this image as a png in your directory uncomment this line
plt.savefig('200323-StackImages.png')