MCPcopy Create free account
hub / github.com/PAIR-code/lit / IndexedDataset

Class IndexedDataset

lit_nlp/api/dataset.py:252–417  ·  view source on GitHub ↗

Dataset with additional indexing information.

Source from the content-addressed store, hash-verified

250
251
252class IndexedDataset(Dataset):
253 """Dataset with additional indexing information."""
254
255 _index: dict[ExampleId, IndexedInput] = {}
256
257 def _normalize_example(
258 self, data: JsonDict, ex_id: ExampleId, meta: lit_types.InputMetadata
259 ):
260 return types.MappingProxyType(dict(data, _id=ex_id, _meta=meta))
261
262 def index_inputs(
263 self, examples: list[lit_types.JsonDict]
264 ) -> list[IndexedInput]:
265 """Create indexed versions of inputs."""
266 indexed = []
267 for example in examples:
268 ex_id = example.get(INPUT_ID_FIELD, self.id_fn(example))
269 ex_meta = example.get(
270 INPUT_META_FIELD,
271 lit_types.InputMetadata(added=None, parentId=None, source=None),
272 )
273 indexed.append(
274 IndexedInput(
275 data=types.MappingProxyType(
276 example | {INPUT_ID_FIELD: ex_id, INPUT_META_FIELD: ex_meta}
277 ),
278 id=ex_id,
279 meta=ex_meta,
280 )
281 )
282 return indexed
283
284 def __init__(
285 self,
286 *args,
287 id_fn: Optional[IdFnType] = None,
288 indexed_examples: Optional[list[IndexedInput]] = None,
289 **kw,
290 ):
291 # The base Dataset class will initialize self._examples in this call to
292 # super().__init__(), which may or may not include the _id and _meta fields.
293 super().__init__(*args, **kw)
294 self.id_fn = id_fn if id_fn is not None else input_hash
295
296 if indexed_examples:
297 self._indexed_examples = indexed_examples
298 # Ensure that all indexed exampls provide a readonly view of their data.
299 for ie in self._indexed_examples:
300 if not isinstance((ie_data := ie['data']), types.MappingProxyType):
301 ie['data'] = self._normalize_example(ie_data, ie['id'], ie['meta'])
302 else:
303 self._indexed_examples = self.index_inputs(self._examples)
304
305 self._examples = [
306 self._normalize_example(ex['data'], ex['id'], ex.get('meta', {}))
307 for ex in self._indexed_examples
308 ]
309 self._index = {ex['id']: ex for ex in self._indexed_examples}

Callers 3

_slicerMethod · 0.85
loadMethod · 0.85
load_lit_formatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected