<commit_before>/*
 * Image.cpp
 *
 *  Created on: Sep 8, 2009
 *      Author: rasmussn
 */

#include "Image.hpp"

#ifdef PV_USE_MPI
#include <mpi.h>
#endif
#include <assert.h>
#include <string.h>

namespace PV {

Image::Image() {
   initialize_base();
}

Image::Image(const char * name, HyPerCol * hc, const char * filename) {
   initialize_base();
   initialize(name, hc, filename);
}

Image::~Image() {
   free(filename);
   filename = NULL;
   Communicator::freeDatatypes(mpi_datatypes); mpi_datatypes = NULL;

   if(writePosition){
      if (getParent()->icCommunicator()->commRank()==0 && fp_pos != NULL && fp_pos->isfile) {
         PV_fclose(fp_pos);
      }
   }
}

int Image::initialize_base() {
   mpi_datatypes = NULL;
   data = NULL;
   filename = NULL;
   imageData = NULL;
   useImageBCflag = false;
   autoResizeFlag = false;
   writeImages = false;
   writeImagesExtension = NULL;
   inverseFlag = false;
   normalizeLuminanceFlag = false;
   offsets[0] = 0;
   offsets[1] = 0;
   jitterFlag = false;
   jitterType = RANDOM_WALK;
   timeSinceLastJitter = 0;
   jitterRefractoryPeriod = 0;
   stepSize = 0;
   persistenceProb = 0.0;
   recurrenceProb = 1.0;
   biasChangeTime = LONG_MAX;
   writePosition = 0;
   fp_pos = NULL;
   biases[0]   = getOffsetX();
   biases[1]   = getOffsetY();
   frameNumber = 0;
   return PV_SUCCESS;
}

int Image::initialize(const char * name, HyPerCol * hc, const char * filename) {
   if (filename != NULL) {
      this->filename = strdup(filename);
      assert( this->filename != NULL);
   }
   else {
      this->filename = NULL;
   }

   HyPerLayer::initialize(name, hc, 0);
   // Much of the functionality that was previously here has been moved to either read-methods, communicateInitInfo, or allocateDataStructures

   int status = PV_SUCCESS;

   this->lastUpdateTime = 0.0;

   PVParams * params = parent->parameters();

   assert(!params->presentAndNotBeenRead(name, "jitterFlag"));
   if (jitterFlag) {
      assert(!params->presentAndNotBeenRead(name, "offsetX"));
      assert(!params->presentAndNotBeenRead(name, "offsetY"));
      biases[0] = getOffsetX();
      biases[1] = getOffsetY();
   }

   // TODO - Add option to use image size to determine layer size and column size

   return status;
}

int Image::setParams(PVParams * params) {
   int status = HyPerLayer::setParams(params);

   readOffsets(params);
   readWriteImagesFlag(params);

   readUseImageBCflag(params);
   readAutoResizeFlag(params);
   readInverseFlag(params);
   readNormalizeLuminanceFlag(params);

   readFrameNumber(params);

   // Although Image itself does not use jitter, both Movie and Patterns do, so jitterFlag is read in Image.
   readJitterFlag(params);
   readJitterType(params);
   readJitterRefractoryPeriod(params);
   readStepSize(params);
   readPersistenceProb(params);
   readRecurrenceProb(params);
   readBiasChangeTime(params);
   readBiasConstraintMethod(params);
   readOffsetConstraintMethod(params);

   return status;
}

int Image::readOffsets(PVParams * params) {

   offsets[0]      = (int) params->value(name,"offsetX", offsets[0]);
   offsets[1]      = (int) params->value(name,"offsetY", offsets[1]);

   return PV_SUCCESS;
}

void Image::readWriteImagesFlag(PVParams * params) {
   this->writeImages = params->value(name, "writeImages", writeImages) != 0;
}

void Image::readWriteImagesExtension(PVParams * params) {
   assert(!params->presentAndNotBeenRead(name, "writeImages"));
   if (this->writeImages) {
      if (params->stringPresent(name, "writeImagesExtension")) {
         writeImagesExtension = strdup(params->stringValue(name, "writeImagesExtension", false));
      }
      else {<commit_msg>One more try: call to readWritePosition<commit_after>/*
 * Image.cpp
 *
 *  Created on: Sep 8, 2009
 *      Author: rasmussn
 */

#include "Image.hpp"

#ifdef PV_USE_MPI
#include <mpi.h>
#endif
#include <assert.h>
#include <string.h>

namespace PV {

Image::Image() {
   initialize_base();
}

Image::Image(const char * name, HyPerCol * hc, const char * filename) {
   initialize_base();
   initialize(name, hc, filename);
}

Image::~Image() {
   free(filename);
   filename = NULL;
   Communicator::freeDatatypes(mpi_datatypes); mpi_datatypes = NULL;

   if(writePosition){
      if (getParent()->icCommunicator()->commRank()==0 && fp_pos != NULL && fp_pos->isfile) {
         PV_fclose(fp_pos);
      }
   }
}

int Image::initialize_base() {
   mpi_datatypes = NULL;
   data = NULL;
   filename = NULL;
   imageData = NULL;
   useImageBCflag = false;
   autoResizeFlag = false;
   writeImages = false;
   writeImagesExtension = NULL;
   inverseFlag = false;
   normalizeLuminanceFlag = false;
   offsets[0] = 0;
   offsets[1] = 0;
   jitterFlag = false;
   jitterType = RANDOM_WALK;
   timeSinceLastJitter = 0;
   jitterRefractoryPeriod = 0;
   stepSize = 0;
   persistenceProb = 0.0;
   recurrenceProb = 1.0;
   biasChangeTime = LONG_MAX;
   writePosition = 0;
   fp_pos = NULL;
   biases[0]   = getOffsetX();
   biases[1]   = getOffsetY();
   frameNumber = 0;
   return PV_SUCCESS;
}

int Image::initialize(const char * name, HyPerCol * hc, const char * filename) {
   if (filename != NULL) {
      this->filename = strdup(filename);
      assert( this->filename != NULL);
   }
   else {
      this->filename = NULL;
   }

   HyPerLayer::initialize(name, hc, 0);
   // Much of the functionality that was previously here has been moved to either read-methods, communicateInitInfo, or allocateDataStructures

   int status = PV_SUCCESS;

   this->lastUpdateTime = 0.0;

   PVParams * params = parent->parameters();

   assert(!params->presentAndNotBeenRead(name, "jitterFlag"));
   if (jitterFlag) {
      assert(!params->presentAndNotBeenRead(name, "offsetX"));
      assert(!params->presentAndNotBeenRead(name, "offsetY"));
      biases[0] = getOffsetX();
      biases[1] = getOffsetY();
   }

   // TODO - Add option to use image size to determine layer size and column size

   return status;
}

int Image::setParams(PVParams * params) {
   int status = HyPerLayer::setParams(params);

   readOffsets(params);
   readWriteImagesFlag(params);

   readUseImageBCflag(params);
   readAutoResizeFlag(params);
   readInverseFlag(params);
   readNormalizeLuminanceFlag(params);

   readFrameNumber(params);

   // Although Image itself does not use jitter, both Movie and Patterns do, so jitterFlag is read in Image.
   readJitterFlag(params);
   readJitterType(params);
   readJitterRefractoryPeriod(params);
   readStepSize(params);
   readPersistenceProb(params);
   readRecurrenceProb(params);
   readBiasChangeTime(params);
   readBiasConstraintMethod(params);
   readOffsetConstraintMethod(params);
   readWritePosition(params);

   return status;
}

int Image::readOffsets(PVParams * params) {

   offsets[0]      = (int) params->value(name,"offsetX", offsets[0]);
   offsets[1]      = (int) params->value(name,"offsetY", offsets[1]);

   return PV_SUCCESS;
}

void Image::readWriteImagesFlag(PVParams * params) {
   this->writeImages = params->value(name, "writeImages", writeImages) != 0;
}

void Image::readWriteImagesExtension(PVParams * params) {
   assert(!params->presentAndNotBeenRead(name, "writeImages"));
   if (this->writeImages) {
      if (params->stringPresent(name, "writeImagesExtension")) {
         writeImagesExtension = strdup(params->stringValue(name, "writeImagesExtension", false));
      }
      else {
{'fraction_non_alphanumeric': 0.12793522267206478, 'fraction_numerical': 0.008367071524966262, 'mean_word_length': 3.8949801849405548, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, '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': '4399873', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2386, 'n_words': 522}