Medial Code Documentation
Loading...
Searching...
No Matches
SigSum_AlgoMarker.h
1#pragma once
2
3//===============================================================================
4// AlgoMarker.h
5//-------------------------------------------------------------------------------
6//
7// Wrapper for a DLL that will contain :
8// (1) All the needed API's to perform as an AlgoMarker
9// (2) The option to run full MedProcessTools models with MedRepository inputs.
10// (3) All in one single DLL managing it all.
11//
12//===============================================================================
13
14
15// AM_DLL_EXPORT is defined only in the matching .cpp file to handle the dll building
16// apps just include this h file and hence will work in import mode.
17
18#define _SCL_SECURE_NO_WARNINGS
19
20#ifdef _MSC_VER
21#define strncasecmp _strnicmp
22#define strcasecmp _stricmp
23#endif
24
25
26#ifdef _WIN32
27#if defined AM_DLL_IMPORT
28#define DLL_WORK_MODE __declspec(dllimport)
29#else // !AM_DLL_IMPORT
30#define DLL_WORK_MODE __declspec(dllexport)
31#endif // AM_DLL_IMPORT
32#else // !_WIN32
33#if defined AM_DLL_IMPORT
34#define DLL_WORK_MODE
35#else // !AM_DLL_IMPORT
36#define DLL_WORK_MODE __attribute__ ((visibility ("default")))
37#endif //AM_DLL_IMPORT
38#endif // _WIN32
39
40#ifdef _WIN32
41#pragma warning(disable: 4251)
42#endif
43
44//
45// includes of Medial Internal Libraries
46//
47#include "SigSum_AlgoMarkerErr.h"
48
49#include <string>
50#include <map>
51#include <unordered_map>
52#include <stdint.h>
53
54#undef LOCAL_SECTION
55#define LOCAL_SECTION LOG_APP
56#define LOCAL_LEVEL LOG_DEF_LEVEL
57using namespace std;
58#include <vector>
59
60#define AM_UNDEFINED_VALUE -9999.99
61
62
63typedef enum {
64 AM_TYPE_UNDEFINED = 0,
65 AM_TYPE_MEDIAL_INFRA = 1
66} AlgoMarkerType;
67
68
69
70//===============================================================================
71// Responses and Requests classes
72//===============================================================================
73extern "C" class DLL_WORK_MODE AMPoint {
74public:
75 int pid = -1;
76 long long timestamp = -1;
77
78 void set(int _pid, long long _timestamp) { pid = _pid; timestamp = _timestamp; }
79
80 void clear() { pid = -1; timestamp = -1; }
81};
82
83//-------------------------------------------------------------------------------
84extern "C" class DLL_WORK_MODE AMMessages {
85
86private:
87
88 vector<int> codes;
89 vector<string> args_strs;
90
91 vector<char *> args; // for easier c c# export. pointing to strings , so no need to free.
92 int need_to_update_args = 0;
93
94public:
95
96 // get things
97 int get_n_msgs() { return (int)codes.size(); }
98 void get_messages(int *n_msgs, int **msgs_codes, char ***msgs_args);
99
100 // insert
101 void insert_message(int code, const char *arg_ch);
102
103 // clear
104 void clear() {
105 codes.clear();
106 args_strs.clear();
107 args.clear();
108 }
109
110};
111
112//-------------------------------------------------------------------------------
113extern "C" class DLL_WORK_MODE AMScore {
114private:
115 // no need to release the score type pointer
116 char *p_score_type = NULL;
117 float score = (float)AM_UNDEFINED_VALUE;
118 AMMessages msgs;
119
120public:
121 // get things
122 void get_score(float *_score, char **_score_type)
123 {
124 *_score = score;
125 *_score_type = p_score_type;
126
127 //char* x = p_score_type;
128
129 /* if (p_score_type != NULL)
130 {
131 string x = string(p_score_type);
132 *_score_type = (char *)x.c_str();
133 }*/
134 }
135 AMMessages *get_msgs() { return &msgs; }
136
137 // set things
138 void set_score_type(char *_score_type) { p_score_type = _score_type; }
139 void set_score(float _score) { score = _score; }
140
141 // clear
142 void clear()
143 {
144 msgs.clear();
145 p_score_type = NULL;
146 score = (float)AM_UNDEFINED_VALUE;
147 }
148};
149
150//-------------------------------------------------------------------------------
151extern "C" class DLL_WORK_MODE AMResponse {
152
153private:
154
155 // p_score_types just points to the common info in the AMResponses class, no need to free
156 vector<AMScore> scores;
157
158 AMPoint point;
159
160 AMMessages msgs;
161
162public:
163
164 vector<string> verificationConfig;
165 map<string, int> scoresIndexs;
166 int need_to_update_scoreTypes = 0;
167
168 // get things
169 int get_patient_id() { return point.pid; }
170 long long get_timestamp() { return point.timestamp; }
171 int get_n_scores() { return (int)scores.size(); }
172 AMScore *get_am_score(int idx) { if (idx < 0 || idx >= scores.size()) return NULL; return &scores[idx]; }
173 int get_score(int idx, float *_score, char **_score_type) {
174 if (idx < 0 || idx >= scores.size()) return AM_FAIL_RC;
175 scores[idx].get_score(_score, _score_type);
176 return AM_OK_RC;
177 }
178 AMMessages *get_score_msgs(int idx) { if (idx < 0 || idx >= scores.size()) return NULL; return scores[idx].get_msgs(); }
179 AMMessages *get_msgs() { return &msgs; }
180
181 // set things
182 void set_patient_id(int _patient_id) { point.pid = _patient_id; }
183 void set_timestamp(long long _timestamp) { point.timestamp = _timestamp; }
184 void set_score(int idx, float _score, char *_score_type) { if (idx >= 0 && idx < scores.size()) scores[idx].set_score(_score); scores[idx].set_score_type(_score_type); }
185 void init_scores(int size) { scores.clear(); scores.resize(size); }
186
187 // clear
188 void clear()
189 {
190 for (auto element : scores)
191 {
192 element.clear();
193 }
194 scores.clear();
195 point.clear();
196 }
197
198
199};
200
201extern "C" class DLL_WORK_MODE AMData {
202
203public:
204
205 int patient_id;
206 string signalName;
207 int TimeStamps_len;
208 long long* TimeStamps;
209 int Values_len;
210 float* Values;
211
212 // clear
213 void clear()
214 {
215 signalName.clear();
216 delete[] Values;
217 delete[] TimeStamps;
218 Values = NULL;
219 TimeStamps = NULL;
220 }
221};
222
223extern "C" class DLL_WORK_MODE AMDataStr {
224
225public:
226
227 int patient_id;
228 string signalName;
229 int TimeStamps_len;
230 long long* TimeStamps;
231 int Values_len;
232 char** Values;
233
234 // clear
235 void clear()
236 {
237 signalName.clear();
238 delete[] Values;
239 delete[] TimeStamps;
240 Values = NULL;
241 TimeStamps = NULL;
242 }
243};
244//-------------------------------------------------------------------------------
245extern "C" class DLL_WORK_MODE AMResponses {
246
247private:
248
249 string requestId = "";
250 string version = "";
251
252 // For each point: pid , time : we hold an AMResponse object that contains all the results on all types for this time point
253 // plus all its specific messages
254 vector<AMResponse> responses;
255 //map<pair<int, long long>, int> point2response_idx;
256
257 // score_types : these are common to all responses
258 vector<string> score_types_str;
259 vector<char *> score_types;
260 //unordered_map<string, int> stype2idx;
261
262 // In here we report messages not specific to a single Response
263 AMMessages shared_msgs;
264public:
265
266 vector<string> verificationConfig;
267
268 // get things
269 int get_n_responses()
270 {
271 return (int)responses.size();
272 }
273 AMResponse *get_response(int index) { if (index >= (int)responses.size()) return NULL; return &(responses[index]); }
274 int get_response_index_by_point(int _pid, long long _timestamp); // if does not exist returns -1.
275 // AMResponse *get_response_by_point(int _pid, long long _timestamp); // if does not exist, return NULL
276 void get_score_types(int *n_score_types, char ***_score_types);
277 AMMessages *get_shared_messages() { return &shared_msgs; }
278 char *get_request_id() { return (char *)requestId.c_str(); }
279 char *get_version() { return (char *)version.c_str(); }
280 int get_score(int _pid, long long _timestamp, char *_score_type, float *out_score);
281 int get_score_by_type(int index, char *_score_type, float *out_score);
282 vector<char *> *get_score_type_vec_ptr() { return &score_types; }
283
284 // set things
285 void set_request_id(char *request_id) { requestId = string(request_id); }
286 void set_version(char *_version) { version = string(_version); }
287 void insert_score_types(char **_score_type, int n_score_types);
288 AMResponse *create_point_response(int _pid, long long _timestamp);
289
290 // clear
291 void clear() {
292 requestId.clear();
293 version.clear();
294 for (auto element : responses)
295 {
296 element.clear();
297 }
298 responses.clear();
299 //point2response_idx.clear();
300 score_types_str.clear();
301 score_types.clear();
302 //stype2idx.clear();
303 shared_msgs.clear();
304 }
305
306};
307
308//-------------------------------------------------------------------------------
309extern "C" class DLL_WORK_MODE AMRequest {
310
311private:
312
313 // Id for tracking given by user
314 string requestId = "";
315
316 // score types asked for
317 // Currently supporting : "Raw"
318 vector<string> score_types_str;
319
320 // list of points to give scores at
321 vector<AMPoint> points;
322
323public:
324
325 vector<string> verificationConfig;
326
327
328 // get things
329 char *get_request_id() { return (char *)requestId.c_str(); }
330 int get_n_score_types() { return (int)score_types_str.size(); }
331 char *get_score_type(int index) { if (index >= get_n_score_types()) return NULL; return (char *)score_types_str[index].c_str(); }
332 int get_n_points() { return (int)points.size(); }
333 AMPoint *get_point(int index) { if (index >= get_n_points()) return NULL; return &points[index]; }
334 int get_pid(int index) { if (index >= get_n_points()) return -1; return points[index].pid; }
335 long long get_timestamp(int index) { if (index >= get_n_points()) return -1; return points[index].timestamp; }
336
337 // set things
338 void set_request_id(char *req_id) { requestId = string(req_id); }
339 void insert_point(int _pid, long long _timestamp) { AMPoint p; p.set(_pid, _timestamp); points.push_back(p); }
340 void insert_score_types(char **_score_types, int n_score_types) { for (int i = 0; i<n_score_types; i++) score_types_str.push_back(string(_score_types[i])); }
341
342 // clear
343 void clear()
344 {
345 for (auto element : points)
346 {
347 element.clear();
348 }
349 requestId.clear();
350 score_types_str.clear();
351 points.clear();
352 }
353};
354
355
356//===============================================================================
357// Base AlgoMarker class
358//===============================================================================
359extern "C" class DLL_WORK_MODE AlgoMarker {
360private:
361 AlgoMarkerType type;
362 string name = "";
363 string config_fname = "";
364 vector<string> supported_score_types;
365
366
367public:
368
369 // major APIs
370 // When creating a new type of algomarker one needs to inherit from this class, and
371 // make sure to implement the following virtual APIs. This will suffice.
372 virtual int Load(const char *config_f) { return 0; }
373 virtual int Unload() { return 0; }
374 virtual int ClearData() { return 0; }
375 virtual int AddData(int patient_id, const char *signalName, int TimeStamps_len, long long* TimeStamps, int Values_len, float* Values) { return 0; }
376 virtual int AddDataStr(int patient_id, const char *signalName, int TimeStamps_len, long long* TimeStamps, int Values_len, char** Values) { return 0; }
377 virtual int Calculate(AMRequest *request, AMResponses *responses) { return 0; }
378
379 vector<AMData*> data;
380 vector<string> verificationConfig;
381
382 // check supported score types in the supported_score_types vector
383 int IsScoreTypeSupported(const char *_stype);
384
385 // get things
386 int get_type() { return (int)type; }
387 char *get_name() { return (char *)name.c_str(); }
388 char *get_config() { return (char *)config_fname.c_str(); }
389
390 // set things
391 void set_type(int _type) { type = (AlgoMarkerType)_type; }
392 void set_name(const char *_name) { name = string(_name); }
393 void set_config(const char *_config_f) { config_fname = string(_config_f); }
394 void add_supported_stype(const char *stype) { supported_score_types.push_back(string(stype)); }
395
396 // get a new AlgoMarker
397 static AlgoMarker *make_algomarker(AlgoMarkerType am_type);
398
399 virtual ~AlgoMarker() {};
400
401};
402
403
404//===============================================================================
405// MedialInfraAlgoMarker - an AlgoMarker that works with Medial infrastructure
406//===============================================================================
407extern "C" class DLL_WORK_MODE MedialInfraAlgoMarker : public AlgoMarker {
408
409private:
410 // some configs
411 string type_in_config_file = "";
412 string rep_fname = "";
413 string model_fname = "";
414 //string input_tests_filters = "";
415
416 int read_config(string conf_f);
417
418 //vector<string> supported_score_types ={ "Raw" };
419
420
421public:
423 {
424 set_type((int)AM_TYPE_MEDIAL_INFRA);
425 add_supported_stype("Raw");
426 //data = new vector<AMData*>();
427 }
428
429 int Load(const char *config_f);
430 int Unload();
431 int ClearData();
432 int AddData(int patient_id, const char *signalName, int TimeStamps_len, long long* TimeStamps, int Values_len, float* Values);
433 int AddDataStr(int patient_id, const char *signalName, int TimeStamps_len, long long* TimeStamps, int Values_len, char** Values);
434 int Calculate(AMRequest *request, AMResponses *responses);
435
436};
437
438
440{
441 static ofstream *logFileStream;
442public:
443 static void closeLogFile();
444 static bool TurnOnLogs;
445 static vector<string> Arguments;
446 static const char* FunctionName;
447 static bool DoesEnterFunction;
448 static void WriteToLog(string line);
449 static void FlushLogData();
450 static void StartFunction(const char* function);
451 static int StartFunction(int returnValue, const char* function);
452 static void EndFunction(const char* function);
453 static int EndFunction(int returnValue, const char* function);
454 static void AddArgument(string argumentName, string argument);
455 static void AddArgument(string argumentName, char* argument);
456 static void AddArgument(string argumentName, int* argument);
457 static void AddArgument(string argumentName, float* argument);
458 static void AddArgument(string argumentName, long long* argument);
459 static void AddArgument(string argumentName, unsigned long long argument);
460 static void AddArgument(string argumentName, int argument);
461 static void AddArgument(string argumentName, float argument);
462 static void AddArgument(string argumentName, long long argument);
463 static void AddArgument(string argumentName, char** argument);
464 static void AddArgument(string argumentName, int** argument, int arraySize);
465 static void AddArgument(string argumentName, char*** argument, int arraySize);
466 static void AddArgument(string argumentName, char** argument, int arraySize);
467 static void AddArgument(string argumentName, long long* argument, int arraySize);
468 static void AddArgument(string argumentName, int* argument, int arraySize);
469 static void AddArgument(string argumentName, float* argument, int arraySize);
470 static void AddArgument(string argumentName, AlgoMarker* argument);
471 static void AddArgument(string argumentName, AlgoMarker** argument);
472 static void AddArgument(string argumentName, AMRequest* argument);
473 static void AddArgument(string argumentName, AMRequest** argument);
474 static void AddArgument(string argumentName, AMResponses* argument);
475 static void AddArgument(string argumentName, AMResponses** argument);
476 static void AddArgument(string argumentName, AMResponse* argument);
477 static void AddArgument(string argumentName, AMResponse** argument);
478};
479
480//========================================================================================
481// Actual API to be used via C# :: External users should work ONLY with these !!
482// Any change to the below functions must rely only on exposed API of the above classes.
483//========================================================================================
484
485// all return codes are defined in AlgoMarkerErr.h
486
487// create a new AlgoMarker of type am_type and init its name
488extern "C" DLL_WORK_MODE int AM_API_Create(int am_type, AlgoMarker **new_am);
489
490// loading AlgoMarker and making it ready to get Requests
491extern "C" DLL_WORK_MODE int AM_API_Load(AlgoMarker* pAlgoMarker, const char *config_fname);
492
493// clearing data from AlgoMarker (recommended at the start and/or end of each query session
494extern "C" DLL_WORK_MODE int AM_API_ClearData(AlgoMarker* pAlgoMarker);
495
496// adding data to an AlgoMarker
497// this API allows adding a specific signal, with matching arrays of times and values
498extern "C" DLL_WORK_MODE int AM_API_AddData(AlgoMarker* pAlgoMarker, int patient_id, const char *signalName, int TimeStamps_len, long long* TimeStamps, int Values_len, float* Values);
499
500// adding data string to an AlgoMarker
501// this API allows adding a specific signal, with matching arrays of times and string values
502extern "C" DLL_WORK_MODE int AM_API_AddDataStr(AlgoMarker* pAlgoMarker, int patient_id, const char *signalName, int TimeStamps_len, long long* TimeStamps, int Values_len, char** Values);
503
504// Prepare a Request
505// Null RC means failure
506extern "C" DLL_WORK_MODE int AM_API_CreateRequest(char *requestId, char **score_types, int n_score_types, int *patient_ids, long long *time_stamps, int n_points, AMRequest **new_req);
507
508// Get scores for a ready request
509extern "C" DLL_WORK_MODE int AM_API_Calculate(AlgoMarker *pAlgoMarker, AMRequest *request, AMResponses *responses);
510
511
512// Create a new empty responses object
513extern "C" DLL_WORK_MODE int AM_API_CreateResponses(AMResponses **new_responses);
514
515// exploring responses
516extern "C" DLL_WORK_MODE int AM_API_GetResponsesNum(AMResponses *responses);
517extern "C" DLL_WORK_MODE int AM_API_GetSharedMessages(AMResponses *responses, int *n_msgs, int **msgs_codes, char ***msgs_args);
518extern "C" DLL_WORK_MODE int AM_API_GetResponseIndex(AMResponses *responses, int _pid, long long _timestamp);
519
520extern "C" DLL_WORK_MODE int AM_API_GetResponseAtIndex(AMResponses *responses, int index, AMResponse **response);
521extern "C" DLL_WORK_MODE int AM_API_GetResponseScoresNum(AMResponse *response, int *n_scores);
522
523extern "C" DLL_WORK_MODE int AM_API_GetResponseScoreByIndex(AMResponse *response, int score_index, float *score, char **_score_type);
524extern "C" DLL_WORK_MODE int AM_API_GetScoreMessages(AMResponse *response, int score_index, int *n_msgs, int **msgs_codes, char ***msgs_args);
525extern "C" DLL_WORK_MODE int AM_API_GetResponseMessages(AMResponse *response, int *n_msgs, int **msgs_codes, char ***msgs_args);
526
527extern "C" DLL_WORK_MODE int AM_API_GetResponsePoint(AMResponse *response, int *pid, long long *timestamp);
528
529extern "C" DLL_WORK_MODE int AM_API_GetResponsesRequestId(AMResponses *responses, char **requestId);
530//extern "C" DLL_WORK_MODE int AM_API_GetResponseScoreByType(AMResponses *responses, int res_index, char *_score_type, float *out_score);
531
532// get the name of an algomarker
533extern "C" DLL_WORK_MODE int AM_API_GetName(AlgoMarker *pAlgoMArker, char **name);
534
535// Dispose of AlgoMarker - free all memory
536extern "C" DLL_WORK_MODE void AM_API_DisposeAlgoMarker(AlgoMarker *pAlgoMarker);
537
538// Dispose of AMRequest - free all memory
539extern "C" DLL_WORK_MODE void AM_API_DisposeRequest(AMRequest *pRequest);
540
541// Dispose of responses - free all memory
542extern "C" DLL_WORK_MODE void AM_API_DisposeResponses(AMResponses *responses);
543
544extern "C" DLL_WORK_MODE int AM_API_GetResponseScoreByType(AMResponses *responses, int res_index, char *_score_type, float *out_score);
Definition SigSum_AlgoMarker.h:223
Definition SigSum_AlgoMarker.h:201
Definition AlgoMarker.h:88
Definition AlgoMarker.h:74
Definition AlgoMarker.h:233
Definition AlgoMarker.h:136
Definition AlgoMarker.h:184
Definition AlgoMarker.h:113
Definition AlgoMarker.h:272
Definition SigSum_AlgoMarker.h:440
Definition AlgoMarker.h:337