easyvvuq.decoders.simple_csv

A Decoder for CSV format files.

  1"""A Decoder for CSV format files.
  2"""
  3
  4import os
  5import logging
  6import csv
  7from easyvvuq import OutputType
  8
  9__copyright__ = """
 10
 11    Copyright 2018 Robin A. Richardson, David W. Wright
 12
 13    This file is part of EasyVVUQ
 14
 15    EasyVVUQ is free software: you can redistribute it and/or modify
 16    it under the terms of the Lesser GNU General Public License as published by
 17    the Free Software Foundation, either version 3 of the License, or
 18    (at your option) any later version.
 19
 20    EasyVVUQ is distributed in the hope that it will be useful,
 21    but WITHOUT ANY WARRANTY; without even the implied warranty of
 22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 23    Lesser GNU General Public License for more details.
 24
 25    You should have received a copy of the Lesser GNU General Public License
 26    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 27
 28"""
 29__license__ = "LGPL"
 30
 31
 32logger = logging.Logger(__name__)
 33
 34
 35class SimpleCSV:
 36    """CSV Decoder.
 37
 38    Parameters
 39    ----------
 40    target_filename: str
 41        Filename of a CSV file to decode.
 42    ouput_columns: list
 43        A list of column names that will be selected to appear in the output.
 44    """
 45
 46    def __init__(self, target_filename, output_columns, dialect='excel'):
 47        if len(output_columns) == 0:
 48            msg = "output_columns cannot be empty."
 49            logger.error(msg)
 50            raise RuntimeError(msg)
 51        self.target_filename = target_filename
 52        self.output_columns = output_columns
 53        self.output_type = OutputType('sample')
 54        self.dialect = dialect
 55
 56    @staticmethod
 57    def _get_output_path(run_info=None, outfile=None):
 58        """Constructs absolute path from the `target_filename` and the `run_dir` parameter
 59        in the `run_info` retrieved from the database.
 60
 61        Parameters
 62        ----------
 63        run_info: dict
 64            Run info as retrieved from the database.
 65        outfile: str
 66            Filename of the file to be parsed.
 67
 68        Returns
 69        -------
 70        str
 71            An absolute path to the output file in the run directory.
 72        """
 73        run_path = run_info['run_dir']
 74        if not os.path.isdir(run_path):
 75            raise RuntimeError(f"Run directory does not exist: {run_path}")
 76        return os.path.join(run_path, outfile)
 77
 78    def parse_sim_output(self, run_info={}):
 79        """Parses the CSV file and converts it to the EasyVVUQ internal dictionary based
 80        format. The file is parsed in such a way that each column will appear as a vector
 81        QoI in the output dictionary.
 82
 83        For example if the file contains the following data
 84        a,b
 85        1,2
 86        3,4
 87
 88        And both `a` and `b` are specified as `output_columns` the output will look as follows
 89        {'a': [1, 3], 'b': [2, 4]}.
 90
 91        Parameters
 92        ----------
 93        run_info: dict
 94            Information about the run (used to retrieve construct the absolute path
 95            to the CSV file that needs decoding.
 96        """
 97        out_path = self._get_output_path(run_info, self.target_filename)
 98        results = {}
 99        for column in self.output_columns:
