Spaces:
Running on Zero
Running on Zero
Download scripts/score.py from Ziyuan-Wang/RIBOSPAN-FM-Inference: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/Ziyuan-Wang/RIBOSPAN-FM-Inference/resolve/main/scripts/score.py
- Command line
-
hf download hf://spaces/Ziyuan-Wang/RIBOSPAN-FM-Inference/scripts/score.py
-
curl -L -o score.py https://huggingface.co/spaces/Ziyuan-Wang/RIBOSPAN-FM-Inference/resolve/main/scripts/score.py
2.72 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Online masked-marginal RNA substitution scoring.""" | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import Any | |
| import numpy as np | |
| from scripts.batching import batches, with_oom_retry | |
| from scripts.logits import MLMScorer, MaskPattern | |
| from scripts.model_io import LoadedRiboSpan | |
| from scripts.requests import Job, Record, Variant, group_variants | |
| class ScoringGroup: | |
| record: Record | |
| start: int | |
| end: int | |
| positions: tuple[int, ...] | |
| variants: tuple[Variant, ...] | |
| def pattern(self) -> MaskPattern: | |
| return MaskPattern(self.record.sequence[self.start : self.end], tuple(pos - self.start for pos in self.positions)) | |
| def _score_log_odds( | |
| scorer: MLMScorer, | |
| group: ScoringGroup, | |
| log_probs: np.ndarray, | |
| ) -> dict[str, float]: | |
| """Sum log-probability differences conditioned on the jointly masked sites.""" | |
| row_by_position = {position: i for i, position in enumerate(group.positions)} | |
| scores = {} | |
| for variant in group.variants: | |
| total = 0.0 | |
| for mutation in variant.mutations: | |
| row = row_by_position[mutation.position] | |
| total += float(log_probs[row, scorer.nt_id(mutation.mt)] - log_probs[row, scorer.nt_id(mutation.wt)]) | |
| if not np.isfinite(total): | |
| raise RuntimeError("non-finite variant score") | |
| scores[variant.label] = total | |
| return scores | |
| def score_job(job: Job, models: dict[str, LoadedRiboSpan]) -> dict[str, Any]: | |
| groups = [ | |
| ScoringGroup(record, start, end, positions, tuple(variants)) | |
| for record in job.records | |
| for (start, end, positions), variants in group_variants(record) | |
| ] | |
| by_id: dict[str, dict[str, float]] = {record.id: {} for record in job.records} | |
| for chunk in batches(groups, key=lambda group: (group.record.model.name, group.end - group.start + 2)): | |
| scorer = MLMScorer(models[chunk[0].record.model.name]) | |
| log_probs = with_oom_retry(lambda part: scorer.log_probs([item.pattern for item in part]), chunk) | |
| for group, values in zip(chunk, log_probs, strict=True): | |
| by_id[group.record.id].update(_score_log_odds(scorer, group, values)) | |
| return {"results": [ | |
| { | |
| "id": record.id, | |
| "model_id": record.model.name, | |
| "variants": [ | |
| { | |
| "variant": variant.label, | |
| "score": by_id[record.id][variant.label], | |
| "window": {"start": variant.start, "end": variant.end}, | |
| } | |
| for variant in record.variants | |
| ], | |
| } | |
| for record in job.records | |
| ]} | |