# Path: segeval/data/tsv.py
# def input_linear_mass_tsv(filepath, delimiter=DEFAULT_DELIMITER):
#     '''
#     Takes a file path.  Returns segmentation mass codings as a :class:`Dataset`.
# 
#     :param filepath: path to the mass file containing segment mass codings.
#     :param delimiter:    the delimiter used when reading a TSV file (by default,
#                          a tab, but it can also be a comma, whitespace, etc.
#     :type filepath: str
#     :type delimiter: str
#     '''
# 
#     from segeval.data import Dataset, name_from_filepath
#     # List version of file
#     header = []
#     dataset = Dataset()
#     item = name_from_filepath(filepath)
#     dataset[item] = dict()
#     # Open file
#     with open(filepath, 'rU') as csv_file:
#         # Read in file
#         reader = csv.reader(csv_file, delimiter=delimiter)
#         for i, row in enumerate(reader):
#             # Read annotators from header
#             if i is 0:
#                 for col_name in row[1:]:
#                     header.append(col_name)
#             # Read data
#             else:
#                 coder = None
#                 for j, col in enumerate(row):
#                     # Skip the first col
#                     if j is 0:
#                         coder = str(col)
#                         dataset[item][coder] = list()
#                     else:
#                         dataset[item][coder].append(int(col))
#                 dataset[item][coder] = tuple(dataset[item][coder])
# 
#     return dataset
# 
# def input_linear_positions_tsv(filepath, delimiter=DEFAULT_DELIMITER):
#     '''
#     Takes a file path.  Returns segmentation mass codings as a :class:`Dataset`.
# 
#     :param filepath: path to the mass file containing segment position
#                          codings.
#     :param delimiter:    the delimiter used when reading a TSV file (by default,
#                          a tab, but it can also be a comma, whitespace, etc.
#     :type filepath: str
#     :type delimiter: str
# 
#     .. deprecated:: 1.0
# 
#     .. warning:: This I/O function is for legacy files only and will be removed
#         in later versions.
#     '''
#     dataset = input_linear_mass_tsv(filepath, delimiter)
#     # Convert each segment position to masses
#     for item, coder_positions in dataset.items():
#         for coder, positions in coder_positions.items():
#             dataset[item][coder] = convert_positions_to_masses(positions)
#     # Return
#     return dataset
# 
# Path: segeval/data/samples.py
# HEARST_1997_STARGAZER = Dataset(
#     {'stargazer':
#         {'1': (2,3,3,1,3,6,3),
#          '2': (2,8,2,4,2,3),
#          '3': (2,1,2,3,1,3,1,3,2,2,1),
#          '4': (2,1,4,1,1,3,1,4,3,1),
#          '5': (3,2,4,3,5,4),
#          '6': (2,3,4,2,2,5,3),
#          '7': (2,3,2,2,3,1,3,2,3)}
#      })


import unittest
import os
from segeval.data.tsv import (input_linear_mass_tsv, input_linear_positions_tsv)
from segeval.data.samples import HEARST_1997_STARGAZER

