# Path: sksurv/io/arffread.py
# def loadarff(filename):
#     """Load ARFF file
# 
#     Parameters
#     ----------
#     filename : string
#         Path to ARFF file
# 
#     Returns
#     -------
#     data_frame : :class:`pandas.DataFrame`
#         DataFrame containing data of ARFF file
#     """
#     data, meta = scipy_loadarff(filename)
#     return _to_pandas(data, meta)
# 
# Path: sksurv/io/arffwrite.py
# def writearff(data, filename, relation_name=None, index=True):
#     """Write ARFF file
# 
#     Parameters
#     ----------
#     data : :class:`pandas.DataFrame`
#         DataFrame containing data
# 
#     filename : string or file-like object
#         Path to ARFF file or file-like object. In the latter case,
#         the handle is closed by calling this function.
# 
#     relation_name : string, optional, default: "pandas"
#         Name of relation in ARFF file.
# 
#     index : boolean, optional, default: True
#         Write row names (index)
#     """
#     if isinstance(filename, str):
#         fp = open(filename, 'w')
# 
#         if relation_name is None:
#             relation_name = os.path.basename(filename)
#     else:
#         fp = filename
# 
#         if relation_name is None:
#             relation_name = "pandas"
# 
#     try:
#         data = _write_header(data, fp, relation_name, index)
#         fp.write("\n")
#         _write_data(data, fp)
#     finally:
#         fp.close()


from collections import OrderedDict
from io import StringIO
from sksurv.io import loadarff, writearff
import numpy
import pandas
import pandas.testing as tm
import pytest
                     '"2014-10-31 14:13:01"\n',
                     '"2004-03-13 19:49:31"\n',
                     '"1998-12-06 09:10:11"\n']


def test_loadarff_dataframe():
    contents = "".join(EXPECTED_NO_QUOTES)
    with StringIO(contents) as fp:
        actual_df = loadarff(fp)

    expected_df = pandas.DataFrame.from_dict(OrderedDict(
        [("attr_nominal",
          pandas.Series(pandas.Categorical.from_codes(
              [1, 2, 0, -1, 2, 1],
              ["beer", "water", "wine"]))),
         ("attr_nominal_spaces",
          pandas.Series(pandas.Categorical.from_codes(
              [2, 0, -1, 1, 0, 1],
              ['hard liquor', 'mate', 'red wine'])))
         ]
    ))

    tm.assert_frame_equal(expected_df, actual_df, check_exact=True)


def test_writearff_nominal(temp_file):
    data = pandas.DataFrame({'attr_nominal': ['water', 'wine', 'beer', None, 'wine', 'water'],
                             'attr_nominal_spaces': ['red wine', 'hard liquor', None, 'mate', 'hard liquor',
                                                     'mate']})


# Path: sksurv/io/arffread.py
# def loadarff(filename):
#     """Load ARFF file
# 
#     Parameters
#     ----------
#     filename : string
#         Path to ARFF file
# 
#     Returns
#     -------
#     data_frame : :class:`pandas.DataFrame`
#         DataFrame containing data of ARFF file
#     """
#     data, meta = scipy_loadarff(filename)
#     return _to_pandas(data, meta)
# 
# Path: sksurv/io/arffwrite.py
# def writearff(data, filename, relation_name=None, index=True):
#     """Write ARFF file
# 
#     Parameters
#     ----------
#     data : :class:`pandas.DataFrame`
#         DataFrame containing data
# 
#     filename : string or file-like object
#         Path to ARFF file or file-like object. In the latter case,
#         the handle is closed by calling this function.
# 
#     relation_name : string, optional, default: "pandas"
#         Name of relation in ARFF file.
# 
#     index : boolean, optional, default: True
#         Write row names (index)
#     """
#     if isinstance(filename, str):
#         fp = open(filename, 'w')
# 
#         if relation_name is None:
#             relation_name = os.path.basename(filename)
#     else:
#         fp = filename
# 
#         if relation_name is None:
#             relation_name = "pandas"
# 
#     try:
#         data = _write_header(data, fp, relation_name, index)
#         fp.write("\n")
#         _write_data(data, fp)
#     finally:
#         fp.close()


# Path: tests/test_io.py
from collections import OrderedDict
from io import StringIO
from sksurv.io import loadarff, writearff
import numpy
import pandas
import pandas.testing as tm
import pytest

                     '"2014-10-31 14:13:01"\n',
                     '"2004-03-13 19:49:31"\n',
                     '"1998-12-06 09:10:11"\n']


def test_loadarff_dataframe():
    contents = "".join(EXPECTED_NO_QUOTES)
    with StringIO(contents) as fp:
        actual_df = loadarff(fp)

    expected_df = pandas.DataFrame.from_dict(OrderedDict(
        [("attr_nominal",
          pandas.Series(pandas.Categorical.from_codes(
              [1, 2, 0, -1, 2, 1],
              ["beer", "water", "wine"]))),
         ("attr_nominal_spaces",
          pandas.Series(pandas.Categorical.from_codes(
              [2, 0, -1, 1, 0, 1],
              ['hard liquor', 'mate', 'red wine'])))
         ]
    ))

    tm.assert_frame_equal(expected_df, actual_df, check_exact=True)


def test_writearff_nominal(temp_file):
    data = pandas.DataFrame({'attr_nominal': ['water', 'wine', 'beer', None, 'wine', 'water'],
                             'attr_nominal_spaces': ['red wine', 'hard liquor', None, 'mate', 'hard liquor',
                                                     'mate']})


    writearff(data, temp_file, relation_name='test_nominal', index=False)