-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_season_spotter-archive.py
285 lines (202 loc) · 7.52 KB
/
create_season_spotter-archive.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/python
import panoptesPythonAPI
import csv
import os
import json
# TO DO
# add other users after they have created accounts
# ------------------
# FILES WITH CONTENT
# ------------------
#manifestfile = "beta_manifest.csv"
manifestfile = "beta_manifest_mini.csv"
workflow_dir = "workflows/"
linkfile = "subjectsets_and_workflows.csv"
byline = "metatext/byline.txt"
introduction = "metatext/introduction.txt"
science = "metatext/science_case.txt"
faq = "metatext/faq.txt"
education = "metatext/education_content.txt"
results = "metatext/results.txt"
logo = "https://raw.githubusercontent.com/mkosmala/SeasonSpotterLanding/gh-pages/projectimages/SeasonSpotter_logo_small.png"
background = "https://raw.githubusercontent.com/mkosmala/SeasonSpotterLanding/gh-pages/projectimages/tamaracks_fall_RNC_cropped_1024x768.jpg"
# ------------------------------
# PROJECT NAME AND COLLABORATORS
# ------------------------------
project = "Season Spotter Test31"
collaborators = ["imaginaryfriend"]
# -----------
# DEFINITIONS
# -----------
# vegetation type lookup
veg_lookup = { "AG":"Agriculture",
"DB":"Deciduous Broadleaf Forest",
"DN":"Deciduous Needle-leaf Forest",
"EB":"Evergreen Broadleaf Forest",
"EN":"Evergreen Needle-leaf Forest",
"GR":"Grassland",
"MX":"Mixed Forest",
"SH":"Shrubland",
"TN":"Tundra",
"WL":"Wetland" }
#---
# Add collaborators
#---
def add_collaborators(projid,token):
# add each collaborator
for coll in collaborators:
# look up their ID
collid = panoptesPythonAPI.get_userid_from_username(coll,token)
if collid == -1:
print "Warning! User \"" + coll + "\" does not exist. Cannot add as collaborator."
else:
print "Adding collaborator: " + coll + ", ID: " + str(collid)
panoptesPythonAPI.add_collaborator(projid,collid,token)
return
#---
# Create the workflows
#---
def create_workflows(projid,token):
workflows = os.listdir(workflow_dir)
# load in each workflow
for wf in workflows:
wf_path = workflow_dir + wf
with open(wf_path,'r') as wffile:
workflow = wffile.read().replace('\n', '')
wfname = wf.split('.')[0]
# and build it
print "Building workflow: " + wfname
wfid = panoptesPythonAPI.create_workflow(projid,wfname,workflow,token)
print " ID: " + wfid
return
# ---
# Create subject sets and upload the images
# ---
def create_subject_sets_and_subjects(projid,token):
# read the manifest
with open(manifestfile,'r') as mfile:
# discard header and get csv object
mfile.readline()
# for each image
mreader = csv.reader(mfile,delimiter=',',quotechar='\"')
for row in mreader:
image = row[0]
subjset = row[1]
site = row[2]
vegabbr = row[3]
loc = row[4]
lat = row[5]
lon = row[6]
ele = row[7]
# look up the vegetation type
if vegabbr in veg_lookup:
vegtype = veg_lookup[vegabbr]
else:
vegtype = vegabbr
# check to see if the subject set(s) exists; create it if not
subjsetnum = panoptesPythonAPI.get_subject_set(projid,subjset,token)
if subjsetnum == -1:
print "Building SubjectSet: " + subjset
subjsetnum = panoptesPythonAPI.create_empty_subject_set(projid,subjset,token)
# create the metadata object
meta = """ "Camera": \"""" + site + """\",
"Location": \"""" + loc + """\",
"Vegetation": \"""" + vegtype + """\",
"Latitute": \"""" + lat + """\",
"Longitude": \"""" + lon + """\",
"Elevation": \"""" + ele + """\" """
# create the subject
print "Adding Subject: " + image
subjid = panoptesPythonAPI.create_subject(projid,meta,image,token)
# add it to the subject set(s)
print "Linking Subject " + image + " to Subject Set " + subjset
panoptesPythonAPI.add_subject_to_subject_set(subjsetnum,subjid,token)
return
# ---
# Link SubjectSets to Workflows
# ---
def link_subject_sets_and_workflows(projid,token):
# initialize the link list
linklist = []
# read in the link file
with open(linkfile,'r') as lfile:
# ignore header line
lfile.readline()
# read in all the pairs
lreader = csv.reader(lfile,delimiter=',')
for row in lreader:
linklist.append(row)
# get all the subject sets in this project
subjsetids = panoptesPythonAPI.get_project_subject_sets(projid,token)
# for each one, link to workflow(s)
for ssid in subjsetids:
#print "\nssid = " + str(ssid)
# get the subjectset name
ssname = panoptesPythonAPI.get_subject_set_name(projid,ssid,token)
#print " " + ssname
#foundmatch = False
# look through the links for workflow matches
for eachpair in linklist:
filewf = eachpair[0]
filess = eachpair[1]
# and link the matches
if filess == ssname:
# get the workflow id
wfid = panoptesPythonAPI.get_workflow(projid,filewf,token)
# link
print "Linking subject set " + ssname + " to workflow " + filewf
panoptesPythonAPI.link_subject_set_and_workflow(ssid,wfid,token)
#foundmatch = True
#if not foundmatch:
# print " no matching workflow found for subjectset: " + ssname
return
def build_project_info():
info = {}
info["display_name"] = project
with open (byline, "r") as infile:
info["description"] = infile.read()
with open (introduction, "r") as infile:
info["introduction"] = infile.read()
with open (science, "r") as infile:
info["science_case"] = infile.read()
with open (faq, "r") as infile:
info["faq"] = infile.read()
with open (education, "r") as infile:
info["education_content"] = infile.read()
#with open (results, "r") as infile:
# info["result"] = infile.read()
info["avatar"] = logo
info["background_image"] = background
info["primary_language"] = "en-us"
info["private"] = False
#info["beta"] = True
#info["live"] = True
# for testing
info["configuration"] = {'user_chooses_workflow':True}
# for production
#info["configuration"] = {'user_chooses_workflow':False}
return info
########
# MAIN #
########
# get token
token = panoptesPythonAPI.get_bearer_token("mkosmala","hard2guess")
# create a project for simple workflows
# check to see if it's already created. if not, create it
projid = panoptesPythonAPI.get_projectid_from_projectname(project,"mkosmala",token)
if projid==-1:
print "Creating project: " + project
project_info = build_project_info()
projid = panoptesPythonAPI.create_user_project(project_info,token)
else:
print "Project already exists"
exit(0)
print " ID: " + projid
# add collaborators
#add_collaborators(projid,token)
# add workflows
create_workflows(projid,token)
# add subject sets
create_subject_sets_and_subjects(projid,token)
# link the subject sets and workflows
link_subject_sets_and_workflows(projid,token)