Inviwo 0.9.12-pre
Inviwo documentation
Loading...
Searching...
No Matches
inviwo::CSVReader Class Reference

A reader for comma separated value (CSV) files with customizable delimiters and filters. The default delimiter is ',' and headers are included. Floating point values are stored as float32 unless double precision is enabled. More...

#include <csvreader.h>

+ Inheritance diagram for inviwo::CSVReader:

Public Types

enum class  EmptyField { Throw , EmptyOrZero , NanOrZero }
 

Public Member Functions

 CSVReader (std::string_view delimiters=defaultDelimiters, bool hasHeader=defaultFirstRowHeader, bool doublePrecision=defaultDoublePrecision)
 
 CSVReader (const CSVReader &)=default
 
 CSVReader (CSVReader &&) noexcept=default
 
CSVReaderoperator= (const CSVReader &)=default
 
CSVReaderoperator= (CSVReader &&) noexcept=default
 
virtual CSVReaderclone () const override
 
CSVReadersetDelimiters (const std::string &delim)
 
const std::string & getDelimiters () const
 
CSVReadersetStripQuotes (bool stripQuotes)
 
bool getStripQuotes () const
 
CSVReadersetFirstRowHeader (bool hasHeader)
 
bool hasFirstRowHeader () const
 
CSVReadersetFirstColIndices (bool hasIndices)
 
bool hasFirstColIndices () const
 
CSVReadersetUnitsInHeaders (bool unitInHeaders)
 
bool hasUnitsInHeaders () const
 
CSVReadersetUnitRegexp (std::string_view regexp)
 
const std::string & getUnitRegexp () const
 
CSVReadersetEnableDoublePrecision (bool useDoublePrecision)
 
bool hasDoublePrecision () const
 
CSVReadersetNumberOfExampleRows (size_t rows)
 
size_t getNumberOfExamplesRows () const
 
CSVReadersetLocale (std::string_view loc)
 
const std::string & getLocale () const
 
CSVReadersetFilters (const csvfilters::Filters &filters)
 
const csvfilters::FiltersgetFilters () const
 
CSVReadersetHandleEmptyFields (EmptyField emptyField)
 
EmptyField getHandleEmptyFields () const
 
virtual std::shared_ptr< DataFramereadData (const std::filesystem::path &fileName) override
 
std::shared_ptr< DataFramereadData (std::istream &stream) const
 
virtual bool setOption (std::string_view key, std::any value) override
 
virtual std::any getOption (std::string_view key) override
 
- Public Member Functions inherited from inviwo::DataReaderType< DataFrame >
 DataReaderType (const DataReaderType &rhs)=default
 
 DataReaderType (DataReaderType &&rhs) noexcept=default
 
DataReaderTypeoperator= (const DataReaderType &that)=default
 
DataReaderTypeoperator= (DataReaderType &&that) noexcept=default
 
virtual std::shared_ptr< DataFramereadData (const std::filesystem::path &filePath, MetaDataOwner *)
 
- Public Member Functions inherited from inviwo::DataReader
 DataReader (const DataReader &rhs)=default
 
 DataReader (DataReader &&rhs) noexcept=default
 
DataReaderoperator= (const DataReader &that)=default
 
DataReaderoperator= (DataReader &&that) noexcept=default
 
const std::vector< FileExtension > & getExtensions () const
 
void addExtension (FileExtension ext)
 
template<typename T >
bool readsType () const
 

Static Public Attributes

static constexpr std::string_view defaultDelimiters = ","
 
static constexpr bool defaultStripQuotes = true
 
static constexpr bool defaultFirstRowHeader = true
 
static constexpr bool defaultFirstColIndices = false
 
static constexpr bool defaultUnitInHeaders = true
 
static constexpr std::string_view defaultUnitRegexp = R"((.*)\[(.*)\])"
 
static constexpr bool defaultDoublePrecision = false
 
static constexpr size_t defaultNumberOfExampleRows = 50
 
static constexpr std::string_view defaultLocale = "C"
 
static constexpr EmptyField defaultEmptyField = EmptyField::NanOrZero
 

Additional Inherited Members

- Protected Member Functions inherited from inviwo::DataReaderType< DataFrame >
virtual bool canRead (const std::type_index &index) const override
 
- Protected Member Functions inherited from inviwo::DataReader
void checkExists (const std::filesystem::path &path) const
 
std::ifstream open (const std::filesystem::path &path, std::ios_base::openmode mode=std::ios_base::in) const
 
- Protected Attributes inherited from inviwo::DataReader
std::vector< FileExtensionextensions_
 

Detailed Description

A reader for comma separated value (CSV) files with customizable delimiters and filters. The default delimiter is ',' and headers are included. Floating point values are stored as float32 unless double precision is enabled.

Member Enumeration Documentation

◆ EmptyField

enum class inviwo::CSVReader::EmptyField
strong

How to handle missing / empty data

Enumerator
Throw 

Throw a DataReaderException.

EmptyOrZero 

"" for categorical and 0 for numerical values

NanOrZero 

"" for categorical, NaN for floating point, and 0 for integer values

Member Function Documentation

◆ clone()

virtual CSVReader * inviwo::CSVReader::clone ( ) const
overridevirtual

◆ getOption()

