def setUp(self): self.basedir = os.path.realpath(tempfile.mkdtemp()) self.stgFreq = FreqOutput("./sampleData") self.tSteps = [3, 6, 9, 12, 15] self.timeVals = [0.0125, 0.0375, 0.0625, 0.0875, 0.1125] self.VRMSVals = [3, 3.2, 3.8, 3.4, 2.6]
class StgFreqTestCase(unittest.TestCase): def setUp(self): self.basedir = os.path.realpath(tempfile.mkdtemp()) self.stgFreq = FreqOutput("./sampleData") self.tSteps = [3, 6, 9, 12, 15] self.timeVals = [0.0125, 0.0375, 0.0625, 0.0875, 0.1125] self.VRMSVals = [3, 3.2, 3.8, 3.4, 2.6] def tearDown(self): shutil.rmtree(self.basedir) def test_getHeaders(self): headers = self.stgFreq.getHeaders() self.assertEqual(headers, ['Timestep','Time','VRMS']) self.assertEqual(self.stgFreq.headerColMap, {'Timestep':0, 'Time':1, 'VRMS':2}) def test_getAllRecords(self): records = self.stgFreq.getAllRecords() for ii in range(0,5): self.assertEqual(records[ii][0], self.tSteps[ii]) self.assertAlmostEqual(records[ii][1], self.timeVals[ii]) self.assertAlmostEqual(records[ii][2], self.VRMSVals[ii]) def test_getValueAtStep(self): self.stgFreq.populateFromFile() tI = 2 tStep = self.tSteps[tI] time4 = self.stgFreq.getValueAtStep("Time", tStep) self.assertAlmostEqual(time4, self.timeVals[tI]) VRMS2 = self.stgFreq.getValueAtStep("VRMS", tStep) self.assertAlmostEqual(VRMS2, self.VRMSVals[tI]) def test_getRecordDictAtStep(self): self.stgFreq.populateFromFile() tI = 2 tStep = self.tSteps[tI] recordDict = self.stgFreq.getRecordDictAtStep(tStep) self.assertEqual(recordDict, {'Timestep':tStep,'Time':self.timeVals[tI], "VRMS":self.VRMSVals[tI]}) def test_finalStep(self): self.assertEqual(self.tSteps[-1], self.stgFreq.finalStep()) def test_getValuesArray(self): valArray = self.stgFreq.getValuesArray('Time') for ii, val in enumerate(valArray): self.assertAlmostEqual(val, self.timeVals[ii]) def test_getTimeStepsArray(self): timeStepsArray = self.stgFreq.getTimeStepsArray() self.assertEqual(self.tSteps, timeStepsArray) def test_getMin(self): for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: minVal, minStep = self.stgFreq.getMin(headerName) calcMin = min(testList) calcIndex = testList.index(calcMin) self.assertAlmostEqual(minVal, calcMin) self.assertEqual(minStep, self.tSteps[calcIndex]) def test_getMax(self): for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: maxVal, maxStep = self.stgFreq.getMax(headerName) #print "max for header '%s' calc to be %f at step %d"\ # % (headerName, maxVal, maxStep) calcMax = max(testList) calcIndex = testList.index(calcMax) self.assertAlmostEqual(maxVal, calcMax) self.assertEqual(maxStep, self.tSteps[calcIndex]) def test_getMean(self): meanVal = self.stgFreq.getMean('VRMS') calcMean = sum(self.VRMSVals, 0.0) / len(self.VRMSVals) self.assertAlmostEqual(meanVal, calcMean) def test_getReductionOps(self): #Test the MinOp for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: minVal, minStep = self.stgFreq.getReductionOp(headerName, stgfreq.minOp) calcMin = min(testList) calcIndex = testList.index(calcMin) self.assertAlmostEqual(minVal, calcMin) self.assertEqual(minStep, self.tSteps[calcIndex]) #Test the MaxOp for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: maxVal, maxStep = self.stgFreq.getReductionOp(headerName, stgfreq.maxOp) calcMax = max(testList) calcIndex = testList.index(calcMax) self.assertAlmostEqual(maxVal, calcMax) self.assertEqual(maxStep, self.tSteps[calcIndex]) #Test the last op for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: lastVal, lastStep = self.stgFreq.getReductionOp(headerName, stgfreq.lastOp) calcLast = testList[-1] calcIndex = len(testList)-1 self.assertAlmostEqual(lastVal, calcLast) self.assertEqual(lastStep, self.tSteps[calcIndex]) #Test the first op for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: firstVal, firstStep = self.stgFreq.getReductionOp(headerName, stgfreq.firstOp) calcFirst = testList[0] calcIndex = 0 self.assertAlmostEqual(firstVal, calcFirst) self.assertEqual(firstStep, self.tSteps[calcIndex]) #Test the closestToVal for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: for targIndex in range(len(self.tSteps)): targVal = testList[targIndex] + 0.000001 closestVal, closestStep = self.stgFreq.getReductionOp( headerName, stgfreq.closestToVal, targVal=targVal) self.assertAlmostEqual(closestVal, testList[targIndex]) self.assertEqual(closestStep, self.tSteps[targIndex]) #Test the closestToStep for headerName, testList in [('Time', self.timeVals), ('VRMS', self.VRMSVals)]: for targIndex in range(len(self.tSteps)): targStep = self.tSteps[targIndex] + 1 closestVal, closestStep = self.stgFreq.getReductionOp( headerName, stgfreq.closestToStep, targStep=targStep) self.assertAlmostEqual(closestVal, testList[targIndex]) self.assertEqual(closestStep, self.tSteps[targIndex]) #Test the closestToSimTime for headerName, testList in [('VRMS', self.VRMSVals)]: for targIndex in range(len(self.tSteps)): targTime = self.timeVals[targIndex] + 0.00002 closestVal, closestStep = self.stgFreq.getReductionOp( headerName, stgfreq.closestToSimTime, targTime=targTime) self.assertAlmostEqual(closestVal, testList[targIndex]) self.assertEqual(closestStep, self.tSteps[targIndex]) def test_plotOverTime(self): self.stgFreq.plotOverTime("Time", show=False, path="output/Plots") self.stgFreq.plotOverTime("VRMS", show=False, path="output/Plots") def test_printAllMinMax(self): self.stgFreq.printAllMinMax()