Skip to content

Instantly share code, notes, and snippets.

@mattcai
Created April 10, 2020 18:32
Show Gist options
  • Save mattcai/7d35eae3b369c637063ea1b2685d9eb0 to your computer and use it in GitHub Desktop.
Save mattcai/7d35eae3b369c637063ea1b2685d9eb0 to your computer and use it in GitHub Desktop.
starfish ilastik segmentation incompatible with cropped imagestacks
# %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import os
from starfish import Experiment, FieldOfView, ImageStack
from starfish.types import Axes, Levels
from starfish.image import Filter
from starfish.util.plot import imshow_plane
# load data
experiment = Experiment.from_json('https://d26bikfyahveg8.cloudfront.net/RNAScope/formatted/experiment.json')
dapi = experiment["fov_000"].get_image('nuclei')
cropped_dapi = dapi.sel({Axes.X: (195,2048), Axes.Y: (0,741)})
# preprocess dapi images, may not be necessary
def preprocess(dapi):
blur = Filter.GaussianLowPass(sigma=5)
blurred = blur.run(dapi)
clip = Filter.Clip(p_min=1, p_max=95, level_method=Levels.SCALE_BY_CHUNK)
clipped = clip.run(blurred)
return clipped
dapi_p = preprocess(cropped_dapi)
# these paths need to be set for your environment and ilastik model has to be trained first
# ilastik_exe_path = '/Applications/ilastik-1.3.3post2-OSX.app/Contents/ilastik-release/run_ilastik.sh'
# ilastik_proj_path = os.path.join('/Users/mcai/RNAScope_Vignette', 'RNAScope_pixelclass.ilp')
# create filter and run
ipp = Filter.IlastikPretrainedProbability(ilastik_executable=ilastik_exe_path, ilastik_project=ilastik_proj_path)
probabilities = ipp.run(stack=dapi_p.sel({Axes.ROUND: 0}))
# or import .h5 probabilities file that was exported with default settings in ilastik pixel classification
# probabilities = ipp.import_ilastik_probabilities(path_to_h5_file="/Users/mcai/RNAScope_Vignette/preprocessed_dapi_Probabilities_float32.h5")
# View probability map next to dapi image
f, (ax1, ax2) = plt.subplots(ncols=2)
ax1.imshow(cropped_dapi.sel({Axes.ROUND: 0}).xarray.values.squeeze())
ax1.set_title("Dapi")
ax2.imshow(probabilities.xarray.values.squeeze())
ax2.set_title("Probabilities")
f.tight_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment