Medial Code Documentation
Loading...
Searching...
No Matches
MedPlotly.h
1#pragma once
2//
3// Class to handle creation of charts using the plotly js package
4//
5// Main functionalities:
6// (1) Configurable page content
7// (2) Single signal charts
8// (3) Panel charts
9// (4) drugs heatmap
10// (5) Major demographics table (TBD as a table)
11// (6) Registries View (TBD)
12// (7) ReadCodes View
13//
14//
15
16
17
18#include <string>
19#include <iostream>
20#include <sstream>
21#include <iomanip>
22#include <boost/algorithm/string/split.hpp>
23#include <InfraMed/InfraMed/InfraMed.h>
24#include <InfraMed/InfraMed/MedPidRepository.h>
27
29#undef LOCAL_SECTION
30#define LOCAL_SECTION LOG_APP
31#define LOCAL_LEVEL LOG_DEF_LEVEL
32using namespace std;
33
34
35//=========================================================================================================
36namespace PlotlyColorDefaults {
37
38
39 static const vector<string> bg_opaque_colors = { "rgba(162, 217, 206,0.333)" , "rgba(199, 210, 232, 0.333)" , "rgba(220, 199, 232,0.333)",
40 "rgba(232, 199, 204,0.333)", "rgba(200, 232, 199,0.333)", "rgba(232, 217, 199,0.333)", "rgba(215, 216, 192,0.333)" };
41
42 static string get_bg_color(int i) { return PlotlyColorDefaults::bg_opaque_colors[i % (int)PlotlyColorDefaults::bg_opaque_colors.size()]; }
43
44};
45
46//=========================================================================================================
48public:
49 int null_zeros = 1;
50 int log_scale = 1;
51 int time_chan = 0;
52 int val_chan = 0;
53 bool get_ascenders_codes = false;
54 int ascender_limit = 1;
55 string filter_regex_codes = "";
56 string remove_regex_codes = "";
57
58 int init(map<string, string>& _map);
59};
60
61//=========================================================================================================
63public:
64 string name = ""; // to be used when adding it
65 string title = ""; // title of panel chart
66 vector<string> sigs; // sigs to be drawn on panel chart.
67 vector<string> drugs; // list of drug groups to visualize
68 vector<string> drug_colors; // bg colors to use. (when not initialized, will be initialized from a defaulted list).
69 int null_zeros = -1; // -1: means take default
70 int log_scale = -1; // -1: means take default
71 int width = -1;
72 int height = -1;
73 int block_mode = -1;
74
75 PanelInfo() {};
76 PanelInfo(string _name, string _title, const vector<string> &_sigs, const vector<string> &_drugs, const vector<string> &_drug_colors, int _null_zeros, int _log_scale) {
77 name = _name; title = _title; sigs = _sigs; drugs = _drugs; drug_colors = _drug_colors; null_zeros = _null_zeros; log_scale = _log_scale;
78 }
79
80 int init(map<string, string>& _map);
81};
82
83
84//=========================================================================================================
86public:
87 vector<string> drugs;
88 string color0;
89 string color1;
90 int granularity_months = 1;
91 int min_date = 20000101;
92 string drug_sig = "Drug";
93 int width = 1200;
94 int height = 200;
95
96
97 int init(map<string, string>& _map);
98};
99
100//=========================================================================================================
101// General Params:
102// Either defaulted , or read from a config file
103//=========================================================================================================
105public:
106 string page_title = "Patient Report";
107
108 // signal specific params
109 map<string, SignalParams> sig_params;
110
111 // Drugs groups
112 map<string, vector<string>> drugs_groups;
113
114 // Panels
115 map<string, PanelInfo> panels;
116
117 // drugs heatmap
118 DrugsHeatMapParams dhm_params;
119
120 // view order
121 vector<string> views;
122
123 // javascript
124 string js_dir = "";
125 vector<string> js_files;
126
127 // zeros handling
128 int null_zeros_default = 1; // in default we treat zeros as outliers and don't draw them.
129 int log_scale_default = 1;
130 int width_default = 600;
131 int height_default = 400;
132 int block_mode_default = 1;
133
134 // time unit , currently supporting Date and Minutes
135 int rep_time_unit = MedTime::Date; // default is Date.
136
137 MedModel model_rep_processors;
138 bool load_dynamically = true;
139 vector<string> phisical_read_sigs;
140 vector<string> all_need_sigs;
141
142 int read_config(const string &config_fname);
143
144};
145
146//=========================================================================================================
147// Used in order to be able to mark some interesting times on generated charts
149public:
150 int time;
151 string name;
152 string color = ""; // if empty will use default color lists
153
154 ChartTimeSign() {};
155 ChartTimeSign(int t, const string &_name, const string _color) { time = t; name = _name; color = _color; }
156 int init(map<string, string>& _map);
157
158 // example of how to initialize a vector:
159 //vector<ChartTimeSign> vec = { ChartTimeSign(1,"a","b") , ChartTimeSign(2,"d","e") };
160
161};
162
164public:
165 LocalViewsParams() {};
166 LocalViewsParams(int _pid, int _from, int _to) { pid = _pid; from_date = _from; to_date = _to; }
167 int pid = 0;
168 int from_date = 0;
169 int to_date = 0;
170
171
172};
173
175public:
176 PidRec rec_static;
177 PidDynamicRec rec_dynamic;
178 bool use_dynamic = false;
179
180 void uget(int sid, UniversalSigVec &usv) {
181 if (use_dynamic)
182 rec_dynamic.uget(sid, rec_dynamic.get_n_versions() - 1, usv);
183 else
184 rec_static.uget(sid, usv);
185 }
186 void uget(const string &sid, UniversalSigVec &usv) {
187 if (use_dynamic)
188 rec_dynamic.uget(sid, rec_dynamic.get_n_versions() - 1, usv);
189 else
190 rec_static.uget(sid, usv);
191 }
192 void *get(const string &sid, int &len) {
193 if (use_dynamic)
194 return rec_dynamic.get(sid, rec_dynamic.get_n_versions() - 1, len);
195 else
196 return rec_static.get(sid, len);
197 }
198 int pid() {
199 if (use_dynamic)
200 return rec_dynamic.pid;
201 else
202 return rec_static.pid;
203 }
204 MedRepository *my_base_rep() {
205 if (use_dynamic)
206 return rec_dynamic.my_base_rep;
207 else
208 return rec_static.my_base_rep;
209 }
210};
211
212//=========================================================================================================
213// MedPatientPlotlyDate:
214// - Built to handle date based repositories
215// - allows configurable viewing of a given PidRec
216//=========================================================================================================
218
219public:
220 MedPlotlyParams params;
221
222 int read_config(const string &config_name) { return params.read_config(config_name); }
223
224 // gets a pid record and returns a string representing the html for its view page based on configuration
225 // mode is either file (for an html file:/// version) or server (for use with a web server returning it)
226 int get_rec_html(string &shtml, LocalViewsParams &lvp, PidDataRec &rec, const string &mode, const vector<ChartTimeSign> &sign_times, const vector<string> &view);
227 int get_rec_html(string &shtml, LocalViewsParams &lvp, PidDataRec &rec, const string &mode, const vector<ChartTimeSign> &sign_times) {
228 return get_rec_html(shtml, lvp, rec, mode, sign_times, params.views);
229 }
230
231 int init_rep_processors(MedPidRepository &rep, const string &rep_conf);
232
233private:
234 // builders for html
235 int add_html_header(string &shtml, const string &mode);
236 int add_basic_demographics(string &shtml, PidDataRec &rec, vector<ChartTimeSign> &times);
237 int add_panel_chart(string &shtml, LocalViewsParams &lvp, PidDataRec &rec, const PanelInfo &pi, const vector<ChartTimeSign> &times);
238 int add_drugs_heatmap(string &shtml, PidDataRec &rec);
239
240 // THIN_RC report
241 bool add_categorical_chart(string &shtml, PidDataRec &rec, const vector<ChartTimeSign> &times,
242 const string &sig_name, string &div_name, bool show_legend, int channel);
243
244 void add_search_box(string &shtml, const string &sig_name, const string &div_chart, const string &div_table, int channel);
245
246 // categorical signal , add as table
247 bool add_categorical_table(string sig, string &shtml, PidDataRec &rec, const vector<ChartTimeSign> &times, string &div_name);
248
249 // heatmap creation
250 void get_drugs_heatmap(PidDataRec &rec, vector<int> &_xdates, vector<string> &_sets_names, vector<vector<float>> &_hmap, const vector<string> &drugs);
251
252
253 // adding a specific dataset of a sepcific sig + channel
254 void add_xy_js(string &shtml, vector<int> &dates, vector<float> &vals, int null_zeros_flag, string prefix);
255 void add_xy_js(string &shtml, UniversalSigVec &usv, int time_chan, int chan, int null_zeros_flag, string prefix);
256
257 void add_dataset_js(string &html, UniversalSigVec &usv, int time_chan, int chan, int null_zeros_flag, string prefix, string sname, int yaxis, string sig);
258 void add_bg_dataset_js(string &shtml, vector<int> &dates, vector<float> &vals, int null_zeros_flag, string color, string prefix, string sname, int yaxis, string name);
259
260 // helpers
261 string date_to_string(int date, bool fix_days_valid = false);
262 string time_to_string(int time, int time_unit);
263 string time_to_string(int time) { return time_to_string(time, params.rep_time_unit); }
264 void get_usv_min_max(UniversalSigVec &usv, float &vmin, float &vmax);
265
266};
Logger.h - allowing logs with more control.
MedTime.h.
Definition MedPlotly.h:148
int init(map< string, string > &_map)
Virtual to init object from parsed fields.
Definition MedPlotly.cpp:130
Definition MedPlotly.h:85
int init(map< string, string > &_map)
Virtual to init object from parsed fields.
Definition MedPlotly.cpp:115
Definition MedPlotly.h:163
A model = repCleaner + featureGenerator + featureProcessor + MedPredictor.
Definition MedModel.h:56
Definition MedPlotly.h:217
Definition MedPidRepository.h:87
Definition MedPlotly.h:104
Definition InfraMed.h:303
static const int Date
dates are in full regular format YYYYMMDD
Definition MedTime.h:25
Definition MedPlotly.h:62
int init(map< string, string > &_map)
Virtual to init object from parsed fields.
Definition MedPlotly.cpp:92
Definition MedPlotly.h:174
Definition MedPidRepository.h:127
Definition MedPidRepository.h:43
Definition SerializableObject.h:33
Definition MedPlotly.h:47
int init(map< string, string > &_map)
Virtual to init object from parsed fields.
Definition MedPlotly.cpp:72