File size: 4,487 Bytes
aa02c28 | 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 | import copy
import json
import random
import wave
from pathlib import Path
import numpy as np
import pytest
from psi.data.seed.third_party_dataset import contained, file_hash, replace_microphone
from psi.data.seed.live_multimodal_overlay import continuous_audio, interaction_task
def fixture(tmp_path):
pcm = np.arange(16000, dtype=np.int16)
audio = tmp_path / 'input.wav'
with wave.open(str(audio), 'wb') as f:
f.setnchannels(1); f.setsampwidth(2); f.setframerate(16000)
f.writeframes(pcm.tobytes())
steps = [dict(step_index=i, states=np.zeros(36), control_label=1,
update_semantic_cache=i in (0,20), semantic_window_index=i//20)
for i in range(21)]
item = dict(steps=steps, microphone_speech_intervals=[dict(start_seconds=.2,end_seconds=.4)],
interaction_mode='turn-taking')
path = tmp_path/'timeline.npz'
np.savez(path, states=np.zeros((21,36)), control_label=np.ones(21,dtype=np.uint8))
recipe = dict(session_id='example', session_type='mixed_short', background_language='en',
interference_intervals=[dict(start_seconds=.1,end_seconds=.7)],
timeline=dict(path=path.name,file_sha256=file_hash(path),action_steps=21),
audio=dict(path=audio.name,sha256=file_hash(audio),sample_rate=16000,channels=1,
sample_width_bytes=2,window_samples=9600,stride_samples=6400,
window_count=2,sample_count=16000),
supervision_targets=[], target_speech_events=[dict(start_sample=3200,end_sample=6400)])
return item,recipe,pcm
def test_exact_pcm_overlap_and_current_pose_preserved(tmp_path):
item,recipe,pcm=fixture(tmp_path)
item['steps'][0]['states'][15]=.8 # Latest raised-arm pose need not match old archive.
actual=replace_microphone(item,recipe,tmp_path)
np.testing.assert_array_equal(continuous_audio(actual),pcm.astype(np.float32)/32768.)
assert actual['steps'][0]['states'][15]==.8
assert interaction_task(actual)=='third-party'
assert actual['interaction_mode']=='turn-taking'
assert 'audio_waveform' not in actual['steps'][1]
def test_reject_timing_drift(tmp_path):
item,recipe,_=fixture(tmp_path)
item['steps'][3]['control_label']=0
with pytest.raises(ValueError,match='timing/supervision drift'):
replace_microphone(item,recipe,tmp_path)
def test_reject_changed_speech_and_audio(tmp_path):
item,recipe,_=fixture(tmp_path)
item['microphone_speech_intervals'][0]['start_seconds']=.3
with pytest.raises(ValueError,match='speech shifted'):
replace_microphone(item,recipe,tmp_path)
item['microphone_speech_intervals'][0]['start_seconds']=.2
recipe['audio']['sha256']='bad'
with pytest.raises(ValueError,match='audio corrupted'):
replace_microphone(item,recipe,tmp_path)
def test_reject_escape(tmp_path):
with pytest.raises(ValueError,match='escapes'):
contained(tmp_path,'../outside.wav')
def test_appended_source_keeps_rng_and_base_membership(monkeypatch,tmp_path):
import psi.data.seed.third_party_dataset as module
recipe=dict(session_id='x',session_type='single_short',timeline_seed=123,
composition_variant_id=4,composition_salt=5,
turns=[dict(row_id=1,motion_filename='motion',audio_dir='audio',voice_name='voice',
motion_dir=str(tmp_path),motion_sha256='unused')],
timeline=dict(action_steps=31))
class Raw:
records=[dict(row_id=1,filename='motion',audio_dir='audio',motion_dir=str(tmp_path))]
sessions=((records[0],),)
def __len__(self):return 1
def __getitem__(self,index):return {'original':index}
def estimated_stream_steps(self,index):return 10
def _build_source(self,index):return {'random':random.random()}
monkeypatch.setattr(module,'read_recipes',lambda *a:(recipe,))
monkeypatch.setattr(module,'replace_microphone',lambda item,*a:item)
monkeypatch.setattr(module,'verify_primitive',lambda *a:None)
for name in ('joint_pos.csv','body_pos.csv','body_quat.csv'):
(tmp_path/name).touch()
raw=Raw(); ds=module.ThirdPartyAugmentedDataset(raw,tmp_path,'unused','train')
state=random.getstate()
assert ds[1]==ds[1]
assert random.getstate()==state
assert ds[0]==raw[0]
assert len(raw.sessions)==1 and len(ds)==2
assert ds.estimated_stream_steps(1)==31
|