virtual std::any inviwo::CSVReader::getOption ( std::string_view key)
overridevirtual

Get any of the settings supported by the reader, supported keys:

  • Delimiters (string)
  • StripQuotes (bool)
  • FirstRowHeader (bool)
  • FirstColIndices (bool)
  • UnitsInHeaders (bool)
  • UnitRegexp (sting)
  • DoublePrecision (bool)
  • NumberOfExampleRows (size_t)
  • Locale (string)
  • HandleEmptyFields (EmptyField)
  • Filters (csvfilters::Filters)

Reimplemented from inviwo::DataReader.

◆ readData() [1/2]

virtual std::shared_ptr< DataFrame > inviwo::CSVReader::readData ( const std::filesystem::path & fileName)
overridevirtual

read a CSV file from a file

Parameters
fileNamename of the input CSV file
Returns
a DataFrame containing the CSV data
Exceptions
FileExceptionif the file cannot be accessed
DataReaderExceptionif the file contains no data, the first row should hold column headers, but they cannot be found, or if there are unmatched quotes at the end of the file

Implements inviwo::DataReaderType< DataFrame >.

◆ readData() [2/2]

std::shared_ptr< DataFrame > inviwo::CSVReader::readData ( std::istream & stream) const

read a CSV file from a input stream, e.g. a std::ifstream. In case file streams are used, the file must have be opened prior calling this function.

Parameters
streaminput stream with the CSV data
Returns
a DataFrame containing the CSV data
Exceptions
DataReaderExceptionif the given stream is in a bad state, the stream contains no data, the first row should hold column headers, but they cannot be found, or if there are unmatched quotes at the end of the stream

◆ setDelimiters()

CSVReader & inviwo::CSVReader::setDelimiters ( const std::string & delim)

◆ setEnableDoublePrecision()

CSVReader & inviwo::CSVReader::setEnableDoublePrecision ( bool useDoublePrecision)

Sets the precision for columns containing floating point values. If useDoublePrecision is true, values are stored as double (64 bits), otherwise float (32 bits) is used.

See also
CSVReader::defaultDoublePrecision

◆ setFilters()

CSVReader & inviwo::CSVReader::setFilters ( const csvfilters::Filters & filters)

Sets row and column filters.

See also
Filters

◆ setFirstColIndices()

CSVReader & inviwo::CSVReader::setFirstColIndices ( bool hasIndices)

◆ setFirstRowHeader()

CSVReader & inviwo::CSVReader::setFirstRowHeader ( bool hasHeader)

◆ setHandleEmptyFields()

CSVReader & inviwo::CSVReader::setHandleEmptyFields ( EmptyField emptyField)

◆ setLocale()

CSVReader & inviwo::CSVReader::setLocale ( std::string_view loc)

◆ setNumberOfExampleRows()

CSVReader & inviwo::CSVReader::setNumberOfExampleRows ( size_t rows)

◆ setOption()

virtual bool inviwo::CSVReader::setOption ( std::string_view key,
std::any value )
overridevirtual

Set any of the settings supported by the reader, supported keys:

  • Delimiters (string)
  • StripQuotes (bool)
  • FirstRowHeader (bool)
  • FirstColIndices (bool)
  • UnitsInHeaders (bool)
  • UnitRegexp (sting)
  • DoublePrecision (bool)
  • NumberOfExampleRows (size_t)
  • Locale (string)
  • HandleEmptyFields (EmptyField)
  • Filters (csvfilters::Filters)

Reimplemented from inviwo::DataReader.

◆ setStripQuotes()

CSVReader & inviwo::CSVReader::setStripQuotes ( bool stripQuotes)

◆ setUnitRegexp()

CSVReader & inviwo::CSVReader::setUnitRegexp ( std::string_view regexp)

◆ setUnitsInHeaders()

CSVReader & inviwo::CSVReader::setUnitsInHeaders ( bool unitInHeaders)

Member Data Documentation

◆ defaultDelimiters

std::string_view inviwo::CSVReader::defaultDelimiters = ","
staticconstexpr

◆ defaultDoublePrecision

bool inviwo::CSVReader::defaultDoublePrecision = false
staticconstexpr
See also
CSVReader::setDoublePrecision

◆ defaultEmptyField

EmptyField inviwo::CSVReader::defaultEmptyField = EmptyField::NanOrZero
staticconstexpr

◆ defaultFirstColIndices

bool inviwo::CSVReader::defaultFirstColIndices = false
staticconstexpr

◆ defaultFirstRowHeader

bool inviwo::CSVReader::defaultFirstRowHeader = true
staticconstexpr

◆ defaultLocale

std::string_view inviwo::CSVReader::defaultLocale = "C"
staticconstexpr

◆ defaultNumberOfExampleRows

size_t inviwo::CSVReader::defaultNumberOfExampleRows = 50
staticconstexpr

◆ defaultStripQuotes

bool inviwo::CSVReader::defaultStripQuotes = true
staticconstexpr

◆ defaultUnitInHeaders

bool inviwo::CSVReader::defaultUnitInHeaders = true
staticconstexpr
See also
CSVReader::setUnitInHeaderss

◆ defaultUnitRegexp

std::string_view inviwo::CSVReader::defaultUnitRegexp = R"((.*)\[(.*)\])"
staticconstexpr

The documentation for this class was generated from the following file: