-
Notifications
You must be signed in to change notification settings - Fork 0
/
cognitive_clock.py
41 lines (33 loc) · 1.67 KB
/
cognitive_clock.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
import numpy as np
import matplotlib.pyplot as plt
from pathlib2 import Path
import cognitive_tests
import pickle
class cognitive_clock:
def __init__(self, root_dir=""):
self.all_info = self.load_model(root_dir)
def load_model(self, root_dir="", models_dir='models', model_version = '1.0', export_date = '2022-04-22'):
model_export_path = Path(root_dir) / models_dir / f'model_{model_version}_{export_date}.pkl'
with open(model_export_path, 'rb') as file:
all_info = pickle.load(file)
return all_info
def prepare_data(self, tests_258, tests_274, tests_278):
test_258 = cognitive_tests.gen_258(tests_258)
test_274 = cognitive_tests.gen_274(tests_274)
test_278 = cognitive_tests.gen_278(tests_278)
cur_tests = np.concatenate([test_258, test_274, test_278])
return cur_tests
def compute_cognitive_age(self, tests_258, tests_274, tests_278):
tests = self.prepare_data(tests_258, tests_274, tests_278)
return self.compute_cognitive_age_tests(self, tests)
def compute_cognitive_age_tests(self, tests):
feature_names = cognitive_tests.gen_names_258() + cognitive_tests.gen_names_274() + cognitive_tests.gen_names_278()
df_model_features = self.all_info['df_model_features']
selected_features = df_model_features['selected_features']
model = self.all_info['model']
tests = tests[selected_features]
xp_mean = df_model_features.mean_feature.values
xp_sd = df_model_features.sd_feature.values
xp = (tests - xp_mean) / xp_sd
cognitive_age = model.predict(xp.reshape(1, -1)).item()
return cognitive_age