Medial Code Documentation
Loading...
Searching...
No Matches
MedUtils.h
1//
2// MedUtils: Collection of several needed utilities (Files IO, Matrix data type, Data structures, etc)
3//
4
5// This is a simple h file to include all library parts
6
7#ifndef __MED_UTILS_H__
8#define __MED_UTILS_H__
9
10//#include "MedGenUtils.h"
11//#include "MedMat.h"
12//#include "MedMedical.h"
13//#include "MedDataStructures.h"
15#include "assert.h"
16#include "MedPlot.h"
17#if !defined(MES_LIBRARY)
18#include <boost/program_options.hpp>
19#endif
20#include <boost/spirit/home/support/detail/hold_any.hpp>
21#include <boost/algorithm/string.hpp>
22#include <unordered_map>
23#include <MedMat/MedMat/MedMatConstants.h>
24#include "MedRunPath.h"
25#include "MedSamplingHelper.h"
26
27using namespace std;
28
29enum MedBinningType {
30 BIN_EQUIDIST,
31 BIN_EQUISIZE,
32 BIN_LAST
33};
34
35// Discretization
36template <class S> int discretize(vector<S>& x, vector<int>& binned_x, int& nbins, int max_bins);
37template <class S> int discretize(vector<S>& x, vector<int>& binned_x, int& nbins, int max_bins, float missing_value);
38template <class S> int discretize(vector<S>& x, vector<int>& binned_x, int& nbins, int max_bins, MedBinningType binning);
39template <class S> int discretize(vector<S>& x, vector<int>& binned_x, int& nbins, int max_bins, float missing_value, MedBinningType binning);
40
41#if !defined(MES_LIBRARY)
42namespace po = boost::program_options;
43#endif
44
48namespace medial {
52 namespace print {
54 template<class T> string print_obj(T obj, const string &format);
56 template<class T> void print_vec(const vector<T> &vec, const string &title, const string &format, const string &delimeter = ", ");
58 void print_vec(const vector<string> &vec, const string &title, const string &delimeter = ", ");
60 template<class T> void print_hist_vec(const vector<T> &vec, const string &title, const string &format, const vector<double> *prctile_samples = NULL);
61 #if !defined(MES_LIBRARY)
63 string print_any(po::variable_value &a);
64 #endif
65
66 void log_with_file(ofstream &fw, const char *format_str, ...);
67 }
71 namespace process {
73 template<class T> void prctils(const vector<T> &x, const vector<double> &prc, vector<T> &res, const vector<float> *weights = NULL);
75 template<typename T> int binary_search_index(const T *begin, const T *end, T val);
77 template<typename T> int binary_search_position(const T *begin, const T *end, T val, bool reversed = false);
79 template<typename T> int binary_search_position_last(const T *begin, const T *end, T val, bool reversed = false);
80
82 template<class T> int binary_search_position(const vector<T> &v, T search);
84 template<class T> int binary_search_position(const vector<T> &v, T search,int start, int end);
85 }
86
90 namespace sort_ops {
92 template<typename T> void get_sort_indexes(const vector<T> &x, const bool descending_order, bool const abs_val, vector<int> &indexes);
93 }
94
95
99 namespace io {
101 void read_codes_file(const string &file_path, vector<string> &tokens);
102 template<class T> string get_list(const unordered_map<string, T> &ls, const string &delimeter = ",") {
103 string res = "";
104 for (auto it = ls.begin(); it != ls.end(); ++it)
105 if (it == ls.begin())
106 res += it->first;
107 else
108 res += delimeter + it->first;
109 return res;
110 }
111 template<class T> string get_list_op(const unordered_map<T, string> &ls, const string &delimeter = ",") {
112 string res = "";
113 for (auto it = ls.begin(); it != ls.end(); ++it)
114 if (it == ls.begin())
115 res += it->second;
116 else
117 res += delimeter + it->second;
118 return res;
119 }
120 template<class ContainerType> string get_list(const ContainerType &ls, const string &delimeter = ",");
121
122 #if !defined(MES_LIBRARY)
131 private:
132 string base_config;
133 bool init_called;
134
135 po::options_description desc;
139 virtual void post_process() {};
141 string get_section(const string &full_help, const string &search);
142
144 void list_sections(const string &full_help, vector<string> &all_sec);
145 protected:
147 void init(po::options_description &prg_options, const string &app_l = "");
149 ProgramArgs_base() { init_called = false; debug = false; }
150 public:
151 bool debug;
152 po::variables_map vm;
153 string app_logo = "\
154## ## ######## ######## #### ### ## \n\
155### ### ## ## ## ## ## ## ## \n\
156#### #### ## ## ## ## ## ## ## \n\
157## ### ## ###### ## ## ## ## ## ## \n\
158## ## ## ## ## ## ######### ## \n\
159## ## ## ## ## ## ## ## ## \n\
160## ## ######## ######## #### ## ## ######## ";
161
162
163
165 virtual int parse_parameters(int argc, char *argv[]);
166
167 };
168 #endif
169 }
170
171
173 string get_git_version();
174}
175
176#include "MedUtils_imp.h"
177
178#endif
Logger.h - allowing logs with more control.
A Library to plot graphs in HTML using plotly.js Example Code: vector<map<float,...
A basic class wrapper to parse command args has default "h", "help", "debug" and "base_config" for re...
Definition MedUtils.h:130
string app_logo
the application logo/name
Definition MedUtils.h:153
void init(po::options_description &prg_options, const string &app_l="")
an init function
Definition MedUtils.cpp:356
bool debug
a debug flag for verbose printing. will be init from command args
Definition MedUtils.h:151
ProgramArgs_base()
the ctor of base class
Definition MedUtils.h:149
virtual int parse_parameters(int argc, char *argv[])
the main function to parse the command arguments
Definition MedUtils.cpp:404
process(fname, allow_type)
Definition lint.py:152
void read_codes_file(const string &file_path, vector< string > &tokens)
reads file with codes name to vector
Definition MedUtils.cpp:533
void print_hist_vec(const vector< T > &vec, const string &title, const string &format, const vector< double > *prctile_samples=NULL)
printing vector elements hist prctiles in list [] with title to MLOG
Definition MedUtils.cpp:139
string print_obj(T obj, const string &format)
general print to string woth format
Definition MedUtils.cpp:47
string print_any(po::variable_value &a)
print boost program options object
Definition MedUtils.cpp:336
void print_vec(const vector< T > &vec, const string &title, const string &format, const string &delimeter=", ")
printing vector elements in list [] with title to MLOG
Definition MedUtils.cpp:113
int binary_search_position(const T *begin, const T *end, T val, bool reversed=false)
binary search for position to add new element in sorted manner (first position if equal elements foun...
Definition MedUtils.cpp:232
void prctils(const vector< T > &x, const vector< double > &prc, vector< T > &res, const vector< float > *weights=NULL)
calc prctile
Definition MedUtils.cpp:57
int binary_search_index(const T *begin, const T *end, T val)
binary search for index. -1 if not found
Definition MedUtils.cpp:205
int binary_search_position_last(const T *begin, const T *end, T val, bool reversed=false)
binary search for position to add new element in sorted manner (last position if equal elements found...
Definition MedUtils.cpp:277
void get_sort_indexes(const vector< T > &x, const bool descending_order, bool const abs_val, vector< int > &indexes)
sorts x (or abs(x)) and returns the indexes of sorted values in indexes (descending or ascending)
Definition MedUtils.cpp:12
medial namespace for function
Definition InfraMed.h:667
string get_git_version()
general print to string woth format
Definition MedUtils.cpp:554
Definition BFloat16.h:88