gReLU 1.0.1__py3-none-any.whl
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.
- gReLU-1.0.1.dist-info/LICENSE.txt +21 -0
- gReLU-1.0.1.dist-info/METADATA +106 -0
- gReLU-1.0.1.dist-info/RECORD +58 -0
- gReLU-1.0.1.dist-info/WHEEL +5 -0
- gReLU-1.0.1.dist-info/top_level.txt +1 -0
- grelu/__init__.py +16 -0
- grelu/data/__init__.py +0 -0
- grelu/data/augment.py +227 -0
- grelu/data/dataset.py +1013 -0
- grelu/data/preprocess.py +710 -0
- grelu/data/utils.py +63 -0
- grelu/design.py +287 -0
- grelu/interpret/__init__.py +0 -0
- grelu/interpret/motifs.py +319 -0
- grelu/interpret/score.py +390 -0
- grelu/io/__init__.py +136 -0
- grelu/io/bed.py +97 -0
- grelu/io/bigwig.py +98 -0
- grelu/io/fasta.py +48 -0
- grelu/io/genome.py +93 -0
- grelu/io/meme.py +115 -0
- grelu/lightning/__init__.py +1097 -0
- grelu/lightning/losses.py +77 -0
- grelu/lightning/metrics.py +146 -0
- grelu/model/__init__.py +0 -0
- grelu/model/blocks.py +761 -0
- grelu/model/heads.py +135 -0
- grelu/model/layers.py +399 -0
- grelu/model/models.py +692 -0
- grelu/model/position.py +80 -0
- grelu/model/trunks/__init__.py +338 -0
- grelu/model/trunks/borzoi.py +166 -0
- grelu/model/trunks/enformer.py +277 -0
- grelu/model/trunks/explainn.py +119 -0
- grelu/resources/__init__.py +138 -0
- grelu/resources/__pycache__/__init__.cpython-310.pyc +0 -0
- grelu/resources/blacklists/encode/hg19-blacklist.v2.bed +834 -0
- grelu/resources/blacklists/encode/hg38-blacklist.v2.bed +636 -0
- grelu/resources/blacklists/encode/mm10-blacklist.v2.bed +3435 -0
- grelu/resources/blacklists/mito_combined/hg19.full.blacklist.bed +902 -0
- grelu/resources/blacklists/mito_combined/hg19_mm10_c.full.blacklist.bed +1425 -0
- grelu/resources/blacklists/mito_combined/hg38.full.blacklist.bed +820 -0
- grelu/resources/blacklists/mito_combined/mm10.full.blacklist.bed +523 -0
- grelu/resources/blacklists/mito_combined/mm9.full.blacklist.bed +3415 -0
- grelu/resources/meme/JASPAR2022_CORE_non-redundant_pfms_meme.txt +32224 -0
- grelu/resources/meme/jvierstra_consensus_pwms.meme +10405 -0
- grelu/sequence/__init__.py +0 -0
- grelu/sequence/format.py +428 -0
- grelu/sequence/metrics.py +105 -0
- grelu/sequence/mutate.py +252 -0
- grelu/sequence/utils.py +452 -0
- grelu/transforms/__init__.py +0 -0
- grelu/transforms/label_transforms.py +50 -0
- grelu/transforms/prediction_transforms.py +248 -0
- grelu/transforms/seq_transforms.py +132 -0
- grelu/utils.py +153 -0
- grelu/variant.py +389 -0
- grelu/visualize.py +740 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Custom loss functions
|
|
3
|
+
"""
|
|
4
|
+
import torch
|
|
5
|
+
import torch.nn.functional as F
|
|
6
|
+
from torch import Tensor, nn
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PoissonMultinomialLoss(nn.Module):
|
|
10
|
+
"""
|
|
11
|
+
Possion decomposition with multinomial specificity term.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
total_weight: Weight of the Poisson total term.
|
|
15
|
+
eps: Added small value to avoid log(0). Only needed if log_input = False.
|
|
16
|
+
log_input: If True, the input is transformed with torch.exp to produce predicted
|
|
17
|
+
counts. Otherwise, the input is assumed to already represent predicted
|
|
18
|
+
counts.
|
|
19
|
+
reduction: "mean" or "none".
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
total_weight: float = 1,
|
|
25
|
+
eps: float = 1e-7,
|
|
26
|
+
log_input: bool = True,
|
|
27
|
+
reduction: str = "mean",
|
|
28
|
+
) -> None:
|
|
29
|
+
super().__init__()
|
|
30
|
+
self.eps = eps
|
|
31
|
+
self.total_weight = total_weight
|
|
32
|
+
self.log_input = log_input
|
|
33
|
+
self.reduction = reduction
|
|
34
|
+
|
|
35
|
+
def forward(self, input: Tensor, target: Tensor) -> Tensor:
|
|
36
|
+
"""
|
|
37
|
+
Loss computation
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
input: Tensor of shape (B, T, L)
|
|
41
|
+
target: Tensor of shape (B, T, L)
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
Loss value
|
|
45
|
+
"""
|
|
46
|
+
seq_len = target.shape[-1]
|
|
47
|
+
|
|
48
|
+
if self.log_input:
|
|
49
|
+
input = torch.exp(input)
|
|
50
|
+
else:
|
|
51
|
+
input += self.epsilon
|
|
52
|
+
|
|
53
|
+
# Assuming count predictions
|
|
54
|
+
total_target = target.sum(axis=-1)
|
|
55
|
+
total_input = input.sum(axis=-1)
|
|
56
|
+
|
|
57
|
+
# total count poisson loss, mean across targets
|
|
58
|
+
poisson_term = F.poisson_nll_loss(
|
|
59
|
+
total_input, total_target, log_input=False, reduction="none"
|
|
60
|
+
) # B x T
|
|
61
|
+
poisson_term /= seq_len
|
|
62
|
+
|
|
63
|
+
# Get multinomial probabilities
|
|
64
|
+
p_input = input / total_input
|
|
65
|
+
log_p_input = torch.log(p_input)
|
|
66
|
+
|
|
67
|
+
# multinomial loss
|
|
68
|
+
multinomial_dot = -torch.multiply(target, log_p_input) # B x T x L
|
|
69
|
+
multinomial_term = multinomial_dot.mean(axis=-1) # B x T
|
|
70
|
+
|
|
71
|
+
# Combine
|
|
72
|
+
loss = multinomial_term + self.total_weight * poisson_term
|
|
73
|
+
|
|
74
|
+
if self.reduction == "mean":
|
|
75
|
+
return loss.mean()
|
|
76
|
+
else:
|
|
77
|
+
return loss
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Metrics to measure performance of a predictive sequence model
|
|
3
|
+
These metrics should produce an output value per task or averaged across tasks
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import torch
|
|
8
|
+
import torchmetrics
|
|
9
|
+
from sklearn.metrics import precision_recall_curve
|
|
10
|
+
from torchmetrics import Metric
|
|
11
|
+
from torchmetrics.utilities.checks import _check_same_shape
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BestF1(Metric):
|
|
15
|
+
"""
|
|
16
|
+
Metric class to calculate the best F1 score for each task.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
num_labels: Number of tasks
|
|
20
|
+
average: If true, return the average metric across tasks.
|
|
21
|
+
Otherwise, return a separate value for each task
|
|
22
|
+
|
|
23
|
+
As input to forward and update the metric accepts the following input:
|
|
24
|
+
preds: Probabilities of shape (N, n_tasks, L)
|
|
25
|
+
target: Ground truth labels of shape (N, n_tasks, L)
|
|
26
|
+
|
|
27
|
+
As output of forward and compute the metric returns the following output:
|
|
28
|
+
output: A tensor with the best F1 score
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, num_labels: int = 1, average: bool = True) -> None:
|
|
32
|
+
super().__init__()
|
|
33
|
+
self.add_state("preds", default=torch.empty(0, num_labels), dist_reduce_fx=None)
|
|
34
|
+
self.add_state(
|
|
35
|
+
"target", default=torch.empty(0, num_labels), dist_reduce_fx=None
|
|
36
|
+
)
|
|
37
|
+
self.average = average
|
|
38
|
+
|
|
39
|
+
def update(self, preds: torch.Tensor, target: torch.Tensor) -> None:
|
|
40
|
+
_check_same_shape(preds, target)
|
|
41
|
+
preds = preds.swapaxes(1, 2).flatten(start_dim=0, end_dim=1) # NxL, n_tasks
|
|
42
|
+
target = target.swapaxes(1, 2).flatten(start_dim=0, end_dim=1) # NxL, n_tasks
|
|
43
|
+
self.preds = torch.vstack([self.preds, preds])
|
|
44
|
+
self.target = torch.vstack([self.target, target])
|
|
45
|
+
|
|
46
|
+
def compute(self) -> torch.Tensor:
|
|
47
|
+
best_f1_list = []
|
|
48
|
+
|
|
49
|
+
# Compute best F1 per task
|
|
50
|
+
for task in range(self.preds.shape[1]):
|
|
51
|
+
y_pred = self.preds[:, task].detach().cpu().numpy()
|
|
52
|
+
y_true = self.target[:, task].detach().cpu().numpy().astype(int)
|
|
53
|
+
prec, rec, thre = precision_recall_curve(y_true, y_pred)
|
|
54
|
+
f1_scores = 2 * rec * prec / (rec + prec + 1e-20)
|
|
55
|
+
best_f1 = np.nanmax(f1_scores)
|
|
56
|
+
best_f1_list.append(best_f1)
|
|
57
|
+
|
|
58
|
+
# Convert to tensor for consistency
|
|
59
|
+
output = torch.tensor(best_f1_list).type(torch.float)
|
|
60
|
+
|
|
61
|
+
# Average over tasks if required
|
|
62
|
+
if self.average:
|
|
63
|
+
return output.mean()
|
|
64
|
+
else:
|
|
65
|
+
return output
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class MSE(Metric):
|
|
69
|
+
"""
|
|
70
|
+
Metric class to calculate the MSE for each task.
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
num_outputs: Number of tasks
|
|
74
|
+
average: If true, return the average metric across tasks.
|
|
75
|
+
Otherwise, return a separate value for each task
|
|
76
|
+
|
|
77
|
+
As input to forward and update the metric accepts the following input:
|
|
78
|
+
preds: Predictions of shape (N, n_tasks, L)
|
|
79
|
+
target: Ground truth labels (N, n_tasks, L)
|
|
80
|
+
|
|
81
|
+
As output of forward and compute the metric returns the following output:
|
|
82
|
+
output: A tensor with the MSE
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
def __init__(self, num_outputs: int = 1, average: bool = True) -> None:
|
|
86
|
+
super().__init__()
|
|
87
|
+
self.add_state(
|
|
88
|
+
"sum_squared_error", default=torch.zeros(num_outputs), dist_reduce_fx="sum"
|
|
89
|
+
)
|
|
90
|
+
self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum")
|
|
91
|
+
self.average = average
|
|
92
|
+
|
|
93
|
+
def update(self, preds: torch.Tensor, target: torch.Tensor) -> None:
|
|
94
|
+
_check_same_shape(preds, target)
|
|
95
|
+
if target.dim() > 1:
|
|
96
|
+
self.total += target.shape[0]
|
|
97
|
+
else:
|
|
98
|
+
self.total += len(target)
|
|
99
|
+
|
|
100
|
+
diff = preds - target # (N, n_tasks, L)
|
|
101
|
+
self.sum_squared_error += diff.square().sum(axis=0).mean(axis=-1)
|
|
102
|
+
|
|
103
|
+
def compute(self) -> torch.Tensor:
|
|
104
|
+
# Compute the mean squared error
|
|
105
|
+
output = self.sum_squared_error / self.total
|
|
106
|
+
|
|
107
|
+
# Average across tasks if needed
|
|
108
|
+
if self.average:
|
|
109
|
+
return output.mean()
|
|
110
|
+
else:
|
|
111
|
+
return output
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class PearsonCorrCoef(Metric):
|
|
115
|
+
"""
|
|
116
|
+
Metric class to calculate the Pearson correlation coefficient for each task.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
num_outputs: Number of tasks
|
|
120
|
+
average: If true, return the average metric across tasks.
|
|
121
|
+
Otherwise, return a separate value for each task
|
|
122
|
+
|
|
123
|
+
As input to forward and update the metric accepts the following input:
|
|
124
|
+
preds: Predictions of shape (N, n_tasks, L)
|
|
125
|
+
target: Ground truth labels of shape (N, n_tasks, L)
|
|
126
|
+
|
|
127
|
+
As output of forward and compute the metric returns the following output:
|
|
128
|
+
output: A tensor with the Pearson coefficient.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
def __init__(self, num_outputs: int = 1, average: bool = True) -> None:
|
|
132
|
+
super().__init__()
|
|
133
|
+
self.pearson = torchmetrics.PearsonCorrCoef(num_outputs=num_outputs)
|
|
134
|
+
self.average = average
|
|
135
|
+
|
|
136
|
+
def update(self, preds: torch.Tensor, target: torch.Tensor) -> None:
|
|
137
|
+
preds = preds.swapaxes(1, 2).flatten(start_dim=0, end_dim=1) # Nx L, n_tasks
|
|
138
|
+
target = target.swapaxes(1, 2).flatten(start_dim=0, end_dim=1) # Nx L, n_tasks
|
|
139
|
+
self.pearson.update(preds, target)
|
|
140
|
+
|
|
141
|
+
def compute(self) -> torch.Tensor:
|
|
142
|
+
output = self.pearson.compute()
|
|
143
|
+
if self.average:
|
|
144
|
+
return output.mean()
|
|
145
|
+
else:
|
|
146
|
+
return output
|
grelu/model/__init__.py
ADDED
|
File without changes
|