map
docarray.utils.map
map_docs(docs, func, backend='thread', num_worker=None, pool=None, show_progress=False)
Return an iterator that applies func
to every Document in docs
in parallel,
yielding the results.
from docarray import DocList
from docarray.documents import ImageDoc
from docarray.utils.map import map_docs
def load_url_to_tensor(img: ImageDoc) -> ImageDoc:
img.tensor = img.url.load()
return img
url = (
'https://upload.wikimedia.org/wikipedia/commons/8/80/'
'Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg'
)
docs = DocList[ImageDoc]([ImageDoc(url=url) for _ in range(100)])
docs = DocList[ImageDoc](
list(map_docs(docs, load_url_to_tensor, backend='thread'))
) # threading is usually a good option for IO-bound tasks such as loading an
# ImageDoc from url
for doc in docs:
assert doc.tensor is not None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
T
|
DocList to apply function to |
required |
func |
Callable[[T_doc], T_doc]
|
required | |
backend |
str
|
|
'thread'
|
num_worker |
Optional[int]
|
the number of parallel workers. If not given, the number of CPUs in the system will be used. |
None
|
pool |
Optional[Union[Pool, ThreadPool]]
|
use an existing/external process or thread pool. If given, you will be responsible for closing the pool. |
None
|
show_progress |
bool
|
show a progress bar. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Generator[T_doc, None, None]
|
yield Documents returned from |
Source code in docarray/utils/map.py
map_docs_batched(docs, func, batch_size, backend='thread', num_worker=None, shuffle=False, pool=None, show_progress=False)
Return an iterator that applies func
to every minibatch of iterable in parallel,
yielding the results.
Each element in the returned iterator is an AnyDocArray
.
from docarray import BaseDoc, DocList
from docarray.utils.map import map_docs_batched
class MyDoc(BaseDoc):
name: str
def upper_case_name(docs: DocList[MyDoc]) -> DocList[MyDoc]:
docs.name = [n.upper() for n in docs.name]
return docs
batch_size = 16
docs = DocList[MyDoc]([MyDoc(name='my orange cat') for _ in range(100)])
it = map_docs_batched(docs, upper_case_name, batch_size=batch_size)
for i, d in enumerate(it):
docs[i * batch_size : (i + 1) * batch_size] = d
assert len(docs) == 100
print(docs.name[:3])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
T
|
DocList to apply function to |
required |
batch_size |
int
|
Size of each generated batch (except the last one, which might be smaller). |
required |
shuffle |
bool
|
If set, shuffle the Documents before dividing into minibatches. |
False
|
func |
Callable[[T], Union[T, T_doc]]
|
a function that takes an :class: |
required |
backend |
str
|
|
'thread'
|
num_worker |
Optional[int]
|
the number of parallel workers. If not given, then the number of CPUs in the system will be used. |
None
|
show_progress |
bool
|
show a progress bar |
False
|
pool |
Optional[Union[Pool, ThreadPool]]
|
use an existing/external pool. If given, |
None
|
Returns:
Type | Description |
---|---|
Generator[Union[T, T_doc], None, None]
|
yield DocLists returned from |
Source code in docarray/utils/map.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|