Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update config names #106

Merged
merged 41 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
dad9107
updated hashes to newer ones
yfukai Jul 29, 2022
ed4535c
changed source to main
yfukai Jul 29, 2022
7ad185a
ci experiment with dev branch
yfukai Jul 29, 2022
2b6de1e
updated branch
yfukai Jul 29, 2022
901107c
added tests for wrong argments
yfukai Aug 4, 2022
9743896
updated classifier to beta
yfukai Aug 4, 2022
0bca0c2
Merge branch 'dev' of https://github.com/peng-lab/BaSiCPy into minor_…
yfukai Aug 4, 2022
1931b5a
updated copyright
yfukai Aug 5, 2022
bda6d38
just added spaces
yfukai Aug 9, 2022
593fe22
updated downsampled data to Zenodo
yfukai Aug 9, 2022
acc685e
renamed data to datasets
yfukai Aug 9, 2022
b83d629
typo fix...
yfukai Aug 9, 2022
7443964
deleted depricated tools
yfukai Aug 9, 2022
2e6bdba
fixed package names ...
yfukai Aug 9, 2022
3e3e165
import fix
yfukai Aug 9, 2022
a56bac5
:busts_in_silhouette: Update @yfukai as a contributor
yfukai Aug 9, 2022
71dd65d
added my contribution for ci...
yfukai Aug 9, 2022
3678748
Merge branch 'update_hashes' of ssh://github.com/yfukai/BaSiCPy into …
yfukai Aug 13, 2022
c7b3ec3
Merge branch 'update_hashes' into minor_change_before_beta_release
yfukai Aug 13, 2022
8b0643e
removed deprecated routines from old DCT2d
yfukai Aug 13, 2022
e1e9947
trying to add darkfield weight and changing weight for ladmap
yfukai Oct 6, 2022
e0f8202
fixed typo
yfukai Oct 6, 2022
f8928a7
added attribute
yfukai Oct 6, 2022
380c19b
Merge branch 'dev' into dct_tools_update
yfukai Oct 29, 2022
2d8a1d0
updated tests and fixed jax version to solve problems
yfukai Oct 29, 2022
382a834
Merge branch 'dev' of https://github.com/peng-lab/BaSiCPy into update…
yfukai Oct 29, 2022
a39d1e2
Merge branch 'dct_tools_update' into update_coef_definitions
yfukai Oct 29, 2022
5814078
rewrite ladmap to normalize B
yfukai Nov 3, 2022
a3dee62
working ver?
yfukai Nov 5, 2022
987a473
updated test data
yfukai Nov 10, 2022
c766fa9
udpated weight for darkfield
yfukai Nov 10, 2022
fd6a592
updated test data
yfukai Nov 10, 2022
4826265
removed print
yfukai Nov 10, 2022
02a3e00
fixed normalization const
yfukai Nov 10, 2022
05353a5
added test to show basic does not depend on the intensity scale
yfukai Nov 10, 2022
a323989
small update
yfukai Nov 10, 2022
6174fc9
updated coefs
yfukai Nov 11, 2022
8f97fec
updated field names
yfukai Nov 11, 2022
fc148a8
Merge branch 'dev' into update_config_names
yfukai Nov 17, 2022
97d1fcc
fixed typo in log
yfukai Nov 17, 2022
0a3ce1f
updated the definition for self._sparse_cost_darkfield in approximate
yfukai Nov 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/notebooks/WSI_brain.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
"outputs": [],
"source": [
"basic = BaSiC(get_darkfield=True, lambda_flatfield_coef=10)\n",
"basic = BaSiC(get_darkfield=True, smoothness_flatfield=10)\n",
"basic.fit(images)"
]
},
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/timelapse_brightfield.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
"outputs": [],
"source": [
"basic = BaSiC(get_darkfield=True, lambda_flatfield_coef=10)\n",
"basic = BaSiC(get_darkfield=True, smoothness_flatfield=10)\n",
"basic.fit(images)"
]
},
Expand Down
6 changes: 3 additions & 3 deletions docs/notebooks/timelapse_nanog.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
"outputs": [],
"source": [
"basic = BaSiC(get_darkfield=True, lambda_flatfield_coef=10)\n",
"basic = BaSiC(get_darkfield=True, smoothness_flatfield=10)\n",
"basic.fit(images)"
]
},
Expand Down Expand Up @@ -129,8 +129,8 @@
"outputs": [],
"source": [
"basic = BaSiC(\n",
" get_darkfield=True, lambda_flatfield_coef=100\n",
") # increase lambda_flatfield_coef for smoother flatfield\n",
" get_darkfield=True, smoothness_flatfield=100\n",
") # increase smoothness_flatfield for smoother flatfield\n",
"basic.fit(images)"
]
},
Expand Down
1,167 changes: 31 additions & 1,136 deletions misc_notebooks/compare_lagrangian.ipynb

