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 <inviwo/dataframe/io/csvreader.h>

Inheritance diagram for inviwo::CSVReader:

Public Types

enum class  EmptyField { Throw , EmptyOrZero , NanOrZero }

Public Member Functions

virtual CSVReader * clone () const override
 CSVReader (const CSVReader &)=default
 CSVReader (CSVReader &&) noexcept=default
 CSVReader (std::string_view delimiters=defaultDelimiters, bool hasHeader=defaultFirstRowHeader, bool doublePrecision=defaultDoublePrecision)
const std::string & getDelimiters () const
const csvfilters::FiltersgetFilters () const
EmptyField getHandleEmptyFields () const
const std::string & getLocale () const
size_t getNumberOfExamplesRows () const
virtual std::any getOption (std::string_view key) override
bool getStripQuotes () const
const std::string & getUnitRegexp () const
bool hasDoublePrecision () const
bool hasFirstColIndices () const
bool hasFirstRowHeader () const
bool hasUnitsInHeaders () const
CSVReader & operator= (const CSVReader &)=default
CSVReader & operator= (CSVReader &&) noexcept=default
virtual std::shared_ptr< DataFramereadData (const std::filesystem::path &fileName) override
std::shared_ptr< DataFramereadData (std::istream &stream) const
CSVReader & setDelimiters (const std::string &delim)
CSVReader & setEnableDoublePrecision (bool useDoublePrecision)
CSVReader & setFilters (const csvfilters::Filters &filters)
CSVReader & setFirstColIndices (bool hasIndices)
CSVReader & setFirstRowHeader (bool hasHeader)
CSVReader & setHandleEmptyFields (EmptyField emptyField)
CSVReader & setLocale (std::string_view loc)
CSVReader & setNumberOfExampleRows (size_t rows)
virtual bool setOption (std::string_view key, std::any value) override
CSVReader & setStripQuotes (bool stripQuotes)
CSVReader & setUnitRegexp (std::string_view regexp)
CSVReader & setUnitsInHeaders (bool unitInHeaders)
Public Member Functions inherited from inviwo::DataReaderType< DataFrame >
DataReaderType & operator= (const DataReaderType &that)=default
Public Member Functions inherited from inviwo::DataReader
void addExtension (FileExtension ext)
 DataReader (const DataReader &rhs)=default
 DataReader (DataReader &&rhs) noexcept=default
const std::vector< FileExtension > & getExtensions () const
DataReader & operator= (const DataReader &that)=default
DataReader & operator= (DataReader &&that) noexcept=default
template<typename T>
bool readsType () const

Static Public Attributes

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

Additional Inherited Members

Protected Member Functions inherited from inviwo::DataReaderType< DataFrame >
virtual bool canRead (const std::type_index &index) const override
Static Protected Member Functions inherited from inviwo::DataReader
static void checkExists (const std::filesystem::path &path)
static std::filesystem::path downloadAndCacheIfUrl (const std::filesystem::path &url)
static std::ifstream open (const std::filesystem::path &path, std::ios_base::openmode mode=std::ios_base::in)
static std::ifstream openAndCacheIfUrl (const std::filesystem::path &path, std::ios_base::openmode mode=std::ios_base::in)
static std::string readFileContents (const std::filesystem::path &path)
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:
  • modules/dataframe/include/inviwo/dataframe/io/csvreader.h