Skip to content

Commit b867ca8

Browse files
authored
Adding LTFU stratification (#98)
* adding LTFU stratification * Addressing PR comment
1 parent a0a5da5 commit b867ca8

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

src/event/internals/treatment_internals.hpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Created Date: 2025-04-18 //
55
// Author: Matthew Carroll //
66
// ----- //
7-
// Last Modified: 2025-05-12 //
7+
// Last Modified: 2025-05-19 //
88
// Modified By: Matthew Carroll //
99
// ----- //
1010
// Copyright (c) 2025 Syndemics Lab at Boston Medical Center //
@@ -16,8 +16,10 @@
1616
#include <string>
1717

1818
// Library Includes
19+
#include <hepce/data/types.hpp>
1920
#include <hepce/utils/config.hpp>
2021
#include <hepce/utils/formatting.hpp>
22+
#include <hepce/utils/logging.hpp>
2123

2224
// Local Includes
2325
#include "event_internals.hpp"
@@ -43,9 +45,10 @@ class TreatmentBase : public EventBase {
4345
double toxicity = 1.0;
4446
};
4547
struct TreatmentProbabilities {
46-
double loss_to_follow_up = 0.0;
4748
double initialization = 0.0;
4849
};
50+
using ltfu_map_t = std::unordered_map<data::PregnancyState, double>;
51+
ltfu_map_t _ltfu_probability;
4952

5053
TreatmentBase(datamanagement::ModelData &model_data,
5154
const std::string &log_name = "console")
@@ -56,8 +59,6 @@ class TreatmentBase : public EventBase {
5659
SetTreatmentLimit(
5760
utils::GetIntFromConfig("treatment.treatment_limit", model_data));
5861

59-
_probabilities.loss_to_follow_up = utils::GetDoubleFromConfig(
60-
"treatment.ltfu_probability", model_data);
6162
_costs.treatment =
6263
utils::GetDoubleFromConfig("treatment.treatment_cost", model_data);
6364
_utilities.treatment = utils::GetDoubleFromConfig(
@@ -78,9 +79,6 @@ class TreatmentBase : public EventBase {
7879
_utilities = u;
7980
}
8081
inline void SetTreatmentCosts(const TreatmentCosts &c) { _costs = c; }
81-
inline void SetTreatmentProbabilities(const TreatmentProbabilities &p) {
82-
_probabilities = p;
83-
}
8482
inline void SetEligibilities(const Eligibilities &e) { _eligibilities = e; }
8583

8684
inline bool GetTreatmentLimit() const { return _treatment_limit; }
@@ -105,6 +103,26 @@ class TreatmentBase : public EventBase {
105103
"eligibility.ineligible_time_former_threshold", model_data);
106104
}
107105

106+
inline void LoadLostToFollowUpData(datamanagement::ModelData &model_data) {
107+
std::any storage = ltfu_map_t{};
108+
try {
109+
model_data.GetDBSource("inputs").Select(
110+
LostToFollowUpSQL(), LostToFollowUpCallback, storage);
111+
} catch (std::exception &e) {
112+
std::stringstream msg;
113+
msg << "Error getting " << GetInfectionType()
114+
<< " Lost To Follow Up Data: " << e.what();
115+
hepce::utils::LogError(GetLogName(), msg.str());
116+
return;
117+
}
118+
_ltfu_probability = std::any_cast<ltfu_map_t>(storage);
119+
if (_ltfu_probability.empty()) {
120+
std::stringstream s;
121+
s << GetInfectionType() << " Linking Data is Empty...";
122+
hepce::utils::LogWarning(GetLogName(), s.str());
123+
}
124+
}
125+
108126
/// @brief
109127
/// @return
110128
virtual data::InfectionType GetInfectionType() const = 0;
@@ -126,7 +144,9 @@ class TreatmentBase : public EventBase {
126144
// follow up
127145
if (!person.GetTreatmentDetails(GetInfectionType())
128146
.initiated_treatment &&
129-
(sampler.GetDecision({_probabilities.loss_to_follow_up}) == 0)) {
147+
(sampler.GetDecision({_ltfu_probability[person.GetPregnancyDetails()
148+
.pregnancy_state]}) ==
149+
0)) {
130150
QuitEngagement(person);
131151
return true;
132152
}
@@ -172,13 +192,25 @@ class TreatmentBase : public EventBase {
172192
: false;
173193
}
174194

195+
static void LostToFollowUpCallback(std::any &storage,
196+
const SQLite::Statement &stmt) {
197+
ltfu_map_t *temp = std::any_cast<ltfu_map_t>(&storage);
198+
data::PregnancyState state =
199+
static_cast<data::PregnancyState>(stmt.getColumn(0).getInt());
200+
(*temp)[state] = stmt.getColumn(1).getDouble();
201+
}
202+
175203
private:
176204
int _treatment_limit = 0;
177205
TreatmentUtilities _utilities;
178206
TreatmentCosts _costs;
179207
TreatmentProbabilities _probabilities;
180208
Eligibilities _eligibilities;
181209

210+
inline const std::string LostToFollowUpSQL() const {
211+
return "SELECT pregnancy_state, probability FROM lost_to_follow_up;";
212+
}
213+
182214
/// @brief
183215
/// @param
184216
/// @return

0 commit comments

Comments
 (0)