File size: 4,074 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""JSON-only adapter executed by the configured generator interpreter."""
import json
import argparse
import sys
import traceback


def schema():
    from embodied_pipeline.gen import appearance, config

    implemented = []
    for flavor in config.FLAVORS:
        try:
            config.load({'scene': 's', 'seed': 0, 'cast': [], 'flavor': flavor})
        except config.ConfigError as exc:
            if 'not implemented' in str(exc):
                continue
        implemented.append(flavor)
    moving = []
    for mode in config.MOTION_MODES:   # follow is in the vocabulary but config.load refuses it: offer only what generates
        try:
            config.load({'scene': 's', 'seed': 0, 'cast': [], 'observers': [{**config.DEFAULT_OBSERVER, 'id': 'o', 'motion_mode': mode}]})
        except config.ConfigError as exc:
            if 'not implemented' in str(exc):
                continue
        moving.append(mode)
    # Known editable constraints; the generator remains the validator.
    arrangements = {t: n for t, n in config.ARRANGEMENTS.items() if t not in config.RETIRED}
    fields = {'stop_at': {'where': {'type': 'location'}, 'dwell_s': {'type': 'number', 'min': 0}},
              'meet': {'where': {'type': 'location'}, 'dwell_s': {'type': 'number', 'min': 0}},
              'covisible_event': {'event': {'type': 'enum', 'values': ['dynamic']}},
              'observer_body': {'times': {'type': 'fixed', 'value': 1}}, 'covisible_region': {}}
    from np.planning import GENERATOR_TYPE_LABELS
    return {
        'schema': 'np-gen-schema/1',
        'keys': {key: sorted(value) for key, value in config.KEYS.items()},
        'flavors': list(config.FLAVORS), 'implemented_flavors': implemented,
        'observer_kinds': config.OBS_KINDS, 'motion_modes': list(config.MOTION_MODES),
        'implemented_motion_modes': moving, 'arrangement_labels': {t: GENERATOR_TYPE_LABELS.get(t, t) for t in arrangements},
        'view_modes': list(config.VIEW_MODES), 'arrangements': arrangements,
        'arrangement_fields': {t: fields[t] for t in arrangements},
        'recording_keys': ['length_frac', 'link_overlap_frac', 'split_frac', 'split_excl_frac', 'split_offset_max_frac', 'max_shared'],
        'bodies': config.BODIES, 'profiles': config.PROFILES,
        'profile_keys': list(config.PROFILE_KEYS), 'default_profile': config.DEFAULT_PROFILE,
        'default_observer': config.DEFAULT_OBSERVER,
        'dist_kinds': ['fixed', 'uniform', 'normal', 'lognormal', 'choice', 'mixture'],
        'max_humans': appearance.MAX_HUMANS,
    }


def expand(batch, capability_root=None):
    from embodied_pipeline.gen import config, expand as generator_expand

    configs, selection = [], {}
    for raw in generator_expand.expand(batch, selection=selection, capability_root=capability_root):
        try:
            configs.append({'config': config.load(raw), 'errors': []})
        except config.ConfigError as exc:
            configs.append({'config': raw, 'errors': [str(exc)]})
    return {'count': len(configs), 'configs': configs, 'structural_selection': selection}


def load(raw):
    from embodied_pipeline.gen import config

    try:
        return {'config': config.load(raw), 'errors': []}
    except config.ConfigError as exc:
        return {'config': raw, 'errors': [str(exc)]}


if __name__ == '__main__':
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument('mode', choices=['schema', 'expand', 'load'])
        parser.add_argument('--capability-root')
        args = parser.parse_args()
        mode = args.mode
        if mode == 'schema':
            result = schema()
        elif mode == 'expand':
            result = expand(json.load(sys.stdin), capability_root=args.capability_root)
        elif mode == 'load':
            result = load(json.load(sys.stdin))
        else:
            raise ValueError('unknown bridge mode: ' + mode)
        print(json.dumps(result, ensure_ascii=False, allow_nan=False))
    except Exception:
        traceback.print_exc(file=sys.stderr)
        sys.exit(2)