bench.py 2.32 KB
Newer Older
1
2
3
4
5
6
import argparse
import os
import glob
import time
import statistics
import json
7
8
9
import sys

sys.path.append(os.path.realpath(os.path.join(__file__, "..", "..", "..")))
10
11
12
13

import tqdm

from pyrodigal import Nodes, Sequence
14
from pyrodigal._pyrodigal import METAGENOMIC_BINS, ConnectionScorer
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
from pyrodigal.tests.fasta import parse


parser = argparse.ArgumentParser()
parser.add_argument("-r", "--runs", default=10, type=int)
parser.add_argument("-d", "--data", required=True)
parser.add_argument("-o", "--output", required=True)
args = parser.parse_args()


def score_connections(nodes, scorer, tinf):
    scorer.index(nodes)
    for i in range(500, len(nodes)):
        # compute boundary
        j = 0 if i < 500 else i - 500
        # score connections without fast-indexing skippable nodes
        scorer.compute_skippable(j, i)
        scorer.score_connections(nodes, j, i, tinf, final=True)


results = dict(results=[])
for filename in tqdm.tqdm(glob.glob(os.path.join(args.data, "*.fna"))):

    # load sequence
    with open(filename) as f:
        record = next(parse(f))
    seq = Sequence.from_string(record.seq)
    tinf = METAGENOMIC_BINS[0].training_info

    # create nodes
    nodes = Nodes()
46
    nodes.extract(seq, translation_table=tinf.translation_table)
47
48

    # run connection scoring
49
    for backend in ["avx", "sse", "generic", None]:
50
        times = []
51
        for run in tqdm.tqdm(range(args.runs), desc=str(backend), leave=False):
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
            # initialize scorer
            scorer = ConnectionScorer(backend=backend)
            scorer_nodes = nodes.copy()
            # time how long it takes to score connections
            t1 = time.time()
            score_connections(scorer_nodes, scorer, tinf)
            t2 = time.time()
            # record runtime
            times.append(t2 - t1)
        # store benchmark result
        results["results"].append({
            "sequence": os.path.basename(filename),
            "backend": backend,
            "node_count": len(nodes),
            "nucleotide_count": len(seq),
            "times": times,
            "mean": statistics.mean(times),
            "stddev": statistics.stdev(times),
            "median": statistics.median(times),
            "min": min(times),
            "max": max(times),
        })


with open(args.output, "w") as f:
    json.dump(results, f, sort_keys=True, indent=4)