Skip to content

Commit 6a14896

Browse files
add new batch creation method, clean up notebook (#1231)
Co-authored-by: ezekielemerson <eemerson2325@gmail.com>
1 parent 7b15f47 commit 6a14896

File tree

1 file changed

+133
-95
lines changed

1 file changed

+133
-95
lines changed

examples/basics/batches.ipynb

Lines changed: 133 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,28 @@
3030
{
3131
"metadata": {},
3232
"source": [
33-
"## Batches\n",
33+
"# Batches\n",
3434
"https://docs.labelbox.com/docs/batches"
3535
],
3636
"cell_type": "markdown"
3737
},
3838
{
3939
"metadata": {},
4040
"source": [
41-
"* A Batch is collection of datarows picked out of a Data Set.\n",
42-
"* A Datarow cannot be part of more than one batch in a project.\n",
43-
"* Batches work for all data types, but there should only be one data type per batch.\n",
44-
"* Batches may not be shared between projects.\n",
45-
"* Batches may have Datarows from multiple Datasets.\n",
46-
"* Datarows can only be attached to a Project as part of a single Batch.\n",
47-
"* Currently only benchmarks quality settings is supported in batch projects\n",
48-
"* You can set priority for each Batch."
41+
"* A batch is collection of data rows.\n",
42+
"* A data row cannot be part of more than one batch in a given project.\n",
43+
"* Batches work for all data types, but there can only be one data type per project.\n",
44+
"* Batches can not be shared between projects.\n",
45+
"* Batches may have data rows from multiple datasets.\n",
46+
"* Currently, only benchmarks quality settings is supported in batch projects\n",
47+
"* You can set the priority for each batch."
4948
],
5049
"cell_type": "markdown"
5150
},
5251
{
5352
"metadata": {},
5453
"source": [
55-
"!pip install \"labelbox[data]\""
54+
"!pip install \"labelbox[data]\" -q"
5655
],
5756
"cell_type": "code",
5857
"outputs": [],
@@ -72,22 +71,29 @@
7271
{
7372
"metadata": {},
7473
"source": [
75-
"# API Key and Client\n",
76-
"Provide a valid api key below in order to properly connect to the Labelbox Client."
74+
"## API key and client\n",
75+
"Provide a valid API key below in order to properly connect to the Labelbox Client."
7776
],
7877
"cell_type": "markdown"
7978
},
8079
{
8180
"metadata": {},
8281
"source": [
83-
"# Add your api key\n",
84-
"API_KEY = None\n",
82+
"# Add your API key\n",
83+
"API_KEY = \"\"\n",
8584
"client = lb.Client(api_key=API_KEY)"
8685
],
8786
"cell_type": "code",
8887
"outputs": [],
8988
"execution_count": null
9089
},
90+
{
91+
"metadata": {},
92+
"source": [
93+
"## Create a dataset and data rows"
94+
],
95+
"cell_type": "markdown"
96+
},
9197
{
9298
"metadata": {},
9399
"source": [
@@ -108,90 +114,65 @@
108114
"print(\"RESULT URL: \", data_rows.result_url)"
109115
],
110116
"cell_type": "code",
111-
"outputs": [
112-
{
113-
"name": "stderr",
114-
"output_type": "stream",
115-
"text": [
116-
"WARNING:labelbox.schema.task:There are errors present. Please look at `task.errors` for more details\n"
117-
]
118-
},
119-
{
120-
"name": "stdout",
121-
"output_type": "stream",
122-
"text": [
123-
"ERRORS: []\n",
124-
"RESULT URL: https://storage.labelbox.com/cl3ahv73w1891087qbwzs3edd%2Fdata-row-imports-results%2Fcl94vbi4g4ijw07y07shadc7k_cl94vbjcv1dh707y2f2g4cwh4.json?Expires=1665619363366&KeyName=labelbox-assets-key-3&Signature=VJOqZZUjnnT4s45on3zzYdcagOs\n"
125-
]
126-
}
127-
],
117+
"outputs": [],
128118
"execution_count": null
129119
},
130120
{
131121
"metadata": {},
132122
"source": [
133-
"# Setup batch project"
123+
"## Setup batch project"
134124
],
135125
"cell_type": "markdown"
136126
},
137127
{
138128
"metadata": {},
139129
"source": [
140-
"project = client.create_project( name=\"Demo-Batches-Project\", \n",
141-
" media_type=lb.MediaType.Image\n",
142-
" )\n",
143-
"print(\"Project Name:\", project.name ,\n",
144-
" \" Project Id:\", project.uid )"
130+
"project = client.create_project(\n",
131+
" name=\"Demo-Batches-Project\", \n",
132+
" media_type=lb.MediaType.Image\n",
133+
")\n",
134+
"print(\"Project Name: \", project.name, \"Project ID: \", project.uid)"
145135
],
146136
"cell_type": "code",
147-
"outputs": [
148-
{
149-
"name": "stdout",
150-
"output_type": "stream",
151-
"text": [
152-
"Project Name: Demo-Batches-Project Project Id: cl94vbpr849gg08ytd6rd423x\n"
153-
]
154-
}
155-
],
137+
"outputs": [],
156138
"execution_count": null
157139
},
158140
{
159141
"metadata": {},
160142
"source": [
161-
"### Select all data rows from the dataset created earlier that will be added to the batch.\n"
143+
"## Create batches"
162144
],
163145
"cell_type": "markdown"
164146
},
165147
{
166148
"metadata": {},
167149
"source": [
168-
"data_row_ids = [dr.uid for dr in dataset.export_data_rows()]\n",
169-
"print(\"Number of data row ids:\", len(data_row_ids))"
150+
"### Select all data rows from the dataset\n"
170151
],
171-
"cell_type": "code",
172-
"outputs": [
173-
{
174-
"name": "stdout",
175-
"output_type": "stream",
176-
"text": [
177-
"Number of data row ids: 8\n"
178-
]
179-
}
152+
"cell_type": "markdown"
153+
},
154+
{
155+
"metadata": {},
156+
"source": [
157+
"global_keys = [data_row.global_key for data_row in dataset.export_data_rows()]\n",
158+
"print(\"Number of global keys:\", len(global_keys))"
180159
],
160+
"cell_type": "code",
161+
"outputs": [],
181162
"execution_count": null
182163
},
183164
{
184165
"metadata": {},
185166
"source": [
186-
"## Select a random sample\n",
167+
"### Select a random sample\n",
187168
"This method is useful if you have large datasets and only want to work with a handful of data rows"
188169
],
189170
"cell_type": "markdown"
190171
},
191172
{
192173
"metadata": {},
193174
"source": [
194-
"sample = random.sample(data_row_ids, 4)"
175+
"sample = random.sample(global_keys, 4)"
195176
],
196177
"cell_type": "code",
197178
"outputs": [],
@@ -200,83 +181,140 @@
200181
{
201182
"metadata": {},
202183
"source": [
203-
"# Batch Manipulation"
184+
"### Create a batch\n",
185+
"This method takes in a list of either data row IDs or `DataRow` objects into a `data_rows` argument or global keys into a `global_keys` argument, but both approaches cannot be used in the same method."
204186
],
205187
"cell_type": "markdown"
206188
},
207189
{
208190
"metadata": {},
209191
"source": [
210-
"### Create a Batch:\n"
192+
"batch = project.create_batch(\n",
193+
" name=\"Demo-First-Batch\", # Each batch in a project must have a unique name\n",
194+
" global_keys=sample, # A list of data rows or data row ids\n",
195+
" priority=5 # priority between 1(Highest) - 5(lowest)\n",
196+
")\n",
197+
"# number of data rows in the batch\n",
198+
"print(\"Number of data rows in batch: \", batch.size)"
199+
],
200+
"cell_type": "code",
201+
"outputs": [],
202+
"execution_count": null
203+
},
204+
{
205+
"metadata": {},
206+
"source": [
207+
"### Create multiple batches\n",
208+
"The `project.create_batches()` method accepts up to 1 million data rows. Batches are chunked into groups of 100k if necessary, which is the maximum batch size. This method takes in a list of either data row IDs or `DataRow` objects into a `data_rows` argument or global keys into a `global_keys` argument, but both approaches cannot be used in the same method.\n",
209+
"\n",
210+
"This method takes in a list of either data row IDs or `DataRow` objects into a `data_rows` argument or global keys into a `global_keys` argument, but both approaches cannot be used in the same method. Batches will be created with the specified `name_prefix` argument and a unique suffix to ensure unique batch names. The suffix will be a 4-digit number starting at `0000`.\n",
211+
"\n",
212+
"For example, if the name prefix is `demo-create-batches-` and three batches are created, the names will be `demo-create-batches-0000`, `demo-create-batches-0001`, and `demo-create-batches-0002`. This method will throw an error if a batch with the same name already exists.\n",
213+
"\n",
214+
"In the code below, only one batch will be created, since we are only using the few data rows we created above. Creating over 100k data rows for this demonstration is not sensible, but this method is the preferred approach for batch creation as it will gracefully handle massive sets of data rows."
211215
],
212216
"cell_type": "markdown"
213217
},
214218
{
215219
"metadata": {},
216220
"source": [
217-
"batch = project.create_batch(\n",
218-
" \"Demo-First-Batch\", # Each batch in a project must have a unique name\n",
219-
" sample, # A list of data rows or data row ids\n",
220-
" 5 # priority between 1(Highest) - 5(lowest)\n",
221+
"# First, we must create a second project so that we can re-use the data rows we already created.\n",
222+
"second_project = client.create_project(\n",
223+
" name=\"Second-Demo-Batches-Project\", \n",
224+
" media_type=lb.MediaType.Image\n",
221225
")\n",
222-
"# number of data rows in the batch\n",
223-
"print(\"Number of data rows in batch: \", batch.size)"
226+
"print(\"Project Name: \", second_project.name, \"Project ID: \", second_project.uid)\n",
227+
"\n",
228+
"# Then, use the method that will create multiple batches if necessary.\n",
229+
"task = second_project.create_batches(\n",
230+
" name_prefix=\"demo-create-batches-\",\n",
231+
" global_keys=global_keys,\n",
232+
" priority=5\n",
233+
")\n",
234+
"\n",
235+
"print(\"Errors: \", task.errors())\n",
236+
"print(\"Result: \", task.result())"
224237
],
225238
"cell_type": "code",
226-
"outputs": [
227-
{
228-
"name": "stdout",
229-
"output_type": "stream",
230-
"text": [
231-
"Number of data rows in batch: 4\n"
232-
]
233-
}
239+
"outputs": [],
240+
"execution_count": null
241+
},
242+
{
243+
"metadata": {},
244+
"source": [
245+
"### Create batches from a dataset\n",
246+
"\n",
247+
"If you wish to create batches in a project using all the data rows of a dataset, instead of having to gather global keys or ID and using subsets of data rows, you can use the `project.create_batches_from_dataset()` method. This method takes in a dataset ID and creates a batch (or batches if there are more than 100k data rows) comprised of all data rows not already in the project.\n",
248+
"\n",
249+
"The same logic applies to the `name_prefix` argument and the naming of batches as described in the section immediately above."
250+
],
251+
"cell_type": "markdown"
252+
},
253+
{
254+
"metadata": {},
255+
"source": [
256+
"# First, we must create a third project so that we can re-use the data rows we already created.\n",
257+
"third_project = client.create_project(\n",
258+
" name=\"Third-Demo-Batches-Project\", \n",
259+
" media_type=lb.MediaType.Image\n",
260+
")\n",
261+
"print(\"Project Name: \", third_project.name, \"Project ID: \", third_project.uid)\n",
262+
"\n",
263+
"# Then, use the method to create batches from a dataset.\n",
264+
"task = third_project.create_batches_from_dataset(\n",
265+
" name_prefix=\"demo-batches-from-dataset-\",\n",
266+
" dataset_id=dataset.uid,\n",
267+
" priority=5\n",
268+
")\n",
269+
"\n",
270+
"print(\"Errors: \", task.errors())\n",
271+
"print(\"Result: \", task.result())"
234272
],
273+
"cell_type": "code",
274+
"outputs": [],
235275
"execution_count": null
236276
},
237277
{
238278
"metadata": {},
239279
"source": [
240-
"### Manage Batches\n",
241-
"Note: You can view your batch data through the *Data Rows tab*"
280+
"## Manage batches\n",
281+
"Note: You can view your batch data through the **Data Rows** tab."
242282
],
243283
"cell_type": "markdown"
244284
},
245285
{
246286
"metadata": {},
247287
"source": [
248-
"## Export the data row ids\n",
288+
"### View batches"
289+
],
290+
"cell_type": "markdown"
291+
},
292+
{
293+
"metadata": {},
294+
"source": [
295+
"## Export the data row iDs\n",
249296
"data_rows = [dr for dr in batch.export_data_rows()]\n",
250-
"print(\"Data Rows in Batch: \", data_rows)\n",
297+
"print(\"Data rows in batch: \", data_rows)\n",
251298
"\n",
252299
"## List the batches in your project\n",
253300
"for batch in project.batches():\n",
254-
" print(\"Batch Name: \", batch.name , \" Batch ID:\", batch.uid)\n"
301+
" print(\"Batch name: \", batch.name , \" Batch ID:\", batch.uid)\n"
255302
],
256303
"cell_type": "code",
257-
"outputs": [
258-
{
259-
"name": "stdout",
260-
"output_type": "stream",
261-
"text": [
262-
"Data Rows in Batch: [<DataRow ID: cl94vbjjn0wb8075i74pcb54v>, <DataRow ID: cl94vbjjn0wb0075i9i542qtp>, <DataRow ID: cl94vbjjn0waw075i11rser6b>, <DataRow ID: cl94vbjjn0was075igz3789ff>]\n",
263-
"Batch Name: Demo-First-Batch Batch ID: 39f3fb00-49c1-11ed-ad8c-4b0085ccfe8b\n"
264-
]
265-
}
266-
],
304+
"outputs": [],
267305
"execution_count": null
268306
},
269307
{
270308
"metadata": {},
271309
"source": [
272-
"# Archive Batch"
310+
"### Archive a batch"
273311
],
274312
"cell_type": "markdown"
275313
},
276314
{
277315
"metadata": {},
278316
"source": [
279-
"# archiving batch removes all queued data rows from the project\n",
317+
"# Archiving a batch removes all queued data rows in the batch from the project\n",
280318
"batch.remove_queued_data_rows()"
281319
],
282320
"cell_type": "code",
@@ -287,7 +325,7 @@
287325
"metadata": {},
288326
"source": [
289327
"## Clean up \n",
290-
"Uncomment and run the cell below to delete the batch, dataset and/or project created in this demo"
328+
"Uncomment and run the cell below to optionally delete the batch, dataset, and/or project created in this demo."
291329
],
292330
"cell_type": "markdown"
293331
},
@@ -308,4 +346,4 @@
308346
"execution_count": null
309347
}
310348
]
311-
}
349+
}

0 commit comments

Comments
 (0)