Skip to content

Instantly share code, notes, and snippets.

@nburn42
Created January 24, 2019 21:19
Show Gist options
  • Save nburn42/e67340c3b3388ff89aa866d625672300 to your computer and use it in GitHub Desktop.
Save nburn42/e67340c3b3388ff89aa866d625672300 to your computer and use it in GitHub Desktop.
labelbox reupload
def delete_labels(client, label_id_list):
res = client.execute('''
mutation DeleteLabels($label_id_list: [ID!]) {
deleteLabels(
labelIds: $label_id_list
){
id
deleted
}
}
''', {"label_id_list": label_id_list})
data = json.loads(res)['data']
return data
def relabel_keys(client, key_list, exact_model_name, df, confidence_threshold, project_id, prediction_model_id):
"""
:param confidence_threshold: the confidence bound to add to predictions
:param exact_model_name: the full model name
:param key_list: A list of key strings
:param client: labelbox client
:param project_id: labelbox project id
:param prediction_model_id: labelbox prediction model id
:param df: the dataframe containing all the evaluation data
:type df: pd.Dataframe
"""
label_ids = []
for key in key_list:
response = get_label_for_key(client, key)
print response
if len(response) == 0:
print "there is no labelbox label for key {} skipping".format(key)
continue
datarow_id = response[0]['dataRow']['id']
for r in response:
label_ids.append(r['id'])
temp_df = df[
(df['model'] == exact_model_name) & (df['key'] == key) & (
df['confidence'] >= confidence_threshold)]
prediction_label = format_dataframe_for_labelbox_prediction(temp_df)
print datarow_id
create_prediction(client, json.dumps(prediction_label), prediction_model_id,
project_id, datarow_id)
delete_labels(client, label_ids)
def create_prediction(client, label, prediction_model_id, project_id, data_row_id):
res_str = client.execute("""
mutation CreatePredictionFromAPI($label: String!, $predictionModelId: ID!, $projectId: ID!, $dataRowId: ID!) {
createPrediction(data:{
label: $label,
predictionModelId: $predictionModelId,
projectId: $projectId,
dataRowId: $dataRowId,
}){
id
}
}
""", {
'label': label,
'predictionModelId': prediction_model_id,
'projectId': project_id,
'dataRowId': data_row_id
})
res = json.loads(res_str)
return res['data']['createPrediction']['id']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment