<commit_before>"""
This module contains the csv interface.
"""

import pandas

from ..classes.pointisotherm import PointIsotherm


def isotherm_to_csv(isotherm, path, separator=','):
    '''

    A function that turns the isotherm into a csv
    file with the data and properties.

    Parameters
    ----------
    isotherm : PointIsotherm
        Isotherm to be written to csv.
    path : str
        Path to the file to be written.
    separator : str, optional
        Separator used int the csv file. Defaults to '',''.

    '''

    with open(path, mode='w') as file:

        isotherm_data = isotherm.to_dict()
        isotherm_data.pop('id', None)         # make sure id is not passed

        file.writelines([x + separator + str(y) + '\n'
                         for (x, y) in isotherm_data.items()])

        file.write('data:[pressure, loading, other...]\n')

        # get headings in an ordered way
        headings = [
            isotherm.pressure_key,
            isotherm.loading_key,
        ]
        if isotherm.other_keys:
            headings.extend(isotherm.other_keys)
        data = isotherm.data()[headings]

        file.write(data.to_csv(None, sep=separator, index=False, header=True))

    return


def isotherm_from_csv(path, separator=','):
    """
    A function that will get the experiment and sample data from a csv file
    file and return the isotherm object.

    Parameters
    ----------
    path : str
        Path to the file to be read.
    separator : str, optional
        Separator used int the csv file. Defaults to '',''.

    Returns
    -------
    PointIsotherm
        The isotherm contained in the csv file.
    """

    with open(path) as file:
        line = file.readline().rstrip()
        sample_info = {}

        while not line.startswith('data'):
            values = line.split(sep=separator)
            sample_info.update({values[0]: values[1]})
            line = file.readline().rstrip()

        data_df = pandas.read_csv(file, sep=separator)

    isotherm = PointIsotherm(
        data_df,
        loading_key=data_df.columns[0],
        pressure_key=data_df.columns[1],
        other_keys=list(data_df.columns[2:]),
        **sample_info)

    return isotherm
<commit_msg>switched column order in csv files<commit_after>"""
This module contains the csv interface.
"""

import pandas

from ..classes.pointisotherm import PointIsotherm


def isotherm_to_csv(isotherm, path, separator=','):
    '''

    A function that turns the isotherm into a csv
    file with the data and properties.

    Parameters
    ----------
    isotherm : PointIsotherm
        Isotherm to be written to csv.
    path : str
        Path to the file to be written.
    separator : str, optional
        Separator used int the csv file. Defaults to '',''.

    '''

    with open(path, mode='w') as file:

        isotherm_data = isotherm.to_dict()
        isotherm_data.pop('id', None)         # make sure id is not passed

        file.writelines([x + separator + str(y) + '\n'
                         for (x, y) in isotherm_data.items()])

        file.write('data:[pressure, loading, other...]\n')

        # get headings in an ordered way
        headings = [
            isotherm.pressure_key,
            isotherm.loading_key,
        ]
        if isotherm.other_keys:
            headings.extend(isotherm.other_keys)
        data = isotherm.data()[headings]

        file.write(data.to_csv(None, sep=separator, index=False, header=True))

    return


def isotherm_from_csv(path, separator=','):
    """
    A function that will get the experiment and sample data from a csv file
    file and return the isotherm object.

    Parameters
    ----------
    path : str
        Path to the file to be read.
    separator : str, optional
        Separator used int the csv file. Defaults to '',''.

    Returns
    -------
    PointIsotherm
        The isotherm contained in the csv file.
    """

    with open(path) as file:
        line = file.readline().rstrip()
        sample_info = {}

        while not line.startswith('data'):
            values = line.split(sep=separator)
            sample_info.update({values[0]: values[1]})
            line = file.readline().rstrip()

        data_df = pandas.read_csv(file, sep=separator)

    isotherm = PointIsotherm(
        data_df,
        pressure_key=data_df.columns[0],
        loading_key=data_df.columns[1],
        other_keys=list(data_df.columns[2:]),
        **sample_info)

    return isotherm

{'fraction_non_alphanumeric': 0.11093854004881296, 'fraction_numerical': 0.002218770800976259, 'mean_word_length': 2.1262135922330097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6182460', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1324, 'n_words': 381}