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

Method __getitem__

cdslib/core/data/chunked_memmap.py:144–179  ·  view source on GitHub ↗

Return the ith's sample (in global index) as if calling samples[idx]. Note: 1. Each sample is returned in their native shape (no padding) 2. The sample is returned as ndarray (so int's shape will be (1,))

(self, idx: int)

Source from the content-addressed store, hash-verified

142 return len(self.chunk_idxs)
143
144 def __getitem__(self, idx: int):
145 """Return the ith's sample (in global index) as if calling samples[idx].
146
147 Note:
148 1. Each sample is returned in their native shape (no padding)
149 2. The sample is returned as ndarray (so int's shape will be (1,))
150 """
151 if isinstance(idx, slice):
152 # do not support slicing
153 raise NotImplementedError
154
155 if self.chunk_filename_dict is None or self.chunk_shape_dict is None or self.chunk_idxs is None:
156 return None
157
158 # get chunk idx of ith sample
159 target_chunk_idx = self.chunk_idxs[idx]
160
161 # open memmap
162 memmap_filename = self.chunk_filename_dict[target_chunk_idx]
163 memmap_shape = self.chunk_shape_dict[target_chunk_idx]
164 sample_shape = self.chunk_sample_shapes_dict[target_chunk_idx][self.chunk_local_idxs[idx]]
165
166 # check if memmap_shape or sample_shape contains any 0
167 if np.prod(memmap_shape) == 0 or np.prod(sample_shape) == 0:
168 out = np.zeros(sample_shape, dtype=self.dtype)
169 return out
170
171 offset = self.chunk_offsets_dict[target_chunk_idx][self.chunk_local_idxs[idx]] * self.itemsize # offset in byte
172 arr = np.memmap(
173 memmap_filename,
174 dtype=self.dtype,
175 mode="r",
176 offset=offset,
177 shape=tuple(sample_shape),
178 )
179 return np.array(arr)
180
181 def get_shape(self, idx: int) -> T.Sequence[int]:
182 """Returns the shape of idx-th sample."""

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected