Go to the documentation of this file.00001 #include <bwi_tools/common/Util.h>
00002
00003 #include <fstream>
00004 #include <cmath>
00005 #include <cassert>
00006 #include <stdlib.h>
00007 #include <limits>
00008
00009
00010
00011 std::map<int,double> TIME_MAP;
00012
00013 void tic(int id) {
00014 TIME_MAP[id] = getTime();
00015 }
00016
00017 double toc(int id) {
00018 return getTime() - TIME_MAP[id];
00019 }
00020
00021 void toc(double &counter, int id) {
00022 counter += toc(id);
00023 }
00024
00025 unsigned int vectorMaxInd(const std::vector<float> &arr) {
00026 float maxVal;
00027 unsigned int maxInd = 0;
00028 vectorMax(arr,maxVal,maxInd);
00029 return maxInd;
00030 }
00031
00032
00033 void vectorMax(const std::vector<float> &arr, float &maxVal, unsigned int &maxInd) {
00034 maxVal = -1 * std::numeric_limits<float>::infinity();
00035 for (unsigned int i = 0; i < arr.size(); i++) {
00036 if (arr[i] > maxVal) {
00037 maxVal = arr[i];
00038 maxInd = i;
00039 }
00040 }
00041 }
00042
00043
00044 float softmax(float x1, float x2, float factor) {
00045 x1 = exp(factor * x1);
00046 x2 = exp(factor * x2);
00047 return x1 / (x1 + x2);
00048 }
00049
00050
00051 void softmax(const std::vector<unsigned int> &vals, float factor, std::vector<float> &probs) {
00052 assert(vals.size() >= 1);
00053 probs.clear();
00054 std::vector<float> expVals(vals.size());
00055 float total = 0;
00056
00057 for (unsigned int i = 0; i < vals.size(); i++) {
00058 expVals[i] = exp(factor * vals[i]);
00059 total += expVals[i];
00060 }
00061 for (unsigned int i = 0; i < vals.size(); i++) {
00062 probs.push_back(expVals[i] / total);
00063 }
00064 }
00065
00066 bool readJson(const std::string &filename, Json::Value &value) {
00067 Json::Reader reader;
00068 std::ifstream in(filename.c_str());
00069 if (!in.good()) {
00070 std::cerr << "readJson: ERROR opening file: " << filename << std::endl;
00071 return false;
00072 }
00073 Json::Value root;
00074 bool parsingSuccessful = reader.parse(in,root);
00075 in.close();
00076 if (!parsingSuccessful) {
00077 std::cerr << "readJson: ERROR parsing file: " << filename << std::endl;
00078 std::cerr << reader.getFormatedErrorMessages();
00079 return false;
00080 }
00081 Json::Value::Members members = root.getMemberNames();
00082 for (unsigned int i = 0; i < members.size(); i++) {
00083 value[members[i]] = root[members[i]];
00084 }
00085
00086 return true;
00087 }
00088
00089 struct ReplaceMap {
00090 std::map<std::string,std::string> replacementMap;
00091
00092 ReplaceMap(const std::map<std::string,std::string> &replacementMap):
00093 replacementMap(replacementMap)
00094 {
00095 }
00096
00097 void operator() (Json::Value &value) {
00098
00099 if (value.isString()) {
00100 std::map<std::string,std::string>::const_iterator it;
00101 std::string str = value.asString();
00102 for (it = replacementMap.begin(); it != replacementMap.end(); it++) {
00103 size_t pos = str.find(it->first);
00104 if (pos != std::string::npos) {
00105 str.replace(pos,it->first.length(),it->second);
00106 }
00107 }
00108 value = Json::Value(str);
00109 }
00110 }
00111 };
00112
00113 void jsonReplaceStrings(Json::Value &value, const std::map<std::string,std::string> &replacementMap) {
00114 jsonReplaceStrings(value,ReplaceMap(replacementMap));
00115 }
00116
00117 void jsonReplaceStrings(Json::Value &value, boost::function<void (Json::Value &)> replace) {
00118 replace(value);
00119
00120 Json::Value::iterator child;
00121 if (value.isObject()) {
00122 std::vector<std::string> names = value.getMemberNames();
00123 for (unsigned int i = 0; i < names.size(); i++) {
00124 jsonReplaceStrings(value[names[i]],replace);
00125 }
00126 } else if (value.isArray()) {
00127 for (unsigned int i = 0; i < value.size(); i++) {
00128 jsonReplaceStrings(value[i],replace);
00129 }
00130 }
00131 }
00132
00133 std::string indent(unsigned int indentation) {
00134 return std::string(indentation * 2,' ');
00135 }
00136
00137 std::string tempFilename() {
00138 char filename[] = "/tmp/tmpXXXXXX";
00139 int fd = mkstemp(filename);
00140 if (fd == -1) {
00141 std::cerr << "Problem generating filename" << std::endl;
00142 exit(19);
00143 }
00144 close(fd);
00145 return std::string(filename);
00146 }