Skip to content

Commit

Permalink
Removing deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Dec 6, 2023
1 parent 5b31667 commit 84ec778
Show file tree
Hide file tree
Showing 22 changed files with 1,527 additions and 1,826 deletions.
2 changes: 1 addition & 1 deletion docs/cpp_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The relative distance of each pixel of the ROI.
* :download:`GooseEYE.hpp <../include/GooseEYE/GooseEYE.hpp>`
* :ref:`Example <theory_heightheight>`.

GooseEYE::Clusters
GooseEYE::clusters
------------------

Get clusters.
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/W2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main()
prrng::pcg32 rng(0);
rowmat += rng.normal({N * N}, 0.0, (double)(M) / (double)(N));
colmat += rng.normal({N * N}, 0.0, (double)(M) / (double)(N));
auto dr = rng.random({N * N}) * 2.0 + 0.1;
auto dr = rng.normal({N * N}, 0.0, 2.0) + 0.1;
r = r * dr;

// generate image
Expand Down
20 changes: 15 additions & 5 deletions docs/examples/W2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,30 @@ int main()
W = xt::where(xt::equal(I, 1), 0, W);

// compute individual damage clusters and their centers
GooseEYE::Clusters Clusters(W);
auto clusters = Clusters.labels();
auto centers = Clusters.centers();
auto labels = GooseEYE::clusters(W);
auto names = xt::unique(labels);
auto cpos = GooseEYE::labels_centers(labels, names);
auto centers = xt::zeros_like(labels);
int m = static_cast<int>(labels.shape(0));
int n = static_cast<int>(labels.shape(1));
for (size_t i = 0; i < names.size(); ++i) {
auto row = (int)round(cpos(i, 0));
auto col = (int)round(cpos(i, 1));
row = row < 0 ? 0 : (row >= m ? m - 1 : row);
col = col < 0 ? 0 : (col >= n ? n - 1 : col);
centers(row, col) = names(i);
}

// weighted correlation
auto WI = GooseEYE::W2({101, 101}, W, I, W);

// collapsed weighted correlation
auto WIc = GooseEYE::W2c({101, 101}, clusters, centers, I, W);
auto WIc = GooseEYE::W2c({101, 101}, labels, centers, I, W);

// check against previous versions
H5Easy::File data("W2c.h5", H5Easy::File::ReadOnly);
MYASSERT(xt::all(xt::equal(I, H5Easy::load<decltype(I)>(data, "I"))));
MYASSERT(xt::all(xt::equal(clusters, H5Easy::load<decltype(clusters)>(data, "clusters"))));
MYASSERT(xt::all(xt::equal(labels, H5Easy::load<decltype(labels)>(data, "labels"))));
MYASSERT(xt::all(xt::equal(centers, H5Easy::load<decltype(centers)>(data, "centers"))));
MYASSERT(xt::all(xt::equal(W, H5Easy::load<decltype(W)>(data, "W"))));
MYASSERT(xt::allclose(WI, H5Easy::load<decltype(WI)>(data, "WI")));
Expand Down
Binary file modified docs/examples/W2c.h5
Binary file not shown.
22 changes: 14 additions & 8 deletions docs/examples/W2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@
W[img == 1] = 0

# compute individual damage clusters and their centers
Clusters = GooseEYE.Clusters(W)
clusters = Clusters.labels()
centers = Clusters.centers()
labels = GooseEYE.clusters(W)
names = np.unique(labels)[1:]
cpos = GooseEYE.labels_centers(labels, names)
cpos = np.rint(cpos).astype(int)
cpos[:, 0] = np.clip(cpos[:, 0], 0, labels.shape[0] - 1)
cpos[:, 1] = np.clip(cpos[:, 1], 0, labels.shape[1] - 1)
index = np.ravel_multi_index(cpos.T, labels.shape)
centers = np.zeros_like(labels)
centers.flat[index] = names

# weighted correlation
WI = GooseEYE.W2((101, 101), W, img, fmask=W)

# collapsed weighted correlation
WIc = GooseEYE.W2c((101, 101), clusters, centers, img, fmask=W)
WIc = GooseEYE.W2c((101, 101), labels, centers, img, fmask=W)
# </snippet>

if __name__ == "__main__":
Expand All @@ -58,7 +64,7 @@

