Unverified Commit 18004d21 authored by Amy Stamile's avatar Amy Stamile Committed by GitHub
Browse files

reduce conversion and gtests (#4174)

* inital reduce conversion and gtests

* removed comment per PR feedback

* Removed log variables in tests.
parent 5feebf28
Loading
Loading
Loading
Loading
+13 −88
Original line number Diff line number Diff line
#include "Isis.h"
#include "IException.h"
#include "IString.h"
#include "ProcessByLine.h"
#include "Reduce.h"

#include <cmath>
#include "Application.h"
#include "Pvl.h"
#include "reduce_app.h"

using namespace std;
using namespace Isis;

void IsisMain() {
  try {
    // We will be processing by line
    ProcessByLine p;
    double sscale, lscale;
    int ins, inl, inb;
    int ons, onl;
    vector<QString> bands;
    Cube inCube;

    // To propogate labels, set input cube,
    // this cube will be cleared after output cube is set.
    p.SetInputCube("FROM");

    // Setup the input and output cubes
  UserInterface &ui = Application::GetUserInterface();
    QString replaceMode = ui.GetAsString("VPER_REPLACE");
    CubeAttributeInput cai(ui.GetAsString("FROM"));
    bands = cai.bands();

    inCube.setVirtualBands(bands);

    QString from = ui.GetFileName("FROM");
    inCube.open(from);

    ins = inCube.sampleCount();
    inl = inCube.lineCount();
    inb = inCube.bandCount();

    QString alg  = ui.GetString("ALGORITHM");
    double vper = ui.GetDouble("VALIDPER") / 100.;

    if(ui.GetString("MODE") == "TOTAL") {
      ons = ui.GetInteger("ONS");
      onl = ui.GetInteger("ONL");
      sscale = (double)ins / (double)ons;
      lscale = (double)inl / (double)onl;
    }
    else {
      sscale = ui.GetDouble("SSCALE");
      lscale = ui.GetDouble("LSCALE");
      ons = (int)((double)ins / sscale + 0.5);
      onl = (int)((double)inl / lscale + 0.5);
    }

    if(ons > ins || onl > inl) {
      QString msg = "Number of output samples/lines must be less than or equal";
      msg = msg + " to the input samples/lines.";
      throw IException(IException::User, msg, _FILEINFO_);
    }

    //  Allocate output file
    Cube *ocube = p.SetOutputCube("TO", ons, onl, inb);
    // Our processing routine only needs 1
    // the original set was for info about the cube only
    p.ClearInputCubes();

    // Start the processing
    PvlGroup results;
    if(alg == "AVERAGE"){
      Average average(&inCube, sscale, lscale, vper, replaceMode);
      p.ProcessCubeInPlace(average, false);
      results = average.UpdateOutputLabel(ocube);
  Pvl appLog;
  try {
    reduce(ui, &appLog);
  }
    else if(alg == "NEAREST") {
      Nearest near(&inCube, sscale, lscale);
      p.ProcessCubeInPlace(near, false);
      results = near.UpdateOutputLabel(ocube);
  catch (...) {
    for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) {
      Application::Log(*grpIt);
    }

    // Cleanup
    inCube.close();
    p.EndProcess();

    // Write the results to the log
    Application::Log(results);
  } // REFORMAT THESE ERRORS INTO ISIS TYPES AND RETHROW
  catch (IException &) {
    throw;
  }
  catch (std::exception const &se) {
    QString message = "std::exception: " + (QString)se.what();
    throw IException(IException::User, message, _FILEINFO_);
  }
  catch (...) {
    QString message = "Other Error";
    throw IException(IException::User, message, _FILEINFO_);

  for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) {
    Application::Log(*grpIt);
  }
}
+104 −0
Original line number Diff line number Diff line
#include "IException.h"
#include "IString.h"
#include "ProcessByLine.h"
#include "reduce_app.h"
#include "Reduce.h"


#include <cmath>

using namespace std;
namespace Isis{

  void reduce(UserInterface &ui, Pvl *log) {
    try {
      // We will be processing by line
      ProcessByLine p;
      double sscale, lscale;
      int ins, inl, inb;
      int ons, onl;
      vector<QString> bands;
      Cube inCube;

      // To propogate labels, set input cube,
      // this cube will be cleared after output cube is set.
      CubeAttributeInput &inputAtt = ui.GetInputAttribute("FROM");
      p.SetInputCube(ui.GetFileName("FROM"), inputAtt);

      // Setup the input and output cubes
      QString replaceMode = ui.GetAsString("VPER_REPLACE");
      CubeAttributeInput cai(ui.GetAsString("FROM"));
      bands = cai.bands();

      QString from = ui.GetFileName("FROM");
      inCube.setVirtualBands(bands);
      inCube.open(from);

      ins = inCube.sampleCount();
      inl = inCube.lineCount();
      inb = inCube.bandCount();

      QString alg  = ui.GetString("ALGORITHM");
      double vper = ui.GetDouble("VALIDPER") / 100.;

      if(ui.GetString("MODE") == "TOTAL") {
        ons = ui.GetInteger("ONS");
        onl = ui.GetInteger("ONL");
        sscale = (double)ins / (double)ons;
        lscale = (double)inl / (double)onl;
      }
      else {
        sscale = ui.GetDouble("SSCALE");
        lscale = ui.GetDouble("LSCALE");
        ons = (int)((double)ins / sscale + 0.5);
        onl = (int)((double)inl / lscale + 0.5);
      }

      if(ons > ins || onl > inl) {
        QString msg = "Number of output samples/lines must be less than or equal";
        msg = msg + " to the input samples/lines.";
        throw IException(IException::User, msg, _FILEINFO_);
      }

      //  Allocate output file
      CubeAttributeOutput &att = ui.GetOutputAttribute("TO");
      Cube *ocube = p.SetOutputCube(ui.GetFileName("TO"), att, ons, onl, inb);
      // Our processing routine only needs 1
      // the original set was for info about the cube only
      p.ClearInputCubes();

      // Start the processing
      PvlGroup results;
      if(alg == "AVERAGE"){
        Average average(&inCube, sscale, lscale, vper, replaceMode);
        p.ProcessCubeInPlace(average, false);
        results = average.UpdateOutputLabel(ocube);
      }
      else if(alg == "NEAREST") {
        Nearest near(&inCube, sscale, lscale);
        p.ProcessCubeInPlace(near, false);
        results = near.UpdateOutputLabel(ocube);
      }

      // Cleanup
      inCube.close();
      p.EndProcess();

      // Write the results to the log
      if (log){
        log->addGroup(results);
      }
    } // REFORMAT THESE ERRORS INTO ISIS TYPES AND RETHROW
    catch (IException &) {
      throw;
    }
    catch (std::exception const &se) {
      QString message = "std::exception: " + (QString)se.what();
      throw IException(IException::User, message, _FILEINFO_);
    }
    catch (...) {
      QString message = "Other Error";
      throw IException(IException::User, message, _FILEINFO_);
    }
  }
}
+10 −0
Original line number Diff line number Diff line
#ifndef reduceapp_h
#define reduceapp_h

#include "UserInterface.h"

namespace Isis{
  extern void reduce(UserInterface &ui, Pvl *log=nullptr);
}

#endif
+0 −4
Original line number Diff line number Diff line
BLANKS = "%-6s"    
LENGTH = "%-40s"

include $(ISISROOT)/make/isismake.tststree
+0 −22
Original line number Diff line number Diff line
APPNAME = reduce

labels3.txt.IGNORELINES = ByteOrder StartByte Bytes TileSamples TileLines
labels6.txt.IGNORELINES = ByteOrder StartByte Bytes TileSamples TileLines

include $(ISISROOT)/make/isismake.tsts

commands:
	$(APPNAME) from= $(INPUT)/peaks.cub \
	  to= $(OUTPUT)/reduceTruth3.cub \
	  algorithm=average \
	  mode=total \
	  ons=100 \
	  onl=100 > /dev/null;
	catlab from=$(OUTPUT)/reduceTruth3.cub  to=$(OUTPUT)/labels3.txt > /dev/null;
	$(APPNAME) from= $(OUTPUT)/reduceTruth3.cub \
          to= $(OUTPUT)/reduceTruth6.cub \
          algorithm=average \
	  mode=total \
	  ons=50 \
          onl=50 > /dev/null;
	catlab from=$(OUTPUT)/reduceTruth6.cub to=$(OUTPUT)/labels6.txt > /dev/null;
Loading