Commit 7a470f39 authored by Joseph Larralde's avatar Joseph Larralde
Browse files

updated rapidPiPoTools to v0.2.1 of pipo

parent 9fa50687
......@@ -9,10 +9,10 @@
/**
* @mainpage RAPID-MIX API
*
*
* @section Introduction
*
* Hello, API ! <br>
* Hello, API ! <br>
* All the classes, functions and structs documented here belong to the
* <code>rapidmix</code> namespace.
*
......@@ -39,6 +39,7 @@
#define RAPIDMIX_VERSION_MINOR 2
#define RAPIDMIX_VERSION_PATCH 1
#define RAPIDMIX_VERSION "2.2.1"
#define RAPIDMIX_JSON_DOC_VERSION "1.0.0"
#define RAPIDMIX_REVISION "10-NOVEMBER-2017"
#include "machineLearning/machineLearning.h"
......
#include "rapidmix.h"
#include "rapidPiPoHost.h"
#include <iostream>
#include <fstream>
//=========================== H O S T === U T I L S ==========================//
/*
static const unsigned int maxWordLen = 256;
static bool getPiPoInstanceAndAttrName(const char *attrName,
char *instanceName,
char *pipoAttrName)
{
const char *dot = std::strrchr(attrName, '.');
if (dot != NULL)
{
long pipoAttrNameLen = dot - attrName;
std::strcpy(pipoAttrName, dot + 1);
if (pipoAttrNameLen > maxWordLen)
{
pipoAttrNameLen = maxWordLen;
}
std::strncpy(instanceName, attrName, pipoAttrNameLen);
instanceName[pipoAttrNameLen] = '\0';
return true;
}
return false;
}
//*/
static void fromPiPoStreamAttributes(PiPoStreamAttributes &src,
pipoStreamAttributes &dst)
RapidPiPoStreamAttributes &dst)
{
unsigned int numCols = src.dims[0];
unsigned int numLabels = src.numLabels;
if (numLabels > PIPO_MAX_LABELS) {
numLabels = PIPO_MAX_LABELS;
}
if (numLabels > numCols) {
numLabels = numCols;
}
......@@ -76,7 +47,7 @@ static void fromPiPoStreamAttributes(PiPoStreamAttributes &src,
}
static void toPiPoStreamAttributes(pipoStreamAttributes &src,
static void toPiPoStreamAttributes(RapidPiPoStreamAttributes &src,
PiPoStreamAttributes &dst)
{
const char *labs[src.labels.size()];
......@@ -101,258 +72,31 @@ static void toPiPoStreamAttributes(pipoStreamAttributes &src,
//========================= H O S T === M E T H O D S ========================//
PiPoHost::PiPoHost() :
inputStreamAttrs(PIPO_MAX_LABELS),
outputStreamAttrs(PIPO_MAX_LABELS)
{
PiPoCollection::init();
this->out = new PiPoOut(this);
this->graph = nullptr;
}
PiPoHost::~PiPoHost()
{
if (this->graph != nullptr)
{
delete this->graph;
}
delete this->out;
}
bool
PiPoHost::setGraph(std::string name)
{
if (this->graph != nullptr)
{
delete this->graph;
}
this->graph = PiPoCollection::create(name);
if (this->graph != NULL)
{
this->graphName = name;
this->graph->setReceiver((PiPo *)this->out);
return true;
}
this->graph = nullptr;
this->graphName = "undefined";
return false;
}
void
PiPoHost::clearGraph()
{
if (this->graph != nullptr)
{
delete this->graph;
this->graph = nullptr;
}
}
void
PiPoHost::onNewFrameOut(double time, std::vector<PiPoValue> &frame)
{
std::cout << time << std::endl;
std::cout << "please override this method" << std::endl;
}
std::vector<PiPoValue>
PiPoHost::getLastFrameOut()
{
return this->out->getLastFrame();
}
int
PiPoHost::setInputStreamAttributes(pipoStreamAttributes &sa, bool propagate)
RapidPiPoHost::setInputStreamAttributes(RapidPiPoStreamAttributes &rpsa)
{
toPiPoStreamAttributes(sa, inputStreamAttrs);
if (propagate)
{
return this->propagateInputStreamAttributes();
}
return 0;
PiPoStreamAttributes psa;
toPiPoStreamAttributes(rpsa, psa);
return PiPoHost::setInputStreamAttributes(psa);
}
pipoStreamAttributes
PiPoHost::getOutputStreamAttributes()
{
pipoStreamAttributes sa;
fromPiPoStreamAttributes(this->outputStreamAttrs, sa);
return sa;
}
int
PiPoHost::frames(double time, double weight, PiPoValue *values, unsigned int size,
unsigned int num)
{
return this->graph->frames(time, weight, values, size, num);
}
// bool
// setAttr(const std::string &attrName, bool value)
// {
// }
// bool
// setAttr(const std::string &attrName, int value)
// {
// }
bool
PiPoHost::setAttr(const std::string &attrName, double value)
{
PiPo::Attr *attr = this->graph->getAttr(attrName.c_str());
if (attr != NULL)
{
int iAttr = attr->getIndex();
return this->graph->setAttr(iAttr, value);
}
return false;
}
bool
PiPoHost::setAttr(const std::string &attrName, const std::vector<double> &values)
{
PiPo::Attr *attr = this->graph->getAttr(attrName.c_str());
if (attr != NULL)
{
int iAttr = attr->getIndex();
double vals[values.size()];
unsigned int i = 0;
for (auto &value : values)
{
vals[i] = value;
i++;
}
return this->graph->setAttr(iAttr, &vals[0], static_cast<unsigned int>(values.size()));
}
return false;
}
bool
PiPoHost::setAttr(const std::string &attrName, const std::string &value) // for enums
{
PiPo::Attr *attr = this->graph->getAttr(attrName.c_str());
if (attr != NULL)
{
// int iAttr = attr->getIndex();
PiPo::Type type = attr->getType();
if (type == PiPo::Type::Enum)
{
std::vector<const char *> *list = attr->getEnumList();
for (int i = 0; i < list->size(); i++)
{
if (strcmp(list->at(i), value.c_str()) == 0)
{
attr->set(0, i);
return true;
}
}
}
}
return false;
}
std::vector<std::string>
PiPoHost::getAttrNames()
{
std::vector<std::string> res;
for (unsigned int i = 0; i < this->graph->getNumAttrs(); ++i)
{
res.push_back(this->graph->getAttr(i)->getName());
}
return res;
}
double
PiPoHost::getDoubleAttr(const std::string &attrName)
{
PiPo::Attr *attr = this->graph->getAttr(attrName.c_str());
if (attr != NULL) {
// int iAttr = attr->getIndex();
PiPo::Type type = attr->getType();
if (type == PiPo::Type::Double) {
return attr->getDbl(0);
}
}
return 0;
}
std::vector<double>
PiPoHost::getDoubleArrayAttr(const std::string &attrName)
{
std::vector<double> res;
PiPo::Attr *attr = this->graph->getAttr(attrName.c_str());
if (attr != NULL) {
// int iAttr = attr->getIndex();
PiPo::Type type = attr->getType();
if (type == PiPo::Type::Double) {
for (int i = 0; i < attr->getSize(); ++i) {
res.push_back(attr->getDbl(i));
}
}
}
return res;
}
std::string
PiPoHost::getEnumAttr(const std::string &attrName)
void
RapidPiPoHost::onNewFrame(double time, double weight, PiPoValue *values, unsigned int size)
{
PiPo::Attr *attr = this->graph->getAttr(attrName.c_str());
if (attr != NULL) {
// int iAttr = attr->getIndex();
PiPo::Type type = attr->getType();
if (type == PiPo::Type::Enum) {
return attr->getStr(0);
}
}
return "";
std::cout << "I received " << size << " new frame(s)" << std::endl;
}
//============================= JSON FORMATTING ==============================//
//----------------------------- JSON FORMATTING ------------------------------//
std::string
PiPoHost::getJSON()
RapidPiPoHost::getJSON()
{
Json::Value result = toJSON();
return result.toStyledString();
}
void
PiPoHost::writeJSON(const std::string &filepath)
RapidPiPoHost::writeJSON(const std::string &filepath)
{
Json::Value root = toJSON();
std::ofstream jsonOut;
......@@ -363,7 +107,7 @@ PiPoHost::writeJSON(const std::string &filepath)
}
bool
PiPoHost::putJSON(const std::string &jsonMessage)
RapidPiPoHost::putJSON(const std::string &jsonMessage)
{
Json::Value parsedFromString;
Json::Reader reader;
......@@ -372,7 +116,7 @@ PiPoHost::putJSON(const std::string &jsonMessage)
}
bool
PiPoHost::readJSON(const std::string &filepath)
RapidPiPoHost::readJSON(const std::string &filepath)
{
Json::Value root;
std::ifstream file(filepath);
......@@ -380,23 +124,26 @@ PiPoHost::readJSON(const std::string &filepath)
return fromJSON(root);
}
//======================== PROTECTED HOST METHODS ============================//
//-------------------------- PRIVATE HOST METHODS ----------------------------//
Json::Value
PiPoHost::toJSON()
RapidPiPoHost::toJSON()
{
Json::Value root;
Json::Value metadata;
Json::Value pipodata;
metadata["creator"] = "RAPID-MIX API";// C++";
metadata["version"] = "v0.1.1"; //TODO: This should be a macro someplace
metadata["family"] = "pipo";
root["metadata"] = metadata;
/*
root["docType"] = "rapid-mix:signal-processing";
root["docVersion"] = RAPIDMIX_JSON_DOC_VERSION;
Json::Value target;
target["name"] = "pipo";
target["version"] = PIPO_SDK_VERSION;
root["target"] = target;
Json::Value pipodata;
pipodata["description"] = this->graphName;
Json::Value inputStream;
inputStream["hasTimeTags"] = inputStreamAttrs.hasTimeTags;
inputStream["rate"] = inputStreamAttrs.rate;
......@@ -407,7 +154,7 @@ PiPoHost::toJSON()
inputStream["hasVarSize"] = inputStreamAttrs.hasVarSize;
inputStream["domain"] = inputStreamAttrs.domain;
inputStream["maxFrames"] = inputStreamAttrs.maxFrames;
Json::Value outputStream;
outputStream["hasTimeTags"] = outputStreamAttrs.hasTimeTags;
outputStream["rate"] = outputStreamAttrs.rate;
......@@ -422,9 +169,9 @@ PiPoHost::toJSON()
Json::Value streams;
streams["input"] = inputStream;
streams["output"] = outputStream;
pipodata["streamAttributes"] = streams;
Json::Value params;
int n = this->graph->getNumAttrs();
params.resize(static_cast<Json::ArrayIndex>(n));
......@@ -434,25 +181,39 @@ PiPoHost::toJSON()
Json::Value param;
PiPo::Attr *a = this->graph->getAttr(i);
param["name"] = a->getName();
param["type"] = 0; // DO SOMETHING TO GET TYPES FROM PIPO IN A NICE WAY
param["value"] = a->getDbl(0);
params[i] = param;
}
pipodata["parameters"] = params;
root["pipodata"] = pipodata;
root["payload"] = pipodata;
//*/
return root;
}
bool
PiPoHost::fromJSON(Json::Value &jv)
RapidPiPoHost::fromJSON(Json::Value &jv)
{
if (jv["metadata"]["family"].asString().compare("pipo") == 0 &&
jv["pipodata"].size() > 0) {
/*
if (jv["docType"].asString().compare("rapid-mix:signal-processing") == 0 &&
//jv["docVersion"].asString().compare(RAPIDMIX_JSON_DOC_VERSION) == 0 &&
jv["target"]["name"].asString().compare("pipo") == 0 &&
jv["payload"].size() > 0) {
this->setGraph(jv["pipodata"]["description"].asString());
Json::Value params = jv["payload"]["parameters"]
unsigned int nAttrs = params.size();
for (unsigned int i = 0; i < nAttrs; ++i)
{
}
Json::Value inputStream = jv["pipodata"]["streamAttributes"]["input"];
// setInputStreamAttributes(
// inputStream["hasTimeTags"].getDbl()//,
......@@ -460,305 +221,7 @@ PiPoHost::fromJSON(Json::Value &jv)
// );
return true;
}
return false;
}
//========================= PRIVATE HOST METHODS =============================//
int
PiPoHost::propagateInputStreamAttributes()
{
if (this->graph != nullptr)
{
return this->graph->streamAttributes(this->inputStreamAttrs.hasTimeTags,
this->inputStreamAttrs.rate,
this->inputStreamAttrs.offset,
this->inputStreamAttrs.dims[0],
this->inputStreamAttrs.dims[1],
this->inputStreamAttrs.labels,
this->inputStreamAttrs.hasVarSize,
this->inputStreamAttrs.domain,
this->inputStreamAttrs.maxFrames);
}
return 0;
}
void
PiPoHost::setOutputAttributes(bool hasTimeTags, double rate, double offset,
unsigned int width, unsigned int height,
const char **labels, bool hasVarSize,
double domain, unsigned int maxFrames) {
if (labels != NULL) {
int numLabels = width;
if (numLabels > PIPO_MAX_LABELS) {
numLabels = PIPO_MAX_LABELS;
}
for (unsigned int i = 0; i < numLabels; i++) {
try {
this->outputStreamAttrs.labels[i] = labels[i];
} catch(std::exception e) {
this->outputStreamAttrs.labels[i] = "unnamed";
}
}
this->outputStreamAttrs.numLabels = numLabels;
} else {
this->outputStreamAttrs.numLabels = 0;
}
this->outputStreamAttrs.hasTimeTags = hasTimeTags;
this->outputStreamAttrs.rate = rate;
this->outputStreamAttrs.offset = offset;
this->outputStreamAttrs.dims[0] = width;
this->outputStreamAttrs.dims[1] = height;
this->outputStreamAttrs.hasVarSize = hasVarSize;
this->outputStreamAttrs.domain = domain;
this->outputStreamAttrs.maxFrames = maxFrames;
}
// void
// PiPoHost::streamAttributesChanged(PiPo *pipo, PiPo::Attr *attr) {
// this->propagateInputAttributes();
// }
// void
// PiPoHost::signalError(PiPo *pipo, std::string errorMsg) {
// // todo
// }
// void
// PiPoHost::signalWarning(PiPo *pipo, std::string warningMsg) {
// // todo
// }
//--------------------- INPUT STREAM ATTRIBUTES SETTERS ----------------------//
/*
void
PiPoHost::setInputHasTimeTags(bool hasTimeTags, bool propagate) {
this->inputStreamAttrs.hasTimeTags = hasTimeTags;
if (propagate) {
this->propagateInputAttributes();
}
}
#define MIN_PIPO_SAMPLERATE (1.0 / 31536000000.0) // once a year
#define MAX_PIPO_SAMPLERATE (96000000000.0)
void
PiPoHost::setInputFrameRate(double rate, bool propagate) {
if (rate <= MIN_PIPO_SAMPLERATE) {
this->inputStreamAttrs.rate = MIN_PIPO_SAMPLERATE;
} else if (rate >= MAX_PIPO_SAMPLERATE) {
this->inputStreamAttrs.rate = MAX_PIPO_SAMPLERATE;
} else {
this->inputStreamAttrs.rate = rate;
}
if (propagate) {
this->propagateInputAttributes();
}
}
void
PiPoHost::setInputFrameOffset(double offset, bool propagate) {
this->inputStreamAttrs.offset = offset;
if (propagate) {
this->propagateInputAttributes();
}
}
void
PiPoHost::setInputDims(int width, int height, bool propagate) {
this->inputStreamAttrs.dims[0] = width;
this->inputStreamAttrs.dims[1] = height;
if (propagate) {
this->propagateInputAttributes();
}
}
void
PiPoHost::setInputLabels(const std::vector<std::string> &labels, bool propagate) {
const char *labs[labels.size()];
for (unsigned int i = 0; i < labels.size(); ++i) {
labs[i] = labels[i].c_str();
}
this->inputStreamAttrs.labels = &labs[0];
if (propagate) {