generated from FNNDSC/python-chrisapp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
covidnet_meta.py
85 lines (70 loc) · 3.37 KB
/
covidnet_meta.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
#!/usr/bin/env python
from pathlib import Path
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter
from importlib.metadata import Distribution
from chris_plugin import chris_plugin, PathMapper
import shutil
import copy
import json
__pkg = Distribution.from_name(__package__)
__version__ = __pkg.version
DISPLAY_TITLE = r"""
_ _ _ _ _
| | (_) | | | | | |
_ __ | |______ ___ _____ ___ __| |_ __ ___| |_ ______ _ __ ___ ___| |_ __ _
| '_ \| |______/ __/ _ \ \ / / |/ _` | '_ \ / _ \ __|______| '_ ` _ \ / _ \ __/ _` |
| |_) | | | (_| (_) \ V /| | (_| | | | | __/ |_ | | | | | | __/ || (_| |
| .__/|_| \___\___/ \_/ |_|\__,_|_| |_|\___|\__| |_| |_| |_|\___|\__\__,_|
| |
|_|
"""
parser = ArgumentParser(description='A ChRIS plugin that analyzes an upstream COVID prediction.json file and, if COVID infection inferred, will exit with an exception. This has the effect of coloring the node red in the DAG representation.',
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--predictionGlob', default='**/prediction-default.json',
help='the upstream prediction file glob (for multiprediction)')
parser.add_argument('-P', '--prediction', default = '',
help='single prediction file')
parser.add_argument('-V', '--version', action='version',
version=f'%(prog)s {__version__}')
@staticmethod
def run_one(options):
# fetch input data and copy to output
with open('{}/prediction-default.json'.format(options.inputdir)) as f:
classification_data = json.load(f)
shutil.copy('{}/prediction-default.json'.format(options.inputdir),
'{}/prediction-default.json'.format(options.outputdir))
print("COVID infection probability: %f" % round(float(classification_data['COVID-19'])*100, 2))
if classification_data['prediction'] == "COVID-19":
print("")
print("This plugin will raise an exception which, if run in the ChRIS UI will")
print("Have the effect of coloring this node in the compute graph RED.")
print("")
raise Exception("COVID infection inferred!")
# documentation: https://fnndsc.github.io/chris_plugin/chris_plugin.html#chris_plugin
@chris_plugin(
parser=parser,
title='COVIDnet-meta',
category='', # ref. https://chrisstore.co/plugins
min_memory_limit='4Gi', # supported units: Mi, Gi
min_cpu_limit='1000m', # millicores, e.g. "1000m" = 1 CPU core
min_gpu_limit=0 # set min_gpu_limit=1 to enable GPU
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
print(DISPLAY_TITLE)
print('Version: %s' % f'{__version__}')
if options.prediction:
run_one(options)
else:
mapper = PathMapper.file_mapper(
input_dir=Path(options.inputdir),
output_dir=Path(options.outputdir),
glob=options.predictionGlob
)
for sub_input, sub_output in mapper:
sub_options = copy.copy(options)
sub_options.prediction = str(sub_input.name)
sub_options.inputdir = str(sub_input.parent)
sub_options.outputdir = str(sub_output.parent)
run_one(sub_options)
if __name__ == '__main__':
main()