1
1
from scipy .stats import percentileofscore
2
2
import numpy as np
3
-
3
+ from pandarallel import pandarallel
4
+ from multiprocessing import cpu_count
4
5
class QuantileScoreComputer (object ):
5
6
6
7
def __init__ (self ,metadata ,
@@ -33,4 +34,68 @@ def compute_score(self, drug, cell, score):
33
34
#score as harmonic mean of efficacy and selectivity
34
35
score = 2 * (efficacy * selectivity )/ (efficacy + selectivity )
35
36
36
- return score
37
+ return score
38
+
39
+ def parallel_compute_score (self ,df ,
40
+ drug_col = 'DrugID' ,cell_col = 'DepMapID' ,score_col = 'Predictions' ,score_col_out = 'QuantileScore' ,
41
+ n_jobs = - 1 ):
42
+
43
+ if n_jobs == - 1 :
44
+ n_jobs = cpu_count ()
45
+
46
+ pandarallel .initialize (nb_workers = n_jobs )
47
+ df [score_col_out ] = df .parallel_apply (lambda x : self .compute_score (drug = x [drug_col ],cell = x [cell_col ],score = x [score_col ]),axis = 1 )
48
+ return df [score_col_out ]
49
+
50
+ def compute_drug_score (self ,drug ,score ,return_distrib = False ):
51
+ score = 1 - (percentileofscore (self .distrib_drugs [drug ], score )* 0.01 )
52
+ if return_distrib :
53
+ return score , self .distrib_drugs [drug ]
54
+ else :
55
+ return score
56
+
57
+ def compute_cell_score (self ,cell ,score ,return_distrib = False ):
58
+ score = 1 - (percentileofscore (self .distrib_cells [cell ], score )* 0.01 )
59
+ if return_distrib :
60
+ return score , self .distrib_cells [cell ]
61
+ else :
62
+ return score
63
+
64
+ def add_cells (self ,cells ,
65
+ cell_col = 'DepMapID' ,score_col = 'Predictions' ):
66
+
67
+ temp = cells [[cell_col ,score_col ]]
68
+ temp = temp .groupby (cell_col )[score_col ].agg (list )
69
+
70
+ temp_dict = temp .to_dict ()
71
+ self .distrib_cells = {** self .distrib_cells ,** temp_dict }
72
+
73
+ def save (self , filepath ):
74
+ """Save the QuantileScoreComputer object to a file using numpy.
75
+
76
+ Args:
77
+ filepath (str): Path where to save the object
78
+ """
79
+ save_dict = {
80
+ 'distrib_cells' : self .distrib_cells ,
81
+ 'distrib_drugs' : self .distrib_drugs
82
+ }
83
+ np .save (filepath , save_dict )
84
+
85
+ @classmethod
86
+ def load (cls , filepath ):
87
+ """Load a QuantileScoreComputer object from a file.
88
+
89
+ Args:
90
+ filepath (str): Path to the saved object
91
+
92
+ Returns:
93
+ QuantileScoreComputer: Loaded object
94
+ """
95
+ load_dict = np .load (filepath , allow_pickle = True ).item ()
96
+
97
+ obj = cls .__new__ (cls )
98
+ obj .distrib_cells = load_dict ['distrib_cells' ]
99
+ obj .distrib_drugs = load_dict ['distrib_drugs' ]
100
+ return obj
101
+
0 commit comments