Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ec88f83
Add kdescent to testing env
aphearin Jul 15, 2024
4cc5d72
Begin to build unit test of kdescent application
aphearin Jul 15, 2024
1fff82b
flake8 fix
aphearin Jul 15, 2024
e7b2a72
Add failing assert to check workflow setup
aphearin Jul 15, 2024
4d46271
Add test that should fail if kdescent is not installed
aphearin Jul 15, 2024
8575962
Fix import issue
aphearin Jul 15, 2024
29ca8a3
axopt --> optax
aphearin Jul 15, 2024
59fd622
Remove temporary assert
aphearin Jul 15, 2024
e3dd3a3
Concatenate data
aphearin Jul 15, 2024
8c1b015
@alanpearl I have a working call to compare_kde_counts but the value …
aphearin Jul 15, 2024
ddc3025
Loss is gigantic even when fiducial and proposed models are extremely…
aphearin Jul 15, 2024
4924292
Resolve my confusion over magnitude of kde-based loss
aphearin Jul 15, 2024
76b4eb7
Restore large excursion in u_param space
aphearin Jul 15, 2024
222a656
Add single-constraint loss function
aphearin Jul 16, 2024
543c12b
Add convenience function get_single_sample_self_fit_target_data
aphearin Jul 16, 2024
d140396
Add multi-sample loss for self-fit data
aphearin Jul 16, 2024
8b76b14
add failing test of kdescent.adam
aphearin Jul 16, 2024
8d05efb
Increase robustness of test_single_sample_kde_loss_self_fit, which no…
aphearin Jul 16, 2024
8b9438c
test_single_sample_kde_loss_self_fit passes now with num_target_redsh…
aphearin Jul 16, 2024
e35b043
Start ignoring tests based on previous implementation that uses Mh(t)…
aphearin Jul 17, 2024
4ba4e4e
Start building 1-d kde optimizer
aphearin Jul 17, 2024
9db6911
Add new implementation of single_sample_kde_loss_self_fit based on 5 …
aphearin Jul 17, 2024
2098f4e
New unit test of multi-obs loss passes
aphearin Jul 17, 2024
a5c6ce3
New unit test of kdescent.adam passes
aphearin Jul 17, 2024
2efee6a
Merge pull request #128 from ArgonneCPAC/kde_loss_dev_1d
aphearin Jul 18, 2024
d5eb239
Begin to develop new module kde2d_wrappers.py
aphearin Jul 18, 2024
ce7edad
Implement 2d single_sample_kde_loss_self_fit. Next steps are to fill …
aphearin Jul 18, 2024
6ff8c29
Add remaining kernels
aphearin Jul 19, 2024
f8a0596
Switch to logarithmic specific mass accretion rate
aphearin Jul 22, 2024
0ade3b8
Fix bug in test_mc_diffmah_preds
aphearin Jul 22, 2024
ba32dde
Merge pull request #129 from ArgonneCPAC/kde_loss_dev_2d
aphearin Jul 22, 2024
11c1711
Add get_single_cen_sample_target_data function
aphearin Jul 22, 2024
e653f9f
working call to single_sample_kde_loss_kern but results are singular
aphearin Jul 23, 2024
75fe5c1
renormalize MAHs to zero to at lgm_obs
aphearin Jul 23, 2024
0d07615
Switch to fractional errors
aphearin Jul 24, 2024
c152375
nan grad test passes
aphearin Jul 24, 2024
22ef9ae
nan grad test passes for 100 different random u_params
aphearin Jul 24, 2024
5b10f55
nan grad test passes for multi-objective loss
aphearin Jul 24, 2024
fd658d3
Add convenience function get_cens_target_data
aphearin Jul 24, 2024
94d427d
Fix bug in test workflow
aphearin Jul 24, 2024
74f2716
Restore kdescent installation into workflow
aphearin Jul 24, 2024
6c8e899
Add astropy into workflow
aphearin Jul 24, 2024
1dcf2e0
Remove hard-coding of NH_PER_M0BIN
aphearin Jul 24, 2024
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
7 changes: 6 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ jobs:
setuptools \
"setuptools_scm>=7,<8" \
scipy \
python-build
python-build \
jaxopt \
tqdm \
optax \
astropy
pip install --no-deps git+https://github.com/AlanPearl/kdescent.git
python -m pip install --no-build-isolation --no-deps -e .

