otbVectorDimensionalityReduction.cxx 16.20 KiB
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 * This file is part of Orfeo Toolbox
 *     https://www.orfeo-toolbox.org/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbOGRDataSourceWrapper.h"
#include "otbOGRFeatureWrapper.h"
#include "itkVariableLengthVector.h"
#include "otbStatisticsXMLFileReader.h"
#include "itkListSample.h"
#include "otbShiftScaleSampleListFilter.h"
#include "otbDimensionalityReductionModelFactory.h"
#include <time.h>
namespace otb
namespace Wrapper
/** Utility function to negate std::isalnum */
bool IsNotAlphaNum(char c)
return !std::isalnum(c);
/**
 * \class VectorDimensionalityReduction
 * Apply a dimensionality reduction model on a vector file
class VectorDimensionalityReduction : public Application
public:
    /** Standard class typedefs. */
  typedef VectorDimensionalityReduction Self;
  typedef Application Superclass;
  typedef itk::SmartPointer<Self> Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;
  /** Standard macro */
  itkNewMacro(Self);
  itkTypeMacro(Self, Application)
  /** Filters typedef */
  typedef float                                           ValueType;
  typedef itk::VariableLengthVector<ValueType>            InputSampleType;
  typedef itk::Statistics::ListSample<InputSampleType>    ListSampleType;
  typedef MachineLearningModel<
    itk::VariableLengthVector<ValueType>,
    itk::VariableLengthVector<ValueType> >                DimensionalityReductionModelType;
  typedef DimensionalityReductionModelFactory<
    ValueType,ValueType>                                  DimensionalityReductionModelFactoryType;
  typedef DimensionalityReductionModelType::Pointer       ModelPointerType;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
/** Statistics Filters typedef */ typedef itk::VariableLengthVector<ValueType> MeasurementType; typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; typedef otb::Statistics::ShiftScaleSampleListFilter< ListSampleType, ListSampleType> ShiftScaleFilterType; protected: ~VectorDimensionalityReduction() override { DimensionalityReductionModelFactoryType::CleanFactories(); } private: void DoInit() override { SetName("VectorDimensionalityReduction"); SetDescription("Performs dimensionality reduction of the input vector data " "according to a model file."); SetDocName("Vector Dimensionality Reduction"); SetDocAuthors("OTB-Team"); SetDocLongDescription("This application performs a vector data " "dimensionality reduction based on a model file produced by the " "TrainDimensionalityReduction application."); SetDocSeeAlso("TrainDimensionalityReduction"); SetDocLimitations("None"); AddDocTag(Tags::Learning); AddParameter(ParameterType_InputVectorData, "in", "Name of the input vector data"); SetParameterDescription("in","The input vector data to reduce."); AddParameter(ParameterType_InputFilename, "instat", "Statistics file"); SetParameterDescription("instat", "A XML file containing mean and standard " "deviation to center and reduce samples before dimensionality reduction " "(produced by ComputeImagesStatistics application)."); MandatoryOff("instat"); AddParameter(ParameterType_InputFilename, "model", "Model file"); SetParameterDescription("model", "A model file (produced by the " "TrainDimensionalityReduction application,"); AddParameter(ParameterType_OutputFilename, "out", "Output vector data file " "containing the reduced vector"); SetParameterDescription("out","Output vector data file storing sample " "values (OGR format). If not given, the input vector data file is used. " "In overwrite mode, the original features will be lost."); MandatoryOff("out"); AddParameter(ParameterType_ListView, "feat", "Input features to use for reduction"); SetParameterDescription("feat", "List of field names in the input vector data used as features for reduction."); AddParameter(ParameterType_Choice, "featout", "Output feature"); SetParameterDescription("featout", "Naming of output features"); AddChoice("featout.prefix", "Prefix"); SetParameterDescription("featout.prefix", "Use a name prefix"); AddParameter(ParameterType_String, "featout.prefix.name", "Feature name prefix"); SetParameterDescription("featout.prefix.name","Name prefix for output " "features. This prefix is followed by the numeric index of each output feature."); SetParameterString("featout.prefix.name","reduced_", false); AddChoice("featout.list","List"); SetParameterDescription("featout.list", "Use a list with all names"); AddParameter(ParameterType_StringList, "featout.list.names", "Feature name list"); SetParameterDescription("featout.list.names", "List of field names for the output " "features which result from the reduction."); AddParameter(ParameterType_Int, "pcadim", "Principal component dimension");