Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ Deprecations
.. Improvements
.. ~~~~~~~~~~~~

.. Bug fixes
.. ~~~~~~~~~
Bug fixes
~~~~~~~~~

- Fixed bug causing high memory usage in :func:`pedigree_kinship` when
evaluating a chunked relationship matrix with the Hamilton-Kerr method.
(:user:`timothymillar`, :pr:`1335`, :issue:`1333`)

.. Documentation
.. ~~~~~~~~~~~~~
Expand Down
9 changes: 7 additions & 2 deletions sgkit/stats/pedigree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,15 @@ def pedigree_kinship(
# new dataset
dims = ["samples_0", "samples_1"]
if return_relationship:
relationship = kinship * 2
if method == EST_HAMILTON_KERR:
ploidy = tau.sum(axis=-1)
relationship *= np.sqrt(ploidy[None, :] / 2 * ploidy[:, None] / 2)
ploidy_0 = ploidy.rechunk((kinship.chunks[0],))
ploidy_1 = ploidy.rechunk((kinship.chunks[1],))
scale = da.sqrt(ploidy_0[:, None] * ploidy_1[None, :])
assert kinship.chunks == scale.chunks
relationship = kinship * scale
else:
relationship = kinship * 2
arrays = {
variables.stat_pedigree_kinship: xr.DataArray(kinship, dims=dims),
variables.stat_pedigree_relationship: xr.DataArray(relationship, dims=dims),
Expand Down
23 changes: 23 additions & 0 deletions sgkit/tests/test_pedigree.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,29 @@ def test_pedigree_kinship__Hamilton_Kerr_compress_parent_dimension(
np.testing.assert_array_almost_equal(actual, expect)


def test_pedigree_kinship__Hamilton_Kerr_chunked_mem():
# tests fix for https://github.com/sgkit-dev/sgkit/issues/1333
# if chunking is not applied to the ploidy matrix correctly
# this will try to allocate > 7TB of RAM (and presumably fail)
ds = xr.Dataset()
ds["parent"] = ["samples", "parents"], np.full((1_000_000, 2), -1, int)
ds["stat_Hamilton_Kerr_tau"] = ["samples", "parents"], np.full(
(1_000_000, 2), 2, np.uint64
)
ds["stat_Hamilton_Kerr_lambda"] = ["samples", "parents"], np.full(
(1_000_000, 2), 0.0
)
matrix = sg.pedigree_kinship(
ds,
method="Hamilton-Kerr",
chunks=((10, 999990), (20, 999980)),
return_relationship=True,
).stat_pedigree_relationship
expect = np.eye(10, 20)
actual = matrix[0:10, 0:20].compute()
np.testing.assert_array_almost_equal(actual, expect)


@pytest.mark.parametrize("method", ["diploid", "Hamilton-Kerr"])
def test_pedigree_kinship__half_founder(method):
ds0 = sg.simulate_genotype_call_dataset(n_variant=1, n_sample=6)
Expand Down
Loading