From 66e44f684aa79f6721b30d3c5f3cc637de223c2b Mon Sep 17 00:00:00 2001 From: Niklaus Johner Date: Tue, 2 Jul 2024 12:26:13 +0200 Subject: [PATCH] Move caching of ConfintStore inside permute_picking. Caching an SQL connection on import of scoary works fine when used as a command line tool, but not when scoary is integrated in another python application, as this can lead to the SQL object being used in a different thread than the one it was created in. --- scoary/permutations.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/scoary/permutations.py b/scoary/permutations.py index c09de03..21c1490 100644 --- a/scoary/permutations.py +++ b/scoary/permutations.py @@ -1,7 +1,8 @@ -import os import logging -import pandas as pd +import os + import numpy as np +import pandas as pd from .KeyValueStore import KeyValueStore from .picking import pick @@ -39,9 +40,6 @@ def set(self, tree: str, n_pos_assoc: int, n_permut: int, confidence_interval: [ self.con.commit() -CONFINT_CACHE = ConfintStore(table_name='confint_cache', db_path=os.environ.get('CONFINT_DB', None)) - - def create_permuted_df(labels: [str], n_positive: int, n_permut: int, random_state: int = None): if random_state: np.random.seed(random_state) @@ -74,6 +72,9 @@ def permute_picking( n_reused = 0 pvals = [] + CONFINT_CACHE = ConfintStore(table_name='confint_cache', + db_path=os.environ.get('CONFINT_DB', None)) + for _, row in result_df.iterrows(): label_to_gene = genes_bool_df.loc[row.Gene] unique_topology = tree.uniquify(label_to_gene)