limix / struct-lmm

Structured Linear Mixed Model is a method to test for loci that interact with multiple environments.
Other
14 stars 10 forks source link

Slow start-up #25

Closed horta closed 4 years ago

horta commented 5 years ago

The first run of struct-lmm takes more than 4s no matter how small the input is.

Example

from struct_lmm import StructLMM
import numpy as np
from time import time

tests = ["inter", "assoc"]
rhos = [0.0, 0.1 ** 2, 0.2 ** 2, 0.3 ** 2, 0.4 ** 2, 0.5 ** 2, 0.5, 1.0]

def run(n, k):

    random = np.random.RandomState(0)
    y = random.randn(n, 1)
    E = random.randn(n, k)
    G = random.randn(n, 3)
    M = np.ones((y.shape[0], 1))

    print("y.shape: {}".format(y.shape))
    print("E.shape: {}".format(E.shape))
    print("G.shape: {}".format(G.shape))
    print()

    start_time = time()

    if "inter" in tests:
        slmi = StructLMM(y, E, W=E, rho_list=[0])

    if "assoc" in tests:
        slmm = StructLMM(y, E, W=E, rho_list=rhos)
        slmm.fit_null(F=M, verbose=False)

    _pvi = np.zeros(G.shape[1])
    _pva = np.zeros(G.shape[1])
    for snp in range(G.shape[1]):
        x = G[:, [snp]]

        if "inter" in tests:
            # interaction test
            M1 = np.hstack((M, x))
            slmi.fit_null(F=M1, verbose=False)
            _pvi[snp] = slmi.score_2_dof(x)

        if "assoc" in tests:
            # association test
            _pva[snp] = slmm.score_2_dof(x)

    elapsed_time = time() - start_time

    print("Elapsed time: {:.2f}s".format(elapsed_time))
    print()

if __name__ == "__main__":

    n = 5
    k = 3
    run(n, k)

    n = 1000
    k = 3
    run(n, k)

    n = 5
    k = 3
    run(n, k)

The above code outputs

y.shape: (5, 1)
E.shape: (5, 3)
G.shape: (5, 3)

Elapsed time: 4.31s

y.shape: (1000, 1)
E.shape: (1000, 3)
G.shape: (1000, 3)

Elapsed time: 1.21s

y.shape: (5, 1)
E.shape: (5, 3)
G.shape: (5, 3)

Elapsed time: 1.49s

on my machine. That slow start-up is slowing down limix tests quite a lot as tests often require clean start-up, which amounts to >4s. It also makes debugging limix package regarding struct-lmm run a slow process as most of the time is spent in that start-up.

horta commented 4 years ago

This has been solved.