Large diffs are not rendered by default.

503 changes: 8 additions & 495 deletions misc_notebooks/compare_with_reference_impl.ipynb

Large diffs are not rendered by default.

418 changes: 27 additions & 391 deletions misc_notebooks/organize_test_data.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions misc_notebooks/test_3d_fit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
"text": [
"INFO:basicpy.basicpy:Initializing BaSiC 5326684736 with parameters: \n",
"get_darkfield: False\n",
"lambda_flatfield_coef: 10\n",
"smoothness_flatfield: 10\n",
"\n",
"INFO:basicpy.basicpy:=== BaSiC fit started ===\n",
"INFO:basicpy.basicpy:reweighting iteration 0\n",
Expand All @@ -262,7 +262,7 @@
}
],
"source": [
"basic = BaSiC(get_darkfield=False, lambda_flatfield_coef=10)\n",
"basic = BaSiC(get_darkfield=False, smoothness_flatfield=10)\n",
"basic.fit(images)"
]
},
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ project_urls =
[options]
packages = find:
install_requires =
jax
jaxlib>=0.3.10 # to import jaxlib.xla_extension.XlaRuntimeError
jax>=0.3.10,<=0.3.23
jaxlib>=0.3.10,<=0.3.23 # to import jaxlib.xla_extension.XlaRuntimeError
numpy
pooch
pydantic>=1.9.1
Expand Down
62 changes: 45 additions & 17 deletions src/basicpy/_jax_routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class BaseFit(BaseModel):
1e-6,
description="Optimization tolerance for update diff.",
)
lambda_darkfield: float = Field(
smoothness_darkfield: float = Field(
0.0,
description="Darkfield smoothness weight for sparse reguralization.",
)
lambda_darkfield_sparse: float = Field(
sparse_cost_darkfield: float = Field(
0.0,
description="Darkfield sparseness weight for sparse reguralization.",
)
lambda_flatfield: float = Field(
smoothness_flatfield: float = Field(
0.0,
description="Flatfield smoothness weight for sparse reguralization.",
)
Expand Down Expand Up @@ -88,6 +88,7 @@ def _fit_jit(
self,
Im,
W,
W_D,
S,
D_R,
D_Z,
Expand All @@ -105,6 +106,7 @@ def _fit_jit(
self._step,
Im,
W,
W_D,
)
# while self._cond(vals):
# vals = step(vals)
Expand Down Expand Up @@ -149,6 +151,7 @@ def fit(
self,
Im,
W,
W_D,
S,
D_R,
D_Z,
Expand All @@ -167,7 +170,11 @@ def fit(
raise ValueError("I_R must have the same shape as images.shape")
if W.shape != Im.shape:
raise ValueError("weight must have the same shape as images.shape")
return self._fit_jit(Im, W, S, D_R, D_Z, B, I_R)
if W_D.shape != Im.shape[1:]:
raise ValueError(
"darkfield weight must have the same shape as images.shape[1:]"
)
return self._fit_jit(Im, W, W_D, S, D_R, D_Z, B, I_R)

def fit_baseline(
self,
Expand Down Expand Up @@ -208,25 +215,28 @@ def _step(
self,
Im,
weight,
dark_weight,
vals,
):
k, S, D_R, D_Z, I_R, B, Y, mu, fit_residual, value_diff = vals
T_max = Im.shape[0]

I_B = S[newax, ...] * B[:, newax, newax, newax] + D_R[newax, ...] + D_Z
eta_S = jnp.sum(B**2) * 1.02
eta_S = jnp.sum(B**2) * 1.02 + 0.01
S_new = (
S
+ jnp.sum(B[:, newax, newax, newax] * (Im - I_B - I_R + Y / mu), axis=0)
/ eta_S
)
S_new = idct3d(_jshrinkage(dct3d(S_new), self.lambda_flatfield / (eta_S * mu)))
mean_S = jnp.mean(S_new)
S_new = jnp.where(mean_S > 0, S_new / mean_S, S_new)
B = jnp.where(mean_S > 0, B * mean_S, B)
S_new = idct3d(
_jshrinkage(dct3d(S_new), self.smoothness_flatfield / (eta_S * mu))
)
S_new = jnp.where(S_new.min() < 0, S_new - S_new.min(), S_new)
dS = S_new - S
S = S_new

I_B = S[newax, ...] * B[:, newax, newax, newax] + D_R[newax, ...] + D_Z
I_R_new = _jshrinkage(Im - I_B + Y / mu, weight / mu)
I_R_new = _jshrinkage(Im - I_B + Y / mu, weight / mu / T_max)
dI_R = I_R_new - I_R
I_R = I_R_new

Expand All @@ -235,6 +245,11 @@ def _step(
B_new = jnp.sum(S[newax, ...] * (R + Y / mu), axis=(1, 2, 3)) / S_sq
B_new = jnp.where(S_sq > 0, B_new, B)
B_new = jnp.maximum(B_new, 0)

mean_B = jnp.mean(B_new)
B_new = jnp.where(mean_B > 0, B_new / mean_B, B_new)
S = jnp.where(mean_B > 0, S * mean_B, S)

dB = B_new - B
B = B_new

Expand All @@ -250,9 +265,11 @@ def _step(
Im - BS - D_R[newax, ...] - D_Z - I_R + Y / mu, axis=0
)
D_R_new = idct3d(
_jshrinkage(dct3d(D_R_new), self.lambda_darkfield / eta_D / mu)
_jshrinkage(dct3d(D_R_new), self.smoothness_darkfield / eta_D / mu)
)
D_R_new = _jshrinkage(
D_R_new, self.sparse_cost_darkfield * dark_weight / eta_D / mu
)
D_R_new = _jshrinkage(D_R_new, self.lambda_darkfield_sparse / eta_D / mu)
dD_R = D_R_new - D_R
D_R = D_R_new

Expand Down Expand Up @@ -290,8 +307,10 @@ def _step(
@jit
def _step_only_baseline(self, Im, weight, S, D, vals):
k, I_R, B, Y, mu, fit_residual, value_diff = vals
T_max = Im.shape[0]

I_B = S[newax, ...] * B[:, newax, newax, newax] + D[newax, ...]
I_R_new = _jshrinkage(Im - I_B + Y / mu, weight / mu)
I_R_new = _jshrinkage(Im - I_B + Y / mu, weight / mu / T_max)
dI_R = I_R_new - I_R
I_R = I_R_new

Expand Down Expand Up @@ -321,9 +340,14 @@ def _step_only_baseline(self, Im, weight, S, D, vals):
return (k + 1, I_R, B, Y, mu, fit_residual, value_diff)

def calc_weights(self, I_B, I_R):
return jnp.ones_like(I_R, dtype=jnp.float32) / (
Ws = jnp.ones_like(I_R, dtype=jnp.float32) / (
jnp.abs(I_R / (I_B + self.epsilon)) + self.epsilon
)
return Ws / jnp.mean(Ws)

def calc_dark_weights(self, D_R):
Ws = np.ones_like(D_R, dtype=jnp.float32) / (jnp.abs(D_R) + self.epsilon)
return Ws / jnp.mean(Ws)

def calc_weights_baseline(self, I_B, I_R):
return self.calc_weights(I_B, I_R)
Expand All @@ -342,6 +366,7 @@ def _step(
self,
Im,
weight,
dark_weight,
vals,
):
k, S, D_R, D_Z, I_R, B, Y, mu, fit_residual, value_diff = vals
Expand All @@ -362,7 +387,7 @@ def _step(
# print(type(temp_W))
temp_W = jnp.mean(temp_W, axis=0)
S_hat = S_hat + dct2d(temp_W)
S_hat = _jshrinkage(S_hat, self.lambda_flatfield / (self._ent1 * mu))
S_hat = _jshrinkage(S_hat, self.smoothness_flatfield / (self._ent1 * mu))
S = idct2d(S_hat)
I_B = S[newax, ...] * B[:, newax, newax] + D_R[newax, ...] + D_Z
I_R = (Im - I_B + Y / mu) / self._ent1
Expand Down Expand Up @@ -407,9 +432,9 @@ def _step(

# smooth A_offset
D_R = dct2d(D_R)
D_R = _jshrinkage(D_R, self.lambda_darkfield / (self._ent2 * mu))
D_R = _jshrinkage(D_R, self.smoothness_darkfield / (self._ent2 * mu))
D_R = idct2d(D_R)
D_R = _jshrinkage(D_R, self.lambda_darkfield_sparse / (self._ent2 * mu))
D_R = _jshrinkage(D_R, self.sparse_cost_darkfield / (self._ent2 * mu))
D_R = D_R + Z
fit_residual = R - I_B
Y = Y + mu * fit_residual
Expand Down Expand Up @@ -463,6 +488,9 @@ def calc_weights(self, I_B, I_R):
weight = weight / jnp.mean(weight)
return weight[:, newax, ...]

def calc_dark_weights(self, D_R):
return jnp.ones_like(D_R)

def calc_weights_baseline(self, I_B, I_R):
I_B = I_B[:, 0, ...]
I_R = I_R[:, 0, ...]
Expand Down
Loading