def testConstantRegion(self): """A constant region does not have any local extrema and NaN values should be returned.""" y = numpy.array([2,2,2],'f') result = EF.estimateExtremumPosition(y,self.x) self.assertEqual(result,(numpy.NaN,numpy.NaN,numpy.NaN)) result = EF.estimateExtremumPosition(y,self.x,sigmaY=self.zeros) self.assertEqual(result,(numpy.NaN,numpy.NaN,numpy.NaN))
def testExactGeneric(self): result = EF.estimateExtremumPosition(self.y,self.x) self.assertEqual(result,(2.5+1/1.5,numpy.NaN,1.0)) result = EF.estimateExtremumPosition(self.y,self.x,sigmaY=self.zeros) self.assertEqual(result,(2.5+1/1.5,0.0,1.0)) result = EF.estimateExtremumPosition(self.y,self.x,sigmaY=self.error) numpy.testing.assert_almost_equal(numpy.array(result), numpy.array((2.5+1/1.5,0.02/1.5**2,1.0)))
def testSymmetricMaximum(self): y = numpy.array([2,2,1],'f') result = EF.estimateExtremumPosition(y,self.x) self.assertEqual(result,(2.5,numpy.NaN,-1.0)) result = EF.estimateExtremumPosition(y,self.x,sigmaY=self.zeros) self.assertEqual(result,(2.5,0.0,-1.0)) result = EF.estimateExtremumPosition(y,self.x,sigmaY=self.error) numpy.testing.assert_almost_equal(numpy.array(result), numpy.array((2.5,0.02,-1.0)))
def testExactMaximum(self): y = numpy.array([2,3,2],'f') result = EF.estimateExtremumPosition(y,self.x) self.assertEqual(result,(3.0,numpy.NaN,-1.0)) result = EF.estimateExtremumPosition(y,self.x,sigmaY=self.zeros) self.assertEqual(result,(3.0,0.0,-1.0)) result = EF.estimateExtremumPosition(y,self.x,sigmaY=self.error) numpy.testing.assert_almost_equal(numpy.array(result), numpy.array((3.0,0.005,-1.0)))
def testParabelArray(self): x = numpy.array([-2,-1,0,1,2],'float') field = numpy.array([(x-0.2)**2-0.5,(x+0.1)**2]) inputField = DC.FieldContainer(field, error=numpy.resize(numpy.repeat(0.1,len(field)),field.shape), dimensions=[DC.FieldContainer(numpy.array([1,2]), longname='type',shortname=r'\theta'), DC.FieldContainer(x,longname='position',shortname='x')], longname='parabel', shortname='f') def error(y): error = inputField.error[0,0] / (y[1]-2*y[2]+y[3])**2 error *= numpy.abs(y[2]-y[3]) + numpy.abs(y[1]-y[3]) + numpy.abs(y[1]-y[2]) return error expectedResult = DC.FieldContainer(numpy.array([[0.2,-0.1]]), longname = 'position of the local minima of parabel', shortname = 'x_0', error = numpy.array([[error(field[0]),error(field[1])]])) expectedResult.dimensions[-1] = DC.FieldContainer(numpy.array([1,2]), longname='type', shortname=r'\theta') w = EF.ExtremumFinder(None) w.paramExtremum.value=u'minima' #Retrieve result from worker result = w.locate(inputField) DC.assertEqual(result,expectedResult)
def testParabel(self): x = numpy.array([-2, -1, 0, 1, 2], 'float') + 0.1 y = x**2 inputField = DC.FieldContainer(y, error=numpy.repeat(0.1, len(y)), dimensions=[ DC.FieldContainer( x, longname='abscissae', shortname='x') ], longname='parabel', shortname='f') error = inputField.error[slice(0, 1)] / (y[1] - 2 * y[2] + y[3])**2 error *= numpy.abs(y[2] - y[3]) + numpy.abs(y[1] - y[3]) + numpy.abs(y[1] - y[2]) expectedResult = DC.FieldContainer( numpy.array([0.0]), longname='position of the local minimum of parabel', shortname='x_0', error=error) w = EF.ExtremumFinder(None) w.paramExtremum.value = u'minima' #Retrieve result from worker result = w.locate(inputField) DC.assertEqual(result, expectedResult)
def testMinima(self): """Test the correct computation of all local minima for a bistable potential.""" #Predict result x0,curv,mask = fixedPoints(numpy.array([self.LAMBDA]),kappa1=self.kappa1) expectedResult = DC.FieldContainer(numpy.extract(curv[0]>0,x0[0]), unit = self.xField.unit, longname = 'position of the local minima of electric potential', shortname = 'x_0') #Retrieve result from worker w = EF.ExtremumFinder(None) w.paramExtremum.value=u'minima' result = w.locate(self.V) #Testing DC.assertEqual(result,expectedResult)
def testParabelSymmetricallyBoxedMinimum(self): x = numpy.array([-2,-1,0,1,2],'float')-0.5 y = x**2 inputField = DC.FieldContainer(y, error=numpy.repeat(0.1,len(y)), dimensions=[DC.FieldContainer(x,longname='abscissae',shortname='x')], longname='parabel', shortname='f' ) expectedResult = DC.FieldContainer(numpy.array([0.0]), longname = 'position of the local minimum of parabel', shortname = 'x_0', error = numpy.array([0.1]) ) w = EF.ExtremumFinder(None) w.paramExtremum.value=u'minima' #Retrieve result from worker result = w.locate(inputField) DC.assertEqual(result,expectedResult)
def testRoots(self): """Test the correct computation of all local extrema for a bistable potential.""" #Prepare dimensions self.prepareDimensions() lambField = DC.FieldContainer(self.lambDim, unit='1 V / m**3', longname='parameter', shortname='\lambda') xField = DC.FieldContainer(self.xDim[0], unit='1 m', longname='position', shortname='x') #Prepare potential V = [] for i in xrange(len(lambField.data)): u = xField.data V.append(-lambField.data[i] / 2 * u**2 + u**4 / 4 - u * self.kappa1) self.V = DC.FieldContainer(numpy.array(V), unit='1 V', dimensions=[lambField, xField], longname='electric potential', shortname=r'\varphi') #Predict result x0, curv, mask = fixedPoints(lambField.data, kappa1=self.kappa1) error = 1.0 / curv.transpose() error[:] = numpy.nan data = x0.transpose() expectedResult = DC.FieldContainer( data, unit=xField.unit, mask=numpy.isnan(data), dimensions=[DC.generateIndex(0, 3), lambField], longname='position of the local extrema of electric potential', shortname='x_0') #Configure worker w = EF.ExtremumFinder(None) w.paramExtremum.value = u'both' #Retrieve result from worker result = w.locate(self.V) self.test(result, expectedResult)