Skip to content

Commit

Permalink
Use Matplotlib OO style throughout doc/examples/applications (#7346)
Browse files Browse the repository at this point in the history
Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
Co-authored-by: Lars Grüter <lagru+github@mailbox.org>
  • Loading branch information
3 people committed Mar 21, 2024
1 parent f703f11 commit e453a53
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 94 deletions.
2 changes: 1 addition & 1 deletion doc/examples/applications/plot_3d_image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

def show_plane(ax, plane, cmap="gray", title=None):
ax.imshow(plane, cmap=cmap)
ax.axis("off")
ax.set_axis_off()

if title:
ax.set_title(title)
Expand Down
22 changes: 11 additions & 11 deletions doc/examples/applications/plot_coins_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

fig, axes = plt.subplots(1, 2, figsize=(8, 3))
axes[0].imshow(coins, cmap=plt.cm.gray)
axes[0].axis('off')
axes[0].set_axis_off()
axes[1].plot(hist_centers, hist, lw=2)
axes[1].set_title('histogram of gray values')

Expand All @@ -42,9 +42,9 @@
axes[1].set_title('coins > 150')

for a in axes:
a.axis('off')
a.set_axis_off()

plt.tight_layout()
fig.tight_layout()

######################################################################
# Edge-based segmentation
Expand All @@ -61,7 +61,7 @@
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(edges, cmap=plt.cm.gray)
ax.set_title('Canny detector')
ax.axis('off')
ax.set_axis_off()

######################################################################
# These contours are then filled using mathematical morphology.
Expand All @@ -73,7 +73,7 @@
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(fill_coins, cmap=plt.cm.gray)
ax.set_title('filling the holes')
ax.axis('off')
ax.set_axis_off()


######################################################################
Expand All @@ -87,7 +87,7 @@
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(coins_cleaned, cmap=plt.cm.gray)
ax.set_title('removing small objects')
ax.axis('off')
ax.set_axis_off()

######################################################################
# However, this method is not very robust, since contours that are not
Expand All @@ -107,7 +107,7 @@
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(elevation_map, cmap=plt.cm.gray)
ax.set_title('elevation map')
ax.axis('off')
ax.set_axis_off()

######################################################################
# Next we find markers of the background and the coins based on the extreme
Expand All @@ -120,7 +120,7 @@
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(markers, cmap=plt.cm.nipy_spectral)
ax.set_title('markers')
ax.axis('off')
ax.set_axis_off()

######################################################################
# Finally, we use the watershed transform to fill regions of the elevation
Expand All @@ -132,7 +132,7 @@
fig, ax = plt.subplots(figsize=(4, 3))
ax.imshow(segmentation_coins, cmap=plt.cm.gray)
ax.set_title('segmentation')
ax.axis('off')
ax.set_axis_off()

######################################################################
# This last method works even better, and the coins can be segmented and
Expand All @@ -150,8 +150,8 @@
axes[1].imshow(image_label_overlay)

for a in axes:
a.axis('off')
a.set_axis_off()

plt.tight_layout()
fig.tight_layout()

plt.show()
10 changes: 5 additions & 5 deletions doc/examples/applications/plot_colocalization_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@
a.set_axis_off()

# plot pixel intensity scatter
plt.figure()
plt.scatter(proteinA, proteinB)
plt.title('Pixel intensity')
plt.xlabel('Protein A intensity')
plt.ylabel('Protein B intensity')
fig, ax = plt.subplots()
ax.scatter(proteinA, proteinB)
ax.set_title('Pixel intensity')
ax.set_xlabel('Protein A intensity')
ax.set_ylabel('Protein B intensity')

#####################################################################
# The intensities look linearly correlated so Pearson's Correlation Coefficient
Expand Down
4 changes: 2 additions & 2 deletions doc/examples/applications/plot_cornea_spot_inpainting.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def plot_comparison(plot1, plot2, title1, title2):

mask_0 = image_med < thresh_0

plot_comparison(mask_0, mask_2, "No offset", "offset = 15")
plot_comparison(mask_0, mask_2, "No offset", "Offset = 15")

#####################################################################
# Remove fine-grained features
Expand Down Expand Up @@ -184,7 +184,7 @@ def plot_comparison(plot1, plot2, title1, title2):
# Next, we can make the detected areas wider by applying a dilation filter:

mask_dilate = ski.morphology.dilation(mask_open, footprint)
plot_comparison(mask_open, mask_dilate, "before", "after dilation")
plot_comparison(mask_open, mask_dilate, "Before", "After dilation")

#####################################################################
# Dilation enlarges bright regions and shrinks dark regions.
Expand Down
11 changes: 5 additions & 6 deletions doc/examples/applications/plot_face_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
``min_neighbor_number`` and ``intersection_score_threshold`` parameters are
made to cluster the excessive detections of the same face and to filter out
false detections. True faces usually has a lot of detections around them and
false detections. True faces usually have a lot of detections around them and
false ones usually have single detection. First algorithm searches for
clusters: two rectangle detections are placed in the same cluster if the
intersection score between them is larger then
``intersection_score_threshold``. The intersection score is computed using the
equation (intersection area) / (small rectangle ratio). The described
intersection criteria was chosen over intersection over union to avoid a corner
case when small rectangle inside of a big one have small intersection score.
case when small rectangle inside of a big one has small intersection score.
Then each cluster is thresholded using ``min_neighbor_number`` parameter which
leaves the clusters that have a same or bigger number of detections in them.
Expand Down Expand Up @@ -80,12 +80,11 @@
img=img, scale_factor=1.2, step_ratio=1, min_size=(60, 60), max_size=(123, 123)
)

