RegalFire's picture
RegalFire verified synthetic computational benchmark v1
7424015 verified
Raw History Blame Contribute Delete
5.37 kB
from pathlib import Path
import sys
_test_root=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(_test_root/'generator'))
sys.path.insert(0,str(_test_root/'validator'))
import copy
import random
import tempfile
from pathlib import Path
import unittest
from jsonschema import Draft202012Validator
import make
import check
from common import *
class OracleTests(unittest.TestCase):
def test_all_families_and_corrupt_labels(self):
for kind in NAMES:
for i,family in enumerate(FAMILIES[kind]):
with self.subTest(kind=kind,family=family):
for attempt in range(100):
try:inp,out=make.MAKERS[kind](random.Random(12345+i+attempt*100),family);break
except ValueError:continue
check.CHECKS[kind](inp,out)
bad=copy.deepcopy(out)
if kind=='nav':bad['reachability_changed']=not bad['reachability_changed']
elif kind=='coord':bad['optimal_makespan']=10000
elif kind=='reads':bad['passed_filter']=not bad['passed_filter']
elif kind=='align':bad['edit_distance']+=1
else:bad['union_overlap_bases']+=1
with self.assertRaises(ValueError):check.CHECKS[kind](inp,bad)
def test_myers_against_exhaustive_small_cases(self):
import itertools
sequences=['']+[''.join(chars) for n in range(1,5) for chars in itertools.product('AC',repeat=n)]
for a in sequences:
for b in sequences:self.assertEqual(make.alignment(a,b)[0],check.myers(a,b))
def test_interval_touching_and_coordinate_conversion(self):
inp={'contigs':{'synthetic_A':100},'features':[{'contig':'synthetic_A','start':1,'end':10,'basis':'one_closed','strand':'+'},{'contig':'synthetic_A','start':10,'end':20,'basis':'zero_half_open','strand':'+'},{'contig':'synthetic_A','start':20,'end':30,'basis':'zero_half_open','strand':'-'}],
'query':{'contig':'synthetic_A','start':10,'end':20,'basis':'zero_half_open','strand':'+'},'same_strand':True}
expected={'query_bed':[10,20],'matches':[{'feature_index':1,'overlap_bases':10}],'union_overlap_bases':10,'query_length':10}
self.assertEqual(make.interval_label(inp),expected);check.intervals(inp,expected)
def test_phred_and_threshold_boundaries(self):
inp={'fastq':'@synthetic_read\nACGN\n+\n555#','policy':{'min_length':3,'max_n':0,'mean_q_min':20,'trim_q_min':10}}
out=make.read_label(inp['fastq'],inp['policy']);self.assertEqual(out['trimmed_sequence'],'ACG');self.assertTrue(out['passed_filter']);check.reads(inp,out)
def test_grid_symmetry_semantic_identity(self):
inp,out=make.nav(random.Random(12),'gap_close');fp,group=semantic('nav',inp)
grid,p,c=transforms(inp['grid'],[inp['start'],inp['goal'],inp['edit']],inp['costs'])[3]
other={'grid':grid,'start':list(p[0]),'goal':list(p[1]),'edit':list(p[2]),'costs':c}
self.assertEqual((fp,group),semantic('nav',other))
def test_schema_rejects_wrong_label_types(self):
inp,out=make.align(random.Random(3),'clustered_indels');fp,group=semantic('align',inp)
row={'id':'ALIGN-0000000','index':0,'seed':1,'family':'clustered_indels','input':inp,'expected':out,'fingerprint':fp,'group_id':group,'split':split(group),'provenance':provenance('align')}
Draft202012Validator(schema('align')).validate(row)
row['expected']['within_threshold']=1
with self.assertRaises(Exception):Draft202012Validator(schema('align')).validate(row)
def test_resume_and_byte_reproduction(self):
with tempfile.TemporaryDirectory() as tmp:
out=Path(tmp)/'a';make.generate('reads',out,100,987)
original=(out/'dataset.jsonl').read_bytes();make.generate('reads',out,100,987,resume=True)
self.assertEqual(original,(out/'dataset.jsonl').read_bytes())
second=Path(tmp)/'b';make.generate('reads',second,100,987)
self.assertEqual(original,(second/'dataset.jsonl').read_bytes())
with self.assertRaises(ValueError):make.generate('reads',out,100,988,resume=True)
def test_resume_after_missing_final_file_and_partial_commit(self):
with tempfile.TemporaryDirectory() as tmp:
out=Path(tmp)/'run';make.generate('reads',out,1000,991)
original=(out/'dataset.jsonl').read_bytes()
(out/'dataset.jsonl').unlink();(out/'chunks/0000500.jsonl').unlink()
make.generate('reads',out,1000,991,resume=True)
self.assertEqual(original,(out/'dataset.jsonl').read_bytes())
def test_missing_file_and_reject_preservation(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);report=check.validate('reads',root/'missing.jsonl',100,root/'report')
self.assertEqual(report['status'],'FAIL');self.assertTrue((root/'report/rejected.jsonl').read_text())
path=root/'broken.jsonl';path.write_text('{broken json}\n',encoding='utf-8')
report=check.validate('reads',path,100,root/'report2');self.assertEqual(report['rejected'],1)
self.assertIn('broken json',(root/'report2/rejected.jsonl').read_text())
if __name__=='__main__':unittest.main()