def test_01(self): """TestUnitsBasic.test_01(): 1e5 random units converted to and fro and tested to 10 Sig. Fig.""" myCats = Units.unitCategories() cCount = 0 random.seed() tS = time.clock() while cCount < 1e2: # Choose a category at random iC = random.randint(0, len(myCats) - 1) myUnits = Units.units(myCats[iC]) #print 'Category: %s' % myCats[iC] #print ' Units: %s' % ', '.join(myUnits) uCount = 0 while uCount < 1e3: iU_1 = random.randint(0, len(myUnits) - 1) iU_2 = random.randint(0, len(myUnits) - 1) val = random.random() newVal = Units.convert(val, myUnits[iU_1], myUnits[iU_2]) oldVal = Units.convert(newVal, myUnits[iU_2], myUnits[iU_1]) #print #print(oldVal) self.assertAlmostEqual(oldVal, val, places=10) #self.assertAlmostEqual(oldVal, val) uCount += 1 cCount += 1 tE = time.clock() - tS sys.stderr.write('Time: %8.3f rate %10.3f k/S ' % (tE, (cCount * uCount) / (1024 * tE)))
def test_03(self): """TestUnitsBasic.test_03(): 1e5 fixed units converted to and fro and tested to 10 Sig. Fig.""" myCats = Units.unitCategories() cCount = 0 random.seed() tS = time.perf_counter() while cCount < 1e2: # Choose a category at random iC = random.randint(0, len(myCats) - 1) myUnits = Units.units(myCats[iC]) iU_1 = random.randint(0, len(myUnits) - 1) iU_2 = random.randint(0, len(myUnits) - 1) val = random.random() uCount = 0 while uCount < 1e3: newVal = Units.convert(val, myUnits[iU_1], myUnits[iU_2]) self.assertAlmostEqual( Units.convert(newVal, myUnits[iU_2], myUnits[iU_1]), val, places=10, ) uCount += 1 cCount += 1 tE = time.perf_counter() - tS sys.stderr.write('Time: %8.3f rate %10.3f k/S ' % (tE, (cCount * uCount) / (1024 * tE)))
def newEngValInOpticalUnits(self): """Returns a new EngVal converting me to the 'optical' units if possible. For example a value in b'.1IN" will be converted to b'FEET'.""" myUnits = Units.opticalUnits(self.uom) if myUnits == self.uom: return EngVal(self.value, self.uom) return EngVal(Units.convert(self.value, self.uom, myUnits), myUnits)
def xAxisSpacingOptical(self): """The numerical value of the X axis frame spacing. This is is the extreme range of X axis values divided by the number of frames - 1.""" return Units.convert(self._rle.frameSpacing(), self._rle.xAxisUnits, Units.opticalUnits(self._rle.xAxisUnits) #self._dfsr.ebs.opticalLogScale )
def xAxisLastValOptical(self): """The numerical value of the X axis of the last frame in 'optical' units.""" return Units.convert( self._rle.xAxisLastFrame(), self._rle.xAxisUnits, Units.opticalUnits(self._rle.xAxisUnits) #self._dfsr.ebs.opticalLogScale )
def _genXPosText(self, xFrom, xInc, units): """Generates unbounded series of X text positions as (Dim(), value). xFrom - The starting value, positions may not include this if fractional. First x position generated will be math.ceil() if xInc, math.floor() otherwise. xInc - True if X increases. units - Units of X axis.""" for xVal in self._genXAxisText(xFrom, xInc, units): myDim = Coord.Dim(Units.convert(xVal - xFrom, units, b'INCH'), 'in') yield myDim.divide(self._scale), xVal
def test_04(self): """TestUnitsBasic.test_04(): 1e5 fixed units converted to and fro untested.""" myCats = Units.unitCategories() cCount = 0 random.seed() tS = time.perf_counter() while cCount < 1e1: # Choose a category at random iC = random.randint(0, len(myCats) - 1) myUnits = Units.units(myCats[iC]) iU_1 = random.randint(0, len(myUnits) - 1) iU_2 = random.randint(0, len(myUnits) - 1) val = random.random() uCount = 0 while uCount < 1e4: newVal = Units.convert(val, myUnits[iU_1], myUnits[iU_2]) uCount += 1 cCount += 1 tE = time.perf_counter() - tS sys.stderr.write('Time: %8.3f rate %10.3f k/S ' % (tE, (cCount * uCount) / (1024 * tE)))
def genXPosStroke(self, xFrom, xInc, units): """Generates unbounded series of X line positions as (Dim(), Stroke()). xFrom - The starting value as a float, positions may not include this if fractional. First x position generated will be math.ceil() if xInc, math.floor() otherwise. xInc - A boolean, True if X increases. units - Units of X axis e.g. b'FEET'.""" for xPos, stroke in self._genXAxisStroke(xFrom, xInc, units): myDim = Coord.Dim(Units.convert(xPos - xFrom, units, b'INCH'), 'in') yield myDim.divide(self._scale), stroke
def genXAxisRange(self, evFrom, evTo): """Generates a bounded series of X axis line plot positions as (Dim(), Stroke()). evFrom and evTo and EngVal objects.""" logging.info('XGrid.genXAxisRange(): evFrom={!s:s}, evTo={!s:s}'.format(evFrom, evTo)) evFrom = self._makeEngValOptical(evFrom) evTo = self._makeEngValOptical(evTo) if evFrom.uom != evTo.uom: evTo.convert(evFrom.uom) xInc = evTo > evFrom for xPos, stroke in self._genXAxisStroke(evFrom.value, xInc, evFrom.uom): if xInc and xPos > evTo \ or not xInc and xPos < evTo: break myDim = Coord.Dim(Units.convert(xPos - evFrom.value, evFrom.uom, b'INCH'), 'in') yield myDim.divide(self._scale), stroke
def test_01(self): """TestInternals(): __UNIT_MAP.""" #print #print Units.unitCategories() self.assertEqual( sorted(Units.unitCategories()), sorted([ b'VELO', b'CURR', b'ACCE', b'VISC', b'COND', b'ENER', b'TTIM', b'V/LE', b'DFRA', b'TEMP', b'HTRA', b'DENS', b'PERM', b'ATTE', b'MASS', b'ROTA', b'EGR ', b'T/L ', b'POWE', b'UNKN', b'LENG', b'DIME', b'FORC', b'ILEN', b'VOLU', b'RESI', b'C/T ', b'M/L ', b'PLEN', b'FREQ', b'IMAS', b'ERES', b'EPOT', b'RVEL', b'AREA', b'TIME', b'A/L ', b'PRES', ]), )
def _makeEngValOptical(self, theEv): """Converts an EngVal to 'optical' units e.g. b'.1IN' goes to b'FEET'.""" return theEv.newEngValInUnits(Units.opticalUnits(theEv.uom))
def xAxisUnitsOptical(self): """Returns the actual units to 'optical' i.e. user friendly units. For example if the Xaxis was in b'.1IN' the 'optical' units would be b'FEET".""" return Units.opticalUnits(self._rle.xAxisUnits)
def randomUnit(): """Returns a random unit mnemonic.""" return random.choice(Units.units(theCat=None))
def test_01(self): """TestUnitsBasic.test_01(): Convert metres to feet.""" self.assertEqual(Units.convert(1.0, b"M ", b"FEET"), 1.0 / 0.3048)
def convert(self, theUnits): """Convert my value to the supplied units in-place. May raise an ExceptionUnits.""" if theUnits != self.uom: self.value = Units.convert(self.value, self.uom, theUnits) self.uom = theUnits
def newEngValInUnits(self, theUnits): """Returns a new EngVal converting me to the supplied units. May raise an ExceptionUnits.""" if theUnits == self.uom: return EngVal(self.value, self.uom) return EngVal(Units.convert(self.value, self.uom, theUnits), theUnits)
def getInUnits(self, theUnits): """Returns my value to the supplied units. May raise an ExceptionUnits.""" if theUnits == self.uom: return self.value return Units.convert(self.value, self.uom, theUnits)