def getSplit(proband, fileName):
    fu = FileUtil()
    filePath = "%s%s/%s" % (experimentDir, proband, fileName)
    dto = fu.getDto(filePath)
    s1, s2, s3, s4 = _getStartStopPercent(dto)
    awake = fu.getPartialDto(dto, s1, s2)
    drowsy = fu.getPartialDto(dto, s3, s4)
    return [awake, drowsy]
class TestFileUtil(BaseTest):
    def setUp(self):
        self.util = FileUtil()
        self.dto = self._readData()
        self.mneObj = self._getMNEObject(self.dto)

    def _getMNEObject(self, dto):
        return MNEUtil().createMNEObjectFromEEGDto(dto)

    def _readData(self):
        return self.util.getDto(self.getData1024CSV())

    def test_isCSVFile(self):
        self.assertTrue(self.util.isCSVFile("path/to/sth.csv"))

        self.assertFalse(self.util.isCSVFile("path/.csv/sth.txt"))
        self.assertFalse(self.util.isCSVFile("path/to/sth.raw.fif"))
        self.assertFalse(self.util.isCSVFile("path/to/sth.ica.fif"))

    def test_getDto(self):
        dto = self.util.getDto(self.getData1024CSV())
        dto2 = self.util.getDto(self.getData1024FIF())

        self.assertAlmostEqual(dto.samplingRate, dto2.samplingRate, delta=0.1)
        assert_array_equal(dto.getEEGHeader(), dto2.getEEGHeader())
        assert_array_equal(dto.getEEGData(), dto2.getEEGData())

    def test_getDto_dtoinput(self):
        dto = self.util.getDto(self.getData1024CSV())
        dto2 = self.util.getDto(dto)

        self.assertTrue(dto is dto2)
        self.assertAlmostEqual(dto.samplingRate, dto2.samplingRate, delta=0.1)
        assert_array_equal(dto.getEEGHeader(), dto2.getEEGHeader())
        assert_array_equal(dto.getEEGData(), dto2.getEEGData())

    def test_convertMNEToTableDto(self):
        dto2 = self.util.convertMNEToTableDto(self.mneObj)

        self.assertListEqual([TIMESTAMP_STRING] + self.dto.getEEGHeader() +
                             self.dto.getGyroHeader(), dto2.getHeader())
        assert_array_equal(self.dto.getEEGData(), dto2.getEEGData())
        self.assertEqual(self.dto.filePath, dto2.filePath)
        self.assertTrue(dto2.hasEEGData)

    def test_getMNEFileName_given(self):
        filePath = "path/to/sth"
        mneFilePath = self.util.getMNEFileName(self.mneObj, filePath)
        self.assertEqual(filePath + ".raw.fif", mneFilePath)

    def test_getMNEFileName_givenCSV(self):
        filePath = "path/to/sth"
        mneFilePath = self.util.getMNEFileName(self.mneObj, filePath + ".csv")
        self.assertEqual(filePath + ".raw.fif", mneFilePath)

    def test_getMNEFileName_givenCSVmiddle(self):
        filePath = "path/.csv/sth"
        mneFilePath = self.util.getMNEFileName(self.mneObj, filePath)
        self.assertEqual(filePath + ".raw.fif", mneFilePath)

    def test_getMNEFileName_extension(self):
        filePath = "path/to/sth"
        self.mneObj.info["description"] = filePath
        mneFilePath = self.util.getMNEFileName(self.mneObj, None)
        self.assertEqual(filePath + ".raw.fif", mneFilePath)

    def test_getMNEFileName_CSV(self):
        filePath = "path/to/sth"
        self.mneObj.info["description"] = filePath + ".csv"
        mneFilePath = self.util.getMNEFileName(self.mneObj, None)
        self.assertEqual(filePath + ".raw.fif", mneFilePath)

    def test_getPartialDto(self):
        end = len(self.dto) / 2
        copyDto = self.util.getPartialDto(self.dto, 0, end)

        self.assertFalse(self.dto is copyDto)
        self.assertFalse(self.dto.header is copyDto.header)
        assert_array_equal(self.dto.header, copyDto.header)
        self.assertTrue(self.dto.filePath == copyDto.filePath)
        self.assertTrue(self.dto.samplingRate == copyDto.samplingRate)

        partData = copyDto.data
        fullData = self.dto.data

        self.assertFalse(fullData is partData)
        assert_array_equal(fullData[0:end, :], partData)
        self.assertEqual(len(partData), end)
        self.assertTrue(fullData.shape[0] > partData.shape[0])
        self.assertTrue(fullData.shape[1] == partData.shape[1])
class DummyDataSource(object):
    def __init__(self, filePath=None, infinite=True):
        '''
        Reads data from filePath or ./../../data/dummy_4096.csv and builds the data structure
        '''
        self.filePath = self.setFilePath(filePath)
        self.infinite = infinite
        self.fileUtil = FileUtil()
        self.hasMore = False
        self.data = []
        self.len = 0
        self.index = 0

    def setFilePath(self, filePath):
        if filePath == None:
            return [scriptPath + "/../../../data/dummy_4096.csv"]
        elif type(filePath) != list:
            return [filePath]
        else:
            return filePath

    def convert(self):
        for filePath in self.filePath:
            dto = self.fileUtil.getDto(filePath)
            self._readHeader(dto)
            self._readRawData(dto)
            self.samplingRate = dto.getSamplingRate()
            self._buildDataStructure()
        logging.info("%s: Using %d dummy datasets" %
                     (self.__class__.__name__, self.len))

    def _readHeader(self, dto):
        self.header = dto.getHeader()
        self.fields = dto.getEEGHeader() + dto.getGyroHeader()
        self._hasQuality()

    def _hasQuality(self):
        self.hasQuality = all([(("Q" + field) in self.header)
                               for field in self.fields])

    def _readRawData(self, dto):
        self.rawData = dto.getData()
        self.len += len(self.rawData)
        if self.len > 0:
            self.hasMore = True

    def dequeue(self):
        pass

    def _getNextIndex(self):
        self.index += 1
        if self.index >= len(self.data) and not self.infinite:
            self.hasMore = False
        self.index %= self.len

    def _buildDataStructure(self):
        pass

    def close(self):
        pass

    def stop(self):
        pass