Exemplo n.º 1
0
    def test_subDataManager(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        self.assertIs(dataManager.subDataManager, subDataManager)
        self.assertIs(dataManager.subDataManager.subDataManager,
                      subSubDataManager)
Exemplo n.º 2
0
    def test_getSubDataManagerForDepth(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')
        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        self.assertEqual(dataManager.getDataManagerForLevel(0), dataManager)
        self.assertEqual(dataManager.getDataManagerForLevel(0), dataManager)
        self.assertEqual(dataManager.getDataManagerForLevel(0), dataManager)
        self.assertEqual(dataManager.getDataManagerForLevel(1), subDataManager)
        self.assertEqual(dataManager.getDataManagerForLevel(2),
                         subSubDataManager)
        self.assertEqual(dataManager.getDataManagerForLevel(3), None)
Exemplo n.º 3
0
    def test_getDataEntryDeepCopy(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        dataManager.addDataEntry('parameters', 5)
        subDataManager.addDataEntry('states', 1)
        subSubDataManager.addDataEntry('subActions', 2, -10, 10)

        myData = dataManager.createDataObject([1, 1, 1])

        # set the data for all subActions of all episodes, steps and subSteps
        myData.setDataEntry('subActions', [], np.ones((1, 2)))

        self.assertTrue((myData.dataStructure['steps'][0]['subSteps'][0]
                         ['subActions'][0] == np.array([1, 1])).all())

        data1 = myData.getDataEntry('subActions', [])
        data2 = myData.getDataEntry('subActions', [], True)
        data3 = myData.getDataEntry('subActions', [], False)
        data4 = myData.getDataEntry('subActions', [], False)

        self.assertTrue((data1 == data2).all())
        self.assertTrue((data2 == data3).all())
        self.assertTrue((data3 == data4).all())

        data2[0, 1] = 7  # This should NOT have any impact on any other data

        self.assertTrue((data2 != data1).any())
        self.assertTrue((data2 != data3).any())
        self.assertTrue((data2 != data4).any())

        data5 = myData.getDataEntry('subActions', [])

        data3[0, 0] = 8  # This MAY impact the data in the data structure,
        # but should not impact data1 or data2

        self.assertTrue((data5 == data1).all())
        self.assertTrue((data3 != data1).any())
        self.assertTrue((data3 == data4).any())

        myData.setDataEntry('subActions', [],
                            data2)  # still no impact on data1
        self.assertTrue((data5 == data1).all())
        self.assertTrue(((myData.getDataEntry('subActions', [])) == [1,
                                                                     7]).all())
Exemplo n.º 4
0
def createTestManager():
    dataManager = DataManager('episodes')
    subDataManager = DataManager('steps')
    subSubDataManager = DataManager('subSteps')

    dataManager.subDataManager = subDataManager
    subDataManager.subDataManager = subSubDataManager

    dataManager.addDataEntry('parameters', 5, -100, 100)
    dataManager.addDataEntry('contexts', 1, -100, 100)
    subDataManager.addDataEntry('states', 1, -100, 100)
    subDataManager.addDataEntry('actions', 2)
    subSubDataManager.addDataEntry('subStates', 1)
    subSubDataManager.addDataEntry('subActions', 2)
    return dataManager
Exemplo n.º 5
0
    def test_reserveStorage(self):
        dataManager = DataManager('episodes')
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataEntry('contexts', 2)

        subDataManager = DataManager('steps')
        subDataManager.addDataEntry('states', 2)
        subDataManager.addDataEntry('actions', 2)

        dataManager.subDataManager = subDataManager

        data = dataManager.createDataObject([100, 20])

        data.reserveStorage([20, 20])

        self.assertEqual(data.dataStructure['contexts'].shape[0], 20)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 20)

        for i in range(0, 20):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             20)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 20)

        data.reserveStorage([50, 100])

        self.assertEqual(data.dataStructure['contexts'].shape[0], 50)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 50)

        for i in range(0, 50):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             100)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 100)

        data.reserveStorage([50, 0])

        self.assertEqual(data.dataStructure['contexts'].shape[0], 50)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 50)

        for i in range(0, 50):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             0)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 0)

        data.reserveStorage(15, ...)

        self.assertEqual(data.dataStructure['contexts'].shape[0], 50)
        self.assertEqual(data.dataStructure['parameters'].shape[0], 50)

        for i in range(0, 50):
            self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0],
                             15)
            self.assertEqual(
                data.dataStructure['steps'][i]['actions'].shape[0], 15)
