-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_season_spotter.py
363 lines (262 loc) · 10.2 KB
/
create_season_spotter.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/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"
auth = "authentication.txt"
byline_simple = "metatext/byline_simple.txt"
byline_complex = "metatext/byline_complex.txt"
introduction_simple = "metatext/introduction_simple.txt"
introduction_complex = "metatext/introduction_complex.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_simple = "Season Spotter BASIC Beta1"
project_complex = "Season Spotter DETAILS Beta1"
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(projids,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)
for projid in projids:
panoptesPythonAPI.add_collaborator(projid,collid,token)
return
#---
# Create the workflows
#---
def create_workflows(projids,token):
#workflows = os.listdir(workflow_dir)
with open(linkfile,'r') as lfile:
# discard header
lfile.readline()
# keep track of workflows already done
donewf = []
# go through each line
lreader = csv.reader(lfile,delimiter=',',quotechar='\"')
for row in lreader:
wfname = row[0]
wfset = row[2]
# do new workflows
if wfname not in donewf:
wf_path = workflow_dir + wfname + ".txt"
with open(wf_path,'r') as wffile:
workflow = wffile.read().replace('\n', '')
# add this workflow to the correct project
wfproj = projids[0]
if wfset == "complex":
wfproj = projids[1]
# and build it
print "Building workflow " + wfname + " in project " + wfproj
wfid = panoptesPythonAPI.create_workflow(wfproj,wfname,workflow,token)
print " ID: " + wfid
# record that we've done this workflow
donewf.append(wfname)
return
# ---
# Create subject sets and upload the images
# ---
def create_subject_sets_and_subjects(projids,token):
# create a dictionary of which project to use for each subject set
ssproj = {}
with open(linkfile,'r') as lfile:
# discard header
lfile.readline()
# go through each line and add to dictionary
lreader = csv.reader(lfile,delimiter=',',quotechar='\"')
for row in lreader:
ssproj[row[1]] = projids[0]
if row[2]=="complex":
ssproj[row[1]] = projids[1]
# 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(ssproj[subjset],subjset,token)
if subjsetnum == -1:
print "Building SubjectSet: " + subjset
subjsetnum = panoptesPythonAPI.create_empty_subject_set(ssproj[subjset],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(ssproj[subjset],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(projids,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)
# for both projects
for projid in projids:
# get all the subject sets in this project
subjsetids = panoptesPythonAPI.get_project_subject_sets(projid,token)
# for each subject set, 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(projtype):
info = {}
# simple
if projtype == "simple":
info["display_name"] = project_simple
with open (byline_simple, "r") as infile:
info["description"] = infile.read()
with open (introduction_simple, "r") as infile:
info["introduction"] = infile.read()
# complex
else:
info["display_name"] = project_complex
with open (byline_complex, "r") as infile:
info["description"] = infile.read()
with open (introduction_complex, "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
with open(auth,'r') as authfile:
areader = csv.reader(authfile,delimiter=',')
row = areader.next()
username = row[0]
password = row[1]
token = panoptesPythonAPI.get_bearer_token(username,password)
# create a project for simple workflows
# check to see if it's already created. if not, create it
projid_simp = panoptesPythonAPI.get_projectid_from_projectname(project_simple,username,token)
if projid_simp==-1:
print "Creating project: " + project_simple
project_info = build_project_info("simple")
projid_simp = panoptesPythonAPI.create_user_project(project_info,token)
else:
print "Project already exists: " + project_simple
exit(0)
print " ID: " + projid_simp
# create a project for complex workflows
# check to see if it's already created. if not, create it
projid_comp = panoptesPythonAPI.get_projectid_from_projectname(project_complex,username,token)
if projid_comp==-1:
print "Creating project: " + project_complex
project_info = build_project_info("complex")
projid_comp = panoptesPythonAPI.create_user_project(project_info,token)
else:
print "Project already exists: " + project_complex
exit(0)
print " ID: " + projid_comp
# put the project ids together
projids = (projid_simp,projid_comp)
# add collaborators
#add_collaborators(projids,token)
# add workflows
create_workflows(projids,token)
# add subject sets
create_subject_sets_and_subjects(projids,token)
# link the subject sets and workflows
link_subject_sets_and_workflows(projids,token)