plt.imshow(img)
img_desc = plt.gca()
plt.set_cmap('gray')
fig, ax = plt.subplots()
ax.imshow(img, cmap='gray')

for patch in detected:
img_desc.add_patch(
ax.axes.add_patch(
patches.Rectangle(
(patch['c'], patch['r']),
patch['width'],
Expand Down
16 changes: 8 additions & 8 deletions doc/examples/applications/plot_human_mitosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
fig, ax = plt.subplots(ncols=2, figsize=(10, 5))
ax[0].imshow(image)
ax[0].set_title('Original')
ax[0].axis('off')
ax[0].set_axis_off()
ax[1].imshow(regions)
ax[1].set_title('Multi-Otsu thresholding')
ax[1].axis('off')
ax[1].set_axis_off()
plt.show()

#####################################################################
Expand Down Expand Up @@ -111,13 +111,13 @@
fig, ax = plt.subplots(ncols=3, figsize=(15, 5))
ax[0].imshow(image)
ax[0].set_title('Original')
ax[0].axis('off')
ax[0].set_axis_off()
ax[2].imshow(cells)
ax[2].set_title('All nuclei?')
ax[2].axis('off')
ax[2].set_axis_off()
ax[1].imshow(dividing)
ax[1].set_title('Dividing nuclei?')
ax[1].axis('off')
ax[1].set_axis_off()
plt.show()

#####################################################################
Expand Down Expand Up @@ -147,7 +147,7 @@
fig, ax = plt.subplots(figsize=(5, 5))
ax.imshow(binary_smoother_dividing)
ax.set_title('Dividing nuclei')
ax.axis('off')
ax.set_axis_off()
plt.show()

#####################################################################
Expand Down Expand Up @@ -179,10 +179,10 @@
fig, ax = plt.subplots(ncols=2, figsize=(10, 5))
ax[0].imshow(cells, cmap='gray')
ax[0].set_title('Overlapping nuclei')
ax[0].axis('off')
ax[0].set_axis_off()
ax[1].imshow(color.label2rgb(segmented_cells, bg_label=0))
ax[1].set_title('Segmented nuclei')
ax[1].axis('off')
ax[1].set_axis_off()
plt.show()

#####################################################################
Expand Down
25 changes: 16 additions & 9 deletions doc/examples/applications/plot_image_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
ax1.set_title('Equalized')
ax2.imshow(comp_equalized, cmap='gray')
ax2.set_title('Checkerboard comparison')

for a in (ax0, ax1, ax2):
a.axis('off')
plt.tight_layout()
plt.plot()
a.set_axis_off()

fig.tight_layout()


######################################################################
# Diff
Expand All @@ -70,10 +72,12 @@
ax1.set_title('Rotated')
ax2.imshow(diff_rotated, cmap='gray')
ax2.set_title('Diff comparison')

for a in (ax0, ax1, ax2):
a.axis('off')
plt.tight_layout()
plt.plot()
a.set_axis_off()

fig.tight_layout()


######################################################################
# Blend
Expand All @@ -94,7 +98,10 @@
ax1.set_title('Rotated')
ax2.imshow(blend_rotated, cmap='gray')
ax2.set_title('Blend comparison')

for a in (ax0, ax1, ax2):
a.axis('off')
plt.tight_layout()
plt.plot()
a.set_axis_off()

fig.tight_layout()

plt.show()
12 changes: 6 additions & 6 deletions doc/examples/applications/plot_morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def plot_comparison(original, filtered, filter_name):
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True)
ax1.imshow(original, cmap=plt.cm.gray)
ax1.set_title('original')
ax1.axis('off')
ax1.set_axis_off()
ax2.imshow(filtered, cmap=plt.cm.gray)
ax2.set_title(filter_name)
ax2.axis('off')
ax2.set_axis_off()


######################################################################
Expand Down Expand Up @@ -102,10 +102,10 @@ def plot_comparison(original, filtered, filter_name):
# that are *smaller* than the structuring element are removed. The dilation
# operation that follows ensures that light regions that are *larger* than
# the structuring element retain their original size. Notice how the light
# and dark shapes in the center their original thickness but the 3 lighter
# and dark shapes in the center retain their original thickness but the 3 lighter
# patches in the bottom get completely eroded. The size dependence is
# highlighted by the outer white ring: The parts of the ring thinner than the
# structuring element were completely erased, while the thicker region at the
# structuring element are completely erased, while the thicker region at the
# top retains its original thickness.
#
# Closing
Expand All @@ -125,12 +125,12 @@ def plot_comparison(original, filtered, filter_name):
plot_comparison(phantom, closed, 'closing')

######################################################################
# Since ``closing`` an image starts with an dilation operation, dark regions
# Since ``closing`` an image starts with a dilation operation, dark regions
# that are *smaller* than the structuring element are removed. The dilation
# operation that follows ensures that dark regions that are *larger* than the
# structuring element retain their original size. Notice how the white
# ellipses at the bottom get connected because of dilation, but other dark
# region retain their original sizes. Also notice how the crack we added is
# regions retain their original sizes. Also notice how the crack we added is
# mostly removed.
#
# White tophat
Expand Down
4 changes: 2 additions & 2 deletions doc/examples/applications/plot_pixel_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
_, ax = plt.subplots()
ax.imshow(color.label2rgb(labeled, retina))
ax.set_axis_off()
_ = ax.set_title('thresholded vesselness')
_ = ax.set_title('Thresholded vesselness')

###############################################################################
# Finally, we can `skeletonize <skimage.morphology.skeletonize>` this label
Expand All @@ -90,7 +90,7 @@
ax.scatter(centroid[1], centroid[0], label='centroid')
ax.legend()
ax.set_axis_off()
ax.set_title('vessel graph center vs centroid')
ax.set_title('Vessel graph center vs centroid')
# sphinx_gallery_thumbnail_number = 4

plt.show()

0 comments on commit e453a53

Please sign in to comment.