100            results[column] = []
101        with open(out_path, 'r', newline='') as csvfile:
102            reader = csv.DictReader(csvfile, dialect=self.dialect)
103            for row in reader:
104                for column in self.output_columns:
105                    try:
106                        results[column].append(float(row[column]))
107                    except ValueError:
108                        results[column].append(row[column])
109                    except KeyError:
110                        raise RuntimeError('column not found in the csv file: {}'.format(column))
111        return results
logger = <Logger easyvvuq.decoders.simple_csv (NOTSET)>
class SimpleCSV:
 36class SimpleCSV:
 37    """CSV Decoder.
 38
 39    Parameters
 40    ----------
 41    target_filename: str
 42        Filename of a CSV file to decode.
 43    ouput_columns: list
 44        A list of column names that will be selected to appear in the output.
 45    """
 46
 47    def __init__(self, target_filename, output_columns, dialect='excel'):
 48        if len(output_columns) == 0:
 49            msg = "output_columns cannot be empty."
 50            logger.error(msg)
 51            raise RuntimeError(msg)
 52        self.target_filename = target_filename
 53        self.output_columns = output_columns
 54        self.output_type = OutputType('sample')
 55        self.dialect = dialect
 56
 57    @staticmethod
 58    def _get_output_path(run_info=None, outfile=None):
 59        """Constructs absolute path from the `target_filename` and the `run_dir` parameter
 60        in the `run_info` retrieved from the database.
 61
 62        Parameters
 63        ----------
 64        run_info: dict
 65            Run info as retrieved from the database.
 66        outfile: str
 67            Filename of the file to be parsed.
 68
 69        Returns
 70        -------
 71        str
 72            An absolute path to the output file in the run directory.
 73        """
 74        run_path = run_info['run_dir']
 75        if not os.path.isdir(run_path):
 76            raise RuntimeError(f"Run directory does not exist: {run_path}")
 77        return os.path.join(run_path, outfile)
 78
 79    def parse_sim_output(self, run_info={}):
 80        """Parses the CSV file and converts it to the EasyVVUQ internal dictionary based
 81        format. The file is parsed in such a way that each column will appear as a vector
 82        QoI in the output dictionary.
 83
 84        For example if the file contains the following data
 85        a,b
 86        1,2
 87        3,4
 88
 89        And both `a` and `b` are specified as `output_columns` the output will look as follows
 90        {'a': [1, 3], 'b': [2, 4]}.
 91
 92        Parameters
 93        ----------
 94        run_info: dict
 95            Information about the run (used to retrieve construct the absolute path
 96            to the CSV file that needs decoding.
 97        """
 98        out_path = self._get_output_path(run_info, self.target_filename)
 99        results = {}
100        for column in self.output_columns:
101            results[column] = []
102        with open(out_path, 'r', newline='') as csvfile:
103            reader = csv.DictReader(csvfile, dialect=self.dialect)
104            for row in reader:
105                for column in self.output_columns:
106                    try:
107                        results[column].append(float(row[column]))
108                    except ValueError:
109                        results[column].append(row[column])
110                    except KeyError:
111                        raise RuntimeError('column not found in the csv file: {}'.format(column))
112        return results

CSV Decoder.

Parameters
  • target_filename (str): Filename of a CSV file to decode.
  • ouput_columns (list): A list of column names that will be selected to appear in the output.
SimpleCSV(target_filename, output_columns, dialect='excel')
47    def __init__(self, target_filename, output_columns, dialect='excel'):
48        if len(output_columns) == 0:
49            msg = "output_columns cannot be empty."
50            logger.error(msg)
51            raise RuntimeError(msg)
52        self.target_filename = target_filename
53        self.output_columns = output_columns
54        self.output_type = OutputType('sample')
55        self.dialect = dialect
target_filename
output_columns
output_type
dialect
def parse_sim_output(self, run_info={}):
 79    def parse_sim_output(self, run_info={}):
 80        """Parses the CSV file and converts it to the EasyVVUQ internal dictionary based
 81        format. The file is parsed in such a way that each column will appear as a vector
 82        QoI in the output dictionary.
 83
 84        For example if the file contains the following data
 85        a,b
 86        1,2
 87        3,4
 88
 89        And both `a` and `b` are specified as `output_columns` the output will look as follows
 90        {'a': [1, 3], 'b': [2, 4]}.
 91
 92        Parameters
 93        ----------
 94        run_info: dict
 95            Information about the run (used to retrieve construct the absolute path
 96            to the CSV file that needs decoding.
 97        """
 98        out_path = self._get_output_path(run_info, self.target_filename)
 99        results = {}
100        for column in self.output_columns:
101            results[column] = []
102        with open(out_path, 'r', newline='') as csvfile:
103            reader = csv.DictReader(csvfile, dialect=self.dialect)
104            for row in reader:
105                for column in self.output_columns:
106                    try:
107                        results[column].append(float(row[column]))
108                    except ValueError:
109                        results[column].append(row[column])
110                    except KeyError:
111                        raise RuntimeError('column not found in the csv file: {}'.format(column))
112        return results

Parses the CSV file and converts it to the EasyVVUQ internal dictionary based format. The file is parsed in such a way that each column will appear as a vector QoI in the output dictionary.

For example if the file contains the following data a,b 1,2 3,4

And both a and b are specified as output_columns the output will look as follows {'a': [1, 3], 'b': [2, 4]}.

Parameters
  • run_info (dict): Information about the run (used to retrieve construct the absolute path to the CSV file that needs decoding.