forked from cvisionai/tator-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaf.py
107 lines (90 loc) · 3.33 KB
/
leaf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
""" This example shows how to create a hierarchy of leaves for label autocomplete.
"""
import logging
import sys
import argparse
from textwrap import dedent
import yaml
import tator
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)
def _create_children(api, tree, project, type_id, parent):
children = tree.get('children')
if children:
specs = [{'project': project,
'type': type_id,
'name': child['name'],
'parent': parent} for child in children]
response = api.create_leaf_list(project, body=specs)
for child_id, child in zip(response.id, children):
_create_children(api, child, project, type_id, child_id)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=dedent('''\
Creates a label hierarchy with Leaf objects.
This utility accepts a yaml file with the following format:
name: GoT # The name of the hierarchy.
children:
- name: Targaryen
children:
- name: Maekar I
children:
- name: Maester Aemon
- name: Aegon V
children:
- name: Jaehaerys II
children:
- name: Aerys II (Mad King)
children:
- name: Daenerys
- name: Viserys
- name: Rhaegar
children:
- name: Aegon
- name: Rhaelle
- name: Stark
children:
- name: Rickard
children:
- name: Brandon
- name: Eddard
children:
- name: Robb
- name: Sansa
- name: Arya
- name: Brandon
- name: Rickon
- name: Benjen
- name: Lyanna
Once leaves are created, the autocomplete service will be available at:
https://<domain>/rest/Leaves/Suggestion/<project_name>.GoT/<project>
To narrow scope of the autocomplete service (for example just Starks):
https://<domain>/rest/Leaves/Suggestion/<project_name>.GoT.Stark/<project>
To use an autocomplete service on a string attribute type, set the autocomplete field
as follows:
{...
'autocomplete': {'serviceUrl': 'https://<domain>/rest/Leaves/Suggestion/<project_name>.GoT/<project>'},
...}
'''), formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--host', help='Host containing source project.', required=True)
parser.add_argument('--token', help='Token for host containing source project.', required=True)
parser.add_argument('--type_id', help="Leaf type ID.", type=int, required=True)
parser.add_argument('--input', help="Path to YAML file defining label hierarchy.", type=str,
required=True)
args = parser.parse_args()
tator_api = tator.get_api(args.host, args.token)
# Get the leaf type.
leaf_type = tator_api.get_leaf_type(args.type_id)
project = leaf_type.project
# Read input file.
with open(args.input, 'r') as f:
tree = yaml.safe_load(f)
# Create root leaf.
root_spec = {
'project': project,
'type': leaf_type.id,
'name': tree['name'],
}
response = tator_api.create_leaf_list(project=project, body=root_spec)
# Create children recursively.
_create_children(tator_api, tree, project, leaf_type.id, response.id[0])