Skip to content

Commit d04ba55

Browse files
committed
Update api_encoding_train_test.ipynb and ray as optional import
1 parent 98c40b7 commit d04ba55

File tree

6 files changed

+746
-44
lines changed

6 files changed

+746
-44
lines changed

pypef/dca/plmc_encoding.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@
4646
logger = logging.getLogger('pypef.dca.encoding')
4747

4848
import numpy as np
49-
import ray
5049
from tqdm import tqdm
5150
import pickle
5251

52+
from pypef.main import USE_RAY
53+
if USE_RAY:
54+
import ray
55+
5356
from pypef.utils.variant_data import amino_acids
57+
from pypef.ml.parallelization import ConditionalDecorator
5458

5559
_SLICE = np.s_[:]
5660

@@ -741,7 +745,7 @@ def get_encoded_sequence(
741745
return encoded_seq
742746

743747

744-
@ray.remote
748+
@ConditionalDecorator(ray.remote, USE_RAY)
745749
def _get_data_parallel(
746750
variants: list,
747751
sequences: list,

pypef/hybrid/hybrid_run.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
import logging
1616
logger = logging.getLogger('pypef.dca.dca_run')
17-
import ray
17+
18+
from pypef.main import USE_RAY
19+
if USE_RAY:
20+
import ray
1821

1922
from pypef.utils.variant_data import read_csv, get_wt_sequence
2023
from pypef.dca.plmc_encoding import save_plmc_dca_encoding_model
@@ -34,7 +37,7 @@ def run_pypef_hybrid_modeling(arguments):
3437
model_type = 'undefined'
3538
if model_type in ['GREMLIN', 'DCAHybridModel'] and threads > 1:
3639
logger.info(f'No (Ray) parallelization for {model_type} model...')
37-
elif model_type not in ['GREMLIN', 'DCAHybridModel'] and threads > 1:
40+
elif model_type not in ['GREMLIN', 'DCAHybridModel'] and threads > 1 and USE_RAY:
3841
ray.init()
3942
logger.info(f'Using {threads} threads for running...')
4043
if model_type == 'DCAHybridModel':

pypef/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
A2M format [default: False].
287287
"""
288288

289+
USE_RAY = False
289290

290291
from sys import argv, version_info
291292
from pypef import __version__

pypef/ml/ml_run.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
import os
1616
from os import listdir
1717
from os.path import isfile, join
18-
import ray
18+
1919
import logging
2020
logger = logging.getLogger('pypef.ml.ml_run')
2121

22+
from pypef.main import USE_RAY
23+
if USE_RAY:
24+
import ray
25+
2226
from pypef.ml.parallelization import aaindex_performance_parallel
2327
from pypef.ml.regression import (
2428
read_models, formatted_output, performance_list, save_model, predict, predict_ts
@@ -35,7 +39,7 @@ def run_pypef_pure_ml(arguments):
3539
"""
3640
threads = abs(arguments['--threads']) if arguments['--threads'] is not None else 1
3741
threads = threads + 1 if threads == 0 else threads
38-
if threads > 1:
42+
if threads > 1 and USE_RAY:
3943
ray.init()
4044
if arguments['--show']:
4145
if arguments['MODELS'] != str(5):

pypef/ml/parallelization.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import matplotlib
2222
matplotlib.use('Agg')
2323
import os
24-
import ray
2524
import warnings
2625

26+
from pypef.main import USE_RAY
27+
if USE_RAY:
28+
import ray
29+
2730
from pypef.utils.variant_data import get_sequences_from_file
2831
from pypef.ml.regression import (
2932
full_aaidx_txt_path, path_aaindex_dir, AAIndexEncoding, get_regressor_performances
@@ -34,7 +37,19 @@
3437
warnings.filterwarnings(action='ignore', category=UserWarning, module='sklearn')
3538

3639

37-
@ray.remote
40+
class ConditionalDecorator(object):
41+
def __init__(self, dec, condition):
42+
self.decorator = dec
43+
self.condition = condition
44+
45+
def __call__(self, func):
46+
if not self.condition:
47+
# Return the function unchanged, not decorated
48+
return func
49+
return self.decorator(func)
50+
51+
52+
@ConditionalDecorator(ray.remote, USE_RAY)
3853
def parallel(
3954
d,
4055
core,

scripts/Encoding_low_N/api_encoding_train_test.ipynb

Lines changed: 711 additions & 36 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)