Download dataloader/pim_annotations.py from ZenoNing/ConsDrive: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/ZenoNing/ConsDrive/resolve/main/dataloader/pim_annotations.py
- Command line
-
hf download hf://ZenoNing/ConsDrive/dataloader/pim_annotations.py
-
curl -L -o pim_annotations.py https://huggingface.co/ZenoNing/ConsDrive/resolve/main/dataloader/pim_annotations.py
2.69 kB
| """Causal semantic interaction labels. Missing/unknown is never a negative label.""" | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from models.pim import SLOT_NAMES | |
| FIELDS = ('observed', 'pending', 'resolved') | |
| def parse_annotation(payload, slot_names=SLOT_NAMES): | |
| if not isinstance(payload, dict) or not isinstance(payload.get('slots'), dict): | |
| raise ValueError('PIM label must have a slots object') | |
| slots = payload['slots'] | |
| unknown = set(slots) - set(slot_names) | |
| if unknown: | |
| raise ValueError(f'Unknown semantic slots: {sorted(unknown)}') | |
| out = {} | |
| for field in FIELDS: | |
| out[field] = np.zeros(len(slot_names), dtype=np.float32) | |
| out[field + '_valid'] = np.zeros(len(slot_names), dtype=np.bool_) | |
| complete = payload.get('observation_complete', False) | |
| if not isinstance(complete, bool): | |
| raise ValueError('observation_complete must be boolean') | |
| if complete: | |
| out['observed_valid'][:] = True | |
| for i, name in enumerate(slot_names): | |
| event = slots.get(name, {}) | |
| if not isinstance(event, dict): | |
| raise ValueError(f'{name}: expected a label object') | |
| if 'resolution_evidence' in event and not isinstance(event['resolution_evidence'], bool): | |
| raise ValueError(f'{name}.resolution_evidence must be boolean') | |
| for field in FIELDS: | |
| value = event.get(field) | |
| if value is not None and not isinstance(value, bool): | |
| raise ValueError(f'{name}.{field}: use true/false/null') | |
| if value is not None: | |
| out[field][i] = float(value) | |
| out[field + '_valid'][i] = True | |
| if event.get('resolved') is True: | |
| if event.get('resolution_evidence') is not True: | |
| raise ValueError(f'{name}: resolved=true requires resolution_evidence=true') | |
| if event.get('pending') is True: | |
| raise ValueError(f'{name}: cannot be pending and resolved simultaneously') | |
| elif event.get('resolved') is False and event.get('pending') is not True: | |
| # An unknown state must not be relabelled as a known negative. | |
| out['resolved_valid'][i] = bool(event.get('resolution_evidence', False)) | |
| return out | |
| def load_annotation(path, required=True, slot_names=SLOT_NAMES): | |
| path = Path(path) | |
| if not path.is_file(): | |
| if required: | |
| raise FileNotFoundError(f'PIM labels missing: {path}. See docs/PIM.md') | |
| return None | |
| try: | |
| return parse_annotation(json.loads(path.read_text(encoding='utf-8')), slot_names) | |
| except (ValueError, TypeError) as exc: | |
| raise ValueError(f'{path}: {exc}') from exc | |