import btk
import unittest
import _TDDConfigure

class IMUsExtractorTest(unittest.TestCase):
  def test_NoAcquisition(self):
    imuse = btk.btkIMUsExtractor()
    output = imuse.GetOutput()
    output.Update()
    self.assertEqual(output.GetItemNumber(), 0)

  def test_NoIMU(self):
    imuse = btk.btkIMUsExtractor()
    imuse.SetInput(btk.btkAcquisition())
    output = imuse.GetOutput()
    output.Update()
    self.assertEqual(output.GetItemNumber(), 0)

  def test_OneAcquisition(self):
    acq = btk.btkAcquisition()
    acq.Init(0,200,6)
    imu = btk.btkMetaDataCreateChild(acq.GetMetaData(), 'IMU')
    btk.btkMetaDataCreateChild(imu, 'USED', 1)
    btk.btkMetaDataCreateChild(imu, 'LABELS', btk.btkStringArray(1,'Foo'))
    btk.btkMetaDataCreateChild(imu, 'DESCRIPTIONS', btk.btkStringArray(1,'BAR'))
    btk.btkMetaDataCreateChild(imu, 'TYPE', btk.btkIntArray(1,1))
    channels = btk.btkIntArray(6); channels[0] = 1; channels[1] = 2; channels[2] = 3; channels[3] = 4; channels[4] = 5; channels[5] = 6
    btk.btkMetaDataCreateChild(imu, 'CHANNEL', channels, 6)
    imuse = btk.btkIMUsExtractor()
    imuse.SetInput(acq)
    output = imuse.GetOutput()
    output.Update()
    self.assertEqual(output.GetItemNumber(), 1)
    self.assertEqual(output.GetItem(0).GetLabel(), 'Foo')
    self.assertEqual(output.GetItem(0).GetDescription(), 'BAR')
    self.assertEqual(output.GetItem(0).GetAccelerometerX().GetLabel(), acq.GetAnalog(0).GetLabel())
    self.assertEqual(output.GetItem(0).GetAccelerometerY().GetLabel(), acq.GetAnalog(1).GetLabel())
    self.assertEqual(output.GetItem(0).GetAccelerometerZ().GetLabel(), acq.GetAnalog(2).GetLabel())
    self.assertEqual(output.GetItem(0).GetGyroscopeX().GetLabel(), acq.GetAnalog(3).GetLabel())
    self.assertEqual(output.GetItem(0).GetGyroscopeY().GetLabel(), acq.GetAnalog(4).GetLabel())
    self.assertEqual(output.GetItem(0).GetGyroscopeZ().GetLabel(), acq.GetAnalog(5).GetLabel())

  def test_OneAcquisition_NoDescriptions(self):
    acq = btk.btkAcquisition()
    acq.Init(0,200,6)
    imu = btk.btkMetaDataCreateChild(acq.GetMetaData(), 'IMU')
    btk.btkMetaDataCreateChild(imu, 'USED', 1)
    btk.btkMetaDataCreateChild(imu, 'LABELS', btk.btkStringArray(1,'Foo'))
    btk.btkMetaDataCreateChild(imu, 'TYPE', btk.btkIntArray(1,1))
    channels = btk.btkIntArray(6); channels[0] = 6; channels[1] = 5; channels[2] = 4; channels[3] = 3; channels[4] = 2; channels[5] = 1
    btk.btkMetaDataCreateChild(imu, 'CHANNEL', channels, 6)
    imuse = btk.btkIMUsExtractor()
    imuse.SetInput(acq)
    output = imuse.GetOutput()
    output.Update()
    self.assertEqual(output.GetItemNumber(), 1)
    self.assertEqual(output.GetItem(0).GetLabel(), 'Foo')
    self.assertEqual(output.GetItem(0).GetDescription(), '')
    self.assertEqual(output.GetItem(0).GetAccelerometerX().GetLabel(), acq.GetAnalog(5).GetLabel())
    self.assertEqual(output.GetItem(0).GetAccelerometerY().GetLabel(), acq.GetAnalog(4).GetLabel())
    self.assertEqual(output.GetItem(0).GetAccelerometerZ().GetLabel(), acq.GetAnalog(3).GetLabel())
    self.assertEqual(output.GetItem(0).GetGyroscopeX().GetLabel(), acq.GetAnalog(2).GetLabel())
    self.assertEqual(output.GetItem(0).GetGyroscopeY().GetLabel(), acq.GetAnalog(1).GetLabel())
    self.assertEqual(output.GetItem(0).GetGyroscopeZ().GetLabel(), acq.GetAnalog(0).GetLabel())

  def test_OneAcquisition_NoLabelsAndDescriptions(self):
    acq = btk.btkAcquisition()
    acq.Init(0,200,6)
    imu = btk.btkMetaDataCreateChild(acq.GetMetaData(), 'IMU')
    btk.btkMetaDataCreateChild(imu, 'USED', 1)
    btk.btkMetaDataCreateChild(imu, 'TYPE', btk.btkIntArray(1,1))
    channels = btk.btkIntArray(6); channels[0] = 6; channels[1] = 5; channels[2] = 4; channels[3] = 3; channels[4] = 2; channels[5] = 1
    btk.btkMetaDataCreateChild(imu, 'CHANNEL', channels, 6)
    imuse = btk.btkIMUsExtractor()
    imuse.SetInput(acq)
    output = imuse.GetOutput()
    output.Update()
    self.assertEqual(output.GetItemNumber(), 1)
    self.assertEqual(output.GetItem(0).GetLabel(), 'IMU #1')