-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
382 lines (370 loc) · 13.9 KB
/
makefile
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
###############################################################################
#
# COPYRIGHT NOTICE
# Mark O. Hatfield Clinical Research Center
# National Institutes of Health
# United States Department of Health and Human Services
#
# This software was developed and is owned by the National Institutes of
# Health Clinical Center (NIHCC), an agency of the United States Department
# of Health and Human Services, which is making the software available to the
# public for any commercial or non-commercial purpose under the following
# open-source BSD license.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# (1) Redistributions of source code must retain this copyright
# notice, this list of conditions and the following disclaimer.
#
# (2) Redistributions in binary form must reproduce this copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# (3) Neither the names of the National Institutes of Health Clinical
# Center, the National Institutes of Health, the U.S. Department of
# Health and Human Services, nor the names of any of the software
# developers may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# (4) Please acknowledge NIHCC as the source of this software by including
# the phrase "Courtesy of the U.S. National Institutes of Health Clinical
# Center"or "Source: U.S. National Institutes of Health Clinical Center."
#
# THIS SOFTWARE IS PROVIDED BY THE U.S. GOVERNMENT AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.
#
# You are under no obligation whatsoever to provide any bug fixes,
# patches, or upgrades to the features, functionality or performance of
# the source code ("Enhancements") to anyone; however, if you choose to
# make your Enhancements available either publicly, or directly to
# the National Institutes of Health Clinical Center, without imposing a
# separate written license agreement for such Enhancements, then you hereby
# grant the following license: a non-exclusive, royalty-free perpetual license
# to install, use, modify, prepare derivative works, incorporate into
# other computer software, distribute, and sublicense such Enhancements or
# derivative works thereof, in binary and source code form.
#
###############################################################################
SHELL=/bin/bash
## Path to Python 3 binary to use (with HARE requirements pre-installed)
PY=python3
## Initial preprocessing, using SpaCy for tokenization; generates 2 files:
## (1) .tokens -- tokenized version of dataset, maintaining line breaks
## (2) .labels -- tokenized version with one token and corresponding label per line
preprocess_with_SpaCy:
@if [ -z "${DATASET}" ]; then \
echo "Must supply DATASET (using a section header in data/config.ini)"; \
exit; \
else \
DATASET=${DATASET}; \
fi; \
DATADIR=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Experiments DataDirectory); \
if [ ! -d "$${DATADIR}/$${DATASET}" ]; then \
mkdir $${DATADIR}/$${DATASET}; \
fi; \
${PY} -m data.extract_data_files \
-c data/config.ini \
--dataset $${DATASET} \
-t SpaCy \
$${DATADIR}/$${DATASET}/preprocessed.SpaCy \
-l $${DATADIR}/$${DATASET}/preprocessed.SpaCy.log
## Initial preprocessing, using WordPiece (via BERT) for tokenization; generates 2 files:
## (1) .tokens -- tokenized version of dataset, maintaining line breaks
## (2) .labels -- tokenized version with one token and corresponding label per line
preprocess_with_WordPiece:
@export PYTHONPATH=/home/griffisd/toolkits/bert:$${PYTHONPATH}; \
if [ -z "${MODEL}" ]; then \
echo "Must supply MODEL (using a BERT model reference from data/config.ini)"; \
exit; \
else \
MODEL=${MODEL}; \
fi; \
if [ -z "${DATASET}" ]; then \
echo "Must supply DATASET (using a section header in data/config.ini)"; \
exit; \
else \
DATASET=${DATASET}; \
fi; \
DATADIR=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Experiments DataDirectory); \
if [ ! -d "$${DATADIR}/$${DATASET}" ]; then \
mkdir $${DATADIR}/$${DATASET}; \
fi; \
${PY} -m data.extract_data_files \
-c data/config.ini \
--dataset $${DATASET} \
-t BERT \
--BERT-vocab-file $$(${PY} -m cli_configparser.read_setting -c data/config.ini BERT "$${MODEL} Vocabfile") \
$${DATADIR}/$${DATASET}/preprocessed.BERT.$${MODEL} \
-l $${DATADIR}/$${DATASET}/preprocessed.BERT.$${MODEL}.log
## Generate splits at document-level for cross-fold validation
## Split generation is linked to sample-level IDs, so will not
## necessarily map across different preprocessed versions of the
## same dataset.
## TODO: Add script to automatically remap these IDs
generate_cross_validation_splits:
@if [ -z "${DATASET}" ]; then \
echo "Must supply DATASET (using a section header in data/config.ini)"; \
exit; \
else \
DATASET=${DATASET}; \
fi; \
if [ -z "${SPEC}" ]; then \
echo "Must supply SPEC (specifier for preprocessed file, e.g. SPEC=SpaCy or SPEC=BERT.clinicalBERT)"; \
exit; \
else \
SPEC=${SPEC}; \
fi; \
if [ -z "${K}" ]; then \
K=10; \
fi; \
DATADIR=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Experiments DataDirectory); \
if [ ! -d "$${DATADIR}/$${DATASET}/splits" ]; then \
mkdir $${DATADIR}/$${DATASET}/splits; \
fi; \
${PY} -m experiments.document_splits \
$${DATADIR}/$${DATASET}/preprocessed.$${SPEC}.labels \
$${DATADIR}/$${DATASET}/splits/preprocessed.$${SPEC}.splits \
--dev-size 0.2 \
--n-folds $${K} \
-l $${DATADIR}/$${DATASET}/splits/preprocessed.$${SPEC}.splits.log
## Feature generation with ELMo
## Requires that allennlp be in the PATH
generate_ELMo_features:
@if [ -z "${MODEL}" ]; then \
echo "Must supply MODEL (using an ELMo model reference from data/config.ini)"; \
exit; \
else \
MODEL=${MODEL}; \
fi; \
if [ -z "${DATASET}" ]; then \
echo "Must supply DATASET (using a section header in data/config.ini)"; \
exit; \
else \
DATASET=${DATASET}; \
fi; \
DATADIR=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Experiments DataDirectory); \
allennlp elmo \
--options-file $$(${PY} -m cli_configparser.read_setting -c data/config.ini ELMo "$${MODEL} OptionsFile") \
--weight-file $$(${PY} -m cli_configparser.read_setting -c data/config.ini ELMo "$${MODEL} WeightsFile") \
--all \
$${DATADIR}/$${DATASET}/preprocessed.SpaCy.tokens \
$${DATADIR}/$${DATASET}/preprocessed.SpaCy.ELMo-embedded.$${MODEL}.hdf5
## Feature generation with BERT
## Requires that BERT code be cloned from GitHuab; see utils/get_bert_hdf5_features.sh for details
generate_BERT_features:
@bash utils/get_bert_hdf5_features.sh ${DATASET} ${MODEL} ${GPU}
## Master training maketarget
## Supports:
## - Choice of dataset to train on (DATASET flag)
## - Cross-validation or training on full set (FULL_TRAIN flag)
## - Hyperparameter tuning (CLASS_WEIGHTS, DROPOUT, NEGRATIO, POSFRAC, LAYERS flags)
## - Choice of embedding method (METHOD flag)
## - Choice of embedding model (MODEL flag)
train:
@export CUDA_VISIBLE_DEVICES=${GPU}; \
if [ -z "${MODEL}" ]; then \
echo "MODEL must be supplied"; \
exit; \
else \
MODEL=${MODEL}; \
fi; \
if [ -z "${DATASET}" ]; then \
echo "Must supply DATASET (using a section header in data/config.ini)"; \
exit; \
else \
DATASET=${DATASET}; \
fi; \
if [ -z "${CLASS_WEIGHTS}" ]; then \
CLASSFLAG=; \
LOGPREFIX=$${LOGPREFIX}; \
else \
CLASSFLAG="--class-weights ${CLASS_WEIGHTS}"; \
LOGPREFIX=hptune.class-weights.${CLASS_WEIGHTS}; \
fi; \
if [ -z "${DROPOUT}" ]; then \
DROPOUTFLAG=; \
LOGPREFIX=$${LOGPREFIX}; \
else \
DROPOUTFLAG="--dropout-keep-prob ${DROPOUT}"; \
LOGPREFIX=hptune.dropout-keep-prob.${DROPOUT}; \
fi; \
if [ -z "${NEGRATIO}" ]; then \
NEGRATIOFLAG=; \
LOGPREFIX=$${LOGPREFIX}; \
else \
NEGRATIOFLAG="--negative-training-ratio ${NEGRATIO}"; \
LOGPREFIX=hptune.neg-ratio.${NEGRATIO}; \
fi; \
if [ -z "${POSFRAC}" ]; then \
POSFRACFLAG=; \
LOGPREFIX=$${LOGPREFIX}; \
else \
POSFRACFLAG="--positive-training-fraction ${POSFRAC}"; \
LOGPREFIX=hptune.pos-fraction.${POSFRAC}; \
fi; \
if [ -z "${LAYERS}" ]; then \
LAYERSFLAG=; \
LOGPREFIX=$${LOGPREFIX}; \
else \
LAYERSFLAG="--layer-dims ${LAYERS}"; \
LOGPREFIX=hptune.layers.${LAYERS}; \
fi; \
if [ -z "${DEBUG}" ]; then \
DEBUGFLAG=; \
LOGPREFIX=$${LOGPREFIX}; \
else \
DEBUGFLAG="--debug"; \
LOGPREFIX=debug; \
fi; \
DATADIR=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Experiments DataDirectory); \
case "${METHOD}" in \
BERT) \
METHODSFLAGS="--embedding-method bert --embedding-dim 768 --num-bert-layers 3"; \
EMBEDDINGSFILE=$${DATADIR}/$${DATASET}/preprocessed.BERT.$${MODEL}.hdf5; \
TRAININGFILE=$${DATADIR}/$${DATASET}/preprocessed.BERT.$${MODEL}.labels; \
SPLITSFILE=$${DATADIR}/$${DATASET}/splits/preprocessed.BERT.$${MODEL}.splits; \
LOGSUFFIX=BERT.$${MODEL}; \
;; \
ELMo) \
METHODSFLAGS="--embedding-method elmo --embedding-dim 1024"; \
EMBEDDINGSFILE=$${DATADIR}/$${DATASET}/preprocessed.SpaCy.ELMo-embedded.$${MODEL}.hdf5; \
TRAININGFILE=$${DATADIR}/$${DATASET}/preprocessed.SpaCy.labels; \
SPLITSFILE=$${DATADIR}/$${DATASET}/splits/preprocessed.SpaCy.splits; \
LOGSUFFIX=ELMo.$${MODEL}; \
;; \
Static) \
METHODSFLAGS="--embedding-method static --embedding-dim"; \
METHODSFLAGS="$${METHODSFLAGS} $$(${PY} -m cli_configparser.read_setting -c data/config.ini Static "$${MODEL} Dimensionality")"; \
EMBEDDINGSFILE=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Static "$${MODEL} File"); \
TRAININGFILE=$${DATADIR}/$${DATASET}/preprocessed.SpaCy.labels; \
SPLITSFILE=$${DATADIR}/$${DATASET}/splits/preprocessed.SpaCy.splits; \
LOGSUFFIX=Static.$${MODEL}; \
;; \
*) \
echo "METHOD must be supplied as one of [Static, BERT, ELMo]"; \
exit; \
;; \
esac; \
if [ -z "${FULL_TRAIN}" ]; then \
SPLITSFLAG="--splits $${SPLITSFILE}"; \
LOGPREFIX="xval"; \
else \
SPLITSFLAG=; \
LOGPREFIX="full"; \
fi; \
if [ ! -d "$${DATADIR}/$${DATASET}/experiments" ]; then \
mkdir $${DATADIR}/$${DATASET}/experiments; \
fi; \
${PY} -m experiments.train \
--training $${TRAININGFILE} \
$${METHODSFLAGS} \
--embeddings-file $${EMBEDDINGSFILE} \
$${SPLITSFLAG} \
$${CLASSFLAG} \
$${DROPOUTFLAG} \
$${NEGRATIOFLAG} \
$${POSFRACFLAG} \
$${LAYERSFLAG} \
$${DEBUGFLAG} \
--batch-size 25 \
-m $${DATADIR}/$${DATASET}/experiments/$${LOGPREFIX}.$${LOGSUFFIX}
## Master test maketarget
## Supports
## - Specification of pretrained model (EXP flag)
## - Dataset to run on (DATASET)
## - Specification of per-fold pretrained model (FOLD flag)
## - Model structure (LAYERS flag; must match what was used for the pretrained model)
## - Running on labeled or unlabeled data (UNLABELED flag)
## - Embedding method and model (METHOD and MODEL; must match pretrained model)
test:
@export CUDA_VISIBLE_DEVICES=${GPU}; \
if [ -z "${EXP}" ]; then \
echo "EXP must be specified"; \
exit; \
fi; \
if [ -z "${DATASET}" ]; then \
echo "Must supply DATASET (using a section header in data/config.ini)"; \
exit; \
else \
DATASET=${DATASET}; \
fi; \
if [ -z "${TRAIN_DATASET}" ]; then \
echo "Must supply TRAIN_DATASET (using a section header in data/config.ini)"; \
exit; \
else \
TRAIN_DATASET=${TRAIN_DATASET}; \
fi; \
if [ -z "${MODEL}" ]; then \
echo "MODEL must be supplied"; \
exit; \
else \
MODEL=${MODEL}; \
fi; \
if [ -z "${FOLD}" ]; then \
FOLD=0; \
else \
FOLD=${FOLD}; \
fi; \
if [ -z "${LAYERS}" ]; then \
LAYERSFLAG=; \
else \
LAYERSFLAG="--layer-dims ${LAYERS}"; \
fi; \
if [ -z "${UNLABELED}" ]; then \
LABELEDFLAG="--labeled"; \
else \
LABELEDFLAG=; \
fi; \
if [ -z "${METHOD}" ]; then \
echo "METHOD must be supplied as one of [Static, BERT, ELMo]"; \
exit; \
fi; \
if [ -z "${MODEL}" ]; then \
echo "MODEL must be supplied"; \
exit; \
else \
MODEL=${MODEL}; \
fi; \
DATADIR=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Experiments DataDirectory); \
case "${METHOD}" in \
BERT) \
METHODSFLAGS="--embedding-method bert --embedding-dim 768 --num-bert-layers 3"; \
EMBEDDINGSFILE=$${DATADIR}/$${DATASET}/preprocessed.BERT.$${MODEL}.hdf5; \
TESTFILE=$${DATADIR}/$${DATASET}/preprocessed.BERT.$${MODEL}.labels; \
;; \
ELMo) \
METHODSFLAGS="--embedding-method elmo --embedding-dim 1024"; \
EMBEDDINGSFILE=$${DATADIR}/$${DATASET}/preprocessed.SpaCy.ELMo-embedded.$${MODEL}.hdf5; \
TESTFILE=$${DATADIR}/$${DATASET}/preprocessed.SpaCy.labels; \
;; \
Static) \
METHODSFLAGS="--embedding-method static --embedding-dim"; \
METHODSFLAGS="$${METHODSFLAGS} $$(${PY} -m cli_configparser.read_setting -c data/config.ini Static "$${MODEL} Dimensionality")"; \
EMBEDDINGSFILE=$$(${PY} -m cli_configparser.read_setting -c data/config.ini Static "$${MODEL} File"); \
TESTFILE=$${DATADIR}/$${DATASET}/preprocessed.SpaCy.labels; \
;; \
*) \
echo "METHOD must be supplied as one of [Static, BERT, ELMo]"; \
exit; \
;; \
esac; \
${PY} -m experiments.run_pretrained \
--testing $${TESTFILE} \
$${METHODSFLAGS} \
--embeddings-file $${EMBEDDINGSFILE} \
$${LAYERSFLAG} \
--batch-size 25 \
$${LABELEDFLAG} \
--dropout-keep-prob 1.0 \
-c $${DATADIR}/$${TRAIN_DATASET}/experiments/${EXP}/model.fold${FOLD} \
-l $${DATADIR}/$${TRAIN_DATASET}/experiments/${EXP}/$${DATASET}.model.fold${FOLD}.log \
--predictions $${DATADIR}/$${TRAIN_DATASET}/experiments/${EXP}/$${DATASET}.model.fold${FOLD}.predictions
## Start the web-based visualization interface
## Reads settings from visualization/viz_config.ini
## Requires browser access to local webserver
start_web_interface:
@export FLASK_APP=visualization/app.py; \
${PY} -m flask run