Skip to content

Commit

Permalink
fix: skip experiments pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Feb 2, 2025
1 parent dff8510 commit f8671cc
Showing 1 changed file with 87 additions and 25 deletions.
112 changes: 87 additions & 25 deletions experiments/seathru/dbscan_seathru.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#%%
# pylint: skip-file


import itertools as it

# %%
from pathlib import Path
import matplotlib.pyplot as plt
from typing import Tuple

import cv2
import matplotlib.pyplot as plt
import numpy as np
import skimage
from PIL import Image
from sklearn.cluster import KMeans, DBSCAN
from scipy.optimize import minimize
from typing import Tuple
import itertools as it
from skimage.util import img_as_ubyte
import skimage
from sklearn.cluster import DBSCAN, KMeans

# %%
# name = "T_S04856"
name = "T_S04923"
Expand All @@ -18,12 +24,17 @@
tif_file = Path(f"../../data/seathru/D3/D3/depth/depth{name}.tif")
# %%
clusters = 10


# %%
def uint8_2_double(array: np.ndarray):
return array.astype(np.float64) / 255.0


def double_2_uint8(array: np.ndarray):
return (array * 255).astype(np.uint8)


# %%
def imshow(img: np.ndarray, color_channel="rgb"):
if img.dtype == np.float64 and len(img.shape) == 3:
Expand All @@ -34,10 +45,12 @@ def imshow(img: np.ndarray, color_channel="rgb"):
elif color_channel == "hsv":
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)

#plt.imshow(img)
# plt.imshow(img)

#if len(img.shape) == 2:
# if len(img.shape) == 2:
# plt.colorbar()


# %%
png = uint8_2_double(cv2.imread(png_file))
height, width, _ = png.shape
Expand All @@ -48,7 +61,7 @@ def imshow(img: np.ndarray, color_channel="rgb"):
# %%
tif_image = Image.open(tif_file)
tif = np.array(tif_image)
tif = cv2.resize(tif, (width, height), interpolation = cv2.INTER_NEAREST)
tif = cv2.resize(tif, (width, height), interpolation=cv2.INTER_NEAREST)

imshow(tif)
# %%
Expand All @@ -60,10 +73,12 @@ def imshow(img: np.ndarray, color_channel="rgb"):

tif_flat.shape
# %%
kmeans = KMeans(n_clusters=clusters, random_state=0, n_init="auto").fit(tif_flat[tif_flat != 0].reshape(-1, 1))
kmeans = KMeans(n_clusters=clusters, random_state=0, n_init="auto").fit(
tif_flat[tif_flat != 0].reshape(-1, 1)
)
labels = np.zeros(tif_flat.shape, dtype=np.uint8)
labels[tif_flat != 0] = kmeans.labels_
labels[tif_flat == 0] = 255 # Use 255 to represent nan
labels[tif_flat == 0] = 255 # Use 255 to represent nan
means = np.array(kmeans.cluster_centers_).flatten()
means_args = np.argsort(means)

Expand Down Expand Up @@ -92,7 +107,9 @@ def imshow(img: np.ndarray, color_channel="rgb"):
g_filtered = g[mask].flatten()
r_filtered = r[mask].flatten()

pixels = np.array([[b,g,r] for b,g,r in zip(b_filtered, g_filtered , r_filtered)])
pixels = np.array(
[[b, g, r] for b, g, r in zip(b_filtered, g_filtered, r_filtered)]
)
idx = np.nonzero(np.all(pixels <= np.percentile(pixels, 1, axis=0), axis=1))
selected_pixels = pixels[idx]

Expand All @@ -110,45 +127,81 @@ def imshow(img: np.ndarray, color_channel="rgb"):
tif.min(), tif.max()
# %%
z_values


# %%
def estimate_backscatter(B_inf: float, beta_B: float, J_prime: float, beta_D_prime: float, z):
return B_inf * (1 - np.exp(- beta_B * z)) + (J_prime * np.exp(- beta_D_prime * z))
def estimate_backscatter(
B_inf: float, beta_B: float, J_prime: float, beta_D_prime: float, z
):
return B_inf * (1 - np.exp(-beta_B * z)) + (J_prime * np.exp(-beta_D_prime * z))


# %%
def optimize_estimate_backscatter(arguments: Tuple[float, float, float, float], B_hat: np.ndarray, z: np.ndarray):
def optimize_estimate_backscatter(
arguments: Tuple[float, float, float, float], B_hat: np.ndarray, z: np.ndarray
):
B_inf, beta_B, J_prime, beta_D_prime = arguments