with h5py.File(root / "W2c.h5", "w") as file:
file["I"] = img
file["clusters"] = clusters
file["labels"] = labels
file["centers"] = centers
file["W"] = W
file["WI"] = WI
Expand All @@ -69,7 +75,7 @@

with h5py.File(root / "W2c.h5") as file:
assert np.all(np.equal(file["I"][...], img))
assert np.all(np.equal(file["clusters"][...], clusters))
assert np.all(np.equal(file["labels"][...], labels))
assert np.all(np.equal(file["centers"][...], centers))
assert np.all(np.equal(file["W"][...], W))
assert np.allclose(file["WI"][...], WI)
Expand All @@ -90,7 +96,7 @@
np.array([[0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0]], dtype="float64")
)

fig, axes = plt.subplots(figsize=(18, 6), nrows=1, ncols=3)
fig, axes = plt.subplots(figsize=(20, 6), nrows=1, ncols=3, constrained_layout=True)

# ---

Expand Down Expand Up @@ -152,5 +158,5 @@
cbar.set_ticks([-phi, 0, +phi])
cbar.set_ticklabels([r"$-\varphi$", "0", r"$+\varphi$"])

fig.savefig(root / "W2c.svg")
fig.savefig(root / "W2c.svg", bbox_inches="tight")
plt.close(fig)
372 changes: 186 additions & 186 deletions docs/examples/W2c.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/examples/clusters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ int main()
auto I = GooseEYE::dummy_circles({100, 100}, true);

// clusters
auto clusters = GooseEYE::clusters(I, false);
auto labels = GooseEYE::clusters(I, false);

// clusters, if the image is periodic
auto clusters_periodic = GooseEYE::clusters(I, true);
auto labels_periodic = GooseEYE::clusters(I, true);

return 0;
}
64 changes: 29 additions & 35 deletions docs/examples/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
img = GooseEYE.dummy_circles((500, 500), periodic=True)

# clusters
clusters = GooseEYE.clusters(img, periodic=False)
labels = GooseEYE.clusters(img, periodic=False)

# clusters, if the image is periodic
clusters_periodic = GooseEYE.clusters(img, periodic=True)
labels_periodic = GooseEYE.clusters(img, periodic=True)
# </snippet>

if __name__ == "__main__":
Expand All @@ -30,57 +30,51 @@

with h5py.File(root / "clusters.h5", "w") as file:
file["I"] = img
file["clusters"] = clusters
file["clusters_periodic"] = clusters_periodic
file["clusters"] = labels
file["clusters_periodic"] = labels_periodic

if args.check:
import h5py

with h5py.File(root / "clusters.h5") as file:
assert np.all(np.equal(file["I"][...], img))
assert np.all(np.equal(file["clusters"][...], clusters))
assert np.all(np.equal(file["clusters_periodic"][...], clusters_periodic))
assert np.all(np.equal(file["clusters"][...], labels))
assert np.all(np.equal(file["clusters_periodic"][...], labels_periodic))

if args.plot or args.show:
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.cm as cm
import cppcolormap as cm
import prrng
from mpl_toolkits.axes_grid1 import make_axes_locatable

# color-scheme: modify such that the background is white
# N.B. for a transparent background -> 4th column == 1.
cmap = cm.jet(range(256))
names = np.unique(labels)
names_periodic = np.unique(labels_periodic)
cmap = cm.jet(names.size)
cmap[0, :3] = 1.0
cmap = mpl.colors.ListedColormap(cmap)

# reshuffle for better visualisation
assert np.all(np.diff(np.unique(clusters)) == 1)
assert np.all(np.diff(np.unique(clusters_periodic)) == 1)
assert np.unique(clusters).size >= np.unique(clusters_periodic).size
rng = prrng.pcg32()
lab = np.unique(clusters)
lab = lab[lab != 0]
new = np.copy(lab).astype(np.int64)
rng.shuffle(new)
rename = np.array(([0] + list(lab), [0] + list(new))).T
clusters = GooseEYE.labels_rename(clusters, rename)
lmap = GooseEYE.labels_map(clusters_periodic, clusters)
unq, unq_idx, unq_cnt = np.unique(lmap[:, 1], return_inverse=True, return_counts=True)
assert np.all(np.in1d(np.unique(clusters_periodic), lmap[:, 0]))
assert np.all(np.equal(np.sort(lmap[:, 0]), np.unique(lmap[:, 0])))
assert np.all(np.equal(np.sort(lmap[:, 1]), np.unique(lmap[:, 1])))
clusters_periodic = GooseEYE.labels_rename(clusters_periodic, lmap)
names = names.astype(np.int64)
index = 1 + np.arange(names.size - 1)
rng.shuffle(index)
cmap = cmap[[0] + list(index), :]

