|
| 1 | +import pandas |
| 2 | +from labelbox import Client as labelboxClient |
| 3 | + |
| 4 | +def create_annotation_upload_dict(client:labelboxClient, table:pandas.core.frame.DataFrame, row_data_col:str, global_key_col:str, |
| 5 | + project_id_col:str, annotation_index:dict, divider:str="///", verbose:bool=False): |
| 6 | + if not annotation_index: |
| 7 | + project_id_to_upload_dict = {} |
| 8 | + errors = f"No annotation index provided - no annotations uploaded" |
| 9 | + else: |
| 10 | + try: |
| 11 | + project_id_to_upload_dict = {project_id : [] for project_id in get_unique_values_function(table, project_id_col)} |
| 12 | + for project_id in project_id_to_upload_dict: |
| 13 | + project_id_to_upload_dict[project_id] = [] |
| 14 | + project_id_to_ontology_index[project_id] = get_ontology_schema_to_name_path( |
| 15 | + ontology=client.get_project(project_id).ontology(), divider=divider, invert=True |
| 16 | + ) |
| 17 | + if verbose: |
| 18 | + for index, row in tqdm(table.iterrows()): |
| 19 | + for column_name in annotation_index.keys(): |
| 20 | + ndjsons = create_ndjsons( |
| 21 | + annotation_values=row[column_name], |
| 22 | + annotation_type=annotation_index[column_name], |
| 23 | + ontology_index=project_id_to_ontology_index[row[project_id_col]], |
| 24 | + divide=divider |
| 25 | + ) |
| 26 | + for ndjson in ndjsons: |
| 27 | + project_id_to_upload_dict[row[project_id_col]].append(ndjson) |
| 28 | + for index, row in table.iterrows(): |
| 29 | + for column_name in annotation_index.keys(): |
| 30 | + ndjsons = create_ndjsons( |
| 31 | + annotation_values=row[column_name], |
| 32 | + annotation_type=annotation_index[column_name], |
| 33 | + ontology_index=project_id_to_ontology_index[row[project_id_col]], |
| 34 | + divide=divider |
| 35 | + ) |
| 36 | + for ndjson in ndjsons: |
| 37 | + project_id_to_upload_dict[row[project_id_col]].append(ndjson) |
| 38 | + except Exception as e: |
| 39 | + errors = e |
| 40 | + return project_id_to_upload_dict, errors |
0 commit comments