- name: test
Expand Down
Empty file.
109 changes: 109 additions & 0 deletions diffmah/diffmahpop_kernels/kdescent_testing/dmp_wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
"""

from jax import value_and_grad, vmap

try:
import kdescent
except ImportError:
pass
from jax import jit as jjit
from jax import numpy as jnp

from .. import diffmahpop_params as dpp
from .. import mc_diffmahpop_kernels as mdk


@jjit
def mc_diffmah_preds(diffmahpop_u_params, pred_data):
diffmahpop_params = dpp.get_diffmahpop_params_from_u_params(diffmahpop_u_params)
tarr, lgm_obs, t_obs, ran_key, lgt0 = pred_data
_res = mdk._mc_diffmah_halo_sample(
diffmahpop_params, tarr, lgm_obs, t_obs, ran_key, lgt0
)
ftpt0 = _res[3]
log_mah_tpt0 = _res[6]
log_mah_tp = _res[8]
return log_mah_tpt0, log_mah_tp, ftpt0


@jjit
def single_sample_kde_loss_self_fit(
diffmahpop_u_params,
tarr,
lgm_obs,
t_obs,
ran_key,
lgt0,
log_mahs_target,
weights_target,
):
pred_data = tarr, lgm_obs, t_obs, ran_key, lgt0
_res = mc_diffmah_preds(diffmahpop_u_params, pred_data)
log_mah_tpt0, log_mah_tp, ftpt0 = _res
log_mahs_pred = jnp.concatenate((log_mah_tpt0, log_mah_tp))
weights_pred = jnp.concatenate((ftpt0, 1 - ftpt0))

kcalc = kdescent.KCalc(log_mahs_target, weights_target)
model_counts, truth_counts = kcalc.compare_kde_counts(
ran_key, log_mahs_pred, weights_pred
)
diff = model_counts - truth_counts
loss = jnp.mean(diff**2)
return loss


single_sample_kde_loss_and_grad_self_fit = jjit(
value_and_grad(single_sample_kde_loss_self_fit)
)


@jjit
def get_single_sample_self_fit_target_data(
u_params, tarr, lgm_obs, t_obs, ran_key, lgt0
):
pred_data = tarr, lgm_obs, t_obs, ran_key, lgt0
_res = mc_diffmah_preds(u_params, pred_data)
log_mah_tpt0, log_mah_tp, ftpt0 = _res
log_mahs_target = jnp.concatenate((log_mah_tpt0, log_mah_tp))
weights_target = jnp.concatenate((ftpt0, 1 - ftpt0))
return log_mahs_target, weights_target


_A = (None, 0, 0, 0, 0, None)
get_multisample_self_fit_target_data = jjit(
vmap(get_single_sample_self_fit_target_data, in_axes=_A)
)

_L = (None, 0, 0, 0, 0, None, 0, 0)
_multisample_kde_loss_self_fit = jjit(vmap(single_sample_kde_loss_self_fit, in_axes=_L))


@jjit
def multisample_kde_loss_self_fit(
diffmahpop_u_params,
tarr_matrix,
lgmobsarr,
tobsarr,
ran_keys,
lgt0,
log_mahs_targets,
weights_targets,
):
losses = _multisample_kde_loss_self_fit(
diffmahpop_u_params,
tarr_matrix,
lgmobsarr,
tobsarr,
ran_keys,
lgt0,
log_mahs_targets,
weights_targets,
)
loss = jnp.mean(losses)
return loss


multisample_kde_loss_and_grad_self_fit = jjit(
value_and_grad(multisample_kde_loss_self_fit)
)
146 changes: 146 additions & 0 deletions diffmah/diffmahpop_kernels/kdescent_testing/kde1d_wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
"""

from jax import random as jran
from jax import value_and_grad, vmap

try:
import kdescent
except ImportError:
pass
from jax import jit as jjit
from jax import numpy as jnp

from .. import diffmahpop_params as dpp
from .. import mc_diffmahpop_kernels as mdk

N_T_PER_BIN = 5


@jjit
def mc_diffmah_preds(diffmahpop_u_params, pred_data):
diffmahpop_params = dpp.get_diffmahpop_params_from_u_params(diffmahpop_u_params)
tarr, lgm_obs, t_obs, ran_key, lgt0 = pred_data
_res = mdk._mc_diffmah_halo_sample(
diffmahpop_params, tarr, lgm_obs, t_obs, ran_key, lgt0
)
ftpt0 = _res[3]
log_mah_tpt0 = _res[6]
log_mah_tp = _res[8]
return log_mah_tpt0, log_mah_tp, ftpt0


