@@ -0,0 +1,2422 @@
+# IPython log file%0A%0Aimport numpy as np%0Afrom scipy import ndimage as ndi%0Aimport napari%0Afrom skimage import morphology%0Afrom skimage import measure%0Aimport pandas as pd%0A%0Aviewer = napari.Viewer()%0Aviewer.open('/Users/jni/Desktop/Cystic%5C Index.ome.tif')%0A%0Alabels = viewer.layers%5B'Cystic Index.ome'%5D.data%0Atissue = (labels == 4)  # found label by inspecting status bar on viewer%0Alayer = viewer.layers%5B'Cystic Index.ome'%5D%0A%0Asqum_per_pixel = 3.9792**2  # found by using ome-types plugin drag-n-drop%0Apixels_in_1000 = 1000 / squm_per_pixel%0Apixels_in_1000  # ~64%0A%0Atissue_plugged = morphology.remove_small_holes(tissue, area_threshold=64)%0A%0Aviewer.add_labels(tissue_plugged)  # just to see how we did%0Alabels%5Btissue_plugged%5D = 4  # now fix up original labels%0Alayer.refresh()  # update display as we've updated the labels data%0A%0A# we used many labels for cysts because they can look quite different.%0A# but now we want to only distinguish cyst vs non-cyst, so we merge them.%0Aany_cyst = (labels == 1) + (labels == 2) + (labels == 3) + (labels == 5)%0Alabels%5Bany_cyst%5D = 1%0Alayer.refresh()%0A%0A# Now we remove the cruft in the background/things that fall outside the slice%0A# of tissue. These can be seen as %22holes%22 in the background. The tissue itself%0A# is just one big hole, but it's so big that it will get filtered out by our%0A# size threshold.%0Abackground = (labels == 0)%0Abackground_filled = morphology.remove_small_holes(background, 500000)%0Aviewer.add_labels(background_filled)%0Alabels%5Bbackground_filled%5D = 0%0Alayer.refresh()%0A%0A# Now we go from cysts/no cyst to individually-labeled cysts. This is moving%0A# from *semantic segmentation* (does this pixel belong to a cyst, or not?) to%0A# *instance segmentation* (does this pixel belong to cyst number 2439?)%0Aindividual_cysts = ndi.label(labels == 1)%5B0%5D%0Aviewer.add_labels(individual_cysts)%0A# Note: editing the labels in the layer above with painting or plugins will%0A# edit the %22individual_cysts%22 array in-place, which we can then use below.%0A%0A%0A# Now, measure the properties we're interested it%0Atable = pd.DataFrame(measure.regionprops_table(%0A    individual_cysts, properties=%5B'label', 'area'%5D))%0Atable%5B'area (%C2%B5m2)'%5D = table%5B'area'%5D * squm_per_pixel%0Atable.head()  # first five rows%0Atable.describe()  # statistics about each column%0Atable.to_excel('one-image.xlsx')%0A%0A# Measure the total area of cysts in this file%0Acyst_percent = np.sum(labels == 1) / np.sum(labels != 0) * 100%0Aprint(cyst_percent)%0A%0Anapari.run()%0A

Add script for kidney cyst analysis using labels output from QuPath