Note
Go to the end to download the full example code.
Basic usage of CDSS to generate a protocol recommendation.#
This example demonstrates how to use the DataLoader, DataProcessor, and CDSS to load data and generate a 7-day protocol plan for testing/CI purposes.
/home/runner/work/ai-cdss/ai-cdss/src/ai_cdss/processing/feature_builder.py:93: FutureWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.
df = df.groupby(by=[PATIENT_ID, SESSION_DATE], group_keys=False).apply(
PATIENT_ID PROTOCOL_ID PPF \
0 1 1 0.562178
1 1 2 0.965500
2 1 3 0.812396
3 1 4 0.719061
4 1 5 0.409213
CONTRIB ADHERENCE_RECENT \
0 [0.252232, 0.03653, 0.118418, 0.147197, 0.007801] 0.699687
1 [0.119867, 0.02396, 0.00862, 0.381091, 0.431962] 0.722053
2 [0.34855, 0.076633, 0.02168, 0.243166, 0.122367] 0.768798
3 [0.026373, 0.13851, 0.007091, 0.486407, 0.06068] 0.795200
4 [0.139366, 0.047926, 0.094186, 0.101513, 0.026... 0.757253
DELTA_DM WEEKS_SINCE_START SESSION_INDEX USAGE USAGE_WEEK \
0 0.003929 79 14 10 0
1 -0.003380 79 11 10 0
2 0.007271 79 12 10 0
3 -0.007977 79 11 10 0
4 0.004960 79 11 10 0
DAYS SCORE
0 [2, 3, 4, 5, 6] 1.265794
1 [0, 1, 3, 4, 5, 6] 1.684172
2 [0, 1, 2, 3, 5, 6] 1.588465
3 [0, 1, 2, 3, 4, 5, 6] 1.506284
4 [0, 2, 3, 4, 5, 6] 1.171426
import sys
sys.path.append("..")
import pandas as pd
from ai_cdss.cdss import CDSS
from ai_cdss.constants import EWMA_ALPHA
from ai_cdss.loaders.mock_loader import DataLoaderMock
from ai_cdss.models import DataUnit, DataUnitName, DataUnitSet, Granularity
from ai_cdss.processing.processor import DataProcessor
from IPython.display import display
print(__doc__)
PATIENT_LIST = [1, 2, 3]
# Parameters
rgs_mode = "app"
weights = [1, 1, 1]
alpha = EWMA_ALPHA
n = 12
days = 7
protocols_per_day = 5
# Services
loader = DataLoaderMock(num_patients=5, num_protocols=5, num_sessions=10)
processor = DataProcessor()
# Execution
session = loader.load_session_data(patient_list=PATIENT_LIST)
timeseries = loader.load_timeseries_data(patient_list=PATIENT_LIST)
ppf = loader.load_ppf_data(patient_list=PATIENT_LIST)
patient = loader.load_patient_data(patient_list=PATIENT_LIST)
protocol_similarity = loader.load_protocol_similarity()
# Construct DataUnitSet for processing
units = [session, ppf, patient]
data_unit_set = DataUnitSet(units)
scores = processor.process_data(data_unit_set, scoring_date=pd.Timestamp.today())
# CDSS
cdss = CDSS(scoring=scores, n=n, days=days, protocols_per_day=protocols_per_day)
# Results
patient_id = 1
recommendation = cdss.recommend(
patient_id=patient_id, protocol_similarity=protocol_similarity
)
recommendation.to_csv(f"recommendation_{patient_id}_new.csv", index=False)
with pd.option_context("display.max_columns", None):
display(recommendation)
Total running time of the script: (0 minutes 2.160 seconds)