File size: 6,516 Bytes
fb0074a | 1 2 3 4 5 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 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 | import io
from pathlib import Path
from types import SimpleNamespace
import numpy as np
import pytest
from scipy.io import wavfile
from external_visual import JoinedReader, exclusion
from session_overlay import ExternalOverlay, timing
from common import sha256
def test_split_archive_reader(tmp_path):
paths=[]
for i,b in enumerate((b'abc',b'',b'defgh')):
p=tmp_path/str(i);p.write_bytes(b);paths.append(p)
with JoinedReader(paths) as stream, io.BufferedReader(stream,2) as buffer:
assert buffer.read(4)==b'abcd'
assert buffer.read()==b'efgh'
def test_timestamp_not_relocated_to_fit():
row=dict(question_time=6.13,duration_seconds=20)
assert timing(row,[(3,9)],2,12,11.8)==(4.13,6.13)
assert timing(row,[(1,4),(7,12)],2,12,11.8) is None
assert timing({**row,'duration_seconds':12},[(3,9)],2,12,11.8) is None
assert timing(row,[(3,9)],2,12,6.2) is None # room for reply windows
def test_original_future_tasks_never_admitted():
assert exclusion(dict(task='FPD'))=='anticipation_or_proactive_task_not_prefix_qa'
assert exclusion(dict(task='CRR'))=='anticipation_or_proactive_task_not_prefix_qa'
assert exclusion(dict(task='EPM',video='Ego4D/clips/x.mp4'))=='source_access_not_confirmed'
def test_overlay_keeps_motion_controls_and_original_events(tmp_path):
wave=(np.sin(np.arange(32000)*.05)*2000).astype(np.int16)
path=tmp_path/'question.wav';wavfile.write(path,16000,wave)
steps=[dict(step_index=i,semantic_window_index=i//20,update_semantic_cache=i%20==0,
audio_waveform=np.zeros(9600,np.float32),states=np.zeros(36,np.float32),
actions=np.full((30,40),i,np.float32),control_label=1) for i in range(600)]
original=dict(source_id='immutable_motion',steps=steps,session_voice_name='female_01_en',
interaction_mode='turn-taking',microphone_speech_intervals=[dict(start_seconds=0,end_seconds=2)])
overlay=ExternalOverlay(SimpleNamespace(motion_lead_seconds=1.,motion_tail_seconds=.8,question_fade_seconds=.02))
overlay.by_voice={'female_01_en':[dict(question_time=6.13,duration_seconds=20,
asset_id='ovo_test',question_audio_path=str(path),question_audio_sha256=sha256(path),
video_path='video.mp4',visual_features_path='features.pt',question='What is visible?',answer='A cup.',
data_domain='OVO-Bench',external_id='ovo_test',original_question='What is visible?',
original_answer='A cup.',options=['A cup.','A bowl.'],correct_option=0,
video_sha256='sha',visual_receipt='review.json')]}
result=overlay.compose(original)
assert result['multimodal_overlay']['mode']=='qa'
assert abs(result['multimodal_overlay']['question_end']-6.13)<1/16000
assert result['microphone_speech_intervals']==original['microphone_speech_intervals']
for before,after in zip(steps,result['steps']):
assert after['actions'] is before['actions']
assert after['states'] is before['states']
assert after['control_label']==before['control_label']
assert not before['audio_waveform'].any()
assert any(s['audio_waveform'].any() for s in result['steps'])
def test_unselected_cleanup_is_exact_and_preserves_other_files(tmp_path):
from prepare import discard_unselected
row=dict(dataset_index=7,sample_id='hfd_session_00007')
for suffix in ('.json','.pt','_microphone.wav','_reference.npz'):
(tmp_path/(row['sample_id']+suffix)).write_bytes(b'generated')
keep=tmp_path/'keep.txt';keep.write_text('user evidence')
discard_unselected(row,tmp_path)
assert list(tmp_path.iterdir())==[keep]
with pytest.raises(ValueError):
discard_unselected(dict(dataset_index=7,sample_id='../keep'),tmp_path)
def test_revision_refuses_changed_inputs(tmp_path):
from revision import link_checked, same_asset
source=tmp_path/'old';source.write_bytes(b'original source')
dest=tmp_path/'new'
with pytest.raises(ValueError):link_checked(source,dest,'wrong hash')
assert not dest.exists()
link_checked(source,dest,sha256(source))
assert source.stat().st_ino==dest.stat().st_ino
asset=dict(asset_id='q',voice='v',video_sha256='video',visual_features_sha256='feature',
question_audio_sha256='pcm',visual_receipt_sha256='review',question_time=5,
question='Q',answer='A',original_question='Q',original_answer='A')
row=dict(voice='v',overlay=dict(asset_id='q',question='Q',answer='A',external_benchmark=dict(
source_video_sha256='video',feature_sha256='feature',question_audio_sha256='pcm',
review_receipt_sha256='review',question_time=5,original_question='Q',original_answer='A')))
assert same_asset(row,asset)
assert not same_asset(row,{**asset,'answer':'different'})
assert not same_asset(row,{**asset,'question_time':6})
def test_revision_reuses_only_complete_identical_results(tmp_path):
import json
from common import write_json
from revision import reuse_generated
old=tmp_path/'old';new=tmp_path/'new';out=tmp_path/'old_outputs';dest=tmp_path/'new_outputs'
for folder in (old,new,out):folder.mkdir()
rows=[]
for sid in ('unchanged','changed'):
row=dict(sample_id=sid)
for field,suffix in [('source','pt'),('reference','npz'),('microphone','wav')]:
name=sid+'_'+field+'.'+suffix
(old/name).write_bytes((sid+field).encode());(new/name).write_bytes((sid+field).encode())
row[field+'_file']=name;row[field+'_sha256']=sha256(old/name)
rows.append(row)
previous=dict(run='same',run_config_sha256='config',rows=rows)
write_json(old/'manifest.json',previous)
after=json.loads(json.dumps(previous));after['rows'][1]['source_sha256']='different'
write_json(new/'manifest.json',after)
identity=dict(cohort_sha256=sha256(old/'manifest.json'),run='same',step=1,native_speech=True)
write_json(out/'identity.json',identity)
for row in rows:
sid=row['sample_id'];(out/(sid+'.npz')).write_bytes(b'motion')
write_json(out/(sid+'.json'),dict(**identity,answers=[]))
reuse_generated(old,new,out,dest)
assert (dest/'unchanged.npz').exists() and not (dest/'changed.npz').exists()
result=json.loads((dest/'unchanged.json').read_text())
assert result['cohort_sha256']==sha256(new/'manifest.json')
assert result['reuse_provenance']['original_cohort_sha256']==sha256(old/'manifest.json')
assert json.loads((out/'unchanged.json').read_text())==dict(**identity,answers=[])
|