MCPcopy Create free account
hub / github.com/apple/ml-pointersect / IndexFileReader

Class IndexFileReader

cdslib/core/data/index_reader.py:15–138  ·  view source on GitHub ↗

The class implements a simple reader for an index file like scp files. Given an index file (e.g., scp file, with or without the unique id), the class creates a indexed list of the file content. For example, if i-th line in the scp file contains "uid_abc abc.npy", - dset[i] re

Source from the content-addressed store, hash-verified

13
14
15class IndexFileReader:
16 """
17 The class implements a simple reader for an index file like scp files.
18
19 Given an index file (e.g., scp file, with or without the unique id),
20 the class creates a indexed list of the file content.
21
22 For example, if i-th line in the scp file contains "uid_abc abc.npy",
23
24 - dset[i] returns the array contained in abc.npy.
25 - dset.get_uid(i) returns uid_abc.
26
27 Currently, we support only npy files.
28
29 Note:
30 The class is not a proper scp file reader.
31 If a typical scp reader is what you are after,
32 please use kaldiio.
33
34 """
35
36 def __init__(self, index_filename: str, with_uid: bool, root_dir: str = None):
37 """
38 Args:
39 index_filename:
40 the index file that lists all the data, one per line.
41 with_uid:
42 If with_uid is True, each line is composed of
43 `a_unique_id_of_the_data filename_of_the_data`.
44 If with_uid is False, each line is composed of
45 `filename_of_the_data`.
46 root_dir:
47 The root folder for the files listed in the index_filename.
48 For example, if root_dir = 'a/b' and index_filename[0] = 'c/d.npy',
49 the file is at 'a/b/c/d.npy'.
50 If None is given, use the folder of the index_filename as root_dir.
51 If '' is given, no modification will be made.
52 """
53 self.index_filename = index_filename
54 self.with_uid = with_uid
55 self.root_dir = root_dir
56 if self.root_dir is None:
57 self.root_dir = os.path.dirname(self.index_filename)
58
59 # read the index file
60 self.uids, self.filenames = self._read_index_file(
61 index_filename=self.index_filename,
62 with_uid=self.with_uid,
63 root_dir=self.root_dir,
64 )
65
66 def __len__(self):
67 """Returns the number of files in the index file."""
68 return len(self.filenames)
69
70 def __getitem__(self, i: int) -> T.Any:
71 """Returns the content of the i-th line in the index file."""
72

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected