first full mupp version allowing variables on scripting level, interactively still missing.

This commit is contained in:
suter_a 2020-04-30 17:11:57 +02:00
parent 7276ff4486
commit cbbe1d21a7
6 changed files with 284 additions and 24 deletions

View File

@ -113,7 +113,7 @@ int PmuppScript::executeScript()
} else if (cmd.startsWith("var")) {
status = var_cmd(cmd);
} else if (cmd.startsWith("col")) {
std::cout << "debug> will eventually handle linking of a variable to a collection ..." << std::endl;
// nothing to be done here, since var handles it internally
} else {
std::cerr << "**ERROR** found unkown script command '" << cmd.toLatin1().constData() << "'." << std::endl << std::endl;
status = -2;
@ -300,14 +300,32 @@ int PmuppScript::addX(const QString str)
// clean up plot info first
fPlotInfo.clear();
// make sure that label is found in ALL collections
// make sure that label is found in ALL collections, or in variables
// first check collections
bool foundInColl(true), foundInVar(true);
QString collName("");
for (int i=0; i<fParamDataHandler->GetNoOfCollections(); i++) {
coll = fParamDataHandler->GetCollection(i);
if (!foundLabel(coll, label)) { // label not found
foundInColl = false;
collName = coll->GetName();
break;
/* //as35
std::cerr << std::endl << "**ERROR** couldn't find '" << label.toLatin1().constData() << "' in collection '" << coll->GetName().toLatin1().constData() << "'" << std::endl << std::endl;
return -4;
*/ //as35
}
}
// second check variables
if (!foundVariable(label)) { // label not found
foundInVar = false;
}
// make sure label(s) have been found
if (!foundInColl && !foundInVar) { // not all labels found, neither in collection nor variables.
std::cerr << std::endl << "**ERROR** couldn't find '" << label.toLatin1().constData() << "' in collection '" << collName.toLatin1().constData() << "'," << std::endl;
std::cerr << " nor is it a defined variable" << std::endl << std::endl;
return -4;
}
// resize fPlotInfo to the number of selections
fPlotInfo.resize(fParamDataHandler->GetNoOfCollections());
@ -318,15 +336,32 @@ int PmuppScript::addX(const QString str)
fPlotInfo[i].xLabel = label;
}
} else { // a specific selection
// check that label is found in the selected collection
// check that label is found in the selected collection, or in variables
coll = fParamDataHandler->GetCollection(fSelected);
if (coll == 0) {
std::cerr << std::endl << "**ERROR** in addX: selected collection couldn't be found ..." << std::endl << std::endl;
return -3;
}
// first check collection
bool foundInColl(true), foundInVar(true);
QString collName("");
if (!foundLabel(coll, label)) { // label not found
foundInColl = false;
collName = coll->GetName();
/* //as35
std::cerr << std::endl << "**ERROR** couldn't find '" << label.toLatin1().constData() << "' in collection '" << coll->GetName().toLatin1().constData() << "'" << std::endl << std::endl;
return -4;
*/ //as35
}
// second check variables
if (!foundVariable(label)) { // label not found
foundInVar = false;
}
// make sure label(s) have been found
if (!foundInColl && !foundInVar) { // not all labels found, neither in collection nor variables.
std::cerr << std::endl << "**ERROR** couldn't find '" << label.toLatin1().constData() << "' in collection '" << collName.toLatin1().constData() << "'," << std::endl;
std::cerr << " nor is it a defined variable" << std::endl << std::endl;
return -4;
}
// feed plot entry
@ -362,16 +397,40 @@ int PmuppScript::addY(const QString str)
std::cerr << std::endl << "**ERROR** in addY. addY called without previous 'select' command." << std::endl << std::endl;
return -2;
} else if (fSelected == -1) { // i.e. select ALL
// make sure that label(s) is/are found in ALL collections
// make sure that label(s) is/are found in ALL collections, or in variables
// first check collections
bool foundInColl(true), foundInVar(true);
int idx = -1;
QString collName("");
for (int i=0; i<fParamDataHandler->GetNoOfCollections(); i++) {
coll = fParamDataHandler->GetCollection(i);
for (int j=0; j<label.size(); j++) {
if (!foundLabel(coll, label[j])) { // label not found
foundInColl = false;
collName = coll->GetName();
idx = j;
break;
/* //as35
std::cerr << std::endl << "**ERROR** couldn't find '" << label[j].toLatin1().constData() << "' in collection '" << coll->GetName().toLatin1().constData() << "'" << std::endl << std::endl;
return -4;
*/ //as35
}
}
}
// second check variables
for (int i=0; i<label.size(); i++) {
if (!foundVariable(label[i])) { // label not found
foundInVar = false;
idx = i;
break;
}
}
// make sure label(s) have been found
if (!foundInColl && !foundInVar) { // not all labels found, neither in collection nor variables.
std::cerr << std::endl << "**ERROR** couldn't find '" << label[idx].toLatin1().constData() << "' in collection '" << collName.toLatin1().constData() << "'," << std::endl;
std::cerr << " nor is it a defined variable" << std::endl << std::endl;
return -4;
}
// feed plot info with y-label(s)
for (int i=0; i<fPlotInfo.size(); i++) {
@ -381,19 +440,44 @@ int PmuppScript::addY(const QString str)
// clear yLabel
fPlotEntry.yLabel.clear();
// check that label is found in the selected collection
// check that label is found in the selected collection, or in the variables
coll = fParamDataHandler->GetCollection(fSelected);
if (coll == 0) {
std::cerr << std::endl << "**ERROR** in addY: selected collection couldn't be found ..." << std::endl << std::endl;
return -3;
}
// first check specific collection
bool foundInColl(true), foundInVar(true);
int idx = -1;
QString collName("");
for (int i=0; i<label.size(); i++) {
if (!foundLabel(coll, label[i])) { // label not found
foundInColl = false;
collName = coll->GetName();
idx = i;
break;
/* //as35
std::cerr << std::endl << "**ERROR** couldn't find '" << label[i].toLatin1().constData() << "' in collection '" << coll->GetName().toLatin1().constData() << "'" << std::endl << std::endl;
return -4;
*/ //as35
}
}
// second check variables
for (int i=0; i<label.size(); i++) {
if (!foundVariable(label[i])) { // label not found
foundInVar = false;
idx = i;
break;
}
}
// make sure label(s) have been found
if (!foundInColl && !foundInVar) { // not all labels found, neither in collection nor variables.
std::cerr << std::endl << "**ERROR** couldn't find '" << label[idx].toLatin1().constData() << "' in collection '" << collName.toLatin1().constData() << "'," << std::endl;
std::cerr << " nor is it a defined variable" << std::endl << std::endl;
return -4;
}
fPlotEntry.yLabel = label;
// add plot entry
@ -517,6 +601,16 @@ int PmuppScript::macro(const QString str, const QString plotFln)
// get collection name
collName = fParamDataHandler->GetCollectionName(i);
xx = fParamDataHandler->GetValues(collName, fPlotInfo[i].xLabel);
if (xx.size() == 0) { // it is a variable
int idx = getVarIndex(fPlotInfo[i].xLabel);
if (idx == -1) {
std::cerr << std::endl;
std::cerr << "**ERROR** Couldn't get x-label '" << fPlotInfo[i].xLabel.toLatin1().data() << "'." << std::endl;
std::cerr << " This should never happens." << std::endl;
return -3;
}
xx = QVector<double>::fromStdVector(fVarHandler[idx].getValues());
}
// get x-axis min/max
minMax(xx, x_min, x_max);
if (count==0) {
@ -533,6 +627,18 @@ int PmuppScript::macro(const QString str, const QString plotFln)
yy = fParamDataHandler->GetValues(collName, fPlotInfo[i].yLabel[j]);
yyPosErr = fParamDataHandler->GetPosErr(collName, fPlotInfo[i].yLabel[j]);
yyNegErr = fParamDataHandler->GetNegErr(collName, fPlotInfo[i].yLabel[j]);
if (yy.size() == 0) { // it's a variable
int idx = getVarIndex(fPlotInfo[i].yLabel[j]);
if (idx == -1) {
std::cerr << std::endl;
std::cerr << "**ERROR** Couldn't get y-label '" << fPlotInfo[i].yLabel[j].toLatin1().data() << "'." << std::endl;
std::cerr << " This should never happens." << std::endl;
return -3;
}
yy = QVector<double>::fromStdVector(fVarHandler[idx].getValues());
yyPosErr = QVector<double>::fromStdVector(fVarHandler[idx].getErrors());
yyNegErr = QVector<double>::fromStdVector(fVarHandler[idx].getErrors());
}
// get y-axis min/max
minMax(yy, y_min, y_max);
if (count==0) {
@ -684,7 +790,7 @@ int PmuppScript::var_cmd(const QString str)
if (idx == -1) // var not linked to collection, ignore it
return 0;
// check for the related error variable if present
// check if the related error variable is present
QString varErr = QString("%1%2").arg(tok[1]).arg("Err");
QString varErrCmd("");
for (int i=0; i<fScript.size(); i++) {
@ -700,7 +806,9 @@ int PmuppScript::var_cmd(const QString str)
parse_str += varErrCmd.toLatin1().data();
}
PVarHandler varHandler(fParamDataHandler->GetCollection(idx), parse_str);
PVarHandler varHandler(fParamDataHandler->GetCollection(idx), parse_str, tok[1].toLatin1().data());
if (!varHandler.isValid())
return 1;
fVarHandler.push_back(varHandler);
return 0;
@ -725,6 +833,44 @@ bool PmuppScript::foundLabel(PmuppCollection *coll, const QString label)
return result;
}
//--------------------------------------------------------------------------
/**
* @brief PmuppScript::foundVariable
* @param var
* @return
*/
bool PmuppScript::foundVariable(const QString var)
{
bool result = false;
for (int i=0; i<fVarHandler.size(); i++) {
if (!fVarHandler[i].getVarName().compare(var)) {
result = true;
break;
}
}
return result;
}
//--------------------------------------------------------------------------
/**
* @brief PmuppScript::getVarIndex
* @param var
* @return
*/
int PmuppScript::getVarIndex(const QString var)
{
int idx = -1;
for (int i=0; i<fVarHandler.size(); i++) {
if (!fVarHandler[i].getVarName().compare(var)) {
idx = i;
break;
}
}
return idx;
}
//--------------------------------------------------------------------------
/**
* @brief PmuppScript::minMax

View File

@ -89,6 +89,8 @@ class PmuppScript : public QObject
QVector<PVarHandler> fVarHandler;
bool foundLabel(PmuppCollection *coll, const QString label);
bool foundVariable(const QString var);
int getVarIndex(const QString var);
void minMax(QVector<double> dvec, double &min, double &max);
QString getNicerLabel(const QString label);

View File

@ -5,22 +5,21 @@ loadPath ./
load YBCO-40nm-FC-E3p8keV-B10mT-Tscan.db # collection 0
load YBCO-40nm-FC-E3p8keV-B150mT-Tscan.db # collection 1
# define the variables (on this level the parsing will be done)
# B=10mT
var quatsch
var SigmaSC_10 = pow(abs(pow($Sigma,2.0)-pow(0.11,2.0)), 0.5) # 0.11 (1/us) is the nuclear contribution (T>Tc)
var SigmaSC_10Err = pow(pow($Sigma*$SigmaErr,2.0)+pow(0.11*0.0025,2.0), 0.5)/$SigmaSC_10
# B=150mT
var SigmaSC_150 = pow(abs(pow($Sigma,2.0)-pow(0.075,2.0)), 0.5) # 0.075 (1/us) is the nuclear contribution (T>Tc)
var SigmaSC_150Err = pow(pow($Sigma*$SigmaErr,2.0)+pow(0.075*0.0025,2.0), 0.5)/$SigmaSC_150
# link variables to collection (on this level the semantic checks will be done)
col 0 : SigmaSC_10 # error variable SigmaSC_10Err doesn't need to be given, it is automatically linked to SigmaSC_10
col 1 : SigmaSC_150
norm
select 0
x dataT
y SigmaSC_10 # (on this level the evaluation will be done)
y SigmaSC_10
select 1
x dataT

View File

@ -36,24 +36,32 @@
#include <QString>
#include "Pmupp.h"
#include <PAst.hpp>
#include "PAst.hpp"
#include "PProgram.hpp"
class PVarHandler {
public:
PVarHandler(PmuppCollection *coll, std::string parse_str);
PVarHandler(PmuppCollection *coll, std::string parse_str, std::string var_name);
bool isValid() { return fIsValid; }
QString getVarName() { return QString(fVarName.c_str()); }
std::vector<double> getValues();
std::vector<double> getErrors();
private:
PmuppCollection *fColl; ///< collection need for parsing and evaluation
std::string fParseStr; ///< the variable input to be parsed
std::string fVarName; ///< variable name
mupp::prog::PVarHandler fVar; ///< values of the evaluation
bool fIsValid;
mupp::ast::statement_list fAst; ///< the AST
void injectPredefVariables();
std::string getVarName(int idx);
std::vector<double> getData(int idx);
std::vector<double> getDataErr(int idx);
};
#endif //_PVARHANDLER_H_

View File

@ -1,4 +1,7 @@
target_sources(mupp
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/PVarHandler.cpp
${CMAKE_CURRENT_LIST_DIR}/PExpression.cpp
${CMAKE_CURRENT_LIST_DIR}/PProgram.cpp
${CMAKE_CURRENT_LIST_DIR}/PStatement.cpp
)

View File

@ -31,22 +31,54 @@
#include "PVarHandler.h"
/*
#include "PSkipper.hpp"
#include "PErrorHandler.hpp"
#include "PStatement.hpp"
#include "PAstDump.hpp"
#include "PProgram.hpp"
*/
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::PVarHandler
*/
PVarHandler::PVarHandler(PmuppCollection *coll, std::string parse_str) :
fColl(coll), fParseStr(parse_str), fIsValid(false)
PVarHandler::PVarHandler(PmuppCollection *coll, std::string parse_str, std::string var_name) :
fColl(coll), fParseStr(parse_str), fVarName(var_name), fIsValid(false)
{
injectPredefVariables();
typedef std::string::const_iterator iterator_type;
iterator_type iter = fParseStr.begin();
iterator_type end = fParseStr.end();
mupp::PErrorHandler<iterator_type> error_handler(iter, end); // the error handler
mupp::parser::PStatement<iterator_type> parser(error_handler); // the parser
mupp::prog::PProgram prog(error_handler); // our compiler, and exec
mupp::parser::PSkipper<iterator_type> skipper; // the skipper parser
// perform the parsing
bool success = phrase_parse(iter, end, parser, skipper, fAst);
if (success && iter == end) {
if (prog(fAst)) { // semantic analysis
std::vector<double> data, dataErr;
for (unsigned int i=0; i<fColl->GetRun(0).GetNoOfParam(); i++) {
data = getData(i);
dataErr = getDataErr(i);
prog.add_predef_var_values(getVarName(i), data, dataErr);
}
mupp::prog::PProgEval eval(prog.getVars()); // setup evaluation stage
eval(fAst); // evaluate stuff
// keep data
bool ok;
fVar = eval.getVar(fVarName, ok);
if (!ok) {
std::cerr << "**ERROR** evalution failed..." << std::endl;
fIsValid = false;
}
}
fIsValid = true;
} else {
std::cerr << "**ERROR** parsing failed..." << std::endl;
}
}
//--------------------------------------------------------------------------
@ -56,9 +88,11 @@ PVarHandler::PVarHandler(PmuppCollection *coll, std::string parse_str) :
*/
std::vector<double> PVarHandler::getValues()
{
std::vector<double> result;
std::vector<double> data;
if (fIsValid)
data = fVar.GetValue();
return result;
return data;
}
//--------------------------------------------------------------------------
@ -68,9 +102,11 @@ std::vector<double> PVarHandler::getValues()
*/
std::vector<double> PVarHandler::getErrors()
{
std::vector<double> result;
std::vector<double> data;
if (fIsValid)
data = fVar.GetError();
return result;
return data;
}
//--------------------------------------------------------------------------
@ -83,8 +119,9 @@ void PVarHandler::injectPredefVariables()
mupp::ast::variable_declaration var;
std::string varName, errVarName;
for (int i=0; i<fColl->GetNoOfRuns(); i++) {
varName = fColl->GetRun(i).GetName().toLatin1().data();
PmuppRun run = fColl->GetRun(0);
for (int i=0; i<run.GetNoOfParam(); i++) {
varName = run.GetParam(i).GetName().toLatin1().data();
errVarName = varName + "Err";
// inject err_name
var.lhs.name = errVarName;
@ -96,3 +133,68 @@ void PVarHandler::injectPredefVariables()
fAst.push_front(var_stat);
}
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getVarName
* @param idx
* @return
*/
std::string PVarHandler::getVarName(int idx)
{
std::string name("??");
// make sure idx is in range
if (idx >= fColl->GetRun(0).GetNoOfParam())
return name;
return fColl->GetRun(0).GetParam(idx).GetName().toLatin1().data();
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getData
* @param idx
* @return
*/
std::vector<double> PVarHandler::getData(int idx)
{
std::vector<double> data;
// make sure idx is in range
if (idx >= fColl->GetRun(0).GetNoOfParam())
return data;
double dval;
for (int i=0; i<fColl->GetNoOfRuns(); i++) {
dval = fColl->GetRun(i).GetParam(idx).GetValue();
data.push_back(dval);
}
return data;
}
//--------------------------------------------------------------------------
/**
* @brief PVarHandler::getDataErr
* @param idx
* @return
*/
std::vector<double> PVarHandler::getDataErr(int idx)
{
std::vector<double> err;
// make sure idx is in range
if (idx >= fColl->GetRun(0).GetNoOfParam())
return err;
double dvalPos, dvalNeg;
for (int i=0; i<fColl->GetNoOfRuns(); i++) {
dvalPos = fColl->GetRun(i).GetParam(idx).GetPosErr();
dvalNeg = fColl->GetRun(i).GetParam(idx).GetNegErr();
dvalPos = sqrt(fabs(dvalPos*dvalNeg)); // geometric mean of pos/neg error
err.push_back(dvalPos);
}
return err;
}