Exemplo n.º 6
0
    def test_getDataObject(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataEntry('contexts', 2)
        subDataManager.addDataEntry('states', 1)
        subDataManager.addDataEntry('actions', 2)
        subSubDataManager.addDataEntry('subStates', 1)
        subSubDataManager.addDataEntry('subActions', 2)

        myData = dataManager.createDataObject([10, 5, 1])

        self.assertTrue(dataManager.finalized)
        self.assertEqual(len(myData.dataStructure['parameters']), 10)
        self.assertEqual(len(myData.dataStructure['contexts']), 10)
        self.assertEqual(len(myData.dataStructure['parameters'][0]), 5)
        self.assertEqual(len(myData.dataStructure['parameters'][9]), 5)
        self.assertEqual(len(myData.dataStructure['contexts'][0]), 2)
        self.assertEqual(len(myData.dataStructure['contexts'][9]), 2)
        self.assertEqual(len(myData.dataStructure['steps']), 10)
        self.assertEqual(len(myData.dataStructure['steps'][0]), 3)
        self.assertEqual(len(myData.dataStructure['steps'][0]['states']), 5)
        self.assertEqual(len(myData.dataStructure['steps'][0]['states'][0]), 1)
        self.assertEqual(len(myData.dataStructure['steps'][0]['actions']), 5)
        self.assertEqual(len(myData.dataStructure['steps'][0]['actions'][0]),
                         2)
        self.assertEqual(len(myData.dataStructure['steps'][0]['actions'][1]),
                         2)
        self.assertEqual(len(myData.dataStructure['steps'][0]['subSteps']), 5)
        self.assertEqual(
            len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']),
            1)
        self.assertEqual(
            len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']),
            1)
        self.assertEqual(
            len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']
                [0]), 2)
Exemplo n.º 7
0
def createTestManagerSteps():
    dataManager = DataManager('episodes')
    dataManager.addDataEntry('contexts', 2)
    subDataManager = DataManager('steps', isTimeSeries=True)

    dataManager.subDataManager = subDataManager

    subDataManager.addDataEntry('states', 1, -100, 100)
    subDataManager.addDataEntry('actions', 1)
    subDataManager.addDataEntry('rewards', 1)

    return dataManager
Exemplo n.º 8
0
    def test_getNumElementsForIndex(self):
        manager = DataUtil.createTestManager()
        data = manager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElementsForIndex(0), 3)
        self.assertEqual(data.getNumElementsForIndex(0, [...]), 3)
        self.assertEqual(data.getNumElementsForIndex(1), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., slice(0, 2)]), 6)
        self.assertEqual(
            data.getNumElementsForIndex(1,
                                        [slice(2, 3), slice(0, 2)]), 2)
        self.assertEqual(data.getNumElementsForIndex(2), 60)
        self.assertEqual(
            data.getNumElementsForIndex(2, ...),
            60)  # test implicit cast to array of second argument (deprecated)
        self.assertEqual(data.getNumElementsForIndex(2, [...]),
                         60)  # this should be the standart way
        self.assertEqual(data.getNumElementsForIndex(2, [..., ...]), 60)
        self.assertEqual(data.getNumElementsForIndex(2, [..., ..., ...]), 60)
        self.assertEqual(
            data.getNumElementsForIndex(2, [..., ..., slice(0, 2)]), 24)
        self.assertEqual(
            data.getNumElementsForIndex(
                2, [..., slice(1, 4), slice(0, 2)]), 18)
        self.assertEqual(
            data.getNumElementsForIndex(
                2, [slice(0, 1), slice(2, 3),
                    slice(0, 2)]), 2)

        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        dataManager.subDataManager = subDataManager
        dataManager.addDataEntry('parameters', 5)
        dataManager.addDataAlias('pAlias', [('parameters', ...)])
        dataManager.addDataEntry('contexts', 2)
        data = dataManager.createDataObject([3, 4, 5])

        self.assertEqual(data.getNumElementsForIndex(0), 3)
        self.assertEqual(data.getNumElementsForIndex(0, [...]), 3)
        self.assertEqual(data.getNumElementsForIndex(1), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12)
        self.assertEqual(data.getNumElementsForIndex(1, [slice(2, 3)]), 4)
