inference/predictor API¶
DNAPredictor
¶
DNA sequence predictor using fine-tuned models.
This class provides functionality for making predictions using DNA language models. It handles model loading, inference, and result processing.
Source code in dnallm/inference/predictor.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
|
__init__(model, tokenizer, config)
¶
Initialize the predictor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
any
|
Fine-tuned model instance. |
required |
tokenizer
|
any
|
Tokenizer for the model. |
required |
config
|
dict
|
Configuration dictionary containing task settings and inference parameters. |
required |
Source code in dnallm/inference/predictor.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
batch_predict(dataloader, do_pred=True, output_hidden_states=False, output_attentions=False)
¶
Predict for a batch of sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader
|
DataLoader
|
DataLoader object containing sequences. |
required |
do_pred
|
bool
|
Whether to do prediction. |
True
|
output_hidden_states
|
bool
|
Whether to output hidden states. |
False
|
output_attentions
|
bool
|
Whether to output attentions. |
False
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[Tensor, list]
|
A tuple containing: - torch.Tensor: All logits - dict: Predictions dictionary - dict: Embeddings dictionary |
Source code in dnallm/inference/predictor.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
calculate_metrics(logits, labels, plot=False)
¶
Calculate evaluation metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Union[List, Tensor]
|
Model predictions. |
required |
labels
|
Union[List, Tensor]
|
True labels. |
required |
plot
|
bool
|
Whether to plot metrics. |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Dictionary containing evaluation metrics. |
Source code in dnallm/inference/predictor.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
format_output(predictions)
¶
Format output predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions
|
tuple[Tensor, list]
|
Tuple containing predictions. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Dictionary containing formatted predictions. |
Source code in dnallm/inference/predictor.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
generate_dataset(seq_or_path, batch_size=1, seq_col='sequence', label_col='labels', sep=None, fasta_sep='|', multi_label_sep=None, uppercase=False, lowercase=False, keep_seqs=True, do_encode=True)
¶
Generate dataset from sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq_or_path
|
Union[str, List[str]]
|
Single sequence or path to a file containing sequences. |
required |
batch_size
|
int
|
Batch size for DataLoader. |
1
|
seq_col
|
str
|
Column name for sequences. |
'sequence'
|
label_col
|
str
|
Column name for labels. |
'labels'
|
sep
|
str
|
Delimiter for CSV, TSV, or TXT files. |
None
|
fasta_sep
|
str
|
Delimiter for FASTA files. |
'|'
|
multi_label_sep
|
str
|
Delimiter for multi-label sequences. |
None
|
uppercase
|
bool
|
Whether to convert sequences to uppercase. |
False
|
lowercase
|
bool
|
Whether to convert sequences to lowercase. |
False
|
keep_seqs
|
bool
|
Whether to keep sequences in the dataset. |
True
|
do_encode
|
bool
|
Whether to encode sequences. |
True
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
A tuple containing: - Dataset object - DataLoader object |
Raises:
Type | Description |
---|---|
ValueError
|
If input is neither a file path nor a list of sequences. |
Source code in dnallm/inference/predictor.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
logits_to_preds(logits)
¶
Convert model logits to predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
list
|
Model output logits. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[Tensor, list]
|
A tuple containing: - torch.Tensor: Model predictions - list: Human-readable labels |
Raises:
Type | Description |
---|---|
ValueError
|
If task type is not supported. |
Source code in dnallm/inference/predictor.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
plot_attentions(seq_idx=0, layer=-1, head=-1, width=800, height=800, save_path=None)
¶
Plot attention map.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq_idx
|
int
|
Index of the sequence to plot. |
0
|
layer
|
int
|
Layer index to plot. |
-1
|
head
|
int
|
Head index to plot. |
-1
|
width
|
int
|
Width of the plot. |
800
|
height
|
int
|
Height of the plot. |
800
|
save_path
|
Optional[str]
|
Path to save the plot. |
None
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
If no attention weights are available. |
object |
None
|
Attention map visualization if available. |
Source code in dnallm/inference/predictor.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
|
plot_hidden_states(reducer='t-SNE', ncols=4, width=300, height=300, save_path=None)
¶
Embedding visualization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reducer
|
str
|
Dimensionality reduction method to use. |
't-SNE'
|
ncols
|
int
|
Number of columns in the plot grid. |
4
|
width
|
int
|
Width of the plot. |
300
|
height
|
int
|
Height of the plot. |
300
|
save_path
|
Optional[str]
|
Path to save the plot. |
None
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
If no hidden states are available. |
object |
None
|
Embedding visualization if available. |
Source code in dnallm/inference/predictor.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
|
predict_file(file_path, evaluate=False, output_hidden_states=False, output_attentions=False, seq_col='sequence', label_col='labels', sep=None, fasta_sep='|', multi_label_sep=None, uppercase=False, lowercase=False, save_to_file=False, plot_metrics=False)
¶
Predict from a file containing sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path to the file containing sequences. |
required |
evaluate
|
bool
|
Whether to evaluate the predictions. |
False
|
output_hidden_states
|
bool
|
Whether to output hidden states. |
False
|
output_attentions
|
bool
|
Whether to output attentions. |
False
|
seq_col
|
str
|
Column name for sequences. |
'sequence'
|
label_col
|
str
|
Column name for labels. |
'labels'
|
sep
|
str
|
Delimiter for CSV, TSV, or TXT files. |
None
|
fasta_sep
|
str
|
Delimiter for FASTA files. |
'|'
|
multi_label_sep
|
str
|
Delimiter for multi-label sequences. |
None
|
uppercase
|
bool
|
Whether to convert sequences to uppercase. |
False
|
lowercase
|
bool
|
Whether to convert sequences to lowercase. |
False
|
save_to_file
|
bool
|
Whether to save predictions to file. |
False
|
plot_metrics
|
bool
|
Whether to plot metrics. |
False
|
Returns:
Type | Description |
---|---|
Union[tuple, dict]
|
Union[tuple, dict]: Either: - List of dictionaries containing predictions - Tuple of (predictions, metrics) if evaluate=True |
Source code in dnallm/inference/predictor.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
predict_seqs(sequences, evaluate=False, output_hidden_states=False, output_attentions=False, save_to_file=False)
¶
Predict for sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequences
|
Union[str, List[str]]
|
Single sequence or list of sequences. |
required |
evaluate
|
bool
|
Whether to evaluate the predictions. |
False
|
output_hidden_states
|
bool
|
Whether to output hidden states and attentions. |
False
|
output_attentions
|
bool
|
Whether to output attentions. |
False
|
save_to_file
|
bool
|
Whether to save predictions to file. |
False
|
Returns:
Type | Description |
---|---|
Union[tuple, dict]
|
Union[tuple, dict]: Either: - Dictionary containing predictions - Tuple of (predictions, metrics) if evaluate=True |
Source code in dnallm/inference/predictor.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
plot_attention_map(attentions, sequences, tokenizer, seq_idx=0, layer=-1, head=-1, width=800, height=800, save_path=None)
¶
Plot attention map. Args: attentions (tuple): Tuple containing attention weights. sequences (list): List of sequences. tokenizer: Tokenizer object. seq_idx (int): Index of the sequence to plot. layer (int): Layer index. head (int): Head index. width (int): Width of the plot. height (int): Height of the plot. save_path (str): Path to save the plot. Returns: attn_map: Altair chart object.
Source code in dnallm/inference/plot.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|
plot_bars(data, show_score=True, ncols=3, width=200, height=50, bar_width=30, domain=(0.0, 1.0), save_path=None, separate=False)
¶
Plot bar chart. Args: data (dict): Data to be plotted. show_score (bool): Whether to show the score on the plot. ncols (int): Number of columns in the plot. width (int): Width of the plot. height (int): Height of the plot. bar_width (int): Width of the bars in the plot. save_path (str): Path to save the plot. Returns: pbars: Altair chart object.
Source code in dnallm/inference/plot.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
plot_curve(data, show_score=True, width=400, height=400, save_path=None, separate=False)
¶
Plot curve chart. Args: data (dict): Data to be plotted. show_score (bool): Whether to show the score on the plot. width (int): Width of the plot. height (int): Height of the plot. save_path (str): Path to save the plot. Returns: plines: Altair chart object.
Source code in dnallm/inference/plot.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
plot_embeddings(hidden_states, attention_mask, reducer='t-SNE', labels=None, label_names=None, ncols=4, width=300, height=300, save_path=None, separate=False)
¶
Visualize embeddings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
tuple
|
Tuple containing hidden states. |
required |
attention_mask
|
tuple
|
Tuple containing attention mask. |
required |
reducer
|
str
|
Dimensionality reduction method. Options: PCA, t-SNE, UMAP. |
't-SNE'
|
labels
|
list
|
List of labels for the data points. |
None
|
label_names
|
list
|
List of label names. |
None
|
ncols
|
int
|
Number of columns in the plot. |
4
|
width
|
int
|
Width of the plot. |
300
|
height
|
int
|
Height of the plot. |
300
|
save_path
|
str
|
Path to save the plot. |
None
|
separate
|
bool
|
Whether to return separate plots for each layer. |
False
|
Returns: pdots: Altair chart object.
Source code in dnallm/inference/plot.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
plot_muts(data, show_score=False, width=None, height=100, save_path=None)
¶
Visualize mutation effects
Source code in dnallm/inference/plot.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
|
plot_scatter(data, show_score=True, ncols=3, width=400, height=400, save_path=None, separate=False)
¶
Plot scatter chart. Args: data (dict): Data to be plotted. show_score (bool): Whether to show the score on the plot. ncols (int): Number of columns in the plot. width (int): Width of the plot. height (int): Height of the plot. save_path (str): Path to save the plot. Returns: pdots: Altair chart object.
Source code in dnallm/inference/plot.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
prepare_data(metrics, task_type='binary')
¶
Prepare data for plotting. Args: metrics (dict): Dictionary containing model metrics. task_type (str): Task type Returns: tuple: Tuple containing bar data and curve data.
Source code in dnallm/inference/plot.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
save_metrics(metrics, output_dir)
¶
Save metrics to files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metrics
|
Dict
|
Dictionary containing metrics. |
required |
output_dir
|
Path
|
Directory to save metrics. |
required |
Source code in dnallm/inference/predictor.py
591 592 593 594 595 596 597 598 599 600 601 602 |
|
save_predictions(predictions, output_dir)
¶
Save predictions to files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions
|
Dict
|
Dictionary containing predictions. |
required |
output_dir
|
Path
|
Directory to save predictions. |
required |
Source code in dnallm/inference/predictor.py
578 579 580 581 582 583 584 585 586 587 588 589 |
|