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