-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_config.py
404 lines (378 loc) · 15.3 KB
/
create_config.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
''' create_config.py
Create a janelia-neuronbridge-data config file
'''
import argparse
import copy
import json
from operator import attrgetter
import re
import sys
import boto3
from botocore.exceptions import ClientError
import inquirer
from inquirer.themes import BlueComposure
from tqdm import tqdm
import jrc_common.jrc_common as JRC
import doi_common.doi_common as DL
# pylint: disable=broad-exception-caught,logging-fstring-interpolation
__version__ = '1.3.0'
BASE = {}
RELEASES = {}
# Database
DB = {}
# AWS S3
AWSS3 = {"client": None}
def terminate_program(msg=None):
''' Terminate the program gracefully
Keyword arguments:
msg: error message or object
Returns:
None
'''
if msg:
if not isinstance(msg, str):
msg = f"An exception of type {type(msg).__name__} occurred. Arguments:\n{msg.args}"
LOGGER.critical(msg)
sys.exit(-1 if msg else 0)
def initialize_program():
''' Initialize database connection and AWS S3 client
Keyword arguments:
None
Returns:
None
'''
# Database
try:
dbconfig = JRC.get_config("databases")
except Exception as err:
terminate_program(err)
for source in ['sage', 'neuronbridge']:
dbo = attrgetter(f"{source}.prod.read")(dbconfig)
LOGGER.info("Connecting to %s prod on %s as %s", dbo.name, dbo.host, dbo.user)
try:
DB["nb" if source == "neuronbridge" else source] = JRC.connect_database(dbo)
except Exception as err:
terminate_program(err)
try:
aws = JRC.get_config("aws")
except Exception as err:
terminate_program(err)
BASE['s3'] = aws.base_aws_url + "/"
# AWS S3
if ARG.MANIFOLD == 'prod':
sts_client = boto3.client('sts')
aro = sts_client.assume_role(RoleArn=aws.role_arn,
RoleSessionName="AssumeRoleSession1")
credentials = aro['Credentials']
AWSS3["client"] = boto3.client('s3',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'])
else:
AWSS3["client"] = boto3.client('s3')
def get_prefix(lib):
''' Get the prefix from a FlyEM library name
Keyword arguments:
lib: library name
Returns:
Prefix
'''
return lib.replace("FlyEM ", "").replace(" v", ":v").replace(" ", "_").lower()
def read_object(bucket, key):
''' Read a file from AWS S3 and return the contents
Keyword arguments:
bucket: bucket name
key: object key
Returns:
JSON
'''
LOGGER.debug(f"Reading {bucket}/{key}")
try:
data = AWSS3["client"].get_object(Bucket=bucket, Key=key)
contents = data['Body'].read().decode("utf-8")
except ClientError as err:
if err.response['Error']['Code'] == 'NoSuchKey':
LOGGER.warning(f"Could not find {bucket}/{key}")
return None
raise
except Exception as err:
LOGGER.error(f"Could not read {bucket}/{key}")
terminate_program(err)
return json.loads(contents)
def get_count(lib, template, source, release=None):
''' Get the searchable neuron count from AWS S3 or publishedURL
Keyword arguments:
lib: library name
template: alignment space
release: ALPS release (default: None)
Returns:
Count
'''
if source == "mongo":
coll = DB['nb'].publishedURL
payload = {"libraryName": lib, "alignmentSpace": template}
if release:
payload["alpsRelease"] = release
else:
LOGGER.info(f"Getting count from MongoDB for {lib}/{template}")
count = coll.count_documents(payload)
if ARG.DEBUG:
rows = coll.find(payload)
with open(f"{lib}_{template}.txt", 'w', encoding='ascii') as outfile:
for row in rows:
outfile.write(f"{row['uploaded']['searchable_neurons']}\n")
return count
bucket = "janelia-flylight-color-depth"
if ARG.MANIFOLD != "prod":
bucket = f"{bucket}-{ARG.MANIFOLD}"
count = read_object(bucket, f"{template}/{lib.replace(' ', '_')}/" \
+ "searchable_neurons/counts_denormalized.json")
if not count:
return 0
return count["objectCount"]
def get_em_releases_block(lib, count):
''' Get the releases block for a FlyEM library
Keyword arguments:
lib: library name
count: image count
Returns:
Releases block
'''
if "FlyEM" in lib:
key = lib.replace("FlyEM ", "").lower()
key = re.sub(r" v.+", "", key)
elif "FlyWire FAFB" in lib:
key = "flywire_fafb"
else:
terminate_program(f"Can't parse EM library {lib}")
if key not in EMDOI:
terminate_program(f"Missing entry in em_dois configuration for {key} ({lib})")
dois = {}
if not EMDOI[key]:
LOGGER.warning(f"Undefined DOI in em_dois configuration for {key} ({lib})")
else:
if isinstance(EMDOI[key], list):
for doi in EMDOI[key]:
dois[doi] = DL.short_citation(doi)
else:
dois[EMDOI[key]] = DL.short_citation(EMDOI[key])
payload = {lib.replace(" ", "_"): {"count": count,
"dois": dois
}}
return [payload]
def get_flylight_dois(release):
''' Get the DOIs for FlyLight for a release
Keyword arguments:
release: ALPS release
Returns:
Dictionary of DOIs (DOI: citation)
'''
if release in RELEASES:
return RELEASES[release]
stmt = "SELECT DISTINCT value FROM line_property_vw WHERE type='doi' AND name in " \
+ "(SELECT DISTINCT line FROM image_data_mv WHERE alps_release=%s) ORDER BY 1"
try:
DB['sage']['cursor'].execute(stmt, (release,))
rows = DB['sage']['cursor'].fetchall()
except Exception as err:
terminate_program(err)
dois = []
for row in rows:
if "in prep" in row['value'].lower():
continue
dois.append(row['value'])
if LMDOI['global']:
dois.extend(LMDOI['global'])
if release in LMDOI['release']:
dois.extend(LMDOI['release'][release])
doirecs = {}
for doi in dois:
doirecs[doi] = DL.short_citation(doi)
RELEASES[release] = doirecs
return doirecs
def get_lm_releases_block(libint, area):
''' Get the releases block for a FlyLight library
Keyword arguments:
libint: library internal name (e.g. flylight_split_gal4_published)
area: anatomical area
Returns:
Releases block
'''
try:
coll = DB['nb'].publishedURL
rows = coll.distinct("alpsRelease", {"libraryName": libint})
except Exception as err:
terminate_program(err)
payload = []
LOGGER.debug(f"Getting releases for {libint} {area}")
progress = False
if len(rows) > 1:
progress = True
rows = (pbar := tqdm(rows, position=1, colour="#4169e1"))
for rel in rows:
if progress:
pbar.set_description(rel)
count = get_count(libint, AREAS[area]["alignmentSpace"], 'mongo', rel)
if not count:
continue
dois = get_flylight_dois(rel)
if not dois:
continue
payload.append({rel: {"count": count,
"dois": dois}})
return payload
def get_libraries(area):
''' Get libraries to create a customSearch block
Keyword arguments:
area: anatomical area
Returns:
JSON customSearch block
'''
libs = JRC.simplenamespace_to_dict(JRC.get_config("cdm_library"))
coll = DB['nb'].publishedURL
rows = coll.distinct("libraryName",
{"alignmentSpace": AREAS[area]['alignmentSpace']})
msg = f"Select libraries for {AREAS[area]['label']} ({AREAS[area]['alignmentSpace']})"
libraries = {}
for lib, val in libs.items():
if lib in rows:
libraries[val['name'].replace("_", " ")] = lib
defaults = ['FlyLight Gen1 MCFO', 'FlyLight Split-GAL4 Drivers']
quest = [inquirer.Checkbox('checklist',
message=msg,
choices=sorted(libraries), default=defaults)]
libs = inquirer.prompt(quest, theme=BlueComposure())['checklist']
csblock = {"lmLibraries": [],
"emLibraries": []
}
for lib in (pbar := tqdm(libs, position=0, leave=True, colour="cyan")):
pbar.set_description(lib)
count = get_count(libraries[lib] if ARG.SOURCE == "mongo" else lib, \
AREAS[area]['alignmentSpace'], ARG.SOURCE)
if 'FlyLight' in lib:
payload = get_lm_releases_block(libraries[lib], area)
csblock["lmLibraries"].append({"name": lib.replace(" ", "_"),
"count": count,
"releases": payload
})
else:
payload = get_em_releases_block(lib, count)
csblock["emLibraries"].append({"name": lib.replace(" ", "_"),
"publishedNamePrefix": get_prefix(lib),
"count": count,
"releases": payload
})
return csblock
def create_config():
''' Create a new config file
Keyword arguments:
None
Returns:
None
'''
manifest = {}
master = {"anatomicalAreas": AREAS,
"stores": {}
}
release = {"anatomicalAreas": AREAS,
"stores": {}
}
base = BASE['s3']
for area in AREAS:
fullblock = get_libraries(area)
# Adjust the customSearch block
csblock = copy.deepcopy(fullblock)
csblock["searchFolder"] = "searchable_neurons"
for libtype in ["lmLibraries", "emLibraries"]:
if libtype not in csblock:
continue
for lib in csblock[libtype]:
del lib["releases"]
key = f"fl:open_data:{area.lower()}"
short = "" if ARG.MANIFOLD == "prod" else f"-{ARG.MANIFOLD}"
manifest = {"label": f"FlyLight {area} Open Data Store",
"anatomicalArea": area,
"prefixes": {"CDM": f"{base}janelia-flylight-color-depth{short}/",
"CDMThumbnail": f"{base}janelia-flylight-color-depth-thumbnails" \
+ f"{short}/",
"CDMInput": f"{base}janelia-flylight-color-depth{short}/",
"CDMMatch": f"{base}janelia-flylight-color-depth{short}/",
"CDMBest": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"CDMBestThumbnail": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"CDMSkel": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"SignalMip": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"SignalMipMasked": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"SignalMipMaskedSkel": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"SignalMipExpression": f"{base}janelia-ppp-match-{ARG.MANIFOLD}/",
"AlignedBodySWC": f"{base}janelia-flylight-color-depth{short}/",
"AlignedBodyOBJ": f"{base}janelia-flylight-color-depth{short}/",
"CDSResults": f"{base}janelia-neuronbridge-data-" \
+ f"{ARG.MANIFOLD}/{ARG.VERSION}/metadata" \
+ "/cdsresults/",
"PPPMResults": f"{base}janelia-neuronbridge-data-" \
+ f"{ARG.MANIFOLD}/{ARG.VERSION}/metadata/" \
+ "pppmresults/",
"VisuallyLosslessStack": f"{base}janelia-flylight-imagery/",
"Gal4Expression": f"{base}janelia-flylight-imagery/"
},
"customSearch": csblock
}
master['stores'][key] = manifest
manifest = {"label": f"FlyLight {area} Open Data Store",
"anatomicalArea": area,
"customSearch": fullblock}
release['stores'][key] = manifest
try:
LOGGER.info("Writing new_config.json")
with open("new_config.json", "w", encoding="ascii") as outstream:
if ARG.HUMAN:
outstream.write(json.dumps(master, indent=4) + "\n")
else:
outstream.write(json.dumps(master) + "\n")
except Exception as err:
LOGGER.error("Could not write new_config.json")
terminate_program(err)
print("Wrote new_config.json")
try:
LOGGER.info("Writing references.json")
with open("references.json", "w", encoding="ascii") as outstream:
if ARG.HUMAN:
outstream.write(json.dumps(release, indent=4) + "\n")
else:
outstream.write(json.dumps(release) + "\n")
except Exception as err:
LOGGER.error("Could not write new_config.json")
terminate_program(err)
print("Wrote references.json")
# -----------------------------------------------------------------------------
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(
description="Create a janelia-neuronbridge-data config file")
PARSER.add_argument('--source', dest='SOURCE', action='store',
default='s3', choices=['s3', 'mongo'],
help='Source for searchable neuron count (s3, mongo)')
PARSER.add_argument('--version', dest='VERSION', action='store',
default='v3_2_1', help='Version')
PARSER.add_argument('--manifold', dest='MANIFOLD', action='store',
default='prod', choices=['dev', 'prod'],
help='Manifold (dev, prod)')
PARSER.add_argument('--human', dest='HUMAN', action='store_true',
default=False,
help='Generate human-readable file (indented with whitespace)')
PARSER.add_argument('--verbose', dest='VERBOSE', action='store_true',
default=False, help='Flag, Chatty')
PARSER.add_argument('--debug', dest='DEBUG', action='store_true',
default=False, help='Flag, Very chatty')
ARG = PARSER.parse_args()
LOGGER = JRC.setup_logging(ARG)
if not re.match(r"^v\d+_\d+_\d+$", ARG.VERSION):
terminate_program("--version must be in the format v_x_y_z; e.g. v_3_2_1")
try:
EMDOI = JRC.simplenamespace_to_dict(JRC.get_config("em_dois"))
LMDOI = JRC.simplenamespace_to_dict(JRC.get_config("lm_dois"))
AREAS = JRC.simplenamespace_to_dict(JRC.get_config("neuprint"))["areas"]
except Exception as gerr:
terminate_program(gerr)
initialize_program()
create_config()
terminate_program()