pflex 1.7.0__tar.gz → 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pflex-1.7.0 → pflex-2.0.0}/PKG-INFO +54 -1
- {pflex-1.7.0 → pflex-2.0.0}/README.md +53 -0
- pflex-2.0.0/docs/early-precision-metrics-proposal.md +348 -0
- pflex-2.0.0/docs/half-max-auc-spec.md +119 -0
- {pflex-1.7.0 → pflex-2.0.0}/docs/user-guide.md +2 -0
- {pflex-1.7.0 → pflex-2.0.0}/pyproject.toml +1 -1
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/__init__.py +3 -3
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/analysis.py +240 -2
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/plotting.py +250 -2
- pflex-2.0.0/tests/test_half_max_auc.py +340 -0
- {pflex-1.7.0 → pflex-2.0.0}/uv.lock +1 -1
- {pflex-1.7.0 → pflex-2.0.0}/.gitignore +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/.python-version +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/LICENSE +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/docs/getting-started.md +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/docs/index.md +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/docs/installation.md +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/mkdocs.yml +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/__init__.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/example_input/__init__.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/example_input/skin_cell_lines_corum_genes.parquet +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/example_input/soft_tissue_cell_lines_corum_genes.parquet +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/CORUM.parquet +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/GOBP.parquet +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/PATHWAY.parquet +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/__init__.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/corum.csv +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/gobp.csv +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/data/functional_standard/pathway.csv +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/examples/basic_usage.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/logging_config.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/preprocessing.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/style.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/src/pflex/utils.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/tests/conftest.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/tests/test_benchmark.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/tests/test_correlation.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/tests/test_package_data.py +0 -0
- {pflex-1.7.0 → pflex-2.0.0}/tests/test_scatter_selection.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pflex
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.0
|
|
4
4
|
Summary: pflex is a benchmarking toolkit for evaluating CRISPR screen results against biological functional standards. The toolkit computes gene-level and module-level performance metrics, helping researchers systematically assess the biological relevance and resolution of their CRISPR screening data.
|
|
5
5
|
Author-email: Yasir Demirtas <tyasird@hotmail.com>
|
|
6
6
|
License-File: LICENSE
|
|
@@ -51,6 +51,7 @@ Genetic networks derived from omics data are a powerful tool for systematic gene
|
|
|
51
51
|
- Precision-recall curve generation for ranked gene lists
|
|
52
52
|
- Evaluation using CORUM-derived modules, GO terms, and pathways
|
|
53
53
|
- Module-level resolution analysis and visualization
|
|
54
|
+
- Half-max AUC: an early-precision score next to the standard AUPRC
|
|
54
55
|
- Easy integration into CRISPR screen workflows
|
|
55
56
|
- Packaged DepMap example inputs filtered to CORUM genes
|
|
56
57
|
|
|
@@ -226,6 +227,52 @@ The calculation and plotting calls are intentionally separate. Calling a
|
|
|
226
227
|
filter plotting function before its matching calculation raises an error that
|
|
227
228
|
identifies the required preparation function.
|
|
228
229
|
|
|
230
|
+
### Global AUPRC and half-max AUC
|
|
231
|
+
|
|
232
|
+
**1. Standard AUPRC.** `pra()` computes the area under the global
|
|
233
|
+
precision-recall curve for each dataset, as before:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
flex.plot_precision_recall_curve() # PR curves
|
|
237
|
+
flex.plot_auc_scores() # one AUPRC bar per dataset
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
**2. Half-max AUC (early precision).** AUPRC is dominated by the long tail of
|
|
241
|
+
low-precision pairs, so a dataset with a better top of the ranking can still
|
|
242
|
+
get a lower AUPRC. The half-max AUC looks only at the top of the curves, after
|
|
243
|
+
`pra()` has been run for all datasets:
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
flex.plot_precision_recall_curve(half_max=True) # adds the threshold and the window end
|
|
247
|
+
flex.plot_half_max_auc_scores() # one half-max AUC bar per dataset
|
|
248
|
+
scores = flex.half_max_auc_scores() # {dataset: {"score": ..., ...}}
|
|
249
|
+
flex.plot_half_max_explainer() # step-by-step figure: formula + results table
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
How it works:
|
|
253
|
+
|
|
254
|
+
- **Threshold** = background + ½ × (max precision − background), the same for
|
|
255
|
+
all datasets. Background is the precision of a random ranking (true pairs /
|
|
256
|
+
all pairs).
|
|
257
|
+
- **Window**: from the first TP to the latest TP where any dataset falls below
|
|
258
|
+
the threshold, the same for all datasets.
|
|
259
|
+
- **Score**: the average precision in that window, scaled so a random ranking
|
|
260
|
+
is 0 and a perfect one is 1.
|
|
261
|
+
|
|
262
|
+
`plot_half_max_explainer()` draws this calculation step by step for your
|
|
263
|
+
datasets (on the linear TP axis the score uses), with the formula and a
|
|
264
|
+
results table under the plot; useful for sharing.
|
|
265
|
+
|
|
266
|
+
Options (on `half_max_auc_scores`, `plot_half_max_auc_scores` and
|
|
267
|
+
`plot_half_max_explainer`):
|
|
268
|
+
|
|
269
|
+
- `fraction="three_quarter"` or `"quarter"`: threshold at 3/4 or 1/4 instead of 1/2.
|
|
270
|
+
- `max_rank=3`: use the 3rd highest precision instead of the max, so one or two
|
|
271
|
+
lucky first pairs cannot set the threshold.
|
|
272
|
+
|
|
273
|
+
The full definition and all parameters are in
|
|
274
|
+
[docs/half-max-auc-spec.md](docs/half-max-auc-spec.md).
|
|
275
|
+
|
|
229
276
|
See the [User Guide](https://tyasird.github.io/pFLEX/user-guide/) for a detailed explanation of every input field, configuration key, function, return value, and output.
|
|
230
277
|
|
|
231
278
|
---
|
|
@@ -266,8 +313,14 @@ for name, dataset in data.items():
|
|
|
266
313
|
corr = flex.perform_corr(dataset, config["corr_function"])
|
|
267
314
|
flex.pra(name, corr, is_corr=True)
|
|
268
315
|
|
|
316
|
+
# Standard AUPRC
|
|
269
317
|
flex.plot_precision_recall_curve()
|
|
270
318
|
flex.plot_auc_scores()
|
|
319
|
+
|
|
320
|
+
# Half-max AUC (early precision, 1/2 max threshold)
|
|
321
|
+
flex.plot_precision_recall_curve(half_max=True)
|
|
322
|
+
flex.plot_half_max_auc_scores()
|
|
323
|
+
flex.plot_half_max_explainer()
|
|
271
324
|
```
|
|
272
325
|
|
|
273
326
|
For a runnable full workflow, see [src/pflex/examples/basic_usage.py](src/pflex/examples/basic_usage.py).
|
|
@@ -19,6 +19,7 @@ Genetic networks derived from omics data are a powerful tool for systematic gene
|
|
|
19
19
|
- Precision-recall curve generation for ranked gene lists
|
|
20
20
|
- Evaluation using CORUM-derived modules, GO terms, and pathways
|
|
21
21
|
- Module-level resolution analysis and visualization
|
|
22
|
+
- Half-max AUC: an early-precision score next to the standard AUPRC
|
|
22
23
|
- Easy integration into CRISPR screen workflows
|
|
23
24
|
- Packaged DepMap example inputs filtered to CORUM genes
|
|
24
25
|
|
|
@@ -194,6 +195,52 @@ The calculation and plotting calls are intentionally separate. Calling a
|
|
|
194
195
|
filter plotting function before its matching calculation raises an error that
|
|
195
196
|
identifies the required preparation function.
|
|
196
197
|
|
|
198
|
+
### Global AUPRC and half-max AUC
|
|
199
|
+
|
|
200
|
+
**1. Standard AUPRC.** `pra()` computes the area under the global
|
|
201
|
+
precision-recall curve for each dataset, as before:
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
flex.plot_precision_recall_curve() # PR curves
|
|
205
|
+
flex.plot_auc_scores() # one AUPRC bar per dataset
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
**2. Half-max AUC (early precision).** AUPRC is dominated by the long tail of
|
|
209
|
+
low-precision pairs, so a dataset with a better top of the ranking can still
|
|
210
|
+
get a lower AUPRC. The half-max AUC looks only at the top of the curves, after
|
|
211
|
+
`pra()` has been run for all datasets:
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
flex.plot_precision_recall_curve(half_max=True) # adds the threshold and the window end
|
|
215
|
+
flex.plot_half_max_auc_scores() # one half-max AUC bar per dataset
|
|
216
|
+
scores = flex.half_max_auc_scores() # {dataset: {"score": ..., ...}}
|
|
217
|
+
flex.plot_half_max_explainer() # step-by-step figure: formula + results table
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
How it works:
|
|
221
|
+
|
|
222
|
+
- **Threshold** = background + ½ × (max precision − background), the same for
|
|
223
|
+
all datasets. Background is the precision of a random ranking (true pairs /
|
|
224
|
+
all pairs).
|
|
225
|
+
- **Window**: from the first TP to the latest TP where any dataset falls below
|
|
226
|
+
the threshold, the same for all datasets.
|
|
227
|
+
- **Score**: the average precision in that window, scaled so a random ranking
|
|
228
|
+
is 0 and a perfect one is 1.
|
|
229
|
+
|
|
230
|
+
`plot_half_max_explainer()` draws this calculation step by step for your
|
|
231
|
+
datasets (on the linear TP axis the score uses), with the formula and a
|
|
232
|
+
results table under the plot; useful for sharing.
|
|
233
|
+
|
|
234
|
+
Options (on `half_max_auc_scores`, `plot_half_max_auc_scores` and
|
|
235
|
+
`plot_half_max_explainer`):
|
|
236
|
+
|
|
237
|
+
- `fraction="three_quarter"` or `"quarter"`: threshold at 3/4 or 1/4 instead of 1/2.
|
|
238
|
+
- `max_rank=3`: use the 3rd highest precision instead of the max, so one or two
|
|
239
|
+
lucky first pairs cannot set the threshold.
|
|
240
|
+
|
|
241
|
+
The full definition and all parameters are in
|
|
242
|
+
[docs/half-max-auc-spec.md](docs/half-max-auc-spec.md).
|
|
243
|
+
|
|
197
244
|
See the [User Guide](https://tyasird.github.io/pFLEX/user-guide/) for a detailed explanation of every input field, configuration key, function, return value, and output.
|
|
198
245
|
|
|
199
246
|
---
|
|
@@ -234,8 +281,14 @@ for name, dataset in data.items():
|
|
|
234
281
|
corr = flex.perform_corr(dataset, config["corr_function"])
|
|
235
282
|
flex.pra(name, corr, is_corr=True)
|
|
236
283
|
|
|
284
|
+
# Standard AUPRC
|
|
237
285
|
flex.plot_precision_recall_curve()
|
|
238
286
|
flex.plot_auc_scores()
|
|
287
|
+
|
|
288
|
+
# Half-max AUC (early precision, 1/2 max threshold)
|
|
289
|
+
flex.plot_precision_recall_curve(half_max=True)
|
|
290
|
+
flex.plot_half_max_auc_scores()
|
|
291
|
+
flex.plot_half_max_explainer()
|
|
239
292
|
```
|
|
240
293
|
|
|
241
294
|
For a runnable full workflow, see [src/pflex/examples/basic_usage.py](src/pflex/examples/basic_usage.py).
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
# Proposal: adjusted early-precision metrics for per-complex analysis
|
|
2
|
+
|
|
3
|
+
## Status and purpose
|
|
4
|
+
|
|
5
|
+
This document describes two proposed metrics that could replace or complement
|
|
6
|
+
per-complex AUPRC in pFLEX:
|
|
7
|
+
|
|
8
|
+
1. **Adjusted BEELINE early precision**
|
|
9
|
+
2. **Adjusted early precision at a fixed recall target**
|
|
10
|
+
|
|
11
|
+
The motivation is that pFLEX is primarily interested in the quality of the
|
|
12
|
+
highest-ranked gene pairs. AUPRC summarizes the full precision-recall curve, so
|
|
13
|
+
good performance late in the ranking can compensate for weaker performance at
|
|
14
|
+
the beginning. Early-precision metrics focus directly on the useful early part
|
|
15
|
+
of the ranking.
|
|
16
|
+
|
|
17
|
+
These should be implemented as two separate functions because they answer
|
|
18
|
+
different questions. Neither function should choose an "optimal" cutoff from
|
|
19
|
+
the observed results; all cutoffs must be defined before comparing datasets.
|
|
20
|
+
|
|
21
|
+
## Per-complex candidate universe
|
|
22
|
+
|
|
23
|
+
For a target complex with five measured genes, there are
|
|
24
|
+
|
|
25
|
+
```text
|
|
26
|
+
C(5, 2) = 10
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
true within-complex pairs. These cannot be the only candidate pairs: if all ten
|
|
30
|
+
candidates were true pairs, precision would always be 1 and the ranking could
|
|
31
|
+
not be evaluated.
|
|
32
|
+
|
|
33
|
+
The existing pFLEX per-module logic provides an appropriate candidate universe.
|
|
34
|
+
For each target complex it:
|
|
35
|
+
|
|
36
|
+
1. starts from the globally score-sorted gene-pair table;
|
|
37
|
+
2. selects pairs containing at least one gene from the target complex;
|
|
38
|
+
3. labels a pair positive when it belongs to the target complex;
|
|
39
|
+
4. retains unannotated pairs as negative candidates; and
|
|
40
|
+
5. removes pairs annotated only to other complexes.
|
|
41
|
+
|
|
42
|
+
For example, if the dataset contains 23 genes and five belong to the target
|
|
43
|
+
complex, the unfiltered incident-pair universe contains:
|
|
44
|
+
|
|
45
|
+
```text
|
|
46
|
+
10 within-complex pairs + (5 x 18) complex-to-outside pairs = 100 pairs
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The actual pFLEX value may be smaller because pairs annotated only to other
|
|
50
|
+
complexes are removed. Therefore the functions must derive the following from
|
|
51
|
+
the actual filtered label vector, not from a theoretical gene count:
|
|
52
|
+
|
|
53
|
+
```text
|
|
54
|
+
N = number of eligible candidate pairs
|
|
55
|
+
P = number of positive target-complex pairs
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
For the examples below, assume `N = 100` and `P = 10`.
|
|
59
|
+
|
|
60
|
+
## Metric 1: adjusted BEELINE early precision
|
|
61
|
+
|
|
62
|
+
### Question answered
|
|
63
|
+
|
|
64
|
+
> If we inspect as many top predictions as there are known positive pairs in
|
|
65
|
+
> the complex, what fraction is correct, after accounting for random success?
|
|
66
|
+
|
|
67
|
+
### Raw score
|
|
68
|
+
|
|
69
|
+
Set the prediction cutoff to the number of positives:
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
K = P
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If six of the first ten eligible predictions are positive:
|
|
76
|
+
|
|
77
|
+
```text
|
|
78
|
+
raw BEELINE EP = 6 / 10 = 0.60
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Thus the cutoff automatically scales with complex size. A complex with 45
|
|
82
|
+
positive pairs is evaluated at the first 45 predictions, while one with 190
|
|
83
|
+
positive pairs is evaluated at the first 190 predictions.
|
|
84
|
+
|
|
85
|
+
### Random baseline and adjustment
|
|
86
|
+
|
|
87
|
+
For a random ordering, the expected precision in any fixed-size prefix is the
|
|
88
|
+
positive prevalence:
|
|
89
|
+
|
|
90
|
+
```text
|
|
91
|
+
random baseline = P / N
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
In the example:
|
|
95
|
+
|
|
96
|
+
```text
|
|
97
|
+
random baseline = 10 / 100 = 0.10
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Normalize random performance to 0 and perfect performance to 1:
|
|
101
|
+
|
|
102
|
+
```text
|
|
103
|
+
adjusted BEELINE EP = (raw EP - random baseline) / (1 - random baseline)
|
|
104
|
+
= (0.60 - 0.10) / (1 - 0.10)
|
|
105
|
+
= 0.56
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Interpretation:
|
|
109
|
+
|
|
110
|
+
- `0`: expected random performance;
|
|
111
|
+
- `1`: perfect performance;
|
|
112
|
+
- values below `0`: worse than random;
|
|
113
|
+
- `0.56`: 56% of the possible improvement from random to perfect was achieved.
|
|
114
|
+
|
|
115
|
+
### Proposed function
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
adjusted_beeline_ep(labels, scores=None)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`labels` is the target-complex binary label vector in prediction-score order.
|
|
122
|
+
`scores` is optional but is needed if boundary ties are handled explicitly.
|
|
123
|
+
|
|
124
|
+
The result should expose at least:
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
adjusted_score
|
|
128
|
+
raw_score
|
|
129
|
+
random_baseline
|
|
130
|
+
n_candidates # N
|
|
131
|
+
n_positives # P
|
|
132
|
+
requested_cutoff # P
|
|
133
|
+
effective_cutoff
|
|
134
|
+
true_positives_at_cutoff
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Metric 2: adjusted early precision at fixed recall
|
|
138
|
+
|
|
139
|
+
### Question answered
|
|
140
|
+
|
|
141
|
+
> How precise is the ranking at the moment when a fixed fraction of the
|
|
142
|
+
> complex's known positive pairs has been recovered?
|
|
143
|
+
|
|
144
|
+
This metric uses the same recall target for every complex. The proposed default
|
|
145
|
+
is 20%, with 50% available as a sensitivity analysis.
|
|
146
|
+
|
|
147
|
+
### Raw score
|
|
148
|
+
|
|
149
|
+
For recall target `q`, define the target number of recovered positives as:
|
|
150
|
+
|
|
151
|
+
```text
|
|
152
|
+
T = ceil(q x P)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
With `P = 10` and `q = 0.20`:
|
|
156
|
+
|
|
157
|
+
```text
|
|
158
|
+
T = ceil(0.20 x 10) = 2 positives
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Suppose the second positive occurs at rank 3:
|
|
162
|
+
|
|
163
|
+
```text
|
|
164
|
+
rank 1: positive
|
|
165
|
+
rank 2: negative
|
|
166
|
+
rank 3: positive <- target reached
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Then:
|
|
170
|
+
|
|
171
|
+
```text
|
|
172
|
+
raw EP at 20% recall = T / rank_of_Tth_positive
|
|
173
|
+
= 2 / 3
|
|
174
|
+
= 0.67
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
At 50% recall, `T = 5`. If the fifth positive occurs at rank 8:
|
|
178
|
+
|
|
179
|
+
```text
|
|
180
|
+
raw EP at 50% recall = 5 / 8 = 0.625
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Random baseline and adjustment
|
|
184
|
+
|
|
185
|
+
The random baseline is not exactly `P / N` for this metric. The stopping rank
|
|
186
|
+
changes with the random ordering, and taking the reciprocal of that random rank
|
|
187
|
+
introduces a finite-sample effect, especially for small complexes.
|
|
188
|
+
|
|
189
|
+
Let `R_T` be the rank at which the `T`th positive occurs in a random ordering.
|
|
190
|
+
The exact random baseline is:
|
|
191
|
+
|
|
192
|
+
```text
|
|
193
|
+
E[T / R_T]
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
It can be calculated deterministically from the negative-hypergeometric/order-
|
|
197
|
+
statistic distribution:
|
|
198
|
+
|
|
199
|
+
```text
|
|
200
|
+
Pr(R_T = r) = C(r - 1, T - 1) C(N - r, P - T) / C(N, P)
|
|
201
|
+
|
|
202
|
+
random baseline = sum over r of (T / r) Pr(R_T = r)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
where `r` ranges from `T` to `N - P + T`.
|
|
206
|
+
|
|
207
|
+
The same adjustment is then applied:
|
|
208
|
+
|
|
209
|
+
```text
|
|
210
|
+
adjusted EP at recall = (raw score - random baseline)
|
|
211
|
+
/ (1 - random baseline)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
The exact calculation is preferred to Monte Carlo permutations because it is
|
|
215
|
+
deterministic, fast for the expected pFLEX sizes, and has no simulation error or
|
|
216
|
+
random-seed dependency. A numerically stable implementation should use log
|
|
217
|
+
combinations or a recurrence rather than constructing very large binomial
|
|
218
|
+
coefficients directly.
|
|
219
|
+
|
|
220
|
+
### Proposed function
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
adjusted_ep_at_recall(labels, recall_target=0.20)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The result should expose at least:
|
|
227
|
+
|
|
228
|
+
```text
|
|
229
|
+
adjusted_score
|
|
230
|
+
raw_score
|
|
231
|
+
random_baseline
|
|
232
|
+
recall_target
|
|
233
|
+
target_true_positives # T
|
|
234
|
+
rank_at_target # R_T in the observed ranking
|
|
235
|
+
n_candidates # N
|
|
236
|
+
n_positives # P
|
|
237
|
+
achieved_recall
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
The function should accept `recall_target=0.50` without otherwise changing the
|
|
241
|
+
definition.
|
|
242
|
+
|
|
243
|
+
## Edge cases and methodological decisions
|
|
244
|
+
|
|
245
|
+
### Small complexes
|
|
246
|
+
|
|
247
|
+
Small complexes remain intrinsically noisy under either metric. For example, a
|
|
248
|
+
complex with three positive pairs reaches 20% recall after only one positive.
|
|
249
|
+
One rank change can then alter the score substantially.
|
|
250
|
+
|
|
251
|
+
Recommended policy:
|
|
252
|
+
|
|
253
|
+
- keep `T = ceil(q x P)` so the metric retains a clear definition;
|
|
254
|
+
- do not silently replace it with `max(2, T)`, because that changes the actual
|
|
255
|
+
recall target differently for complexes of different sizes;
|
|
256
|
+
- store `P` with every result;
|
|
257
|
+
- visually flag or exclude small complexes in downstream comparison plots;
|
|
258
|
+
- initially evaluate a minimum such as `P >= 10`, but choose and report the
|
|
259
|
+
final threshold through a sensitivity analysis rather than optimizing it for
|
|
260
|
+
the desired result.
|
|
261
|
+
|
|
262
|
+
### Score ties
|
|
263
|
+
|
|
264
|
+
Correlation matrices can contain tied prediction scores. Arbitrarily breaking a
|
|
265
|
+
tie at the cutoff can make a metric depend on row ordering. A tie policy must be
|
|
266
|
+
specified and tested.
|
|
267
|
+
|
|
268
|
+
Proposed policy for BEELINE-style EP: include all rows tied at the score of the
|
|
269
|
+
`K`th prediction, and divide by the resulting effective prefix length. This
|
|
270
|
+
matches the conceptual BEELINE treatment of boundary ties.
|
|
271
|
+
|
|
272
|
+
For fixed-recall EP, tied groups require a separate explicit convention. Two
|
|
273
|
+
reasonable options are:
|
|
274
|
+
|
|
275
|
+
1. include the full tied group containing the target positive and calculate
|
|
276
|
+
precision after that group; or
|
|
277
|
+
2. use the expected precision over all orderings within the tied group.
|
|
278
|
+
|
|
279
|
+
The second option is less order-dependent but more complex. This decision should
|
|
280
|
+
be made before implementation and recorded in the public documentation.
|
|
281
|
+
|
|
282
|
+
### Missing or invalid cases
|
|
283
|
+
|
|
284
|
+
Both functions should return a missing score with a reason when:
|
|
285
|
+
|
|
286
|
+
- there are no eligible candidates;
|
|
287
|
+
- there are no positive pairs;
|
|
288
|
+
- labels contain invalid values; or
|
|
289
|
+
- the requested recall target is outside `(0, 1]`.
|
|
290
|
+
|
|
291
|
+
The functions should not silently convert such cases to zero.
|
|
292
|
+
|
|
293
|
+
### Dataset-to-dataset scatter plots
|
|
294
|
+
|
|
295
|
+
When the same complex is compared between two datasets, differing measured-gene
|
|
296
|
+
coverage can change `P`, `N`, and the meaning of the point. We should decide
|
|
297
|
+
whether pairwise scatter plots will:
|
|
298
|
+
|
|
299
|
+
1. use the intersection of measurable genes for that complex in both datasets,
|
|
300
|
+
giving directly comparable scores; or
|
|
301
|
+
2. retain each dataset's own measurable genes and clearly expose the two values
|
|
302
|
+
of `P` and `N`.
|
|
303
|
+
|
|
304
|
+
The first option is methodologically cleaner for direct pairwise comparison.
|
|
305
|
+
|
|
306
|
+
## Proposed use in pFLEX
|
|
307
|
+
|
|
308
|
+
Use **adjusted BEELINE EP** as the primary per-complex scatter metric because it
|
|
309
|
+
has a recognizable definition and an intuitive complex-size-dependent budget.
|
|
310
|
+
|
|
311
|
+
Use **adjusted EP at 20% recall** as a complementary early-recovery metric, and
|
|
312
|
+
repeat the analysis at 50% recall as a sensitivity check. If dataset similarity,
|
|
313
|
+
dataset orthogonality, and highlighted complexes remain consistent across these
|
|
314
|
+
definitions, the conclusions are less likely to be artifacts of a single
|
|
315
|
+
cutoff.
|
|
316
|
+
|
|
317
|
+
Initially retain AUPRC in exported results for backward compatibility and for
|
|
318
|
+
comparison. Replacing plot defaults and similarity calculations should happen
|
|
319
|
+
only after the new scores have been validated on synthetic rankings, perfect
|
|
320
|
+
rankings, random rankings, reversed rankings, tied scores, and complexes of
|
|
321
|
+
multiple sizes.
|
|
322
|
+
|
|
323
|
+
## Suggested implementation sequence
|
|
324
|
+
|
|
325
|
+
1. Implement a shared validator for a sorted binary label vector.
|
|
326
|
+
2. Implement the exact fixed-recall random baseline helper.
|
|
327
|
+
3. Implement `adjusted_beeline_ep`.
|
|
328
|
+
4. Decide and implement the tie convention for `adjusted_ep_at_recall`.
|
|
329
|
+
5. Implement `adjusted_ep_at_recall` with configurable recall target.
|
|
330
|
+
6. Add unit tests for hand-calculated examples and all edge cases.
|
|
331
|
+
7. Add the raw score, adjusted score, baseline, `N`, `P`, and cutoff metadata to
|
|
332
|
+
the per-module result table.
|
|
333
|
+
8. Compare metric behavior against existing per-module AUPRC across complex
|
|
334
|
+
sizes before changing plotting or similarity defaults.
|
|
335
|
+
|
|
336
|
+
## Questions for review
|
|
337
|
+
|
|
338
|
+
1. Is adjusted BEELINE EP the best primary metric for per-complex scatter plots?
|
|
339
|
+
2. Is 20% recall a defensible primary fixed-recall target, with 50% as a
|
|
340
|
+
sensitivity analysis?
|
|
341
|
+
3. Should direct dataset comparisons always use the intersection of measured
|
|
342
|
+
genes per complex?
|
|
343
|
+
4. What is the fairest and clearest tie policy for fixed-recall EP?
|
|
344
|
+
5. Should very small complexes be filtered by positive-pair count, shown with
|
|
345
|
+
uncertainty, or both?
|
|
346
|
+
6. Is the current pFLEX per-module negative universe scientifically appropriate,
|
|
347
|
+
or should a different candidate universe be defined before adopting these
|
|
348
|
+
metrics?
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# Spec: half-max AUC (early precision, global PRA, pFLEX 2.0)
|
|
2
|
+
|
|
3
|
+
## Why
|
|
4
|
+
|
|
5
|
+
Global AUPRC averages precision over all of recall, so the long low-precision
|
|
6
|
+
tail (thousands of TPs near background) decides the value. A dataset that is
|
|
7
|
+
clearly better among its top predictions can lose to one with a long, weak
|
|
8
|
+
tail. The half-max AUC only scores the high-precision head of the curves, in
|
|
9
|
+
one TP window that is the same for all datasets.
|
|
10
|
+
|
|
11
|
+
## Scope
|
|
12
|
+
|
|
13
|
+
Global PRA only. Standard pFLEX AUPRC (`pra()`, `pr_auc`,
|
|
14
|
+
`corrected_pr_auc`) is unchanged: it is still computed over linear recall.
|
|
15
|
+
Per-module AUPRC and the scatter plots are unchanged.
|
|
16
|
+
|
|
17
|
+
## Axis and weighting
|
|
18
|
+
|
|
19
|
+
The score is computed on the **linear TP axis**: every TP in the window has
|
|
20
|
+
the same weight. There is no log scaling and no other weighting. PR plots use a
|
|
21
|
+
log TP axis for reading only; the numbers do not follow the plot. Any weighting
|
|
22
|
+
(e.g. giving earlier TPs more weight) is a separate method change that has to be
|
|
23
|
+
proposed, described and benchmarked before it is implemented.
|
|
24
|
+
|
|
25
|
+
## Definition
|
|
26
|
+
|
|
27
|
+
Input: the score-sorted global pair table of every dataset from `pra()`.
|
|
28
|
+
Datasets are compared over the same genes (`analysis_genes="shared"`), so they
|
|
29
|
+
have the same number of true positives `P`, pairs `N` and background.
|
|
30
|
+
|
|
31
|
+
1. **Curve points.** One point per true positive: for the k-th TP found at rank
|
|
32
|
+
`r_k`, `p(k) = k / r_k`.
|
|
33
|
+
2. **Start for max and crossing.** `K0 = 1` by default: both are searched from
|
|
34
|
+
the first TP. Option `k0=10` starts the search at the 10th TP, where the log
|
|
35
|
+
plot starts (the first TPs are noisy). The area always starts at TP 1.
|
|
36
|
+
3. **Background.** `p_bg = P / N`, the precision of a random ranking. The same
|
|
37
|
+
for every dataset; if it differs, a warning is logged and the reference
|
|
38
|
+
dataset's value is used for the threshold.
|
|
39
|
+
4. **Max (one for all datasets).** The highest `p(k)`, `k >= K0`, of any
|
|
40
|
+
dataset (default `max_window = 1`: the plain max). Option `max_window = 3`:
|
|
41
|
+
the highest precision held for 3 consecutive TPs (the max of the rolling
|
|
42
|
+
minimum over 3 TPs), so a single early hit cannot set it. The dataset it
|
|
43
|
+
comes from is the reference (`threshold_from`). Option `max_rank = 3`: the
|
|
44
|
+
3rd highest precision instead of the highest, so one or two lucky early
|
|
45
|
+
hits cannot set it (a curve at 1, 1, 1, ... still gives 1). Default 1.
|
|
46
|
+
5. **Threshold (one for all datasets).**
|
|
47
|
+
`p_threshold = p_bg + f × (p_max - p_bg)`, with `f` = 1/2 (half max,
|
|
48
|
+
**default**), 3/4 or 1/4; `fraction="half"`, `"three_quarter"`, `"quarter"`
|
|
49
|
+
or a number in (0, 1). Example: max 0.8, bg 0.1 → 3/4: 0.625, 1/2: 0.45,
|
|
50
|
+
1/4: 0.275. A lower fraction cuts later.
|
|
51
|
+
6. **Crossing (per dataset).** `k_cross` = the first `k >= K0` with
|
|
52
|
+
`p(k) < p_threshold`, interpolated linearly between the last TP above and
|
|
53
|
+
the first TP below. Later recoveries are ignored. `K0` if the curve starts
|
|
54
|
+
below the threshold, `P` if it never falls below.
|
|
55
|
+
7. **Window (one for all datasets).** `K = max k_cross` over the datasets
|
|
56
|
+
(`cut_from` names the dataset). Every dataset is scored over TP 1 to K.
|
|
57
|
+
8. **Score.**
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
area = ∫ p(k) dk from k = 1 to k = K (trapezoid, linear TP axis)
|
|
61
|
+
mean_precision = area / (K - 1) (the average precision over the window)
|
|
62
|
+
score = (mean_precision - p_bg) / (1 - p_bg)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Measured from the background: a random ranking scores 0, a perfect ranking
|
|
66
|
+
1, worse than random below 0 (not clipped). The window and the background
|
|
67
|
+
are the same for every dataset.
|
|
68
|
+
|
|
69
|
+
A dataset that falls below the threshold before `K` keeps its lower precision
|
|
70
|
+
up to `K` in the average, so dropping early lowers its score.
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
- `half_max_auc(prediction, k0=1, threshold=None, k_end=None, max_window=1, fraction=0.5, max_rank=1)`:
|
|
75
|
+
one ranked label vector. Without `threshold` it uses the dataset's own
|
|
76
|
+
threshold; without `k_end` its own crossing as the window end.
|
|
77
|
+
- `half_max_auc_scores(fraction=0.5, k0=1, max_window=1, max_rank=1)`: reads every cached
|
|
78
|
+
`pra` result, computes the shared threshold and window, scores all datasets,
|
|
79
|
+
saves each under `half_max_auc` and returns `{dataset: result}`. Plots and
|
|
80
|
+
`save_results_to_csv()` call it, so scores always reflect the current set of
|
|
81
|
+
datasets.
|
|
82
|
+
|
|
83
|
+
Each result holds:
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
score (mean precision - background) / (1 - background)
|
|
87
|
+
mean_precision average precision over TP 1 to K
|
|
88
|
+
area trapezoid area under precision, TP 1 to K (linear TP axis)
|
|
89
|
+
p_max, k_max this dataset's own max (from K0, held max_window TPs) and its TP
|
|
90
|
+
p_bg background P / N
|
|
91
|
+
fraction 3/4, 1/2 or 1/4
|
|
92
|
+
p_threshold shared threshold; threshold_from: reference dataset
|
|
93
|
+
k_cross this dataset's own crossing
|
|
94
|
+
k_cut shared window end K; cut_from: dataset it comes from
|
|
95
|
+
n_tp (P), n_pairs (N), k0, max_window, max_rank, reason
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Edge cases
|
|
99
|
+
|
|
100
|
+
- `P < K0` (fewer than 10 TPs): score NaN with a reason.
|
|
101
|
+
- No dataset with precision above background: all scores NaN.
|
|
102
|
+
- Every dataset below the threshold from `K0`: `K = K0`, the window is TP 1 to
|
|
103
|
+
`K0`.
|
|
104
|
+
- Window end beyond a dataset's last TP (only with different gene sets): cut
|
|
105
|
+
at its last TP, with a reason.
|
|
106
|
+
- Point exactly on the threshold counts as above.
|
|
107
|
+
|
|
108
|
+
## Plotting
|
|
109
|
+
|
|
110
|
+
- `plot_precision_recall_curve(half_max=True, half_max_fraction=0.5)`: on the
|
|
111
|
+
usual log-TP plot, the shared threshold (grey dashed), each dataset's
|
|
112
|
+
crossing (dot) and the window end K (grey dotted). No area is shaded there,
|
|
113
|
+
because the score is not an area on a log axis.
|
|
114
|
+
- `plot_half_max_auc_scores(fraction=0.5)`: one bar per dataset
|
|
115
|
+
(`half_max_auc_values`, `three_quarter_max_auc_values`,
|
|
116
|
+
`quarter_max_auc_values`).
|
|
117
|
+
- `plot_half_max_explainer(fraction=0.5)` (optional): step-by-step figure on
|
|
118
|
+
the linear TP axis, with the scored area, the formula and a results table.
|
|
119
|
+
- `save_results_to_csv()` writes `csv/half_max_auc.csv` (default fraction).
|
|
@@ -190,6 +190,8 @@ Global plots:
|
|
|
190
190
|
|
|
191
191
|
- `plot_precision_recall_curve(line_width=1.0, hide_minor_ticks=True)` shows precision against the cumulative number of true-positive gene pairs. This is the main global performance curve; higher precision at the same true-positive count means better ranking.
|
|
192
192
|
- `plot_auc_scores()` shows one bar per dataset using the global AUPRC value saved by `pra()`. Higher AUPRC means the ranked gene-pair network recovers functional-standard pairs earlier in the ranking.
|
|
193
|
+
- `plot_half_max_auc_scores(fraction=0.5)` shows one bar per dataset with the half-max AUC, an early-precision score. Global AUPRC averages precision over all of recall, so a long low-precision tail can outweigh a short high-precision head. The half-max AUC scores only the head, in one TP window for all datasets: take the highest precision of any dataset, searched from the first TP (options: `k0=10` to start at the 10th TP, `max_window=3` to require it for 3 consecutive TPs, `max_rank=3` to take the 3rd highest precision), and the background (global prevalence, P/N); the threshold is background + `fraction` of the distance to that max (1/2 by default, or 3/4 or 1/4: `fraction="half"`, `"three_quarter"`, `"quarter"`); the window runs from the first TP to the latest TP where any dataset's precision first falls below the threshold. The score is each dataset's average precision over that window on the linear TP axis (every TP the same weight, no log scaling), measured from the background: (mean - background) / (1 - background), so a random ranking scores 0 and a perfect one 1. Standard AUPRC is unchanged. `flex.half_max_auc_scores(fraction=...)` returns the values. `plot_precision_recall_curve(half_max=True, half_max_fraction=...)` marks the threshold (grey dashed), each dataset's crossing (dot) and the window end (grey dotted) on the usual log-TP plot, without shading. Half-max values are exported to `csv/half_max_auc.csv`. Definition: `docs/half-max-auc-spec.md`.
|
|
194
|
+
- `plot_half_max_explainer(dataset_names=None, fraction=0.5)` (optional) draws a step-by-step explanation of the score for sharing, on the linear TP axis the score is computed on: the PR curves with the background, the max, the threshold, each dataset's crossing, the single TP line and the scored area marked, and below the plot how the score is calculated, the formula and a table with each dataset's crossing, mean precision, score and AUPRC. Saved as `half_max_explainer` (`three_quarter_max_explainer`, `quarter_max_explainer`) in the output folder.
|
|
193
195
|
|
|
194
196
|
Per-module plots:
|
|
195
197
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pflex"
|
|
3
|
-
version = "
|
|
3
|
+
version = "2.0.0"
|
|
4
4
|
description = "pflex is a benchmarking toolkit for evaluating CRISPR screen results against biological functional standards. The toolkit computes gene-level and module-level performance metrics, helping researchers systematically assess the biological relevance and resolution of their CRISPR screening data."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
from .logging_config import log
|
|
2
2
|
from .utils import dsave, dload
|
|
3
3
|
from .preprocessing import example_input_path, get_example_data_path, load_datasets, get_common_genes, filter_matrix_by_genes, load_functional_standard, filter_duplicate_terms
|
|
4
|
-
from .analysis import initialize, prepare_terms_for_dataset, pra, pra_per_module, fast_corr, perform_corr, is_symmetric, binary, has_mirror_of_first_pair, convert_full_to_half_matrix, drop_mirror_pairs, quick_sort, module_contributions, save_results_to_csv, update_matploblib_config, mpr_prepare, mpr_filter, globalpr_filter
|
|
4
|
+
from .analysis import initialize, prepare_terms_for_dataset, pra, pra_per_module, fast_corr, perform_corr, is_symmetric, binary, has_mirror_of_first_pair, convert_full_to_half_matrix, drop_mirror_pairs, quick_sort, module_contributions, save_results_to_csv, half_max_auc, half_max_auc_scores, update_matploblib_config, mpr_prepare, mpr_filter, globalpr_filter
|
|
5
5
|
from .plotting import (
|
|
6
|
-
plot_precision_recall_curve, plot_aggregated_pra, plot_iqr_pra, plot_all_runs_pra, plot_per_module_scatter,
|
|
6
|
+
plot_precision_recall_curve, plot_half_max_auc_scores, plot_half_max_explainer, plot_aggregated_pra, plot_iqr_pra, plot_all_runs_pra, plot_per_module_scatter,
|
|
7
7
|
plot_per_module_scatter_by_size, plot_module_contributions, plot_significant_modules, plot_auc_scores,
|
|
8
8
|
plot_mpr_module_coverage_curve, plot_mpr_module_auc_scores, plot_mpr_summary,
|
|
9
9
|
plot_mpr_filter, plot_globalpr_filter, plot_panels
|
|
@@ -37,7 +37,7 @@ __all__ = [ "log", "example_input_path", "get_example_data_path", "fast_corr",
|
|
|
37
37
|
"perform_corr", "is_symmetric", "binary", "has_mirror_of_first_pair", "convert_full_to_half_matrix",
|
|
38
38
|
"drop_mirror_pairs", "quick_sort", "module_contributions", "plot_precision_recall_curve",
|
|
39
39
|
"plot_aggregated_pra", "plot_iqr_pra", "plot_all_runs_pra", "plot_per_module_scatter", "plot_per_module_scatter_by_size", "plot_module_contributions",
|
|
40
|
-
"plot_significant_modules", "plot_auc_scores", "plot_mpr_module_auc_scores", "save_results_to_csv", "update_matploblib_config",
|
|
40
|
+
"plot_significant_modules", "plot_auc_scores", "plot_half_max_auc_scores", "plot_half_max_explainer", "plot_mpr_module_auc_scores", "save_results_to_csv", "half_max_auc", "half_max_auc_scores", "update_matploblib_config",
|
|
41
41
|
"mpr_prepare", "mpr_filter", "globalpr_filter", "plot_mpr_filter", "plot_globalpr_filter", "plot_panels",
|
|
42
42
|
"plot_mpr_module_coverage_curve", "plot_mpr_summary", "main"
|
|
43
43
|
]
|