<commit_before># -*- coding: utf-8 -*-
"""
CSV Tabular Data Input / Output
===============================

Defines various input / output objects for *CSV* tabular data files:

-   :func:`colour.read_spectral_data_from_csv_file`
-   :func:`colour.read_sds_from_csv_file`
-   :func:`colour.write_sds_to_csv_file`
"""

import csv
import numpy as np
import os
import tempfile
from collections import OrderedDict

from colour.colorimetry import SpectralDistribution
from colour.constants import DEFAULT_FLOAT_DTYPE
from colour.utilities import filter_kwargs

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = '<EMAIL>'
__status__ = 'Production'

__all__ = [
    'read_spectral_data_from_csv_file', 'read_sds_from_csv_file',
    'write_sds_to_csv_file'
]


def read_spectral_data_from_csv_file(path, **kwargs):
    """
    Reads the spectral data from given *CSV* file in the following form::

        390,  4.15003E-04,  3.68349E-04,  9.54729E-03
        395,  1.05192E-03,  9.58658E-04,  2.38250E-02
        400,  2.40836E-03,  2.26991E-03,  5.66498E-02
        ...
        830,  9.74306E-07,  9.53411E-08,  0.00000

    and returns it as an *OrderedDict* as follows::

        {
            'wavelength': ndarray,
            'field 1': ndarray,
            'field 2': ndarray,
            ...,
            'field n': ndarray
        }

    Parameters
    ----------
    path : unicode
        *CSV* file path.

    Other Parameters
    ----------------
    \\**kwargs : dict, optional
        Keywords arguments passed to :func:`np.recfromcsv` definition.

    Returns
    -------
    OrderedDict
        *CSV* file content.

    Notes
    -----
    -   A *CSV* spectral data file should define at least define two fields:
        one for the wavelengths and one for the associated values of one
        spectral distribution.

    Examples
    --------
    >>> import os
    >>> from pprint import pprint
    >>> csv_file = os.path.join(os.path.dirname(__file__), 'tests',
    ...                         'resources', 'colorchecker_n_ohta.csv')
    >>> sds_data = read_spectral_data_from_csv_file(csv_file)
    >>> pprint(list(sds_data.keys()))
    ['wavelength',
     '1',
     '2',
     '3',
     '4',
     '5',
     '6',
     '7',
     '8',
     '9',
     '10',
     '11',
     '12',
     '13',
     '14',
     '15',
     '16',
     '17',
     '18',
     '19',
     '20',
     '21',
     '22',
     '23',
     '24']
    """

    settings = {
        'case_sensitive': True,
        'deletechars': '',
        'replace_space': ' ',
        'dtype': DEFAULT_FLOAT_DTYPE,
    }
    settings.update(**kwargs)

    transpose = settings.get('transpose')
    if transpose:
        delimiter = settings.get('delimiter')
        if delimiter is not None:
            del settings['delimiter']

        with open(path, 'r') as csv_file:
            content = zip(*csv.reader(csv_file, delimiter=delimiter))

        transposed_csv_file = tempfile.NamedTemporaryFile(
            mode='w', delete=False)
        csv.writer(transposed_csv_file).writerows(content)
        path = transposed_csv_file.name

    data = np.recfromcsv(path, **filter_kwargs(np.genfromtxt, **settings))

    if transpose:
        os.unlink(transposed_csv_file.name)

    return OrderedDict([(name, data[name]) for name in data.dtype.names])


def read_sds_from_csv_file(path, **kwargs):
    """
    Reads the spectral data from given *CSV* file and returns its content as an<commit_msg>Add default delimiter to transposed CSV data reader.<commit_after># -*- coding: utf-8 -*-
"""
CSV Tabular Data Input / Output
===============================

Defines various input / output objects for *CSV* tabular data files:

-   :func:`colour.read_spectral_data_from_csv_file`
-   :func:`colour.read_sds_from_csv_file`
-   :func:`colour.write_sds_to_csv_file`
"""

import csv
import numpy as np
import os
import tempfile
from collections import OrderedDict

from colour.colorimetry import SpectralDistribution
from colour.constants import DEFAULT_FLOAT_DTYPE
from colour.utilities import filter_kwargs

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = '<EMAIL>'
__status__ = 'Production'

__all__ = [
    'read_spectral_data_from_csv_file', 'read_sds_from_csv_file',
    'write_sds_to_csv_file'
]


def read_spectral_data_from_csv_file(path, **kwargs):
    """
    Reads the spectral data from given *CSV* file in the following form::

        390,  4.15003E-04,  3.68349E-04,  9.54729E-03
        395,  1.05192E-03,  9.58658E-04,  2.38250E-02
        400,  2.40836E-03,  2.26991E-03,  5.66498E-02
        ...
        830,  9.74306E-07,  9.53411E-08,  0.00000

    and returns it as an *OrderedDict* as follows::

        {
            'wavelength': ndarray,
            'field 1': ndarray,
            'field 2': ndarray,
            ...,
            'field n': ndarray
        }

    Parameters
    ----------
    path : unicode
        *CSV* file path.

    Other Parameters
    ----------------
    \\**kwargs : dict, optional
        Keywords arguments passed to :func:`np.recfromcsv` definition.

    Returns
    -------
    OrderedDict
        *CSV* file content.

    Notes
    -----
    -   A *CSV* spectral data file should define at least define two fields:
        one for the wavelengths and one for the associated values of one
        spectral distribution.

    Examples
    --------
    >>> import os
    >>> from pprint import pprint
    >>> csv_file = os.path.join(os.path.dirname(__file__), 'tests',
    ...                         'resources', 'colorchecker_n_ohta.csv')
    >>> sds_data = read_spectral_data_from_csv_file(csv_file)
    >>> pprint(list(sds_data.keys()))
    ['wavelength',
     '1',
     '2',
     '3',
     '4',
     '5',
     '6',
     '7',
     '8',
     '9',
     '10',
     '11',
     '12',
     '13',
     '14',
     '15',
     '16',
     '17',
     '18',
     '19',
     '20',
     '21',
     '22',
     '23',
     '24']
    """

    settings = {
        'case_sensitive': True,
        'deletechars': '',
        'replace_space': ' ',
        'dtype': DEFAULT_FLOAT_DTYPE,
    }
    settings.update(**kwargs)

    transpose = settings.get('transpose')
    if transpose:
        delimiter = settings.get('delimiter', ',')
        if settings.get('delimiter') is not None:
            del settings['delimiter']

        with open(path, 'r') as csv_file:
            content = zip(*csv.reader(csv_file, delimiter=delimiter))

        transposed_csv_file = tempfile.NamedTemporaryFile(
            mode='w', delete=False)
        csv.writer(transposed_csv_file).writerows(content)
        path = transposed_csv_file.name

    data = np.recfromcsv(path, **filter_kwargs(np.genfromtxt, **settings))

    if transpose:
        os.unlink(transposed_csv_file.name)

    return OrderedDict([(name, data[name]) for name in data.dtype.names])


def read_sds_from_csv_file(path, **kwargs):
    """
    Reads the spectral data from given *CSV* file and returns its content as an
{'fraction_non_alphanumeric': 0.14051394204483325, 'fraction_numerical': 0.04291962821213778, 'mean_word_length': 2.3641379310344828, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1507399', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2392, 'n_words': 562}