forked from zsb87/PyHealth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
examples/learning_models/example_sequence_cpu_phenotyping.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Example of using GRU on MIMIC demo mortality prediction | ||
""" | ||
# License: BSD 2 clause | ||
|
||
|
||
# environment setting | ||
import os | ||
import sys | ||
|
||
sys.path.append( | ||
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..'))) | ||
root_dir = os.path.abspath(os.path.join(__file__, "../../..")) | ||
os.chdir(root_dir) | ||
|
||
sys.path.append(root_dir) | ||
|
||
### May choose any of these models | ||
from pyhealth.models.sequence.dipole import Dipole | ||
# from pyhealth.models.sequence.lstm import LSTM as model | ||
# from pyhealth.models.sequence.gru import GRU as GRU | ||
# from pyhealth.models.sequence.embedgru import EmbedGRU as model | ||
# from pyhealth.models.sequence.retain import Retain as model | ||
# from pyhealth.models.sequence.raim import RAIM as model | ||
# from pyhealth.models.sequence.tlstm import tLSTM as model | ||
# from pyhealth.models.sequence.xgboost import XGBoost as model | ||
# from pyhealth.models.sequence.rf import RandomForest as model | ||
|
||
from pyhealth.data.expdata_generator import sequencedata as expdata_generator | ||
from pyhealth.evaluation.evaluator import func | ||
|
||
if __name__ == "__main__": | ||
# override here to specify where the data locates | ||
# root_dir = '' | ||
# root_dir = os.path.abspath(os.path.join(__file__, "../../..")) | ||
data_dir = os.path.join(root_dir, 'datasets', 'mimic') | ||
|
||
expdata_id = '2020.0811.data.phenotyping.test.v2' | ||
|
||
# set up the datasets | ||
cur_dataset = expdata_generator(expdata_id, root_dir=root_dir) | ||
cur_dataset.get_exp_data(sel_task='phenotyping', data_root=data_dir) | ||
cur_dataset.load_exp_data() | ||
# cur_dataset.show_data() | ||
|
||
# initialize the model for training | ||
expmodel_id = '2020.0811.model.phenotyping.test.v2' | ||
clf = Dipole(expmodel_id=expmodel_id, n_batchsize=20, use_gpu=False, | ||
n_epoch=30) | ||
clf.fit(cur_dataset.train, cur_dataset.valid) | ||
|
||
# load the best model for inference | ||
clf.load_model() | ||
clf.inference(cur_dataset.test) | ||
pred_results = clf.get_results() | ||
print(pred_results) | ||
|
||
# evaluate the model | ||
r = func(pred_results['hat_y'], pred_results['y']) | ||
print(r) |
58 changes: 58 additions & 0 deletions
58
examples/learning_models/example_sequence_gpu_mortality.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
# environment setting | ||
import os | ||
import sys | ||
|
||
sys.path.append( | ||
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..'))) | ||
root_dir = os.path.abspath(os.path.join(__file__, "../../..")) | ||
os.chdir(root_dir) | ||
|
||
sys.path.append(root_dir) | ||
|
||
### May choose any of these models | ||
# from pyhealth.models.sequence.dipole import Dipole as model | ||
from pyhealth.models.sequence.lstm import LSTM as model | ||
# from pyhealth.models.sequence.gru import GRU as GRU | ||
# from pyhealth.models.sequence.embedgru import EmbedGRU as model | ||
# from pyhealth.models.sequence.retain import Retain as model | ||
# from pyhealth.models.sequence.raim import RAIM as model | ||
# from pyhealth.models.sequence.tlstm import tLSTM as model | ||
# from pyhealth.models.sequence.xgboost import XGBoost as model | ||
# from pyhealth.models.sequence.rf import RandomForest as model | ||
|
||
from pyhealth.data.expdata_generator import sequencedata as expdata_generator | ||
from pyhealth.evaluation.evaluator import func | ||
|
||
if __name__ == "__main__": | ||
# override here to specify where the data locates | ||
# root_dir = '' | ||
# root_dir = os.path.abspath(os.path.join(__file__, "../../..")) | ||
data_dir = os.path.join(root_dir, 'datasets', 'cms') | ||
|
||
expdata_id = '2020.0810.data.mortality.mimic' | ||
|
||
# set up the datasets | ||
cur_dataset = expdata_generator(expdata_id, root_dir=root_dir) | ||
cur_dataset.get_exp_data(sel_task='mortality', data_root=data_dir) | ||
cur_dataset.load_exp_data() | ||
cur_dataset.show_data() | ||
|
||
# initialize the model for training | ||
# turn on GPU by setting use_gpu to True | ||
expmodel_id = '2020.0810.gru.data.mortality.mimic.gpu' | ||
clf = model(expmodel_id=expmodel_id, n_batchsize=20, use_gpu=True, | ||
n_epoch=100, gpu_ids='0,1') | ||
clf.fit(cur_dataset.train, cur_dataset.valid) | ||
|
||
# load the best model for inference | ||
clf.load_model() | ||
clf.inference(cur_dataset.test) | ||
pred_results = clf.get_results() | ||
print(pred_results['hat_y']) | ||
|
||
# evaluate the model | ||
r = func(pred_results['hat_y'], pred_results['y']) | ||
print(r) |
This file was deleted.
Oops, something went wrong.