@jjit
def single_sample_kde_loss_self_fit(
diffmahpop_u_params,
tarr,
lgm_obs,
t_obs,
ran_key,
lgt0,
log_mahs_target,
weights_target,
):
s = (-1, 1)
kcalc0 = kdescent.KCalc(log_mahs_target[:, 0].reshape(s), weights_target)
kcalc1 = kdescent.KCalc(log_mahs_target[:, 1].reshape(s), weights_target)
kcalc2 = kdescent.KCalc(log_mahs_target[:, 2].reshape(s), weights_target)
kcalc3 = kdescent.KCalc(log_mahs_target[:, 3].reshape(s), weights_target)
kcalc4 = kdescent.KCalc(log_mahs_target[:, 4].reshape(s), weights_target)

ran_key, pred_key = jran.split(ran_key, 2)
pred_data = tarr, lgm_obs, t_obs, pred_key, lgt0
_res = mc_diffmah_preds(diffmahpop_u_params, pred_data)
log_mah_tpt0, log_mah_tp, ftpt0 = _res

log_mahs_pred = jnp.concatenate((log_mah_tpt0, log_mah_tp))
weights_pred = jnp.concatenate((ftpt0, 1 - ftpt0))

kcalc_keys = jran.split(ran_key, N_T_PER_BIN)

model_counts0, truth_counts0 = kcalc0.compare_kde_counts(
kcalc_keys[0], log_mahs_pred[:, 0].reshape(s), weights_pred
)
model_counts1, truth_counts1 = kcalc1.compare_kde_counts(
kcalc_keys[1], log_mahs_pred[:, 1].reshape(s), weights_pred
)
model_counts2, truth_counts2 = kcalc2.compare_kde_counts(
kcalc_keys[2], log_mahs_pred[:, 2].reshape(s), weights_pred
)
model_counts3, truth_counts3 = kcalc3.compare_kde_counts(
kcalc_keys[3], log_mahs_pred[:, 3].reshape(s), weights_pred
)
model_counts4, truth_counts4 = kcalc4.compare_kde_counts(
kcalc_keys[4], log_mahs_pred[:, 4].reshape(s), weights_pred
)

diff0 = model_counts0 - truth_counts0
diff1 = model_counts1 - truth_counts1
diff2 = model_counts2 - truth_counts2
diff3 = model_counts3 - truth_counts3
diff4 = model_counts4 - truth_counts4

loss0 = jnp.mean(diff0**2)
loss1 = jnp.mean(diff1**2)
loss2 = jnp.mean(diff2**2)
loss3 = jnp.mean(diff3**2)
loss4 = jnp.mean(diff4**2)

loss = loss0 + loss1 + loss2 + loss3 + loss4
return loss


single_sample_kde_loss_and_grad_self_fit = jjit(
value_and_grad(single_sample_kde_loss_self_fit)
)


@jjit
def get_single_sample_self_fit_target_data(
u_params, tarr, lgm_obs, t_obs, ran_key, lgt0
):
pred_data = tarr, lgm_obs, t_obs, ran_key, lgt0
_res = mc_diffmah_preds(u_params, pred_data)
log_mah_tpt0, log_mah_tp, ftpt0 = _res
log_mahs_target = jnp.concatenate((log_mah_tpt0, log_mah_tp))
weights_target = jnp.concatenate((ftpt0, 1 - ftpt0))
return log_mahs_target, weights_target


_A = (None, 0, 0, 0, 0, None)
get_multisample_self_fit_target_data = jjit(
vmap(get_single_sample_self_fit_target_data, in_axes=_A)
)

_L = (None, 0, 0, 0, 0, None, 0, 0)
_multisample_kde_loss_self_fit = jjit(vmap(single_sample_kde_loss_self_fit, in_axes=_L))


@jjit
def multisample_kde_loss_self_fit(
diffmahpop_u_params,
tarr_matrix,
lgmobsarr,
tobsarr,
ran_keys,
lgt0,
log_mahs_targets,
weights_targets,
):
losses = _multisample_kde_loss_self_fit(
diffmahpop_u_params,
tarr_matrix,
lgmobsarr,
tobsarr,
ran_keys,
lgt0,
log_mahs_targets,
weights_targets,
)
loss = jnp.mean(losses)
return loss


multisample_kde_loss_and_grad_self_fit = jjit(
value_and_grad(multisample_kde_loss_self_fit)
)
Loading