def test_nextAliases(self): dataManager = DataManager("manager", isTimeSeries=True) dataManager.addDataEntry("entry1", 1) dataManager.addDataEntry("entry2", 3) dataManager.addDataAlias("entries", [('entry1', ...), ('entry2', ...)]) data1 = dataManager.createDataObject([10]) dataMat = np.array(range(0, 11)) dataMat.resize(11, 1) dataMatNext = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) dataMatLast = np.array([0, 0, 1, 2, 3, 4, 5, 6, 7, 8]) dataMatNext.resize(10, 1) dataMatLast.resize(10, 1) data1.setDataEntry("allEntry1", ..., dataMat) nextEntry1 = data1.getDataEntry("nextEntry1", ...) allDataEntry1 = data1.getDataEntry("allEntry1", ...) lastEntry1 = data1.getDataEntry("lastEntry1", ...) self.assertTrue((nextEntry1 == dataMatNext).all()) self.assertTrue((allDataEntry1 == dataMat).all()) self.assertTrue((lastEntry1 == dataMatLast).all()) allDataEntries = data1.getDataEntry('allEntries', slice(0, 3)) allDataEntriesMat = np.array([[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0]]) self.assertTrue((allDataEntries == allDataEntriesMat).all())
def test_dataSuffix(self): settings = SettingsManager.getDefaultSettings() settings.clean() dataManager = DataManager("manager") settings.pushSuffixStack('Agent1') dataManager.addDataEntry('parameters1', 1) dataManager.addDataEntry('parameters2', 1) dataManager.addDataAlias('parameters', [('parameters1', ...), ('parameters2', ...)]) self.assertTrue(dataManager.isDataEntry("parameters1Agent1")) self.assertFalse(dataManager.isDataEntry("parameters1")) data = dataManager.createDataObject(10) data.setDataEntry('parameters1Agent1', ..., np.ones((10, 1)) * 5) dummyManipulator = TestSuffixManipulator(dataManager) data[...] >> dummyManipulator.dummyFunction >> data self.assertTrue( np.all( data.getDataEntry("parameters2Agent1") == data.getDataEntry("parameters1Agent1") + 10)) self.assertTrue( np.all( data.getDataEntry("parametersAgent1") == np.hstack(( data.getDataEntry("parameters1Agent1"), data.getDataEntry("parameters2Agent1"))))) settings.popSuffixStack()
def test_get_alias_data(self): dataManager = DataManager('episodes') dataManager.addDataEntry('parameters', 5) dataManager.addDataEntry('contexts', 5) dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 2))]) dataManager.addDataAlias('twoAlias', [('parameters', slice(0, 2)), ('contexts', slice(2, 5))]) myData = dataManager.createDataObject([10, 5, 1]) myData.dataStructure['parameters'] = np.ones((10, 5)) myData.dataStructure['contexts'] = np.ones((10, 5)) * 2 paramAlias = myData.dataStructure['parameterAlias'] paramAlias[:] = np.ones(2) * 3 paramAlias[0][1] = 10 myData.dataStructure['parameterAlias'] = paramAlias self.assertEqual(myData.dataStructure['parameters'][0][1], 10) self.assertEqual(myData.dataStructure['parameters'][0][2], 1) self.assertEqual(myData.dataStructure['parameters'][3][1], 3) self.assertEqual(myData.dataStructure['parameters'][5][3], 1) twoAlias = myData.dataStructure['twoAlias'] twoAlias[4] = np.ones(5) * 4 twoAlias[5] = np.ones(5) * 5 twoAlias[6] = np.ones(5) * 6 twoAlias[-1] = np.ones(5) * 9 myData.dataStructure['twoAlias'] = twoAlias self.assertEqual(myData.dataStructure['twoAlias'][0][3], 2) self.assertEqual(myData.dataStructure['twoAlias'][-1][3], 9) self.assertEqual(myData.dataStructure['twoAlias'][4][2], 4) self.assertEqual(myData.dataStructure['twoAlias'][5][2], 5) self.assertEqual(myData.dataStructure['parameters'][0][0], 3) self.assertEqual(myData.dataStructure['parameters'][0][1], 10) self.assertEqual(myData.dataStructure['parameters'][0][2], 1) self.assertEqual(myData.dataStructure['parameters'][4][2], 1) self.assertEqual(myData.dataStructure['parameters'][5][2], 1) self.assertEqual(myData.dataStructure['contexts'][1][3], 2) self.assertEqual(myData.dataStructure['contexts'][6][3], 6) self.assertEqual(myData.dataStructure['contexts'][-1][3], 9) self.assertEqual(myData.dataStructure['contexts'][4][0], 2) self.assertEqual(myData.dataStructure['contexts'][5][1], 2) self.assertEqual(myData.dataStructure['contexts'][4][2], 4) self.assertEqual(myData.dataStructure['contexts'][5][2], 5)
def test_restrictedEntry(self): dataManager = DataManager("manager", isTimeSeries=False) dataManager.addDataEntry("entry1", 1) dataManager.addDataEntry("entry2", 3, isPeriodic=[True, False, True]) dataManager.addDataAlias("entries", [('entry1', ...), ('entry2', ...)]) data = dataManager.createDataObject(2) data.setDataEntry('entries', ..., np.array([[0, 8, 0, 2], [0, 2, 0, -8]])) entryRestricted = data.getDataEntry('entries_restricted') self.assertTrue( sum( sum( np.abs(entryRestricted - np.array([[0, 1, 0, 1], [0, 1, 0, -1]])))) < 0.001)
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)
def test_PeriodicEntry(self): dataManager = DataManager("manager", isTimeSeries=False) dataManager.addDataEntry("entry1", 1) dataManager.addDataEntry("entry2", 3, isPeriodic=[True, False, True]) dataManager.addDataAlias("entries", [('entry1', ...), ('entry2', ...)]) periodTest = dataManager.getPeriodicity('entries') self.assertTrue(periodTest == [False, True, False, True]) data = dataManager.createDataObject(2) data.setDataEntry('entries', ..., np.array([[0, 8, 0, 2], [0, 2, 0, -8]])) entryPeriodic = data.getDataEntry('entries_periodic') self.assertTrue( sum( sum( np.abs(entryPeriodic - np.array([[0, 1.71681469, 0, 2], [0, 2, 0, 4.56637061]])))) < 0.001)
def test_addDataAlias(self): dataManager = DataManager('episodes') self.assertIsInstance(dataManager.dataAliases, dict) dataManager.addDataEntry('parameters', 5, -1, 1) dataManager.addDataEntry('contexts', 5, -1, 1) # Add alias dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 1))]) self.assertEqual(dataManager.dataAliases['parameterAlias'].entryList, [('parameters', slice(0, 1))]) # Replace entry of same alias dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 2))]) self.assertEqual(dataManager.dataAliases['parameterAlias'].entryList, [('parameters', slice(0, 2))]) # Add another entry to alias dataManager.addDataAlias('parameterAlias', [('contexts', ...)]) self.assertEqual(dataManager.dataAliases['parameterAlias'].entryList, [('parameters', slice(0, 2)), ('contexts', ...)]) # Recursive alias dataManager.addDataAlias('aliasToParameterAlias', [('parameterAlias', ...)]) self.assertEqual( dataManager.dataAliases['aliasToParameterAlias'].entryList, [('parameterAlias', ...)]) # Alias cycle dataManager.addDataAlias('badAlias', [('aliasToParameterAlias', ...)]) self.assertRaises(ValueError, dataManager.addDataAlias, 'aliasToParameterAlias', [('badAlias', ...)]) self.assertRaises(ValueError, dataManager.addDataAlias, 'badAlias', [('badAlias', ...)]) self.assertRaises(ValueError, dataManager.addDataAlias, 'contexts', [('contexts', ...)])
def test_addDataEntry_name_conflict(self): dataManager = DataManager('episodes') dataManager.addDataEntry('parameters', 5) dataManager.addDataAlias('conflict', [('parameters', ...)]) self.assertRaises(ValueError, dataManager.addDataEntry, 'conflict', 0)
import time ''' In this example we are going to see how to use aliases. ''' # create data manager dataManager = DataManager('episodes') # add data entries dataManager.addDataEntry('parameters', 5) dataManager.addDataEntry('contexts', 5) # add an alias # parameterAlias points to the first two dimensions of parameters dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 2))]) # it's also possible to create an alias that points to another alias # 'aliasAlias' will now be the aquivalent to 'parameters' dataManager.addDataAlias('aliasAlias', [('parameterAlias', ...), ('contexts', slice(2, 5))]) dataManager.addDataAlias('parametersAliasVertical', [('parameters', slice(0, 2)), ('parameters', slice(2, 4))], useConcatVertical=True) # create the data object myData = dataManager.createDataObject([3]) # set the entries of the 'parameters' entry. This will also affect the alias # that points to 'parameters' parameters = myData[...].parameters parameters[:] = [1, 2, 3, 4, 5]