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