Skip to content

Commit 43f46f7

Browse files
committed
main: Reorganise so arg parsing and env settings come before tf imports
1 parent b3b8f9b commit 43f46f7

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

src/geneml/main.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
run_model,
2222
)
2323

24+
args = parse_args()
25+
params = build_params_namedtuple(args)
26+
27+
if params.cpu_only:
28+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
29+
30+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
31+
32+
# TF and any modules that use it need to be imported after environment variables are set
33+
import tensorflow as tf # noqa: E402, I001
34+
from geneml.model_loader import get_cached_gene_ml_model # noqa: E402, I001
35+
2436
logger = logging.getLogger("geneml")
2537

2638

@@ -88,13 +100,6 @@ def process_contig(contig_id: str, seq: str, params: Params, tensorflow_thread_c
88100
"""
89101
start_time = time.time()
90102

91-
import tensorflow as tf
92-
93-
from geneml.model_loader import get_cached_gene_ml_model
94-
95-
if params.cpu_only:
96-
tf.config.set_visible_devices([], 'GPU')
97-
98103
tf.config.threading.set_inter_op_parallelism_threads(tensorflow_thread_count)
99104
tf.config.threading.set_intra_op_parallelism_threads(tensorflow_thread_count)
100105

@@ -262,13 +267,6 @@ def main() -> None:
262267
Returns:
263268
None.
264269
"""
265-
args = parse_args()
266-
params = build_params_namedtuple(args)
267-
268-
if params.cpu_only:
269-
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
270-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
271-
272270
logfile = ''.join([params.basepath, '.log'])
273271
setup_logger(logfile, debug = params.debug, verbose= params.verbose)
274272
write_setup_info(params)

0 commit comments

Comments
 (0)