Datasets:
Download code/dynamic_dataset/repair_native.py from Heiheihaha17/MESH-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 6.29 kB
-
https://huggingface.co/datasets/Heiheihaha17/MESH-Benchmark/resolve/main/code/dynamic_dataset/repair_native.py
- Command line
-
hf download hf://datasets/Heiheihaha17/MESH-Benchmark/code/dynamic_dataset/repair_native.py
-
curl -L -o repair_native.py https://huggingface.co/datasets/Heiheihaha17/MESH-Benchmark/resolve/main/code/dynamic_dataset/repair_native.py
6.29 kB
| """Resample failed trajectories, preserving real speech, duration and room assets.""" | |
| from __future__ import annotations | |
| import argparse,ast,copy | |
| from pathlib import Path | |
| import numpy as np | |
| from .common import * | |
| from .geometry import audit,initial_positions | |
| from .render import sampling_nodes | |
| from .runtime import ensure_compute | |
| def is_near_failure(failure): | |
| prefix='Native direct path direction/delay failed: ' | |
| message=failure.get('error','') | |
| if failure.get('error_type')!='ValueError' or not message.startswith(prefix):return False | |
| try: | |
| angle,delay,distance=ast.literal_eval(message[len(prefix):]) | |
| return 0<=distance<.15 and (angle>3. or delay>.003) | |
| except (ValueError,TypeError,SyntaxError):return False | |
| def minimum_node_distance(scene,step=.2): | |
| smallest=float('inf') | |
| for source in scene['humans']+scene['playback_sources']: | |
| a=position(source,np.array(sampling_nodes(source,step))) | |
| for receiver in scene['robots']: | |
| if source['entity_id']==receiver['entity_id']:continue | |
| b=position(receiver,np.array(sampling_nodes(receiver,step))) | |
| smallest=min(smallest,float(np.linalg.norm(a[:,None,:]-b[None,:,:],axis=-1).min())) | |
| return smallest | |
| def propose(group,revision=None,minimum=.3): | |
| if revision is None:revision=group.get('native_geometry_repair',{}).get('revision',0)+1 | |
| seed=int(uid(group['seed'],'native_geometry_repair',revision),16)%2**32;rng=np.random.default_rng(seed) | |
| base=copy.deepcopy(group['scenes'][0]);base['geometry_audit']={} | |
| for e in base['humans']+base['robots']:e['displacement_m']=[0.,0.,0.] | |
| for outer in range(100): | |
| template=copy.deepcopy(base);reused=initial_positions(template['robots']+template['humans'],group['room'],rng,force=outer>0) | |
| scenes=[] | |
| for branch in BRANCHES: | |
| for trial in range(240): | |
| scene=copy.deepcopy(template);scene['branch']=branch;scene['difficulty']='easy' if branch.startswith('easy') else 'hard' | |
| moving=[scene['humans'][0]['entity_id']] if branch=='easy_human' else [scene['robots'][group['index']%len(scene['robots'])]['entity_id']] if branch=='easy_robot' else [e['entity_id'] for e in scene['humans']+scene['robots']] | |
| scene['moving_entity_ids']=moving | |
| for e in scene['humans']+scene['robots']: | |
| if e['entity_id'] not in moving:continue | |
| length=float(rng.uniform(2.005,min(2.7,e['motion_duration_s']*.8/1.875-.005)));angle=float(rng.uniform(-np.pi,np.pi)) | |
| e['displacement_m']=[length*np.cos(angle),0.,length*np.sin(angle)] | |
| for source in scene['playback_sources']: | |
| receiver=next(r for r in scene['robots'] if r['entity_id']==source['entity_id']) | |
| for key in ['initial_xyz_m','displacement_m','motion_start_s','motion_duration_s','yaw_deg']:source[key]=copy.deepcopy(receiver[key]) | |
| check=audit(scene,group['room'],full_visibility=False) | |
| if check is None:continue | |
| separation=minimum_node_distance(scene) | |
| if separation<minimum:continue | |
| check=audit(scene,group['room'],full_visibility=True) | |
| if check is None:continue | |
| scene['geometry_audit']=dict(**check,minimum_source_receiver_node_distance_m=separation) | |
| scene['initial_geometry_reused']=bool(base['initial_geometry_reused'] and outer==0 and reused) | |
| scene['geometry_attempt']=outer;scene['motion_attempt']=trial;scenes.append(scene);break | |
| else:break | |
| if len(scenes)==4: | |
| result=copy.deepcopy(group);new_id=uid(group['group_id'],'native_geometry_repair',revision) | |
| result.update(group_id=new_id,scenes=scenes,native_geometry_repair=dict(parent_group_id=group['group_id'],revision=revision,seed=seed, | |
| minimum_source_receiver_node_distance_m=minimum,preserves_speech_room_duration=True)) | |
| for scene in scenes: | |
| scene['scene_group_id']=new_id;scene['scene_id']=uid(new_id,scene['branch'],[e['initial_xyz_m']+e['displacement_m'] for e in scene['humans']+scene['robots']]) | |
| return result | |
| raise ValueError('Cannot sample stable two-time RIR geometry in this frozen room') | |
| def repair(root,minimum=.3,indices=None): | |
| ensure_compute();groups=list(rows(root/'private/recipes.jsonl'));replaced={};records=[] | |
| for i,group in enumerate(groups): | |
| if indices is not None and group['index'] not in indices:continue | |
| failure=root/'reports/failures'/(group['group_id']+'.json') | |
| if not failure.exists():continue | |
| if (root/'reports/groups'/(group['group_id']+'.json')).exists():raise ValueError('Never repair an already-accepted group') | |
| if not is_near_failure(read(failure)):raise ValueError('This failure requires manual investigation before changing geometry: '+str(failure)) | |
| original=copy.deepcopy(group);new=propose(group,minimum=minimum) | |
| write(root/'private/rejected_recipes'/(group['group_id']+'.json'),dict(recipe=original,failure=read(failure))) | |
| groups[i]=new;replaced[group['group_id']]=new['group_id'];records.append(dict(index=group['index'],old_group_id=group['group_id'],new_group_id=new['group_id'],failure=read(failure))) | |
| print('REPAIRED NATIVE GEOMETRY',i,new['group_id'],flush=True) | |
| for group in groups: | |
| for scene in group['scenes']: | |
| donor=scene.get('absent_keyword_provenance') | |
| if donor and donor['group_id'] in replaced:donor['group_id']=replaced[donor['group_id']] | |
| write(root/'private/plans'/f"{group['index']:04d}.json",group) | |
| jsonl(root/'private/recipes.jsonl',groups) | |
| previous=root/'reports/native_geometry_repairs.json';history=read(previous) if previous.exists() else [] | |
| write(previous,history+records) | |
| return records | |
| if __name__=='__main__': | |
| p=argparse.ArgumentParser();p.add_argument('--root',type=Path,required=True);p.add_argument('--minimum-node-distance',type=float,default=.3) | |
| p.add_argument('--indices',type=int,nargs='+',help='Investigate only explicitly selected failed indices') | |
| a=p.parse_args();repair(a.root.resolve(),a.minimum_node_distance,a.indices) | |