<commit_before>/*
 * InitV.cpp
 *
 *  Created on: Dec 6, 2011
 *      Author: pschultz
 */

#include "InitV.hpp"

namespace PV {
InitV::InitV() {
   initialize_base();
   // Default constructor for derived classes.  A derived class should call
   // InitV::initialize from its own initialize routine, A derived class's
   // constructor should not call a non-default InitV constructor.
}

InitV::InitV(HyPerCol * hc, const char * groupName) {
   initialize_base();
   initialize(hc, groupName);
}

int InitV::initialize_base() {
   groupName = NULL;
   initVTypeCode = UndefinedInitV;
   filename = NULL;
   return PV_SUCCESS;
}

int InitV::initialize(HyPerCol * hc, const char * groupName) {
   int status = PV_SUCCESS;
   this->groupName = strdup(groupName);
   PVParams * params = hc->parameters();
   useStderr = hc->icCommunicator()->commRank()==0;
   const char * initVType = params->stringValue(groupName, "InitVType", true);
   if( initVType == NULL ) {
      initVTypeCode = ConstantV;
      constantValue = params->value(groupName, "valueV", V_REST);
      printerr("Using InitVType = \"ConstantV\" with valueV = %f\n", groupName, constantValue);
   }
   else if( !strcmp(initVType, "ConstantV") ) {
      initVTypeCode = ConstantV;
      constantValue = params->value(groupName, "valueV", V_REST);
   }
   else if( !strcmp(initVType, "ZeroV")) {
      initVTypeCode = ConstantV;
      constantValue = 0.0f;
   }
   else if( !strcmp(initVType, "UniformRandomV") ) {
      minV = params->value(groupName, "minV", 0.0f);
      maxV = params->value(groupName, "maxV", minV+1.0f);
      initVTypeCode = UniformRandomV;
   }
   else if( !strcmp(initVType, "GaussianRandomV") ) {
      meanV = params->value(groupName, "meanV", 0.0f);
      sigmaV = params->value(groupName, "sigmaV", 1.0f);
      initVTypeCode = GaussianRandomV;
   }
   else if( !strcmp(initVType, "InitVFromFile") ) {
      filename = params->stringValue(groupName, "Vfilename", true);
      if( filename == NULL ) {
         initVTypeCode = UndefinedInitV;
         printerr("InitV::initialize, group \"%s\": for InitVFromFile, string parameter \"Vfilename\" must be defined.  Exiting\n", groupName);
         abort();
      }
      initVTypeCode = InitVFromFile;
   }
   else {
      initVTypeCode = UndefinedInitV;
      printerr("InitV::initialize, group \"%s\": InitVType \"%s\" not recognized.\n", groupName, initVType);
      abort();
   }
   return status;
}

InitV::~InitV() {}

int InitV::calcV(HyPerLayer * layer) {
   int status = PV_SUCCESS;
   switch(initVTypeCode) {
   case UndefinedInitV:
      status = PV_FAILURE;
      printerr("InitV::calcV: InitVType was undefined.\n");
      break;
   case ConstantV:
      status = calcConstantV(layer->getV(), layer->getNumNeurons());
      break;
   case UniformRandomV:
      status = calcUniformRandomV(layer->getV(), layer->getNumNeurons());
      break;
   case GaussianRandomV:
      status = calcGaussianRandomV(layer->getV(), layer->getNumNeurons());
      break;
   case InitVFromFile:
      status = calcVFromFile(layer->getCLayer(), layer->getParent()->icCommunicator());
      break;
   default:
      status = PV_FAILURE;
      printerr("InitV::calcV: InitVType was an unrecognized type.\n");
      break;
   }
   return status;
}

int InitV::calcConstantV(pvdata_t * V, int numNeurons) {
   for( int k=0; k<numNeurons; k++ ) V[k] = constantValue;
   return PV_SUCCESS;
}

int InitV::calcGaussianRandomV(pvdata_t * V, int numNeurons) {
   for( int k=0; k<numNeurons; k++ ) V[k] = generateGaussianRand();
   return PV_SUCCESS;
}

#define GENERATEGAUSSIANRAND_TWOPI (6.283185307179586)
pvdata_t InitV::generateGaussianRand() {
   pvdata_t V;
   if( valueIsBeingHeld) {
      V = heldValue;
      valueIsBeingHeld = false;
   }
   else {
      double U1, U2;
      U1 = pv_random_prob();
      U2 = pv_random_prob();
      double t = GENERATEGAUSSIANRAND_TWOPI * U2;
      double r = sigmaV*sqrt(-2*log(U1)); // U1<1 so r is real
      V = r*cos(t) + meanV;
      heldValue = r*sin(t) + meanV;
      valueIsBeingHeld = true;
   }
   return V;

}

int InitV::calcUniformRandomV(pvdata_t * V, int numNeurons) {
   for( int k=0; k<numNeurons; k++ ) V[k] = generateUnifRand();
   return PV_SUCCESS;
}

pvdata_t InitV::generateUnifRand() {
   pvdata_t V = ( (pvdata_t) (pv_random()*uniformMultiplier) ) + minV;
   return V;
}

int InitV::calcVFromFile(PVLayer * clayer, InterColComm * icComm) {
   int status = PV_SUCCESS;
   const PVLayerLoc * loc = &(clayer->loc);
   pvdata_t * V = clayer->V;
   PVLayerLoc fileLoc;
   int filetype = getFileType(filename);
   if( filetype == PVP_FILE_TYPE) {
      int params[NUM_BIN_PARAMS];
      double timed;
      int pvpfiletype, datatype;
      int numParams = NUM_BIN_PARAMS;
      status = pvp_read_header(this->filename, icComm, &timed,
                          &pvpfiletype, &datatype, params, &numParams);
      assert(status == PV_SUCCESS);
      assert(checkLoc(loc, params[INDEX_NX], params[INDEX_NY], params[INDEX_NF], params[INDEX_NX_GLOBAL], params[INDEX_NY_GLOBAL])==PV_SUCCESS);
      switch(pvpfiletype) {
      case PVP_FILE_TYPE:
         status = read_pvdata(this->filename, icComm, &timed, V,
                              &fileLoc, PV_FLOAT_TYPE, false, false);
         break;
      case PVP_ACT_FILE_TYPE:
         printerr("calcVFromFile for file \"%s\": sparse activity files are not yet implemented.\n", this->filename);
         abort();
         break;
      case PVP_NONSPIKING_ACT_FILE_TYPE:
         status = readNonspikingActFile(this->filename, icComm, &timed, V, params[INDEX_NBANDS]-1, &clayer->loc, datatype, false, false);
         break;
      default:
         printerr("calcVFromFile: file \"%s\" is not an activity pvp file.\n", this->filename);
         abort();
         break;
      }

   }
   else { // Treat as an image file
      status = getImageInfo(this->filename, icComm, &fileLoc);
      assert(status == PV_SUCCESS);
      assert(checkLoc(loc, fileLoc.nx, fileLoc.ny, fileLoc.nf, fileLoc.nxGlobal, fileLoc.nyGlobal)==PV_SUCCESS);
      int n=clayer->numNeurons;
      unsigned char * buf = new unsigned char[n];
      status = scatterImageFileGDAL(this->filename, 0, 0, icComm, &fileLoc, buf);
      pvdata_t scale = 0.003921569; // 1.0/255.0  This needs to change if pvdata_t stops being float
      for( int k=0; k<n; k++ ) {
         V[k] = (pvdata_t) buf[k]*scale;
      }
      delete buf;
   }
   return status;<commit_msg>Bugfix for InitVFromFile with a .pvp file<commit_after>/*
 * InitV.cpp
 *
 *  Created on: Dec 6, 2011
 *      Author: pschultz
 */

#include "InitV.hpp"

namespace PV {
InitV::InitV() {
   initialize_base();
   // Default constructor for derived classes.  A derived class should call
   // InitV::initialize from its own initialize routine, A derived class's
   // constructor should not call a non-default InitV constructor.
}

InitV::InitV(HyPerCol * hc, const char * groupName) {
   initialize_base();
   initialize(hc, groupName);
}

int InitV::initialize_base() {
   groupName = NULL;
   initVTypeCode = UndefinedInitV;
   filename = NULL;
   return PV_SUCCESS;
}

int InitV::initialize(HyPerCol * hc, const char * groupName) {
   int status = PV_SUCCESS;
   this->groupName = strdup(groupName);
   PVParams * params = hc->parameters();
   useStderr = hc->icCommunicator()->commRank()==0;
   const char * initVType = params->stringValue(groupName, "InitVType", true);
   if( initVType == NULL ) {
      initVTypeCode = ConstantV;
      constantValue = params->value(groupName, "valueV", V_REST);
      printerr("Using InitVType = \"ConstantV\" with valueV = %f\n", groupName, constantValue);
   }
   else if( !strcmp(initVType, "ConstantV") ) {
      initVTypeCode = ConstantV;
      constantValue = params->value(groupName, "valueV", V_REST);
   }
   else if( !strcmp(initVType, "ZeroV")) {
      initVTypeCode = ConstantV;
      constantValue = 0.0f;
   }
   else if( !strcmp(initVType, "UniformRandomV") ) {
      minV = params->value(groupName, "minV", 0.0f);
      maxV = params->value(groupName, "maxV", minV+1.0f);
      initVTypeCode = UniformRandomV;
   }
   else if( !strcmp(initVType, "GaussianRandomV") ) {
      meanV = params->value(groupName, "meanV", 0.0f);
      sigmaV = params->value(groupName, "sigmaV", 1.0f);
      initVTypeCode = GaussianRandomV;
   }
   else if( !strcmp(initVType, "InitVFromFile") ) {
      filename = params->stringValue(groupName, "Vfilename", true);
      if( filename == NULL ) {
         initVTypeCode = UndefinedInitV;
         printerr("InitV::initialize, group \"%s\": for InitVFromFile, string parameter \"Vfilename\" must be defined.  Exiting\n", groupName);
         abort();
      }
      initVTypeCode = InitVFromFile;
   }
   else {
      initVTypeCode = UndefinedInitV;
      printerr("InitV::initialize, group \"%s\": InitVType \"%s\" not recognized.\n", groupName, initVType);
      abort();
   }
   return status;
}

InitV::~InitV() {}

int InitV::calcV(HyPerLayer * layer) {
   int status = PV_SUCCESS;
   switch(initVTypeCode) {
   case UndefinedInitV:
      status = PV_FAILURE;
      printerr("InitV::calcV: InitVType was undefined.\n");
      break;
   case ConstantV:
      status = calcConstantV(layer->getV(), layer->getNumNeurons());
      break;
   case UniformRandomV:
      status = calcUniformRandomV(layer->getV(), layer->getNumNeurons());
      break;
   case GaussianRandomV:
      status = calcGaussianRandomV(layer->getV(), layer->getNumNeurons());
      break;
   case InitVFromFile:
      status = calcVFromFile(layer->getCLayer(), layer->getParent()->icCommunicator());
      break;
   default:
      status = PV_FAILURE;
      printerr("InitV::calcV: InitVType was an unrecognized type.\n");
      break;
   }
   return status;
}

int InitV::calcConstantV(pvdata_t * V, int numNeurons) {
   for( int k=0; k<numNeurons; k++ ) V[k] = constantValue;
   return PV_SUCCESS;
}

int InitV::calcGaussianRandomV(pvdata_t * V, int numNeurons) {
   for( int k=0; k<numNeurons; k++ ) V[k] = generateGaussianRand();
   return PV_SUCCESS;
}

#define GENERATEGAUSSIANRAND_TWOPI (6.283185307179586)
pvdata_t InitV::generateGaussianRand() {
   pvdata_t V;
   if( valueIsBeingHeld) {
      V = heldValue;
      valueIsBeingHeld = false;
   }
   else {
      double U1, U2;
      U1 = pv_random_prob();
      U2 = pv_random_prob();
      double t = GENERATEGAUSSIANRAND_TWOPI * U2;
      double r = sigmaV*sqrt(-2*log(U1)); // U1<1 so r is real
      V = r*cos(t) + meanV;
      heldValue = r*sin(t) + meanV;
      valueIsBeingHeld = true;
   }
   return V;

}

int InitV::calcUniformRandomV(pvdata_t * V, int numNeurons) {
   for( int k=0; k<numNeurons; k++ ) V[k] = generateUnifRand();
   return PV_SUCCESS;
}

pvdata_t InitV::generateUnifRand() {
   pvdata_t V = ( (pvdata_t) (pv_random()*uniformMultiplier) ) + minV;
   return V;
}

int InitV::calcVFromFile(PVLayer * clayer, InterColComm * icComm) {
   int status = PV_SUCCESS;
   const PVLayerLoc * loc = &(clayer->loc);
   pvdata_t * V = clayer->V;
   PVLayerLoc fileLoc;
   int filetype = getFileType(filename);
   if( filetype == PVP_FILE_TYPE) {
      int params[NUM_BIN_PARAMS];
      double timed;
      int pvpfiletype, datatype;
      int numParams = NUM_BIN_PARAMS;
      status = pvp_read_header(this->filename, icComm, &timed,
                          &pvpfiletype, &datatype, params, &numParams);
      assert(status == PV_SUCCESS);
      status = checkLoc(loc, params[INDEX_NX], params[INDEX_NY], params[INDEX_NF], params[INDEX_NX_GLOBAL], params[INDEX_NY_GLOBAL]);
      assert(status == PV_SUCCESS);
      fileLoc.nx = params[INDEX_NX];
      fileLoc.ny = params[INDEX_NY];
      fileLoc.nf = params[INDEX_NF];
      fileLoc.nb = params[INDEX_NB];
      fileLoc.nxGlobal = params[INDEX_NX_GLOBAL];
      fileLoc.nyGlobal = params[INDEX_NY_GLOBAL];
      fileLoc.kx0 = icComm->commColumn()*fileLoc.nx;
      fileLoc.ky0 = icComm->commRow()*fileLoc.ny;
      switch(pvpfiletype) {
      case PVP_FILE_TYPE:
         status = read_pvdata(this->filename, icComm, &timed, V,
                              &fileLoc, PV_FLOAT_TYPE, false, false);
         break;
      case PVP_ACT_FILE_TYPE:
         printerr("calcVFromFile for file \"%s\": sparse activity files are not yet implemented.\n", this->filename);
         abort();
         break;
      case PVP_NONSPIKING_ACT_FILE_TYPE:
         status = readNonspikingActFile(this->filename, icComm, &timed, V, params[INDEX_NBANDS]-1, &clayer->loc, datatype, false, false);
         break;
      default:
         printerr("calcVFromFile: file \"%s\" is not an activity pvp file.\n", this->filename);
         abort();
         break;
      }

   }
   else { // Treat as an image file
      status = getImageInfoGDAL(this->filename, icComm, &fileLoc);
      assert(status == PV_SUCCESS);
      assert(checkLoc(loc, fileLoc.nx, fileLoc.ny, fileLoc.nf, fileLoc.nxGlobal, fileLoc.nyGlobal)==PV_SUCCESS);
      int n=clayer->numNeurons;
      unsigned char * buf = new unsigned char[n];
      status = scatterImageFileGDAL(this->filename, 0, 0, icComm, &fileLoc, buf);
      pvdata_t scale = 0.003921569; // 1.0/255.0  This needs to change if pvdata_t stops being float
      for( int k=0; k<n; k++ ) {
         V[k] = (pvdata_t) buf[k]*scale;
      }
      delete buf;
   }
   return status;
{'fraction_non_alphanumeric': 0.13266836658167092, 'fraction_numerical': 0.00974951252437378, 'mean_word_length': 3.1477449455676516, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4399874', 'n_tokens_mistral': 4727, 'n_tokens_neox': 4595, 'n_words': 1020}