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