diff --git a/_includes/datatypes/activities/datatypes_skimage_napari.py b/_includes/datatypes/activities/datatypes_skimage_napari.py index b71a07de..f7e9301b 100644 --- a/_includes/datatypes/activities/datatypes_skimage_napari.py +++ b/_includes/datatypes/activities/datatypes_skimage_napari.py @@ -1,19 +1,21 @@ # Import modules -import napari +from load_from_url import load_from_url +from napari.viewer import Viewer import numpy as np # Open napari -viewer = napari.Viewer() +viewer = Viewer() # Open the image # - Uncomment the image that you want to read -from skimage.io import imread -image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_noisy_different_intensity.tif') -#image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_intensity_clipping_issue_a.tif') -#image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_intensity_clipping_issue_b.tif') -#image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary__two_objects.tif') -#image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__autophagosomes.tif') -#image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__scanR_datatype_issue.tif') +image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_noisy_different_intensity.tif') +#image_url = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_intensity_clipping_issue_a.tif') +#image_url = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_intensity_clipping_issue_b.tif') +#image_url = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary__two_objects.tif') +#image_url = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__autophagosomes.tif') +#image_url = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__scanR_datatype_issue.tif') +print(image_url.dims) +image = image_url.data # View the image viewer.add_image(image) diff --git a/_includes/image_file_formats/activities/image_file_formats_skimage_napari.py b/_includes/image_file_formats/activities/image_file_formats_skimage_napari.py new file mode 100644 index 00000000..38189739 --- /dev/null +++ b/_includes/image_file_formats/activities/image_file_formats_skimage_napari.py @@ -0,0 +1,46 @@ +from load_from_url import load_from_url +from napari.viewer import Viewer + +### NOTE: xml/h5 currently not working! + +# image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_PLK1_control.tif') +# image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_xyc__two_images.lif') +# image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xyz__multiple_images.czi') +image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xyzct_16bit__mitosis.ics') +### image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xyzct_16bit__mitosis.xml') + + +# Visualize and explore available metadata +print(image_url.metadata) + +# Explore some useful metadata +# Appreciate that accessing the metadata is not always straightforward +print(image_url.dims) +print(image_url.physical_pixel_sizes) +print(image_url.metadata.images[0].pixels.time_increment) + +# Visualize the dataset in napari viewer +napari_viewer = Viewer() +napari_viewer.add_image(image_url.data, scale = image_url.physical_pixel_sizes) + +# Resave the dataset as ome tif with pixel size +from aicsimageio.writers.ome_tiff_writer import OmeTiffWriter +import numpy as np +OmeTiffWriter.save(image_url.data.astype(np.uint16), + "file.ome.tif", + dim_order="TCZYX", + physical_pixel_sizes=image_url.physical_pixel_sizes) + +# Resave dataset as gif +from aicsimageio.writers.timeseries_writer import TimeseriesWriter +OmeTiffWriter.save(image_url.data.astype(np.uint16), + "file.gif", + dim_order="TCZYX", + physical_pixel_sizes=image_url.physical_pixel_sizes) + +# Reload ome tif +from aicsimageio import AICSImage +resaved = AICSImage('file.ome.tif') + +# Appreciate that metadata has changed +print(resaved.metadata) diff --git a/_includes/lut/activities/explore_luts_skimage_napari.py b/_includes/lut/activities/explore_luts_skimage_napari.py index 3a032dbc..98501152 100644 --- a/_includes/lut/activities/explore_luts_skimage_napari.py +++ b/_includes/lut/activities/explore_luts_skimage_napari.py @@ -1,38 +1,41 @@ -# Instantiate the napari viewer -import napari -viewer = napari.Viewer() +from load_from_url import load_from_url +from napari.viewer import Viewer +import numpy as np # Read the image -from skimage.io import imread -image = imread('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_high_dynamic_range.tif') +image_url = load_from_url('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_high_dynamic_range.tif') +print(image_url.dims) +image = image_url.data # Check image type and values -import numpy as np print(image.dtype, np.min(image), np.max(image)) +# Create a new napari viewer. +napari_viewer = Viewer() # View the intensity image as grayscale -viewer.add_image(image, name='image_grayscale', colormap='gray') +napari_viewer.add_image(image, name='image_grayscale', colormap='gray') # Change brightness and contrast -viewer.layers['image_grayscale'].contrast_limits=(100, 175) +napari_viewer.layers['image_grayscale'].contrast_limits=(100, 175) # Napari GUI: explore different contrast limits # View the intensity image as grayscale -viewer.add_image(image, name='image_grayscale2', colormap='gray') +napari_viewer.add_image(image, name='image_grayscale2', colormap='gray') # Napari GUI: visualize images side by side # Napari GUI: change brightness and contrast to visualize dim nuclei # Check available colormap -print(list(napari.utils.colormaps.AVAILABLE_COLORMAPS)) +from napari.utils import colormaps +print(list(colormaps.AVAILABLE_COLORMAPS)) # Change colormap -viewer.add_image(image, name='image_turbo', colormap='turbo') +napari_viewer.add_image(image, name='image_turbo', colormap='turbo') # Napari GUI: explore the LUTs # Extract image data from the layers -image_grayscale = viewer.layers['image_grayscale'].data -image_grayscale2 = viewer.layers['image_grayscale2'].data +image_grayscale = napari_viewer.layers['image_grayscale'].data +image_grayscale2 = napari_viewer.layers['image_grayscale2'].data # Compare raw data print(image_grayscale[0:5,0:5]) print(image_grayscale2[0:5,0:5]) -print((image_grayscale == image_grayscale2).all()) +print((image_grayscale == image_grayscale2).all()) \ No newline at end of file diff --git a/_includes/pixels/activities/pixels_skimage_napari.py b/_includes/pixels/activities/pixels_skimage_napari.py index dc31cd0b..13196536 100644 --- a/_includes/pixels/activities/pixels_skimage_napari.py +++ b/_includes/pixels/activities/pixels_skimage_napari.py @@ -3,13 +3,16 @@ ## Install them into your activated conda environment. ## conda activate skimage-napari-tutorial ## pip install napari-plot-profile -## pip install matplotlib ####################################################### -from skimage.io import imread +from load_from_url import load_from_url + # Load the image. # You can also load a local image by providing the path to the file. -image = imread("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_noisy_different_intensity.tif") +image_url = load_from_url("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_noisy_different_intensity.tif") +print(image_url.dims) + +image = image_url.data # Print image shape print(image.shape) @@ -18,13 +21,13 @@ print(image) # Top left corner is [y, x] = [r, c] = [0, 0] -print(image[0, 0]) +print(image[0, 0, 0, 0, 0]) # [y, x] = [r, c] = [1, 0] -print(image[1, 0]) +print(image[0, 0, 0, 1, 0]) # [y, x] = [r, c] = [0, 2] -print(image[0, 2]) +print(image[0, 0, 0, 0, 2]) from napari.viewer import Viewer # Create a new napari viewer. diff --git a/_includes/spatial_calibration/activities/spatial_calibration_skimage_napari.py b/_includes/spatial_calibration/activities/spatial_calibration_skimage_napari.py index 3ae56b3f..e22ebe7e 100644 --- a/_includes/spatial_calibration/activities/spatial_calibration_skimage_napari.py +++ b/_includes/spatial_calibration/activities/spatial_calibration_skimage_napari.py @@ -3,26 +3,29 @@ ## installation activity for skimage napari. ####################################################### -# Import python packages. +# Import python packages +from load_from_url import load_from_url from napari.viewer import Viewer -from load_from_url import load_from_url -# Read image with AICSImage. -image = load_from_url("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xyz_8bit__mitotic_plate_calibrated.tif") +# Read image with AICSImage +image_url = load_from_url("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xyz_8bit__mitotic_plate_calibrated.tif") +image = image_url.data -# Display information about dimensions. -print(image.dims) +# Display information about dimensions +print(image_url.dims) +print(image.shape) +# Note that dims of image_url gives the same info as numpy array shape -# Display physical pixel size read from the metadata. -print(image.physical_pixel_sizes) +# Display physical pixel size read from the metadata +print(image_url.physical_pixel_sizes) # Create a new napari_viewer napari_viewer = Viewer() -# Add image with scaling to the viewer. +# Add image with scaling to the viewer napari_viewer.add_image(image.data, scale=image.physical_pixel_sizes) -# Napari GUI: Change order of visible axes. -# Napari GUI: Explor 3D visualization. +# Napari GUI: Change order of visible axes +# Napari GUI: Explor 3D visualization -# Note: As of now napari lacks funcitonality to easily measure distances between points in an open image. +# Note: As of now napari lacks funcitonality to easily measure distances between points in an open image # Currently, no orthogonal view plugin exists for napari. diff --git a/_includes/tool_installation/activities/tool_installation_napari.md b/_includes/tool_installation/activities/tool_installation_napari.md index 6a98225f..d874c059 100644 --- a/_includes/tool_installation/activities/tool_installation_napari.md +++ b/_includes/tool_installation/activities/tool_installation_napari.md @@ -1,16 +1,14 @@ -### Install skimage napari - Essentially, follow these instructions: https://alisterburt.github.io/napari-workshops/SciPy-0722/scipy_installation.html #### Installation (only once) 1. install miniconda (TODO) -1. `conda create -n skimage-napari-tutorial python=3.9` +1. `conda create -y -n skimage-napari-tutorial -c conda-forge python=3.9` 1. `conda activate skimage-napari-tutorial` -1. `conda install -c conda-forge notebook matplotlib` +1. `pip install "napari[all]"` +1. `pip install notebook matplotlib` 1. `pip install aicsimageio` 1. `conda install -c conda-forge bioformats_jar` -1. `pip install "napari[all]"` 1. `pip install napari-brightness-contrast` 1. `pip install napari-plot-profile` 1. create a directory called `skimage-napari-tutorial` (e.g. on your Desktop) diff --git a/functions/load_from_url.py b/functions/load_from_url.py index cf874d48..292a8a80 100644 --- a/functions/load_from_url.py +++ b/functions/load_from_url.py @@ -1,24 +1,46 @@ import os, tempfile -import urllib +import requests from aicsimageio import AICSImage from aicsimageio.readers import BioformatsReader -def load_from_url(url): +def load_from_url(url, verbose=False): with tempfile.TemporaryDirectory() as tempdir: # extract image name - fname = os.path.join(tempdir, url.rsplit('/', 1)[1]) - # download image from url and save it with the same name - r = urllib.request.urlretrieve(url, fname) + url_fname = url.rsplit('/', 1)[1] + file_name, file_ext = os.path.splitext(url_fname) + url_file_name, _ = os.path.splitext(url) + fname = os.path.join(tempdir, file_name+file_ext) + + if verbose: + print('Saving file\n', fname, '\nin:\n', tempdir) + + # download url file and save it with the same name + r = requests.get(url) + open(fname, "wb").write(r.content) + + # if file is ics, download corresponding ids + if file_ext=='.ics': + r = requests.get(url_file_name+'.ids') + ids_name = os.path.join(tempdir, file_name+'.ids') + open(ids_name, "wb").write(r.content) + + # if file is xml, download corresponding h5 + if file_ext=='.xml': + r = requests.get(url_file_name+'.h5') + ids_name = os.path.join(tempdir, file_name+'.h5') + open(ids_name, "wb").write(r.content) + # load image using AICSImage to retrieve metadata image = AICSImage(fname, reader=BioformatsReader) _ = image.data # this is to make sure the image is loaded into memory return image + if __name__=='__main__': url = 'https://github.com/NEUBIAS/training-resources/raw/master/image_data/xyz_8bit_calibrated__mri_full_head.tif' - image = load_from_url(url) + image = load_from_url(url, verbose=True) print(image.data) print(image.physical_pixel_sizes)