.. moduleauthor:: Chris Fournier <chris.m.fournier@gmail.com>
'''
from __future__ import absolute_import


class TestTsv(unittest.TestCase):

    '''
    Test data merge functions.
    '''

    test_data_dir = os.path.split(__file__)[0]

    def test_input_linear_mass_tsv(self):
        '''
        Test mass TSV file input.
        '''
        tsv_file = os.path.join(self.test_data_dir, 'hearst1997.tsv')
        dataset = input_linear_mass_tsv(tsv_file)
        self.assertEqual(dataset['hearst1997'],
                         HEARST_1997_STARGAZER['stargazer'])

    def test_input_linear_positions_tsv(self):
        '''
        Test position TSV file input.
        '''
        tsv_file = os.path.join(self.test_data_dir, 'hearst1997_positions.csv')
        dataset = input_linear_positions_tsv(tsv_file, delimiter=',')
        self.assertEqual(dataset['hearst1997_positions'],

# Path: segeval/data/tsv.py
# def input_linear_mass_tsv(filepath, delimiter=DEFAULT_DELIMITER):
#     '''
#     Takes a file path.  Returns segmentation mass codings as a :class:`Dataset`.
# 
#     :param filepath: path to the mass file containing segment mass codings.
#     :param delimiter:    the delimiter used when reading a TSV file (by default,
#                          a tab, but it can also be a comma, whitespace, etc.
#     :type filepath: str
#     :type delimiter: str
#     '''
# 
#     from segeval.data import Dataset, name_from_filepath
#     # List version of file
#     header = []
#     dataset = Dataset()
#     item = name_from_filepath(filepath)
#     dataset[item] = dict()
#     # Open file
#     with open(filepath, 'rU') as csv_file:
#         # Read in file
#         reader = csv.reader(csv_file, delimiter=delimiter)
#         for i, row in enumerate(reader):
#             # Read annotators from header
#             if i is 0:
#                 for col_name in row[1:]:
#                     header.append(col_name)
#             # Read data
#             else:
#                 coder = None
#                 for j, col in enumerate(row):
#                     # Skip the first col
#                     if j is 0:
#                         coder = str(col)
#                         dataset[item][coder] = list()
#                     else:
#                         dataset[item][coder].append(int(col))
#                 dataset[item][coder] = tuple(dataset[item][coder])
# 
#     return dataset
# 
# def input_linear_positions_tsv(filepath, delimiter=DEFAULT_DELIMITER):
#     '''
#     Takes a file path.  Returns segmentation mass codings as a :class:`Dataset`.
# 
#     :param filepath: path to the mass file containing segment position
#                          codings.
#     :param delimiter:    the delimiter used when reading a TSV file (by default,
#                          a tab, but it can also be a comma, whitespace, etc.
#     :type filepath: str
#     :type delimiter: str
# 
#     .. deprecated:: 1.0
# 
#     .. warning:: This I/O function is for legacy files only and will be removed
#         in later versions.
#     '''
#     dataset = input_linear_mass_tsv(filepath, delimiter)
#     # Convert each segment position to masses
#     for item, coder_positions in dataset.items():
#         for coder, positions in coder_positions.items():
#             dataset[item][coder] = convert_positions_to_masses(positions)
#     # Return
#     return dataset
# 
# Path: segeval/data/samples.py
# HEARST_1997_STARGAZER = Dataset(
#     {'stargazer':
#         {'1': (2,3,3,1,3,6,3),
#          '2': (2,8,2,4,2,3),
#          '3': (2,1,2,3,1,3,1,3,2,2,1),
#          '4': (2,1,4,1,1,3,1,4,3,1),
#          '5': (3,2,4,3,5,4),
#          '6': (2,3,4,2,2,5,3),
#          '7': (2,3,2,2,3,1,3,2,3)}
#      })


# Path: segeval/data/tsv_test.py
import unittest
import os
from segeval.data.tsv import (input_linear_mass_tsv, input_linear_positions_tsv)
from segeval.data.samples import HEARST_1997_STARGAZER


.. moduleauthor:: Chris Fournier <chris.m.fournier@gmail.com>
'''
from __future__ import absolute_import


class TestTsv(unittest.TestCase):

    '''
    Test data merge functions.
    '''

    test_data_dir = os.path.split(__file__)[0]

    def test_input_linear_mass_tsv(self):
        '''
        Test mass TSV file input.
        '''
        tsv_file = os.path.join(self.test_data_dir, 'hearst1997.tsv')
        dataset = input_linear_mass_tsv(tsv_file)
        self.assertEqual(dataset['hearst1997'],
                         HEARST_1997_STARGAZER['stargazer'])

    def test_input_linear_positions_tsv(self):
        '''
        Test position TSV file input.
        '''
        tsv_file = os.path.join(self.test_data_dir, 'hearst1997_positions.csv')
        dataset = input_linear_positions_tsv(tsv_file, delimiter=',')
        self.assertEqual(dataset['hearst1997_positions'],

                         HEARST_1997_STARGAZER['stargazer'])