Exemplo n.º 9
0
    def test_mergeDataFront(self):
        dataManager = DataManager("manager")
        subDataManager = DataManager("subDataManager")
        dataManager.subDataManager = subDataManager
        dataManager.addDataEntry("entry1", 2)
        subDataManager.addDataEntry("entry2", 3)

        data1 = dataManager.createDataObject([10, 10])
        data1.setDataEntry("entry1", ..., np.ones((10, 2)))
        data1.setDataEntry("entry2", ..., np.ones((100, 3)))

        data2 = dataManager.createDataObject([5, 5])
        data2.setDataEntry("entry1", ..., np.zeros((5, 2)))
        data2.setDataEntry("entry2", ..., np.zeros((25, 3)))

        data1.mergeData(data2, True)

        self.assertEqual(data1.getNumElements("entry1"), 15)
        self.assertEqual(len(data1.dataStructure["subDataManager"]), 15)
        self.assertTrue((data1.getDataEntry("entry1", ...) == np.vstack(
            (np.zeros((5, 2)), np.ones((10, 2))))).all())
Exemplo n.º 10
0
from pypost.data import DataManagerTimeSeries
from pypost.data import DataType

import numpy as np
'''
In this example we are going to see how to use aliases.
'''

# create data manager
dataManager = DataManager('episodes')
subDataManager = DataManagerTimeSeries('steps')

subDataManager.addDataEntry('states', 2)
subDataManager.addDataEntry('actions', 3, dataType=DataType.discrete)

dataManager.subDataManager = subDataManager

# create data object with 10 episodes
data = dataManager.createDataObject(10)

# fill episodes. we want to use a different number of timesteps for each episode

for i in range(0, 10):
    numTimeSteps = i + 5
    data.reserveStorage(numTimeSteps, [i])

    print('Size of states matrix: ', data[i].states.shape)

    #fill in some dummy actions
    data[i].allStates = np.array(
        np.vstack(
Exemplo n.º 11
0
    def test_getDataEntryDimensions(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        dataManager.addDataEntry('parameters', 5)
        subDataManager.addDataEntry('states', 1)
        subSubDataManager.addDataEntry('subStates', 1)
        subSubDataManager.addDataEntry('subActions', 2)

        myData = dataManager.createDataObject([10, 5, 3])

        self.assertEqual(myData.getDataEntry(['parameters']).shape[0], 10)

        self.assertEqual(
            myData.getDataEntry(['parameters'], [...]).shape[0], 10)

        self.assertEqual(myData.getDataEntry(['parameters'], [1]).shape[0], 1)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [1]).shape[0], 5)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [3]).shape[0], 5)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [..., 1]).shape[0], 10)

        self.assertEqual(
            myData.getDataEntry(['steps', 'states'], [..., ...]).shape[0], 50)

        self.assertEqual(myData.getDataEntry(['steps', 'states']).shape[0], 50)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., ..., ...]).shape[0], 150)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions']).shape[0],
            150)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., ..., 1]).shape[0], 50)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., 1, ...]).shape[0], 30)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., 1]).shape[0], 30)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [..., 1, 1]).shape[0], 10)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, ..., ...]).shape[0], 15)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1]).shape[0], 15)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                1).shape[0], 15)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, ..., 1]).shape[0], 5)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, 1, ...]).shape[0], 3)

        self.assertEqual(
            myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                [1, 1, 1]).shape[0], 1)
