示例#1
0
int cLibsvmLiveSink::myFinaliseInstance()
{
  int ap=0;

  int ret = cDataSink::myFinaliseInstance();
  if (ret==0) return 0;
  
  // TODO: binary model files...
  // load model
  SMILE_MSG(2,"loading LibSVM model for instance '%s' ...",getInstName()); 
  if((model=svm_load_model(modelfile))==0) {
    COMP_ERR("can't open libSVM model file '%s'",modelfile);
  }

  nClasses = svm_get_nr_class(model);
  svmType = svm_get_svm_type(model);

  if(predictProbability) {
    if ((svmType==NU_SVR) || (svmType==EPSILON_SVR)) {
      nClasses = 0;
      SMILE_MSG(2,"LibSVM prob. model (regression) for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g",svm_get_svr_probability(model));
    } else {
      labels=(int *) malloc(nClasses*sizeof(int));
      svm_get_labels(model,labels);

      SMILE_MSG(3,"LibSVM %i labels in model '%s':",nClasses,modelfile);
      int j;
      for(j=0;j<nClasses;j++)
        SMILE_MSG(3,"  Label[%i] : '%d'",j,labels[j]);
	}
  }

  //?? move this in front of above if() block ?
  if ((predictProbability)&&(nClasses>0))
    probEstimates = (double *) malloc(nClasses*sizeof(double));

  // load scale
  if((scale=svm_load_scale(scalefile))==0) {
	COMP_ERR("can't open libSVM scale file '%s'",scalefile);
  }

  // load selection
  loadSelection(fselection);

  //TODO: check compatibility of getLevelN() (possibly after selection), number of features in model, and scale
  
  if (nClasses>0) {
    // load class mapping
    loadClasses(classes);
  } else {
    if (classes != NULL) SMILE_IWRN(2,"not loading given class mapping file for regression SVR model (there are no classes...)!");
  }

  return ret;
}
std::shared_ptr<DeclarationNode> TemplateFunctionDeclarationNode::instantiateWithArguments(TemplateArguments arguments) 
{
    auto template_info = TemplateInfo(defined_symbol, arguments);
    RebuildTreeVisitor rebuild(template_info);

    statements -> accept(rebuild);    
    auto stat = rebuild.result();

    auto new_return_type_info = rebuild.processTypeInfo(info_.returnTypeInfo());
    auto new_formal_params = std::vector<ParamInfo>{ };

    for ( auto param : info_.formalParams() )
        new_formal_params.emplace_back(param.name(), rebuild.processTypeInfo(param.typeInfo()));
    auto new_info = FunctionDeclarationInfo(new_return_type_info, new_formal_params);

    auto decl = std::make_shared<FunctionDeclarationNode>(template_info.getInstName(), new_info, std::move(stat), traits_, is_unsafe);

	decl -> scope = scope;
    decl -> build_scope();

    return decl;
}
int cFunctionalsVecToVec::myConfigureInstance()
{
  int i,j;

  cComponentManager *_compman = getCompMan();
  if (_compman != NULL) {
    int nTp = _compman->getNtypes();
    nFunctTp = 0;
    for (i=0; i<nTp; i++) {
      const char * tp = _compman->getComponentType(i,1);
      if (tp!=NULL) {
        if (!strncmp(tp,"cFunctional",11)&&strcmp(tp,COMPONENT_NAME_CFUNCTIONALSVECTOVEC)) {
           // find beginning "cFunctional" but not our own type (cFunctinals)
          const char *fn = tp+11;
          if (nFunctTpAlloc == nFunctTp) { // realloc:
            functTp = (char **)crealloc( functTp, sizeof(char*)*(nFunctTpAlloc+N_BLOCK_ALLOC), nFunctTpAlloc );
            functTpI = (int *)crealloc( functTpI, sizeof(int)*(nFunctTpAlloc+N_BLOCK_ALLOC), nFunctTpAlloc );
            functI = (int *)crealloc( functI, sizeof(int)*(nFunctTpAlloc+N_BLOCK_ALLOC), nFunctTpAlloc );
            functN = (int *)crealloc( functN, sizeof(int)*(nFunctTpAlloc+N_BLOCK_ALLOC), nFunctTpAlloc );
            functObj = (cFunctionalComponent **)crealloc( functObj, sizeof(cFunctionalComponent *)*(nFunctTpAlloc+N_BLOCK_ALLOC), nFunctTpAlloc );
            nFunctTpAlloc += N_BLOCK_ALLOC;
          }
          functTp[nFunctTp] = strdup(fn);
          functTpI[nFunctTp] = i;
          nFunctTp++;
        }
      }
    }
  }
  SMILE_DBG(2,"(inst '%s') found %i cFunctionalXXXX component types.",getInstName(),nFunctTp);

  // fetch enabled functionals list
  nFunctionalsEnabled = getArraySize("functionalsEnabled");
  nFunctValues = 0;
  requireSorted = 0;
  for (i=0; i<nFunctionalsEnabled; i++) {
    const char *fname = getStr_f(myvprint("functionalsEnabled[%i]",i));
    char *tpname = myvprint("cFunctional%s",fname);
    for (j=0; j<nFunctTp; j++) {
      if (!strcmp(functTp[j],fname)) {
        functI[i] = j;
        break;
      }
    }
// TODO: find duplicates in functionalsEnabled Array!!!

    if (j<nFunctTp) {
      // and create corresponding component instances...
        SMILE_DBG(3,"(inst '%s') creating Functional object 'cFunctional%s'.",fname);
        char *_tmp = myvprint("%s.%s",getInstName(),fname);
        cFunctionalComponent * tmp = (cFunctionalComponent *)(_compman->createComponent)(_tmp,tpname);
        free(_tmp);
        if (tmp==NULL) OUT_OF_MEMORY;
        tmp->setComponentEnvironment(_compman, -1, this);
        functN[i] = tmp->getNoutputValues();
        requireSorted += tmp->getRequireSorted();
        nFunctValues += functN[i];
        functObj[i] = tmp;
        //functTp[i]  = strdup(fname);
    } else {
      SMILE_ERR(1,"(inst '%s') Functional object '%s' specified in 'functionalsEnabled' array, however no type 'cFunctional%s' exists!",getInstName(),fname,fname);
      functObj[i] = NULL;
      functN[i] = 0;
      free(tpname);
      return 0;
      //functTp[i]  = NULL;
    }
    free(tpname);
  }
  if (requireSorted)
    SMILE_DBG(2,"%i Functional components require sorted data.",requireSorted);

  return cVectorProcessor::myConfigureInstance();

}
示例#4
0
void cLibsvmSink::fetchConfig()
{
  cDataSink::fetchConfig();
  
  filename = getStr("filename");
  SMILE_DBG(3,"filename = '%s'",filename);

  lag = getInt("lag");
  SMILE_DBG(3,"lag = %i",lag);

  append = getInt("append");
  if (append) { SMILE_DBG(3,"append to file is enabled"); }

  timestamp = getInt("timestamp");
  if (append) { SMILE_DBG(3,"printing timestamp attribute (index 1) enabled"); }

  instanceBase = getStr("instanceBase");
  SMILE_DBG(3,"instanceBase = '%s'",instanceBase);

  instanceName = getStr("instanceName");
  SMILE_DBG(3,"instanceName = '%s'",instanceName);
  
  int i;
  nClasses = getArraySize("class");
  classname = (char**)calloc(1,sizeof(char*)*nClasses);
  for (i=0; i<nClasses; i++) {
    const char *tmp = getStr_f(myvprint("class[%i]",i));
    if (tmp!=NULL) classname[i] = strdup(tmp);
  }

  if (isSet("targetNumAll")) {
    targetNumAll = getInt("targetNumAll");
  }
  if (isSet("targetStrAll")) {
    if (nClasses <=0) COMP_ERR("cannt have 'targetStrAll' option if no class names have been defined using the 'class' option! (inst '%s')",getInstName());
    targetNumAll = getClassIndex(getStr("targetStrAll"));
  }
  nInst = getArraySize("targetNum");
  if (nInst > 0) {
    target = (int *)calloc(1,sizeof(int)*nInst);
    for (i=0; i<nInst; i++) {
      target[i] = getInt_f(myvprint("targetNum[%i]",i));
      if (target[i] < 0) COMP_ERR("invalid class index %i for instance %i (in 'targetNum' option of instance '%s')",target[i],i,getInstName());
    }
  } else {
    nInst = getArraySize("targetStr");
    if (nInst > 0) {
      if (nClasses <=0) COMP_ERR("cannt have 'targetStr' option if no class names have been defined using the 'class' option! (inst '%s')",getInstName());
      target = (int *)calloc(1,sizeof(int)*nInst);
      for (i=0; i<nInst; i++) {
        target[i] = getClassIndex(getStr_f(myvprint("targetStr[%i]",i)));
        if (target[i] < 0) COMP_ERR("invalid class index %i for instance %i (from class '%s' in 'targetStr' option of instance '%s')",target[i],i,getStr_f(myvprint("targetStr[%i]",i)),getInstName());
      }
    } else { nInst = 0; }
  }

}
示例#5
0
int cLibsvmSink::myFinaliseInstance()
{
  int ap=0;

  int ret = cDataSink::myFinaliseInstance();
  if (ret==0) return 0;
  
  if (append) {
    // check if file exists:
    filehandle = fopen(filename, "r");
    if (filehandle != NULL) {
      fclose(filehandle);
      filehandle = fopen(filename, "a");
      ap=1;
    } else {
      filehandle = fopen(filename, "w");
    }
  } else {
    filehandle = fopen(filename, "w");
  }
  if (filehandle == NULL) {
    COMP_ERR("Error opening file '%s' for writing (component instance '%s', type '%s')",filename, getInstName(), getTypeName());
  }
  
//  if (!ap) {
  // write header ....
//  if (timestamp) {
//    fprintf(filehandle, "@attribute frameTime numeric%s",NEWLINE);
//  }
/*
  long _N = reader->getLevelN();
  long i;
  for(i=0; i<_N; i++) {
    char *tmp = reader->getElementName(i);
    fprintf(filehandle, "@attribute %s numeric%s",tmp,NEWLINE);
    free(tmp);
  }
*/
//  }
  
  return ret;
}
示例#6
0
void cLibsvmLiveSink::processResult(long long tick, long frameIdx, double time, float res, double *probEstim, int nClasses, double dur)
{
  //ros::Rate loop_rate(10);

//  int count = 0;
//  while (ros::ok())
//  {
//// %EndTag(ROS_OK)%
//    /**
//     * This is a message object. You stuff it with data, and then publish it.
//     */
//// %Tag(FILL_MESSAGE)%
//    std_msgs::String msg;

//    std::stringstream ss;
//    ss << "hello world " << count;
//    msg.data = ss.str();
//// %EndTag(FILL_MESSAGE)%

//// %Tag(ROSCONSOLE)%
//    ROS_INFO("%s", msg.data.c_str());
//// %EndTag(ROSCONSOLE)%

//    /**
//     * The publish() function is how you send messages. The parameter
//     * is the message object. The type of this object must agree with the type
//     * given as a template parameter to the advertise<>() call, as was done
//     * in the constructor above.
//     */
//// %Tag(PUBLISH)%
//    chatter_pub.publish(msg);
//// %EndTag(PUBLISH)%

//// %Tag(SPINONCE)%
//    ros::spinOnce();
//// %EndTag(SPINONCE)%

//// %Tag(RATE_SLEEP)%
//    loop_rate.sleep();
//// %EndTag(RATE_SLEEP)%
//    ++count;
//  }

  std_msgs::String emoMsg;
  std_msgs::String affectMsg;

  if (printResult) {
    if ((nCls>0)&&(nClasses > 0)&&(classNames != NULL)) {
      if (labels!=NULL) {
        if ((int)res >= nClasses) res = (float)(nClasses-1);
        if (res < 0.0) res = 0.0;
        res = (float)labels[(int)res];
      }
      if ((int)res >= nCls) res = (float)nCls;
      if (res < 0.0) res = 0.0;
      SMILE_PRINT("\n LibSAVM  '%s' result (@ time: %f) :  ~~> %s <~~",getInstName(),time,classNames[(int)res]);
      std::string instName = getInstName();
      std::string emoName = "emodbEmotion";
      std::string affectName = "abcAffect";
      if (instName == emoName)
      {
          emoMsg.data = classNames[(int)res];
          emo_pub.publish(emoMsg);
      }
      if (instName == affectName)
      {
          affectMsg.data = classNames[(int)res];
          affect_pub.publish(affectMsg);
      }
    } else {
      SMILE_PRINT("\n LibSBVM  '%s' result (@ time: %f) :  ~~> %.2f <~~",getInstName(),time,res);
    }
    if (probEstim != NULL) {
      int i;
      for (i=0; i<nClasses; i++) {
        int idx = i;
        if (labels!=NULL) idx = labels[i];
        if ((nCls>0)&&(nClasses > 0)&&(classNames != NULL)) {
          if (idx >= nCls) idx=nCls-1;
          if (idx < 0) idx = 0;
          SMILE_PRINT("     prob. class '%s': \t %f",classNames[idx],probEstim[i]);
        } else {
          SMILE_PRINT("     prob. class %i : \t %f",idx,probEstim[i]);
        }
      }
    }
  }


  // send result as componentMessage 
  if (sendResult) {
    cComponentMessage msg("classificationResult", resultMessageName);
    if ((nCls>0)&&(nClasses > 0)&&(classNames != NULL)) {
      if (labels!=NULL) {
        if ((int)res >= nClasses) res = (float)(nClasses-1);
        if (res < 0.0) res = 0.0;
        res = (float)labels[(int)res];
      }
      if ((int)res >= nCls) res = (float)nCls;
      if (res < 0.0) res = 0.0;
      strncpy(msg.msgtext, classNames[(int)res], CMSG_textLen);
    }
    msg.floatData[0] = res;
    msg.intData[0]   = nClasses;
    msg.custData     = probEstim;
    msg.userTime1    = time;
    msg.userTime2    = time+dur;

    // TO TEST .....
    sendComponentMessage( resultRecp, &msg );
    SMILE_IDBG(3,"sending 'classificationResult' message to '%s'",resultRecp);
  }
}
示例#7
0
int cCsvSink::myFinaliseInstance()
{
  int ap=0;

  int ret = cDataSink::myFinaliseInstance();
  if (ret==0) return 0;
  
  if (append) {
    // check if file exists:
    filehandle = fopen(filename, "r");
    if (filehandle != NULL) {
      fclose(filehandle);
      filehandle = fopen(filename, "a");
      ap=1;
    } else {
      filehandle = fopen(filename, "w");
    }
  } else {
    filehandle = fopen(filename, "w");
  }
  if (filehandle == NULL) {
    COMP_ERR("Error opening file '%s' for writing (component instance '%s', type '%s')",filename, getInstName(), getTypeName());
  }
  
  if ((!ap)&&(printHeader)) {
    // write header ....
    if (number) {
      fprintf(filehandle, "frameIndex%c",delimChar);
    }
    if (timestamp) {
      fprintf(filehandle, "frameTime%c",delimChar);
    }

    long _N = reader_->getLevelN();
    long i;
    for(i=0; i<_N-1; i++) {
      char *tmp = reader_->getElementName(i);
      fprintf(filehandle, "%s%c",tmp,delimChar);
      free(tmp);
    }
    char *tmp = reader_->getElementName(i);
    fprintf(filehandle, "%s%s",tmp,NEWLINE);
    free(tmp);
  }
  
  return ret;
}
示例#8
0
int cArffSink::myFinaliseInstance()
{
  int ap=0;
  if (disabledSink_) {
    filehandle = NULL;
    return 1;
  }
  int ret = cDataSink::myFinaliseInstance();
  if (ret==0) return 0;

  if (append) {
    // check if file exists:
    filehandle = fopen(filename, "r");
    if (filehandle != NULL) {
      fclose(filehandle);
      filehandle = fopen(filename, "a");
      ap=1;
    } else {
      filehandle = fopen(filename, "w");
    }
  } else {
    filehandle = fopen(filename, "w");
  }
  if (filehandle == NULL) {
    COMP_ERR("Error opening file '%s' for writing (component instance '%s', type '%s')",filename, getInstName(), getTypeName());
  }

  if (instanceNameFromMetadata) {
    prname = 1;
  } else {
    if ((instanceBase!=NULL)&&(strlen(instanceBase)>0)&&
        (instanceBase[0] != '-' || strlen(instanceBase) > 1)
       ) prname = 2;
    if ((instanceName!=NULL)&&(strlen(instanceName)>0)&&
        (instanceName[0] != '-' || strlen(instanceName) > 1)
       ) prname = 1;
  }
  long _N = reader_->getLevelN();

  if (!ap) {
    // write arff header ....
    fprintf(filehandle, "@relation %s%s%s",relation,NEWLINE,NEWLINE);
    if (prname) {
      fprintf(filehandle, "@attribute name string%s",NEWLINE);
    }
    if (number) {
      fprintf(filehandle, "@attribute frameIndex numeric%s",NEWLINE);
    }
    if (timestamp) {
      fprintf(filehandle, "@attribute frameTime numeric%s",NEWLINE);
    }
    if (frameLength) {
      fprintf(filehandle, "@attribute frameLength numeric%s",NEWLINE);
    }

    long i;
    if (_N > 10000) {
      SMILE_IMSG(2,"writing ARFF header (%i features), this may take a while (it is a lot of data and heavy weight string formatting) ...",_N);
    } else {
      SMILE_IMSG(2,"writing ARFF header (%i features)...",_N);
    }
    for(i=0; i<_N; i++) {
      char *tmp = reader_->getElementName(i);
      fprintf(filehandle, "@attribute %s numeric%s",tmp,NEWLINE);
      free(tmp);
      if ((i>0)&&(i%20000==0)) {
        SMILE_IMSG(2,"Status: %i feature names written.",i);
      }
    }
    if (_N > 6000) {
      SMILE_IMSG(2,"finished writing ARFF header.");
    }

    // TODO: classes..... as config file options...
    if (nClasses > 0) {
      for (i=0; i<nClasses; i++) {
        if (classtype[i] == NULL) fprintf(filehandle, "@attribute %s numeric%s",classname[i],NEWLINE);
        else fprintf(filehandle, "@attribute %s %s%s",classname[i],classtype[i],NEWLINE);
      }
    } else {
      // default dummy class attribute...
      if (printDefaultClassDummyAttribute) {
        fprintf(filehandle, "@attribute class {0,1,2,3}%s",NEWLINE);
      }
    }
    fprintf(filehandle, "%s@data%s%s",NEWLINE,NEWLINE,NEWLINE);
    fflush(filehandle);
  }
  return ret;
}
long cFunctionalComponent::process(INT_DMEM *in, INT_DMEM *inSorted, INT_DMEM *out, long Nin, long Nout)
{
  SMILE_ERR(1,"dataType INT_DMEM not yet supported in component '%s' of type '%s'",getTypeName(), getInstName());
  return 0;
}