lmap = GooseEYE.labels_map(labels_periodic, labels)
assert names_periodic.size == lmap.shape[0]
assert np.unique(lmap[:, 0]).size == lmap.shape[0]
assert np.unique(lmap[:, 1]).size == lmap.shape[0]
labels_periodic = GooseEYE.labels_rename(labels_periodic, lmap)

try:
plt.style.use(["goose", "goose-latex"])
except OSError:
pass

fig, axes = plt.subplots(figsize=(8 * 4, 6), nrows=1, ncols=4)
fig, axes = plt.subplots(figsize=(8 * 3, 6), nrows=1, ncols=4, constrained_layout=True)

ax = axes[0]
im = ax.imshow(img, clim=(0, 1), cmap=mpl.colors.ListedColormap(cm.gray([0, 255])))
bw = mpl.colors.ListedColormap(np.array([[1, 1, 1, 1], [0, 0, 0, 1]]))
im = ax.imshow(img, clim=(0, 1), cmap=bw)
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
ax.set_xlim([0, 500])
Expand All @@ -94,7 +88,7 @@
cbar.set_ticks([0, 1])

ax = axes[1]
im = ax.imshow(clusters, clim=(0, np.max(clusters) + 1), cmap=cmap)
im = ax.imshow(labels, clim=(0, names.size), cmap=mpl.colors.ListedColormap(cmap))
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
ax.set_xlim([0, 500])
Expand All @@ -104,7 +98,7 @@
ax.set_title(r"clusters")

ax = axes[2]
im = ax.imshow(clusters_periodic, clim=(0, np.max(clusters_periodic) + 1), cmap=cmap)
im = ax.imshow(labels_periodic, clim=(0, names.size), cmap=mpl.colors.ListedColormap(cmap))
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
ax.set_xlim([0, 500])
Expand All @@ -115,9 +109,9 @@

ax = axes[3]
im = ax.imshow(
np.where(clusters_periodic != clusters, clusters_periodic, 0),
clim=(0, np.max(clusters_periodic) + 1),
cmap=cmap,
np.where(labels_periodic != labels, labels_periodic, 0),
clim=(0, names.size),
cmap=mpl.colors.ListedColormap(cmap),
)
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
Expand All @@ -130,5 +124,5 @@
if args.show:
plt.show()
else:
fig.savefig(root / "clusters.svg")
fig.savefig(root / "clusters.svg", bbox_inches="tight")
plt.close(fig)
336 changes: 168 additions & 168 deletions docs/examples/clusters.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions docs/examples/clusters_centers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ int main()
auto I = GooseEYE::dummy_circles({500, 500}, true);

// clusters
GooseEYE::Clusters clusters(I, false);
auto labels = clusters.labels();
auto centers = clusters.center_positions();
auto labels = GooseEYE::clusters(I, false);
auto names = xt::unique(labels);
auto centers = GooseEYE::labels_centers(labels, names, false);

// clusters, if the image is periodic
GooseEYE::Clusters clusters_periodic(I, true);
auto labels_periodic = clusters_periodic.labels();
auto centers_periodic = clusters_periodic.center_positions();
auto labels_periodic = GooseEYE::clusters(I, true);
auto names_periodic = xt::unique(labels_periodic);
auto centers_periodic = GooseEYE::labels_centers(labels_periodic, names_periodic, true);

// check against previous versions
H5Easy::File data("clusters_centers.h5", H5Easy::File::ReadOnly);
Expand Down
Binary file modified docs/examples/clusters_centers.h5
Binary file not shown.
50 changes: 28 additions & 22 deletions docs/examples/clusters_centers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
img = GooseEYE.dummy_circles((500, 500), periodic=True)

# clusters
clusters = GooseEYE.Clusters(img, periodic=False)
labels = clusters.labels()
centers = clusters.center_positions()
labels = GooseEYE.clusters(img, periodic=False)
names = np.unique(labels)
centers = GooseEYE.labels_centers(labels, names, periodic=False)