return np.sum((B_hat - estimate_backscatter(B_inf, beta_B, J_prime, beta_D_prime, z)) ** 2)
return np.sum(
(B_hat - estimate_backscatter(B_inf, beta_B, J_prime, beta_D_prime, z)) ** 2
)


# %%
result = minimize(optimize_estimate_backscatter, [1, 1, 1, 1], args=(dark_pixels[:, 0], z_values), bounds=[(0,1), (0,5), (0,1), (0,5)], method="SLSQP")
result = minimize(
optimize_estimate_backscatter,
[1, 1, 1, 1],
args=(dark_pixels[:, 0], z_values),
bounds=[(0, 1), (0, 5), (0, 1), (0, 5)],
method="SLSQP",
)
b_args = result.x

b_args
# %%
optimize_estimate_backscatter(b_args, dark_pixels[:, 0], z_values)
# %%
result = minimize(optimize_estimate_backscatter, [1, 1, 1, 1], args=(dark_pixels[:, 1], z_values), bounds=[(0,1), (0,5), (0,1), (0,5)], method="SLSQP")
result = minimize(
optimize_estimate_backscatter,
[1, 1, 1, 1],
args=(dark_pixels[:, 1], z_values),
bounds=[(0, 1), (0, 5), (0, 1), (0, 5)],
method="SLSQP",
)
g_args = result.x

g_args
# %%
optimize_estimate_backscatter(g_args, dark_pixels[:, 1], z_values)
# %%
result = minimize(optimize_estimate_backscatter, [1, 1, 1, 1], args=(dark_pixels[:, 2], z_values), bounds=[(0,1), (0,5), (0,1), (0,5)], method="SLSQP")
result = minimize(
optimize_estimate_backscatter,
[1, 1, 1, 1],
args=(dark_pixels[:, 2], z_values),
bounds=[(0, 1), (0, 5), (0, 1), (0, 5)],
method="SLSQP",
)
r_args = result.x

r_args
# %%
optimize_estimate_backscatter(r_args, dark_pixels[:, 2], z_values)
# %%
B_b = estimate_backscatter(b_args[0], b_args[1], b_args[2], b_args[3], tif_flat).reshape((height, width))
B_b = estimate_backscatter(
b_args[0], b_args[1], b_args[2], b_args[3], tif_flat
).reshape((height, width))

imshow(B_b)
# %%
B_g = estimate_backscatter(g_args[0], g_args[1], g_args[2], g_args[3], tif_flat).reshape((height, width))
B_g = estimate_backscatter(
g_args[0], g_args[1], g_args[2], g_args[3], tif_flat
).reshape((height, width))

imshow(B_g)
# %%
B_r = estimate_backscatter(r_args[0], r_args[1], r_args[2], r_args[3], tif_flat).reshape((height, width))
B_r = estimate_backscatter(
r_args[0], r_args[1], r_args[2], r_args[3], tif_flat
).reshape((height, width))

imshow(B_r)
# %%
Expand Down Expand Up @@ -189,7 +242,12 @@ def optimize_estimate_backscatter(arguments: Tuple[float, float, float, float],

epsilon
# %%
X = np.array([(int(y), int(x), tif[y, x]) for y, x in it.product(np.arange(height), np.arange(width))])
X = np.array(
[
(int(y), int(x), tif[y, x])
for y, x in it.product(np.arange(height), np.arange(width))
]
)

X
# %%
Expand All @@ -200,17 +258,21 @@ def optimize_estimate_backscatter(arguments: Tuple[float, float, float, float],
tif_rebuilt[int(X[i, 0]), int(X[i, 1])] = X[i, 2]

imshow(tif_rebuilt)


# %%
def metric(a: np.ndarray, b: np.ndarray):
if np.linalg.norm(a[:2] - b[:2]) > 1:
return np.inf

return np.linalg.norm(a[2] - b[2])


# %%
print("Coffee Time")
dbscan = DBSCAN(eps=epsilon, min_samples=2, n_jobs=-1, metric=metric).fit(X)

dbscan.labels_
# %%
print("No more coffee")
np.savez_compressed("dbscan", dbscan.labels_)
np.savez_compressed("dbscan", dbscan.labels_)

0 comments on commit f8671cc

Please sign in to comment.