Exemplo n.º 12
0
    def test_setgetDataEntry(self):
        dataManager = DataManager('episodes')
        subDataManager = DataManager('steps')
        subSubDataManager = DataManager('subSteps')

        dataManager.subDataManager = subDataManager
        subDataManager.subDataManager = subSubDataManager

        subSubDataManager.addDataEntry('subActions', 2, -10, 10)

        myData = dataManager.createDataObject([10, 5, 3])

        # set the data for all subActions of all episodes, steps and subSteps
        myData.setDataEntry(['steps', 'subSteps', 'subActions'], [],
                            np.ones((150, 2)))

        # all subActions in the 3rd subSteps of all steps of the 2nd episode
        # should have different parameters
        myData.setDataEntry(['steps', 'subSteps', 'subActions'], [1, ..., 2],
                            np.array([[2, 1], [2, 2], [2, 3], [2, 4], [2, 5]]))

        self.assertTrue((myData.dataStructure['steps'][0]['subSteps'][0]
                         ['subActions'][0] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][1]
                         ['subActions'][1] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][9]['subSteps'][4]
                         ['subActions'][2] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][4]
                         ['subActions'][1] == np.array([1, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][0]
                         ['subActions'][2] == np.array([2, 1])).all())

        self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][4]
                         ['subActions'][2] == np.array([2, 5])).all())

        # tests for getDataEntry
        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [0, 0]) == myData.dataStructure['steps'][0]
             ['subSteps'][0]['subActions']).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 1) == np.array([[1, 1], [1, 1], [2,
                                                                  1], [1, 1],
                                                 [1, 1], [2, 2], [1,
                                                                  1], [1, 1],
                                                 [2, 3], [1, 1], [1,
                                                                  1], [2, 4],
                                                 [1, 1], [1, 1], [2,
                                                                  5]])).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [1, ..., 2]) == np.array([[2, 1], [2, 2],
                                                           [2, 3], [2, 4],
                                                           [2, 5]])).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [1, 3, 2]) == np.array([[2, 4]])).all())

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 [2, 3, 2]) == np.array([[1, 1]])).all())

        # all subActions in every second subStep of all steps of the 2nd episode
        # should have different parameters
        myData.setDataEntry(['steps', 'subSteps', 'subActions'],
                            [1, slice(0, 5, 2), 2],
                            np.array([[3, 1], [3, 2], [3, 3]]))

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 1) == np.array([[1, 1], [1, 1], [3,
                                                                  1], [1, 1],
                                                 [1, 1], [2, 2], [1,
                                                                  1], [1, 1],
                                                 [3, 2], [1, 1], [1,
                                                                  1], [2, 4],
                                                 [1, 1], [1, 1], [3,
                                                                  3]])).all())

        # all subActions in the first 3 subSteps of all steps of the 2nd episode
        # should have different parameters
        myData.setDataEntry(['steps', 'subSteps', 'subActions'],
                            [1, slice(0, -2), 2],
                            np.array([[4, 1], [4, 2], [4, 3]]))

        self.assertTrue(
            (myData.getDataEntry(['steps', 'subSteps', 'subActions'],
                                 1) == np.array([[1, 1], [1, 1], [4,
                                                                  1], [1, 1],
                                                 [1, 1], [4, 2], [1,
                                                                  1], [1, 1],
                                                 [4, 3], [1, 1], [1,
                                                                  1], [2, 4],
                                                 [1, 1], [1, 1], [3,
                                                                  3]])).all())

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, 2, slice(0, 1)], np.array([[5, 1], [5, 2]]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'], [1, ..., 0],
                          np.array([[5, 1], [5, 2], [5, 3]]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, slice(0, -2), 2], np.array([[5, 1], [5, 2]]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, slice(0, -2), 2], np.array([]))

        self.assertRaises(ValueError, myData.setDataEntry,
                          ['steps', 'subSteps', 'subActions'],
                          [1, slice(0, -2), 2],
                          np.array([[5, 1], [5, 2], [5, 3], [5, 4]]))