# clusters, if the image is periodic
clusters_periodic = GooseEYE.Clusters(img, periodic=True)
labels_periodic = clusters_periodic.labels()
centers_periodic = clusters_periodic.center_positions()
labels_periodic = GooseEYE.clusters(img, periodic=True)
names_periodic = np.unique(labels_periodic)
centers_periodic = GooseEYE.labels_centers(labels_periodic, names_periodic, periodic=True)
# </snippet>

if __name__ == "__main__":
Expand Down Expand Up @@ -51,19 +51,24 @@
if args.plot:
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.cm as cm
import cppcolormap as cm
import prrng
from mpl_toolkits.axes_grid1 import make_axes_locatable

lab = np.sort(np.unique(labels))[1:]
lp = np.sort(np.unique(labels_periodic))[1:]
centers = centers[lab, :]
centers_periodic = centers_periodic[lp, :]

# color-scheme: modify such that the background is white
# N.B. for a transparent background -> 4th column == 1.
cmap = cm.jet(range(256))
cmap = cm.jet(names.size)
cmap[0, :3] = 1.0
cmap = mpl.colors.ListedColormap(cmap)

rng = prrng.pcg32()
names = names.astype(np.int64)
index = 1 + np.arange(names.size - 1)
rng.shuffle(index)
cmap = cmap[[0] + list(index), :]

lmap = GooseEYE.labels_map(labels_periodic, labels)
assert names_periodic.size == lmap.shape[0]
assert np.unique(lmap[:, 0]).size == lmap.shape[0]
assert np.unique(lmap[:, 1]).size == lmap.shape[0]
labels_periodic = GooseEYE.labels_rename(labels_periodic, lmap)

try:
plt.style.use(["goose", "goose-latex"])
Expand All @@ -73,7 +78,8 @@
fig, axes = plt.subplots(figsize=(18, 6), nrows=1, ncols=3)

ax = axes[0]
im = ax.imshow(img, clim=(0, 1), cmap=mpl.colors.ListedColormap(cm.gray([0, 255])))
bw = mpl.colors.ListedColormap(np.array([[1, 1, 1, 1], [0, 0, 0, 1]]))
im = ax.imshow(img, clim=(0, 1), cmap=bw)
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
ax.set_xlim([0, 500])
Expand All @@ -87,8 +93,8 @@
cbar.set_ticks([0, 1])

ax = axes[1]
im = ax.imshow(labels, clim=(0, np.max(labels) + 1), cmap=cmap)
ax.plot(centers[:, 1], centers[:, 0], ls="none", marker="o", color="r")
im = ax.imshow(labels, clim=(0, names.size), cmap=mpl.colors.ListedColormap(cmap))
ax.plot(centers[1:, 1], centers[1:, 0], ls="none", marker="+", color="k")
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
ax.set_xlim([0, 500])
Expand All @@ -102,15 +108,15 @@
cbar.set_ticks([])

ax = axes[2]
im = ax.imshow(labels_periodic, clim=(0, np.max(labels) + 1), cmap=cmap)
ax.plot(centers_periodic[:, 1], centers_periodic[:, 0], ls="none", marker="o", color="r")
im = ax.imshow(labels_periodic, clim=(0, names.size), cmap=mpl.colors.ListedColormap(cmap))
ax.plot(centers_periodic[:, 1], centers_periodic[:, 0], ls="none", marker="+", color="k")
ax.xaxis.set_ticks([0, 500])
ax.yaxis.set_ticks([0, 500])
ax.set_xlim([0, 500])
ax.set_ylim([0, 500])
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$y$")
ax.set_title(r"clusters (periodic)")
ax.set_title(r"clusters, periodic (color-matched)")
div = make_axes_locatable(ax)
cax = div.append_axes("right", size="5%", pad=0.1)
cbar = plt.colorbar(im, cax=cax)
Expand Down
1,980 changes: 1,032 additions & 948 deletions docs/examples/clusters_centers.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/examples/clusters_dilate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main()
I(15, 16) = 1;

// clusters
auto C = GooseEYE::Clusters(I).labels();
auto C = GooseEYE::clusters(I);

// dilate
auto CD = GooseEYE::dilate(C);
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/clusters_dilate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
img[15, 16] = True

# clusters
C = GooseEYE.Clusters(img).labels()
C = GooseEYE.clusters(img)

# dilate
CD = GooseEYE.dilate(C)
Expand Down
Loading

0 comments on commit 84ec778

Please sign in to comment.