AlgoMarkers
AlgoMarkers are wrappers to models or calculators that are able to "talk" to the AlgoAnalyzer via the API defined in AlgoMarker.h (the api funcs start with AM_API_... ). Since the AlgoAnalyzer uses the AlgoMarker as a shared library from a c# code, it is written is "C" style bare bones to allow for the integration. This page describes the following:
- How to write a new AlgoMarker
- Compile the AlgoMarker shared library
- The MedialInfra AlgoMarker
- How to freeze a MedialInfra AlgoMarker
- How to test the AlgoMarker shared library
How to write a new Custom AlgoMarker
The AlgoMarker base class is in AlgoMarker.h in the AlgoMarker lib which is part of medial research libs git. So to work with it pull it from git first. In order to write a new AlgoMarker one has to go through the following steps:
- Write a new class that inherits from AlgoMarker (or from one of its derived classes), and make sure the following virtual functions are all filled:
- virtual int Load(const char *config_f) : gets a config file (or NULL if you don't need one), and gets the AlgoMarker into a working state.
- virtual int Unload() : releases all allocated memory and closes
- virtual int AddData(int patient_id, const char signalName, int TimeStamps_len, long long TimeStamps, int Values_len, float* Values)
- Adds data to the AlgoMarker which later could be used to ask scores for
- Data is added per pid, for a specific signalName, and then 2 arrays giving the time and value channels. The order in each array is element by element and then each channel for each element.
- virtual int ClearData() : Clear all data in the AlgoMarker
- virtual int Calculate(AMRequest request, AMResponses responses)
- Major API : once the AlgoMarker is loaded and has data, one can give it requests and get responses (=results).
- To handle requests and responses one can check their classes and the example AlgoMarkers already implemented.
- Add a type to your new AlgoMarker in the enum AlgoMarkerType
- Update the AlgoMarker::make_algomarker routine to support your new class
- That's It !! compile and ready to go. NO NEED to touch any of the API routines - they only rely on the implementation of the 5 routines in (1).
[!NOTE]
In most use cases(all till now), you won't need to do that and write a custom AlgoMarker. You can use TYPE = "MEDIAL_INFRA" and our existing implementation supports all what you need. So you can skip this step.
Compile The AlgoMarker library
- Follow the AlgoMarker Library Setup to build the
libdyn_AlgoMarker.so
library, or use a pre-built version if available.
The MedialInfra AlgoMarker
The MedialInfra AlgoMarker allows using any model that was trained using Medial MedProcessTools infrastructure. On top of that it allows also for some nice configuration of eligibility testing on input data, and is packaged with a configuration file that's easy to edit and work with.
Configuration File
When Loading a new MedialInfra AlgoMarker a configuration file is given. The format of the general file is explained in the next example:
Eligibility rules configuration
Most of the options can be seen in the above example, here are the basics:
- Each filter is given in a line : FILTER
| | | | | | - Currently
is always 'simple' or 'attr' : see examples above and definitions below : one of WARNING or ERROR. A warning will run the test, report the problem if found, but not fail the example and still will give a score for it. : ACC=1 or ACC=0 : state from which filters to accumulate the overall number of outliers (MAX_OVERALL_OUTLIERS) : read code to return in the message case the test did not pass : internal read code returned (another layer of error codes that is neededed : string - free text that will be returned as the error message in case the test did not pass. Filter params is given in a string , separator is ';' and there should be no spaces/tabs. It has many options, see the class SanitySampleFilter to view them formally.
Parameters for simple filter type
- sig : name of the signal we want to test
- time_ch , val_ch : time and value channels to test signal with (defaults are 0 and 0)
- win_time_unit : default Days
- samples_time_unit: default Date
- min_val , max_val : min and max allowed values for a signal (>=min <=max) when testing for outliers.
- win_from, win_to : a time window before and relative to the sample in which we apply the test:
- Each of the tests asked for will be done only on the signal results in the given time window.
- Default is all the tests (infinite window to the past)
- min_Nvals, max_Nvals : test min or max number of values (ALL values not just those in a given range if one is given) for the given signal and window.
- max_outliers : maximal allowed number of outliers for the signal in the given time window
- min_left : minimal number of results left after throwing the outliers.
- allowed_values : list of values , seprated by comma (',') . Test verifies that all values (in the given window) are one of the given allowed values.
- values_in_dictionary : if =1 (default 0) , will test that all the values of the signal are valid values in the repository dictionary. Useful for categorical data such as Drug or RC. A filter can be configured to do one simple test, or several. As explained above many filters can be defined. All filters defined will run on every point (pid,timepoint pair) , and all the filters that did not pass will push their error message into the relevant response messages. For cases in which the sample did not pass a filter defined as ERROR , the AlgoMarker will not generate a score. However for cases which only had warnings, it will.
Parameters for attribute ('attr') filter type
To set this filter use 'attr' in filter type (see example above). The use of this filters relies on the fact the model was built with an option to create those attributes and inject them into the resulted MedSamples object at prediction time. The classical use is to let cleaners and testers running in the model to report their results this way. Formally these actions will add an attribute to each prediction, and the filter defined here is able to create rules based on those attributes. Parameters:
- name : the name of the attribute to test
-
max : the maximal value allowed. An error or warning will be given for any value larger than this. To make sure a model creates those attributes it is needed to make it do so in its json definition.
-
To a basic cleaner ( "rp_type": "basic_cln" in json) add : "nrem_attr": "nRem","ntrim_attr": "nTrim", in order to get an attribute of the number of removed signals, and/or trimmed ones. Note this counts the total number and not in a specific time window....
- To force a panel use for example:
- {"action_type":"rep_processor","rp_type":"req","signals":"Hemoglobin,RBC,Hematocrit", "win_from": "0", "win_to": "365"},
- this will count how many of the panel signals are missing in the given time window (relative to prediction point). Note that if placed AFTER cleaners (reccomended) it will test this on the cleaned data that may have some values removed.
How to freeze a MedialInfra AlgoMarker Version
If using a non frozen libraries version:Either create a branch with a suitable name for your freeze for the Libs gitTag your version in the branch you want to work with.
It is reccomended to freeze also code for Tools git, and code for releavant projects (for example the Diabetes git project for a diabetes algomarker, etc).When freezing several different gits, make sure to tag all of them with the same tagDocument your freeze and branches/tags names.
- Each version that is being compiled with our scripts, contains the git last commit information and date of compilation for both MR_Libs and MR_Tools. No need to tag/create branches or things you might forget.
- Make sure you have everything prepared:
- A model file trained for the AlgoMarker. Better test this very algomarker gives the expected results on your test set, and runs with the frozen libraries version.
- The repository files you were working with: main needed:
- the .repository file
- the .signals file
- the dictionary files for the signals you are using.
- Create a new directory, call it with the agreed upon algomarker+version name
- Put there:
- The algomarker model file
- The repository files used
- Prepare an algo marker config file as explained above
- Make sure the eligibility rules are the ones you want.
- Good time to run a small unit-test here to see it loads, runs and gives expected results on some prepared data set.
- Zip directory.
It is also describred in here
How to test the AlgoMarker library
The AlgoMarker project includes the DllAPITester, which can be used to test the DLL and compare its output to scores given by the Flow app with existing repository. With this way we can run score compare and check that the results using AlgoMarker library and our tools used in the research are equivelent. After all json/format conversions and using this wrapper.
DllAPITester Help
Example Run | |
---|---|
Testing a single example directly | |
---|---|