Skip to content

bmti: add faster cg-based solver, remove old solvers #149

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
26 changes: 7 additions & 19 deletions dadapy/density_advanced.py
Original file line number Diff line number Diff line change
@@ -362,7 +362,6 @@ def compute_density_BMTI(
self,
delta_F_inv_cov="uncorr",
comp_log_den_err=False,
mem_efficient=False,
alpha=1,
log_den=None,
log_den_err=None,
@@ -384,8 +383,6 @@ def compute_density_BMTI(
finding the approximate diagonal inverse which multiplied by C gives the least-squares closest
matrix to the identity in the Frobenius norm
comp_log_den_err (bool): if True, compute the error on the BMTI estimates. Can be highly time consuming
mem_efficient (bool): if True, use a sparse matrice to solve BMTI linear system (slower). If False, use a
dense NxN matrix; this is faster, but can require a great amount of memory if the system is large.
alpha (float): can take values from 0.0 to 1.0. Indicates the portion of BMTI in the sum of the likelihoods
alpha*L_BMTI + (1-alpha)*L_kstarNN. Setting alpha=1.0 corresponds to not reguarising BMTI.
log_den (np.ndarray(float)): size N. The array of the log-densities of the regulariser.
@@ -415,13 +412,6 @@ def compute_density_BMTI(
self.log_den = log_den
self.log_den_err = log_den_err

# add a warnings.warning if self.N > 10000 and mem_efficient is False
if self.N > 15000 and mem_efficient is False:
warnings.warn(
"The number of points is large and the memory efficient option is not selected. \
If you run into memory issues, consider using the slower memory efficient option."
)

if self.verb:
print("BMTI density estimation started")
sec = time.time()
@@ -432,14 +422,16 @@ def compute_density_BMTI(
sec2 = time.time()

if self.verb:
print("{0:0.2f} seconds to fill get linear system ready".format(sec2 - sec))
print("{0:0.2f} seconds to get the linear system ready".format(sec2 - sec))

# solve linear system
log_den = self._solve_BMTI_reg_linar_system(A, deltaFcum, mem_efficient)
log_den = self._solve_BMTI_reg_linar_system(A, deltaFcum)
self.log_den = log_den

if self.verb:
print("{0:0.2f} seconds to solve linear system".format(time.time() - sec2))
print(
"{0:0.2f} seconds to solve the linear system".format(time.time() - sec2)
)
sec2 = time.time()

# compute error
@@ -529,10 +521,6 @@ def _get_BMTI_reg_linear_system(self, delta_F_inv_cov, alpha):

return A, deltaFcum

def _solve_BMTI_reg_linar_system(self, A, deltaFcum, mem_efficient):
if mem_efficient is False:
log_den = np.linalg.solve(A.todense(), deltaFcum)
else:
log_den = sparse.linalg.spsolve(A.tocsr(), deltaFcum)

def _solve_BMTI_reg_linar_system(self, A, deltaFcum):
log_den = sparse.linalg.cg(A, deltaFcum, atol=0.0, maxiter=None)[0]
return log_den
2 changes: 1 addition & 1 deletion tests/test_density_advanced/test_density_advanced.py
Original file line number Diff line number Diff line change
@@ -133,4 +133,4 @@ def test_density_BMTI():
da.set_id(2)
da.compute_density_BMTI(alpha=0.99)

assert np.allclose(da.log_den, expected_density_BMTI)
assert np.allclose(da.log_den, expected_density_BMTI, rtol=1e-05, atol=1e-01)