CsvReader

@author: Marco Bonvini

class estimationpy.fmu_utils.csv_reader.CsvReader(filename='')

This class provides functionalities that can be used to provide input to an FMU model or to a state/parameter estimation algorithm through CSV files.

An object of class CsvReader manages CSV file that have the following format:

time, x, y, zeta, kappa hat, T [k]
0.0, 3.4, 23.0, 22, 5, 77.5
0.1, 3.4, 23.0, 22, 5, 77.3
0.2, 3.4, 23.0, 22, 5, 76.8
0.3, 3.4, 23.0, 22, 5, 34.4
0.4, 3.4, 23.0, 22, 5, 72.22
0.5, 3.4, 23.0, 22, 5, 71.9
0.6, 3.4, 23.0, 22, 5, 70.9

The first row is mandatory and is the header of the corresponding table created when importing the CSV file. The remaining rows contain data that are separated by commas.

The first column is time associated to the data series. This column will be used as index to create a pandas DataFrame. Since the first column is used as index, its name won’t be available among the column names associated tot the DataFrame.

NOTE:
The method assumes the first column of the CSV file is time, measured in seconds, and UTC referenced.
__init__(filename='')

Constructor for the class CsvReader. The method initializes the type of dialect used to interpet the CSV file, the list containing the names of the columns, and which columns are selected.

Parameters:filename (str) – The path that defines the CSV file to open. The argument is optinal because it can be specified later.
__open_csv__(csv_file)

This private method is used to open a CSV file given a path name specified by the parameter csv_file. The method uses the function pandas.io.parsers.read_csv to open the file.

NOTE:
The method assumes the first column of the CSV file is time, measured in seconds, and UTC referenced.
Parameters:

filename (str) – The path that defines the CSV file to open.

Returns:

The DataFrame object containing the data of the CSV file.

Return type:

pandas.DataFrame

Raises:
  • IOError – The method raises an IOErrror if the file specified by the argument csv_file does not exist.
  • ValueError – The method raises an ValueError if the file specified by the argument csv_file can’t be succesfully indexed.
__str__()

This method returns a string representation of the instance.

Returns:a String representation of the instance.
Return type:string
get_column_names()

This method returns a list containing the names of the columns contained in the csv file.

Returns:a List containing the names of the columns in the CSV file.
Return type:list(str)
get_data_series()

This method returns a pandas Series object that contains the data read from the CSV file at the column selected with the method set_selected_column().

Before calling this method make sure:

  1. the path of the CSV file has been specified,
  2. the name of the column to selected has been specified.
Returns:A Series object representing the time series data contained in the selected column. In case the file is not specified, or the column is not specified, the method returns an empty Series.
Return type:pandas.Series
get_file_name()

This method returns the filename of the CSV file associated to this object.

Returns:a String representing the path of the CSV file.
Return type:string
get_selected_column()

This method returns the name of the column selected.

Returns:Name of the column selected. If no columns are selected the method returns an empty string.
Return type:string
open_csv(filename)

This method open a CSV file given a path name specified by the parameter filename. The method uses the underlying method __open_csv__() to open the file.

NOTE:
The method assumes the first column of the CSV file is time, measured in seconds, and UTC referenced.
Parameters:

filename (str) – The path that defines the CSV file to open.

Returns:

True is the CSV is loaded and is not empty, False otherwise.

Return type:

bool

Raises:
  • IOError – The method raises an IOErrror if the file specified by the argument csv_file does not exist.
  • ValueError – The method raises an ValueError if the file specified by the argument csv_file can’t be succesfully indexed.
print_dialect_information()

This method print the information about the dialect used by the Csv Reader.

Returns:None
set_selected_column(columnName)

This method allows to specify which of the columns in the CSV file is selected. Once a column is selecetd, it’s possible to get the corresponding pandas.Series with the method get_data_series() .

Parameters:columnName (str) – The name of the column to be selected.
Returns:True if the name is successfully selected, False otherwise (e.g., if the name is not present in the available column names).
Return type:bool