1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* 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.
*
*=========================================================================*/
#ifndef itkCSVNumericObjectFileWriter_h
#define itkCSVNumericObjectFileWriter_h
#include "itkLightProcessObject.h"
#include "itkMacro.h"
#include "itkArray2D.h"
#include "vnl/vnl_matrix.h"
#include "vnl/vnl_matrix_fixed.h"
#include "itkMatrix.h"
#include <vector>
#include "itkSize.h"
namespace itk
{
/**
* \class CSVNumericObjectFileWriter
* \brief Writes out numeric itk data objects to a csv file.
*
* CSVNumericObjectFileWriter writes numeric data from an itk object into a
* csv file. It is templated over the type of data being written, the number
* of rows and the number of columns. The writer uses the datablock member from
* vnl_matrix or vnl_matrix_fixed. As of now the objects itkMatrix, itkArray2D,
* and any vnl_matrix based objects are supported.
*
* The user specifies the file name and the field delimiter character using
* the Set macro method for each. The user can also write out row and column
* headers. The methods ColumnHeadersPushBack() and RowHeadersPushBack() can be
* used to push strings into the vectors for row and column headers. The row
* and column headers can also be set using the Set macro methods for each if
* vectors for the headers are already set. The SetInput() method is overloaded
* to allow various itk objects to be set as input.
*
* The method Write() is used to output the object data to a csv file.
*
* The writer will output a warning if the number of row headers is not
* consistent with the number of rows in the input object or if the number of
* column headers is not consistent with the number of columns in the input
* object. It is up to the user to check this.
*
*
* \ingroup ITKIOCSV
*/
template <typename TValue, unsigned int VRows = 0, unsigned int VColumns = 0>
class ITK_TEMPLATE_EXPORT CSVNumericObjectFileWriter : public LightProcessObject
{
public:
ITK_DISALLOW_COPY_AND_MOVE(CSVNumericObjectFileWriter);
/** Standard class type aliases */
using Self = CSVNumericObjectFileWriter;
using Superclass = LightProcessObject;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(CSVNumericObjectFileWriter);
// Matrix types
using vnlMatrixType = vnl_matrix<TValue>;
using vnlFixedMatrixType = vnl_matrix_fixed<TValue, VRows, VColumns>;
using itkMatrixType = itk::Matrix<TValue, VRows, VColumns>;
using StringVectorType = std::vector<std::string>;
using SizeValueType = itk::Size<2>::SizeValueType;
/* Specify the name of the output file */
itkSetStringMacro(FileName);
itkSetMacro(FieldDelimiterCharacter, char);
/** Set the input object if the matrix is of vnl_matrix type or Array2D. */
void
SetInput(const vnlMatrixType * obj);
/** Set the input object if the matrix is of vnl_matrix_fixed type. */
void
SetInput(const vnlFixedMatrixType * obj);
/** Set the input object if the matrix is of itkMatrixType. */
void
SetInput(const itkMatrixType * obj);
void
ColumnHeadersPushBack(const std::string &);
void
RowHeadersPushBack(const std::string &);
void
SetColumnHeaders(const StringVectorType & columnheaders);
void
SetRowHeaders(const StringVectorType & rowheaders);
/* Checks that all essential components are plugged in */
void
PrepareForWriting();
/** Write out the object */
virtual void
Write();
/** Aliased to the Write() method to be consistent with the rest of the
* pipeline. */
virtual void
Update();
protected:
CSVNumericObjectFileWriter();
~CSVNumericObjectFileWriter() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;
private:
std::string m_FileName{};
TValue * m_InputObject{};
char m_FieldDelimiterCharacter{};
SizeValueType m_Rows{};
SizeValueType m_Columns{};
StringVectorType m_ColumnHeaders{};
StringVectorType m_RowHeaders{};
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkCSVNumericObjectFileWriter.hxx"
#endif
#endif
|