Skip to content

Commit

Permalink
feat(CARMA): adding time limited CARMA (#475)
Browse files Browse the repository at this point in the history
* feat(CARMA): adding time limited CARMA

* feat: correct the function

* feat(CARMA): added tests

* chore: small fix of varaible types

* chore: fix of test descriptions
  • Loading branch information
addramir committed Mar 5, 2024
1 parent dfc1700 commit 9f1bec0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/gentropy/method/carma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""CARMA outlier detection method."""
from __future__ import annotations

import concurrent.futures
from itertools import combinations
from math import floor, lgamma
from typing import Any
Expand All @@ -14,6 +15,34 @@
class CARMA:
"""Implementation of CARMA outlier detection method."""

@staticmethod
def time_limited_CARMA_spike_slab_noEM(
z: np.ndarray, ld: np.ndarray, sec_threshold: float = 600
) -> dict[str, Any]:
"""The wrapper for the CARMA_spike_slab_noEM function that runs the function in a separate thread and terminates it if it takes too long.
Args:
z (np.ndarray): Numeric vector representing z-scores.
ld (np.ndarray): Numeric matrix representing the linkage disequilibrium (LD) matrix.
sec_threshold (float): The time threshold in seconds.
Returns:
dict[str, Any]: A dictionary containing the following results:
- PIPs: A numeric vector of posterior inclusion probabilities (PIPs) for all SNPs or None.
- B_list: A dataframe containing the marginal likelihoods and the corresponding model space or None.
- Outliers: A list of outlier SNPs or None.
"""
try:
# Execute CARMA.CARMA_spike_slab_noEM with a timeout
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(CARMA.CARMA_spike_slab_noEM, z=z, ld=ld)
result = future.result(timeout=sec_threshold)
except concurrent.futures.TimeoutError:
# If execution exceeds the timeout, return None
result = {"PIPs": None, "B_list": None, "Outliers": None}

return result

@staticmethod
def CARMA_spike_slab_noEM(
z: np.ndarray,
Expand Down Expand Up @@ -105,7 +134,6 @@ def CARMA_spike_slab_noEM(
"Outliers": all_C_list["conditional_S_list"],
}


return results_list

@staticmethod
Expand Down
19 changes: 19 additions & 0 deletions tests/gentropy/method/test_carma.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,22 @@ def test_MCS_modified(
num_causal=10,
)
assert np.allclose(np.round(np.corrcoef(l1_pips, pips)[0, 1], decimals=2), 1)

def test_time_limited_CARMA_spike_slab_noEM_pips_no_restriction(
self: TestCARMA, sample_data_for_carma: list[np.ndarray]
) -> None:
"""Test of CARMA PIPs with liberal (no) time restriction."""
ld = sample_data_for_carma[0]
z = sample_data_for_carma[1]
pips = sample_data_for_carma[2]
_l = CARMA.time_limited_CARMA_spike_slab_noEM(z=z, ld=ld, sec_threshold=600)
assert np.allclose(np.round(np.corrcoef(_l["PIPs"], pips)[0, 1], decimals=2), 1)

def test_time_limited_CARMA_spike_slab_noEM_pips_restriction(
self: TestCARMA, sample_data_for_carma: list[np.ndarray]
) -> None:
"""Test of CARMA PIPs with time restriction."""
ld = sample_data_for_carma[0]
z = sample_data_for_carma[1]
_l = CARMA.time_limited_CARMA_spike_slab_noEM(z=z, ld=ld, sec_threshold=0.001)
assert _l["Outliers"] is None and _l["PIPs"] is None

0 comments on commit 9f1bec0

Please sign in to comment.